|
| 1 | +/** |
| 2 | + * DeepSeek V4 is stateless on the Responses API and requires the caller to replay |
| 3 | + * prior assistant reasoning text on every continuation (issue #875). The passthrough |
| 4 | + * sanitizer must therefore keep `reasoning.content` for models listed in |
| 5 | + * `preserveReasoningContentModels` (seeded for DeepSeek thinking models) while still |
| 6 | + * blanking it for ChatGPT-native passthrough and still stripping proxy-minted ocxr1 |
| 7 | + * envelopes. A routable DeepSeek entry also has to be usable as a sub-agent model. |
| 8 | + */ |
| 9 | +import { afterEach, describe, expect, test } from "bun:test"; |
| 10 | +import { sanitizeReasoningInputContent } from "../src/adapters/openai-responses"; |
| 11 | +import { buildSubagentModelChain, isSubagentModelUnavailable } from "../src/codex/subagent-model-fallback"; |
| 12 | +import { providerConfigSeed } from "../src/providers/derive"; |
| 13 | +import { getProviderRegistryEntry } from "../src/providers/registry"; |
| 14 | +import { OCX_REASONING_PREFIX } from "../src/responses/reasoning-envelope"; |
| 15 | +import { handleResponses } from "../src/server/responses/core"; |
| 16 | +import type { OcxConfig, OcxProviderConfig } from "../src/types"; |
| 17 | +const MODEL = "deepseek-v4-flash"; |
| 18 | + |
| 19 | +const RAW_REASONING = { |
| 20 | + type: "reasoning", |
| 21 | + id: "rs_1", |
| 22 | + summary: [], |
| 23 | + content: [{ type: "reasoning_text", text: "inspect the failing build" }], |
| 24 | +}; |
| 25 | + |
| 26 | +function deepseekProvider(): OcxProviderConfig { |
| 27 | + return { ...providerConfigSeed(getProviderRegistryEntry("deepseek")!), apiKey: "sk-test" }; |
| 28 | +} |
| 29 | + |
| 30 | +describe("sanitizeReasoningInputContent respects replay-capable models", () => { |
| 31 | + test("keeps raw reasoning content for a preserve-listed model", () => { |
| 32 | + const out = sanitizeReasoningInputContent( |
| 33 | + { input: [RAW_REASONING] }, |
| 34 | + { preserveContentModels: ["deepseek-v4-flash"], modelId: MODEL }, |
| 35 | + ) as { input: Array<Record<string, unknown>> }; |
| 36 | + expect(out.input[0].content).toEqual(RAW_REASONING.content); |
| 37 | + }); |
| 38 | + |
| 39 | + test("still blanks raw reasoning content for non-preserving passthrough", () => { |
| 40 | + const out = sanitizeReasoningInputContent( |
| 41 | + { input: [RAW_REASONING] }, |
| 42 | + { modelId: "gpt-5.6" }, |
| 43 | + ) as { input: Array<Record<string, unknown>> }; |
| 44 | + expect(out.input[0].content).toEqual([]); |
| 45 | + }); |
| 46 | + |
| 47 | + test("still strips ocxr1 envelopes while preserving content for DeepSeek", () => { |
| 48 | + const enveloped = { |
| 49 | + ...RAW_REASONING, |
| 50 | + id: "rs_2", |
| 51 | + encrypted_content: `${OCX_REASONING_PREFIX}abc`, |
| 52 | + }; |
| 53 | + const out = sanitizeReasoningInputContent( |
| 54 | + { input: [enveloped] }, |
| 55 | + { preserveContentModels: ["deepseek-v4-flash"], modelId: MODEL }, |
| 56 | + ) as { input: Array<Record<string, unknown>> }; |
| 57 | + expect(out.input[0].content).toEqual(RAW_REASONING.content); |
| 58 | + expect(out.input[0].encrypted_content).toBeUndefined(); |
| 59 | + }); |
| 60 | +}); |
| 61 | + |
| 62 | +describe("a DeepSeek Responses continuation keeps its reasoning replay", () => { |
| 63 | + const originalFetch = globalThis.fetch; |
| 64 | + afterEach(() => { globalThis.fetch = originalFetch; }); |
| 65 | + |
| 66 | + test("a tool-call follow-up carries reasoning content and tool output upstream", async () => { |
| 67 | + let capturedBody: Record<string, unknown> | undefined; |
| 68 | + globalThis.fetch = (async (_input: RequestInfo | URL, init?: RequestInit) => { |
| 69 | + capturedBody = JSON.parse(String(init?.body ?? "{}")) as Record<string, unknown>; |
| 70 | + return Response.json({ |
| 71 | + id: "resp_deepseek_2", |
| 72 | + object: "response", |
| 73 | + status: "completed", |
| 74 | + output: [], |
| 75 | + }); |
| 76 | + }) as typeof fetch; |
| 77 | + |
| 78 | + const config = { providers: { deepseek: deepseekProvider() } } as unknown as OcxConfig; |
| 79 | + await handleResponses( |
| 80 | + new Request("http://localhost/v1/responses", { |
| 81 | + method: "POST", |
| 82 | + headers: { "content-type": "application/json" }, |
| 83 | + body: JSON.stringify({ |
| 84 | + model: MODEL, |
| 85 | + stream: false, |
| 86 | + store: false, |
| 87 | + input: [ |
| 88 | + RAW_REASONING, |
| 89 | + { type: "function_call", id: "fc_1", call_id: "call_1", name: "exec_command", arguments: "{}" }, |
| 90 | + { type: "function_call_output", call_id: "call_1", output: "build failed" }, |
| 91 | + ], |
| 92 | + }), |
| 93 | + }), |
| 94 | + config, |
| 95 | + { model: "", provider: "" }, |
| 96 | + ); |
| 97 | + |
| 98 | + const input = capturedBody!.input as unknown[]; |
| 99 | + expect(input).toEqual(expect.arrayContaining([ |
| 100 | + expect.objectContaining({ type: "reasoning", content: RAW_REASONING.content }), |
| 101 | + expect.objectContaining({ type: "function_call_output", call_id: "call_1" }), |
| 102 | + ])); |
| 103 | + }); |
| 104 | +}); |
| 105 | + |
| 106 | +describe("DeepSeek V4 Flash is a viable sub-agent candidate", () => { |
| 107 | + test("it is routable and sits first in a sub-agent fallback chain", () => { |
| 108 | + const config = { |
| 109 | + defaultProvider: "deepseek", |
| 110 | + providers: { deepseek: deepseekProvider() }, |
| 111 | + } as unknown as OcxConfig; |
| 112 | + const chain = buildSubagentModelChain("deepseek/deepseek-v4-flash", config); |
| 113 | + expect(chain[0]).toBe("deepseek/deepseek-v4-flash"); |
| 114 | + expect(isSubagentModelUnavailable("deepseek/deepseek-v4-flash", config)).toBe(false); |
| 115 | + }); |
| 116 | +}); |
0 commit comments