@@ -7,6 +7,7 @@ import {
77 getReasoningDeltaFromStreamChunk,
88 isOpenAIGPT,
99 sanitizeGeminiToolMessages,
10+ stripGPTCommentaryFromHistory,
1011 } from "./turn.ts";
1112
1213 describe("isOpenAIGPT", () => {
@@ -226,6 +227,141 @@ describe("sanitizeGeminiToolMessages", () => {
226227 });
227228 });
228229
230+describe("stripGPTCommentaryFromHistory", () => {
231+ test("strips commentary text parts from GPT assistant messages", () => {
232+ const messages: CoreMessage[] = [
233+ { role: "user", content: "do the task" },
234+ {
235+ role: "assistant",
236+ content: [
237+ {
238+ type: "text",
239+ text: "I'll call shell: to=functions.shell json{...}",
240+ providerOptions: {
241+ openai: { itemId: "msg-1", phase: "commentary" },
242+ },
243+ },
244+ {
245+ type: "tool-call",
246+ toolCallId: "call-1",
247+ toolName: "shell",
248+ input: { command: "ls" },
249+ },
250+ ],
251+ } as unknown as CoreMessage,
252+ {
253+ role: "tool",
254+ content: [
255+ {
256+ type: "tool-result",
257+ toolCallId: "call-1",
258+ toolName: "shell",
259+ output: { type: "text", value: "file.txt" },
260+ },
261+ ],
262+ } as unknown as CoreMessage,
263+ {
264+ role: "assistant",
265+ content: [
266+ {
267+ type: "text",
268+ text: "Done!",
269+ providerOptions: {
270+ openai: { itemId: "msg-2", phase: "final_answer" },
271+ },
272+ },
273+ ],
274+ } as unknown as CoreMessage,
275+ ];
276+
277+ const result = stripGPTCommentaryFromHistory(
278+ messages,
279+ "openai/gpt-5.3-codex",
280+ );
281+
282+ // commentary part stripped from first assistant message
283+ const firstAssistant = result[1] as {
284+ role: "assistant";
285+ content: Array<Record<string, unknown>>;
286+ };
287+ expect(firstAssistant.content).toHaveLength(1);
288+ expect(firstAssistant.content[0]?.type).toBe("tool-call");
289+
290+ // final_answer text preserved in second assistant message
291+ const secondAssistant = result[3] as {
292+ role: "assistant";
293+ content: Array<Record<string, unknown>>;
294+ };
295+ expect(secondAssistant.content).toHaveLength(1);
296+ expect(secondAssistant.content[0]?.type).toBe("text");
297+ });
298+
299+ test("also strips when phase is in providerMetadata (legacy field)", () => {
300+ const messages: CoreMessage[] = [
301+ {
302+ role: "assistant",
303+ content: [
304+ {
305+ type: "text",
306+ text: "thinking...",
307+ providerMetadata: {
308+ openai: { itemId: "msg-x", phase: "commentary" },
309+ },
310+ },
311+ { type: "text", text: "result", providerOptions: {} },
312+ ],
313+ } as unknown as CoreMessage,
314+ ];
315+
316+ const result = stripGPTCommentaryFromHistory(messages, "zen/gpt-5.4");
317+ const assistant = result[0] as {
318+ role: "assistant";
319+ content: Array<Record<string, unknown>>;
320+ };
321+ expect(assistant.content).toHaveLength(1);
322+ expect(assistant.content[0]?.text).toBe("result");
323+ });
324+
325+ test("leaves non-GPT models untouched", () => {
326+ const messages: CoreMessage[] = [
327+ {
328+ role: "assistant",
329+ content: [
330+ {
331+ type: "text",
332+ text: "thinking...",
333+ providerOptions: { openai: { phase: "commentary" } },
334+ },
335+ ],
336+ } as unknown as CoreMessage,
337+ ];
338+
339+ expect(
340+ stripGPTCommentaryFromHistory(messages, "anthropic/claude-sonnet-4-5"),
341+ ).toBe(messages);
342+ });
343+
344+ test("returns same reference when no commentary parts are present", () => {
345+ const messages: CoreMessage[] = [
346+ { role: "user", content: "hi" },
347+ {
348+ role: "assistant",
349+ content: [
350+ {
351+ type: "text",
352+ text: "hello",
353+ providerOptions: { openai: { phase: "final_answer" } },
354+ },
355+ ],
356+ } as unknown as CoreMessage,
357+ ];
358+
359+ expect(stripGPTCommentaryFromHistory(messages, "openai/gpt-4o")).toBe(
360+ messages,
361+ );
362+ });
363+});
364+
229365 describe("applyContextPruning", () => {
230366 test("balanced mode prunes stale tool history", () => {
231367 const messages: CoreMessage[] = [