UNPKG

call-ai

Version:

Lightweight library for making AI API calls with streaming support

229 lines (228 loc) 6.92 kB
import { callAi } from "./api.js"; export type Falsy = false | null | undefined | 0 | ""; export interface OriginalError { readonly originalError: Error; readonly refreshError: Error; readonly status: number; } export interface ContentItem { readonly type: "text" | "image_url"; readonly text?: string; readonly image_url?: { readonly url: string; }; } export interface Message { readonly role: "user" | "system" | "assistant"; readonly content: string | ContentItem[]; } export interface ResponseMeta { model: string; endpoint?: string; timing: { readonly startTime: number; endTime?: number; duration?: number; }; rawResponse?: ModelId | string; } export interface ModelId { readonly model: string; readonly id: string; } export interface Schema { readonly name?: string; readonly properties: Record<string, unknown>; readonly required?: string[]; readonly additionalProperties?: boolean; readonly [key: string]: unknown; } export interface ToolUseType { readonly type: "tool_use"; readonly input: string; readonly tool_calls: OpenAIFunctionCall[]; } export declare function isToolUseType(obj: unknown): obj is ToolUseType; export interface ToolUseResponse { readonly tool_use: { readonly input: string; }; } export declare function isToolUseResponse(obj: unknown): obj is ToolUseResponse; export interface AIResult { choices: { message: { content?: string; function_call: string | ToolUseType | ToolUseResponse; tool_calls?: string; }; text?: string; }[]; } export interface OpenAIFunctionCall { readonly type: "function"; readonly function: { readonly arguments?: string; readonly name?: string; readonly description?: string; readonly parameters?: RequestSchema | ProcessedSchema; }; } export declare function isOpenAIArray(obj: unknown): obj is OpenAIFunctionCall[]; export interface RequestSchema { model?: string; name?: string; type: "object"; description?: string; properties?: unknown; required?: unknown[]; parameters?: RequestSchema; additionalProperties?: unknown; } export interface SchemaAIMessageRequest { model: string; messages: Message[]; max_tokens: number; temperature: number; top_p: number; stream: boolean; response_format?: SchemaAIJsonSchemaRequest["response_format"] | SchemaAIJsonObjectRequest["response_format"]; [key: string]: unknown; } export interface ProcessedSchema { properties: Record<string, unknown>; items?: ProcessedSchema; [key: string]: unknown; } export interface SchemaType { readonly type: string; } export interface SchemaDescription { readonly description: string; } export interface SchemaAIJsonObjectRequest { response_format: { type: "json_object"; }; } export interface SchemaAIJsonSchemaRequest { response_format: { type: "json_schema"; json_schema: { name: string; strict?: boolean; schema: ProcessedSchema; }; }; } interface SchemaAIToolRequest { tools: OpenAIFunctionCall[]; tool_choice: OpenAIFunctionCall; } interface SchemaAISimpleMsg { readonly messages: Message[]; } export interface ModelStrategy { readonly name: string; readonly prepareRequest: (schema: Schema | Falsy, messages: Message[]) => SchemaAISimpleMsg | SchemaAIMessageRequest | SchemaAIToolRequest | SchemaAIJsonSchemaRequest | SchemaAIJsonObjectRequest; readonly processResponse: (content: string | ToolUseType | ToolUseResponse | OpenAIFunctionCall[]) => string; readonly shouldForceStream?: boolean; } export interface CallAIErrorParams { readonly message: string; readonly status: number; readonly statusText?: string; readonly details?: unknown; readonly contentType?: string; readonly statusCode?: number; readonly response?: { readonly status: number; }; readonly partialContent?: string; readonly name?: string; readonly cause?: unknown; readonly originalError?: CallAIErrorParams | Error; readonly refreshError?: unknown; readonly errorType?: string; } export declare class CallAIError extends Error { readonly message: string; readonly status: number; readonly statusText?: string; readonly details?: unknown; readonly contentType?: string; readonly originalError?: CallAIErrorParams | Error; readonly refreshError?: unknown; readonly errorType?: string; readonly partialContent?: string; constructor(params: CallAIErrorParams); } export type SchemaStrategyType = "json_schema" | "tool_mode" | "system_message" | "none"; export interface SchemaStrategy { readonly strategy: SchemaStrategyType; readonly model: string; readonly prepareRequest: ModelStrategy["prepareRequest"]; readonly processResponse: ModelStrategy["processResponse"]; readonly shouldForceStream: boolean; } export type StreamResponse = AsyncGenerator<string, string, unknown>; export type ThenableStreamResponse = AsyncGenerator<string, string, unknown> & Promise<StreamResponse>; export interface CallAIOptions { readonly apiKey?: string; readonly model?: string; readonly endpoint?: string; readonly chatUrl?: string; stream?: boolean; refreshToken?: string; readonly updateRefreshToken?: (currentToken: string) => Promise<string>; readonly schema?: Schema | null; readonly modalities?: string[]; readonly skipRetry?: boolean; readonly skipRefresh?: boolean; readonly debug?: boolean; readonly referer?: string; readonly title?: string; readonly schemaStrategy?: SchemaStrategy; readonly maxTokens?: number; temperature?: number; readonly topP?: number; response_format?: { type: "json_object"; }; readonly mock?: Mocks; [key: string]: unknown; } export interface Mocks { readonly fetch?: typeof fetch; readonly callAI?: typeof callAi; } export interface AIResponse { readonly text: string; readonly usage?: { readonly promptTokens: number; readonly completionTokens: number; readonly totalTokens: number; }; readonly model: string; } export interface ImageResponse { readonly created: number; readonly data: { readonly b64_json: string; readonly url?: string; readonly revised_prompt?: string; }[]; } export interface ImageGenOptions { readonly apiKey?: string; readonly model?: string; readonly size?: string; readonly quality?: string; readonly style?: string; readonly images?: File[]; readonly imgUrl?: string; readonly debug?: boolean; readonly mock?: Mocks; } export type ImageEditOptions = ImageGenOptions; export {};