Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Upload desktop videos so the chat can show them

AdityaUmale6056e799bc1e
diff --git a/src/lib/composer-attachments.ts b/src/lib/composer-attachments.ts
index 40e2fe8eb0..8e7d0ee9e5 100644
--- a/src/lib/composer-attachments.ts
+++ b/src/lib/composer-attachments.ts
@@ -206,6 +206,13 @@ const ACCEPTED_AUDIO_MIMES = new Set([
...Object.values(AUDIO_MIMES_BY_EXTENSION),
"audio/x-m4a", "audio/x-wav", "audio/wave", "audio/x-flac",
]);
+/** Videos the chat plays inline; one past the upload limit keeps its disk path where the app has one. */
+const VIDEO_MIMES_BY_EXTENSION: Readonly<Record<string, string>> = {
+ mp4: "video/mp4",
+ m4v: "video/mp4",
+ mov: "video/quicktime",
+};
+const ACCEPTED_VIDEO_MIMES = new Set(Object.values(VIDEO_MIMES_BY_EXTENSION));
export function documentMime(file: Pick<File, "name" | "type">): string | null {
const declared = file.type.split(";", 1)[0]!.trim().toLowerCase();
@@ -382,9 +389,11 @@ export async function imageAttachmentFromFile(
export async function fileAttachmentFromFile(file: File, anyType = false): Promise<FileAttachment | null> {
const declared = file.type.split(";", 1)[0]!.trim().toLowerCase();
const extension = file.name.split(".").at(-1)?.toLowerCase() ?? "";
+ const video = ACCEPTED_VIDEO_MIMES.has(declared) ? declared : VIDEO_MIMES_BY_EXTENSION[extension];
const mime = documentMime(file) ??
(ACCEPTED_AUDIO_MIMES.has(declared) ? declared : AUDIO_MIMES_BY_EXTENSION[extension]) ??
ARCHIVE_MIMES_BY_EXTENSION[extension] ??
+ (file.size <= FILE_MAX_BYTES ? video : undefined) ??
(anyType ? declared || "application/octet-stream" : undefined);
if (!mime) return null;
if (file.size > FILE_MAX_BYTES) throw new AttachError(file.name, FILE_MAX_BYTES);
diff --git a/src/lib/intake-files.test.ts b/src/lib/intake-files.test.ts
index 7bce323494..ddff211a30 100644
--- a/src/lib/intake-files.test.ts
+++ b/src/lib/intake-files.test.ts
@@ -1,7 +1,7 @@
import { describe, expect, it, vi } from "vitest";
import { TURN_IMAGE_MAX_BYTES as SERVER_TURN_IMAGE_MAX_BYTES, TURN_IMAGE_MAX_COUNT as SERVER_TURN_IMAGE_MAX_COUNT } from "../../server/turn-images";
-import { fitImagesToSlots, IMAGE_MAX_BYTES, imageBytesLeft, imageSlotsLeft, imagesThatFit, intakeFiles, TURN_IMAGE_MAX_BYTES, TURN_IMAGE_MAX_COUNT, type Attachment } from "./composer-attachments";
+import { FILE_MAX_BYTES, fitImagesToSlots, IMAGE_MAX_BYTES, imageBytesLeft, imageSlotsLeft, imagesThatFit, intakeFiles, TURN_IMAGE_MAX_BYTES, TURN_IMAGE_MAX_COUNT, type Attachment } from "./composer-attachments";
type Fake = { name: string; size: number; type: string; text: () => Promise<string> };
const file = (name: string, type: string, size = 10): Fake => ({
@@ -37,6 +37,43 @@ describe("intakeFiles", () => {
fetch.mockRestore();
}
});
+ it("uploads a desktop video into the private store instead of attaching its disk path", async () => {
+ const fetch = vi.spyOn(globalThis, "fetch").mockImplementation(async (_url, init) => {
+ const type = new Headers(init?.headers).get("content-type");
+ const name = type === "video/quicktime" ? "Screen.mov" : "clip.mp4";
+ return new Response(JSON.stringify({ path: `/private/attachments/${name}`, name, bytes: 3 }), {
+ status: 201, headers: { "content-type": "application/json" },
+ });
+ });
+ const getPath = vi.fn(onDisk);
+ try {
+ const out = await intakeFiles([
+ new File(["mp4"], "clip.mp4", { type: "video/mp4" }),
+ new File(["mov"], "Screen.MOV", { type: "" }),
+ ], { allowImages: true, getPath, uploadImage: async () => null });
+ expect(fetch.mock.calls.map(([, init]) => new Headers(init?.headers).get("content-type")).sort()).toEqual(["video/mp4", "video/quicktime"]);
+ expect(out.attachments).toEqual([
+ expect.objectContaining({ kind: "file", path: "/private/attachments/clip.mp4" }),
+ expect.objectContaining({ kind: "file", path: "/private/attachments/Screen.mov" }),
+ ]);
+ expect(out.problems).toEqual([]);
+ expect(getPath).not.toHaveBeenCalled();
+ } finally {
+ fetch.mockRestore();
+ }
+ });
+ it("keeps a desktop video over the upload limit as its disk path", async () => {
+ const fetch = vi.spyOn(globalThis, "fetch");
+ try {
+ const big = { ...file("film.mp4", "video/mp4", FILE_MAX_BYTES + 1), arrayBuffer: async () => new ArrayBuffer(0) };
+ const out = await intakeFiles([big], { allowImages: true, getPath: onDisk, uploadImage: async () => null });
+ expect(out.attachments).toEqual([expect.objectContaining({ kind: "file", path: "/Users/me/film.mp4" })]);
+ expect(out.problems).toEqual([]);
+ expect(fetch).not.toHaveBeenCalled();
+ } finally {
+ fetch.mockRestore();
+ }
+ });
it("uploads a mind map as an XMind workbook though the browser gives it no type", async () => {
const fetch = vi.spyOn(globalThis, "fetch").mockResolvedValue(new Response(JSON.stringify({
path: "/private/attachments/id.md", name: "Plan.xmind.md", bytes: 9,