UNPKG

jorel

Version:

A unified wrapper for working with LLMs from multiple providers, including streams, images, documents & automatic tool use.

114 lines (113 loc) 3.84 kB
import { LlmFunction, LlmToolCall } from "../providers"; import { MaybeUndefined, Nullable } from "../shared"; import { LlmTool, LlmToolConfiguration, LLmToolContextSegment } from "./llm-tool"; import { LlmToolKitUtilities } from "./utilities"; /** * A toolkit for managing one or more LLM tools. */ export declare class LlmToolKit { readonly tools: LlmTool[]; allowParallelCalls: boolean; static readonly utilities: LlmToolKitUtilities; /** * Allow access from the instance as well */ get utilities(): LlmToolKitUtilities; constructor(tools: (LlmTool | LlmToolConfiguration)[], config?: { allowParallelCalls?: boolean; }); /** * Whether the toolkit has any tools */ get hasTools(): boolean; /** * Get all tools as LlmFunction objects */ get asLlmFunctions(): MaybeUndefined<LlmFunction[]>; /** * Deserialize strings * @param input */ static deserialize(input: string, strict?: boolean): object; /** * Serialize objects * @param input */ static serialize(input: object): string; /** * Create a new toolkit with only selected (allowed) tools * @param allowedToolIds */ withAllowedToolsOnly(allowedToolIds: string[]): LlmToolKit; /** * Get the next tool call that requires processing * @param input */ getNextToolCall(input: LlmToolCall[]): Nullable<{ toolCall: LlmToolCall; tool: LlmTool; }>; /** * Register one or more tools * @param tools */ registerTools(tools: (LlmTool | LlmToolConfiguration)[]): void; /** * Register a new tool * @param tool Tool or tool configuration */ registerTool(tool: LlmTool | LlmToolConfiguration): void; /** * Unregister a tool * @param id Tool name */ unregisterTool(id: string): void; /** * Get a tool by name * @param id Tool name */ getTool(id: string): Nullable<LlmTool>; /** * Classify what type of tool calls are present. This implementation goes beyond the basic classification inside * the toolkit utilities to provide more detailed information, such as pending transfers and missing executors * which require access to the tool instances * @param toolCalls */ classifyToolCalls(toolCalls: LlmToolCall[]): "approvalPending" | "transferPending" | "executionPending" | "completed" | "missingExecutor"; /** * Process a single tool call and return the updated tool call * @param toolCall * @param config * @returns Object containing the updated tool call and a boolean indicating whether the tool call was handled * If the tool call was not handled, it requires additional processing (e.g. approval or delegation) */ processToolCall(toolCall: LlmToolCall, config?: { retryFailed?: boolean; context?: LLmToolContextSegment; secureContext?: LLmToolContextSegment; }): Promise<{ toolCall: LlmToolCall; handled: boolean; }>; /** * Process tool calls * * This method will execute the tools and return the results. * All tool calls must be approved or rejected before processing. * * @param input Object containing tool calls (e.g. llm "assistant_with_tools" message) * @param config * @returns Object containing tool calls with results * @throws Error if a tool is not found or if any tool calls still require approval */ processCalls<T extends { toolCalls?: LlmToolCall[]; }>(input: T, config?: { retryFailed?: boolean; context?: LLmToolContextSegment; secureContext?: LLmToolContextSegment; maxErrors?: number; maxCalls?: number; abortSignal?: AbortSignal; }): Promise<T>; }