xonecas.io
64875
Mc

mini-coder / commit cdaeafb

fix(llm-api): suppress openai commentary text deltas

xonecas2026-03-14 cdaeafbda6e95213aa6408ceec8d55c61a663976 parent 13654ac

2 files changed

src/llm-api/turn-execution.test.ts+68−0
@@ -89,4 +89,72 @@ describe("mapFullStreamToTurnEvents", () => {
8989 toolName: "shell",
9090 });
9191 });
92+
93+ test("suppresses commentary text deltas for openai phased text parts", async () => {
94+ const events = await collectEvents([
95+ {
96+ type: "text-start",
97+ id: "msg-1",
98+ providerMetadata: { openai: { itemId: "msg-1", phase: "commentary" } },
99+ },
100+ { type: "text-delta", id: "msg-1", text: "to=functions.shell json{}" },
101+ {
102+ type: "text-end",
103+ id: "msg-1",
104+ providerMetadata: { openai: { itemId: "msg-1", phase: "commentary" } },
105+ },
106+ {
107+ type: "tool-call",
108+ toolCallId: "call-3",
109+ toolName: "shell",
110+ input: { command: "echo hi" },
111+ },
112+ {
113+ type: "tool-result",
114+ toolCallId: "call-3",
115+ toolName: "shell",
116+ output: "hi",
117+ },
118+ {
119+ type: "text-start",
120+ id: "msg-2",
121+ providerMetadata: {
122+ openai: { itemId: "msg-2", phase: "final_answer" },
123+ },
124+ },
125+ { type: "text-delta", id: "msg-2", text: "Done" },
126+ {
127+ type: "text-end",
128+ id: "msg-2",
129+ providerMetadata: {
130+ openai: { itemId: "msg-2", phase: "final_answer" },
131+ },
132+ },
133+ ]);
134+
135+ expect(events).toEqual([
136+ {
137+ type: "tool-call-start",
138+ toolCallId: "call-3",
139+ toolName: "shell",
140+ args: { command: "echo hi" },
141+ },
142+ {
143+ type: "tool-result",
144+ toolCallId: "call-3",
145+ toolName: "shell",
146+ result: "hi",
147+ isError: false,
148+ },
149+ { type: "text-delta", delta: "Done" },
150+ ]);
151+ });
152+
153+ test("keeps unphased text deltas visible", async () => {
154+ const events = await collectEvents([
155+ { type: "text-delta", id: "msg-plain", text: "visible" },
156+ ]);
157+
158+ expect(events).toEqual([{ type: "text-delta", delta: "visible" }]);
159+ });
92160 });
src/llm-api/turn-execution.ts+63−1
@@ -106,6 +106,14 @@ interface StreamToolChunk {
106106 [key: string]: unknown;
107107 }
108108
109+interface StreamTextChunk {
110+ type?: string;
111+ id?: unknown;
112+ providerMetadata?: unknown;
113+ providerOptions?: unknown;
114+ [key: string]: unknown;
115+}
116+
109117 const TOOL_START_CHUNK_TYPES = new Set(["tool-input-start", "tool-call"]);
110118 const TOOL_RESULT_CHUNK_TYPES = new Set(["tool-result", "tool-error"]);
111119
@@ -121,6 +129,57 @@ function normalizeToolName(raw: unknown): string {
121129 return trimmed || "tool";
122130 }
123131
132+function isRecord(value: unknown): value is Record<string, unknown> {
133+ return value !== null && typeof value === "object";
134+}
135+
136+function normalizeTextPartId(raw: unknown): string | null {
137+ if (typeof raw !== "string") return null;
138+ const trimmed = raw.trim();
139+ return trimmed ? trimmed : null;
140+}
141+
142+function getOpenAITextPhase(
143+ chunk: StreamTextChunk,
144+): "commentary" | "final_answer" | null {
145+ const providerData = isRecord(chunk.providerMetadata)
146+ ? chunk.providerMetadata
147+ : isRecord(chunk.providerOptions)
148+ ? chunk.providerOptions
149+ : null;
150+ if (!providerData) return null;
151+ const openai = providerData.openai;
152+ if (!isRecord(openai)) return null;
153+ return openai.phase === "commentary" || openai.phase === "final_answer"
154+ ? openai.phase
155+ : null;
156+}
157+
158+class StreamTextPhaseTracker {
159+ private phaseByTextPartId = new Map<string, "commentary" | "final_answer">();
160+
161+ shouldEmit(chunk: StreamTextChunk): boolean {
162+ const textPartId = normalizeTextPartId(chunk.id);
163+ switch (chunk.type) {
164+ case "text-start": {
165+ const phase = getOpenAITextPhase(chunk);
166+ if (textPartId && phase) this.phaseByTextPartId.set(textPartId, phase);
167+ return false;
168+ }
169+ case "text-end": {
170+ if (textPartId) this.phaseByTextPartId.delete(textPartId);
171+ return false;
172+ }
173+ case "text-delta": {
174+ if (!textPartId) return true;
175+ return this.phaseByTextPartId.get(textPartId) !== "commentary";
176+ }
177+ default:
178+ return true;
179+ }
180+ }
181+}
182+
124183 class StreamToolCallTracker {
125184 private syntheticCount = 0;
126185 private pendingByTool = new Map<string, string[]>();
@@ -189,11 +248,14 @@ export async function* mapFullStreamToTurnEvents(
189248 },
190249 ): AsyncGenerator<TurnEvent> {
191250 const toolCallTracker = new StreamToolCallTracker();
251+ const textPhaseTracker = new StreamTextPhaseTracker();
192252 for await (const originalChunk of stream) {
193253 const chunk = toolCallTracker.assign(originalChunk);
194- if (shouldLogStreamChunk(chunk)) {
254+ const shouldEmitChunk = textPhaseTracker.shouldEmit(chunk);
255+ if (shouldEmitChunk && shouldLogStreamChunk(chunk)) {
195256 opts.onChunk?.(chunk);
196257 }
258+ if (!shouldEmitChunk) continue;
197259 const event = mapStreamChunkToTurnEvent(chunk);
198260 if (event) yield event;
199261 }