fix: 实时上报 Codex 工具事件
This commit is contained in:
+30
-16
@@ -47,6 +47,7 @@ export interface CodexStdioTurnOptions {
|
||||
env?: NodeJS.ProcessEnv;
|
||||
codexHome?: string;
|
||||
abortSignal?: AbortSignal;
|
||||
onEvent?: (event: BackendEvent) => void | Promise<void>;
|
||||
}
|
||||
|
||||
interface PendingRequest {
|
||||
@@ -302,15 +303,15 @@ export class CodexStdioBackendSession {
|
||||
return [{ type: "backend_status", payload: { phase: "codex-app-server-closed", appServerExit: closeEvent(closeInfo) } }];
|
||||
}
|
||||
|
||||
async getClient(options: CodexStdioTurnOptions, env: NodeJS.ProcessEnv, events: BackendEvent[]): Promise<CodexStdioClient> {
|
||||
async getClient(options: CodexStdioTurnOptions, env: NodeJS.ProcessEnv, emitEvent: (event: BackendEvent) => void): Promise<CodexStdioClient> {
|
||||
const key = codexClientKey(options, env);
|
||||
if (this.client && !this.client.isClosed && this.clientKey === key) {
|
||||
events.push({ type: "backend_status", payload: { phase: "codex-app-server:reused", ...backendMetadata(options), protocol: codexProtocol } });
|
||||
emitEvent({ type: "backend_status", payload: { phase: "codex-app-server:reused", ...backendMetadata(options), protocol: codexProtocol } });
|
||||
return this.client;
|
||||
}
|
||||
const closeEvents = await this.close();
|
||||
events.push(...closeEvents);
|
||||
events.push({
|
||||
for (const event of closeEvents) emitEvent(event);
|
||||
emitEvent({
|
||||
type: "backend_status",
|
||||
payload: {
|
||||
phase: "codex-app-server-starting",
|
||||
@@ -332,7 +333,7 @@ export class CodexStdioBackendSession {
|
||||
const initializeResult = requireResponseRecord(await this.client.request("initialize", { clientInfo: { name: "agentrun", title: "AgentRun", version: "0.1.0" }, capabilities: { experimentalApi: true } }, requestTimeoutMs), "initialize");
|
||||
validateInitializeResponse(initializeResult);
|
||||
this.client.notify("initialized", {});
|
||||
events.push({ type: "backend_status", payload: { phase: "initialize:completed", ...backendMetadata(options), protocol: codexProtocol } });
|
||||
emitEvent({ type: "backend_status", payload: { phase: "initialize:completed", ...backendMetadata(options), protocol: codexProtocol } });
|
||||
return this.client;
|
||||
}
|
||||
|
||||
@@ -356,6 +357,18 @@ async function runCodexStdioTurnWithSession(options: CodexStdioTurnOptions, sess
|
||||
if (secretFailure) return secretFailure;
|
||||
const env = childEnv(options, codexHome);
|
||||
const events: BackendEvent[] = [];
|
||||
let liveEventWrite = Promise.resolve();
|
||||
const emitEvent = (event: BackendEvent): void => {
|
||||
const redactedEvent: BackendEvent = { ...event, payload: redactJson(event.payload) };
|
||||
if (options.onEvent) {
|
||||
liveEventWrite = liveEventWrite.then(() => Promise.resolve(options.onEvent?.(redactedEvent))).catch(() => undefined);
|
||||
return;
|
||||
}
|
||||
events.push(redactedEvent);
|
||||
};
|
||||
const emitEvents = (nextEvents: BackendEvent[]): void => {
|
||||
for (const event of nextEvents) emitEvent(event);
|
||||
};
|
||||
if (options.abortSignal?.aborted) {
|
||||
const cancelled = { status: "cancelled" as const, failureKind: "cancelled" as const, message: "cancel requested" };
|
||||
events.push({ type: "backend_status", payload: { phase: "turn-cancelled", failureKind: "cancelled" } });
|
||||
@@ -374,7 +387,7 @@ async function runCodexStdioTurnWithSession(options: CodexStdioTurnOptions, sess
|
||||
const abortTurn = (): void => {
|
||||
if (terminal) return;
|
||||
terminal = { status: "cancelled", failureKind: "cancelled", message: "cancel requested" };
|
||||
events.push({ type: "backend_status", payload: { phase: "turn-cancelled", failureKind: "cancelled" } });
|
||||
emitEvent({ type: "backend_status", payload: { phase: "turn-cancelled", failureKind: "cancelled" } });
|
||||
client?.stop();
|
||||
terminalResolve();
|
||||
};
|
||||
@@ -382,7 +395,7 @@ async function runCodexStdioTurnWithSession(options: CodexStdioTurnOptions, sess
|
||||
const timeout = setTimeout(() => {
|
||||
if (terminal) return;
|
||||
terminal = { status: "failed", failureKind: "backend-timeout", message: `codex stdio turn timed out after ${options.timeoutMs}ms` };
|
||||
events.push({ type: "error", payload: { failureKind: terminal.failureKind, message: terminal.message, phase: "turn:timeout" } });
|
||||
emitEvent({ type: "error", payload: { failureKind: terminal.failureKind, message: terminal.message, phase: "turn:timeout" } });
|
||||
client?.stop();
|
||||
terminalResolve();
|
||||
}, positiveTimeout(options.timeoutMs));
|
||||
@@ -392,19 +405,19 @@ async function runCodexStdioTurnWithSession(options: CodexStdioTurnOptions, sess
|
||||
if (normalized.turnId) turnId = normalized.turnId;
|
||||
if (normalized.assistantDelta) assistantText += normalized.assistantDelta;
|
||||
if (typeof normalized.assistantFinal === "string" && normalized.assistantFinal.trim().length > 0) finalAssistantText = normalized.assistantFinal;
|
||||
events.push(...normalized.events);
|
||||
emitEvents(normalized.events);
|
||||
if (normalized.terminal && !terminal) {
|
||||
terminal = normalized.terminal;
|
||||
terminalResolve();
|
||||
}
|
||||
});
|
||||
try {
|
||||
client = await session.getClient(options, env, events);
|
||||
client = await session.getClient(options, env, emitEvent);
|
||||
|
||||
const startThread = async (phasePrefix = "thread/start"): Promise<string> => {
|
||||
const response = requireResponseRecord(await client!.request("thread/start", withOptionalModel({ cwd: options.cwd, approvalPolicy: options.approvalPolicy, sandbox: options.sandbox, serviceName: "agentrun" }, options.model), requestTimeoutMs), "thread/start");
|
||||
const nextThreadId = requireNestedId(response, "thread/start", "thread");
|
||||
events.push({ type: "backend_status", payload: { phase: `${phasePrefix}:completed`, threadId: nextThreadId } });
|
||||
emitEvent({ type: "backend_status", payload: { phase: `${phasePrefix}:completed`, threadId: nextThreadId } });
|
||||
return nextThreadId;
|
||||
};
|
||||
|
||||
@@ -412,11 +425,11 @@ async function runCodexStdioTurnWithSession(options: CodexStdioTurnOptions, sess
|
||||
try {
|
||||
const threadResponse = requireResponseRecord(await client.request("thread/resume", withOptionalModel({ threadId: options.threadId, cwd: options.cwd, approvalPolicy: options.approvalPolicy, sandbox: options.sandbox }, options.model), requestTimeoutMs), "thread/resume");
|
||||
threadId = requireNestedId(threadResponse, "thread/resume", "thread");
|
||||
events.push({ type: "backend_status", payload: { phase: "thread/resume:completed", threadId } });
|
||||
emitEvent({ type: "backend_status", payload: { phase: "thread/resume:completed", threadId } });
|
||||
} catch (error) {
|
||||
const failure = normalizeFailure(error);
|
||||
if (!isStaleThreadResumeFailure(failure)) throw error;
|
||||
events.push({
|
||||
emitEvent({
|
||||
type: "backend_status",
|
||||
payload: {
|
||||
phase: "thread/resume:stale-thread-fallback",
|
||||
@@ -434,7 +447,7 @@ async function runCodexStdioTurnWithSession(options: CodexStdioTurnOptions, sess
|
||||
|
||||
const turnResponse = requireResponseRecord(await client.request("turn/start", withOptionalModel({ threadId, input: textInput(options.prompt), cwd: options.cwd, approvalPolicy: options.approvalPolicy }, options.model), requestTimeoutMs), "turn/start");
|
||||
turnId = requireNestedId(turnResponse, "turn/start", "turn");
|
||||
events.push({ type: "backend_status", payload: { phase: "turn/start:completed", turnId } });
|
||||
emitEvent({ type: "backend_status", payload: { phase: "turn/start:completed", turnId } });
|
||||
|
||||
const race = await Promise.race([
|
||||
terminalPromise.then(() => ({ kind: "terminal" as const })),
|
||||
@@ -442,14 +455,14 @@ async function runCodexStdioTurnWithSession(options: CodexStdioTurnOptions, sess
|
||||
]);
|
||||
if (race.kind === "closed" && !terminal) {
|
||||
terminal = terminalFromClose(race.closeInfo);
|
||||
events.push({ type: "error", payload: { failureKind: terminal.failureKind, message: terminal.message, phase: "transport:closed-before-terminal", appServerExit: closeEvent(race.closeInfo) } });
|
||||
emitEvent({ type: "error", payload: { failureKind: terminal.failureKind, message: terminal.message, phase: "transport:closed-before-terminal", appServerExit: closeEvent(race.closeInfo) } });
|
||||
}
|
||||
if (!terminal) terminal = { status: "failed", failureKind: "backend-response-invalid", message: "codex app-server did not emit turn/completed" };
|
||||
} catch (error) {
|
||||
if (!terminal) {
|
||||
const failure = normalizeFailure(error);
|
||||
terminal = { status: failure.failureKind === "secret-unavailable" ? "blocked" : "failed", failureKind: failure.failureKind, message: failure.message };
|
||||
events.push({ type: "error", payload: { failureKind: failure.failureKind, message: failure.message, phase: failure.phase, details: failure.details } });
|
||||
emitEvent({ type: "error", payload: { failureKind: failure.failureKind, message: failure.message, phase: failure.phase, details: failure.details } });
|
||||
}
|
||||
} finally {
|
||||
stopNotifications();
|
||||
@@ -457,10 +470,11 @@ async function runCodexStdioTurnWithSession(options: CodexStdioTurnOptions, sess
|
||||
clearTimeout(timeout);
|
||||
}
|
||||
if (!terminal) terminal = { status: "failed", failureKind: "backend-response-invalid", message: "codex app-server finished without terminal status" };
|
||||
if (terminal.status !== "completed") events.push(...await session.close());
|
||||
if (terminal.status !== "completed") emitEvents(await session.close());
|
||||
const reply = finalAssistantText.trim().length > 0 ? finalAssistantText : assistantText;
|
||||
if (reply.trim().length > 0) events.push({ type: "assistant_message", payload: { text: reply } });
|
||||
events.push({ type: "terminal_status", payload: { terminalStatus: terminal.status, failureKind: terminal.failureKind, message: terminal.message } });
|
||||
await liveEventWrite;
|
||||
return { terminalStatus: terminal.status, failureKind: terminal.failureKind, failureMessage: terminal.message, events: events.map((event) => ({ ...event, payload: redactJson(event.payload) })), ...(threadId ? { threadId } : {}), ...(turnId ? { turnId } : {}) };
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user