Skip to content

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

Rust

share anthropic tool choice mapping

Raine Virtaaeb9533cc98e
diff --git a/src/providers/codex/translate/request.ts b/src/providers/codex/translate/request.ts
index 410ab1f..4881f26 100644
--- a/src/providers/codex/translate/request.ts
+++ b/src/providers/codex/translate/request.ts
@@ -7,6 +7,7 @@ import type {
import { codexEffort, codexServiceTier } from "../../../config.ts";
import {
assertValidEffort,
+ mapToolChoice as mapAnthropicToolChoice,
flattenSystemText,
imageBlockToUrl,
normalizeContent,
@@ -199,17 +200,9 @@ export function translateRequest(
}
function mapToolChoice(choice: AnthropicRequest["tool_choice"]): ResponsesRequest["tool_choice"] {
- if (!choice) return "auto";
- switch (choice.type) {
- case "auto":
- return "auto";
- case "none":
- return "none";
- case "any":
- return "required";
- case "tool":
- return choice.name ? { type: "function", name: choice.name } : "required";
- }
+ const mapped = mapAnthropicToolChoice(choice);
+ if (mapped === "auto" || mapped === "none" || mapped === "required") return mapped;
+ return { type: "function", name: mapped.name };
}
export const buildInstructions = flattenSystemText;
diff --git a/src/providers/kimi/translate/request.ts b/src/providers/kimi/translate/request.ts
index fc5195c..59bbbee 100644
--- a/src/providers/kimi/translate/request.ts
+++ b/src/providers/kimi/translate/request.ts
@@ -6,6 +6,7 @@ import type {
} from "../../../anthropic/schema.ts";
import {
assertValidEffort,
+ mapToolChoice as mapAnthropicToolChoice,
flattenSystemText,
imageBlockToUrl,
isToolResultImageBlock,
@@ -121,17 +122,9 @@ function mapReasoningEffort(
}
function mapToolChoice(choice: AnthropicRequest["tool_choice"]): KimiToolChoice {
- if (!choice) return "auto";
- switch (choice.type) {
- case "auto":
- return "auto";
- case "none":
- return "none";
- case "any":
- return "required";
- case "tool":
- return choice.name ? { type: "function", function: { name: choice.name } } : "required";
- }
+ const mapped = mapAnthropicToolChoice(choice);
+ if (mapped === "auto" || mapped === "none" || mapped === "required") return mapped;
+ return { type: "function", function: { name: mapped.name } };
}
export const buildSystemMessage = flattenSystemText;
diff --git a/src/providers/translate/anthropic-content.ts b/src/providers/translate/anthropic-content.ts
index 95c84e6..3f31d7c 100644
--- a/src/providers/translate/anthropic-content.ts
+++ b/src/providers/translate/anthropic-content.ts
@@ -17,6 +17,24 @@ export function assertValidEffort(effort: unknown): void {
}
}
+export type AnthropicToolChoice = "auto" | "none" | "required" | { type: "function"; name: string };
+
+export function mapToolChoice(
+ choice?: AnthropicRequest["tool_choice"],
+): AnthropicToolChoice {
+ if (!choice) return "auto";
+ switch (choice.type) {
+ case "auto":
+ return "auto";
+ case "none":
+ return "none";
+ case "any":
+ return "required";
+ case "tool":
+ return choice.name ? { type: "function", name: choice.name } : "required";
+ }
+}
+
export function flattenSystemText(system: AnthropicRequest["system"]): string | undefined {
if (!system) return undefined;
const blocks: AnthropicTextBlock[] =