Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(gemini-video): send a clip attached twice once, fit no-clip frames to the media budget, route video-only turns as attachments
AdityaUmale5f2616d87d39
diff --git a/server/turn-images.test.ts b/server/turn-images.test.tsindex fe4cfcb962..586cf5109a 100644--- a/server/turn-images.test.ts+++ b/server/turn-images.test.ts@@ -335,7 +335,7 @@ describe("native video clips", () => {} finally { rmSync(root, { recursive: true, force: true }); }});- it("keeps only the frames that fit beside a sent clip, and every frame when no clip is sent", async () => {+ it("keeps only the frames that fit beside a sent clip, and every frame that fits when no clip is sent", async () => {const frame = (bytes: number) => ({ path: `/tmp/frame-${bytes}.jpg`, mime: "image/jpeg" as const, bytes, source: "video-frame" as const });const frames = [frame(2 * MiB), frame(4 * MiB), frame(MiB)];const sampler = vi.mocked(videoFrameImages), actual = sampler.getMockImplementation()!;@@ -370,6 +370,20 @@ describe("native video clips", () => {} finally { sampler.mockImplementation(actual); }});+ it("sends a file attached twice in a burst once, so a different clip still fits the engine's limit", () => {+ expect(nativeClips([clip(5 * MiB), clip(5 * MiB), clip(4 * MiB)], gemini)).toEqual([clip(5 * MiB), clip(4 * MiB)]);+ });++ it("keeps only the frames that fit beside the turn's pictures when no clip is sent", async () => {+ const frame = (bytes: number) => ({ path: `/tmp/frame-${bytes}.jpg`, mime: "image/jpeg" as const, bytes, source: "video-frame" as const });+ const frames = [frame(2 * MiB), frame(6 * MiB), frame(MiB)];+ const sampler = vi.mocked(videoFrameImages), actual = sampler.getMockImplementation()!;+ sampler.mockImplementation(async (videos) => videos?.length ? frames : []);+ try {+ expect(await videoFramesWithin(2, [clip(MiB)], [], [image(9 * MiB), image(9 * MiB)])).toEqual([frame(2 * MiB), frame(MiB)]);+ } finally { sampler.mockImplementation(actual); }+ });+it("gives no clip to an engine without a video limit, so it keeps getting frames", () => {expect(nativeClips([clip(10)], { nativeImageInput: true })).toEqual([]);expect(nativeClips([clip(10)], { nativeVideoBytes: 100 })).toEqual([]);diff --git a/server/turn-images.ts b/server/turn-images.tsindex f37ac54180..27ad3416fb 100644--- a/server/turn-images.ts+++ b/server/turn-images.ts@@ -176,27 +176,28 @@ export function extractTurnImages(const mediaUrlBytes = (media: readonly { mime: string; bytes: number }[]) => media.reduce((total, item) => total + chatMediaUrlBytes(item), 0);-/** The clips an engine that takes video gets as they are: those within its byte limit that also keep the turn's `images` inside the request's media budget. */+/** The clips an engine that takes video gets as they are, each file once: those within its byte limit that also keep the turn's `images` inside the request's media budget. */export function nativeClips(videos: readonly TurnVideoInput[], engine: Pick<ProviderAdapter["capabilities"], "nativeImageInput" | "nativeVideoBytes">,images: readonly TurnImageInput[] = []): TurnVideoInput[] {const limit = engine.nativeImageInput ? engine.nativeVideoBytes ?? 0 : 0;let clipBytes = 0, encoded = mediaUrlBytes(images);+ const paths = new Set<string>();return videos.filter((clip) => {const url = chatMediaUrlBytes(clip);- if (clipBytes + clip.bytes > limit || encoded + url > CHAT_IMAGE_BUDGET) return false;+ if (paths.has(clip.path) || clipBytes + clip.bytes > limit || encoded + url > CHAT_IMAGE_BUDGET) return false;+ paths.add(clip.path);clipBytes += clip.bytes;encoded += url;return true;});}-/** Stills sampled from a message's videos not `sent` as clips, never taking a turn that already carries `earlier` images past its image count;- * beside a sent clip, only the frames that still fit the request's media budget with the clips and the turn's other pictures (`alongside`). */+/** Stills sampled from a message's videos not `sent` as clips, never taking a turn that already carries `earlier` images past its image count,+ * and only the frames that still fit the request's media budget with the sent clips and the turn's other pictures (`alongside`). */export async function videoFramesWithin(earlier: number, videos: readonly TurnVideoInput[], sent: readonly TurnVideoInput[] = [],alongside: readonly TurnImageInput[] = []): Promise<TurnImageInput[]> {const sentPaths = new Set(sent.map((clip) => clip.path));const frames = await videoFrameImages(videos.filter((video) => !sentPaths.has(video.path)), TURN_IMAGE_MAX_COUNT - earlier);- if (!sent.length) return frames;let room = CHAT_IMAGE_BUDGET - mediaUrlBytes([...sent, ...alongside]);return frames.filter((frame) => {const url = chatMediaUrlBytes(frame);diff --git a/server/turn-route.test.ts b/server/turn-route.test.tsindex 18a750957a..04c119091f 100644--- a/server/turn-route.test.ts+++ b/server/turn-route.test.ts@@ -136,6 +136,13 @@ describe("routeDirectTurn", () => {expect(routeDirectTurn(bot, {}, worked, message, instance, false).tier).toBe("complex");});+ it("keeps a message whose only attachment is a video on the configured model", () => {+ const video = { text: "and the other one?", images: [], videos: [{ path: "/attachments/clip.mp4" }] };+ const route = routeDirectTurn(bot, {}, [], video, instance, false);+ expect(route.tier).toBe("complex");+ expect(route.quickModel).toBeUndefined();+ });+it("never moves a short line when the sign-in can refuse a catalog model, as Codex on a ChatGPT account does", () => {const codex = { models: mixed, adapter: { capabilities: { effortLevels: ["low", "high"] as const } } };const own = { replyStyle: "conversational" as const, modelSelection: { instanceId: "codex", model: "gpt-5.4", effort: "high" as const } };diff --git a/server/turn-route.ts b/server/turn-route.tsindex 1a9883ff23..5b7de62130 100644--- a/server/turn-route.ts+++ b/server/turn-route.ts@@ -93,7 +93,7 @@ export function routeDirectTurn(bot: { replyStyle?: ReplyStyle; modelSelection: ModelSelection },task: { modelSelection?: ModelSelection; usage?: { context?: { tokens: number } } },path: readonly { role: string; kind?: string }[],- message: { text: string; images: readonly unknown[] },+ message: { text: string; images: readonly unknown[]; videos?: readonly unknown[] },instance: { models?: ModelCatalog; quickModelsUsable?: () => boolean; adapter: { capabilities: { effortLevels?: readonly EffortLevel[] } } },automated: boolean,refused?: ReadonlySet<string>,@@ -105,7 +105,7 @@ export function routeDirectTurn(automated,signals: {text: message.text,- attachments: message.images.length,+ attachments: message.images.length + (message.videos?.length ?? 0),priorTurnUsedTools: lastAsk >= 0 && path.slice(lastAsk + 1).some((m) => m.kind === "activity"),},model: bot.modelSelection.model,