Skip to content

Use Claude Code with your ChatGPT, Kimi, Cursor or Grok subscription via a local Anthropic-compatible proxy

Rust

harden tool result content handling

Raine Virta5c1f67ff7d4d
diff --git a/src/anthropic/schema.ts b/src/anthropic/schema.ts
index c707cb6..ae58faf 100644
--- a/src/anthropic/schema.ts
+++ b/src/anthropic/schema.ts
@@ -21,7 +21,7 @@ export interface AnthropicToolUseBlock {
export interface AnthropicToolResultBlock {
type: "tool_result"
tool_use_id: string
- content: string | Array<AnthropicTextBlock | AnthropicImageBlock>
+ content: string | AnthropicToolResultContentBlock[]
is_error?: boolean
}
@@ -31,6 +31,14 @@ export interface AnthropicThinkingBlock {
signature?: string
}
+export type AnthropicToolResultContentBlock =
+ | AnthropicTextBlock
+ | AnthropicImageBlock
+ | AnthropicToolUseBlock
+ | AnthropicToolResultBlock
+ | AnthropicThinkingBlock
+ | (Record<string, unknown> & { type?: unknown })
+
export type AnthropicContentBlock =
| AnthropicTextBlock
| AnthropicImageBlock
diff --git a/src/providers/codex/translate/request.test.ts b/src/providers/codex/translate/request.test.ts
index efb4f96..fdf0b0f 100644
--- a/src/providers/codex/translate/request.test.ts
+++ b/src/providers/codex/translate/request.test.ts
@@ -1,7 +1,8 @@
import { afterEach, describe, expect, it } from "bun:test"
import { loadConfig } from "../../../config.ts"
import type { AnthropicRequest } from "../../../anthropic/schema.ts"
-import { InvalidServiceTierError, translateRequest } from "./request.ts"
+import { countTokens } from "../count-tokens.ts"
+import { InvalidServiceTierError, toolResultToString, translateRequest } from "./request.ts"
const baseRequest: AnthropicRequest = {
model: "claude-sonnet-4-6",
messages: [{ role: "user", content: "hello" }],
@@ -68,6 +69,79 @@ describe("translateRequest", () => {
expect(() => translateRequest(baseRequest)).toThrow('Invalid service tier override: "standard"')
})
+ it("translates unsupported tool result content blocks without throwing", () => {
+ const translated = translateRequest({
+ ...baseRequest,
+ messages: [
+ {
+ role: "user",
+ content: [
+ {
+ type: "tool_result",
+ tool_use_id: "toolu_1",
+ content: [
+ { type: "text", text: "visible output" },
+ { type: "thinking", thinking: "hidden thought" },
+ ],
+ },
+ ],
+ },
+ ],
+ })
+
+ expect(translated.input).toEqual([
+ {
+ type: "function_call_output",
+ call_id: "toolu_1",
+ output: "visible output\n[unsupported content block omitted: thinking]",
+ },
+ ])
+ })
+
+ it("preserves text and image tool result stringification", () => {
+ expect(
+ toolResultToString([
+ { type: "text", text: "caption" },
+ {
+ type: "image",
+ source: { type: "base64", media_type: "image/png", data: "abc" },
+ },
+ { type: "image", source: { type: "url", url: "https://example.invalid/a.png" } },
+ ]),
+ ).toBe("caption\n[image omitted: image/png]\n[image omitted: url]")
+ })
+
+ it("counts unsupported tool result content blocks without throwing", () => {
+ expect(
+ countTokens({
+ ...baseRequest,
+ messages: [
+ {
+ role: "user",
+ content: [
+ {
+ type: "tool_result",
+ tool_use_id: "toolu_1",
+ content: [{ type: "thinking", thinking: "hidden thought" }],
+ },
+ ],
+ },
+ ],
+ }),
+ ).toBeGreaterThan(0)
+ })
+
+ it("treats malformed tool result content blocks as unsupported", () => {
+ expect(
+ toolResultToString([
+ { type: "text" },
+ { type: "image" },
+ ]),
+ ).toBe(
+ "[unsupported content block omitted: text]\n[unsupported content block omitted: image]",
+ )
+ })
+
it("returns only the expected top-level upstream request fields", () => {
const translated = translateRequest({
...baseRequest,
diff --git a/src/providers/codex/translate/request.ts b/src/providers/codex/translate/request.ts
index 74ddf08..039becf 100644
--- a/src/providers/codex/translate/request.ts
+++ b/src/providers/codex/translate/request.ts
@@ -5,6 +5,7 @@ import type {
AnthropicRequest,
AnthropicTextBlock,
AnthropicTool,
+ AnthropicToolResultContentBlock,
} from "../../../anthropic/schema.ts"
import { codexEffort, codexServiceTier } from "../../../config.ts"
@@ -261,16 +262,47 @@ function imageToUrl(block: Extract<AnthropicContentBlock, { type: "image" }>): s
return `data:${block.source.media_type};base64,${block.source.data}`
}
+function unsupportedToolResultBlockToString(block: AnthropicToolResultContentBlock): string {
+ const type = typeof block.type === "string" ? block.type : "unknown"
+ return `[unsupported content block omitted: ${type}]`
+}
+
+function isToolResultTextBlock(
+ block: AnthropicToolResultContentBlock,
+): block is AnthropicTextBlock {
+ return block.type === "text" && typeof block.text === "string"
+}
+
+function isRecord(value: unknown): value is Record<string, unknown> {
+ return !!value && typeof value === "object"
+}
+
+function isToolResultImageBlock(
+ block: AnthropicToolResultContentBlock,
+): block is AnthropicImageBlock {
+ if (block.type !== "image") return false
+ const source = block.source
+ if (!isRecord(source)) return false
+ if (source.type === "url") return typeof source.url === "string"
+ return (
+ source.type === "base64" &&
+ typeof source.media_type === "string" &&
+ typeof source.data === "string"
+ )
+}
+
export function toolResultToString(
- content: string | Array<AnthropicTextBlock | AnthropicImageBlock>,
+ content: string | AnthropicToolResultContentBlock[],
): string {
if (typeof content === "string") return content
return content
.map((b) => {
- if (b.type === "text") return b.text
- const mt =
- b.source.type === "base64" ? b.source.media_type : "url"
- return `[image omitted: ${mt}]`
+ if (isToolResultTextBlock(b)) return b.text
+ if (isToolResultImageBlock(b)) {
+ const mt = b.source.type === "base64" ? b.source.media_type : "url"
+ return `[image omitted: ${mt}]`
+ }
+ return unsupportedToolResultBlockToString(b)
})
.join("\n")
}
diff --git a/src/providers/kimi/translate/request.test.ts b/src/providers/kimi/translate/request.test.ts
new file mode 100644
index 0000000..65cab82
--- /dev/null
+++ b/src/providers/kimi/translate/request.test.ts
@@ -0,0 +1,93 @@
+import { describe, expect, it } from "bun:test"
+import type { AnthropicRequest } from "../../../anthropic/schema.ts"
+import { countTokens } from "../count-tokens.ts"
+import { translateRequest } from "./request.ts"
+
+describe("translateRequest", () => {
+ it("translates unsupported tool result content blocks as text parts", () => {
+ const req: AnthropicRequest = {
+ model: "kimi-k2",
+ messages: [
+ {
+ role: "user",
+ content: [
+ {
+ type: "tool_result",
+ tool_use_id: "toolu_1",
+ content: [
+ { type: "text", text: "visible output" },
+ { type: "thinking", thinking: "hidden thought" },
+ ],
+ },
+ ],
+ },
+ ],
+ }
+
+ expect(translateRequest(req).messages).toEqual([
+ {
+ role: "tool",
+ tool_call_id: "toolu_1",
+ content: [
+ { type: "text", text: "visible output" },
+ { type: "text", text: "[unsupported content block omitted: thinking]" },
+ ],
+ },
+ ])
+ })
+
+ it("preserves image tool result content parts", () => {
+ const req: AnthropicRequest = {
+ model: "kimi-k2",
+ messages: [
+ {
+ role: "user",
+ content: [
+ {
+ type: "tool_result",
+ tool_use_id: "toolu_1",
+ content: [
+ { type: "text", text: "caption" },
+ {
+ type: "image",
+ source: { type: "base64", media_type: "image/png", data: "abc" },
+ },
+ ],
+ },
+ ],
+ },
+ ],
+ }
+
+ expect(translateRequest(req).messages).toEqual([
+ {
+ role: "tool",
+ tool_call_id: "toolu_1",
+ content: [
+ { type: "text", text: "caption" },
+ { type: "image_url", image_url: { url: "data:image/png;base64,abc" } },
+ ],
+ },
+ ])
+ })
+
+ it("counts unsupported tool result content blocks without throwing", () => {
+ const req: AnthropicRequest = {
+ model: "kimi-k2",
+ messages: [
+ {
+ role: "user",
+ content: [
+ {
+ type: "tool_result",
+ tool_use_id: "toolu_1",
+ content: [{ type: "thinking", thinking: "hidden thought" }],
+ },
+ ],
+ },
+ ],
+ }
+
+ expect(countTokens(req)).toBeGreaterThan(0)
+ })
+})
diff --git a/src/providers/kimi/translate/request.ts b/src/providers/kimi/translate/request.ts
index 470b79f..3545616 100644
--- a/src/providers/kimi/translate/request.ts
+++ b/src/providers/kimi/translate/request.ts
@@ -5,6 +5,7 @@ import type {
AnthropicRequest,
AnthropicTextBlock,
AnthropicTool,
+ AnthropicToolResultContentBlock,
} from "../../../anthropic/schema.ts"
// OpenAI-compatible chat-completions request shape used by Kimi.
@@ -250,8 +251,37 @@ function imageToUrl(block: Extract<AnthropicContentBlock, { type: "image" }>): s
return `data:${block.source.media_type};base64,${block.source.data}`
}
+function unsupportedToolResultBlockToString(block: AnthropicToolResultContentBlock): string {
+ const type = typeof block.type === "string" ? block.type : "unknown"
+ return `[unsupported content block omitted: ${type}]`
+}
+
+function isToolResultTextBlock(
+ block: AnthropicToolResultContentBlock,
+): block is AnthropicTextBlock {
+ return block.type === "text" && typeof block.text === "string"
+}
+
+function isRecord(value: unknown): value is Record<string, unknown> {
+ return !!value && typeof value === "object"
+}
+
+function isToolResultImageBlock(
+ block: AnthropicToolResultContentBlock,
+): block is AnthropicImageBlock {
+ if (block.type !== "image") return false
+ const source = block.source
+ if (!isRecord(source)) return false
+ if (source.type === "url") return typeof source.url === "string"
+ return (
+ source.type === "base64" &&
+ typeof source.media_type === "string" &&
+ typeof source.data === "string"
+ )
+}
+
export function toolResultContent(
- content: string | Array<AnthropicTextBlock | AnthropicImageBlock>,
+ content: string | AnthropicToolResultContentBlock[],
isError: boolean | undefined,
): string | KimiToolResultPart[] {
const prefix = isError ? "[tool execution error]\n" : ""
@@ -260,12 +290,12 @@ export function toolResultContent(
const parts: KimiToolResultPart[] = []
if (prefix) parts.push({ type: "text", text: prefix })
for (const b of content) {
- if (b.type === "text") {
+ if (isToolResultTextBlock(b)) {
parts.push({ type: "text", text: b.text })
- } else {
- // Kimi accepts image_url parts inside role="tool" content (its own
- // ReadMediaFile tool uses exactly this shape).
+ } else if (isToolResultImageBlock(b)) {
parts.push({ type: "image_url", image_url: { url: imageToUrl(b) } })
+ } else {
+ parts.push({ type: "text", text: unsupportedToolResultBlockToString(b) })
}
}
if (parts.length === 1 && parts[0]!.type === "text") return parts[0]!.text
@@ -273,15 +303,18 @@ export function toolResultContent(
}
export function toolResultToString(
- content: string | Array<AnthropicTextBlock | AnthropicImageBlock>,
+ content: string | AnthropicToolResultContentBlock[],
): string {
// Kept for the token counter, which wants a flat string.
if (typeof content === "string") return content
return content
.map((b) => {
- if (b.type === "text") return b.text
- const mt = b.source.type === "base64" ? b.source.media_type : "url"
- return `[image omitted: ${mt}]`
+ if (isToolResultTextBlock(b)) return b.text
+ if (isToolResultImageBlock(b)) {
+ const mt = b.source.type === "base64" ? b.source.media_type : "url"
+ return `[image omitted: ${mt}]`
+ }
+ return unsupportedToolResultBlockToString(b)
})
.join("\n")
}