|
| 1 | +import { describe, test, expect } from "bun:test"; |
| 2 | +import type { ConversationTurn, InferenceOptions } from "@intx/types/runtime"; |
| 3 | +import { |
| 4 | + createDeepSeekV4Adapter, |
| 5 | + DEEPSEEK_V4_PROVIDER, |
| 6 | +} from "./deepseek-v4-adapter.js"; |
| 7 | +import { createOpenAICompatibleAdapter } from "./openai-compatible-adapter.js"; |
| 8 | + |
| 9 | +const source = { |
| 10 | + id: "test", |
| 11 | + provider: DEEPSEEK_V4_PROVIDER, |
| 12 | + baseURL: "https://example.test", |
| 13 | + apiKey: "sk-test", |
| 14 | + model: "deepseek-v4-pro", |
| 15 | +} as unknown as Parameters<typeof createDeepSeekV4Adapter>[0]; |
| 16 | + |
| 17 | +const messages: ConversationTurn[] = [ |
| 18 | + { |
| 19 | + role: "user", |
| 20 | + content: [{ type: "text", text: "hi" }], |
| 21 | + } as unknown as ConversationTurn, |
| 22 | +]; |
| 23 | + |
| 24 | +function bodyFor(options: InferenceOptions): Record<string, unknown> { |
| 25 | + const adapter = createDeepSeekV4Adapter(source); |
| 26 | + const built = adapter.buildRequest(messages, "deepseek-v4-pro", options); |
| 27 | + return JSON.parse(built.body) as Record<string, unknown>; |
| 28 | +} |
| 29 | + |
| 30 | +describe("deepseek-v4 adapter effort wiring", () => { |
| 31 | + test.each(["xhigh", "max"] as const)( |
| 32 | + "wires V4 effort %s through raw and turns thinking on", |
| 33 | + (effort) => { |
| 34 | + const body = bodyFor({ |
| 35 | + providerOptions: { reasoning_effort: effort }, |
| 36 | + } as InferenceOptions); |
| 37 | + expect(body["reasoning_effort"]).toBe(effort); |
| 38 | + expect(body["chat_template_kwargs"]).toEqual({ thinking: true }); |
| 39 | + }, |
| 40 | + ); |
| 41 | + |
| 42 | + test("none → chat_template_kwargs.thinking false and NO reasoning_effort", () => { |
| 43 | + const body = bodyFor({ |
| 44 | + providerOptions: { reasoning_effort: "none" }, |
| 45 | + } as InferenceOptions); |
| 46 | + expect(body["chat_template_kwargs"]).toEqual({ thinking: false }); |
| 47 | + expect("reasoning_effort" in body).toBe(false); |
| 48 | + }); |
| 49 | + |
| 50 | + test("off-ladder effort is left untouched (no high→xhigh coercion)", () => { |
| 51 | + const body = bodyFor({ |
| 52 | + providerOptions: { reasoning_effort: "high" }, |
| 53 | + } as InferenceOptions); |
| 54 | + expect(body["reasoning_effort"]).toBe("high"); |
| 55 | + expect("chat_template_kwargs" in body).toBe(false); |
| 56 | + }); |
| 57 | + |
| 58 | + test("unset effort does not inject empty chat_template_kwargs", () => { |
| 59 | + const body = bodyFor({} as InferenceOptions); |
| 60 | + expect("chat_template_kwargs" in body).toBe(false); |
| 61 | + expect(body["reasoning_effort"]).toBeUndefined(); |
| 62 | + }); |
| 63 | +}); |
| 64 | + |
| 65 | +describe("deepseek-v4 adapter request settings", () => { |
| 66 | + test("stream_options include_usage is true", () => { |
| 67 | + const body = bodyFor({} as InferenceOptions); |
| 68 | + expect(body["stream_options"]).toEqual({ include_usage: true }); |
| 69 | + }); |
| 70 | + |
| 71 | + test("top_p 0.95 and temperature 1.0 defaulted when absent", () => { |
| 72 | + const body = bodyFor({} as InferenceOptions); |
| 73 | + expect(body["top_p"]).toBe(0.95); |
| 74 | + expect(body["temperature"]).toBe(1.0); |
| 75 | + }); |
| 76 | + |
| 77 | + test("top_p and temperature preserved when providerOptions set them", () => { |
| 78 | + const body = bodyFor({ |
| 79 | + providerOptions: { temperature: 0.7, top_p: 0.9 }, |
| 80 | + } as InferenceOptions); |
| 81 | + expect(body["top_p"]).toBe(0.9); |
| 82 | + expect(body["temperature"]).toBe(0.7); |
| 83 | + }); |
| 84 | + |
| 85 | + test("provider-set stream_options is preserved (absent-only)", () => { |
| 86 | + const body = bodyFor({ |
| 87 | + providerOptions: { stream_options: { include_usage: false } }, |
| 88 | + } as InferenceOptions); |
| 89 | + expect(body["stream_options"]).toEqual({ include_usage: false }); |
| 90 | + }); |
| 91 | + |
| 92 | + test("chat_template_kwargs merge preserves existing keys", () => { |
| 93 | + const body = bodyFor({ |
| 94 | + providerOptions: { |
| 95 | + reasoning_effort: "xhigh", |
| 96 | + chat_template_kwargs: { some: "existing" }, |
| 97 | + }, |
| 98 | + } as InferenceOptions); |
| 99 | + expect(body["chat_template_kwargs"]).toEqual({ |
| 100 | + thinking: true, |
| 101 | + some: "existing", |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + test("provider-set thinking is preserved (absent-only thinking)", () => { |
| 106 | + const noneThenTrue = bodyFor({ |
| 107 | + providerOptions: { |
| 108 | + reasoning_effort: "none", |
| 109 | + chat_template_kwargs: { thinking: true }, |
| 110 | + }, |
| 111 | + } as InferenceOptions); |
| 112 | + expect(noneThenTrue["chat_template_kwargs"]).toEqual({ thinking: true }); |
| 113 | + |
| 114 | + const xhighThenFalse = bodyFor({ |
| 115 | + providerOptions: { |
| 116 | + reasoning_effort: "xhigh", |
| 117 | + chat_template_kwargs: { thinking: false }, |
| 118 | + }, |
| 119 | + } as InferenceOptions); |
| 120 | + expect(xhighThenFalse["chat_template_kwargs"]).toEqual({ thinking: false }); |
| 121 | + }); |
| 122 | +}); |
| 123 | + |
| 124 | +describe("deepseek-v4 adapter inherits base quirks", () => { |
| 125 | + const withThinking: ConversationTurn[] = [ |
| 126 | + { role: "user", content: [{ type: "text", text: "hi" }] }, |
| 127 | + { |
| 128 | + role: "assistant", |
| 129 | + model: "deepseek-v4", |
| 130 | + content: [ |
| 131 | + { type: "thinking", thinking: "ponder" }, |
| 132 | + { type: "text", text: "hello" }, |
| 133 | + ], |
| 134 | + }, |
| 135 | + { role: "user", content: [{ type: "text", text: "again" }] }, |
| 136 | + ] as unknown as ConversationTurn[]; |
| 137 | + |
| 138 | + test("keeps reasoning_content on replayed assistant turns (inherited base)", () => { |
| 139 | + const adapter = createDeepSeekV4Adapter(source); |
| 140 | + const built = adapter.buildRequest( |
| 141 | + withThinking, |
| 142 | + "deepseek-v4", |
| 143 | + {} as InferenceOptions, |
| 144 | + ); |
| 145 | + const assistant = ( |
| 146 | + JSON.parse(built.body) as { messages: Record<string, unknown>[] } |
| 147 | + ).messages.find((m) => m["role"] === "assistant"); |
| 148 | + expect(assistant?.["reasoning_content"]).toBe("ponder"); |
| 149 | + }); |
| 150 | + |
| 151 | + test("vendor-qualified V4 id keeps reasoning_content on replays (org/ prefix)", () => { |
| 152 | + const adapter = createDeepSeekV4Adapter(source); |
| 153 | + const built = adapter.buildRequest( |
| 154 | + withThinking, |
| 155 | + "deepseek-ai/DeepSeek-V4-Flash-0731", |
| 156 | + { |
| 157 | + providerOptions: { reasoning_effort: "xhigh" }, |
| 158 | + } as InferenceOptions, |
| 159 | + ); |
| 160 | + const body = JSON.parse(built.body) as { |
| 161 | + messages: Record<string, unknown>[]; |
| 162 | + reasoning_effort?: unknown; |
| 163 | + }; |
| 164 | + const assistant = body.messages.find((m) => m["role"] === "assistant"); |
| 165 | + expect(assistant?.["reasoning_content"]).toBe("ponder"); |
| 166 | + expect(body.reasoning_effort).toBe("xhigh"); |
| 167 | + }); |
| 168 | + |
| 169 | + test("non-V4 select a generic body build (base short-circuits, no V4 fields)", () => { |
| 170 | + const adapter = createDeepSeekV4Adapter({ |
| 171 | + ...source, |
| 172 | + model: "gpt-5", |
| 173 | + } as typeof source); |
| 174 | + const built = adapter.buildRequest( |
| 175 | + messages, |
| 176 | + "gpt-5", |
| 177 | + {} as InferenceOptions, |
| 178 | + ); |
| 179 | + const body = JSON.parse(built.body) as Record<string, unknown>; |
| 180 | + expect("top_p" in body).toBe(false); |
| 181 | + expect("temperature" in body).toBe(false); |
| 182 | + expect("stream_options" in body).toBe(false); |
| 183 | + expect("chat_template_kwargs" in body).toBe(false); |
| 184 | + expect(body["model"]).toBe("gpt-5"); |
| 185 | + }); |
| 186 | + |
| 187 | + test("keeps reasoning_content for non-DeepSeek models", () => { |
| 188 | + const adapter = createDeepSeekV4Adapter({ |
| 189 | + ...source, |
| 190 | + model: "kimi-k2", |
| 191 | + } as typeof source); |
| 192 | + const built = adapter.buildRequest( |
| 193 | + withThinking, |
| 194 | + "kimi-k2", |
| 195 | + {} as InferenceOptions, |
| 196 | + ); |
| 197 | + const assistant = ( |
| 198 | + JSON.parse(built.body) as { messages: Record<string, unknown>[] } |
| 199 | + ).messages.find((m) => m["role"] === "assistant"); |
| 200 | + expect(assistant?.["reasoning_content"]).toBe("ponder"); |
| 201 | + }); |
| 202 | +}); |
| 203 | + |
| 204 | +describe("deepseek-v4 adapter byte-identical to base for non-V4", () => { |
| 205 | + test("non-V4 body matches the patched generic base byte-for-byte", () => { |
| 206 | + const baseAdapter = createOpenAICompatibleAdapter({ |
| 207 | + ...source, |
| 208 | + model: "gpt-5", |
| 209 | + } as typeof source); |
| 210 | + const baseBuilt = baseAdapter.buildRequest(messages, "gpt-5", { |
| 211 | + providerOptions: { reasoning_effort: "none" }, |
| 212 | + } as InferenceOptions); |
| 213 | + |
| 214 | + const adapter = createDeepSeekV4Adapter({ |
| 215 | + ...source, |
| 216 | + model: "gpt-5", |
| 217 | + } as typeof source); |
| 218 | + const built = adapter.buildRequest(messages, "gpt-5", { |
| 219 | + providerOptions: { reasoning_effort: "none" }, |
| 220 | + } as InferenceOptions); |
| 221 | + |
| 222 | + const baseBody = JSON.parse(baseBuilt.body) as Record<string, unknown>; |
| 223 | + const body = JSON.parse(built.body) as Record<string, unknown>; |
| 224 | + expect(body).toEqual(baseBody); |
| 225 | + }); |
| 226 | +}); |
0 commit comments