fix: keep codex stdio server alive across turns

This commit is contained in:
Codex
2026-06-02 01:35:31 +08:00
parent 4ddea73629
commit 1ff2f114b3
5 changed files with 171 additions and 46 deletions
+21 -2
View File
@@ -1,5 +1,5 @@
import type { BackendTurnResult, CommandRecord, RunRecord } from "../common/types.js";
import { runCodexStdioTurn, type CodexStdioTurnOptions } from "./codex-stdio.js";
import { CodexStdioBackendSession, runCodexStdioTurn, type CodexStdioTurnOptions } from "./codex-stdio.js";
import { backendProfileSpec } from "../common/backend-profiles.js";
export interface BackendAdapterOptions {
@@ -11,11 +11,30 @@ export interface BackendAdapterOptions {
env?: NodeJS.ProcessEnv;
}
export interface BackendSession {
runTurn(run: RunRecord, command: CommandRecord, options?: BackendAdapterOptions): Promise<BackendTurnResult>;
close(): Promise<BackendTurnResult["events"]>;
}
export async function runBackendTurn(run: RunRecord, command: CommandRecord, options: BackendAdapterOptions = {}): Promise<BackendTurnResult> {
const spec = backendProfileSpec(run.backendProfile);
if (!spec || spec.backendKind !== "codex-app-server-stdio") {
return { terminalStatus: "failed", failureKind: "backend-failed", failureMessage: `unsupported backendProfile ${run.backendProfile}`, events: [{ type: "error", payload: { failureKind: "backend-failed", backendProfile: run.backendProfile } }] };
}
return runCodexStdioTurn(backendTurnOptions(run, command, options));
}
export function createBackendSession(run: RunRecord, options: BackendAdapterOptions = {}): BackendSession | null {
const spec = backendProfileSpec(run.backendProfile);
if (!spec || spec.backendKind !== "codex-app-server-stdio") return null;
const session = new CodexStdioBackendSession();
return {
runTurn: async (nextRun, command, turnOptions = {}) => session.runTurn(backendTurnOptions(nextRun, command, { ...options, ...turnOptions })),
close: async () => session.close(),
};
}
export function backendTurnOptions(run: RunRecord, command: CommandRecord, options: BackendAdapterOptions = {}): CodexStdioTurnOptions {
const prompt = typeof command.payload.prompt === "string" ? command.payload.prompt : JSON.stringify(command.payload);
const turnOptions: CodexStdioTurnOptions = {
backendProfile: run.backendProfile,
@@ -33,5 +52,5 @@ export async function runBackendTurn(run: RunRecord, command: CommandRecord, opt
if (options.env) turnOptions.env = options.env;
if (options.codexHome) turnOptions.codexHome = options.codexHome;
if (options.abortSignal) turnOptions.abortSignal = options.abortSignal;
return runCodexStdioTurn(turnOptions);
return turnOptions;
}