import type { BackendTurnResult, CommandRecord, RunRecord } from "../common/types.js"; import { runCodexStdioTurn, type CodexStdioTurnOptions } from "./codex-stdio.js"; export interface BackendAdapterOptions { codexCommand?: string; codexArgs?: string[]; codexHome?: string; env?: NodeJS.ProcessEnv; } export async function runBackendTurn(run: RunRecord, command: CommandRecord, options: BackendAdapterOptions = {}): Promise { if (run.backendProfile !== "codex") { return { terminalStatus: "failed", failureKind: "backend-failed", failureMessage: `unsupported backendProfile ${run.backendProfile}`, events: [{ type: "error", payload: { failureKind: "backend-failed", backendProfile: run.backendProfile } }] }; } const prompt = typeof command.payload.prompt === "string" ? command.payload.prompt : JSON.stringify(command.payload); const turnOptions: CodexStdioTurnOptions = { prompt, cwd: typeof run.workspaceRef.path === "string" ? run.workspaceRef.path : process.cwd(), approvalPolicy: run.executionPolicy.approval, sandbox: run.executionPolicy.sandbox, timeoutMs: run.executionPolicy.timeoutMs, }; if (typeof command.payload.model === "string") turnOptions.model = command.payload.model; if (typeof command.payload.threadId === "string") turnOptions.threadId = command.payload.threadId; if (options.codexCommand) turnOptions.command = options.codexCommand; if (options.codexArgs) turnOptions.args = options.codexArgs; if (options.env) turnOptions.env = options.env; if (options.codexHome) turnOptions.codexHome = options.codexHome; return runCodexStdioTurn(turnOptions); }