UNPKG

aiwrapper

Version:

A Universal AI Wrapper for JavaScript & TypeScript

158 lines (157 loc) 4.53 kB
export type LangMessageRole = "user" | "assistant" | "tool-results"; export type LangMessageContent = string | LangContentPart[] | ToolRequest[] | ToolResult[]; export type LangMessageMeta = Record<string, any>; export type LangContentPart = { type: "text"; text: string; } | { type: "image"; image: LangContentImage; alt?: string; } | { type: "reasoning"; text: string; }; /** * Interface for tool requests that can be sent to language models */ export interface ToolRequest { callId: string; name: string; arguments: Record<string, any>; } /** * Interface for tool execution results */ export interface ToolResult { toolId: string; name: string; result: any; } export type LangContentImage = { kind: "url"; url: string; } | { kind: "base64"; base64: string; mimeType?: string; } | { kind: "bytes"; bytes: ArrayBuffer | Uint8Array; mimeType?: string; } | { kind: "blob"; blob: Blob; mimeType?: string; }; export type LangImageOutput = { url?: string; base64?: string; mimeType?: string; width?: number; height?: number; metadata?: Record<string, any>; }; /** * Built-in tools provided by the language model provider * These tools don't require handlers as they're executed by the provider */ export type BuiltInLangTool = { name: string; [key: string]: any; }; /** * Union type for all tool types: custom functions with handlers and built-in provider tools */ export type LangTool = LangToolWithHandler | BuiltInLangTool; export type LangToolWithHandler = { name: string; description: string; parameters: Record<string, any>; handler: (args: Record<string, any>) => any | Promise<any>; }; export type LangMessageItem = LangMessageItemText | LangMessageItemReasoning | LangMessageItemImage | LangMessageItemTool | LangMessageItemToolResult; export type LangMessageItemText = { type: "text"; text: string; }; export type LangMessageItemReasoning = { type: "reasoning"; text: string; }; export type LangMessageItemImage = { type: "image"; url?: string; base64?: string; mimeType?: string; width?: number; height?: number; metadata?: Record<string, any>; }; export type LangMessageItemTool = { type: "tool"; name: string; callId: string; arguments: Record<string, any>; }; export type LangMessageItemToolResult = { type: "tool-result"; name: string; callId: string; result: any; }; export declare class LangMessage { role: LangMessageRole; items: LangMessageItem[]; meta?: Record<string, any>; constructor(role: LangMessageRole, text: string, meta?: Record<string, any>); constructor(role: LangMessageRole, items: LangMessageItem[], meta?: Record<string, any>); get text(): string; get reasoning(): string; get object(): any | null; get toolRequests(): LangMessageItemTool[]; get toolResults(): LangMessageItemToolResult[]; get images(): LangMessageItemImage[]; } export declare class LangMessages extends Array<LangMessage> { availableTools?: LangTool[]; finished: boolean; aborted: boolean; instructions?: string; constructor(); constructor(initialPrompt: string, opts?: { tools?: LangTool[]; }); constructor(initialMessages: LangMessage[], opts?: { tools?: LangTool[]; }); constructor(initialMessages: LangMessages, opts?: { tools?: LangTool[]; }); constructor(initialMessages: { role: LangMessageRole; items: LangMessageItem[]; meta?: Record<string, any>; }[], opts?: { tools?: LangTool[]; }); /** * Last assistant message as text */ get answer(): string; get object(): any | null; get assistantImages(): LangImageOutput[]; get userImages(): LangImageOutput[]; private getImagesFromLastMessage; addUserMessage(content: string): this; addUserItems(items: LangMessageItem[]): this; addToolResultsMessage(items: LangMessageItemToolResult[]): this; addUserImages(image: LangContentImage): this; addUserImages(images: LangContentImage[]): this; private createImageMessageItem; private static encodeBytesAsBase64; addAssistantMessage(content: string, meta?: Record<string, any>): this; addAssistantItems(items: LangMessageItem[], meta?: Record<string, any>): this; executeRequestedTools(meta?: Record<string, any>): Promise<LangMessage | null>; toString(): string; }