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.
59 lines
1.8 KiB
TypeScript
59 lines
1.8 KiB
TypeScript
export function cn(...parts: Array<string | false | null | undefined>): string {
|
|
return parts.filter(Boolean).join(" ");
|
|
}
|
|
|
|
export function uid(prefix = "id"): string {
|
|
const rand =
|
|
typeof crypto !== "undefined" && "randomUUID" in crypto
|
|
? crypto.randomUUID().slice(0, 8)
|
|
: Math.random().toString(36).slice(2, 10);
|
|
return `${prefix}_${Date.now().toString(36)}_${rand}`;
|
|
}
|
|
|
|
/** Deterministic-ish shuffle (Fisher-Yates). Returns a new array. */
|
|
export function shuffle<T>(input: readonly T[]): T[] {
|
|
const out = [...input];
|
|
for (let i = out.length - 1; i > 0; i--) {
|
|
const j = Math.floor(Math.random() * (i + 1));
|
|
[out[i], out[j]] = [out[j], out[i]];
|
|
}
|
|
return out;
|
|
}
|
|
|
|
export function formatDate(timestamp: number): string {
|
|
try {
|
|
return new Intl.DateTimeFormat(undefined, {
|
|
day: "numeric",
|
|
month: "short",
|
|
year: "numeric",
|
|
}).format(new Date(timestamp));
|
|
} catch {
|
|
return "";
|
|
}
|
|
}
|
|
|
|
/** Remove ```json fences and stray prose around a JSON object. */
|
|
export function stripFences(text: string): string {
|
|
let value = text.trim();
|
|
const fence = value.match(/```(?:json)?\s*([\s\S]*?)```/i);
|
|
if (fence) value = fence[1].trim();
|
|
if (value.startsWith("{") || value.startsWith("[")) return value;
|
|
const start = value.indexOf("{");
|
|
const end = value.lastIndexOf("}");
|
|
if (start !== -1 && end > start) return value.slice(start, end + 1);
|
|
return value;
|
|
}
|
|
|
|
export function parseJsonLoose(text: string): unknown {
|
|
const cleaned = stripFences(text);
|
|
try {
|
|
return JSON.parse(cleaned);
|
|
} catch {
|
|
// Last resort: tolerate trailing commas from chatty models.
|
|
return JSON.parse(cleaned.replace(/,\s*([}\]])/g, "$1"));
|
|
}
|
|
}
|
|
|
|
export function truncate(value: string, max: number): string {
|
|
return value.length > max ? `${value.slice(0, max - 1)}…` : value;
|
|
}
|