Skip to content

Commit e0a854f

Browse files
committed
Revert "fix: rm user message when dealing w/ image attachments, use proper tool attachment instead"
This reverts commit de2de09.
1 parent bd914a8 commit e0a854f

2 files changed

Lines changed: 29 additions & 41 deletions

File tree

packages/opencode/src/session/message-v2.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
import { BusEvent } from "@/bus/bus-event"
22
import z from "zod"
33
import { NamedError } from "@opencode-ai/util/error"
4-
import {
5-
APICallError,
6-
convertToModelMessages,
7-
LoadAPIKeyError,
8-
type ModelMessage,
9-
type UIMessage,
10-
type ToolSet,
11-
} from "ai"
4+
import { APICallError, convertToModelMessages, LoadAPIKeyError, type ModelMessage, type UIMessage } from "ai"
125
import { Identifier } from "../id/id"
136
import { LSP } from "../lsp"
147
import { Snapshot } from "@/snapshot"
@@ -439,7 +432,7 @@ export namespace MessageV2 {
439432
})
440433
export type WithParts = z.infer<typeof WithParts>
441434

442-
export function toModelMessage(input: WithParts[], options?: { tools?: ToolSet }): ModelMessage[] {
435+
export function toModelMessage(input: WithParts[]): ModelMessage[] {
443436
const result: UIMessage[] = []
444437

445438
for (const msg of input) {
@@ -510,14 +503,30 @@ export namespace MessageV2 {
510503
})
511504
if (part.type === "tool") {
512505
if (part.state.status === "completed") {
506+
if (part.state.attachments?.length) {
507+
result.push({
508+
id: Identifier.ascending("message"),
509+
role: "user",
510+
parts: [
511+
{
512+
type: "text",
513+
text: `Tool ${part.tool} returned an attachment:`,
514+
},
515+
...part.state.attachments.map((attachment) => ({
516+
type: "file" as const,
517+
url: attachment.url,
518+
mediaType: attachment.mime,
519+
filename: attachment.filename,
520+
})),
521+
],
522+
})
523+
}
513524
assistantMessage.parts.push({
514525
type: ("tool-" + part.tool) as `tool-${string}`,
515526
state: "output-available",
516527
toolCallId: part.callID,
517528
input: part.state.input,
518-
output: part.state.time.compacted
519-
? "[Old tool result content cleared]"
520-
: { output: part.state.output, attachments: part.state.attachments },
529+
output: part.state.time.compacted ? "[Old tool result content cleared]" : part.state.output,
521530
callProviderMetadata: part.metadata,
522531
})
523532
}
@@ -556,12 +565,7 @@ export namespace MessageV2 {
556565
}
557566
}
558567

559-
return convertToModelMessages(
560-
result.filter((msg) => msg.parts.some((part) => part.type !== "step-start")),
561-
{
562-
tools: options?.tools,
563-
},
564-
)
568+
return convertToModelMessages(result.filter((msg) => msg.parts.some((part) => part.type !== "step-start")))
565569
}
566570

567571
export const stream = fn(Identifier.schema("session"), async function* (sessionID) {

packages/opencode/src/session/prompt.ts

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ export namespace SessionPrompt {
597597
sessionID,
598598
system: [...(await SystemPrompt.environment()), ...(await SystemPrompt.custom())],
599599
messages: [
600-
...MessageV2.toModelMessage(sessionMessages, { tools }),
600+
...MessageV2.toModelMessage(sessionMessages),
601601
...(isLastStep
602602
? [
603603
{
@@ -716,18 +716,10 @@ export namespace SessionPrompt {
716716
)
717717
return result
718718
},
719-
toModelOutput(result: { output: string; attachments?: MessageV2.FilePart[] }) {
720-
if (!result.attachments?.length) return { type: "text", value: result.output }
719+
toModelOutput(result) {
721720
return {
722-
type: "content",
723-
value: [
724-
{ type: "text", text: result.output },
725-
...result.attachments.map((a) => ({
726-
type: "media" as const,
727-
data: a.url.slice(a.url.indexOf(",") + 1),
728-
mediaType: a.mime,
729-
})),
730-
],
721+
type: "text",
722+
value: result.output,
731723
}
732724
},
733725
})
@@ -814,18 +806,10 @@ export namespace SessionPrompt {
814806
content: result.content, // directly return content to preserve ordering when outputting to model
815807
}
816808
}
817-
item.toModelOutput = (result: { output: string; attachments?: MessageV2.FilePart[] }) => {
818-
if (!result.attachments?.length) return { type: "text", value: result.output }
809+
item.toModelOutput = (result) => {
819810
return {
820-
type: "content",
821-
value: [
822-
{ type: "text", text: result.output },
823-
...result.attachments.map((a) => ({
824-
type: "media" as const,
825-
data: a.url.slice(a.url.indexOf(",") + 1),
826-
mediaType: a.mime,
827-
})),
828-
],
811+
type: "text",
812+
value: result.output,
829813
}
830814
}
831815
tools[key] = item

0 commit comments

Comments
 (0)