import { ChatCompletionTool, ChatCompletionMessageToolCall as ChatCompletionMessageToolCall$1, ChatCompletion } from 'openai/resources'; import { z } from 'zod'; import { JsonSchema7Type } from 'zod-to-json-schema'; import { ChatCompletionMessageToolCall } from 'openai/resources/chat/completions'; /** * Define an OpenAI Function call using zod instead of JSON schema. */ type ZodFunctionDef = { /** Function name. */ name: string; /** A description of what the function does. */ description: string; /** Zod schema defining the function's parameters, to convert to JSON schema. */ schema: z.ZodType; }; /** * OpenAI expects function definitions to use this JSON Schema format. */ type JsonSchemaFunctionDef = { name: string; description?: string; parameters: { type: "object"; properties: Record; required: string[] | undefined; }; }; /** * Convert a ZodFunctionDef to JSON Schema. */ declare function toJsonSchema(functionDef: ZodFunctionDef): JsonSchemaFunctionDef; /** * Convert a ZodFunctionDef to the newer OpenAI tool format. */ declare function toTool(functionDef: ZodFunctionDef): ChatCompletionTool; /** * Extend Error to include an HTTP status code and optional source info. */ declare class HttpError extends Error { /** * HTTP status code. */ status: number; /** * Optional string indicating an external source of the error, such as "OpenAI". */ source?: string; constructor(message: string, status?: number, source?: string); } /** * A function handler threw an error. */ declare class FunctionHandlerError extends HttpError { constructor(name: string, err: unknown); } /** * A ZodFunctionHandler combines a Zod function definition with a function call handler. * * The handler takes the parsed/typed function parameters as arguments, * and can perform any (async) computation and/or return an arbitrary output. */ type ZodFunctionHandler = ZodFunctionDef & { handler: (parameters: Parameters) => Promise; }; /** * Create a ZodFunctionHandler. * * You can also create one directly, but this convenience function * infers type parameters automatically from the schema/handler types. */ declare function createFunctionHandler, Output>({ name, description, schema, handler, }: { name: string; description: string; schema: Schema; handler: (parameters: z.infer) => Promise; }): ZodFunctionHandler, Output>; /** * Handle zero or more tool calls, using zero or more ZodFunctionHandlers. * * Returns the array of corresponding tool output if successful. * Throws an error if any tool name isn't found, any arguments are invalid, or any handler throws. */ declare function handleToolCalls(handlers: ZodFunctionHandler[], toolCalls: ChatCompletionMessageToolCall[] | undefined): Promise; type FunctionReducer = (prevState: State, functionName: string, args: Parameters) => State; /** * A reducer function threw an error. */ declare class FunctionReducerError extends HttpError { constructor(name: string, err: unknown); } /** * A ZodFunctionReducer is a more advanced alternative to ZodFunctionHandlers. * * It can be useful when: * - You are using function calls to modify an object's state (such as the state of a chart), not in a chat messages context * - You have many functions and want to organize state updates in one place * * This pattern is inspired by "reducers" in Elm/Redux/React, replacing "actions" with LLM function calls. * For example, see: https://react.dev/reference/react/useReducer */ type ZodFunctionReducer = { functions: ZodFunctionDef[]; reducer: FunctionReducer; }; /** * Handle tool calls by updating state using a reducer function. * * Returns the new state if successful. * Throws an error if a tool name isn't found, arguments are invalid, or a reducer throws. */ declare function reduceToolCalls(prevState: State, functionReducer: ZodFunctionReducer, toolCalls: ChatCompletionMessageToolCall$1[] | undefined): Promise; /** * Assert that a ChatCompletion response contains a single tool_call. * Throw an error if it doesn't. */ declare function assertSingleToolCall(completion: ChatCompletion, label?: string): void; /** * Parse a function call's arguments string using a Zod schema. * * Returns the parsed/typed output if valid. * Otherwise throws an InvalidFunctionArguments error. */ declare function parseArguments(name: string, args: string, schema: z.ZodType): Parameters; export { FunctionHandlerError, FunctionReducerError, type ZodFunctionDef, type ZodFunctionHandler, type ZodFunctionReducer, assertSingleToolCall, createFunctionHandler, handleToolCalls, parseArguments, reduceToolCalls, toJsonSchema, toTool };