The third segment of the note-powered wizards (Study deck / Homework / Podcast) takes the same intake - photos, documents, pasted text, the share target - and turns it into a spoken episode. - prompts: PODCAST_SYSTEM_PROMPT writes for the ear (plain prose, no markdown, no visual references), chaptered into 3-8 sections around a length target. - /api/podcast mirrors /api/generate's plumbing (rate limit, model gate, provider keys, heartbeats, the 2x deadline, the optional vision pass). - storage: frezmo.podcasts.v1, zod-validated, included in the backup and the encrypted sync payload; a new library tab lists episodes with listen / rename / delete-with-undo, and the wizard switch deep-links in. - player: the transcript is word-wrapped spans and the word under the voice rides a lyric-style highlight that keeps itself centred. Two clocks: exact onboundary events where the browser reports them, a per-word length timer where it does not (iOS Safari); the first boundary retires the timer. Pause = cancel + re-speak from the word, which makes resume, chapter jumps and tap-any-word-to-seek the same operation; the position saves on pause, page-hide and unmount, so reopening resumes. Speed rebuilds the engine and keeps playing; the Aero soundtrack pauses itself when the voice starts; a browser with no voice gets an honest note and a readable transcript. - the script sits on a glass pane (plain paragraphs over Aero's grass failed contrast; the scan now covers the player too). tests/podcast-test.mjs (17th suite) stubs the model and the speech engine and drives the highlight by hand: 26 checks. All suites green, contrast clean. Version 0.7.3, SW v95.
234 lines
8.8 KiB
TypeScript
234 lines
8.8 KiB
TypeScript
import { z } from "zod";
|
|
|
|
import { extractNotes, generatePodcast, notesToText, type PipelineStage } from "@/lib/ai";
|
|
import { getServerConfig, resolveModelName } from "@/lib/config";
|
|
import { INERT_OLLAMA_AUTH, resolveModelTarget } from "@/lib/providers-server";
|
|
import { friendlyErrorMessage, OllamaError } from "@/lib/ollama";
|
|
import { rejectCrossOrigin, rejectOversizedBody, requestLocale } from "@/lib/http-guard";
|
|
import { clientKey, rateLimit, rateLimitResponse } from "@/lib/rate-limit";
|
|
import { acquireModelGate } from "@/lib/model-guard";
|
|
import { translate } from "@/lib/i18n-dict";
|
|
import { dataUrlSchema } from "@/lib/schemas";
|
|
|
|
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({
|
|
textModel: z.string().min(1).max(200).optional(),
|
|
visionModel: z.string().min(1).max(200).optional(),
|
|
images: z.array(dataUrlSchema(MAX_IMAGE_CHARS)).max(16).optional(),
|
|
text: z.string().max(120_000).optional(),
|
|
/** Target episode length in minutes. */
|
|
minutes: z.number().int().min(1).max(15).optional(),
|
|
focus: z.string().max(300).optional(),
|
|
/** Language the episode must be written in (usually detected by the vision step). */
|
|
language: z.string().max(60).optional(),
|
|
});
|
|
|
|
type StreamEvent =
|
|
| { type: "stage"; stage: PipelineStage; message: string }
|
|
| { type: "podcast"; podcast: unknown; meta: Record<string, unknown> }
|
|
| { type: "error"; error: string; kind?: string }
|
|
| { type: "ping" };
|
|
|
|
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;
|
|
|
|
/**
|
|
* Notes → a spoken episode. Same plumbing as /api/generate (rate limit, model
|
|
* gate, provider keys, heartbeats, one hard deadline), one model call for the
|
|
* script after the optional vision pass.
|
|
*/
|
|
export async function POST(request: Request) {
|
|
const locale = requestLocale(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(`podcast:${clientKey(request)}`, config.rateLimits.generate, config.rateLimits.windowMs);
|
|
if (!limit.allowed) return rateLimitResponse(limit, locale);
|
|
|
|
const resolved = resolveModelTarget(config, request, locale);
|
|
if ("error" in resolved) {
|
|
return Response.json({ error: resolved.error.message, kind: resolved.error.kind }, { status: resolved.error.status });
|
|
}
|
|
const target = resolved.target;
|
|
const provider = target.kind === "provider" ? target.target : undefined;
|
|
const baseAuth = target.kind === "provider" ? INERT_OLLAMA_AUTH : target.auth;
|
|
|
|
let payload: unknown;
|
|
try {
|
|
payload = await request.json();
|
|
} catch {
|
|
return Response.json({ error: translate(locale, "server.bodyNotJson") }, { status: 400 });
|
|
}
|
|
|
|
const parsed = requestSchema.safeParse(payload);
|
|
if (!parsed.success) {
|
|
return Response.json(
|
|
{
|
|
error: translate(locale, "server.invalidRequest", {
|
|
detail: parsed.error.issues
|
|
.slice(0, 3)
|
|
.map((issue) => `${issue.path.join(".") || "body"} ${issue.message}`)
|
|
.join("; "),
|
|
}),
|
|
},
|
|
{ status: 400 },
|
|
);
|
|
}
|
|
|
|
const { images, text, minutes, focus, language } = parsed.data;
|
|
const imageList = (images ?? []).map(stripDataUrlPrefix).filter(Boolean);
|
|
|
|
if (imageList.length === 0 && !text?.trim()) {
|
|
return Response.json({ error: translate(locale, "server.needContent") }, { status: 400 });
|
|
}
|
|
|
|
const providerTextModel = provider ? parsed.data.textModel ?? parsed.data.visionModel : undefined;
|
|
const providerVisionModel = provider ? parsed.data.visionModel ?? parsed.data.textModel : undefined;
|
|
if (provider && !providerTextModel) {
|
|
return Response.json(
|
|
{ error: translate(locale, "server.providerModelNeeded"), kind: "bad_provider" },
|
|
{ status: 400 },
|
|
);
|
|
}
|
|
const textModel = provider ? providerTextModel! : config.textModel;
|
|
const visionModel = provider ? providerVisionModel ?? providerTextModel! : config.visionModel;
|
|
|
|
const gate = acquireModelGate(request, config, locale);
|
|
if ("response" in gate) return gate.response;
|
|
|
|
const encode = encoder();
|
|
const abort = new AbortController();
|
|
/* One vision pass plus one script: the same 2x deadline as the deck flow. */
|
|
const globalTimer = setTimeout(() => abort.abort(), config.timeoutMs * 2);
|
|
const onRequestAbort = () => abort.abort();
|
|
if (request.signal.aborted) abort.abort();
|
|
else request.signal.addEventListener("abort", onRequestAbort, { once: true });
|
|
let heartbeat: ReturnType<typeof setInterval> | null = null;
|
|
|
|
const stream = new ReadableStream<Uint8Array>({
|
|
async start(controller) {
|
|
const send = (event: StreamEvent) => {
|
|
try {
|
|
controller.enqueue(encode(event));
|
|
} catch {
|
|
/* client went away */
|
|
}
|
|
};
|
|
|
|
heartbeat = setInterval(() => send({ type: "ping" }), 15_000);
|
|
try {
|
|
let sourceText: string;
|
|
let detectedLanguage: string | undefined = language;
|
|
let title: string | undefined;
|
|
let summary: string | undefined;
|
|
let repaired = false;
|
|
|
|
if (imageList.length > 0) {
|
|
send({ type: "stage", stage: "vision", message: "Reading your notes…" });
|
|
const extraction = await extractNotes({
|
|
model: visionModel,
|
|
repairModel: textModel,
|
|
auth: baseAuth,
|
|
provider,
|
|
images: imageList,
|
|
text,
|
|
signal: abort.signal,
|
|
onProgress: (message) => send({ type: "stage", stage: "vision", message }),
|
|
});
|
|
sourceText = notesToText(extraction.notes);
|
|
title = extraction.notes.title;
|
|
summary = extraction.notes.summary;
|
|
detectedLanguage = extraction.notes.language || language;
|
|
repaired = repaired || extraction.repaired || extraction.languageRepaired;
|
|
send({ type: "stage", stage: "generation", message: "Writing the script…" });
|
|
} else {
|
|
sourceText = text!.trim();
|
|
send({ type: "stage", stage: "generation", message: "Writing the script…" });
|
|
}
|
|
|
|
const generated = await generatePodcast({
|
|
model: textModel,
|
|
auth: baseAuth,
|
|
provider,
|
|
notes: sourceText,
|
|
minutes: minutes ?? 4,
|
|
language: detectedLanguage,
|
|
focus,
|
|
signal: abort.signal,
|
|
});
|
|
|
|
send({
|
|
type: "podcast",
|
|
podcast: {
|
|
title: generated.podcast.title || title || "Your podcast",
|
|
language: generated.podcast.language || detectedLanguage,
|
|
summary: generated.podcast.summary ?? summary,
|
|
sourceText,
|
|
sections: generated.podcast.sections,
|
|
},
|
|
meta: {
|
|
repaired,
|
|
language: generated.podcast.language || detectedLanguage || null,
|
|
visionModel: imageList.length ? (provider ? visionModel : resolveModelName(config.visionModel, baseAuth.directCloud)) : null,
|
|
textModel: provider ? textModel : resolveModelName(config.textModel, baseAuth.directCloud),
|
|
},
|
|
});
|
|
} catch (error) {
|
|
const message = friendlyErrorMessage(error, baseAuth.baseUrl || provider?.baseUrl || "", locale);
|
|
// Never log the request body: it may contain the user's notes.
|
|
console.error("[api/podcast]", error instanceof OllamaError ? `${error.kind}: ${error.message}` : error);
|
|
send({
|
|
type: "error",
|
|
error: message,
|
|
kind: error instanceof OllamaError ? error.kind : "unknown",
|
|
});
|
|
} finally {
|
|
if (heartbeat) clearInterval(heartbeat);
|
|
clearTimeout(globalTimer);
|
|
request.signal.removeEventListener("abort", onRequestAbort);
|
|
gate.gate.release();
|
|
try {
|
|
controller.close();
|
|
} catch {
|
|
/* already closed */
|
|
}
|
|
}
|
|
},
|
|
cancel() {
|
|
if (heartbeat) clearInterval(heartbeat);
|
|
clearTimeout(globalTimer);
|
|
request.signal.removeEventListener("abort", onRequestAbort);
|
|
gate.gate.release();
|
|
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;
|
|
}
|