Codebase audit: fix bugs, performance issues, dead code, and edge cases

1. Bugs fixed:
- Backup schema now accepts theme: "aero" so backups exported under
  Frutiger Aero pass parseBackup on import, and applyImport in "replace"
  mode replaces SRS schedules, answer history, and chats instead of
  merging into existing SRS or skipping empty arrays.
- Typed FillBlank cards bind the input value to chosen ?? value instead
  of chosen ?? "", and normalise() preserves Unicode letters (\p{L}\p{N})
  when grading non-ASCII answers.
- OpenAI-compatible vision calls (contentFor in lib/openai-compat.ts)
  prefix data:image/jpeg;base64, when routes pass stripped base64.
- Streaming calls (ollamaChatStream, providerChatStream) keep their
  AbortController active across the entire body stream and API routes
  forward request.signal so stopping a chat or cancelling a run aborts
  the upstream request immediately.
- HomeworkCard uses homeworkModeLabelKey so "check" records display the
  Checked label instead of Step-by-step guide.
- Desktop dropzones on /create and /homework accept dropped documents as
  well as images.
- LibraryScreen filters reported questions out of its due and mistake
  counts to match ReviewClient.
- Clearing a provider key resets generation if it pointed at that
  provider; chat conversations persist and restore provider; ModelPicker
  keys items by provider:name.
- RouteTransition no longer mutates module-level lastPath during render.
- Service worker asset handler falls back to Response.error() when an
  uncached asset fetch fails offline.
- Confetti and .ice-orb / .ice-bubble respect the in-app reduce-motion
  toggle.

2. Performance & cleanup:
- Deduplicate window storage listeners across all six localStorage
  stores (one listener while subscribed instead of N^2 notifications).
- Use todayAnswerCount in LibraryScreen and pass sorted folders to
  DeckCard.
- Memoize MessageRow with stable callbacks in ChatScreen and compute
  last message IDs in a single pass.
- Close ImageBitmap after drawing in lib/image.ts and destroy pdf.js
  documents in finally in lib/documents.ts.
