@providerprotocol/ai 0.0.1 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -18,12 +18,13 @@ import {
18
18
  RetryAfterStrategy,
19
19
  TokenBucket
20
20
  } from "./chunk-CUCRF5W6.js";
21
+ import "./chunk-X5G4EHL7.js";
21
22
  import {
22
23
  DynamicKey,
23
24
  RoundRobinKeys,
24
25
  UPPError,
25
26
  WeightedKeys
26
- } from "./chunk-FTFX2VET.js";
27
+ } from "./chunk-SUNYWHTH.js";
27
28
 
28
29
  // src/types/turn.ts
29
30
  function createTurn(messages, toolExecutions, usage, cycles, data) {
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/types/turn.ts","../src/types/stream.ts","../src/core/llm.ts","../src/core/image.ts","../src/types/content.ts","../src/types/thread.ts","../src/index.ts"],"sourcesContent":["import type { Message, AssistantMessage } from './messages.ts';\nimport type { ToolExecution } from './tool.ts';\n\n/**\n * Token usage information\n */\nexport interface TokenUsage {\n /** Input tokens across all cycles */\n inputTokens: number;\n\n /** Output tokens across all cycles */\n outputTokens: number;\n\n /** Total tokens */\n totalTokens: number;\n\n /** Per-cycle breakdown (if available) */\n cycles?: Array<{\n inputTokens: number;\n outputTokens: number;\n }>;\n}\n\n/**\n * A Turn represents the complete result of one inference call,\n * including all messages produced during tool execution loops.\n */\nexport interface Turn<TData = unknown> {\n /**\n * All messages produced during this inference, in chronological order.\n * Types: UserMessage, AssistantMessage (may include toolCalls), ToolResultMessage\n */\n readonly messages: Message[];\n\n /** The final assistant response (convenience accessor) */\n readonly response: AssistantMessage;\n\n /** Tool executions that occurred during this turn */\n readonly toolExecutions: ToolExecution[];\n\n /** Aggregate token usage for the entire turn */\n readonly usage: TokenUsage;\n\n /** Total number of inference cycles (1 + number of tool rounds) */\n readonly cycles: number;\n\n /**\n * Structured output data (if structure was provided).\n * Type is inferred from the schema when using TypeScript.\n */\n readonly data?: TData;\n}\n\n/**\n * Create a Turn from accumulated data\n */\nexport function createTurn<TData = unknown>(\n messages: Message[],\n toolExecutions: ToolExecution[],\n usage: TokenUsage,\n cycles: number,\n data?: TData\n): Turn<TData> {\n // Find the last assistant message as the response\n const response = messages\n .filter((m): m is AssistantMessage => m.type === 'assistant')\n .pop();\n\n if (!response) {\n throw new Error('Turn must contain at least one assistant message');\n }\n\n return {\n messages,\n response,\n toolExecutions,\n usage,\n cycles,\n data,\n };\n}\n\n/**\n * Create empty token usage\n */\nexport function emptyUsage(): TokenUsage {\n return {\n inputTokens: 0,\n outputTokens: 0,\n totalTokens: 0,\n cycles: [],\n };\n}\n\n/**\n * Aggregate token usage from multiple cycles\n */\nexport function aggregateUsage(usages: TokenUsage[]): TokenUsage {\n const cycles: TokenUsage['cycles'] = [];\n let inputTokens = 0;\n let outputTokens = 0;\n\n for (const usage of usages) {\n inputTokens += usage.inputTokens;\n outputTokens += usage.outputTokens;\n cycles.push({\n inputTokens: usage.inputTokens,\n outputTokens: usage.outputTokens,\n });\n }\n\n return {\n inputTokens,\n outputTokens,\n totalTokens: inputTokens + outputTokens,\n cycles,\n };\n}\n","import type { Turn } from './turn.ts';\n\n/**\n * Stream event types\n */\nexport type StreamEventType =\n | 'text_delta'\n | 'reasoning_delta'\n | 'image_delta'\n | 'audio_delta'\n | 'video_delta'\n | 'tool_call_delta'\n | 'message_start'\n | 'message_stop'\n | 'content_block_start'\n | 'content_block_stop';\n\n/**\n * Event delta data (type-specific)\n */\nexport interface EventDelta {\n text?: string;\n data?: Uint8Array;\n toolCallId?: string;\n toolName?: string;\n argumentsJson?: string;\n}\n\n/**\n * A streaming event\n */\nexport interface StreamEvent {\n /** Event type */\n type: StreamEventType;\n\n /** Index of the content block this event belongs to */\n index: number;\n\n /** Event data (type-specific) */\n delta: EventDelta;\n}\n\n/**\n * Stream result - async iterable that also provides final turn\n */\nexport interface StreamResult<TData = unknown>\n extends AsyncIterable<StreamEvent> {\n /**\n * Get the complete Turn after streaming finishes.\n * Resolves when the stream completes.\n */\n readonly turn: Promise<Turn<TData>>;\n\n /** Abort the stream */\n abort(): void;\n}\n\n/**\n * Create a stream result from an async generator and completion promise\n */\nexport function createStreamResult<TData = unknown>(\n generator: AsyncGenerator<StreamEvent, void, unknown>,\n turnPromise: Promise<Turn<TData>>,\n abortController: AbortController\n): StreamResult<TData> {\n return {\n [Symbol.asyncIterator]() {\n return generator;\n },\n turn: turnPromise,\n abort() {\n abortController.abort();\n },\n };\n}\n\n/**\n * Create a text delta event\n */\nexport function textDelta(text: string, index = 0): StreamEvent {\n return {\n type: 'text_delta',\n index,\n delta: { text },\n };\n}\n\n/**\n * Create a tool call delta event\n */\nexport function toolCallDelta(\n toolCallId: string,\n toolName: string,\n argumentsJson: string,\n index = 0\n): StreamEvent {\n return {\n type: 'tool_call_delta',\n index,\n delta: { toolCallId, toolName, argumentsJson },\n };\n}\n\n/**\n * Create a message start event\n */\nexport function messageStart(): StreamEvent {\n return {\n type: 'message_start',\n index: 0,\n delta: {},\n };\n}\n\n/**\n * Create a message stop event\n */\nexport function messageStop(): StreamEvent {\n return {\n type: 'message_stop',\n index: 0,\n delta: {},\n };\n}\n\n/**\n * Create a content block start event\n */\nexport function contentBlockStart(index: number): StreamEvent {\n return {\n type: 'content_block_start',\n index,\n delta: {},\n };\n}\n\n/**\n * Create a content block stop event\n */\nexport function contentBlockStop(index: number): StreamEvent {\n return {\n type: 'content_block_stop',\n index,\n delta: {},\n };\n}\n","import type {\n LLMOptions,\n LLMInstance,\n LLMRequest,\n LLMResponse,\n InferenceInput,\n BoundLLMModel,\n LLMCapabilities,\n} from '../types/llm.ts';\nimport type { UserMessage, AssistantMessage } from '../types/messages.ts';\nimport type { ContentBlock, TextBlock } from '../types/content.ts';\nimport type { Tool, ToolExecution, ToolResult } from '../types/tool.ts';\nimport type { Turn, TokenUsage } from '../types/turn.ts';\nimport type { StreamResult, StreamEvent } from '../types/stream.ts';\nimport type { Thread } from '../types/thread.ts';\nimport type { ProviderConfig } from '../types/provider.ts';\nimport { UPPError } from '../types/errors.ts';\nimport {\n Message,\n UserMessage as UserMessageClass,\n ToolResultMessage,\n isUserMessage,\n isAssistantMessage,\n} from '../types/messages.ts';\nimport { createTurn, aggregateUsage, emptyUsage } from '../types/turn.ts';\nimport { createStreamResult } from '../types/stream.ts';\nimport { generateShortId } from '../utils/id.ts';\n\n/**\n * Default maximum iterations for tool execution\n */\nconst DEFAULT_MAX_ITERATIONS = 10;\n\n/**\n * Create an LLM instance\n */\nexport function llm<TParams = unknown>(\n options: LLMOptions<TParams>\n): LLMInstance<TParams> {\n const { model: modelRef, config = {}, params, system, tools, toolStrategy, structure } = options;\n\n // Validate that the provider supports LLM\n const provider = modelRef.provider;\n if (!provider.modalities.llm) {\n throw new UPPError(\n `Provider '${provider.name}' does not support LLM modality`,\n 'INVALID_REQUEST',\n provider.name,\n 'llm'\n );\n }\n\n // Bind the model\n const boundModel = provider.modalities.llm.bind(modelRef.modelId) as BoundLLMModel<TParams>;\n\n // Validate capabilities at bind time\n const capabilities = boundModel.capabilities;\n\n // Check for structured output capability\n if (structure && !capabilities.structuredOutput) {\n throw new UPPError(\n `Provider '${provider.name}' does not support structured output`,\n 'INVALID_REQUEST',\n provider.name,\n 'llm'\n );\n }\n\n // Check for tools capability\n if (tools && tools.length > 0 && !capabilities.tools) {\n throw new UPPError(\n `Provider '${provider.name}' does not support tools`,\n 'INVALID_REQUEST',\n provider.name,\n 'llm'\n );\n }\n\n // Build the instance\n const instance: LLMInstance<TParams> = {\n model: boundModel,\n system,\n params,\n capabilities,\n\n async generate(\n historyOrInput: Message[] | Thread | InferenceInput,\n ...inputs: InferenceInput[]\n ): Promise<Turn> {\n const { history, messages } = parseInputs(historyOrInput, inputs);\n return executeGenerate(\n boundModel,\n config,\n system,\n params,\n tools,\n toolStrategy,\n structure,\n history,\n messages\n );\n },\n\n stream(\n historyOrInput: Message[] | Thread | InferenceInput,\n ...inputs: InferenceInput[]\n ): StreamResult {\n // Check streaming capability\n if (!capabilities.streaming) {\n throw new UPPError(\n `Provider '${provider.name}' does not support streaming`,\n 'INVALID_REQUEST',\n provider.name,\n 'llm'\n );\n }\n const { history, messages } = parseInputs(historyOrInput, inputs);\n return executeStream(\n boundModel,\n config,\n system,\n params,\n tools,\n toolStrategy,\n structure,\n history,\n messages\n );\n },\n };\n\n return instance;\n}\n\n/**\n * Type guard to check if a value is a Message instance.\n * Uses instanceof for class instances, with fallback to timestamp check\n * for deserialized/reconstructed Message objects.\n */\nfunction isMessageInstance(value: unknown): value is Message {\n if (value instanceof Message) {\n return true;\n }\n // Fallback for deserialized Messages that aren't class instances:\n // Messages have 'timestamp' (Date), ContentBlocks don't\n if (\n typeof value === 'object' &&\n value !== null &&\n 'timestamp' in value &&\n 'type' in value &&\n 'id' in value\n ) {\n const obj = value as Record<string, unknown>;\n // Message types are 'user', 'assistant', 'tool_result'\n // ContentBlock types are 'text', 'image', 'audio', 'video', 'binary'\n const messageTypes = ['user', 'assistant', 'tool_result'];\n return messageTypes.includes(obj.type as string);\n }\n return false;\n}\n\n/**\n * Parse inputs to determine history and new messages\n */\nfunction parseInputs(\n historyOrInput: Message[] | Thread | InferenceInput,\n inputs: InferenceInput[]\n): { history: Message[]; messages: Message[] } {\n // Check if it's a Thread first (has 'messages' array property)\n if (\n typeof historyOrInput === 'object' &&\n historyOrInput !== null &&\n 'messages' in historyOrInput &&\n Array.isArray((historyOrInput as Thread).messages)\n ) {\n const thread = historyOrInput as Thread;\n const newMessages = inputs.map(inputToMessage);\n return { history: [...thread.messages], messages: newMessages };\n }\n\n // Check if first arg is Message[] (history)\n if (Array.isArray(historyOrInput) && historyOrInput.length > 0) {\n const first = historyOrInput[0];\n if (isMessageInstance(first)) {\n // It's history (Message[])\n const newMessages = inputs.map(inputToMessage);\n return { history: historyOrInput as Message[], messages: newMessages };\n }\n }\n\n // It's input (no history) - could be string, single Message, or ContentBlock\n const allInputs = [historyOrInput as InferenceInput, ...inputs];\n const newMessages = allInputs.map(inputToMessage);\n return { history: [], messages: newMessages };\n}\n\n/**\n * Convert an InferenceInput to a Message\n */\nfunction inputToMessage(input: InferenceInput): Message {\n if (typeof input === 'string') {\n return new UserMessageClass(input);\n }\n\n // It's already a Message\n if ('type' in input && 'id' in input && 'timestamp' in input) {\n return input as Message;\n }\n\n // It's a ContentBlock - wrap in UserMessage\n const block = input as ContentBlock;\n if (block.type === 'text') {\n return new UserMessageClass((block as TextBlock).text);\n }\n\n return new UserMessageClass([block as any]);\n}\n\n/**\n * Execute a non-streaming generate call with tool loop\n */\nasync function executeGenerate<TParams>(\n model: BoundLLMModel<TParams>,\n config: ProviderConfig,\n system: string | undefined,\n params: TParams | undefined,\n tools: Tool[] | undefined,\n toolStrategy: LLMOptions<TParams>['toolStrategy'],\n structure: LLMOptions<TParams>['structure'],\n history: Message[],\n newMessages: Message[]\n): Promise<Turn> {\n // Validate media capabilities for all input messages\n validateMediaCapabilities(\n [...history, ...newMessages],\n model.capabilities,\n model.provider.name\n );\n const maxIterations = toolStrategy?.maxIterations ?? DEFAULT_MAX_ITERATIONS;\n const allMessages: Message[] = [...history, ...newMessages];\n const toolExecutions: ToolExecution[] = [];\n const usages: TokenUsage[] = [];\n let cycles = 0;\n\n // Track structured data from responses (providers handle extraction)\n let structuredData: unknown;\n\n // Tool loop\n while (cycles < maxIterations + 1) {\n cycles++;\n\n const request: LLMRequest<TParams> = {\n messages: allMessages,\n system,\n params,\n tools,\n structure,\n config,\n };\n\n const response = await model.complete(request);\n usages.push(response.usage);\n allMessages.push(response.message);\n\n // Track structured data from provider (if present)\n if (response.data !== undefined) {\n structuredData = response.data;\n }\n\n // Check for tool calls\n if (response.message.hasToolCalls && tools && tools.length > 0) {\n // If provider already extracted structured data, don't try to execute tool calls\n // (some providers use tool calls internally for structured output)\n if (response.data !== undefined) {\n break;\n }\n\n // Check if we've hit max iterations (subtract 1 because we already incremented)\n if (cycles >= maxIterations) {\n await toolStrategy?.onMaxIterations?.(maxIterations);\n throw new UPPError(\n `Tool execution exceeded maximum iterations (${maxIterations})`,\n 'INVALID_REQUEST',\n model.provider.name,\n 'llm'\n );\n }\n\n // Execute tools\n const results = await executeTools(\n response.message,\n tools,\n toolStrategy,\n toolExecutions\n );\n\n // Add tool results\n allMessages.push(new ToolResultMessage(results));\n\n continue;\n }\n\n // No tool calls - we're done\n break;\n }\n\n // Use structured data from provider if structure was requested\n const data = structure ? structuredData : undefined;\n\n return createTurn(\n allMessages.slice(history.length), // Only messages from this turn\n toolExecutions,\n aggregateUsage(usages),\n cycles,\n data\n );\n}\n\n/**\n * Execute a streaming generate call with tool loop\n */\nfunction executeStream<TParams>(\n model: BoundLLMModel<TParams>,\n config: ProviderConfig,\n system: string | undefined,\n params: TParams | undefined,\n tools: Tool[] | undefined,\n toolStrategy: LLMOptions<TParams>['toolStrategy'],\n structure: LLMOptions<TParams>['structure'],\n history: Message[],\n newMessages: Message[]\n): StreamResult {\n // Validate media capabilities for all input messages\n validateMediaCapabilities(\n [...history, ...newMessages],\n model.capabilities,\n model.provider.name\n );\n\n const abortController = new AbortController();\n\n // Shared state between generator and turn promise\n const allMessages: Message[] = [...history, ...newMessages];\n const toolExecutions: ToolExecution[] = [];\n const usages: TokenUsage[] = [];\n let cycles = 0;\n let generatorError: Error | null = null;\n let structuredData: unknown; // Providers extract this\n\n // Deferred to signal when generator completes\n let resolveGenerator: () => void;\n let rejectGenerator: (error: Error) => void;\n const generatorDone = new Promise<void>((resolve, reject) => {\n resolveGenerator = resolve;\n rejectGenerator = reject;\n });\n\n const maxIterations = toolStrategy?.maxIterations ?? DEFAULT_MAX_ITERATIONS;\n\n // Create the async generator - this is the ONLY place that calls the API\n async function* generateStream(): AsyncGenerator<StreamEvent, void, unknown> {\n try {\n while (cycles < maxIterations + 1) {\n cycles++;\n\n const request: LLMRequest<TParams> = {\n messages: allMessages,\n system,\n params,\n tools,\n structure,\n config,\n signal: abortController.signal,\n };\n\n const streamResult = model.stream(request);\n\n // Forward stream events\n for await (const event of streamResult) {\n yield event;\n }\n\n // Get the response\n const response = await streamResult.response;\n usages.push(response.usage);\n allMessages.push(response.message);\n\n // Track structured data from provider (if present)\n if (response.data !== undefined) {\n structuredData = response.data;\n }\n\n // Check for tool calls\n if (response.message.hasToolCalls && tools && tools.length > 0) {\n // If provider already extracted structured data, don't try to execute tool calls\n // (some providers use tool calls internally for structured output)\n if (response.data !== undefined) {\n break;\n }\n\n if (cycles >= maxIterations) {\n await toolStrategy?.onMaxIterations?.(maxIterations);\n throw new UPPError(\n `Tool execution exceeded maximum iterations (${maxIterations})`,\n 'INVALID_REQUEST',\n model.provider.name,\n 'llm'\n );\n }\n\n // Execute tools\n const results = await executeTools(\n response.message,\n tools,\n toolStrategy,\n toolExecutions\n );\n\n // Add tool results\n allMessages.push(new ToolResultMessage(results));\n\n continue;\n }\n\n break;\n }\n resolveGenerator();\n } catch (error) {\n generatorError = error as Error;\n rejectGenerator(error as Error);\n throw error;\n }\n }\n\n // Turn promise waits for the generator to complete, then builds the Turn\n const turnPromise = (async (): Promise<Turn> => {\n await generatorDone;\n\n if (generatorError) {\n throw generatorError;\n }\n\n // Use structured data from provider if structure was requested\n const data = structure ? structuredData : undefined;\n\n return createTurn(\n allMessages.slice(history.length),\n toolExecutions,\n aggregateUsage(usages),\n cycles,\n data\n );\n })();\n\n return createStreamResult(generateStream(), turnPromise, abortController);\n}\n\n/**\n * Execute tools from an assistant message\n */\nasync function executeTools(\n message: AssistantMessage,\n tools: Tool[],\n toolStrategy: LLMOptions<unknown>['toolStrategy'],\n executions: ToolExecution[]\n): Promise<ToolResult[]> {\n const toolCalls = message.toolCalls ?? [];\n const results: ToolResult[] = [];\n\n // Build tool map\n const toolMap = new Map(tools.map((t) => [t.name, t]));\n\n // Execute tools (in parallel)\n const promises = toolCalls.map(async (call) => {\n const tool = toolMap.get(call.toolName);\n if (!tool) {\n return {\n toolCallId: call.toolCallId,\n result: `Tool '${call.toolName}' not found`,\n isError: true,\n };\n }\n\n const startTime = Date.now();\n\n // Notify strategy\n await toolStrategy?.onToolCall?.(tool, call.arguments);\n\n // Check before call\n if (toolStrategy?.onBeforeCall) {\n const shouldRun = await toolStrategy.onBeforeCall(tool, call.arguments);\n if (!shouldRun) {\n return {\n toolCallId: call.toolCallId,\n result: 'Tool execution skipped',\n isError: true,\n };\n }\n }\n\n // Check approval\n let approved = true;\n if (tool.approval) {\n try {\n approved = await tool.approval(call.arguments);\n } catch (error) {\n // Approval threw - propagate\n throw error;\n }\n }\n\n if (!approved) {\n const execution: ToolExecution = {\n toolName: tool.name,\n toolCallId: call.toolCallId,\n arguments: call.arguments,\n result: 'Tool execution denied',\n isError: true,\n duration: Date.now() - startTime,\n approved: false,\n };\n executions.push(execution);\n\n return {\n toolCallId: call.toolCallId,\n result: 'Tool execution denied by approval handler',\n isError: true,\n };\n }\n\n // Execute tool\n try {\n const result = await tool.run(call.arguments);\n\n await toolStrategy?.onAfterCall?.(tool, call.arguments, result);\n\n const execution: ToolExecution = {\n toolName: tool.name,\n toolCallId: call.toolCallId,\n arguments: call.arguments,\n result,\n isError: false,\n duration: Date.now() - startTime,\n approved,\n };\n executions.push(execution);\n\n return {\n toolCallId: call.toolCallId,\n result,\n isError: false,\n };\n } catch (error) {\n await toolStrategy?.onError?.(tool, call.arguments, error as Error);\n\n const errorMessage = error instanceof Error ? error.message : String(error);\n\n const execution: ToolExecution = {\n toolName: tool.name,\n toolCallId: call.toolCallId,\n arguments: call.arguments,\n result: errorMessage,\n isError: true,\n duration: Date.now() - startTime,\n approved,\n };\n executions.push(execution);\n\n return {\n toolCallId: call.toolCallId,\n result: errorMessage,\n isError: true,\n };\n }\n });\n\n results.push(...(await Promise.all(promises)));\n return results;\n}\n\n/**\n * Check if messages contain media that requires specific capabilities\n */\nfunction validateMediaCapabilities(\n messages: Message[],\n capabilities: LLMCapabilities,\n providerName: string\n): void {\n for (const msg of messages) {\n if (!isUserMessage(msg)) continue;\n\n for (const block of msg.content) {\n if (block.type === 'image' && !capabilities.imageInput) {\n throw new UPPError(\n `Provider '${providerName}' does not support image input`,\n 'INVALID_REQUEST',\n providerName,\n 'llm'\n );\n }\n if (block.type === 'video' && !capabilities.videoInput) {\n throw new UPPError(\n `Provider '${providerName}' does not support video input`,\n 'INVALID_REQUEST',\n providerName,\n 'llm'\n );\n }\n if (block.type === 'audio' && !capabilities.audioInput) {\n throw new UPPError(\n `Provider '${providerName}' does not support audio input`,\n 'INVALID_REQUEST',\n providerName,\n 'llm'\n );\n }\n }\n }\n}\n","import type { ImageSource, ImageBlock } from '../types/content.ts';\n\n/**\n * Image class for handling images in UPP\n */\nexport class Image {\n readonly source: ImageSource;\n readonly mimeType: string;\n readonly width?: number;\n readonly height?: number;\n\n private constructor(\n source: ImageSource,\n mimeType: string,\n width?: number,\n height?: number\n ) {\n this.source = source;\n this.mimeType = mimeType;\n this.width = width;\n this.height = height;\n }\n\n /**\n * Check if this image has data loaded (false for URL sources)\n */\n get hasData(): boolean {\n return this.source.type !== 'url';\n }\n\n /**\n * Convert to base64 string (throws if source is URL)\n */\n toBase64(): string {\n if (this.source.type === 'base64') {\n return this.source.data;\n }\n\n if (this.source.type === 'bytes') {\n return btoa(\n Array.from(this.source.data)\n .map((b) => String.fromCharCode(b))\n .join('')\n );\n }\n\n throw new Error('Cannot convert URL image to base64. Fetch the image first.');\n }\n\n /**\n * Convert to data URL (throws if source is URL)\n */\n toDataUrl(): string {\n const base64 = this.toBase64();\n return `data:${this.mimeType};base64,${base64}`;\n }\n\n /**\n * Get raw bytes (throws if source is URL)\n */\n toBytes(): Uint8Array {\n if (this.source.type === 'bytes') {\n return this.source.data;\n }\n\n if (this.source.type === 'base64') {\n const binaryString = atob(this.source.data);\n const bytes = new Uint8Array(binaryString.length);\n for (let i = 0; i < binaryString.length; i++) {\n bytes[i] = binaryString.charCodeAt(i);\n }\n return bytes;\n }\n\n throw new Error('Cannot get bytes from URL image. Fetch the image first.');\n }\n\n /**\n * Get the URL (only for URL sources)\n */\n toUrl(): string {\n if (this.source.type === 'url') {\n return this.source.url;\n }\n\n throw new Error('This image does not have a URL source.');\n }\n\n /**\n * Convert to ImageBlock for use in messages\n */\n toBlock(): ImageBlock {\n return {\n type: 'image',\n source: this.source,\n mimeType: this.mimeType,\n width: this.width,\n height: this.height,\n };\n }\n\n /**\n * Create from file path (reads file into memory)\n */\n static async fromPath(path: string): Promise<Image> {\n const file = Bun.file(path);\n const data = await file.arrayBuffer();\n const mimeType = file.type || detectMimeType(path);\n\n return new Image(\n { type: 'bytes', data: new Uint8Array(data) },\n mimeType\n );\n }\n\n /**\n * Create from URL reference (does not fetch - providers handle URL conversion)\n */\n static fromUrl(url: string, mimeType?: string): Image {\n const detected = mimeType || detectMimeTypeFromUrl(url);\n return new Image({ type: 'url', url }, detected);\n }\n\n /**\n * Create from raw bytes\n */\n static fromBytes(data: Uint8Array, mimeType: string): Image {\n return new Image({ type: 'bytes', data }, mimeType);\n }\n\n /**\n * Create from base64 string\n */\n static fromBase64(base64: string, mimeType: string): Image {\n return new Image({ type: 'base64', data: base64 }, mimeType);\n }\n\n /**\n * Create from an existing ImageBlock\n */\n static fromBlock(block: ImageBlock): Image {\n return new Image(\n block.source,\n block.mimeType,\n block.width,\n block.height\n );\n }\n}\n\n/**\n * Detect MIME type from file extension\n */\nfunction detectMimeType(path: string): string {\n const ext = path.split('.').pop()?.toLowerCase();\n\n switch (ext) {\n case 'jpg':\n case 'jpeg':\n return 'image/jpeg';\n case 'png':\n return 'image/png';\n case 'gif':\n return 'image/gif';\n case 'webp':\n return 'image/webp';\n case 'svg':\n return 'image/svg+xml';\n case 'bmp':\n return 'image/bmp';\n case 'ico':\n return 'image/x-icon';\n default:\n return 'application/octet-stream';\n }\n}\n\n/**\n * Detect MIME type from URL\n */\nfunction detectMimeTypeFromUrl(url: string): string {\n try {\n const pathname = new URL(url).pathname;\n return detectMimeType(pathname);\n } catch {\n return 'application/octet-stream';\n }\n}\n","/**\n * Content block types for messages\n */\n\n/**\n * Image source types\n */\nexport type ImageSource =\n | { type: 'base64'; data: string }\n | { type: 'url'; url: string }\n | { type: 'bytes'; data: Uint8Array };\n\n/**\n * Text content block\n */\nexport interface TextBlock {\n type: 'text';\n text: string;\n}\n\n/**\n * Image content block\n */\nexport interface ImageBlock {\n type: 'image';\n source: ImageSource;\n mimeType: string;\n width?: number;\n height?: number;\n}\n\n/**\n * Audio content block\n */\nexport interface AudioBlock {\n type: 'audio';\n data: Uint8Array;\n mimeType: string;\n duration?: number;\n}\n\n/**\n * Video content block\n */\nexport interface VideoBlock {\n type: 'video';\n data: Uint8Array;\n mimeType: string;\n duration?: number;\n width?: number;\n height?: number;\n}\n\n/**\n * Binary content block for arbitrary data\n */\nexport interface BinaryBlock {\n type: 'binary';\n data: Uint8Array;\n mimeType: string;\n metadata?: Record<string, unknown>;\n}\n\n/**\n * All content block types\n */\nexport type ContentBlock =\n | TextBlock\n | ImageBlock\n | AudioBlock\n | VideoBlock\n | BinaryBlock;\n\n/**\n * Content types allowed in user messages\n */\nexport type UserContent =\n | TextBlock\n | ImageBlock\n | AudioBlock\n | VideoBlock\n | BinaryBlock;\n\n/**\n * Content types allowed in assistant messages\n */\nexport type AssistantContent =\n | TextBlock\n | ImageBlock\n | AudioBlock\n | VideoBlock;\n\n/**\n * Helper to create a text block\n */\nexport function text(content: string): TextBlock {\n return { type: 'text', text: content };\n}\n\n/**\n * Type guard for TextBlock\n */\nexport function isTextBlock(block: ContentBlock): block is TextBlock {\n return block.type === 'text';\n}\n\n/**\n * Type guard for ImageBlock\n */\nexport function isImageBlock(block: ContentBlock): block is ImageBlock {\n return block.type === 'image';\n}\n\n/**\n * Type guard for AudioBlock\n */\nexport function isAudioBlock(block: ContentBlock): block is AudioBlock {\n return block.type === 'audio';\n}\n\n/**\n * Type guard for VideoBlock\n */\nexport function isVideoBlock(block: ContentBlock): block is VideoBlock {\n return block.type === 'video';\n}\n\n/**\n * Type guard for BinaryBlock\n */\nexport function isBinaryBlock(block: ContentBlock): block is BinaryBlock {\n return block.type === 'binary';\n}\n","import { generateId } from '../utils/id.ts';\nimport {\n Message,\n UserMessage,\n AssistantMessage,\n ToolResultMessage,\n} from './messages.ts';\nimport type { MessageType, MessageMetadata } from './messages.ts';\nimport type { ContentBlock, UserContent, AssistantContent } from './content.ts';\nimport type { Turn } from './turn.ts';\nimport type { ToolCall, ToolResult } from './tool.ts';\n\n/**\n * Serialized message format\n */\nexport interface MessageJSON {\n id: string;\n type: MessageType;\n content: ContentBlock[];\n toolCalls?: ToolCall[];\n results?: ToolResult[];\n metadata?: MessageMetadata;\n timestamp: string;\n}\n\n/**\n * Serialized thread format\n */\nexport interface ThreadJSON {\n id: string;\n messages: MessageJSON[];\n createdAt: string;\n updatedAt: string;\n}\n\n/**\n * Thread - A utility class for managing conversation history\n * Users control their own history; Thread is optional\n */\nexport class Thread {\n /** Unique thread identifier */\n readonly id: string;\n\n /** Internal message storage */\n private _messages: Message[];\n\n /** Creation timestamp */\n private _createdAt: Date;\n\n /** Last update timestamp */\n private _updatedAt: Date;\n\n /**\n * Create a new thread, optionally with initial messages\n */\n constructor(messages?: Message[]) {\n this.id = generateId();\n this._messages = messages ? [...messages] : [];\n this._createdAt = new Date();\n this._updatedAt = new Date();\n }\n\n /** All messages in the thread (readonly) */\n get messages(): readonly Message[] {\n return this._messages;\n }\n\n /** Number of messages */\n get length(): number {\n return this._messages.length;\n }\n\n /**\n * Append messages from a turn\n */\n append(turn: Turn): this {\n this._messages.push(...turn.messages);\n this._updatedAt = new Date();\n return this;\n }\n\n /**\n * Add raw messages\n */\n push(...messages: Message[]): this {\n this._messages.push(...messages);\n this._updatedAt = new Date();\n return this;\n }\n\n /**\n * Add a user message\n */\n user(content: string | UserContent[]): this {\n this._messages.push(new UserMessage(content));\n this._updatedAt = new Date();\n return this;\n }\n\n /**\n * Add an assistant message\n */\n assistant(content: string | AssistantContent[]): this {\n this._messages.push(new AssistantMessage(content));\n this._updatedAt = new Date();\n return this;\n }\n\n /**\n * Get messages by type\n */\n filter(type: MessageType): Message[] {\n return this._messages.filter((m) => m.type === type);\n }\n\n /**\n * Get the last N messages\n */\n tail(count: number): Message[] {\n return this._messages.slice(-count);\n }\n\n /**\n * Create a new thread with a subset of messages\n */\n slice(start?: number, end?: number): Thread {\n return new Thread(this._messages.slice(start, end));\n }\n\n /**\n * Clear all messages\n */\n clear(): this {\n this._messages = [];\n this._updatedAt = new Date();\n return this;\n }\n\n /**\n * Convert to plain message array\n */\n toMessages(): Message[] {\n return [...this._messages];\n }\n\n /**\n * Serialize to JSON\n */\n toJSON(): ThreadJSON {\n return {\n id: this.id,\n messages: this._messages.map((m) => this.messageToJSON(m)),\n createdAt: this._createdAt.toISOString(),\n updatedAt: this._updatedAt.toISOString(),\n };\n }\n\n /**\n * Deserialize from JSON\n */\n static fromJSON(json: ThreadJSON): Thread {\n const messages = json.messages.map((m) => Thread.messageFromJSON(m));\n const thread = new Thread(messages);\n // Override the generated id with the serialized one\n (thread as { id: string }).id = json.id;\n thread._createdAt = new Date(json.createdAt);\n thread._updatedAt = new Date(json.updatedAt);\n return thread;\n }\n\n /**\n * Iterate over messages\n */\n [Symbol.iterator](): Iterator<Message> {\n return this._messages[Symbol.iterator]();\n }\n\n /**\n * Convert a message to JSON\n */\n private messageToJSON(m: Message): MessageJSON {\n const base: MessageJSON = {\n id: m.id,\n type: m.type,\n content: [],\n metadata: m.metadata,\n timestamp: m.timestamp.toISOString(),\n };\n\n if (m instanceof UserMessage) {\n base.content = m.content;\n } else if (m instanceof AssistantMessage) {\n base.content = m.content;\n base.toolCalls = m.toolCalls;\n } else if (m instanceof ToolResultMessage) {\n base.results = m.results;\n }\n\n return base;\n }\n\n /**\n * Reconstruct a message from JSON\n */\n private static messageFromJSON(json: MessageJSON): Message {\n const options = {\n id: json.id,\n metadata: json.metadata,\n };\n\n switch (json.type) {\n case 'user':\n return new UserMessage(json.content as UserContent[], options);\n case 'assistant':\n return new AssistantMessage(\n json.content as AssistantContent[],\n json.toolCalls,\n options\n );\n case 'tool_result':\n return new ToolResultMessage(json.results ?? [], options);\n default:\n throw new Error(`Unknown message type: ${json.type}`);\n }\n }\n}\n","// Core entry points\nexport { llm } from './core/llm.ts';\nexport { createProvider } from './core/provider.ts';\nexport { Image } from './core/image.ts';\n\n// Namespace object for alternative import style\nimport { llm } from './core/llm.ts';\n\n/**\n * UPP namespace object\n * Provides ai.llm(), ai.embedding(), ai.image() style access\n */\nexport const ai = {\n llm,\n // embedding, // Coming soon\n // image, // Coming soon\n};\n\n// Re-export all types from types/index.ts\nexport * from './types/index.ts';\n\n// Re-export HTTP utilities\nexport {\n RoundRobinKeys,\n WeightedKeys,\n DynamicKey,\n ExponentialBackoff,\n LinearBackoff,\n NoRetry,\n TokenBucket,\n RetryAfterStrategy,\n} from './http/index.ts';\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwDO,SAAS,WACd,UACA,gBACA,OACA,QACA,MACa;AAEb,QAAM,WAAW,SACd,OAAO,CAAC,MAA6B,EAAE,SAAS,WAAW,EAC3D,IAAI;AAEP,MAAI,CAAC,UAAU;AACb,UAAM,IAAI,MAAM,kDAAkD;AAAA,EACpE;AAEA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;AAKO,SAAS,aAAyB;AACvC,SAAO;AAAA,IACL,aAAa;AAAA,IACb,cAAc;AAAA,IACd,aAAa;AAAA,IACb,QAAQ,CAAC;AAAA,EACX;AACF;AAKO,SAAS,eAAe,QAAkC;AAC/D,QAAM,SAA+B,CAAC;AACtC,MAAI,cAAc;AAClB,MAAI,eAAe;AAEnB,aAAW,SAAS,QAAQ;AAC1B,mBAAe,MAAM;AACrB,oBAAgB,MAAM;AACtB,WAAO,KAAK;AAAA,MACV,aAAa,MAAM;AAAA,MACnB,cAAc,MAAM;AAAA,IACtB,CAAC;AAAA,EACH;AAEA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA,aAAa,cAAc;AAAA,IAC3B;AAA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,MACR,SAAS,CAAC;AAAA,MACV,UAAU,EAAE;AAAA,MACZ,WAAW,EAAE,UAAU,YAAY;AAAA,IACrC;AAEA,QAAI,aAAa,aAAa;AAC5B,WAAK,UAAU,EAAE;AAAA,IACnB,WAAW,aAAa,kBAAkB;AACxC,WAAK,UAAU,EAAE;AACjB,WAAK,YAAY,EAAE;AAAA,IACrB,WAAW,aAAa,mBAAmB;AACzC,WAAK,UAAU,EAAE;AAAA,IACnB;AAEA,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA,EAKA,OAAe,gBAAgB,MAA4B;AACzD,UAAM,UAAU;AAAA,MACd,IAAI,KAAK;AAAA,MACT,UAAU,KAAK;AAAA,IACjB;AAEA,YAAQ,KAAK,MAAM;AAAA,MACjB,KAAK;AACH,eAAO,IAAI,YAAY,KAAK,SAA0B,OAAO;AAAA,MAC/D,KAAK;AACH,eAAO,IAAI;AAAA,UACT,KAAK;AAAA,UACL,KAAK;AAAA,UACL;AAAA,QACF;AAAA,MACF,KAAK;AACH,eAAO,IAAI,kBAAkB,KAAK,WAAW,CAAC,GAAG,OAAO;AAAA,MAC1D;AACE,cAAM,IAAI,MAAM,yBAAyB,KAAK,IAAI,EAAE;AAAA,IACxD;AAAA,EACF;AACF;;;ACrNO,IAAM,KAAK;AAAA,EAChB;AAAA;AAAA;AAGF;","names":["text","newMessages"]}
1
+ {"version":3,"sources":["../src/types/turn.ts","../src/types/stream.ts","../src/core/llm.ts","../src/core/image.ts","../src/types/content.ts","../src/types/thread.ts","../src/index.ts"],"sourcesContent":["import type { Message, AssistantMessage } from './messages.ts';\nimport type { ToolExecution } from './tool.ts';\n\n/**\n * Token usage information\n */\nexport interface TokenUsage {\n /** Input tokens across all cycles */\n inputTokens: number;\n\n /** Output tokens across all cycles */\n outputTokens: number;\n\n /** Total tokens */\n totalTokens: number;\n\n /** Per-cycle breakdown (if available) */\n cycles?: Array<{\n inputTokens: number;\n outputTokens: number;\n }>;\n}\n\n/**\n * A Turn represents the complete result of one inference call,\n * including all messages produced during tool execution loops.\n */\nexport interface Turn<TData = unknown> {\n /**\n * All messages produced during this inference, in chronological order.\n * Types: UserMessage, AssistantMessage (may include toolCalls), ToolResultMessage\n */\n readonly messages: Message[];\n\n /** The final assistant response (convenience accessor) */\n readonly response: AssistantMessage;\n\n /** Tool executions that occurred during this turn */\n readonly toolExecutions: ToolExecution[];\n\n /** Aggregate token usage for the entire turn */\n readonly usage: TokenUsage;\n\n /** Total number of inference cycles (1 + number of tool rounds) */\n readonly cycles: number;\n\n /**\n * Structured output data (if structure was provided).\n * Type is inferred from the schema when using TypeScript.\n */\n readonly data?: TData;\n}\n\n/**\n * Create a Turn from accumulated data\n */\nexport function createTurn<TData = unknown>(\n messages: Message[],\n toolExecutions: ToolExecution[],\n usage: TokenUsage,\n cycles: number,\n data?: TData\n): Turn<TData> {\n // Find the last assistant message as the response\n const response = messages\n .filter((m): m is AssistantMessage => m.type === 'assistant')\n .pop();\n\n if (!response) {\n throw new Error('Turn must contain at least one assistant message');\n }\n\n return {\n messages,\n response,\n toolExecutions,\n usage,\n cycles,\n data,\n };\n}\n\n/**\n * Create empty token usage\n */\nexport function emptyUsage(): TokenUsage {\n return {\n inputTokens: 0,\n outputTokens: 0,\n totalTokens: 0,\n cycles: [],\n };\n}\n\n/**\n * Aggregate token usage from multiple cycles\n */\nexport function aggregateUsage(usages: TokenUsage[]): TokenUsage {\n const cycles: TokenUsage['cycles'] = [];\n let inputTokens = 0;\n let outputTokens = 0;\n\n for (const usage of usages) {\n inputTokens += usage.inputTokens;\n outputTokens += usage.outputTokens;\n cycles.push({\n inputTokens: usage.inputTokens,\n outputTokens: usage.outputTokens,\n });\n }\n\n return {\n inputTokens,\n outputTokens,\n totalTokens: inputTokens + outputTokens,\n cycles,\n };\n}\n","import type { Turn } from './turn.ts';\n\n/**\n * Stream event types\n */\nexport type StreamEventType =\n | 'text_delta'\n | 'reasoning_delta'\n | 'image_delta'\n | 'audio_delta'\n | 'video_delta'\n | 'tool_call_delta'\n | 'message_start'\n | 'message_stop'\n | 'content_block_start'\n | 'content_block_stop';\n\n/**\n * Event delta data (type-specific)\n */\nexport interface EventDelta {\n text?: string;\n data?: Uint8Array;\n toolCallId?: string;\n toolName?: string;\n argumentsJson?: string;\n}\n\n/**\n * A streaming event\n */\nexport interface StreamEvent {\n /** Event type */\n type: StreamEventType;\n\n /** Index of the content block this event belongs to */\n index: number;\n\n /** Event data (type-specific) */\n delta: EventDelta;\n}\n\n/**\n * Stream result - async iterable that also provides final turn\n */\nexport interface StreamResult<TData = unknown>\n extends AsyncIterable<StreamEvent> {\n /**\n * Get the complete Turn after streaming finishes.\n * Resolves when the stream completes.\n */\n readonly turn: Promise<Turn<TData>>;\n\n /** Abort the stream */\n abort(): void;\n}\n\n/**\n * Create a stream result from an async generator and completion promise\n */\nexport function createStreamResult<TData = unknown>(\n generator: AsyncGenerator<StreamEvent, void, unknown>,\n turnPromise: Promise<Turn<TData>>,\n abortController: AbortController\n): StreamResult<TData> {\n return {\n [Symbol.asyncIterator]() {\n return generator;\n },\n turn: turnPromise,\n abort() {\n abortController.abort();\n },\n };\n}\n\n/**\n * Create a text delta event\n */\nexport function textDelta(text: string, index = 0): StreamEvent {\n return {\n type: 'text_delta',\n index,\n delta: { text },\n };\n}\n\n/**\n * Create a tool call delta event\n */\nexport function toolCallDelta(\n toolCallId: string,\n toolName: string,\n argumentsJson: string,\n index = 0\n): StreamEvent {\n return {\n type: 'tool_call_delta',\n index,\n delta: { toolCallId, toolName, argumentsJson },\n };\n}\n\n/**\n * Create a message start event\n */\nexport function messageStart(): StreamEvent {\n return {\n type: 'message_start',\n index: 0,\n delta: {},\n };\n}\n\n/**\n * Create a message stop event\n */\nexport function messageStop(): StreamEvent {\n return {\n type: 'message_stop',\n index: 0,\n delta: {},\n };\n}\n\n/**\n * Create a content block start event\n */\nexport function contentBlockStart(index: number): StreamEvent {\n return {\n type: 'content_block_start',\n index,\n delta: {},\n };\n}\n\n/**\n * Create a content block stop event\n */\nexport function contentBlockStop(index: number): StreamEvent {\n return {\n type: 'content_block_stop',\n index,\n delta: {},\n };\n}\n","import type {\n LLMOptions,\n LLMInstance,\n LLMRequest,\n LLMResponse,\n InferenceInput,\n BoundLLMModel,\n LLMCapabilities,\n} from '../types/llm.ts';\nimport type { UserMessage, AssistantMessage } from '../types/messages.ts';\nimport type { ContentBlock, TextBlock } from '../types/content.ts';\nimport type { Tool, ToolExecution, ToolResult } from '../types/tool.ts';\nimport type { Turn, TokenUsage } from '../types/turn.ts';\nimport type { StreamResult, StreamEvent } from '../types/stream.ts';\nimport type { Thread } from '../types/thread.ts';\nimport type { ProviderConfig } from '../types/provider.ts';\nimport { UPPError } from '../types/errors.ts';\nimport {\n Message,\n UserMessage as UserMessageClass,\n ToolResultMessage,\n isUserMessage,\n isAssistantMessage,\n} from '../types/messages.ts';\nimport { createTurn, aggregateUsage, emptyUsage } from '../types/turn.ts';\nimport { createStreamResult } from '../types/stream.ts';\nimport { generateShortId } from '../utils/id.ts';\n\n/**\n * Default maximum iterations for tool execution\n */\nconst DEFAULT_MAX_ITERATIONS = 10;\n\n/**\n * Create an LLM instance\n */\nexport function llm<TParams = unknown>(\n options: LLMOptions<TParams>\n): LLMInstance<TParams> {\n const { model: modelRef, config = {}, params, system, tools, toolStrategy, structure } = options;\n\n // Validate that the provider supports LLM\n const provider = modelRef.provider;\n if (!provider.modalities.llm) {\n throw new UPPError(\n `Provider '${provider.name}' does not support LLM modality`,\n 'INVALID_REQUEST',\n provider.name,\n 'llm'\n );\n }\n\n // Bind the model\n const boundModel = provider.modalities.llm.bind(modelRef.modelId) as BoundLLMModel<TParams>;\n\n // Validate capabilities at bind time\n const capabilities = boundModel.capabilities;\n\n // Check for structured output capability\n if (structure && !capabilities.structuredOutput) {\n throw new UPPError(\n `Provider '${provider.name}' does not support structured output`,\n 'INVALID_REQUEST',\n provider.name,\n 'llm'\n );\n }\n\n // Check for tools capability\n if (tools && tools.length > 0 && !capabilities.tools) {\n throw new UPPError(\n `Provider '${provider.name}' does not support tools`,\n 'INVALID_REQUEST',\n provider.name,\n 'llm'\n );\n }\n\n // Build the instance\n const instance: LLMInstance<TParams> = {\n model: boundModel,\n system,\n params,\n capabilities,\n\n async generate(\n historyOrInput: Message[] | Thread | InferenceInput,\n ...inputs: InferenceInput[]\n ): Promise<Turn> {\n const { history, messages } = parseInputs(historyOrInput, inputs);\n return executeGenerate(\n boundModel,\n config,\n system,\n params,\n tools,\n toolStrategy,\n structure,\n history,\n messages\n );\n },\n\n stream(\n historyOrInput: Message[] | Thread | InferenceInput,\n ...inputs: InferenceInput[]\n ): StreamResult {\n // Check streaming capability\n if (!capabilities.streaming) {\n throw new UPPError(\n `Provider '${provider.name}' does not support streaming`,\n 'INVALID_REQUEST',\n provider.name,\n 'llm'\n );\n }\n const { history, messages } = parseInputs(historyOrInput, inputs);\n return executeStream(\n boundModel,\n config,\n system,\n params,\n tools,\n toolStrategy,\n structure,\n history,\n messages\n );\n },\n };\n\n return instance;\n}\n\n/**\n * Type guard to check if a value is a Message instance.\n * Uses instanceof for class instances, with fallback to timestamp check\n * for deserialized/reconstructed Message objects.\n */\nfunction isMessageInstance(value: unknown): value is Message {\n if (value instanceof Message) {\n return true;\n }\n // Fallback for deserialized Messages that aren't class instances:\n // Messages have 'timestamp' (Date), ContentBlocks don't\n if (\n typeof value === 'object' &&\n value !== null &&\n 'timestamp' in value &&\n 'type' in value &&\n 'id' in value\n ) {\n const obj = value as Record<string, unknown>;\n // Message types are 'user', 'assistant', 'tool_result'\n // ContentBlock types are 'text', 'image', 'audio', 'video', 'binary'\n const messageTypes = ['user', 'assistant', 'tool_result'];\n return messageTypes.includes(obj.type as string);\n }\n return false;\n}\n\n/**\n * Parse inputs to determine history and new messages\n */\nfunction parseInputs(\n historyOrInput: Message[] | Thread | InferenceInput,\n inputs: InferenceInput[]\n): { history: Message[]; messages: Message[] } {\n // Check if it's a Thread first (has 'messages' array property)\n if (\n typeof historyOrInput === 'object' &&\n historyOrInput !== null &&\n 'messages' in historyOrInput &&\n Array.isArray((historyOrInput as Thread).messages)\n ) {\n const thread = historyOrInput as Thread;\n const newMessages = inputs.map(inputToMessage);\n return { history: [...thread.messages], messages: newMessages };\n }\n\n // Check if first arg is Message[] (history)\n if (Array.isArray(historyOrInput) && historyOrInput.length > 0) {\n const first = historyOrInput[0];\n if (isMessageInstance(first)) {\n // It's history (Message[])\n const newMessages = inputs.map(inputToMessage);\n return { history: historyOrInput as Message[], messages: newMessages };\n }\n }\n\n // It's input (no history) - could be string, single Message, or ContentBlock\n const allInputs = [historyOrInput as InferenceInput, ...inputs];\n const newMessages = allInputs.map(inputToMessage);\n return { history: [], messages: newMessages };\n}\n\n/**\n * Convert an InferenceInput to a Message\n */\nfunction inputToMessage(input: InferenceInput): Message {\n if (typeof input === 'string') {\n return new UserMessageClass(input);\n }\n\n // It's already a Message\n if ('type' in input && 'id' in input && 'timestamp' in input) {\n return input as Message;\n }\n\n // It's a ContentBlock - wrap in UserMessage\n const block = input as ContentBlock;\n if (block.type === 'text') {\n return new UserMessageClass((block as TextBlock).text);\n }\n\n return new UserMessageClass([block as any]);\n}\n\n/**\n * Execute a non-streaming generate call with tool loop\n */\nasync function executeGenerate<TParams>(\n model: BoundLLMModel<TParams>,\n config: ProviderConfig,\n system: string | undefined,\n params: TParams | undefined,\n tools: Tool[] | undefined,\n toolStrategy: LLMOptions<TParams>['toolStrategy'],\n structure: LLMOptions<TParams>['structure'],\n history: Message[],\n newMessages: Message[]\n): Promise<Turn> {\n // Validate media capabilities for all input messages\n validateMediaCapabilities(\n [...history, ...newMessages],\n model.capabilities,\n model.provider.name\n );\n const maxIterations = toolStrategy?.maxIterations ?? DEFAULT_MAX_ITERATIONS;\n const allMessages: Message[] = [...history, ...newMessages];\n const toolExecutions: ToolExecution[] = [];\n const usages: TokenUsage[] = [];\n let cycles = 0;\n\n // Track structured data from responses (providers handle extraction)\n let structuredData: unknown;\n\n // Tool loop\n while (cycles < maxIterations + 1) {\n cycles++;\n\n const request: LLMRequest<TParams> = {\n messages: allMessages,\n system,\n params,\n tools,\n structure,\n config,\n };\n\n const response = await model.complete(request);\n usages.push(response.usage);\n allMessages.push(response.message);\n\n // Track structured data from provider (if present)\n if (response.data !== undefined) {\n structuredData = response.data;\n }\n\n // Check for tool calls\n if (response.message.hasToolCalls && tools && tools.length > 0) {\n // If provider already extracted structured data, don't try to execute tool calls\n // (some providers use tool calls internally for structured output)\n if (response.data !== undefined) {\n break;\n }\n\n // Check if we've hit max iterations (subtract 1 because we already incremented)\n if (cycles >= maxIterations) {\n await toolStrategy?.onMaxIterations?.(maxIterations);\n throw new UPPError(\n `Tool execution exceeded maximum iterations (${maxIterations})`,\n 'INVALID_REQUEST',\n model.provider.name,\n 'llm'\n );\n }\n\n // Execute tools\n const results = await executeTools(\n response.message,\n tools,\n toolStrategy,\n toolExecutions\n );\n\n // Add tool results\n allMessages.push(new ToolResultMessage(results));\n\n continue;\n }\n\n // No tool calls - we're done\n break;\n }\n\n // Use structured data from provider if structure was requested\n const data = structure ? structuredData : undefined;\n\n return createTurn(\n allMessages.slice(history.length), // Only messages from this turn\n toolExecutions,\n aggregateUsage(usages),\n cycles,\n data\n );\n}\n\n/**\n * Execute a streaming generate call with tool loop\n */\nfunction executeStream<TParams>(\n model: BoundLLMModel<TParams>,\n config: ProviderConfig,\n system: string | undefined,\n params: TParams | undefined,\n tools: Tool[] | undefined,\n toolStrategy: LLMOptions<TParams>['toolStrategy'],\n structure: LLMOptions<TParams>['structure'],\n history: Message[],\n newMessages: Message[]\n): StreamResult {\n // Validate media capabilities for all input messages\n validateMediaCapabilities(\n [...history, ...newMessages],\n model.capabilities,\n model.provider.name\n );\n\n const abortController = new AbortController();\n\n // Shared state between generator and turn promise\n const allMessages: Message[] = [...history, ...newMessages];\n const toolExecutions: ToolExecution[] = [];\n const usages: TokenUsage[] = [];\n let cycles = 0;\n let generatorError: Error | null = null;\n let structuredData: unknown; // Providers extract this\n\n // Deferred to signal when generator completes\n let resolveGenerator: () => void;\n let rejectGenerator: (error: Error) => void;\n const generatorDone = new Promise<void>((resolve, reject) => {\n resolveGenerator = resolve;\n rejectGenerator = reject;\n });\n\n const maxIterations = toolStrategy?.maxIterations ?? DEFAULT_MAX_ITERATIONS;\n\n // Create the async generator - this is the ONLY place that calls the API\n async function* generateStream(): AsyncGenerator<StreamEvent, void, unknown> {\n try {\n while (cycles < maxIterations + 1) {\n cycles++;\n\n const request: LLMRequest<TParams> = {\n messages: allMessages,\n system,\n params,\n tools,\n structure,\n config,\n signal: abortController.signal,\n };\n\n const streamResult = model.stream(request);\n\n // Forward stream events\n for await (const event of streamResult) {\n yield event;\n }\n\n // Get the response\n const response = await streamResult.response;\n usages.push(response.usage);\n allMessages.push(response.message);\n\n // Track structured data from provider (if present)\n if (response.data !== undefined) {\n structuredData = response.data;\n }\n\n // Check for tool calls\n if (response.message.hasToolCalls && tools && tools.length > 0) {\n // If provider already extracted structured data, don't try to execute tool calls\n // (some providers use tool calls internally for structured output)\n if (response.data !== undefined) {\n break;\n }\n\n if (cycles >= maxIterations) {\n await toolStrategy?.onMaxIterations?.(maxIterations);\n throw new UPPError(\n `Tool execution exceeded maximum iterations (${maxIterations})`,\n 'INVALID_REQUEST',\n model.provider.name,\n 'llm'\n );\n }\n\n // Execute tools\n const results = await executeTools(\n response.message,\n tools,\n toolStrategy,\n toolExecutions\n );\n\n // Add tool results\n allMessages.push(new ToolResultMessage(results));\n\n continue;\n }\n\n break;\n }\n resolveGenerator();\n } catch (error) {\n generatorError = error as Error;\n rejectGenerator(error as Error);\n throw error;\n }\n }\n\n // Turn promise waits for the generator to complete, then builds the Turn\n const turnPromise = (async (): Promise<Turn> => {\n await generatorDone;\n\n if (generatorError) {\n throw generatorError;\n }\n\n // Use structured data from provider if structure was requested\n const data = structure ? structuredData : undefined;\n\n return createTurn(\n allMessages.slice(history.length),\n toolExecutions,\n aggregateUsage(usages),\n cycles,\n data\n );\n })();\n\n return createStreamResult(generateStream(), turnPromise, abortController);\n}\n\n/**\n * Execute tools from an assistant message\n */\nasync function executeTools(\n message: AssistantMessage,\n tools: Tool[],\n toolStrategy: LLMOptions<unknown>['toolStrategy'],\n executions: ToolExecution[]\n): Promise<ToolResult[]> {\n const toolCalls = message.toolCalls ?? [];\n const results: ToolResult[] = [];\n\n // Build tool map\n const toolMap = new Map(tools.map((t) => [t.name, t]));\n\n // Execute tools (in parallel)\n const promises = toolCalls.map(async (call) => {\n const tool = toolMap.get(call.toolName);\n if (!tool) {\n return {\n toolCallId: call.toolCallId,\n result: `Tool '${call.toolName}' not found`,\n isError: true,\n };\n }\n\n const startTime = Date.now();\n\n // Notify strategy\n await toolStrategy?.onToolCall?.(tool, call.arguments);\n\n // Check before call\n if (toolStrategy?.onBeforeCall) {\n const shouldRun = await toolStrategy.onBeforeCall(tool, call.arguments);\n if (!shouldRun) {\n return {\n toolCallId: call.toolCallId,\n result: 'Tool execution skipped',\n isError: true,\n };\n }\n }\n\n // Check approval\n let approved = true;\n if (tool.approval) {\n try {\n approved = await tool.approval(call.arguments);\n } catch (error) {\n // Approval threw - propagate\n throw error;\n }\n }\n\n if (!approved) {\n const execution: ToolExecution = {\n toolName: tool.name,\n toolCallId: call.toolCallId,\n arguments: call.arguments,\n result: 'Tool execution denied',\n isError: true,\n duration: Date.now() - startTime,\n approved: false,\n };\n executions.push(execution);\n\n return {\n toolCallId: call.toolCallId,\n result: 'Tool execution denied by approval handler',\n isError: true,\n };\n }\n\n // Execute tool\n try {\n const result = await tool.run(call.arguments);\n\n await toolStrategy?.onAfterCall?.(tool, call.arguments, result);\n\n const execution: ToolExecution = {\n toolName: tool.name,\n toolCallId: call.toolCallId,\n arguments: call.arguments,\n result,\n isError: false,\n duration: Date.now() - startTime,\n approved,\n };\n executions.push(execution);\n\n return {\n toolCallId: call.toolCallId,\n result,\n isError: false,\n };\n } catch (error) {\n await toolStrategy?.onError?.(tool, call.arguments, error as Error);\n\n const errorMessage = error instanceof Error ? error.message : String(error);\n\n const execution: ToolExecution = {\n toolName: tool.name,\n toolCallId: call.toolCallId,\n arguments: call.arguments,\n result: errorMessage,\n isError: true,\n duration: Date.now() - startTime,\n approved,\n };\n executions.push(execution);\n\n return {\n toolCallId: call.toolCallId,\n result: errorMessage,\n isError: true,\n };\n }\n });\n\n results.push(...(await Promise.all(promises)));\n return results;\n}\n\n/**\n * Check if messages contain media that requires specific capabilities\n */\nfunction validateMediaCapabilities(\n messages: Message[],\n capabilities: LLMCapabilities,\n providerName: string\n): void {\n for (const msg of messages) {\n if (!isUserMessage(msg)) continue;\n\n for (const block of msg.content) {\n if (block.type === 'image' && !capabilities.imageInput) {\n throw new UPPError(\n `Provider '${providerName}' does not support image input`,\n 'INVALID_REQUEST',\n providerName,\n 'llm'\n );\n }\n if (block.type === 'video' && !capabilities.videoInput) {\n throw new UPPError(\n `Provider '${providerName}' does not support video input`,\n 'INVALID_REQUEST',\n providerName,\n 'llm'\n );\n }\n if (block.type === 'audio' && !capabilities.audioInput) {\n throw new UPPError(\n `Provider '${providerName}' does not support audio input`,\n 'INVALID_REQUEST',\n providerName,\n 'llm'\n );\n }\n }\n }\n}\n","import type { ImageSource, ImageBlock } from '../types/content.ts';\n\n/**\n * Image class for handling images in UPP\n */\nexport class Image {\n readonly source: ImageSource;\n readonly mimeType: string;\n readonly width?: number;\n readonly height?: number;\n\n private constructor(\n source: ImageSource,\n mimeType: string,\n width?: number,\n height?: number\n ) {\n this.source = source;\n this.mimeType = mimeType;\n this.width = width;\n this.height = height;\n }\n\n /**\n * Check if this image has data loaded (false for URL sources)\n */\n get hasData(): boolean {\n return this.source.type !== 'url';\n }\n\n /**\n * Convert to base64 string (throws if source is URL)\n */\n toBase64(): string {\n if (this.source.type === 'base64') {\n return this.source.data;\n }\n\n if (this.source.type === 'bytes') {\n return btoa(\n Array.from(this.source.data)\n .map((b) => String.fromCharCode(b))\n .join('')\n );\n }\n\n throw new Error('Cannot convert URL image to base64. Fetch the image first.');\n }\n\n /**\n * Convert to data URL (throws if source is URL)\n */\n toDataUrl(): string {\n const base64 = this.toBase64();\n return `data:${this.mimeType};base64,${base64}`;\n }\n\n /**\n * Get raw bytes (throws if source is URL)\n */\n toBytes(): Uint8Array {\n if (this.source.type === 'bytes') {\n return this.source.data;\n }\n\n if (this.source.type === 'base64') {\n const binaryString = atob(this.source.data);\n const bytes = new Uint8Array(binaryString.length);\n for (let i = 0; i < binaryString.length; i++) {\n bytes[i] = binaryString.charCodeAt(i);\n }\n return bytes;\n }\n\n throw new Error('Cannot get bytes from URL image. Fetch the image first.');\n }\n\n /**\n * Get the URL (only for URL sources)\n */\n toUrl(): string {\n if (this.source.type === 'url') {\n return this.source.url;\n }\n\n throw new Error('This image does not have a URL source.');\n }\n\n /**\n * Convert to ImageBlock for use in messages\n */\n toBlock(): ImageBlock {\n return {\n type: 'image',\n source: this.source,\n mimeType: this.mimeType,\n width: this.width,\n height: this.height,\n };\n }\n\n /**\n * Create from file path (reads file into memory)\n */\n static async fromPath(path: string): Promise<Image> {\n const file = Bun.file(path);\n const data = await file.arrayBuffer();\n const mimeType = file.type || detectMimeType(path);\n\n return new Image(\n { type: 'bytes', data: new Uint8Array(data) },\n mimeType\n );\n }\n\n /**\n * Create from URL reference (does not fetch - providers handle URL conversion)\n */\n static fromUrl(url: string, mimeType?: string): Image {\n const detected = mimeType || detectMimeTypeFromUrl(url);\n return new Image({ type: 'url', url }, detected);\n }\n\n /**\n * Create from raw bytes\n */\n static fromBytes(data: Uint8Array, mimeType: string): Image {\n return new Image({ type: 'bytes', data }, mimeType);\n }\n\n /**\n * Create from base64 string\n */\n static fromBase64(base64: string, mimeType: string): Image {\n return new Image({ type: 'base64', data: base64 }, mimeType);\n }\n\n /**\n * Create from an existing ImageBlock\n */\n static fromBlock(block: ImageBlock): Image {\n return new Image(\n block.source,\n block.mimeType,\n block.width,\n block.height\n );\n }\n}\n\n/**\n * Detect MIME type from file extension\n */\nfunction detectMimeType(path: string): string {\n const ext = path.split('.').pop()?.toLowerCase();\n\n switch (ext) {\n case 'jpg':\n case 'jpeg':\n return 'image/jpeg';\n case 'png':\n return 'image/png';\n case 'gif':\n return 'image/gif';\n case 'webp':\n return 'image/webp';\n case 'svg':\n return 'image/svg+xml';\n case 'bmp':\n return 'image/bmp';\n case 'ico':\n return 'image/x-icon';\n default:\n return 'application/octet-stream';\n }\n}\n\n/**\n * Detect MIME type from URL\n */\nfunction detectMimeTypeFromUrl(url: string): string {\n try {\n const pathname = new URL(url).pathname;\n return detectMimeType(pathname);\n } catch {\n return 'application/octet-stream';\n }\n}\n","/**\n * Content block types for messages\n */\n\n/**\n * Image source types\n */\nexport type ImageSource =\n | { type: 'base64'; data: string }\n | { type: 'url'; url: string }\n | { type: 'bytes'; data: Uint8Array };\n\n/**\n * Text content block\n */\nexport interface TextBlock {\n type: 'text';\n text: string;\n}\n\n/**\n * Image content block\n */\nexport interface ImageBlock {\n type: 'image';\n source: ImageSource;\n mimeType: string;\n width?: number;\n height?: number;\n}\n\n/**\n * Audio content block\n */\nexport interface AudioBlock {\n type: 'audio';\n data: Uint8Array;\n mimeType: string;\n duration?: number;\n}\n\n/**\n * Video content block\n */\nexport interface VideoBlock {\n type: 'video';\n data: Uint8Array;\n mimeType: string;\n duration?: number;\n width?: number;\n height?: number;\n}\n\n/**\n * Binary content block for arbitrary data\n */\nexport interface BinaryBlock {\n type: 'binary';\n data: Uint8Array;\n mimeType: string;\n metadata?: Record<string, unknown>;\n}\n\n/**\n * All content block types\n */\nexport type ContentBlock =\n | TextBlock\n | ImageBlock\n | AudioBlock\n | VideoBlock\n | BinaryBlock;\n\n/**\n * Content types allowed in user messages\n */\nexport type UserContent =\n | TextBlock\n | ImageBlock\n | AudioBlock\n | VideoBlock\n | BinaryBlock;\n\n/**\n * Content types allowed in assistant messages\n */\nexport type AssistantContent =\n | TextBlock\n | ImageBlock\n | AudioBlock\n | VideoBlock;\n\n/**\n * Helper to create a text block\n */\nexport function text(content: string): TextBlock {\n return { type: 'text', text: content };\n}\n\n/**\n * Type guard for TextBlock\n */\nexport function isTextBlock(block: ContentBlock): block is TextBlock {\n return block.type === 'text';\n}\n\n/**\n * Type guard for ImageBlock\n */\nexport function isImageBlock(block: ContentBlock): block is ImageBlock {\n return block.type === 'image';\n}\n\n/**\n * Type guard for AudioBlock\n */\nexport function isAudioBlock(block: ContentBlock): block is AudioBlock {\n return block.type === 'audio';\n}\n\n/**\n * Type guard for VideoBlock\n */\nexport function isVideoBlock(block: ContentBlock): block is VideoBlock {\n return block.type === 'video';\n}\n\n/**\n * Type guard for BinaryBlock\n */\nexport function isBinaryBlock(block: ContentBlock): block is BinaryBlock {\n return block.type === 'binary';\n}\n","import { generateId } from '../utils/id.ts';\nimport {\n Message,\n UserMessage,\n AssistantMessage,\n ToolResultMessage,\n} from './messages.ts';\nimport type { MessageType, MessageMetadata } from './messages.ts';\nimport type { ContentBlock, UserContent, AssistantContent } from './content.ts';\nimport type { Turn } from './turn.ts';\nimport type { ToolCall, ToolResult } from './tool.ts';\n\n/**\n * Serialized message format\n */\nexport interface MessageJSON {\n id: string;\n type: MessageType;\n content: ContentBlock[];\n toolCalls?: ToolCall[];\n results?: ToolResult[];\n metadata?: MessageMetadata;\n timestamp: string;\n}\n\n/**\n * Serialized thread format\n */\nexport interface ThreadJSON {\n id: string;\n messages: MessageJSON[];\n createdAt: string;\n updatedAt: string;\n}\n\n/**\n * Thread - A utility class for managing conversation history\n * Users control their own history; Thread is optional\n */\nexport class Thread {\n /** Unique thread identifier */\n readonly id: string;\n\n /** Internal message storage */\n private _messages: Message[];\n\n /** Creation timestamp */\n private _createdAt: Date;\n\n /** Last update timestamp */\n private _updatedAt: Date;\n\n /**\n * Create a new thread, optionally with initial messages\n */\n constructor(messages?: Message[]) {\n this.id = generateId();\n this._messages = messages ? [...messages] : [];\n this._createdAt = new Date();\n this._updatedAt = new Date();\n }\n\n /** All messages in the thread (readonly) */\n get messages(): readonly Message[] {\n return this._messages;\n }\n\n /** Number of messages */\n get length(): number {\n return this._messages.length;\n }\n\n /**\n * Append messages from a turn\n */\n append(turn: Turn): this {\n this._messages.push(...turn.messages);\n this._updatedAt = new Date();\n return this;\n }\n\n /**\n * Add raw messages\n */\n push(...messages: Message[]): this {\n this._messages.push(...messages);\n this._updatedAt = new Date();\n return this;\n }\n\n /**\n * Add a user message\n */\n user(content: string | UserContent[]): this {\n this._messages.push(new UserMessage(content));\n this._updatedAt = new Date();\n return this;\n }\n\n /**\n * Add an assistant message\n */\n assistant(content: string | AssistantContent[]): this {\n this._messages.push(new AssistantMessage(content));\n this._updatedAt = new Date();\n return this;\n }\n\n /**\n * Get messages by type\n */\n filter(type: MessageType): Message[] {\n return this._messages.filter((m) => m.type === type);\n }\n\n /**\n * Get the last N messages\n */\n tail(count: number): Message[] {\n return this._messages.slice(-count);\n }\n\n /**\n * Create a new thread with a subset of messages\n */\n slice(start?: number, end?: number): Thread {\n return new Thread(this._messages.slice(start, end));\n }\n\n /**\n * Clear all messages\n */\n clear(): this {\n this._messages = [];\n this._updatedAt = new Date();\n return this;\n }\n\n /**\n * Convert to plain message array\n */\n toMessages(): Message[] {\n return [...this._messages];\n }\n\n /**\n * Serialize to JSON\n */\n toJSON(): ThreadJSON {\n return {\n id: this.id,\n messages: this._messages.map((m) => this.messageToJSON(m)),\n createdAt: this._createdAt.toISOString(),\n updatedAt: this._updatedAt.toISOString(),\n };\n }\n\n /**\n * Deserialize from JSON\n */\n static fromJSON(json: ThreadJSON): Thread {\n const messages = json.messages.map((m) => Thread.messageFromJSON(m));\n const thread = new Thread(messages);\n // Override the generated id with the serialized one\n (thread as { id: string }).id = json.id;\n thread._createdAt = new Date(json.createdAt);\n thread._updatedAt = new Date(json.updatedAt);\n return thread;\n }\n\n /**\n * Iterate over messages\n */\n [Symbol.iterator](): Iterator<Message> {\n return this._messages[Symbol.iterator]();\n }\n\n /**\n * Convert a message to JSON\n */\n private messageToJSON(m: Message): MessageJSON {\n const base: MessageJSON = {\n id: m.id,\n type: m.type,\n content: [],\n metadata: m.metadata,\n timestamp: m.timestamp.toISOString(),\n };\n\n if (m instanceof UserMessage) {\n base.content = m.content;\n } else if (m instanceof AssistantMessage) {\n base.content = m.content;\n base.toolCalls = m.toolCalls;\n } else if (m instanceof ToolResultMessage) {\n base.results = m.results;\n }\n\n return base;\n }\n\n /**\n * Reconstruct a message from JSON\n */\n private static messageFromJSON(json: MessageJSON): Message {\n const options = {\n id: json.id,\n metadata: json.metadata,\n };\n\n switch (json.type) {\n case 'user':\n return new UserMessage(json.content as UserContent[], options);\n case 'assistant':\n return new AssistantMessage(\n json.content as AssistantContent[],\n json.toolCalls,\n options\n );\n case 'tool_result':\n return new ToolResultMessage(json.results ?? [], options);\n default:\n throw new Error(`Unknown message type: ${json.type}`);\n }\n }\n}\n","// Core entry points\nexport { llm } from './core/llm.ts';\nexport { createProvider } from './core/provider.ts';\nexport { Image } from './core/image.ts';\n\n// Namespace object for alternative import style\nimport { llm } from './core/llm.ts';\n\n/**\n * UPP namespace object\n * Provides ai.llm(), ai.embedding(), ai.image() style access\n */\nexport const ai = {\n llm,\n // embedding, // Coming soon\n // image, // Coming soon\n};\n\n// Re-export all types from types/index.ts\nexport * from './types/index.ts';\n\n// Re-export HTTP utilities\nexport {\n RoundRobinKeys,\n WeightedKeys,\n DynamicKey,\n ExponentialBackoff,\n LinearBackoff,\n NoRetry,\n TokenBucket,\n RetryAfterStrategy,\n} from './http/index.ts';\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwDO,SAAS,WACd,UACA,gBACA,OACA,QACA,MACa;AAEb,QAAM,WAAW,SACd,OAAO,CAAC,MAA6B,EAAE,SAAS,WAAW,EAC3D,IAAI;AAEP,MAAI,CAAC,UAAU;AACb,UAAM,IAAI,MAAM,kDAAkD;AAAA,EACpE;AAEA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;AAKO,SAAS,aAAyB;AACvC,SAAO;AAAA,IACL,aAAa;AAAA,IACb,cAAc;AAA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1,108 @@
1
+ import { b as Provider } from '../provider-CUJWjgNl.js';
2
+
3
+ /**
4
+ * Ollama-specific LLM parameters
5
+ * These map to Ollama's runtime options
6
+ */
7
+ interface OllamaLLMParams {
8
+ /** Maximum number of tokens to predict (default: -1 = infinite) */
9
+ num_predict?: number;
10
+ /** Temperature for randomness (default: 0.8) */
11
+ temperature?: number;
12
+ /** Top-p (nucleus) sampling (default: 0.9) */
13
+ top_p?: number;
14
+ /** Top-k sampling (default: 40) */
15
+ top_k?: number;
16
+ /** Minimum probability for a token to be considered (default: 0.0) */
17
+ min_p?: number;
18
+ /** Typical p sampling (default: 1.0 = disabled) */
19
+ typical_p?: number;
20
+ /** Repeat penalty (default: 1.1) */
21
+ repeat_penalty?: number;
22
+ /** Number of tokens to look back for repeat penalty (default: 64) */
23
+ repeat_last_n?: number;
24
+ /** Presence penalty (default: 0.0) */
25
+ presence_penalty?: number;
26
+ /** Frequency penalty (default: 0.0) */
27
+ frequency_penalty?: number;
28
+ /** Mirostat sampling mode (0 = disabled, 1 = Mirostat, 2 = Mirostat 2.0) */
29
+ mirostat?: 0 | 1 | 2;
30
+ /** Mirostat learning rate (default: 0.1) */
31
+ mirostat_eta?: number;
32
+ /** Mirostat target entropy (default: 5.0) */
33
+ mirostat_tau?: number;
34
+ /** Penalize newlines (default: true) */
35
+ penalize_newline?: boolean;
36
+ /** Stop sequences */
37
+ stop?: string[];
38
+ /** Seed for deterministic sampling (default: random) */
39
+ seed?: number;
40
+ /** Number of tokens to keep from initial prompt (default: 4) */
41
+ num_keep?: number;
42
+ /** Context window size (default: model-dependent) */
43
+ num_ctx?: number;
44
+ /** Number of batches (default: 512) */
45
+ num_batch?: number;
46
+ /** Number of threads (default: auto) */
47
+ num_thread?: number;
48
+ /** Number of layers to offload to GPU (default: auto) */
49
+ num_gpu?: number;
50
+ /** Main GPU to use (default: 0) */
51
+ main_gpu?: number;
52
+ /** Enable low VRAM mode */
53
+ low_vram?: boolean;
54
+ /** Enable f16 KV cache */
55
+ f16_kv?: boolean;
56
+ /** Use mmap for model loading */
57
+ use_mmap?: boolean;
58
+ /** Use mlock for memory locking */
59
+ use_mlock?: boolean;
60
+ /** Vocabulary only mode */
61
+ vocab_only?: boolean;
62
+ /** NUMA support */
63
+ numa?: boolean;
64
+ /** TFS-Z sampling (default: 1.0 = disabled) */
65
+ tfs_z?: number;
66
+ /** Enable thinking mode (for models that support it) */
67
+ think?: boolean | 'high' | 'medium' | 'low';
68
+ /** Keep model loaded in memory (string duration like "5m" or number of seconds) */
69
+ keep_alive?: string | number;
70
+ /** Return log probabilities */
71
+ logprobs?: boolean;
72
+ /** Number of top log probabilities to return */
73
+ top_logprobs?: number;
74
+ }
75
+
76
+ /**
77
+ * Ollama provider
78
+ * Supports LLM modality with local Ollama models
79
+ *
80
+ * Ollama is a local LLM server that supports many open-source models including:
81
+ * - Llama 3.x
82
+ * - Mistral
83
+ * - Mixtral
84
+ * - Gemma
85
+ * - Qwen
86
+ * - DeepSeek
87
+ * - Phi
88
+ * - And many more
89
+ *
90
+ * @example
91
+ * ```ts
92
+ * import { llm } from 'provider-protocol';
93
+ * import { ollama } from 'provider-protocol/ollama';
94
+ *
95
+ * const model = llm(ollama('llama3.2'));
96
+ * const result = await model.generate('Hello, how are you?');
97
+ * ```
98
+ *
99
+ * @example Custom server URL
100
+ * ```ts
101
+ * const model = llm(ollama('llama3.2'), {
102
+ * baseUrl: 'http://my-ollama-server:11434',
103
+ * });
104
+ * ```
105
+ */
106
+ declare const ollama: Provider<unknown>;
107
+
108
+ export { type OllamaLLMParams, ollama };