@hashbrownai/angular
Version:
Angular bindings for Hashbrown AI
1,213 lines (1,194 loc) • 41.8 kB
TypeScript
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, MagicTextFragmentText, MagicTextFragmentCitation, MagicTextTag, MagicTextHasWhitespace, MagicTextFragment, ModelInput, TransportOrFactory, SystemPrompt, ɵui as _ui } from '@hashbrownai/core';
/**
* Creates a function with an input schema.
*
* @public
* @param cfg - The configuration for the function containing:
* - `name`: The name of the function
* - `description`: The description of the function
* - `args`: The input schema of the function
* - `result`: The result schema of the function
* - `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.
*
* @public
* @param cfg - The configuration for the function containing:
* - `name`: The name of the function
* - `description`: The description of the function
* - `result`: The result schema of the function
* - `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.
*
* @public
* @param cfg - The configuration for the function containing:
* - `name`: The name of the function
* - `description`: The description of the function
* - `args`: The args schema of the function
* - `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.
*
* @public
* @param cfg - The configuration for the function containing:
* - `name`: The name of the function
* - `description`: The description of the function
* - `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.
*
* @public
*/
interface CreateRuntimeOptions {
/**
* The timeout for the runtime.
*
* @defaultValue 10000
*/
timeout?: number;
/**
* The functions that are available in the runtime.
*/
functions: [...RuntimeFunctionRef<any, any>[]];
}
/**
* Creates a new runtime.
*
* @public
* @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.
*
* @public
*/
interface CreateToolJavaScriptOptions {
/**
* The runtime to use for the JavaScript code, created with `defineAsyncRuntime`.
*/
runtime: RuntimeRef;
}
/**
* The schema for the javascript tool.
*/
declare const schema: s.ObjectType<{
code: s.StringType;
}>;
/**
* 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.
*
* @public
* @param options - The options for creating the tool.
* @returns The tool.
*/
declare function createToolJavaScript<Result>({ runtime, }: CreateToolJavaScriptOptions): Chat.Tool<'javascript', s.Infer<typeof schema>, Result>;
/**
* Creates a tool with a schema.
*
* @public
* @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 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 with a unknown JSON schema.
*
* @public
* @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 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, Result>(input: {
name: Name;
description: string;
schema: object;
handler: (input: any, abortSignal: AbortSignal) => Promise<Result>;
}): Chat.Tool<Name, any, Result>;
/**
* Creates a tool without a schema.
*
* @public
* @param input - The input for the tool containing:
* - `name`: The name of the tool
* - `description`: The description of the tool
* - `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 Prettify<T> = {
[K in keyof T]: T[K];
} & {};
/**
* @public
*/
type AngularSignalLike<T> = () => T;
/**
* @public
*/
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>;
/**
* @public
*/
interface ExposedComponent<T extends {
new (...args: any[]): any;
}> {
component: T;
name: string;
description: string;
children?: 'any' | 'text' | ExposedComponent<any>[] | false;
props?: ComponentPropSchema<T>;
}
/**
* Exposes a component by combining it with additional configuration details.
*
* @public
* @typeParam T - The type of the Angular component.
* @param component - The Angular component to be exposed.
* @param config - The configuration object for the component, excluding the component itself.
* @returns 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>;
name?: string;
}>): ExposedComponent<T>;
/**
* A type that represents a signal or a value.
*
* @public
* @typeParam T - The type of the signal or value.
*/
type SignalLike<T> = T | Signal<T>;
declare const TAG_NAME_REGISTRY: unique symbol;
/**
* @public
*/
type TagNameRegistry = {
[tagName: string]: {
props: Record<string, s.HashbrownType>;
component: Type<object>;
};
};
/**
* @public
*/
interface UiChatSchemaComponent {
$tag: string;
$children: string | UiChatSchemaComponent[];
$props: Record<string, any>;
}
/**
* @public
*/
interface UiChatSchema {
ui: UiChatSchemaComponent[];
}
/**
* @public
*/
type UiAssistantMessage<Tools extends Chat.AnyTool = Chat.AnyTool> = Chat.AssistantMessage<UiChatSchema, Tools> & {
[TAG_NAME_REGISTRY]: TagNameRegistry;
};
/**
* @public
*/
type UiUserMessage = Chat.UserMessage;
/**
* @public
*/
type UiErrorMessage = Chat.ErrorMessage;
/**
* @public
*/
type UiChatMessage<Tools extends Chat.AnyTool = Chat.AnyTool> = UiAssistantMessage<Tools> | UiUserMessage | UiErrorMessage;
/**
* Renders messages generated by the assistant from uiChatResource.
*
* @public
* @example
*
* ```html
* <hb-render-message [message]="message" />
* ```
*/
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[];
/**
* @internal
*/
isTextNode(node: UiChatSchemaComponent): boolean;
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>;
}
/** @public */
type MagicTextCitation = {
id: string;
url: string;
};
/** @public */
type MagicTextLinkClickEvent = {
mouseEvent: MouseEvent;
href: string;
fragment: MagicTextFragmentText;
};
/** @public */
type MagicTextCitationClickEvent = {
mouseEvent: MouseEvent;
citation: {
id: string;
url?: string;
};
fragment: MagicTextFragmentCitation;
};
/** @public */
type MagicTextWhitespacePosition = 'before' | 'after';
/** @public */
type MagicTextWhitespaceContext = {
position: MagicTextWhitespacePosition;
render: boolean;
fragment: MagicTextFragment;
};
/** @public */
type MagicTextRenderTextContext = {
text: string;
tags: MagicTextTag[];
state: MagicTextFragmentText['state'];
isStatic: boolean;
renderWhitespace: MagicTextHasWhitespace;
isCode: boolean;
fragment: MagicTextFragmentText;
};
/** @public */
type MagicTextRenderLinkContext = Prettify<MagicTextRenderTextContext & {
href: string;
title?: string;
ariaLabel?: string;
rel?: string;
target?: string;
link: NonNullable<MagicTextFragmentText['marks']['link']>;
}>;
/** @public */
interface MagicTextRenderCitationContext {
citation: {
id: string;
number: number | string;
url?: string;
};
text: string;
state: MagicTextFragmentCitation['state'];
isStatic: boolean;
renderWhitespace: MagicTextHasWhitespace;
fragment: MagicTextFragmentCitation;
}
type $Implicit<T> = {
$implicit: T;
};
declare class MagicTextRenderLink {
readonly template: TemplateRef<MagicTextRenderLinkContext>;
constructor(template: TemplateRef<MagicTextRenderLinkContext>);
static ngTemplateContextGuard(dir: MagicTextRenderLink, context: unknown): context is $Implicit<MagicTextRenderLinkContext>;
static ɵfac: _angular_core.ɵɵFactoryDeclaration<MagicTextRenderLink, never>;
static ɵdir: _angular_core.ɵɵDirectiveDeclaration<MagicTextRenderLink, "ng-template[hbMagicTextRenderLink]", never, {}, {}, never, never, true, never>;
}
declare class MagicTextRenderText {
readonly template: TemplateRef<MagicTextRenderTextContext>;
constructor(template: TemplateRef<MagicTextRenderTextContext>);
static ngTemplateContextGuard(dir: MagicTextRenderText, context: unknown): context is $Implicit<MagicTextRenderTextContext>;
static ɵfac: _angular_core.ɵɵFactoryDeclaration<MagicTextRenderText, never>;
static ɵdir: _angular_core.ɵɵDirectiveDeclaration<MagicTextRenderText, "ng-template[hbMagicTextRenderText]", never, {}, {}, never, never, true, never>;
}
/** @public */
declare class MagicTextRenderCitation {
readonly template: TemplateRef<MagicTextRenderCitationContext>;
constructor(template: TemplateRef<MagicTextRenderCitationContext>);
static ngTemplateContextGuard(dir: MagicTextRenderCitation, context: unknown): context is $Implicit<MagicTextRenderCitationContext>;
static ɵfac: _angular_core.ɵɵFactoryDeclaration<MagicTextRenderCitation, never>;
static ɵdir: _angular_core.ɵɵDirectiveDeclaration<MagicTextRenderCitation, "ng-template[hbMagicTextRenderCitation]", never, {}, {}, never, never, true, never>;
}
/** @public */
declare class MagicTextRenderWhitespace {
readonly template: TemplateRef<MagicTextWhitespaceContext>;
constructor(template: TemplateRef<MagicTextWhitespaceContext>);
static ngTemplateContextGuard(dir: MagicTextRenderWhitespace, context: unknown): context is $Implicit<MagicTextWhitespaceContext>;
static ɵfac: _angular_core.ɵɵFactoryDeclaration<MagicTextRenderWhitespace, never>;
static ɵdir: _angular_core.ɵɵDirectiveDeclaration<MagicTextRenderWhitespace, "ng-template[hbMagicTextRenderWhitespace]", never, {}, {}, never, never, true, never>;
}
/** @public */
declare class MagicText {
readonly text: _angular_core.InputSignal<string>;
readonly defaultLinkTarget: _angular_core.InputSignal<string>;
readonly defaultLinkRel: _angular_core.InputSignal<string>;
readonly citations: _angular_core.InputSignal<MagicTextCitation[] | undefined>;
readonly linkClick: _angular_core.OutputEmitterRef<MagicTextLinkClickEvent>;
readonly citationClick: _angular_core.OutputEmitterRef<MagicTextCitationClickEvent>;
readonly linkTemplate: _angular_core.Signal<MagicTextRenderLink | undefined>;
readonly textTemplate: _angular_core.Signal<MagicTextRenderText | undefined>;
readonly citationTemplate: _angular_core.Signal<MagicTextRenderCitation | undefined>;
readonly whitespaceTemplate: _angular_core.Signal<MagicTextRenderWhitespace | undefined>;
protected fragments: _angular_core.Signal<(MagicTextFragmentCitation | {
text: string;
id: string;
range: {
start: number;
end: number;
};
state: "final" | "provisional";
rev: number;
kind: "text";
marks: {
strong?: true;
em?: true;
code?: true;
link?: {
href: string;
title?: string;
ariaLabel?: string;
target?: string;
rel?: string;
unknownAttrs?: Record<string, string>;
policy: "allowed";
};
};
type: "text";
key: string;
tags: MagicTextTag[];
wrappers: MagicTextTag[];
whitespace: {
before: boolean;
after: boolean;
};
renderWhitespace: MagicTextHasWhitespace;
isCode: boolean;
isStatic: boolean;
})[]>;
protected citationLookup: _angular_core.Signal<Map<string, string>>;
protected whitespaceContext(fragment: MagicTextFragment, position: MagicTextWhitespacePosition, index: number): {
$implicit: {
position: MagicTextWhitespacePosition;
render: boolean;
fragment: MagicTextFragment;
};
position: MagicTextWhitespacePosition;
render: boolean;
fragment: MagicTextFragment;
index: number;
};
protected templateContext<T>(node: T): $Implicit<T> & {
node: T;
};
protected toTextNode(fragment: MagicTextFragmentText): MagicTextRenderTextContext;
protected normalizeFragmentText(fragment: MagicTextFragmentText): string;
protected toLinkNode(fragment: MagicTextFragmentText): MagicTextRenderLinkContext;
protected toCitationNode(fragment: MagicTextFragmentCitation): MagicTextRenderCitationContext;
protected handleLinkClick(event: MouseEvent, fragment: MagicTextFragmentText): void;
protected handleCitationClick(event: MouseEvent, context: MagicTextRenderCitationContext): void;
static ɵfac: _angular_core.ɵɵFactoryDeclaration<MagicText, never>;
static ɵcmp: _angular_core.ɵɵComponentDeclaration<MagicText, "hb-magic-text", never, { "text": { "alias": "text"; "required": true; "isSignal": true; }; "defaultLinkTarget": { "alias": "defaultLinkTarget"; "required": false; "isSignal": true; }; "defaultLinkRel": { "alias": "defaultLinkRel"; "required": false; "isSignal": true; }; "citations": { "alias": "citations"; "required": false; "isSignal": true; }; }, { "linkClick": "linkClick"; "citationClick": "citationClick"; }, ["linkTemplate", "textTemplate", "citationTemplate", "whitespaceTemplate"], never, true, never>;
}
/**
* Represents the reactive chat resource, including current messages and control methods.
*
* @public
* @typeParam Tools - The set of tool definitions available to the chat.
* @param sendMessage - Send a new user message to the chat.
* @param 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>[]> {
/** Indicates whether the chat is currently receiving tokens. */
isReceiving: Signal<boolean>;
/** Indicates whether the chat is currently sending a user message. */
isSending: Signal<boolean>;
/** Indicates whether the chat is currently generating assistant output. */
isGenerating: Signal<boolean>;
/** Indicates whether the chat is running tool calls. */
isRunningToolCalls: Signal<boolean>;
/** Aggregate loading flag across transport, generation, tool calls, and thread load/save. */
isLoading: Signal<boolean>;
/** Indicates whether a thread load request is in flight. */
isLoadingThread: Signal<boolean>;
/** Indicates whether a thread save request is in flight. */
isSavingThread: Signal<boolean>;
/** Transport/request error before generation frames arrive. */
sendingError: Signal<Error | undefined>;
/** Error emitted during generation frames. */
generatingError: Signal<Error | undefined>;
/** Thread loading error, if present. */
threadLoadError: Signal<{
error: string;
stacktrace?: string;
} | undefined>;
/** Thread saving error, if present. */
threadSaveError: Signal<{
error: string;
stacktrace?: string;
} | undefined>;
/**
* Send a new user message to the chat.
*
* @param message - The user message to send.
*/
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;
/**
* Remove the last assistant response and re-send the previous user message. Returns true if a reload was performed.
*
* @returns Whether the resource was reloaded.
*/
reload: () => boolean;
/**
* The last assistant message for the chat.
*
*/
lastAssistantMessage: Signal<Chat.AssistantMessage<string, Tools> | undefined>;
}
/**
* Configuration options for the chat resource.
*
* @public
* @typeParam Tools - The set of tool definitions available to the chat.
* @param system - The system (assistant) prompt.
* @param model - The model identifier to use for the chat.
* @param tools - Optional array of bound tools available to the chat.
* @param messages - Optional initial list of chat messages.
* @param debounce - Optional debounce interval in milliseconds between user inputs.
* @param debugName - Optional name used for debugging in logs.
* @param 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: ModelInput | Signal<ModelInput>;
/**
* The tools to use for the chat.
*
* @typeParam Tools - The set of tool definitions available to the chat.
*/
tools?: Tools[];
/**
* The initial messages for the chat.
*
* @typeParam Tools - The set of tool definitions available to 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;
/**
* Custom transport to use for this chat resource.
*/
transport?: TransportOrFactory;
/**
* Optional thread identifier used to load or continue an existing conversation.
*/
threadId?: SignalLike<string | undefined>;
}
/**
* This Angular resource provides a reactive chat interface for send and receiving messages from a model.
* The resource-based API includes signals for the current messages, status, and control methods for sending and stopping messages.
*
* @public
* @remarks
* The `chatResource` function provides the most basic functionality for un-structured chats. Unstructured chats include things like general chats and natural language controls.
*
* @param options - Configuration for the chat resource.
* @returns An object with reactive signals and methods for interacting with the chat.
* @typeParam Tools - The set of tool definitions available to the chat.
* @example
* This example demonstrates how to use the `chatResource` function to create a simple chat component.
*
* ```ts
* const chat = chatResource({
* system: 'hashbrowns should be covered and smothered',
* model: 'gpt-5',
* });
*
* chat.sendMessage(\{ role: 'user', content: 'Write a short story about breakfast.' \});
* ```
*/
declare function chatResource<Tools extends Chat.AnyTool>(options: ChatResourceOptions<Tools>): ChatResourceRef<Tools>;
/**
* A reference to the completion resource.
*
* @public
*/
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;
/** Indicates whether the chat is receiving tokens. */
isReceiving: Signal<boolean>;
/** Indicates whether the chat is sending. */
isSending: Signal<boolean>;
/** Indicates whether the chat is generating. */
isGenerating: Signal<boolean>;
/** Indicates whether tool calls are running. */
isRunningToolCalls: Signal<boolean>;
/** Aggregate loading flag across transport, generation, tool calls, and thread load/save. */
isLoading: Signal<boolean>;
/** Whether a thread load request is in flight. */
isLoadingThread: Signal<boolean>;
/** Whether a thread save request is in flight. */
isSavingThread: Signal<boolean>;
/** Error encountered while loading a thread. */
threadLoadError: Signal<{
error: string;
stacktrace?: string;
} | undefined>;
/** Error encountered while saving a thread. */
threadSaveError: Signal<{
error: string;
stacktrace?: string;
} | undefined>;
/** Transport/request error before generation frames arrive. */
sendingError: Signal<Error | undefined>;
/** Error emitted during generation frames. */
generatingError: Signal<Error | undefined>;
}
/**
* Options for the completion resource.
*
* @public
*/
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;
/**
* Custom transport override for this completion resource.
*/
transport?: TransportOrFactory;
/**
* Optional thread identifier used to load or continue an existing conversation.
*/
threadId?: SignalLike<string | undefined>;
}
/**
* Creates a completion resource.
*
* @public
* @param options - The options for the completion resource.
* @typeParam Input - The type of the input to the completion.
* @returns The completion resource.
*/
declare function completionResource<Input>(options: CompletionResourceOptions<Input>): CompletionResourceRef;
/**
* A reference to the structured chat resource.
*
* @public
* @typeParam Output - The type of the output from the chat.
* @typeParam Tools - The set of tool definitions available to the chat.
*/
interface StructuredChatResourceRef<Output, Tools extends Chat.AnyTool> extends Resource<Chat.Message<Output, Tools>[]> {
/**
* Indicates whether the underlying chat call is currently sending a message.
*/
isSending: Signal<boolean>;
/**
* Indicates whether the chat is generating assistant output.
*/
isGenerating: Signal<boolean>;
/**
* Whether the resource is currently receiving a response from the model.
*/
isReceiving: Signal<boolean>;
/**
* Whether the chat is running tool calls.
*/
isRunningToolCalls: Signal<boolean>;
/**
* Aggregate loading flag across transport, generation, tool calls, and thread load/save.
*/
isLoading: Signal<boolean>;
/** Indicates whether a thread load request is in flight. */
isLoadingThread: Signal<boolean>;
/** Indicates whether a thread save request is in flight. */
isSavingThread: Signal<boolean>;
/** Transport/request error before generation frames arrive. */
sendingError: Signal<Error | undefined>;
/** Error emitted during generation frames. */
generatingError: Signal<Error | undefined>;
/** Thread loading error, if present. */
threadLoadError: Signal<{
error: string;
stacktrace?: string;
} | undefined>;
/** Thread saving error, if present. */
threadSaveError: Signal<{
error: string;
stacktrace?: string;
} | undefined>;
/**
* Send a new user message to the chat.
*
* @param message - The user message to send.
*/
sendMessage: (message: Chat.UserMessage) => void;
/**
* Cause current messages to be resent. Can be used after an error in chat.
*/
resendMessages: () => void;
/**
* Update the chat messages.
*
* @param messages - The new array of chat messages.
*/
setMessages: (messages: Chat.Message<Output, Tools>[]) => void;
/**
* Remove the last assistant response and re-send the previous user message. Returns true if a reload was performed.
*
* @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;
lastAssistantMessage: Signal<Chat.AssistantMessage<Output, Tools> | undefined>;
}
/**
* Options for the structured chat resource.
*
* @public
*/
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: ModelInput;
/**
* 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;
/**
* Custom transport override for the structured chat resource.
*/
transport?: TransportOrFactory;
/**
* Whether this structured chat is generating UI content.
*/
ui?: boolean;
/**
* Optional thread identifier used to load or continue an existing conversation.
*/
threadId?: SignalLike<string | undefined>;
}
/**
* Creates a structured chat resource.
*
* @public
* @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.
*
* @public
*/
interface StructuredCompletionResourceRef<Output> extends Resource<Output | null> {
/**
* Indicates whether the underlying completion call is currently sending a message.
*/
isSending: Signal<boolean>;
/**
* Indicates whether the completion is generating assistant output.
*/
isGenerating: Signal<boolean>;
/**
* Indicates whether the underlying completion call is currently receiving tokens.
*/
isReceiving: Signal<boolean>;
/** Indicates whether tool calls are running. */
isRunningToolCalls: Signal<boolean>;
/** Aggregate loading flag across transport, generation, tool calls, and thread load/save. */
isLoading: Signal<boolean>;
/** Whether a thread load request is in flight. */
isLoadingThread: Signal<boolean>;
/** Whether a thread save request is in flight. */
isSavingThread: Signal<boolean>;
/** Error encountered while loading a thread. */
threadLoadError: Signal<{
error: string;
stacktrace?: string;
} | undefined>;
/** Error encountered while saving a thread. */
threadSaveError: Signal<{
error: string;
stacktrace?: string;
} | undefined>;
/** Transport/request error before generation frames arrive. */
sendingError: Signal<Error | undefined>;
/** Error emitted during generation frames. */
generatingError: Signal<Error | undefined>;
/**
* 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 structured completion resource.
*
* @public
*/
interface StructuredCompletionResourceOptions<Input, Schema extends s.HashbrownType> {
/**
* The model to use for the structured completion resource.
*/
model: ModelInput;
/**
* 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;
/**
* The number of retries for the structured completion resource.
*/
retries?: number;
/**
* The debounce time for the structured completion resource.
*/
debounce?: number;
/**
* Optional transport override for this structured completion resource.
*/
transport?: TransportOrFactory;
/**
* Whether this completion is UI generating.
*/
ui?: boolean;
/**
* Optional thread identifier used to load or continue an existing conversation.
*/
threadId?: SignalLike<string | undefined>;
}
/**
* Creates a structured completion resource.
*
* @public
* @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>;
/**
* @public
*/
type UiChatMessageOutput = s.ObjectType<{
ui: s.ArrayType<s.ObjectType<_ui.ComponentTreeSchema>>;
}>;
/**
* Options for the UI chat resource.
*
* @public
* @typeParam Tools - The set of tool definitions available to the chat.
*/
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: ModelInput;
/**
* The system prompt to use for the UI chat resource.
*/
system: string | Signal<string> | SystemPrompt | Signal<SystemPrompt>;
/**
* The initial messages for the UI chat resource.
*
* @typeParam Tools - The set of tool definitions available to the chat.
*/
messages?: Chat.Message<s.Infer<UiChatMessageOutput>, Tools>[];
/**
* The tools to use for the UI chat resource.
*
* @typeParam Tools - The set of tool definitions available to the chat.
*/
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;
/**
* Custom transport override for the UI chat resource.
*/
transport?: TransportOrFactory;
/**
* Optional thread identifier used to load or continue an existing conversation.
*/
threadId?: SignalLike<string | undefined>;
}
/**
* A reference to the UI chat resource.
*
* @public
*/
interface UiChatResourceRef<Tools extends Chat.AnyTool> extends Resource<UiChatMessage<Tools>[]> {
/**
* Send a new user message to the chat.
*
* @param message - The user message to send.
*/
sendMessage: (message: Chat.UserMessage) => void;
/**
* Cause current messages to be resent. Can be used after an error in chat.
*/
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<UiAssistantMessage<Tools> | undefined>;
}
/**
* Creates a UI chat resource.
*
* @public
* @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>;
/**
* Options for the UI completion resource.
*
* @public
*/
interface UiCompletionResourceOptions<Input, Tools extends Chat.AnyTool = Chat.AnyTool> {
/**
* The components to use for the UI completion resource.
*/
components: ExposedComponent<any>[];
/**
* The signal that produces the input for the completion.
*/
input: Signal<Input | null | undefined>;
/**
* The model to use for the UI completion resource.
*/
model: ModelInput;
/**
* The system prompt to use for the UI completion resource.
*/
system: string | Signal<string> | SystemPrompt | Signal<SystemPrompt>;
/**
* The tools to use for the UI completion resource.
*/
tools?: Tools[];
/**
* The debug name for the UI completion resource.
*/
debugName?: string;
/**
* The API URL to use for the UI completion resource.
*/
apiUrl?: string;
/**
* The number of retries for the UI completion resource.
*/
retries?: number;
/**
* The debounce time for the UI completion resource.
*/
debounce?: number;
/**
* Custom transport override for the UI completion resource.
*/
transport?: TransportOrFactory;
/**
* Optional thread identifier used to load or continue an existing conversation.
*/
threadId?: SignalLike<string | undefined>;
}
/**
* A reference to the UI completion resource.
*
* @public
*/
interface UiCompletionResourceRef<Tools extends Chat.AnyTool> extends Resource<UiAssistantMessage<Tools> | null> {
/**
* Indicates whether the underlying completion call is currently sending a request.
*/
isSending: Signal<boolean>;
/**
* Indicates whether the underlying completion call is currently receiving data.
*/
isReceiving: Signal<boolean>;
/**
* Reloads the completion.
*
* @returns Whether the completion was reloaded.
*/
reload: () => boolean;
/**
* Stops any currently streaming response.
*
* @param clearStreamingMessage - Whether to clear the current streaming response.
*/
stop: (clearStreamingMessage?: boolean) => void;
}
/**
* Creates a UI completion resource that returns UI assistant messages.
*
* @public
* @param options - The options for the UI completion resource.
* @returns The UI completion resource.
*/
declare function uiCompletionResource<Input, Tools extends Chat.AnyTool = Chat.AnyTool>(options: UiCompletionResourceOptions<Input, Tools>): UiCompletionResourceRef<Tools>;
/**
* Hashbrown must be configured with a base URL,
* and may optionally include middleware and a flag
* to emulate structured output.
*
* @public
*/
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;
/**
* Optional transport to use instead of the default HTTP transport.
*/
transport?: TransportOrFactory;
}
/**
* Provides the Hashbrown configuration.
*
* @public
* @param options - The Hashbrown configuration.
* @returns The Hashbrown configuration.
*/
declare function provideHashbrown(options: ProvideHashbrownOptions): Provider;
export { MagicText, MagicTextRenderCitation, MagicTextRenderLink, MagicTextRenderText, MagicTextRenderWhitespace, RenderMessageComponent, chatResource, completionResource, createRuntime, createRuntimeFunction, createTool, createToolJavaScript, exposeComponent, provideHashbrown, structuredChatResource, structuredCompletionResource, uiChatResource, uiCompletionResource };
export type { AngularSignalLike, ChatResourceOptions, ChatResourceRef, CompletionResourceOptions, CompletionResourceRef, ComponentPropSchema, CreateRuntimeOptions, CreateToolJavaScriptOptions, ExposedComponent, MagicTextCitation, MagicTextCitationClickEvent, MagicTextLinkClickEvent, MagicTextRenderCitationContext, MagicTextRenderLinkContext, MagicTextRenderTextContext, MagicTextWhitespaceContext, MagicTextWhitespacePosition, ProvideHashbrownOptions, SignalLike, StructuredChatResourceOptions, StructuredChatResourceRef, StructuredCompletionResourceOptions, StructuredCompletionResourceRef, TagNameRegistry, UiAssistantMessage, UiChatMessage, UiChatMessageOutput, UiChatResourceOptions, UiChatResourceRef, UiChatSchema, UiChatSchemaComponent, UiCompletionResourceOptions, UiCompletionResourceRef, UiErrorMessage, UiUserMessage };