First revision: DeepSeek, numeric questions, background assistant, UI fixes
All checks were successful
Verify and publish V2 container / build-and-push (push) Successful in 1m2s
All checks were successful
Verify and publish V2 container / build-and-push (push) Successful in 1m2s
Providers and pricing - Add a DeepSeek provider: thinking mode with low/high/max effort, reasoning_content replayed between tool steps, cache-hit usage, /models discovery and catalog prices. - Fix "does not support tool calling": a rejected forced tool_choice (DeepSeek thinking mode) now asks for the call in the prompt; only a genuine no-tools 400 is "unsupported". - Seven reasoning-effort levels (Off to Max) mapped per adapter. - Cached-input price per model, cache-write and DeepSeek off-peak aware cost estimates. - Built-in bge-small embedding model (transformers.js) as the semantic search default. Quizzes - Numeric (type-the-answer) questions: units ignored, optional ± tolerance; editor, runner, importer, templates and generation prompts. - Quiz start screen instead of showing answers; navigator ring no longer clipped and key legend removed; clock only for timed exams; 7-point letter grades. Assistant and generation - Assistant turns run on the server independently of the browser, with re-attach and an explicit stop; drafts tray shows generations in progress. - Per-chat model switcher, @-tagging of class materials, full-width assistant page and a smaller assistant button. - Editable generation instructions and built-in presets, separate from the chatbot import instructions. Other - Dialog footer padding, keyboard hints removed, copy buttons work over plain HTTP, class and folder shares include documents. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
ccdad9728b
commit
f0aabe4d5d
63 changed files with 3892 additions and 488 deletions
|
|
@ -38,7 +38,7 @@ import {
|
|||
type TestResult,
|
||||
} from '../types.ts';
|
||||
import { lookupModelDefaults } from './catalog.ts';
|
||||
import { isChatModelId, makeUsage, num, parseArgsStrict, parseToolArguments, runTest, str, TEST_TIMEOUT_MS, withCatalogDefaults } from './common.ts';
|
||||
import { isChatModelId, makeUsage, nearestEffort, num, parseArgsStrict, parseToolArguments, runTest, str, TEST_TIMEOUT_MS, withCatalogDefaults } from './common.ts';
|
||||
import { ensureOk, errorFromProvider, httpRequest, isRecord, requestJson, sseJson, trimBaseUrl, type ErrorContext } from './http.ts';
|
||||
|
||||
type Json = Record<string, unknown>;
|
||||
|
|
@ -120,7 +120,10 @@ export function claudeProfile(model: string): ClaudeProfile {
|
|||
}
|
||||
|
||||
/** Thinking budgets for the legacy `budget_tokens` models. */
|
||||
const LEGACY_BUDGET = { low: 2_048, medium: 8_192, high: 16_384 } as const;
|
||||
const LEGACY_BUDGET = { minimal: 1_024, low: 2_048, medium: 8_192, high: 16_384, xhigh: 24_576, max: 32_000 } as const;
|
||||
|
||||
/** `output_config.effort` levels. "xhigh" exists on the newest models only; a rejection steps it down. */
|
||||
const CLAUDE_EFFORTS = ['low', 'medium', 'high', 'xhigh', 'max'] as const;
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* Request building */
|
||||
|
|
@ -133,6 +136,8 @@ interface BuildOptions {
|
|||
useFallbacks: boolean;
|
||||
eagerToolInput: boolean;
|
||||
promptCache: boolean;
|
||||
/** Highest effort level the model accepted, after a rejection. */
|
||||
effortCap?: (typeof CLAUDE_EFFORTS)[number];
|
||||
}
|
||||
|
||||
/** Tool-call ids must match ^[a-zA-Z0-9_-]+$; ids minted by other providers may not. */
|
||||
|
|
@ -268,7 +273,7 @@ function buildBody(req: ChatRequest, profile: ClaudeProfile, opts: BuildOptions)
|
|||
// `display: "summarized"` because these models hide the reasoning text by
|
||||
// default, which would look like a long pause in the UI.
|
||||
body.thinking = adaptive;
|
||||
if (effort) outputConfig.effort = effort;
|
||||
if (effort) outputConfig.effort = opts.effortCap ? nearestEffort(effort, CLAUDE_EFFORTS.filter((e) => CLAUDE_EFFORTS.indexOf(e) <= CLAUDE_EFFORTS.indexOf(opts.effortCap!))) : nearestEffort(effort, CLAUDE_EFFORTS);
|
||||
} else if (effort) {
|
||||
// Forced tool use cannot be combined with thinking on the older generations.
|
||||
if (profile.thinking === 'opt_in' && !forcing) body.thinking = adaptive;
|
||||
|
|
@ -276,7 +281,7 @@ function buildBody(req: ChatRequest, profile: ClaudeProfile, opts: BuildOptions)
|
|||
const budget = Math.min(LEGACY_BUDGET[effort], maxTokens - 1_024); // must be < max_tokens, min 1024
|
||||
if (budget >= 1_024) body.thinking = { type: 'enabled', budget_tokens: budget };
|
||||
}
|
||||
if (profile.effort) outputConfig.effort = effort;
|
||||
if (profile.effort) outputConfig.effort = opts.effortCap ? nearestEffort(effort, CLAUDE_EFFORTS.filter((e) => CLAUDE_EFFORTS.indexOf(e) <= CLAUDE_EFFORTS.indexOf(opts.effortCap!))) : nearestEffort(effort, CLAUDE_EFFORTS);
|
||||
}
|
||||
if (Object.keys(outputConfig).length > 0) body.output_config = outputConfig;
|
||||
}
|
||||
|
|
@ -375,6 +380,7 @@ async function* stream(cfg: ProviderConfig, req: ChatRequest): AsyncGenerator<St
|
|||
else if (opts.useFallbacks && /fallback|anthropic-beta/i.test(detail)) opts.useFallbacks = false;
|
||||
else if (opts.eagerToolInput && /eager_input_streaming/i.test(detail)) opts.eagerToolInput = false;
|
||||
else if (opts.promptCache && /cache_control/i.test(detail)) opts.promptCache = false;
|
||||
else if (/effort/i.test(detail) && opts.effortCap !== 'medium' && req.reasoningEffort && req.reasoningEffort !== 'none') opts.effortCap = opts.effortCap === 'high' ? 'medium' : opts.effortCap === 'xhigh' ? 'high' : 'xhigh';
|
||||
else throw err;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -129,10 +129,28 @@ const GEMINI: Record<string, Entry> = {
|
|||
'gemini-2.5-flash-lite': { label: 'Gemini 2.5 Flash-Lite', priceIn: 0.1, priceOut: 0.4, capabilities: GEMINI_CAPS },
|
||||
};
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* DeepSeek */
|
||||
/* Prices and limits: https://api-docs.deepseek.com/quick_start/pricing (fetched 2026-11-15)
|
||||
/* Peak-hour list prices are stored. Off-peak hours (everything outside 01:00-04:00 and
|
||||
/* 06:00-10:00 UTC, Monday to Friday) cost half; `priceMultiplier` applies that per call.
|
||||
/* deepseek-v4-flash / deepseek-v4-flash-vision-exp are retired aliases served by
|
||||
/* deepseek-flash at the Flash price.
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
const DS_LIMITS = { contextWindow: 1_048_576, maxOutput: 393_216 };
|
||||
|
||||
const DEEPSEEK: Record<string, Entry> = {
|
||||
'deepseek-flash': { label: 'DeepSeek V4.1 Flash', ...DS_LIMITS, priceIn: 0.3, priceCachedIn: 0.006, priceOut: 1.2, capabilities: { tools: true, vision: true, pdf: false, reasoning: true, temperature: true } },
|
||||
'deepseek-v4-flash': { label: 'DeepSeek V4 Flash (alias of Flash)', ...DS_LIMITS, priceIn: 0.3, priceCachedIn: 0.006, priceOut: 1.2, capabilities: { tools: true, vision: true, pdf: false, reasoning: true, temperature: true } },
|
||||
'deepseek-v4-pro': { label: 'DeepSeek V4 Pro', ...DS_LIMITS, priceIn: 1.32, priceCachedIn: 0.044, priceOut: 3.96, capabilities: { tools: true, vision: false, pdf: false, reasoning: true, temperature: true } },
|
||||
};
|
||||
|
||||
const TABLES: Record<ProviderKind, Record<string, Entry>> = {
|
||||
openai: OPENAI,
|
||||
anthropic: ANTHROPIC,
|
||||
gemini: GEMINI,
|
||||
deepseek: DEEPSEEK,
|
||||
// Aggregators and local servers host arbitrary models at their own prices.
|
||||
openai_compatible: {},
|
||||
ollama: {},
|
||||
|
|
@ -168,6 +186,7 @@ function lookupIn(table: Record<string, Entry>, id: string): Partial<DiscoveredM
|
|||
// capabilities are a fair guess, the price is not.
|
||||
delete out.priceIn;
|
||||
delete out.priceOut;
|
||||
delete out.priceCachedIn;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
|
@ -185,11 +204,12 @@ export function lookupModelDefaults(kind: ProviderKind, modelId: string): Partia
|
|||
// Routers name models "<vendor>/<model>" (`anthropic/claude-sonnet-5-5`).
|
||||
// Borrow capabilities and limits from the vendor's table, never its prices.
|
||||
const bare = id.slice(id.lastIndexOf('/') + 1).replace(/:[a-z]+$/, '');
|
||||
for (const table of [OPENAI, ANTHROPIC, GEMINI]) {
|
||||
for (const table of [OPENAI, ANTHROPIC, GEMINI, DEEPSEEK]) {
|
||||
const hit = lookupIn(table, bare);
|
||||
if (Object.keys(hit).length > 0) {
|
||||
delete hit.priceIn;
|
||||
delete hit.priceOut;
|
||||
delete hit.priceCachedIn;
|
||||
// Temperature limits are properties of the first-party APIs, not of the router.
|
||||
if (hit.capabilities) delete hit.capabilities.temperature;
|
||||
return hit;
|
||||
|
|
@ -204,3 +224,51 @@ export function lookupModelDefaults(kind: ProviderKind, modelId: string): Partia
|
|||
export function catalogModelIds(kind: ProviderKind): string[] {
|
||||
return Object.keys(TABLES[kind]);
|
||||
}
|
||||
|
||||
/**
|
||||
* What a cache-hit input token costs when a model has no cached price of its own, as a
|
||||
* fraction of the normal input price. These are the providers' published discounts for
|
||||
* their current models: OpenAI GPT-5 and later, Claude cache reads and Gemini 2.5 and later
|
||||
* all bill cached input at 10%. Older OpenAI models (GPT-4.1 25%, GPT-4o / o-series 50%)
|
||||
* discount less. Servers behind the compatible adapter and Ollama have no known discount.
|
||||
*/
|
||||
export function cachedInputRatio(kind: ProviderKind, modelId: string): number {
|
||||
const id = modelId.slice(modelId.lastIndexOf('/') + 1);
|
||||
switch (kind) {
|
||||
case 'openai':
|
||||
if (/^gpt-4\.1/.test(id)) return 0.25;
|
||||
if (/^(gpt-4o|o\d)/.test(id)) return 0.5;
|
||||
return 0.1;
|
||||
case 'anthropic':
|
||||
case 'gemini':
|
||||
return 0.1;
|
||||
case 'deepseek':
|
||||
return 0.02;
|
||||
default:
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/** USD per million cached input tokens: the model's own price when set, otherwise the provider discount. */
|
||||
export function cachedInputPrice(kind: ProviderKind, modelId: string, priceIn: number | null, priceCachedIn: number | null): number {
|
||||
if (priceCachedIn !== null && priceCachedIn !== undefined) return priceCachedIn;
|
||||
return (priceIn ?? 0) * cachedInputRatio(kind, modelId);
|
||||
}
|
||||
|
||||
/** Cache writes: Claude bills writing the (5-minute) prompt cache at 1.25x the input price. */
|
||||
export function cacheWriteRatio(kind: ProviderKind): number {
|
||||
return kind === 'anthropic' ? 1.25 : 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Time-of-day pricing. DeepSeek charges half its list price outside peak hours
|
||||
* (01:00-04:00 and 06:00-10:00 UTC, Monday to Friday).
|
||||
*/
|
||||
export function priceMultiplier(kind: ProviderKind, at: number = Date.now()): number {
|
||||
if (kind !== 'deepseek') return 1;
|
||||
const d = new Date(at);
|
||||
const day = d.getUTCDay();
|
||||
const hour = d.getUTCHours();
|
||||
const peak = day >= 1 && day <= 5 && ((hour >= 1 && hour < 4) || (hour >= 6 && hour < 10));
|
||||
return peak ? 1 : 0.5;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@
|
|||
* connection-test wrapper.
|
||||
*/
|
||||
import { jsonrepair } from 'jsonrepair';
|
||||
import { AIError, type DiscoveredModel, type ProviderKind, type TestResult, type Usage } from '../types.ts';
|
||||
import { AIError, REASONING_EFFORTS, type DiscoveredModel, type ProviderKind, type ReasoningEffort, type TestResult, type Usage } from '../types.ts';
|
||||
import { lookupModelDefaults } from './catalog.ts';
|
||||
import { isRecord } from './http.ts';
|
||||
|
||||
|
|
@ -151,3 +151,22 @@ export async function runTest(label: string, check: () => Promise<{ modelCount?:
|
|||
|
||||
/** Timeout used by `test()` so a dead endpoint fails fast instead of hanging the settings page. */
|
||||
export const TEST_TIMEOUT_MS = 20_000;
|
||||
|
||||
/**
|
||||
* Maps a requested effort onto the closest level a provider accepts. Ties go to the higher
|
||||
* level, so "medium" on a low/high-only API becomes "high" rather than silently weaker.
|
||||
*/
|
||||
export function nearestEffort<T extends string>(effort: ReasoningEffort, allowed: readonly T[]): T {
|
||||
const rank = (e: string) => REASONING_EFFORTS.indexOf(e as ReasoningEffort);
|
||||
const want = rank(effort);
|
||||
let best = allowed[0];
|
||||
let bestDistance = Number.POSITIVE_INFINITY;
|
||||
for (const level of allowed) {
|
||||
const d = Math.abs(rank(level) - want);
|
||||
if (d < bestDistance || (d === bestDistance && rank(level) > rank(best))) {
|
||||
best = level;
|
||||
bestDistance = d;
|
||||
}
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
|
|
|||
112
server/ai/providers/deepseek.ts
Normal file
112
server/ai/providers/deepseek.ts
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
/**
|
||||
* DeepSeek — Chat Completions at https://api.deepseek.com.
|
||||
* https://api-docs.deepseek.com/api/create-chat-completion/
|
||||
* https://api-docs.deepseek.com/guides/thinking_mode/
|
||||
*
|
||||
* The wire format is OpenAI-compatible, so this reuses the compatible adapter's engine with
|
||||
* the differences DeepSeek documents:
|
||||
* - Thinking is ON by default. `thinking: {type: "disabled"}` turns it off;
|
||||
* `reasoning_effort` accepts none / low / high / max.
|
||||
* - In thinking mode `tool_choice: "required"` and named tool choices are rejected with a
|
||||
* 400. Structured generation keeps thinking on (answer keys are noticeably more accurate
|
||||
* with it) and asks for the tool call in the prompt with `tool_choice: "auto"`. Only when
|
||||
* the effort is set to "none" is thinking off, and then the tool choice is forced.
|
||||
* - When tools are present, the `reasoning_content` of every earlier assistant turn must be
|
||||
* sent back, or the API answers 400. It is kept in `providerData` and replayed.
|
||||
* - Usage reports `prompt_cache_hit_tokens`; cache hits are billed at a much lower rate.
|
||||
* - `/models` lists context window, output limit, input modalities and effort levels.
|
||||
*/
|
||||
import { AIError, type ChatRequest, type DiscoveredModel, type ModelCapabilities, type ProviderAdapter, type ProviderConfig, type TestResult } from '../types.ts';
|
||||
import { isChatModelId, nearestEffort, num, runTest, str, TEST_TIMEOUT_MS, withCatalogDefaults } from './common.ts';
|
||||
import { isRecord, requestJson, trimBaseUrl } from './http.ts';
|
||||
import { streamChatCompletions, type CompatOptions } from './openaiCompatible.ts';
|
||||
|
||||
export const DEEPSEEK_BASE_URL = 'https://api.deepseek.com';
|
||||
const LABEL = 'DeepSeek';
|
||||
const EFFORTS = ['low', 'high', 'max'] as const;
|
||||
|
||||
function base(cfg: ProviderConfig): string {
|
||||
return trimBaseUrl(cfg.baseUrl?.trim() || DEEPSEEK_BASE_URL);
|
||||
}
|
||||
|
||||
/** "none" turns thinking off; a model marked as not reasoning is treated the same way. */
|
||||
function effortOf(req: ChatRequest): ChatRequest['reasoningEffort'] {
|
||||
return req.capabilities?.reasoning === false ? 'none' : req.reasoningEffort;
|
||||
}
|
||||
|
||||
function options(cfg: ProviderConfig): CompatOptions {
|
||||
if (!cfg.apiKey) throw new AIError('not_configured', `Add your DeepSeek API key for "${cfg.name}" in Settings.`);
|
||||
return {
|
||||
kind: 'deepseek',
|
||||
label: cfg.name || LABEL,
|
||||
// DeepSeek serves the same API at the root and under /v1; the explicit /v1 keeps the
|
||||
// compatible engine from guessing.
|
||||
baseUrl: `${base(cfg)}/v1`,
|
||||
apiKey: cfg.apiKey,
|
||||
headers: cfg.headers,
|
||||
replayReasoning: true,
|
||||
forcedToolChoice: (req) => effortOf(req) === 'none',
|
||||
adjustBody: (body, req) => {
|
||||
const effort = effortOf(req);
|
||||
delete body.reasoning_effort;
|
||||
if (effort === 'none' || body.tool_choice === 'required' || isRecord(body.tool_choice)) {
|
||||
// Non-thinking mode (also the only mode that accepts a forced tool choice).
|
||||
body.thinking = { type: 'disabled' };
|
||||
} else {
|
||||
body.thinking = { type: 'enabled' };
|
||||
if (effort) body.reasoning_effort = nearestEffort(effort, EFFORTS);
|
||||
// Sampling parameters have no effect in thinking mode; leave them out.
|
||||
delete body.temperature;
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async function listModels(cfg: ProviderConfig, signal?: AbortSignal, timeoutMs?: number): Promise<DiscoveredModel[]> {
|
||||
const o = options(cfg);
|
||||
const res = await requestJson(`${base(cfg)}/models`, {
|
||||
provider: o.label,
|
||||
secrets: [o.apiKey, ...Object.values(o.headers ?? {})],
|
||||
headers: { ...o.headers, Authorization: `Bearer ${o.apiKey}` },
|
||||
signal,
|
||||
firstByteTimeoutMs: timeoutMs,
|
||||
totalTimeoutMs: timeoutMs,
|
||||
});
|
||||
const list = isRecord(res) && Array.isArray(res.data) ? res.data : [];
|
||||
const found: DiscoveredModel[] = [];
|
||||
for (const m of list) {
|
||||
if (!isRecord(m)) continue;
|
||||
const id = str(m.id);
|
||||
if (!id || !isChatModelId(id)) continue;
|
||||
const model: DiscoveredModel = { id };
|
||||
if (str(m.name) && str(m.name) !== id) model.label = str(m.name);
|
||||
if (num(m.context_window)) model.contextWindow = num(m.context_window);
|
||||
if (num(m.max_output_tokens)) model.maxOutput = num(m.max_output_tokens);
|
||||
const caps: Partial<ModelCapabilities> = { tools: true, pdf: false };
|
||||
if (Array.isArray(m.input_modalities)) caps.vision = m.input_modalities.includes('image');
|
||||
if (isRecord(m.effort)) caps.reasoning = true;
|
||||
model.capabilities = caps;
|
||||
found.push(model);
|
||||
}
|
||||
return withCatalogDefaults('deepseek', found);
|
||||
}
|
||||
|
||||
async function test(cfg: ProviderConfig): Promise<TestResult> {
|
||||
return runTest(cfg.name || LABEL, async () => ({ modelCount: (await listModels(cfg, undefined, TEST_TIMEOUT_MS)).length }));
|
||||
}
|
||||
|
||||
export const deepseekAdapter: ProviderAdapter = {
|
||||
kind: 'deepseek',
|
||||
meta: {
|
||||
label: 'DeepSeek',
|
||||
defaultBaseUrl: DEEPSEEK_BASE_URL,
|
||||
requiresApiKey: true,
|
||||
requiresBaseUrl: false,
|
||||
supportsDiscovery: true,
|
||||
docsUrl: 'https://platform.deepseek.com/api_keys',
|
||||
keyHint: 'Starts with sk-',
|
||||
},
|
||||
stream: (cfg, req) => streamChatCompletions(options(cfg), req),
|
||||
listModels: (cfg, signal) => listModels(cfg, signal),
|
||||
test,
|
||||
};
|
||||
|
|
@ -29,7 +29,7 @@ import { type EmbedRequest, type EmbedResult,
|
|||
type StreamEvent,
|
||||
type TestResult,
|
||||
} from '../types.ts';
|
||||
import { isChatModelId, makeUsage, newToolCallId, num, runTest, str, TEST_TIMEOUT_MS, withCatalogDefaults } from './common.ts';
|
||||
import { isChatModelId, makeUsage, nearestEffort, newToolCallId, num, runTest, str, TEST_TIMEOUT_MS, withCatalogDefaults } from './common.ts';
|
||||
import { ensureOk, errorFromProvider, httpRequest, isRecord, requestJson, sseJson, trimBaseUrl, type ErrorContext, type HttpResponse } from './http.ts';
|
||||
|
||||
type Json = Record<string, unknown>;
|
||||
|
|
@ -140,7 +140,8 @@ function thinkingStyleFor(model: string): ThinkingStyle {
|
|||
return 'off';
|
||||
}
|
||||
|
||||
const BUDGETS = { none: 0, low: 2_048, medium: 8_192, high: 24_576 } as const;
|
||||
const BUDGETS = { none: 0, minimal: 512, low: 2_048, medium: 8_192, high: 24_576, xhigh: 32_768, max: 32_768 } as const;
|
||||
const GEMINI_LEVELS = ['minimal', 'low', 'medium', 'high'] as const;
|
||||
|
||||
function buildBody(req: ChatRequest, thinking: ThinkingStyle, jsonMode: boolean): Json {
|
||||
const caps = req.capabilities ?? {};
|
||||
|
|
@ -165,7 +166,7 @@ function buildBody(req: ChatRequest, thinking: ThinkingStyle, jsonMode: boolean)
|
|||
const effort = req.reasoningEffort;
|
||||
if (thinking === 'level') {
|
||||
const cfg: Json = { includeThoughts: true };
|
||||
if (effort) cfg.thinkingLevel = effort === 'none' ? 'minimal' : effort;
|
||||
if (effort) cfg.thinkingLevel = nearestEffort(effort, GEMINI_LEVELS);
|
||||
gen.thinkingConfig = cfg;
|
||||
} else if (effort) {
|
||||
// 2.5 Pro cannot switch thinking off; its minimum budget is 128 tokens.
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import { AIError, type ProviderAdapter, type ProviderKind } from '../types.ts';
|
||||
import { anthropicAdapter } from './anthropic.ts';
|
||||
import { deepseekAdapter } from './deepseek.ts';
|
||||
import { geminiAdapter } from './gemini.ts';
|
||||
import { ollamaAdapter } from './ollama.ts';
|
||||
import { openaiAdapter } from './openai.ts';
|
||||
|
|
@ -9,6 +10,7 @@ const ADAPTERS: Record<ProviderKind, ProviderAdapter> = {
|
|||
openai: openaiAdapter,
|
||||
anthropic: anthropicAdapter,
|
||||
gemini: geminiAdapter,
|
||||
deepseek: deepseekAdapter,
|
||||
openai_compatible: openaiCompatibleAdapter,
|
||||
ollama: ollamaAdapter,
|
||||
};
|
||||
|
|
@ -23,4 +25,4 @@ export function listAdapterMeta(): Array<{ kind: ProviderKind } & ProviderAdapte
|
|||
return (Object.keys(ADAPTERS) as ProviderKind[]).map((kind) => ({ kind, ...ADAPTERS[kind].meta }));
|
||||
}
|
||||
|
||||
export { CATALOG_UPDATED, lookupModelDefaults } from './catalog.ts';
|
||||
export { CATALOG_UPDATED, cachedInputPrice, cacheWriteRatio, lookupModelDefaults, priceMultiplier } from './catalog.ts';
|
||||
|
|
|
|||
|
|
@ -103,6 +103,9 @@ function toInput(messages: ChatMessage[], caps: Partial<ModelCapabilities>, drop
|
|||
return input;
|
||||
}
|
||||
|
||||
/** The next value to try when a model rejects a reasoning effort. */
|
||||
const OPENAI_EFFORT_FALLBACK: Record<string, string | null> = { none: 'minimal', minimal: 'low', low: null, medium: null, high: null, xhigh: 'high' };
|
||||
|
||||
interface Flags {
|
||||
/** Value sent as reasoning.effort, or null to omit the field. */
|
||||
effort: string | null;
|
||||
|
|
@ -139,8 +142,9 @@ function buildBody(req: ChatRequest, flags: Flags): Json {
|
|||
async function* stream(cfg: ProviderConfig, req: ChatRequest): AsyncGenerator<StreamEvent> {
|
||||
const ctx = errorContext(cfg);
|
||||
const flags: Flags = {
|
||||
// "none" is only understood by recent models; if it is rejected we fall back to "low".
|
||||
effort: req.reasoningEffort ?? null,
|
||||
// OpenAI's top level is "xhigh". "none" and "xhigh" are only understood by recent
|
||||
// models; a rejected value steps towards "medium" and is finally omitted.
|
||||
effort: req.reasoningEffort ? (req.reasoningEffort === 'max' ? 'xhigh' : req.reasoningEffort) : null,
|
||||
summary: true,
|
||||
temperature: true,
|
||||
dropReasoning: false,
|
||||
|
|
@ -156,7 +160,7 @@ async function* stream(cfg: ProviderConfig, req: ChatRequest): AsyncGenerator<St
|
|||
} catch (err) {
|
||||
if (!(err instanceof AIError) || err.status !== 400 || attempt >= 5) throw err;
|
||||
const detail = (err.providerMessage ?? '').toLowerCase();
|
||||
if (flags.effort && /reasoning\.effort|reasoning_effort|effort/.test(detail)) flags.effort = flags.effort === 'none' ? 'minimal' : flags.effort === 'minimal' ? 'low' : null;
|
||||
if (flags.effort && /reasoning\.effort|reasoning_effort|effort/.test(detail)) flags.effort = OPENAI_EFFORT_FALLBACK[flags.effort] ?? null;
|
||||
else if (flags.summary && /summary/.test(detail)) flags.summary = false;
|
||||
else if (flags.include && /include|encrypted_content/.test(detail)) flags.include = false;
|
||||
else if (flags.temperature && /temperature/.test(detail)) flags.temperature = false;
|
||||
|
|
|
|||
|
|
@ -27,10 +27,10 @@ import { type EmbedRequest, type EmbedResult,
|
|||
type StreamEvent,
|
||||
type TestResult,
|
||||
} from '../types.ts';
|
||||
import { parseEmbeddings, dataUrl, isChatModelId, makeUsage, newToolCallId, num, parseArgsStrict, parseToolArguments, runTest, str, TEST_TIMEOUT_MS, withCatalogDefaults } from './common.ts';
|
||||
import { parseEmbeddings, dataUrl, isChatModelId, makeUsage, nearestEffort, newToolCallId, num, parseArgsStrict, parseToolArguments, runTest, str, TEST_TIMEOUT_MS, withCatalogDefaults } from './common.ts';
|
||||
import { ensureOk, errorFromProvider, hostOf, httpRequest, isLocalUrl, isRecord, requestJson, sseJson, trimBaseUrl, type ErrorContext, type HttpResponse } from './http.ts';
|
||||
|
||||
type Json = Record<string, unknown>;
|
||||
export type Json = Record<string, unknown>;
|
||||
|
||||
/** Accepts ".../v1", ".../v1/", ".../v1/chat/completions" or a bare host and returns the API root. */
|
||||
export function normalizeCompatBase(url: string): string {
|
||||
|
|
@ -51,8 +51,26 @@ export interface CompatOptions {
|
|||
baseUrl: string;
|
||||
apiKey: string | null;
|
||||
headers?: Record<string, string>;
|
||||
/**
|
||||
* Server-specific request tweaks (DeepSeek's `thinking` switch, its effort levels...).
|
||||
* Called on the finished body before it is sent.
|
||||
*/
|
||||
adjustBody?: (body: Json, req: ChatRequest) => void;
|
||||
/**
|
||||
* The server requires the reasoning text of earlier assistant turns to be sent back
|
||||
* (`reasoning_content`), as DeepSeek does in thinking mode with tools.
|
||||
*/
|
||||
replayReasoning?: boolean;
|
||||
/**
|
||||
* Whether `required` / named tool choices may be sent for this request. When false the
|
||||
* tool stays available with `auto` and the system prompt asks for the call instead.
|
||||
*/
|
||||
forcedToolChoice?: (req: ChatRequest) => boolean;
|
||||
}
|
||||
|
||||
/** Levels sent as `reasoning_effort` by default. "max" has no OpenAI-style equivalent. */
|
||||
const COMPAT_EFFORTS = ['minimal', 'low', 'medium', 'high', 'xhigh'] as const;
|
||||
|
||||
function userContent(parts: ContentPart[], caps: Partial<ModelCapabilities>): string | Json[] {
|
||||
const blocks: Json[] = [];
|
||||
for (const p of parts) {
|
||||
|
|
@ -72,7 +90,7 @@ function userContent(parts: ContentPart[], caps: Partial<ModelCapabilities>): st
|
|||
return blocks;
|
||||
}
|
||||
|
||||
export function toChatMessages(req: ChatRequest, systemRole: 'system' | 'developer' = 'system'): Json[] {
|
||||
export function toChatMessages(req: ChatRequest, systemRole: 'system' | 'developer' = 'system', replay?: { kind: ProviderKind }): Json[] {
|
||||
const caps = req.capabilities ?? {};
|
||||
const out: Json[] = [];
|
||||
if (req.system?.trim()) out.push({ role: systemRole, content: req.system.trim() });
|
||||
|
|
@ -82,6 +100,12 @@ export function toChatMessages(req: ChatRequest, systemRole: 'system' | 'develop
|
|||
const calls = msg.content.filter((p) => p.type === 'tool_call') as Extract<ContentPart, { type: 'tool_call' }>[];
|
||||
if (!text && calls.length === 0) continue;
|
||||
const wire: Json = { role: 'assistant', content: text || null };
|
||||
if (replay) {
|
||||
// Same server: hand back its reasoning verbatim. A turn written by another provider
|
||||
// has none, which these servers accept as an empty string.
|
||||
const pd = msg.providerData;
|
||||
wire.reasoning_content = pd?.kind === replay.kind && isRecord(pd.data) && typeof pd.data.reasoning_content === 'string' ? pd.data.reasoning_content : '';
|
||||
}
|
||||
if (calls.length > 0) {
|
||||
wire.tool_calls = calls.map((c) => ({ id: c.id, type: 'function', function: { name: c.name, arguments: JSON.stringify(c.input ?? {}) } }));
|
||||
}
|
||||
|
|
@ -103,6 +127,8 @@ export function toChatMessages(req: ChatRequest, systemRole: 'system' | 'develop
|
|||
|
||||
interface BodyFlags {
|
||||
tools: boolean;
|
||||
/** Send `required` / named tool choices as such; false = ask for the call in the prompt instead. */
|
||||
forcedChoice: boolean;
|
||||
usage: boolean;
|
||||
jsonMode: boolean;
|
||||
temperature: boolean;
|
||||
|
|
@ -110,23 +136,40 @@ interface BodyFlags {
|
|||
maxKey: 'max_tokens' | 'max_completion_tokens' | null;
|
||||
}
|
||||
|
||||
function buildBody(req: ChatRequest, flags: BodyFlags): Json {
|
||||
function forcedToolNote(choice: ChatRequest['toolChoice']): string | null {
|
||||
if (choice === 'required') return 'For this turn you must call one of the available tools. Begin your response with the tool call.';
|
||||
if (isRecord(choice)) return `For this turn you must call the \`${choice.name}\` tool. Begin your response with that tool call and do not answer in plain text.`;
|
||||
return null;
|
||||
}
|
||||
|
||||
function buildBody(req: ChatRequest, flags: BodyFlags, o: CompatOptions): Json {
|
||||
const caps = req.capabilities ?? {};
|
||||
const body: Json = { model: req.model, messages: toChatMessages(req), stream: true };
|
||||
const sendTools = flags.tools && caps.tools !== false && Boolean(req.tools?.length);
|
||||
const note = sendTools && !flags.forcedChoice ? forcedToolNote(req.toolChoice) : null;
|
||||
const effective = note ? { ...req, system: [req.system?.trim(), note].filter(Boolean).join('\n\n') } : req;
|
||||
const body: Json = { model: req.model, messages: toChatMessages(effective, 'system', o.replayReasoning ? { kind: o.kind } : undefined), stream: true };
|
||||
if (flags.usage) body.stream_options = { include_usage: true };
|
||||
if (flags.tools && caps.tools !== false && req.tools?.length) {
|
||||
body.tools = req.tools.map((t) => ({ type: 'function', function: { name: t.name, description: t.description, parameters: { type: 'object', ...t.parameters } } }));
|
||||
if (sendTools) {
|
||||
body.tools = req.tools!.map((t) => ({ type: 'function', function: { name: t.name, description: t.description, parameters: { type: 'object', ...t.parameters } } }));
|
||||
const choice = req.toolChoice;
|
||||
if (choice === 'none' || choice === 'required' || choice === 'auto') body.tool_choice = choice;
|
||||
else if (isRecord(choice)) body.tool_choice = { type: 'function', function: { name: choice.name } };
|
||||
if (choice === 'none' || choice === 'auto') body.tool_choice = choice;
|
||||
else if (choice === 'required') body.tool_choice = flags.forcedChoice ? 'required' : 'auto';
|
||||
else if (isRecord(choice)) body.tool_choice = flags.forcedChoice ? { type: 'function', function: { name: choice.name } } : 'auto';
|
||||
}
|
||||
if (flags.temperature && req.temperature !== undefined && caps.temperature !== false) body.temperature = req.temperature;
|
||||
if (flags.maxKey && req.maxOutputTokens) body[flags.maxKey] = req.maxOutputTokens;
|
||||
if (flags.reasoning && caps.reasoning === true && req.reasoningEffort && req.reasoningEffort !== 'none') body.reasoning_effort = req.reasoningEffort;
|
||||
if (flags.reasoning && caps.reasoning === true && req.reasoningEffort && req.reasoningEffort !== 'none') body.reasoning_effort = nearestEffort(req.reasoningEffort, COMPAT_EFFORTS);
|
||||
if (flags.jsonMode && req.jsonMode) body.response_format = { type: 'json_object' };
|
||||
if (o.adjustBody) {
|
||||
o.adjustBody(body, req);
|
||||
if (!flags.reasoning) delete body.reasoning_effort;
|
||||
}
|
||||
return body;
|
||||
}
|
||||
|
||||
/** A 400 that says the model cannot do tool calling at all (not just a rejected parameter). */
|
||||
const NO_TOOLS_RE = /(does not|doesn't|do not|cannot|can't|not) support(s|ed)? (tool|function)|(tool|function)[ _-]?(call(ing|s)?|use)? (is|are) not (supported|available|enabled)|unsupported.{0,20}(tool|function)|no tool support/;
|
||||
|
||||
function mapFinish(reason: string): StopReason {
|
||||
switch (reason) {
|
||||
case 'stop':
|
||||
|
|
@ -160,14 +203,15 @@ export async function* streamChatCompletions(o: CompatOptions, req: ChatRequest)
|
|||
const ctx = errorContext(o);
|
||||
const base = normalizeCompatBase(o.baseUrl);
|
||||
const local = isLocalUrl(base);
|
||||
const flags: BodyFlags = { tools: true, usage: true, jsonMode: true, temperature: true, reasoning: true, maxKey: 'max_tokens' };
|
||||
const flags: BodyFlags = { tools: true, forcedChoice: o.forcedToolChoice ? o.forcedToolChoice(req) : true, usage: true, jsonMode: true, temperature: true, reasoning: true, maxKey: 'max_tokens' };
|
||||
const forcing = req.toolChoice === 'required' || isRecord(req.toolChoice);
|
||||
|
||||
let http: HttpResponse;
|
||||
for (let attempt = 0; ; attempt += 1) {
|
||||
http = await httpRequest(`${base}/chat/completions`, {
|
||||
...ctx,
|
||||
headers: authHeaders(o),
|
||||
body: buildBody(req, flags),
|
||||
body: buildBody(req, flags, o),
|
||||
signal: req.signal,
|
||||
// A local server may spend minutes loading a model into memory before the first byte.
|
||||
firstByteTimeoutMs: local ? 10 * 60_000 : undefined,
|
||||
|
|
@ -184,9 +228,12 @@ export async function* streamChatCompletions(o: CompatOptions, req: ChatRequest)
|
|||
else if (flags.reasoning && /reasoning_effort|reasoning/.test(detail) && req.reasoningEffort) flags.reasoning = false;
|
||||
else if (flags.temperature && /temperature/.test(detail)) flags.temperature = false;
|
||||
else if (flags.jsonMode && req.jsonMode && /response_format|json_object|json mode/.test(detail)) flags.jsonMode = false;
|
||||
else if (flags.tools && req.tools?.length && /tool|function/.test(detail)) {
|
||||
// A forced tool choice is often what is rejected (DeepSeek in thinking mode, some
|
||||
// routers): keep the tools and ask for the call in the prompt instead.
|
||||
else if (flags.forcedChoice && forcing && flags.tools && req.tools?.length && /tool_choice|tool choice|required|function_call|named/.test(detail)) flags.forcedChoice = false;
|
||||
else if (flags.tools && req.tools?.length && NO_TOOLS_RE.test(detail)) {
|
||||
// The model (or server) cannot do tool calling at all.
|
||||
if (req.toolChoice === 'required' || isRecord(req.toolChoice)) {
|
||||
if (forcing) {
|
||||
throw new AIError('unsupported', `${o.label} reports that "${req.model}" does not support tool calling. Pick a different model for this task.`, {
|
||||
status: err.status,
|
||||
providerMessage: err.providerMessage,
|
||||
|
|
@ -200,6 +247,7 @@ export async function* streamChatCompletions(o: CompatOptions, req: ChatRequest)
|
|||
let text = '';
|
||||
let finish = '';
|
||||
let reasoningSeen = false;
|
||||
let reasoningText = '';
|
||||
const usage = { input: 0, output: 0, cacheRead: 0, reasoning: 0 };
|
||||
const calls: { id: string; name: string; args: string }[] = [];
|
||||
|
||||
|
|
@ -211,6 +259,8 @@ export async function* streamChatCompletions(o: CompatOptions, req: ChatRequest)
|
|||
usage.input = num(u.prompt_tokens) || usage.input;
|
||||
usage.output = num(u.completion_tokens) || usage.output;
|
||||
if (isRecord(u.prompt_tokens_details)) usage.cacheRead = num(u.prompt_tokens_details.cached_tokens);
|
||||
// DeepSeek's own field (the same number as cached_tokens where both are sent).
|
||||
if (!usage.cacheRead && num(u.prompt_cache_hit_tokens)) usage.cacheRead = num(u.prompt_cache_hit_tokens);
|
||||
if (isRecord(u.completion_tokens_details)) usage.reasoning = num(u.completion_tokens_details.reasoning_tokens);
|
||||
}
|
||||
const choice = Array.isArray(data.choices) ? data.choices[0] : undefined;
|
||||
|
|
@ -222,6 +272,7 @@ export async function* streamChatCompletions(o: CompatOptions, req: ChatRequest)
|
|||
const reasoning = str(delta.reasoning_content) || str(delta.reasoning) || str(delta.thinking);
|
||||
if (reasoning) {
|
||||
reasoningSeen = true;
|
||||
if (o.replayReasoning) reasoningText += reasoning;
|
||||
yield { type: 'reasoning', delta: reasoning };
|
||||
}
|
||||
const piece = typeof delta.content === 'string' ? delta.content : '';
|
||||
|
|
@ -270,6 +321,7 @@ export async function* streamChatCompletions(o: CompatOptions, req: ChatRequest)
|
|||
throw new AIError('invalid_response', `${o.label} returned an empty response.`, { retryable: true });
|
||||
}
|
||||
const message: ChatMessage = { role: 'assistant', content };
|
||||
if (o.replayReasoning) message.providerData = { kind: o.kind, data: { reasoning_content: reasoningText } };
|
||||
yield {
|
||||
type: 'done',
|
||||
message,
|
||||
|
|
|
|||
|
|
@ -6,7 +6,9 @@
|
|||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import { AIError, type ChatMessage, type ChatRequest, type ProviderAdapter, type ProviderConfig, type ProviderKind, type StreamEvent, type ToolDef, type Usage } from '../types.ts';
|
||||
import { anthropicAdapter } from './anthropic.ts';
|
||||
import { lookupModelDefaults } from './catalog.ts';
|
||||
import { cachedInputPrice, lookupModelDefaults, priceMultiplier } from './catalog.ts';
|
||||
import { nearestEffort } from './common.ts';
|
||||
import { deepseekAdapter } from './deepseek.ts';
|
||||
import { toGeminiSchema, geminiAdapter } from './gemini.ts';
|
||||
import { parseSSE } from './http.ts';
|
||||
import { ollamaAdapter } from './ollama.ts';
|
||||
|
|
@ -751,6 +753,139 @@ describe('openai-compatible stream_options fallback', () => {
|
|||
});
|
||||
});
|
||||
|
||||
/** A Chat Completions stream that calls one tool, with optional reasoning text first. */
|
||||
function compatToolStream(name: string, args: string, reasoning?: string): string[] {
|
||||
return [
|
||||
...(reasoning ? [ev({ choices: [{ index: 0, delta: { reasoning_content: reasoning } }] })] : []),
|
||||
ev({ choices: [{ index: 0, delta: { tool_calls: [{ index: 0, id: 'call_9', type: 'function', function: { name, arguments: args } }] } }] }),
|
||||
ev({ choices: [{ index: 0, delta: {}, finish_reason: 'tool_calls' }], usage: { prompt_tokens: 100, completion_tokens: 5, prompt_cache_hit_tokens: 60 } }),
|
||||
DONE,
|
||||
];
|
||||
}
|
||||
|
||||
describe('openai-compatible forced tool choice', () => {
|
||||
it('asks for the call in the prompt when a forced tool_choice is rejected, instead of reporting no tool support', async () => {
|
||||
stubFetch(
|
||||
queue(
|
||||
() => jsonResponse({ error: { message: 'deepseek-flash does not support this tool_choice in thinking mode', type: 'invalid_request_error' } }, 400),
|
||||
() => sseResponse(compatToolStream('lookup', '{"q":"cats"}')),
|
||||
),
|
||||
);
|
||||
const { done } = await run(compatKit.adapter, compatKit.cfg, { model: 'some-model', messages: [user('hi')], tools: [TOOL], toolChoice: { name: 'lookup' } });
|
||||
expect(calls).toHaveLength(2);
|
||||
expect(calls[0]!.body.tool_choice).toEqual({ type: 'function', function: { name: 'lookup' } });
|
||||
expect(calls[1]!.body.tool_choice).toBe('auto');
|
||||
expect(calls[1]!.body.tools).toHaveLength(1);
|
||||
expect(calls[1]!.body.messages[0].content).toContain('must call the `lookup` tool');
|
||||
expect(done.message.content).toEqual([{ type: 'tool_call', id: 'call_9', name: 'lookup', input: { q: 'cats' } }]);
|
||||
});
|
||||
|
||||
it('only reports "unsupported" when the server says tools are not supported at all', async () => {
|
||||
stubFetch(queue(() => jsonResponse({ error: { message: 'This model does not support tools.' } }, 400)));
|
||||
const err = await failure(run(compatKit.adapter, compatKit.cfg, { model: 'tiny', messages: [user('hi')], tools: [TOOL], toolChoice: 'required' }));
|
||||
expect(err.code).toBe('unsupported');
|
||||
});
|
||||
|
||||
it('does not mistake an unrelated 400 that mentions tools for missing tool support', async () => {
|
||||
stubFetch(queue(() => jsonResponse({ error: { message: "Messages with role 'tool' must be a response to a preceding message with 'tool_calls'." } }, 400)));
|
||||
const err = await failure(run(compatKit.adapter, compatKit.cfg, { model: 'm', messages: [user('hi')], tools: [TOOL], toolChoice: 'required' }));
|
||||
expect(err.code).not.toBe('unsupported');
|
||||
});
|
||||
});
|
||||
|
||||
describe('deepseek adapter', () => {
|
||||
const cfg = cfgOf('deepseek', 'sk-test-key-123456');
|
||||
|
||||
it('keeps thinking on for forced tool calls and asks for the call in the prompt instead', async () => {
|
||||
stubFetch(queue(() => sseResponse(compatToolStream('lookup', '{"q":"x"}'))));
|
||||
const { done } = await run(deepseekAdapter, cfg, { model: 'deepseek-flash', system: 'Base', messages: [user('hi')], tools: [TOOL], toolChoice: { name: 'lookup' }, reasoningEffort: 'high', capabilities: { reasoning: true, tools: true } });
|
||||
expect(calls[0]!.url).toBe('https://api.deepseek.com/v1/chat/completions');
|
||||
expect(calls[0]!.headers.Authorization).toBe('Bearer sk-test-key-123456');
|
||||
expect(calls[0]!.body.thinking).toEqual({ type: 'enabled' });
|
||||
expect(calls[0]!.body.reasoning_effort).toBe('high');
|
||||
expect(calls[0]!.body.tool_choice).toBe('auto');
|
||||
expect(calls[0]!.body.messages[0].content).toMatch(/^Base\n\nFor this turn you must call the `lookup` tool/);
|
||||
expect(done.message.content[0]).toMatchObject({ type: 'tool_call', name: 'lookup' });
|
||||
});
|
||||
|
||||
it('forces the tool choice only with thinking off (effort "none")', async () => {
|
||||
stubFetch(queue(() => sseResponse(compatToolStream('lookup', '{"q":"x"}'))));
|
||||
await run(deepseekAdapter, cfg, { model: 'deepseek-flash', messages: [user('hi')], tools: [TOOL], toolChoice: { name: 'lookup' }, reasoningEffort: 'none', capabilities: { reasoning: true, tools: true } });
|
||||
expect(calls[0]!.body.thinking).toEqual({ type: 'disabled' });
|
||||
expect(calls[0]!.body.reasoning_effort).toBeUndefined();
|
||||
expect(calls[0]!.body.tool_choice).toEqual({ type: 'function', function: { name: 'lookup' } });
|
||||
});
|
||||
|
||||
it('maps effort levels onto low / high / max and keeps thinking on', async () => {
|
||||
for (const [effort, expected] of [['minimal', 'low'], ['medium', 'high'], ['xhigh', 'max'], ['max', 'max']] as const) {
|
||||
stubFetch(queue(() => sseResponse(compatKit.text(['ok']))));
|
||||
await run(deepseekAdapter, cfg, { model: 'deepseek-flash', messages: [user('hi')], reasoningEffort: effort, capabilities: { reasoning: true }, temperature: 0.3 });
|
||||
expect(calls[0]!.body.thinking).toEqual({ type: 'enabled' });
|
||||
expect(calls[0]!.body.reasoning_effort).toBe(expected);
|
||||
expect(calls[0]!.body.temperature).toBeUndefined();
|
||||
}
|
||||
stubFetch(queue(() => sseResponse(compatKit.text(['ok']))));
|
||||
await run(deepseekAdapter, cfg, { model: 'deepseek-flash', messages: [user('hi')], reasoningEffort: 'none', capabilities: { reasoning: true } });
|
||||
expect(calls[0]!.body.thinking).toEqual({ type: 'disabled' });
|
||||
});
|
||||
|
||||
it('keeps reasoning_content and replays it on the next request, with an empty string for other providers', async () => {
|
||||
stubFetch(queue(() => sseResponse(compatToolStream('lookup', '{"q":"cats"}', 'Let me look that up.'))));
|
||||
const first = await run(deepseekAdapter, cfg, { model: 'deepseek-flash', messages: [user('hi')], tools: [TOOL], toolChoice: 'auto' });
|
||||
expect(first.done.message.providerData).toEqual({ kind: 'deepseek', data: { reasoning_content: 'Let me look that up.' } });
|
||||
expect(first.done.usage).toMatchObject({ inputTokens: 100, cacheReadTokens: 60 });
|
||||
|
||||
stubFetch(queue(() => sseResponse(compatKit.text(['done']))));
|
||||
const foreign = toolCallMsg({ providerData: { kind: 'openai', data: { x: 1 } } });
|
||||
await run(deepseekAdapter, cfg, { model: 'deepseek-flash', messages: [user('hi'), first.done.message, toolResultMsg, user('again'), foreign, toolResultMsg], tools: [TOOL], toolChoice: 'auto' });
|
||||
const assistants = calls[0]!.body.messages.filter((m: any) => m.role === 'assistant');
|
||||
expect(assistants[0].reasoning_content).toBe('Let me look that up.');
|
||||
expect(assistants[1].reasoning_content).toBe('');
|
||||
});
|
||||
|
||||
it('lists models with limits, vision and reasoning from /models', async () => {
|
||||
stubFetch(
|
||||
queue(() =>
|
||||
jsonResponse({
|
||||
data: [
|
||||
{ id: 'deepseek-flash', name: 'DeepSeek-V4.1-Flash', context_window: 1048576, max_output_tokens: 393216, input_modalities: ['text', 'image'], effort: { supported_levels: ['low', 'high', 'max'] } },
|
||||
{ id: 'deepseek-v4-pro', name: 'DeepSeek-V4-Pro', context_window: 1048576, max_output_tokens: 393216, input_modalities: ['text'], effort: { supported_levels: ['low', 'high', 'max'] } },
|
||||
],
|
||||
}),
|
||||
),
|
||||
);
|
||||
const models = await deepseekAdapter.listModels(cfg);
|
||||
expect(calls[0]!.url).toBe('https://api.deepseek.com/models');
|
||||
const flash = models.find((m) => m.id === 'deepseek-flash')!;
|
||||
expect(flash).toMatchObject({ contextWindow: 1048576, maxOutput: 393216, priceIn: 0.3, priceCachedIn: 0.006, priceOut: 1.2 });
|
||||
expect(flash.capabilities).toMatchObject({ tools: true, vision: true, reasoning: true });
|
||||
expect(models.find((m) => m.id === 'deepseek-v4-pro')!.capabilities?.vision).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('reasoning effort and cache pricing helpers', () => {
|
||||
it('maps efforts to the nearest supported level, rounding ties up', () => {
|
||||
expect(nearestEffort('medium', ['low', 'high', 'max'] as const)).toBe('high');
|
||||
expect(nearestEffort('minimal', ['low', 'high', 'max'] as const)).toBe('low');
|
||||
expect(nearestEffort('xhigh', ['minimal', 'low', 'medium', 'high'] as const)).toBe('high');
|
||||
expect(nearestEffort('max', ['low', 'medium', 'high', 'xhigh', 'max'] as const)).toBe('max');
|
||||
});
|
||||
|
||||
it('uses a model cached price when set and the provider discount otherwise', () => {
|
||||
expect(cachedInputPrice('deepseek', 'deepseek-flash', 0.3, 0.006)).toBe(0.006);
|
||||
expect(cachedInputPrice('anthropic', 'claude-sonnet-5-5', 2, null)).toBeCloseTo(0.2);
|
||||
expect(cachedInputPrice('openai', 'gpt-4o', 2.5, null)).toBeCloseTo(1.25);
|
||||
expect(cachedInputPrice('openai_compatible', 'x', 1, null)).toBe(1);
|
||||
});
|
||||
|
||||
it('halves DeepSeek prices outside peak hours only', () => {
|
||||
expect(priceMultiplier('deepseek', Date.UTC(2026, 10, 16, 2, 30))).toBe(1); // Monday 02:30 UTC
|
||||
expect(priceMultiplier('deepseek', Date.UTC(2026, 10, 16, 12, 0))).toBe(0.5); // Monday noon
|
||||
expect(priceMultiplier('deepseek', Date.UTC(2026, 10, 15, 2, 30))).toBe(0.5); // Sunday
|
||||
expect(priceMultiplier('openai', Date.UTC(2026, 10, 16, 12, 0))).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('openai-compatible listModels', () => {
|
||||
it('parses OpenRouter-style metadata, filters embeddings and merges vendor catalog defaults', async () => {
|
||||
stubFetch(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue