import type { UniDeskConfig } from "../config"; import type { RenderedCliResult } from "../output"; import { desiredAccountNames } from "./accounts"; import { readCodexPoolConfig } from "./config"; import { protectedManualAccountsForTarget } from "./public-exposure"; import { renderSub2ApiTempUnschedulableCredentials } from "./redaction"; import { boolField, compactCapture, parseJsonOutput, runRemoteCodexPoolScript } from "./remote"; import { compactRuntimeErrors, renderRuntimeResult } from "./runtime-render"; import { runtimeScript } from "./runtime-remote-script"; import { parseRuntimeOptions, type RuntimeOptions } from "./runtime-options"; import { renderRuntimeMutationResult } from "./runtime-mutation-render"; import { codexPoolRuntimeTarget } from "./runtime-target"; export async function codexPoolRuntime(config: UniDeskConfig, args: string[]): Promise | RenderedCliResult> { const options = parseRuntimeOptions(args); const pool = readCodexPoolConfig(); const target = codexPoolRuntimeTarget(options.targetId); const selectedTemplate = options.kind !== "temp-unschedulable" || options.template === null ? null : pool.runtime.templatesById[options.template]; if (options.template !== null && selectedTemplate === undefined) { throw new Error(`unknown runtime template ${options.template}; available: ${pool.runtime.templates.map((item) => item.id).join(", ")}`); } const payload = { action: options.action, account: options.account, accounts: options.accounts, group: options.group, allGroups: options.allGroups, platform: options.platform, pageToken: options.pageToken, kind: options.kind, priority: options.priority, name: options.name, confirm: options.confirm, full: options.full, since: options.since, tail: options.tail, groupName: pool.groupName, adminEmailDefault: pool.adminEmailDefault, managedAccountNames: desiredAccountNames(pool), protectedManualAccountNames: protectedManualAccountsForTarget(pool, target).map((item) => item.accountName), templates: pool.runtime.templates.map((template) => ({ id: template.id, kind: template.kind, description: template.description, credentials: renderSub2ApiTempUnschedulableCredentials(template.tempUnschedulable), })), selectedTemplate: selectedTemplate === null || selectedTemplate === undefined ? null : { id: selectedTemplate.id, kind: selectedTemplate.kind, credentials: renderSub2ApiTempUnschedulableCredentials(selectedTemplate.tempUnschedulable), }, }; const result = await runRemoteCodexPoolScript(config, "runtime", runtimeScript(payload, target), target); const parsed = parseJsonOutput(result.stdout); const ok = result.exitCode === 0 && boolField(parsed, "ok", false); if (!options.full && !options.raw && !options.json && (options.action === "apply" || options.action === "delete")) { return renderRuntimeMutationResult(ok, options, parsed, result.exitCode); } if (options.raw) { return { ok, action: "platform-infra-sub2api-codex-pool-runtime", remote: compactCapture(result, { full: parsed === null || result.exitCode !== 0 }), parsed, valuesPrinted: false, }; } const response = { ok, action: "platform-infra-sub2api-codex-pool-runtime", target: { id: target.id, route: target.route, namespace: target.namespace, runtimeMode: target.runtimeMode, endpoint: target.serviceDns, }, request: { operation: options.action, account: options.account, accounts: options.accounts, group: options.group, allGroups: options.allGroups, platform: options.platform, pageToken: options.pageToken, template: options.template, kind: options.kind, priority: options.priority, name: options.name, confirm: options.confirm, since: options.action === "errors" || options.action === "infrastructure" ? options.since : undefined, tail: options.action === "errors" || options.action === "infrastructure" ? options.tail : undefined, }, runtime: compactRuntimeErrors(parsed, options), remote: compactCapture(result, { full: result.exitCode !== 0 || parsed === null }), disclosure: options.action === "list" ? { get: "bun scripts/cli.ts platform-infra sub2api codex-pool runtime get --account ", errors: "bun scripts/cli.ts platform-infra sub2api codex-pool runtime errors [--account ] [--since 24h]", } : undefined, valuesPrinted: false, }; return options.full || options.json ? response : renderRuntimeResult(response, options); }