initialize project with basic structure and dependencies
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
import { db } from "../../../db/index";
|
||||
import { courses } from "../../../db/schema";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { generateCourseInBackground } from "../../../utils/generateCourse";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const id = getRouterParam(event, "id")!;
|
||||
|
||||
const course = await db.query.courses.findFirst({ where: eq(courses.id, id) });
|
||||
if (!course) throw createError({ statusCode: 404, message: "Course not found" });
|
||||
|
||||
await db.update(courses).set({ status: "processing", stage: "parsing_pdfs" }).where(eq(courses.id, id));
|
||||
|
||||
// fire and forget — response returns immediately
|
||||
console.log(`[revisione:${id.slice(0, 8)}] background generation triggered for "${course.title}"`);
|
||||
generateCourseInBackground(id).catch(() => {});
|
||||
|
||||
setResponseStatus(event, 202);
|
||||
return { message: "Course generation started" };
|
||||
});
|
||||
@@ -0,0 +1,43 @@
|
||||
import { db } from "../../../db/index";
|
||||
import { courses, topics, userProgress, lessons } from "../../../db/schema";
|
||||
import { eq, inArray } from "drizzle-orm";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const id = getRouterParam(event, "id")!;
|
||||
|
||||
const course = await db.query.courses.findFirst({
|
||||
where: eq(courses.id, id),
|
||||
});
|
||||
|
||||
if (!course) throw createError({ statusCode: 404, message: "Course not found" });
|
||||
|
||||
const topicRows = await db.query.topics.findMany({
|
||||
where: eq(topics.courseId, id),
|
||||
orderBy: (t, { asc }) => asc(t.order),
|
||||
});
|
||||
|
||||
const progressRows = await db.query.userProgress.findMany({
|
||||
where: eq(userProgress.courseId, id),
|
||||
});
|
||||
|
||||
const progressMap: Record<string, typeof progressRows[0]> = {};
|
||||
for (const p of progressRows) progressMap[p.topicId] = p;
|
||||
|
||||
const topicIds = topicRows.map((t) => t.id);
|
||||
|
||||
const lessonRows = topicIds.length
|
||||
? await db.query.lessons.findMany({ where: inArray(lessons.topicId, topicIds) })
|
||||
: [];
|
||||
|
||||
const lessonTopicIds = new Set(lessonRows.map((l) => l.topicId));
|
||||
|
||||
return {
|
||||
...course,
|
||||
topics: topicRows.map((t) => ({
|
||||
...t,
|
||||
prerequisiteTopicIds: JSON.parse(t.prerequisiteTopicIds ?? "[]"),
|
||||
progress: progressMap[t.id] ?? null,
|
||||
hasLesson: lessonTopicIds.has(t.id),
|
||||
})),
|
||||
};
|
||||
});
|
||||
@@ -0,0 +1,27 @@
|
||||
import { db } from "../../../db/index";
|
||||
import { courses } from "../../../db/schema";
|
||||
import { eq } from "drizzle-orm";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const id = getRouterParam(event, "id")!;
|
||||
const body = await readBody(event);
|
||||
|
||||
const course = await db.query.courses.findFirst({ where: eq(courses.id, id) });
|
||||
if (!course) throw createError({ statusCode: 404, message: "Course not found" });
|
||||
|
||||
const updates: Partial<typeof courses.$inferInsert> = {};
|
||||
|
||||
if (body.title !== undefined) {
|
||||
if (!body.title.trim()) throw createError({ statusCode: 400, message: "Title cannot be empty" });
|
||||
updates.title = body.title.trim();
|
||||
}
|
||||
if (body.subject !== undefined) updates.subject = body.subject;
|
||||
|
||||
if (Object.keys(updates).length === 0) {
|
||||
throw createError({ statusCode: 400, message: "Nothing to update" });
|
||||
}
|
||||
|
||||
await db.update(courses).set(updates).where(eq(courses.id, id));
|
||||
|
||||
return { ok: true };
|
||||
});
|
||||
@@ -0,0 +1,52 @@
|
||||
import { db } from "../../../db/index";
|
||||
import { uploads, courses } from "../../../db/schema";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { randomUUID } from "crypto";
|
||||
import { writeFile, mkdir } from "fs/promises";
|
||||
import { resolve } from "path";
|
||||
import { parsePdf } from "../../../utils/parsePdf";
|
||||
import { detectUploadType } from "../../../utils/detectUploadType";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const id = getRouterParam(event, "id")!;
|
||||
|
||||
const course = await db.query.courses.findFirst({ where: eq(courses.id, id) });
|
||||
if (!course) throw createError({ statusCode: 404, message: "Course not found" });
|
||||
|
||||
const formData = await readFormData(event);
|
||||
const file = formData.get("file") as File | null;
|
||||
|
||||
if (!file) {
|
||||
throw createError({ statusCode: 400, message: "file is required" });
|
||||
}
|
||||
|
||||
const uploadDir = resolve(process.cwd(), "uploads", id);
|
||||
await mkdir(uploadDir, { recursive: true });
|
||||
|
||||
const uploadId = randomUUID();
|
||||
const safeFilename = `${uploadId}-${file.name.replace(/[^a-zA-Z0-9._-]/g, "_")}`;
|
||||
const storedPath = resolve(uploadDir, safeFilename);
|
||||
|
||||
const buffer = Buffer.from(await file.arrayBuffer());
|
||||
await writeFile(storedPath, buffer);
|
||||
|
||||
let extractedText: string | null = null;
|
||||
try {
|
||||
extractedText = await parsePdf(buffer);
|
||||
} catch {
|
||||
// non-fatal
|
||||
}
|
||||
|
||||
const detectedType = await detectUploadType(file.name, extractedText ?? "");
|
||||
|
||||
await db.insert(uploads).values({
|
||||
id: uploadId,
|
||||
courseId: id,
|
||||
filename: file.name,
|
||||
type: detectedType,
|
||||
storedPath,
|
||||
extractedText,
|
||||
});
|
||||
|
||||
return { uploadId, filename: file.name, type: detectedType };
|
||||
});
|
||||
@@ -0,0 +1,35 @@
|
||||
import { db } from "../../db/index";
|
||||
import { courses, topics, userProgress } from "../../db/schema";
|
||||
import { eq, and } from "drizzle-orm";
|
||||
|
||||
export default defineEventHandler(async () => {
|
||||
const allCourses = await db.query.courses.findMany({
|
||||
orderBy: (c, { desc }) => desc(c.createdAt),
|
||||
});
|
||||
|
||||
const result = [];
|
||||
|
||||
for (const course of allCourses) {
|
||||
const topicRows = await db.query.topics.findMany({
|
||||
where: eq(topics.courseId, course.id),
|
||||
});
|
||||
|
||||
const topicCount = topicRows.length;
|
||||
|
||||
let completedCount = 0;
|
||||
if (topicCount > 0) {
|
||||
const progressRows = await db.query.userProgress.findMany({
|
||||
where: eq(userProgress.courseId, course.id),
|
||||
});
|
||||
completedCount = progressRows.filter((p) => p.lessonComplete).length;
|
||||
}
|
||||
|
||||
result.push({
|
||||
...course,
|
||||
topicCount,
|
||||
completedCount,
|
||||
});
|
||||
}
|
||||
|
||||
return result;
|
||||
});
|
||||
@@ -0,0 +1,16 @@
|
||||
import { db } from "../../db/index";
|
||||
import { courses } from "../../db/schema";
|
||||
import { randomUUID } from "crypto";
|
||||
|
||||
export default defineEventHandler(async () => {
|
||||
const id = randomUUID();
|
||||
|
||||
await db.insert(courses).values({
|
||||
id,
|
||||
title: "Untitled Course",
|
||||
subject: "Unknown",
|
||||
status: "processing",
|
||||
});
|
||||
|
||||
return { courseId: id };
|
||||
});
|
||||
@@ -0,0 +1,18 @@
|
||||
import { db } from "../../../db/index";
|
||||
import { topics, lessons } from "../../../db/schema";
|
||||
import { eq } from "drizzle-orm";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const id = getRouterParam(event, "id")!;
|
||||
|
||||
const topic = await db.query.topics.findFirst({ where: eq(topics.id, id) });
|
||||
if (!topic) throw createError({ statusCode: 404, message: "Topic not found" });
|
||||
|
||||
const lesson = await db.query.lessons.findFirst({ where: eq(lessons.topicId, id) });
|
||||
if (!lesson) throw createError({ statusCode: 404, message: "Lesson not yet generated" });
|
||||
|
||||
return {
|
||||
...lesson,
|
||||
content: JSON.parse(lesson.content),
|
||||
};
|
||||
});
|
||||
@@ -0,0 +1,46 @@
|
||||
import { db } from "../../../db/index";
|
||||
import { topics, userProgress } from "../../../db/schema";
|
||||
import { eq, and } from "drizzle-orm";
|
||||
import { randomUUID } from "crypto";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const id = getRouterParam(event, "id")!;
|
||||
|
||||
const topic = await db.query.topics.findFirst({ where: eq(topics.id, id) });
|
||||
if (!topic) throw createError({ statusCode: 404, message: "Topic not found" });
|
||||
|
||||
const body = await readBody(event);
|
||||
const { lessonComplete, quizScore, tookBranches, branchCount } = body ?? {};
|
||||
|
||||
const existing = await db.query.userProgress.findFirst({
|
||||
where: and(
|
||||
eq(userProgress.topicId, id),
|
||||
eq(userProgress.courseId, topic.courseId)
|
||||
),
|
||||
});
|
||||
|
||||
if (existing) {
|
||||
await db
|
||||
.update(userProgress)
|
||||
.set({
|
||||
lessonComplete: lessonComplete ?? existing.lessonComplete,
|
||||
quizScore: quizScore ?? existing.quizScore,
|
||||
tookBranches: tookBranches ?? existing.tookBranches,
|
||||
branchCount: branchCount ?? existing.branchCount,
|
||||
updatedAt: new Date().toISOString(),
|
||||
})
|
||||
.where(eq(userProgress.id, existing.id));
|
||||
} else {
|
||||
await db.insert(userProgress).values({
|
||||
id: randomUUID(),
|
||||
courseId: topic.courseId,
|
||||
topicId: id,
|
||||
lessonComplete: lessonComplete ?? false,
|
||||
quizScore: quizScore ?? null,
|
||||
tookBranches: tookBranches ?? false,
|
||||
branchCount: branchCount ?? 0,
|
||||
});
|
||||
}
|
||||
|
||||
return { ok: true };
|
||||
});
|
||||
@@ -0,0 +1,21 @@
|
||||
import { db } from "../../../db/index";
|
||||
import { topics, quizQuestions } from "../../../db/schema";
|
||||
import { eq } from "drizzle-orm";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const id = getRouterParam(event, "id")!;
|
||||
|
||||
const topic = await db.query.topics.findFirst({ where: eq(topics.id, id) });
|
||||
if (!topic) throw createError({ statusCode: 404, message: "Topic not found" });
|
||||
|
||||
const questions = await db.query.quizQuestions.findMany({
|
||||
where: eq(quizQuestions.topicId, id),
|
||||
});
|
||||
|
||||
if (questions.length === 0) throw createError({ statusCode: 404, message: "Quiz not yet generated" });
|
||||
|
||||
return questions.map((q) => ({
|
||||
...q,
|
||||
options: q.options ? JSON.parse(q.options) : null,
|
||||
}));
|
||||
});
|
||||
@@ -0,0 +1,25 @@
|
||||
import { db } from "../../db/index";
|
||||
import { uploads } from "../../db/schema";
|
||||
import { eq } from "drizzle-orm";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const id = getRouterParam(event, "id")!;
|
||||
|
||||
const upload = await db.query.uploads.findFirst({ where: eq(uploads.id, id) });
|
||||
if (!upload) throw createError({ statusCode: 404, message: "Upload not found" });
|
||||
|
||||
const body = await readBody(event);
|
||||
const { type } = body ?? {};
|
||||
|
||||
const valid = ["slides", "past_paper", "lab_worksheet"];
|
||||
if (!type || !valid.includes(type)) {
|
||||
throw createError({ statusCode: 400, message: "type must be slides, past_paper, or lab_worksheet" });
|
||||
}
|
||||
|
||||
await db
|
||||
.update(uploads)
|
||||
.set({ type: type as "slides" | "past_paper" | "lab_worksheet" })
|
||||
.where(eq(uploads.id, id));
|
||||
|
||||
return { ok: true, type };
|
||||
});
|
||||
@@ -0,0 +1,12 @@
|
||||
import { drizzle } from "drizzle-orm/better-sqlite3";
|
||||
import Database from "better-sqlite3";
|
||||
import * as schema from "./schema";
|
||||
import { resolve } from "path";
|
||||
|
||||
const dbPath = process.env.DATABASE_PATH || resolve(process.cwd(), "revisione.db");
|
||||
|
||||
const sqlite = new Database(dbPath);
|
||||
sqlite.pragma("journal_mode = WAL");
|
||||
sqlite.pragma("foreign_keys = ON");
|
||||
|
||||
export const db = drizzle(sqlite, { schema });
|
||||
@@ -0,0 +1,8 @@
|
||||
import { migrate } from "drizzle-orm/better-sqlite3/migrator";
|
||||
import { db } from "./index";
|
||||
import { resolve } from "path";
|
||||
|
||||
const migrationsFolder = resolve(process.cwd(), "drizzle");
|
||||
|
||||
migrate(db, { migrationsFolder });
|
||||
console.log("Migrations complete");
|
||||
@@ -0,0 +1,92 @@
|
||||
import { sqliteTable, text, integer, real } from "drizzle-orm/sqlite-core";
|
||||
import { sql } from "drizzle-orm";
|
||||
|
||||
export const courses = sqliteTable("courses", {
|
||||
id: text("id").primaryKey(),
|
||||
title: text("title").notNull(),
|
||||
subject: text("subject").notNull(),
|
||||
status: text("status", { enum: ["processing", "ready", "error"] })
|
||||
.notNull()
|
||||
.default("processing"),
|
||||
stage: text("stage", {
|
||||
enum: ["parsing_pdfs", "analysing_sources", "building_curriculum", "finalising", "ready", "error"],
|
||||
}),
|
||||
costAI: real("cost_ai").default(0),
|
||||
costAudio: real("cost_audio").default(0),
|
||||
auditReport: text("audit_report"),
|
||||
auditScore: integer("audit_score"),
|
||||
organisation: text("organisation"),
|
||||
createdAt: text("created_at")
|
||||
.notNull()
|
||||
.default(sql`(datetime('now'))`),
|
||||
});
|
||||
|
||||
export const uploads = sqliteTable("uploads", {
|
||||
id: text("id").primaryKey(),
|
||||
courseId: text("course_id")
|
||||
.notNull()
|
||||
.references(() => courses.id),
|
||||
filename: text("filename").notNull(),
|
||||
type: text("type", { enum: ["slides", "past_paper", "lab_worksheet"] }).notNull(),
|
||||
storedPath: text("stored_path").notNull(),
|
||||
extractedText: text("extracted_text"),
|
||||
createdAt: text("created_at")
|
||||
.notNull()
|
||||
.default(sql`(datetime('now'))`),
|
||||
});
|
||||
|
||||
export const topics = sqliteTable("topics", {
|
||||
id: text("id").primaryKey(),
|
||||
courseId: text("course_id")
|
||||
.notNull()
|
||||
.references(() => courses.id),
|
||||
title: text("title").notNull(),
|
||||
description: text("description").notNull(),
|
||||
order: integer("order").notNull(),
|
||||
prerequisiteTopicIds: text("prerequisite_topic_ids").notNull().default("[]"),
|
||||
difficulty: integer("difficulty").notNull().default(1),
|
||||
relevantFiles: text("relevant_files"),
|
||||
});
|
||||
|
||||
export const lessons = sqliteTable("lessons", {
|
||||
id: text("id").primaryKey(),
|
||||
topicId: text("topic_id")
|
||||
.notNull()
|
||||
.references(() => topics.id),
|
||||
content: text("content").notNull(),
|
||||
ttsProvider: text("tts_provider"),
|
||||
createdAt: text("created_at")
|
||||
.notNull()
|
||||
.default(sql`(datetime('now'))`),
|
||||
});
|
||||
|
||||
export const quizQuestions = sqliteTable("quiz_questions", {
|
||||
id: text("id").primaryKey(),
|
||||
topicId: text("topic_id")
|
||||
.notNull()
|
||||
.references(() => topics.id),
|
||||
question: text("question").notNull(),
|
||||
type: text("type", { enum: ["mcq", "short_answer", "worked"] }).notNull(),
|
||||
options: text("options"),
|
||||
answer: text("answer").notNull(),
|
||||
explanation: text("explanation").notNull(),
|
||||
});
|
||||
|
||||
export const userProgress = sqliteTable("user_progress", {
|
||||
id: text("id").primaryKey(),
|
||||
courseId: text("course_id")
|
||||
.notNull()
|
||||
.references(() => courses.id),
|
||||
topicId: text("topic_id")
|
||||
.notNull()
|
||||
.references(() => topics.id),
|
||||
lessonComplete: integer("lesson_complete", { mode: "boolean" })
|
||||
.notNull()
|
||||
.default(false),
|
||||
quizScore: integer("quiz_score"),
|
||||
tookBranches: integer("took_branches", { mode: "boolean" }).notNull().default(false),
|
||||
branchCount: integer("branch_count").notNull().default(0),
|
||||
updatedAt: text("updated_at")
|
||||
.notNull()
|
||||
.default(sql`(datetime('now'))`),
|
||||
});
|
||||
@@ -0,0 +1,7 @@
|
||||
import { generateLabels } from "../utils/generateLabels";
|
||||
|
||||
export default defineNitroPlugin(async () => {
|
||||
generateLabels().catch((err) => {
|
||||
console.error("[labels] startup generation failed:", err?.message ?? err);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,9 @@
|
||||
import { migrate } from "drizzle-orm/better-sqlite3/migrator";
|
||||
import { db } from "../db/index";
|
||||
import { resolve } from "path";
|
||||
|
||||
export default defineNitroPlugin(async () => {
|
||||
const migrationsFolder = resolve(process.cwd(), "drizzle");
|
||||
migrate(db, { migrationsFolder });
|
||||
console.log("[revisione] db migrations applied");
|
||||
});
|
||||
@@ -0,0 +1,29 @@
|
||||
import { db } from "../db/index";
|
||||
import { courses } from "../db/schema";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { generateCourseInBackground } from "../utils/generateCourse";
|
||||
|
||||
export default defineNitroPlugin(async () => {
|
||||
console.log("[revisione] resumeGeneration plugin started");
|
||||
|
||||
try {
|
||||
const stuck = await db.query.courses.findMany({
|
||||
where: eq(courses.status, "processing"),
|
||||
});
|
||||
|
||||
console.log(`[revisione] query complete — found ${stuck.length} course(s)`);
|
||||
|
||||
if (stuck.length === 0) return;
|
||||
|
||||
console.log(`[revisione] found ${stuck.length} course(s) stuck in processing — resuming`);
|
||||
|
||||
for (const course of stuck) {
|
||||
console.log(`[revisione] resuming "${course.title}" (${course.id.slice(0, 8)})`);
|
||||
generateCourseInBackground(course.id).catch((err) => {
|
||||
console.error(`[revisione] background generation threw:`, err?.message ?? err);
|
||||
});
|
||||
}
|
||||
} catch (err: any) {
|
||||
console.error("[revisione] resumeGeneration plugin error:", err?.message ?? err);
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,196 @@
|
||||
import { db } from "../db/index";
|
||||
import { courses, uploads, topics, lessons, quizQuestions } from "../db/schema";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { askAI } from "./openrouter";
|
||||
|
||||
function parseJSON<T>(raw: string): T {
|
||||
try {
|
||||
return JSON.parse(raw);
|
||||
} catch {
|
||||
const cleaned = raw.replace(/^```[a-z]*\n?/i, "").replace(/\n?```$/i, "").trim();
|
||||
return JSON.parse(cleaned);
|
||||
}
|
||||
}
|
||||
|
||||
function renderSteps(steps: any[]): string {
|
||||
return steps.map((s: any, i: number) => {
|
||||
const lines: string[] = [` Step ${i + 1} [${s.type.toUpperCase()}]`];
|
||||
|
||||
if (s.title) lines.push(` Title: ${s.title}`);
|
||||
if (s.body) lines.push(` Body: ${s.body}`);
|
||||
if (s.callout) lines.push(` Callout: ${s.callout}`);
|
||||
if (Array.isArray(s.bullets)) {
|
||||
lines.push(` Bullets:\n${s.bullets.map((b: string) => ` - ${b}`).join("\n")}`);
|
||||
}
|
||||
if (Array.isArray(s.options)) {
|
||||
lines.push(` Options:\n${s.options.map((o: string, oi: number) => ` ${["A","B","C","D"][oi]}: ${o}`).join("\n")}`);
|
||||
lines.push(` Answer: ${s.answer}`);
|
||||
if (s.explanation) lines.push(` Explanation: ${s.explanation}`);
|
||||
}
|
||||
|
||||
// include branches if present
|
||||
if (s.branches && typeof s.branches === "object") {
|
||||
for (const [wrongOpt, branch] of Object.entries(s.branches as Record<string, any>)) {
|
||||
lines.push(` Branch for wrong answer "${wrongOpt}":`);
|
||||
for (const bs of branch.steps ?? []) {
|
||||
lines.push(` [BRANCH CONCEPT] ${bs.title ?? ""}: ${bs.body ?? ""}`);
|
||||
}
|
||||
if (branch.confirmQuestion) {
|
||||
const cq = branch.confirmQuestion;
|
||||
lines.push(` [CONFIRM QUESTION] ${cq.body}`);
|
||||
if (Array.isArray(cq.options)) {
|
||||
lines.push(` Options: ${cq.options.join(" | ")}`);
|
||||
}
|
||||
lines.push(` Answer: ${cq.answer}`);
|
||||
}
|
||||
if (branch.hardStop) {
|
||||
lines.push(` [HARD STOP] ${branch.hardStop}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return lines.join("\n");
|
||||
}).join("\n");
|
||||
}
|
||||
|
||||
export async function auditCourse(courseId: string): Promise<void> {
|
||||
console.log(`[audit:${courseId.slice(0, 8)}] starting post-generation audit`);
|
||||
|
||||
try {
|
||||
const course = await db.query.courses.findFirst({ where: eq(courses.id, courseId) });
|
||||
if (!course) throw new Error("Course not found");
|
||||
|
||||
const uploadRows = await db.query.uploads.findMany({ where: eq(uploads.courseId, courseId) });
|
||||
const topicRows = await db.query.topics.findMany({
|
||||
where: eq(topics.courseId, courseId),
|
||||
orderBy: (t, { asc }) => asc(t.order),
|
||||
});
|
||||
|
||||
const lessonMap: Record<string, any> = {};
|
||||
for (const topic of topicRows) {
|
||||
const lesson = await db.query.lessons.findFirst({ where: eq(lessons.topicId, topic.id) });
|
||||
if (lesson) lessonMap[topic.id] = lesson;
|
||||
}
|
||||
|
||||
const quizMap: Record<string, any[]> = {};
|
||||
for (const topic of topicRows) {
|
||||
quizMap[topic.id] = await db.query.quizQuestions.findMany({ where: eq(quizQuestions.topicId, topic.id) });
|
||||
}
|
||||
|
||||
// full source text — no truncation
|
||||
const primaryParts: string[] = [];
|
||||
const secondaryParts: string[] = [];
|
||||
for (const u of uploadRows) {
|
||||
if (!u.extractedText) continue;
|
||||
if (u.type === "past_paper" || u.type === "lab_worksheet") {
|
||||
primaryParts.push(`--- ${u.filename} (${u.type}) ---\n${u.extractedText}`);
|
||||
} else {
|
||||
secondaryParts.push(`--- ${u.filename} (${u.type}) ---\n${u.extractedText}`);
|
||||
}
|
||||
}
|
||||
|
||||
const primaryText = primaryParts.join("\n\n") || "(none)";
|
||||
const secondaryText = secondaryParts.join("\n\n") || "(none)";
|
||||
|
||||
// topics block
|
||||
const topicsBlock = topicRows
|
||||
.map((t) => `${t.order + 1}. ${t.title} (difficulty ${t.difficulty}/5)\n ${t.description}`)
|
||||
.join("\n");
|
||||
|
||||
// full lesson block — every step, every branch, every confirm question, every hard stop
|
||||
const lessonsBlock = topicRows.map((t) => {
|
||||
const lesson = lessonMap[t.id];
|
||||
if (!lesson) return `Topic: ${t.title}\n (no lesson generated)`;
|
||||
let content: any = {};
|
||||
try { content = JSON.parse(lesson.content); } catch {}
|
||||
const keyConcepts = (content.keyConcepts ?? []).join(", ");
|
||||
const steps = renderSteps(content.steps ?? []);
|
||||
return `Topic: ${t.title}\nKey concepts: ${keyConcepts}\n${steps}`;
|
||||
}).join("\n\n---\n\n");
|
||||
|
||||
// full quiz block — question, all options, answer, explanation
|
||||
const quizBlock = topicRows.map((t) => {
|
||||
const qs = quizMap[t.id] ?? [];
|
||||
if (qs.length === 0) return `Topic: ${t.title}\n (no quiz questions)`;
|
||||
const qList = qs.map((q: any, i: number) => {
|
||||
const lines = [` Q${i + 1} [${q.type}]: ${q.question}`];
|
||||
if (q.options) {
|
||||
try {
|
||||
const opts: string[] = JSON.parse(q.options);
|
||||
lines.push(` Options: ${opts.join(" | ")}`);
|
||||
} catch {}
|
||||
}
|
||||
lines.push(` Answer: ${q.answer}`);
|
||||
lines.push(` Explanation: ${q.explanation}`);
|
||||
return lines.join("\n");
|
||||
}).join("\n");
|
||||
return `Topic: ${t.title}\n${qList}`;
|
||||
}).join("\n\n---\n\n");
|
||||
|
||||
const prompt = `You are auditing an AI-generated course against a single standard: can a student who completes this course answer every question in every past paper and lab worksheet provided?
|
||||
|
||||
This is a pass/fail standard, not a score. However, also provide a score for tracking progress toward that standard.
|
||||
|
||||
PRIMARY SOURCES (past papers + lab worksheets):
|
||||
${primaryText}
|
||||
|
||||
SECONDARY SOURCES (lecture slides):
|
||||
${secondaryText}
|
||||
|
||||
THE GENERATED COURSE:
|
||||
${course.title}
|
||||
|
||||
TOPICS COVERED (${topicRows.length} topics):
|
||||
${topicsBlock}
|
||||
|
||||
LESSONS GENERATED:
|
||||
${lessonsBlock}
|
||||
|
||||
QUIZ QUESTIONS:
|
||||
${quizBlock}
|
||||
|
||||
YOUR AUDIT PROCESS:
|
||||
1. Go through every past paper question one by one. For each question, determine: could a student who completed this course answer it fully and correctly? If not, why not — what is missing or underdeveloped?
|
||||
2. Go through every lab worksheet task one by one. Same question.
|
||||
3. Go through every concept in the lecture slides. Is it covered in the course to a level of full understanding?
|
||||
4. Identify every gap — topics missing, algorithms not taught to implementation level, calculations not drilled, pseudocode not covered, procedures not walked through.
|
||||
|
||||
Return only valid JSON, no markdown:
|
||||
{
|
||||
"overallScore": 0-100,
|
||||
"passesStandard": true/false,
|
||||
"examReadiness": "honest plain English verdict",
|
||||
"unansweredPaperQuestions": [
|
||||
{ "source": "2023 Q2(a)", "question": "brief description", "gap": "what the course fails to teach that would be needed" }
|
||||
],
|
||||
"coverageAnalysis": { "totalExaminedTopics": 0, "coveredTopics": 0, "coveragePercent": 0 },
|
||||
"gaps": [
|
||||
{ "topic": "...", "severity": "high/medium/low", "appearsInSources": "...", "courseCoverage": "..." }
|
||||
],
|
||||
"lessonQuality": [
|
||||
{ "topicTitle": "...", "score": 0-100, "notes": "..." }
|
||||
],
|
||||
"recommendations": ["..."]
|
||||
}`;
|
||||
|
||||
const config = useRuntimeConfig();
|
||||
const evaluatorModel = (config as any).openrouterEvaluatorModel;
|
||||
|
||||
const result = await askAI(
|
||||
[{ role: "user", content: prompt }],
|
||||
{ maxRetries: 2, maxTokens: 4000, ...(evaluatorModel ? { model: evaluatorModel } : {}) }
|
||||
);
|
||||
const report = parseJSON<{ overallScore: number }>(result.text);
|
||||
|
||||
await db.update(courses)
|
||||
.set({ auditReport: result.text, auditScore: report.overallScore ?? null })
|
||||
.where(eq(courses.id, courseId));
|
||||
|
||||
console.log(`[audit:${courseId.slice(0, 8)}] ✓ audit complete — score: ${report.overallScore ?? "?"}/100`);
|
||||
} catch (err: any) {
|
||||
console.error(`[audit:${courseId.slice(0, 8)}] ✗ audit failed: ${err?.message ?? err}`);
|
||||
await db.update(courses)
|
||||
.set({ auditReport: null })
|
||||
.where(eq(courses.id, courseId));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
import { askAI } from "./openrouter";
|
||||
import { useRuntimeConfig } from "#imports";
|
||||
|
||||
type UploadType = "slides" | "past_paper" | "lab_worksheet";
|
||||
|
||||
const PAST_PAPER_RE = /exam|past[\s_-]?paper|specimen|mock|resit|20(1[9]|2[0-6])/i;
|
||||
const LAB_RE = /lab|worksheet|practical|experiment|\bprac\b/i;
|
||||
const SLIDES_RE = /lecture|slides|\blec\b|week\d|topic\d|chapter/i;
|
||||
|
||||
export async function detectUploadType(filename: string, extractedText: string): Promise<UploadType> {
|
||||
const name = filename.toLowerCase();
|
||||
|
||||
// layer 1 — filename heuristics
|
||||
if (PAST_PAPER_RE.test(name)) return "past_paper";
|
||||
if (LAB_RE.test(name)) return "lab_worksheet";
|
||||
if (SLIDES_RE.test(name)) return "slides";
|
||||
|
||||
// layer 2 — AI classification
|
||||
const opening = extractedText.slice(0, 1500);
|
||||
|
||||
const prompt = `You are classifying a university document. Based on the filename and the opening text, classify it as exactly one of: past_paper, lab_worksheet, or slides.
|
||||
|
||||
past_paper: an exam or test with questions students must answer under exam conditions
|
||||
lab_worksheet: a practical worksheet with experiments, procedures, or guided tasks
|
||||
slides: lecture slides or course notes presenting theory and concepts
|
||||
|
||||
Filename: ${filename}
|
||||
Opening text: ${opening}
|
||||
|
||||
Respond with only one of: past_paper, lab_worksheet, slides`;
|
||||
|
||||
try {
|
||||
const config = useRuntimeConfig();
|
||||
const classificationModel = (config as any).openrouterClassificationModel ?? "anthropic/claude-haiku-4-5";
|
||||
|
||||
const { text } = await askAI(
|
||||
[{ role: "user", content: prompt }],
|
||||
{ model: classificationModel, temperature: 0 }
|
||||
);
|
||||
|
||||
const result = text.trim().toLowerCase() as UploadType;
|
||||
if (result === "past_paper" || result === "lab_worksheet" || result === "slides") {
|
||||
return result;
|
||||
}
|
||||
} catch {
|
||||
// fall through to default
|
||||
}
|
||||
|
||||
return "slides";
|
||||
}
|
||||
@@ -0,0 +1,732 @@
|
||||
import { db } from "../db/index";
|
||||
import { courses, uploads, topics, lessons, quizQuestions } from "../db/schema";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { randomUUID } from "crypto";
|
||||
import { askAI } from "./openrouter";
|
||||
import { auditCourse } from "./auditCourse";
|
||||
import { generateStepTTS, generateQuestionTTS, generateOptionTTS, generateTTSToPath } from "./generateTTS";
|
||||
|
||||
type Stage = "parsing_pdfs" | "analysing_sources" | "building_curriculum" | "finalising" | "ready" | "error";
|
||||
|
||||
interface CourseContext {
|
||||
courseTitle: string;
|
||||
subject: string;
|
||||
topicsInOrder: { order: number; title: string; description: string }[];
|
||||
completedLessons: {
|
||||
order: number;
|
||||
title: string;
|
||||
keyConcepts: string[];
|
||||
analogiesUsed: string[];
|
||||
}[];
|
||||
}
|
||||
|
||||
function log(courseId: string, msg: string) {
|
||||
const short = courseId.slice(0, 8);
|
||||
console.log(`[revisione:${short}] ${msg}`);
|
||||
}
|
||||
|
||||
async function setStage(courseId: string, stage: Stage) {
|
||||
await db.update(courses).set({ stage }).where(eq(courses.id, courseId));
|
||||
log(courseId, `stage → ${stage}`);
|
||||
}
|
||||
|
||||
function parseJSON<T>(raw: string): T {
|
||||
try {
|
||||
return JSON.parse(raw);
|
||||
} catch {
|
||||
const cleaned = raw.replace(/^```[a-z]*\n?/i, "").replace(/\n?```$/i, "").trim();
|
||||
return JSON.parse(cleaned);
|
||||
}
|
||||
}
|
||||
|
||||
// returns mutated steps array with audioPath/audioChunks embedded, and total audio cost
|
||||
async function generateLessonAudio(
|
||||
steps: any[],
|
||||
lessonId: string,
|
||||
courseId: string
|
||||
): Promise<{ steps: any[]; cost: number }> {
|
||||
let cost = 0;
|
||||
|
||||
for (let si = 0; si < steps.length; si++) {
|
||||
const step = steps[si];
|
||||
|
||||
if (step.type === "concept" || step.type === "example") {
|
||||
const text = [step.body, step.callout].filter(Boolean).join(" ");
|
||||
if (!text.trim()) continue;
|
||||
|
||||
const result = await generateStepTTS(text, lessonId, si);
|
||||
if (result) {
|
||||
step.audioPath = result.audioPath;
|
||||
step.audioChunks = result.audioChunks;
|
||||
cost += result.cost;
|
||||
}
|
||||
} else if (step.type === "summary") {
|
||||
const text = Array.isArray(step.bullets) ? step.bullets.join(". ") : "";
|
||||
if (!text.trim()) continue;
|
||||
|
||||
const result = await generateStepTTS(text, lessonId, si);
|
||||
if (result) {
|
||||
step.audioPath = result.audioPath;
|
||||
step.audioChunks = result.audioChunks;
|
||||
cost += result.cost;
|
||||
}
|
||||
} else if (step.type === "question") {
|
||||
// question narration
|
||||
if (step.body?.trim()) {
|
||||
const qResult = await generateQuestionTTS(step.body, lessonId, si);
|
||||
if (qResult) {
|
||||
step.questionAudioPath = qResult.audioPath;
|
||||
step.questionAudioChunks = qResult.audioChunks;
|
||||
cost += qResult.cost;
|
||||
}
|
||||
}
|
||||
|
||||
// per-option audio
|
||||
if (Array.isArray(step.options)) {
|
||||
step.optionAudioPaths = [];
|
||||
for (let oi = 0; oi < step.options.length; oi++) {
|
||||
const optText = step.options[oi];
|
||||
if (optText?.trim()) {
|
||||
const oResult = await generateOptionTTS(optText, lessonId, si, oi);
|
||||
if (oResult) {
|
||||
step.optionAudioPaths[oi] = oResult.audioPath;
|
||||
cost += oResult.cost;
|
||||
} else {
|
||||
step.optionAudioPaths[oi] = null;
|
||||
}
|
||||
} else {
|
||||
step.optionAudioPaths[oi] = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
log(courseId, ` step ${si} (${step.type}) TTS done`);
|
||||
}
|
||||
|
||||
return { steps, cost };
|
||||
}
|
||||
|
||||
export async function generateCourseInBackground(courseId: string) {
|
||||
try {
|
||||
const course = await db.query.courses.findFirst({ where: eq(courses.id, courseId) });
|
||||
if (!course) throw new Error(`Course ${courseId} not found`);
|
||||
|
||||
const costs = { ai: 0, audio: 0 };
|
||||
|
||||
log(courseId, `starting generation for "${course.title}"`);
|
||||
|
||||
// ── STEP 1 — load uploads ───────────────────────────────────────────────
|
||||
await setStage(courseId, "parsing_pdfs");
|
||||
|
||||
const uploadRows = await db.query.uploads.findMany({
|
||||
where: eq(uploads.courseId, courseId),
|
||||
});
|
||||
|
||||
if (uploadRows.length === 0) throw new Error("No uploads found for this course");
|
||||
|
||||
log(courseId, `found ${uploadRows.length} upload(s)`);
|
||||
|
||||
const primaryParts: string[] = [];
|
||||
const secondaryParts: string[] = [];
|
||||
|
||||
for (const upload of uploadRows) {
|
||||
if (!upload.extractedText) {
|
||||
log(courseId, ` skipping "${upload.filename}" — no extracted text`);
|
||||
continue;
|
||||
}
|
||||
const snippet = `--- ${upload.filename} ---\n${upload.extractedText}`;
|
||||
log(courseId, ` loaded "${upload.filename}" (${upload.type}, ${upload.extractedText.length} chars)`);
|
||||
|
||||
if (upload.type === "past_paper" || upload.type === "lab_worksheet") {
|
||||
primaryParts.push(snippet);
|
||||
} else {
|
||||
secondaryParts.push(snippet);
|
||||
}
|
||||
}
|
||||
|
||||
log(courseId, `source split — primary: ${primaryParts.length}, secondary: ${secondaryParts.length}`);
|
||||
|
||||
// ── STEP 1b — infer title, subject, and topic count ────────────────────
|
||||
await setStage(courseId, "analysing_sources");
|
||||
|
||||
const allExtracted = [
|
||||
...primaryParts.join("\n\n"),
|
||||
...secondaryParts.join("\n\n"),
|
||||
].join("\n\n");
|
||||
|
||||
log(courseId, "inferring course title and subject from documents…");
|
||||
|
||||
const inferenceResult = await askAI([{
|
||||
role: "user",
|
||||
content: `You are analysing a set of university course documents including lecture slides, past exam papers, and lab worksheets.
|
||||
|
||||
Based on the content, return a JSON object with:
|
||||
- "title": a concise course name (e.g. "Computer Vision", "Thermodynamics", "Microeconomics")
|
||||
- "subject": the broader academic discipline (e.g. "Computer Science", "Physics", "Economics")
|
||||
- "organisation": the university or institution these materials are from (e.g. "University of Essex", "Imperial College London"). Infer this from headers, exam paper footers, logos described in text, or module codes. Return null if you genuinely cannot determine it.
|
||||
|
||||
Return only valid JSON, no markdown.
|
||||
|
||||
DOCUMENTS:
|
||||
${allExtracted}`,
|
||||
}]);
|
||||
costs.ai += inferenceResult.cost;
|
||||
|
||||
let inferredMeta: { title: string; subject: string; organisation?: string | null } = {
|
||||
title: course.title,
|
||||
subject: course.subject,
|
||||
};
|
||||
try {
|
||||
inferredMeta = parseJSON(inferenceResult.text);
|
||||
} catch {
|
||||
log(courseId, "inference parse failed, using defaults");
|
||||
}
|
||||
|
||||
log(courseId, `inferred → title: "${inferredMeta.title}", subject: "${inferredMeta.subject}"`);
|
||||
|
||||
await db.update(courses)
|
||||
.set({
|
||||
title: inferredMeta.title,
|
||||
subject: inferredMeta.subject,
|
||||
...(inferredMeta.organisation != null ? { organisation: inferredMeta.organisation } : {}),
|
||||
})
|
||||
.where(eq(courses.id, courseId));
|
||||
|
||||
// ── STEP 2 — generate topic list (skip if topics already saved) ─────────
|
||||
|
||||
let savedTopics = await db.query.topics.findMany({
|
||||
where: eq(topics.courseId, courseId),
|
||||
orderBy: (t, { asc }) => asc(t.order),
|
||||
});
|
||||
|
||||
if (savedTopics.length > 0) {
|
||||
log(courseId, `resuming — found ${savedTopics.length} existing topic(s), skipping curriculum generation`);
|
||||
} else {
|
||||
const primaryText = primaryParts.join("\n\n");
|
||||
const secondaryText = secondaryParts.join("\n\n");
|
||||
|
||||
const availableFilesBlock = uploadRows
|
||||
.map((u) => `- ${u.filename} (${u.type})`)
|
||||
.join("\n");
|
||||
|
||||
const curriculumPrompt = `You are designing a complete revision course from scratch.
|
||||
|
||||
Your only measure of success is this: a student who completes every lesson in this course must be able to:
|
||||
- Answer every question in every past paper provided, including calculation questions, pseudocode questions, diagram questions, and scenario questions
|
||||
- Perform every procedure, method, and algorithm named in the source material — not just describe them, but actually do them
|
||||
- Fully understand every concept present in the source material, with no gaps
|
||||
|
||||
This is a non-negotiable standard. Do not summarise. Do not compress topics together if doing so would leave any gap in the student's ability to answer a past paper question. If meeting this standard requires 50 topics, generate 50 topics. If it requires 8, generate 8. There is no limit in either direction.
|
||||
|
||||
BEFORE generating topics, do the following analysis mentally:
|
||||
1. Read every past paper question carefully. For each question, ask: what does a student need to know and be able to DO to answer this? List every distinct skill, concept, calculation method, algorithm, and procedure required.
|
||||
2. Read every lab worksheet. For each task, ask: what does a student need to know and be able to DO to complete this? Add any new skills, concepts, or procedures to the list.
|
||||
3. Read the lecture slides. Add any concept or topic that appears in the slides but is not yet in the list.
|
||||
4. Now organise the list into topics, ordered from simplest to most complex, such that each topic assumes only the knowledge of topics before it.
|
||||
|
||||
TOPIC REQUIREMENTS:
|
||||
- Every distinct algorithm named in the source material must have at least one dedicated topic that teaches it to implementation level — the student must be able to apply it step by step, not just name it
|
||||
- Every calculation that appears in a past paper must be covered in a topic that teaches the student to perform that exact type of calculation by hand, with worked examples matching the exam style
|
||||
- Every procedure that appears in a lab worksheet must be covered in a topic that teaches the student to carry out that procedure
|
||||
- If a past paper asks for pseudocode, the corresponding topic must teach the student to write that pseudocode
|
||||
- Conceptual understanding alone is never sufficient. Every topic must result in a student who can DO something, not just know something
|
||||
|
||||
AVAILABLE SOURCE FILES (you must reference these exact filenames in relevantFiles):
|
||||
${availableFilesBlock}
|
||||
|
||||
PRIMARY SOURCES (past papers + lab worksheets — these define what the student must be able to do):
|
||||
${primaryText || "(none provided)"}
|
||||
|
||||
SECONDARY SOURCES (lecture slides — use for additional concepts and explanations):
|
||||
${secondaryText || "(none provided)"}
|
||||
|
||||
Return only valid JSON — an array of topics with no markdown:
|
||||
[{ "title": "...", "description": "...", "difficulty": 1-5, "order": 1, "relevantFiles": ["filename.pdf"] }]
|
||||
|
||||
relevantFiles must list only filenames from the AVAILABLE SOURCE FILES list that directly contain content for this topic. Include at minimum the files that have past paper questions or lab tasks this topic must prepare the student for.
|
||||
|
||||
The description must be specific about what the student will be able to DO after completing this topic, not just what it covers.`;
|
||||
|
||||
await setStage(courseId, "building_curriculum");
|
||||
log(courseId, "calling OpenRouter for curriculum…");
|
||||
const curriculumResult = await askAI([{ role: "user", content: curriculumPrompt }]);
|
||||
costs.ai += curriculumResult.cost;
|
||||
const curriculum = parseJSON<{ title: string; description: string; difficulty: number; relevantFiles?: string[] }[]>(curriculumResult.text);
|
||||
|
||||
if (!Array.isArray(curriculum) || curriculum.length === 0) {
|
||||
throw new Error("AI returned an empty curriculum");
|
||||
}
|
||||
|
||||
log(courseId, `curriculum received — ${curriculum.length} topics:`);
|
||||
curriculum.forEach((t, i) => log(courseId, ` ${i + 1}. ${t.title} (difficulty ${t.difficulty})`));
|
||||
|
||||
await setStage(courseId, "finalising");
|
||||
|
||||
for (let i = 0; i < curriculum.length; i++) {
|
||||
const t = curriculum[i];
|
||||
await db.insert(topics).values({
|
||||
id: randomUUID(),
|
||||
courseId,
|
||||
title: t.title,
|
||||
description: t.description,
|
||||
order: i,
|
||||
difficulty: Math.min(5, Math.max(1, t.difficulty ?? 1)),
|
||||
prerequisiteTopicIds: "[]",
|
||||
relevantFiles: JSON.stringify(t.relevantFiles ?? []),
|
||||
});
|
||||
}
|
||||
|
||||
// re-fetch so we have the real DB rows with IDs
|
||||
savedTopics = await db.query.topics.findMany({
|
||||
where: eq(topics.courseId, courseId),
|
||||
orderBy: (t, { asc }) => asc(t.order),
|
||||
});
|
||||
|
||||
log(courseId, `saved ${savedTopics.length} topics to DB`);
|
||||
}
|
||||
|
||||
await setStage(courseId, "finalising");
|
||||
|
||||
// ── STEP 3 — build course context from what's already done ─────────────
|
||||
const courseSubject = inferredMeta?.subject ?? course.subject;
|
||||
const topicListText = savedTopics.map((t) => `${t.order + 1}. ${t.title}`).join("\n");
|
||||
|
||||
const courseContext: CourseContext = {
|
||||
courseTitle: course.title,
|
||||
subject: course.subject,
|
||||
topicsInOrder: savedTopics.map((t) => ({
|
||||
order: t.order,
|
||||
title: t.title,
|
||||
description: t.description,
|
||||
})),
|
||||
completedLessons: [],
|
||||
};
|
||||
|
||||
// ── STEP 4 — generate lessons + quizzes, skipping completed ones ────────
|
||||
for (const topic of savedTopics) {
|
||||
const i = topic.order;
|
||||
const isFirst = i === 0;
|
||||
|
||||
// check what's already generated for this topic
|
||||
const existingLesson = await db.query.lessons.findFirst({
|
||||
where: eq(lessons.topicId, topic.id),
|
||||
});
|
||||
const existingQuiz = await db.query.quizQuestions.findMany({
|
||||
where: eq(quizQuestions.topicId, topic.id),
|
||||
});
|
||||
|
||||
if (existingLesson && existingQuiz.length > 0) {
|
||||
// fully done — just add to context and move on
|
||||
const content = JSON.parse(existingLesson.content) as {
|
||||
keyConcepts?: string[];
|
||||
analogiesUsed?: string[];
|
||||
};
|
||||
courseContext.completedLessons.push({
|
||||
order: i,
|
||||
title: topic.title,
|
||||
keyConcepts: content.keyConcepts ?? [],
|
||||
analogiesUsed: content.analogiesUsed ?? [],
|
||||
});
|
||||
log(courseId, ` [${i + 1}/${savedTopics.length}] "${topic.title}" already complete — skipping`);
|
||||
continue;
|
||||
}
|
||||
|
||||
// build prior knowledge block from what's completed so far
|
||||
let priorKnowledge: string;
|
||||
if (courseContext.completedLessons.length === 0) {
|
||||
priorKnowledge = "This is the very first lesson — assume zero prior knowledge of the subject.";
|
||||
} else {
|
||||
priorKnowledge = courseContext.completedLessons
|
||||
.map((l) => `- ${l.title}: covered concepts [${l.keyConcepts.join(", ")}] using analogies [${l.analogiesUsed.join(", ")}]`)
|
||||
.join("\n");
|
||||
}
|
||||
|
||||
// build source context for this topic from its relevantFiles, falling back to all primary sources
|
||||
const topicRelevantFiles: string[] = (() => {
|
||||
try { return JSON.parse(topic.relevantFiles ?? "[]"); } catch { return []; }
|
||||
})();
|
||||
|
||||
const relevantUploads = topicRelevantFiles.length > 0
|
||||
? uploadRows.filter((u) => topicRelevantFiles.includes(u.filename) && u.extractedText)
|
||||
: uploadRows.filter((u) => (u.type === "past_paper" || u.type === "lab_worksheet") && u.extractedText);
|
||||
|
||||
const primaryTextForLesson = relevantUploads
|
||||
.map((u) => `--- ${u.filename} ---\n${u.extractedText}`)
|
||||
.join("\n\n");
|
||||
|
||||
const secondaryTextForLesson = topicRelevantFiles.length > 0
|
||||
? uploadRows
|
||||
.filter((u) => topicRelevantFiles.includes(u.filename) && u.extractedText && u.type === "slides")
|
||||
.map((u) => `--- ${u.filename} ---\n${u.extractedText}`)
|
||||
.join("\n\n")
|
||||
: secondaryParts.join("\n\n");
|
||||
|
||||
// generate lesson if missing
|
||||
let lessonContent: { keyConcepts: string[]; analogiesUsed: string[]; steps: any[] };
|
||||
|
||||
if (existingLesson) {
|
||||
log(courseId, ` [${i + 1}/${savedTopics.length}] lesson already exists for "${topic.title}", generating quiz only`);
|
||||
lessonContent = JSON.parse(existingLesson.content);
|
||||
} else {
|
||||
const lessonPrompt = `You are writing a lesson for a course on ${courseSubject}.
|
||||
|
||||
YOUR ONLY MEASURE OF SUCCESS:
|
||||
A student who completes this lesson must be able to answer any past paper or lab question that requires knowledge of this topic. That means they must be able to DO the thing, not just understand it. If this topic involves a calculation, they must be able to perform it. If it involves an algorithm, they must be able to apply it step by step. If it involves pseudocode, they must be able to write it. Conceptual understanding alone is never the goal — competence is the goal.
|
||||
|
||||
WHAT THE STUDENT KNOWS:
|
||||
- Basic English, everyday maths (arithmetic, simple algebra, fractions, proportions), and general school-level science
|
||||
- Nothing domain-specific about ${courseSubject} unless it appears below
|
||||
- Everything explicitly taught in previous lessons:
|
||||
|
||||
${isFirst ? `This is the very first lesson. The student knows nothing about this subject yet. Start from absolute zero.` : courseContext.completedLessons.map((l) => `Lesson ${l.order + 1} — ${l.title}: ${l.keyConcepts.join(", ")}`).join("\n")}
|
||||
|
||||
DO NOT use any technical term that does not appear in the above list or is not introduced and explained in the current lesson. This is a hard rule. It applies everywhere — questions, options, callouts, summaries.
|
||||
|
||||
COURSE STRUCTURE:
|
||||
This course has ${savedTopics.length} lessons in this order:
|
||||
${topicListText}
|
||||
|
||||
YOUR CURRENT LESSON: ${topic.title} — ${topic.description}
|
||||
|
||||
SOURCE MATERIAL:
|
||||
The following are the actual source files relevant to this topic — past papers, lab worksheets, and lecture slides. Your lesson must prepare the student to answer every question in these files that relates to this topic:
|
||||
${primaryTextForLesson || "(no primary sources provided)"}
|
||||
${secondaryTextForLesson ? `\nLECTURE SLIDES:\n${secondaryTextForLesson}` : ""}
|
||||
|
||||
TEACHING PHILOSOPHY:
|
||||
|
||||
OPENING:
|
||||
- The very first sentence must make the student curious, smile, or feel something. Never open with a definition, a recap, or a statement of what they are about to learn.
|
||||
- Open with the analogy or human moment immediately.
|
||||
|
||||
ANALOGIES:
|
||||
- Every concept step must open with a concrete real-world analogy before any technical language.
|
||||
- The analogy comes first. The technical idea is revealed through it.
|
||||
- Never repeat an analogy used in a previous lesson.
|
||||
- Analogies must connect to everyday life, not the subject domain.
|
||||
|
||||
BUILDING ON PRIOR KNOWLEDGE:
|
||||
- Freely use terms and concepts from completed lessons without re-explaining them.
|
||||
- Reference prior concepts as bridges: "remember how X worked — this is that same idea applied to Y."
|
||||
|
||||
MATHEMATICS, ALGORITHMS, AND PROCEDURES:
|
||||
- Before any formula, write one sentence in plain English saying what the relationship means intuitively. Vary the phrasing — never use "In plain terms" more than once per lesson.
|
||||
- After introducing a formula or algorithm, immediately show a complete worked example that matches the style of the past paper questions for this topic.
|
||||
- Never show more than one formula per step.
|
||||
- Never introduce a variable without saying in plain English what it represents.
|
||||
- If this topic requires the student to perform a procedure step by step, there must be at least one example step that walks through the complete procedure on a concrete example, showing every step explicitly.
|
||||
- If past papers ask for pseudocode on this topic, there must be a concept or example step that shows the pseudocode and explains each line.
|
||||
|
||||
QUESTIONS:
|
||||
- Every question must be answerable using only what has been explicitly taught in this lesson up to that point, plus concepts from completed lessons.
|
||||
- Questions immediately after the first concept step must be the simplest — their only job is to confirm the student understood the core analogy.
|
||||
- Never ask a student to perform a full calculation in a single question. Use only:
|
||||
(a) PARTIAL WORKING: Show known values and partial working, ask the student to identify the correct next step.
|
||||
(b) INTERPRET THE RESULT: Give the numerical answer, ask what it means in context.
|
||||
(c) SPOT THE ERROR: Show a worked example with a mistake, ask the student to identify what went wrong and why.
|
||||
- Answer options must be short and scannable — under 15 words each.
|
||||
- Wrong answer options must represent genuine conceptual misconceptions, not arithmetic errors.
|
||||
- Never use a technical term in any answer option that has not already been taught.
|
||||
|
||||
RHYTHM AND PACING:
|
||||
- Never place two question steps consecutively without a concept or example step between them.
|
||||
- The lesson must not become more question-heavy in the second half.
|
||||
- A concept or example step must always appear after the final question and before the summary.
|
||||
- Every concept and example step body: 3-4 sentences maximum.
|
||||
- The lesson should feel like it has a rhythm: teach, check, teach, check, show, check, land.
|
||||
|
||||
TONE:
|
||||
- Warm, clear, occasionally witty. The most engaging teacher the student has ever had.
|
||||
- Never dry, never robotic, never formal for formality's sake.
|
||||
- Short sentences. Active voice. Concrete over abstract.
|
||||
|
||||
SUMMARY:
|
||||
- Bullet count must exactly match keyConcepts count.
|
||||
- Each bullet must be a complete thought that makes sense without reading the lesson.
|
||||
- The final bullet must gesture forward — what will this knowledge unlock?
|
||||
|
||||
OUTPUT FORMAT:
|
||||
Return only valid JSON with no markdown fences:
|
||||
{
|
||||
"keyConcepts": ["..."],
|
||||
"analogiesUsed": ["..."],
|
||||
"steps": [
|
||||
{ "type": "concept", "title": "...", "body": "..." },
|
||||
{ "type": "question", "body": "...", "options": ["...", "...", "...", "..."], "answer": "full correct answer text", "explanation": "..." },
|
||||
{ "type": "example", "title": "...", "body": "...", "callout": "..." },
|
||||
{ "type": "question", "body": "...", "options": ["...", "...", "...", "..."], "answer": "full correct answer text", "explanation": "..." },
|
||||
{ "type": "summary", "title": "Key Takeaways", "bullets": ["...", "..."] }
|
||||
]
|
||||
}
|
||||
|
||||
Steps must interleave concept/example and question types — never two questions or two concepts in a row. Minimum 6 steps, maximum 16. Use more steps when the topic requires it to achieve full competence.`;
|
||||
|
||||
log(courseId, ` [${i + 1}/${savedTopics.length}] generating lesson for "${topic.title}"…`);
|
||||
const lessonResult = await askAI([{ role: "user", content: lessonPrompt }]);
|
||||
costs.ai += lessonResult.cost;
|
||||
lessonContent = parseJSON(lessonResult.text);
|
||||
|
||||
const lessonId = randomUUID();
|
||||
|
||||
const ttsProvider = (useRuntimeConfig().ttsProvider as string | undefined)?.toLowerCase() ?? "elevenlabs";
|
||||
|
||||
await db.insert(lessons).values({
|
||||
id: lessonId,
|
||||
topicId: topic.id,
|
||||
content: JSON.stringify(lessonContent),
|
||||
ttsProvider,
|
||||
});
|
||||
|
||||
log(courseId, ` [${i + 1}/${savedTopics.length}] lesson saved — ${lessonContent.steps?.length ?? 0} steps, concepts: [${(lessonContent.keyConcepts ?? []).join(", ")}]`);
|
||||
|
||||
// generate per-step TTS — non-fatal, embeds audio into step objects
|
||||
log(courseId, ` [${i + 1}/${savedTopics.length}] generating per-step TTS for lesson ${lessonId}…`);
|
||||
try {
|
||||
const { steps: stepsWithAudio, cost: audioCost } = await generateLessonAudio(
|
||||
lessonContent.steps as any[],
|
||||
lessonId,
|
||||
courseId
|
||||
);
|
||||
lessonContent.steps = stepsWithAudio;
|
||||
costs.audio += audioCost;
|
||||
|
||||
// update DB with embedded audio
|
||||
await db.update(lessons)
|
||||
.set({ content: JSON.stringify(lessonContent) })
|
||||
.where(eq(lessons.id, lessonId));
|
||||
} catch (err: any) {
|
||||
console.error(`[revisione] TTS generation failed for lesson ${lessonId}: ${err?.message ?? err}`);
|
||||
}
|
||||
|
||||
// ── generate branches for each question step ──────────────────────
|
||||
log(courseId, ` [${i + 1}/${savedTopics.length}] generating branches for lesson ${lessonId}…`);
|
||||
let branchesChanged = false;
|
||||
|
||||
for (let si = 0; si < lessonContent.steps.length; si++) {
|
||||
const step = lessonContent.steps[si] as any;
|
||||
if (step.type !== "question") continue;
|
||||
|
||||
const OPTION_LABELS = ["A", "B", "C", "D"];
|
||||
const optionsText = (step.options as string[])
|
||||
.map((o: string, idx: number) => `${OPTION_LABELS[idx]}: ${o}`)
|
||||
.join("\n");
|
||||
|
||||
const priorBlock = courseContext.completedLessons.length
|
||||
? courseContext.completedLessons
|
||||
.map((l) => `- ${l.title}: [${l.keyConcepts.join(", ")}]`)
|
||||
.join("\n")
|
||||
: "This is the first lesson.";
|
||||
|
||||
const branchPrompt = `You are writing remediation branches for a question in a lesson about ${courseSubject}.
|
||||
|
||||
SOURCE MATERIAL FOR THIS TOPIC:
|
||||
${primaryTextForLesson || "(none)"}
|
||||
|
||||
THE QUESTION:
|
||||
${step.body}
|
||||
|
||||
THE OPTIONS:
|
||||
${optionsText}
|
||||
|
||||
THE CORRECT ANSWER: ${step.answer}
|
||||
|
||||
WHAT THE STUDENT KNOWS SO FAR:
|
||||
${priorBlock}
|
||||
|
||||
YOUR TASK:
|
||||
For each wrong answer option, write a branch that:
|
||||
1. Opens by acknowledging the specific thinking behind that wrong answer — not generically ("that's wrong") but specifically ("It makes sense to think that, because X — but here is where that reasoning breaks down...")
|
||||
2. Contains 1-2 short teaching steps that directly address the specific misconception behind that wrong answer
|
||||
3. Ends with a confirming question that tests whether the student now understands the correct concept
|
||||
4. Includes a warm, honest hard-stop message for if they fail the confirming question too
|
||||
|
||||
BRANCH TEACHING RULES:
|
||||
- Each branch must feel like a patient teacher who has seen this exact mistake before and knows exactly how to fix it
|
||||
- The opening must name the misconception specifically — never say "incorrect" or "wrong" — say "here is what that answer is actually describing..." or "that reasoning would be right if... but in this case..."
|
||||
- Steps must be 2-3 sentences maximum — these are micro-lessons, not full explanations
|
||||
- The confirming question must be simpler than the original question — it tests the core concept only
|
||||
- The confirming question options must be short and scannable — under 12 words each
|
||||
- The hard stop message must be warm, not discouraging — acknowledge that some concepts need more time, tell them specifically what to look up, and invite them back
|
||||
- Never use technical terms that haven't been taught yet
|
||||
|
||||
Return only valid JSON, no markdown. Structure:
|
||||
{
|
||||
"branches": {
|
||||
"{exact text of wrong option}": {
|
||||
"steps": [
|
||||
{ "type": "concept", "title": "...", "body": "..." }
|
||||
],
|
||||
"confirmQuestion": {
|
||||
"body": "...",
|
||||
"options": ["...", "...", "...", "..."],
|
||||
"answer": "full correct answer text",
|
||||
"explanation": "..."
|
||||
},
|
||||
"hardStop": "..."
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Only generate branches for the 3 wrong options. Do not generate a branch for the correct answer.`;
|
||||
|
||||
try {
|
||||
const branchResult = await askAI([{ role: "user", content: branchPrompt }]);
|
||||
costs.ai += branchResult.cost;
|
||||
|
||||
const parsed = parseJSON<{ branches: Record<string, any> }>(branchResult.text);
|
||||
step.branches = parsed.branches ?? {};
|
||||
branchesChanged = true;
|
||||
|
||||
log(courseId, ` step ${si} branches generated — ${Object.keys(step.branches).length} wrong options`);
|
||||
|
||||
// TTS for each branch
|
||||
const wrongOptions = (step.options as string[]).filter((o: string) => o !== step.answer);
|
||||
for (let bi = 0; bi < wrongOptions.length; bi++) {
|
||||
const wrongOpt = wrongOptions[bi];
|
||||
const branch = step.branches[wrongOpt];
|
||||
if (!branch) continue;
|
||||
|
||||
// branch concept steps
|
||||
for (let bsi = 0; bsi < (branch.steps ?? []).length; bsi++) {
|
||||
const bStep = branch.steps[bsi];
|
||||
const text = [bStep.body, bStep.callout].filter(Boolean).join(" ");
|
||||
if (!text.trim()) continue;
|
||||
const r = await generateTTSToPath(text, lessonId, `branch_${si}_${bi}_step_${bsi}.mp3`);
|
||||
if (r) {
|
||||
bStep.audioPath = r.audioPath;
|
||||
bStep.audioChunks = r.audioChunks;
|
||||
costs.audio += r.cost;
|
||||
}
|
||||
}
|
||||
|
||||
// confirm question narration
|
||||
if (branch.confirmQuestion?.body?.trim()) {
|
||||
const r = await generateTTSToPath(branch.confirmQuestion.body, lessonId, `branch_${si}_${bi}_confirm_q.mp3`);
|
||||
if (r) {
|
||||
branch.confirmQuestion.questionAudioPath = r.audioPath;
|
||||
branch.confirmQuestion.questionAudioChunks = r.audioChunks;
|
||||
costs.audio += r.cost;
|
||||
}
|
||||
}
|
||||
|
||||
// confirm question options
|
||||
if (Array.isArray(branch.confirmQuestion?.options)) {
|
||||
branch.confirmQuestion.optionAudioPaths = [];
|
||||
for (let oi = 0; oi < branch.confirmQuestion.options.length; oi++) {
|
||||
const optText = branch.confirmQuestion.options[oi];
|
||||
if (optText?.trim()) {
|
||||
const r = await generateTTSToPath(optText, lessonId, `branch_${si}_${bi}_confirm_opt_${oi}.mp3`);
|
||||
branch.confirmQuestion.optionAudioPaths[oi] = r ? r.audioPath : null;
|
||||
if (r) costs.audio += r.cost;
|
||||
} else {
|
||||
branch.confirmQuestion.optionAudioPaths[oi] = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// hard stop narration
|
||||
if (branch.hardStop?.trim()) {
|
||||
const r = await generateTTSToPath(branch.hardStop, lessonId, `branch_${si}_${bi}_hardstop.mp3`);
|
||||
if (r) {
|
||||
branch.hardStopAudioPath = r.audioPath;
|
||||
costs.audio += r.cost;
|
||||
}
|
||||
}
|
||||
|
||||
log(courseId, ` step ${si} branch ${bi} TTS done`);
|
||||
}
|
||||
} catch (err: any) {
|
||||
console.error(`[revisione] branch gen failed for step ${si} in lesson ${lessonId}: ${err?.message ?? err}`);
|
||||
}
|
||||
}
|
||||
|
||||
if (branchesChanged) {
|
||||
await db.update(lessons)
|
||||
.set({ content: JSON.stringify(lessonContent) })
|
||||
.where(eq(lessons.id, lessonId));
|
||||
log(courseId, ` [${i + 1}/${savedTopics.length}] branches + audio saved to DB`);
|
||||
}
|
||||
}
|
||||
|
||||
courseContext.completedLessons.push({
|
||||
order: i,
|
||||
title: topic.title,
|
||||
keyConcepts: lessonContent.keyConcepts ?? [],
|
||||
analogiesUsed: lessonContent.analogiesUsed ?? [],
|
||||
});
|
||||
|
||||
// generate quiz if missing
|
||||
if (existingQuiz.length > 0) {
|
||||
log(courseId, ` [${i + 1}/${savedTopics.length}] quiz already exists for "${topic.title}" — skipping`);
|
||||
} else {
|
||||
const quizPrompt = `You are an exam question writer for a university course on ${courseSubject}.
|
||||
|
||||
COURSE CONTEXT:
|
||||
The student has just completed a lesson on "${topic.title}" which covered: ${(lessonContent.keyConcepts ?? []).join(", ")}.
|
||||
This is topic ${i + 1} of ${savedTopics.length} — difficulty level: ${topic.difficulty}/5.
|
||||
|
||||
SOURCE MATERIAL FOR THIS TOPIC (use these to match question style, difficulty, and content exactly):
|
||||
${primaryTextForLesson || "(none provided)"}
|
||||
|
||||
Generate 4 quiz questions for this topic. Mix MCQ and short_answer types. For MCQ, provide 4 options labeled A, B, C, D.
|
||||
Match the difficulty level — topic 1 should be very approachable, later topics can be more demanding.
|
||||
|
||||
Respond with ONLY valid JSON array, no markdown fences:
|
||||
[
|
||||
{
|
||||
"question": "...",
|
||||
"type": "mcq",
|
||||
"options": ["A. ...", "B. ...", "C. ...", "D. ..."],
|
||||
"answer": "A",
|
||||
"explanation": "..."
|
||||
},
|
||||
{
|
||||
"question": "...",
|
||||
"type": "short_answer",
|
||||
"options": null,
|
||||
"answer": "...",
|
||||
"explanation": "..."
|
||||
}
|
||||
]`;
|
||||
|
||||
log(courseId, ` [${i + 1}/${savedTopics.length}] generating quiz for "${topic.title}"…`);
|
||||
const quizResult = await askAI([{ role: "user", content: quizPrompt }]);
|
||||
costs.ai += quizResult.cost;
|
||||
const questions = parseJSON<{
|
||||
question: string;
|
||||
type: string;
|
||||
options: string[] | null;
|
||||
answer: string;
|
||||
explanation: string;
|
||||
}[]>(quizResult.text);
|
||||
|
||||
for (const q of questions) {
|
||||
await db.insert(quizQuestions).values({
|
||||
id: randomUUID(),
|
||||
topicId: topic.id,
|
||||
question: q.question,
|
||||
type: q.type as "mcq" | "short_answer" | "worked",
|
||||
options: q.options ? JSON.stringify(q.options) : null,
|
||||
answer: q.answer,
|
||||
explanation: q.explanation,
|
||||
});
|
||||
}
|
||||
|
||||
log(courseId, ` [${i + 1}/${savedTopics.length}] quiz saved — ${questions.length} questions`);
|
||||
}
|
||||
}
|
||||
|
||||
// ── STEP 5 — mark ready ─────────────────────────────────────────────────
|
||||
await db.update(courses)
|
||||
.set({ status: "ready", stage: "ready", costAI: costs.ai, costAudio: costs.audio })
|
||||
.where(eq(courses.id, courseId));
|
||||
log(courseId, `✓ generation complete — ${savedTopics.length} topics | cost: AI $${costs.ai.toFixed(4)}, audio $${costs.audio.toFixed(4)}`);
|
||||
|
||||
// ── STEP 6 — post-generation audit (non-blocking) ───────────────────────
|
||||
await auditCourse(courseId);
|
||||
} catch (err: any) {
|
||||
console.error(`[revisione:${courseId.slice(0, 8)}] ✗ generation failed: ${err?.message ?? err}`);
|
||||
await db.update(courses).set({ status: "error", stage: "error" }).where(eq(courses.id, courseId));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
import { resolve } from "path";
|
||||
import { generateClip, fileExists } from "./generateTTS";
|
||||
|
||||
const LABEL_CLIPS: Record<string, string> = {
|
||||
A_1: "[calm] A,",
|
||||
A_2: "[calm] A,",
|
||||
A_3: "[thoughtful] A,",
|
||||
A_4: "[measured] A,",
|
||||
|
||||
B_1: "[calm] B,",
|
||||
B_2: "[thoughtful] B,",
|
||||
B_3: "[calm] B,",
|
||||
B_4: "[measured] B,",
|
||||
|
||||
C_1: "[thoughtful] C,",
|
||||
C_2: "[calm] C,",
|
||||
C_3: "[measured] C,",
|
||||
C_4: "[calm] C,",
|
||||
|
||||
D_1: "[measured] D.",
|
||||
D_2: "[calm] D.",
|
||||
D_3: "[thoughtful] D.",
|
||||
D_4: "[measured] D.",
|
||||
};
|
||||
|
||||
const VOICE_SETTINGS = { stability: 0.3, similarity_boost: 0.75 };
|
||||
|
||||
const TRANSITION_TEXT = "[thoughtful] Let's look at this a different way...";
|
||||
|
||||
const CORRECT_CLIPS: Record<string, string> = {
|
||||
correct_1: "Exactly right.",
|
||||
correct_2: "That's it.",
|
||||
correct_3: "Spot on.",
|
||||
correct_4: "Perfect.",
|
||||
};
|
||||
|
||||
export async function generateLabels() {
|
||||
const config = useRuntimeConfig();
|
||||
const apiKey = config.elevenlabsApiKey;
|
||||
const voiceId = config.elevenlabsVoiceId;
|
||||
|
||||
if (!apiKey) {
|
||||
console.warn("[labels] ELEVENLABS_API_KEY not set — skipping label generation");
|
||||
return;
|
||||
}
|
||||
|
||||
const labelsDir = resolve(process.cwd(), "public/audio/labels");
|
||||
|
||||
for (const [key, text] of Object.entries(LABEL_CLIPS)) {
|
||||
const outPath = `${labelsDir}/${key}.mp3`;
|
||||
|
||||
if (await fileExists(outPath)) continue;
|
||||
|
||||
console.log(`[labels] generating ${key}.mp3…`);
|
||||
await generateClip(text, outPath, apiKey, voiceId, {
|
||||
model: "eleven_v3",
|
||||
voice_settings: VOICE_SETTINGS,
|
||||
});
|
||||
}
|
||||
|
||||
// branch transition clip
|
||||
const transitionPath = resolve(process.cwd(), "public/audio/branch-transition.mp3");
|
||||
if (!(await fileExists(transitionPath))) {
|
||||
console.log("[labels] generating branch-transition.mp3…");
|
||||
await generateClip(TRANSITION_TEXT, transitionPath, apiKey, voiceId, {
|
||||
model: "eleven_v3",
|
||||
voice_settings: VOICE_SETTINGS,
|
||||
});
|
||||
}
|
||||
|
||||
// correct affirmation clips
|
||||
const audioDir = resolve(process.cwd(), "public/audio");
|
||||
for (const [key, text] of Object.entries(CORRECT_CLIPS)) {
|
||||
const outPath = `${audioDir}/${key}.mp3`;
|
||||
if (await fileExists(outPath)) continue;
|
||||
console.log(`[labels] generating ${key}.mp3…`);
|
||||
await generateClip(text, outPath, apiKey, voiceId, {
|
||||
model: "eleven_v3",
|
||||
voice_settings: { stability: 0.35, similarity_boost: 0.8 },
|
||||
});
|
||||
}
|
||||
|
||||
console.log("[labels] all label clips ready");
|
||||
}
|
||||
@@ -0,0 +1,329 @@
|
||||
import { mkdir, writeFile, access } from "fs/promises";
|
||||
import { resolve } from "path";
|
||||
|
||||
export interface AudioChunk {
|
||||
text: string;
|
||||
start: number;
|
||||
end: number;
|
||||
}
|
||||
|
||||
export interface TTSResult {
|
||||
audioPath: string;
|
||||
audioChunks: AudioChunk[];
|
||||
cost: number;
|
||||
}
|
||||
|
||||
|
||||
async function callElevenLabs(
|
||||
text: string,
|
||||
apiKey: string,
|
||||
voiceId: string
|
||||
): Promise<{ audio: Buffer; chunks: AudioChunk[]; cost: number } | null> {
|
||||
const res = await fetch(
|
||||
`https://api.elevenlabs.io/v1/text-to-speech/${voiceId}/with-timestamps`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
"xi-api-key": apiKey,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
text,
|
||||
model_id: "eleven_turbo_v2_5",
|
||||
output_format: "mp3_44100_128",
|
||||
}),
|
||||
}
|
||||
);
|
||||
|
||||
if (!res.ok) {
|
||||
const errText = await res.text().catch(() => "");
|
||||
console.error(`[tts] ElevenLabs error ${res.status}: ${errText}`);
|
||||
return null;
|
||||
}
|
||||
|
||||
const charCost = parseInt(res.headers.get("character-cost") ?? "0", 10);
|
||||
const cost = charCost * 0.0003;
|
||||
|
||||
const data = await res.json() as {
|
||||
audio_base64: string;
|
||||
alignment: {
|
||||
characters: string[];
|
||||
character_start_times_seconds: number[];
|
||||
character_end_times_seconds: number[];
|
||||
};
|
||||
};
|
||||
|
||||
const audio = Buffer.from(data.audio_base64, "base64");
|
||||
const { characters, character_start_times_seconds: starts, character_end_times_seconds: ends } = data.alignment;
|
||||
|
||||
const words: { word: string; charStart: number; charEnd: number }[] = [];
|
||||
let i = 0;
|
||||
while (i < text.length) {
|
||||
while (i < text.length && /\s/.test(text[i])) i++;
|
||||
if (i >= text.length) break;
|
||||
const wordStart = i;
|
||||
while (i < text.length && !/\s/.test(text[i])) i++;
|
||||
words.push({ word: text.slice(wordStart, i), charStart: wordStart, charEnd: i - 1 });
|
||||
}
|
||||
|
||||
function timeForChar(charIdx: number, side: "start" | "end"): number {
|
||||
let accumulated = 0;
|
||||
for (let ci = 0; ci < characters.length; ci++) {
|
||||
const c = characters[ci];
|
||||
if (accumulated + c.length > charIdx) {
|
||||
return side === "start" ? starts[ci] : ends[ci];
|
||||
}
|
||||
accumulated += c.length;
|
||||
}
|
||||
const last = side === "start" ? starts[starts.length - 1] : ends[ends.length - 1];
|
||||
return last ?? 0;
|
||||
}
|
||||
|
||||
const chunks: AudioChunk[] = [];
|
||||
for (let wi = 0; wi < words.length; wi += 3) {
|
||||
const slice = words.slice(wi, wi + 3);
|
||||
chunks.push({
|
||||
text: slice.map(w => w.word).join(" "),
|
||||
start: timeForChar(slice[0].charStart, "start"),
|
||||
end: timeForChar(slice[slice.length - 1].charEnd, "end"),
|
||||
});
|
||||
}
|
||||
|
||||
return { audio, chunks, cost };
|
||||
}
|
||||
|
||||
|
||||
// Fish Audio doesnt return alignment data, so we return empty chunks
|
||||
async function callFishAudio(
|
||||
text: string,
|
||||
apiKey: string,
|
||||
voiceId: string
|
||||
): Promise<{ audio: Buffer; chunks: AudioChunk[]; cost: number } | null> {
|
||||
const res = await fetch("https://api.fish.audio/v1/tts", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Authorization": `Bearer ${apiKey}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
text,
|
||||
reference_id: voiceId,
|
||||
format: "mp3",
|
||||
mp3_bitrate: 128,
|
||||
streaming: false,
|
||||
}),
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
const errText = await res.text().catch(() => "");
|
||||
console.error(`[tts] Fish Audio error ${res.status}: ${errText}`);
|
||||
return null;
|
||||
}
|
||||
|
||||
const audio = Buffer.from(await res.arrayBuffer());
|
||||
return { audio, chunks: [], cost: 0 };
|
||||
}
|
||||
|
||||
|
||||
function getProvider() {
|
||||
const config = useRuntimeConfig();
|
||||
return (config.ttsProvider as string | undefined)?.toLowerCase() ?? "elevenlabs";
|
||||
}
|
||||
|
||||
async function callTTS(
|
||||
text: string
|
||||
): Promise<{ audio: Buffer; chunks: AudioChunk[]; cost: number } | null> {
|
||||
const config = useRuntimeConfig();
|
||||
const provider = getProvider();
|
||||
|
||||
if (provider === "fishaudio") {
|
||||
const apiKey = config.fishAudioApiKey as string;
|
||||
const voiceId = config.fishAudioVoiceId as string;
|
||||
if (!apiKey) return null;
|
||||
return callFishAudio(text, apiKey, voiceId);
|
||||
}
|
||||
|
||||
const apiKey = config.elevenlabsApiKey as string;
|
||||
const voiceId = config.elevenlabsVoiceId as string;
|
||||
if (!apiKey) return null;
|
||||
return callElevenLabs(text, apiKey, voiceId);
|
||||
}
|
||||
|
||||
|
||||
export async function generateStepTTS(
|
||||
text: string,
|
||||
lessonId: string,
|
||||
stepIndex: number
|
||||
): Promise<TTSResult | null> {
|
||||
try {
|
||||
const result = await callTTS(text);
|
||||
if (!result) return null;
|
||||
|
||||
const dir = resolve(process.cwd(), `public/audio/lessons/${lessonId}`);
|
||||
await mkdir(dir, { recursive: true });
|
||||
const filename = `step_${stepIndex}.mp3`;
|
||||
await writeFile(`${dir}/${filename}`, result.audio);
|
||||
|
||||
const audioPath = `/audio/lessons/${lessonId}/${filename}`;
|
||||
console.log(`[tts] step ${stepIndex} for lesson ${lessonId} — ${result.chunks.length} chunks | $${result.cost.toFixed(4)}`);
|
||||
return { audioPath, audioChunks: result.chunks, cost: result.cost };
|
||||
} catch (err: any) {
|
||||
console.error(`[tts] step ${stepIndex} for lesson ${lessonId} failed: ${err?.message ?? err}`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function generateQuestionTTS(
|
||||
text: string,
|
||||
lessonId: string,
|
||||
stepIndex: number
|
||||
): Promise<TTSResult | null> {
|
||||
try {
|
||||
const result = await callTTS(text);
|
||||
if (!result) return null;
|
||||
|
||||
const dir = resolve(process.cwd(), `public/audio/lessons/${lessonId}`);
|
||||
await mkdir(dir, { recursive: true });
|
||||
const filename = `step_${stepIndex}_question.mp3`;
|
||||
await writeFile(`${dir}/${filename}`, result.audio);
|
||||
|
||||
const audioPath = `/audio/lessons/${lessonId}/${filename}`;
|
||||
return { audioPath, audioChunks: result.chunks, cost: result.cost };
|
||||
} catch (err: any) {
|
||||
console.error(`[tts] question ${stepIndex} for lesson ${lessonId} failed: ${err?.message ?? err}`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function generateOptionTTS(
|
||||
text: string,
|
||||
lessonId: string,
|
||||
stepIndex: number,
|
||||
optionIndex: number
|
||||
): Promise<{ audioPath: string; cost: number } | null> {
|
||||
try {
|
||||
const result = await callTTS(text);
|
||||
if (!result) return null;
|
||||
|
||||
const dir = resolve(process.cwd(), `public/audio/lessons/${lessonId}`);
|
||||
await mkdir(dir, { recursive: true });
|
||||
const filename = `step_${stepIndex}_option_${optionIndex}.mp3`;
|
||||
await writeFile(`${dir}/${filename}`, result.audio);
|
||||
|
||||
const audioPath = `/audio/lessons/${lessonId}/${filename}`;
|
||||
return { audioPath, cost: result.cost };
|
||||
} catch (err: any) {
|
||||
console.error(`[tts] option ${stepIndex}/${optionIndex} for lesson ${lessonId} failed: ${err?.message ?? err}`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// generate a plain audio clip with no timestamp data (used for label clips)
|
||||
// opts are ElevenLabs-specific and are ignored when using Fish Audio
|
||||
export async function generateClip(
|
||||
text: string,
|
||||
outPath: string,
|
||||
apiKey: string,
|
||||
voiceId: string,
|
||||
opts?: { model?: string; voice_settings?: Record<string, number> }
|
||||
): Promise<{ cost: number } | null> {
|
||||
const provider = getProvider();
|
||||
|
||||
try {
|
||||
let buffer: Buffer;
|
||||
let cost = 0;
|
||||
|
||||
if (provider === "fishaudio") {
|
||||
const config = useRuntimeConfig();
|
||||
const fishKey = config.fishAudioApiKey as string;
|
||||
const fishVoice = (config.fishAudioVoiceId as string) || voiceId;
|
||||
|
||||
const res = await fetch("https://api.fish.audio/v1/tts", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Authorization": `Bearer ${fishKey}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
text,
|
||||
reference_id: fishVoice,
|
||||
format: "mp3",
|
||||
mp3_bitrate: 128,
|
||||
streaming: false,
|
||||
}),
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
const errText = await res.text().catch(() => "");
|
||||
console.error(`[tts] Fish Audio clip error ${res.status}: ${errText}`);
|
||||
return null;
|
||||
}
|
||||
|
||||
buffer = Buffer.from(await res.arrayBuffer());
|
||||
} else {
|
||||
const res = await fetch(
|
||||
`https://api.elevenlabs.io/v1/text-to-speech/${voiceId}`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
"xi-api-key": apiKey,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
text,
|
||||
model_id: opts?.model ?? "eleven_turbo_v2_5",
|
||||
output_format: "mp3_44100_128",
|
||||
...(opts?.voice_settings ? { voice_settings: opts.voice_settings } : {}),
|
||||
}),
|
||||
}
|
||||
);
|
||||
|
||||
if (!res.ok) {
|
||||
const errText = await res.text().catch(() => "");
|
||||
console.error(`[tts] clip error ${res.status}: ${errText}`);
|
||||
return null;
|
||||
}
|
||||
|
||||
const charCost = parseInt(res.headers.get("character-cost") ?? "0", 10);
|
||||
cost = charCost * 0.0003;
|
||||
buffer = Buffer.from(await res.arrayBuffer());
|
||||
}
|
||||
|
||||
await mkdir(resolve(process.cwd(), "public/audio/labels"), { recursive: true });
|
||||
await writeFile(outPath, buffer);
|
||||
return { cost };
|
||||
} catch (err: any) {
|
||||
console.error(`[tts] clip failed for ${outPath}: ${err?.message ?? err}`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function generateTTSToPath(
|
||||
text: string,
|
||||
lessonId: string,
|
||||
filename: string
|
||||
): Promise<TTSResult | null> {
|
||||
try {
|
||||
const result = await callTTS(text);
|
||||
if (!result) return null;
|
||||
|
||||
const dir = resolve(process.cwd(), `public/audio/lessons/${lessonId}`);
|
||||
await mkdir(dir, { recursive: true });
|
||||
await writeFile(`${dir}/${filename}`, result.audio);
|
||||
|
||||
const audioPath = `/audio/lessons/${lessonId}/${filename}`;
|
||||
return { audioPath, audioChunks: result.chunks, cost: result.cost };
|
||||
} catch (err: any) {
|
||||
console.error(`[tts] ${filename} for lesson ${lessonId} failed: ${err?.message ?? err}`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function fileExists(path: string): Promise<boolean> {
|
||||
try {
|
||||
await access(path);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
interface Message {
|
||||
role: "system" | "user" | "assistant";
|
||||
content: string;
|
||||
}
|
||||
|
||||
interface AskAIOptions {
|
||||
model?: string;
|
||||
temperature?: number;
|
||||
maxRetries?: number;
|
||||
maxTokens?: number;
|
||||
}
|
||||
|
||||
export interface AskAIResult {
|
||||
text: string;
|
||||
cost: number;
|
||||
}
|
||||
|
||||
function sleep(ms: number) {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
|
||||
export async function askAI(messages: Message[], options: AskAIOptions = {}): Promise<AskAIResult> {
|
||||
const config = useRuntimeConfig();
|
||||
const apiKey = config.openrouterApiKey;
|
||||
|
||||
if (!apiKey) throw new Error("OPENROUTER_API_KEY is not set");
|
||||
|
||||
const model = options.model ?? (config as any).openrouterModel ?? "anthropic/claude-sonnet-4-5";
|
||||
const maxRetries = options.maxRetries ?? 4;
|
||||
|
||||
let lastError: any;
|
||||
|
||||
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
||||
const label = attempt > 0 ? ` (attempt ${attempt + 1}/${maxRetries + 1})` : "";
|
||||
const promptPreview = messages[messages.length - 1]?.content?.slice(0, 120).replace(/\n/g, " ");
|
||||
console.log(`[openrouter] → ${model}${label} | prompt: "${promptPreview}…"`);
|
||||
const t0 = Date.now();
|
||||
|
||||
try {
|
||||
const res = await $fetch<{ id?: string; choices: { message: { content: string } }[]; usage?: { prompt_tokens?: number; completion_tokens?: number; cost?: number } }>(
|
||||
"https://openrouter.ai/api/v1/chat/completions",
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${apiKey}`,
|
||||
"Content-Type": "application/json",
|
||||
"HTTP-Referer": "https://revisi.one",
|
||||
"X-Title": "Revisi.one",
|
||||
},
|
||||
body: {
|
||||
model,
|
||||
messages,
|
||||
temperature: options.temperature ?? 0.3,
|
||||
...(options.maxTokens ? { max_tokens: options.maxTokens } : {}),
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
const elapsed = ((Date.now() - t0) / 1000).toFixed(1);
|
||||
const content = res.choices?.[0]?.message?.content;
|
||||
|
||||
if (!content) {
|
||||
console.error(`[openrouter] ✗ empty response after ${elapsed}s — full response:`, JSON.stringify(res));
|
||||
throw new Error("Empty response from OpenRouter");
|
||||
}
|
||||
|
||||
const usage = res.usage;
|
||||
const tokenInfo = usage ? ` | tokens: ${usage.prompt_tokens ?? "?"}→${usage.completion_tokens ?? "?"}` : "";
|
||||
console.log(`[openrouter] ✓ ${elapsed}s${tokenInfo} | reply: "${content.slice(0, 120).replace(/\n/g, " ")}…"`);
|
||||
|
||||
const cost = usage?.cost ?? 0;
|
||||
|
||||
return { text: content, cost };
|
||||
} catch (err: any) {
|
||||
lastError = err;
|
||||
const elapsed = ((Date.now() - t0) / 1000).toFixed(1);
|
||||
const status = err?.response?.status ?? err?.statusCode ?? err?.status;
|
||||
const body = err?.data ?? err?.response?._data ?? "(no body)";
|
||||
|
||||
console.error(`[openrouter] ✗ ${elapsed}s — status: ${status ?? "unknown"} | error: ${err?.message}`);
|
||||
if (body && body !== "(no body)") {
|
||||
console.error(`[openrouter] response body:`, JSON.stringify(body).slice(0, 400));
|
||||
}
|
||||
|
||||
// 402 = insufficient credits — wait 60s and keep retrying indefinitely
|
||||
if (status === 402) {
|
||||
console.warn(`[openrouter] insufficient credits — waiting 60s before retry…`);
|
||||
await sleep(60_000);
|
||||
attempt--; // don't count against maxRetries
|
||||
continue;
|
||||
}
|
||||
|
||||
// only retry on 429 and 5xx
|
||||
if (status !== 429 && (status < 500 || status > 599)) throw err;
|
||||
|
||||
if (attempt < maxRetries) {
|
||||
const retryAfterHeader = err?.response?.headers?.get?.("retry-after");
|
||||
const retryAfterSec = retryAfterHeader ? parseInt(retryAfterHeader, 10) : NaN;
|
||||
const backoffMs = isNaN(retryAfterSec)
|
||||
? Math.min(1000 * 2 ** attempt + Math.random() * 500, 30000)
|
||||
: retryAfterSec * 1000;
|
||||
|
||||
console.warn(`[openrouter] retrying in ${Math.round(backoffMs)}ms…`);
|
||||
await sleep(backoffMs);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw lastError;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { createRequire } from "module";
|
||||
|
||||
const require = createRequire(import.meta.url);
|
||||
const pdfParse = require("pdf-parse");
|
||||
|
||||
export async function parsePdf(buffer: Buffer): Promise<string> {
|
||||
const data = await pdfParse(buffer);
|
||||
return data.text;
|
||||
}
|
||||
Reference in New Issue
Block a user