diff options
| author | Adam Malczewski <[email protected]> | 2026-05-19 20:44:09 +0900 |
|---|---|---|
| committer | Adam Malczewski <[email protected]> | 2026-05-19 20:44:09 +0900 |
| commit | b3d16aa62a1b8724fe57ab3846a83f03a4a3fbc0 (patch) | |
| tree | 480a1fa2c95bf87ab214f709b1b4414aa64e8929 /packages/core | |
| parent | f78a91c20f658dd404277919a0b872b352c99bb6 (diff) | |
| download | dispatch-b3d16aa62a1b8724fe57ab3846a83f03a4a3fbc0.tar.gz dispatch-b3d16aa62a1b8724fe57ab3846a83f03a4a3fbc0.zip | |
fix: DeepSeek reasoning_content dropped on multi-step tool calls
- Base URL corrected: zen/v1 -> zen/go/v1 (opencode-go provider)
- Model changed: deepseek-v4-flash-free -> deepseek-v4-flash
- Added wrapLanguageModel middleware to inject reasoning_content via
providerMetadata.openaiCompatible before each stream call
- Fixed test mocks: removed vi.importActual (unsupported in Bun), added
tool factory mocks, preserved real tool export in ai mock
- Added 11 tests for the normalizeMessages middleware
Diffstat (limited to 'packages/core')
| -rw-r--r-- | packages/core/src/llm/provider.ts | 67 | ||||
| -rw-r--r-- | packages/core/tests/agent/agent.test.ts | 4 | ||||
| -rw-r--r-- | packages/core/tests/llm/provider.test.ts | 284 |
3 files changed, 352 insertions, 3 deletions
diff --git a/packages/core/src/llm/provider.ts b/packages/core/src/llm/provider.ts index 06f7b72..2a917b2 100644 --- a/packages/core/src/llm/provider.ts +++ b/packages/core/src/llm/provider.ts @@ -1,9 +1,74 @@ import { createOpenAICompatible } from "@ai-sdk/openai-compatible"; +import { wrapLanguageModel } from "ai"; +import type { LanguageModelV1Middleware, LanguageModelV1Prompt } from "ai"; + +/** + * Normalize messages for interleaved reasoning providers (DeepSeek). + * Extracts { type: "reasoning" } / { type: "redacted-reasoning" } parts from + * assistant message content arrays and moves them into + * providerMetadata.openaiCompatible.reasoning_content so + * @ai-sdk/openai-compatible serializes them correctly for the API. + * + * IMPORTANT: The messages in params.prompt are already in LanguageModelV1Prompt + * format (post-conversion by the AI SDK). They use `providerMetadata`, NOT + * `providerOptions` — that conversion happens before the middleware runs. + */ +function normalizeMessages(msgs: unknown[]): unknown[] { + return msgs.map((msg: unknown) => { + const message = msg as Record<string, unknown>; + if (message.role !== "assistant" || !Array.isArray(message.content)) return message; + + const content = message.content as Array<Record<string, unknown>>; + const reasoningParts = content.filter( + (p) => p.type === "reasoning" || p.type === "redacted-reasoning", + ); + const reasoningText = reasoningParts.map((p) => p.text ?? "").join(""); + + const filteredContent = content.filter( + (p) => p.type !== "reasoning" && p.type !== "redacted-reasoning", + ); + + const existingMetadata = (message.providerMetadata ?? {}) as Record<string, unknown>; + const existingOpenAICompat = (existingMetadata.openaiCompatible ?? {}) as Record<string, unknown>; + + return { + ...message, + content: filteredContent, + providerMetadata: { + ...existingMetadata, + openaiCompatible: { + ...existingOpenAICompat, + reasoning_content: reasoningText, + }, + }, + }; + }); +} export function createProvider(config: { apiKey: string; baseURL: string }) { - return createOpenAICompatible({ + const provider = createOpenAICompatible({ name: "opencode-zen", apiKey: config.apiKey, baseURL: config.baseURL, }); + + return (modelId: string) => { + const middleware: LanguageModelV1Middleware = { + middlewareVersion: "v1" as const, + transformParams: async ({ type, params }) => { + if (type === "stream" && params.prompt) { + return { + ...params, + prompt: normalizeMessages(params.prompt as unknown[]) as LanguageModelV1Prompt, + }; + } + return params; + }, + }; + + return wrapLanguageModel({ + model: provider(modelId), + middleware: [middleware], + }); + }; } diff --git a/packages/core/tests/agent/agent.test.ts b/packages/core/tests/agent/agent.test.ts index 81d42c8..92df90e 100644 --- a/packages/core/tests/agent/agent.test.ts +++ b/packages/core/tests/agent/agent.test.ts @@ -4,8 +4,8 @@ import { Agent } from "../../src/agent/agent.js"; import type { AgentConfig } from "../../src/types/index.js"; // Mock the ai module's streamText -vi.mock("ai", async (importOriginal) => { - const actual = await importOriginal<typeof import("ai")>(); +vi.mock("ai", async () => { + const actual = await import("ai"); return { ...actual, streamText: vi.fn(), diff --git a/packages/core/tests/llm/provider.test.ts b/packages/core/tests/llm/provider.test.ts new file mode 100644 index 0000000..9b5439e --- /dev/null +++ b/packages/core/tests/llm/provider.test.ts @@ -0,0 +1,284 @@ +import { describe, expect, it, vi } from "vitest"; + +// We test normalizeMessages through the middleware by mocking the provider +// layers and capturing what transformParams does to the prompt. + +// Mock wrapLanguageModel to capture the middleware +vi.mock("ai", async () => { + const actual = await import("ai"); + return { + ...actual, + wrapLanguageModel: vi.fn(({ model, middleware }) => { + // Return a wrapper that exposes the middleware for testing + const wrapped = actual.wrapLanguageModel({ model, middleware }); + (wrapped as unknown as Record<string, unknown>)._middleware = middleware; + return wrapped; + }), + streamText: vi.fn(), + }; +}); + +// Mock provider factory +vi.mock("@ai-sdk/openai-compatible", () => ({ + createOpenAICompatible: vi.fn(() => (modelId: string) => ({ + id: `mock-${modelId}`, + doGenerate: vi.fn(), + doStream: vi.fn(), + })), +})); + +const { createProvider } = await import("../../src/llm/provider.js"); + +// A helper that runs the middleware's transformParams on a prompt +// and returns the resulting normalized prompt. +async function runTransform( + prompt: unknown[], +): Promise<unknown[]> { + const wrappedModel = createProvider({ + apiKey: "test-key", + baseURL: "https://example.com/v1", + })("test-model"); + + const middleware = ( + (wrappedModel as unknown) as { _middleware: Array<{ transformParams: (args: { type: string; params: Record<string, unknown> }) => Promise<unknown> }> } + )._middleware; + + const result = await middleware[0]!.transformParams({ + type: "stream", + params: { prompt }, + }); + + return (result as Record<string, unknown>).prompt as unknown[]; +} + +describe("createProvider middleware", () => { + it("passes through non-stream calls unchanged", async () => { + const wrappedModel = createProvider({ + apiKey: "test-key", + baseURL: "https://example.com/v1", + })("test-model"); + + const middleware = ( + (wrappedModel as unknown) as { _middleware: Array<{ transformParams: (args: { type: string; params: Record<string, unknown> }) => Promise<unknown> }> } + )._middleware; + + const params = { prompt: [], temperature: 0.5 }; + const result = (await middleware[0]!.transformParams({ + type: "generate", + params, + })) as Record<string, unknown>; + + expect(result).toEqual(params); + }); + + it("strips reasoning parts and sets reasoning_content on providerMetadata", async () => { + const prompt = [ + { + role: "assistant", + content: [ + { type: "reasoning", text: "I should use the list_files tool." }, + { type: "text", text: "Let me check the directory." }, + { + type: "tool-call", + toolCallId: "call_1", + toolName: "list_files", + args: { path: "." }, + }, + ], + }, + ]; + + const normalized = await runTransform(prompt); + const msg = normalized[0] as Record<string, unknown>; + + // Reasoning parts removed from content + const content = msg.content as Array<Record<string, unknown>>; + expect(content).toHaveLength(2); + expect(content.find((p) => p.type === "reasoning")).toBeUndefined(); + expect(content.find((p) => p.type === "text")).toBeDefined(); + expect(content.find((p) => p.type === "tool-call")).toBeDefined(); + + // reasoning_content set on providerMetadata + const pm = msg.providerMetadata as Record<string, unknown>; + const compat = pm.openaiCompatible as Record<string, unknown>; + expect(compat.reasoning_content).toBe("I should use the list_files tool."); + }); + + it("sets empty reasoning_content when no reasoning parts exist", async () => { + const prompt = [ + { + role: "assistant", + content: [ + { type: "text", text: "Hello!" }, + ], + }, + ]; + + const normalized = await runTransform(prompt); + const msg = normalized[0] as Record<string, unknown>; + + // Content unchanged + const content = msg.content as Array<Record<string, unknown>>; + expect(content).toHaveLength(1); + expect(content[0]!.type).toBe("text"); + + // reasoning_content always set, even empty + const pm = msg.providerMetadata as Record<string, unknown>; + const compat = pm.openaiCompatible as Record<string, unknown>; + expect(compat.reasoning_content).toBe(""); + }); + + it("does not modify user messages", async () => { + const prompt = [ + { + role: "user", + content: [{ type: "text", text: "What dir am I in?" }], + }, + ]; + + const normalized = await runTransform(prompt); + expect(normalized).toEqual(prompt); + }); + + it("does not modify system messages", async () => { + const prompt = [ + { role: "system", content: "You are a helpful assistant." }, + ]; + + const normalized = await runTransform(prompt); + expect(normalized).toEqual(prompt); + }); + + it("handles assistant with plain string content (not array)", async () => { + const prompt = [ + { + role: "assistant", + content: "Hello world", + }, + ]; + + const normalized = await runTransform(prompt); + expect(normalized).toEqual(prompt); + }); + + it("handles redacted-reasoning type parts", async () => { + const prompt = [ + { + role: "assistant", + content: [ + { type: "redacted-reasoning", text: "[redacted chain of thought]" }, + { type: "text", text: "Here is the result." }, + ], + }, + ]; + + const normalized = await runTransform(prompt); + const msg = normalized[0] as Record<string, unknown>; + + const content = msg.content as Array<Record<string, unknown>>; + expect(content.find((p) => p.type === "redacted-reasoning")).toBeUndefined(); + expect(content.find((p) => p.type === "text")).toBeDefined(); + + const pm = msg.providerMetadata as Record<string, unknown>; + const compat = pm.openaiCompatible as Record<string, unknown>; + expect(compat.reasoning_content).toBe("[redacted chain of thought]"); + }); + + it("preserves existing providerMetadata fields", async () => { + const prompt = [ + { + role: "assistant", + content: [ + { type: "reasoning", text: "thinking..." }, + { type: "text", text: "done" }, + ], + providerMetadata: { + openaiCompatible: { custom_field: "keep-me" }, + }, + }, + ]; + + const normalized = await runTransform(prompt); + const msg = normalized[0] as Record<string, unknown>; + const pm = msg.providerMetadata as Record<string, unknown>; + const compat = pm.openaiCompatible as Record<string, unknown>; + + expect(compat.custom_field).toBe("keep-me"); + expect(compat.reasoning_content).toBe("thinking..."); + }); + + it("handles multi-message prompts with mixed roles", async () => { + const prompt = [ + { role: "system", content: "Be helpful." }, + { role: "user", content: [{ type: "text", text: "hi" }] }, + { + role: "assistant", + content: [ + { type: "reasoning", text: "I'll say hello." }, + { type: "text", text: "Hi there!" }, + ], + }, + ]; + + const normalized = await runTransform(prompt); + + // System and user unchanged + expect(normalized[0]).toEqual(prompt[0]); + expect(normalized[1]).toEqual(prompt[1]); + + // Assistant transformed + const msg = normalized[2] as Record<string, unknown>; + const pm = msg.providerMetadata as Record<string, unknown>; + const compat = pm.openaiCompatible as Record<string, unknown>; + expect(compat.reasoning_content).toBe("I'll say hello."); + }); + + it("concatenates multiple reasoning parts", async () => { + const prompt = [ + { + role: "assistant", + content: [ + { type: "reasoning", text: "Step 1: " }, + { type: "reasoning", text: "Step 2: " }, + { type: "reasoning", text: "Step 3." }, + { type: "text", text: "Final answer." }, + ], + }, + ]; + + const normalized = await runTransform(prompt); + const msg = normalized[0] as Record<string, unknown>; + const pm = msg.providerMetadata as Record<string, unknown>; + const compat = pm.openaiCompatible as Record<string, unknown>; + expect(compat.reasoning_content).toBe("Step 1: Step 2: Step 3."); + }); + + it("applies to every assistant message in a multi-step history", async () => { + const prompt = [ + { + role: "assistant", + content: [ + { type: "reasoning", text: "First thought." }, + { type: "tool-call", toolCallId: "c1", toolName: "list_files", args: {} }, + ], + }, + { + role: "assistant", + content: [ + { type: "reasoning", text: "Second thought." }, + { type: "text", text: "All done." }, + ], + }, + ]; + + const normalized = await runTransform(prompt); + + const msg1 = normalized[0] as Record<string, unknown>; + const compat1 = (msg1.providerMetadata as Record<string, unknown>).openaiCompatible as Record<string, unknown>; + expect(compat1.reasoning_content).toBe("First thought."); + + const msg2 = normalized[1] as Record<string, unknown>; + const compat2 = (msg2.providerMetadata as Record<string, unknown>).openaiCompatible as Record<string, unknown>; + expect(compat2.reasoning_content).toBe("Second thought."); + }); +}); |
