Skip to content

Commit 9c97b93

Browse files
committed
feat(tui): configure file path auto-attachments
1 parent 454b4bc commit 9c97b93

10 files changed

Lines changed: 116 additions & 6 deletions

File tree

‎packages/tui/src/component/dialog-config.tsx‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,15 @@ export const settings: Setting[] = [
249249
values: ["compact", "full"],
250250
keywords: ["paste summary", "clipboard", "pasted content"],
251251
},
252+
{
253+
title: "File path attachments",
254+
category: "Input",
255+
path: ["prompt", "paste_attach"],
256+
default: "all",
257+
values: ["all", "images", "never"],
258+
labels: ["all supported files", "images only", "paths only"],
259+
keywords: ["paste", "drop", "PDF", "attachments", "file paths"],
260+
},
252261
{
253262
title: "Image previews",
254263
category: "Input",

‎packages/tui/src/component/prompt/index.tsx‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1473,7 +1473,11 @@ export function Prompt(props: PromptProps) {
14731473
async function pasteInputText(text: string, changed: () => boolean) {
14741474
const normalizedText = text.replace(/\r\n/g, "\n").replace(/\r/g, "\n")
14751475
const pastedContent = normalizedText.trim()
1476-
const attachments = await resolvePastedAttachments(pastedContent, terminalEnvironment.platform)
1476+
const attachments = await resolvePastedAttachments(
1477+
pastedContent,
1478+
terminalEnvironment.platform,
1479+
config.prompt?.paste_attach,
1480+
)
14771481
if (changed()) return
14781482
if (attachments) {
14791483
attachments.forEach((attachment) => {

‎packages/tui/src/component/prompt/local-attachment.ts‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ export type LocalAttachment =
1414
| Readonly<{ type: "text"; mime: "image/svg+xml"; content: string }>
1515
| Readonly<{ type: "binary"; mime: string; content: Uint8Array }>
1616

17-
export function readLocalAttachment(file: string, maxBytes = MAX_LOCAL_ATTACHMENT_BYTES) {
17+
export type PasteAttach = "all" | "images" | "never"
18+
19+
export function readLocalAttachment(file: string, maxBytes = MAX_LOCAL_ATTACHMENT_BYTES, mode: PasteAttach = "all") {
1820
return readLocalAttachmentWith(
1921
{
2022
readText: async (value, limit) => (await readFileBounded(value, limit)).toString("utf8"),
@@ -23,22 +25,25 @@ export function readLocalAttachment(file: string, maxBytes = MAX_LOCAL_ATTACHMEN
2325
},
2426
file,
2527
maxBytes,
28+
mode,
2629
)
2730
}
2831

29-
export async function resolvePastedAttachments(text: string, platform: string) {
32+
export async function resolvePastedAttachments(text: string, platform: string, mode: PasteAttach = "all") {
33+
if (mode === "never") return undefined
3034
const pastedContent = text.trim()
3135
const filepath = normalizePastedFilepath(pastedContent, platform)
3236
if (/^(https?):\/\//.test(filepath)) return undefined
3337

34-
const attachment = await readLocalAttachment(filepath)
38+
const attachment = await readLocalAttachment(filepath, MAX_LOCAL_ATTACHMENT_BYTES, mode)
3539
const attachments = attachment ? [{ filepath, attachment }] : []
3640
if (!attachment) {
3741
const filepaths = parsePastedFilepaths(pastedContent, platform)
3842
if (filepaths.length <= 1) return undefined
3943
let remaining = MAX_LOCAL_ATTACHMENT_BYTES
4044
for (const candidate of filepaths) {
41-
const next = await readLocalAttachment(candidate, remaining)
45+
// Keep a mixed drop intact as text rather than silently omitting any paths.
46+
const next = await readLocalAttachment(candidate, remaining, mode)
4247
if (!next) return undefined
4348
remaining -= typeof next.content === "string" ? Buffer.byteLength(next.content) : next.content.byteLength
4449
attachments.push({ filepath: candidate, attachment: next })
@@ -161,9 +166,12 @@ export async function readLocalAttachmentWith(
161166
files: LocalFiles,
162167
path: string,
163168
maxBytes = MAX_LOCAL_ATTACHMENT_BYTES,
169+
mode: PasteAttach = "all",
164170
): Promise<LocalAttachment | undefined> {
171+
if (mode === "never") return undefined
165172
const mime = await files.mime(path).catch(() => undefined)
166173
if (!mime) return undefined
174+
if (mode === "images" && !mime.startsWith("image/")) return undefined
167175
if (!mime.startsWith("image/") && mime !== "application/pdf") return undefined
168176
if (mime === "image/svg+xml") {
169177
const content = await files.readText(path, maxBytes).catch(() => undefined)

‎packages/tui/src/config/index.tsx‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ export const Info = Schema.Struct({
149149
paste: Schema.optional(Schema.Literals(["compact", "full"])).annotate({
150150
description: "Display large pastes as compact placeholders or full text",
151151
}),
152+
paste_attach: Schema.optional(Schema.Literals(["all", "images", "never"])).annotate({
153+
description:
154+
"Auto-attach pasted file paths: all supported files (default), images only, or never. Other paths remain text. Clipboard images are unaffected.",
155+
}),
152156
image_preview: Schema.optional(Schema.Boolean).annotate({
153157
description: "Show image attachment previews above the prompt input",
154158
}),

‎packages/tui/src/mini/footer.prompt.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ type PromptInput = {
126126
theme: Accessor<RunFooterTheme>
127127
mono: Accessor<boolean>
128128
imagePreview?: boolean
129+
pasteAttach?: "all" | "images" | "never"
129130
clipboard?: Pick<ClipboardService, "read">
130131
history?: Accessor<RunPrompt[]>
131132
queuedPrompts: Accessor<FooterQueuedPrompt[]>
@@ -883,7 +884,7 @@ export function createPromptState(input: PromptInput): PromptState {
883884
? [{ type: "file" as const, uri: `data:${content.mime};base64,${content.data}`, filename: "clipboard" }]
884885
: shell()
885886
? undefined
886-
: await resolvePastedAttachments(normalized, process.platform)
887+
: await resolvePastedAttachments(normalized, process.platform, input.pasteAttach)
887888
if (changed()) return
888889
// A paste's own text edits must not cancel a submit waiting on that paste.
889890
applyingPaste = true

‎packages/tui/src/mini/footer.view.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ export function RunFooterView(props: RunFooterViewProps) {
379379
theme,
380380
mono: () => props.mono,
381381
imagePreview: props.tuiConfig.prompt?.image_preview,
382+
pasteAttach: props.tuiConfig.prompt?.paste_attach,
382383
clipboard: props.clipboard,
383384
history: props.history,
384385
queuedPrompts: queue,

‎packages/tui/test/config-v2.test.tsx‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@ import { CommandMap, Definitions } from "../src/config/v1/keybind"
99

1010
const decodeInfo = Schema.decodeUnknownSync(Info)
1111

12+
test("validates file path attachment preferences", () => {
13+
for (const mode of ["all", "images", "never"] as const) {
14+
expect(decodeInfo({ prompt: { paste_attach: mode } })).toEqual({ prompt: { paste_attach: mode } })
15+
}
16+
expect(() => decodeInfo({ prompt: { paste_attach: "paths" } })).toThrow()
17+
expect(settings.find((setting) => setting.path.join(".") === "prompt.paste_attach")).toMatchObject({
18+
default: "all",
19+
values: ["all", "images", "never"],
20+
})
21+
})
22+
1223
test("validates the explicit diff source defaults", () => {
1324
for (const source of ["branch", "committed", "working", "turn"] as const) {
1425
expect(decodeInfo({ diffs: { source } })).toEqual({ diffs: { source } })

‎packages/tui/test/mini/footer.view.test.tsx‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,30 @@ test("a failed clipboard read cancels a waiting submit without losing the draft"
444444
}
445445
})
446446

447+
test.each(["images", "never"] as const)("mini keeps PDF paths as text in %s mode", async (mode) => {
448+
await using tmp = await tmpdir()
449+
const file = `${tmp.path}/report.pdf`
450+
await Bun.write(file, "%PDF-1.7")
451+
const submitted: RunPrompt[] = []
452+
const app = await renderFooter({
453+
tuiConfig: createTuiResolvedConfig({ prompt: { paste_attach: mode } }),
454+
onSubmit: (prompt) => {
455+
submitted.push(prompt)
456+
return true
457+
},
458+
})
459+
try {
460+
await app.renderOnce()
461+
await app.mockInput.pasteBracketedText(file)
462+
app.mockInput.pressEnter()
463+
await app.waitFor(() => submitted.length === 1)
464+
expect(submitted[0].text).toBe(file)
465+
expect(submitted[0].parts).toEqual([])
466+
} finally {
467+
app.cleanup()
468+
}
469+
})
470+
447471
test("mini attaches dropped image paths and removes attachments with their labels", async () => {
448472
await using tmp = await tmpdir()
449473
await Bun.write(`${tmp.path}/one image.png`, diffImageFixture)

‎packages/tui/test/prompt/local-attachment.test.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,48 @@ describe("prompt local attachments", () => {
119119
}
120120
})
121121

122+
test("images mode leaves PDFs and mixed drops as paths while still attaching images", async () => {
123+
await using tmp = await tmpdir()
124+
const image = path.join(tmp.path, "one image.png")
125+
const pdf = path.join(tmp.path, "two file.pdf")
126+
await Promise.all([Bun.write(image, new Uint8Array([1, 2, 3])), Bun.write(pdf, "%PDF-1.7")])
127+
128+
for (const input of [pdf, `'${pdf}'`, pathToFileURL(pdf).href, `'${image}' '${pdf}'`]) {
129+
expect(await resolvePastedAttachments(input, process.platform, "images")).toBeUndefined()
130+
}
131+
expect(await resolvePastedAttachments(image, process.platform, "images")).toEqual([
132+
{ type: "file", uri: "data:image/png;base64,AQID", filename: "one image.png" },
133+
])
134+
expect(await resolvePastedAttachments(`'${image}' '${image}'`, process.platform, "images")).toHaveLength(2)
135+
})
136+
137+
test("never mode leaves image, SVG, and PDF drops as paths", async () => {
138+
await using tmp = await tmpdir()
139+
for (const name of ["image.png", "image.svg", "report.pdf"]) {
140+
const file = path.join(tmp.path, name)
141+
await Bun.write(file, "content")
142+
expect(await resolvePastedAttachments(file, process.platform, "never")).toBeUndefined()
143+
}
144+
})
145+
146+
test("excluded files are not read", async () => {
147+
const reads: string[] = []
148+
const tracked: LocalFiles = {
149+
mime: async () => "application/pdf",
150+
readBytes: async (file) => {
151+
reads.push(file)
152+
return new Uint8Array([1])
153+
},
154+
readText: async (file) => {
155+
reads.push(file)
156+
return "content"
157+
},
158+
}
159+
expect(await readLocalAttachmentWith(tracked, "/report.pdf", MAX_LOCAL_ATTACHMENT_BYTES, "images")).toBeUndefined()
160+
expect(await readLocalAttachmentWith(tracked, "/report.pdf", MAX_LOCAL_ATTACHMENT_BYTES, "never")).toBeUndefined()
161+
expect(reads).toEqual([])
162+
})
163+
122164
test("falls back to plain text for unsupported or incomplete drops", async () => {
123165
await using tmp = await tmpdir()
124166
const image = path.join(tmp.path, "image.png")

‎services/www/src/docs/content/cli/config.mdx‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ Configure mouse capture, scrolling, editor context, pastes, and prompt image pre
9191
"prompt": {
9292
"editor": true,
9393
"paste": "compact",
94+
"paste_attach": "all",
9495
"image_preview": false
9596
}
9697
}
@@ -105,10 +106,15 @@ Configure mouse capture, scrolling, editor context, pastes, and prompt image pre
105106
| `scroll.acceleration` | boolean | Accelerates repeated scrolling. When enabled, it takes precedence over speed. |
106107
| `prompt.editor` | boolean | Includes the active editor file or selection as prompt context. |
107108
| `prompt.paste` | `compact` or `full` | Shows large pastes as compact placeholders or as full text. |
109+
| `prompt.paste_attach` | `all`, `images`, or `never` | Auto-attaches supported file paths, images only, or no paths. Defaults to `all`. |
108110
| `prompt.image_preview` | boolean | Shows image attachment previews above the prompt input. |
109111

110112
</div>
111113

114+
Set `prompt.paste_attach` to `images` to keep PDF paths as text while still attaching images.
115+
Use `never` to insert all dropped or pasted file paths as text. Images copied directly to the clipboard
116+
are still attached. If a multi-file drop contains an excluded file, the whole drop stays as text.
117+
112118
## Sessions
113119

114120
Configure how full-screen TUI sessions are presented and where new sessions start:

0 commit comments

Comments
 (0)