Freezmo: self-hosted notes-to-activities study app
Next.js 15 app that turns photos, PDFs, DOCX/PPTX and text into a swipeable feed of AI-generated activities (flashcards, multiple choice, true/false, fill-in-the-blank, matching, ordering, short answer) using the user's own Ollama models. Highlights: - All AI calls run in server route handlers; the browser never talks to Ollama. - Bring-your-own-key mode (default): the key lives only in the browser and is sent per-request; server, local-daemon and shared-key modes also supported. - Client-side document extraction (pdf.js, mammoth, jszip) with scanned-PDF page rendering for the vision model; notes stay in their original language. - ChatGPT-style shell: collapsible sidebar / mobile drawer, neutral token-based light + dark themes, folder organisation, PWA (manifest, SW, offline page). - Strict JSON validation with zod + auto-repair, rate limiting, and request guards (cross-origin, body-size, API-key sanitising). - setup.sh bootstraps deps, .env.local and a production build. Gitignore excludes .env*, the generated pdf.js worker and local scratch files.
This commit is contained in:
parent
3baa0fa8ce
commit
3ea2cea100
83 changed files with 10909 additions and 165 deletions
226
app/api/generate/route.ts
Normal file
226
app/api/generate/route.ts
Normal file
|
|
@ -0,0 +1,226 @@
|
|||
import { z } from "zod";
|
||||
|
||||
import { extractNotes, generateActivities, notesToText, type PipelineStage } from "@/lib/ai";
|
||||
import { AUTH_ERROR_MESSAGES, authErrorKind, getServerConfig, resolveAuth, resolveModelName } from "@/lib/config";
|
||||
import { friendlyErrorMessage, OllamaError } from "@/lib/ollama";
|
||||
import { rejectCrossOrigin, rejectOversizedBody } from "@/lib/http-guard";
|
||||
import { clientKey, rateLimit, rateLimitResponse } from "@/lib/rate-limit";
|
||||
import { difficultySchema } from "@/lib/schemas";
|
||||
import { uid } from "@/lib/utils";
|
||||
import { xpForActivity } from "@/lib/xp";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
export const dynamic = "force-dynamic";
|
||||
export const maxDuration = 300;
|
||||
|
||||
/** ~8 MB per image after client-side resizing; up to 16 images (photos + PDF pages). */
|
||||
const MAX_IMAGE_CHARS = 8_000_000;
|
||||
|
||||
const requestSchema = z.object({
|
||||
images: z.array(z.string().min(32).max(MAX_IMAGE_CHARS)).max(16).optional(),
|
||||
text: z.string().max(120_000).optional(),
|
||||
difficulty: difficultySchema.default("medium"),
|
||||
count: z.number().int().min(4).max(18).optional(),
|
||||
focus: z.string().max(300).optional(),
|
||||
/** Language the deck must be written in (usually detected by the vision step). */
|
||||
language: z.string().max(60).optional(),
|
||||
mode: z.enum(["create", "more"]).default("create"),
|
||||
existing: z
|
||||
.object({
|
||||
title: z.string().max(120),
|
||||
types: z.array(z.string()).max(30),
|
||||
count: z.number().int().min(0).max(100),
|
||||
})
|
||||
.optional(),
|
||||
});
|
||||
|
||||
type StreamEvent =
|
||||
| { type: "stage"; stage: PipelineStage; message: string }
|
||||
| { type: "deck"; deck: unknown; meta: Record<string, unknown> }
|
||||
| { type: "error"; error: string; kind?: string };
|
||||
|
||||
function encoder() {
|
||||
const enc = new TextEncoder();
|
||||
return (event: StreamEvent) => enc.encode(`${JSON.stringify(event)}\n`);
|
||||
}
|
||||
|
||||
/** Generous but finite: ~16 compressed images plus JSON overhead. */
|
||||
const MAX_BODY_BYTES = 33554432;
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const crossOrigin = rejectCrossOrigin(request);
|
||||
if (crossOrigin) return crossOrigin;
|
||||
const oversized = rejectOversizedBody(request, MAX_BODY_BYTES);
|
||||
if (oversized) return oversized;
|
||||
|
||||
const config = getServerConfig();
|
||||
|
||||
// Cheap requests are rejected before any work happens.
|
||||
const limit = rateLimit(`generate:${clientKey(request)}`, config.rateLimits.generate, config.rateLimits.windowMs);
|
||||
if (!limit.allowed) return rateLimitResponse(limit);
|
||||
|
||||
const auth = resolveAuth(config, request);
|
||||
if ("error" in auth) {
|
||||
return Response.json(
|
||||
{ error: AUTH_ERROR_MESSAGES[auth.error], kind: authErrorKind(auth.error) },
|
||||
{ status: 401 },
|
||||
);
|
||||
}
|
||||
|
||||
let payload: unknown;
|
||||
try {
|
||||
payload = await request.json();
|
||||
} catch {
|
||||
return Response.json({ error: "Request body was not valid JSON." }, { status: 400 });
|
||||
}
|
||||
|
||||
const parsed = requestSchema.safeParse(payload);
|
||||
if (!parsed.success) {
|
||||
return Response.json(
|
||||
{
|
||||
error: `Invalid request: ${parsed.error.issues
|
||||
.slice(0, 3)
|
||||
.map((issue) => `${issue.path.join(".") || "body"} ${issue.message}`)
|
||||
.join("; ")}`,
|
||||
},
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
const { images, text, difficulty, count, focus, mode, existing, language } = parsed.data;
|
||||
const imageList = (images ?? []).map(stripDataUrlPrefix).filter(Boolean);
|
||||
|
||||
if (imageList.length === 0 && !text?.trim()) {
|
||||
return Response.json(
|
||||
{ error: "Add at least one photo of your notes or paste some text to build a deck." },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
const requestedCount = count ?? config.defaultActivityCount;
|
||||
const encode = encoder();
|
||||
const abort = new AbortController();
|
||||
// A single hard deadline for the whole pipeline (two sequential model calls).
|
||||
const globalTimer = setTimeout(() => abort.abort(), config.timeoutMs * 2);
|
||||
|
||||
const stream = new ReadableStream<Uint8Array>({
|
||||
async start(controller) {
|
||||
const send = (event: StreamEvent) => {
|
||||
try {
|
||||
controller.enqueue(encode(event));
|
||||
} catch {
|
||||
/* client went away */
|
||||
}
|
||||
};
|
||||
|
||||
try {
|
||||
let sourceText: string;
|
||||
let deckTitle: string | undefined;
|
||||
let subject: string | undefined;
|
||||
let summary: string | undefined;
|
||||
let detectedLanguage: string | undefined = language;
|
||||
let repaired = false;
|
||||
|
||||
if (imageList.length > 0) {
|
||||
send({ type: "stage", stage: "vision", message: "Reading your notes…" });
|
||||
const extraction = await extractNotes({
|
||||
model: config.visionModel,
|
||||
repairModel: config.textModel,
|
||||
auth: auth.auth,
|
||||
images: imageList,
|
||||
text,
|
||||
signal: abort.signal,
|
||||
onProgress: (message) => send({ type: "stage", stage: "vision", message }),
|
||||
});
|
||||
sourceText = notesToText(extraction.notes);
|
||||
deckTitle = extraction.notes.title;
|
||||
subject = extraction.notes.subject;
|
||||
summary = extraction.notes.summary;
|
||||
detectedLanguage = extraction.notes.language || language;
|
||||
repaired = repaired || extraction.repaired || extraction.languageRepaired;
|
||||
send({
|
||||
type: "stage",
|
||||
stage: "generation",
|
||||
message: "Finding the key ideas…",
|
||||
});
|
||||
} else {
|
||||
sourceText = text!.trim();
|
||||
send({ type: "stage", stage: "generation", message: "Finding the key ideas…" });
|
||||
}
|
||||
|
||||
const generated = await generateActivities({
|
||||
model: config.textModel,
|
||||
auth: auth.auth,
|
||||
notes: sourceText,
|
||||
difficulty,
|
||||
count: requestedCount,
|
||||
language: detectedLanguage,
|
||||
existing: mode === "more" ? existing : undefined,
|
||||
focus,
|
||||
signal: abort.signal,
|
||||
});
|
||||
|
||||
const activities = generated.deck.activities.map((activity) => ({
|
||||
...activity,
|
||||
id: uid("act"),
|
||||
xp: xpForActivity(activity.type, difficulty),
|
||||
}));
|
||||
|
||||
send({
|
||||
type: "deck",
|
||||
deck: {
|
||||
title: generated.deck.title || deckTitle || "Your study deck",
|
||||
subject: generated.deck.subject ?? subject,
|
||||
summary: generated.deck.summary ?? summary,
|
||||
language: generated.deck.language || detectedLanguage,
|
||||
sourceText,
|
||||
difficulty,
|
||||
activities,
|
||||
},
|
||||
meta: {
|
||||
requested: generated.requested,
|
||||
generated: activities.length,
|
||||
repaired,
|
||||
language: generated.deck.language || detectedLanguage || null,
|
||||
visionModel: imageList.length ? resolveModelName(config.visionModel, auth.auth.directCloud) : null,
|
||||
textModel: resolveModelName(config.textModel, auth.auth.directCloud),
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
const message = friendlyErrorMessage(error, auth.auth.baseUrl);
|
||||
// Never log the request body: it may contain the user's notes.
|
||||
console.error("[api/generate]", error instanceof OllamaError ? `${error.kind}: ${error.message}` : error);
|
||||
send({
|
||||
type: "error",
|
||||
error: message,
|
||||
kind: error instanceof OllamaError ? error.kind : "unknown",
|
||||
});
|
||||
} finally {
|
||||
clearTimeout(globalTimer);
|
||||
try {
|
||||
controller.close();
|
||||
} catch {
|
||||
/* already closed */
|
||||
}
|
||||
}
|
||||
},
|
||||
cancel() {
|
||||
clearTimeout(globalTimer);
|
||||
abort.abort();
|
||||
},
|
||||
});
|
||||
|
||||
return new Response(stream, {
|
||||
headers: {
|
||||
"Content-Type": "application/x-ndjson; charset=utf-8",
|
||||
"Cache-Control": "no-store, no-transform",
|
||||
Connection: "keep-alive",
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function stripDataUrlPrefix(value: string): string {
|
||||
const commaIndex = value.indexOf(",");
|
||||
if (value.startsWith("data:") && commaIndex !== -1) return value.slice(commaIndex + 1);
|
||||
return value;
|
||||
}
|
||||
71
app/api/grade/route.ts
Normal file
71
app/api/grade/route.ts
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
import { z } from "zod";
|
||||
|
||||
import { gradeShortAnswer } from "@/lib/ai";
|
||||
import { AUTH_ERROR_MESSAGES, authErrorKind, getServerConfig, resolveAuth } from "@/lib/config";
|
||||
import { friendlyErrorMessage, OllamaError } from "@/lib/ollama";
|
||||
import { rejectCrossOrigin, rejectOversizedBody } from "@/lib/http-guard";
|
||||
import { clientKey, rateLimit, rateLimitResponse } from "@/lib/rate-limit";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
const requestSchema = z.object({
|
||||
question: z.string().min(1).max(1000),
|
||||
referenceAnswer: z.string().min(1).max(2000),
|
||||
keyPoints: z.array(z.string().max(300)).max(8).default([]),
|
||||
userAnswer: z.string().min(1).max(2000),
|
||||
/** Language of the deck so feedback comes back in the same language. */
|
||||
language: z.string().max(60).optional(),
|
||||
});
|
||||
|
||||
/** Generous but finite: ~16 compressed images plus JSON overhead. */
|
||||
const MAX_BODY_BYTES = 65536;
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const crossOrigin = rejectCrossOrigin(request);
|
||||
if (crossOrigin) return crossOrigin;
|
||||
const oversized = rejectOversizedBody(request, MAX_BODY_BYTES);
|
||||
if (oversized) return oversized;
|
||||
|
||||
const config = getServerConfig();
|
||||
|
||||
const limit = rateLimit(`grade:${clientKey(request)}`, config.rateLimits.grade, config.rateLimits.windowMs);
|
||||
if (!limit.allowed) return rateLimitResponse(limit);
|
||||
|
||||
const auth = resolveAuth(config, request);
|
||||
if ("error" in auth) {
|
||||
return Response.json(
|
||||
{ error: AUTH_ERROR_MESSAGES[auth.error], kind: authErrorKind(auth.error) },
|
||||
{ status: 401 },
|
||||
);
|
||||
}
|
||||
|
||||
let payload: unknown;
|
||||
try {
|
||||
payload = await request.json();
|
||||
} catch {
|
||||
return Response.json({ error: "Request body was not valid JSON." }, { status: 400 });
|
||||
}
|
||||
|
||||
const parsed = requestSchema.safeParse(payload);
|
||||
if (!parsed.success) {
|
||||
return Response.json({ error: "Invalid grading request." }, { status: 400 });
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await gradeShortAnswer({
|
||||
model: config.textModel,
|
||||
auth: auth.auth,
|
||||
...parsed.data,
|
||||
// Grading is quick; don't let a hung request block the feed for minutes.
|
||||
signal: AbortSignal.timeout(Math.min(config.timeoutMs, 120_000)),
|
||||
});
|
||||
return Response.json(result);
|
||||
} catch (error) {
|
||||
console.error("[api/grade]", error instanceof OllamaError ? `${error.kind}: ${error.message}` : error);
|
||||
return Response.json(
|
||||
{ error: friendlyErrorMessage(error, auth.auth.baseUrl), kind: error instanceof OllamaError ? error.kind : "unknown" },
|
||||
{ status: 502 },
|
||||
);
|
||||
}
|
||||
}
|
||||
131
app/api/health/route.ts
Normal file
131
app/api/health/route.ts
Normal file
|
|
@ -0,0 +1,131 @@
|
|||
import { AUTH_ERROR_MESSAGES, authErrorKind, canonicalModelName, getServerConfig, resolveAuth, resolveModelName, type AuthMode } from "@/lib/config";
|
||||
import { friendlyErrorMessage, ollamaListModels, ollamaVerifyKey, OllamaError } from "@/lib/ollama";
|
||||
import { clientKey, rateLimit, rateLimitResponse } from "@/lib/rate-limit";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
export type ModelHealth = {
|
||||
configured: string;
|
||||
resolved: string;
|
||||
available: boolean | null;
|
||||
note?: string;
|
||||
};
|
||||
|
||||
export type HealthResponse = {
|
||||
ok: boolean;
|
||||
reachable: boolean;
|
||||
mode: "local-daemon" | "direct-cloud";
|
||||
authMode: AuthMode;
|
||||
/** True when the browser has to provide its own key and hasn't. */
|
||||
needsKey: boolean;
|
||||
/** null = not checked (no key), true/false = Ollama accepted/rejected the key. */
|
||||
keyAccepted: boolean | null;
|
||||
baseUrl: string;
|
||||
hasApiKey: boolean;
|
||||
vision: ModelHealth;
|
||||
text: ModelHealth;
|
||||
latencyMs: number | null;
|
||||
modelCount: number | null;
|
||||
error: string | null;
|
||||
kind?: string;
|
||||
checkedAt: string;
|
||||
};
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const config = getServerConfig();
|
||||
|
||||
const limit = rateLimit(`health:${clientKey(request)}`, config.rateLimits.health, config.rateLimits.windowMs);
|
||||
if (!limit.allowed) return rateLimitResponse(limit);
|
||||
|
||||
const started = Date.now();
|
||||
const resolvedAuth = resolveAuth(config, request);
|
||||
|
||||
const health: HealthResponse = {
|
||||
ok: false,
|
||||
reachable: false,
|
||||
mode: config.directCloud ? "direct-cloud" : "local-daemon",
|
||||
authMode: config.authMode,
|
||||
needsKey: "error" in resolvedAuth,
|
||||
keyAccepted: null,
|
||||
baseUrl: config.baseUrl,
|
||||
hasApiKey: !("error" in resolvedAuth) && Boolean(resolvedAuth.auth.apiKey),
|
||||
vision: {
|
||||
configured: config.visionModel,
|
||||
resolved: resolveModelName(config.visionModel, config.directCloud),
|
||||
available: null,
|
||||
},
|
||||
text: {
|
||||
configured: config.textModel,
|
||||
resolved: resolveModelName(config.textModel, config.directCloud),
|
||||
available: null,
|
||||
},
|
||||
latencyMs: null,
|
||||
modelCount: null,
|
||||
error: null,
|
||||
checkedAt: new Date().toISOString(),
|
||||
};
|
||||
|
||||
if ("error" in resolvedAuth) {
|
||||
health.error =
|
||||
resolvedAuth.error === "missing_key"
|
||||
? "No Ollama API key yet. Add your key in the settings dialog — it is stored only in this browser."
|
||||
: AUTH_ERROR_MESSAGES[resolvedAuth.error];
|
||||
health.kind = authErrorKind(resolvedAuth.error);
|
||||
return Response.json(health satisfies HealthResponse, { headers: { "Cache-Control": "no-store" } });
|
||||
}
|
||||
|
||||
try {
|
||||
const models = await ollamaListModels(resolvedAuth.auth);
|
||||
health.reachable = true;
|
||||
health.latencyMs = Date.now() - started;
|
||||
health.modelCount = models.length;
|
||||
|
||||
// A key is only "accepted" once Ollama authenticated a real request —
|
||||
// model listings are public, so an actual (tiny, cached) chat proves it.
|
||||
if (resolvedAuth.auth.apiKey) {
|
||||
const probe = await ollamaVerifyKey(resolvedAuth.auth, config.textModel, config.healthTimeoutMs * 3);
|
||||
health.keyAccepted = probe.valid;
|
||||
if (probe.valid === false) {
|
||||
health.error = "Ollama rejected this API key. Create a new one at ollama.com/settings/keys and paste it again.";
|
||||
health.kind = "auth";
|
||||
return Response.json(health satisfies HealthResponse, { headers: { "Cache-Control": "no-store" } });
|
||||
}
|
||||
if (probe.reason === "no_credits") {
|
||||
health.error = `The key works, but ${config.textModel} is not included in this account's plan. Add usage credits or switch to a free-tier model in .env.local.`;
|
||||
health.kind = "no_credits";
|
||||
}
|
||||
}
|
||||
|
||||
const names = new Set(models.map((model) => canonicalModelName(model.name)));
|
||||
for (const key of ["vision", "text"] as const) {
|
||||
const entry = health[key];
|
||||
const wanted = canonicalModelName(entry.resolved);
|
||||
const listed = names.has(wanted);
|
||||
entry.available = listed;
|
||||
if (!listed) {
|
||||
entry.note =
|
||||
health.mode === "local-daemon"
|
||||
? `Not in the daemon's model list. Run \`ollama pull ${entry.resolved}\` or check your sign-in.`
|
||||
: `Not returned by ${config.baseUrl}/api/tags. Check the model name and your key's plan.`;
|
||||
}
|
||||
}
|
||||
|
||||
health.ok = Boolean(health.vision.available && health.text.available && health.keyAccepted !== false);
|
||||
if (!health.ok && !health.error) {
|
||||
health.error = "Ollama is reachable, but at least one configured model is not available.";
|
||||
}
|
||||
} catch (error) {
|
||||
health.error = friendlyErrorMessage(error, config.baseUrl);
|
||||
health.kind = error instanceof OllamaError ? error.kind : "unknown";
|
||||
if (error instanceof OllamaError && (error.kind === "auth" || error.kind === "http")) {
|
||||
health.keyAccepted = false;
|
||||
}
|
||||
health.latencyMs = null;
|
||||
}
|
||||
|
||||
return Response.json(health satisfies HealthResponse, {
|
||||
status: 200,
|
||||
headers: { "Cache-Control": "no-store" },
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue