|
1 | 1 | import { BusEvent } from "@/bus/bus-event" |
2 | 2 | import z from "zod" |
3 | 3 | 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" |
12 | 5 | import { Identifier } from "../id/id" |
13 | 6 | import { LSP } from "../lsp" |
14 | 7 | import { Snapshot } from "@/snapshot" |
@@ -439,7 +432,7 @@ export namespace MessageV2 { |
439 | 432 | }) |
440 | 433 | export type WithParts = z.infer<typeof WithParts> |
441 | 434 |
|
442 | | - export function toModelMessage(input: WithParts[], options?: { tools?: ToolSet }): ModelMessage[] { |
| 435 | + export function toModelMessage(input: WithParts[]): ModelMessage[] { |
443 | 436 | const result: UIMessage[] = [] |
444 | 437 |
|
445 | 438 | for (const msg of input) { |
@@ -510,14 +503,30 @@ export namespace MessageV2 { |
510 | 503 | }) |
511 | 504 | if (part.type === "tool") { |
512 | 505 | 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 | + } |
513 | 524 | assistantMessage.parts.push({ |
514 | 525 | type: ("tool-" + part.tool) as `tool-${string}`, |
515 | 526 | state: "output-available", |
516 | 527 | toolCallId: part.callID, |
517 | 528 | 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, |
521 | 530 | callProviderMetadata: part.metadata, |
522 | 531 | }) |
523 | 532 | } |
@@ -556,12 +565,7 @@ export namespace MessageV2 { |
556 | 565 | } |
557 | 566 | } |
558 | 567 |
|
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"))) |
565 | 569 | } |
566 | 570 |
|
567 | 571 | export const stream = fn(Identifier.schema("session"), async function* (sessionID) { |
|
0 commit comments