mirror of
https://github.com/Memo-2023/mana-monorepo.git
synced 2026-05-23 17:06:41 +02:00
generateObject() in the AI SDK falls back to a tool-call mode when the provider doesn't advertise structured-output support — and tool calling through Ollama isn't reliable enough that the schema-validation step passes. The response was failing with 'No object generated: response did not match schema' even though the underlying mana-llm + Ollama roundtrip works correctly when called with response_format directly (verified via curl). Set supportsStructuredOutputs:true on the createOpenAICompatible factory so the AI SDK uses response_format json_schema mode. mana-llm already routes that to Ollama's native format field thanks to the companion fix in services/mana-llm/src/providers/ollama.py — verified end-to-end with the MealAnalysisSchema and Gemma 3 4B. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
117 lines
3.8 KiB
TypeScript
117 lines
3.8 KiB
TypeScript
/**
|
|
* Planta module — Photo upload + AI plant identification.
|
|
*
|
|
* CRUD for plants, photos, watering is handled by mana-sync. This
|
|
* module owns the server-only operations: photo upload to mana-media
|
|
* and structured plant identification via the Vercel AI SDK
|
|
* (`generateObject`) using the shared PlantIdentificationSchema in
|
|
* @mana/shared-types. See nutriphi/routes.ts for the rationale behind
|
|
* the AI SDK + Zod approach.
|
|
*/
|
|
|
|
import { Hono } from 'hono';
|
|
import { generateObject } from 'ai';
|
|
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
|
|
import {
|
|
AI_SCHEMA_VERSION,
|
|
PlantIdentificationSchema,
|
|
type AiResponseEnvelope,
|
|
type PlantIdentification,
|
|
} from '@mana/shared-types';
|
|
import { logger, type AuthVariables } from '@mana/shared-hono';
|
|
|
|
const LLM_URL = process.env.MANA_LLM_URL || 'http://localhost:3025';
|
|
// See nutriphi/routes.ts for the rationale on the default model and
|
|
// the /v1 base URL.
|
|
const VISION_MODEL = process.env.VISION_MODEL || 'ollama/gemma3:4b';
|
|
|
|
const llm = createOpenAICompatible({
|
|
name: 'mana-llm',
|
|
baseURL: `${LLM_URL}/v1`,
|
|
// See nutriphi/routes.ts for the rationale on this flag.
|
|
supportsStructuredOutputs: true,
|
|
});
|
|
|
|
const IDENTIFICATION_PROMPT = `Du bist ein Pflanzenexperte. Analysiere das Pflanzenfoto und liefere eine strukturierte Identifikation mit lateinischem Namen, deutschen Trivialnamen, Pflegehinweisen und einer Gesundheitseinschätzung. Antworte auf Deutsch.`;
|
|
|
|
// See nutriphi/routes.ts for the rationale: this is a forward-compat
|
|
// hint for Anthropic prompt caching, ignored by Gemini today.
|
|
const SYSTEM_CACHE_HINT = {
|
|
anthropic: { cacheControl: { type: 'ephemeral' as const } },
|
|
};
|
|
|
|
/** Wrap a validated AI object in the standard wire-format envelope. */
|
|
function envelope(data: PlantIdentification): AiResponseEnvelope<PlantIdentification> {
|
|
return { schemaVersion: AI_SCHEMA_VERSION, data };
|
|
}
|
|
|
|
const routes = new Hono<{ Variables: AuthVariables }>();
|
|
|
|
// ─── Photo Upload (server-only: S3 storage) ─────────────────
|
|
|
|
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;
|
|
const plantId = formData.get('plantId') as string | 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: 'planta', userId });
|
|
|
|
return c.json(
|
|
{
|
|
storagePath: result.id,
|
|
publicUrl: result.urls.original,
|
|
mediaId: result.id,
|
|
plantId,
|
|
},
|
|
201
|
|
);
|
|
} catch (err) {
|
|
logger.error('planta.upload_failed', {
|
|
error: err instanceof Error ? err.message : String(err),
|
|
});
|
|
return c.json({ error: 'Upload failed' }, 500);
|
|
}
|
|
});
|
|
|
|
// ─── AI Analysis (Gemini Vision on uploaded URL) ─────────────
|
|
|
|
routes.post('/analysis/identify', 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: PlantIdentificationSchema,
|
|
messages: [
|
|
{
|
|
role: 'system',
|
|
content: IDENTIFICATION_PROMPT,
|
|
providerOptions: SYSTEM_CACHE_HINT,
|
|
},
|
|
{
|
|
role: 'user',
|
|
content: [
|
|
{ type: 'text', text: 'Analysiere diese Pflanze.' },
|
|
{ type: 'image', image: new URL(photoUrl) },
|
|
],
|
|
},
|
|
],
|
|
});
|
|
return c.json(envelope(object));
|
|
} catch (err) {
|
|
logger.error('planta.analysis_failed', {
|
|
error: err instanceof Error ? err.message : String(err),
|
|
});
|
|
return c.json({ error: 'Analysis failed' }, 500);
|
|
}
|
|
});
|
|
|
|
export { routes as plantaRoutes };
|