Compare commits

...

7 Commits

Author SHA1 Message Date
Keysat 0ed41765da Mine AGENTS.md brain onto disk: resolve TODOs, extract AI subsystem guide
CI / proof-of-work (Next.js app) (push) Has been cancelled
CI / start9/0.4 (StartOS package code) (push) Has been cancelled
Retrofit per the playbook. Resolve both AGENTS.md TODOs with verified
facts (make-target set; db:seed is live at image-build + local dev),
reconcile the AI-provider count (4 files -> 5 registered providers), and
extract the AI subsystem cheat-sheet into docs/guides/ai-subsystem.md,
lazy-loaded via a .claude/rules symlink with an index line in AGENTS.md.

All AGENTS.md commands verified green (tests 177/177, build, tsc, lint).
2026-06-12 20:26:14 -05:00
Keysat 29b9d2437c Add AGENTS.md, ROADMAP.md, and CLAUDE.md symlink
Onboarding doc for fresh agent sessions: stack, commands, layout,
conventions, and an Always/Never list of gotchas hit during the AI
overhaul. Current state section tracks the 1.1.0:7 checkpoint.
ROADMAP.md holds the longer-term backlog. CLAUDE.md symlinks AGENTS.md
so Claude Code loads it. Secrets kept out — private registry/file-host
URLs and creds referenced by file location, not value.
2026-06-12 20:02:27 -05:00
Keysat 1a77a0bfc2 v1.1.0:7 — exercise-history popup auto-loads on scroll
The popup HAD an IntersectionObserver-based infinite scroll (since
v1.0.0:6 alongside the main workout-history page), but the observer
was unreliable inside an `absolute`-positioned scroll container with
a small 60px rootMargin. It often didn't fire at all — leaving the
user with a popup that scrolled internally but never fetched more
data even when hundreds of history entries existed server-side.

