Skip to content

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

fix(antigravity): address review on media links

kengnattakit91d43c908fd7
diff --git a/server/drivers/acp/core.ts b/server/drivers/acp/core.ts
index 1d15f776c7..970d64921a 100644
--- a/server/drivers/acp/core.ts
+++ b/server/drivers/acp/core.ts
@@ -1389,7 +1389,7 @@ export function createAcpDriver(support: AcpSupport): ProviderDriver<AcpConfig>
: promptInput.text;
const imageBlocks = support.images === true && runtimeAcceptsImages
? await readAcpImageBlocks(turn.images ?? [])
- : []; const media = await readAcpMediaLinks(support.mediaLinks === true && !turn.guestConfined ? promptTurn.text : "");
+ : []; const media = await readAcpMediaLinks(support.mediaLinks === true && !turn.guestConfined ? turn.text : "", imageBlocks.reduce((sum, block) => sum + Math.floor(block.data.length * 3 / 4), 0));
if (support.modelVariants && !modelControls.confirms(cliTurn.model)) {
throw new Error(`${support.displayName} changed model before the prompt`);
}
diff --git a/server/drivers/acp/media-links.test.ts b/server/drivers/acp/media-links.test.ts
index 6c4f9eece0..c089671d0c 100644
--- a/server/drivers/acp/media-links.test.ts
+++ b/server/drivers/acp/media-links.test.ts
@@ -29,6 +29,11 @@ describe("findMediaPaths", () => {
expect((await findMediaPaths(text)).map((file) => file.path)).toEqual([clip, photo]);
});
+ it("treats two spellings of the same file as one", async () => {
+ const other = process.platform === "win32" ? photo.replaceAll("\\", "/") : photo.replace(scratch, `${scratch}/.`);
+ expect((await findMediaPaths(`${photo} and ${other}`)).map((file) => file.path)).toEqual([photo]);
+ });
+
it("ignores text without media", async () => {
expect(await findMediaPaths("see notes.md and https://example.com/a.png")).toEqual([]);
});
@@ -36,7 +41,7 @@ describe("findMediaPaths", () => {
describe("readAcpMediaLinks", () => {
it("links files within the budget and names the ones left out", async () => {
- const { blocks, note } = await readAcpMediaLinks(`${clip}\n${big}\n${photo}`, 600);
+ const { blocks, note } = await readAcpMediaLinks(`${clip}\n${big}\n${photo}`, 0, 600);
expect(blocks).toEqual([
{ type: "resource_link", uri: acpMediaUri(clip), name: "clip one.mp4" },
{ type: "resource_link", uri: acpMediaUri(photo), name: "photo.png" },
@@ -45,6 +50,24 @@ describe("readAcpMediaLinks", () => {
expect(note).toContain(`Not attached: ${big} (0.0 MB)`);
});
+ it("counts media the prompt already carries against the same budget", async () => {
+ const { blocks, note } = await readAcpMediaLinks(`${clip}\n${photo}`, 350, 600);
+ expect(blocks).toEqual([{ type: "resource_link", uri: acpMediaUri(photo), name: "photo.png" }]);
+ expect(note).toContain(`Not attached: ${clip} (0.0 MB)`);
+ expect(note).toContain("images attached to it included");
+ });
+
+ it("keeps looking past many files that do not fit for one that does", async () => {
+ const many = Array.from({ length: 20 }, (_, index) => {
+ const file = join(scratch, `huge ${index}.mp4`);
+ writeFileSync(file, Buffer.alloc(900));
+ return file;
+ });
+ const { blocks, note } = await readAcpMediaLinks([...many, photo].join("\n"), 0, 600);
+ expect(blocks).toEqual([{ type: "resource_link", uri: acpMediaUri(photo), name: "photo.png" }]);
+ expect(note).toContain(many[19]);
+ });
+
it("adds nothing when the text names no media", async () => {
expect(await readAcpMediaLinks("no media here")).toEqual({ blocks: [], note: "" });
});
diff --git a/server/drivers/acp/media-links.ts b/server/drivers/acp/media-links.ts
index 200d7b8146..e5ce0baa28 100644
--- a/server/drivers/acp/media-links.ts
+++ b/server/drivers/acp/media-links.ts
@@ -34,37 +34,39 @@ export function acpMediaUri(path: string, platform: NodeJS.Platform = process.pl
return platform === "win32" ? `file:${encoded.replace(/^([A-Za-z])%3A/u, "$1:")}` : `file://${encoded}`;
}
-/** Existing local media files named in `text`, in order and without repeats. Spaces in a path are allowed:
- * each candidate start on the line is tried from the nearest, and the first that names a file wins. */
+/** Existing local media files named in `text`, in order and without repeats (the same file named twice, by
+ * file identity, so case-sensitive file systems keep distinct names). Spaces in a path are allowed: each
+ * candidate start on the line is tried from the nearest, and the first that names a file wins. */
export async function findMediaPaths(text: string): Promise<Array<{ path: string; size: number }>> {
const found: Array<{ path: string; size: number }> = [];
const seen = new Set<string>();
for (const match of text.matchAll(MEDIA_END)) {
- if (found.length >= ACP_MEDIA_MAX_FILES * 2) break;
const end = match.index + match[0].length;
const line = text.slice(text.lastIndexOf("\n", match.index) + 1, end);
const starts = [...line.matchAll(PATH_START)].map((start) => start.index).reverse();
for (const start of starts) {
const path = line.slice(start).trim();
- const key = path.toLowerCase().replaceAll("\\", "/");
- if (seen.has(key)) break;
- const info = await stat(path).catch(() => null);
- if (info?.isFile()) {
+ const info = await stat(path, { bigint: true }).catch(() => null);
+ if (!info?.isFile()) continue;
+ const key = `${info.dev}:${info.ino}`;
+ if (!seen.has(key)) {
seen.add(key);
- found.push({ path, size: info.size });
- break;
+ found.push({ path, size: Number(info.size) });
}
+ break;
}
}
return found;
}
-/** Prompt blocks for the media named in `text`, within one prompt's budget, and a note saying what was and was not attached. */
-export async function readAcpMediaLinks(text: string, budget = ACP_MEDIA_MAX_BYTES): Promise<AcpMediaLinks> {
+/** Prompt blocks for the media named in `text`, within one prompt's budget, and a note saying what was and was
+ * not attached. `usedBytes` is media the prompt already carries (composer images): it counts against the same
+ * budget, so when it alone fills the budget every named file is left out and named in the note. */
+export async function readAcpMediaLinks(text: string, usedBytes = 0, budget = ACP_MEDIA_MAX_BYTES): Promise<AcpMediaLinks> {
const blocks: AcpResourceLink[] = [];
const attached: string[] = [];
const skipped: Array<{ path: string; size: number }> = [];
- let total = 0;
+ let total = usedBytes;
for (const file of await findMediaPaths(text)) {
if (attached.length < ACP_MEDIA_MAX_FILES && total + file.size <= budget) {
total += file.size;
@@ -81,7 +83,7 @@ export async function readAcpMediaLinks(text: string, budget = ACP_MEDIA_MAX_BYT
if (skipped.length) {
const limitMb = Math.floor(budget / 1048576);
note += `\n\n(Not attached: ${skipped.map((file) => `${file.path} (${(file.size / 1048576).toFixed(1)} MB)`).join(" | ")}.` +
- ` One message carries at most ${limitMb} MB of media and a file tool cannot show media, so you cannot see these.` +
+ ` One message carries at most ${limitMb} MB of media${usedBytes > 0 ? ", images attached to it included," : ""} and a file tool cannot show media, so you cannot see these.` +
` Say so plainly; to view one, shrink or split it into parts of ${limitMb} MB or less and ask to have each part sent in its own message.)`;
}
return { blocks, note };