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
|
|
@ -11,7 +11,7 @@ import { z } from 'zod';
|
|||
import type { DraftCard, DraftQuestion } from '../../shared/content.ts';
|
||||
import { draftsFromValue } from '../../shared/importer.ts';
|
||||
import type { DB } from '../db.ts';
|
||||
import { badRequest, notFound, type AppContext } from '../http.ts';
|
||||
import { badRequest, HttpError, notFound, type AppContext } from '../http.ts';
|
||||
import { getAppSettings } from '../settings.ts';
|
||||
import { createAttempt, missedQuestionIds } from '../services/attempts.ts';
|
||||
import { createClass, createFolder, getClass, getDeck, getQuiz, listCards, listQuestions, resolveFolder } from '../services/content.ts';
|
||||
|
|
@ -52,6 +52,16 @@ export type AssistantEvent =
|
|||
export interface AssistantContext {
|
||||
classId?: string | null;
|
||||
documentIds?: string[];
|
||||
/** Materials the user tagged with @ in this message. */
|
||||
items?: TaggedItem[];
|
||||
}
|
||||
|
||||
export const TAG_KINDS = ['quiz', 'deck', 'note', 'document', 'folder'] as const;
|
||||
export type TagKind = (typeof TAG_KINDS)[number];
|
||||
export interface TaggedItem {
|
||||
kind: TagKind;
|
||||
id: string;
|
||||
title: string;
|
||||
}
|
||||
|
||||
interface ToolRun {
|
||||
|
|
@ -483,7 +493,7 @@ const TOOLS: ToolSpec[] = [
|
|||
type: 'array',
|
||||
items: obj(
|
||||
{
|
||||
type: { type: 'string', enum: ['single', 'multi'] },
|
||||
type: { type: 'string', enum: ['single', 'multi', 'numeric'], description: 'numeric = the student types a number (calculations); its options hold only the correct answer, e.g. "12.5 mL".' },
|
||||
stem: S,
|
||||
options: { type: 'array', items: obj({ text: S, correct: { type: 'boolean' }, why: S }, ['text', 'correct']) },
|
||||
rationale: S,
|
||||
|
|
@ -744,6 +754,8 @@ interface MessageRow {
|
|||
interface MessageMeta {
|
||||
hidden?: boolean;
|
||||
toolResults?: boolean;
|
||||
/** Materials tagged in a user message. */
|
||||
tags?: TaggedItem[];
|
||||
tools?: ToolActivity[];
|
||||
ui?: UiItem[];
|
||||
model?: string;
|
||||
|
|
@ -754,6 +766,7 @@ export interface DisplayMessage {
|
|||
id: number;
|
||||
role: 'user' | 'assistant';
|
||||
text: string;
|
||||
tags?: TaggedItem[];
|
||||
tools: ToolActivity[];
|
||||
ui: UiItem[];
|
||||
model?: string;
|
||||
|
|
@ -773,7 +786,7 @@ export function displayMessages(db: DB, conversationId: string): DisplayMessage[
|
|||
const text = msg.content.filter((p) => p.type === 'text').map((p) => (p as { text: string }).text).join('');
|
||||
if (row.role === 'user') {
|
||||
current = null;
|
||||
out.push({ id: row.id, role: 'user', text, tools: [], ui: [], createdAt: row.created_at });
|
||||
out.push({ id: row.id, role: 'user', text, tools: [], ui: [], createdAt: row.created_at, ...(meta.tags?.length ? { tags: meta.tags } : {}) });
|
||||
continue;
|
||||
}
|
||||
if (!current) {
|
||||
|
|
@ -857,6 +870,20 @@ function systemPrompt(app: AppContext, context: AssistantContext): string {
|
|||
const docs = db.all<{ id: string; title: string }>(`SELECT id, title FROM documents WHERE id IN (${context.documentIds.map(() => '?').join(',')})`, ...context.documentIds);
|
||||
if (docs.length) lines.push(`The user has these documents in focus: ${docs.map((d) => `"${d.title}" (id ${d.id})`).join(', ')}. Questions like "summarise this" refer to them.`);
|
||||
}
|
||||
if (context.items?.length) {
|
||||
const how: Record<TagKind, string> = {
|
||||
quiz: 'open it with get_quiz',
|
||||
deck: 'open it with get_deck',
|
||||
note: 'read it with read_note',
|
||||
document: 'search it with search_documents or read it with read_document; pass its id as a source when generating',
|
||||
folder: 'list its contents with list_materials',
|
||||
};
|
||||
lines.push(
|
||||
`In their latest message the user tagged these materials, so "this", "these" and similar words refer to them. Use the ids directly instead of searching for them:\n${context.items
|
||||
.map((i) => `- ${i.kind} "${i.title}" — id ${i.id}${TAG_URL[i.kind] ? `, link ${TAG_URL[i.kind]}${i.id}` : ''} (${how[i.kind]})`)
|
||||
.join('\n')}`,
|
||||
);
|
||||
}
|
||||
lines.push(
|
||||
`How to work:
|
||||
- Use tools instead of guessing. To answer questions about their material, search or read the documents first and base the answer on what you find.
|
||||
|
|
@ -877,10 +904,131 @@ function systemPrompt(app: AppContext, context: AssistantContext): string {
|
|||
export const assistantRequestSchema = z.object({
|
||||
conversationId: z.string().nullish(),
|
||||
message: z.string().trim().min(1).max(20_000),
|
||||
context: z.object({ classId: z.string().nullish(), documentIds: z.array(z.string()).max(20).optional() }).optional(),
|
||||
context: z
|
||||
.object({
|
||||
classId: z.string().nullish(),
|
||||
documentIds: z.array(z.string()).max(20).optional(),
|
||||
items: z.array(z.object({ kind: z.enum(TAG_KINDS), id: z.string().max(60) })).max(20).optional(),
|
||||
})
|
||||
.optional(),
|
||||
modelId: z.string().nullish(),
|
||||
});
|
||||
|
||||
/** App paths for linking tagged items in answers (folders have no page of their own). */
|
||||
const TAG_URL: Record<TagKind, string> = { quiz: '/quizzes/', deck: '/decks/', note: '/notes/', document: '/documents/', folder: '' };
|
||||
|
||||
const TAG_TABLE: Record<TagKind, { table: string; title: string }> = {
|
||||
quiz: { table: 'quizzes', title: 'title' },
|
||||
deck: { table: 'decks', title: 'title' },
|
||||
note: { table: 'notes', title: 'title' },
|
||||
document: { table: 'documents', title: 'title' },
|
||||
folder: { table: 'folders', title: 'name' },
|
||||
};
|
||||
|
||||
/** Looks tagged items up so the model (and the transcript) get their real titles; unknown ids are dropped. */
|
||||
function resolveTags(db: DB, items: { kind: TagKind; id: string }[] | undefined): TaggedItem[] {
|
||||
const out: TaggedItem[] = [];
|
||||
for (const i of items ?? []) {
|
||||
const meta = TAG_TABLE[i.kind];
|
||||
const row = db.get<{ title: string }>(`SELECT ${meta.title} AS title FROM ${meta.table} WHERE id = ?`, i.id);
|
||||
if (row && !out.some((o) => o.kind === i.kind && o.id === i.id)) out.push({ kind: i.kind, id: i.id, title: row.title });
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/** Library items the user can tag with @: one class's materials, or a title search across everything. */
|
||||
export function mentionables(db: DB, opts: { classId?: string | null; q?: string }): (TaggedItem & { className: string; classColor: string; folderName?: string })[] {
|
||||
const q = (opts.q ?? '').trim().slice(0, 80);
|
||||
const like = `%${q.replace(/[\\%_]/g, (m) => `\\${m}`)}%`;
|
||||
const out: (TaggedItem & { className: string; classColor: string; folderName?: string })[] = [];
|
||||
const limit = opts.classId ? 300 : 12;
|
||||
for (const kind of TAG_KINDS) {
|
||||
const meta = TAG_TABLE[kind];
|
||||
const where = [opts.classId ? 't.class_id = ?' : '1 = 1', q ? `t.${meta.title} LIKE ? ESCAPE '\\'` : '1 = 1'];
|
||||
const params = [...(opts.classId ? [opts.classId] : []), ...(q ? [like] : [])];
|
||||
const folderJoin = kind === 'folder' ? '' : 'LEFT JOIN folders f ON f.id = t.folder_id';
|
||||
const folderCol = kind === 'folder' ? "'' AS folder_name" : 'f.name AS folder_name';
|
||||
const rows = db.all<{ id: string; title: string; class_name: string; class_color: string; folder_name: string | null }>(
|
||||
`SELECT t.id, t.${meta.title} AS title, k.name AS class_name, k.color AS class_color, ${folderCol}
|
||||
FROM ${meta.table} t JOIN classes k ON k.id = t.class_id ${folderJoin}
|
||||
WHERE ${where.join(' AND ')} ${kind === 'document' ? "AND t.status = 'ready'" : ''}
|
||||
ORDER BY t.updated_at DESC LIMIT ?`,
|
||||
...params,
|
||||
limit,
|
||||
);
|
||||
for (const r of rows) out.push({ kind, id: r.id, title: r.title, className: r.class_name, classColor: r.class_color, ...(r.folder_name ? { folderName: r.folder_name } : {}) });
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/* ------------------------------ running turns ------------------------------ */
|
||||
|
||||
/**
|
||||
* A turn keeps running on the server when the browser disconnects (the user leaves the
|
||||
* page, closes the drawer, switches to another conversation). Its events are buffered so a
|
||||
* client that comes back can replay them and follow along; only "Stop" cancels it.
|
||||
*/
|
||||
interface LiveTurn {
|
||||
events: AssistantEvent[];
|
||||
listeners: Set<(event: AssistantEvent) => void>;
|
||||
controller: AbortController;
|
||||
}
|
||||
const live = new Map<string, LiveTurn>();
|
||||
|
||||
export function isTurnRunning(conversationId: string): boolean {
|
||||
return live.has(conversationId);
|
||||
}
|
||||
|
||||
/** Replays the running turn's events, then streams the rest. Returns an unsubscribe function, or null when nothing is running. */
|
||||
export function followTurn(conversationId: string, listener: (event: AssistantEvent) => void): (() => void) | null {
|
||||
const turn = live.get(conversationId);
|
||||
if (!turn) return null;
|
||||
for (const e of turn.events) listener(e);
|
||||
turn.listeners.add(listener);
|
||||
return () => turn.listeners.delete(listener);
|
||||
}
|
||||
|
||||
export function stopTurn(conversationId: string): boolean {
|
||||
const turn = live.get(conversationId);
|
||||
if (!turn) return false;
|
||||
turn.controller.abort();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts a turn in the background and returns its conversation id at once. Problems that
|
||||
* prevent the turn from starting (no model configured, unknown model, a turn already
|
||||
* running in this conversation) throw here, before anything is saved.
|
||||
*/
|
||||
export function startAssistantTurn(app: AppContext, input: z.input<typeof assistantRequestSchema>): { conversationId: string } {
|
||||
const req = assistantRequestSchema.parse(input);
|
||||
if (req.conversationId && live.has(req.conversationId)) throw new HttpError(409, 'busy', 'The assistant is still answering in this conversation. Wait for it or press Stop.');
|
||||
resolveModel(app, 'chat', req.modelId);
|
||||
const controller = new AbortController();
|
||||
const turn: LiveTurn = { events: [], listeners: new Set(), controller };
|
||||
let id = '';
|
||||
const emit = (event: AssistantEvent) => {
|
||||
turn.events.push(event);
|
||||
for (const fn of turn.listeners) fn(event);
|
||||
};
|
||||
const started = runAssistantTurn(app, req, emit, controller.signal, (conversationId) => {
|
||||
id = conversationId;
|
||||
live.set(conversationId, turn);
|
||||
});
|
||||
void started
|
||||
.catch((err) => {
|
||||
const ai = err instanceof AIError ? err : null;
|
||||
if (!ai) app.log('error', 'Assistant turn failed', err);
|
||||
emit({ type: 'error', code: ai?.code ?? 'server', message: ai?.message ?? 'The assistant stopped unexpectedly.' });
|
||||
emit({ type: 'done', conversationId: id, title: '' });
|
||||
})
|
||||
.finally(() => {
|
||||
if (id && live.get(id) === turn) live.delete(id);
|
||||
});
|
||||
if (!id) throw new AIError('server', 'The assistant could not start.');
|
||||
return { conversationId: id };
|
||||
}
|
||||
|
||||
const MAX_STEPS = 12;
|
||||
const TOOL_OUTPUT_LIMIT = 28_000;
|
||||
|
||||
|
|
@ -890,10 +1038,16 @@ export async function runAssistantTurn(
|
|||
input: z.input<typeof assistantRequestSchema>,
|
||||
emit: (event: AssistantEvent) => void,
|
||||
signal?: AbortSignal,
|
||||
/** Called synchronously, before the first await, with the conversation id. */
|
||||
onConversation?: (conversationId: string) => void,
|
||||
): Promise<void> {
|
||||
const { db } = app;
|
||||
const req = assistantRequestSchema.parse(input);
|
||||
const context: AssistantContext = { classId: req.context?.classId ?? null, documentIds: req.context?.documentIds };
|
||||
const tags = resolveTags(db, req.context?.items);
|
||||
// Tagged documents join the documents in focus so retrieval and generation use them.
|
||||
const taggedDocs = tags.filter((t) => t.kind === 'document').map((t) => t.id);
|
||||
const documentIds = [...new Set([...(req.context?.documentIds ?? []), ...taggedDocs])];
|
||||
const context: AssistantContext = { classId: req.context?.classId ?? null, documentIds: documentIds.length ? documentIds : undefined, items: tags };
|
||||
const resolved = resolveModel(app, 'chat', req.modelId);
|
||||
const at = now();
|
||||
|
||||
|
|
@ -912,8 +1066,9 @@ export async function runAssistantTurn(
|
|||
const save = (role: 'user' | 'assistant', message: ChatMessage, meta: MessageMeta = {}) =>
|
||||
db.run('INSERT INTO messages (conversation_id, role, content, meta, created_at) VALUES (?, ?, ?, ?, ?)', conversationId, role, JSON.stringify(message), JSON.stringify(meta), now());
|
||||
|
||||
save('user', { role: 'user', content: [{ type: 'text', text: req.message }] });
|
||||
save('user', { role: 'user', content: [{ type: 'text', text: req.message }] }, tags.length ? { tags } : {});
|
||||
db.run('UPDATE conversations SET updated_at = ? WHERE id = ?', now(), conversationId);
|
||||
onConversation?.(conversationId);
|
||||
emit({ type: 'start', conversationId, model: resolved.model.label });
|
||||
|
||||
const env: ToolEnv = { app, conversationId, context, signal, modelId: req.modelId };
|
||||
|
|
|
|||
|
|
@ -39,6 +39,7 @@ import {
|
|||
import { chunkById, formatExcerpts, searchChunks, selectContext, sourceLabel, type ChunkHit } from '../services/documents.ts';
|
||||
import { hybridSearch } from '../services/semantic.ts';
|
||||
import { weakTopics } from '../services/stats.ts';
|
||||
import { getSetting, setSetting } from '../settings.ts';
|
||||
import { newId, now, parseJson } from '../util.ts';
|
||||
import { generateStructured, resolveModel, runChat } from './service.ts';
|
||||
import { AIError } from './types.ts';
|
||||
|
|
@ -297,10 +298,15 @@ const QUESTION_SCHEMA = {
|
|||
items: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
type: { type: 'string', enum: ['single', 'multi'], description: 'single = one correct answer; multi = select all that apply' },
|
||||
type: {
|
||||
type: 'string',
|
||||
enum: ['single', 'multi', 'numeric'],
|
||||
description: 'single = one correct answer; multi = select all that apply; numeric = a calculation the student answers by typing the number',
|
||||
},
|
||||
stem: { type: 'string', description: 'The question. Markdown allowed.' },
|
||||
options: {
|
||||
type: 'array',
|
||||
description: 'Answer choices. For numeric questions: exactly one option, the correct answer with its unit (e.g. "12.5 mL"), correct = true, and "why" showing the calculation.',
|
||||
items: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
|
|
@ -343,6 +349,8 @@ const CARD_SCHEMA = {
|
|||
required: ['cards'],
|
||||
};
|
||||
|
||||
const NUMERIC_RULE = `- Calculation items (dosages, weight-based doses, IV and drip rates, conversions, intake and output) use type "numeric": the student types the number instead of choosing. Give exactly one option, the correct answer with its unit (for example "12.5 mL"), marked correct, with "why" showing the working. State the unit and the rounding rule in the stem ("Round to the nearest tenth."). Only the number is checked, so the student may leave out the unit.`;
|
||||
|
||||
const NCLEX_RULES = `Write NCLEX-style (Next Generation NCLEX) items:
|
||||
- Test clinical judgment at the application and analysis level: recognise cues, analyse, prioritise, choose and evaluate actions. Avoid pure recall.
|
||||
- Open with a brief, realistic client scenario containing only relevant data (age, diagnosis, key findings, vital signs or lab values with units).
|
||||
|
|
@ -351,18 +359,21 @@ const NCLEX_RULES = `Write NCLEX-style (Next Generation NCLEX) items:
|
|||
- Every distractor must be plausible to a student with partial knowledge and wrong for a specific reason. Keep choices parallel in grammar and similar in length. Never use "all of the above" or "none of the above". Do not give the answer away through wording or length.
|
||||
- Multiple-choice items have exactly 4 choices with exactly one best answer. Select-all-that-apply items have 5 or 6 choices with 2 to 5 correct, and the stem ends with "Select all that apply."
|
||||
- Use generic drug names. Use current, standard practice.
|
||||
- The rationale teaches: state why the correct answer is correct, name the principle behind it, and give each choice its own "why".`;
|
||||
- The rationale teaches: state why the correct answer is correct, name the principle behind it, and give each choice its own "why".
|
||||
${NUMERIC_RULE}`;
|
||||
|
||||
const STANDARD_RULES = `Write clear exam-quality questions:
|
||||
- Each question tests one idea and has one defensible answer set.
|
||||
- Mix understanding and application; avoid trivia and trick wording.
|
||||
- Distractors are plausible and parallel. Never use "all of the above" or "none of the above".
|
||||
- Multiple-choice items have 4 choices with exactly one correct. Select-all items have 4 to 6 choices with at least 2 correct.
|
||||
- The rationale explains the correct answer, and each choice gets its own "why".`;
|
||||
- The rationale explains the correct answer, and each choice gets its own "why".
|
||||
${NUMERIC_RULE}`;
|
||||
|
||||
const RECALL_RULES = `Write straightforward recall questions that check key facts, definitions, normal values and classifications.
|
||||
- Multiple-choice items have 4 choices with exactly one correct. Select-all items have 4 to 6 choices with at least 2 correct.
|
||||
- Keep stems short and unambiguous. Each choice gets a brief "why".`;
|
||||
- Keep stems short and unambiguous. Each choice gets a brief "why".
|
||||
${NUMERIC_RULE}`;
|
||||
|
||||
const CARD_RULES: Record<string, string> = {
|
||||
concise: 'Each card tests exactly one fact. Fronts are specific questions or terms; backs are short (a phrase or one or two lines).',
|
||||
|
|
@ -380,9 +391,71 @@ const NOTE_RULES: Record<string, string> = {
|
|||
cheat_sheet: 'Write a one-page cheat sheet in Markdown: dense, scannable, organised under short headings, using tables for values, drug classes and comparisons, and bold for must-know facts.',
|
||||
};
|
||||
|
||||
function baseSystem(req: GenerateRequest, hasSources: boolean): string {
|
||||
const BASE_ROLE = 'You are an expert educator writing study material for a nursing student. Accuracy is critical: this material is used to prepare for licensing exams.';
|
||||
|
||||
/* ------------------------------ editable instructions ------------------------------ */
|
||||
|
||||
/**
|
||||
* The instructions the built-in generator (Generate and the assistant) sends to the model.
|
||||
* Every entry can be replaced in Settings → Templates; an absent value means the built-in
|
||||
* wording. These are separate from the instructions for outside chatbots
|
||||
* (`LLM_INSTRUCTIONS`), which also have to describe the import JSON format.
|
||||
*/
|
||||
export const GENERATION_INSTRUCTIONS = {
|
||||
base: { group: 'All content', label: 'Role', text: BASE_ROLE },
|
||||
'quiz.nclex': { group: 'Quiz questions', label: 'NCLEX style', text: NCLEX_RULES },
|
||||
'quiz.standard': { group: 'Quiz questions', label: 'Standard exam style', text: STANDARD_RULES },
|
||||
'quiz.recall': { group: 'Quiz questions', label: 'Quick recall', text: RECALL_RULES },
|
||||
'deck.concise': { group: 'Flashcards', label: 'Concise', text: CARD_RULES.concise },
|
||||
'deck.detailed': { group: 'Flashcards', label: 'Detailed', text: CARD_RULES.detailed },
|
||||
'deck.medication': { group: 'Flashcards', label: 'Medication', text: CARD_RULES.medication },
|
||||
'deck.definition': { group: 'Flashcards', label: 'Term and definition', text: CARD_RULES.definition },
|
||||
'note.study_guide': { group: 'Study guides', label: 'Study guide', text: NOTE_RULES.study_guide },
|
||||
'note.summary': { group: 'Study guides', label: 'Summary', text: NOTE_RULES.summary },
|
||||
'note.outline': { group: 'Study guides', label: 'Outline', text: NOTE_RULES.outline },
|
||||
'note.cheat_sheet': { group: 'Study guides', label: 'Cheat sheet', text: NOTE_RULES.cheat_sheet },
|
||||
} as const;
|
||||
export type InstructionKey = keyof typeof GENERATION_INSTRUCTIONS;
|
||||
const INSTRUCTIONS_SETTING = 'ai.instructions';
|
||||
|
||||
export function instructionOverrides(db: DB): Partial<Record<InstructionKey, string>> {
|
||||
return getSetting<Partial<Record<InstructionKey, string>>>(db, INSTRUCTIONS_SETTING, {});
|
||||
}
|
||||
|
||||
export function instruction(db: DB, key: InstructionKey): string {
|
||||
const own = instructionOverrides(db)[key];
|
||||
return own?.trim() ? own : GENERATION_INSTRUCTIONS[key].text;
|
||||
}
|
||||
|
||||
export function listInstructions(db: DB) {
|
||||
const own = instructionOverrides(db);
|
||||
return (Object.keys(GENERATION_INSTRUCTIONS) as InstructionKey[]).map((key) => ({
|
||||
key,
|
||||
group: GENERATION_INSTRUCTIONS[key].group,
|
||||
label: GENERATION_INSTRUCTIONS[key].label,
|
||||
text: own[key]?.trim() ? own[key]! : GENERATION_INSTRUCTIONS[key].text,
|
||||
builtin: GENERATION_INSTRUCTIONS[key].text,
|
||||
customized: Boolean(own[key]?.trim()),
|
||||
}));
|
||||
}
|
||||
|
||||
export const instructionPatchSchema = z.object({
|
||||
key: z.enum(Object.keys(GENERATION_INSTRUCTIONS) as [InstructionKey, ...InstructionKey[]]),
|
||||
/** null restores the built-in wording. */
|
||||
text: z.string().trim().min(10).max(20_000).nullable(),
|
||||
});
|
||||
|
||||
export function saveInstruction(db: DB, input: z.input<typeof instructionPatchSchema>): void {
|
||||
const { key, text } = instructionPatchSchema.parse(input);
|
||||
const next = { ...instructionOverrides(db) };
|
||||
if (text === null || text === GENERATION_INSTRUCTIONS[key].text) delete next[key];
|
||||
else next[key] = text;
|
||||
setSetting(db, INSTRUCTIONS_SETTING, next);
|
||||
}
|
||||
|
||||
function baseSystem(db: DB, req: GenerateRequest, hasSources: boolean): string {
|
||||
const lines = [
|
||||
'You are an expert educator writing study material for a nursing student. Accuracy is critical: this material is used to prepare for licensing exams.',
|
||||
instruction(db, 'base'),
|
||||
hasSources
|
||||
? 'Base everything on the provided source material. Do not introduce facts that contradict it. You may rely on standard, well-established knowledge only to write plausible distractors and clear explanations.'
|
||||
: 'No source material was provided, so rely on current, well-established knowledge. If something is uncertain or varies by institution, prefer the most widely taught standard.',
|
||||
|
|
@ -598,8 +671,8 @@ async function runGeneration(app: AppContext, draftId: string, req: GenerateRequ
|
|||
const notes: string[] = [];
|
||||
|
||||
const style = req.style ?? 'nclex';
|
||||
const rules = isQuiz ? (style === 'nclex' ? NCLEX_RULES : style === 'recall' ? RECALL_RULES : STANDARD_RULES) : CARD_RULES[req.cardStyle ?? 'concise'];
|
||||
const system = `${baseSystem(req, hasSources)}\n\n${rules}`;
|
||||
const rules = isQuiz ? instruction(db, `quiz.${style}`) : instruction(db, `deck.${req.cardStyle ?? 'concise'}`);
|
||||
const system = `${baseSystem(db, req, hasSources)}\n\n${rules}`;
|
||||
const sata = req.sataShare ?? (style === 'nclex' ? 0.3 : 0.2);
|
||||
const difficulty = req.difficulty ?? 'mixed';
|
||||
|
||||
|
|
@ -609,7 +682,8 @@ async function runGeneration(app: AppContext, draftId: string, req: GenerateRequ
|
|||
const ask = isQuiz
|
||||
? [
|
||||
`Write ${want} ${style === 'nclex' ? 'NCLEX-style ' : ''}questions${req.focus ? ` about: ${req.focus}` : ''}.`,
|
||||
sata <= 0 ? 'Use only multiple-choice (single) questions.' : sata >= 1 ? 'Use only select-all-that-apply (multi) questions.' : `About ${sataCount} of them should be select-all-that-apply (multi); the rest multiple choice (single).`,
|
||||
sata <= 0 ? 'Use only multiple-choice (single) questions, apart from calculations.' : sata >= 1 ? 'Use only select-all-that-apply (multi) questions, apart from calculations.' : `About ${sataCount} of them should be select-all-that-apply (multi); the rest multiple choice (single).`,
|
||||
'If the material involves calculations (dosages, IV rates, conversions), write those as numeric questions the student answers by typing the number.',
|
||||
difficulty === 'mixed' ? 'Mix difficulty: mostly medium, with a few easy and a few hard.' : `Difficulty: ${difficulty}.`,
|
||||
sources.groups.length > 0 ? 'Spread the questions across the excerpts instead of clustering on one. Set "source" to the id of the excerpt each question is based on.' : '',
|
||||
req.source.deckId ? 'Turn the flashcards into questions: use each card\'s answer as the correct choice and write distractors that a student could confuse with it.' : '',
|
||||
|
|
@ -703,7 +777,7 @@ async function generateNote(app: AppContext, draftId: string, req: GenerateReque
|
|||
const hasSources = sources.groups.length > 0 || Boolean(sources.inline);
|
||||
if (!hasSources && !req.focus) throw badRequest('Choose source material or describe a topic to write about.');
|
||||
const title = defaultTitle(app, req, sources);
|
||||
const system = `${baseSystem(req, hasSources)}\n\n${NOTE_RULES[style]}\n\n${
|
||||
const system = `${baseSystem(db, req, hasSources)}\n\n${instruction(db, `note.${style}`)}\n\n${
|
||||
sources.groups.length > 0 ? 'Cite the excerpts you draw on by putting their id in brackets at the end of the relevant line or paragraph, e.g. [c42]. Use only ids that appear in the source.' : ''
|
||||
}\nStart directly with the content (no preamble, no title line).`;
|
||||
|
||||
|
|
@ -740,7 +814,7 @@ async function generateNote(app: AppContext, draftId: string, req: GenerateReque
|
|||
task: 'generate',
|
||||
purpose: 'study guide',
|
||||
modelId: req.modelId,
|
||||
system: `${baseSystem(req, true)}\n\nCondense this part of the material into detailed notes: every key concept, value, medication, cause, sign, intervention and warning, as Markdown bullet points under short headings. Keep the bracketed excerpt ids (e.g. [c42]) next to the points they support. Do not add outside information.`,
|
||||
system: `${baseSystem(db, req, true)}\n\nCondense this part of the material into detailed notes: every key concept, value, medication, cause, sign, intervention and warning, as Markdown bullet points under short headings. Keep the bracketed excerpt ids (e.g. [c42]) next to the points they support. Do not add outside information.`,
|
||||
messages: [{ role: 'user', content: [{ type: 'text', text: `${req.focus ? `Focus on: ${req.focus}\n\n` : ''}SOURCE EXCERPTS (part ${i + 1} of ${passes.length}):\n\n${formatExcerpts(passes[i])}` }] }],
|
||||
maxOutputTokens: 6_000,
|
||||
signal,
|
||||
|
|
|
|||
101
server/ai/localEmbed.ts
Normal file
101
server/ai/localEmbed.ts
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
/**
|
||||
* Built-in embedding model, used for semantic search when no cloud embedding provider is
|
||||
* configured. Runs on the server's CPU through ONNX Runtime (transformers.js), so it needs no
|
||||
* API key and costs nothing.
|
||||
*
|
||||
* Model: BAAI bge-small-en-v1.5 (ONNX, 8-bit quantised, about 34 MB, 384 dimensions). It is a
|
||||
* strong small retrieval model; queries get BGE's recommended instruction prefix. The files
|
||||
* are downloaded from Hugging Face on first use into DATA_DIR/models and reused afterwards.
|
||||
*
|
||||
* The library is imported lazily: the app starts (and tests run) without loading ONNX Runtime
|
||||
* until something actually needs an embedding.
|
||||
*/
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { AIError } from './types.ts';
|
||||
|
||||
export const LOCAL_PROVIDER_ID = 'local';
|
||||
export const LOCAL_EMBEDDING_MODEL = 'Xenova/bge-small-en-v1.5';
|
||||
export const LOCAL_EMBEDDING_LABEL = 'bge-small-en-v1.5 (built in)';
|
||||
export const LOCAL_EMBEDDING_DIMS = 384;
|
||||
const QUERY_PREFIX = 'Represent this sentence for searching relevant passages: ';
|
||||
/** Inputs per forward pass: small enough to keep memory flat on a home server. */
|
||||
const BATCH = 16;
|
||||
/** About 512 tokens, the model's window. Longer input is truncated by the tokenizer anyway. */
|
||||
const MAX_CHARS = 2000;
|
||||
|
||||
type Extractor = (texts: string[], opts: { pooling: 'cls' | 'mean'; normalize: boolean }) => Promise<{ tolist(): number[][] }>;
|
||||
|
||||
const state: { extractor: Promise<Extractor> | null; cacheDir: string; status: 'idle' | 'loading' | 'ready' | 'error'; error: string } = {
|
||||
extractor: null,
|
||||
cacheDir: '',
|
||||
status: 'idle',
|
||||
error: '',
|
||||
};
|
||||
|
||||
export function localModelDir(dataDir: string): string {
|
||||
return path.join(dataDir, 'models');
|
||||
}
|
||||
|
||||
/** True when the model files are already on disk (no download needed). */
|
||||
export function localModelDownloaded(dataDir: string): boolean {
|
||||
const dir = path.join(localModelDir(dataDir), ...LOCAL_EMBEDDING_MODEL.split('/'));
|
||||
try {
|
||||
return fs.existsSync(path.join(dir, 'onnx', 'model_quantized.onnx')) && fs.existsSync(path.join(dir, 'tokenizer.json'));
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export function localModelStatus(dataDir: string) {
|
||||
return { model: LOCAL_EMBEDDING_MODEL, label: LOCAL_EMBEDDING_LABEL, dimensions: LOCAL_EMBEDDING_DIMS, downloaded: localModelDownloaded(dataDir), status: state.status, error: state.error };
|
||||
}
|
||||
|
||||
async function load(dataDir: string): Promise<Extractor> {
|
||||
const cacheDir = localModelDir(dataDir);
|
||||
if (state.extractor && state.cacheDir === cacheDir) return state.extractor;
|
||||
state.cacheDir = cacheDir;
|
||||
state.status = 'loading';
|
||||
state.error = '';
|
||||
state.extractor = (async () => {
|
||||
fs.mkdirSync(cacheDir, { recursive: true });
|
||||
let lib: typeof import('@huggingface/transformers');
|
||||
try {
|
||||
lib = await import('@huggingface/transformers');
|
||||
} catch (err) {
|
||||
throw new AIError('unsupported', 'The built-in embedding model is not available in this installation.', { cause: err });
|
||||
}
|
||||
lib.env.cacheDir = cacheDir;
|
||||
lib.env.allowRemoteModels = true;
|
||||
lib.env.allowLocalModels = true;
|
||||
lib.env.localModelPath = cacheDir;
|
||||
const extractor = await lib.pipeline('feature-extraction', LOCAL_EMBEDDING_MODEL, { dtype: 'q8' });
|
||||
return extractor as unknown as Extractor;
|
||||
})();
|
||||
try {
|
||||
const ready = await state.extractor;
|
||||
state.status = 'ready';
|
||||
return ready;
|
||||
} catch (err) {
|
||||
state.extractor = null;
|
||||
state.status = 'error';
|
||||
const message = err instanceof AIError ? err.message : `The built-in embedding model could not be loaded: ${err instanceof Error ? err.message : String(err)}`;
|
||||
state.error = message;
|
||||
throw err instanceof AIError ? err : new AIError('network', `${message}. It is downloaded once (about 35 MB) from huggingface.co; check that the server can reach the internet.`, { cause: err, retryable: true });
|
||||
}
|
||||
}
|
||||
|
||||
/** Embeds texts with the built-in model. Vectors come back L2-normalised. */
|
||||
export async function localEmbed(dataDir: string, texts: string[], purpose: 'document' | 'query', signal?: AbortSignal): Promise<number[][]> {
|
||||
const extractor = await load(dataDir);
|
||||
const out: number[][] = [];
|
||||
for (let i = 0; i < texts.length; i += BATCH) {
|
||||
if (signal?.aborted) throw new AIError('aborted', 'The request was cancelled.');
|
||||
const batch = texts.slice(i, i + BATCH).map((t) => (purpose === 'query' ? QUERY_PREFIX : '') + (t.slice(0, MAX_CHARS) || ' '));
|
||||
const tensor = await extractor(batch, { pooling: 'cls', normalize: true });
|
||||
out.push(...tensor.tolist());
|
||||
// Let the event loop serve requests between batches; indexing runs in the background.
|
||||
await new Promise((resolve) => setImmediate(resolve));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -10,11 +10,12 @@ import type { DB } from '../db.ts';
|
|||
import { HttpError, notFound, type AppContext } from '../http.ts';
|
||||
import { getSetting, setSetting } from '../settings.ts';
|
||||
import { newId, now, parseJson } from '../util.ts';
|
||||
import { getAdapter, lookupModelDefaults } from './providers/index.ts';
|
||||
import { cachedInputPrice, cacheWriteRatio, getAdapter, lookupModelDefaults, priceMultiplier } from './providers/index.ts';
|
||||
import {
|
||||
AIError,
|
||||
DEFAULT_CAPABILITIES,
|
||||
PROVIDER_KINDS,
|
||||
REASONING_EFFORTS,
|
||||
type ChatMessage,
|
||||
type ChatRequest,
|
||||
type DiscoveredModel,
|
||||
|
|
@ -57,6 +58,7 @@ interface ModelRow {
|
|||
max_output: number | null;
|
||||
price_in: number | null;
|
||||
price_out: number | null;
|
||||
price_cached_in: number | null;
|
||||
}
|
||||
|
||||
export const TASKS = ['chat', 'generate', 'vision', 'quick'] as const;
|
||||
|
|
@ -82,7 +84,7 @@ const EMPTY_TASK: TaskConfig = { modelId: null, temperature: null, reasoningEffo
|
|||
export const taskConfigSchema = z.object({
|
||||
modelId: z.string().nullable(),
|
||||
temperature: z.number().min(0).max(2).nullable(),
|
||||
reasoningEffort: z.enum(['none', 'low', 'medium', 'high']).nullable(),
|
||||
reasoningEffort: z.enum(REASONING_EFFORTS).nullable(),
|
||||
maxOutputTokens: z.number().int().min(256).max(200000).nullable(),
|
||||
});
|
||||
|
||||
|
|
@ -122,6 +124,8 @@ function mapModel(row: ModelRow & { provider_name?: string; provider_kind?: stri
|
|||
maxOutput: row.max_output,
|
||||
priceIn: row.price_in,
|
||||
priceOut: row.price_out,
|
||||
/** Null = not set; costs then use the provider's usual cache discount. */
|
||||
priceCachedIn: row.price_cached_in,
|
||||
};
|
||||
}
|
||||
export type ModelDto = ReturnType<typeof mapModel>;
|
||||
|
|
@ -249,11 +253,12 @@ export async function discoverModels(app: AppContext, id: string): Promise<{ add
|
|||
if (current) {
|
||||
// Refresh facts the provider reports, but never overwrite prices or flags the user may have edited.
|
||||
app.db.run(
|
||||
'UPDATE ai_models SET context_window = COALESCE(?, context_window), max_output = COALESCE(?, max_output), price_in = COALESCE(price_in, ?), price_out = COALESCE(price_out, ?), updated_at = ? WHERE id = ?',
|
||||
'UPDATE ai_models SET context_window = COALESCE(?, context_window), max_output = COALESCE(?, max_output), price_in = COALESCE(price_in, ?), price_out = COALESCE(price_out, ?), price_cached_in = COALESCE(price_cached_in, ?), updated_at = ? WHERE id = ?',
|
||||
m.contextWindow ?? null,
|
||||
m.maxOutput ?? null,
|
||||
m.priceIn ?? null,
|
||||
m.priceOut ?? null,
|
||||
m.priceCachedIn ?? null,
|
||||
at,
|
||||
current.id,
|
||||
);
|
||||
|
|
@ -270,8 +275,8 @@ function insertModel(db: DB, providerId: string, m: DiscoveredModel, source: str
|
|||
const id = newId();
|
||||
const at = now();
|
||||
db.run(
|
||||
`INSERT INTO ai_models (id, provider_id, model, label, source, enabled, capabilities, context_window, max_output, price_in, price_out, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
`INSERT INTO ai_models (id, provider_id, model, label, source, enabled, capabilities, context_window, max_output, price_in, price_out, price_cached_in, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
id,
|
||||
providerId,
|
||||
m.id,
|
||||
|
|
@ -283,6 +288,7 @@ function insertModel(db: DB, providerId: string, m: DiscoveredModel, source: str
|
|||
m.maxOutput ?? null,
|
||||
m.priceIn ?? null,
|
||||
m.priceOut ?? null,
|
||||
m.priceCachedIn ?? null,
|
||||
at,
|
||||
at,
|
||||
);
|
||||
|
|
@ -331,6 +337,7 @@ export const modelPatchSchema = z.object({
|
|||
maxOutput: z.number().int().min(16).max(10_000_000).nullable().optional(),
|
||||
priceIn: z.number().min(0).max(100000).nullable().optional(),
|
||||
priceOut: z.number().min(0).max(100000).nullable().optional(),
|
||||
priceCachedIn: z.number().min(0).max(100000).nullable().optional(),
|
||||
});
|
||||
|
||||
export function patchModel(db: DB, id: string, input: z.input<typeof modelPatchSchema>): ModelDto {
|
||||
|
|
@ -339,7 +346,7 @@ export function patchModel(db: DB, id: string, input: z.input<typeof modelPatchS
|
|||
if (!row) throw notFound('Model');
|
||||
const caps = { ...parseJson<Partial<ModelCapabilities>>(row.capabilities, {}), ...patch.capabilities };
|
||||
db.run(
|
||||
'UPDATE ai_models SET enabled = ?, label = ?, capabilities = ?, context_window = ?, max_output = ?, price_in = ?, price_out = ?, updated_at = ? WHERE id = ?',
|
||||
'UPDATE ai_models SET enabled = ?, label = ?, capabilities = ?, context_window = ?, max_output = ?, price_in = ?, price_out = ?, price_cached_in = ?, updated_at = ? WHERE id = ?',
|
||||
patch.enabled === undefined ? row.enabled : patch.enabled ? 1 : 0,
|
||||
patch.label ?? row.label,
|
||||
JSON.stringify(caps),
|
||||
|
|
@ -347,6 +354,7 @@ export function patchModel(db: DB, id: string, input: z.input<typeof modelPatchS
|
|||
patch.maxOutput === undefined ? row.max_output : patch.maxOutput,
|
||||
patch.priceIn === undefined ? row.price_in : patch.priceIn,
|
||||
patch.priceOut === undefined ? row.price_out : patch.priceOut,
|
||||
patch.priceCachedIn === undefined ? row.price_cached_in : patch.priceCachedIn,
|
||||
now(),
|
||||
id,
|
||||
);
|
||||
|
|
@ -423,19 +431,33 @@ export function resolveModel(app: AppContext, task: TaskName, overrideModelId?:
|
|||
|
||||
/* ------------------------------ usage ------------------------------ */
|
||||
|
||||
export function estimateCost(model: Pick<ModelDto, 'priceIn' | 'priceOut'>, usage: Usage): number | null {
|
||||
/**
|
||||
* Estimated USD cost of one call. Input tokens are split into fresh, cache-hit and
|
||||
* cache-write tokens, each at its own rate: the model's cached price when set, otherwise
|
||||
* the provider's published cache discount; Claude's cache writes cost 1.25x. Providers
|
||||
* with time-of-day pricing (DeepSeek off-peak) are adjusted for when the call was made.
|
||||
*/
|
||||
export function estimateCost(
|
||||
model: Pick<ModelDto, 'priceIn' | 'priceOut' | 'model'> & { priceCachedIn?: number | null; providerKind?: ProviderKind | '' },
|
||||
usage: Usage,
|
||||
at: number = Date.now(),
|
||||
): number | null {
|
||||
if (model.priceIn === null && model.priceOut === null) return null;
|
||||
// Cached input is billed at a fraction of the list price by every provider; 10% is a close, conservative stand-in.
|
||||
const cached = usage.cacheReadTokens ?? 0;
|
||||
const fresh = Math.max(0, usage.inputTokens - cached);
|
||||
return ((fresh + cached * 0.1) * (model.priceIn ?? 0) + usage.outputTokens * (model.priceOut ?? 0)) / 1_000_000;
|
||||
const kind = (model.providerKind || 'openai_compatible') as ProviderKind;
|
||||
const priceIn = model.priceIn ?? 0;
|
||||
const cachedRate = cachedInputPrice(kind, model.model, model.priceIn, model.priceCachedIn ?? null);
|
||||
const cached = Math.min(usage.inputTokens, usage.cacheReadTokens ?? 0);
|
||||
const written = Math.min(usage.inputTokens - cached, usage.cacheWriteTokens ?? 0);
|
||||
const fresh = Math.max(0, usage.inputTokens - cached - written);
|
||||
const input = fresh * priceIn + cached * cachedRate + written * priceIn * cacheWriteRatio(kind);
|
||||
return ((input + usage.outputTokens * (model.priceOut ?? 0)) * priceMultiplier(kind, at)) / 1_000_000;
|
||||
}
|
||||
|
||||
function recordUsage(app: AppContext, r: ResolvedModel, task: string, usage: Usage | null, started: number, error?: string): void {
|
||||
try {
|
||||
app.db.run(
|
||||
`INSERT INTO ai_usage (at, provider_id, provider_name, model, task, input_tokens, output_tokens, cache_read_tokens, cost, ok, error, duration_ms)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
`INSERT INTO ai_usage (at, provider_id, provider_name, model, task, input_tokens, output_tokens, cache_read_tokens, cache_write_tokens, cost, ok, error, duration_ms)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
now(),
|
||||
r.provider.id,
|
||||
r.provider.name,
|
||||
|
|
@ -444,6 +466,7 @@ function recordUsage(app: AppContext, r: ResolvedModel, task: string, usage: Usa
|
|||
usage?.inputTokens ?? 0,
|
||||
usage?.outputTokens ?? 0,
|
||||
usage?.cacheReadTokens ?? 0,
|
||||
usage?.cacheWriteTokens ?? 0,
|
||||
usage ? estimateCost(r.model, usage) : null,
|
||||
error ? 0 : 1,
|
||||
(error ?? '').slice(0, 300),
|
||||
|
|
@ -597,7 +620,7 @@ export async function generateStructured(app: AppContext, opts: StructuredOption
|
|||
export function usageSummary(db: DB, days: number) {
|
||||
const since = now() - days * 86_400_000;
|
||||
const byModel = db.all<Record<string, any>>(
|
||||
`SELECT provider_name AS provider, model, COUNT(*) AS calls, SUM(input_tokens) AS input, SUM(output_tokens) AS output, SUM(cost) AS cost,
|
||||
`SELECT provider_name AS provider, model, COUNT(*) AS calls, SUM(input_tokens) AS input, SUM(cache_read_tokens) AS cached, SUM(output_tokens) AS output, SUM(cost) AS cost,
|
||||
SUM(CASE WHEN ok = 0 THEN 1 ELSE 0 END) AS errors, SUM(CASE WHEN cost IS NULL AND ok = 1 THEN 1 ELSE 0 END) AS unpriced
|
||||
FROM ai_usage WHERE at >= ? GROUP BY provider_name, model ORDER BY cost DESC, calls DESC`,
|
||||
since,
|
||||
|
|
@ -607,7 +630,7 @@ export function usageSummary(db: DB, days: number) {
|
|||
since,
|
||||
);
|
||||
const total = db.get<Record<string, any>>(
|
||||
`SELECT COUNT(*) AS calls, COALESCE(SUM(input_tokens), 0) AS input, COALESCE(SUM(output_tokens), 0) AS output, COALESCE(SUM(cost), 0) AS cost FROM ai_usage WHERE at >= ?`,
|
||||
`SELECT COUNT(*) AS calls, COALESCE(SUM(input_tokens), 0) AS input, COALESCE(SUM(cache_read_tokens), 0) AS cached, COALESCE(SUM(output_tokens), 0) AS output, COALESCE(SUM(cost), 0) AS cost FROM ai_usage WHERE at >= ?`,
|
||||
since,
|
||||
)!;
|
||||
const recentErrors = db.all<Record<string, any>>(`SELECT at, provider_name AS provider, model, task, error FROM ai_usage WHERE ok = 0 AND at >= ? ORDER BY at DESC LIMIT 10`, since);
|
||||
|
|
|
|||
|
|
@ -6,9 +6,9 @@
|
|||
* `server/ai/providers/` may depend on a provider-specific request or response.
|
||||
*/
|
||||
|
||||
export type ProviderKind = 'openai' | 'anthropic' | 'gemini' | 'openai_compatible' | 'ollama';
|
||||
export type ProviderKind = 'openai' | 'anthropic' | 'gemini' | 'deepseek' | 'openai_compatible' | 'ollama';
|
||||
|
||||
export const PROVIDER_KINDS: ProviderKind[] = ['openai', 'anthropic', 'gemini', 'openai_compatible', 'ollama'];
|
||||
export const PROVIDER_KINDS: ProviderKind[] = ['openai', 'anthropic', 'gemini', 'deepseek', 'openai_compatible', 'ollama'];
|
||||
|
||||
/** Decrypted provider connection, only ever held server-side. */
|
||||
export interface ProviderConfig {
|
||||
|
|
@ -51,7 +51,12 @@ export interface ToolDef {
|
|||
parameters: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export type ReasoningEffort = 'none' | 'low' | 'medium' | 'high';
|
||||
/**
|
||||
* Reasoning effort, from "off" to the most a provider offers. Adapters map each level onto
|
||||
* the nearest value their API accepts (see `nearestEffort`).
|
||||
*/
|
||||
export const REASONING_EFFORTS = ['none', 'minimal', 'low', 'medium', 'high', 'xhigh', 'max'] as const;
|
||||
export type ReasoningEffort = (typeof REASONING_EFFORTS)[number];
|
||||
|
||||
export interface ModelCapabilities {
|
||||
tools: boolean;
|
||||
|
|
@ -108,6 +113,8 @@ export interface DiscoveredModel {
|
|||
/** USD per million tokens, when known. */
|
||||
priceIn?: number;
|
||||
priceOut?: number;
|
||||
/** USD per million cached (cache-hit) input tokens, when the provider prices them separately. */
|
||||
priceCachedIn?: number;
|
||||
}
|
||||
|
||||
export interface TestResult {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue