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

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:
Elijah 2026-11-15 12:48:17 -07:00
parent ccdad9728b
commit f0aabe4d5d
63 changed files with 3892 additions and 488 deletions

View file

@ -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);