Files
DeepTutor/web/features/chat/controllers/pending-attachments.ts
Bingxi Zhao (Frank) 28c11e2ae8 feat: update notes, chat interactions and workspace UI on preview
Remove course pages, APIs and the course_study capability. Add anchored note
threads and reviewed learning-block generation, queued and withdrawable chat
messages, quote attachments, side chats, and the calendar/favorites/navigation
updates.

Keep preview's existing backend architecture. Wire the new features through
its Python application and existing chat engine, retain legacy selected-text
requests, persist mid-turn messages in replay, and restore note context when
regenerating. Leave stored user data intact.

Validate frontend contracts, dependency boundaries, types, 1290 node tests,
840 rendered tests, lint, localization and the production build. Exercise
session/notes/API regressions, 53 chat-engine tests and 20 Python/native
integration tests with isolated runtime data.
2026-10-26 22:13:34 +08:00

88 lines
2.3 KiB
TypeScript

import type { AttachmentLimits } from "@/lib/attachment-limits";
import { classifyFile, isSvgFilename } from "@/lib/doc-attachments";
import {
extractBase64FromDataUrl,
readFileAsDataUrl,
} from "@/lib/file-attachments";
export interface PendingAttachment {
type: string;
filename: string;
base64?: string;
previewUrl?: string;
size?: number;
mimeType?: string;
/** A quote's own text (``type: "quote"``), for the composer to show. */
text?: string;
}
/** What one composer attachment becomes on the wire. */
export function toOutgoingAttachment(attachment: PendingAttachment): {
type: string;
filename: string;
base64?: string;
mime_type?: string;
} {
return {
type: attachment.type,
filename: attachment.filename,
base64: attachment.base64,
mime_type: attachment.mimeType,
};
}
export type AttachmentRejectionReason =
| "unsupported"
| "too_large"
| "quota";
export interface AttachmentRejection {
name: string;
reason: AttachmentRejectionReason;
}
export function selectAttachmentFiles(
files: File[],
existingBytes: number,
limits: AttachmentLimits,
): { accepted: File[]; rejected: AttachmentRejection[] } {
let runningTotal = existingBytes;
const accepted: File[] = [];
const rejected: AttachmentRejection[] = [];
for (const file of files) {
if (!classifyFile(file)) {
rejected.push({ name: file.name, reason: "unsupported" });
continue;
}
if (file.size > limits.maxFileBytes) {
rejected.push({ name: file.name, reason: "too_large" });
continue;
}
if (runningTotal + file.size > limits.maxTotalBytes) {
rejected.push({ name: file.name, reason: "quota" });
break;
}
runningTotal += file.size;
accepted.push(file);
}
return { accepted, rejected };
}
export async function fileToPendingAttachment(
file: File,
): Promise<PendingAttachment> {
const raw = await readFileAsDataUrl(file);
const svg = isSvgFilename(file.name) || file.type === "image/svg+xml";
const isImage = !svg && file.type.startsWith("image/");
return {
type: isImage ? "image" : "file",
filename: file.name,
base64: extractBase64FromDataUrl(raw),
previewUrl: isImage || svg ? raw : undefined,
size: file.size,
mimeType: file.type || undefined,
};
}