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 } }