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.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,48 +1,36 @@
|
||||
import { NextRequest } from 'next/server';
|
||||
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';
|
||||
import {
|
||||
PROGRAM_OUTPUT_SHAPE,
|
||||
parseAIProgram,
|
||||
} 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
|
||||
*
|
||||
* Body: { templateId?: string, userInput: string }
|
||||
* Body: { templateId?: string, userInput: string, includeHistory?: boolean }
|
||||
*
|
||||
* Streams the model response as Server-Sent Events:
|
||||
* event: generation data: {"id":"...generationId..."}
|
||||
* event: text data: {"delta":"..."}
|
||||
* event: usage data: {"tokensIn":N,"tokensOut":M}
|
||||
* event: complete data: {"parsedOk":true|false,"errorMessage":"..."}
|
||||
* v1.1.0:4: this endpoint now KICKS OFF a background runner and returns
|
||||
* the new generation id immediately. The caller subscribes to live
|
||||
* deltas via GET /api/ai/generations/[id]/stream (SSE) or polls via
|
||||
* GET /api/ai/generations/[id]. Navigating away no longer cancels the
|
||||
* generation — the runner keeps writing to the row in the background.
|
||||
*
|
||||
* Reads the user's AI provider config from UserPreferences. The full
|
||||
* library of exercises is appended to the system prompt so the model
|
||||
* picks real exercise IDs.
|
||||
*
|
||||
* 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.
|
||||
* Response:
|
||||
* 201 { id: "...generationId..." }
|
||||
* 400 { error: "..." }
|
||||
*/
|
||||
|
||||
const bodySchema = z.object({
|
||||
templateId: z.string().optional().nullable(),
|
||||
userInput: z.string().min(1),
|
||||
/**
|
||||
* When true, build + append a compact summary of the user's
|
||||
* recent (90-day) workout history to the system prompt. Lets the
|
||||
* model design around stagnations, current strength levels, and
|
||||
* actual training frequency.
|
||||
*/
|
||||
includeHistory: z.boolean().optional().default(false),
|
||||
});
|
||||
|
||||
@@ -51,53 +39,34 @@ export const dynamic = 'force-dynamic';
|
||||
export async function POST(request: NextRequest) {
|
||||
const user = await getCurrentUser();
|
||||
if (!user) {
|
||||
return new Response(JSON.stringify({ error: 'Unauthorized' }), {
|
||||
status: 401,
|
||||
headers: { 'content-type': 'application/json' },
|
||||
});
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
|
||||
const body = await request.json().catch(() => ({}));
|
||||
const parsed = bodySchema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
error: 'Invalid body',
|
||||
details: parsed.error.errors,
|
||||
}),
|
||||
{ status: 400, headers: { 'content-type': 'application/json' } },
|
||||
return NextResponse.json(
|
||||
{ error: 'Invalid body', details: parsed.error.errors },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
// Load the user's AI provider config.
|
||||
const prefs = await prisma.userPreferences.findUnique({
|
||||
where: { userId: user.id },
|
||||
});
|
||||
if (!prefs?.aiProvider || !prefs?.aiModel) {
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
return NextResponse.json(
|
||||
{
|
||||
error:
|
||||
'AI is not configured. Open Settings → AI integration and pick a provider + model.',
|
||||
}),
|
||||
{ status: 400, headers: { 'content-type': 'application/json' } },
|
||||
);
|
||||
}
|
||||
const provider = getProvider(prefs.aiProvider);
|
||||
if (!provider) {
|
||||
return new Response(
|
||||
JSON.stringify({ error: `Unknown provider: ${prefs.aiProvider}` }),
|
||||
{ status: 400, headers: { 'content-type': 'application/json' } },
|
||||
},
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
// Load the template if provided, else use a no-op default.
|
||||
// Load the template if provided.
|
||||
let template:
|
||||
| {
|
||||
id: string;
|
||||
name: string;
|
||||
systemPrompt: string;
|
||||
userPromptTemplate: string;
|
||||
}
|
||||
| { id: string; name: string; systemPrompt: string; userPromptTemplate: string }
|
||||
| null = null;
|
||||
if (parsed.data.templateId) {
|
||||
const t = await prisma.aIPromptTemplate.findFirst({
|
||||
@@ -113,23 +82,15 @@ export async function POST(request: NextRequest) {
|
||||
},
|
||||
});
|
||||
if (!t) {
|
||||
return new Response(
|
||||
JSON.stringify({ error: 'Template not found.' }),
|
||||
{ status: 404, headers: { 'content-type': 'application/json' } },
|
||||
);
|
||||
return NextResponse.json({ error: 'Template not found.' }, { status: 404 });
|
||||
}
|
||||
template = t;
|
||||
}
|
||||
|
||||
// Load the user's exercise library to embed in the system prompt.
|
||||
// Library for the prompt.
|
||||
const exercises = await prisma.exercise.findMany({
|
||||
where: { userId: user.id },
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
type: true,
|
||||
muscleGroups: true,
|
||||
},
|
||||
select: { id: true, name: true, type: true, muscleGroups: true },
|
||||
});
|
||||
const libraryJson = JSON.stringify(
|
||||
exercises.map((e) => ({
|
||||
@@ -146,138 +107,58 @@ export async function POST(request: NextRequest) {
|
||||
})),
|
||||
);
|
||||
|
||||
// If requested, build the workout-history summary block.
|
||||
// History context if requested.
|
||||
let historyBlock = '';
|
||||
if (parsed.data.includeHistory) {
|
||||
const summary = await buildHistorySummary(prisma, user.id);
|
||||
historyBlock = formatHistoryContext(summary);
|
||||
}
|
||||
|
||||
// Stitch the final system + user prompts.
|
||||
const baseSystem = template?.systemPrompt ?? DEFAULT_SYSTEM_PROMPT;
|
||||
const systemPrompt = `${baseSystem}
|
||||
// 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}
|
||||
|
||||
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}${historyBlock}`;
|
||||
|
||||
const userPromptBody =
|
||||
template?.userPromptTemplate.replace(/{{userInput}}/g, parsed.data.userInput) ??
|
||||
parsed.data.userInput;
|
||||
|
||||
// Persist the pending row up front so the user can see it in
|
||||
// history even if the stream dies mid-flight.
|
||||
const generation = await prisma.aIGeneration.create({
|
||||
data: {
|
||||
userId: user.id,
|
||||
templateId: template?.id ?? null,
|
||||
templateName: template?.name ?? null,
|
||||
userInput: parsed.data.userInput,
|
||||
systemPrompt,
|
||||
userPrompt: userPromptBody,
|
||||
provider: provider.id,
|
||||
model: prefs.aiModel,
|
||||
status: 'pending',
|
||||
},
|
||||
const id = await kickoffGeneration({
|
||||
prisma,
|
||||
userId: user.id,
|
||||
templateId: template?.id ?? null,
|
||||
templateName: template?.name ?? null,
|
||||
userInput: parsed.data.userInput,
|
||||
systemPrompt,
|
||||
userPrompt: userPromptBody,
|
||||
provider: prefs.aiProvider,
|
||||
model: prefs.aiModel,
|
||||
apiKey: prefs.aiApiKey,
|
||||
baseUrl: prefs.aiBaseUrl,
|
||||
});
|
||||
|
||||
// Stream the model output as SSE.
|
||||
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
|
||||
},
|
||||
});
|
||||
return NextResponse.json({ id }, { status: 201 });
|
||||
}
|
||||
|
||||
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.
|
||||
|
||||
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.`;
|
||||
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).`;
|
||||
|
||||
Reference in New Issue
Block a user