GeezerVison is showing github.com — fix(ai): prevent unsupported tool files from breaking Chat sessions · anomalyco/opencode@511983b
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/ai/src/protocols/openai-chat.ts +16-3Lines changed: 16 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -518,12 +518,25 @@ const lowerToolMessages = Effect.fnUntraced(function* ( | 518 | 518 | continue | 519 | 519 | } | 520 | 520 | const content: ReadonlyArray<Tool.Content> = part.result.value | 521 | | - const text = content.filter((item) => item.type === "text").map((item) => item.text) | | 521 | + const text: string[] = [] | | 522 | + for (const item of content) { | | 523 | + if (item.type === "text") { | | 524 | + text.push(item.text) | | 525 | + continue | | 526 | + } | | 527 | + const media = ProviderShared.toolFileMedia(item) | | 528 | + // Tool files persist in history, so explain unsupported types instead of failing every later request. | | 529 | + if (media.media.kind !== "image" && media.media.mediaType.toLowerCase() !== "application/pdf") { | | 530 | + text.push( | | 531 | + `[File omitted: ${item.name ?? "unnamed file"} (${item.mime}). OpenAI Chat does not support this file type.]`, | | 532 | + ) | | 533 | + continue | | 534 | + } | | 535 | + attachments.push(yield* lowerMedia(media)) | | 536 | + } | 522 | 537 | messages.push( | 523 | 538 | toolMessage(options.toolCallID?.(part.id) ?? part.id, text.join("\n"), options.cacheControl?.(part.cache)), | 524 | 539 | ) | 525 | | - const files = content.filter((item) => item.type === "file") | 526 | | - attachments.push(...(yield* Effect.forEach(files, (item) => lowerMedia(ProviderShared.toolFileMedia(item))))) | 527 | 540 | } | 528 | 541 | return { messages, attachments } | 529 | 542 | }) | |
Collapse file Copy file name to clipboardExpand all lines: packages/ai/test/provider/openai-chat.test.ts +89Lines changed: 89 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -605,6 +605,95 @@ describe("OpenAI Chat route", () => { | 605 | 605 | }), | 606 | 606 | ) | 607 | 607 |
| | 608 | + for (const file of [ | | 609 | + { mime: "application/vnd.apache.parquet", name: "table.parquet" }, | | 610 | + { mime: "text/csv", name: undefined }, | | 611 | + ]) { | | 612 | + it.effect(`continues history containing an unsupported ${file.mime} tool file`, () => | | 613 | + Effect.gen(function* () { | | 614 | + const messages = [ | | 615 | + Message.user("Read the table."), | | 616 | + Message.assistant([ToolCallPart.make({ id: "call_table", name: "read", input: {} })]), | | 617 | + Message.tool({ | | 618 | + id: "call_table", | | 619 | + name: "read", | | 620 | + resultType: "content", | | 621 | + result: [ | | 622 | + { type: "text", text: "Table read successfully" }, | | 623 | + { type: "file", mime: file.mime, name: file.name, uri: "data:application/octet-stream;base64,UEFSMQ==" }, | | 624 | + ], | | 625 | + }), | | 626 | + ] | | 627 | + const stored = encodeJson(messages) | | 628 | + for (const history of [messages, [...messages, Message.user("What can we do next?")]]) { | | 629 | + const prepared = yield* compileRequest(LLM.request({ model, messages: history })) | | 630 | + expect(prepared.body.messages[2]).toMatchObject({ | | 631 | + role: "tool", | | 632 | + tool_call_id: "call_table", | | 633 | + }) | | 634 | + expect(prepared.body.messages[2]?.content).toContain("Table read successfully") | | 635 | + const text = encodeJson(prepared.body.messages) | | 636 | + expect(text).toContain("omitted") | | 637 | + expect(text).toContain(file.name ?? "unnamed file") | | 638 | + expect(text).toContain(file.mime) | | 639 | + expect(text).not.toContain("UEFSMQ==") | | 640 | + if (history.length > messages.length) | | 641 | + expect(prepared.body.messages.at(-1)).toEqual({ role: "user", content: "What can we do next?" }) | | 642 | + } | | 643 | + expect(encodeJson(messages)).toBe(stored) | | 644 | + }), | | 645 | + ) | | 646 | + } | | 647 | + | | 648 | + it.effect("keeps PNG and PDF tool files while explaining an omitted CSV", () => | | 649 | + Effect.gen(function* () { | | 650 | + const prepared = yield* compileRequest( | | 651 | + LLM.request({ | | 652 | + model, | | 653 | + messages: [ | | 654 | + Message.assistant([ToolCallPart.make({ id: "call_files", name: "read", input: {} })]), | | 655 | + Message.tool({ | | 656 | + id: "call_files", | | 657 | + name: "read", | | 658 | + resultType: "content", | | 659 | + result: [ | | 660 | + { type: "text", text: "Files read successfully" }, | | 661 | + { type: "file", mime: "image/png", name: "pixel.png", uri: "data:image/png;base64,AAEC" }, | | 662 | + { type: "file", mime: "text/csv", name: "table.csv", uri: "data:text/csv;base64,YSxi" }, | | 663 | + { | | 664 | + type: "file", | | 665 | + mime: "application/pdf", | | 666 | + name: "report.pdf", | | 667 | + uri: "data:application/pdf;base64,JVBERi0=", | | 668 | + }, | | 669 | + ], | | 670 | + }), | | 671 | + Message.user("Describe what is available."), | | 672 | + ], | | 673 | + }), | | 674 | + ) | | 675 | + expect(prepared.body.messages[1]).toMatchObject({ | | 676 | + role: "tool", | | 677 | + tool_call_id: "call_files", | | 678 | + }) | | 679 | + expect(prepared.body.messages[1]?.content).toContain("Files read successfully") | | 680 | + expect(prepared.body.messages[1]?.content).toContain("omitted") | | 681 | + expect(prepared.body.messages[1]?.content).toContain("table.csv") | | 682 | + expect(prepared.body.messages[1]?.content).toContain("text/csv") | | 683 | + expect(prepared.body.messages.slice(2)).toEqual([ | | 684 | + { | | 685 | + role: "user", | | 686 | + content: [ | | 687 | + { type: "image_url", image_url: { url: "data:image/png;base64,AAEC" } }, | | 688 | + { type: "file", file: { filename: "report.pdf", file_data: "data:application/pdf;base64,JVBERi0=" } }, | | 689 | + ], | | 690 | + }, | | 691 | + { role: "user", content: "Describe what is available." }, | | 692 | + ]) | | 693 | + expect(encodeJson(prepared.body.messages)).not.toContain("YSxi") | | 694 | + }), | | 695 | + ) | | 696 | + | 608 | 697 | it.effect("bridges image tool results before their synthetic user message when required", () => | 609 | 698 | Effect.gen(function* () { | 610 | 699 | const prepared = yield* compileRequest( | |
|