fix: honor registered DeepSeek endpoint

This commit is contained in:
冰朔 2026-08-05 11:14:10 +08:00
commit 380fca2b9f
2 changed files with 24 additions and 3 deletions

View file

@ -1,5 +1,11 @@
const DEFAULT_TIMEOUT_MS = 45_000;
function completionEndpoint(apiUrl) {
const value = String(apiUrl || "").replace(/\/+$/, "");
if (/\/chat\/completions$/i.test(value)) return value;
return `${value}/chat/completions`;
}
function extractJson(text) {
const value = String(text || "").trim();
if (!value) throw new Error("model_response_empty");
@ -36,7 +42,7 @@ export class DeepSeekJsonClient {
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), this.timeoutMs);
try {
const response = await this.fetchImpl(`${this.apiUrl}/chat/completions`, {
const response = await this.fetchImpl(completionEndpoint(this.apiUrl), {
method: "POST",
headers: {
authorization: `Bearer ${this.apiKey}`,
@ -78,4 +84,4 @@ export class DeepSeekJsonClient {
}
}
export { extractJson };
export { completionEndpoint, extractJson };

View file

@ -1,12 +1,27 @@
import assert from "node:assert/strict";
import test from "node:test";
import { DeepSeekJsonClient, extractJson } from "./model-client.mjs";
import {
DeepSeekJsonClient,
completionEndpoint,
extractJson,
} from "./model-client.mjs";
test("extractJson accepts strict JSON and fenced JSON", () => {
assert.deepEqual(extractJson('{"ok":true}'), { ok: true });
assert.deepEqual(extractJson('```json\n{"ok":true}\n```'), { ok: true });
});
test("completionEndpoint accepts either a base URL or a complete registered endpoint", () => {
assert.equal(
completionEndpoint("https://api.deepseek.com/v1"),
"https://api.deepseek.com/v1/chat/completions",
);
assert.equal(
completionEndpoint("https://api.deepseek.com/chat/completions"),
"https://api.deepseek.com/chat/completions",
);
});
test("DeepSeek client sends the secret only as authorization and returns JSON", async () => {
let observed;
const client = new DeepSeekJsonClient({