ai-chatbot/lib/ai/deepseek-compatible-fetch.ts

22 lines
1.2 KiB
TypeScript

/** Native Agent history does not retain provider-specific reasoning fields.
* DeepSeek defaults to thinking mode, which requires those fields on subsequent
* tool rounds. Use its documented non-thinking tool protocol by default. */
export function isDeepSeekOfficialUrl(value?: string | null) {
try { return new URL(value || "").hostname === "api.deepseek.com"; }
catch { return false; }
}
export function createDeepSeekCompatibleFetch(baseFetch: typeof fetch = fetch): typeof fetch {
return async (input, init) => {
if (typeof init?.body !== "string" || !isDeepSeekOfficialUrl(String(input instanceof Request ? input.url : input)))
return baseFetch(input, init);
let body: Record<string, unknown>;
try { body = JSON.parse(init.body); } catch { return baseFetch(input, init); }
// An explicit thinking configuration belongs to its caller. Agent requests
// use the default non-thinking mode; ordinary callers can opt in explicitly.
if (!body || typeof body !== "object" || Array.isArray(body) || body.thinking !== undefined)
return baseFetch(input, init);
return baseFetch(input, { ...init, body: JSON.stringify({ ...body, thinking: { type: "disabled" } }) });
};
}