mirror of
https://github.com/Memo-2023/mana-monorepo.git
synced 2026-05-20 00:41:26 +02:00
Final milestone of docs/plans/llm-fallback-aliases.md. Every backend
caller now requests models via the `mana/<class>` alias system instead
of hardcoded `ollama/...` strings. mana-llm resolves aliases through
`services/mana-llm/aliases.yaml` with health-aware fallback (M3) and
emits resolved-model + fallback metrics (M4).
SSOT moved to `packages/shared-ai/src/llm-aliases.ts` so apps/api,
apps/mana/apps/web, and services/mana-ai all import the same
`MANA_LLM` constant via the existing `@mana/shared-ai` workspace
dependency. Three additional sites (memoro-server, mana-events,
mana-research) inline the alias string with a SSOT comment because
they don't pull @mana/shared-ai today.
Migrated 14 sites across 10 files:
- apps/api: writing(LONG_FORM), comic(STRUCTURED), context(FAST_TEXT),
food(VISION), plants(VISION), research orchestrator (3 tiers
collapsed to STRUCTURED+FAST_TEXT/LONG_FORM)
- apps/mana/apps/web: voice/parse-task + parse-habit (STRUCTURED)
- services/mana-ai: planner llm-client + tick.ts (REASONING)
- services/mana-events: website-extractor (STRUCTURED, inlined)
- services/mana-research: mana-llm client (FAST_TEXT, inlined)
- apps/memoro/apps/server: ai.ts (FAST_TEXT, inlined)
Legacy env-vars removed: WRITING_MODEL, COMIC_STORYBOARD_MODEL,
VISION_MODEL, MANA_LLM_DEFAULT_MODEL. The chain in aliases.yaml is
now the single tuning surface; SIGHUP reloads it without redeploys.
New `scripts/validate-llm-strings.mjs` regex-scans 2538 files for
hardcoded `<provider>/<model>` strings and fails the build if any
land outside the SSOT or the explicitly-allowed paths (image-gen
modules, model-inspector code, this validator itself, the registry).
Wired into `validate:all` next to the i18n + theme validators.
Verified: `pnpm validate:llm-strings` clean, `pnpm --filter @mana/api
type-check` clean, `pnpm --filter @mana/ai-service type-check`
clean. Web type-check has 2 pre-existing errors in
SettingsSidebar.svelte (i18n MessageFormatter type drift, last
touched in 988c17a67 — unrelated to this work).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
220 lines
7.4 KiB
TypeScript
220 lines
7.4 KiB
TypeScript
/**
|
|
* Food module — Meal analysis (Gemini Vision via mana-llm) + recommendations.
|
|
*
|
|
* CRUD for meals, goals, favorites is handled by mana-sync. This module
|
|
* owns the server-only operations: photo upload to mana-media, structured
|
|
* AI analysis using the Vercel AI SDK (`generateObject`) against the
|
|
* shared Zod schema in @mana/shared-types, and a small rule-based
|
|
* recommendation engine.
|
|
*
|
|
* Why generateObject + Zod instead of raw fetch?
|
|
* - Runtime validation of the AI response — if Gemini drifts on a
|
|
* field, we throw at the boundary instead of corrupting downstream
|
|
* state. The frontend never sees malformed data.
|
|
* - Provider-portable structured outputs: the AI SDK translates one
|
|
* Zod schema into OpenAI strict json_schema / Anthropic tool-use /
|
|
* Gemini response_schema depending on which backend mana-llm routes
|
|
* to. We don't have to know which.
|
|
* - Single source of truth: the same MealAnalysisSchema is consumed
|
|
* by the unified web app via `z.infer<typeof MealAnalysisSchema>`,
|
|
* so changes here propagate end-to-end without manual sync.
|
|
*/
|
|
|
|
import { Hono } from 'hono';
|
|
import { generateObject } from 'ai';
|
|
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
|
|
import {
|
|
AI_SCHEMA_VERSION,
|
|
MealAnalysisSchema,
|
|
type AiResponseEnvelope,
|
|
type MealAnalysis,
|
|
} from '@mana/shared-types';
|
|
import { logger, type AuthVariables } from '@mana/shared-hono';
|
|
import { MANA_LLM } from '@mana/shared-ai';
|
|
|
|
const LLM_URL = process.env.MANA_LLM_URL || 'http://localhost:3025';
|
|
// mana-llm resolves this alias to a healthy vision model (chain in
|
|
// services/mana-llm/aliases.yaml). To swap the chain, edit the YAML
|
|
// and SIGHUP — no service redeploy here.
|
|
const VISION_MODEL = MANA_LLM.VISION;
|
|
|
|
const llm = createOpenAICompatible({
|
|
name: 'mana-llm',
|
|
// mana-llm exposes /v1/chat/completions (see services/mana-llm/CLAUDE.md +
|
|
// src/main.py:125). The AI SDK's openai-compatible adapter appends
|
|
// /chat/completions to baseURL, so baseURL ends in /v1.
|
|
baseURL: `${LLM_URL}/v1`,
|
|
// Tell the AI SDK that mana-llm honours OpenAI-style strict
|
|
// json_schema response_format. Without this, generateObject() falls
|
|
// back to a tool-call mode that Ollama-backed models don't support
|
|
// reliably and the response fails to validate against the Zod schema.
|
|
// mana-llm's Ollama provider translates response_format → Ollama's
|
|
// native `format` field (services/mana-llm/src/providers/ollama.py)
|
|
// so this is honoured end-to-end.
|
|
supportsStructuredOutputs: true,
|
|
});
|
|
|
|
const ANALYSIS_PROMPT = `Du bist ein Ernährungsexperte. Analysiere die Mahlzeit und gib strukturierte Nährwertdaten zurück. Schätze realistische Portionsgrößen und Kalorien. Antworte auf Deutsch.`;
|
|
|
|
/**
|
|
* Provider hints attached to the system message. Forward-compat:
|
|
*
|
|
* - anthropic.cacheControl: ephemeral system-prompt caching. NO-OP today
|
|
* because (a) we route to Gemini via mana-llm and (b) the prompt is
|
|
* ~50 tokens — well under Anthropic's 1024-token cache minimum. Becomes
|
|
* active automatically when mana-llm routes to Claude AND the prompt
|
|
* grows (e.g. once we attach per-user dietary preferences as system
|
|
* context, which would push us past the threshold).
|
|
*
|
|
* Kept here so the day we flip the backend, we don't have to revisit
|
|
* every route to enable caching — it just starts working.
|
|
*/
|
|
const SYSTEM_CACHE_HINT = {
|
|
anthropic: { cacheControl: { type: 'ephemeral' as const } },
|
|
};
|
|
|
|
/** Wrap a validated AI object in the standard wire-format envelope. */
|
|
function envelope(data: MealAnalysis): AiResponseEnvelope<MealAnalysis> {
|
|
return { schemaVersion: AI_SCHEMA_VERSION, data };
|
|
}
|
|
|
|
const routes = new Hono<{ Variables: AuthVariables }>();
|
|
|
|
// ─── Photo Upload (server-only: S3 storage via mana-media) ───
|
|
|
|
routes.post('/photos/upload', async (c) => {
|
|
const userId = c.get('userId');
|
|
const formData = await c.req.formData();
|
|
const file = formData.get('file') as File | null;
|
|
|
|
if (!file) return c.json({ error: 'No file provided' }, 400);
|
|
if (file.size > 10 * 1024 * 1024) return c.json({ error: 'File too large (max 10MB)' }, 400);
|
|
|
|
try {
|
|
const { uploadImageToMedia } = await import('../../lib/media');
|
|
const buffer = await file.arrayBuffer();
|
|
const result = await uploadImageToMedia(buffer, file.name, { app: 'food', userId });
|
|
|
|
return c.json(
|
|
{
|
|
mediaId: result.id,
|
|
publicUrl: result.urls.original,
|
|
thumbnailUrl: result.urls.thumbnail || result.urls.original,
|
|
storagePath: result.id,
|
|
},
|
|
201
|
|
);
|
|
} catch (err) {
|
|
logger.error('food.upload_failed', {
|
|
error: err instanceof Error ? err.message : String(err),
|
|
});
|
|
return c.json({ error: 'Upload failed' }, 500);
|
|
}
|
|
});
|
|
|
|
// ─── Photo Analysis (Gemini Vision on uploaded URL) ──────────
|
|
|
|
routes.post('/analysis/photo', async (c) => {
|
|
const { photoUrl } = await c.req.json();
|
|
if (!photoUrl) return c.json({ error: 'photoUrl required' }, 400);
|
|
|
|
try {
|
|
const { object } = await generateObject({
|
|
model: llm(VISION_MODEL),
|
|
schema: MealAnalysisSchema,
|
|
messages: [
|
|
{
|
|
role: 'system',
|
|
content: ANALYSIS_PROMPT,
|
|
providerOptions: SYSTEM_CACHE_HINT,
|
|
},
|
|
{
|
|
role: 'user',
|
|
content: [
|
|
{ type: 'text', text: 'Analysiere diese Mahlzeit.' },
|
|
{ type: 'image', image: new URL(photoUrl) },
|
|
],
|
|
},
|
|
],
|
|
temperature: 0.3,
|
|
});
|
|
return c.json(envelope(object));
|
|
} catch (err) {
|
|
logger.error('food.photo_analysis_failed', {
|
|
error: err instanceof Error ? err.message : String(err),
|
|
});
|
|
return c.json({ error: 'Analysis failed' }, 500);
|
|
}
|
|
});
|
|
|
|
// ─── Text Analysis (Gemini on a free-text meal description) ──
|
|
|
|
routes.post('/analysis/text', async (c) => {
|
|
const { description } = await c.req.json();
|
|
if (!description) return c.json({ error: 'description required' }, 400);
|
|
|
|
try {
|
|
const { object } = await generateObject({
|
|
model: llm(VISION_MODEL),
|
|
schema: MealAnalysisSchema,
|
|
messages: [
|
|
{
|
|
role: 'system',
|
|
content: ANALYSIS_PROMPT,
|
|
providerOptions: SYSTEM_CACHE_HINT,
|
|
},
|
|
{
|
|
role: 'user',
|
|
content: `Analysiere diese Mahlzeit: ${description}`,
|
|
},
|
|
],
|
|
temperature: 0.3,
|
|
});
|
|
return c.json(envelope(object));
|
|
} catch (err) {
|
|
logger.error('food.text_analysis_failed', {
|
|
error: err instanceof Error ? err.message : String(err),
|
|
});
|
|
return c.json({ error: 'Analysis failed' }, 500);
|
|
}
|
|
});
|
|
|
|
// ─── Recommendations (server-only: rule engine) ──────────────
|
|
|
|
routes.post('/recommendations/generate', async (c) => {
|
|
const { dailyNutrition } = await c.req.json();
|
|
const hints: Array<{ type: string; priority: string; message: string; nutrient?: string }> = [];
|
|
|
|
if (dailyNutrition) {
|
|
if (dailyNutrition.protein < 25) {
|
|
hints.push({
|
|
type: 'hint',
|
|
priority: 'medium',
|
|
message:
|
|
'Deine Proteinzufuhr ist niedrig. Versuche Hülsenfrüchte, Eier oder Joghurt einzubauen.',
|
|
nutrient: 'protein',
|
|
});
|
|
}
|
|
if (dailyNutrition.fiber < 10) {
|
|
hints.push({
|
|
type: 'hint',
|
|
priority: 'medium',
|
|
message: 'Mehr Ballaststoffe! Vollkornprodukte, Gemüse und Obst helfen.',
|
|
nutrient: 'fiber',
|
|
});
|
|
}
|
|
if (dailyNutrition.sugar > 50) {
|
|
hints.push({
|
|
type: 'hint',
|
|
priority: 'high',
|
|
message:
|
|
'Dein Zuckerkonsum ist hoch. Achte auf versteckten Zucker in Getränken und Fertigprodukten.',
|
|
nutrient: 'sugar',
|
|
});
|
|
}
|
|
}
|
|
|
|
return c.json({ recommendations: hints });
|
|
});
|
|
|
|
export { routes as foodRoutes };
|