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

@ -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(