Files
DeepTutor/web/components/chat/preview/previewerFor.ts
Bingxi Zhao (Frank) a053fecf6e release: v1.6.11
The interface ships in French and Ukrainian alongside English and
Chinese, and /language pins a reply language for one conversation.
Immersive reading organizes collections as colored folders, adds a
fullscreen learning mode, and turns Explain / Translate / Guide me on a
selection into ordinary messages in the reading chat; "Quiz me on this
page" runs one quiz turn without changing the conversation's mode, which
retires the reading-openers endpoint and its task model. Images embedded
in DOCX, PPTX and PDF are extracted in place and reach vision models.

Office files preview as paged PDFs through LibreOffice (now in the Docker
image), falling back to the in-browser renderers without it. Web sources
re-sync on a durable schedule, linked folders sync on demand, .bib files
index, and a zotero_search tool queries Zotero. Settings gains Learning
progress and credential-free presets; admins import users from CSV;
split deployments can hand a session to a public device. Grok CLI joins
the subagents, Unifically the providers, ChatGPT exports the importers.

Gate cleanup: mypy/bandit findings in the new launcher, audio-overview,
web-source, reading-store and textbook modules; the task-model copy test
follows the retired reading_openers kind.

See assets/releases/ver1-6-11.md for the full notes, and the README
Releases row plus its eleven translations.
2026-09-24 09:59:56 +08:00

148 lines
4.8 KiB
TypeScript

/**
* Maps a chat attachment to the preview renderer it should use.
*
* The drawer uses the returned ``kind`` to dynamically import the matching
* renderer (see ``./previewers/*.tsx``). Office files first use the server's
* PDF conversion for a paged preview, then fall back to client rendering or
* extracted text when conversion is unavailable.
*/
import { langForFilename } from "@/lib/code-languages";
import { extOf } from "@/lib/doc-attachments";
export type PreviewKind =
| "pdf"
| "image"
| "video"
| "svg"
| "markdown"
| "code"
| "text"
| "docx"
| "xlsx"
| "office-text"
| "fallback";
export interface FilePreviewSource {
/** Display name; also used to derive the file extension. */
filename: string;
/** MIME type when known (image/png, application/pdf, …). */
mimeType?: string;
/** Backend classification — "image" or anything else. Useful for
* attachments where the filename has no extension but the MIME is set. */
type?: string;
/** Public URL served by /files/attachments. Preferred over base64. */
url?: string;
/** Inline base64 payload — only present for pending (un-sent) attachments
* or messages that pre-date the storage rollout. */
base64?: string;
/** Plain-text rendering of office docs, populated by the backend. */
extractedText?: string;
/** Optional endpoint that returns extracted plain text on demand. */
extractedTextUrl?: string;
/** Original byte size, used for empty-state copy. */
size?: number;
/** Stable id; lets the drawer build a stable React key. */
id?: string;
}
// OOXML formats with a browser fallback (docx-preview / exceljs).
const DOCX_EXTS = new Set([".docx", ".docm"]);
const XLSX_EXTS = new Set([".xlsx", ".xlsm"]);
// PowerPoint and legacy Office formats use extracted text if PDF conversion
// fails. A slide with only graphics has no extracted text to display.
const OFFICE_BINARY_EXTS = new Set([".pptx", ".pptm", ".ppt", ".doc", ".xls"]);
const OFFICE_TEXT_MIMES = new Set([
"application/vnd.openxmlformats-officedocument.presentationml.presentation",
"application/vnd.ms-powerpoint.presentation.macroEnabled.12",
"application/vnd.ms-powerpoint",
"application/msword",
"application/vnd.ms-excel",
]);
const MARKDOWN_EXTS = new Set([".md", ".markdown", ".rst", ".asciidoc"]);
const PLAIN_TEXT_EXTS = new Set([
".txt",
".text",
".log",
".csv",
".tsv",
".env",
".conf",
]);
const RASTER_IMAGE_EXTS = new Set([
".png",
".jpg",
".jpeg",
".gif",
".webp",
".bmp",
".tif",
".tiff",
".avif",
]);
const VIDEO_EXTS = new Set([".mp4", ".webm", ".mov", ".m4v", ".ogv"]);
/** Heuristic: does *source* refer to an image we can render via <img>? */
function isImage(source: FilePreviewSource, ext: string): boolean {
if (RASTER_IMAGE_EXTS.has(ext)) return true;
if (
source.mimeType?.startsWith("image/") &&
source.mimeType !== "image/svg+xml"
)
return true;
if (source.type === "image") return true;
return false;
}
export function previewKindFor(source: FilePreviewSource): PreviewKind {
const ext = extOf(source.filename || "");
const mime = source.mimeType || "";
if (ext === ".pdf" || mime === "application/pdf") return "pdf";
if (ext === ".svg" || mime === "image/svg+xml") return "svg";
if (isImage(source, ext)) return "image";
if (VIDEO_EXTS.has(ext) || mime.startsWith("video/")) return "video";
if (MARKDOWN_EXTS.has(ext) || mime === "text/markdown") return "markdown";
if (
DOCX_EXTS.has(ext) ||
mime ===
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"
)
return "docx";
if (
XLSX_EXTS.has(ext) ||
mime === "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
)
return "xlsx";
if (OFFICE_BINARY_EXTS.has(ext) || OFFICE_TEXT_MIMES.has(mime))
return "office-text";
// Catches both extension-based mappings (.js, .ts, .go, .vue, .lua, …)
// and special filenames without extensions (Dockerfile, Makefile, …).
if (langForFilename(source.filename || "")) return "code";
if (PLAIN_TEXT_EXTS.has(ext) || mime.startsWith("text/")) return "text";
return "fallback";
}
/**
* Build the in-browser-loadable URL for the preview, falling back to a
* data URL when the original file lives only as base64 in memory (the
* pending-attachment case in the composer).
*
* Returns ``null`` when neither is available; renderers should then show a
* "preview not available" affordance.
*/
export function resolveSourceUrl(
source: FilePreviewSource,
apiUrl: (path: string) => string,
): string | null {
if (source.url) {
return source.url.startsWith("http") || source.url.startsWith("blob:")
? source.url
: apiUrl(source.url);
}
if (source.base64 && source.mimeType) {
return `data:${source.mimeType};base64,${source.base64}`;
}
return null;
}