Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

fix: keep raw image results out of uploads

ogsamratc76764704135
diff --git a/apps/server/src/computer-use/media-capture.ts b/apps/server/src/computer-use/media-capture.ts
index 89116c09..bfbf7a44 100644
--- a/apps/server/src/computer-use/media-capture.ts
+++ b/apps/server/src/computer-use/media-capture.ts
@@ -27,10 +27,12 @@ export function usesComputer(message: unknown): boolean {
return isRecord(message) && Array.isArray(message.parts) && message.parts.some((part) => isRecord(part) && part.type === "tool" && isComputerTool(part.tool));
}
+/** Whether a message may hold tool-result images: an image attachment, or a tool part's metadata (raw MCP results). */
function hasImageAttachment(message: unknown): boolean {
if (!isRecord(message) || !Array.isArray(message.parts)) return false;
- return message.parts.some((part) => isRecord(part) && part.type === "tool" && isRecord(part.state) && Array.isArray(part.state.attachments)
- && part.state.attachments.some((a) => isRecord(a) && typeof a.url === "string" && a.url.startsWith("data:image/")));
+ return message.parts.some((part) => isRecord(part) && part.type === "tool" && isRecord(part.state)
+ && ((Array.isArray(part.state.attachments) && part.state.attachments.some((a) => isRecord(a) && typeof a.url === "string" && a.url.startsWith("data:image/")))
+ || (isRecord(part.state.metadata) && Object.keys(part.state.metadata).length > 0)));
}
export type MediaRewriter = (sessionId: string, message: unknown) => unknown;
diff --git a/apps/server/src/computer-use/media-image.test.ts b/apps/server/src/computer-use/media-image.test.ts
index 0de5cd89..a3aa3182 100644
--- a/apps/server/src/computer-use/media-image.test.ts
+++ b/apps/server/src/computer-use/media-image.test.ts
@@ -198,6 +198,23 @@ describe("media references", () => {
expect(spool.has(meta.sha256, "png")).toBe(true);
});
+ test("raw MCP image results kept in a tool part's metadata become refs too (same frame, one file)", () => {
+ const dir = tmp();
+ const spool = createMediaSpool(path.join(dir, "spool"));
+ const png = encodePng(frame(40, 30, 21));
+ const part = toolPart("m", [dataUrl("image/png", png)]);
+ const withMeta = { ...part, state: { ...part.state, metadata: { omnirushMcpApp: { content: [{ type: "text", text: "{}" }, { type: "image", mimeType: "image/png", data: png.toString("base64") }] } } } };
+ const out = rewriteMessageMedia({ parts: [withMeta] }, { spool, index: createMediaIndex(path.join(dir, "i.json")), limits: {} });
+ const state = (out.message.parts[0] as typeof withMeta).state;
+ expect(JSON.stringify(state)).not.toContain(png.toString("base64"));
+ const content = state.metadata.omnirushMcpApp.content[1] as Record<string, unknown>;
+ expect(content).toEqual({ type: "image", mimeType: "image/png", url: `omnirush-media:${sha256(png)}.png`, sha256: sha256(png) });
+ expect(String((state.attachments[0] as Record<string, unknown>).thumb)).toStartWith("data:image/jpeg");
+ expect(out.files.length).toBe(1);
+ const untouched = { parts: [{ type: "tool", tool: "bash", state: { status: "completed", output: "ok", metadata: { exit: 0 } } }] };
+ expect(rewriteMessageMedia(untouched, { spool, index: createMediaIndex(path.join(dir, "j.json")), limits: {} }).message).toBe(untouched);
+ });
+
test("data URL parsing", () => {
expect(parseImageDataUrl("data:image/png;base64,AQID")).toEqual({ mime: "image/png", bytes: Buffer.from([1, 2, 3]) });
expect(parseImageDataUrl("data:text/plain;base64,AQID")).toBeNull();
diff --git a/apps/server/src/computer-use/shared/PARITY.sha256 b/apps/server/src/computer-use/shared/PARITY.sha256
index eabb3622..b2131ce3 100644
--- a/apps/server/src/computer-use/shared/PARITY.sha256
+++ b/apps/server/src/computer-use/shared/PARITY.sha256
@@ -6,7 +6,7 @@ c601c77fd65561657b103b6ec3c5e4e822d38d4cf9d535da368cc2466698a9d5 policy.mjs
103c58c3908bebab415e5f1878abf752a2b07498f502855298a72cb58225798f policy.d.mts
17e55894695db8183a659a6c868bd8b1ac9348ccb0e0618b3d8aa94adf98d61a image.mjs
f11dd35d6db769398222387695262aeeac5dbaf9df16191d62a8811e8613e25d image.d.mts
-80e88fcc18b489720ef31dd273c15387606df3e9a2ffe4508762c5f51125b33c media.mjs
+98ffcc70c69299917c9804c32b786e58c1677edf7ea0732a3fe12b9f6781ba29 media.mjs
c12540410a558f4b48960c75b7a09e0660d6de679a6e49e2542427530fd9a3d4 media.d.mts
02be6c62de13f3c8a55a0167c751742086c16bfadb201e9ce8e7f1b955d2376d native-worker.mjs
8d53edb30e8d8f582bb02cad06731266f7362dab3d98b08ed8e5d3b308ae7208 portable-host.mjs
diff --git a/apps/server/src/computer-use/shared/media.mjs b/apps/server/src/computer-use/shared/media.mjs
index 73b440cc..a640617c 100644
--- a/apps/server/src/computer-use/shared/media.mjs
+++ b/apps/server/src/computer-use/shared/media.mjs
@@ -241,12 +241,8 @@ export function rewriteMessageMedia(message, ctx) {
let thumbsLeft = Number.isFinite(ctx.thumbBudget) ? ctx.thumbBudget : THUMBS_PER_MESSAGE_BYTES;
const files = [];
const budget = budgets(ctx.limits);
- const parts = message.parts.map((part) => {
- const attachments = isRecord(part) && part.type === "tool" && isRecord(part.state) && Array.isArray(part.state.attachments) ? part.state.attachments : null;
- if (!attachments || !attachments.some((a) => isRecord(a) && typeof a.url === "string" && a.url.startsWith("data:image/"))) return part;
- const next = attachments.map((attachment) => {
- const parsed = isRecord(attachment) ? parseImageDataUrl(attachment.url) : null;
- if (!parsed) return attachment;
+ /** The ref fields of one image (bytes as a data URL), files queued once per chat. */
+ const refOf = (parsed, allowThumb) => {
const sha = sha256(parsed.bytes);
let meta = ctx.spool.meta(sha);
if (!meta) {
@@ -268,11 +264,10 @@ export function rewriteMessageMedia(message, ctx) {
if (model) files.push({ path: paths.model, file: ctx.spool.modelPath(sha, meta.ext), sha, kind: "model", bytes: meta.bytes });
if (full) files.push({ path: paths.full, file: ctx.spool.fullPath(sha), sha, kind: "full", bytes: meta.full.bytes });
}
- const { url: _url, ...rest } = attachment;
- const ref = { ...rest, mime: meta.mime, url: mediaUrl(sha, meta.ext), sha256: sha, width: meta.width, height: meta.height, bytes: meta.bytes };
+ const ref = { mime: meta.mime, url: mediaUrl(sha, meta.ext), sha256: sha, width: meta.width, height: meta.height, bytes: meta.bytes };
if (meta.full) ref.full = entry.f ? { ...meta.full } : { omitted: "budget" };
if (!entry.m) ref.omitted = "budget";
- if (first) {
+ if (first && allowThumb) {
const thumb = ctx.spool.thumb(sha);
if (thumb && thumb.length * 4 / 3 + 32 <= thumbsLeft) {
ref.thumb = `data:image/jpeg;base64,${thumb.toString("base64")}`;
@@ -281,8 +276,41 @@ export function rewriteMessageMedia(message, ctx) {
}
changed = true;
return ref;
- });
- return { ...part, state: { ...part.state, attachments: next } };
+ };
+ // Raw MCP results kept in a tool part's metadata ({ type: "image", data, mimeType }): the bytes become a ref too.
+ const metadataImages = (value, depth = 0) => {
+ if (depth > 6) return value;
+ if (Array.isArray(value)) return value.map((item) => metadataImages(item, depth + 1));
+ if (!isRecord(value)) return value;
+ if (value.type === "image" && typeof value.data === "string" && typeof value.mimeType === "string") {
+ const parsed = parseImageDataUrl(`data:${value.mimeType};base64,${value.data}`);
+ if (!parsed) return value;
+ const { data: _data, ...rest } = value;
+ const ref = refOf(parsed, false);
+ return { ...rest, url: ref.url, sha256: ref.sha256 };
+ }
+ let out = null;
+ for (const [key, item] of Object.entries(value)) {
+ const next = metadataImages(item, depth + 1);
+ if (next !== item) { out ??= { ...value }; out[key] = next; }
+ }
+ return out ?? value;
+ };
+ const parts = message.parts.map((part) => {
+ if (!isRecord(part) || part.type !== "tool" || !isRecord(part.state)) return part;
+ // Attachments first: they carry the chat's first thumbnail of a frame.
+ const attachments = Array.isArray(part.state.attachments) ? part.state.attachments : null;
+ const next = attachments && attachments.some((a) => isRecord(a) && typeof a.url === "string" && a.url.startsWith("data:image/"))
+ ? attachments.map((attachment) => {
+ const parsed = isRecord(attachment) ? parseImageDataUrl(attachment.url) : null;
+ if (!parsed) return attachment;
+ const { url: _url, ...rest } = attachment;
+ return { ...rest, ...refOf(parsed, true) };
+ })
+ : attachments;
+ const metadata = isRecord(part.state.metadata) ? metadataImages(part.state.metadata) : part.state.metadata;
+ if (next === attachments && metadata === part.state.metadata) return part;
+ return { ...part, state: { ...part.state, ...(next !== attachments ? { attachments: next } : {}), ...(metadata !== part.state.metadata ? { metadata } : {}) } };
});
if (changed) ctx.index.save();
return { message: changed ? { ...message, parts } : message, changed, files };
diff --git a/apps/server/src/opencode-plugins/omnirush-extensions-preview.ts b/apps/server/src/opencode-plugins/omnirush-extensions-preview.ts
index 2ffc98ea..0487e751 100644
--- a/apps/server/src/opencode-plugins/omnirush-extensions-preview.ts
+++ b/apps/server/src/opencode-plugins/omnirush-extensions-preview.ts
@@ -932,12 +932,13 @@ export const OmniRushExtensionsPreview = async (factoryInput?: unknown) => {
output.headers["x-omnirush-session-id"] = input.sessionID;
output.headers["x-omnirush-task-id"] = input.message.id;
},
- "tool.execute.after": async (_input: unknown, output: unknown) => {
+ "tool.execute.after": async (input: unknown, output: unknown) => {
// OpenCode 1.17.x keeps the text projection of an MCP result but drops
// structuredContent and result _meta before persisting the completed tool
// part. Preserve those standard fields in the existing metadata channel
// so OmniRush.ai can host the UI without replaying the tool call.
- preserveMcpResult(output);
+ // The built-in computer use server has no app UI: its screenshots stay attachments only.
+ if (!(isRecord(input) && typeof input.tool === "string" && input.tool.startsWith("computer-use_"))) preserveMcpResult(output);
},
"experimental.chat.system.transform": async (input: unknown, output: { system: string[] }) => {
const mergedInput = mergeTransformInputWithFactoryContext(input, factoryContext);