GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(ai): prevent unsupported tool files from breaking Chat sessions · anomalyco/opencode@511983b

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • packages/ai
    • src/protocols
      • openai-chat.ts
    • test/provider
      • openai-chat.test.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/ai/src/protocols/openai-chat.ts‎

Copy file name to clipboardExpand all lines: packages/ai/src/protocols/openai-chat.ts
+16-3Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -518,12 +518,25 @@ const lowerToolMessages = Effect.fnUntraced(function* (
518518
continue
519519
}
520520
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+
}
522537
messages.push(
523538
toolMessage(options.toolCallID?.(part.id) ?? part.id, text.join("\n"), options.cacheControl?.(part.cache)),
524539
)
525-
const files = content.filter((item) => item.type === "file")
526-
attachments.push(...(yield* Effect.forEach(files, (item) => lowerMedia(ProviderShared.toolFileMedia(item)))))
527540
}
528541
return { messages, attachments }
529542
})
Collapse file

‎packages/ai/test/provider/openai-chat.test.ts‎

Copy file name to clipboardExpand all lines: packages/ai/test/provider/openai-chat.test.ts
+89Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,95 @@ describe("OpenAI Chat route", () => {
605605
}),
606606
)
607607

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+
608697
it.effect("bridges image tool results before their synthetic user message when required", () =>
609698
Effect.gen(function* () {
610699
const prepared = yield* compileRequest(

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.