feat(ai): Paperclip-style env-var provider presets + zero-config local default

Two product decisions from tonight:

1. Public AI providers can now be published by an admin as named presets
   (lib/ai/types.ts's PublicAiPreset: name/baseUrl/model/apiKeyEnvVar).
   The admin names an env var, never a secret value - the actual key is
   whatever ops has set in the server's real environment, same custody
   model as the existing AI_SERVER_BASE_URL var. A new server route
   (app/api/ai/public/chat) resolves it and makes the call itself, which
   also sidesteps the CORS/wrong-base-URL failure class chatPublic hit
   earlier tonight. Users pick a preset from a dropdown in Settings -
   Answer with - no key field at all; personal BYOK (paste your own key)
   stays available as a secondary "Add your own key" option, not removed.
   Admin UI: new "Public - org-managed presets" card in the AI policy tab.

2. AI now defaults ON instead of requiring setup (lib/ai/auto-provision.ts):
   on first load, if no provider is chosen yet, probe OpenCode (this app
   auto-spawns `opencode serve` itself, so it's the one local option with
   zero external install step) then Ollama via the existing auto-discovery,
   and adopt whichever answers. Never overrides an explicit choice - only
   fires while provider is still null. Wired into both AI entry points
   (the Ask button and the Settings pane) so it resolves before either
   renders its "not configured" state.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Bernd Rodler
2026-08-07 14:01:00 +02:00
co-authored by Claude Sonnet 5
parent 1aa0a4686b
commit f121678e2a
13 changed files with 580 additions and 17 deletions
+87
View File
@@ -0,0 +1,87 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { DEFAULT_AI_ENTITLEMENT, DEFAULT_AI_POLICY, type AiPolicy } from '../types';
import { loadAiSettings } from '../local-settings';
const { listOpencodeModels } = vi.hoisted(() => ({ listOpencodeModels: vi.fn() }));
vi.mock('../local-client', () => ({ listOpencodeModels }));
const { discoverLocalOllama, recommendDefaultModel } = vi.hoisted(() => ({
discoverLocalOllama: vi.fn(),
recommendDefaultModel: vi.fn(),
}));
vi.mock('../local-discovery', () => ({ discoverLocalOllama, recommendDefaultModel }));
const { supportsLocalLlm } = vi.hoisted(() => ({ supportsLocalLlm: vi.fn(() => true) }));
vi.mock('../../platform-capabilities', () => ({ supportsLocalLlm }));
// Imported after the mocks so it picks up the mocked modules.
const { ensureDefaultProvider, _resetAutoProvisionForTests } = await import('../auto-provision');
function policyWith(classes: AiPolicy['entitlement']['classes']): AiPolicy {
return { ...DEFAULT_AI_POLICY, entitlement: { ...DEFAULT_AI_ENTITLEMENT, classes } };
}
describe('ensureDefaultProvider', () => {
beforeEach(() => {
window.localStorage.clear();
_resetAutoProvisionForTests();
listOpencodeModels.mockReset();
discoverLocalOllama.mockReset();
recommendDefaultModel.mockReset();
supportsLocalLlm.mockReturnValue(true);
});
afterEach(() => {
vi.unstubAllGlobals();
});
it('prefers OpenCode when it has a usable model', async () => {
listOpencodeModels.mockResolvedValue([{ ref: 'opencode/deepseek-v4-flash-free', label: 'DeepSeek V4 Flash Free' }]);
const next = await ensureDefaultProvider(policyWith(['opencode', 'local']));
expect(next.provider).toBe('opencode');
expect(next.opencodeModel).toBe('opencode/deepseek-v4-flash-free');
expect(discoverLocalOllama).not.toHaveBeenCalled();
expect(loadAiSettings().provider).toBe('opencode'); // persisted, not just returned
});
it('falls back to Ollama when OpenCode is unreachable', async () => {
listOpencodeModels.mockRejectedValue(new Error('No local OpenCode server is running'));
discoverLocalOllama.mockResolvedValue({ baseUrl: 'http://127.0.0.1:11434', models: [{ name: 'qwen2.5:32b' }] });
recommendDefaultModel.mockReturnValue('qwen2.5:32b');
const next = await ensureDefaultProvider(policyWith(['opencode', 'local']));
expect(next.provider).toBe('local');
expect(next.localModel).toBe('qwen2.5:32b');
});
it('leaves provider unset when neither is available', async () => {
listOpencodeModels.mockResolvedValue([]);
discoverLocalOllama.mockResolvedValue(null);
const next = await ensureDefaultProvider(policyWith(['opencode', 'local']));
expect(next.provider).toBeNull();
});
it('never overrides an explicit choice already saved', async () => {
const { saveAiSettings, DEFAULT_AI_SETTINGS } = await import('../local-settings');
saveAiSettings({ ...DEFAULT_AI_SETTINGS, provider: 'server', serverModel: 'qwen2.5:32b' });
const next = await ensureDefaultProvider(policyWith(['opencode', 'local', 'server']));
expect(next.provider).toBe('server');
expect(listOpencodeModels).not.toHaveBeenCalled();
});
it('only probes once per module lifetime even if called again', async () => {
listOpencodeModels.mockResolvedValue([]);
discoverLocalOllama.mockResolvedValue(null);
await ensureDefaultProvider(policyWith(['opencode', 'local']));
await ensureDefaultProvider(policyWith(['opencode', 'local']));
expect(listOpencodeModels).toHaveBeenCalledTimes(1);
});
});
+34 -1
View File
@@ -1,5 +1,5 @@
import { describe, it, expect, vi, afterEach } from 'vitest';
import { chatPublic } from '../local-client';
import { chatPublic, chatPublicManaged } from '../local-client';
describe('chatPublic', () => {
afterEach(() => {
@@ -46,3 +46,36 @@ describe('chatPublic', () => {
).resolves.toBe('hello there');
});
});
describe('chatPublicManaged', () => {
afterEach(() => {
vi.unstubAllGlobals();
});
it('posts presetId (never a key) to the same-origin route', async () => {
const fetchMock = vi.fn().mockResolvedValue({
ok: true,
json: async () => ({ answer: 'hi from the org preset' }),
});
vi.stubGlobal('fetch', fetchMock);
const answer = await chatPublicManaged('preset-abc123', [{ role: 'user', content: 'hi' }]);
expect(answer).toBe('hi from the org preset');
expect(fetchMock).toHaveBeenCalledWith('/api/ai/public/chat', expect.objectContaining({
method: 'POST',
body: JSON.stringify({ presetId: 'preset-abc123', messages: [{ role: 'user', content: 'hi' }] }),
}));
});
it('surfaces the server-side error (e.g. env var not set) verbatim', async () => {
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
ok: false,
status: 503,
json: async () => ({ error: 'Env var "DEEPSEEK_API_KEY" is not set on the server for preset "DeepSeek (org)"' }),
}));
await expect(chatPublicManaged('preset-abc123', [{ role: 'user', content: 'hi' }]))
.rejects.toThrow(/Env var "DEEPSEEK_API_KEY" is not set/);
});
});
+22
View File
@@ -0,0 +1,22 @@
import { describe, it, expect } from 'vitest';
import { presetActiveId, isPresetActiveId, presetIdFromActiveId } from '../local-settings';
describe('preset active-id helpers', () => {
it('round-trips a preset id through the prefixed activeProfileId space', () => {
const activeId = presetActiveId('preset-abc123');
expect(activeId).toBe('preset:preset-abc123');
expect(isPresetActiveId(activeId)).toBe(true);
expect(presetIdFromActiveId(activeId)).toBe('preset-abc123');
});
it('does not mistake a personal profile id for a preset id', () => {
const profileId = 'profile-xyz789-abc123';
expect(isPresetActiveId(profileId)).toBe(false);
expect(presetIdFromActiveId(profileId)).toBeNull();
});
it('handles null safely', () => {
expect(isPresetActiveId(null)).toBe(false);
expect(presetIdFromActiveId(null)).toBeNull();
});
});
+66
View File
@@ -0,0 +1,66 @@
// First-run, zero-config default provider (product decision 2026-08-07:
// "user wants to use AI so set the local one to on always by default" — not
// "user wants to configure AI"). Before this, a fresh install left
// `settings.provider` at `null` and every AI entry point just told the user
// to go set one up in Settings.
//
// Priority: OpenCode first, then Ollama. OpenCode is the one local option
// this app controls end to end — electron/main.ts auto-spawns
// `opencode serve` itself, so "OpenCode has a model" only depends on what's
// already authenticated in its own auth.json, not on the user having
// installed anything separately. Ollama is second because it's an external
// dependency the user must have installed and started themselves — real,
// but not zero-config the way OpenCode is here.
//
// Never overrides an explicit choice: fires only while `provider` is still
// `null`, and at most once per page load (module-level `attempted`) so a
// component re-mounting doesn't re-probe on every render.
import { loadAiSettings, saveAiSettings, type AiLocalSettings } from './local-settings';
import { listOpencodeModels } from './local-client';
import { discoverLocalOllama, recommendDefaultModel } from './local-discovery';
import { supportsLocalLlm } from '../platform-capabilities';
import type { AiPolicy } from './types';
let attempted = false;
/** Test-only: lets a fresh module state be simulated without a full reload. */
export function _resetAutoProvisionForTests(): void {
attempted = false;
}
export async function ensureDefaultProvider(policy: AiPolicy): Promise<AiLocalSettings> {
const current = loadAiSettings();
if (current.provider !== null || attempted) return current;
attempted = true;
if (policy.entitlement.classes.includes('opencode')) {
try {
const models = await listOpencodeModels();
if (models[0]) {
const next: AiLocalSettings = { ...current, provider: 'opencode', opencodeModel: models[0].ref };
saveAiSettings(next);
return next;
}
} catch {
// opencode not reachable yet (still starting, or the CLI isn't
// installed) — fall through to Ollama rather than surfacing an error
// for a default the user never asked for.
}
}
if (supportsLocalLlm() && policy.entitlement.classes.includes('local')) {
const discovery = await discoverLocalOllama();
if (discovery) {
const recommended = recommendDefaultModel(discovery.models) ?? discovery.models[0]?.name ?? null;
if (recommended) {
const next: AiLocalSettings = {
...current, provider: 'local', localBaseUrl: discovery.baseUrl, localModel: recommended,
};
saveAiSettings(next);
return next;
}
}
}
return current;
}
+30 -3
View File
@@ -169,6 +169,24 @@ export async function chatPublic(
return content;
}
// ── Public, admin-managed presets — the Paperclip-style alternative to
// pasting a personal key (decision 2026-08-07). The client only ever sends a
// presetId; the server resolves the actual key from its own environment (see
// app/api/ai/public/chat/route.ts) and makes the call itself, which also
// sidesteps the CORS/wrong-base-URL failure class chatPublic is exposed to. ──
export async function chatPublicManaged(presetId: string, messages: ChatMessage[]): Promise<string> {
const res = await fetch('/api/ai/public/chat', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ presetId, messages }),
});
const body = await res.json().catch(() => ({}));
if (!res.ok) throw new Error(body?.error || `Provider returned ${res.status}`);
if (!body?.answer) throw new Error('Provider returned no message content');
return body.answer as string;
}
// ── OpenCode: a locally-running `opencode serve` (github.com/sst/opencode),
// the same agent runtime Paperclip drives as an adapter. Reached through THIS
// app's own backend (app/api/ai/opencode/*) rather than directly, for the same
@@ -438,7 +456,12 @@ export interface AskConfig {
localBaseUrl: string;
localModel: string | null;
serverModel: string | null;
/** Personal BYOK profile — mutually exclusive with publicPresetId; the
* caller sets exactly one depending on which the user picked. */
publicProfile: ResolvedPublicProfile | null;
/** Admin-managed preset id (see chatPublicManaged) — mutually exclusive
* with publicProfile. */
publicPresetId?: string | null;
opencodeModel?: string | null;
/** Cookie slot of the account whose local index should be searched. Omitting
* it reads whichever account the resolver finds first — see fetchLocalLeg. */
@@ -452,7 +475,7 @@ export async function askMail(question: string, config: AskConfig): Promise<AskR
if (config.provider === 'server' && !config.serverModel) {
throw new Error('No server model selected');
}
if (config.provider === 'public' && !config.publicProfile) {
if (config.provider === 'public' && !config.publicProfile && !config.publicPresetId) {
throw new Error('No provider profile selected');
}
if (config.provider === 'opencode' && !config.opencodeModel) {
@@ -467,8 +490,12 @@ export async function askMail(question: string, config: AskConfig): Promise<AskR
let answer: string;
let seatJustAssigned = false;
if (config.provider === 'public') {
const profile = config.publicProfile as ResolvedPublicProfile;
answer = await chatPublic(profile.baseUrl, profile.apiKey, profile.model, messages);
if (config.publicPresetId) {
answer = await chatPublicManaged(config.publicPresetId, messages);
} else {
const profile = config.publicProfile as ResolvedPublicProfile;
answer = await chatPublic(profile.baseUrl, profile.apiKey, profile.model, messages);
}
} else if (config.provider === 'server') {
const result = await chatServer(config.serverModel as string, messages);
answer = result.answer;
+20
View File
@@ -50,6 +50,26 @@ function newProfileId(): string {
return `profile-${Math.random().toString(36).slice(2, 10)}-${Math.random().toString(36).slice(2, 10)}`;
}
/**
* `activeProfileId` names either a personal BYOK profile (its own id, as
* always) or an admin-managed preset (see PublicAiPreset), prefixed so the
* two id spaces can never collide without adding a second field everywhere
* that reads/writes activeProfileId.
*/
const PRESET_PREFIX = 'preset:';
export function presetActiveId(presetId: string): string {
return PRESET_PREFIX + presetId;
}
export function isPresetActiveId(activeId: string | null): boolean {
return !!activeId && activeId.startsWith(PRESET_PREFIX);
}
export function presetIdFromActiveId(activeId: string | null): string | null {
return activeId && activeId.startsWith(PRESET_PREFIX) ? activeId.slice(PRESET_PREFIX.length) : null;
}
/** One-time upgrade from the earlier single-profile shape (a bare
* publicBaseUrl/publicModel pair) into the profile list, so a browser that
* already saved settings before profiles existed doesn't just lose them. */
+36
View File
@@ -22,6 +22,33 @@
export type AiClass = 'local' | 'server' | 'public' | 'opencode';
/**
* An admin-published, server-managed `public`-class provider — the
* Paperclip-style alternative to a user pasting their own key (decision
* 2026-08-07): the admin names an env var (e.g. "DEEPSEEK_API_KEY") instead
* of typing a secret value anywhere in this config. The actual value is
* whatever ops has set in the server's real environment (k8s secret, .env,
* Electron packaging) — same custody model as the existing AI_SERVER_BASE_URL
* var, just admin-nameable instead of hardcoded. Resolved server-side only,
* in app/api/ai/public/chat/route.ts; never sent to a browser.
*/
export interface PublicAiPreset {
id: string;
name: string;
baseUrl: string;
model: string;
apiKeyEnvVar: string;
}
/** What a client is allowed to know about a preset — no baseUrl/apiKeyEnvVar,
* since the client only ever refers to a preset by id and never calls the
* provider itself. */
export interface PublicAiPresetOption {
id: string;
name: string;
model: string;
}
export interface AiEntitlement {
licensed: boolean;
subject: 'user' | 'tenant';
@@ -45,6 +72,9 @@ export interface AiPolicy {
/** Base-URL prefixes a BYOK profile's baseUrl must match. null = unrestricted
* (today's behavior). Advisory/client-side only — see spec §6.1. */
publicProviderAllowlist: string[] | null;
/** Admin-managed provider presets available to every user (see
* PublicAiPreset) — sanitized to {id,name,model} for the client. */
publicPresets: PublicAiPresetOption[];
}
export const DEFAULT_AI_ENTITLEMENT: AiEntitlement = {
@@ -63,6 +93,7 @@ export const DEFAULT_AI_POLICY: AiPolicy = {
retrievalEnabled: true,
consent: null,
publicProviderAllowlist: null,
publicPresets: [],
};
// Admin-authored console config (docs/AI-ASSISTANT-CONCEPT.md §6 /
@@ -83,6 +114,10 @@ export interface AiConsoleConfig {
/** null = unrestricted BYOK base URLs (today's behavior, unchanged).
* Non-null = base URL must start with one of these prefixes. */
publicProviderAllowlist: string[] | null;
/** Server-managed `public`-class presets — the Paperclip-style env-var-key
* picker (decision 2026-08-07). Empty by default; adding one here is what
* makes it show up in every user's Settings picker and in AiPolicy.publicPresets. */
publicPresets: PublicAiPreset[];
/** Master switch for the retrieval leg (mail-content → embeddings).
* Independent of classesEnabled.server. Defaults true. */
retrievalEnabled: boolean;
@@ -93,6 +128,7 @@ export const DEFAULT_AI_CONSOLE_CONFIG: AiConsoleConfig = {
classesEnabled: {},
serverModelAllowlist: null,
publicProviderAllowlist: null,
publicPresets: [],
retrievalEnabled: true,
consent: null,
};