Fix: replace IntersectionObserver with a plain `scroll` event
listener on the popup. Fires whenever the user scrolls within 300px
of the bottom (matching WorkoutsList's lookahead on the main page).
Also runs once on mount in case the first page doesn't fill the
popup.

Bottom status row now shows "Loading more..." / "Scroll to load
more" / "End of history" so the user has feedback on state.

No schema, no API, no data.
2026-05-13 09:35:53 -05:00
Keysat 01529204cb v1.1.0:6 — exercise history popup scrolls further
The clock-icon popup in the workout editor was capped at max-h-80
(~320px = ~5 history rows). Users with multi-year history saw older
sessions hidden behind a tiny inner scrollbar. Bumped to 70vh so it
scales with the viewport — ~15+ rows on a normal display, more on a
large monitor.

The IntersectionObserver pagination already loaded more rows on
demand; the old cap just kept them off-screen.

Pure CSS-class change. No schema, no API, no data.
2026-05-13 09:28:32 -05:00
Keysat 35539a9341 v1.1.0:5 — Gemini model menu correctness
User pointed out their Google AI Studio dropdown shows gemini-3-pro,
gemini-3.1-pro, gemini-3-flash and gemini-2.5-flash — not the longer
preview names I shipped in v1.1.0:4. The menu was missing all the
Flash variants entirely.

Fix:
  - Add gemini-3.1-pro (short form, what AI Studio shows)
  - Add gemini-3.1-flash + gemini-3.1-flash-lite (the cheapest 3.x)
  - Add gemini-3-pro + gemini-3-flash (older tier, still available)
  - Pricing entries for all of the above (~$0.50/$3 per M for Flash)

Pure data fix; no schema or behavior changes.
2026-05-11 12:51:17 -05:00
Keysat 7a62690a4a v1.1.0:4 — multi-config AI, background generation, ollama auto-detect, system prompt overhaul
User-feedback-driven release after testing v1.1.0:3. Nine themes:

1. Multi-config persistence
   - New AIConfigProfile table (per-user). Save N configs, toggle one
     active. Switching providers no longer wipes the previous setup.
   - UserPreferences gains activeAIConfigId; legacy single-config
     columns are mirrored from the active profile so existing reads
     keep working without conditional logic.
   - Idempotent boot migration lifts any existing single-config row
     into a default profile.

2. Ollama auto-detect
   - The "Add config" form probes /api/tags on the StartOS internal
     addresses (ollama.startos / ollama.embassy on :11434). If
     reachable: URL pre-fills, model field becomes a dropdown of
     installed models. Fixes the copy-paste UX.

3. Curated model dropdowns for major providers
   - Claude: Opus 4.7, Sonnet 4.6 (1M ctx), Haiku 4.5
   - OpenAI: GPT-5.5, 5.4, 5.4-mini, 5.4-nano
   - Gemini: 3.1-pro-preview, 2.5-pro, 2.5-flash, etc.
   - "Other (type your own)" stays for niche models.
   - Fixes "I tried gemini-3.0-pro and got 404."

4. Background generation
   - lib/ai/generationRunner.ts: detached runner with in-memory
     pub/sub bus. POST /api/ai/generate kicks it off and returns
     immediately. SSE stream attaches by id. The runner survives
     request cancellation; navigating away no longer kills it.
   - New AIGeneration columns: progressText (in-flight stream),
     durationMs (final wall-clock).
   - Generate UI shows a banner explaining background-safety.
   - History detail page polls progress + renders partial JSON
     live for cross-process resume (page refresh, new tab).

5. System prompt overhaul
   - lib/ai/systemPromptBase.ts: structural contract prepended to
     every template. Forces JSON-only output, library-exerciseId
     usage (kills "exerciseId doesn't belong to this user" errors),
     and per-resistance-exercise suggestedWeight (with-history vs
     without-history variants).
   - aiExerciseSchema + ProgramExercise gain suggestedWeight +
     suggestedWeightUnit. Starting a workout from a ProgramDay
     pre-populates SetLog.weight from the suggestion.

6. Test connection improvements
   - Latency in seconds (was ms — confusing for slow Ollama).
   - Stale "✓ Connected" clears on form change.
   - Per-config Test (no need to activate first).
   - Generous maxOutputTokens for thinking models.
   - Gemini surfaces finishReason on empty response (e.g. "blocked
     by safety filter") instead of generic "empty response."
   - Test endpoint accepts a draft body so you can verify before
     saving + before activating.

7. History detail view
   - Click row → full program tree + exact prompts sent. Apply from
     here without re-generating. Pending rows poll for progress.

8. Sidebar sub-navigation
   - AI: Generate / History / Templates
   - Settings: General / Password / Sessions / AI integration /
     Export / Instance (admin) / Danger zone, with anchor scroll.

9. API key UX
   - "Key saved" indicator on saved configs (was confusing to see
     an empty input after a successful save).

Schema migrations (additive, idempotent in entrypoint):
  - AIConfigProfile table created
  - UserPreferences.activeAIConfigId
  - AIGeneration.progressText + durationMs
  - ProgramExercise.suggestedWeight + suggestedWeightUnit

Tests: 16 new (systemPromptBase, modelMenu, generationRunner). 177
total pass.
2026-05-11 08:09:01 -05:00
Keysat dba478aa23 v1.1.0:3 — AI upgrades: history context, test connection, cost estimator, streaming preview
Four incremental upgrades to the AI program generator. No schema change, no /data migration.

1. History as context (the killer feature)
   - lib/ai/historyContext.ts builds a 90-day per-exercise rollup:
     frequency, recent weights, estimated 1RM (Epley), avg RPE,
     days-since-last, plus a STAGNANT flag when the heaviest weight in
     the new half doesn't beat the old half.
   - Generate page surfaces an "Include my workout history as context"
     checkbox (default on at >=10 logged workouts). When checked, the
     ~1-3 KB summary is appended to the system prompt so the model can
     recommend things like "you've stalled bench at 245 — try paused reps."
   - We deliberately don't ship raw set logs (privacy + token cost).

2. Test connection
   - POST /api/ai/test sends a tiny "say hi in 3 words" prompt and
     reports latency + first sample, or the error inline.
   - "Test connection" button next to "Save AI config" in
     Settings -> AI integration. Verifies provider/model/key/baseUrl
     without going through full program generation.

3. Cost estimator
   - lib/ai/pricing.ts ships a price table for major models
     (Claude 3.5/3.7/4/4.5, GPT-4o/5/o1/o3/o4-mini, Gemini 1.5/2.0/2.5).
     Ollama always returns 0; openai-compatible returns null.
   - Generation history shows per-row cost + a 30-day rolling total
     at the top of the page.

4. Streaming preview render
   - lib/ai/lenientJson.ts: stack-aware partial-JSON parser that
     auto-closes open strings/brackets/braces in reverse-of-opening
     order, drops dangling key:value pairs and partial keywords.
     Returns a best-effort snapshot of the program-so-far on each chunk.
   - Generate UI now renders a live "Building program..." panel that
     updates as weeks/days/exercises arrive instead of just showing
     raw text and waiting for stream end.

Tests: 26 new (ai-historyContext.test.ts, ai-lenientJson.test.ts,
ai-pricing.test.ts). 161 total pass.
2026-05-10 22:17:35 -05:00
52 changed files with 5045 additions and 566 deletions
+1
View File
@@ -0,0 +1 @@
../../docs/guides/ai-subsystem.md
+1
View File
@@ -25,6 +25,7 @@ logs/
.env.*.local .env.*.local
# Local DB snapshots that aren't part of the package # Local DB snapshots that aren't part of the package
app.db
proof-of-work-*.db proof-of-work-*.db
*.db-journal *.db-journal
*.db-wal *.db-wal
+109
View File
@@ -0,0 +1,109 @@
# AGENTS.md — Proof of Work
Self-hosted multi-user workout logger (Next.js app) packaged as a StartOS 0.4 `s9pk`, published to a private Start9 registry.
## Stack (versions that matter)
- **Next.js 14** (App Router, server components + server actions, SSE streaming)
- **React 18**, **TypeScript 5**, **TailwindCSS 3**
- **Prisma 5** ORM over **SQLite** (WAL mode; tuned at boot)
- **bcrypt** (native — NOT bcryptjs), **zod 3** for validation
- **Vitest 4** for tests
- **@start9labs/start-sdk** for the 0.4 packaging layer
- Node **>= 20** to build
## Layout (two projects in one repo)
```
proof-of-work/ ← the Next.js app (THIS is where you run npm)
app/ ← App Router routes; app/api/** = route handlers
app/main/ ← authed UI; navigation.tsx = sidebar
components/ ← React components (workouts/, ai/, settings/)
lib/ai/ ← AI subsystem (see below)
lib/ai/providers/ ← claude.ts openai.ts gemini.ts ollama.ts + index.ts (getProvider; openai.ts exports both openai + openai-compatible = 5 registered providers)
prisma/schema.prisma ← schema (mirror; real DB migrates via entrypoint ALTERs)
prisma/*.seed.json ← curated exercise library + AI templates (reconciled each boot)
tests/ ← Vitest specs (ai-*.test.ts, routes-*.test.ts, ...)
start9/0.4/ ← StartOS packaging wrapper
docker_entrypoint.sh ← boot: first-boot seed, additive ALTERs, library reconcile
Makefile / s9pk.mk ← s9pk build (ARCHES := x86)
startos/versions/ ← one file per ExVer version + index.ts (the version graph)
~/.proof-of-work/ ← publish.sh + unpublish.sh (NOT in repo; self-hosted registry)
```
`workout-planner/` is scratch (only `logs/`) — ignore. `start9/0.4/*.s9pk` are build artifacts.
## Commands
Run app commands **from `proof-of-work/`** (running tsc/vitest/next from repo root fails — wrong cwd):
```bash
cd proof-of-work
npm run dev # local dev server
npm run build # next build (run this to catch route/type errors before shipping)
npm run lint # next lint
npm test # vitest run (full suite)
npx vitest run tests/ai-pricing.test.ts # single file
npx vitest run -t "findPrice" # single test by name
npx tsc --noEmit # typecheck only
npx prisma generate # REQUIRED after editing schema.prisma (else TS can't see new fields)
```
Build/sideload the s9pk (from `start9/0.4/`): `make x86` then `make install`. Targets come from `s9pk.mk` (the wrapper `Makefile` just sets `ARCHES := x86`):
- `make x86` — build the x86 s9pk.
- `make install` — sideload the newest local `.s9pk` to the StartOS box at `host:` in `~/.startos/config.yaml` (via `start-cli package install`).
- `make publish` — upload every `.s9pk` to the S3 bucket (`s9pk-s3base:`) and index it on `registry:` from `~/.startos/config.yaml` (via `s3cmd` + `start-cli s9pk publish`). **Distinct from `~/.proof-of-work/publish.sh`** below.
- `make clean` — remove build artifacts.
Both `install` and `publish` read host/registry config from `~/.startos/config.yaml`, which is **not in the repo** — verify against the live setup, not from a checkout.
Canonical publish path for this project: `~/.proof-of-work/publish.sh` (builds, uploads to FileBrowser, registers) — separate from the generic `make publish`. Unpublish: `~/.proof-of-work/unpublish.sh`.
`npm run db:seed` (= `tsx prisma/seed.ts`) seeds the `InstanceSettings` singleton + curated library; it is **live, not dead** — invoked at Docker image-build time (`start9/0.4/Dockerfile`) to bake the library into the image, and also the local-dev first-run path (`proof-of-work/README.md`). Runtime first-boot/upgrade seeding is handled separately by `docker_entrypoint.sh`.
## Conventions
- **Versioning is ExVer**: `1.1.0:4` (note the colon). Every release = a new `start9/0.4/startos/versions/vMAJOR.MINOR.PATCH.N.ts` file, imported into `versions/index.ts` and promoted to `current` (previous `current` moves into `other[]`).
- **Bump the version BEFORE building the s9pk** — Start9 0.4 won't recognize a rebuild as an update otherwise.
- **Schema changes are additive ALTERs in `docker_entrypoint.sh`**, guarded by `PRAGMA table_info` checks. Keep `schema.prisma` in sync as the mirror, but the entrypoint is what migrates live `/data`. Never write a destructive migration.
- **Commit subject** = `vX.Y.Z:N — short summary`, imperative, body explains the *why*.
- **Git remote is self-hosted** (a private Start9 registry + a FileBrowser artifact host), NOT GitHub. The actual registry/file-host URLs are constants in `~/.proof-of-work/{publish,unpublish}.sh`; FileBrowser creds live in `~/.keysat/filebrowser.env` (outside the repo, gitignored). Default branch is `master`.
- Tests live in `proof-of-work/tests/`; mock server-action deps with `vi.hoisted()` + `vi.mock`.
- **Before editing the AI subsystem (`proof-of-work/lib/ai/**` or the generate/generations routes), read `docs/guides/ai-subsystem.md`** — provider abstraction, SSE/lenient-JSON, pricing/model menus, and the background-runner architecture live there.
## Always
- Run `npx prisma generate` after any `schema.prisma` edit, then `npx tsc --noEmit`.
- Run `npm test` AND `npm run build` before shipping a version.
- Add the boot-time `ALTER TABLE` (with an existence guard) for any new column, in `docker_entrypoint.sh`.
- Treat API keys / secrets as plaintext in `/data` BY DESIGN (threat model: the operator owns `/data`). Reference env-var names (`DATABASE_URL`, etc.); never hardcode values.
- Keep migrations idempotent and additive; data already on a user's server must survive upgrades.
- Verify the published file actually changed (size / 404 / Last-Modified) after publish.sh.
## Never
- **Never add `Co-Authored-By` / "Generated with" trailers** to commits — the user authors commits solo. (This was done wrong in earlier commits; do not repeat.)
- **Never reintroduce nonce-based CSP** — it broke first paint. Use the static `'unsafe-inline'` CSP in `next.config.js`.
- **Never run app commands from the repo root** — always `cd proof-of-work` first.
- **Never export non-HTTP-method symbols from a `route.ts`** — Next.js rejects the build (helpers go in `lib/`, e.g. `lib/ai/activateConfig.ts`).
- **Never commit `app.db`, `*.bak`, or any user data** — they're gitignored; double-check `git status` before `git add`.
- **Never click Uninstall on a StartOS package during a data cutover** — it destroys the volume; use Stop.
- **Never assume GitHub** — don't add a GitHub remote or push there.
## Current state
Latest version is **1.1.0:7** (built locally, installed on the StartOS server). The registry is currently **empty** — all versions were unpublished; nothing is downloadable until `publish.sh` runs again.
Working: workout logging, programs (manual + AI), multi-user, curated library, full AI subsystem (5 providers, multi-config, background generation, history detail, cost/duration, Ollama auto-detect, infinite-scroll exercise history).
In progress: none — repo is at a clean checkpoint.
Decided but not implemented: tiered AI prompt formatting — JSON-Schema enforcement (Ollama `format` / OpenAI `response_format`), pipe-separated library, XML-tagged sections, Ollama-only few-shot. Targets local-model output quality; would ship as 1.1.0:8.
Known issues: earlier commits (`8f149d3``5b0535f`) carry `Co-Authored-By` trailers to scrub from history. publish.sh Step 3 (registry register) silently no-op'd on 1.1.0:6 and :7 — uploaded the file but didn't register; investigate before relying on those versions appearing in the registry.
Next steps:
1. Scrub `Co-Authored-By` trailers from history (filter-branch or rebase).
2. Re-publish current version once the Step-3 registry-register failure is diagnosed.
3. Implement the tiered AI prompt formatting (1.1.0:8).
Symlink
+1
View File
@@ -0,0 +1 @@
AGENTS.md
+25
View File
@@ -0,0 +1,25 @@
# ROADMAP — Proof of Work
Longer-term backlog. Near-term state + next steps live in `AGENTS.md` → Current state.
## AI quality
- Tiered prompt formatting (also the immediate next step): JSON-Schema output enforcement via Ollama `format` and OpenAI `response_format`; pipe-separated library rows; XML-tagged prompt sections; Ollama-only few-shot example; stable prefix first for prompt-cache hits.
- Keep `MODEL_MENU` / `PRICES` current as providers ship new models.
## Packaging / distribution
- Diagnose and fix the `publish.sh` Step-3 registry-register silent no-op.
- Build for `arm` / additional arches once StartOS 0.4 supports them on the host.
- Consider submission to the Start9 community registry (use the start9-spec-checker agent first).
## Product
- Adherence tracking: compare logged workouts against the planned `ProgramDay` (the `programDayId` link already exists).
- Per-user export/import polish and scheduled backups.
- Charts/progress views over history (the data and 1RM estimates already exist).
## Hygiene
- Scrub `Co-Authored-By` trailers from git history.
- Revisit `workout-planner/` scratch dir — remove if truly unused.
+31
View File
@@ -0,0 +1,31 @@
---
paths:
- proof-of-work/lib/ai/**
- proof-of-work/app/api/ai/**
---
# AI subsystem
Scoped guidance for the AI generation subsystem (`proof-of-work/lib/ai/**` and the
generate/generations route handlers). Whole-repo rules live in `AGENTS.md`.
## Architecture
- `generate/route.ts` kicks off a **detached background runner** (`generationRunner.ts`)
and returns an id; the client attaches via SSE (`generations/[id]/stream`) and can also
poll the row. Navigating away does NOT cancel generation.
- System prompt = `systemPromptBase.ts` (output contract: JSON-only, library
`exerciseId`s only, suggested weights) + the template's coaching prompt +
`PROGRAM_OUTPUT_SHAPE` + library + optional history block (`historyContext.ts`).
- Multi-config: `AIConfigProfile` rows per user; `UserPreferences.activeAIConfigId`
points at the active one and is mirrored into the legacy `ai*` columns for back-compat.
## Provider abstraction
- Each provider yields an async iterable of `GenerateChunk` (`text` / `usage` / `done` /
`error`); add new ones under `lib/ai/providers/` and register in `index.ts`.
`openai.ts` exports both `openai` and `openai-compatible`, so the four provider files
register **5** providers (`claude`, `openai`, `openai-compatible`, `gemini`, `ollama`).
- Streaming AI uses SSE; partial JSON is recovered with `lib/ai/lenientJson.ts`.
- Pricing/model menus live in `lib/ai/pricing.ts` (`PRICES`, `MODEL_MENU`) — keep them
paired so every menu model has a price entry (there's a test enforcing this).
@@ -0,0 +1,33 @@
import { NextRequest, NextResponse } from 'next/server';
import { getCurrentUser } from '@/lib/auth';
import { prisma } from '@/lib/prisma';
import { activate } from '@/lib/ai/activateConfig';
/**
* POST /api/ai/configs/[id]/activate
*
* Set the named profile as the actor's active AI config. Mirrors the
* profile's fields into UserPreferences (legacy single-config columns)
* so api/ai/generate + api/ai/test continue to work as-is.
*/
export async function POST(
_req: NextRequest,
{ params }: { params: { id: string } },
) {
const user = await getCurrentUser();
if (!user) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
const profile = await prisma.aIConfigProfile.findFirst({
where: { id: params.id, userId: user.id },
});
if (!profile) return NextResponse.json({ error: 'Not found' }, { status: 404 });
await activate(user.id, profile.id, {
provider: profile.provider,
model: profile.model,
baseUrl: profile.baseUrl,
apiKey: profile.apiKey,
});
return NextResponse.json({ success: true, activeId: profile.id });
}
@@ -0,0 +1,153 @@
import { NextRequest, NextResponse } from 'next/server';
import { z } from 'zod';
import { getCurrentUser } from '@/lib/auth';
import { prisma } from '@/lib/prisma';
import { activate } from '@/lib/ai/activateConfig';
/**
* GET /api/ai/configs/[id] Single config (apiKey redacted).
* PATCH /api/ai/configs/[id] Update fields. Empty/null clears.
* Re-mirrors to UserPreferences if active.
* DELETE /api/ai/configs/[id] Remove. If it was active, falls back to
* the most-recently-created remaining
* profile (or clears if none left).
*/
export async function GET(
_req: NextRequest,
{ params }: { params: { id: string } },
) {
const user = await getCurrentUser();
if (!user) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
const p = await prisma.aIConfigProfile.findFirst({
where: { id: params.id, userId: user.id },
select: {
id: true,
name: true,
provider: true,
model: true,
baseUrl: true,
apiKey: true,
createdAt: true,
},
});
if (!p) return NextResponse.json({ error: 'Not found' }, { status: 404 });
return NextResponse.json({
id: p.id,
name: p.name,
provider: p.provider,
model: p.model,
baseUrl: p.baseUrl,
keyConfigured: !!p.apiKey,
createdAt: p.createdAt.toISOString(),
});
}
const patchSchema = z.object({
name: z.string().min(1).max(80).optional(),
model: z.string().min(1).max(200).optional(),
baseUrl: z.string().url().nullable().optional().or(z.literal('')),
apiKey: z.string().nullable().optional(),
});
export async function PATCH(
request: NextRequest,
{ params }: { params: { id: string } },
) {
const user = await getCurrentUser();
if (!user) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
const body = await request.json().catch(() => ({}));
const parsed = patchSchema.safeParse(body);
if (!parsed.success) {
return NextResponse.json(
{ error: 'Invalid body', details: parsed.error.errors },
{ status: 400 },
);
}
const existing = await prisma.aIConfigProfile.findFirst({
where: { id: params.id, userId: user.id },
});
if (!existing) return NextResponse.json({ error: 'Not found' }, { status: 404 });
const data: Record<string, string | null> = {};
if (parsed.data.name !== undefined) data.name = parsed.data.name;
if (parsed.data.model !== undefined) data.model = parsed.data.model;
if (parsed.data.baseUrl !== undefined)
data.baseUrl = parsed.data.baseUrl || null;
if (parsed.data.apiKey !== undefined)
data.apiKey = parsed.data.apiKey || null;
const updated = await prisma.aIConfigProfile.update({
where: { id: params.id },
data,
});
// If this was the active config, mirror the new fields back into
// UserPreferences so existing read paths (api/ai/test, api/ai/generate
// current implementation) see the latest values.
const prefs = await prisma.userPreferences.findUnique({
where: { userId: user.id },
select: { activeAIConfigId: true },
});
if (prefs?.activeAIConfigId === params.id) {
await activate(user.id, params.id, {
provider: updated.provider,
model: updated.model,
baseUrl: updated.baseUrl,
apiKey: updated.apiKey,
});
}
return NextResponse.json({ success: true });
}
export async function DELETE(
_req: NextRequest,
{ params }: { params: { id: string } },
) {
const user = await getCurrentUser();
if (!user) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
const existing = await prisma.aIConfigProfile.findFirst({
where: { id: params.id, userId: user.id },
});
if (!existing) return NextResponse.json({ error: 'Not found' }, { status: 404 });
await prisma.aIConfigProfile.delete({ where: { id: params.id } });
// If we just deleted the active config, demote-or-remove gracefully.
const prefs = await prisma.userPreferences.findUnique({
where: { userId: user.id },
select: { activeAIConfigId: true },
});
if (prefs?.activeAIConfigId === params.id) {
const fallback = await prisma.aIConfigProfile.findFirst({
where: { userId: user.id },
orderBy: { createdAt: 'desc' },
});
if (fallback) {
await activate(user.id, fallback.id, {
provider: fallback.provider,
model: fallback.model,
baseUrl: fallback.baseUrl,
apiKey: fallback.apiKey,
});
} else {
await prisma.userPreferences.update({
where: { userId: user.id },
data: {
activeAIConfigId: null,
aiProvider: null,
aiModel: null,
aiBaseUrl: null,
aiApiKey: null,
},
});
}
}
return NextResponse.json({ success: true });
}
+119
View File
@@ -0,0 +1,119 @@
import { NextRequest, NextResponse } from 'next/server';
import { z } from 'zod';
import { getCurrentUser } from '@/lib/auth';
import { prisma } from '@/lib/prisma';
import { activate } from '@/lib/ai/activateConfig';
/**
* v1.1.0:4 — Multi-config CRUD.
*
* GET /api/ai/configs List the actor's saved AI configs +
* their active id. apiKey is REDACTED in
* list output (only `keyConfigured: bool`).
* POST /api/ai/configs Create a new config. Pass `setActive: true`
* to also activate it.
*
* Per-row endpoints in [id]/route.ts. "Activate" is its own POST in
* [id]/activate/route.ts so the action is explicit + auditable.
*/
const PROVIDERS = ['claude', 'openai', 'openai-compatible', 'gemini', 'ollama'] as const;
export async function GET() {
const user = await getCurrentUser();
if (!user) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
const [profiles, prefs] = await Promise.all([
prisma.aIConfigProfile.findMany({
where: { userId: user.id },
orderBy: { createdAt: 'asc' },
select: {
id: true,
name: true,
provider: true,
model: true,
baseUrl: true,
apiKey: true, // pulled only to compute keyConfigured; never returned
createdAt: true,
},
}),
prisma.userPreferences.findUnique({
where: { userId: user.id },
select: { activeAIConfigId: true },
}),
]);
return NextResponse.json({
activeId: prefs?.activeAIConfigId ?? null,
configs: profiles.map((p) => ({
id: p.id,
name: p.name,
provider: p.provider,
model: p.model,
baseUrl: p.baseUrl,
keyConfigured: !!p.apiKey,
createdAt: p.createdAt.toISOString(),
})),
});
}
const createSchema = z.object({
name: z.string().min(1).max(80).optional(),
provider: z.enum(PROVIDERS),
model: z.string().min(1).max(200),
baseUrl: z.string().url().nullable().optional().or(z.literal('')),
apiKey: z.string().nullable().optional(),
setActive: z.boolean().optional(),
});
export async function POST(request: NextRequest) {
const user = await getCurrentUser();
if (!user) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
const body = await request.json().catch(() => ({}));
const parsed = createSchema.safeParse(body);
if (!parsed.success) {
return NextResponse.json(
{ error: 'Invalid body', details: parsed.error.errors },
{ status: 400 },
);
}
const { name, provider, model, baseUrl, apiKey, setActive } = parsed.data;
const profile = await prisma.aIConfigProfile.create({
data: {
userId: user.id,
name: name ?? defaultName(provider, model),
provider,
model,
baseUrl: baseUrl || null,
apiKey: apiKey || null,
},
});
if (setActive) {
await activate(user.id, profile.id, { provider, model, baseUrl, apiKey });
}
return NextResponse.json({
id: profile.id,
name: profile.name,
provider: profile.provider,
model: profile.model,
baseUrl: profile.baseUrl,
keyConfigured: !!profile.apiKey,
activated: !!setActive,
});
}
function defaultName(provider: string, model: string): string {
const PRETTY: Record<string, string> = {
claude: 'Claude',
openai: 'OpenAI',
'openai-compatible': 'Custom',
gemini: 'Gemini',
ollama: 'Ollama',
};
const label = PRETTY[provider] ?? provider;
return `${label} · ${model}`;
}
+73 -174
View File
@@ -1,38 +1,37 @@
import { NextRequest } from 'next/server'; import { NextRequest, NextResponse } from 'next/server';
import { z } from 'zod'; import { z } from 'zod';
import { getCurrentUser } from '@/lib/auth'; import { getCurrentUser } from '@/lib/auth';
import { prisma } from '@/lib/prisma'; import { prisma } from '@/lib/prisma';
import { getProvider } from '@/lib/ai/providers';
import { import {
PROGRAM_OUTPUT_SHAPE, PROGRAM_OUTPUT_SHAPE,
parseAIProgram,
} from '@/lib/ai/programSchema'; } from '@/lib/ai/programSchema';
import {
buildHistorySummary,
formatHistoryContext,
} from '@/lib/ai/historyContext';
import { buildBaseSystemPrompt } from '@/lib/ai/systemPromptBase';
import { kickoffGeneration } from '@/lib/ai/generationRunner';
/** /**
* POST /api/ai/generate * POST /api/ai/generate
* *
* Body: { templateId?: string, userInput: string } * Body: { templateId?: string, userInput: string, includeHistory?: boolean }
* *
* Streams the model response as Server-Sent Events: * v1.1.0:4: this endpoint now KICKS OFF a background runner and returns
* event: generation data: {"id":"...generationId..."} * the new generation id immediately. The caller subscribes to live
* event: text data: {"delta":"..."} * deltas via GET /api/ai/generations/[id]/stream (SSE) or polls via
* event: usage data: {"tokensIn":N,"tokensOut":M} * GET /api/ai/generations/[id]. Navigating away no longer cancels the
* event: complete data: {"parsedOk":true|false,"errorMessage":"..."} * generation — the runner keeps writing to the row in the background.
* *
* Reads the user's AI provider config from UserPreferences. The full * Response:
* library of exercises is appended to the system prompt so the model * 201 { id: "...generationId..." }
* picks real exercise IDs. * 400 { error: "..." }
*
* On error (no provider configured, model error, etc.) emits a single
* `event: error` and closes.
*
* Always writes one AIGeneration row, regardless of success — so the
* History page can show failed attempts too.
*/ */
const bodySchema = z.object({ const bodySchema = z.object({
templateId: z.string().optional().nullable(), templateId: z.string().optional().nullable(),
userInput: z.string().min(1), userInput: z.string().min(1),
includeHistory: z.boolean().optional().default(false),
}); });
export const dynamic = 'force-dynamic'; export const dynamic = 'force-dynamic';
@@ -40,53 +39,34 @@ export const dynamic = 'force-dynamic';
export async function POST(request: NextRequest) { export async function POST(request: NextRequest) {
const user = await getCurrentUser(); const user = await getCurrentUser();
if (!user) { if (!user) {
return new Response(JSON.stringify({ error: 'Unauthorized' }), { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
status: 401,
headers: { 'content-type': 'application/json' },
});
} }
const body = await request.json().catch(() => ({})); const body = await request.json().catch(() => ({}));
const parsed = bodySchema.safeParse(body); const parsed = bodySchema.safeParse(body);
if (!parsed.success) { if (!parsed.success) {
return new Response( return NextResponse.json(
JSON.stringify({ { error: 'Invalid body', details: parsed.error.errors },
error: 'Invalid body', { status: 400 },
details: parsed.error.errors,
}),
{ status: 400, headers: { 'content-type': 'application/json' } },
); );
} }
// Load the user's AI provider config.
const prefs = await prisma.userPreferences.findUnique({ const prefs = await prisma.userPreferences.findUnique({
where: { userId: user.id }, where: { userId: user.id },
}); });
if (!prefs?.aiProvider || !prefs?.aiModel) { if (!prefs?.aiProvider || !prefs?.aiModel) {
return new Response( return NextResponse.json(
JSON.stringify({ {
error: error:
'AI is not configured. Open Settings → AI integration and pick a provider + model.', 'AI is not configured. Open Settings → AI integration and pick a provider + model.',
}), },
{ status: 400, headers: { 'content-type': 'application/json' } }, { status: 400 },
);
}
const provider = getProvider(prefs.aiProvider);
if (!provider) {
return new Response(
JSON.stringify({ error: `Unknown provider: ${prefs.aiProvider}` }),
{ status: 400, headers: { 'content-type': 'application/json' } },
); );
} }
// Load the template if provided, else use a no-op default. // Load the template if provided.
let template: let template:
| { | { id: string; name: string; systemPrompt: string; userPromptTemplate: string }
id: string;
name: string;
systemPrompt: string;
userPromptTemplate: string;
}
| null = null; | null = null;
if (parsed.data.templateId) { if (parsed.data.templateId) {
const t = await prisma.aIPromptTemplate.findFirst({ const t = await prisma.aIPromptTemplate.findFirst({
@@ -102,23 +82,15 @@ export async function POST(request: NextRequest) {
}, },
}); });
if (!t) { if (!t) {
return new Response( return NextResponse.json({ error: 'Template not found.' }, { status: 404 });
JSON.stringify({ error: 'Template not found.' }),
{ status: 404, headers: { 'content-type': 'application/json' } },
);
} }
template = t; template = t;
} }
// Load the user's exercise library to embed in the system prompt. // Library for the prompt.
const exercises = await prisma.exercise.findMany({ const exercises = await prisma.exercise.findMany({
where: { userId: user.id }, where: { userId: user.id },
select: { select: { id: true, name: true, type: true, muscleGroups: true },
id: true,
name: true,
type: true,
muscleGroups: true,
},
}); });
const libraryJson = JSON.stringify( const libraryJson = JSON.stringify(
exercises.map((e) => ({ exercises.map((e) => ({
@@ -135,131 +107,58 @@ export async function POST(request: NextRequest) {
})), })),
); );
// Stitch the final system + user prompts. // History context if requested.
const baseSystem = template?.systemPrompt ?? DEFAULT_SYSTEM_PROMPT; let historyBlock = '';
const systemPrompt = `${baseSystem} if (parsed.data.includeHistory) {
const summary = await buildHistorySummary(prisma, user.id);
historyBlock = formatHistoryContext(summary);
}
// v1.1.0:4 base prompt with output contract + weight rules. Stitched
// BEFORE the template's coaching philosophy so output rules win when
// they conflict.
const weightUnit = (prefs.defaultWeightUnit as 'lbs' | 'kg') || 'lbs';
const isLocalModel = prefs.aiProvider === 'ollama';
const basePrompt = buildBaseSystemPrompt({
weightUnit,
hasHistoryContext: parsed.data.includeHistory,
isLocalModel,
});
const templatePrompt = template?.systemPrompt ?? DEFAULT_TEMPLATE_PROMPT;
const systemPrompt = `${basePrompt}
# COACHING PHILOSOPHY (template-specific)
${templatePrompt}
# OUTPUT SHAPE
OUTPUT SHAPE — emit ONLY a JSON object matching this shape (no commentary, no markdown fences):
${PROGRAM_OUTPUT_SHAPE} ${PROGRAM_OUTPUT_SHAPE}
LIBRARY — pick exerciseId values from this list when possible. If you need an exercise the user doesn't have, set exerciseId to null and put the proposed name in exerciseName; the user will resolve it during preview. # LIBRARY (use these exerciseIds; do not invent ids)
${libraryJson}`;
${libraryJson}${historyBlock}`;
const userPromptBody = const userPromptBody =
template?.userPromptTemplate.replace(/{{userInput}}/g, parsed.data.userInput) ?? template?.userPromptTemplate.replace(/{{userInput}}/g, parsed.data.userInput) ??
parsed.data.userInput; parsed.data.userInput;
// Persist the pending row up front so the user can see it in const id = await kickoffGeneration({
// history even if the stream dies mid-flight. prisma,
const generation = await prisma.aIGeneration.create({ userId: user.id,
data: { templateId: template?.id ?? null,
userId: user.id, templateName: template?.name ?? null,
templateId: template?.id ?? null, userInput: parsed.data.userInput,
templateName: template?.name ?? null, systemPrompt,
userInput: parsed.data.userInput, userPrompt: userPromptBody,
systemPrompt, provider: prefs.aiProvider,
userPrompt: userPromptBody, model: prefs.aiModel,
provider: provider.id, apiKey: prefs.aiApiKey,
model: prefs.aiModel, baseUrl: prefs.aiBaseUrl,
status: 'pending',
},
}); });
// Stream the model output as SSE. return NextResponse.json({ id }, { status: 201 });
const encoder = new TextEncoder();
const stream = new ReadableStream<Uint8Array>({
async start(controller) {
const send = (event: string, data: unknown) =>
controller.enqueue(
encoder.encode(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`),
);
send('generation', { id: generation.id });
let raw = '';
let tokensIn: number | undefined;
let tokensOut: number | undefined;
let providerError: string | null = null;
try {
for await (const chunk of provider.generate({
apiKey: prefs.aiApiKey,
baseUrl: prefs.aiBaseUrl,
model: prefs.aiModel!, // validated non-null at top of POST
systemPrompt,
userPrompt: userPromptBody,
signal: request.signal,
})) {
if (chunk.type === 'text') {
raw += chunk.delta;
send('text', { delta: chunk.delta });
} else if (chunk.type === 'usage') {
tokensIn = chunk.tokensIn;
tokensOut = chunk.tokensOut;
} else if (chunk.type === 'error') {
providerError = chunk.message;
}
}
} catch (e) {
providerError = (e as Error).message;
}
// Parse + validate the assembled response.
let parsedOk = false;
let parseErr: string | null = null;
let parsedJson: string | null = null;
if (!providerError && raw) {
const r = parseAIProgram(raw);
if (r.ok) {
parsedOk = true;
parsedJson = JSON.stringify(r.program);
} else {
parseErr = r.reason;
}
}
// Persist the final state.
const status = providerError
? 'failed'
: parsedOk
? 'completed'
: 'failed';
const errorMessage =
providerError ?? (parsedOk ? null : parseErr ?? 'Empty response');
await prisma.aIGeneration.update({
where: { id: generation.id },
data: {
rawResponse: raw || null,
parsedProgram: parsedJson,
tokensIn: tokensIn ?? null,
tokensOut: tokensOut ?? null,
status,
errorMessage,
},
});
send('usage', { tokensIn, tokensOut });
send('complete', { parsedOk, errorMessage });
controller.close();
},
});
return new Response(stream, {
status: 200,
headers: {
'content-type': 'text/event-stream',
'cache-control': 'no-store',
'x-accel-buffering': 'no', // disable nginx buffering if proxied
},
});
} }
const DEFAULT_SYSTEM_PROMPT = `You are a strength and conditioning coach. The user will describe what they want; you produce a complete training program as JSON. const DEFAULT_TEMPLATE_PROMPT = `You are a strength and conditioning coach. The user will describe what they want; design a program that matches their goal, experience, equipment, and time budget. Pick exercises from the LIBRARY and stay close to evidence-based programming for the requested goal (hypertrophy / strength / power / conditioning / general fitness).`;
Constraints:
- Pick exercises from the LIBRARY below by their id. Prefer compound lifts for primary slots and accessories for the back half of each session.
- Keep volume reasonable: 4-7 exercises per session, 60-75 minutes total.
- Use rep ranges that match the goal: hypertrophy 6-12, strength 3-6, power 1-5.
- For each exercise specify sets + reps (range or single) + rest in seconds. RPE is optional but useful for intensity-based programs.
- If the user asks for something a single library exercise can't satisfy, pick the closest fit and add a coaching note explaining the variation.
If you cannot produce a complete program for any reason, emit a JSON object with the durationWeeks and weeks arrays best-effort and add a top-level "description" explaining the gap.`;
@@ -0,0 +1,127 @@
import { NextRequest, NextResponse } from 'next/server';
import { getCurrentUser } from '@/lib/auth';
import { prisma } from '@/lib/prisma';
import { subscribe } from '@/lib/ai/generationRunner';
/**
* GET /api/ai/generations/[id]/stream
*
* SSE attach to an in-flight generation. The runner that POST
* /api/ai/generate kicked off lives in this Node process; this
* endpoint subscribes to its in-memory bus and forwards each delta
* as an SSE event.
*
* Late-joining (after some text has streamed): the runner buffers
* everything emitted so far, and the subscription replays the buffer
* on attach, so refresh / new tab catches up cleanly.
*
* Already-finished: subscribe() replays the buffer and returns a
* no-op unsubscribe. We close the connection right after the buffer
* drains.
*
* Cross-process resume (pod restart, separate process): the in-memory
* bus is empty, so the SSE will be silent. The client should fall
* back to polling /api/ai/generations/[id] for `progressText` until
* the row hits a terminal status. The Generate UI does this.
*/
export const dynamic = 'force-dynamic';
export async function GET(
request: NextRequest,
{ params }: { params: { id: string } },
) {
const user = await getCurrentUser();
if (!user) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
// Authorize.
const row = await prisma.aIGeneration.findFirst({
where: { id: params.id, userId: user.id },
select: { id: true, status: true, progressText: true, errorMessage: true, parsedProgram: true, tokensIn: true, tokensOut: true, durationMs: true },
});
if (!row) return NextResponse.json({ error: 'Not found' }, { status: 404 });
const encoder = new TextEncoder();
const send = (controller: ReadableStreamDefaultController, event: string, data: unknown) =>
controller.enqueue(
encoder.encode(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`),
);
const stream = new ReadableStream<Uint8Array>({
start(controller) {
let closed = false;
const safeClose = () => {
if (closed) return;
closed = true;
try {
controller.close();
} catch {
/* already closed */
}
};
// First: send a `generation` event with the id so clients can
// confirm what they attached to (and consume the same protocol
// their old code expected).
send(controller, 'generation', { id: params.id });
// If the row already finished while we weren't looking, send
// its known progress + complete + close. (Cross-process resume
// OR fast finish before subscribe attached.)
if (row.status !== 'pending') {
if (row.progressText) {
send(controller, 'text', { delta: row.progressText });
}
send(controller, 'complete', {
parsedOk: row.status === 'completed' || row.status === 'applied',
errorMessage: row.errorMessage ?? undefined,
tokensIn: row.tokensIn ?? undefined,
tokensOut: row.tokensOut ?? undefined,
durationMs: row.durationMs ?? undefined,
});
safeClose();
return;
}
const unsub = subscribe(params.id, (d) => {
if (closed) return;
if (d.type === 'text') send(controller, 'text', { delta: d.delta });
else if (d.type === 'usage')
send(controller, 'usage', {
tokensIn: d.tokensIn,
tokensOut: d.tokensOut,
});
else if (d.type === 'complete') {
send(controller, 'complete', {
parsedOk: d.parsedOk,
errorMessage: d.errorMessage,
tokensIn: d.tokensIn,
tokensOut: d.tokensOut,
durationMs: d.durationMs,
});
safeClose();
} else if (d.type === 'error') {
send(controller, 'complete', {
parsedOk: false,
errorMessage: d.errorMessage,
});
safeClose();
}
});
request.signal.addEventListener('abort', () => {
unsub();
safeClose();
});
},
});
return new Response(stream, {
status: 200,
headers: {
'content-type': 'text/event-stream',
'cache-control': 'no-store',
'x-accel-buffering': 'no',
},
});
}
@@ -28,6 +28,7 @@ export async function GET(request: NextRequest) {
model: true, model: true,
tokensIn: true, tokensIn: true,
tokensOut: true, tokensOut: true,
durationMs: true,
status: true, status: true,
errorMessage: true, errorMessage: true,
appliedProgramId: true, appliedProgramId: true,
@@ -0,0 +1,101 @@
import { NextRequest, NextResponse } from 'next/server';
import { getCurrentUser } from '@/lib/auth';
/**
* GET /api/ai/ollama/models?baseUrl=...
*
* Probes Ollama at the supplied baseUrl (or http://ollama.startos:11434
* by default) and returns the list of installed models, plus a status
* flag the UI uses to decide whether to:
* - pre-fill the URL field
* - render a model dropdown vs a free-text input
* - show a "no models installed yet" hint
*
* Authenticated route — we don't want unauthenticated visitors fingerprinting
* the local network.
*
* Response:
* { ok: true, baseUrl, models: [{ name, sizeBytes, modifiedAt }], ms }
* { ok: false, baseUrl, error, ms }
*/
const PROBE_TIMEOUT_MS = 5_000;
const DEFAULT_CANDIDATES = [
'http://ollama.startos:11434',
'http://ollama.embassy:11434',
];
export async function GET(request: NextRequest) {
const user = await getCurrentUser();
if (!user) return NextResponse.json({ ok: false, error: 'Unauthorized' }, { status: 401 });
const url = new URL(request.url);
const explicit = url.searchParams.get('baseUrl');
// If the caller specified a URL, probe just that. Otherwise walk the
// candidate list and return the first that responds (so the UI can
// auto-discover whether the user runs ollama.startos OR ollama.embassy).
const candidates = explicit ? [explicit] : DEFAULT_CANDIDATES;
for (const candidate of candidates) {
const result = await probe(candidate);
if (result.ok) return NextResponse.json(result);
// For an explicit URL, return the failure right away.
if (explicit) return NextResponse.json(result);
}
return NextResponse.json({
ok: false,
baseUrl: candidates[0],
error: 'No Ollama instance responded at the default StartOS addresses.',
ms: 0,
});
}
async function probe(baseUrl: string) {
const t0 = Date.now();
const ctrl = new AbortController();
const timer = setTimeout(() => ctrl.abort(), PROBE_TIMEOUT_MS);
try {
const res = await fetch(baseUrl.replace(/\/$/, '') + '/api/tags', {
signal: ctrl.signal,
});
clearTimeout(timer);
if (!res.ok) {
return {
ok: false as const,
baseUrl,
error: `Ollama returned HTTP ${res.status}`,
ms: Date.now() - t0,
};
}
const body = (await res.json()) as {
models?: Array<{
name: string;
size?: number;
modified_at?: string;
}>;
};
return {
ok: true as const,
baseUrl,
models: (body.models ?? []).map((m) => ({
name: m.name,
sizeBytes: m.size ?? null,
modifiedAt: m.modified_at ?? null,
})),
ms: Date.now() - t0,
};
} catch (e) {
clearTimeout(timer);
return {
ok: false as const,
baseUrl,
error:
ctrl.signal.aborted
? `Timed out after ${PROBE_TIMEOUT_MS / 1000}s`
: (e as Error).message,
ms: Date.now() - t0,
};
}
}
+190
View File
@@ -0,0 +1,190 @@
import { NextRequest, NextResponse } from 'next/server';
import { z } from 'zod';
import { getCurrentUser } from '@/lib/auth';
import { prisma } from '@/lib/prisma';
import { getProvider } from '@/lib/ai/providers';
/**
* POST /api/ai/test
*
* Body (optional):
* {
* // If supplied: test this draft config without saving it.
* // Otherwise: test the actor's currently active config.
* provider?: string,
* model?: string,
* baseUrl?: string,
* apiKey?: string,
* // If supplied + apiKey is null: pull the saved key for that
* // profile (so the UI can test a saved profile by id without
* // forcing the user to re-type the key).
* useSavedKeyForId?: string,
* }
*
* Sends a tiny "say hi in 3 words" prompt. Reports latency, sample
* reply (or finishReason if Gemini blocks it).
*
* Times out after 30s — long enough for cold Ollama starts.
*/
const TEST_TIMEOUT_MS = 30_000;
const bodySchema = z.object({
provider: z.string().optional(),
model: z.string().optional(),
baseUrl: z.string().nullable().optional(),
apiKey: z.string().nullable().optional(),
useSavedKeyForId: z.string().optional(),
});
export async function POST(request: NextRequest) {
const user = await getCurrentUser();
if (!user) {
return NextResponse.json({ ok: false, error: 'Unauthorized' }, { status: 401 });
}
const raw = await request.json().catch(() => ({}));
const parsed = bodySchema.safeParse(raw);
if (!parsed.success) {
return NextResponse.json(
{ ok: false, error: 'Invalid body' },
{ status: 400 },
);
}
const draft = parsed.data;
// Resolve the config to test:
// 1. If draft.provider is set → use the draft fields (testing
// a not-yet-saved config in the UI).
// 2. Else if draft.useSavedKeyForId is set → load that profile.
// 3. Else → use the active config (legacy single-config columns).
let provider: string | null;
let model: string | null;
let baseUrl: string | null;
let apiKey: string | null;
if (draft.provider) {
provider = draft.provider;
model = draft.model ?? null;
baseUrl = draft.baseUrl ?? null;
apiKey = draft.apiKey ?? null;
// Allow the UI to fill in just provider+model+baseUrl and have
// us pull the saved key by profile id (so the user doesn't have
// to retype it just to retest).
if (draft.useSavedKeyForId && (apiKey == null || apiKey === '')) {
const saved = await prisma.aIConfigProfile.findFirst({
where: { id: draft.useSavedKeyForId, userId: user.id },
select: { apiKey: true },
});
if (saved?.apiKey) apiKey = saved.apiKey;
}
} else if (draft.useSavedKeyForId) {
const saved = await prisma.aIConfigProfile.findFirst({
where: { id: draft.useSavedKeyForId, userId: user.id },
});
if (!saved) {
return NextResponse.json(
{ ok: false, error: 'Config not found.' },
{ status: 404 },
);
}
provider = saved.provider;
model = saved.model;
baseUrl = saved.baseUrl;
apiKey = saved.apiKey;
} else {
const prefs = await prisma.userPreferences.findUnique({
where: { userId: user.id },
select: { aiProvider: true, aiModel: true, aiBaseUrl: true, aiApiKey: true },
});
provider = prefs?.aiProvider ?? null;
model = prefs?.aiModel ?? null;
baseUrl = prefs?.aiBaseUrl ?? null;
apiKey = prefs?.aiApiKey ?? null;
}
if (!provider || !model) {
return NextResponse.json(
{
ok: false,
error: 'Pick a provider + model first.',
},
{ status: 400 },
);
}
const providerImpl = getProvider(provider);
if (!providerImpl) {
return NextResponse.json(
{ ok: false, error: `Unknown provider: ${provider}` },
{ status: 400 },
);
}
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), TEST_TIMEOUT_MS);
const t0 = Date.now();
let sample = '';
let tokensIn: number | undefined;
let tokensOut: number | undefined;
let providerError: string | null = null;
try {
for await (const chunk of providerImpl.generate({
apiKey,
baseUrl,
model,
systemPrompt:
'You are a connectivity test. Reply with EXACTLY three words: "Hello there friend." Nothing else.',
userPrompt: 'Say hi.',
signal: controller.signal,
// Generous output budget so thinking models (Gemini 2.5/3.x,
// OpenAI o-series) actually have room to emit visible text after
// their internal reasoning. Cheap because the prompt is tiny.
maxOutputTokens: 4096,
})) {
if (chunk.type === 'text') sample += chunk.delta;
else if (chunk.type === 'usage') {
tokensIn = chunk.tokensIn;
tokensOut = chunk.tokensOut;
} else if (chunk.type === 'error') {
providerError = chunk.message;
}
}
} catch (e) {
providerError =
controller.signal.aborted
? `Timed out after ${Math.round(TEST_TIMEOUT_MS / 1000)}s`
: (e as Error).message;
} finally {
clearTimeout(timer);
}
const ms = Date.now() - t0;
if (providerError) {
return NextResponse.json({ ok: false, error: providerError, ms }, { status: 200 });
}
if (!sample.trim()) {
return NextResponse.json(
{
ok: false,
error:
'Empty reply. The provider returned a response with no text. ' +
'For Gemini this often means a safety filter blocked the output ' +
'(check the model name + try a flagship model). For thinking ' +
'models the answer may have been spent on internal reasoning — ' +
'try a non-thinking model.',
ms,
},
{ status: 200 },
);
}
return NextResponse.json({
ok: true,
sample: sample.trim().slice(0, 200),
tokensIn,
tokensOut,
ms,
});
}
@@ -57,21 +57,35 @@ export async function POST(
); );
} }
// v1.1.0:4: pull the user's preferred weight unit so we can fall
// back to it when the program day didn't specify one.
const prefs = await prisma.userPreferences.findUnique({
where: { userId: user.id },
select: { defaultWeightUnit: true },
});
const userPrefUnit = prefs?.defaultWeightUnit ?? "lbs";
// Build SetLog rows: for each planned exercise, pre-create N // Build SetLog rows: for each planned exercise, pre-create N
// empty sets where N = exercise.sets ?? 1. The user fills in // empty sets where N = exercise.sets ?? 1. The user fills in
// reps/weight when they actually do them. // reps/weight when they actually do them. v1.1.0:4: if the
// ProgramExercise has a `suggestedWeight`, seed it on every set
// so the user starts with a target instead of a blank field.
const setLogsCreate: { const setLogsCreate: {
exerciseId: string; exerciseId: string;
setNumber: number; setNumber: number;
weight: number | null;
weightUnit: string; weightUnit: string;
}[] = []; }[] = [];
for (const ex of day.exercises) { for (const ex of day.exercises) {
const setCount = ex.sets ?? 1; const setCount = ex.sets ?? 1;
const unit =
ex.suggestedWeightUnit ?? ex.exercise.defaultWeightUnit ?? userPrefUnit;
for (let n = 1; n <= setCount; n++) { for (let n = 1; n <= setCount; n++) {
setLogsCreate.push({ setLogsCreate.push({
exerciseId: ex.exerciseId, exerciseId: ex.exerciseId,
setNumber: n, setNumber: n,
weightUnit: ex.exercise.defaultWeightUnit ?? "lbs", weight: ex.suggestedWeight ?? null,
weightUnit: unit,
}); });
} }
} }
+5 -1
View File
@@ -11,7 +11,7 @@ export default async function GeneratePage() {
const user = await getCurrentUser(); const user = await getCurrentUser();
if (!user) redirect('/auth/login'); if (!user) redirect('/auth/login');
const [templates, exercises, prefs] = await Promise.all([ const [templates, exercises, prefs, workoutCount] = await Promise.all([
prisma.aIPromptTemplate.findMany({ prisma.aIPromptTemplate.findMany({
where: { OR: [{ userId: null }, { userId: user.id }] }, where: { OR: [{ userId: null }, { userId: user.id }] },
orderBy: [{ isBuiltIn: 'desc' }, { name: 'asc' }], orderBy: [{ isBuiltIn: 'desc' }, { name: 'asc' }],
@@ -31,6 +31,9 @@ export default async function GeneratePage() {
where: { userId: user.id }, where: { userId: user.id },
select: { aiProvider: true, aiModel: true }, select: { aiProvider: true, aiModel: true },
}), }),
prisma.workout.count({
where: { userId: user.id, deletedAt: null },
}),
]); ]);
const aiConfigured = !!prefs?.aiProvider && !!prefs?.aiModel; const aiConfigured = !!prefs?.aiProvider && !!prefs?.aiModel;
@@ -74,6 +77,7 @@ export default async function GeneratePage() {
exercises={exercises} exercises={exercises}
providerLabel={prefs!.aiProvider!} providerLabel={prefs!.aiProvider!}
modelLabel={prefs!.aiModel!} modelLabel={prefs!.aiModel!}
workoutCount={workoutCount}
/> />
)} )}
</div> </div>
@@ -0,0 +1,90 @@
import { redirect, notFound } from 'next/navigation';
import Link from 'next/link';
import { ChevronLeft } from 'lucide-react';
import { getCurrentUser } from '@/lib/auth';
import { prisma } from '@/lib/prisma';
import GenerationDetail from '@/components/ai/GenerationDetail';
export const dynamic = 'force-dynamic';
/**
* v1.1.0:4 — Detail view for a single AIGeneration row.
*
* Why: previously a generation that finished while you weren't watching
* disappeared into a List that only showed metadata. To re-examine the
* model's output you had to apply it (which committed a Program). This
* page lets you see the parsed program tree first, then either:
* - Apply it (creates a Program — same flow as Generate's preview)
* - Re-generate from the same prompt
* - View the raw model response + the exact system/user prompts sent
*
* Status flows:
* pending → progress + stream attach (so reloading the page during
* a long Ollama run picks up where it left off)
* completed → static program tree + Apply
* applied → "View applied program" link
* failed → error + raw response details
*/
export default async function GenerationDetailPage({
params,
}: {
params: { id: string };
}) {
const user = await getCurrentUser();
if (!user) redirect('/auth/login');
const [row, exercises] = await Promise.all([
prisma.aIGeneration.findFirst({
where: { id: params.id, userId: user.id },
}),
prisma.exercise.findMany({
where: { userId: user.id },
select: { id: true, name: true, type: true },
orderBy: [{ type: 'asc' }, { name: 'asc' }],
}),
]);
if (!row) notFound();
return (
<div className="min-h-screen bg-[#0A0A0A]">
<div className="border-b border-zinc-800">
<div className="max-w-3xl mx-auto px-4 py-4 sm:py-6 flex items-center gap-3">
<Link
href="/main/ai/history"
className="text-zinc-400 hover:text-white"
aria-label="Back to history"
>
<ChevronLeft className="w-5 h-5" />
</Link>
<h1 className="text-2xl sm:text-3xl font-bold text-white">
AI · Generation
</h1>
</div>
</div>
<div className="max-w-3xl mx-auto px-4 py-6">
<GenerationDetail
row={{
id: row.id,
templateName: row.templateName,
userInput: row.userInput,
systemPrompt: row.systemPrompt,
userPrompt: row.userPrompt,
rawResponse: row.rawResponse,
parsedProgram: row.parsedProgram,
progressText: row.progressText,
provider: row.provider,
model: row.model,
tokensIn: row.tokensIn,
tokensOut: row.tokensOut,
durationMs: row.durationMs,
status: row.status,
errorMessage: row.errorMessage,
appliedProgramId: row.appliedProgramId,
createdAt: row.createdAt.toISOString(),
}}
exercises={exercises}
/>
</div>
</div>
);
}
@@ -23,6 +23,7 @@ export default async function HistoryPage() {
model: true, model: true,
tokensIn: true, tokensIn: true,
tokensOut: true, tokensOut: true,
durationMs: true,
status: true, status: true,
errorMessage: true, errorMessage: true,
appliedProgramId: true, appliedProgramId: true,
+4 -1
View File
@@ -15,7 +15,10 @@ export default async function MainLayout({
return ( return (
<div className="min-h-screen flex flex-col bg-[#0A0A0A]"> <div className="min-h-screen flex flex-col bg-[#0A0A0A]">
<Navigation userName={user.name || user.email || 'User'} /> <Navigation
userName={user.name || user.email || 'User'}
isAdmin={user.isAdmin}
/>
<main className="flex-1 app-content pb-20 md:pb-0"> <main className="flex-1 app-content pb-20 md:pb-0">
{children} {children}
</main> </main>
+99 -20
View File
@@ -14,23 +14,76 @@ import { logoutAction } from './actions';
interface NavigationProps { interface NavigationProps {
userName: string; userName: string;
isAdmin: boolean;
} }
const navLinks = [ interface NavSubItem {
/** Either a route href or a section anchor (#…) on the parent page. */
href: string;
label: string;
/** Admin-only — hidden for non-admin users. */
adminOnly?: boolean;
}
interface NavLink {
href: string;
label: string;
icon: typeof LayoutDashboard;
/** v1.1.0:4 — sub-navigation rendered when the user is on this section.
* Items can either deep-link to a sibling route or scroll to an anchor
* on the parent page. */
subItems?: NavSubItem[];
}
const navLinks: NavLink[] = [
{ href: '/main/dashboard', label: 'Dashboard', icon: LayoutDashboard }, { href: '/main/dashboard', label: 'Dashboard', icon: LayoutDashboard },
{ href: '/main/workouts', label: 'Workouts', icon: Dumbbell }, { href: '/main/workouts', label: 'Workouts', icon: Dumbbell },
{ href: '/main/programs', label: 'Programs', icon: Calendar }, { href: '/main/programs', label: 'Programs', icon: Calendar },
{ href: '/main/ai', label: 'AI', icon: Sparkles }, {
href: '/main/ai',
label: 'AI',
icon: Sparkles,
subItems: [
{ href: '/main/ai/generate', label: 'Generate' },
{ href: '/main/ai/history', label: 'History' },
{ href: '/main/ai/templates', label: 'Templates' },
],
},
{ href: '/main/exercises', label: 'Exercises', icon: ListChecks }, { href: '/main/exercises', label: 'Exercises', icon: ListChecks },
{ href: '/main/settings', label: 'Settings', icon: Settings }, {
href: '/main/settings',
label: 'Settings',
icon: Settings,
subItems: [
{ href: '/main/settings#general', label: 'General' },
{ href: '/main/settings#password', label: 'Password' },
{ href: '/main/settings#sessions', label: 'Sessions' },
{ href: '/main/settings#ai', label: 'AI integration' },
{ href: '/main/settings#data', label: 'Export & import' },
{ href: '/main/settings#instance', label: 'Instance', adminOnly: true },
{ href: '/main/settings#danger', label: 'Danger zone' },
],
},
]; ];
export default function Navigation({ userName }: NavigationProps) { export default function Navigation({ userName, isAdmin }: NavigationProps) {
const pathname = usePathname(); const pathname = usePathname();
const router = useRouter(); const router = useRouter();
const isActive = (href: string) => { // A top-level item is "active" if the current pathname matches it
return pathname === href || pathname.startsWith(href + '/'); // exactly OR is a subpage. We use this to decide whether to expand
// the sub-nav under it.
const isActive = (href: string) =>
pathname === href || pathname.startsWith(href + '/');
// A sub-item's active state depends on what it points to:
// - Route subitem (no #): exact pathname match
// - Anchor subitem (has #): always inactive in nav (anchor change
// doesn't fire pathname). The browser handles the highlight.
const isSubActive = (subHref: string) => {
const [path] = subHref.split('#');
if (subHref.includes('#')) return false;
return pathname === path;
}; };
const handleLogout = async () => { const handleLogout = async () => {
@@ -46,24 +99,50 @@ export default function Navigation({ userName }: NavigationProps) {
<h2 className="text-3xl font-display text-white tracking-wider">Proof of Work</h2> <h2 className="text-3xl font-display text-white tracking-wider">Proof of Work</h2>
</div> </div>
<nav className="flex-1 overflow-y-auto p-4 space-y-2"> <nav className="flex-1 overflow-y-auto p-4 space-y-1">
{navLinks.map((link) => { {navLinks.map((link) => {
const Icon = link.icon; const Icon = link.icon;
const active = isActive(link.href); const active = isActive(link.href);
return ( return (
<a <div key={link.href}>
key={link.href} <a
href={link.href} href={link.href}
className={`flex items-center gap-3 px-4 py-2.5 rounded transition-all duration-200 ${ className={`flex items-center gap-3 px-4 py-2.5 rounded transition-all duration-200 ${
active active
? 'bg-white text-black font-semibold' ? 'bg-white text-black font-semibold'
: 'text-zinc-500 hover:text-white hover:bg-zinc-900' : 'text-zinc-500 hover:text-white hover:bg-zinc-900'
}`} }`}
> >
<Icon className="w-5 h-5 flex-shrink-0" /> <Icon className="w-5 h-5 flex-shrink-0" />
<span className="text-sm">{link.label}</span> <span className="text-sm">{link.label}</span>
</a> </a>
{/* Expand sub-nav when this section is active. */}
{active && link.subItems && link.subItems.length > 0 && (
<ul className="ml-4 mt-1 mb-2 border-l border-zinc-800 pl-3 space-y-0.5">
{link.subItems
.filter((s) => !s.adminOnly || isAdmin)
.map((sub) => {
const subActive = isSubActive(sub.href);
return (
<li key={sub.href}>
<a
href={sub.href}
className={`block px-3 py-1.5 rounded text-xs transition-colors ${
subActive
? 'text-white bg-zinc-800'
: 'text-zinc-500 hover:text-white hover:bg-zinc-900'
}`}
>
{sub.label}
</a>
</li>
);
})}
</ul>
)}
</div>
); );
})} })}
</nav> </nav>
@@ -84,7 +163,7 @@ export default function Navigation({ userName }: NavigationProps) {
</div> </div>
</aside> </aside>
{/* Mobile Bottom Nav */} {/* Mobile Bottom Nav (no sub-nav — limited screen real estate) */}
<header className="flex md:hidden fixed bottom-0 left-0 right-0 border-t border-zinc-800 bg-[#0A0A0A]"> <header className="flex md:hidden fixed bottom-0 left-0 right-0 border-t border-zinc-800 bg-[#0A0A0A]">
<nav className="flex items-center justify-around h-[var(--bottom-nav-height)] w-full"> <nav className="flex items-center justify-around h-[var(--bottom-nav-height)] w-full">
{navLinks.map((link) => { {navLinks.map((link) => {
+11 -9
View File
@@ -30,17 +30,19 @@ export default async function SettingsPage() {
</div> </div>
<div className="max-w-2xl mx-auto px-4 py-6 sm:px-6 space-y-8"> <div className="max-w-2xl mx-auto px-4 py-6 sm:px-6 space-y-8">
<SettingsForm user={user} /> <div id="general"><SettingsForm user={user} /></div>
<ChangePasswordForm /> <div id="password"><ChangePasswordForm /></div>
<SessionsList /> <div id="sessions"><SessionsList /></div>
<AIIntegration /> <div id="ai"><AIIntegration /></div>
<ExportMyData /> <div id="data"><ExportMyData /></div>
{user.isAdmin && instanceSettings && ( {user.isAdmin && instanceSettings && (
<AdminInstanceSettings <div id="instance">
initialSignupsOpen={instanceSettings.signupsOpen} <AdminInstanceSettings
/> initialSignupsOpen={instanceSettings.signupsOpen}
/>
</div>
)} )}
<DangerZone /> <div id="danger"><DangerZone /></div>
</div> </div>
</div> </div>
); );
+285 -153
View File
@@ -2,7 +2,9 @@
import { useEffect, useMemo, useRef, useState } from 'react'; import { useEffect, useMemo, useRef, useState } from 'react';
import { useRouter } from 'next/navigation'; import { useRouter } from 'next/navigation';
import { Loader2, Sparkles, Square } from 'lucide-react'; import { Loader2, Sparkles } from 'lucide-react';
import { lenientJsonParse } from '@/lib/ai/lenientJson';
import { estimateCost, formatCost } from '@/lib/ai/pricing';
const DAY_LABELS = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']; const DAY_LABELS = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
@@ -28,6 +30,8 @@ interface AIExercise {
repsMax?: number | null; repsMax?: number | null;
rpe?: number | null; rpe?: number | null;
restSeconds?: number | null; restSeconds?: number | null;
suggestedWeight?: number | null;
suggestedWeightUnit?: 'lbs' | 'kg' | null;
notes?: string | null; notes?: string | null;
} }
interface AIDay { interface AIDay {
@@ -52,7 +56,15 @@ interface AIProgram {
type Phase = type Phase =
| { kind: 'idle' } | { kind: 'idle' }
| { kind: 'streaming'; raw: string } | {
kind: 'streaming';
raw: string;
// Last successfully parsed snapshot. Sticky — we only update it
// when a new chunk lets lenientJsonParse return a fresh value.
// This kills the flicker we used to have, where the panel toggled
// back to "Waiting for first JSON…" between parseable chunks.
lastPartial: Partial<AIProgram> | null;
}
| { kind: 'parsed'; raw: string; program: AIProgram } | { kind: 'parsed'; raw: string; program: AIProgram }
| { kind: 'failed'; raw: string; message: string }; | { kind: 'failed'; raw: string; message: string };
@@ -61,33 +73,44 @@ export default function GenerateClient({
exercises, exercises,
providerLabel, providerLabel,
modelLabel, modelLabel,
workoutCount,
}: { }: {
templates: Template[]; templates: Template[];
exercises: LibraryExercise[]; exercises: LibraryExercise[];
providerLabel: string; providerLabel: string;
modelLabel: string; modelLabel: string;
workoutCount: number;
}) { }) {
const router = useRouter(); const router = useRouter();
const [templateId, setTemplateId] = useState(templates[0]?.id ?? ''); const [templateId, setTemplateId] = useState(templates[0]?.id ?? '');
const [userInput, setUserInput] = useState(''); const [userInput, setUserInput] = useState('');
const [includeHistory, setIncludeHistory] = useState(workoutCount >= 10);
const [generationId, setGenerationId] = useState<string | null>(null); const [generationId, setGenerationId] = useState<string | null>(null);
const [phase, setPhase] = useState<Phase>({ kind: 'idle' }); const [phase, setPhase] = useState<Phase>({ kind: 'idle' });
const [tokens, setTokens] = useState<{ in?: number; out?: number }>({}); const [tokens, setTokens] = useState<{ in?: number; out?: number; durationMs?: number }>({});
const abortRef = useRef<AbortController | null>(null); const [navWarning, setNavWarning] = useState(false);
const closeStreamRef = useRef<(() => void) | null>(null);
const selectedTemplate = useMemo( // Wire up native warning if the user tries to leave during a stream.
() => templates.find((t) => t.id === templateId), useEffect(() => {
[templates, templateId], if (phase.kind !== 'streaming') return;
); setNavWarning(true);
return () => setNavWarning(false);
}, [phase.kind]);
/**
* Generation kickoff — POST /api/ai/generate gets back an id, then
* we attach to the SSE stream by id. The runner is detached on the
* server: navigating away no longer cancels generation, the row keeps
* filling in. We surface a banner so the user knows that.
*/
const handleGenerate = async () => { const handleGenerate = async () => {
if (!userInput.trim()) return; if (!userInput.trim()) return;
setPhase({ kind: 'streaming', raw: '' }); setPhase({ kind: 'streaming', raw: '', lastPartial: null });
setGenerationId(null); setGenerationId(null);
setTokens({}); setTokens({});
abortRef.current = new AbortController(); let id: string;
let raw = '';
try { try {
const res = await fetch('/api/ai/generate', { const res = await fetch('/api/ai/generate', {
method: 'POST', method: 'POST',
@@ -95,11 +118,11 @@ export default function GenerateClient({
body: JSON.stringify({ body: JSON.stringify({
templateId: templateId || null, templateId: templateId || null,
userInput, userInput,
includeHistory,
}), }),
signal: abortRef.current.signal,
}); });
const body = await res.json().catch(() => ({}));
if (!res.ok) { if (!res.ok) {
const body = await res.json().catch(() => ({}));
setPhase({ setPhase({
kind: 'failed', kind: 'failed',
raw: '', raw: '',
@@ -107,114 +130,127 @@ export default function GenerateClient({
}); });
return; return;
} }
if (!res.body) { id = body.id;
setPhase({ kind: 'failed', raw: '', message: 'No response body.' }); setGenerationId(id);
return;
}
// Parse SSE stream
const reader = res.body.getReader();
const decoder = new TextDecoder();
let buf = '';
let done = false;
while (!done) {
const { value, done: d } = await reader.read();
if (d) {
done = true;
break;
}
buf += decoder.decode(value, { stream: true });
let idx;
while ((idx = buf.indexOf('\n\n')) >= 0) {
const event = buf.slice(0, idx);
buf = buf.slice(idx + 2);
let evtName = 'message';
const dataLines: string[] = [];
for (const line of event.split('\n')) {
if (line.startsWith('event:')) evtName = line.slice(6).trim();
else if (line.startsWith('data:'))
dataLines.push(line.slice(5).trimStart());
}
if (!dataLines.length) continue;
const data = dataLines.join('\n');
let parsed: any;
try {
parsed = JSON.parse(data);
} catch {
continue;
}
if (evtName === 'generation') {
setGenerationId(parsed.id);
} else if (evtName === 'text') {
raw += parsed.delta;
setPhase({ kind: 'streaming', raw });
} else if (evtName === 'usage') {
setTokens({ in: parsed.tokensIn, out: parsed.tokensOut });
} else if (evtName === 'complete') {
// Server already validated/stored the parsed program. We
// fetch the generation record AFTER the stream closes
// (below) to get the parsed JSON. Just record the
// success/failure outcome here; if it failed, render
// the error inline now since we're not going to fetch.
if (!parsed.parsedOk) {
setPhase({
kind: 'failed',
raw,
message: parsed.errorMessage ?? 'Failed to parse model output.',
});
}
}
}
}
} catch (e) { } catch (e) {
if ((e as Error).name === 'AbortError') { setPhase({ kind: 'failed', raw: '', message: (e as Error).message });
setPhase({ kind: 'failed', raw, message: 'Cancelled.' });
} else {
setPhase({
kind: 'failed',
raw,
message: (e as Error).message,
});
}
return; return;
} }
// After stream closes, fetch the generation row to get the parsed // Attach to the SSE stream.
// program (we don't try to re-parse client-side — server already did). attachStream(id);
const id = generationIdRef.current; };
if (id) {
const r = await fetch(`/api/ai/generations/${id}`); const attachStream = (id: string) => {
if (r.ok) { const es = new EventSource(`/api/ai/generations/${id}/stream`);
const gen = await r.json(); closeStreamRef.current = () => es.close();
if (gen.status === 'completed' && gen.parsedProgram) { let raw = '';
setPhase({ let lastPartial: Partial<AIProgram> | null = null;
kind: 'parsed',
raw, es.addEventListener('text', (ev) => {
program: JSON.parse(gen.parsedProgram) as AIProgram, const data = JSON.parse((ev as MessageEvent).data);
}); raw += data.delta;
return; const next = lenientJsonParse(raw) as Partial<AIProgram> | null;
} // Sticky: only replace the snapshot if we got a fresh parse.
if (gen.status === 'failed') { // Otherwise leave the previous one rendered — kills the flicker.
setPhase({ if (next) lastPartial = next;
kind: 'failed', setPhase({ kind: 'streaming', raw, lastPartial });
raw, });
message: gen.errorMessage ?? 'Failed.', es.addEventListener('usage', (ev) => {
}); const data = JSON.parse((ev as MessageEvent).data);
return; setTokens((t) => ({ ...t, in: data.tokensIn, out: data.tokensOut }));
});
es.addEventListener('complete', async (ev) => {
const data = JSON.parse((ev as MessageEvent).data);
es.close();
closeStreamRef.current = null;
setTokens((t) => ({
...t,
in: data.tokensIn ?? t.in,
out: data.tokensOut ?? t.out,
durationMs: data.durationMs,
}));
if (data.parsedOk) {
// Pull the parsed program from the row.
const r = await fetch(`/api/ai/generations/${id}`);
if (r.ok) {
const gen = await r.json();
if (gen.parsedProgram) {
setPhase({
kind: 'parsed',
raw,
program: JSON.parse(gen.parsedProgram) as AIProgram,
});
return;
}
} }
} }
} setPhase({
kind: 'failed',
raw,
message: data.errorMessage ?? 'Failed to parse model output.',
});
});
es.onerror = () => {
// EventSource auto-reconnects on transient errors. We only treat
// it as fatal if we never got a `complete` event AND the stream
// is closed. The simplest signal: readyState===CLOSED.
if (es.readyState === EventSource.CLOSED) {
closeStreamRef.current = null;
setPhase((p) => {
if (p.kind === 'streaming') {
return {
kind: 'failed',
raw: p.raw,
message: 'Stream disconnected. The generation may still be running — check Generation history.',
};
}
return p;
});
}
};
}; };
// Capture the generationId in a ref so the async fetch after the // Beforeunload warning while streaming — important since the user can
// stream has access to it (the closure above sees the initial null). // CLOSE the tab and the generation continues server-side, but data
const generationIdRef = useRef<string | null>(null); // sent after they close won't be visible until they re-open and look
// at history.
useEffect(() => { useEffect(() => {
generationIdRef.current = generationId; if (!navWarning) return;
}, [generationId]); const onBeforeUnload = (e: BeforeUnloadEvent) => {
e.preventDefault();
e.returnValue = '';
};
window.addEventListener('beforeunload', onBeforeUnload);
return () => window.removeEventListener('beforeunload', onBeforeUnload);
}, [navWarning]);
const handleCancel = () => { // Detach on unmount (Next.js client-side nav) — we don't want a
abortRef.current?.abort(); // dangling EventSource. The server keeps generating either way.
}; useEffect(() => {
return () => {
closeStreamRef.current?.();
};
}, []);
// Cost — derived from active provider/model + tokens once both are
// known. Pre-known because we know the provider; use a placeholder
// computation.
const costStr = useMemo(() => {
if (tokens.in == null || tokens.out == null) return null;
const c = estimateCost({
provider: providerLabel,
model: modelLabel,
tokensIn: tokens.in,
tokensOut: tokens.out,
});
return formatCost(c);
}, [providerLabel, modelLabel, tokens.in, tokens.out]);
const selectedTemplate = useMemo(
() => templates.find((t) => t.id === templateId),
[templates, templateId],
);
return ( return (
<div className="space-y-6"> <div className="space-y-6">
@@ -256,48 +292,90 @@ export default function GenerateClient({
/> />
</Field> </Field>
<label className="flex items-start gap-2 text-xs text-zinc-300">
<input
type="checkbox"
checked={includeHistory}
onChange={(e) => setIncludeHistory(e.target.checked)}
disabled={phase.kind === 'streaming' || workoutCount === 0}
className="mt-0.5"
/>
<span>
Include my workout history as context{' '}
<span className="text-zinc-500">
({workoutCount === 0
? 'no workouts logged yet — disabled'
: `last 90 days · summarizes per-exercise frequency, recent weights, stagnations`}
)
</span>
</span>
</label>
<div className="flex items-center gap-2"> <div className="flex items-center gap-2">
{phase.kind === 'streaming' ? ( <button
<button type="button"
type="button" onClick={handleGenerate}
onClick={handleCancel} disabled={!userInput.trim() || phase.kind === 'streaming'}
className="inline-flex items-center gap-2 px-4 py-2 rounded border border-red-900 text-red-400 text-xs uppercase tracking-wider hover:bg-red-900/30" className="inline-flex items-center gap-2 px-5 py-2 rounded bg-white text-black font-bold text-xs uppercase tracking-wider hover:bg-gray-100 disabled:bg-zinc-700 disabled:text-zinc-500"
> >
<Square className="w-3.5 h-3.5" /> <Sparkles className="w-4 h-4" />
Cancel Generate
</button> </button>
) : (
<button
type="button"
onClick={handleGenerate}
disabled={!userInput.trim()}
className="inline-flex items-center gap-2 px-5 py-2 rounded bg-white text-black font-bold text-xs uppercase tracking-wider hover:bg-gray-100 disabled:bg-zinc-700 disabled:text-zinc-500"
>
<Sparkles className="w-4 h-4" />
Generate
</button>
)}
</div> </div>
</section> </section>
{(phase.kind === 'streaming' || phase.kind === 'failed' || phase.kind === 'parsed') && ( {(phase.kind === 'streaming' || phase.kind === 'failed' || phase.kind === 'parsed') && (
<section className="space-y-3"> <section className="space-y-3">
{phase.kind === 'streaming' && (
<div className="rounded bg-blue-950/30 border border-blue-900 px-4 py-3 text-xs text-blue-200">
<p className="font-bold text-blue-100 mb-1">Generation runs in the background.</p>
<p>
You can close this page or navigate away the model will keep
writing on the server. Come back to{' '}
<a href="/main/ai/history" className="underline hover:text-blue-100">
AI · History
</a>{' '}
to see the result. Local Ollama models on slower hardware can take
10+ minutes; commercial APIs typically finish in under a minute.
</p>
</div>
)}
<div className="flex items-center justify-between"> <div className="flex items-center justify-between">
<h2 className="text-sm font-semibold text-white uppercase tracking-wider"> <h2 className="text-sm font-semibold text-white uppercase tracking-wider">
{phase.kind === 'streaming' ? 'Generating...' : 'Response'} {phase.kind === 'streaming' ? 'Generating' : 'Response'}
</h2> </h2>
{(tokens.in != null || tokens.out != null) && ( <span className="text-[11px] text-zinc-500 uppercase tracking-wider">
<span className="text-[11px] text-zinc-500 uppercase tracking-wider"> {tokens.in != null && (
{tokens.in ?? '?'} in · {tokens.out ?? '?'} out <>
</span> {tokens.in} in · {tokens.out ?? '?'} out
)} </>
)}
{costStr && <> · {costStr}</>}
{tokens.durationMs != null && (
<> · {(tokens.durationMs / 1000).toFixed(1)}s</>
)}
</span>
</div> </div>
{phase.kind === 'streaming' && ( {phase.kind === 'streaming' && (
<div className="bg-zinc-950 border border-zinc-800 rounded p-3 font-mono text-[11px] text-zinc-400 max-h-80 overflow-auto whitespace-pre-wrap"> <>
{phase.raw || '(waiting for first token...)'} {phase.lastPartial ? (
<Loader2 className="inline w-3 h-3 animate-spin ml-2" /> <PartialPreview partial={phase.lastPartial} />
</div> ) : (
<div className="text-xs text-zinc-500 italic flex items-center gap-2">
<Loader2 className="w-3 h-3 animate-spin" />
Waiting for the first parseable JSON
</div>
)}
<details className="text-xs text-zinc-500">
<summary className="cursor-pointer">Raw stream</summary>
<div className="bg-zinc-950 border border-zinc-800 rounded p-3 font-mono text-[11px] text-zinc-400 max-h-80 overflow-auto whitespace-pre-wrap mt-2">
{phase.raw || '(waiting for first token…)'}
<Loader2 className="inline w-3 h-3 animate-spin ml-2" />
</div>
</details>
</>
)} )}
{phase.kind === 'failed' && ( {phase.kind === 'failed' && (
@@ -360,9 +438,14 @@ function ProgramPreview({
let n = 0; let n = 0;
for (const w of program.weeks) for (const w of program.weeks)
for (const d of w.days) for (const d of w.days)
for (const ex of d.exercises) if (!ex.exerciseId) n++; for (const ex of d.exercises) {
// Either no id OR an id that doesn't actually exist in the
// user's library (the model invented one). Both must be
// resolved before the apply step accepts the program.
if (!ex.exerciseId || !exerciseLookup.has(ex.exerciseId)) n++;
}
return n; return n;
}, [program]); }, [program, exerciseLookup]);
const setExerciseId = ( const setExerciseId = (
weekIdx: number, weekIdx: number,
@@ -381,7 +464,6 @@ function ProgramPreview({
setProgram((p) => { setProgram((p) => {
const next = structuredClone(p); const next = structuredClone(p);
next.weeks[weekIdx].days[dayIdx].exercises.splice(exIdx, 1); next.weeks[weekIdx].days[dayIdx].exercises.splice(exIdx, 1);
// Renumber order
next.weeks[weekIdx].days[dayIdx].exercises.forEach( next.weeks[weekIdx].days[dayIdx].exercises.forEach(
(ex: AIExercise, i: number) => { (ex: AIExercise, i: number) => {
ex.order = i; ex.order = i;
@@ -451,9 +533,7 @@ function ProgramPreview({
> >
<summary className="cursor-pointer px-3 py-2 text-sm text-white"> <summary className="cursor-pointer px-3 py-2 text-sm text-white">
Week {w.weekNumber} Week {w.weekNumber}
{w.phase && ( {w.phase && <span className="text-zinc-500"> · {w.phase}</span>}
<span className="text-zinc-500"> · {w.phase}</span>
)}
<span className="text-zinc-600 text-xs"> <span className="text-zinc-600 text-xs">
{' '} {' '}
({w.days.length} day{w.days.length === 1 ? '' : 's'}) ({w.days.length} day{w.days.length === 1 ? '' : 's'})
@@ -476,14 +556,19 @@ function ProgramPreview({
</p> </p>
<ul className="mt-2 space-y-2"> <ul className="mt-2 space-y-2">
{d.exercises.map((ex, eIdx) => { {d.exercises.map((ex, eIdx) => {
const isUnknown = !ex.exerciseId; const isUnknown =
!ex.exerciseId || !exerciseLookup.has(ex.exerciseId);
const lib = ex.exerciseId const lib = ex.exerciseId
? exerciseLookup.get(ex.exerciseId) ? exerciseLookup.get(ex.exerciseId)
: null; : null;
return ( return (
<li <li
key={eIdx} key={eIdx}
className={`text-sm ${isUnknown ? 'bg-amber-950/30 border border-amber-900' : 'bg-zinc-950 border border-zinc-800'} rounded p-2`} className={`text-sm ${
isUnknown
? 'bg-amber-950/30 border border-amber-900'
: 'bg-zinc-950 border border-zinc-800'
} rounded p-2`}
> >
<div className="flex items-start justify-between gap-2"> <div className="flex items-start justify-between gap-2">
<div className="min-w-0 flex-1"> <div className="min-w-0 flex-1">
@@ -495,12 +580,15 @@ function ProgramPreview({
</span> </span>
)} )}
</div> </div>
{(ex.sets || ex.repsMin || ex.repsMax || ex.rpe || ex.restSeconds) && ( {(ex.sets || ex.repsMin || ex.repsMax || ex.rpe || ex.restSeconds || ex.suggestedWeight) && (
<div className="text-xs text-zinc-500 mt-0.5"> <div className="text-xs text-zinc-500 mt-0.5">
{ex.sets ? `${ex.sets}×` : ''} {ex.sets ? `${ex.sets}×` : ''}
{ex.repsMin === ex.repsMax || !ex.repsMax {ex.repsMin === ex.repsMax || !ex.repsMax
? (ex.repsMin ?? '?') ? (ex.repsMin ?? '?')
: `${ex.repsMin}-${ex.repsMax}`} : `${ex.repsMin}-${ex.repsMax}`}
{ex.suggestedWeight != null && (
<> @ {ex.suggestedWeight}{ex.suggestedWeightUnit ?? ''}</>
)}
{ex.rpe ? ` @ RPE ${ex.rpe}` : ''} {ex.rpe ? ` @ RPE ${ex.rpe}` : ''}
{ex.restSeconds ? ` · rest ${ex.restSeconds}s` : ''} {ex.restSeconds ? ` · rest ${ex.restSeconds}s` : ''}
</div> </div>
@@ -523,14 +611,14 @@ function ProgramPreview({
{isUnknown && ( {isUnknown && (
<div className="mt-2"> <div className="mt-2">
<select <select
value="" value={ex.exerciseId ?? ''}
onChange={(e) => onChange={(e) =>
setExerciseId(wIdx, dIdx, eIdx, e.target.value || null) setExerciseId(wIdx, dIdx, eIdx, e.target.value || null)
} }
className="w-full text-xs px-2 py-1 rounded border border-amber-900 bg-zinc-900 text-white" className="w-full text-xs px-2 py-1 rounded border border-amber-900 bg-zinc-900 text-white"
> >
<option value=""> <option value="">
Map to existing exercise... Map to existing exercise
</option> </option>
{exercises.map((opt) => ( {exercises.map((opt) => (
<option key={opt.id} value={opt.id}> <option key={opt.id} value={opt.id}>
@@ -589,7 +677,7 @@ function ProgramPreview({
{applying ? ( {applying ? (
<> <>
<Loader2 className="inline w-4 h-4 animate-spin mr-2" /> <Loader2 className="inline w-4 h-4 animate-spin mr-2" />
Applying... Applying
</> </>
) : ( ) : (
'Apply this program' 'Apply this program'
@@ -613,3 +701,47 @@ function Field({ label, children }: { label: string; children: React.ReactNode }
</label> </label>
); );
} }
function PartialPreview({ partial }: { partial: Partial<AIProgram> }) {
const weeks = (partial.weeks as AIWeek[] | undefined) ?? [];
return (
<div className="bg-zinc-950 border border-zinc-800 rounded p-3 space-y-2">
<div className="flex items-center gap-2 text-xs">
<Loader2 className="w-3 h-3 animate-spin text-zinc-500" />
<span className="text-zinc-400">
Building program{' '}
{partial.name && (
<span className="text-white font-semibold">{partial.name}</span>
)}
{partial.type && (
<span className="text-zinc-500"> · {partial.type}</span>
)}
{typeof partial.durationWeeks === 'number' && (
<span className="text-zinc-500"> · {partial.durationWeeks} wk</span>
)}
</span>
</div>
{weeks.length > 0 && (
<ul className="text-xs text-zinc-300 space-y-1">
{weeks.map((w, i) => (
<li key={i}>
<span className="text-zinc-500">Week {w?.weekNumber ?? '?'}:</span>{' '}
{Array.isArray(w?.days)
? `${w.days.length} day${w.days.length === 1 ? '' : 's'} (${
w.days.reduce(
(n: number, d: AIDay) =>
n + (Array.isArray(d?.exercises) ? d.exercises.length : 0),
0,
)
} exercises)`
: '…'}
{w?.phase && (
<span className="text-zinc-500"> · {w.phase}</span>
)}
</li>
))}
</ul>
)}
</div>
);
}
@@ -0,0 +1,630 @@
'use client';
import { useEffect, useMemo, useState } from 'react';
import { useRouter } from 'next/navigation';
import Link from 'next/link';
import { Loader2 } from 'lucide-react';
import { lenientJsonParse } from '@/lib/ai/lenientJson';
import { estimateCost, formatCost } from '@/lib/ai/pricing';
const DAY_LABELS = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
interface AIExercise {
exerciseId: string | null;
exerciseName: string;
order: number;
sets?: number | null;
repsMin?: number | null;
repsMax?: number | null;
rpe?: number | null;
restSeconds?: number | null;
suggestedWeight?: number | null;
suggestedWeightUnit?: 'lbs' | 'kg' | null;
notes?: string | null;
}
interface AIDay {
dayOfWeek: number;
name?: string | null;
description?: string | null;
exercises: AIExercise[];
}
interface AIWeek {
weekNumber: number;
phase?: string | null;
description?: string | null;
days: AIDay[];
}
interface AIProgram {
name: string;
description?: string | null;
type: string;
durationWeeks: number;
weeks: AIWeek[];
}
interface LibraryExercise {
id: string;
name: string;
type: string;
}
interface Row {
id: string;
templateName: string | null;
userInput: string;
systemPrompt: string;
userPrompt: string;
rawResponse: string | null;
parsedProgram: string | null;
progressText: string | null;
provider: string;
model: string;
tokensIn: number | null;
tokensOut: number | null;
durationMs: number | null;
status: string;
errorMessage: string | null;
appliedProgramId: string | null;
createdAt: string;
}
/**
* Client-side detail view for an AIGeneration. Three modes:
*
* - PENDING: poll for progress + render the live partial-JSON preview.
* The runner keeps writing `progressText` even if no SSE clients
* are subscribed, so polling works for cross-process resume too.
*
* - COMPLETED: render the parsed program tree with an Apply button.
* Same UI as the Generate page's preview, factored out below.
*
* - APPLIED: the user already turned this into a Program; show a
* link there. Re-applying isn't allowed (would create a duplicate).
*
* - FAILED: error message + raw response collapsed by default.
*/
export default function GenerationDetail({
row: initialRow,
exercises,
}: {
row: Row;
exercises: LibraryExercise[];
}) {
const router = useRouter();
const [row, setRow] = useState(initialRow);
// Poll while pending. 1.5s cadence — fast enough to feel live,
// gentle on the DB. Stops when status flips terminal.
useEffect(() => {
if (row.status !== 'pending') return;
let cancelled = false;
const tick = async () => {
try {
const r = await fetch(`/api/ai/generations/${row.id}`);
if (!r.ok || cancelled) return;
const fresh = await r.json();
if (cancelled) return;
setRow({
...fresh,
createdAt:
typeof fresh.createdAt === 'string'
? fresh.createdAt
: new Date(fresh.createdAt).toISOString(),
});
} catch {
/* transient — try again */
}
};
const id = setInterval(tick, 1500);
return () => {
cancelled = true;
clearInterval(id);
};
}, [row.id, row.status]);
const cost = useMemo(
() =>
estimateCost({
provider: row.provider,
model: row.model,
tokensIn: row.tokensIn,
tokensOut: row.tokensOut,
}),
[row.provider, row.model, row.tokensIn, row.tokensOut],
);
// Live partial during pending.
const partial = useMemo(
() =>
row.status === 'pending' && row.progressText
? (lenientJsonParse(row.progressText) as Partial<AIProgram> | null)
: null,
[row.status, row.progressText],
);
const parsedProgram = useMemo(
() =>
row.parsedProgram ? (JSON.parse(row.parsedProgram) as AIProgram) : null,
[row.parsedProgram],
);
return (
<div className="space-y-5">
{/* Header / metadata */}
<header className="space-y-2">
<div className="flex items-center gap-2 text-xs text-zinc-500 uppercase tracking-wider flex-wrap">
<StatusPill status={row.status} />
<span>{new Date(row.createdAt).toLocaleString()}</span>
<span className="text-zinc-600">·</span>
<span>
{row.provider} · {row.model}
</span>
{row.tokensIn != null && (
<>
<span className="text-zinc-600">·</span>
<span>
{row.tokensIn} in · {row.tokensOut ?? '?'} out
</span>
</>
)}
{cost != null && (
<>
<span className="text-zinc-600">·</span>
<span>{formatCost(cost)}</span>
</>
)}
{row.durationMs != null && (
<>
<span className="text-zinc-600">·</span>
<span>{formatDuration(row.durationMs)}</span>
</>
)}
</div>
{row.templateName && (
<p className="text-xs text-zinc-400">
Template: <span className="text-zinc-200">{row.templateName}</span>
</p>
)}
</header>
{/* User's prompt */}
<section className="bg-zinc-900 border border-zinc-800 rounded p-4">
<h2 className="text-xs font-semibold text-zinc-400 uppercase tracking-wider mb-2">
Your specifics
</h2>
<p className="text-sm text-zinc-200 whitespace-pre-wrap">{row.userInput}</p>
</section>
{/* Pending: live preview */}
{row.status === 'pending' && (
<section className="space-y-3">
<div className="rounded bg-blue-950/30 border border-blue-900 px-4 py-3 text-xs text-blue-200">
<p className="font-bold text-blue-100 mb-1 flex items-center gap-2">
<Loader2 className="w-3 h-3 animate-spin" />
Still generating
</p>
<p>
Polling every 1.5s for progress. Safe to leave this page
the model keeps running on the server and you&apos;ll see the
result when you come back.
</p>
</div>
{partial ? (
<PartialTree partial={partial} />
) : (
<div className="text-xs text-zinc-500 italic flex items-center gap-2">
<Loader2 className="w-3 h-3 animate-spin" />
Waiting for the first parseable JSON
</div>
)}
</section>
)}
{/* Failed */}
{row.status === 'failed' && (
<section className="space-y-3">
<div className="bg-red-950/40 border border-red-900 rounded p-3 text-sm text-red-300">
{row.errorMessage ?? 'Failed.'}
</div>
<Link
href="/main/ai/generate"
className="inline-block text-xs text-zinc-400 underline hover:text-white"
>
Try again from Generate
</Link>
</section>
)}
{/* Applied — link to the program */}
{row.status === 'applied' && row.appliedProgramId && (
<section>
<Link
href={`/main/programs/${row.appliedProgramId}`}
className="inline-block px-4 py-2 rounded bg-emerald-700 text-white text-xs uppercase tracking-wider font-bold hover:bg-emerald-600"
>
View applied program
</Link>
</section>
)}
{/* Completed (not yet applied) — show preview + Apply */}
{row.status === 'completed' && parsedProgram && (
<ProgramPreview
generationId={row.id}
program={parsedProgram}
exercises={exercises}
onApplied={(programId) => router.push(`/main/programs/${programId}`)}
/>
)}
{/* Raw response + prompts (collapsed) */}
{row.rawResponse && (
<details className="text-xs text-zinc-500">
<summary className="cursor-pointer">Raw model response</summary>
<pre className="bg-zinc-950 border border-zinc-800 rounded p-3 mt-2 whitespace-pre-wrap max-h-96 overflow-auto">
{row.rawResponse}
</pre>
</details>
)}
<details className="text-xs text-zinc-500">
<summary className="cursor-pointer">Exact prompts sent</summary>
<div className="mt-2 space-y-2">
<div>
<p className="font-semibold text-zinc-400 uppercase tracking-wider mb-1">
System
</p>
<pre className="bg-zinc-950 border border-zinc-800 rounded p-3 whitespace-pre-wrap max-h-72 overflow-auto">
{row.systemPrompt}
</pre>
</div>
<div>
<p className="font-semibold text-zinc-400 uppercase tracking-wider mb-1">
User
</p>
<pre className="bg-zinc-950 border border-zinc-800 rounded p-3 whitespace-pre-wrap max-h-72 overflow-auto">
{row.userPrompt}
</pre>
</div>
</div>
</details>
</div>
);
}
function ProgramPreview({
generationId,
program: initial,
exercises,
onApplied,
}: {
generationId: string;
program: AIProgram;
exercises: LibraryExercise[];
onApplied: (programId: string) => void;
}) {
const [program, setProgram] = useState<AIProgram>(initial);
const [applying, setApplying] = useState(false);
const [error, setError] = useState<string | null>(null);
const [startDate, setStartDate] = useState(
new Date().toISOString().slice(0, 10),
);
const [activate, setActivate] = useState(true);
const exerciseLookup = useMemo(
() => new Map(exercises.map((e) => [e.id, e])),
[exercises],
);
const unresolvedCount = useMemo(() => {
let n = 0;
for (const w of program.weeks)
for (const d of w.days)
for (const ex of d.exercises) {
if (!ex.exerciseId || !exerciseLookup.has(ex.exerciseId)) n++;
}
return n;
}, [program, exerciseLookup]);
const setExerciseId = (
weekIdx: number,
dayIdx: number,
exIdx: number,
newId: string | null,
) => {
setProgram((p) => {
const next = structuredClone(p);
next.weeks[weekIdx].days[dayIdx].exercises[exIdx].exerciseId = newId;
return next;
});
};
const removeExercise = (weekIdx: number, dayIdx: number, exIdx: number) => {
setProgram((p) => {
const next = structuredClone(p);
next.weeks[weekIdx].days[dayIdx].exercises.splice(exIdx, 1);
next.weeks[weekIdx].days[dayIdx].exercises.forEach(
(ex: AIExercise, i: number) => {
ex.order = i;
},
);
return next;
});
};
const handleApply = async () => {
if (unresolvedCount > 0) {
setError(
`Resolve all ${unresolvedCount} unknown exercise(s) before applying.`,
);
return;
}
setError(null);
setApplying(true);
try {
const res = await fetch('/api/ai/apply', {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({
generationId,
program,
startDate,
isActive: activate,
}),
});
const body = await res.json();
if (!res.ok) throw new Error(body.error ?? `HTTP ${res.status}`);
onApplied(body.programId);
} catch (e) {
setError((e as Error).message);
} finally {
setApplying(false);
}
};
return (
<div className="bg-zinc-900 border border-zinc-800 rounded p-4 space-y-4">
<div>
<h3 className="text-lg font-bold text-white">{program.name}</h3>
<p className="text-xs text-zinc-500 mt-1">
{program.type} · {program.durationWeeks} week
{program.durationWeeks === 1 ? '' : 's'} · {program.weeks.length}{' '}
week{program.weeks.length === 1 ? '' : 's'} planned
</p>
{program.description && (
<p className="text-sm text-zinc-300 mt-2">{program.description}</p>
)}
</div>
{unresolvedCount > 0 && (
<div className="rounded bg-amber-950/30 border border-amber-900 px-3 py-2 text-xs text-amber-200">
{unresolvedCount} exercise(s) the AI couldn&apos;t map to your
library. Pick a replacement or remove them before applying.
</div>
)}
<div className="space-y-3">
{program.weeks.map((w, wIdx) => (
<details
key={w.weekNumber}
open={wIdx === 0}
className="bg-zinc-950 border border-zinc-800 rounded"
>
<summary className="cursor-pointer px-3 py-2 text-sm text-white">
Week {w.weekNumber}
{w.phase && <span className="text-zinc-500"> · {w.phase}</span>}
<span className="text-zinc-600 text-xs">
{' '}
({w.days.length} day{w.days.length === 1 ? '' : 's'})
</span>
</summary>
<div className="p-3 space-y-2">
{w.days.map((d, dIdx) => (
<div
key={d.dayOfWeek}
className="bg-zinc-900 border border-zinc-800 rounded p-3"
>
<p className="text-xs font-semibold text-zinc-300 uppercase tracking-wider">
{DAY_LABELS[d.dayOfWeek]}
{d.name && (
<span className="text-zinc-500 normal-case font-normal">
{' '}
· {d.name}
</span>
)}
</p>
<ul className="mt-2 space-y-2">
{d.exercises.map((ex, eIdx) => {
const isUnknown =
!ex.exerciseId || !exerciseLookup.has(ex.exerciseId);
const lib = ex.exerciseId
? exerciseLookup.get(ex.exerciseId)
: null;
return (
<li
key={eIdx}
className={`text-sm ${
isUnknown
? 'bg-amber-950/30 border border-amber-900'
: 'bg-zinc-950 border border-zinc-800'
} rounded p-2`}
>
<div className="flex items-start justify-between gap-2">
<div className="min-w-0 flex-1">
<div className="text-white">
{lib?.name ?? ex.exerciseName}
{isUnknown && (
<span className="ml-2 text-[10px] uppercase tracking-wider text-amber-400">
not in library
</span>
)}
</div>
{(ex.sets || ex.repsMin || ex.repsMax || ex.rpe || ex.restSeconds || ex.suggestedWeight) && (
<div className="text-xs text-zinc-500 mt-0.5">
{ex.sets ? `${ex.sets}×` : ''}
{ex.repsMin === ex.repsMax || !ex.repsMax
? (ex.repsMin ?? '?')
: `${ex.repsMin}-${ex.repsMax}`}
{ex.suggestedWeight != null && (
<> @ {ex.suggestedWeight}{ex.suggestedWeightUnit ?? ''}</>
)}
{ex.rpe ? ` @ RPE ${ex.rpe}` : ''}
{ex.restSeconds ? ` · rest ${ex.restSeconds}s` : ''}
</div>
)}
{ex.notes && (
<div className="text-xs text-zinc-400 mt-1 italic">
{ex.notes}
</div>
)}
</div>
<button
type="button"
onClick={() => removeExercise(wIdx, dIdx, eIdx)}
className="text-xs text-red-400 hover:text-red-300 px-1"
title="Remove from program"
>
</button>
</div>
{isUnknown && (
<div className="mt-2">
<select
value={ex.exerciseId ?? ''}
onChange={(e) =>
setExerciseId(wIdx, dIdx, eIdx, e.target.value || null)
}
className="w-full text-xs px-2 py-1 rounded border border-amber-900 bg-zinc-900 text-white"
>
<option value="">
Map to existing exercise
</option>
{exercises.map((opt) => (
<option key={opt.id} value={opt.id}>
{opt.name} ({opt.type})
</option>
))}
</select>
</div>
)}
</li>
);
})}
</ul>
</div>
))}
</div>
</details>
))}
</div>
<div className="border-t border-zinc-800 pt-4 space-y-3">
<div className="grid grid-cols-2 gap-3">
<label className="block">
<span className="text-[11px] font-semibold text-zinc-400 uppercase tracking-wider block mb-1">
Start date
</span>
<input
type="date"
value={startDate}
onChange={(e) => setStartDate(e.target.value)}
className="w-full px-3 py-2 text-sm rounded border border-zinc-700 bg-zinc-800 text-white"
/>
</label>
<label className="flex items-end gap-2">
<input
type="checkbox"
checked={activate}
onChange={(e) => setActivate(e.target.checked)}
className="mb-2"
/>
<span className="text-xs text-zinc-300 mb-2">
Activate this program after applying
</span>
</label>
</div>
{error && (
<div className="rounded bg-red-900/50 px-3 py-2 border border-red-800 text-xs text-red-400">
{error}
</div>
)}
<button
type="button"
onClick={handleApply}
disabled={applying || unresolvedCount > 0}
className="px-5 py-2 rounded bg-emerald-700 text-white font-bold text-xs uppercase tracking-wider hover:bg-emerald-600 disabled:bg-zinc-700 disabled:text-zinc-500"
>
{applying ? (
<>
<Loader2 className="inline w-4 h-4 animate-spin mr-2" />
Applying
</>
) : (
'Apply this program'
)}
</button>
</div>
</div>
);
}
function PartialTree({ partial }: { partial: Partial<AIProgram> }) {
const weeks = (partial.weeks as AIWeek[] | undefined) ?? [];
return (
<div className="bg-zinc-950 border border-zinc-800 rounded p-3 space-y-2">
<div className="text-xs">
{partial.name && (
<span className="text-white font-semibold">{partial.name}</span>
)}
{partial.type && (
<span className="text-zinc-500"> · {partial.type}</span>
)}
{typeof partial.durationWeeks === 'number' && (
<span className="text-zinc-500"> · {partial.durationWeeks} wk</span>
)}
</div>
{weeks.length > 0 && (
<ul className="text-xs text-zinc-300 space-y-1">
{weeks.map((w, i) => (
<li key={i}>
<span className="text-zinc-500">Week {w?.weekNumber ?? '?'}:</span>{' '}
{Array.isArray(w?.days)
? `${w.days.length} day${w.days.length === 1 ? '' : 's'} (${w.days.reduce(
(n: number, d: AIDay) =>
n + (Array.isArray(d?.exercises) ? d.exercises.length : 0),
0,
)} exercises)`
: '…'}
{w?.phase && <span className="text-zinc-500"> · {w.phase}</span>}
</li>
))}
</ul>
)}
</div>
);
}
function StatusPill({ status }: { status: string }) {
const map: Record<string, { color: string; label: string }> = {
pending: { color: 'text-zinc-400 bg-zinc-800', label: 'pending' },
completed: { color: 'text-emerald-400 bg-emerald-950', label: 'completed' },
applied: { color: 'text-emerald-400 bg-emerald-950', label: 'applied' },
failed: { color: 'text-red-400 bg-red-950', label: 'failed' },
};
const m = map[status] ?? map.pending;
return (
<span
className={`inline-flex items-center gap-1 ${m.color} rounded px-2 py-0.5 text-[10px]`}
>
{m.label}
</span>
);
}
function formatDuration(ms: number): string {
if (ms < 1000) return `${ms}ms`;
if (ms < 60_000) return `${(ms / 1000).toFixed(1)}s`;
const m = Math.floor(ms / 60_000);
const s = Math.round((ms % 60_000) / 1000);
return `${m}m ${s}s`;
}
+75 -11
View File
@@ -1,8 +1,9 @@
'use client'; 'use client';
import { useState } from 'react'; import { useMemo, useState } from 'react';
import Link from 'next/link'; import Link from 'next/link';
import { Trash2, CheckCircle2, AlertCircle, Loader2 } from 'lucide-react'; import { Trash2, CheckCircle2, AlertCircle, Loader2 } from 'lucide-react';
import { estimateCost, formatCost } from '@/lib/ai/pricing';
interface Row { interface Row {
id: string; id: string;
@@ -12,6 +13,7 @@ interface Row {
model: string; model: string;
tokensIn: number | null; tokensIn: number | null;
tokensOut: number | null; tokensOut: number | null;
durationMs: number | null;
status: string; status: string;
errorMessage: string | null; errorMessage: string | null;
appliedProgramId: string | null; appliedProgramId: string | null;
@@ -26,6 +28,34 @@ export default function HistoryList({
const [rows, setRows] = useState(initialRows); const [rows, setRows] = useState(initialRows);
const [busyId, setBusyId] = useState<string | null>(null); const [busyId, setBusyId] = useState<string | null>(null);
// Per-row cost + 30-day rolling total. Pricing is best-effort
// (Ollama = free, openai-compatible = unknown, others priced
// from lib/ai/pricing.ts). Free + unknown both contribute 0 to
// the total so it's a lower bound at worst.
const rowsWithCost = useMemo(
() =>
rows.map((r) => ({
...r,
costUsd: estimateCost({
provider: r.provider,
model: r.model,
tokensIn: r.tokensIn,
tokensOut: r.tokensOut,
}),
})),
[rows],
);
const totalLast30Days = useMemo(() => {
const cutoff = Date.now() - 30 * 86_400_000;
let total = 0;
for (const r of rowsWithCost) {
if (r.costUsd != null && new Date(r.createdAt).getTime() >= cutoff) {
total += r.costUsd;
}
}
return total;
}, [rowsWithCost]);
const handleDelete = async (id: string) => { const handleDelete = async (id: string) => {
if (!confirm('Delete this generation? The applied Program (if any) stays.')) if (!confirm('Delete this generation? The applied Program (if any) stays.'))
return; return;
@@ -48,15 +78,27 @@ export default function HistoryList({
} }
return ( return (
<>
<p className="text-xs text-zinc-500 mb-4 uppercase tracking-wider">
Estimated cost (last 30 days):{' '}
<span className="text-zinc-200">{formatCost(totalLast30Days)}</span>
<span className="text-zinc-600">
{' '}
· Ollama + custom-URL gateways excluded
</span>
</p>
<ul className="space-y-3"> <ul className="space-y-3">
{rows.map((r) => ( {rowsWithCost.map((r) => (
<li <li
key={r.id} key={r.id}
className="bg-zinc-900 border border-zinc-800 rounded p-4" className="bg-zinc-900 border border-zinc-800 rounded p-4"
> >
<div className="flex items-start justify-between gap-3"> <div className="flex items-start justify-between gap-3">
<div className="min-w-0 flex-1"> <Link
<div className="flex items-center gap-2 text-xs text-zinc-500 uppercase tracking-wider"> href={`/main/ai/history/${r.id}`}
className="min-w-0 flex-1 hover:bg-zinc-800/30 -m-2 p-2 rounded transition-colors"
>
<div className="flex items-center gap-2 text-xs text-zinc-500 uppercase tracking-wider flex-wrap">
<StatusBadge status={r.status} /> <StatusBadge status={r.status} />
<span>{new Date(r.createdAt).toLocaleString()}</span> <span>{new Date(r.createdAt).toLocaleString()}</span>
<span className="text-zinc-600">·</span> <span className="text-zinc-600">·</span>
@@ -71,6 +113,22 @@ export default function HistoryList({
</span> </span>
</> </>
)} )}
{r.costUsd != null && (
<>
<span className="text-zinc-600">·</span>
<span title="Estimated USD cost based on the model's published per-token pricing">
{formatCost(r.costUsd)}
</span>
</>
)}
{r.durationMs != null && (
<>
<span className="text-zinc-600">·</span>
<span title="Wall-clock generation time">
{formatDuration(r.durationMs)}
</span>
</>
)}
</div> </div>
{r.templateName && ( {r.templateName && (
<p className="text-xs text-zinc-400 mt-1"> <p className="text-xs text-zinc-400 mt-1">
@@ -86,14 +144,11 @@ export default function HistoryList({
</p> </p>
)} )}
{r.appliedProgramId && ( {r.appliedProgramId && (
<Link <span className="inline-block text-xs text-emerald-400 mt-2">
href={`/main/programs/${r.appliedProgramId}`} applied to a program
className="inline-block text-xs text-emerald-400 underline mt-2" </span>
>
View applied program
</Link>
)} )}
</div> </Link>
<button <button
type="button" type="button"
onClick={() => handleDelete(r.id)} onClick={() => handleDelete(r.id)}
@@ -111,9 +166,18 @@ export default function HistoryList({
</li> </li>
))} ))}
</ul> </ul>
</>
); );
} }
function formatDuration(ms: number): string {
if (ms < 1000) return `${ms}ms`;
if (ms < 60_000) return `${(ms / 1000).toFixed(1)}s`;
const m = Math.floor(ms / 60_000);
const s = Math.round((ms % 60_000) / 1000);
return `${m}m ${s}s`;
}
function StatusBadge({ status }: { status: string }) { function StatusBadge({ status }: { status: string }) {
const map: Record<string, { color: string; icon: typeof CheckCircle2 }> = { const map: Record<string, { color: string; icon: typeof CheckCircle2 }> = {
pending: { color: 'text-zinc-400', icon: Loader2 }, pending: { color: 'text-zinc-400', icon: Loader2 },
@@ -1,79 +1,451 @@
'use client'; 'use client';
import { useEffect, useState } from 'react'; import { useEffect, useState } from 'react';
import { Loader2 } from 'lucide-react'; import { Loader2, Plus, Trash2, Star } from 'lucide-react';
import { MODEL_MENU } from '@/lib/ai/pricing';
/**
* v1.1.0:4 — Multi-config AI integration panel.
*
* Lets the user save multiple AI configurations (one per provider, or
* several of the same provider with different models) and toggle one
* as active. Per-config "Test connection" so you can verify before
* activating. Dropdowns of recommended models for major providers.
* Ollama auto-detect: probes the StartOS internal address + offers a
* dropdown of installed models when reachable.
*/
const PROVIDERS = [ const PROVIDERS = [
{ id: 'claude', label: 'Anthropic Claude', requiresKey: true, requiresUrl: false, modelHint: 'claude-sonnet-4-5 / claude-opus-4-5' }, { id: 'claude', label: 'Anthropic Claude', requiresKey: true, requiresUrl: false },
{ id: 'openai', label: 'OpenAI', requiresKey: true, requiresUrl: false, modelHint: 'gpt-5 / gpt-5-mini' }, { id: 'openai', label: 'OpenAI', requiresKey: true, requiresUrl: false },
{ id: 'openai-compatible', label: 'OpenAI-compatible (custom URL)', requiresKey: true, requiresUrl: true, modelHint: 'whatever your gateway exposes' }, {
{ id: 'gemini', label: 'Google Gemini', requiresKey: true, requiresUrl: false, modelHint: 'gemini-2.0-flash / gemini-2.5-pro' }, id: 'openai-compatible',
{ id: 'ollama', label: 'Ollama (self-hosted)', requiresKey: false, requiresUrl: true, modelHint: 'llama3.1:8b / qwen2.5:14b' }, label: 'OpenAI-compatible (custom URL)',
requiresKey: true,
requiresUrl: true,
},
{ id: 'gemini', label: 'Google Gemini', requiresKey: true, requiresUrl: false },
{ id: 'ollama', label: 'Ollama (self-hosted)', requiresKey: false, requiresUrl: true },
] as const; ] as const;
interface Config { type ProviderId = (typeof PROVIDERS)[number]['id'];
aiProvider: string | null;
aiModel: string | null; interface SavedConfig {
aiBaseUrl: string | null; id: string;
aiKeyConfigured: boolean; name: string;
provider: ProviderId;
model: string;
baseUrl: string | null;
keyConfigured: boolean;
createdAt: string;
} }
type TestResult =
| { ok: true; sample: string; tokensIn?: number; tokensOut?: number; ms: number }
| { ok: false; error: string; ms?: number };
export default function AIIntegration() { export default function AIIntegration() {
const [cfg, setCfg] = useState<Config | null>(null); const [configs, setConfigs] = useState<SavedConfig[]>([]);
const [provider, setProvider] = useState<string>(''); const [activeId, setActiveId] = useState<string | null>(null);
const [model, setModel] = useState(''); const [loading, setLoading] = useState(true);
const [baseUrl, setBaseUrl] = useState('');
const [apiKey, setApiKey] = useState('');
const [showKey, setShowKey] = useState(false);
const [keyDirty, setKeyDirty] = useState(false);
const [saving, setSaving] = useState(false);
const [error, setError] = useState<string | null>(null); const [error, setError] = useState<string | null>(null);
const [success, setSuccess] = useState(false); const [showForm, setShowForm] = useState(false);
const [editingId, setEditingId] = useState<string | null>(null);
const refresh = async () => {
setError(null);
try {
const r = await fetch('/api/ai/configs');
if (!r.ok) throw new Error(`HTTP ${r.status}`);
const body = await r.json();
setConfigs(body.configs ?? []);
setActiveId(body.activeId ?? null);
} catch (e) {
setError((e as Error).message);
} finally {
setLoading(false);
}
};
useEffect(() => { useEffect(() => {
fetch('/api/ai/config') refresh();
.then((r) => r.json())
.then((c) => {
setCfg(c);
setProvider(c.aiProvider ?? '');
setModel(c.aiModel ?? '');
setBaseUrl(c.aiBaseUrl ?? '');
})
.catch(() => setError('Failed to load AI config.'));
}, []); }, []);
const handleActivate = async (id: string) => {
const r = await fetch(`/api/ai/configs/${id}/activate`, { method: 'POST' });
if (r.ok) await refresh();
else alert('Failed to activate.');
};
const handleDelete = async (id: string, name: string) => {
if (!confirm(`Delete the AI config "${name}"? You'll need to re-enter it to use it again.`))
return;
const r = await fetch(`/api/ai/configs/${id}`, { method: 'DELETE' });
if (r.ok) await refresh();
else alert('Failed to delete.');
};
return (
<section className="bg-zinc-900 border border-zinc-800 rounded-lg p-6 space-y-4" id="ai-integration">
<header>
<h2 className="text-lg font-bold text-white">AI integration</h2>
<p className="text-sm text-zinc-500 mt-1">
Save multiple AI configurations and toggle which one the{' '}
<span className="text-zinc-300">AI Generate</span> page uses.
Self-hosted Ollama on StartOS auto-detects no key needed.
</p>
</header>
{error && (
<div className="rounded bg-red-900/50 px-3 py-2 border border-red-800 text-xs text-red-400">
{error}
</div>
)}
{loading ? (
<div className="text-zinc-500 text-sm flex items-center gap-2">
<Loader2 className="w-4 h-4 animate-spin" />
Loading configs
</div>
) : (
<>
{configs.length === 0 && !showForm && (
<div className="rounded border border-zinc-800 px-4 py-6 text-sm text-zinc-400 text-center">
No AI configs yet. Add one to start generating programs.
</div>
)}
{configs.length > 0 && (
<ul className="space-y-2">
{configs.map((c) => (
<ConfigRow
key={c.id}
cfg={c}
isActive={c.id === activeId}
isEditing={editingId === c.id}
onActivate={() => handleActivate(c.id)}
onDelete={() => handleDelete(c.id, c.name)}
onEdit={() => setEditingId(editingId === c.id ? null : c.id)}
onSaved={() => {
setEditingId(null);
refresh();
}}
/>
))}
</ul>
)}
{showForm ? (
<ConfigForm
onCancel={() => setShowForm(false)}
onCreated={() => {
setShowForm(false);
refresh();
}}
/>
) : (
<button
type="button"
onClick={() => setShowForm(true)}
className="inline-flex items-center gap-2 px-4 py-2 rounded border border-zinc-700 text-zinc-200 text-xs uppercase tracking-wider hover:bg-zinc-800"
>
<Plus className="w-4 h-4" />
Add AI config
</button>
)}
</>
)}
</section>
);
}
/**
* One saved config row. Shows provider/model/key indicator + active
* badge. Click "Test" to ping the model. Click "Set active" to make
* this the one Generate uses. Click "Edit" to expand an inline form
* for renaming, swapping the model, or rotating the key.
*/
function ConfigRow({
cfg,
isActive,
isEditing,
onActivate,
onDelete,
onEdit,
onSaved,
}: {
cfg: SavedConfig;
isActive: boolean;
isEditing: boolean;
onActivate: () => void;
onDelete: () => void;
onEdit: () => void;
onSaved: () => void;
}) {
const [testing, setTesting] = useState(false);
const [testResult, setTestResult] = useState<TestResult | null>(null);
const handleTest = async () => {
setTesting(true);
setTestResult(null);
try {
const r = await fetch('/api/ai/test', {
method: 'POST',
headers: { 'content-type': 'application/json' },
// Test the saved config by id; the server pulls the stored key.
body: JSON.stringify({ useSavedKeyForId: cfg.id }),
});
setTestResult(await r.json());
} catch (e) {
setTestResult({ ok: false, error: (e as Error).message });
} finally {
setTesting(false);
}
};
const providerMeta = PROVIDERS.find((p) => p.id === cfg.provider);
return (
<li
className={`rounded border ${
isActive ? 'border-emerald-700 bg-emerald-950/20' : 'border-zinc-800 bg-zinc-950'
} p-3 space-y-2`}
>
<div className="flex items-start justify-between gap-3">
<div className="min-w-0 flex-1">
<div className="flex items-center gap-2">
<span className="font-semibold text-white text-sm truncate">
{cfg.name}
</span>
{isActive && (
<span className="inline-flex items-center gap-1 text-[10px] uppercase tracking-wider text-emerald-400 font-bold">
<Star className="w-3 h-3 fill-emerald-400" />
Active
</span>
)}
</div>
<div className="text-xs text-zinc-500 mt-0.5">
{providerMeta?.label ?? cfg.provider} · {cfg.model}
{cfg.baseUrl && (
<>
{' · '}
<code className="text-zinc-400">{cfg.baseUrl}</code>
</>
)}
{providerMeta?.requiresKey && (
<>
{' · '}
<span className={cfg.keyConfigured ? 'text-zinc-400' : 'text-amber-400'}>
{cfg.keyConfigured ? 'Key saved' : 'No key'}
</span>
</>
)}
</div>
</div>
<div className="flex items-center gap-1">
{!isActive && (
<button
type="button"
onClick={onActivate}
className="px-2 py-1 text-[11px] uppercase tracking-wider rounded text-zinc-300 hover:bg-zinc-800"
title="Make this the AI config that Generate uses"
>
Set active
</button>
)}
<button
type="button"
onClick={handleTest}
disabled={testing}
className="px-2 py-1 text-[11px] uppercase tracking-wider rounded text-zinc-300 hover:bg-zinc-800 disabled:opacity-50"
>
{testing ? (
<>
<Loader2 className="inline w-3 h-3 animate-spin mr-1" />
Testing
</>
) : (
'Test'
)}
</button>
<button
type="button"
onClick={onEdit}
className="px-2 py-1 text-[11px] uppercase tracking-wider rounded text-zinc-300 hover:bg-zinc-800"
>
{isEditing ? 'Cancel' : 'Edit'}
</button>
<button
type="button"
onClick={onDelete}
className="p-1 text-red-400 hover:text-red-300"
title="Delete this config"
>
<Trash2 className="w-3.5 h-3.5" />
</button>
</div>
</div>
{testResult && (
<div
className={`rounded px-2 py-1.5 border text-xs ${
testResult.ok
? 'bg-emerald-900/40 border-emerald-800 text-emerald-300'
: 'bg-red-900/50 border-red-800 text-red-400'
}`}
>
{testResult.ok ? (
<>
Connected in {(testResult.ms / 1000).toFixed(1)}s
{testResult.tokensIn != null &&
` · ${testResult.tokensIn} in / ${testResult.tokensOut ?? '?'} out`}
<div className="mt-0.5 text-zinc-400">
Sample reply: <span className="text-zinc-200">{testResult.sample}</span>
</div>
</>
) : (
<> {testResult.error}</>
)}
</div>
)}
{isEditing && (
<div className="border-t border-zinc-800 pt-3">
<ConfigForm
initial={cfg}
onCancel={onEdit}
onCreated={onSaved}
/>
</div>
)}
</li>
);
}
interface ConfigFormProps {
/** When set: editing this saved config (PATCH). Otherwise: creating new (POST). */
initial?: SavedConfig;
onCancel: () => void;
onCreated: () => void;
}
/**
* Add-or-edit form for a single AI config. Logic worth noting:
*
* - Model field is a dropdown of `MODEL_MENU[provider]` for major
* providers; falls through to free text for openai-compatible / ollama
* / "Other (type your own)".
* - For Ollama: probes /api/ai/ollama/models on provider-or-baseUrl
* change and (a) pre-fills the URL if the default StartOS address
* responds, (b) replaces the model dropdown with the actual
* installed models.
* - For Anthropic/OpenAI/Gemini: exposes a "Test draft" button that
* tests the in-progress form values without saving — handy for
* checking a key before committing.
*/
function ConfigForm({ initial, onCancel, onCreated }: ConfigFormProps) {
const isEdit = !!initial;
const [name, setName] = useState(initial?.name ?? '');
const [provider, setProvider] = useState<ProviderId>(initial?.provider ?? 'claude');
const [model, setModel] = useState(initial?.model ?? '');
const [modelMode, setModelMode] = useState<'menu' | 'custom'>(
initial && !MODEL_MENU[initial.provider]?.find((m) => m.id === initial.model)
? 'custom'
: 'menu',
);
const [baseUrl, setBaseUrl] = useState(initial?.baseUrl ?? '');
const [apiKey, setApiKey] = useState('');
const [setActive, setSetActive] = useState(!isEdit); // new configs default to active
const [showKey, setShowKey] = useState(false);
const [saving, setSaving] = useState(false);
const [error, setError] = useState<string | null>(null);
const [testResult, setTestResult] = useState<TestResult | null>(null);
const [testing, setTesting] = useState(false);
// Ollama auto-detect.
const [ollamaModels, setOllamaModels] = useState<{ name: string }[] | null>(null);
const [ollamaProbing, setOllamaProbing] = useState(false);
const [ollamaProbeError, setOllamaProbeError] = useState<string | null>(null);
const meta = PROVIDERS.find((p) => p.id === provider); const meta = PROVIDERS.find((p) => p.id === provider);
// Probe Ollama on provider switch (or baseUrl change while ollama).
useEffect(() => {
if (provider !== 'ollama') {
setOllamaModels(null);
setOllamaProbeError(null);
return;
}
let cancelled = false;
setOllamaProbing(true);
setOllamaProbeError(null);
const url = baseUrl
? `/api/ai/ollama/models?baseUrl=${encodeURIComponent(baseUrl)}`
: '/api/ai/ollama/models';
fetch(url)
.then((r) => r.json())
.then((b) => {
if (cancelled) return;
if (b.ok) {
setOllamaModels(b.models ?? []);
// Pre-fill URL if the user hadn't typed one yet.
if (!baseUrl && b.baseUrl) setBaseUrl(b.baseUrl);
// Pre-pick a model if there's exactly one and we're in create mode.
if (!isEdit && !model && (b.models?.length ?? 0) === 1) {
setModel(b.models[0].name);
}
} else {
setOllamaModels(null);
setOllamaProbeError(b.error ?? 'Probe failed');
}
})
.catch((e) => {
if (!cancelled) setOllamaProbeError((e as Error).message);
})
.finally(() => {
if (!cancelled) setOllamaProbing(false);
});
return () => {
cancelled = true;
};
// We deliberately depend on baseUrl too so changing the URL re-probes.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [provider, baseUrl]);
// Reset draft test result whenever the user changes any input — so the
// green "✓ Connected" indicator never lingers from a previous attempt.
useEffect(() => {
setTestResult(null);
}, [provider, model, baseUrl, apiKey]);
const menu = MODEL_MENU[provider] ?? [];
const showMenu = modelMode === 'menu' && menu.length > 0;
const handleSave = async () => { const handleSave = async () => {
setSaving(true); setSaving(true);
setError(null); setError(null);
setSuccess(false);
try { try {
const body: Record<string, string | null> = { const body: Record<string, unknown> = {
aiProvider: provider || null, name: name || undefined,
aiModel: model || null, provider,
aiBaseUrl: baseUrl || null, model,
baseUrl: baseUrl || null,
}; };
// Only send apiKey if it was changed (avoids stomping a stored key if (apiKey) body.apiKey = apiKey;
// when the user just edits the model name). if (!isEdit) body.setActive = setActive;
if (keyDirty) body.aiApiKey = apiKey || null;
const res = await fetch('/api/ai/config', { const url = isEdit ? `/api/ai/configs/${initial.id}` : '/api/ai/configs';
method: 'POST', const method = isEdit ? 'PATCH' : 'POST';
const r = await fetch(url, {
method,
headers: { 'content-type': 'application/json' }, headers: { 'content-type': 'application/json' },
body: JSON.stringify(body), body: JSON.stringify(body),
}); });
if (!res.ok) { if (!r.ok) {
const b = await res.json().catch(() => ({})); const b = await r.json().catch(() => ({}));
throw new Error(b.error ?? `HTTP ${res.status}`); throw new Error(b.error ?? `HTTP ${r.status}`);
} }
setSuccess(true); onCreated();
setKeyDirty(false);
setApiKey('');
// Refresh the "configured" indicator
const c = await (await fetch('/api/ai/config')).json();
setCfg(c);
setTimeout(() => setSuccess(false), 4000);
} catch (e) { } catch (e) {
setError((e as Error).message); setError((e as Error).message);
} finally { } finally {
@@ -81,135 +453,312 @@ export default function AIIntegration() {
} }
}; };
return ( const handleTestDraft = async () => {
<section className="bg-zinc-900 border border-zinc-800 rounded-lg p-6 space-y-4"> setTesting(true);
<header> setTestResult(null);
<h2 className="text-lg font-bold text-white">AI integration</h2> try {
<p className="text-sm text-zinc-500 mt-1"> const r = await fetch('/api/ai/test', {
Connect a model to generate training programs from natural-language method: 'POST',
prompts. Pick a provider, enter a model + key, and the{' '} headers: { 'content-type': 'application/json' },
<span className="text-zinc-300">AI Generate</span> page will use body: JSON.stringify({
it. Self-hosted Ollama running on your StartOS host needs no key provider,
just point Base URL at it (e.g.{' '} model,
<code className="text-zinc-400">http://ollama.embassy:11434</code>). baseUrl: baseUrl || null,
</p> apiKey: apiKey || null,
</header> // If we're editing and the user didn't change the key field,
// borrow the saved key for the test.
useSavedKeyForId: isEdit ? initial!.id : undefined,
}),
});
setTestResult(await r.json());
} catch (e) {
setTestResult({ ok: false, error: (e as Error).message });
} finally {
setTesting(false);
}
};
<div className="space-y-4"> return (
<Field label="Provider"> <div className="space-y-3 bg-zinc-900 border border-zinc-800 rounded p-3">
<Field label="Name (optional)">
<input
value={name}
onChange={(e) => setName(e.target.value)}
placeholder="e.g. Local Ollama, Claude (work)"
className={inputClass}
/>
</Field>
<Field label="Provider">
<select
value={provider}
onChange={(e) => {
setProvider(e.target.value as ProviderId);
setModel(''); // reset on provider change
setModelMode('menu');
}}
className={inputClass}
disabled={isEdit}
>
{PROVIDERS.map((p) => (
<option key={p.id} value={p.id}>
{p.label}
</option>
))}
</select>
{isEdit && (
<p className="text-[11px] text-zinc-500 mt-1">
Provider can&apos;t be changed; delete this config and add a new one.
</p>
)}
</Field>
{/* Ollama: replace the model dropdown with installed models if probe succeeded */}
{provider === 'ollama' ? (
<Field
label={
<>
Model{' '}
{ollamaProbing ? (
<span className="text-zinc-500 normal-case font-normal">· probing</span>
) : ollamaModels ? (
<span className="text-emerald-400 normal-case font-normal">
· {ollamaModels.length} installed
</span>
) : ollamaProbeError ? (
<span className="text-amber-400 normal-case font-normal">
· could not reach Ollama (type a name)
</span>
) : null}
</>
}
>
{ollamaModels && ollamaModels.length > 0 ? (
<select
value={model}
onChange={(e) => setModel(e.target.value)}
className={inputClass}
>
<option value=""> Pick an installed model </option>
{ollamaModels.map((m) => (
<option key={m.name} value={m.name}>
{m.name}
</option>
))}
</select>
) : (
<input
value={model}
onChange={(e) => setModel(e.target.value)}
placeholder="llama3.1:8b · qwen2.5:14b · mistral:7b"
className={inputClass}
/>
)}
</Field>
) : showMenu ? (
<Field label="Model">
<select <select
value={provider} value={model}
onChange={(e) => setProvider(e.target.value)} onChange={(e) => {
if (e.target.value === '__custom__') {
setModelMode('custom');
setModel('');
} else {
setModel(e.target.value);
}
}}
className={inputClass} className={inputClass}
> >
<option value=""> Disabled (no AI) </option> <option value=""> Pick a model </option>
{PROVIDERS.map((p) => ( {menu.map((m) => (
<option key={p.id} value={p.id}> <option key={m.id} value={m.id}>
{p.label} {m.recommended ? '★ ' : ''}
{m.label}
</option> </option>
))} ))}
<option value="__custom__">Other (type your own)</option>
</select> </select>
</Field> </Field>
) : (
<Field
label={
<>
Model{' '}
{provider !== 'openai-compatible' && menu.length > 0 && (
<button
type="button"
onClick={() => setModelMode('menu')}
className="text-zinc-500 hover:text-zinc-300 normal-case font-normal text-[11px]"
>
· use dropdown
</button>
)}
</>
}
>
<input
value={model}
onChange={(e) => setModel(e.target.value)}
placeholder="exact model id"
className={inputClass}
/>
</Field>
)}
{provider && ( {meta?.requiresUrl && (
<> <Field label="Base URL">
<Field label="Model"> <input
<input value={baseUrl}
value={model} onChange={(e) => setBaseUrl(e.target.value)}
onChange={(e) => setModel(e.target.value)} placeholder={
placeholder={meta?.modelHint ?? ''} meta.id === 'ollama'
className={inputClass} ? 'http://ollama.startos:11434'
/> : 'https://your-gateway.example.com/v1'
</Field> }
className={inputClass}
/>
</Field>
)}
{meta?.requiresUrl && ( {meta?.requiresKey && (
<Field label="Base URL"> <Field
<input label={
value={baseUrl} <>
onChange={(e) => setBaseUrl(e.target.value)} API key{' '}
placeholder={ {isEdit && initial?.keyConfigured && !apiKey && (
meta.id === 'ollama' <span className="text-zinc-500 normal-case font-normal">
? 'http://ollama.embassy:11434' · key saved (leave blank to keep)
: 'https://your-gateway.example.com/v1' </span>
} )}
className={inputClass} </>
/> }
</Field> >
)} <div className="relative">
<input
{meta?.requiresKey && ( type={showKey ? 'text' : 'password'}
<Field value={apiKey}
label={ onChange={(e) => setApiKey(e.target.value)}
cfg?.aiKeyConfigured && !keyDirty placeholder={
? 'API key (configured — leave blank to keep)' isEdit && initial?.keyConfigured ? '•••••••• (saved)' : 'sk-...'
: 'API key' }
} className={`${inputClass} pr-12`}
> />
<div className="relative"> <button
<input type="button"
type={showKey ? 'text' : 'password'} onClick={() => setShowKey(!showKey)}
value={apiKey} className="absolute right-3 top-2 text-xs text-zinc-500 hover:text-zinc-300"
onChange={(e) => { >
setApiKey(e.target.value); {showKey ? 'hide' : 'show'}
setKeyDirty(true); </button>
}}
placeholder={
cfg?.aiKeyConfigured && !keyDirty ? '••••••••' : 'sk-...'
}
className={`${inputClass} pr-12`}
/>
<button
type="button"
onClick={() => setShowKey(!showKey)}
className="absolute right-3 top-2 text-xs text-zinc-500 hover:text-zinc-300"
>
{showKey ? 'hide' : 'show'}
</button>
</div>
<p className="text-[11px] text-zinc-500 mt-1">
Stored plaintext in /data/app.db. Kept inside your StartOS
host; never sent anywhere except the provider you pick.
</p>
</Field>
)}
</>
)}
{error && (
<div className="rounded bg-red-900/50 px-3 py-2 border border-red-800 text-xs text-red-400">
{error}
</div> </div>
)} <p className="text-[11px] text-zinc-500 mt-1">
{success && ( Stored plaintext in /data/app.db on your StartOS host. Never sent
<div className="rounded bg-emerald-900/40 px-3 py-2 border border-emerald-800 text-xs text-emerald-300"> anywhere except the provider you pick.
Saved. </p>
</div> </Field>
)} )}
{!isEdit && (
<label className="flex items-center gap-2 text-xs text-zinc-300">
<input
type="checkbox"
checked={setActive}
onChange={(e) => setSetActive(e.target.checked)}
/>
Make this the active config
</label>
)}
{error && (
<div className="rounded bg-red-900/50 px-3 py-2 border border-red-800 text-xs text-red-400">
{error}
</div>
)}
{testResult && (
<div
className={`rounded px-3 py-2 border text-xs ${
testResult.ok
? 'bg-emerald-900/40 border-emerald-800 text-emerald-300'
: 'bg-red-900/50 border-red-800 text-red-400'
}`}
>
{testResult.ok ? (
<>
Connected in {(testResult.ms / 1000).toFixed(1)}s
{testResult.tokensIn != null &&
` · ${testResult.tokensIn} in / ${testResult.tokensOut ?? '?'} out`}
<div className="mt-0.5 text-zinc-400">
Sample reply: <span className="text-zinc-200">{testResult.sample}</span>
</div>
</>
) : (
<> {testResult.error}</>
)}
</div>
)}
<div className="flex items-center gap-2 pt-1">
<button <button
type="button" type="button"
onClick={handleSave} onClick={handleSave}
disabled={saving} disabled={saving || !provider || !model}
className="px-4 py-2 rounded bg-white text-black font-bold text-xs uppercase tracking-wider hover:bg-gray-100 disabled:bg-zinc-700 disabled:text-zinc-500" className="px-4 py-2 rounded bg-white text-black font-bold text-xs uppercase tracking-wider hover:bg-gray-100 disabled:bg-zinc-700 disabled:text-zinc-500"
> >
{saving ? ( {saving ? (
<> <>
<Loader2 className="inline w-4 h-4 animate-spin mr-2" /> <Loader2 className="inline w-4 h-4 animate-spin mr-2" />
Saving... Saving
</> </>
) : isEdit ? (
'Save changes'
) : ( ) : (
'Save AI config' 'Add this config'
)} )}
</button> </button>
<button
type="button"
onClick={handleTestDraft}
disabled={
testing ||
!provider ||
!model ||
(meta?.requiresUrl && !baseUrl) ||
(meta?.requiresKey && !apiKey && !(isEdit && initial?.keyConfigured))
}
className="px-4 py-2 rounded border border-zinc-700 text-zinc-300 hover:bg-zinc-800 text-xs uppercase tracking-wider disabled:opacity-50"
title="Send a tiny test prompt to verify these credentials"
>
{testing ? (
<>
<Loader2 className="inline w-3.5 h-3.5 animate-spin mr-2" />
Testing
</>
) : (
'Test draft'
)}
</button>
<button
type="button"
onClick={onCancel}
className="px-3 py-2 text-zinc-500 hover:text-zinc-200 text-xs uppercase tracking-wider"
>
Cancel
</button>
</div> </div>
</section> </div>
); );
} }
const inputClass = const inputClass =
'w-full px-3 py-2 text-sm rounded border border-zinc-700 bg-zinc-800 text-white placeholder:text-zinc-500 focus:outline-none focus:ring-2 focus:ring-white/30'; 'w-full px-3 py-2 text-sm rounded border border-zinc-700 bg-zinc-800 text-white placeholder:text-zinc-500 focus:outline-none focus:ring-2 focus:ring-white/30';
function Field({ label, children }: { label: string; children: React.ReactNode }) { function Field({
label,
children,
}: {
label: React.ReactNode;
children: React.ReactNode;
}) {
return ( return (
<label className="block"> <label className="block">
<span className="text-[11px] font-semibold text-zinc-400 uppercase tracking-wider block mb-1"> <span className="text-[11px] font-semibold text-zinc-400 uppercase tracking-wider block mb-1">
@@ -58,44 +58,61 @@ function ExerciseHistoryPopup({
}; };
}, [exerciseId]); }, [exerciseId]);
// Infinite scroll — observe a sentinel below the rendered list. The // v1.1.0:7 — Infinite scroll via a plain scroll listener on the
// root is the popup's scroll container (the popup itself), not the // popup itself. The previous IntersectionObserver implementation was
// viewport, since the user scrolls inside the popup. // unreliable inside an absolute-positioned scroll container (the
// popup is `position: absolute` + `overflow-y-auto`, which some
// browsers don't observe consistently when the root is the same
// element). A `scroll` event on the popup is rock-solid.
//
// Fires whenever the user scrolls within ~300px of the popup's
// bottom edge, mirroring the rootMargin used by the workouts-list
// infinite-scroll on the main page.
//
// Also runs once on first render after history loads — important
// because if the user has 100+ history entries and the first page
// doesn't fill the popup OR the user opens the popup and immediately
// sees content without scrolling, we still want to fetch ahead.
useEffect(() => { useEffect(() => {
if (loading || !hasMore || !sentinelRef.current || !popupRef.current) { if (loading || !hasMore || loadingMore || !popupRef.current) return;
return; const el = popupRef.current;
}
const sentinel = sentinelRef.current; const loadMore = async () => {
const root = popupRef.current; if (loadingMore || !hasMore) return;
const observer = new IntersectionObserver( setLoadingMore(true);
(entries) => { try {
if (!entries[0]?.isIntersecting) return; const res = await fetch(
if (loadingMore || !hasMore) return; `/api/exercises/${exerciseId}?offset=${history.length}&limit=${HISTORY_PAGE_SIZE}`,
setLoadingMore(true); );
(async () => { if (res.ok) {
try { const data = await res.json();
const res = await fetch( setHistory((prev) => [...prev, ...(data.history || [])]);
`/api/exercises/${exerciseId}?offset=${history.length}&limit=${HISTORY_PAGE_SIZE}`, setHasMore(!!data.hasMore);
); } else {
if (res.ok) { setError(`Failed to load more (${res.status})`);
const data = await res.json(); setHasMore(false);
setHistory((prev) => [...prev, ...(data.history || [])]); }
setHasMore(!!data.hasMore); } catch {
} else { setError("Failed to load more");
setError(`Failed to load more (${res.status})`); setHasMore(false);
setHasMore(false); }
} setLoadingMore(false);
} catch { };
setError("Failed to load more");
setHasMore(false); const maybeLoad = () => {
} const { scrollTop, scrollHeight, clientHeight } = el;
setLoadingMore(false); // 300px lookahead — match WorkoutsList's rootMargin behavior.
})(); if (scrollHeight - scrollTop - clientHeight < 300) {
}, loadMore();
{ root, rootMargin: "60px" }, }
); };
observer.observe(sentinel);
return () => observer.disconnect(); el.addEventListener("scroll", maybeLoad, { passive: true });
// Initial check: if the loaded page doesn't fill the popup, we
// still want to fetch the next page so the user doesn't have to
// scroll a near-empty container before more arrives.
maybeLoad();
return () => el.removeEventListener("scroll", maybeLoad);
}, [loading, hasMore, loadingMore, history.length, exerciseId]); }, [loading, hasMore, loadingMore, history.length, exerciseId]);
// Close on outside click // Close on outside click
@@ -112,7 +129,12 @@ function ExerciseHistoryPopup({
return ( return (
<div <div
ref={popupRef} ref={popupRef}
className="absolute left-0 right-0 top-full mt-1 z-50 bg-zinc-900 border border-zinc-700 rounded-lg shadow-xl max-h-80 overflow-y-auto" /* v1.1.0:6 — bump from max-h-80 (~320px, ~5 rows) to 70vh so
power users with multi-year history can scroll through ~15+
sessions without the popup feeling cramped. The
IntersectionObserver already loads more on demand; the old cap
just hid pages of data behind a tiny scrollbar. */
className="absolute left-0 right-0 top-full mt-1 z-50 bg-zinc-900 border border-zinc-700 rounded-lg shadow-xl max-h-[70vh] overflow-y-auto"
> >
<div className="flex items-center justify-between px-3 py-2 border-b border-zinc-800 sticky top-0 bg-zinc-900 z-10"> <div className="flex items-center justify-between px-3 py-2 border-b border-zinc-800 sticky top-0 bg-zinc-900 z-10">
<span className="text-xs font-semibold text-zinc-400 uppercase tracking-wider"> <span className="text-xs font-semibold text-zinc-400 uppercase tracking-wider">
@@ -162,10 +184,21 @@ function ExerciseHistoryPopup({
); );
})} })}
</div> </div>
{/* Sentinel + status row at the bottom of the list */} {/* Status row at the bottom of the list. The sentinel ref
is no longer the load trigger (we use a scroll listener
on the popup itself in v1.1.0:7), but the visual marker
still tells the user whether more is loading or done. */}
<div ref={sentinelRef} className="flex justify-center py-2"> <div ref={sentinelRef} className="flex justify-center py-2">
{loadingMore && ( {loadingMore && (
<Loader className="w-3.5 h-3.5 animate-spin text-zinc-500" /> <span className="inline-flex items-center gap-2 text-[10px] text-zinc-500 uppercase tracking-wider">
<Loader className="w-3.5 h-3.5 animate-spin" />
Loading more...
</span>
)}
{!loadingMore && hasMore && (
<span className="text-[10px] text-zinc-600 uppercase tracking-wider">
Scroll to load more
</span>
)} )}
{!loadingMore && !hasMore && history.length >= HISTORY_PAGE_SIZE && ( {!loadingMore && !hasMore && history.length >= HISTORY_PAGE_SIZE && (
<span className="text-[10px] text-zinc-600 uppercase tracking-wider"> <span className="text-[10px] text-zinc-600 uppercase tracking-wider">
+44
View File
@@ -0,0 +1,44 @@
import { prisma } from '@/lib/prisma';
/**
* Set a saved AIConfigProfile as the actor's active config + mirror its
* fields into the legacy UserPreferences columns so any code path that
* reads aiProvider/aiModel/aiBaseUrl/aiApiKey from prefs (api/ai/test,
* api/ai/generate's existing reads) keeps working without conditional
* logic.
*
* Lives outside the route file because Next.js App Router only allows
* HTTP method exports (GET / POST / etc.) from route.ts modules.
*/
export async function activate(
userId: string,
profileId: string,
fields: {
provider: string;
model: string;
baseUrl?: string | null;
apiKey?: string | null;
},
) {
await prisma.userPreferences.upsert({
where: { userId },
update: {
activeAIConfigId: profileId,
aiProvider: fields.provider,
aiModel: fields.model,
aiBaseUrl: fields.baseUrl || null,
aiApiKey: fields.apiKey || null,
},
create: {
userId,
theme: 'system',
defaultWeightUnit: 'lbs',
defaultRestSeconds: 90,
activeAIConfigId: profileId,
aiProvider: fields.provider,
aiModel: fields.model,
aiBaseUrl: fields.baseUrl || null,
aiApiKey: fields.apiKey || null,
},
});
}
+2
View File
@@ -122,6 +122,8 @@ export async function applyAIProgram(
repsMax: ex.repsMax ?? null, repsMax: ex.repsMax ?? null,
rpe: ex.rpe ?? null, rpe: ex.rpe ?? null,
restSeconds: ex.restSeconds ?? null, restSeconds: ex.restSeconds ?? null,
suggestedWeight: ex.suggestedWeight ?? null,
suggestedWeightUnit: ex.suggestedWeightUnit ?? null,
notes: ex.notes ?? null, notes: ex.notes ?? null,
})) as Prisma.ProgramExerciseCreateManyInput[], })) as Prisma.ProgramExerciseCreateManyInput[],
}); });
+289
View File
@@ -0,0 +1,289 @@
/**
* v1.1.0:4 — Background-friendly generation runner.
*
* Splits the work in two:
*
* 1. The HTTP route (api/ai/generate) calls `kickoffGeneration` to
* create the pending AIGeneration row, validate config, and start
* the model stream in the background. It returns immediately with
* the new row id; the runner continues even after the request is
* cancelled (because we use waitUntil-style pattern via a
* detached promise that owns its own AbortController).
*
* 2. The HTTP route also opens an SSE stream that subscribes to a
* per-generation in-memory event bus, so the live UI sees text
* deltas as they arrive — same UX as before. If the client
* navigates away the stream closes, but the runner keeps writing
* progress to the database; a poll endpoint returns whatever it
* has.
*
* The in-memory bus is a plain Map keyed by generation id. It only
* lives in this Node process; SSE clients only receive deltas from
* a runner started in the SAME process. That's fine because:
* - Single-process Next.js standalone (the StartOS deployment).
* - Cross-process resume goes through the database (poll endpoint
* reads `progressText`).
*
* Lifecycle:
* pending → runner created the row, model stream started
* completed → runner parsed the JSON successfully (parsedProgram set)
* failed → provider error or parse failure (errorMessage set)
* applied → user clicked Apply, Program created (handled in apply route)
*/
import type { PrismaClient } from '@prisma/client';
import { getProvider } from './providers';
import { parseAIProgram } from './programSchema';
export interface GenerationDelta {
type: 'text' | 'usage' | 'complete' | 'error';
/** For text */
delta?: string;
/** For usage / complete */
tokensIn?: number;
tokensOut?: number;
/** For complete */
parsedOk?: boolean;
errorMessage?: string;
durationMs?: number;
}
interface BusEntry {
/** Subscribers waiting for the next chunk. */
subscribers: Set<(d: GenerationDelta) => void>;
/** Buffered deltas for late-joining subscribers (so a poll-then-subscribe
* client doesn't miss the first few tokens). Bounded — we drop oldest
* if it grows past the limit. */
buffer: GenerationDelta[];
/** True once the runner emits its terminal `complete` chunk. */
finished: boolean;
}
const BUFFER_MAX = 5_000;
const bus = new Map<string, BusEntry>();
function ensureEntry(id: string): BusEntry {
let entry = bus.get(id);
if (!entry) {
entry = { subscribers: new Set(), buffer: [], finished: false };
bus.set(id, entry);
}
return entry;
}
function emit(id: string, d: GenerationDelta) {
const entry = ensureEntry(id);
entry.buffer.push(d);
if (entry.buffer.length > BUFFER_MAX) entry.buffer.shift();
for (const fn of entry.subscribers) {
try {
fn(d);
} catch {
/* subscriber teardown handles its own errors */
}
}
if (d.type === 'complete' || d.type === 'error') {
entry.finished = true;
// Schedule cleanup after a grace period so reconnecting clients can
// catch the tail. 60s is enough for a refresh round-trip.
setTimeout(() => bus.delete(id), 60_000).unref?.();
}
}
/**
* Subscribe to deltas for a generation. Returns an unsubscribe.
* `replay: true` first sends the entire buffer to the new subscriber
* (used by the SSE route — late-joining tabs get the full stream).
*/
export function subscribe(
id: string,
fn: (d: GenerationDelta) => void,
replay = true,
): () => void {
const entry = ensureEntry(id);
if (replay) for (const d of entry.buffer) fn(d);
if (entry.finished) {
// Already done — caller will see all buffered events; nothing more.
return () => {};
}
entry.subscribers.add(fn);
return () => entry.subscribers.delete(fn);
}
export interface KickoffOpts {
prisma: PrismaClient;
userId: string;
templateId: string | null;
templateName: string | null;
userInput: string;
systemPrompt: string;
userPrompt: string;
provider: string;
model: string;
apiKey: string | null;
baseUrl: string | null;
}
/**
* Create the AIGeneration row and start the model stream in the
* background. Returns the new row's id; the caller is expected to
* subscribe via `subscribe(id, fn)` for live deltas (or just rely
* on database polling).
*
* The runner outlives the originating request — it owns its own
* AbortController which is NOT linked to the request signal, so
* navigating away from the Generate page does NOT cancel it.
*/
export async function kickoffGeneration(opts: KickoffOpts): Promise<string> {
const generation = await opts.prisma.aIGeneration.create({
data: {
userId: opts.userId,
templateId: opts.templateId,
templateName: opts.templateName,
userInput: opts.userInput,
systemPrompt: opts.systemPrompt,
userPrompt: opts.userPrompt,
provider: opts.provider,
model: opts.model,
status: 'pending',
},
});
// Detach: we want this to keep going if the originating request is
// aborted. Standard Node + Next.js standalone behavior — the runner
// holds a strong reference via `bus` so it won't be GC'd mid-flight.
void runGeneration(generation.id, opts).catch((e) => {
// Last-resort safety net; the runner already logs/persists errors,
// but if even that throws we want to know.
console.error('[generation runner] uncaught:', e);
emit(generation.id, {
type: 'error',
errorMessage: `Runner crashed: ${(e as Error).message}`,
});
});
return generation.id;
}
/** How often we flush `progressText` to the database during streaming.
* Trade-off: too frequent = SQLite write churn; too slow = poll-only
* clients see big jumps. 750ms feels right — perceptibly live without
* hammering the WAL. */
const PROGRESS_FLUSH_MS = 750;
async function runGeneration(generationId: string, opts: KickoffOpts) {
const t0 = Date.now();
const provider = getProvider(opts.provider);
if (!provider) {
await opts.prisma.aIGeneration.update({
where: { id: generationId },
data: {
status: 'failed',
errorMessage: `Unknown provider: ${opts.provider}`,
durationMs: Date.now() - t0,
},
});
emit(generationId, {
type: 'error',
errorMessage: `Unknown provider: ${opts.provider}`,
});
return;
}
const ctrl = new AbortController();
let raw = '';
let tokensIn: number | undefined;
let tokensOut: number | undefined;
let providerError: string | null = null;
// Periodic progress flush.
let lastFlushAt = 0;
const maybeFlush = async (force = false) => {
const now = Date.now();
if (!force && now - lastFlushAt < PROGRESS_FLUSH_MS) return;
lastFlushAt = now;
try {
await opts.prisma.aIGeneration.update({
where: { id: generationId },
data: { progressText: raw },
});
} catch {
/* writes can fail under contention; we'll catch up next tick */
}
};
try {
for await (const chunk of provider.generate({
apiKey: opts.apiKey,
baseUrl: opts.baseUrl,
model: opts.model,
systemPrompt: opts.systemPrompt,
userPrompt: opts.userPrompt,
signal: ctrl.signal,
})) {
if (chunk.type === 'text') {
raw += chunk.delta;
emit(generationId, { type: 'text', delta: chunk.delta });
await maybeFlush();
} else if (chunk.type === 'usage') {
tokensIn = chunk.tokensIn;
tokensOut = chunk.tokensOut;
emit(generationId, {
type: 'usage',
tokensIn,
tokensOut,
});
} else if (chunk.type === 'error') {
providerError = chunk.message;
}
}
} catch (e) {
providerError = (e as Error).message;
}
// Final flush + parse.
await maybeFlush(true);
let parsedOk = false;
let parsedJson: string | null = null;
let parseErr: string | null = null;
if (!providerError && raw) {
const r = parseAIProgram(raw);
if (r.ok) {
parsedOk = true;
parsedJson = JSON.stringify(r.program);
} else {
parseErr = r.reason;
}
}
const status = providerError ? 'failed' : parsedOk ? 'completed' : 'failed';
const errorMessage =
providerError ?? (parsedOk ? null : parseErr ?? 'Empty response');
const durationMs = Date.now() - t0;
try {
await opts.prisma.aIGeneration.update({
where: { id: generationId },
data: {
rawResponse: raw || null,
parsedProgram: parsedJson,
tokensIn: tokensIn ?? null,
tokensOut: tokensOut ?? null,
durationMs,
status,
errorMessage,
},
});
} catch (e) {
console.error('[generation runner] final update failed:', e);
}
emit(generationId, {
type: 'complete',
parsedOk,
errorMessage: errorMessage ?? undefined,
tokensIn,
tokensOut,
durationMs,
});
}
+245
View File
@@ -0,0 +1,245 @@
import type { PrismaClient } from '@prisma/client';
/**
* Build a compact workout-history summary the AI can use as
* context for personalized program generation.
*
* We DELIBERATELY don't ship raw set logs — that would be tens of
* KB per request and burn tokens. Instead we compute per-exercise
* aggregates over a recent window (default 90 days):
*
* - totalSets in window
* - distinct workouts
* - daysSinceLast
* - lastWeight, lastReps (from the most-recent set)
* - bestWeight (heaviest set in window)
* - estimated 1RM (Epley formula on the heaviest weighted set)
* - rpe trend (avg RPE over recent sets, if logged)
* - stagnation flag (heaviest weight unchanged for 4+ weeks AND
* ≥3 sessions of that exercise in those 4+ weeks)
*
* Plus a top-level summary: total workouts, frequency, primary
* exercise types touched.
*
* The output is JSON-stringifiable, ~5-15 KB for a typical user.
*/
export interface HistoryExerciseSummary {
name: string;
type: string;
totalSets: number;
distinctWorkouts: number;
daysSinceLast: number;
lastWeight: number | null;
lastReps: number | null;
bestWeight: number | null;
estimated1RM: number | null;
avgRpe: number | null;
stagnant: boolean;
}
export interface HistorySummary {
windowDays: number;
totalWorkouts: number;
workoutsPerWeek: number;
primaryTypes: string[]; // exercise types by descending volume
exercises: HistoryExerciseSummary[];
}
/** Epley estimated 1RM: weight * (1 + reps / 30) */
function epley1RM(weight: number, reps: number): number {
return Math.round(weight * (1 + reps / 30));
}
export async function buildHistorySummary(
prisma: PrismaClient,
userId: string,
windowDays = 90,
): Promise<HistorySummary> {
const cutoff = new Date(Date.now() - windowDays * 86_400_000);
// Pull every set log in the window with its exercise + workout
// date. One query, one result-set walk.
const sets = await prisma.setLog.findMany({
where: {
workout: {
userId,
deletedAt: null,
date: { gte: cutoff },
},
},
select: {
reps: true,
weight: true,
rpe: true,
exerciseId: true,
workoutId: true,
workout: { select: { date: true } },
exercise: { select: { name: true, type: true } },
},
orderBy: { workout: { date: 'desc' } },
});
if (sets.length === 0) {
return {
windowDays,
totalWorkouts: 0,
workoutsPerWeek: 0,
primaryTypes: [],
exercises: [],
};
}
const workoutIds = new Set(sets.map((s) => s.workoutId));
const totalWorkouts = workoutIds.size;
const weeks = windowDays / 7;
const workoutsPerWeek = Math.round((totalWorkouts / weeks) * 10) / 10;
// Group by exercise
const byExercise = new Map<
string,
{
name: string;
type: string;
sets: typeof sets;
}
>();
for (const s of sets) {
if (!byExercise.has(s.exerciseId)) {
byExercise.set(s.exerciseId, {
name: s.exercise.name,
type: s.exercise.type,
sets: [],
});
}
byExercise.get(s.exerciseId)!.sets.push(s);
}
// Per-exercise summaries
const now = Date.now();
const exercises: HistoryExerciseSummary[] = [];
for (const [, group] of byExercise) {
const groupSets = group.sets;
const distinctWorkouts = new Set(groupSets.map((s) => s.workoutId)).size;
const mostRecent = groupSets[0]; // already date-desc
const daysSinceLast = Math.floor(
(now - mostRecent.workout.date.getTime()) / 86_400_000,
);
const weightedSets = groupSets.filter(
(s): s is typeof s & { weight: number; reps: number } =>
typeof s.weight === 'number' && typeof s.reps === 'number',
);
const bestWeightSet = weightedSets.reduce<
| { weight: number; reps: number }
| null
>((best, s) => {
if (!best || s.weight > best.weight) return s;
return best;
}, null);
const bestWeight = bestWeightSet?.weight ?? null;
const estimated1RM =
bestWeightSet != null ? epley1RM(bestWeightSet.weight, bestWeightSet.reps) : null;
const rpeSets = groupSets.filter(
(s): s is typeof s & { rpe: number } => typeof s.rpe === 'number',
);
const avgRpe =
rpeSets.length > 0
? Math.round(
(rpeSets.reduce((sum, s) => sum + s.rpe, 0) / rpeSets.length) * 10,
) / 10
: null;
// Stagnation: best weight in oldest half == best weight in newest half
// AND ≥3 distinct sessions in the window.
let stagnant = false;
if (distinctWorkouts >= 3 && bestWeight != null && weightedSets.length >= 4) {
const sortedByDate = [...weightedSets].sort(
(a, b) => a.workout.date.getTime() - b.workout.date.getTime(),
);
const mid = Math.floor(sortedByDate.length / 2);
const oldHalfBest = Math.max(...sortedByDate.slice(0, mid).map((s) => s.weight));
const newHalfBest = Math.max(...sortedByDate.slice(mid).map((s) => s.weight));
// No improvement in the new half compared to the old half
if (newHalfBest <= oldHalfBest) stagnant = true;
}
exercises.push({
name: group.name,
type: group.type,
totalSets: groupSets.length,
distinctWorkouts,
daysSinceLast,
lastWeight: mostRecent.weight ?? null,
lastReps: mostRecent.reps ?? null,
bestWeight,
estimated1RM,
avgRpe,
stagnant,
});
}
// Sort exercises by total volume (sets) descending so the most
// important context is first if the model truncates.
exercises.sort((a, b) => b.totalSets - a.totalSets);
// Primary types by aggregate sets
const typeVolume = new Map<string, number>();
for (const ex of exercises) {
typeVolume.set(ex.type, (typeVolume.get(ex.type) ?? 0) + ex.totalSets);
}
const primaryTypes = Array.from(typeVolume.entries())
.sort((a, b) => b[1] - a[1])
.map(([t]) => t);
return {
windowDays,
totalWorkouts,
workoutsPerWeek,
primaryTypes,
exercises,
};
}
/**
* Format a HistorySummary as a compact string the LLM can actually
* use. Aims for <2KB of text even for heavy users.
*/
export function formatHistoryContext(summary: HistorySummary): string {
if (summary.totalWorkouts === 0) {
return `\nUSER HISTORY: no workouts logged in the last ${summary.windowDays} days.`;
}
const lines: string[] = [];
lines.push(
`\nUSER HISTORY (last ${summary.windowDays} days):`,
` ${summary.totalWorkouts} workouts (~${summary.workoutsPerWeek}/week)`,
` Primary work: ${summary.primaryTypes.slice(0, 4).join(', ')}`,
'',
` Per-exercise activity (descending by volume; weights in user's logged unit):`,
);
// Cap at top 30 exercises
const top = summary.exercises.slice(0, 30);
for (const ex of top) {
const bits: string[] = [
`${ex.totalSets}s/${ex.distinctWorkouts}w`,
`${ex.daysSinceLast}d ago`,
];
if (ex.bestWeight != null && ex.lastReps != null)
bits.push(`best ${ex.bestWeight}×${ex.lastReps}`);
if (ex.estimated1RM != null) bits.push(`~${ex.estimated1RM} 1RM`);
if (ex.avgRpe != null) bits.push(`avg RPE ${ex.avgRpe}`);
if (ex.stagnant) bits.push('STAGNANT');
lines.push(` - ${ex.name} (${ex.type}): ${bits.join(' · ')}`);
}
if (summary.exercises.length > top.length) {
lines.push(
` ...and ${summary.exercises.length - top.length} more exercises with lower volume`,
);
}
lines.push(
'',
` When designing the program, weight recent activity heavily. Address STAGNANT exercises if relevant. Don't propose deload-week-heavy work for someone training infrequently, and don't propose 6-day splits for someone averaging <3 sessions/week.`,
);
return lines.join('\n');
}
+116
View File
@@ -0,0 +1,116 @@
/**
* Lenient JSON parser for incremental rendering of in-flight LLM
* output.
*
* The model emits JSON one token at a time. Strict JSON.parse fails
* until the very last `}` arrives. lenientJsonParse instead:
*
* 1. Locates the first `{` (after stripping ```json fences).
* 2. Walks the buffer tracking quote state + an open-bracket
* stack so we know what to close in what order.
* 3. Closes any open string with `"`.
* 4. Trims a partial trailing keyword (true/false/null prefix),
* trailing comma, and dangling key:value pair where value is
* missing.
* 5. Closes open structures in reverse-of-opening order (so
* `[{` closes as `}]`, not `]}`).
* 6. JSON.parse the result; return null if it still fails.
*
* The returned object is a best-effort snapshot of the program so
* far. The Generate UI uses it to render a live preview as the
* model writes; once the stream ends, the FULL response is parsed
* with the strict parser via parseAIProgram for the final render.
*
* This is intentionally simple — partial numbers (e.g. `-2.`) and
* partial escape sequences just return null until the next chunk
* makes them well-formed.
*/
export function lenientJsonParse(raw: string): unknown | null {
if (!raw) return null;
// Strip ```json fences (or plain ``` fences). Tolerates an
// unclosed trailing fence (still streaming).
let s = raw;
const fenced = s.match(/```(?:json)?\s*([\s\S]*?)(?:\s*```|$)/);
if (fenced) s = fenced[1];
// Locate first `{`.
const startIdx = s.indexOf('{');
if (startIdx < 0) return null;
s = s.slice(startIdx);
// Quick path: maybe it's already valid (rare during streaming,
// common after the stream completes).
try {
return JSON.parse(s);
} catch {
// fall through
}
// Walk the buffer tracking the open-bracket stack. We don't try
// to recover from mismatched closers (would be model malformity);
// we just don't pop more than we have.
const stack: Array<'{' | '['> = [];
let inStr = false;
let escape = false;
for (let i = 0; i < s.length; i++) {
const c = s[i];
if (escape) {
escape = false;
continue;
}
if (c === '\\') {
escape = true;
continue;
}
if (c === '"') {
inStr = !inStr;
continue;
}
if (inStr) continue;
if (c === '{') stack.push('{');
else if (c === '}') {
if (stack[stack.length - 1] === '{') stack.pop();
} else if (c === '[') stack.push('[');
else if (c === ']') {
if (stack[stack.length - 1] === '[') stack.pop();
}
}
let candidate = s;
// Close any open string at the tail.
if (inStr) candidate += '"';
// Trim trailing whitespace.
candidate = candidate.replace(/\s+$/, '');
// Drop a partial trailing keyword (`true`/`false`/`null` prefix)
// sitting after a `:`, `,`, or `[`.
candidate = candidate.replace(
/([:,[])\s*(?:t|tr|tru|f|fa|fal|fals|n|nu|nul)$/,
'$1',
);
// Drop a trailing comma (no value follows yet).
candidate = candidate.replace(/,\s*$/, '');
// Drop a dangling key + colon (value not started yet).
candidate = candidate.replace(/"[^"\\]*(?:\\.[^"\\]*)*"\s*:\s*$/, '');
// Drop another trailing comma that may now be exposed.
candidate = candidate.replace(/,\s*$/, '');
// Close stack in reverse-of-opening order. `[{` becomes `}]` not
// `]}` — that's the bug a depth-counter approach would have.
while (stack.length > 0) {
const top = stack.pop()!;
candidate += top === '{' ? '}' : ']';
}
try {
return JSON.parse(candidate);
} catch {
return null;
}
}
+173
View File
@@ -0,0 +1,173 @@
/**
* Per-model pricing in USD per million tokens. Used to estimate the
* cost of an AIGeneration row from its tokensIn/tokensOut.
*
* Prices change. This table is a best-effort starting point for
* common models as of mid-2026; users on other models will see
* `null` cost (we still surface token counts). Updating: edit this
* file and ship — no schema change needed.
*
* Matching strategy: case-insensitive prefix lookup against the
* user's configured model string. Model names like
* "claude-sonnet-4-5-20251022" match the "claude-sonnet-4-5" prefix.
*
* Keys are organized by provider for readability but the lookup is
* provider-agnostic — the model string is the key.
*/
interface PriceEntry {
inputPerM: number; // USD per 1M input tokens
outputPerM: number; // USD per 1M output tokens
}
const PRICES: Record<string, PriceEntry> = {
// Anthropic Claude (Messages API) — opus tier $15/$75, sonnet $3/$15,
// haiku $0.80/$4. New point releases inherit their tier's pricing.
'claude-opus-4-7': { inputPerM: 15, outputPerM: 75 },
'claude-opus-4-6': { inputPerM: 15, outputPerM: 75 },
'claude-opus-4-5': { inputPerM: 15, outputPerM: 75 },
'claude-opus-4': { inputPerM: 15, outputPerM: 75 },
'claude-sonnet-4-6': { inputPerM: 3, outputPerM: 15 },
'claude-sonnet-4-5': { inputPerM: 3, outputPerM: 15 },
'claude-sonnet-4': { inputPerM: 3, outputPerM: 15 },
'claude-haiku-4-5': { inputPerM: 0.8, outputPerM: 4 },
'claude-haiku-4': { inputPerM: 0.8, outputPerM: 4 },
'claude-3-7-sonnet': { inputPerM: 3, outputPerM: 15 },
'claude-3-5-sonnet': { inputPerM: 3, outputPerM: 15 },
'claude-3-5-haiku': { inputPerM: 0.8, outputPerM: 4 },
// OpenAI — gpt-5.x flagships ~$1.25-$2/$10-$15, mini/nano cheaper
'gpt-5.5': { inputPerM: 2, outputPerM: 15 },
'gpt-5.4': { inputPerM: 1.5, outputPerM: 12 },
'gpt-5.4-mini': { inputPerM: 0.3, outputPerM: 2.4 },
'gpt-5.4-nano': { inputPerM: 0.06, outputPerM: 0.5 },
'gpt-5.3': { inputPerM: 1.5, outputPerM: 12 },
'gpt-5.2': { inputPerM: 1.5, outputPerM: 12 },
'gpt-5.1': { inputPerM: 1.25, outputPerM: 10 },
'gpt-5': { inputPerM: 1.25, outputPerM: 10 },
'gpt-5-mini': { inputPerM: 0.25, outputPerM: 2 },
'gpt-5-nano': { inputPerM: 0.05, outputPerM: 0.4 },
'gpt-4o': { inputPerM: 2.5, outputPerM: 10 },
'gpt-4o-mini': { inputPerM: 0.15, outputPerM: 0.6 },
'o1': { inputPerM: 15, outputPerM: 60 },
'o3': { inputPerM: 2, outputPerM: 8 },
'o3-mini': { inputPerM: 1.1, outputPerM: 4.4 },
'o4-mini': { inputPerM: 1.1, outputPerM: 4.4 },
// Google Gemini — Gemini 3.1 Pro is $2/$12 standard; >200K ctx is 2x.
// Gemini 3 Flash is $0.50/$3. 3.1 Flash-Lite is the cheapest of the
// 3.x line. Both short names (gemini-3.1-pro) and long preview names
// (gemini-3.1-pro-preview) are accepted by the API and listed here.
'gemini-3.1-pro-preview': { inputPerM: 2, outputPerM: 12 },
'gemini-3.1-pro': { inputPerM: 2, outputPerM: 12 },
'gemini-3.1-flash-lite': { inputPerM: 0.1, outputPerM: 0.4 },
'gemini-3.1-flash': { inputPerM: 0.5, outputPerM: 3 },
'gemini-3-pro-preview': { inputPerM: 2, outputPerM: 12 },
'gemini-3-pro': { inputPerM: 2, outputPerM: 12 },
'gemini-3-flash-preview': { inputPerM: 0.5, outputPerM: 3 },
'gemini-3-flash': { inputPerM: 0.5, outputPerM: 3 },
'gemini-2.5-pro': { inputPerM: 1.25, outputPerM: 10 },
'gemini-2.5-flash': { inputPerM: 0.3, outputPerM: 2.5 },
'gemini-2.0-flash': { inputPerM: 0.1, outputPerM: 0.4 },
'gemini-2.0-pro': { inputPerM: 1.25, outputPerM: 5 },
'gemini-1.5-pro': { inputPerM: 1.25, outputPerM: 5 },
'gemini-1.5-flash': { inputPerM: 0.075, outputPerM: 0.3 },
};
/**
* Per-provider model menus — source of truth for the "Model" dropdown
* in Settings → AI integration. `recommended` floats to the top. Users
* can still type a custom model name (the dropdown has an "Other"
* option that switches to free-text input). Order = display order.
*
* Update these when new models ship. Keys correspond to provider IDs
* in lib/ai/providers/index.ts.
*/
export interface ModelOption {
/** Exact API model identifier */
id: string;
/** Human-readable label shown in the dropdown */
label: string;
/** Floats to the top + gets a "★" mark */
recommended?: boolean;
}
export const MODEL_MENU: Record<string, ModelOption[]> = {
claude: [
{ id: 'claude-opus-4-7', label: 'Claude Opus 4.7 (most capable)', recommended: true },
{ id: 'claude-sonnet-4-6', label: 'Claude Sonnet 4.6 (1M context, fast)', recommended: true },
{ id: 'claude-haiku-4-5', label: 'Claude Haiku 4.5 (cheapest, fastest)', recommended: true },
{ id: 'claude-opus-4-6', label: 'Claude Opus 4.6' },
{ id: 'claude-sonnet-4-5', label: 'Claude Sonnet 4.5' },
{ id: 'claude-3-7-sonnet-latest', label: 'Claude 3.7 Sonnet' },
],
openai: [
{ id: 'gpt-5.5', label: 'GPT-5.5 (most capable)', recommended: true },
{ id: 'gpt-5.4', label: 'GPT-5.4', recommended: true },
{ id: 'gpt-5.4-mini', label: 'GPT-5.4 Mini (cheap, fast)', recommended: true },
{ id: 'gpt-5.4-nano', label: 'GPT-5.4 Nano (cheapest)' },
{ id: 'gpt-5', label: 'GPT-5' },
{ id: 'gpt-4o', label: 'GPT-4o (legacy)' },
{ id: 'o3', label: 'o3 (reasoning)' },
],
gemini: [
// Names match what Google's AI Studio dropdown shows. Both short
// (gemini-3.1-pro) and long preview names work via the API; we
// ship the short forms because that's what the Studio UI uses.
{ id: 'gemini-3.1-pro', label: 'Gemini 3.1 Pro (most capable)', recommended: true },
{ id: 'gemini-3.1-flash', label: 'Gemini 3.1 Flash (fast, cheap)', recommended: true },
{ id: 'gemini-3.1-flash-lite', label: 'Gemini 3.1 Flash Lite (cheapest)', recommended: true },
{ id: 'gemini-3-pro', label: 'Gemini 3 Pro' },
{ id: 'gemini-3-flash', label: 'Gemini 3 Flash' },
{ id: 'gemini-2.5-pro', label: 'Gemini 2.5 Pro' },
{ id: 'gemini-2.5-flash', label: 'Gemini 2.5 Flash' },
{ id: 'gemini-2.0-flash', label: 'Gemini 2.0 Flash (legacy)' },
],
// openai-compatible + ollama: no curated menu — model names are
// gateway- or host-specific. Ollama auto-detects via /api/tags.
'openai-compatible': [],
ollama: [],
};
/** Find the price entry whose key is a (case-insensitive) prefix of the model string. */
export function findPrice(model: string): PriceEntry | null {
const m = model.toLowerCase();
// Longest-prefix-first so e.g. "claude-sonnet-4-5" beats "claude-sonnet-4".
const sortedKeys = Object.keys(PRICES).sort((a, b) => b.length - a.length);
for (const key of sortedKeys) {
if (m.startsWith(key.toLowerCase())) return PRICES[key];
}
return null;
}
/**
* Estimate the USD cost of a generation. Returns null if the model
* isn't in the price table or if either token count is missing.
* Ollama and openai-compatible custom gateways always return null
* (they're either free or self-priced).
*/
export function estimateCost(opts: {
provider: string;
model: string;
tokensIn: number | null;
tokensOut: number | null;
}): number | null {
if (opts.provider === 'ollama') return 0; // self-hosted, no per-token cost
if (opts.provider === 'openai-compatible') return null; // we don't know the gateway's pricing
if (opts.tokensIn == null || opts.tokensOut == null) return null;
const price = findPrice(opts.model);
if (!price) return null;
return (
(opts.tokensIn / 1_000_000) * price.inputPerM +
(opts.tokensOut / 1_000_000) * price.outputPerM
);
}
/** Format USD to a string suitable for a UI label. Below $0.01 -> "<$0.01". */
export function formatCost(usd: number | null): string {
if (usd == null) return '—';
if (usd === 0) return 'free';
if (usd < 0.01) return '<$0.01';
if (usd < 1) return `$${usd.toFixed(3)}`;
return `$${usd.toFixed(2)}`;
}
+13 -3
View File
@@ -22,6 +22,14 @@ export const aiExerciseSchema = z.object({
repsMax: z.number().int().positive().optional().nullable(), repsMax: z.number().int().positive().optional().nullable(),
rpe: z.number().int().min(1).max(10).optional().nullable(), rpe: z.number().int().min(1).max(10).optional().nullable(),
restSeconds: z.number().int().nonnegative().optional().nullable(), restSeconds: z.number().int().nonnegative().optional().nullable(),
/// Suggested starting weight. Not required (cardio, bodyweight,
/// stretching all leave it null). When provided alongside an
/// exerciseId that the user starts a workout from, this seeds the
/// SetLog.weight as a target.
suggestedWeight: z.number().nonnegative().optional().nullable(),
/// "lbs" | "kg". Optional; the apply step falls back to the user's
/// `defaultWeightUnit` preference when null.
suggestedWeightUnit: z.enum(['lbs', 'kg']).optional().nullable(),
notes: z.string().optional().nullable(), notes: z.string().optional().nullable(),
}); });
@@ -76,14 +84,16 @@ export const PROGRAM_OUTPUT_SHAPE = `{
"description": "<string, optional>", "description": "<string, optional>",
"exercises": [ "exercises": [
{ {
"exerciseId": "<string from the library list, or null if you need an exercise the user doesn't have>", "exerciseId": "<string — REQUIRED — must be an id from the LIBRARY block. If no library exercise fits, pick the closest match and explain in notes; do NOT invent ids.>",
"exerciseName": "<string, the canonical name>", "exerciseName": "<string, the canonical name from the library>",
"order": <int >= 0>, "order": <int >= 0>,
"sets": <int, optional>, "sets": <int, optional but recommended>,
"repsMin": <int, optional>, "repsMin": <int, optional>,
"repsMax": <int, optional>, "repsMax": <int, optional>,
"rpe": <int 1-10, optional>, "rpe": <int 1-10, optional>,
"restSeconds": <int >= 0, optional>, "restSeconds": <int >= 0, optional>,
"suggestedWeight": <number, optional — starting weight; omit/null for cardio, bodyweight, stretching>,
"suggestedWeightUnit": "<\\"lbs\\" | \\"kg\\", optional — defaults to user's preferred unit>",
"notes": "<string, optional, coaching note>" "notes": "<string, optional, coaching note>"
} }
] ]
+1 -1
View File
@@ -34,7 +34,7 @@ export const claude: LLMProvider = {
}, },
body: JSON.stringify({ body: JSON.stringify({
model: opts.model, model: opts.model,
max_tokens: 8000, max_tokens: opts.maxOutputTokens ?? 8000,
stream: true, stream: true,
system: opts.systemPrompt, system: opts.systemPrompt,
messages: [{ role: 'user', content: opts.userPrompt }], messages: [{ role: 'user', content: opts.userPrompt }],
+44 -3
View File
@@ -35,7 +35,7 @@ export const gemini: LLMProvider = {
], ],
generationConfig: { generationConfig: {
temperature: 0.7, temperature: 0.7,
maxOutputTokens: 8000, maxOutputTokens: opts.maxOutputTokens ?? 8000,
}, },
}), }),
signal: opts.signal, signal: opts.signal,
@@ -56,6 +56,8 @@ export const gemini: LLMProvider = {
} }
let tokensIn: number | undefined; let tokensIn: number | undefined;
let tokensOut: number | undefined; let tokensOut: number | undefined;
let textEmitted = false;
let lastFinishReason: string | null = null;
try { try {
// Gemini SSE: same line-delimited "data: ..." frames. // Gemini SSE: same line-delimited "data: ..." frames.
const { sseLines } = await import('../sse'); const { sseLines } = await import('../sse');
@@ -66,17 +68,37 @@ export const gemini: LLMProvider = {
} catch { } catch {
continue; continue;
} }
const parts = evt.candidates?.[0]?.content?.parts; const cand = evt.candidates?.[0];
const parts = cand?.content?.parts;
if (Array.isArray(parts)) { if (Array.isArray(parts)) {
for (const p of parts) { for (const p of parts) {
if (p.text) yield { type: 'text', delta: p.text }; if (p.text) {
yield { type: 'text', delta: p.text };
textEmitted = true;
}
} }
} }
if (cand?.finishReason) {
lastFinishReason = cand.finishReason;
}
if (evt.usageMetadata) { if (evt.usageMetadata) {
tokensIn = evt.usageMetadata.promptTokenCount; tokensIn = evt.usageMetadata.promptTokenCount;
tokensOut = evt.usageMetadata.candidatesTokenCount; tokensOut = evt.usageMetadata.candidatesTokenCount;
} }
} }
// Surface a useful error when Gemini returned 200 OK but emitted
// no text — most often a safety/recitation block, or a thinking
// model that exhausted maxOutputTokens on internal reasoning. The
// test endpoint relies on this to give the user a real message
// instead of a generic "empty response".
if (
!textEmitted &&
lastFinishReason &&
lastFinishReason !== 'STOP'
) {
const friendly = describeFinishReason(lastFinishReason);
yield { type: 'error', message: `Gemini blocked the response: ${friendly}` };
}
yield { type: 'usage', tokensIn, tokensOut }; yield { type: 'usage', tokensIn, tokensOut };
yield { type: 'done' }; yield { type: 'done' };
} catch (e) { } catch (e) {
@@ -87,3 +109,22 @@ export const gemini: LLMProvider = {
} }
}, },
}; };
function describeFinishReason(reason: string): string {
switch (reason) {
case 'SAFETY':
return 'safety filter (try a flagship model or rephrase the prompt)';
case 'RECITATION':
return 'recitation filter';
case 'MAX_TOKENS':
return 'hit the output token limit before finishing — raise maxOutputTokens or use a non-thinking model';
case 'BLOCKLIST':
return 'blocklist match';
case 'PROHIBITED_CONTENT':
return 'prohibited-content filter';
case 'SPII':
return 'sensitive-PII filter';
default:
return reason;
}
}
+3
View File
@@ -34,6 +34,9 @@ export async function* generateOpenAIStyle(
model: opts.model, model: opts.model,
stream: true, stream: true,
stream_options: { include_usage: true }, stream_options: { include_usage: true },
...(opts.maxOutputTokens != null
? { max_completion_tokens: opts.maxOutputTokens }
: {}),
messages: [ messages: [
{ role: 'system', content: opts.systemPrompt }, { role: 'system', content: opts.systemPrompt },
{ role: 'user', content: opts.userPrompt }, { role: 'user', content: opts.userPrompt },
+77
View File
@@ -0,0 +1,77 @@
/**
* Base system-prompt rules prepended to every template's prompt before
* sending to the model. Centralized here so we can tighten output
* constraints in one place rather than editing every template.
*
* Two main jobs:
* 1. Force the JSON output shape (no prose, no fences, picks library
* ids only — fixes "exerciseId doesn't belong to this user" errors)
* 2. Force a suggested starting weight per resistance exercise
* (the model otherwise tends to leave it null, which leaves the
* user with no concrete target on day 1)
*
* Templates supply their *coaching philosophy* (hypertrophy = volume +
* progressive overload, conditioning = aerobic base etc); this module
* supplies the *structural contract*.
*/
export interface BaseSystemPromptOpts {
/** "lbs" | "kg" — the user's preferred weight unit, used as the default
* suggestedWeightUnit when the model omits one. */
weightUnit: 'lbs' | 'kg';
/** Whether the user's workout history is being included. Toggles a
* short instruction on how to use it. */
hasHistoryContext: boolean;
/** True when the model is local (Ollama). Local models tend to need
* shorter, blunter rules and benefit from explicit examples. */
isLocalModel: boolean;
}
export function buildBaseSystemPrompt(opts: BaseSystemPromptOpts): string {
const lines: string[] = [];
lines.push(
'# OUTPUT CONTRACT (mandatory)',
'',
'1. Reply with EXACTLY ONE JSON object. No prose before or after. No ```json fences.',
'2. Every exercise must use an `exerciseId` from the LIBRARY block at the bottom of this prompt. NEVER invent ids. If nothing in the library matches, pick the closest fit and explain the substitution in `notes`.',
`3. Every resistance exercise MUST have a \`suggestedWeight\` (a number, in ${opts.weightUnit}). Cardio, stretching, and bodyweight exercises set it to null.`,
`4. \`suggestedWeightUnit\` should be "${opts.weightUnit}" unless the exercise is conventionally tracked in the other unit (e.g. kettlebells often kg). Omit for non-loaded exercises.`,
'5. Every exercise needs `sets` and either `repsMin` (with `repsMax` if a range) or a duration note.',
'6. Use `rpe` (1-10) for working sets to communicate intensity; warmups can be lower or omitted.',
'7. `restSeconds` is required for compound lifts; optional for accessories.',
'8. Keep day volumes realistic: 4-7 exercises, 60-75 minutes total. Include warm-up sets only if they belong in the program (don\'t list mobility separately unless the user asked).',
'9. The `notes` field is for coaching cues, tempo, technique reminders — keep it short, one sentence.',
);
if (opts.hasHistoryContext) {
lines.push(
'',
'# USING THE HISTORY BLOCK',
'',
'The HISTORY block below summarizes the user\'s last 90 days. Use it to:',
'- Pick `suggestedWeight` near their current working weights, NOT round numbers from nowhere.',
'- Address any STAGNANT lifts: deload, change rep ranges, swap variations, or work at a different RPE.',
'- Respect their training frequency (don\'t prescribe 5x/week if they\'ve been training 3x).',
'- Stay in their movement vocabulary unless they asked for variety.',
);
} else {
lines.push(
'',
'# WEIGHT GUIDANCE WITHOUT HISTORY',
'',
`Without prior performance data, set conservative \`suggestedWeight\` values: 50-65% of typical 1RM for the lift at the user's stated experience level. Use round increments common in commercial gyms (5${opts.weightUnit} jumps; 2.5${opts.weightUnit} for small accessories). Always add a coaching note like "adjust to leave 2-3 reps in reserve" so the user knows it's a starting estimate.`,
);
}
if (opts.isLocalModel) {
lines.push(
'',
'# LOCAL MODEL REMINDER',
'',
'You are running locally with limited reasoning. Stick to the simplest valid program that matches the request. Do not overthink. JSON only.',
);
}
return lines.join('\n');
}
+8
View File
@@ -38,6 +38,14 @@ export interface GenerateOpts {
userPrompt: string; userPrompt: string;
/** AbortSignal for cancellation; the implementation must respect it. */ /** AbortSignal for cancellation; the implementation must respect it. */
signal?: AbortSignal; signal?: AbortSignal;
/**
* v1.1.0:4: explicit max output token budget. Providers honor this
* differently — used to make small "test connection" calls survive
* thinking models (Gemini 2.5+, OpenAI o-series) that may spend
* their default budget on internal reasoning before emitting visible
* text. Default per-provider when omitted.
*/
maxOutputTokens?: number;
} }
export type GenerateChunk = export type GenerateChunk =
+46
View File
@@ -30,6 +30,7 @@ model User {
aiSuggestions AISuggestion[] aiSuggestions AISuggestion[]
aiPromptTemplates AIPromptTemplate[] aiPromptTemplates AIPromptTemplate[]
aiGenerations AIGeneration[] aiGenerations AIGeneration[]
aiConfigProfiles AIConfigProfile[]
userPreferences UserPreferences? userPreferences UserPreferences?
@@index([email]) @@index([email])
@@ -203,6 +204,12 @@ model ProgramExercise {
rpe Int? rpe Int?
restSeconds Int? restSeconds Int?
notes String? notes String?
/// v1.1.0:4 — AI-suggested starting weight (or coach-prescribed
/// for manual programs). When you "Start workout from program day"
/// this pre-populates SetLog.weight so the user has a target. Null
/// = no suggestion, fall back to whatever they did last time.
suggestedWeight Float?
suggestedWeightUnit String? // "lbs" | "kg"; null = use user pref
createdAt DateTime @default(now()) createdAt DateTime @default(now())
// Relations // Relations
@@ -326,6 +333,11 @@ model UserPreferences {
aiModel String? aiModel String?
aiBaseUrl String? aiBaseUrl String?
aiApiKey String? aiApiKey String?
// ─── v1.1.0:4 multi-config: which AIConfigProfile is active ───
// Null = fall back to the legacy single-config columns above (which
// we keep populated as a mirror of the active profile for backwards-
// compat with any code path that still reads them).
activeAIConfigId String?
createdAt DateTime @default(now()) createdAt DateTime @default(now())
updatedAt DateTime @updatedAt updatedAt DateTime @updatedAt
@@ -335,6 +347,32 @@ model UserPreferences {
@@index([userId]) @@index([userId])
} }
/// v1.1.0:4 — A single saved AI provider configuration. Users can
/// have many (one per provider, or several of the same provider with
/// different models/keys) and toggle one as active. The active one is
/// what /api/ai/generate and /api/ai/test use.
///
/// We mirror the active profile back into UserPreferences.aiProvider/
/// aiModel/aiBaseUrl/aiApiKey on every "set active" so any old code
/// path that reads from prefs keeps working without conditional logic.
model AIConfigProfile {
id String @id @default(cuid())
userId String
/// User-chosen label, e.g. "Local Ollama", "Claude Sonnet (work)".
/// Defaults to a generated name on create if not provided.
name String
provider String // 'claude' | 'openai' | 'openai-compatible' | 'gemini' | 'ollama'
model String
baseUrl String? // for openai-compatible + ollama
apiKey String? // plaintext, same threat model as the rest of /data
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
@@index([userId])
}
/// User-defined or shipped prompt templates for AI program generation. /// User-defined or shipped prompt templates for AI program generation.
/// `userId = null` means the template ships with the package (built-in, /// `userId = null` means the template ships with the package (built-in,
/// reconciled per-boot from prisma/aiTemplates.seed.json). `userId = /// reconciled per-boot from prisma/aiTemplates.seed.json). `userId =
@@ -382,12 +420,20 @@ model AIGeneration {
userInput String userInput String
systemPrompt String systemPrompt String
userPrompt String userPrompt String
/// Streamed-so-far text. Updated periodically by the background
/// generator so navigating-away clients can resume display via
/// polling. Final value matches `rawResponse` once status flips
/// to 'completed' or 'failed'.
progressText String?
rawResponse String? rawResponse String?
parsedProgram String? // JSON.stringify of the parsed structure parsedProgram String? // JSON.stringify of the parsed structure
provider String provider String
model String model String
tokensIn Int? tokensIn Int?
tokensOut Int? tokensOut Int?
/// Wall-clock duration in milliseconds from request start to final
/// status flip. Useful for the "this took 10 minutes" stat in the UI.
durationMs Int?
status String @default("pending") status String @default("pending")
errorMessage String? errorMessage String?
appliedProgramId String? appliedProgramId String?
@@ -0,0 +1,78 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
/**
* Tests for the in-memory bus inside lib/ai/generationRunner.ts.
*
* The runner itself touches the database + provider implementations,
* which we don't want to spin up here. The interesting logic worth
* testing is the pub/sub:
* - late-joining subscribers replay the buffered chunks
* - terminal events (complete/error) flip `finished` and stop accepting
* new subscribers
* - bounded buffer (we don't accumulate forever on a chatty model)
*
* To exercise it without spinning up the runner we directly drive the
* bus through a non-exported `emit` ... but it isn't exported, so we
* instead hit it through the (also not exported) bus map. Vitest
* lets us re-import the module's internals via dynamic import + module
* cache reset so we can assert on the public `subscribe` contract by
* spying on the subscriber callback under controlled emit ordering.
*/
// We test the public API; the internals (`bus`, `emit`) aren't reachable
// without monkey-patching, so the strategy is: import + call subscribe,
// and observe what the subscriber receives. We synthesize the writer-side
// by calling the runner's internal flush via... actually the cleanest way
// is to require the module and exploit Node's CJS interop to grab the
// non-exported module-internal map. Instead of fragile reflection, we
// just rebuild a tiny mirror of the bus shape locally and assert the
// contract documented in the module header.
describe('generationRunner module surface', () => {
beforeEach(() => {
vi.resetModules();
});
it('exports kickoffGeneration + subscribe', async () => {
const mod = await import('@/lib/ai/generationRunner');
expect(typeof mod.kickoffGeneration).toBe('function');
expect(typeof mod.subscribe).toBe('function');
});
it('subscribe to an unknown id returns a no-op unsubscribe (no throw)', async () => {
const { subscribe } = await import('@/lib/ai/generationRunner');
const unsub = subscribe('nonexistent-id', () => {});
expect(typeof unsub).toBe('function');
expect(() => unsub()).not.toThrow();
});
it('replay=false on a fresh entry receives no events from buffer', async () => {
const { subscribe } = await import('@/lib/ai/generationRunner');
const seen: unknown[] = [];
const unsub = subscribe('fresh-id', (d) => seen.push(d), false);
expect(seen).toEqual([]);
unsub();
});
});
/**
* Smoke test the contract Generate UI relies on: an EventSource attaches
* AFTER the first text chunk has streamed, and we still receive that
* earlier chunk because `subscribe(id, fn, replay=true)` (the default)
* walks the buffer first.
*
* We can't exercise the real runner without provider mocking — that's
* covered indirectly by the SSE attach route's behavior (see
* tests/routes-ai-templates.test.ts pattern). Here we assert the simple
* fact that `subscribe`'s signature has the replay default.
*/
describe('generationRunner.subscribe replay defaulting', () => {
it('replay defaults to true (third arg optional)', async () => {
const { subscribe } = await import('@/lib/ai/generationRunner');
// No throw on omitted third arg.
expect(() => {
const unsub = subscribe('id', () => {});
unsub();
}).not.toThrow();
});
});
@@ -0,0 +1,224 @@
import { describe, it, expect, beforeEach } from 'vitest';
import { prisma } from '@/lib/prisma';
import {
buildHistorySummary,
formatHistoryContext,
} from '@/lib/ai/historyContext';
beforeEach(async () => {
await prisma.session.deleteMany();
await prisma.setLog.deleteMany();
await prisma.workout.deleteMany();
await prisma.programExercise.deleteMany();
await prisma.programDay.deleteMany();
await prisma.programWeek.deleteMany();
await prisma.program.deleteMany();
await prisma.exercise.deleteMany();
await prisma.aIGeneration.deleteMany();
await prisma.aIPromptTemplate.deleteMany();
await prisma.user.deleteMany();
await prisma.instanceSettings.deleteMany();
});
async function setup() {
const user = await prisma.user.create({
data: { email: 'a@x', passwordHash: 'fake' },
});
const bench = await prisma.exercise.create({
data: {
userId: user.id,
name: 'Bench Press',
type: 'barbell',
muscleGroups: '[]',
},
});
const squat = await prisma.exercise.create({
data: {
userId: user.id,
name: 'Squat',
type: 'barbell',
muscleGroups: '[]',
},
});
return { user, bench, squat };
}
async function logWorkout(
userId: string,
daysAgo: number,
sets: Array<{ exerciseId: string; reps: number; weight: number; rpe?: number }>,
) {
const date = new Date(Date.now() - daysAgo * 86_400_000);
return prisma.workout.create({
data: {
userId,
date,
setLogs: {
create: sets.map((s, i) => ({
exerciseId: s.exerciseId,
setNumber: i + 1,
reps: s.reps,
weight: s.weight,
rpe: s.rpe,
})),
},
},
});
}
describe('buildHistorySummary', () => {
it('returns empty summary for a user with no workouts', async () => {
const user = await prisma.user.create({
data: { email: 'a@x', passwordHash: 'fake' },
});
const s = await buildHistorySummary(prisma, user.id);
expect(s.totalWorkouts).toBe(0);
expect(s.exercises).toEqual([]);
});
it('summarizes a single user\'s recent activity', async () => {
const { user, bench, squat } = await setup();
// 3 bench sessions, 2 squat sessions in last 30 days
await logWorkout(user.id, 1, [
{ exerciseId: bench.id, reps: 5, weight: 225 },
{ exerciseId: bench.id, reps: 5, weight: 225 },
]);
await logWorkout(user.id, 4, [
{ exerciseId: bench.id, reps: 5, weight: 235 },
{ exerciseId: bench.id, reps: 5, weight: 235 },
]);
await logWorkout(user.id, 7, [
{ exerciseId: bench.id, reps: 5, weight: 215 },
{ exerciseId: squat.id, reps: 5, weight: 315 },
]);
await logWorkout(user.id, 14, [
{ exerciseId: squat.id, reps: 5, weight: 305 },
]);
const s = await buildHistorySummary(prisma, user.id);
expect(s.totalWorkouts).toBe(4);
expect(s.workoutsPerWeek).toBeGreaterThan(0);
expect(s.exercises).toHaveLength(2);
const benchSummary = s.exercises.find((e) => e.name === 'Bench Press');
expect(benchSummary).toBeTruthy();
expect(benchSummary!.totalSets).toBe(5);
expect(benchSummary!.distinctWorkouts).toBe(3);
expect(benchSummary!.bestWeight).toBe(235);
expect(benchSummary!.daysSinceLast).toBeLessThanOrEqual(2); // logged 1 day ago
// Epley(235, 5) = 235 * (1 + 5/30) = 274.17 → 274
expect(benchSummary!.estimated1RM).toBe(274);
});
it('flags stagnation on a stuck exercise', async () => {
const { user, bench } = await setup();
// 6 sessions all at the same weight
for (let d = 0; d < 6; d++) {
await logWorkout(user.id, d * 5, [
{ exerciseId: bench.id, reps: 5, weight: 225 },
{ exerciseId: bench.id, reps: 5, weight: 225 },
]);
}
const s = await buildHistorySummary(prisma, user.id);
const bs = s.exercises.find((e) => e.name === 'Bench Press');
expect(bs?.stagnant).toBe(true);
});
it('does NOT flag stagnation on a progressing exercise', async () => {
const { user, bench } = await setup();
// 6 sessions with progressive weight
for (let d = 0; d < 6; d++) {
await logWorkout(user.id, (5 - d) * 7, [
{ exerciseId: bench.id, reps: 5, weight: 200 + d * 10 },
]);
}
const s = await buildHistorySummary(prisma, user.id);
const bs = s.exercises.find((e) => e.name === 'Bench Press');
expect(bs?.stagnant).toBe(false);
});
it('excludes workouts outside the window', async () => {
const { user, bench } = await setup();
await logWorkout(user.id, 5, [{ exerciseId: bench.id, reps: 5, weight: 225 }]);
await logWorkout(user.id, 200, [{ exerciseId: bench.id, reps: 5, weight: 200 }]);
const s = await buildHistorySummary(prisma, user.id, 90);
expect(s.totalWorkouts).toBe(1);
expect(s.exercises[0].totalSets).toBe(1);
});
it('excludes soft-deleted workouts', async () => {
const { user, bench } = await setup();
const w = await logWorkout(user.id, 3, [
{ exerciseId: bench.id, reps: 5, weight: 225 },
]);
await prisma.workout.update({
where: { id: w.id },
data: { deletedAt: new Date() },
});
const s = await buildHistorySummary(prisma, user.id);
expect(s.totalWorkouts).toBe(0);
});
it('isolates per-user data (does not bleed across users)', async () => {
const { user, bench } = await setup();
const otherUser = await prisma.user.create({
data: { email: 'b@x', passwordHash: 'fake' },
});
const otherBench = await prisma.exercise.create({
data: {
userId: otherUser.id,
name: 'Bench Press',
type: 'barbell',
muscleGroups: '[]',
},
});
await logWorkout(user.id, 1, [{ exerciseId: bench.id, reps: 5, weight: 225 }]);
await logWorkout(otherUser.id, 1, [
{ exerciseId: otherBench.id, reps: 100, weight: 999 },
]);
const s = await buildHistorySummary(prisma, user.id);
expect(s.totalWorkouts).toBe(1);
expect(s.exercises[0].bestWeight).toBe(225); // not 999
});
});
describe('formatHistoryContext', () => {
it('emits a friendly message on empty history', () => {
const out = formatHistoryContext({
windowDays: 90,
totalWorkouts: 0,
workoutsPerWeek: 0,
primaryTypes: [],
exercises: [],
});
expect(out).toMatch(/no workouts/);
});
it('formats a populated summary into a compact block', () => {
const out = formatHistoryContext({
windowDays: 90,
totalWorkouts: 30,
workoutsPerWeek: 3.3,
primaryTypes: ['barbell', 'dumbbell', 'cable'],
exercises: [
{
name: 'Bench Press',
type: 'barbell',
totalSets: 36,
distinctWorkouts: 12,
daysSinceLast: 2,
lastWeight: 235,
lastReps: 5,
bestWeight: 245,
estimated1RM: 286,
avgRpe: 8.5,
stagnant: false,
},
],
});
expect(out).toMatch(/30 workouts/);
expect(out).toMatch(/Bench Press/);
expect(out).toMatch(/STAGNANT|RPE/);
});
});
@@ -0,0 +1,88 @@
import { describe, it, expect } from 'vitest';
import { lenientJsonParse } from '@/lib/ai/lenientJson';
describe('lenientJsonParse', () => {
it('returns null on empty input', () => {
expect(lenientJsonParse('')).toBeNull();
});
it('returns null when no { is present', () => {
expect(lenientJsonParse('hello world')).toBeNull();
});
it('returns the object when input is already valid', () => {
expect(lenientJsonParse('{"a":1,"b":[2,3]}')).toEqual({ a: 1, b: [2, 3] });
});
it('strips ```json fences', () => {
expect(lenientJsonParse('```json\n{"a":1}\n```')).toEqual({ a: 1 });
});
it('handles fences not yet closed (still streaming)', () => {
expect(lenientJsonParse('```json\n{"a":1, "b":2')).toEqual({ a: 1, b: 2 });
});
it('finds the first { after preamble', () => {
expect(lenientJsonParse('Here you go:\n{"name":"x"}')).toEqual({
name: 'x',
});
});
it('auto-closes a partial object missing its closing }', () => {
const got = lenientJsonParse('{"name":"X","durationWeeks":4');
expect(got).toEqual({ name: 'X', durationWeeks: 4 });
});
it('auto-closes a partial array missing its closing ]', () => {
const got = lenientJsonParse('{"weeks":[1,2,3');
expect(got).toEqual({ weeks: [1, 2, 3] });
});
it('drops a dangling property key with no value yet', () => {
const got = lenientJsonParse('{"name":"X","notes":');
expect(got).toEqual({ name: 'X' });
});
it('drops a trailing comma after a complete value', () => {
const got = lenientJsonParse('{"a":1,"b":2,');
expect(got).toEqual({ a: 1, b: 2 });
});
it('handles a partial nested structure typical of AI program output', () => {
const partial = `{
"name": "Test",
"type": "hypertrophy",
"durationWeeks": 4,
"weeks": [
{
"weekNumber": 1,
"days": [
{
"dayOfWeek": 1,
"name": "Push",
"exercises": [
{"exerciseId": "abc", "exerciseName": "Bench", "order": 0, "sets": 4
`;
const got = lenientJsonParse(partial) as Record<string, any>;
expect(got).toBeTruthy();
expect(got.name).toBe('Test');
expect(Array.isArray(got.weeks)).toBe(true);
expect(got.weeks[0].weekNumber).toBe(1);
// The dangling exercise object may or may not be present
// depending on truncation; what matters is the parser didn't
// throw.
});
it('handles an open string at the end', () => {
const got = lenientJsonParse('{"description":"A long descrip');
expect(got).toBeTruthy();
expect((got as Record<string, string>).description).toMatch(
/^A long descrip/,
);
});
it('returns null for unrecoverable garbage', () => {
// Mismatched closing brace before any opening is unrecoverable
expect(lenientJsonParse('}}}')).toBeNull();
});
});
+65
View File
@@ -0,0 +1,65 @@
import { describe, it, expect } from 'vitest';
import { MODEL_MENU, findPrice } from '@/lib/ai/pricing';
/**
* The Settings → AI integration model dropdown is sourced from
* MODEL_MENU. These tests guard the invariants:
*
* - Every menu model id is something findPrice() recognizes (so the
* cost estimator won't show "—" for any model the user picks from
* the dropdown).
* - At least one "recommended" entry per major provider — without it
* the UI has nothing to highlight.
* - Ollama + openai-compatible menus are intentionally empty (those
* providers are gateway-/host-specific).
* - At least one Gemini 3.x entry (regression-guard against the
* user's "I tried gemini-3.0-pro and got 404" report).
*/
describe('MODEL_MENU', () => {
it('every menu model id matches a price entry', () => {
for (const [provider, models] of Object.entries(MODEL_MENU)) {
for (const m of models) {
const price = findPrice(m.id);
expect(
price,
`${provider}/${m.id} has no price entry — add it to PRICES in pricing.ts`,
).not.toBeNull();
}
}
});
it('major providers have at least one recommended model', () => {
for (const provider of ['claude', 'openai', 'gemini'] as const) {
const recs = MODEL_MENU[provider]?.filter((m) => m.recommended) ?? [];
expect(
recs.length,
`${provider} has no recommended model — UI has nothing to star`,
).toBeGreaterThan(0);
}
});
it('ollama + openai-compatible menus are empty (model is host-specific)', () => {
expect(MODEL_MENU.ollama).toEqual([]);
expect(MODEL_MENU['openai-compatible']).toEqual([]);
});
it('Gemini menu includes a 3.x model (regression: gemini-3.0-pro 404)', () => {
const ids = MODEL_MENU.gemini.map((m) => m.id);
const has3x = ids.some((id) => /gemini-3/i.test(id));
expect(has3x, `gemini menu lacks any 3.x model: ${ids.join(', ')}`).toBe(
true,
);
});
it('Claude menu includes a Sonnet 4.6 or newer (1M context)', () => {
const ids = MODEL_MENU.claude.map((m) => m.id);
const hasModern = ids.some((id) =>
/claude-(opus-4-7|sonnet-4-6|opus-4-6)/i.test(id),
);
expect(
hasModern,
`claude menu missing 4.6+ tier: ${ids.join(', ')}`,
).toBe(true);
});
});
+116
View File
@@ -0,0 +1,116 @@
import { describe, it, expect } from 'vitest';
import { findPrice, estimateCost, formatCost } from '@/lib/ai/pricing';
describe('findPrice', () => {
it('matches a known model exactly', () => {
expect(findPrice('claude-sonnet-4-5')).toBeTruthy();
});
it('matches a known model with a date suffix (longest-prefix)', () => {
const p = findPrice('claude-sonnet-4-5-20251022');
expect(p?.inputPerM).toBe(3);
expect(p?.outputPerM).toBe(15);
});
it('is case-insensitive', () => {
expect(findPrice('GPT-5-Mini')).toBeTruthy();
});
it('returns null for unknown models', () => {
expect(findPrice('mistral-medium-9000')).toBeNull();
});
it('prefers longer-prefix when multiple keys match', () => {
// claude-sonnet-4-5 is more specific than claude-sonnet-4
const p = findPrice('claude-sonnet-4-5');
expect(p).toEqual({ inputPerM: 3, outputPerM: 15 });
});
});
describe('estimateCost', () => {
it('returns 0 for ollama (self-hosted)', () => {
expect(
estimateCost({
provider: 'ollama',
model: 'llama3.1:8b',
tokensIn: 1000,
tokensOut: 500,
}),
).toBe(0);
});
it('returns null for openai-compatible (unknown gateway pricing)', () => {
expect(
estimateCost({
provider: 'openai-compatible',
model: 'meta-llama/llama-3.1-8b-instruct',
tokensIn: 1000,
tokensOut: 500,
}),
).toBeNull();
});
it('returns null when the model isn\'t in the price table', () => {
expect(
estimateCost({
provider: 'claude',
model: 'claude-vintage-edition',
tokensIn: 1000,
tokensOut: 500,
}),
).toBeNull();
});
it('returns null when token counts are missing', () => {
expect(
estimateCost({
provider: 'claude',
model: 'claude-sonnet-4-5',
tokensIn: null,
tokensOut: 500,
}),
).toBeNull();
});
it('computes the right $ for a known model', () => {
// claude-sonnet-4-5: $3/M in, $15/M out
// 100K in + 50K out = 0.1*3 + 0.05*15 = 0.3 + 0.75 = 1.05
const cost = estimateCost({
provider: 'claude',
model: 'claude-sonnet-4-5',
tokensIn: 100_000,
tokensOut: 50_000,
});
expect(cost).toBeCloseTo(1.05, 5);
});
it('computes correctly for gpt-5-nano (very cheap)', () => {
// gpt-5-nano: $0.05/M in, $0.4/M out
// 1000 in + 500 out = 0.001*0.05 + 0.0005*0.4 = 0.00005 + 0.0002 = 0.00025
const cost = estimateCost({
provider: 'openai',
model: 'gpt-5-nano',
tokensIn: 1000,
tokensOut: 500,
});
expect(cost).toBeCloseTo(0.00025, 8);
});
});
describe('formatCost', () => {
it('formats null as em dash', () => {
expect(formatCost(null)).toBe('—');
});
it('formats 0 as "free"', () => {
expect(formatCost(0)).toBe('free');
});
it('formats sub-cent costs as "<$0.01"', () => {
expect(formatCost(0.0023)).toBe('<$0.01');
});
it('formats sub-dollar costs with 3 decimal places', () => {
expect(formatCost(0.123)).toBe('$0.123');
});
it('formats dollar+ costs with 2 decimal places', () => {
expect(formatCost(2.567)).toBe('$2.57');
});
});
@@ -0,0 +1,95 @@
import { describe, it, expect } from 'vitest';
import { buildBaseSystemPrompt } from '@/lib/ai/systemPromptBase';
/**
* The base system prompt is the structural contract every template
* inherits. These tests pin the *invariants* that must always hold:
* - JSON-only output rule
* - "use library exerciseIds" rule (fixes the bug where the model
* invented ids and apply blew up)
* - "suggested weight is required" rule
* - The conditional history-vs-no-history block toggles correctly
* - The local-model nudge appears for Ollama
*
* Wording can shift over time; these assertions check substrings, not
* exact matches, so coaching tone changes don't break tests.
*/
describe('buildBaseSystemPrompt', () => {
it('always demands JSON-only output (no fences)', () => {
const p = buildBaseSystemPrompt({
weightUnit: 'lbs',
hasHistoryContext: false,
isLocalModel: false,
});
expect(p).toMatch(/JSON object/i);
expect(p).toMatch(/no.+fences/i);
});
it('forces use of library exerciseIds', () => {
const p = buildBaseSystemPrompt({
weightUnit: 'lbs',
hasHistoryContext: false,
isLocalModel: false,
});
expect(p).toMatch(/exerciseId/);
expect(p).toMatch(/library/i);
expect(p).toMatch(/never invent ids/i);
});
it('requires suggestedWeight in the users preferred unit', () => {
const lbsPrompt = buildBaseSystemPrompt({
weightUnit: 'lbs',
hasHistoryContext: false,
isLocalModel: false,
});
expect(lbsPrompt).toMatch(/suggestedWeight/);
expect(lbsPrompt).toMatch(/lbs/);
const kgPrompt = buildBaseSystemPrompt({
weightUnit: 'kg',
hasHistoryContext: false,
isLocalModel: false,
});
expect(kgPrompt).toMatch(/kg/);
});
it('switches to "use the history block" instructions when history is present', () => {
const withHistory = buildBaseSystemPrompt({
weightUnit: 'lbs',
hasHistoryContext: true,
isLocalModel: false,
});
expect(withHistory).toMatch(/HISTORY block/);
expect(withHistory).toMatch(/STAGNANT/);
});
it('switches to conservative-defaults instructions when no history', () => {
const noHistory = buildBaseSystemPrompt({
weightUnit: 'lbs',
hasHistoryContext: false,
isLocalModel: false,
});
expect(noHistory).toMatch(/WEIGHT GUIDANCE WITHOUT HISTORY/);
expect(noHistory).toMatch(/50-65%/);
});
it('adds a "local model" reminder for Ollama', () => {
const local = buildBaseSystemPrompt({
weightUnit: 'lbs',
hasHistoryContext: false,
isLocalModel: true,
});
expect(local).toMatch(/LOCAL MODEL/);
expect(local).toMatch(/JSON only/i);
});
it('omits the local-model reminder for cloud providers', () => {
const cloud = buildBaseSystemPrompt({
weightUnit: 'lbs',
hasHistoryContext: true,
isLocalModel: false,
});
expect(cloud).not.toMatch(/LOCAL MODEL REMINDER/);
});
});
+76
View File
@@ -172,6 +172,82 @@ if command -v sqlite3 >/dev/null 2>&1 && [ -f "$DB_PATH" ]; then
" "
fi fi
# v1.1.0:4 added AIConfigProfile table (multi-config support) +
# UserPreferences.activeAIConfigId pointer + AIGeneration progress/
# duration columns + ProgramExercise suggested-weight columns.
if ! sqlite3 "$DB_PATH" \
"SELECT name FROM sqlite_master WHERE type='table' AND name='AIConfigProfile';" \
2>/dev/null | grep -q AIConfigProfile; then
log "creating AIConfigProfile table"
sqlite3 "$DB_PATH" "
CREATE TABLE AIConfigProfile (
id TEXT PRIMARY KEY,
userId TEXT NOT NULL,
name TEXT NOT NULL,
provider TEXT NOT NULL,
model TEXT NOT NULL,
baseUrl TEXT,
apiKey TEXT,
createdAt DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updatedAt DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (userId) REFERENCES User(id) ON DELETE CASCADE
);
CREATE INDEX AIConfigProfile_userId_idx ON AIConfigProfile(userId);
"
fi
if ! sqlite3 "$DB_PATH" "PRAGMA table_info('UserPreferences');" 2>/dev/null | grep -q "|activeAIConfigId|"; then
log "adding UserPreferences.activeAIConfigId"
sqlite3 "$DB_PATH" "ALTER TABLE UserPreferences ADD COLUMN activeAIConfigId TEXT;"
fi
if ! sqlite3 "$DB_PATH" "PRAGMA table_info('AIGeneration');" 2>/dev/null | grep -q "|progressText|"; then
log "adding AIGeneration.progressText"
sqlite3 "$DB_PATH" "ALTER TABLE AIGeneration ADD COLUMN progressText TEXT;"
fi
if ! sqlite3 "$DB_PATH" "PRAGMA table_info('AIGeneration');" 2>/dev/null | grep -q "|durationMs|"; then
log "adding AIGeneration.durationMs"
sqlite3 "$DB_PATH" "ALTER TABLE AIGeneration ADD COLUMN durationMs INTEGER;"
fi
if ! sqlite3 "$DB_PATH" "PRAGMA table_info('ProgramExercise');" 2>/dev/null | grep -q "|suggestedWeight|"; then
log "adding ProgramExercise.suggestedWeight + suggestedWeightUnit"
sqlite3 "$DB_PATH" "ALTER TABLE ProgramExercise ADD COLUMN suggestedWeight REAL;"
sqlite3 "$DB_PATH" "ALTER TABLE ProgramExercise ADD COLUMN suggestedWeightUnit TEXT;"
fi
# v1.1.0:4 one-shot migration: lift each user's legacy single-config
# (UserPreferences.aiProvider/aiModel/...) into a new AIConfigProfile
# row marked active. Idempotent — only runs for users who have a
# configured legacy config but no profiles yet.
log "migrating any legacy single-config to AIConfigProfile (idempotent)"
sqlite3 "$DB_PATH" "
INSERT INTO AIConfigProfile (id, userId, name, provider, model, baseUrl, apiKey)
SELECT
'c' || lower(hex(randomblob(12))),
up.userId,
'Default (' || up.aiProvider || ')',
up.aiProvider,
up.aiModel,
up.aiBaseUrl,
up.aiApiKey
FROM UserPreferences up
WHERE up.aiProvider IS NOT NULL
AND up.aiModel IS NOT NULL
AND NOT EXISTS (SELECT 1 FROM AIConfigProfile p WHERE p.userId = up.userId);
" 2>/dev/null || log "WARN: legacy-config migration skipped"
# Set activeAIConfigId for users who now have exactly one profile.
sqlite3 "$DB_PATH" "
UPDATE UserPreferences
SET activeAIConfigId = (
SELECT id FROM AIConfigProfile WHERE userId = UserPreferences.userId LIMIT 1
)
WHERE activeAIConfigId IS NULL
AND (SELECT COUNT(*) FROM AIConfigProfile WHERE userId = UserPreferences.userId) = 1;
" 2>/dev/null || true
if ! sqlite3 "$DB_PATH" \ if ! sqlite3 "$DB_PATH" \
"SELECT name FROM sqlite_master WHERE type='table' AND name='InstanceSettings';" \ "SELECT name FROM sqlite_master WHERE type='table' AND name='InstanceSettings';" \
2>/dev/null | grep -q InstanceSettings; then 2>/dev/null | grep -q InstanceSettings; then
+29 -1
View File
@@ -8,6 +8,11 @@ import { v_1_0_0_6 } from './v1.0.0.6'
import { v_1_0_0_7 } from './v1.0.0.7' import { v_1_0_0_7 } from './v1.0.0.7'
import { v_1_1_0_1 } from './v1.1.0.1' import { v_1_1_0_1 } from './v1.1.0.1'
import { v_1_1_0_2 } from './v1.1.0.2' import { v_1_1_0_2 } from './v1.1.0.2'
import { v_1_1_0_3 } from './v1.1.0.3'
import { v_1_1_0_4 } from './v1.1.0.4'
import { v_1_1_0_5 } from './v1.1.0.5'
import { v_1_1_0_6 } from './v1.1.0.6'
import { v_1_1_0_7 } from './v1.1.0.7'
/** /**
* Version graph for the `proof-of-work` package. * Version graph for the `proof-of-work` package.
@@ -25,9 +30,27 @@ import { v_1_1_0_2 } from './v1.1.0.2'
* v1.1.0:1 — Programs UI (manual create / save / follow). * v1.1.0:1 — Programs UI (manual create / save / follow).
* v1.1.0:2 — AI program generation, 5 providers (Claude / OpenAI / * v1.1.0:2 — AI program generation, 5 providers (Claude / OpenAI /
* OpenAI-compatible / Gemini / Ollama). * OpenAI-compatible / Gemini / Ollama).
* v1.1.0:3 — AI upgrades: history-as-context, test connection,
* cost estimator, streaming preview render.
* v1.1.0:4 — AI integration overhaul: multi-config persistence,
* background generation (survives navigation), Ollama
* auto-detect + installed-model dropdown, curated model
* dropdowns for Claude / OpenAI / Gemini with current
* 2026 model names, system-prompt overhaul forcing library
* exerciseIds + suggested weights, sidebar sub-navigation,
* history detail view.
* v1.1.0:5 — Gemini menu correctness: adds gemini-3.1-pro (short),
* gemini-3.1-flash, gemini-3.1-flash-lite, gemini-3-pro,
* gemini-3-flash + pricing entries.
* v1.1.0:6 — Exercise-history popup max-height bumped from ~320px
* (5 rows) to 70vh (~15+ rows). Users with deep history
* can scroll without fighting a tiny inner scrollbar.
* v1.1.0:7 — Exercise-history popup auto-loads more rows on scroll
* (switched from a flaky IntersectionObserver-in-popup to
* a plain scroll listener with 300px lookahead).
*/ */
export const versionGraph = VersionGraph.of({ export const versionGraph = VersionGraph.of({
current: v_1_1_0_2, current: v_1_1_0_7,
other: [ other: [
v_1_0_0_1, v_1_0_0_1,
v_1_0_0_2, v_1_0_0_2,
@@ -37,5 +60,10 @@ export const versionGraph = VersionGraph.of({
v_1_0_0_6, v_1_0_0_6,
v_1_0_0_7, v_1_0_0_7,
v_1_1_0_1, v_1_1_0_1,
v_1_1_0_2,
v_1_1_0_3,
v_1_1_0_4,
v_1_1_0_5,
v_1_1_0_6,
], ],
}) })
+61
View File
@@ -0,0 +1,61 @@
import { IMPOSSIBLE, VersionInfo } from '@start9labs/start-sdk'
/**
* v1.1.0:3 — AI: workout-history context, test connection,
* cost estimator, streaming preview render.
*
* History context (the killer feature)
* - lib/ai/historyContext.ts builds a compact 90-day rollup of
* the user's training: per-exercise frequency, recent weights,
* estimated 1RMs (Epley), avg RPE, days-since-last, plus a
* STAGNANT flag when the heaviest weight in the new half of
* the window doesn't beat the old half.
* - Generate page has an "Include my workout history as context"
* checkbox (default on if you have ≥10 logged workouts). When
* checked, the summary is appended to the system prompt so the
* model can recommend things like "you've stalled bench at 245
* for 6 weeks — try paused reps."
* - The summary is text, ~1-3 KB even for heavy users. We
* deliberately don't ship raw set logs (privacy + token cost).
*
* Test connection
* - POST /api/ai/test sends a tiny "say hi in 3 words" prompt
* against the user's configured provider and reports
* latency + first sample of the response, or the error inline.
* - "Test connection" button next to "Save AI config" in
* Settings → AI integration. Lets you verify the provider/
* model/key/baseUrl combo without going through full program
* generation.
*
* Cost estimator
* - lib/ai/pricing.ts ships a price table for the major models
* (Claude Sonnet/Opus/Haiku 4-5, GPT-5/4o/o3, Gemini 2.5/2.0).
* Ollama always returns 0 (self-hosted, no per-token cost).
* openai-compatible returns null (we don't know the gateway's
* pricing).
* - Generation history shows per-row cost + a 30-day rolling
* total at the top of the page.
*
* Streaming preview render
* - lib/ai/lenientJson.ts: a stack-aware partial-JSON parser
* that auto-closes open strings/brackets/braces in
* reverse-of-opening order, drops dangling key:value pairs
* and partial keywords. Returns a best-effort snapshot of
* the program-so-far on each chunk.
* - Generate UI now renders a live "Building program..." panel
* that updates as weeks/days/exercises arrive, instead of
* just showing raw text and waiting for stream end.
*
* No schema changes. /data is untouched.
*/
export const v_1_1_0_3 = VersionInfo.of({
version: '1.1.0:3',
releaseNotes: {
en_US:
'AI program generation gets four upgrades: (1) include your last 90 days of workout history as context — the model designs around your actual frequency, current weights, and stagnations; (2) "Test connection" button in Settings to verify provider/model/key without a full generation; (3) per-generation USD cost + 30-day rolling total in the history page (Ollama is free, openai-compatible gateways are unknown); (4) streaming preview renders the program tree as the model writes it instead of waiting for the full response. No data migration.',
},
migrations: {
up: async () => {},
down: IMPOSSIBLE,
},
})
+109
View File
@@ -0,0 +1,109 @@
import { IMPOSSIBLE, VersionInfo } from '@start9labs/start-sdk'
/**
* v1.1.0:4 — multi-config AI integration, background generation,
* ollama auto-detect, system-prompt overhaul, history
* detail view, sidebar sub-nav.
*
* Driven by post-v1.1.0:3 user feedback. The biggest themes:
*
* 1) Multi-config persistence
* - You can save N AI configs (one per provider, or several of the
* same provider with different models/keys), toggle one as the
* "active" config, and per-config "Test connection" to verify
* before activating. Switching providers no longer means losing
* the previous setup.
* - New schema: AIConfigProfile table (per-user). UserPreferences
* grows `activeAIConfigId`; the legacy single-config columns are
* kept and mirrored from the active profile so any old code path
* that reads from prefs continues to work.
* - On boot, any user who already had a single-config setup gets
* that config lifted into a default AIConfigProfile + activated.
* Idempotent.
*
* 2) Ollama auto-detect
* - The "Add AI config" form probes /api/tags on the StartOS
* internal addresses (ollama.startos:11434, ollama.embassy:11434).
* If reachable, the URL field auto-fills and the model field
* becomes a dropdown of installed models. No more memorizing
* "the right URL" or pasting a model tag.
*
* 3) Model dropdowns for the leading providers
* - Settings now offers a curated dropdown of recommended models
* for Claude (Opus 4.7, Sonnet 4.6, Haiku 4.5), OpenAI (GPT-5.5,
* 5.4, 5.4-mini, 5.4-nano), and Gemini (3.1-pro-preview, 2.5-pro,
* 2.5-flash). "Other (type your own)" stays available for power
* users on niche models. Fixes the "I tried gemini-3.0-pro and
* got 404" footgun.
*
* 4) Background generation
* - Generation now runs server-side via a detached runner. Closing
* the page or navigating away no longer kills it — the row keeps
* filling in. The Generate UI surfaces a banner explaining this.
* - The new History detail page polls progress + renders the
* partial JSON live; reload-during-streaming "just works." Useful
* for slow local Ollama runs.
* - New AIGeneration columns: progressText (in-flight stream),
* durationMs (final wall-clock).
*
* 5) System prompt overhaul
* - lib/ai/systemPromptBase.ts: a structural contract prepended to
* every template. Forces JSON-only output (no markdown fences),
* forces use of library exerciseIds (no more "exerciseId doesn't
* belong to this user" on apply), and forces a suggestedWeight
* per resistance exercise — both with-history (relative to user's
* lifts) and without-history (% of typical 1RM) variants.
* - aiExerciseSchema gains suggestedWeight + suggestedWeightUnit.
* ProgramExercise gains the same columns. Starting a workout from
* a ProgramDay now pre-populates SetLog.weight from the suggestion
* so users have a target on day 1 instead of a blank field.
*
* 6) Test connection improvements
* - Latency reported in seconds (was ms — confusing for slow Ollama).
* - Stale "✓ Connected" no longer lingers when you change the form.
* - Gemini surfaces finishReason when the response is empty (e.g.
* "blocked by safety filter") instead of the generic "empty
* response — check the model name."
* - Test ping uses generous maxOutputTokens so thinking models
* (Gemini 2.5/3.x, OpenAI o-series) actually emit text after
* reasoning instead of running out of budget.
* - Per-config Test button (no need to activate first).
*
* 7) History detail view
* - Click any AIGeneration row → full read-only program tree, plus
* the exact prompts sent. Apply from here without re-generating.
* - In-flight rows poll for live progress.
*
* 8) Sidebar sub-navigation
* - "AI" expands to Generate / History / Templates.
* - "Settings" expands to General / Password / Sessions /
* AI integration / Export / Instance / Danger zone, with
* anchor scroll to the matching section.
*
* 9) API key UX
* - "Key saved" indicator on saved configs (was confusing to see
* an empty input field after a successful save).
*
* Migrations:
* - AIConfigProfile table created.
* - UserPreferences.activeAIConfigId added.
* - AIGeneration.progressText + .durationMs added.
* - ProgramExercise.suggestedWeight + .suggestedWeightUnit added.
* - One-shot lift of the legacy single-config row into a default
* AIConfigProfile per user.
*
* /data is unchanged in spirit — all migrations are additive ALTERs
* via the boot entrypoint. Existing programs/workouts/exercises stay
* exactly as they were.
*/
export const v_1_1_0_4 = VersionInfo.of({
version: '1.1.0:4',
releaseNotes: {
en_US:
'AI integration overhauled based on user testing: (1) save MULTIPLE AI configs and switch between them — Claude, OpenAI, Gemini, custom OpenAI-compatible, Ollama all coexist with one toggled active; (2) Ollama auto-detect — the form probes ollama.startos:11434 and shows your installed models in a dropdown, no copy-paste; (3) curated model dropdowns for Claude / OpenAI / Gemini with current 2026 models (Claude Opus 4.7, Sonnet 4.6, GPT-5.5, Gemini 3.1 Pro Preview, etc.); (4) generation now runs in the BACKGROUND — close the page, come back, find your program in History; (5) system prompt rewritten so the model picks library exercises only (no more "exerciseId doesn\'t belong to this user" errors) and suggests starting weights per exercise (which seed your first workout when you start a program day); (6) generation duration shown alongside cost; (7) Gemini "empty response" now reports the actual finishReason (safety filter, max tokens, etc.); (8) sidebar shows sub-navigation for AI + Settings sections; (9) click any History row to see the full program tree without applying it. No data loss; the schema migration runs additively at boot.',
},
migrations: {
up: async () => {},
down: IMPOSSIBLE,
},
})
+34
View File
@@ -0,0 +1,34 @@
import { IMPOSSIBLE, VersionInfo } from '@start9labs/start-sdk'
/**
* v1.1.0:5 — Gemini model menu correctness.
*
* Driven by a user pointing out that their Google AI Studio dropdown
* showed `gemini-3-pro` and `gemini-3-flash` (no `.0`), neither of
* which were in our v1.1.0:4 dropdown. The menu I shipped only had
* `gemini-3.1-pro-preview` and missed the Flash variants entirely.
*
* What changed:
* - Add `gemini-3.1-pro` (short form, what AI Studio shows) — both
* short and long-preview names work via the API; we now ship the
* short form because it matches the Studio UI.
* - Add `gemini-3.1-flash` (~$0.50/$3 per M) and
* `gemini-3.1-flash-lite` (the cheapest 3.x).
* - Add `gemini-3-pro` and `gemini-3-flash` — older tier, both still
* available in many accounts.
* - Pricing table grows entries for all of the above so the cost
* estimator works correctly when the user picks any of them.
*
* No code changes elsewhere. Pure data fix.
*/
export const v_1_1_0_5 = VersionInfo.of({
version: '1.1.0:5',
releaseNotes: {
en_US:
'Gemini model dropdown corrected: adds gemini-3.1-pro (the short name AI Studio uses), gemini-3.1-flash, gemini-3.1-flash-lite, gemini-3-pro, and gemini-3-flash — all the names that show up in your Google AI Studio dropdown. Pricing table updated to match (Flash ~$0.50/$3 per M, Flash-Lite even cheaper). Pure data fix; no schema or behavior changes.',
},
migrations: {
up: async () => {},
down: IMPOSSIBLE,
},
})
+25
View File
@@ -0,0 +1,25 @@
import { IMPOSSIBLE, VersionInfo } from '@start9labs/start-sdk'
/**
* v1.1.0:6 — Exercise history popup scrolls further.
*
* The clock-icon popup in the workout editor was capped at max-h-80
* (~320px, ~5 history rows). Anyone with multi-year history saw their
* older sessions hidden behind a tiny inner scrollbar. Bumped to
* 70vh so it scales with the viewport (~15+ rows on a normal display,
* more on a large monitor). The IntersectionObserver pagination
* already loaded more on demand — the cap just hid them.
*
* Pure CSS-class change. No schema, no API, no data.
*/
export const v_1_1_0_6 = VersionInfo.of({
version: '1.1.0:6',
releaseNotes: {
en_US:
'Exercise-history popup (the clock icon while logging or editing a workout) now scales to 70% of viewport height instead of the previous ~320px cap. Users with multi-year history can scroll through ~15+ sessions without fighting a tiny inner scrollbar. Pure UI fix.',
},
migrations: {
up: async () => {},
down: IMPOSSIBLE,
},
})
+34
View File
@@ -0,0 +1,34 @@
import { IMPOSSIBLE, VersionInfo } from '@start9labs/start-sdk'
/**
* v1.1.0:7 — Exercise-history popup auto-loads more rows on scroll.
*
* The popup HAD an IntersectionObserver-based infinite-scroll
* implementation (added in v1.0.0:6 alongside the workout-history
* page version of the same feature), but the observer was fiddly
* inside an `absolute`-positioned scroll container. With the small
* 60px rootMargin it would sometimes not fire at all.
*
* Replaced with a plain `scroll` event listener on the popup. Fires
* when the user scrolls within 300px of the bottom (mirroring the
* lookahead used by WorkoutsList on the main Workouts page). Also
* runs once on mount so if the first page doesn't fill the popup,
* we still fetch the next page proactively.
*
* Cosmetic: bottom-of-list status row now shows "Loading more..." /
* "Scroll to load more" / "End of history" so the user has feedback
* on the state instead of just seeing a thin spinner intermittently.
*
* No schema, no API, no data.
*/
export const v_1_1_0_7 = VersionInfo.of({
version: '1.1.0:7',
releaseNotes: {
en_US:
'Exercise-history popup (clock icon while logging or editing a workout) now reliably auto-loads more rows as you scroll, matching the Workouts page. Switched from a fiddly IntersectionObserver (which sometimes didn\'t fire inside the absolute-positioned popup) to a plain scroll listener with a 300px lookahead. Bottom-of-list now shows "Loading more..." / "Scroll to load more" / "End of history" feedback. Pure UI fix.',
},
migrations: {
up: async () => {},
down: IMPOSSIBLE,
},
})