A desktop coding agent with free access to frontier models.
TypeScript
fix(engine2): keep attachment notes out of the user's message
Samrat Talukdara93719ebcfc8
diff --git a/apps/app/tests/session-sync-tool-parts.test.ts b/apps/app/tests/session-sync-tool-parts.test.tsindex 62d6f6ca..ce527443 100644--- a/apps/app/tests/session-sync-tool-parts.test.ts+++ b/apps/app/tests/session-sync-tool-parts.test.ts@@ -503,3 +503,44 @@ test("live attachment notes render once across repeated updates", () => {cleanup();}});++test("a user message with a pasted image renders only the typed text and the image", () => {+ const syncInput = { workspaceId: "workspace-paste", baseUrl: "http://127.0.0.1:1234", omnirushToken: "token" };+ const cleanup = __createWorkspaceSessionSyncForTest(syncInput);+ const release = trackWorkspaceSessionSync(syncInput, "session-paste");+ try {+ __applySessionSyncEventForTest(syncInput, {+ type: "message.updated",+ properties: { info: { id: "msg-paste", role: "user", sessionID: "session-paste" } },+ });+ // The parts the 2.x engine adapter emits for the prompt the composer sent:+ // the workspace-path note (synthetic), what was typed, and the image.+ const note = [+ "Attached files were copied into this worker workspace for tool access:",+ "- image.png: .opencode/omnirush/inbox/chat-attachments/session-paste/1-image.png (file:///Users/someone/project/.opencode/omnirush/inbox/chat-attachments/session-paste/1-image.png)",+ "Use these paths with Read/Bash/MCP/Docling when a tool needs the file bytes.",+ ].join("\n");+ const parts = [+ { id: "prt_paste_u0", type: "text", synthetic: true, text: note, metadata: { omnirushAttachments: [] } },+ { id: "prt_paste_u1", type: "text", text: "hi" },+ { id: "prt_paste_f0", type: "file", mime: "image/png", filename: "image.png", url: "data:image/png;base64,AAAA" },+ ];+ for (const part of parts) {+ __applySessionSyncEventForTest(syncInput, {+ type: "message.part.updated",+ properties: { part: { ...part, messageID: "msg-paste", sessionID: "session-paste" } },+ });+ }+ const transcript = getReactQueryClient().getQueryData<UIMessage[]>(transcriptKey("workspace-paste", "session-paste"));+ const rendered = transcript?.[0]?.parts ?? [];+ expect(rendered).toMatchObject([+ { type: "text", text: "hi" },+ { type: "file", filename: "image.png", mediaType: "image/png" },+ ]);+ expect(rendered).toHaveLength(2);+ expect(JSON.stringify(rendered)).not.toContain("file:///Users/");+ } finally {+ release();+ cleanup();+ }+});diff --git a/apps/server/src/engine2/events.ts b/apps/server/src/engine2/events.tsindex f6e64a39..101fd57d 100644--- a/apps/server/src/engine2/events.ts+++ b/apps/server/src/engine2/events.ts@@ -18,12 +18,13 @@ import {partId,v1AssistantMessage,v1Error,+ v1UserTextParts,v1PermissionRequest,v1QuestionRequest,v1Session,type QuestionMapping,} from "./shapes.js";-import { arr, isRecord, num, omitUndefined, record, str, type JsonRecord } from "./util.js";+import { arr, isRecord, num, omitUndefined, promptFileUrl, record, str, type JsonRecord } from "./util.js";export type V1Event = { type: string; properties: JsonRecord };/** A translated event and the directory it belongs to (null: every directory). */@@ -272,13 +273,17 @@ export class EventTranslator {const text = str(payload, "text") ?? "";const files = arr(payload, "files");if (text || files.length === 0) {- out.push(this.scoped(sessionID, "message.part.updated", { sessionID, part: { id: partId(id, "u0"), sessionID, messageID: id, type: "text", text }, time: created }));+ const layout = record(record(payload, "metadata"), "omnirush")?.textParts;+ for (const part of v1UserTextParts({ messageID: id, sessionID }, text, layout, false)) {+ out.push(this.scoped(sessionID, "message.part.updated", { sessionID, part, time: created }));+ }}files.forEach((file, index) => {- if (!isRecord(file) || typeof file.uri !== "string") return;+ const url = promptFileUrl(file);+ if (!isRecord(file) || !url) return;out.push(this.scoped(sessionID, "message.part.updated", {sessionID,- part: omitUndefined({ id: partId(id, `f${index}`), sessionID, messageID: id, type: "file", mime: str(file, "mime") ?? "application/octet-stream", filename: str(file, "name"), url: file.uri }),+ part: omitUndefined({ id: partId(id, `f${index}`), sessionID, messageID: id, type: "file", mime: str(file, "mime") ?? "application/octet-stream", filename: str(file, "name"), url }),time: created,}));});diff --git a/apps/server/src/engine2/facade.test.ts b/apps/server/src/engine2/facade.test.tsindex 782236c9..6106daec 100644--- a/apps/server/src/engine2/facade.test.ts+++ b/apps/server/src/engine2/facade.test.ts@@ -178,6 +178,28 @@ describe("1.x engine adapter over the 2.x engine", () => {expect(recorded.map((call) => call.path)).toEqual([`/api/session/${SESSION}/prompt`]);});+ test("a synthetic attachment note reaches the model and its text-part layout is kept", async () => {+ recorded.length = 0;+ const note = "Attached files were copied into this worker workspace for tool access:\n- image.png: .opencode/omnirush/inbox/chat-attachments/s/1-image.png (file:///home/u/p/.opencode/omnirush/inbox/chat-attachments/s/1-image.png)";+ const metadata = { omnirushAttachments: [] };+ await post(`/session/${SESSION}/prompt_async`, {+ model: { providerID: "omnirush", modelID: "gpt-6-astra" },+ variant: "high",+ agent: "omnirush",+ system: "Extra system",+ parts: [+ { type: "text", text: note, synthetic: true, metadata },+ { type: "text", text: "hi" },+ { type: "file", mime: "image/png", filename: "image.png", url: "data:image/png;base64,AAAA" },+ ],+ });+ const prompt = recorded.find((call) => call.path === `/api/session/${SESSION}/prompt`)!;+ expect(prompt.body).toMatchObject({+ text: `${note}\n\nhi`,+ metadata: { omnirush: { textParts: [{ length: note.length, synthetic: true, metadata }, { length: 2 }] } },+ });+ });+test("permissions list and reply in 1.x form", async () => {const list = (await (await get("/permission")).json()) as Array<Record<string, unknown>>;expect(list).toEqual([{ id: "per_1", sessionID: SESSION, permission: "bash", patterns: ["rm -rf build"], metadata: {}, always: ["rm -rf build"] }]);diff --git a/apps/server/src/engine2/facade.ts b/apps/server/src/engine2/facade.tsindex a24313e5..745f2018 100644--- a/apps/server/src/engine2/facade.ts+++ b/apps/server/src/engine2/facade.ts@@ -358,7 +358,9 @@ export async function startEngineFacade(options: EngineFacadeOptions): Promise<Econst turnDiffs = new Map<string, JsonRecord[]>();const attachTurnDiffs = async (sessionID: string, messages: V1Message[]): Promise<void> => {const busy = translator.statusOf(sessionID)?.type === "busy";- const prompts = messages.filter((message) => message.info.role === "user" && !message.parts.some((part) => part.synthetic === true));+ // A prompt may carry synthetic notes beside what was typed; a message of synthetic text alone is not a prompt.+ const prompts = messages.filter((message) => message.info.role === "user"+ && !(message.parts.length > 0 && message.parts.every((part) => part.type === "text" && part.synthetic === true)));for (const [index, message] of prompts.entries()) {const id = String(message.info.id);const key = `${sessionID}\u0000${id}`;@@ -507,15 +509,29 @@ export async function startEngineFacade(options: EngineFacadeOptions): Promise<E/** A 1.x prompt's parts as the 2.x prompt (text, files, agent mentions). */const v2Prompt = (body: JsonRecord): JsonRecord => {const texts: string[] = [];+ // 2.x keeps one text per prompt: the layout of the 1.x text parts lets reads split it back,+ // so synthetic notes (attachment paths, mention instructions) reach the model but stay out+ // of the user's bubble and the trace's typed prompt.+ const textParts: JsonRecord[] = [];+ let splitText = false;const files: JsonRecord[] = [];const agents: JsonRecord[] = [];for (const part of arr(body, "parts")) {if (!isRecord(part)) continue;const type = str(part, "type");- if (type === "text" && typeof part.text === "string") texts.push(part.text);+ if (type === "text" && typeof part.text === "string") {+ texts.push(part.text);+ const synthetic = part.synthetic === true || undefined;+ const metadata = isRecord(part.metadata) && Object.keys(part.metadata).length ? part.metadata : undefined;+ if (synthetic || metadata) splitText = true;+ textParts.push(omitUndefined({ length: part.text.length, synthetic, metadata }));+ }else if (type === "file" && typeof part.url === "string") files.push(omitUndefined({ uri: part.url, name: str(part, "filename") }));else if (type === "agent" && typeof part.name === "string") agents.push({ id: part.name });- else if (type === "subtask" && typeof part.prompt === "string") texts.push(part.prompt);+ else if (type === "subtask" && typeof part.prompt === "string") {+ texts.push(part.prompt);+ textParts.push({ length: part.prompt.length });+ }}// The 1.x user message fields 2.x does not keep (system prompt, agent and model as picked,// tool switches) travel in the message metadata, so reads return them field for field.@@ -525,6 +541,7 @@ export async function startEngineFacade(options: EngineFacadeOptions): Promise<Emodel: model ? omitUndefined({ providerID: model.providerID, modelID: model.modelID, variant: model.variant && model.variant !== "default" ? model.variant : undefined }) : undefined,system: str(body, "system"),tools: isRecord(body.tools) ? body.tools : undefined,+ textParts: splitText ? textParts : undefined,});return omitUndefined({id: str(body, "messageID"),diff --git a/apps/server/src/engine2/native.ts b/apps/server/src/engine2/native.tsindex 80e61cba..320cb764 100644--- a/apps/server/src/engine2/native.ts+++ b/apps/server/src/engine2/native.ts@@ -28,7 +28,7 @@* says `sameAs:<where>` instead of repeating the bytes.*/import type { V1Message } from "./shapes.js";-import { isRecord, type JsonRecord } from "./util.js";+import { isRecord, promptFileUrl, type JsonRecord } from "./util.js";/** Where a 2.x value goes when its name is taken on the 1.x record by another value. */export const NATIVE_COLLISIONS = "v2";@@ -143,14 +143,25 @@ export function enrichPrompt(message: V1Message, native: JsonRecord): void {message.parts.forEach((part, index) => {if (part.type === "file" && typeof part.url === "string") known.set(part.url, `parts[${index}].url`);});+ // Inline file bytes the file part already holds as its `data:` URL are not repeated.+ const files = (Array.isArray(native.files) ? native.files : []).map((file) => {+ if (!isRecord(file) || typeof file.data !== "string" || typeof file.uri === "string") return file;+ const url = promptFileUrl(file);+ const where = url ? known.get(url) : undefined;+ return where ? { ...file, data: `sameAs:${where}` } : file;+ });+ if (Array.isArray(native.files)) native = { ...native, files };message.info = mergeNative({ ...message.info }, elideKnownDataUris(native, known) as JsonRecord, INFO_NESTED);- const files = Array.isArray(native.files) ? native.files : [];const fileParts = message.parts.filter((part) => part.type === "file");files.forEach((file, index) => {const part = fileParts[index];if (!part || !isRecord(file)) return;- const { uri, ...rest } = file;- mergeNative(part, rest);+ // Inline bytes stay on the message's `files` (the part's `data:` URL holds them), and the+ // engine's `source: {type: "inline"}` is not a 1.x file source (a path the UI shows as a+ // document), so neither goes on the part.+ const { uri, data: _data, source, ...rest } = file;+ const v1Source = isRecord(source) && ["file", "symbol", "resource"].includes(String(source.type));+ mergeNative(part, v1Source ? { ...rest, source } : rest);if (uri !== part.url) mergeNative(part, { uri });});}diff --git a/apps/server/src/engine2/shapes.test.ts b/apps/server/src/engine2/shapes.test.tsindex a3f17534..3e00a87a 100644--- a/apps/server/src/engine2/shapes.test.ts+++ b/apps/server/src/engine2/shapes.test.ts@@ -304,6 +304,53 @@ describe("the 2.x engine's own fields ride along (additive)", () => {});});+describe("prompts with synthetic notes", () => {+ const note = "Attached files were copied into this worker workspace for tool access:\n- image.png: .opencode/omnirush/inbox/chat-attachments/s/1-image.png (file:///home/u/p/.opencode/omnirush/inbox/chat-attachments/s/1-image.png)";+ const metadata = { omnirushAttachments: [] };+ const payload = {+ id: "msg_note",+ text: `${note}\n\nhi`,+ files: [{ uri: "data:image/png;base64,AAAA", name: "image.png", mime: "image/png" }],+ metadata: { omnirush: { agent: "build", textParts: [{ length: note.length, synthetic: true, metadata }, { length: 2 }] } },+ };+ const expected = [+ { id: partId("msg_note", "u0"), type: "text", text: note, synthetic: true, metadata },+ { id: partId("msg_note", "u1"), type: "text", text: "hi" },+ { id: partId("msg_note", "f0"), type: "file", filename: "image.png", url: "data:image/png;base64,AAAA" },+ ];++ test("read back: the typed prompt is its own text part, the note a synthetic part", () => {+ const [message] = v1Messages([{ type: "user", time: { created: 1 }, ...payload }], { sessionID: "ses_n", directory: "/w", root: "/" });+ expect(message!.parts).toMatchObject(expected);+ expect(message!.parts[1]!.synthetic).toBeUndefined();+ expect(message!.parts).toHaveLength(3);+ });++ test("live: the enqueued prompt shows the same parts", async () => {+ const translator = new EventTranslator({ version: "2.0.18" });+ const out = await translator.translate({ type: "session.inbox.enqueued", data: { sessionID: "ses_n", inboxID: "msg_note", item: { type: "user", payload } } });+ const parts = out.filter((item) => item.event.type === "message.part.updated").map((item) => item.event.properties.part);+ expect(parts).toMatchObject(expected);+ });++ test("an image the engine keeps inline reads back as its data: URL, bytes not repeated", () => {+ const inline = { data: "AAAA", mime: "image/png", source: { type: "inline" }, name: "image.png" };+ const tree = { type: "user", time: { created: 1 }, ...payload, files: [inline] };+ const [message] = v1Messages([tree], { sessionID: "ses_n", directory: "/w", root: "/" });+ expect(message!.parts).toMatchObject(expected);+ expect(JSON.stringify(message).match(/AAAA/g)).toHaveLength(1);+ // The engine's inline source is not a 1.x file source (the UI would render it as a document).+ expect(message!.parts[2]!.source).toBeUndefined();+ expect(message!.parts[2]!.data).toBeUndefined();+ });++ test("a text that no longer matches the layout stays one part", () => {+ const [message] = v1Messages([{ type: "user", time: { created: 1 }, ...payload, text: "edited" }], { sessionID: "ses_n", directory: "/w", root: "/" });+ expect(message!.parts.filter((part) => part.type === "text")).toMatchObject([{ text: "edited" }]);+ expect(message!.parts[0]!.synthetic).toBeUndefined();+ });+});+describe("live events and read-back agree", () => {test("parts built from the 2.x stream carry the ids and fields of the same parts read back", async () => {const tree = fixture("v2-sub");diff --git a/apps/server/src/engine2/shapes.ts b/apps/server/src/engine2/shapes.tsindex ac42e0d6..2cf77953 100644--- a/apps/server/src/engine2/shapes.ts+++ b/apps/server/src/engine2/shapes.ts@@ -25,7 +25,7 @@* (engine2/events.ts) agree.*/import { withNativeFields } from "./native.js";-import { arr, isRecord, num, omitUndefined, record, str, unwrap, type JsonRecord } from "./util.js";+import { arr, isRecord, num, omitUndefined, promptFileUrl, record, str, unwrap, type JsonRecord } from "./util.js";export type V1Message = { info: JsonRecord; parts: JsonRecord[] };@@ -418,7 +418,7 @@ function userFileParts(files: unknown[], ids: { messageID: string; sessionID: stconst parts: JsonRecord[] = [];files.forEach((file, index) => {if (!isRecord(file)) return;- const url = str(file, "uri");+ const url = promptFileUrl(file);if (!url) return;parts.push(omitUndefined({id: partId(ids.messageID, `f${index}`),@@ -578,6 +578,46 @@ export function v1AssistantMessage(message: JsonRecord, ctx: MessageContext, parreturn { info, parts };}+/** The separator the facade joins a prompt's 1.x text parts with into the one 2.x text. */+const PROMPT_TEXT_SEPARATOR = "\n\n";++/**+ * A prompt's 1.x text parts. The facade records the layout of the text parts+ * it joined (`metadata.omnirush.textParts`: length, synthetic flag, metadata+ * per part); when the stored text still matches that layout it is split back,+ * so a synthetic note stays its own synthetic part. Otherwise one text part.+ */+export function v1UserTextParts(+ ids: { messageID: string; sessionID: string },+ text: string,+ layout: unknown,+ synthetic: boolean,+): JsonRecord[] {+ const base = { sessionID: ids.sessionID, messageID: ids.messageID, type: "text" };+ const single = [omitUndefined({ id: partId(ids.messageID, "u0"), ...base, text, synthetic: synthetic || undefined })];+ if (!Array.isArray(layout) || layout.length === 0) return single;+ const parts: JsonRecord[] = [];+ let offset = 0;+ for (const [index, entry] of layout.entries()) {+ const length = isRecord(entry) ? num(entry, "length") : undefined;+ if (!isRecord(entry) || length === undefined || !Number.isInteger(length) || length < 0) return single;+ if (index > 0) {+ if (text.slice(offset, offset + PROMPT_TEXT_SEPARATOR.length) !== PROMPT_TEXT_SEPARATOR) return single;+ offset += PROMPT_TEXT_SEPARATOR.length;+ }+ if (offset + length > text.length) return single;+ parts.push(omitUndefined({+ id: partId(ids.messageID, `u${index}`),+ ...base,+ text: text.slice(offset, offset + length),+ synthetic: synthetic || entry.synthetic === true || undefined,+ metadata: isRecord(entry.metadata) ? entry.metadata : undefined,+ }));+ offset += length;+ }+ return offset === text.length ? parts : single;+}+function v1UserMessage(message: JsonRecord,ctx: MessageContext,@@ -586,14 +626,14 @@ function v1UserMessage(synthetic: boolean,agent: string,model: ModelRef | undefined,- extra: { system?: string; tools?: JsonRecord } = {},+ extra: { system?: string; tools?: JsonRecord; textParts?: unknown } = {},): V1Message {const id = str(message, "id") ?? "";const created = num(record(message, "time"), "created") ?? 0;const ids = { messageID: id, sessionID: ctx.sessionID };const parts: JsonRecord[] = [];if (text || files.length === 0) {- parts.push(omitUndefined({ id: partId(id, "u0"), sessionID: ctx.sessionID, messageID: id, type: "text", text, synthetic: synthetic || undefined }));+ parts.push(...v1UserTextParts(ids, text, extra.textParts, synthetic));}parts.push(...userFileParts(files, ids));const info = omitUndefined({@@ -746,7 +786,7 @@ export function v1Messages(messages: unknown[], ctx: MessageContext): V1Message[sentModel && str(sentModel, "providerID") && str(sentModel, "modelID")? { providerID: str(sentModel, "providerID")!, modelID: str(sentModel, "modelID")!, ...(str(sentModel, "variant") ? { variant: str(sentModel, "variant")! } : {}) }: replyModel ?? model,- { system: str(sent, "system"), tools: record(sent, "tools") },+ { system: str(sent, "system"), tools: record(sent, "tools"), textParts: sent?.textParts },);lastUserId = String(mapped.info.id);emit(message, [mapped]);diff --git a/apps/server/src/engine2/util.ts b/apps/server/src/engine2/util.tsindex 956be35e..de287d07 100644--- a/apps/server/src/engine2/util.ts+++ b/apps/server/src/engine2/util.ts@@ -45,3 +45,17 @@ export function omitUndefined<T extends JsonRecord>(value: T): T {for (const key of Object.keys(value)) if (value[key] === undefined) delete value[key];return value;}++/**+ * The URL of a prompt file: its `uri`, or, for bytes the engine keeps inline+ * (a pasted image sent as a `data:` URI is stored as `{data, mime, source: {type: "inline"}}`),+ * the `data:` URI again, so the transcript keeps the image.+ */+export function promptFileUrl(file: unknown): string | undefined {+ if (!isRecord(file)) return undefined;+ const uri = str(file, "uri");+ if (uri) return uri;+ const data = str(file, "data");+ const mime = str(file, "mime");+ return data && mime ? `data:${mime};base64,${data}` : undefined;+}