Skip to content

Commit a580fb4

Browse files
authored
tweak: drop ids from attachments in tools, assign them in prompt.ts instead (#13890)
1 parent 9d3c81a commit a580fb4

File tree

7 files changed

+37
-20
lines changed

7 files changed

+37
-20
lines changed

packages/opencode/src/session/prompt.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,12 @@ export namespace SessionPrompt {
445445
log.error("subtask execution failed", { error, agent: task.agent, description: task.description })
446446
return undefined
447447
})
448+
const attachments = result?.attachments?.map((attachment) => ({
449+
...attachment,
450+
id: Identifier.ascending("part"),
451+
sessionID,
452+
messageID: assistantMessage.id,
453+
}))
448454
await Plugin.trigger(
449455
"tool.execute.after",
450456
{
@@ -467,7 +473,7 @@ export namespace SessionPrompt {
467473
title: result.title,
468474
metadata: result.metadata,
469475
output: result.output,
470-
attachments: result.attachments,
476+
attachments,
471477
time: {
472478
...part.state.time,
473479
end: Date.now(),
@@ -797,6 +803,15 @@ export namespace SessionPrompt {
797803
},
798804
)
799805
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+
}
800815
await Plugin.trigger(
801816
"tool.execute.after",
802817
{
@@ -805,9 +820,9 @@ export namespace SessionPrompt {
805820
callID: ctx.callID,
806821
args,
807822
},
808-
result,
823+
output,
809824
)
810-
return result
825+
return output
811826
},
812827
})
813828
}
@@ -855,16 +870,13 @@ export namespace SessionPrompt {
855870
)
856871

857872
const textParts: string[] = []
858-
const attachments: MessageV2.FilePart[] = []
873+
const attachments: Omit<MessageV2.FilePart, "id" | "sessionID" | "messageID">[] = []
859874

860875
for (const contentItem of result.content) {
861876
if (contentItem.type === "text") {
862877
textParts.push(contentItem.text)
863878
} else if (contentItem.type === "image") {
864879
attachments.push({
865-
id: Identifier.ascending("part"),
866-
sessionID: input.session.id,
867-
messageID: input.processor.message.id,
868880
type: "file",
869881
mime: contentItem.mimeType,
870882
url: `data:${contentItem.mimeType};base64,${contentItem.data}`,
@@ -876,9 +888,6 @@ export namespace SessionPrompt {
876888
}
877889
if (resource.blob) {
878890
attachments.push({
879-
id: Identifier.ascending("part"),
880-
sessionID: input.session.id,
881-
messageID: input.processor.message.id,
882891
type: "file",
883892
mime: resource.mimeType ?? "application/octet-stream",
884893
url: `data:${resource.mimeType ?? "application/octet-stream"};base64,${resource.blob}`,
@@ -1157,6 +1166,7 @@ export namespace SessionPrompt {
11571166
pieces.push(
11581167
...result.attachments.map((attachment) => ({
11591168
...attachment,
1169+
id: Identifier.ascending("part"),
11601170
synthetic: true,
11611171
filename: attachment.filename ?? part.filename,
11621172
messageID: info.id,

packages/opencode/src/tool/batch.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ export const BatchTool = Tool.define("batch", async () => {
7777
})
7878

7979
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+
}))
8086

8187
await Session.updatePart({
8288
id: partID,
@@ -91,7 +97,7 @@ export const BatchTool = Tool.define("batch", async () => {
9197
output: result.output,
9298
title: result.title,
9399
metadata: result.metadata,
94-
attachments: result.attachments,
100+
attachments,
95101
time: {
96102
start: callStartTime,
97103
end: Date.now(),

packages/opencode/src/tool/read.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { LSP } from "../lsp"
66
import { FileTime } from "../file/time"
77
import DESCRIPTION from "./read.txt"
88
import { Instance } from "../project/instance"
9-
import { Identifier } from "../id/id"
109
import { assertExternalDirectory } from "./external-directory"
1110
import { InstructionPrompt } from "../session/instruction"
1211

@@ -127,9 +126,6 @@ export const ReadTool = Tool.define("read", {
127126
},
128127
attachments: [
129128
{
130-
id: Identifier.ascending("part"),
131-
sessionID: ctx.sessionID,
132-
messageID: ctx.messageID,
133129
type: "file",
134130
mime,
135131
url: `data:${mime};base64,${Buffer.from(await file.bytes()).toString("base64")}`,

packages/opencode/src/tool/tool.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export namespace Tool {
3636
title: string
3737
metadata: M
3838
output: string
39-
attachments?: MessageV2.FilePart[]
39+
attachments?: Omit<MessageV2.FilePart, "id" | "sessionID" | "messageID">[]
4040
}>
4141
formatValidationError?(error: z.ZodError): string
4242
}>

packages/opencode/src/tool/webfetch.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { Tool } from "./tool"
33
import TurndownService from "turndown"
44
import DESCRIPTION from "./webfetch.txt"
55
import { abortAfterAny } from "../util/abort"
6-
import { Identifier } from "../id/id"
76

87
const MAX_RESPONSE_SIZE = 5 * 1024 * 1024 // 5MB
98
const DEFAULT_TIMEOUT = 30 * 1000 // 30 seconds
@@ -103,9 +102,6 @@ export const WebFetchTool = Tool.define("webfetch", {
103102
metadata: {},
104103
attachments: [
105104
{
106-
id: Identifier.ascending("part"),
107-
sessionID: ctx.sessionID,
108-
messageID: ctx.messageID,
109105
type: "file",
110106
mime,
111107
url: `data:${mime};base64,${base64Content}`,

packages/opencode/test/tool/read.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,9 @@ describe("tool.read truncation", () => {
349349
expect(result.metadata.truncated).toBe(false)
350350
expect(result.attachments).toBeDefined()
351351
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")
352355
},
353356
})
354357
})
@@ -363,6 +366,9 @@ describe("tool.read truncation", () => {
363366
expect(result.attachments).toBeDefined()
364367
expect(result.attachments?.length).toBe(1)
365368
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")
366372
},
367373
})
368374
})

packages/opencode/test/tool/webfetch.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ describe("tool.webfetch", () => {
4646
expect(result.attachments?.[0].type).toBe("file")
4747
expect(result.attachments?.[0].mime).toBe("image/png")
4848
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")
4952
},
5053
})
5154
},

0 commit comments

Comments
 (0)