@reaatech/structured-function-calling-ai-adapter-anthropic
Normalize tool calls to and from Anthropic’s tool-use format.
Installation
pnpm add @reaatech/structured-function-calling-ai-adapter-anthropicAPI
toToolCall
function toToolCall(tu: AnthropicToolUseBlock): ToolCall;Converts an Anthropic ToolUseBlock into a provider-agnostic ToolCall. Treats null input as an empty object {}.
AnthropicToolUseBlock is { type: 'tool_use'; id: string; name: string; input: Record<string, unknown> | null }.
fromToolCallResult
function fromToolCallResult(tc: ToolCall, result: unknown): AnthropicToolResultBlock;Converts a ToolCall and execution result into Anthropic’s ToolResultBlock shape. Serializes result with JSON.stringify.
AnthropicToolResultBlock is { type: 'tool_result'; tool_use_id: string; content: string }.
toAnthropicTools
function toAnthropicTools(defs: readonly ToolDefinition[]): AnthropicTool[];Converts an array of ToolDefinitions to Anthropic tool format. Uses convertToJsonSchema (target defaults to jsonSchema7) for schema conversion.
AnthropicTool is { name: string; description: string; input_schema: Record<string, unknown> }.
Types
type ToolCall = { id: string; name: string; arguments: Record<string, unknown> };
type AnthropicToolUseBlock = { type: 'tool_use'; id: string; name: string; input: Record<string, unknown> | null };
type AnthropicToolResultBlock = { type: 'tool_result'; tool_use_id: string; content: string };
type AnthropicTool = { name: string; description: string; input_schema: Record<string, unknown> };Usage example
import { toToolCall, fromToolCallResult, toAnthropicTools } from '@reaatech/structured-function-calling-ai-adapter-anthropic';
// Convert from Anthropic format
const toolCall = toToolCall({
type: 'tool_use',
id: 'msg_xyz',
name: 'get_weather',
input: { city: 'NYC', units: 'metric' },
});
// → { id: 'msg_xyz', name: 'get_weather', arguments: { city: 'NYC', units: 'metric' } }
// Convert tool definitions to Anthropic tools
const anthropicTools = toAnthropicTools(registry.list());
// Convert result back to Anthropic format
const result = fromToolCallResult(toolCall, { temp: 22, conditions: 'sunny' });
// → { type: 'tool_result', tool_use_id: 'msg_xyz', content: '{"temp":22,"conditions":"sunny"}' }Dependencies
@reaatech/structured-function-calling-ai-core(workspace:*)
