From a8e17aee80a979992e19295f29eb39a518e26be7 Mon Sep 17 00:00:00 2001 From: OpeOginni <107570612+OpeOginni@users.noreply.github.com> Date: Thu, 29 Oct 2026 18:04:39 +0100 Subject: [PATCH] fix(tui): preserve captured attachments and source paths (#60222) --- .../client/src/promise/generated/types.ts | 2 + packages/core/src/session/prompt.ts | 4 +- packages/core/test/session-prompt.test.ts | 41 +++++++++++++++++++ packages/protocol/openapi.json | 3 ++ packages/schema/src/prompt-input.ts | 4 +- .../src/component/prompt/local-attachment.ts | 6 +-- .../tui/test/prompt/local-attachment.test.ts | 25 +++++++++-- 7 files changed, 74 insertions(+), 11 deletions(-) diff --git a/packages/client/src/promise/generated/types.ts b/packages/client/src/promise/generated/types.ts index d7091d1ecd4..8bd2108c4ea 100644 --- a/packages/client/src/promise/generated/types.ts +++ b/packages/client/src/promise/generated/types.ts @@ -3259,6 +3259,7 @@ export type SessionPromptInput = { readonly files?: | ReadonlyArray<{ readonly uri: string + readonly source?: { readonly type: "inline" } | { readonly type: "uri"; readonly uri: string } readonly name?: string readonly description?: string readonly mention?: { readonly start: number; readonly end: number; readonly text: string } @@ -3290,6 +3291,7 @@ export type SessionCommandInput = { readonly files?: | ReadonlyArray<{ readonly uri: string + readonly source?: { readonly type: "inline" } | { readonly type: "uri"; readonly uri: string } readonly name?: string readonly description?: string readonly mention?: { readonly start: number; readonly end: number; readonly text: string } diff --git a/packages/core/src/session/prompt.ts b/packages/core/src/session/prompt.ts index 6d3e591f2fa..af4464e88d8 100644 --- a/packages/core/src/session/prompt.ts +++ b/packages/core/src/session/prompt.ts @@ -99,7 +99,7 @@ const materializeAttachment = Effect.fn("SessionPrompt.materializeAttachment")(f return FileAttachment.create({ data: Base64.make(Buffer.from(blob.data).toString("base64")), mime: blob.mime, - source: { type: "inline" }, + source: input.source ?? { type: "inline" }, name: input.name, description: input.description, mention: input.mention, @@ -109,7 +109,7 @@ const materializeAttachment = Effect.fn("SessionPrompt.materializeAttachment")(f const resolved = input.uri.startsWith("data:") ? { bytes: yield* decodeDataURL(input.uri, label), - source: { type: "inline" as const }, + source: input.source ?? { type: "inline" as const }, start: undefined, end: undefined, name: undefined, diff --git a/packages/core/test/session-prompt.test.ts b/packages/core/test/session-prompt.test.ts index aceb528e75a..2bd2e9b6dbb 100644 --- a/packages/core/test/session-prompt.test.ts +++ b/packages/core/test/session-prompt.test.ts @@ -46,6 +46,7 @@ import { ConfigExperimental } from "@opencode/schema/config/experimental" import { SessionStored } from "@opencode/core/session/stored" import { SessionBlob } from "@opencode/core/session/blob" import { SessionTransfer } from "@opencode/core/session/transfer" +import { PromptInput } from "@opencode/schema/prompt-input" const executionCalls: Session.ID[] = [] const interruptCalls: Session.ID[] = [] @@ -534,6 +535,46 @@ describe("Session.prompt", () => { }), ) + it.live("admits captured image bytes with their source path after the source disappears", () => + Effect.gen(function* () { + yield* setup + const session = yield* Session.Service + const directory = yield* tmpdirScoped("opencode-session-prompt-") + const source = path.join(directory.path, "screenshot.png") + yield* Effect.promise(() => Bun.write(source, Buffer.from(pngBase64, "base64"))) + const bytes = yield* Effect.promise(() => Bun.file(source).arrayBuffer()) + yield* Effect.promise(() => Bun.file(source).delete()) + const file = { + uri: `data:image/png;base64,${Buffer.from(bytes).toString("base64")}`, + source: { type: "uri", uri: pathToFileURL(source).href }, + name: "screenshot.png", + mention: { start: 0, end: 9, text: "[Image 1]" }, + } satisfies PromptInput.FileAttachment + + const message = yield* session.prompt({ + sessionID, + text: "[Image 1] Inspect this image", + files: [Schema.decodeUnknownSync(PromptInput.FileAttachment)(file)], + resume: false, + }) + + expect(message.payload.files).toEqual([ + { + data: pngBase64, + mime: "image/png", + source: file.source, + name: file.name, + mention: file.mention, + }, + ]) + const stored = yield* admitted(message.id) + expect(stored?.type === "user" ? stored.payload.files : undefined).toEqual(message.payload.files) + expect( + Schema.encodeSync(PromptInput.FileAttachment)(Schema.decodeUnknownSync(PromptInput.FileAttachment)(file)), + ).toEqual(file) + }), + ) + it.effect("stores media refs at admission when the session's config enables media_blob", () => Effect.gen(function* () { yield* setup diff --git a/packages/protocol/openapi.json b/packages/protocol/openapi.json index 57f243c69e3..a8449d20a5d 100644 --- a/packages/protocol/openapi.json +++ b/packages/protocol/openapi.json @@ -20669,6 +20669,9 @@ "uri": { "type": "string" }, + "source": { + "$ref": "#/components/schemas/Prompt.FileSource" + }, "name": { "type": "string" }, diff --git a/packages/schema/src/prompt-input.ts b/packages/schema/src/prompt-input.ts index 769a87df72e..645810725b3 100644 --- a/packages/schema/src/prompt-input.ts +++ b/packages/schema/src/prompt-input.ts @@ -1,13 +1,15 @@ export * as PromptInput from "./prompt-input.js" import { Schema } from "effect" -import { AgentAttachment, PromptMention } from "./prompt.js" +import { AgentAttachment, FileSource, PromptMention } from "./prompt.js" import { optional } from "./schema.js" import { Skill } from "./skill.js" export interface FileAttachment extends Schema.Schema.Type {} export const FileAttachment = Schema.Struct({ uri: Schema.String, + // Source metadata does not require reopening the original file. + source: FileSource.pipe(optional), name: Schema.String.pipe(optional), description: Schema.String.pipe(optional), mention: PromptMention.pipe(optional), diff --git a/packages/tui/src/component/prompt/local-attachment.ts b/packages/tui/src/component/prompt/local-attachment.ts index a676d46380a..72afb381069 100644 --- a/packages/tui/src/component/prompt/local-attachment.ts +++ b/packages/tui/src/component/prompt/local-attachment.ts @@ -1,4 +1,3 @@ -import { existsSync } from "node:fs" import path from "node:path" import { pathToFileURL } from "node:url" @@ -61,14 +60,13 @@ export async function resolvePastedAttachments(text: string, platform: string) { }) } -// A local server reads the file itself so the model sees its path. The pasted bytes are the -// fallback for remote servers and for files deleted before submit, like screenshot drags. +// Keep the captured bytes: a local server may not have the terminal's access to the source file. export function promptFilesForServer( files: readonly T[] | undefined, localServer: boolean, ) { return files?.map(({ path: file, ...rest }) => - file && localServer && existsSync(file) ? { ...rest, uri: pathToFileURL(file).href } : rest, + file && localServer ? { ...rest, source: { type: "uri" as const, uri: pathToFileURL(file).href } } : rest, ) } diff --git a/packages/tui/test/prompt/local-attachment.test.ts b/packages/tui/test/prompt/local-attachment.test.ts index dc7899f0ce2..6dd748d38be 100644 --- a/packages/tui/test/prompt/local-attachment.test.ts +++ b/packages/tui/test/prompt/local-attachment.test.ts @@ -180,7 +180,7 @@ describe("prompt local attachments", () => { expect(await resolvePastedAttachments(Array(33).fill(file).join(" "), process.platform)).toBeUndefined() }) - test("sends pasted files by path to a local server and by bytes otherwise", async () => { + test("keeps pasted bytes with source paths only for a local server", async () => { await using tmp = await tmpdir() const image = path.join(tmp.path, "scrëen shot.png") const photo = path.join(tmp.path, "photo.jpg") @@ -204,10 +204,26 @@ describe("prompt local attachments", () => { const mentions = files.map((file) => file.mention) + await Bun.write(image, new Uint8Array([10, 11, 12])) expect(promptFilesForServer(files, true)).toEqual([ - { uri: pathToFileURL(image).href, name: "scrëen shot.png", mention: mentions[0] }, - { uri: pathToFileURL(photo).href, name: "photo.jpg", mention: mentions[1] }, - { uri: pathToFileURL(pdf).href, name: "report.pdf", mention: mentions[2] }, + { + uri: "data:image/png;base64,AQID", + source: { type: "uri", uri: pathToFileURL(image).href }, + name: "scrëen shot.png", + mention: mentions[0], + }, + { + uri: "data:image/jpeg;base64,BwgJ", + source: { type: "uri", uri: pathToFileURL(photo).href }, + name: "photo.jpg", + mention: mentions[1], + }, + { + uri: "data:application/pdf;base64,BAUG", + source: { type: "uri", uri: pathToFileURL(pdf).href }, + name: "report.pdf", + mention: mentions[2], + }, { uri: "data:image/png;base64,Cg==", name: "clipboard" }, ]) expect(promptFilesForServer(files, false)).toEqual([ @@ -220,6 +236,7 @@ describe("prompt local attachments", () => { await Bun.file(image).delete() expect(promptFilesForServer(files, true)?.[0]).toEqual({ uri: "data:image/png;base64,AQID", + source: { type: "uri", uri: pathToFileURL(image).href }, name: "scrëen shot.png", mention: mentions[0], })