Skip to content

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

fix(gemini-video): queue clips sent mid-turn, match sent clips by path, count later lines' pictures

AdityaUmaled8da9e6e564f
diff --git a/server/index.ts b/server/index.ts
index 4badb44d5c..e168c2d248 100644
--- a/server/index.ts
+++ b/server/index.ts
@@ -7240,11 +7240,11 @@ async function acceptDirectSend(
const instance = runningTurnInstance(currentAtStart, threadId);
let steered: SteerOutcome = "refused";
const steerInput = extractTurnImages(text);
- const carriesImages = steerInput.images.length > 0;
+ const carriesVideos = steerInput.videos.length > 0, carriesImages = steerInput.images.length > 0 || carriesVideos;
const steerTarget = handoffs.current(threadId);
const busyAdmission = admit("direct-busy", {
carriesImages,
- engineCanSteerImages: engineSteersImages(instance),
+ engineCanSteerImages: !carriesVideos && engineSteersImages(instance),
pendingComputerSelection: Boolean(computerSelectionTurns.get(threadId)?.selected),
engineCanSteer: Boolean(instance?.adapter.capabilities.queueing && instance.adapter.steer),
followUp,
diff --git a/server/turn-images.test.ts b/server/turn-images.test.ts
index 5c3667f2dd..fe4cfcb962 100644
--- a/server/turn-images.test.ts
+++ b/server/turn-images.test.ts
@@ -5,7 +5,7 @@ import { join } from "node:path";
import { afterEach, describe, expect, it, vi } from "vitest";
import { chatUserContent } from "./drivers/chat-images.ts";
-import { extractReplayImages, extractSteerImages, extractTurnImages, nativeClips, REPLAYED_IMAGE_NOTE, turnMedia } from "./turn-images.ts";
+import { extractReplayImages, extractSteerImages, extractTurnImages, nativeClips, REPLAYED_IMAGE_NOTE, turnMedia, videoFramesWithin } from "./turn-images.ts";
import { videoFrameImages } from "./turn-video.ts";
vi.mock("./turn-video.ts", async (original) => {
@@ -319,7 +319,8 @@ describe("native video clips", () => {
const fits = [png(root, "c.png", 6 * MiB - 30), png(root, "d.png", 6 * MiB - 30)];
const videos = nativeClips([video], gemini, fits);
expect(videos).toEqual([video]);
- expect(() => chatUserContent({ text: "look", images: fits, videos } as Parameters<typeof chatUserContent>[0])).not.toThrow();
+ const content = chatUserContent({ text: "look", images: fits, videos } as Parameters<typeof chatUserContent>[0]);
+ expect(content).toContainEqual({ type: "image_url", image_url: { url: `data:video/mp4;base64,${Buffer.alloc(12 * MiB).toString("base64")}` } });
} finally { rmSync(root, { recursive: true, force: true }); }
});
@@ -348,6 +349,27 @@ describe("native video clips", () => {
} finally { sampler.mockImplementation(actual); }
});
+ it("samples no frames from a clip that is sent, even when its record is a copy", async () => {
+ const sampler = vi.mocked(videoFrameImages), actual = sampler.getMockImplementation()!;
+ sampler.mockImplementation(async (videos) => videos?.length ? [{ path: "/tmp/f.jpg", mime: "image/jpeg", bytes: 1, source: "video-frame" }] : []);
+ try {
+ expect(await videoFramesWithin(0, [clip(MiB)], [{ ...clip(MiB) }])).toEqual([]);
+ } finally { sampler.mockImplementation(actual); }
+ });
+
+ it("keeps a burst's frames and pictures within the turn's picture count, counting later lines' pictures", async () => {
+ const frame = (n: number) => ({ path: `/tmp/frame-${n}.jpg`, mime: "image/jpeg" as const, bytes: 1, source: "video-frame" as const });
+ const frames = Array.from({ length: 10 }, (_, n) => frame(n));
+ const sampler = vi.mocked(videoFrameImages), actual = sampler.getMockImplementation()!;
+ sampler.mockImplementation(async (videos, limit = Infinity) => videos?.length ? frames.slice(0, Math.max(0, limit)) : []);
+ try {
+ const pictures = Array.from({ length: 6 }, (_, n) => image(n + 1));
+ const media = await turnMedia([{ images: [], videos: [clip(20 * MiB)] }, { images: pictures, videos: [] }], gemini);
+ expect(media.images).toHaveLength(10);
+ expect(media.images.slice(-6)).toEqual(pictures);
+ } 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.ts
index 8e2723a521..f37ac54180 100644
--- a/server/turn-images.ts
+++ b/server/turn-images.ts
@@ -194,7 +194,8 @@ export function nativeClips(videos: readonly TurnVideoInput[], engine: Pick<Prov
* 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`). */
export async function videoFramesWithin(earlier: number, videos: readonly TurnVideoInput[], sent: readonly TurnVideoInput[] = [],
alongside: readonly TurnImageInput[] = []): Promise<TurnImageInput[]> {
- const frames = await videoFrameImages(videos.filter((video) => !sent.includes(video)), TURN_IMAGE_MAX_COUNT - earlier);
+ 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) => {
@@ -205,14 +206,14 @@ export async function videoFramesWithin(earlier: number, videos: readonly TurnVi
});
}
-/** A burst's media: clips chosen once for all its lines against every line's pictures, so it fits one request, then each line's pictures and the frames of its clips not sent. */
+/** A burst's media: clips chosen once for all its lines against every line's pictures, then each line's pictures and the frames of its clips not sent. */
export async function turnMedia(lines: readonly { images: readonly TurnImageInput[]; videos: readonly TurnVideoInput[] }[],
engine: Pick<ProviderAdapter["capabilities"], "nativeImageInput" | "nativeVideoBytes">): Promise<{ images: TurnImageInput[]; videos: TurnVideoInput[] }> {
const pictures = lines.flatMap((line) => line.images);
const videos = nativeClips(lines.flatMap((line) => line.videos), engine, pictures);
const images: TurnImageInput[] = [], frames: TurnImageInput[] = [];
for (const line of lines) {
- const sampled = await videoFramesWithin(images.length + line.images.length, line.videos, videos, [...pictures, ...frames]);
+ const sampled = await videoFramesWithin(pictures.length + frames.length, line.videos, videos, [...pictures, ...frames]);
frames.push(...sampled);
images.push(...line.images, ...sampled);
}