GeezerVison is showing github.com — fix(app): batch multi-file prompt attachments (#18722) · msis/opencode@9239d87
File treeExpand file treeCollapse file tree Open diff view settings storybook/.storybook/mocks/app/context
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/app/src/components/prompt-input.tsx +2-6Lines changed: 2 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1043,7 +1043,7 @@ export const PromptInput: Component<PromptInputProps> = (props) => { | 1043 | 1043 | return true | 1044 | 1044 | } | 1045 | 1045 |
| 1046 | | - const { addAttachment, removeAttachment, handlePaste } = createPromptAttachments({ | | 1046 | + const { addAttachments, removeAttachment, handlePaste } = createPromptAttachments({ | 1047 | 1047 | editor: () => editorRef, | 1048 | 1048 | isDialogActive: () => !!dialog.active, | 1049 | 1049 | setDraggingType: (type) => setStore("draggingType", type), | @@ -1388,11 +1388,7 @@ export const PromptInput: Component<PromptInputProps> = (props) => { | 1388 | 1388 | class="hidden" | 1389 | 1389 | onChange={(e) => { | 1390 | 1390 | const list = e.currentTarget.files | 1391 | | - if (list) { | 1392 | | - for (const file of Array.from(list)) { | 1393 | | - void addAttachment(file) | 1394 | | - } | 1395 | | - } | | 1391 | + if (list) void addAttachments(Array.from(list)) | 1396 | 1392 | e.currentTarget.value = "" | 1397 | 1393 | }} | 1398 | 1394 | /> | |
Collapse file Copy file name to clipboardExpand all lines: packages/app/src/components/prompt-input/attachments.ts +21-17Lines changed: 21 additions & 17 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -71,6 +71,18 @@ export function createPromptAttachments(input: PromptAttachmentsInput) { | 71 | 71 |
| 72 | 72 | const addAttachment = (file: File) => add(file) | 73 | 73 |
| | 74 | + const addAttachments = async (files: File[], toast = true) => { | | 75 | + let found = false | | 76 | + | | 77 | + for (const file of files) { | | 78 | + const ok = await add(file, false) | | 79 | + if (ok) found = true | | 80 | + } | | 81 | + | | 82 | + if (!found && files.length > 0 && toast) warn() | | 83 | + return found | | 84 | + } | | 85 | + | 74 | 86 | const removeAttachment = (id: string) => { | 75 | 87 | const current = prompt.current() | 76 | 88 | const next = current.filter((part) => part.type !== "image" || part.id !== id) | @@ -84,18 +96,14 @@ export function createPromptAttachments(input: PromptAttachmentsInput) { | 84 | 96 | event.preventDefault() | 85 | 97 | event.stopPropagation() | 86 | 98 |
| 87 | | - const items = Array.from(clipboardData.items) | 88 | | - const fileItems = items.filter((item) => item.kind === "file") | | 99 | + const files = Array.from(clipboardData.items).flatMap((item) => { | | 100 | + if (item.kind !== "file") return [] | | 101 | + const file = item.getAsFile() | | 102 | + return file ? [file] : [] | | 103 | + }) | 89 | 104 |
| 90 | | - if (fileItems.length > 0) { | 91 | | - let found = false | 92 | | - for (const item of fileItems) { | 93 | | - const file = item.getAsFile() | 94 | | - if (!file) continue | 95 | | - const ok = await add(file, false) | 96 | | - if (ok) found = true | 97 | | - } | 98 | | - if (!found) warn() | | 105 | + if (files.length > 0) { | | 106 | + await addAttachments(files) | 99 | 107 | return | 100 | 108 | } | 101 | 109 |
| @@ -169,12 +177,7 @@ export function createPromptAttachments(input: PromptAttachmentsInput) { | 169 | 177 | const dropped = event.dataTransfer?.files | 170 | 178 | if (!dropped) return | 171 | 179 |
| 172 | | - let found = false | 173 | | - for (const file of Array.from(dropped)) { | 174 | | - const ok = await add(file, false) | 175 | | - if (ok) found = true | 176 | | - } | 177 | | - if (!found && dropped.length > 0) warn() | | 180 | + await addAttachments(Array.from(dropped)) | 178 | 181 | } | 179 | 182 |
| 180 | 183 | onMount(() => { | @@ -191,6 +194,7 @@ export function createPromptAttachments(input: PromptAttachmentsInput) { | 191 | 194 |
| 192 | 195 | return { | 193 | 196 | addAttachment, | | 197 | + addAttachments, | 194 | 198 | removeAttachment, | 195 | 199 | handlePaste, | 196 | 200 | } | |
Collapse file Copy file name to clipboardExpand all lines: packages/app/src/components/prompt-input/build-request-parts.test.ts +26Lines changed: 26 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -49,6 +49,32 @@ describe("buildRequestParts", () => { | 49 | 49 | expect(result.optimisticParts.every((part) => part.sessionID === "ses_1" && part.messageID === "msg_1")).toBe(true) | 50 | 50 | }) | 51 | 51 |
| | 52 | + test("keeps multiple uploaded attachments in order", () => { | | 53 | + const result = buildRequestParts({ | | 54 | + prompt: [{ type: "text", content: "check these", start: 0, end: 11 }], | | 55 | + context: [], | | 56 | + images: [ | | 57 | + { type: "image", id: "img_1", filename: "a.png", mime: "image/png", dataUrl: "data:image/png;base64,AAA" }, | | 58 | + { | | 59 | + type: "image", | | 60 | + id: "img_2", | | 61 | + filename: "b.pdf", | | 62 | + mime: "application/pdf", | | 63 | + dataUrl: "data:application/pdf;base64,BBB", | | 64 | + }, | | 65 | + ], | | 66 | + text: "check these", | | 67 | + messageID: "msg_multi", | | 68 | + sessionID: "ses_multi", | | 69 | + sessionDirectory: "/repo", | | 70 | + }) | | 71 | + | | 72 | + const files = result.requestParts.filter((part) => part.type === "file" && part.url.startsWith("data:")) | | 73 | + | | 74 | + expect(files).toHaveLength(2) | | 75 | + expect(files.map((part) => (part.type === "file" ? part.filename : ""))).toEqual(["a.png", "b.pdf"]) | | 76 | + }) | | 77 | + | 52 | 78 | test("deduplicates context files when prompt already includes same path", () => { | 53 | 79 | const prompt: Prompt = [{ type: "file", path: "src/foo.ts", content: "@src/foo.ts", start: 0, end: 11 }] | 54 | 80 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/app/src/i18n/en.ts +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -276,7 +276,7 @@ export const dict = { | 276 | 276 | "prompt.context.includeActiveFile": "Include active file", | 277 | 277 | "prompt.context.removeActiveFile": "Remove active file from context", | 278 | 278 | "prompt.context.removeFile": "Remove file from context", | 279 | | - "prompt.action.attachFile": "Add file", | | 279 | + "prompt.action.attachFile": "Add files", | 280 | 280 | "prompt.attachment.remove": "Remove attachment", | 281 | 281 | "prompt.action.send": "Send", | 282 | 282 | "prompt.action.stop": "Stop", | |
Collapse file Copy file name to clipboard +44Lines changed: 44 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { describe, expect, test } from "bun:test" | | 2 | +import type { Part } from "@opencode-ai/sdk/v2" | | 3 | +import { extractPromptFromParts } from "./prompt" | | 4 | + | | 5 | +describe("extractPromptFromParts", () => { | | 6 | + test("restores multiple uploaded attachments", () => { | | 7 | + const parts = [ | | 8 | + { | | 9 | + id: "text_1", | | 10 | + type: "text", | | 11 | + text: "check these", | | 12 | + sessionID: "ses_1", | | 13 | + messageID: "msg_1", | | 14 | + }, | | 15 | + { | | 16 | + id: "file_1", | | 17 | + type: "file", | | 18 | + mime: "image/png", | | 19 | + url: "data:image/png;base64,AAA", | | 20 | + filename: "a.png", | | 21 | + sessionID: "ses_1", | | 22 | + messageID: "msg_1", | | 23 | + }, | | 24 | + { | | 25 | + id: "file_2", | | 26 | + type: "file", | | 27 | + mime: "application/pdf", | | 28 | + url: "data:application/pdf;base64,BBB", | | 29 | + filename: "b.pdf", | | 30 | + sessionID: "ses_1", | | 31 | + messageID: "msg_1", | | 32 | + }, | | 33 | + ] satisfies Part[] | | 34 | + | | 35 | + const result = extractPromptFromParts(parts) | | 36 | + | | 37 | + expect(result).toHaveLength(3) | | 38 | + expect(result[0]).toMatchObject({ type: "text", content: "check these" }) | | 39 | + expect(result.slice(1)).toMatchObject([ | | 40 | + { type: "image", filename: "a.png", mime: "image/png", dataUrl: "data:image/png;base64,AAA" }, | | 41 | + { type: "image", filename: "b.pdf", mime: "application/pdf", dataUrl: "data:application/pdf;base64,BBB" }, | | 42 | + ]) | | 43 | + }) | | 44 | +}) |
Collapse file Copy file name to clipboardExpand all lines: packages/storybook/.storybook/mocks/app/context/language.ts +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -8,7 +8,7 @@ const dict: Record<string, string> = { | 8 | 8 | "prompt.placeholder.shell": "Run a shell command...", | 9 | 9 | "prompt.placeholder.summarizeComment": "Summarize this comment", | 10 | 10 | "prompt.placeholder.summarizeComments": "Summarize these comments", | 11 | | - "prompt.action.attachFile": "Attach file", | | 11 | + "prompt.action.attachFile": "Attach files", | 12 | 12 | "prompt.action.send": "Send", | 13 | 13 | "prompt.action.stop": "Stop", | 14 | 14 | "prompt.attachment.remove": "Remove attachment", | |
|