tia/src/app/api/memories/route.ts
Mannu c2cabc01d3 feat(g1-g4): design system, memories pipeline, medical tracking, AI brain
G1 — Design System: 14 UI primitives (Button, Card, Modal, Sheet, Input,
Textarea, Select, EmptyState, LoadingShimmer, ConfirmDialog, WashiTape,
Badge, Avatar, Tabs), PageTransition with Framer Motion, sun/moon CSS vars,
Caveat font, /dev/components visual showcase.

G2 — Memories Pipeline: R2 presigned uploads, Sharp thumbnail generation,
LiteLLM vision captions + pgvector embeddings, CSS masonry gallery with
infinite scroll, private toggle, semantic search fallback to ILIKE.

G3 — Medical: dose log + correction audit trail, IAP vaccine bulk import,
emergency escalation page, pediatrician phone in settings.

G4 — AI Brain: keyword guardrail → LLM classifier → structured DB tool-use
(7 tools) → memory search → general parenting handler; ai_usage table;
22-case medical bypass safety test suite.

DB migrations: 0011_memories, 0012_medical_doses, 0013_ai_usage.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-17 17:48:34 +05:30

58 lines
2.2 KiB
TypeScript

import { NextRequest, NextResponse } from "next/server";
import { requireFamily } from "@/lib/auth";
import { sql } from "@/db";
function getBaseUrl() {
const pub = process.env.R2_PUBLIC_URL;
const id = process.env.R2_ACCOUNT_ID;
return pub || `https://pub-${id}.r2.dev`;
}
function toMemoryDto(m: Record<string, unknown>, baseUrl: string) {
return {
id: m.id,
key: m.r2_key,
url: `${baseUrl}/${m.r2_key}`,
thumbnailUrl: m.r2_thumbnail_key ? `${baseUrl}/${m.r2_thumbnail_key}` : null,
sizeBytes: m.size_bytes,
mimeType: m.mime_type,
title: m.title,
description: m.description,
takenAt: m.taken_at,
visionCaption: m.vision_caption,
visionTags: m.vision_tags,
isPrivate: m.is_private,
processingStatus: m.processing_status,
createdAt: m.created_at,
updatedAt: m.updated_at,
};
}
// GET /api/memories?childId=&limit=30&cursor=
export async function GET(req: NextRequest) {
const auth = await requireFamily();
if (!auth.success) return NextResponse.json({ error: auth.error }, { status: auth.status });
const familyId = auth.session!.familyId!;
const { searchParams } = new URL(req.url);
const childId = searchParams.get("childId");
const limit = Math.min(parseInt(searchParams.get("limit") || "30"), 100);
const cursor = searchParams.get("cursor"); // ISO timestamp for pagination
let rows;
if (childId) {
rows = cursor
? await sql`SELECT * FROM memories WHERE family_id = ${familyId} AND child_id = ${childId} AND created_at < ${cursor} ORDER BY created_at DESC LIMIT ${limit}`
: await sql`SELECT * FROM memories WHERE family_id = ${familyId} AND child_id = ${childId} ORDER BY created_at DESC LIMIT ${limit}`;
} else {
rows = cursor
? await sql`SELECT * FROM memories WHERE family_id = ${familyId} AND created_at < ${cursor} ORDER BY created_at DESC LIMIT ${limit}`
: await sql`SELECT * FROM memories WHERE family_id = ${familyId} ORDER BY created_at DESC LIMIT ${limit}`;
}
const baseUrl = getBaseUrl();
const items = rows.map(m => toMemoryDto(m, baseUrl));
const nextCursor = rows.length === limit ? rows[rows.length - 1].created_at : null;
return NextResponse.json({ success: true, items, nextCursor });
}