Adds a server-side "small model" capability: direct, cheap LLM calls that reuse the user's existing OpenCode provider logins — the mechanism OpenCode uses internally for titles and summaries but does not expose through the SDK or plugins. Zero new dependencies; plain fetch with per-provider wire formats, credentials never leave the server. Core (packages/web/server/lib/small-model): - Resolution mirrors OpenCode's session scoping: explicit settings override → small_model from the OpenCode config → family scan within the session's provider → the session's own model. The global provider scan only serves callers without a session context, and background callers forbid it entirely (restrictToPreferredProvider), so conversation content never reaches a provider the user didn't pick — explicit choices excepted. - Per-provider auth replicating OpenCode's plugin loaders: GitHub Copilot (device token as bearer, no exchange), ChatGPT plan via the codex Responses API (single-flight OAuth refresh written back to auth.json), Anthropic messages, Google generateContent, generic OpenAI-compatible. - OpenCode's free models (opencode/big-pickle, *-free) are never called directly; unauthenticated providers are skipped by design. - Prompt clamping to the model's catalog context limit; thinking disabled where a wire switch exists (Z.AI/GLM, MiniMax-M3, Gemini Flash); robust content parsing with a clear error when a thinking model spends its whol budget on reasoning. - Settings → Sessions gains a Small Model group: use-default checkbox plus an override picker limited to authenticated providers, persisted with web/desktop/VS Code sanitization parity. Consumers: - Session assist: a server-side watcher on the global SSE hub generates a short recap and one suggested follow-up after a session idles quietly fo a minute, stored on session metadata (openchamber.assist). Freshness is keyed to the last assistant message id, so new activity invalidates the payload everywhere with no extra writes. The chat shows the recap under the last message after five quiet minutes and the suggestion as a dismissible chip above the composer (tap fills the input, never sends). Gated by a new Chat setting (default on) that is a hard generation switch. Language is anchored to the conversation itself, with a script-mismatch guard against model/backend language hallucination. - TTS: a third input mode, summarized — long replies are condensed to spoken prose before playback on any TTS engine. - Git: commit-message and PR generation moved off the active chat session onto the small model fed with real diffs and the commit list (bodies included), with a session-transport fallback for free-model-only setups. - Notes: Add to notes distills long selections into 1-3 dense sentences preserving exact identifiers, with verbatim fallback on failure. Fixes along the way: - The global event watcher now starts unconditionally; it was gated behind the desktop-notify env, leaving the server-side event hub dead in packaged apps. - OpenCode re-emits message.updated for old user messages after idle; the watcher no longer mistakes those for new activity. - Session metadata merges from a fresh read right before the PATCH, so writes made during the generation window (suggestion dismissals, review links) are preserved; the assist runtime stops during graceful shutdown.
198 lines
7.1 KiB
JavaScript
198 lines
7.1 KiB
JavaScript
import { describe, it, expect } from 'bun:test';
|
|
import { resolveSmallModel, parseModelRef, isUsableAuthEntry } from './resolve.js';
|
|
|
|
const catalog = {
|
|
google: {
|
|
id: 'google',
|
|
models: {
|
|
'gemini-2.5-flash': { id: 'gemini-2.5-flash', family: 'gemini-flash', release_date: '2025-06-01' },
|
|
'gemini-2.0-flash': { id: 'gemini-2.0-flash', family: 'gemini-flash', release_date: '2024-12-01' },
|
|
'gemini-2.5-pro': { id: 'gemini-2.5-pro', family: 'gemini-pro', release_date: '2025-06-01' },
|
|
},
|
|
},
|
|
anthropic: {
|
|
id: 'anthropic',
|
|
models: {
|
|
'claude-haiku-4-5': { id: 'claude-haiku-4-5', family: 'claude-haiku', release_date: '2025-10-01' },
|
|
'claude-sonnet-4-5': { id: 'claude-sonnet-4-5', family: 'claude-sonnet', release_date: '2025-09-01' },
|
|
},
|
|
},
|
|
};
|
|
|
|
describe('parseModelRef', () => {
|
|
it('splits provider/model on the first slash', () => {
|
|
expect(parseModelRef('anthropic/claude-haiku-4-5')).toEqual({
|
|
providerID: 'anthropic',
|
|
modelID: 'claude-haiku-4-5',
|
|
});
|
|
});
|
|
|
|
it('keeps slashes inside the model id', () => {
|
|
expect(parseModelRef('openrouter/google/gemini-2.5-flash')).toEqual({
|
|
providerID: 'openrouter',
|
|
modelID: 'google/gemini-2.5-flash',
|
|
});
|
|
});
|
|
|
|
it('rejects values without a provider or model part', () => {
|
|
expect(parseModelRef('anthropic/')).toBeNull();
|
|
expect(parseModelRef('/model')).toBeNull();
|
|
expect(parseModelRef('plain')).toBeNull();
|
|
expect(parseModelRef(undefined)).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe('isUsableAuthEntry', () => {
|
|
it('accepts api keys, oauth tokens, and wellknown tokens', () => {
|
|
expect(isUsableAuthEntry({ type: 'api', key: 'sk-x' })).toBe(true);
|
|
expect(isUsableAuthEntry({ type: 'oauth', access: 'a', refresh: 'r', expires: 0 })).toBe(true);
|
|
expect(isUsableAuthEntry({ type: 'wellknown', key: 'k', token: 't' })).toBe(true);
|
|
});
|
|
|
|
it('rejects empty or malformed entries', () => {
|
|
expect(isUsableAuthEntry({ type: 'api', key: '' })).toBe(false);
|
|
expect(isUsableAuthEntry({ type: 'oauth' })).toBe(false);
|
|
expect(isUsableAuthEntry(null)).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('resolveSmallModel', () => {
|
|
it('gives the OpenChamber settings override top priority', () => {
|
|
const result = resolveSmallModel({
|
|
auth: { anthropic: { type: 'api', key: 'sk-x' } },
|
|
catalog,
|
|
settingsSmallModel: 'anthropic/claude-haiku-4-5',
|
|
configSmallModel: 'openai/gpt-4o-mini',
|
|
preferredProviderID: 'anthropic',
|
|
});
|
|
expect(result).toEqual({ providerID: 'anthropic', modelID: 'claude-haiku-4-5', source: 'settings' });
|
|
});
|
|
|
|
it('prefers the configured small_model', () => {
|
|
const result = resolveSmallModel({
|
|
auth: { anthropic: { type: 'api', key: 'sk-x' } },
|
|
catalog,
|
|
configSmallModel: 'openai/gpt-4o-mini',
|
|
});
|
|
expect(result).toEqual({ providerID: 'openai', modelID: 'gpt-4o-mini', source: 'config' });
|
|
});
|
|
|
|
it('scans authenticated providers by family priority, newest first', () => {
|
|
const result = resolveSmallModel({
|
|
auth: {
|
|
google: { type: 'api', key: 'g-key' },
|
|
anthropic: { type: 'api', key: 'sk-x' },
|
|
},
|
|
catalog,
|
|
configSmallModel: null,
|
|
});
|
|
expect(result).toEqual({ providerID: 'google', modelID: 'gemini-2.5-flash', source: 'family-scan' });
|
|
});
|
|
|
|
it('skips providers without a usable credential', () => {
|
|
const result = resolveSmallModel({
|
|
auth: {
|
|
google: { type: 'api', key: '' },
|
|
anthropic: { type: 'api', key: 'sk-x' },
|
|
},
|
|
catalog,
|
|
configSmallModel: null,
|
|
});
|
|
expect(result).toEqual({ providerID: 'anthropic', modelID: 'claude-haiku-4-5', source: 'family-scan' });
|
|
});
|
|
|
|
it('falls back to Copilot utility models when only Copilot is logged in', () => {
|
|
const result = resolveSmallModel({
|
|
auth: { 'github-copilot': { type: 'oauth', access: 't', refresh: 't', expires: 0 } },
|
|
catalog,
|
|
configSmallModel: null,
|
|
});
|
|
expect(result?.providerID).toBe('github-copilot');
|
|
expect(result?.source).toBe('copilot-utility');
|
|
});
|
|
|
|
it('returns null when nothing is authenticated', () => {
|
|
expect(resolveSmallModel({ auth: {}, catalog, configSmallModel: null })).toBeNull();
|
|
});
|
|
|
|
it('prefers the session provider over other authenticated providers', () => {
|
|
const result = resolveSmallModel({
|
|
auth: {
|
|
google: { type: 'api', key: 'g-key' },
|
|
anthropic: { type: 'api', key: 'sk-x' },
|
|
},
|
|
catalog,
|
|
configSmallModel: null,
|
|
preferredProviderID: 'anthropic',
|
|
});
|
|
expect(result).toEqual({ providerID: 'anthropic', modelID: 'claude-haiku-4-5', source: 'family-scan' });
|
|
});
|
|
|
|
it('ignores a preferred provider without a usable login', () => {
|
|
const result = resolveSmallModel({
|
|
auth: { google: { type: 'api', key: 'g-key' } },
|
|
catalog,
|
|
configSmallModel: null,
|
|
preferredProviderID: 'anthropic',
|
|
});
|
|
expect(result).toEqual({ providerID: 'google', modelID: 'gemini-2.5-flash', source: 'family-scan' });
|
|
});
|
|
|
|
it('never uses a session provider without a login (opencode free models)', () => {
|
|
// Vanilla setups default the picker to opencode/big-pickle with no
|
|
// opencode token — those free models only work through OpenCode itself
|
|
// and must never be called directly, so the session context is ignored.
|
|
const result = resolveSmallModel({
|
|
auth: { openai: { type: 'oauth', access: 'a', refresh: 'r', expires: Date.now() + 60_000 } },
|
|
catalog,
|
|
configSmallModel: null,
|
|
preferredProviderID: 'opencode',
|
|
preferredModelID: 'big-pickle',
|
|
});
|
|
expect(result).toEqual({ providerID: 'openai', modelID: 'gpt-5.4-mini', source: 'codex-small' });
|
|
});
|
|
|
|
it('resolves nothing on a vanilla setup with no logins at all', () => {
|
|
const result = resolveSmallModel({
|
|
auth: {},
|
|
catalog,
|
|
configSmallModel: null,
|
|
preferredProviderID: 'opencode',
|
|
preferredModelID: 'big-pickle',
|
|
});
|
|
expect(result).toBeNull();
|
|
});
|
|
|
|
it('falls back to the session model instead of scanning other providers', () => {
|
|
const result = resolveSmallModel({
|
|
auth: {
|
|
'opencode-go': { type: 'api', key: 'oc-key' },
|
|
openai: { type: 'oauth', access: 'a', refresh: 'r', expires: Date.now() + 60_000 },
|
|
},
|
|
catalog: {
|
|
'opencode-go': {
|
|
id: 'opencode-go',
|
|
models: {
|
|
'deepseek-v4-flash': { id: 'deepseek-v4-flash', family: 'deepseek-flash', release_date: '2026-01-01' },
|
|
},
|
|
},
|
|
},
|
|
configSmallModel: null,
|
|
preferredProviderID: 'opencode-go',
|
|
preferredModelID: 'deepseek-v4-flash',
|
|
});
|
|
expect(result).toEqual({ providerID: 'opencode-go', modelID: 'deepseek-v4-flash', source: 'session-model' });
|
|
});
|
|
|
|
it('falls back to the session model itself when nothing resolves', () => {
|
|
const result = resolveSmallModel({
|
|
auth: { mistral: { type: 'api', key: 'm-key' } },
|
|
catalog,
|
|
configSmallModel: null,
|
|
preferredProviderID: 'mistral',
|
|
preferredModelID: 'mistral-large-latest',
|
|
});
|
|
expect(result).toEqual({ providerID: 'mistral', modelID: 'mistral-large-latest', source: 'session-model' });
|
|
});
|
|
});
|