GeezerVison is showing github.com — tweak: drop ids from attachments in tools, assign them in prompt.ts i… · motionharvest/opencode@a580fb4
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/session/prompt.ts +20-10Lines changed: 20 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -445,6 +445,12 @@ export namespace SessionPrompt { | 445 | 445 | log.error("subtask execution failed", { error, agent: task.agent, description: task.description }) | 446 | 446 | return undefined | 447 | 447 | }) | | 448 | + const attachments = result?.attachments?.map((attachment) => ({ | | 449 | + ...attachment, | | 450 | + id: Identifier.ascending("part"), | | 451 | + sessionID, | | 452 | + messageID: assistantMessage.id, | | 453 | + })) | 448 | 454 | await Plugin.trigger( | 449 | 455 | "tool.execute.after", | 450 | 456 | { | @@ -467,7 +473,7 @@ export namespace SessionPrompt { | 467 | 473 | title: result.title, | 468 | 474 | metadata: result.metadata, | 469 | 475 | output: result.output, | 470 | | - attachments: result.attachments, | | 476 | + attachments, | 471 | 477 | time: { | 472 | 478 | ...part.state.time, | 473 | 479 | end: Date.now(), | @@ -797,6 +803,15 @@ export namespace SessionPrompt { | 797 | 803 | }, | 798 | 804 | ) | 799 | 805 | const result = await item.execute(args, ctx) | | 806 | + const output = { | | 807 | + ...result, | | 808 | + attachments: result.attachments?.map((attachment) => ({ | | 809 | + ...attachment, | | 810 | + id: Identifier.ascending("part"), | | 811 | + sessionID: ctx.sessionID, | | 812 | + messageID: input.processor.message.id, | | 813 | + })), | | 814 | + } | 800 | 815 | await Plugin.trigger( | 801 | 816 | "tool.execute.after", | 802 | 817 | { | @@ -805,9 +820,9 @@ export namespace SessionPrompt { | 805 | 820 | callID: ctx.callID, | 806 | 821 | args, | 807 | 822 | }, | 808 | | - result, | | 823 | + output, | 809 | 824 | ) | 810 | | - return result | | 825 | + return output | 811 | 826 | }, | 812 | 827 | }) | 813 | 828 | } | @@ -855,16 +870,13 @@ export namespace SessionPrompt { | 855 | 870 | ) | 856 | 871 |
| 857 | 872 | const textParts: string[] = [] | 858 | | - const attachments: MessageV2.FilePart[] = [] | | 873 | + const attachments: Omit<MessageV2.FilePart, "id" | "sessionID" | "messageID">[] = [] | 859 | 874 |
| 860 | 875 | for (const contentItem of result.content) { | 861 | 876 | if (contentItem.type === "text") { | 862 | 877 | textParts.push(contentItem.text) | 863 | 878 | } else if (contentItem.type === "image") { | 864 | 879 | attachments.push({ | 865 | | - id: Identifier.ascending("part"), | 866 | | - sessionID: input.session.id, | 867 | | - messageID: input.processor.message.id, | 868 | 880 | type: "file", | 869 | 881 | mime: contentItem.mimeType, | 870 | 882 | url: `data:${contentItem.mimeType};base64,${contentItem.data}`, | @@ -876,9 +888,6 @@ export namespace SessionPrompt { | 876 | 888 | } | 877 | 889 | if (resource.blob) { | 878 | 890 | attachments.push({ | 879 | | - id: Identifier.ascending("part"), | 880 | | - sessionID: input.session.id, | 881 | | - messageID: input.processor.message.id, | 882 | 891 | type: "file", | 883 | 892 | mime: resource.mimeType ?? "application/octet-stream", | 884 | 893 | url: `data:${resource.mimeType ?? "application/octet-stream"};base64,${resource.blob}`, | @@ -1157,6 +1166,7 @@ export namespace SessionPrompt { | 1157 | 1166 | pieces.push( | 1158 | 1167 | ...result.attachments.map((attachment) => ({ | 1159 | 1168 | ...attachment, | | 1169 | + id: Identifier.ascending("part"), | 1160 | 1170 | synthetic: true, | 1161 | 1171 | filename: attachment.filename ?? part.filename, | 1162 | 1172 | messageID: info.id, | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/tool/batch.ts +7-1Lines changed: 7 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -77,6 +77,12 @@ export const BatchTool = Tool.define("batch", async () => { | 77 | 77 | }) | 78 | 78 |
| 79 | 79 | const result = await tool.execute(validatedParams, { ...ctx, callID: partID }) | | 80 | + const attachments = result.attachments?.map((attachment) => ({ | | 81 | + ...attachment, | | 82 | + id: Identifier.ascending("part"), | | 83 | + sessionID: ctx.sessionID, | | 84 | + messageID: ctx.messageID, | | 85 | + })) | 80 | 86 |
| 81 | 87 | await Session.updatePart({ | 82 | 88 | id: partID, | @@ -91,7 +97,7 @@ export const BatchTool = Tool.define("batch", async () => { | 91 | 97 | output: result.output, | 92 | 98 | title: result.title, | 93 | 99 | metadata: result.metadata, | 94 | | - attachments: result.attachments, | | 100 | + attachments, | 95 | 101 | time: { | 96 | 102 | start: callStartTime, | 97 | 103 | end: Date.now(), | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/tool/read.ts -4Lines changed: 0 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -6,7 +6,6 @@ import { LSP } from "../lsp" | 6 | 6 | import { FileTime } from "../file/time" | 7 | 7 | import DESCRIPTION from "./read.txt" | 8 | 8 | import { Instance } from "../project/instance" | 9 | | -import { Identifier } from "../id/id" | 10 | 9 | import { assertExternalDirectory } from "./external-directory" | 11 | 10 | import { InstructionPrompt } from "../session/instruction" | 12 | 11 |
| @@ -127,9 +126,6 @@ export const ReadTool = Tool.define("read", { | 127 | 126 | }, | 128 | 127 | attachments: [ | 129 | 128 | { | 130 | | - id: Identifier.ascending("part"), | 131 | | - sessionID: ctx.sessionID, | 132 | | - messageID: ctx.messageID, | 133 | 129 | type: "file", | 134 | 130 | mime, | 135 | 131 | url: `data:${mime};base64,${Buffer.from(await file.bytes()).toString("base64")}`, | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/tool/tool.ts +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -36,7 +36,7 @@ export namespace Tool { | 36 | 36 | title: string | 37 | 37 | metadata: M | 38 | 38 | output: string | 39 | | - attachments?: MessageV2.FilePart[] | | 39 | + attachments?: Omit<MessageV2.FilePart, "id" | "sessionID" | "messageID">[] | 40 | 40 | }> | 41 | 41 | formatValidationError?(error: z.ZodError): string | 42 | 42 | }> | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/tool/webfetch.ts -4Lines changed: 0 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -3,7 +3,6 @@ import { Tool } from "./tool" | 3 | 3 | import TurndownService from "turndown" | 4 | 4 | import DESCRIPTION from "./webfetch.txt" | 5 | 5 | import { abortAfterAny } from "../util/abort" | 6 | | -import { Identifier } from "../id/id" | 7 | 6 |
| 8 | 7 | const MAX_RESPONSE_SIZE = 5 * 1024 * 1024 // 5MB | 9 | 8 | const DEFAULT_TIMEOUT = 30 * 1000 // 30 seconds | @@ -103,9 +102,6 @@ export const WebFetchTool = Tool.define("webfetch", { | 103 | 102 | metadata: {}, | 104 | 103 | attachments: [ | 105 | 104 | { | 106 | | - id: Identifier.ascending("part"), | 107 | | - sessionID: ctx.sessionID, | 108 | | - messageID: ctx.messageID, | 109 | 105 | type: "file", | 110 | 106 | mime, | 111 | 107 | url: `data:${mime};base64,${base64Content}`, | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/test/tool/read.test.ts +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -349,6 +349,9 @@ describe("tool.read truncation", () => { | 349 | 349 | expect(result.metadata.truncated).toBe(false) | 350 | 350 | expect(result.attachments).toBeDefined() | 351 | 351 | expect(result.attachments?.length).toBe(1) | | 352 | + expect(result.attachments?.[0]).not.toHaveProperty("id") | | 353 | + expect(result.attachments?.[0]).not.toHaveProperty("sessionID") | | 354 | + expect(result.attachments?.[0]).not.toHaveProperty("messageID") | 352 | 355 | }, | 353 | 356 | }) | 354 | 357 | }) | @@ -363,6 +366,9 @@ describe("tool.read truncation", () => { | 363 | 366 | expect(result.attachments).toBeDefined() | 364 | 367 | expect(result.attachments?.length).toBe(1) | 365 | 368 | expect(result.attachments?.[0].type).toBe("file") | | 369 | + expect(result.attachments?.[0]).not.toHaveProperty("id") | | 370 | + expect(result.attachments?.[0]).not.toHaveProperty("sessionID") | | 371 | + expect(result.attachments?.[0]).not.toHaveProperty("messageID") | 366 | 372 | }, | 367 | 373 | }) | 368 | 374 | }) | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/test/tool/webfetch.test.ts +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -46,6 +46,9 @@ describe("tool.webfetch", () => { | 46 | 46 | expect(result.attachments?.[0].type).toBe("file") | 47 | 47 | expect(result.attachments?.[0].mime).toBe("image/png") | 48 | 48 | expect(result.attachments?.[0].url.startsWith("data:image/png;base64,")).toBe(true) | | 49 | + expect(result.attachments?.[0]).not.toHaveProperty("id") | | 50 | + expect(result.attachments?.[0]).not.toHaveProperty("sessionID") | | 51 | + expect(result.attachments?.[0]).not.toHaveProperty("messageID") | 49 | 52 | }, | 50 | 53 | }) | 51 | 54 | }, | |
|