|
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 { APICallError, convertToModelMessages, LoadAPIKeyError, type ModelMessage, type UIMessage } from "ai" |
| 4 | +import { |
| 5 | + APICallError, |
| 6 | + convertToModelMessages, |
| 7 | + LoadAPIKeyError, |
| 8 | + type ModelMessage, |
| 9 | + type UIMessage, |
| 10 | + type ToolSet, |
| 11 | +} from "ai" |
5 | 12 | import { Identifier } from "../id/id" |
6 | 13 | import { LSP } from "../lsp" |
7 | 14 | import { Snapshot } from "@/snapshot" |
@@ -432,7 +439,7 @@ export namespace MessageV2 { |
432 | 439 | }) |
433 | 440 | export type WithParts = z.infer<typeof WithParts> |
434 | 441 |
|
435 | | - export function toModelMessage(input: WithParts[]): ModelMessage[] { |
| 442 | + export function toModelMessage(input: WithParts[], options?: { tools?: ToolSet }): ModelMessage[] { |
436 | 443 | const result: UIMessage[] = [] |
437 | 444 |
|
438 | 445 | for (const msg of input) { |
@@ -503,30 +510,14 @@ export namespace MessageV2 { |
503 | 510 | }) |
504 | 511 | if (part.type === "tool") { |
505 | 512 | 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 | | - } |
524 | 513 | assistantMessage.parts.push({ |
525 | 514 | type: ("tool-" + part.tool) as `tool-${string}`, |
526 | 515 | state: "output-available", |
527 | 516 | toolCallId: part.callID, |
528 | 517 | input: part.state.input, |
529 | | - output: part.state.time.compacted ? "[Old tool result content cleared]" : part.state.output, |
| 518 | + output: part.state.time.compacted |
| 519 | + ? "[Old tool result content cleared]" |
| 520 | + : { output: part.state.output, attachments: part.state.attachments }, |
530 | 521 | callProviderMetadata: part.metadata, |
531 | 522 | }) |
532 | 523 | } |
@@ -565,7 +556,12 @@ export namespace MessageV2 { |
565 | 556 | } |
566 | 557 | } |
567 | 558 |
|
568 | | - return convertToModelMessages(result.filter((msg) => msg.parts.some((part) => part.type !== "step-start"))) |
| 559 | + return convertToModelMessages( |
| 560 | + result.filter((msg) => msg.parts.some((part) => part.type !== "step-start")), |
| 561 | + { |
| 562 | + tools: options?.tools, |
| 563 | + }, |
| 564 | + ) |
569 | 565 | } |
570 | 566 |
|
571 | 567 | export const stream = fn(Identifier.schema("session"), async function* (sessionID) { |
|
0 commit comments