Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Attach over-limit files by their place on the desktop's own server
AdityaUmale0029b25d8bb9
diff --git a/src/components/Composer.tsx b/src/components/Composer.tsxindex 2e620f9fdd..e179c1f191 100644--- a/src/components/Composer.tsx+++ b/src/components/Composer.tsx@@ -39,7 +39,7 @@ import {import { DEVS_CHOICE, MentionMenu, teammateChoices, type MentionChoice } from "./MentionMenu"; import { useTeammates } from "@/state/team-people"; import { hiddenFromSidebar, useViewerEmail } from "@/lib/default-bot"; import { useMayAct } from "./SharedQueueLine";import { SlashCommandMenu } from "./SlashCommandMenu"; import { ComposerFailedSends } from "./ComposerFailedSends";import { MentionTextarea } from "./MentionTextarea"; import { useSuggestedReply } from "./use-suggested-reply"; import { SUGGESTION_HINT_ID, SuggestedReplyGhost } from "./SuggestedReplyGhost";-import { ComposerAttachments, pathForFile } from "./ComposerAttachments";+import { ComposerAttachments, localPathForFile, pathForFile } from "./ComposerAttachments";import { PointedChips } from "./app/PointedChips"; import { SetupRequestChip, setupRequestPlaceholder } from "./SetupOffer";import { takePointedChips } from "@/lib/pointed-chips";import { openPageFor } from "@/lib/open-page";@@ -515,6 +515,7 @@ export function Composer({const { attachments: added, notice } = await intakeFilesWithNotice(Array.from(picked), {allowImages: engineSupportsImages,getPath: pathForFile,+ localPath: localPathForFile,uploadImage,imageSlots: imageSlotsLeft(attachments),imageBytes: imageBytesLeft(attachments),diff --git a/src/components/ComposerAttachments.tsx b/src/components/ComposerAttachments.tsxindex ca23a77e31..5b5afcbfbb 100644--- a/src/components/ComposerAttachments.tsx+++ b/src/components/ComposerAttachments.tsx@@ -20,6 +20,11 @@ export function pathForFile(file: File): string {return window.ogb?.getPathForFile?.(file) ?? "";}+/** A file's place for a bot on this same computer: the desktop app on its own server, never one paired to another. */+export function localPathForFile(file: File): string {+ return window.ogb?.remoteClient?.active === true ? "" : pathForFile(file);+}+export function ComposerAttachments({items,onAdd,@@ -84,6 +89,7 @@ export function ComposerAttachments({const { attachments, problems } = await intakeFiles(files, {allowImages: callbacks.current.allowImages,getPath: pathForFile,+ localPath: localPathForFile,uploadImage: callbacks.current.uploadImage,imageSlots: callbacks.current.imageSlots,imageBytes: callbacks.current.imageBytes,diff --git a/src/lib/composer-attachments.ts b/src/lib/composer-attachments.tsindex a5ccf29e29..f7d3424987 100644--- a/src/lib/composer-attachments.ts+++ b/src/lib/composer-attachments.ts@@ -793,13 +793,28 @@ export async function intakeFiles<T extends DroppedFile & { type: string }>(imageSlots?: number;/** Image bytes the message can still take; images past it are left out the same way. */imageBytes?: number;+ /** The file's place on the computer the bot runs on, or "": a file over the upload limits is attached by that place+ * instead, so the bot reads the original there. Only the desktop app on its own server gives one. */+ localPath?: (file: T) => string;},): Promise<{ attachments: Attachment[]; problems: AttachProblem[]; notice: string | null }> {const { allowImages, getPath, uploadImage } = _opts;const images = allowImages ? _files.filter((file) => isImageFile(file)) : [];const overflow = new Set(fitImagesToSlots(images, _opts.imageSlots ?? Infinity, _opts.imageBytes).leftOut);- const leftOut = [...overflow].map((file) => file.name);- const files = _files.filter((file) => !overflow.has(file));+ const inPlace = new Map<T, FileAttachment>();+ if (_opts.localPath) {+ for (const file of _files) {+ if (!overflow.has(file) && file.size <= (allowImages && isImageFile(file) ? IMAGE_MAX_BYTES : FILE_MAX_BYTES)) continue;+ let path = "";+ try {+ path = _opts.localPath(file);+ } catch {+ }+ if (path) inPlace.set(file, fileAttachment(file.name, path, file.size));+ }+ }+ const leftOut = [...overflow].filter((file) => !inPlace.has(file)).map((file) => file.name);+ const files = _files.filter((file) => !overflow.has(file) || inPlace.has(file));const uploadFile = _opts.uploadFile ?? (async (file: T, anyType?: boolean) => {if (!file.arrayBuffer) return null;return fileAttachmentFromFile(file as unknown as File, anyType);@@ -815,6 +830,8 @@ export async function intakeFiles<T extends DroppedFile & { type: string }>(uploadError?: AttachProblem;uploadErrorText?: string;}> => {+ const located = inPlace.get(file);+ if (located) return { attachments: [located], rejectedNames: [] };if (allowImages && isImageFile(file)) {try {const attachment = await uploadImage(file);diff --git a/src/lib/intake-files.test.ts b/src/lib/intake-files.test.tsindex ddff211a30..7204dc0c6e 100644--- a/src/lib/intake-files.test.ts+++ b/src/lib/intake-files.test.ts@@ -274,3 +274,60 @@ describe("intakeFiles", () => {expect(notices).toEqual(["A message takes up to 10 images, 40 MB in all. 11.png wasn't attached."]);});});++describe("intakeFiles on the desktop app's own server: files over the upload limits go by their place", () => {+ const big = (name: string, type: string, size: number) => ({ ...file(name, type, size), arrayBuffer: async () => new ArrayBuffer(0) });++ it("attaches an image over the image limit by its place, keeping its name, without uploading it", async () => {+ const uploadImage = vi.fn(upload);+ const out = await intakeFiles([big("Original photo.png", "image/png", IMAGE_MAX_BYTES + 1), file("small.png", "image/png")], {+ allowImages: true, getPath: onDisk, localPath: onDisk, uploadImage,+ });+ expect(out.attachments).toEqual([+ expect.objectContaining({ kind: "file", name: "Original photo.png", path: "/Users/me/Original photo.png", size: IMAGE_MAX_BYTES + 1 }),+ expect.objectContaining({ kind: "image", name: "small.png" }),+ ]);+ expect(uploadImage.mock.calls.map(([f]) => f.name)).toEqual(["small.png"]);+ expect(out.problems).toEqual([]);+ });++ it("attaches the images past the message's count and bytes by their place instead of leaving them out", async () => {+ const uploadImage = vi.fn(upload);+ const out = await intakeFiles(Array.from({ length: 12 }, (_, index) => file(`${index + 1}.jpg`, "image/jpeg")), {+ allowImages: true, getPath: onDisk, localPath: onDisk, uploadImage, imageSlots: TURN_IMAGE_MAX_COUNT,+ });+ expect(uploadImage).toHaveBeenCalledTimes(TURN_IMAGE_MAX_COUNT);+ expect(out.attachments.filter((a) => a.kind === "file").map((a) => "path" in a ? a.path : "")).toEqual(["/Users/me/11.jpg", "/Users/me/12.jpg"]);+ expect(out.attachments).toHaveLength(12);+ expect(out.notice).toBeNull();+ });++ it("attaches a document over the file limit by its place", async () => {+ const uploadFile = vi.fn(async () => null);+ const out = await intakeFiles([big("Scans.pdf", "application/pdf", FILE_MAX_BYTES + 1)], {+ allowImages: true, getPath: onDisk, localPath: onDisk, uploadImage: upload, uploadFile,+ });+ expect(out.attachments).toEqual([expect.objectContaining({ kind: "file", name: "Scans.pdf", path: "/Users/me/Scans.pdf" })]);+ expect(uploadFile).not.toHaveBeenCalled();+ expect(out.problems).toEqual([]);+ });++ it("keeps today's limits where the file has no place on the bot's computer (phone, browser, a paired window)", async () => {+ const out = await intakeFiles([big("huge.png", "image/png", IMAGE_MAX_BYTES + 1), file("1.png", "image/png"), file("2.png", "image/png")], {+ allowImages: true, getPath: onDisk, localPath: () => "", imageSlots: 1,+ uploadImage: async (f) => {+ if (f.size > IMAGE_MAX_BYTES) throw new Error(`${f.name} exceeds 10 MB`);+ return upload(f);+ },+ });+ expect(out.attachments.map((a) => "name" in a ? a.name : "")).toEqual(["1.png"]);+ expect(out.notice).toBe("A message takes up to 10 images, 40 MB in all. 2.png wasn't attached. huge.png: huge.png exceeds 10 MB");+ });++ it("still uploads files within the limits", async () => {+ const localPath = vi.fn(onDisk);+ const out = await intakeFiles([file("shot.png", "image/png")], { allowImages: true, getPath: onDisk, localPath, uploadImage: upload });+ expect(out.attachments).toEqual([expect.objectContaining({ kind: "image", path: "/api/attachments/shot.png" })]);+ expect(localPath).not.toHaveBeenCalled();+ });+});