Spaces:
Paused
Paused
icebear icebear0828 commited on
fix: handle new Responses SSE lifecycle events (#137)
Browse files* fix: handle new Responses SSE lifecycle events (output_item.added message, content_part)
Upstream Codex API now wraps text responses in message/content_part
lifecycle events. Our parser only recognized output_item.added with
item.type=function_call, causing unknown event log spam.
- Widen output_item.added to accept any item.type (message, etc.)
- Add content_part.added and content_part.done as recognized no-op events
- Update extractor to handle optional call_id/name on non-function_call items
* chore: remove loose index signature from CodexOutputItemAddedEvent.item
---------
Co-authored-by: icebear0828 <icebear0828@users.noreply.github.com>
Files changed (4)
7 unmodified lines89101112137 unmodified lines### Fixed- 新模型(如 `gpt-5.4-mini`)无法被动态发现的问题- 移除 `isCodexCompatibleId()` 白名单过滤,信任后端 `/codex/models` 返回- 同一 Team 的多个账号因共享 `chatgpt_account_id` 只能添加一个的问题(#126)7 unmodified lines8910111213147 unmodified lines### Fixed- Responses SSE 新事件(`response.output_item.added` with `item.type=message`、`response.content_part.added/done`)未被识别,导致 `[CodexEvents] Unknown event` 日志刷屏- 新模型(如 `gpt-5.4-mini`)无法被动态发现的问题- 移除 `isCodexCompatibleId()` 白名单过滤,信任后端 `/codex/models` 返回- 同一 Team 的多个账号因共享 `chatgpt_account_id` 只能添加一个的问题(#126)
91 unmodified lines9293949596979829 unmodified lines12812913013113213313491 unmodified linesbreak;case "response.output_item.added":if (typed.item.type === "function_call") {// Register item_id → call_id mappingitemIdToCallInfo.set(typed.item.id, {callId: typed.item.call_id,29 unmodified lines}case "response.output_item.done":// Completion marker — tool call data already delivered via delta/done eventsbreak;case "response.incomplete":91 unmodified lines9293949596979829 unmodified lines12812913013113213313413513691 unmodified linesbreak;case "response.output_item.added":if (typed.item.type === "function_call" && typed.item.call_id && typed.item.name) {// Register item_id → call_id mappingitemIdToCallInfo.set(typed.item.id, {callId: typed.item.call_id,29 unmodified lines}case "response.output_item.done":case "response.content_part.added":case "response.content_part.done":// Lifecycle markers — no data extraction neededbreak;case "response.incomplete":
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475import { describe, it, expect } from "vitest";import { parseCodexEvent } from "../codex-events.js";import type { CodexSSEEvent } from "../../proxy/codex-api.js";
function makeRaw(event: string, data: unknown): CodexSSEEvent { return { event, data };}
describe("parseCodexEvent — message output_item.added", () => { it("parses output_item.added with item.type=message as known event", () => { const raw = makeRaw("response.output_item.added", { type: "response.output_item.added", item: { id: "msg_abc", type: "message", status: "in_progress", content: [], role: "assistant", }, output_index: 0, sequence_number: 2, }); const result = parseCodexEvent(raw); expect(result.type).toBe("response.output_item.added"); expect(result.type).not.toBe("unknown"); });
it("still parses output_item.added with item.type=function_call", () => { const raw = makeRaw("response.output_item.added", { type: "response.output_item.added", item: { id: "fc_1", type: "function_call", call_id: "call_1", name: "get_weather", }, output_index: 0, }); const result = parseCodexEvent(raw); expect(result.type).toBe("response.output_item.added"); if (result.type === "response.output_item.added") { expect(result.item.type).toBe("function_call"); expect(result.item.call_id).toBe("call_1"); expect(result.item.name).toBe("get_weather"); } });});
describe("parseCodexEvent — content_part events", () => { it("parses response.content_part.added as known event", () => { const raw = makeRaw("response.content_part.added", { type: "response.content_part.added", content_index: 0, item_id: "msg_abc", output_index: 0, part: { type: "output_text", annotations: [], logprobs: [], text: "" }, sequence_number: 3, }); const result = parseCodexEvent(raw); expect(result.type).toBe("response.content_part.added"); });
it("parses response.content_part.done as known event", () => { const raw = makeRaw("response.content_part.done", { type: "response.content_part.done", content_index: 0, item_id: "msg_abc", output_index: 0, part: { type: "output_text", annotations: [], logprobs: [], text: "Hello world" }, sequence_number: 4, }); const result = parseCodexEvent(raw); expect(result.type).toBe("response.content_part.done"); });});62 unmodified lines6364656667686970717273747557 unmodified lines13313413513613713884 unmodified lines22322422522622722822923023123223323423523623723823924024124224324462 unmodified linestype: "response.output_item.added";outputIndex: number;item: {type: "function_call";id: string;call_id: string;name: string;};}export interface CodexFunctionCallArgsDeltaEvent {type: "response.function_call_arguments.delta";delta: string;57 unmodified lines| CodexCompletedEvent| CodexOutputItemAddedEvent| CodexOutputItemDoneEvent| CodexIncompleteEvent| CodexQueuedEvent| CodexFunctionCallArgsDeltaEvent84 unmodified lines: { type: "unknown", raw: data };}case "response.output_item.added": {if (isRecord(data) &&isRecord(data.item) &&data.item.type === "function_call" &&typeof data.item.call_id === "string" &&typeof data.item.name === "string") {return {type: "response.output_item.added",outputIndex: typeof data.output_index === "number" ? data.output_index : 0,item: {type: "function_call",id: typeof data.item.id === "string" ? data.item.id : "",call_id: data.item.call_id,name: data.item.name,},};}return { type: "unknown", raw: data };62 unmodified lines636465666768697071727374757677787980818283848586878889909157 unmodified lines14915015115215315415515684 unmodified lines24124224324424524624724824925025125225325425525625725825926026126226326426526626726826927062 unmodified linestype: "response.output_item.added";outputIndex: number;item: {type: string;id: string;call_id?: string;name?: string;};}export interface CodexContentPartAddedEvent {type: "response.content_part.added";contentIndex: number;outputIndex: number;itemId: string;part: Record<string, unknown>;}export interface CodexContentPartDoneEvent {type: "response.content_part.done";contentIndex: number;outputIndex: number;itemId: string;part: Record<string, unknown>;}export interface CodexFunctionCallArgsDeltaEvent {type: "response.function_call_arguments.delta";delta: string;57 unmodified lines| CodexCompletedEvent| CodexOutputItemAddedEvent| CodexOutputItemDoneEvent| CodexContentPartAddedEvent| CodexContentPartDoneEvent| CodexIncompleteEvent| CodexQueuedEvent| CodexFunctionCallArgsDeltaEvent84 unmodified lines: { type: "unknown", raw: data };}case "response.output_item.added": {if (isRecord(data) && isRecord(data.item) && typeof data.item.type === "string") {const item: CodexOutputItemAddedEvent["item"] = {type: data.item.type,id: typeof data.item.id === "string" ? data.item.id : "",};if (typeof data.item.call_id === "string") item.call_id = data.item.call_id;if (typeof data.item.name === "string") item.name = data.item.name;return {type: "response.output_item.added",outputIndex: typeof data.output_index === "number" ? data.output_index : 0,item,};}return { type: "unknown", raw: data };}case "response.content_part.added":case "response.content_part.done": {if (isRecord(data) && isRecord(data.part)) {return {type: evt.event as "response.content_part.added" | "response.content_part.done",contentIndex: typeof data.content_index === "number" ? data.content_index : 0,outputIndex: typeof data.output_index === "number" ? data.output_index : 0,itemId: typeof data.item_id === "string" ? data.item_id : "",part: data.part as Record<string, unknown>,};}return { type: "unknown", raw: data };
CHANGELOG.md+1
src/translation/codex-event-extractor.ts+4-2
src/types/__tests__/codex-events.test.tsAdded+75
src/types/codex-events.ts+42-16