diff --git a/src/api/providers/__tests__/openai.spec.ts b/src/api/providers/__tests__/openai.spec.ts index 957e45b99f..12b20da8ee 100644 --- a/src/api/providers/__tests__/openai.spec.ts +++ b/src/api/providers/__tests__/openai.spec.ts @@ -1118,6 +1118,46 @@ describe("OpenAiHandler", () => { expect(model.id).toBe("") expect(model.info).toBeDefined() }) + + it("should set preserveReasoning when openAiR1FormatEnabled is on", () => { + const r1Handler = new OpenAiHandler({ + ...mockOptions, + openAiR1FormatEnabled: true, + }) + const model = r1Handler.getModel() + expect(model.info).toEqual({ ...openAiModelInfoSaneDefaults, preserveReasoning: true }) + }) + + it("should set preserveReasoning for deepseek-reasoner model ids without the toggle", () => { + const deepseekHandler = new OpenAiHandler({ + ...mockOptions, + openAiModelId: "deepseek-reasoner", + }) + const model = deepseekHandler.getModel() + expect(model.info.preserveReasoning).toBe(true) + }) + + it("should merge preserveReasoning into custom model info when openAiR1FormatEnabled is on", () => { + const customInfo: ModelInfo = { + ...openAiModelInfoSaneDefaults, + contextWindow: 32_768, + supportsImages: false, + } + const r1Handler = new OpenAiHandler({ + ...mockOptions, + openAiCustomModelInfo: customInfo, + openAiR1FormatEnabled: true, + }) + const model = r1Handler.getModel() + expect(model.info).toEqual({ ...customInfo, preserveReasoning: true }) + expect(model.info.contextWindow).toBe(32_768) + expect(model.info.supportsImages).toBe(false) + }) + + it("should not set preserveReasoning by default", () => { + const model = handler.getModel() + expect(model.info.preserveReasoning).toBeUndefined() + }) }) describe("Azure AI Inference Service", () => { diff --git a/src/api/providers/openai.ts b/src/api/providers/openai.ts index 8ccf3335f0..7b0493b4c9 100644 --- a/src/api/providers/openai.ts +++ b/src/api/providers/openai.ts @@ -98,7 +98,7 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl // the same strict value as `??`. const strictToolSchemas = this.options.openAiStrictToolSchemas ?? DEFAULT_OPEN_AI_STRICT_TOOL_SCHEMAS const isAzureAiInference = this._isAzureAiInference(modelUrl) - const deepseekReasoner = modelId.includes("deepseek-reasoner") || enabledR1Format + const deepseekReasoner = this.usesR1Format(modelId) if (modelId.includes("o1") || modelId.includes("o3") || modelId.includes("o4")) { yield* this.handleO3FamilyMessage(modelId, systemPrompt, messages, metadata) @@ -299,6 +299,14 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl } } + // Single gate for everything that depends on the R1 request format: message + // conversion in createMessage() and reasoning retention in getModel(). Both + // must agree, otherwise reasoning is converted for the request but stripped + // from the follow-up context (or vice versa). + private usesR1Format(modelId: string): boolean { + return modelId.includes("deepseek-reasoner") || (this.options.openAiR1FormatEnabled ?? false) + } + override getModel() { const id = this.options.openAiModelId ?? "" const info: ModelInfo = this.options.openAiCustomModelInfo ?? openAiModelInfoSaneDefaults @@ -311,7 +319,16 @@ export class OpenAiHandler extends BaseProvider implements SingleCompletionHandl settings: { ...this.options, reasoningEffort: info.reasoningEffort }, defaultTemperature: 0, }) - return { id, info, ...params } + // Local OpenAI-compatible reasoning models (llama.cpp, LM Studio, Ollama) + // stream reasoning_content, but Zoo Code strips it from the follow-up context + // unless info.preserveReasoning is set. Whenever createMessage() treats the + // model as R1 (deepseek-reasoner id or the R1 format toggle), also treat it + // as preserving reasoning so the chain is fed back. + return { + id, + info: this.usesR1Format(id) ? { ...info, preserveReasoning: true } : info, + ...params, + } } async completePrompt(prompt: string, options?: CompletePromptOptions): Promise { diff --git a/src/core/task/__tests__/reasoning-preservation.test.ts b/src/core/task/__tests__/reasoning-preservation.test.ts index f34cea1bdc..16e959fcc3 100644 --- a/src/core/task/__tests__/reasoning-preservation.test.ts +++ b/src/core/task/__tests__/reasoning-preservation.test.ts @@ -512,4 +512,70 @@ describe("Task reasoning preservation", () => { text: assistantText, }) }) + + it("should keep plain text reasoning in buildCleanConversationHistory when preserveReasoning is true", async () => { + const task = new Task({ + provider: mockProvider as ClineProvider, + apiConfiguration: mockApiConfiguration, + task: "Test task", + startTask: false, + }) + + const requestModelInfo: ModelInfo = { + contextWindow: 16000, + supportsPromptCache: true, + preserveReasoning: true, + } + + await task["addToApiConversationHistory"]( + { + role: "assistant", + content: [{ type: "text", text: "Reading the file." }], + }, + "I should read the file first.", + ) + + const history = task["buildCleanConversationHistory"](task.apiConversationHistory, requestModelInfo) + + expect(history).toHaveLength(1) + expect(history[0]).toMatchObject({ + role: "assistant", + content: [ + { type: "reasoning", text: "I should read the file first.", summary: [] }, + { type: "text", text: "Reading the file." }, + ], + }) + }) + + it("should strip plain text reasoning in buildCleanConversationHistory when preserveReasoning is not set", async () => { + const task = new Task({ + provider: mockProvider as ClineProvider, + apiConfiguration: mockApiConfiguration, + task: "Test task", + startTask: false, + }) + + // preserveReasoning is undefined on this model info + const requestModelInfo: ModelInfo = { + contextWindow: 16000, + supportsPromptCache: true, + } + + await task["addToApiConversationHistory"]( + { + role: "assistant", + content: [{ type: "text", text: "Reading the file." }], + }, + "I should read the file first.", + ) + + const history = task["buildCleanConversationHistory"](task.apiConversationHistory, requestModelInfo) + + expect(history).toHaveLength(1) + expect(history[0]).toMatchObject({ + role: "assistant", + content: "Reading the file.", + }) + expect(JSON.stringify(history)).not.toContain("reasoning") + }) })