fix: 收敛 codex stdio trace 噪声 (#66)
Co-authored-by: Codex <codex@pikas.tech>
This commit is contained in:
@@ -62,6 +62,12 @@ interface CompletedAssistantMessage {
|
||||
text: string;
|
||||
}
|
||||
|
||||
interface SuppressedNotificationSummary {
|
||||
total: number;
|
||||
byMethod: Record<string, number>;
|
||||
byItemType: Record<string, number>;
|
||||
}
|
||||
|
||||
interface CodexStdioCloseInfo extends JsonRecord {
|
||||
code: number | null;
|
||||
signal: string | null;
|
||||
@@ -382,6 +388,7 @@ async function runCodexStdioTurnWithSession(options: CodexStdioTurnOptions, sess
|
||||
}
|
||||
let assistantText = "";
|
||||
const completedAssistantMessages: CompletedAssistantMessage[] = [];
|
||||
const suppressedNotifications = createSuppressedNotificationSummary();
|
||||
let threadId: string | undefined = options.threadId;
|
||||
let turnId: string | undefined;
|
||||
let terminal: { status: TerminalStatus; failureKind: FailureKind | null; message: string | null } | null = null;
|
||||
@@ -405,7 +412,7 @@ async function runCodexStdioTurnWithSession(options: CodexStdioTurnOptions, sess
|
||||
terminalResolve();
|
||||
}, positiveTimeout(options.timeoutMs));
|
||||
const stopNotifications = session.addNotificationHandler((message) => {
|
||||
const normalized = normalizeCodexNotification(message);
|
||||
const normalized = normalizeCodexNotification(message, suppressedNotifications);
|
||||
if (normalized.threadId) threadId = normalized.threadId;
|
||||
if (normalized.turnId) turnId = normalized.turnId;
|
||||
emitEvents(normalized.events);
|
||||
@@ -480,6 +487,7 @@ async function runCodexStdioTurnWithSession(options: CodexStdioTurnOptions, sess
|
||||
if (!terminal) terminal = { status: "failed", failureKind: "backend-response-invalid", message: "codex app-server finished without terminal status" };
|
||||
if (terminal.status !== "completed") emitEvents(await session.close());
|
||||
if (completedAssistantMessages.length === 0) emitEvents(assistantMessageEventsForTurn(assistantText, terminal.status === "completed"));
|
||||
emitEvents(suppressedNotificationEvents(suppressedNotifications));
|
||||
emitEvent({ 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 } : {}) };
|
||||
@@ -541,7 +549,7 @@ function codexHomeReadiness(codexHome: string): BackendTurnResult | null {
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeCodexNotification(message: JsonRecord): { events: BackendEvent[]; assistantDelta?: string; completedAssistantMessage?: CompletedAssistantMessage; threadId?: string; turnId?: string; terminal?: { status: TerminalStatus; failureKind: FailureKind | null; message: string | null } } {
|
||||
function normalizeCodexNotification(message: JsonRecord, suppressed: SuppressedNotificationSummary): { events: BackendEvent[]; assistantDelta?: string; completedAssistantMessage?: CompletedAssistantMessage; 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") {
|
||||
@@ -552,8 +560,16 @@ function normalizeCodexNotification(message: JsonRecord): { events: BackendEvent
|
||||
const turnId = stringAt(asRecordAt(params, "turn"), "id");
|
||||
return { events: [{ type: "backend_status", payload: { phase: method, turnId } }], ...(turnId ? { turnId } : {}) };
|
||||
}
|
||||
if (isSuppressedCodexStatusNotification(method)) {
|
||||
recordSuppressedNotification(suppressed, method);
|
||||
return { events: [] };
|
||||
}
|
||||
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/reasoning/textDelta") {
|
||||
recordSuppressedNotification(suppressed, method, "reasoning");
|
||||
return { events: [] };
|
||||
}
|
||||
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");
|
||||
@@ -564,7 +580,15 @@ function normalizeCodexNotification(message: JsonRecord): { events: BackendEvent
|
||||
...(completedAssistantMessage ? { completedAssistantMessage } : {}),
|
||||
};
|
||||
}
|
||||
if (method === "item/started" || method === "item/completed") return { events: [{ type: "tool_call", payload: toolCallPayload(method, asRecordAt(params, "item")) }] };
|
||||
if (method === "item/started" || method === "item/completed") {
|
||||
const item = asRecordAt(params, "item");
|
||||
const itemType = typeof item.type === "string" ? item.type : "unknown";
|
||||
if (isSuppressedCodexItemType(itemType)) {
|
||||
recordSuppressedNotification(suppressed, method, itemType);
|
||||
return { events: [] };
|
||||
}
|
||||
return { events: [{ type: "tool_call", payload: toolCallPayload(method, item) }] };
|
||||
}
|
||||
if (method === "error") {
|
||||
const error = asRecordAt(params, "error");
|
||||
const messageText = typeof error.message === "string" ? error.message : "Codex app-server error";
|
||||
@@ -588,6 +612,42 @@ function normalizeCodexNotification(message: JsonRecord): { events: BackendEvent
|
||||
return { events: [{ type: "backend_status", payload: { phase: method } }] };
|
||||
}
|
||||
|
||||
function createSuppressedNotificationSummary(): SuppressedNotificationSummary {
|
||||
return { total: 0, byMethod: {}, byItemType: {} };
|
||||
}
|
||||
|
||||
function recordSuppressedNotification(summary: SuppressedNotificationSummary, method: string, itemType?: string): void {
|
||||
summary.total += 1;
|
||||
summary.byMethod[method] = (summary.byMethod[method] ?? 0) + 1;
|
||||
if (itemType) summary.byItemType[itemType] = (summary.byItemType[itemType] ?? 0) + 1;
|
||||
}
|
||||
|
||||
function suppressedNotificationEvents(summary: SuppressedNotificationSummary): BackendEvent[] {
|
||||
if (summary.total === 0) return [];
|
||||
return [{
|
||||
type: "backend_status",
|
||||
payload: {
|
||||
phase: "codex-app-server-notifications-suppressed",
|
||||
total: summary.total,
|
||||
byMethod: sortCountRecord(summary.byMethod),
|
||||
byItemType: sortCountRecord(summary.byItemType),
|
||||
valuesPrinted: false,
|
||||
},
|
||||
}];
|
||||
}
|
||||
|
||||
function sortCountRecord(input: Record<string, number>): JsonRecord {
|
||||
return Object.fromEntries(Object.entries(input).sort(([left], [right]) => left.localeCompare(right))) as JsonRecord;
|
||||
}
|
||||
|
||||
function isSuppressedCodexStatusNotification(method: string): boolean {
|
||||
return method === "thread/tokenUsage/updated" || method === "account/rateLimits/updated" || method === "warning" || method === "configWarning";
|
||||
}
|
||||
|
||||
function isSuppressedCodexItemType(itemType: string): boolean {
|
||||
return itemType === "reasoning";
|
||||
}
|
||||
|
||||
function assistantMessageEventForCompleted(message: CompletedAssistantMessage, messageIndex: number): BackendEvent {
|
||||
return {
|
||||
type: "assistant_message",
|
||||
|
||||
Reference in New Issue
Block a user