fix: 只用最终 agentMessage 生成 reply
This commit is contained in:
@@ -282,6 +282,7 @@ export async function runCodexStdioTurn(options: CodexStdioTurnOptions): Promise
|
||||
return { terminalStatus: cancelled.status, failureKind: cancelled.failureKind, failureMessage: cancelled.message, events: events.map((event) => ({ ...event, payload: redactJson(event.payload) })) };
|
||||
}
|
||||
let assistantText = "";
|
||||
let finalAssistantText = "";
|
||||
let threadId: string | undefined = options.threadId;
|
||||
let turnId: string | undefined;
|
||||
let terminal: { status: TerminalStatus; failureKind: FailureKind | null; message: string | null } | null = null;
|
||||
@@ -313,6 +314,7 @@ export async function runCodexStdioTurn(options: CodexStdioTurnOptions): Promise
|
||||
if (normalized.threadId) threadId = normalized.threadId;
|
||||
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);
|
||||
if (normalized.terminal && !terminal) {
|
||||
terminal = normalized.terminal;
|
||||
@@ -365,7 +367,8 @@ export async function runCodexStdioTurn(options: CodexStdioTurnOptions): Promise
|
||||
}
|
||||
}
|
||||
if (!terminal) terminal = { status: "failed", failureKind: "backend-response-invalid", message: "codex app-server finished without terminal status" };
|
||||
if (assistantText.trim().length > 0) events.push({ type: "assistant_message", payload: { text: assistantText } });
|
||||
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 } });
|
||||
return { terminalStatus: terminal.status, failureKind: terminal.failureKind, failureMessage: terminal.message, events: events.map((event) => ({ ...event, payload: redactJson(event.payload) })), ...(threadId ? { threadId } : {}), ...(turnId ? { turnId } : {}) };
|
||||
}
|
||||
@@ -426,7 +429,7 @@ function codexHomeReadiness(codexHome: string): BackendTurnResult | null {
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeCodexNotification(message: JsonRecord): { events: BackendEvent[]; assistantDelta?: string; threadId?: string; turnId?: string; terminal?: { status: TerminalStatus; failureKind: FailureKind | null; message: string | null } } {
|
||||
function normalizeCodexNotification(message: JsonRecord): { events: BackendEvent[]; assistantDelta?: string; assistantFinal?: string; threadId?: string; turnId?: string; terminal?: { status: TerminalStatus; failureKind: FailureKind | null; message: string | null } } {
|
||||
const method = typeof message.method === "string" ? message.method : "unknown";
|
||||
const params = asRecordAt(message, "params");
|
||||
if (method === "thread/started") {
|
||||
@@ -439,6 +442,15 @@ function normalizeCodexNotification(message: JsonRecord): { events: BackendEvent
|
||||
}
|
||||
if (method === "item/agentMessage/delta") return { events: [], assistantDelta: typeof params.delta === "string" ? params.delta : "" };
|
||||
if (method === "item/commandExecution/outputDelta") return { events: [{ type: "command_output", payload: commandOutputPayload("stdout", typeof params.delta === "string" ? params.delta : "") }] };
|
||||
if ((method === "item/started" || method === "item/completed") && asRecordAt(params, "item").type === "agentMessage") {
|
||||
const item = asRecordAt(params, "item");
|
||||
const itemId = stringAt(item, "id") ?? stringAt(params, "itemId");
|
||||
const text = method === "item/completed" ? agentMessageText(item) : "";
|
||||
return {
|
||||
events: [{ type: "backend_status", payload: { phase: method === "item/completed" ? "item/agentMessage:completed" : "item/agentMessage:started", itemId, textBytes: Buffer.byteLength(text, "utf8") } }],
|
||||
...(text.trim().length > 0 ? { assistantFinal: text } : {}),
|
||||
};
|
||||
}
|
||||
if (method === "item/started" || method === "item/completed") return { events: [{ type: "tool_call", payload: toolCallPayload(method, asRecordAt(params, "item")) }] };
|
||||
if (method === "error") {
|
||||
const error = asRecordAt(params, "error");
|
||||
@@ -518,6 +530,25 @@ function textInput(text: string): JsonValue[] {
|
||||
return [{ type: "text", text, text_elements: [] }];
|
||||
}
|
||||
|
||||
function agentMessageText(item: JsonRecord): string {
|
||||
for (const key of ["text", "content", "message"]) {
|
||||
const value = item[key];
|
||||
if (typeof value === "string") return value;
|
||||
}
|
||||
for (const key of ["text_elements", "content"]) {
|
||||
const value = item[key];
|
||||
if (!Array.isArray(value)) continue;
|
||||
const parts = value.flatMap((entry) => {
|
||||
if (typeof entry === "string") return [entry];
|
||||
if (typeof entry !== "object" || entry === null || Array.isArray(entry)) return [];
|
||||
const record = entry as JsonRecord;
|
||||
return typeof record.text === "string" ? [record.text] : [];
|
||||
});
|
||||
if (parts.length > 0) return parts.join("");
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
function fileReadable(filePath: string): JsonRecord {
|
||||
try {
|
||||
accessSync(filePath, fsConstants.R_OK);
|
||||
|
||||
Reference in New Issue
Block a user