- Avoid re-parsing serialized sync payloads in POST /api/sync.
- Deduplicate copyText (lib/share.ts -> lib/clipboard.ts), chatMessageId,
  describeExtraction, ALL_FOLDERS, useIsPhone, and remove unused helpers
  and boilerplate public/*.svg files.

Version 0.5.8, SW -> freezmo-v80.
This commit is contained in:
Martin Dvořák 2026-10-14 19:00:17 +00:00
commit 982391ca45
57 changed files with 714 additions and 497 deletions

View file

@ -217,58 +217,65 @@ async function extractPdf(
const images: PreparedImage[] = [];
const warnings: string[] = [];
let imagesTruncated = false;
const numPages = document.numPages;
const totalPages = Math.min(document.numPages, 80);
if (document.numPages > totalPages) {
warnings.push(`Only the first ${totalPages} of ${document.numPages} pages were read.`);
}
for (let pageNumber = 1; pageNumber <= totalPages; pageNumber++) {
onProgress?.({
message: t("doc.readingPage", { n: pageNumber, total: totalPages }),
progress: pageNumber / totalPages,
});
const page = (await document.getPage(pageNumber)) as unknown as PdfPage;
let pageText = "";
try {
const content = await page.getTextContent();
pageText = textFromPdfItems(content.items);
} catch {
warnings.push(`Page ${pageNumber} text could not be extracted.`);
try {
const totalPages = Math.min(numPages, 80);
if (numPages > totalPages) {
warnings.push(`Only the first ${totalPages} of ${numPages} pages were read.`);
}
if (pageText.trim().length >= SCANNED_PAGE_CHAR_THRESHOLD) {
pages.push(`--- page ${pageNumber} ---\n${pageText.trim()}`);
} else if (images.length < MAX_DOCUMENT_IMAGES) {
// Scanned or image-only page: render it so the vision model can read it.
for (let pageNumber = 1; pageNumber <= totalPages; pageNumber++) {
onProgress?.({
message: t("doc.readingPage", { n: pageNumber, total: totalPages }),
progress: pageNumber / totalPages,
});
const page = (await document.getPage(pageNumber)) as unknown as PdfPage;
let pageText = "";
try {
const viewport = page.getViewport({ scale: PDF_RENDER_SCALE });
const canvas = window.document.createElement("canvas");
canvas.width = Math.floor(viewport.width);
canvas.height = Math.floor(viewport.height);
const context = canvas.getContext("2d");
if (!context) throw new Error("no 2d context");
context.fillStyle = "#ffffff";
context.fillRect(0, 0, canvas.width, canvas.height);
await page.render({ canvasContext: context, viewport }).promise;
const blob = await new Promise<Blob | null>((resolve) => canvas.toBlob(resolve, "image/jpeg", 0.9));
if (blob) {
images.push(await prepareImageFromBlob(blob, `${file.name} p${pageNumber}`));
}
const content = await page.getTextContent();
pageText = textFromPdfItems(content.items);
} catch {
warnings.push(`Page ${pageNumber} looks scanned but could not be rendered.`);
warnings.push(`Page ${pageNumber} text could not be extracted.`);
}
} else {
imagesTruncated = true;
}
if (pageText.trim().length < SCANNED_PAGE_CHAR_THRESHOLD) {
// Nothing to add to the text; the image carries the content.
imagesTruncated = images.length >= MAX_DOCUMENT_IMAGES;
}
if (pageText.trim().length >= SCANNED_PAGE_CHAR_THRESHOLD) {
pages.push(`--- page ${pageNumber} ---\n${pageText.trim()}`);
} else if (images.length < MAX_DOCUMENT_IMAGES) {
// Scanned or image-only page: render it so the vision model can read it.
try {
const viewport = page.getViewport({ scale: PDF_RENDER_SCALE });
const canvas = window.document.createElement("canvas");
canvas.width = Math.floor(viewport.width);
canvas.height = Math.floor(viewport.height);
const context = canvas.getContext("2d");
if (!context) throw new Error("no 2d context");
context.fillStyle = "#ffffff";
context.fillRect(0, 0, canvas.width, canvas.height);
await page.render({ canvasContext: context, viewport }).promise;
const blob = await new Promise<Blob | null>((resolve) => canvas.toBlob(resolve, "image/jpeg", 0.9));
canvas.width = 0;
canvas.height = 0;
if (blob) {
images.push(await prepareImageFromBlob(blob, `${file.name} p${pageNumber}`));
}
} catch {
warnings.push(`Page ${pageNumber} looks scanned but could not be rendered.`);
}
} else {
imagesTruncated = true;
}
page.cleanup?.();
if (pageText.trim().length < SCANNED_PAGE_CHAR_THRESHOLD) {
// Nothing to add to the text; the image carries the content.
imagesTruncated = images.length >= MAX_DOCUMENT_IMAGES;
}
page.cleanup?.();
}
} finally {
await (document as { destroy?: () => Promise<void> }).destroy?.().catch(() => {});
}
if (images.length > 0) {
@ -280,7 +287,7 @@ async function extractPdf(
}
return withLimits(file.name, "pdf", pages.join("\n\n"), images, {
pages: document.numPages,
pages: numPages,
warnings,
imagesTruncated,
});
@ -342,14 +349,18 @@ async function extractDocx(file: File): Promise<ExtractedDocument> {
const XML_TEXT_RUN = /<a:t[^>]*>([\s\S]*?)<\/a:t>/g;
const XML_PARAGRAPH = /<\/a:p>/g;
function safeCodePoint(cp: number): string {
return Number.isFinite(cp) && cp > 0 && cp <= 0x10ffff ? String.fromCodePoint(cp) : "";
}
function decodeXmlEntities(value: string): string {
return value
.replace(/&lt;/g, "<")
.replace(/&gt;/g, ">")
.replace(/&quot;/g, '"')
.replace(/&apos;/g, "'")
.replace(/&#(\d+);/g, (_, code: string) => String.fromCodePoint(Number(code)))
.replace(/&#x([0-9a-f]+);/gi, (_, code: string) => String.fromCodePoint(Number.parseInt(code, 16)))
.replace(/&#(\d+);/g, (_, code: string) => safeCodePoint(Number(code)))
.replace(/&#x([0-9a-f]+);/gi, (_, code: string) => safeCodePoint(Number.parseInt(code, 16)))
.replace(/&amp;/g, "&");
}
@ -475,3 +486,18 @@ export function documentsToText(documents: ExtractedDocument[]): string {
.join("\n\n")
.slice(0, MAX_DOCUMENT_CHARS * 2);
}
/** Human-readable summary line for an extracted document (words · pages · images). */
export function describeExtraction(
document: ExtractedDocument,
tr: (key: string, vars?: Record<string, string | number>) => string,
pl: (n: number, base: string, vars?: Record<string, string | number>) => string,
): string {
const parts = [
pl(document.words, "upload.words", { n: document.words.toLocaleString() }),
document.pages ? pl(document.pages, "upload.pages") : "",
document.images.length ? pl(document.images.length, "upload.pageImages") : "",
document.truncated ? tr("upload.truncated") : "",
];
return parts.filter(Boolean).join(" · ");
}