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
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue