|
| 1 | +import { describe, expect, test } from "bun:test" |
| 2 | +import { APICallError } from "ai" |
| 3 | +import { ProviderV2 } from "@opencode-ai/core/provider" |
| 4 | +import { ProviderError } from "@/provider/error" |
| 5 | + |
| 6 | +const deepseekReasoningContent = (statusCode: number) => |
| 7 | + new APICallError({ |
| 8 | + message: `${statusCode} Bad Request`, |
| 9 | + url: "https://api.deepseek.com/v1/chat/completions", |
| 10 | + statusCode, |
| 11 | + responseBody: JSON.stringify({ |
| 12 | + error: { |
| 13 | + message: |
| 14 | + "This model version does not support reasoning_content, please resend all assistant messages that contain reasoning_content", |
| 15 | + }, |
| 16 | + }), |
| 17 | + }) |
| 18 | + |
| 19 | +const parsedApiError = (parsed: ProviderError.ParsedAPICallError) => { |
| 20 | + if (parsed.type !== "api_error") throw new Error("expected api_error") |
| 21 | + return parsed |
| 22 | +} |
| 23 | + |
| 24 | +describe("ProviderError.parseAPICallError", () => { |
| 25 | + test("adds a DeepSeek reasoning_content recovery hint on 400", () => { |
| 26 | + const parsed = parsedApiError( |
| 27 | + ProviderError.parseAPICallError({ |
| 28 | + providerID: ProviderV2.ID.make("deepseek"), |
| 29 | + error: deepseekReasoningContent(400), |
| 30 | + }), |
| 31 | + ) |
| 32 | + |
| 33 | + expect(parsed.message).toContain("DeepSeek requires previous reasoning to be resent verbatim") |
| 34 | + }) |
| 35 | + |
| 36 | + test("does not hint when the DeepSeek error does not mention reasoning_content", () => { |
| 37 | + const error = new APICallError({ |
| 38 | + message: "400 Bad Request", |
| 39 | + url: "https://api.deepseek.com/v1/chat/completions", |
| 40 | + statusCode: 400, |
| 41 | + responseBody: JSON.stringify({ error: { message: "Invalid model" } }), |
| 42 | + }) |
| 43 | + const parsed = parsedApiError( |
| 44 | + ProviderError.parseAPICallError({ |
| 45 | + providerID: ProviderV2.ID.make("deepseek"), |
| 46 | + error, |
| 47 | + }), |
| 48 | + ) |
| 49 | + |
| 50 | + expect(parsed.message).not.toContain("resent verbatim") |
| 51 | + }) |
| 52 | + |
| 53 | + test("does not hint for non-DeepSeek providers", () => { |
| 54 | + const parsed = parsedApiError( |
| 55 | + ProviderError.parseAPICallError({ |
| 56 | + providerID: ProviderV2.ID.make("openai"), |
| 57 | + error: deepseekReasoningContent(400), |
| 58 | + }), |
| 59 | + ) |
| 60 | + |
| 61 | + expect(parsed.message).not.toContain("resent verbatim") |
| 62 | + }) |
| 63 | + |
| 64 | + test("does not crash when the error body message is not a string", () => { |
| 65 | + const error = new APICallError({ |
| 66 | + message: "400 Bad Request", |
| 67 | + url: "https://api.deepseek.com/v1/chat/completions", |
| 68 | + statusCode: 400, |
| 69 | + responseBody: JSON.stringify({ error: { message: { code: "x" } } }), |
| 70 | + }) |
| 71 | + const parsed = parsedApiError( |
| 72 | + ProviderError.parseAPICallError({ |
| 73 | + providerID: ProviderV2.ID.make("deepseek"), |
| 74 | + error, |
| 75 | + }), |
| 76 | + ) |
| 77 | + |
| 78 | + expect(parsed.message).toBe("400 Bad Request") |
| 79 | + }) |
| 80 | +}) |
0 commit comments