From ab914f0587ca051fd4b8294c54d769829b7c436d Mon Sep 17 00:00:00 2001 From: Antigravity Date: Tue, 5 May 2026 21:33:16 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20reasoning=5Fcontent=20DeepSeek=20?= =?UTF-8?q?=E2=80=94=20retry=20avec=20maxSteps=3D1=20si=20erreur=20thinkin?= =?UTF-8?q?g=20mode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Cursor --- .../lib/ai/providers/custom-openai.ts | 47 ++++++++++++------- memento-note/lib/ai/providers/deepseek.ts | 47 ++++++++++++------- 2 files changed, 62 insertions(+), 32 deletions(-) diff --git a/memento-note/lib/ai/providers/custom-openai.ts b/memento-note/lib/ai/providers/custom-openai.ts index fcf3007..63914c9 100644 --- a/memento-note/lib/ai/providers/custom-openai.ts +++ b/memento-note/lib/ai/providers/custom-openai.ts @@ -119,25 +119,40 @@ export class CustomOpenAIProvider implements AIProvider { async generateWithTools(options: ToolUseOptions): Promise { const { tools, maxSteps = 10, systemPrompt, messages, prompt } = options - const opts: Record = { - model: this.model, - tools, - stopWhen: stepCountIs(maxSteps), - } - if (systemPrompt) opts.system = systemPrompt - if (messages) opts.messages = messages - else if (prompt) opts.prompt = prompt - const result = await aiGenerateText(opts as any) - return { - toolCalls: result.toolCalls?.map((tc: any) => ({ toolName: tc.toolName, input: tc.input })) || [], - toolResults: result.toolResults?.map((tr: any) => ({ toolName: tr.toolName, input: tr.input, output: tr.output })) || [], - text: result.text, - steps: result.steps?.map((step: any) => ({ + const buildOpts = (steps: number): Record => { + const opts: Record = { model: this.model, tools, stopWhen: stepCountIs(steps) } + if (systemPrompt) opts.system = systemPrompt + if (messages) opts.messages = messages + else if (prompt) opts.prompt = prompt + return opts + } + + const toResult = (r: any): ToolCallResult => ({ + toolCalls: r.toolCalls?.map((tc: any) => ({ toolName: tc.toolName, input: tc.input })) || [], + toolResults: r.toolResults?.map((tr: any) => ({ toolName: tr.toolName, input: tr.input, output: tr.output })) || [], + text: r.text, + steps: r.steps?.map((step: any) => ({ text: step.text, toolCalls: step.toolCalls?.map((tc: any) => ({ toolName: tc.toolName, input: tc.input })) || [], - toolResults: step.toolResults?.map((tr: any) => ({ toolName: tr.toolName, input: tr.input, output: tr.output })) || [] - })) || [] + toolResults: step.toolResults?.map((tr: any) => ({ toolName: tr.toolName, input: tr.input, output: tr.output })) || [], + })) || [], + }) + + try { + const result = await aiGenerateText(buildOpts(maxSteps) as any) + return toResult(result) + } catch (err: any) { + // DeepSeek reasoning/thinking models require reasoning_content to be passed back + // between multi-step calls, which the AI SDK doesn't handle via the OpenAI-compat layer. + // Retry with a single step so the model calls the tool directly. + const msg: string = err?.message || String(err) + if (msg.includes('reasoning_content') || msg.includes('thinking mode')) { + console.warn('[CustomOpenAI] Reasoning model detected — retrying with maxSteps=1') + const result = await aiGenerateText(buildOpts(1) as any) + return toResult(result) + } + throw err } } diff --git a/memento-note/lib/ai/providers/deepseek.ts b/memento-note/lib/ai/providers/deepseek.ts index 1e1b86d..b59897b 100644 --- a/memento-note/lib/ai/providers/deepseek.ts +++ b/memento-note/lib/ai/providers/deepseek.ts @@ -103,25 +103,40 @@ export class DeepSeekProvider implements AIProvider { async generateWithTools(options: ToolUseOptions): Promise { const { tools, maxSteps = 10, systemPrompt, messages, prompt } = options - const opts: Record = { - model: this.model, - tools, - stopWhen: stepCountIs(maxSteps), - } - if (systemPrompt) opts.system = systemPrompt - if (messages) opts.messages = messages - else if (prompt) opts.prompt = prompt - const result = await aiGenerateText(opts as any) - return { - toolCalls: result.toolCalls?.map((tc: any) => ({ toolName: tc.toolName, input: tc.input })) || [], - toolResults: result.toolResults?.map((tr: any) => ({ toolName: tr.toolName, input: tr.input, output: tr.output })) || [], - text: result.text, - steps: result.steps?.map((step: any) => ({ + const buildOpts = (steps: number): Record => { + const opts: Record = { model: this.model, tools, stopWhen: stepCountIs(steps) } + if (systemPrompt) opts.system = systemPrompt + if (messages) opts.messages = messages + else if (prompt) opts.prompt = prompt + return opts + } + + const toResult = (r: any): ToolCallResult => ({ + toolCalls: r.toolCalls?.map((tc: any) => ({ toolName: tc.toolName, input: tc.input })) || [], + toolResults: r.toolResults?.map((tr: any) => ({ toolName: tr.toolName, input: tr.input, output: tr.output })) || [], + text: r.text, + steps: r.steps?.map((step: any) => ({ text: step.text, toolCalls: step.toolCalls?.map((tc: any) => ({ toolName: tc.toolName, input: tc.input })) || [], - toolResults: step.toolResults?.map((tr: any) => ({ toolName: tr.toolName, input: tr.input, output: tr.output })) || [] - })) || [] + toolResults: step.toolResults?.map((tr: any) => ({ toolName: tr.toolName, input: tr.input, output: tr.output })) || [], + })) || [], + }) + + try { + const result = await aiGenerateText(buildOpts(maxSteps) as any) + return toResult(result) + } catch (err: any) { + // DeepSeek reasoning/thinking models require reasoning_content to be passed back + // between multi-step calls, which the AI SDK doesn't handle automatically. + // Retry with a single step so the model calls the tool directly without multi-turn. + const msg: string = err?.message || String(err) + if (msg.includes('reasoning_content') || msg.includes('thinking mode')) { + console.warn('[DeepSeek] Reasoning model detected — retrying with maxSteps=1') + const result = await aiGenerateText(buildOpts(1) as any) + return toResult(result) + } + throw err } }