UNPKG

@hashbrownai/angular

Version:
585 lines (569 loc) 20.7 kB
import * as _angular_core from '@angular/core'; import { Signal, Type, ApplicationRef, ViewContainerRef, TemplateRef, EmbeddedViewRef, Resource, Provider } from '@angular/core'; import { s, RuntimeFunctionRef, RuntimeRef, Chat, ɵtypes as _types, KnownModelIds, ɵcomponents as _components } from '@hashbrownai/core'; import * as dist_packages_core_src_models_view_models from 'dist/packages/core/src/models/view.models'; /** * Creates a function with an input schema. * * @param cfg - The configuration for the function. * @param cfg.name - The name of the function. * @param cfg.description - The description of the function. * @param cfg.args - The input schema of the function. * @param cfg.result - The result schema of the function. * @param cfg.handler - The handler of the function. * @returns The function reference. */ declare function createRuntimeFunction<ArgsSchema extends s.HashbrownType, ResultSchema extends s.HashbrownType>(cfg: { name: string; description: string; args: ArgsSchema; result: ResultSchema; handler: (input: s.Infer<ArgsSchema>, abortSignal?: AbortSignal) => s.Infer<ResultSchema> | Promise<s.Infer<ResultSchema>>; }): RuntimeFunctionRef<s.Infer<ArgsSchema>, s.Infer<ResultSchema>>; /** * Creates a function without an input schema. * * @param cfg - The configuration for the function. * @param cfg.name - The name of the function. * @param cfg.description - The description of the function. * @param cfg.result - The result schema of the function. * @param cfg.handler - The handler of the function. * @returns The function reference. */ declare function createRuntimeFunction<ResultSchema extends s.HashbrownType>(cfg: { name: string; description: string; result: ResultSchema; handler: (abortSignal?: AbortSignal) => s.Infer<ResultSchema> | Promise<s.Infer<ResultSchema>>; }): RuntimeFunctionRef<null, s.Infer<ResultSchema>>; /** * Creates a function with an input schema. * * @param cfg - The configuration for the function. * @param cfg.name - The name of the function. * @param cfg.description - The description of the function. * @param cfg.args - The args schema of the function. * @param cfg.handler - The handler of the function. * @returns The function reference. */ declare function createRuntimeFunction<ArgsSchema extends s.HashbrownType>(cfg: { name: string; description: string; args: ArgsSchema; handler: (args: s.Infer<ArgsSchema>, abortSignal?: AbortSignal) => void | Promise<void>; }): RuntimeFunctionRef<s.Infer<ArgsSchema>, void>; /** * Creates a function without input or output schema. * * @param cfg - The configuration for the function. * @param cfg.name - The name of the function. * @param cfg.description - The description of the function. * @param cfg.handler - The handler of the function, which returns void or a promise thereof. * @returns The function reference. */ declare function createRuntimeFunction(cfg: { name: string; description: string; handler: (abortSignal?: AbortSignal) => void | Promise<void>; }): RuntimeFunctionRef<null, void>; /** * Options for creating a runtime. */ interface CreateRuntimeOptions { /** * The timeout for the runtime. * * @default 10000 */ timeout?: number; /** * The functions that are available in the runtime. */ functions: [...RuntimeFunctionRef<any, any>[]]; } /** * Creates a new runtime. * * @param options - The options for creating the runtime. * @returns A reference to the runtime. */ declare function createRuntime(options: CreateRuntimeOptions): RuntimeRef; /** * Options for creating a tool that can run JavaScript code. */ interface CreateToolJavaScriptOptions { /** * The runtime to use for the JavaScript code, created with `defineAsyncRuntime`. */ runtime: RuntimeRef; } /** * Creates a tool that allows the LLM to run JavaScript code. It is run * in a stateful JavaScript environment, with no access to the internet, the DOM, * or any function that you have not explicitly defined. * * @param options - The options for creating the tool. * @returns The tool. */ declare function createToolJavaScript({ runtime }: CreateToolJavaScriptOptions): dist_packages_core_src_models_view_models.Tool<"javascript", { code: string; }, any>; /** * Creates a tool with a schema. * * @param input - The input for the tool. * @param input.name - The name of the tool. * @param input.description - The description of the tool. * @param input.schema - The schema of the tool. * @param input.handler - The handler of the tool. * @param Name - The name of the tool. * @param Schema - The schema of the tool. * @param Result - The result of the tool. * @returns The tool. */ declare function createTool<const Name extends string, Schema extends s.HashbrownType, Result>(input: { name: Name; description: string; schema: Schema; handler: (input: s.Infer<Schema>, abortSignal: AbortSignal) => Promise<Result>; }): Chat.Tool<Name, s.Infer<Schema>, Result>; /** * Creates a tool without a schema. * * @param input - The input for the tool. * @param input.name - The name of the tool. * @param input.description - The description of the tool. * @param input.handler - The handler of the tool. * @param Name - The name of the tool. * @param Result - The result of the tool. * @returns The tool. */ declare function createTool<const Name extends string, Result>(input: { name: Name; description: string; handler: (abortSignal: AbortSignal) => Promise<Result>; }): Chat.Tool<Name, void, Result>; type AngularSignalLike<T> = () => T; type ComponentPropSchema<T> = _types.Prettify<T extends { new (...args: any[]): infer P; } ? { [K in keyof P]?: P[K] extends AngularSignalLike<infer U> ? s.Schema<U> : s.Schema<P[K]>; } : never>; interface ExposedComponent<T extends { new (...args: any[]): any; }> { component: T; name: string; description: string; children?: 'any' | ExposedComponent<any>[] | false; props?: ComponentPropSchema<T>; } /** * Exposes a component by combining it with additional configuration details. * * @template T - The type of the Angular component. * @param {T} component - The Angular component to be exposed. * @param {Prettify<Omit<ExposedComponent<T>, 'component' | 'name'>>} config - The configuration object for the component, excluding the component itself. * @returns {ExposedComponent<T>} - An object representing the exposed component, including the component and its configuration. */ declare function exposeComponent<T extends { new (...args: any[]): any; }>(component: T, config: _types.Prettify<Omit<ExposedComponent<T>, 'component' | 'name' | 'props'> & { input?: ComponentPropSchema<T>; }>): ExposedComponent<T>; /** * A type that represents a signal or a value. * * @template T - The type of the signal or value. */ type SignalLike<T> = T | Signal<T>; declare const TAG_NAME_REGISTRY: unique symbol; type TagNameRegistry = { [tagName: string]: { props: Record<string, s.HashbrownType>; component: Type<object>; }; }; interface UiChatSchemaComponent { $tagName: string; $props: Record<string, any>; $children: UiChatSchemaComponent[]; } interface UiChatSchema { ui: UiChatSchemaComponent[]; } type UiAssistantMessage<Tools extends Chat.AnyTool = Chat.AnyTool> = Chat.AssistantMessage<UiChatSchema, Tools> & { [TAG_NAME_REGISTRY]: TagNameRegistry; }; type UiUserMessage = Chat.UserMessage; type UiErrorMessage = Chat.ErrorMessage; type UiChatMessage<Tools extends Chat.AnyTool = Chat.AnyTool> = UiAssistantMessage<Tools> | UiUserMessage | UiErrorMessage; declare class RenderMessageComponent { message: _angular_core.InputSignal<UiAssistantMessage<any>>; /** * @internal */ appRef: ApplicationRef; /** * @internal */ content: _angular_core.Signal<UiChatSchemaComponent[]>; /** * @internal */ tagNameRegistry: _angular_core.Signal<TagNameRegistry | undefined>; /** * @internal */ viewContainerRef: ViewContainerRef; /** * @internal */ rootNodesWeakMap: WeakMap<TemplateRef<any>, any[]>; /** * @internal */ embeddedViewsWeakMap: WeakMap<TemplateRef<any>, EmbeddedViewRef<any>>; /** * @internal */ getTagComponent(tagName: string): _angular_core.Type<object> | null; /** * @internal */ getEmbeddedView(tpl: TemplateRef<any>): EmbeddedViewRef<any>; /** * @internal */ getRootNodes(tpl: TemplateRef<any>): any[]; static ɵfac: _angular_core.ɵɵFactoryDeclaration<RenderMessageComponent, never>; static ɵcmp: _angular_core.ɵɵComponentDeclaration<RenderMessageComponent, "hb-render-message", never, { "message": { "alias": "message"; "required": true; "isSignal": true; }; }, {}, never, never, true, never>; } /** * Represents the reactive chat resource, including current messages and control methods. * * @interface ChatResourceRef * @template Tools * @extends {Resource<Chat.Message<string, Tools>[]>} * @property {(message: Chat.UserMessage) => void} sendMessage - Send a new user message to the chat. * @property {() => boolean} reload - Remove the last assistant response and re-send the previous user message. Returns true if a reload was performed. */ interface ChatResourceRef<Tools extends Chat.AnyTool> extends Resource<Chat.Message<string, Tools>[]> { sendMessage: (message: Chat.UserMessage) => void; /** * Stops any currently-streaming message. * @param clearStreamingMessage Whether the currently-streaming message should be removed from state. */ stop: (clearStreamingMessage?: boolean) => void; reload: () => boolean; lastAssistantMessage: Signal<Chat.AssistantMessage<string, Tools> | undefined>; } /** * Configuration options for the chat resource. * * @interface ChatResourceOptions * @template Tools * @property {string | Signal<string>} system - The system (assistant) prompt. * @property {string | Signal<string>} model - The model identifier to use for the chat. * @property {Tools[]} [tools] - Optional array of bound tools available to the chat. * @property {Chat.Message<string, Tools>[] | Signal<Chat.Message<string, Tools>[]>} [messages] - Optional initial list of chat messages. * @property {number} [debounce] - Optional debounce interval in milliseconds between user inputs. * @property {string} [debugName] - Optional name used for debugging in logs. * @property {string} [apiUrl] - Optional override for the API base URL. */ interface ChatResourceOptions<Tools extends Chat.AnyTool> { /** * The system prompt to use for the chat. */ system: string | Signal<string>; /** * The model to use for the chat. */ model: KnownModelIds | Signal<KnownModelIds>; /** * The tools to use for the chat. */ tools?: Tools[]; /** * The initial messages for the chat. */ messages?: Chat.Message<string, Tools>[] | Signal<Chat.Message<string, Tools>[]>; /** * The debounce time for the chat. */ debounce?: number; /** * The debug name for the chat. */ debugName?: string; /** * The API URL to use for the chat. */ apiUrl?: string; } /** * Creates a chat resource for managing LLM-driven conversations. * * @template Tools * @param {ChatResourceOptions<Tools>} options - Configuration for the chat resource. * @returns {ChatResourceRef<Tools>} An object with reactive signals and methods for interacting with the chat. */ declare function chatResource<Tools extends Chat.AnyTool>(options: ChatResourceOptions<Tools>): ChatResourceRef<Tools>; /** * A reference to the completion resource. */ interface CompletionResourceRef extends Resource<string | null> { /** * Reloads the resource. * * @returns Whether the resource was reloaded. */ reload: () => boolean; /** * Stops any currently-streaming message. * @param clearStreamingMessage Whether the currently-streaming message should be removed from state. */ stop: (clearStreamingMessage?: boolean) => void; } /** * Options for the completion resource. */ interface CompletionResourceOptions<Input> { /** * The model to use for the completion. */ model: SignalLike<string>; /** * The input to the completion. */ input: Signal<Input | null | undefined>; /** * The system prompt to use for the completion. */ system: SignalLike<string>; /** * The API URL to use for the completion. */ apiUrl?: string; /** * The debug name for the completion resource. */ debugName?: string; } /** * Creates a completion resource. * * @param options - The options for the completion resource. * @returns The completion resource. */ declare function completionResource<Input>(options: CompletionResourceOptions<Input>): CompletionResourceRef; /** * A reference to the structured chat resource. */ interface StructuredChatResourceRef<Output, Tools extends Chat.AnyTool> extends Resource<Chat.Message<Output, Tools>[]> { sendMessage: (message: Chat.UserMessage) => void; resendMessages: () => void; setMessages: (messages: Chat.Message<Output, Tools>[]) => void; reload: () => boolean; /** * Stops any currently-streaming message. * @param clearStreamingMessage Whether the currently-streaming message should be removed from state. */ stop: (clearStreamingMessage?: boolean) => void; lastAssistantMessage: Signal<Chat.AssistantMessage<Output, Tools> | undefined>; } /** * Options for the structured chat resource. */ interface StructuredChatResourceOptions<Schema extends s.HashbrownType, Tools extends Chat.AnyTool, Output extends s.Infer<Schema> = s.Infer<Schema>> { /** * The model to use for the structured chat resource. */ model: KnownModelIds | Signal<KnownModelIds>; /** * The system prompt to use for the structured chat resource. */ system: string | Signal<string>; /** * The schema to use for the structured chat resource. */ schema: Schema; /** * The tools to use for the structured chat resource. */ tools?: Tools[]; /** * The initial messages for the structured chat resource. */ messages?: Chat.Message<Output, Tools>[]; /** * The debug name for the structured chat resource. */ debugName?: string; /** * The debounce time for the structured chat resource. */ debounce?: number; /** * The number of retries for the structured chat resource. */ retries?: number; /** * The API URL to use for the structured chat resource. */ apiUrl?: string; } /** * Creates a structured chat resource. * * @param options - The options for the structured chat resource. * @returns The structured chat resource. */ declare function structuredChatResource<Schema extends s.HashbrownType, Tools extends Chat.AnyTool, Output extends s.Infer<Schema> = s.Infer<Schema>>(options: StructuredChatResourceOptions<Schema, Tools, Output>): StructuredChatResourceRef<Output, Tools>; /** * A reference to the structured completion resource. */ interface StructuredCompletionResourceRef<Output> extends Resource<Output | null> { reload: () => boolean; /** * Stops any currently-streaming message. * @param clearStreamingMessage Whether the currently-streaming message should be removed from state. */ stop: (clearStreamingMessage?: boolean) => void; } /** * Options for the structured completion resource. */ interface StructuredCompletionResourceOptions<Input, Schema extends s.HashbrownType> { /** * The model to use for the structured completion resource. */ model: KnownModelIds; /** * The input to the structured completion resource. */ input: Signal<Input | null | undefined>; /** * The schema to use for the structured completion resource. */ schema: Schema; /** * The system prompt to use for the structured completion resource. */ system: SignalLike<string>; /** * The tools to use for the structured completion resource. */ tools?: Chat.AnyTool[]; /** * The debug name for the structured completion resource. */ debugName?: string; /** * The API URL to use for the structured completion resource. */ apiUrl?: string; } /** * Creates a structured completion resource. * * @param options - The options for the structured completion resource. * @returns The structured completion resource. */ declare function structuredCompletionResource<Input, Schema extends s.HashbrownType, Output extends s.Infer<Schema> = s.Infer<Schema>>(options: StructuredCompletionResourceOptions<Input, Schema>): StructuredCompletionResourceRef<Output>; type UiChatMessageOutput = s.ObjectType<{ ui: s.ArrayType<s.ObjectType<_components.ComponentTreeSchema>>; }>; /** * Options for the UI chat resource. */ interface UiChatResourceOptions<Tools extends Chat.AnyTool> { /** * The components to use for the UI chat resource. */ components: ExposedComponent<any>[]; /** * The model to use for the UI chat resource. */ model: KnownModelIds; /** * The system prompt to use for the UI chat resource. */ system: string | Signal<string>; /** * The initial messages for the UI chat resource. */ messages?: Chat.Message<s.Infer<UiChatMessageOutput>, Tools>[]; /** * The tools to use for the UI chat resource. */ tools?: Tools[]; /** * The debug name for the UI chat resource. */ debugName?: string; /** * The debounce time for the UI chat resource. */ debounce?: number; /** * The API URL to use for the UI chat resource. */ apiUrl?: string; } /** * A reference to the UI chat resource. */ interface UiChatResourceRef<Tools extends Chat.AnyTool> extends Resource<UiChatMessage<Tools>[]> { sendMessage: (message: Chat.UserMessage) => void; resendMessages: () => void; /** * Stops any currently-streaming message. * @param clearStreamingMessage Whether the currently-streaming message should be removed from state. */ stop: (clearStreamingMessage?: boolean) => void; /** * The last assistant message for the UI chat resource. */ lastAssistantMessage: Signal<Chat.AssistantMessage<s.Infer<UiChatMessageOutput>, Tools> | undefined>; } /** * Creates a UI chat resource. * * @param args - The arguments for the UI chat resource. * @returns The UI chat resource. */ declare function uiChatResource<Tools extends Chat.AnyTool>(args: UiChatResourceOptions<Tools>): UiChatResourceRef<Tools>; /** * Hashbrown must be configured with a base URL, * and may optionally include middleware and a flag * to emulate structured output. */ interface ProvideHashbrownOptions { /** * The base URL of the Hashbrown API. */ baseUrl: string; /** * Middleware to apply to all requests. */ middleware?: Chat.Middleware[]; /** * Whether to emulate structured output. Useful for models * that don't support tool calling with structured outputs * enabled. When set to true, Hashbrown silently adds an * "output" tool to the the list of tools the model can * call, and then handles the arguments to the tool call * as if the model has produced it via structured outputs. */ emulateStructuredOutput?: boolean; } /** * Provides the Hashbrown configuration. * * @argument options - The Hashbrown configuration. * @returns The Hashbrown configuration. */ declare function provideHashbrown(options: ProvideHashbrownOptions): Provider; export { RenderMessageComponent, chatResource, completionResource, createRuntime, createRuntimeFunction, createTool, createToolJavaScript, exposeComponent, provideHashbrown, structuredChatResource, structuredCompletionResource, uiChatResource }; export type { ChatResourceOptions, ChatResourceRef, CompletionResourceOptions, CompletionResourceRef, ComponentPropSchema, CreateRuntimeOptions, CreateToolJavaScriptOptions, ExposedComponent, ProvideHashbrownOptions, StructuredChatResourceOptions, StructuredChatResourceRef, StructuredCompletionResourceOptions, StructuredCompletionResourceRef, UiAssistantMessage, UiChatMessage, UiChatResourceOptions, UiChatResourceRef, UiChatSchema, UiChatSchemaComponent, UiErrorMessage, UiUserMessage };