From 065308f89183e6470595b0b469e6fa8a55fe0196 Mon Sep 17 00:00:00 2001 From: icn5381 <978009095@qq.com> Date: Mon, 27 Apr 2026 01:26:04 +0800 Subject: [PATCH] fix(openai): preserve reasoning_content in conversation history DeepSeek (and other thinking-mode providers) require reasoning_content to be passed back in subsequent API calls. Previously, ThinkingContent was explicitly skipped during message conversion, causing a 400 error on the second turn of any conversation using thinking mode. --- src/community/openai/__tests__/utils.test.ts | 58 ++++++++++++++++++++ src/community/openai/utils.ts | 2 +- 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 src/community/openai/__tests__/utils.test.ts diff --git a/src/community/openai/__tests__/utils.test.ts b/src/community/openai/__tests__/utils.test.ts new file mode 100644 index 0000000..c85081a --- /dev/null +++ b/src/community/openai/__tests__/utils.test.ts @@ -0,0 +1,58 @@ +import { describe, expect, test } from "bun:test"; + +import type { Message } from "@/foundation"; + +import { convertToOpenAIMessages } from "../utils"; + +describe("convertToOpenAIMessages", () => { + test("preserves thinking content as reasoning_content", () => { + const messages: Message[] = [ + { role: "user", content: [{ type: "text", text: "hello" }] }, + { + role: "assistant", + content: [ + { type: "thinking", thinking: "let me reason about this" }, + { type: "text", text: "hi there" }, + ], + }, + { role: "user", content: [{ type: "text", text: "follow up" }] }, + ]; + + const result = convertToOpenAIMessages(messages); + const assistantMsg = result[1] as unknown as Record; + + expect(assistantMsg.reasoning_content).toBe("let me reason about this"); + expect(assistantMsg.content).toEqual([{ type: "text", text: "hi there" }]); + }); + + test("handles thinking-only assistant message (no text)", () => { + const messages: Message[] = [ + { role: "user", content: [{ type: "text", text: "think about this" }] }, + { + role: "assistant", + content: [{ type: "thinking", thinking: "deep thoughts" }], + }, + ]; + + const result = convertToOpenAIMessages(messages); + const assistantMsg = result[1] as unknown as Record; + + expect(assistantMsg.reasoning_content).toBe("deep thoughts"); + }); + + test("handles assistant message without thinking content", () => { + const messages: Message[] = [ + { role: "user", content: [{ type: "text", text: "hello" }] }, + { + role: "assistant", + content: [{ type: "text", text: "hi" }], + }, + ]; + + const result = convertToOpenAIMessages(messages); + const assistantMsg = result[1] as unknown as Record; + + expect(assistantMsg.reasoning_content).toBeUndefined(); + expect(assistantMsg.content).toEqual([{ type: "text", text: "hi" }]); + }); +}); diff --git a/src/community/openai/utils.ts b/src/community/openai/utils.ts index 081b74a..2ee9c17 100644 --- a/src/community/openai/utils.ts +++ b/src/community/openai/utils.ts @@ -25,7 +25,7 @@ export function convertToOpenAIMessages(messages: Message[]): ChatCompletionMess }; for (const content of message.content) { if (content.type === "thinking") { - continue; + (assistantMessage as unknown as Record).reasoning_content = content.thinking; } else if (content.type === "tool_use") { if (!assistantMessage.tool_calls) { assistantMessage.tool_calls = [];