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>
82 lines
2.7 KiB
TypeScript
82 lines
2.7 KiB
TypeScript
import { describe, it, expect, vi, afterEach } from 'vitest';
|
|
import { chatPublic, chatPublicManaged } from '../local-client';
|
|
|
|
describe('chatPublic', () => {
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals();
|
|
});
|
|
|
|
it('turns a network/CORS-level failure into an actionable message naming the Base URL', async () => {
|
|
vi.stubGlobal('fetch', vi.fn().mockRejectedValue(new TypeError('Failed to fetch')));
|
|
|
|
await expect(
|
|
chatPublic('https://platform.deepseek.com/', 'sk-test', 'deepseek-chat', [
|
|
{ role: 'user', content: 'hi' },
|
|
]),
|
|
).rejects.toThrow(/Could not reach https:\/\/platform\.deepseek\.com\/chat\/completions/);
|
|
});
|
|
|
|
it('still reports the provider-returned status when the request completes', async () => {
|
|
vi.stubGlobal(
|
|
'fetch',
|
|
vi.fn().mockResolvedValue({ ok: false, status: 401, json: async () => ({}) }),
|
|
);
|
|
|
|
await expect(
|
|
chatPublic('https://api.deepseek.com', 'sk-bad', 'deepseek-chat', [
|
|
{ role: 'user', content: 'hi' },
|
|
]),
|
|
).rejects.toThrow('Provider returned 401');
|
|
});
|
|
|
|
it('returns the message content on success', async () => {
|
|
vi.stubGlobal(
|
|
'fetch',
|
|
vi.fn().mockResolvedValue({
|
|
ok: true,
|
|
status: 200,
|
|
json: async () => ({ choices: [{ message: { content: 'hello there' } }] }),
|
|
}),
|
|
);
|
|
|
|
await expect(
|
|
chatPublic('https://api.deepseek.com', 'sk-good', 'deepseek-chat', [
|
|
{ role: 'user', content: 'hi' },
|
|
]),
|
|
).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/);
|
|
});
|
|
});
|