mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 05:59:36 +08:00
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.
88 lines
2.3 KiB
TypeScript
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,
|
|
};
|
|
}
|