Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(chat): mark where a replayed image was sent so the model doesn't read it as new
AdityaUmaleda4a77ccadb7
diff --git a/server/turn-images.test.ts b/server/turn-images.test.tsindex 53e389a1e9..2078ba58c5 100644--- a/server/turn-images.test.ts+++ b/server/turn-images.test.ts@@ -4,7 +4,7 @@ import { join } from "node:path";import { afterEach, describe, expect, it } from "vitest";-import { extractReplayImages, extractSteerImages, extractTurnImages } from "./turn-images.ts";+import { extractReplayImages, extractSteerImages, extractTurnImages, REPLAYED_IMAGE_NOTE } from "./turn-images.ts";const IDS = ["123e4567-e89b-42d3-a456-426614174000",@@ -224,10 +224,35 @@ describe("turn image admission", () => {const result = extractReplayImages(transcript, current, { attachmentsDir: root, maxCount: 3 });expect(result.images.map((entry) => entry.path)).toEqual([join(root, `${IDS[2]}.png`), join(root, `${IDS[3]}.png`)]);- expect(result.transcript.map((entry) => entry.text)).toEqual([transcript[0]!.text, transcript[1]!.text, "older\n\n", "newest\n\n"]);+ expect(result.transcript.map((entry) => entry.text)).toEqual([+ transcript[0]!.text, transcript[1]!.text, `older\n\n${REPLAYED_IMAGE_NOTE}`, `newest\n\n${REPLAYED_IMAGE_NOTE}`,+ ]);expect(transcript[3]!.text).toContain("<attached-image");});+ it("marks where a replayed image was sent, so a later text-only turn never looks like it carries a new image", () => {+ const root = store();+ const tag = (index: number) => `<attached-image path="${image(root, index)}" name="${index}.png" />`;+ const transcript = [+ { role: "user" as const, text: `login fails, see screenshot\n\n${tag(0)}` },+ { role: "assistant" as const, text: "I see the error." },+ { role: "user" as const, text: "A" },+ ];++ const result = extractReplayImages(transcript, [], { attachmentsDir: root });++ expect(result.images.map((entry) => entry.path)).toEqual([join(root, `${IDS[0]}.png`)]);+ expect(result.transcript[0]!.text).toBe(`login fails, see screenshot\n\n${REPLAYED_IMAGE_NOTE}`);+ expect(REPLAYED_IMAGE_NOTE).toMatch(/earlier/i);+ expect(result.transcript[2]!.text).toBe("A");+ });++ it("keeps a current turn's own images unmarked", () => {+ const root = store();+ const result = extractTurnImages(`look\n\n<attached-image path="${image(root, 0)}" name="0.png" />`, { attachmentsDir: root });+ expect(result.text).toBe("look\n\n");+ });+it("stops at the first replayed message whose images no longer fit", () => {const root = store();const tag = (index: number, bytes = 4) => `<attached-image path="${image(root, index, ".png", bytes)}" name="${index}.png" />`;diff --git a/server/turn-images.ts b/server/turn-images.tsindex 9b37333fe7..a84a63fed0 100644--- a/server/turn-images.ts+++ b/server/turn-images.ts@@ -115,11 +115,20 @@ const ownedImage = (path: string, attachmentsDir: string): TurnImageInput | nullconst ownedVideo = (path: string, attachmentsDir: string): TurnVideoInput | null =>ownedAttachment(path, attachmentsDir, OWNED_VIDEO_NAME, VIDEO_MIME_BY_EXTENSION, FILE_MAX_BYTES);+/**+ * Left where a replayed message's image was: the image itself rides on the current turn, so without this the model+ * reads an old screenshot as part of the latest message.+ */+export const REPLAYED_IMAGE_NOTE =+ "[An image was attached to this earlier message. It is attached again to this turn only so you can still see it; it is not new and was not sent with the latest message.]";+export interface ExtractTurnImagesOptions {attachmentsDir?: string;maxCount?: number;maxBytes?: number;maxVideoCount?: number;+ /** Text left in place of each admitted image tag. */+ imageNote?: string;}/**@@ -158,7 +167,8 @@ export function extractTurnImages(let providerText = text;const removedTags = admittedImages.map((item) => item.tag).sort((a, b) => b.start - a.start);- for (const tag of removedTags) providerText = providerText.slice(0, tag.start) + providerText.slice(tag.end);+ const note = options.imageNote ?? "";+ for (const tag of removedTags) providerText = providerText.slice(0, tag.start) + note + providerText.slice(tag.end);return { text: providerText, images: admittedImages.map((item) => item.image), videos };}@@ -202,7 +212,7 @@ export function extractReplayImages<T extends { role: "user" | "assistant"; textfor (let index = replayed.length - 1; index >= 0 && maxCount > 0; index -= 1) {const entry = replayed[index]!;if (entry.role !== "user") continue;- const item = extractTurnImages(entry.text, { ...options, maxCount: Infinity, maxBytes: Infinity });+ const item = extractTurnImages(entry.text, { ...options, maxCount: Infinity, maxBytes: Infinity, imageNote: REPLAYED_IMAGE_NOTE });if (!item.images.length) continue;const bytes = item.images.reduce((total, image) => total + image.bytes, 0);if (item.images.length > maxCount || bytes > maxBytes) break;