UNPKG

@hashbrownai/react

Version:

React components for Hashbrown AI

141 lines (140 loc) 5.13 kB
import { Chat, s } from '@hashbrownai/core'; import { type DependencyList } from 'react'; /** * @public */ export interface ToolOptionsWithInput<Name extends string, Schema extends s.HashbrownType, Result> { /** * The name of the tool. */ name: Name; /** * The description of the tool. This helps the LLM understand its purpose. */ description: string; /** * The schema that describes the input for the tool. */ schema: Schema; /** * The handler of the tool. This is what the LLM agent will call * to execute the tool, passing in an input that adheres to the schema. */ handler: (input: s.Infer<Schema>, abortSignal: AbortSignal) => Promise<Result>; /** * Dependencies that should trigger tool recreation. * The hook will automatically memoize the handler based on these dependencies, * so you can safely pass anonymous functions. */ deps: DependencyList; } /** * @public */ export interface ToolOptionsWithUnknownSchema<Name extends string, Result> { /** * The name of the tool. */ name: Name; /** * The description of the tool. This helps the LLM understand its purpose. */ description: string; /** * The unknown schema that describes the input for the tool. */ schema: object; /** * The handler of the tool. This is what the LLM agent will call * to execute the tool, passing in an input that adheres to the schema. */ handler: (input: any, abortSignal: AbortSignal) => Promise<Result>; /** * Dependencies that should trigger tool recreation. * The hook will automatically memoize the handler based on these dependencies, * so you can safely pass anonymous functions. */ deps: DependencyList; } /** * @public */ export interface ToolOptionsWithoutInput<Name extends string, Result> { /** * The name of the tool. */ name: Name; /** * The description of the tool. This helps the LLM understand its purpose. */ description: string; /** * The handler of the tool. This is what the LLM agent will call * to execute the tool. */ handler: (abortSignal: AbortSignal) => Promise<Result>; /** * Dependencies that should trigger tool recreation. * The hook will automatically memoize the handler based on these dependencies, * so you can safely pass anonymous functions. */ deps: DependencyList; } /** * @public */ export type ToolOptions<Name extends string, Schema extends s.HashbrownType = s.HashbrownType, Result = unknown> = ToolOptionsWithInput<Name, Schema, Result> | ToolOptionsWithUnknownSchema<Name, Result> | ToolOptionsWithoutInput<Name, Result>; /** * Creates a tool with a schema. * * @public * @typeParam Name - The name of the tool. * @typeParam Schema - The schema of the tool. * @typeParam Result - The result of the tool. * @param input - The input for the tool containing: * - `name`: The name of the tool * - `description`: The description of the tool * - `schema`: The schema of the tool * - `handler`: The handler of the tool * @param deps - Dependencies that should trigger tool recreation. * The hook will automatically memoize the handler based on these dependencies, * so you can safely pass anonymous functions. * @param Name - The name of the tool. * @param Schema - The schema of the tool. * @param Result - The result of the tool. * @returns The tool. */ export declare function useTool<const Name extends string, Schema extends s.HashbrownType, Result>(input: ToolOptionsWithInput<Name, Schema, Result>): Chat.Tool<Name, s.Infer<Schema>, Result>; /** * Creates a tool with a unknown JSON schema. * * @public * @typeParam Name - The name of the tool. * @typeParam Result - The result of the tool. * @param input - The input for the tool containing: * - `name`: The name of the tool * - `description`: The description of the tool * - `schema`: The schema of the tool * - `handler`: The handler of the tool */ export declare function useTool<const Name extends string, Result>(input: ToolOptionsWithUnknownSchema<Name, Result>): Chat.Tool<Name, any, Result>; /** * Creates a tool. * * @public * @typeParam Name - The name of the tool. * @typeParam Result - The result of the tool. * @param input - The input for the tool containing: * - `name`: The name of the tool * - `description`: The description of the tool * - `schema`: The schema of the tool * - `handler`: The handler of the tool * @param deps - Dependencies that should trigger tool recreation. * The hook will automatically memoize the handler based on these dependencies, * so you can safely pass anonymous functions. * @param Name - The name of the tool. * @param Schema - The schema of the tool. * @param Result - The result of the tool. * @returns The tool. */ export declare function useTool<const Name extends string, Result>(input: ToolOptionsWithoutInput<Name, Result>): Chat.Tool<Name, void, Result>;