974c3eb07d
Five providers behind one streaming abstraction:
- claude (Anthropic)
- openai (api.openai.com)
- openai-compatible (any base URL — OpenRouter / LiteLLM /
vLLM / Together / your own gateway)
- gemini (Google)
- ollama (self-hosted; no key; LAN URL like
http://ollama.embassy:11434)
The "self-hosted Ollama on Start9" angle is the killer use case —
configure Settings → AI integration with the LAN URL of your Ollama
service and no API keys ever leave your network.
Architecture
- lib/ai/types.ts LLMProvider streaming interface
- lib/ai/sse.ts shared SSE + NDJSON line iterators
- lib/ai/providers/*.ts 5 implementations + factory
- lib/ai/programSchema.ts Zod schema + JSON-schema-for-prompt +
parseAIProgram with markdown-fence
stripping and balanced-brace JSON
extraction
- lib/ai/apply.ts materializes parsed AIProgram into
Program tree (validates exerciseIds,
rejects unresolved nulls, atomic
transaction, sets aiGenerated=true)
Schema
- UserPreferences gets aiProvider/aiModel/aiBaseUrl/aiApiKey
(plaintext — same threat model as the rest of /data). Dead
enableClaudeAI/claudeApiKey columns from v1.0.0:1-7 stay as
no-op fields.
- AIPromptTemplate (userId nullable; userId=NULL = built-in)
- AIGeneration (raw response + parsed program + status +
appliedProgramId + token counts)
- All compat-ALTER'd in docker_entrypoint.sh on first boot.
API
- POST /api/ai/generate SSE streaming: emits
generation/text/usage/complete
events; persists AIGeneration
row up front so failures show
in history too
- POST /api/ai/apply takes user-edited AIProgram,
creates Program, marks
generation as applied
- GET /api/ai/templates built-ins + this user's own
- POST /api/ai/templates create user-owned template
- PATCH /api/ai/templates/[id] edit; built-ins admin-only
- DELETE /api/ai/templates/[id] delete; built-ins admin-only
- GET /api/ai/generations list (paginated)
- GET /api/ai/generations/[id] full row
- DELETE /api/ai/generations/[id] delete one (Program survives)
- GET /api/ai/config returns aiKeyConfigured flag,
never plaintext key
- POST /api/ai/config update provider config
- DELETE /api/admin/ai/generations admin-only "clear all" with
optional userId / olderThanDays
UI
- Settings → AI integration provider/model/URL/key form;
plaintext key warning visible
- /main/ai hub page with cards
- /main/ai/generate template picker + textarea +
live SSE stream + cancel +
ProgramPreview with inline
unknown-exercise resolver +
apply button + redirect to
the new Program
- /main/ai/templates list + create + edit + delete;
per-row "show prompt" expand;
built-in delete warns about
reconcile re-creation
- /main/ai/history list + delete; status badges;
link to applied Program
- Nav: "AI" entry between Programs and Exercises (Sparkles icon)
Built-in templates
- prisma/aiTemplates.seed.json: 5 starter templates (hypertrophy /
strength / endurance / recovery / custom)
- prisma/ensurePromptTemplates.cjs: per-boot reconcile,
INSERT-or-UPDATE keyed on (userId IS NULL AND name=...);
user-created templates never touched
Tests
- tests/ai-programSchema.test.ts: extractJson + parseAIProgram
edge cases (markdown fences, balanced braces, malformed JSON,
Zod shape rejection, unresolved-exerciseId tolerance)
- tests/ai-apply.test.ts: materializes valid AIProgram, rejects
cross-user exerciseIds, rejects unresolved exercises, honors
isActive flag
- tests/routes-ai-templates.test.ts: built-in vs user permissions,
cross-user template isolation, /api/ai/config plaintext-key safety,
provider enum validation
- 123 tests across 14 files, all passing.
No data migration. Existing /data is augmented with the new columns
+ tables only.
121 lines
3.7 KiB
TypeScript
121 lines
3.7 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
|
import { extractJson, parseAIProgram } from '@/lib/ai/programSchema';
|
|
|
|
describe('extractJson', () => {
|
|
it('extracts a bare JSON object', () => {
|
|
expect(extractJson('{"a":1}')).toBe('{"a":1}');
|
|
});
|
|
it('strips ```json fences', () => {
|
|
expect(extractJson('```json\n{"a":1}\n```')).toBe('{"a":1}');
|
|
});
|
|
it('strips bare ``` fences', () => {
|
|
expect(extractJson('```\n{"a":1}\n```')).toBe('{"a":1}');
|
|
});
|
|
it('finds first balanced object after preamble', () => {
|
|
const raw =
|
|
'Here is your program:\n\n{"name":"X","weeks":[{"weekNumber":1,"days":[]}]}\n\nHope that helps!';
|
|
expect(extractJson(raw)).toBe(
|
|
'{"name":"X","weeks":[{"weekNumber":1,"days":[]}]}',
|
|
);
|
|
});
|
|
it('handles braces inside strings', () => {
|
|
const raw = '{"notes":"use {brackets} sparingly","x":1}';
|
|
expect(extractJson(raw)).toBe(raw);
|
|
});
|
|
it('returns null when no object present', () => {
|
|
expect(extractJson('no json at all')).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe('parseAIProgram', () => {
|
|
const valid = {
|
|
name: 'Test',
|
|
type: 'hypertrophy',
|
|
durationWeeks: 4,
|
|
weeks: [
|
|
{
|
|
weekNumber: 1,
|
|
days: [
|
|
{
|
|
dayOfWeek: 1,
|
|
exercises: [
|
|
{
|
|
exerciseId: 'cabc',
|
|
exerciseName: 'Bench Press',
|
|
order: 0,
|
|
sets: 4,
|
|
repsMin: 6,
|
|
repsMax: 10,
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
],
|
|
};
|
|
|
|
it('accepts a valid program', () => {
|
|
const r = parseAIProgram(JSON.stringify(valid));
|
|
expect(r.ok).toBe(true);
|
|
if (r.ok) {
|
|
expect(r.program.name).toBe('Test');
|
|
expect(r.program.weeks[0].days[0].exercises[0].exerciseName).toBe(
|
|
'Bench Press',
|
|
);
|
|
}
|
|
});
|
|
|
|
it('accepts null exerciseId for unresolved exercises', () => {
|
|
const variant = structuredClone(valid);
|
|
variant.weeks[0].days[0].exercises[0] = {
|
|
...variant.weeks[0].days[0].exercises[0],
|
|
exerciseId: null as unknown as string,
|
|
};
|
|
const r = parseAIProgram(JSON.stringify(variant));
|
|
expect(r.ok).toBe(true);
|
|
});
|
|
|
|
it('rejects when no JSON found', () => {
|
|
const r = parseAIProgram('the model just said hello');
|
|
expect(r.ok).toBe(false);
|
|
if (!r.ok) expect(r.reason).toMatch(/Could not find/);
|
|
});
|
|
|
|
it('rejects malformed JSON', () => {
|
|
// Unbalanced braces: extractJson never finds a closing `}`, so
|
|
// the failure mode is "Could not find a JSON object" rather than
|
|
// a parse error per se. Either way, ok=false.
|
|
const r = parseAIProgram('{ "name": "x", "weeks": [');
|
|
expect(r.ok).toBe(false);
|
|
});
|
|
|
|
it('rejects JSON with a parse-level syntax error inside balanced braces', () => {
|
|
const r = parseAIProgram('{ "name": "x", }');
|
|
expect(r.ok).toBe(false);
|
|
if (!r.ok) expect(r.reason).toMatch(/parse error/i);
|
|
});
|
|
|
|
it('rejects when shape is wrong (missing weeks)', () => {
|
|
const bad = { name: 'X', type: 'hypertrophy', durationWeeks: 4 };
|
|
const r = parseAIProgram(JSON.stringify(bad));
|
|
expect(r.ok).toBe(false);
|
|
if (!r.ok) expect(r.reason).toMatch(/shape/);
|
|
});
|
|
|
|
it('rejects when dayOfWeek is out of range', () => {
|
|
const variant = structuredClone(valid);
|
|
variant.weeks[0].days[0].dayOfWeek = 7; // 0-6 only
|
|
const r = parseAIProgram(JSON.stringify(variant));
|
|
expect(r.ok).toBe(false);
|
|
});
|
|
|
|
it('handles a model response wrapped in markdown commentary', () => {
|
|
const wrapped =
|
|
"Sure! Here's your program:\n\n```json\n" +
|
|
JSON.stringify(valid) +
|
|
'\n```\n\nLet me know if you want changes.';
|
|
const r = parseAIProgram(wrapped);
|
|
expect(r.ok).toBe(true);
|
|
});
|
|
});
|