xonecas.io
64875
Mc

mini-coder / commit 1571d49

fix(llm-api): preserve openai commentary as reasoning

xonecas2026-03-14 1571d4931d256f081e309a31bbecfa1da9148ed3 parent 670c3c8

3 files changed

TODO.md+16−0
@@ -1,6 +1,22 @@
11 # TODO
2+
23 - Strange output mid session:
34
5+A fix was made to suppress commentary from gpt to avoid this, but now there is no reasoning output from gpt mdels.
6+
7+The issue is not fixed, it's just suppressed.
8+This seems like an issue on how we are parsing stream events, and not recognzing tool calls from other events.
9+
10+Relevant reasoning output:
11+
12+```
13+I keep running into issues with tool syntax, and it feels like it's because of the interface. I really need to use the correct commentary to call functions.shell. It's important to stick to the standard format to get it right. I wonder if I should double-check the documentation or examples to be sure I’m following things correctly. Just trying to make sure I figure this out! There's probably something simple I'm overlooking.It seems my interface isn’t accepting the call because I’m in anal …
14+
15+ I’m thinking maybe my last few attempts didn’t work because I didn’t include the recipient name. But I believe the functions should work with the shell style I used earlier. I’ll make sure to try using the multi_tool method again with the exact syntax I had. I'm curious if that’ll make a difference in the process. Let’s see how it goes!
16+```
17+
18+Corruption example, there are several sessions in our database where this happened with gpt models
19+
420 ```
521 ◆ Progress update: I’ve implemented a first UX-focused refactor around tool-stream visibility and am now validating stream/event tests plus a manual `bun run dev` run for one-shot tool calls.{"command":"bun test src/llm-api/turn-stream-events.test.ts src/llm-api/turn-execution.test.ts src/cli/tool-render-call.test.ts","timeout":120000}{"command":"bun test src/llm-api/turn-stream-events.test.ts src/llm-api/turn-execution.test.ts src/cli/tool-render-call.test.ts","timeout":120000} to=functions …
622 {"command":"bun test src/llm-api/turn-stream-events.test.ts src/llm-api/turn-execution.test.ts src/cli/tool-render-call.test.ts","timeout":120000}{"command":"bun test src/llm-api/turn-stream-events.test.ts src/llm-api/turn-execution.test.ts src/cli/tool-render-call.test.ts","timeout":120000}to=functions.shell 񹚊ppjson
src/llm-api/turn-execution.test.ts+21−2
@@ -90,14 +90,14 @@ describe("mapFullStreamToTurnEvents", () => {
9090 });
9191 });
9292
93- test("suppresses commentary text deltas for openai phased text parts", async () => {
93+ test("routes openai commentary text deltas into reasoning events", async () => {
9494 const events = await collectEvents([
9595 {
9696 type: "text-start",
9797 id: "msg-1",
9898 providerMetadata: { openai: { itemId: "msg-1", phase: "commentary" } },
9999 },
100- { type: "text-delta", id: "msg-1", text: "to=functions.shell json{}" },
100+ { type: "text-delta", id: "msg-1", text: "thinking aloud" },
101101 {
102102 type: "text-end",
103103 id: "msg-1",
@@ -133,6 +133,7 @@ describe("mapFullStreamToTurnEvents", () => {
133133 ]);
134134
135135 expect(events).toEqual([
136+ { type: "reasoning-delta", delta: "thinking aloud" },
136137 {
137138 type: "tool-call-start",
138139 toolCallId: "call-3",
@@ -157,4 +158,22 @@ describe("mapFullStreamToTurnEvents", () => {
157158
158159 expect(events).toEqual([{ type: "text-delta", delta: "visible" }]);
159160 });
161+
162+ test("keeps commentary text after the phased block as normal output", async () => {
163+ const events = await collectEvents([
164+ {
165+ type: "text-start",
166+ id: "msg-3",
167+ providerMetadata: { openai: { itemId: "msg-3", phase: "commentary" } },
168+ },
169+ { type: "text-delta", id: "msg-3", text: "first thought" },
170+ { type: "text-end", id: "msg-3" },
171+ { type: "text-delta", id: "msg-3", text: " visible" },
172+ ]);
173+
174+ expect(events).toEqual([
175+ { type: "reasoning-delta", delta: "first thought" },
176+ { type: "text-delta", delta: " visible" },
177+ ]);
178+ });
160179 });
src/llm-api/turn-execution.ts+34−10
@@ -155,31 +155,52 @@ function getOpenAITextPhase(
155155 : null;
156156 }
157157
158+type StreamTextRoute = "skip" | "text" | "reasoning";
159+
160+function extractTextDelta(chunk: StreamTextChunk): string {
161+ if (typeof chunk.text === "string") return chunk.text;
162+ if (typeof chunk.textDelta === "string") return chunk.textDelta;
163+ if (typeof chunk.delta === "string") return chunk.delta;
164+ return "";
165+}
166+
158167 class StreamTextPhaseTracker {
159168 private phaseByTextPartId = new Map<string, "commentary" | "final_answer">();
160169
161- shouldEmit(chunk: StreamTextChunk): boolean {
170+ route(chunk: StreamTextChunk): StreamTextRoute {
162171 const textPartId = normalizeTextPartId(chunk.id);
163172 switch (chunk.type) {
164173 case "text-start": {
165174 const phase = getOpenAITextPhase(chunk);
166175 if (textPartId && phase) this.phaseByTextPartId.set(textPartId, phase);
167- return false;
176+ return "skip";
168177 }
169178 case "text-end": {
170179 if (textPartId) this.phaseByTextPartId.delete(textPartId);
171- return false;
180+ return "skip";
172181 }
173182 case "text-delta": {
174- if (!textPartId) return true;
175- return this.phaseByTextPartId.get(textPartId) !== "commentary";
183+ if (!textPartId) return "text";
184+ return this.phaseByTextPartId.get(textPartId) === "commentary"
185+ ? "reasoning"
186+ : "text";
176187 }
177188 default:
178- return true;
189+ return "text";
179190 }
180191 }
181192 }
182193
194+function mapCommentaryChunkToTurnEvent(
195+ chunk: StreamTextChunk,
196+): TurnEvent | null {
197+ if (chunk.type !== "text-delta") return null;
198+ return {
199+ type: "reasoning-delta",
200+ delta: extractTextDelta(chunk),
201+ };
202+}
203+
183204 class StreamToolCallTracker {
184205 private syntheticCount = 0;
185206 private pendingByTool = new Map<string, string[]>();
@@ -251,12 +272,15 @@ export async function* mapFullStreamToTurnEvents(
251272 const textPhaseTracker = new StreamTextPhaseTracker();
252273 for await (const originalChunk of stream) {
253274 const chunk = toolCallTracker.assign(originalChunk);
254- const shouldEmitChunk = textPhaseTracker.shouldEmit(chunk);
255- if (shouldEmitChunk && shouldLogStreamChunk(chunk)) {
275+ const route = textPhaseTracker.route(chunk);
276+ if (route !== "skip" && shouldLogStreamChunk(chunk)) {
256277 opts.onChunk?.(chunk);
257278 }
258- if (!shouldEmitChunk) continue;
259- const event = mapStreamChunkToTurnEvent(chunk);
279+ if (route === "skip") continue;
280+ const event =
281+ route === "reasoning"
282+ ? mapCommentaryChunkToTurnEvent(chunk)
283+ : mapStreamChunkToTurnEvent(chunk);
260284 if (event) yield event;
261285 }
262286 }