@reaatech/structured-function-calling-ai-adapter-openai
Normalize tool calls to and from OpenAI’s function-calling format.
Installation
pnpm add @reaatech/structured-function-calling-ai-adapter-openaiAPI
toToolCall
function toToolCall(fc: OpenAiFunctionCall): ToolCall;Parses an OpenAI ChatCompletionMessageFunctionCall-shaped object into a provider-agnostic ToolCall. Parses fc.arguments as JSON; throws if the string is not valid JSON.
OpenAiFunctionCall is { id?: string | null; name?: string | null; arguments?: string | null } — all fields are nullable to match OpenAI API variations.
fromToolCallResult
function fromToolCallResult(tc: ToolCall, result: unknown): OpenAiToolResult;Converts a ToolCall and execution result into OpenAI’s ChatCompletionRequestMessageToolResult shape. Serializes result with JSON.stringify.
OpenAiToolResult is { tool_call_id: string; output: string }.
toOpenAiTools
function toOpenAiTools(defs: readonly ToolDefinition[]): OpenAiTool[];Converts an array of ToolDefinitions to OpenAI ChatCompletionTool objects. Uses convertToJsonSchema with target: 'openAi' for schema conversion.
OpenAiTool is { type: 'function'; function: { name: string; description: string; parameters: Record<string, unknown> } }.
Types
type ToolCall = { id: string; name: string; arguments: Record<string, unknown> };
type OpenAiFunctionCall = { id?: string | null; name?: string | null; arguments?: string | null };
type OpenAiToolResult = { tool_call_id: string; output: string };
type OpenAiTool = { type: 'function'; function: { name: string; description: string; parameters: Record<string, unknown> } };Usage example
import { toToolCall, fromToolCallResult, toOpenAiTools } from '@reaatech/structured-function-calling-ai-adapter-openai';
import { ToolRegistry } from '@reaatech/structured-function-calling-ai-core';
// Convert from OpenAI format
const toolCall = toToolCall({
id: 'call_abc',
name: 'get_weather',
arguments: '{"city":"NYC","units":"metric"}',
});
// → { id: 'call_abc', name: 'get_weather', arguments: { city: 'NYC', units: 'metric' } }
// Convert tool definitions to OpenAI tools
const registry = new ToolRegistry();
registry.register({ name: 'get_weather', description: 'Get weather', schema, execute: async () => {} });
const openAiTools = toOpenAiTools(registry.list());
// → [{ type: 'function', function: { name: 'get_weather', ... } }]
// Convert result back to OpenAI format
const result = fromToolCallResult(toolCall, { temp: 22, conditions: 'sunny' });
// → { tool_call_id: 'call_abc', output: '{"temp":22,"conditions":"sunny"}' }Dependencies
@reaatech/structured-function-calling-ai-core(workspace:*)
