@providerprotocol/ai 0.0.2 → 0.0.4

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/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @providerprotocol/ai
2
2
 
3
- Unified Provider Protocol (UPP-1.1) implementation for AI inference across multiple providers.
3
+ Unified Provider Protocol (UPP-1.2) implementation for AI inference across multiple providers.
4
4
 
5
5
  ## Install
6
6
 
@@ -15,6 +15,8 @@ import { llm } from '@providerprotocol/ai';
15
15
  import { anthropic } from '@providerprotocol/ai/anthropic';
16
16
  import { openai } from '@providerprotocol/ai/openai';
17
17
  import { google } from '@providerprotocol/ai/google';
18
+ import { ollama } from '@providerprotocol/ai/ollama';
19
+ import { openrouter } from '@providerprotocol/ai/openrouter';
18
20
 
19
21
  // Simple generation
20
22
  const claude = llm({ model: anthropic('claude-sonnet-4-20250514') });
@@ -61,6 +63,8 @@ console.log(turn.data); // { name: 'John', age: 30 }
61
63
  | Anthropic | `@providerprotocol/ai/anthropic` |
62
64
  | OpenAI | `@providerprotocol/ai/openai` |
63
65
  | Google | `@providerprotocol/ai/google` |
66
+ | Ollama | `@providerprotocol/ai/ollama` |
67
+ | OpenRouter | `@providerprotocol/ai/openrouter` |
64
68
 
65
69
  ## Configuration
66
70
 
@@ -5,8 +5,8 @@ import { b as Provider } from '../provider-CUJWjgNl.js';
5
5
  * These are passed through to the Anthropic Messages API
6
6
  */
7
7
  interface AnthropicLLMParams {
8
- /** Maximum number of tokens to generate (required by Anthropic) */
9
- max_tokens: number;
8
+ /** Maximum number of tokens to generate (required by Anthropic API) */
9
+ max_tokens?: number;
10
10
  /** Temperature for randomness (0.0 - 1.0) */
11
11
  temperature?: number;
12
12
  /** Top-p (nucleus) sampling */
@@ -20,12 +20,14 @@ import {
20
20
 
21
21
  // src/providers/anthropic/transform.ts
22
22
  function transformRequest(request, modelId) {
23
- const params = request.params ?? { max_tokens: 4096 };
23
+ const params = request.params ?? {};
24
24
  const anthropicRequest = {
25
25
  model: modelId,
26
- max_tokens: params.max_tokens ?? 4096,
27
26
  messages: request.messages.map(transformMessage)
28
27
  };
28
+ if (params.max_tokens !== void 0) {
29
+ anthropicRequest.max_tokens = params.max_tokens;
30
+ }
29
31
  if (request.system) {
30
32
  anthropicRequest.system = request.system;
31
33
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/providers/anthropic/transform.ts","../../src/providers/anthropic/llm.ts","../../src/providers/anthropic/index.ts"],"sourcesContent":["import type { LLMRequest, LLMResponse } from '../../types/llm.ts';\nimport type { Message } from '../../types/messages.ts';\nimport type { StreamEvent } from '../../types/stream.ts';\nimport type { Tool, ToolCall } from '../../types/tool.ts';\nimport type { TokenUsage } from '../../types/turn.ts';\nimport type { ContentBlock, TextBlock, ImageBlock } from '../../types/content.ts';\nimport {\n AssistantMessage,\n UserMessage,\n ToolResultMessage,\n isUserMessage,\n isAssistantMessage,\n isToolResultMessage,\n} from '../../types/messages.ts';\nimport type {\n AnthropicLLMParams,\n AnthropicRequest,\n AnthropicMessage,\n AnthropicContent,\n AnthropicTool,\n AnthropicResponse,\n AnthropicStreamEvent,\n AnthropicContentBlockDeltaEvent,\n} from './types.ts';\n\n/**\n * Transform UPP request to Anthropic format\n */\nexport function transformRequest<TParams extends AnthropicLLMParams>(\n request: LLMRequest<TParams>,\n modelId: string\n): AnthropicRequest {\n const params = (request.params ?? { max_tokens: 4096 }) as AnthropicLLMParams;\n\n const anthropicRequest: AnthropicRequest = {\n model: modelId,\n max_tokens: params.max_tokens ?? 4096,\n messages: request.messages.map(transformMessage),\n };\n\n // System prompt (top-level in Anthropic)\n if (request.system) {\n anthropicRequest.system = request.system;\n }\n\n // Model parameters\n if (params.temperature !== undefined) {\n anthropicRequest.temperature = params.temperature;\n }\n if (params.top_p !== undefined) {\n anthropicRequest.top_p = params.top_p;\n }\n if (params.top_k !== undefined) {\n anthropicRequest.top_k = params.top_k;\n }\n if (params.stop_sequences) {\n anthropicRequest.stop_sequences = params.stop_sequences;\n }\n if (params.metadata) {\n anthropicRequest.metadata = params.metadata;\n }\n if (params.thinking) {\n anthropicRequest.thinking = params.thinking;\n }\n if (params.service_tier !== undefined) {\n anthropicRequest.service_tier = params.service_tier;\n }\n\n // Tools\n if (request.tools && request.tools.length > 0) {\n anthropicRequest.tools = request.tools.map(transformTool);\n anthropicRequest.tool_choice = { type: 'auto' };\n }\n\n // Structured output via tool-based approach\n // Anthropic doesn't have native structured output, so we use a tool to enforce the schema\n if (request.structure) {\n const structuredTool: AnthropicTool = {\n name: 'json_response',\n description: 'Return the response in the specified JSON format. You MUST use this tool to provide your response.',\n input_schema: {\n type: 'object',\n properties: request.structure.properties,\n required: request.structure.required,\n },\n };\n\n // Add the structured output tool (may coexist with user tools)\n anthropicRequest.tools = [...(anthropicRequest.tools ?? []), structuredTool];\n // Force the model to use the json_response tool\n anthropicRequest.tool_choice = { type: 'tool', name: 'json_response' };\n }\n\n return anthropicRequest;\n}\n\n/**\n * Filter to only valid content blocks with a type property\n */\nfunction filterValidContent<T extends { type?: string }>(content: T[]): T[] {\n return content.filter((c) => c && typeof c.type === 'string');\n}\n\n/**\n * Transform a UPP Message to Anthropic format\n */\nfunction transformMessage(message: Message): AnthropicMessage {\n if (isUserMessage(message)) {\n const validContent = filterValidContent(message.content);\n return {\n role: 'user',\n content: validContent.map(transformContentBlock),\n };\n }\n\n if (isAssistantMessage(message)) {\n const validContent = filterValidContent(message.content);\n const content: AnthropicContent[] = validContent.map(transformContentBlock);\n\n // Add tool calls as tool_use content blocks\n if (message.toolCalls) {\n for (const call of message.toolCalls) {\n content.push({\n type: 'tool_use',\n id: call.toolCallId,\n name: call.toolName,\n input: call.arguments,\n });\n }\n }\n\n return {\n role: 'assistant',\n content,\n };\n }\n\n if (isToolResultMessage(message)) {\n // Tool results are sent as user messages with tool_result content\n return {\n role: 'user',\n content: message.results.map((result) => ({\n type: 'tool_result' as const,\n tool_use_id: result.toolCallId,\n content:\n typeof result.result === 'string'\n ? result.result\n : JSON.stringify(result.result),\n is_error: result.isError,\n })),\n };\n }\n\n throw new Error(`Unknown message type: ${message.type}`);\n}\n\n/**\n * Transform a content block to Anthropic format\n */\nfunction transformContentBlock(block: ContentBlock): AnthropicContent {\n switch (block.type) {\n case 'text':\n return { type: 'text', text: block.text };\n\n case 'image': {\n const imageBlock = block as ImageBlock;\n if (imageBlock.source.type === 'base64') {\n return {\n type: 'image',\n source: {\n type: 'base64',\n media_type: imageBlock.mimeType,\n data: imageBlock.source.data,\n },\n };\n }\n if (imageBlock.source.type === 'url') {\n return {\n type: 'image',\n source: {\n type: 'url',\n url: imageBlock.source.url,\n },\n };\n }\n if (imageBlock.source.type === 'bytes') {\n // Convert bytes to base64\n const base64 = btoa(\n Array.from(imageBlock.source.data)\n .map((b) => String.fromCharCode(b))\n .join('')\n );\n return {\n type: 'image',\n source: {\n type: 'base64',\n media_type: imageBlock.mimeType,\n data: base64,\n },\n };\n }\n throw new Error(`Unknown image source type`);\n }\n\n default:\n throw new Error(`Unsupported content type: ${block.type}`);\n }\n}\n\n/**\n * Transform a UPP Tool to Anthropic format\n */\nfunction transformTool(tool: Tool): AnthropicTool {\n return {\n name: tool.name,\n description: tool.description,\n input_schema: {\n type: 'object',\n properties: tool.parameters.properties,\n required: tool.parameters.required,\n },\n };\n}\n\n/**\n * Transform Anthropic response to UPP LLMResponse\n */\nexport function transformResponse(data: AnthropicResponse): LLMResponse {\n // Extract text content\n const textContent: TextBlock[] = [];\n const toolCalls: ToolCall[] = [];\n let structuredData: unknown;\n\n for (const block of data.content) {\n if (block.type === 'text') {\n textContent.push({ type: 'text', text: block.text });\n } else if (block.type === 'tool_use') {\n // Check if this is the json_response tool (structured output)\n if (block.name === 'json_response') {\n // Extract structured data from tool arguments\n structuredData = block.input;\n }\n toolCalls.push({\n toolCallId: block.id,\n toolName: block.name,\n arguments: block.input,\n });\n }\n // Skip thinking blocks for now\n }\n\n const message = new AssistantMessage(\n textContent,\n toolCalls.length > 0 ? toolCalls : undefined,\n {\n id: data.id,\n metadata: {\n anthropic: {\n stop_reason: data.stop_reason,\n stop_sequence: data.stop_sequence,\n model: data.model,\n },\n },\n }\n );\n\n const usage: TokenUsage = {\n inputTokens: data.usage.input_tokens,\n outputTokens: data.usage.output_tokens,\n totalTokens: data.usage.input_tokens + data.usage.output_tokens,\n };\n\n return {\n message,\n usage,\n stopReason: data.stop_reason ?? 'end_turn',\n data: structuredData,\n };\n}\n\n/**\n * State for accumulating streaming response\n */\nexport interface StreamState {\n messageId: string;\n model: string;\n content: Array<{ type: string; text?: string; id?: string; name?: string; input?: string }>;\n stopReason: string | null;\n inputTokens: number;\n outputTokens: number;\n}\n\n/**\n * Create initial stream state\n */\nexport function createStreamState(): StreamState {\n return {\n messageId: '',\n model: '',\n content: [],\n stopReason: null,\n inputTokens: 0,\n outputTokens: 0,\n };\n}\n\n/**\n * Transform Anthropic stream event to UPP StreamEvent\n * Returns null for events that don't produce UPP events\n */\nexport function transformStreamEvent(\n event: AnthropicStreamEvent,\n state: StreamState\n): StreamEvent | null {\n switch (event.type) {\n case 'message_start':\n state.messageId = event.message.id;\n state.model = event.message.model;\n state.inputTokens = event.message.usage.input_tokens;\n return { type: 'message_start', index: 0, delta: {} };\n\n case 'content_block_start':\n // Initialize content block\n if (event.content_block.type === 'text') {\n state.content[event.index] = { type: 'text', text: '' };\n } else if (event.content_block.type === 'tool_use') {\n state.content[event.index] = {\n type: 'tool_use',\n id: event.content_block.id,\n name: event.content_block.name,\n input: '',\n };\n }\n return { type: 'content_block_start', index: event.index, delta: {} };\n\n case 'content_block_delta': {\n const delta = event.delta;\n if (delta.type === 'text_delta') {\n if (state.content[event.index]) {\n state.content[event.index]!.text =\n (state.content[event.index]!.text ?? '') + delta.text;\n }\n return {\n type: 'text_delta',\n index: event.index,\n delta: { text: delta.text },\n };\n }\n if (delta.type === 'input_json_delta') {\n if (state.content[event.index]) {\n state.content[event.index]!.input =\n (state.content[event.index]!.input ?? '') + delta.partial_json;\n }\n return {\n type: 'tool_call_delta',\n index: event.index,\n delta: {\n argumentsJson: delta.partial_json,\n toolCallId: state.content[event.index]?.id,\n toolName: state.content[event.index]?.name,\n },\n };\n }\n if (delta.type === 'thinking_delta') {\n return {\n type: 'reasoning_delta',\n index: event.index,\n delta: { text: delta.thinking },\n };\n }\n return null;\n }\n\n case 'content_block_stop':\n return { type: 'content_block_stop', index: event.index, delta: {} };\n\n case 'message_delta':\n state.stopReason = event.delta.stop_reason;\n state.outputTokens = event.usage.output_tokens;\n return null;\n\n case 'message_stop':\n return { type: 'message_stop', index: 0, delta: {} };\n\n case 'ping':\n case 'error':\n return null;\n\n default:\n return null;\n }\n}\n\n/**\n * Build LLMResponse from accumulated stream state\n */\nexport function buildResponseFromState(state: StreamState): LLMResponse {\n const textContent: TextBlock[] = [];\n const toolCalls: ToolCall[] = [];\n let structuredData: unknown;\n\n for (const block of state.content) {\n if (block.type === 'text' && block.text) {\n textContent.push({ type: 'text', text: block.text });\n } else if (block.type === 'tool_use' && block.id && block.name) {\n let args: Record<string, unknown> = {};\n if (block.input) {\n try {\n args = JSON.parse(block.input);\n } catch {\n // Invalid JSON - use empty object\n }\n }\n // Check if this is the json_response tool (structured output)\n if (block.name === 'json_response') {\n structuredData = args;\n }\n toolCalls.push({\n toolCallId: block.id,\n toolName: block.name,\n arguments: args,\n });\n }\n }\n\n const message = new AssistantMessage(\n textContent,\n toolCalls.length > 0 ? toolCalls : undefined,\n {\n id: state.messageId,\n metadata: {\n anthropic: {\n stop_reason: state.stopReason,\n model: state.model,\n },\n },\n }\n );\n\n const usage: TokenUsage = {\n inputTokens: state.inputTokens,\n outputTokens: state.outputTokens,\n totalTokens: state.inputTokens + state.outputTokens,\n };\n\n return {\n message,\n usage,\n stopReason: state.stopReason ?? 'end_turn',\n data: structuredData,\n };\n}\n","import type { LLMHandler, BoundLLMModel, LLMRequest, LLMResponse, LLMStreamResult, LLMCapabilities } from '../../types/llm.ts';\nimport type { StreamEvent } from '../../types/stream.ts';\nimport type { LLMProvider } from '../../types/provider.ts';\nimport { UPPError } from '../../types/errors.ts';\nimport { resolveApiKey } from '../../http/keys.ts';\nimport { doFetch, doStreamFetch } from '../../http/fetch.ts';\nimport { parseSSEStream } from '../../http/sse.ts';\nimport { normalizeHttpError } from '../../http/errors.ts';\nimport type { AnthropicLLMParams, AnthropicResponse, AnthropicStreamEvent } from './types.ts';\nimport {\n transformRequest,\n transformResponse,\n transformStreamEvent,\n createStreamState,\n buildResponseFromState,\n} from './transform.ts';\n\nconst ANTHROPIC_API_URL = 'https://api.anthropic.com/v1/messages';\nconst ANTHROPIC_VERSION = '2023-06-01';\n\n/**\n * Anthropic API capabilities\n */\nconst ANTHROPIC_CAPABILITIES: LLMCapabilities = {\n streaming: true,\n tools: true,\n structuredOutput: true,\n imageInput: true,\n videoInput: false,\n audioInput: false,\n};\n\n/**\n * Create Anthropic LLM handler\n */\nexport function createLLMHandler(): LLMHandler<AnthropicLLMParams> {\n // Provider reference injected by createProvider() after construction\n let providerRef: LLMProvider<AnthropicLLMParams> | null = null;\n\n return {\n _setProvider(provider: LLMProvider<AnthropicLLMParams>) {\n providerRef = provider;\n },\n\n bind(modelId: string): BoundLLMModel<AnthropicLLMParams> {\n // Use the injected provider reference (set by createProvider)\n if (!providerRef) {\n throw new UPPError(\n 'Provider reference not set. Handler must be used with createProvider().',\n 'INVALID_REQUEST',\n 'anthropic',\n 'llm'\n );\n }\n\n const model: BoundLLMModel<AnthropicLLMParams> = {\n modelId,\n capabilities: ANTHROPIC_CAPABILITIES,\n\n get provider(): LLMProvider<AnthropicLLMParams> {\n return providerRef!;\n },\n\n async complete(request: LLMRequest<AnthropicLLMParams>): Promise<LLMResponse> {\n const apiKey = await resolveApiKey(\n request.config,\n 'ANTHROPIC_API_KEY',\n 'anthropic',\n 'llm'\n );\n\n const baseUrl = request.config.baseUrl ?? ANTHROPIC_API_URL;\n const body = transformRequest(request, modelId);\n\n const response = await doFetch(\n baseUrl,\n {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n 'x-api-key': apiKey,\n 'anthropic-version': request.config.apiVersion ?? ANTHROPIC_VERSION,\n },\n body: JSON.stringify(body),\n signal: request.signal,\n },\n request.config,\n 'anthropic',\n 'llm'\n );\n\n const data = (await response.json()) as AnthropicResponse;\n return transformResponse(data);\n },\n\n stream(request: LLMRequest<AnthropicLLMParams>): LLMStreamResult {\n const state = createStreamState();\n let responseResolve: (value: LLMResponse) => void;\n let responseReject: (error: Error) => void;\n\n const responsePromise = new Promise<LLMResponse>((resolve, reject) => {\n responseResolve = resolve;\n responseReject = reject;\n });\n\n async function* generateEvents(): AsyncGenerator<StreamEvent, void, unknown> {\n try {\n const apiKey = await resolveApiKey(\n request.config,\n 'ANTHROPIC_API_KEY',\n 'anthropic',\n 'llm'\n );\n\n const baseUrl = request.config.baseUrl ?? ANTHROPIC_API_URL;\n const body = transformRequest(request, modelId);\n body.stream = true;\n\n const response = await doStreamFetch(\n baseUrl,\n {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n 'x-api-key': apiKey,\n 'anthropic-version': request.config.apiVersion ?? ANTHROPIC_VERSION,\n },\n body: JSON.stringify(body),\n signal: request.signal,\n },\n request.config,\n 'anthropic',\n 'llm'\n );\n\n if (!response.ok) {\n const error = await normalizeHttpError(response, 'anthropic', 'llm');\n responseReject(error);\n throw error;\n }\n\n if (!response.body) {\n const error = new UPPError(\n 'No response body for streaming request',\n 'PROVIDER_ERROR',\n 'anthropic',\n 'llm'\n );\n responseReject(error);\n throw error;\n }\n\n for await (const data of parseSSEStream(response.body)) {\n // Check for Anthropic error event\n if (typeof data === 'object' && data !== null && 'type' in data) {\n const event = data as AnthropicStreamEvent;\n\n if (event.type === 'error') {\n const error = new UPPError(\n event.error.message,\n 'PROVIDER_ERROR',\n 'anthropic',\n 'llm'\n );\n responseReject(error);\n throw error;\n }\n\n const uppEvent = transformStreamEvent(event, state);\n if (uppEvent) {\n yield uppEvent;\n }\n }\n }\n\n // Build final response\n responseResolve(buildResponseFromState(state));\n } catch (error) {\n responseReject(error as Error);\n throw error;\n }\n }\n\n return {\n [Symbol.asyncIterator]() {\n return generateEvents();\n },\n response: responsePromise,\n };\n },\n };\n\n return model;\n },\n };\n}\n","import { createProvider } from '../../core/provider.ts';\nimport { createLLMHandler } from './llm.ts';\n\n/**\n * Anthropic provider\n * Supports LLM modality with Claude models\n */\nexport const anthropic = createProvider({\n name: 'anthropic',\n version: '1.0.0',\n modalities: {\n llm: createLLMHandler(),\n },\n});\n\n// Re-export types\nexport type { AnthropicLLMParams } from './types.ts';\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AA4BO,SAAS,iBACd,SACA,SACkB;AAClB,QAAM,SAAU,QAAQ,UAAU,EAAE,YAAY,KAAK;AAErD,QAAM,mBAAqC;AAAA,IACzC,OAAO;AAAA,IACP,YAAY,OAAO,cAAc;AAAA,IACjC,UAAU,QAAQ,SAAS,IAAI,gBAAgB;AAAA,EACjD;AAGA,MAAI,QAAQ,QAAQ;AAClB,qBAAiB,SAAS,QAAQ;AAAA,EACpC;AAGA,MAAI,OAAO,gBAAgB,QAAW;AACpC,qBAAiB,cAAc,OAAO;AAAA,EACxC;AACA,MAAI,OAAO,UAAU,QAAW;AAC9B,qBAAiB,QAAQ,OAAO;AAAA,EAClC;AACA,MAAI,OAAO,UAAU,QAAW;AAC9B,qBAAiB,QAAQ,OAAO;AAAA,EAClC;AACA,MAAI,OAAO,gBAAgB;AACzB,qBAAiB,iBAAiB,OAAO;AAAA,EAC3C;AACA,MAAI,OAAO,UAAU;AACnB,qBAAiB,WAAW,OAAO;AAAA,EACrC;AACA,MAAI,OAAO,UAAU;AACnB,qBAAiB,WAAW,OAAO;AAAA,EACrC;AACA,MAAI,OAAO,iBAAiB,QAAW;AACrC,qBAAiB,eAAe,OAAO;AAAA,EACzC;AAGA,MAAI,QAAQ,SAAS,QAAQ,MAAM,SAAS,GAAG;AAC7C,qBAAiB,QAAQ,QAAQ,MAAM,IAAI,aAAa;A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kBAChB;AAAA,kBACA;AAAA,kBACA;AAAA,kBACA;AAAA,gBACF;AACA,+BAAe,KAAK;AACpB,sBAAM;AAAA,cACR;AAEA,+BAAiB,QAAQ,eAAe,SAAS,IAAI,GAAG;AAEtD,oBAAI,OAAO,SAAS,YAAY,SAAS,QAAQ,UAAU,MAAM;AAC/D,wBAAM,QAAQ;AAEd,sBAAI,MAAM,SAAS,SAAS;AAC1B,0BAAM,QAAQ,IAAI;AAAA,sBAChB,MAAM,MAAM;AAAA,sBACZ;AAAA,sBACA;AAAA,sBACA;AAAA,oBACF;AACA,mCAAe,KAAK;AACpB,0BAAM;AAAA,kBACR;AAEA,wBAAM,WAAW,qBAAqB,OAAO,KAAK;AAClD,sBAAI,UAAU;AACZ,0BAAM;AAAA,kBACR;AAAA,gBACF;AAAA,cACF;AAGA,8BAAgB,uBAAuB,KAAK,CAAC;AAAA,YAC/C,SAAS,OAAO;AACd,6BAAe,KAAc;AAC7B,oBAAM;AAAA,YACR;AAAA,UACF;AAEA,iBAAO;AAAA,YACL,CAAC,OAAO,aAAa,IAAI;AACvB,qBAAO,eAAe;AAAA,YACxB;AAAA,YACA,UAAU;AAAA,UACZ;AAAA,QACF;AAAA,MACF;AAEA,aAAO;AAAA,IACT;AAAA,EACF;AACF;;;AC5LO,IAAM,YAAY,eAAe;AAAA,EACtC,MAAM;AAAA,EACN,SAAS;AAAA,EACT,YAAY;AAAA,IACV,KAAK,iBAAiB;AAAA,EACxB;AACF,CAAC;","names":[]}
1
+ {"version":3,"sources":["../../src/providers/anthropic/transform.ts","../../src/providers/anthropic/llm.ts","../../src/providers/anthropic/index.ts"],"sourcesContent":["import type { LLMRequest, LLMResponse } from '../../types/llm.ts';\nimport type { Message } from '../../types/messages.ts';\nimport type { StreamEvent } from '../../types/stream.ts';\nimport type { Tool, ToolCall } from '../../types/tool.ts';\nimport type { TokenUsage } from '../../types/turn.ts';\nimport type { ContentBlock, TextBlock, ImageBlock } from '../../types/content.ts';\nimport {\n AssistantMessage,\n UserMessage,\n ToolResultMessage,\n isUserMessage,\n isAssistantMessage,\n isToolResultMessage,\n} from '../../types/messages.ts';\nimport type {\n AnthropicLLMParams,\n AnthropicRequest,\n AnthropicMessage,\n AnthropicContent,\n AnthropicTool,\n AnthropicResponse,\n AnthropicStreamEvent,\n AnthropicContentBlockDeltaEvent,\n} from './types.ts';\n\n/**\n * Transform UPP request to Anthropic format\n */\nexport function transformRequest<TParams extends AnthropicLLMParams>(\n request: LLMRequest<TParams>,\n modelId: string\n): AnthropicRequest {\n const params = (request.params ?? {}) as AnthropicLLMParams;\n\n const anthropicRequest: AnthropicRequest = {\n model: modelId,\n messages: request.messages.map(transformMessage),\n };\n\n // Only include max_tokens if provided - let Anthropic API enforce its requirement\n if (params.max_tokens !== undefined) {\n anthropicRequest.max_tokens = params.max_tokens;\n }\n\n // System prompt (top-level in Anthropic)\n if (request.system) {\n anthropicRequest.system = request.system;\n }\n\n // Model parameters\n if (params.temperature !== undefined) {\n anthropicRequest.temperature = params.temperature;\n }\n if (params.top_p !== undefined) {\n anthropicRequest.top_p = params.top_p;\n }\n if (params.top_k !== undefined) {\n anthropicRequest.top_k = params.top_k;\n }\n if (params.stop_sequences) {\n anthropicRequest.stop_sequences = params.stop_sequences;\n }\n if (params.metadata) {\n anthropicRequest.metadata = params.metadata;\n }\n if (params.thinking) {\n anthropicRequest.thinking = params.thinking;\n }\n if (params.service_tier !== undefined) {\n anthropicRequest.service_tier = params.service_tier;\n }\n\n // Tools\n if (request.tools && request.tools.length > 0) {\n anthropicRequest.tools = request.tools.map(transformTool);\n anthropicRequest.tool_choice = { type: 'auto' };\n }\n\n // Structured output via tool-based approach\n // Anthropic doesn't have native structured output, so we use a tool to enforce the schema\n if (request.structure) {\n const structuredTool: AnthropicTool = {\n name: 'json_response',\n description: 'Return the response in the specified JSON format. You MUST use this tool to provide your response.',\n input_schema: {\n type: 'object',\n properties: request.structure.properties,\n required: request.structure.required,\n },\n };\n\n // Add the structured output tool (may coexist with user tools)\n anthropicRequest.tools = [...(anthropicRequest.tools ?? []), structuredTool];\n // Force the model to use the json_response tool\n anthropicRequest.tool_choice = { type: 'tool', name: 'json_response' };\n }\n\n return anthropicRequest;\n}\n\n/**\n * Filter to only valid content blocks with a type property\n */\nfunction filterValidContent<T extends { type?: string }>(content: T[]): T[] {\n return content.filter((c) => c && typeof c.type === 'string');\n}\n\n/**\n * Transform a UPP Message to Anthropic format\n */\nfunction transformMessage(message: Message): AnthropicMessage {\n if (isUserMessage(message)) {\n const validContent = filterValidContent(message.content);\n return {\n role: 'user',\n content: validContent.map(transformContentBlock),\n };\n }\n\n if (isAssistantMessage(message)) {\n const validContent = filterValidContent(message.content);\n const content: AnthropicContent[] = validContent.map(transformContentBlock);\n\n // Add tool calls as tool_use content blocks\n if (message.toolCalls) {\n for (const call of message.toolCalls) {\n content.push({\n type: 'tool_use',\n id: call.toolCallId,\n name: call.toolName,\n input: call.arguments,\n });\n }\n }\n\n return {\n role: 'assistant',\n content,\n };\n }\n\n if (isToolResultMessage(message)) {\n // Tool results are sent as user messages with tool_result content\n return {\n role: 'user',\n content: message.results.map((result) => ({\n type: 'tool_result' as const,\n tool_use_id: result.toolCallId,\n content:\n typeof result.result === 'string'\n ? result.result\n : JSON.stringify(result.result),\n is_error: result.isError,\n })),\n };\n }\n\n throw new Error(`Unknown message type: ${message.type}`);\n}\n\n/**\n * Transform a content block to Anthropic format\n */\nfunction transformContentBlock(block: ContentBlock): AnthropicContent {\n switch (block.type) {\n case 'text':\n return { type: 'text', text: block.text };\n\n case 'image': {\n const imageBlock = block as ImageBlock;\n if (imageBlock.source.type === 'base64') {\n return {\n type: 'image',\n source: {\n type: 'base64',\n media_type: imageBlock.mimeType,\n data: imageBlock.source.data,\n },\n };\n }\n if (imageBlock.source.type === 'url') {\n return {\n type: 'image',\n source: {\n type: 'url',\n url: imageBlock.source.url,\n },\n };\n }\n if (imageBlock.source.type === 'bytes') {\n // Convert bytes to base64\n const base64 = btoa(\n Array.from(imageBlock.source.data)\n .map((b) => String.fromCharCode(b))\n .join('')\n );\n return {\n type: 'image',\n source: {\n type: 'base64',\n media_type: imageBlock.mimeType,\n data: base64,\n },\n };\n }\n throw new Error(`Unknown image source type`);\n }\n\n default:\n throw new Error(`Unsupported content type: ${block.type}`);\n }\n}\n\n/**\n * Transform a UPP Tool to Anthropic format\n */\nfunction transformTool(tool: Tool): AnthropicTool {\n return {\n name: tool.name,\n description: tool.description,\n input_schema: {\n type: 'object',\n properties: tool.parameters.properties,\n required: tool.parameters.required,\n },\n };\n}\n\n/**\n * Transform Anthropic response to UPP LLMResponse\n */\nexport function transformResponse(data: AnthropicResponse): LLMResponse {\n // Extract text content\n const textContent: TextBlock[] = [];\n const toolCalls: ToolCall[] = [];\n let structuredData: unknown;\n\n for (const block of data.content) {\n if (block.type === 'text') {\n textContent.push({ type: 'text', text: block.text });\n } else if (block.type === 'tool_use') {\n // Check if this is the json_response tool (structured output)\n if (block.name === 'json_response') {\n // Extract structured data from tool arguments\n structuredData = block.input;\n }\n toolCalls.push({\n toolCallId: block.id,\n toolName: block.name,\n arguments: block.input,\n });\n }\n // Skip thinking blocks for now\n }\n\n const message = new AssistantMessage(\n textContent,\n toolCalls.length > 0 ? toolCalls : undefined,\n {\n id: data.id,\n metadata: {\n anthropic: {\n stop_reason: data.stop_reason,\n stop_sequence: data.stop_sequence,\n model: data.model,\n },\n },\n }\n );\n\n const usage: TokenUsage = {\n inputTokens: data.usage.input_tokens,\n outputTokens: data.usage.output_tokens,\n totalTokens: data.usage.input_tokens + data.usage.output_tokens,\n };\n\n return {\n message,\n usage,\n stopReason: data.stop_reason ?? 'end_turn',\n data: structuredData,\n };\n}\n\n/**\n * State for accumulating streaming response\n */\nexport interface StreamState {\n messageId: string;\n model: string;\n content: Array<{ type: string; text?: string; id?: string; name?: string; input?: string }>;\n stopReason: string | null;\n inputTokens: number;\n outputTokens: number;\n}\n\n/**\n * Create initial stream state\n */\nexport function createStreamState(): StreamState {\n return {\n messageId: '',\n model: '',\n content: [],\n stopReason: null,\n inputTokens: 0,\n outputTokens: 0,\n };\n}\n\n/**\n * Transform Anthropic stream event to UPP StreamEvent\n * Returns null for events that don't produce UPP events\n */\nexport function transformStreamEvent(\n event: AnthropicStreamEvent,\n state: StreamState\n): StreamEvent | null {\n switch (event.type) {\n case 'message_start':\n state.messageId = event.message.id;\n state.model = event.message.model;\n state.inputTokens = event.message.usage.input_tokens;\n return { type: 'message_start', index: 0, delta: {} };\n\n case 'content_block_start':\n // Initialize content block\n if (event.content_block.type === 'text') {\n state.content[event.index] = { type: 'text', text: '' };\n } else if (event.content_block.type === 'tool_use') {\n state.content[event.index] = {\n type: 'tool_use',\n id: event.content_block.id,\n name: event.content_block.name,\n input: '',\n };\n }\n return { type: 'content_block_start', index: event.index, delta: {} };\n\n case 'content_block_delta': {\n const delta = event.delta;\n if (delta.type === 'text_delta') {\n if (state.content[event.index]) {\n state.content[event.index]!.text =\n (state.content[event.index]!.text ?? '') + delta.text;\n }\n return {\n type: 'text_delta',\n index: event.index,\n delta: { text: delta.text },\n };\n }\n if (delta.type === 'input_json_delta') {\n if (state.content[event.index]) {\n state.content[event.index]!.input =\n (state.content[event.index]!.input ?? '') + delta.partial_json;\n }\n return {\n type: 'tool_call_delta',\n index: event.index,\n delta: {\n argumentsJson: delta.partial_json,\n toolCallId: state.content[event.index]?.id,\n toolName: state.content[event.index]?.name,\n },\n };\n }\n if (delta.type === 'thinking_delta') {\n return {\n type: 'reasoning_delta',\n index: event.index,\n delta: { text: delta.thinking },\n };\n }\n return null;\n }\n\n case 'content_block_stop':\n return { type: 'content_block_stop', index: event.index, delta: {} };\n\n case 'message_delta':\n state.stopReason = event.delta.stop_reason;\n state.outputTokens = event.usage.output_tokens;\n return null;\n\n case 'message_stop':\n return { type: 'message_stop', index: 0, delta: {} };\n\n case 'ping':\n case 'error':\n return null;\n\n default:\n return null;\n }\n}\n\n/**\n * Build LLMResponse from accumulated stream state\n */\nexport function buildResponseFromState(state: StreamState): LLMResponse {\n const textContent: TextBlock[] = [];\n const toolCalls: ToolCall[] = [];\n let structuredData: unknown;\n\n for (const block of state.content) {\n if (block.type === 'text' && block.text) {\n textContent.push({ type: 'text', text: block.text });\n } else if (block.type === 'tool_use' && block.id && block.name) {\n let args: Record<string, unknown> = {};\n if (block.input) {\n try {\n args = JSON.parse(block.input);\n } catch {\n // Invalid JSON - use empty object\n }\n }\n // Check if this is the json_response tool (structured output)\n if (block.name === 'json_response') {\n structuredData = args;\n }\n toolCalls.push({\n toolCallId: block.id,\n toolName: block.name,\n arguments: args,\n });\n }\n }\n\n const message = new AssistantMessage(\n textContent,\n toolCalls.length > 0 ? toolCalls : undefined,\n {\n id: state.messageId,\n metadata: {\n anthropic: {\n stop_reason: state.stopReason,\n model: state.model,\n },\n },\n }\n );\n\n const usage: TokenUsage = {\n inputTokens: state.inputTokens,\n outputTokens: state.outputTokens,\n totalTokens: state.inputTokens + state.outputTokens,\n };\n\n return {\n message,\n usage,\n stopReason: state.stopReason ?? 'end_turn',\n data: structuredData,\n };\n}\n","import type { LLMHandler, BoundLLMModel, LLMRequest, LLMResponse, LLMStreamResult, LLMCapabilities } from '../../types/llm.ts';\nimport type { StreamEvent } from '../../types/stream.ts';\nimport type { LLMProvider } from '../../types/provider.ts';\nimport { UPPError } from '../../types/errors.ts';\nimport { resolveApiKey } from '../../http/keys.ts';\nimport { doFetch, doStreamFetch } from '../../http/fetch.ts';\nimport { parseSSEStream } from '../../http/sse.ts';\nimport { normalizeHttpError } from '../../http/errors.ts';\nimport type { AnthropicLLMParams, AnthropicResponse, AnthropicStreamEvent } from './types.ts';\nimport {\n transformRequest,\n transformResponse,\n transformStreamEvent,\n createStreamState,\n buildResponseFromState,\n} from './transform.ts';\n\nconst ANTHROPIC_API_URL = 'https://api.anthropic.com/v1/messages';\nconst ANTHROPIC_VERSION = '2023-06-01';\n\n/**\n * Anthropic API capabilities\n */\nconst ANTHROPIC_CAPABILITIES: LLMCapabilities = {\n streaming: true,\n tools: true,\n structuredOutput: true,\n imageInput: true,\n videoInput: false,\n audioInput: false,\n};\n\n/**\n * Create Anthropic LLM handler\n */\nexport function createLLMHandler(): LLMHandler<AnthropicLLMParams> {\n // Provider reference injected by createProvider() after construction\n let providerRef: LLMProvider<AnthropicLLMParams> | null = null;\n\n return {\n _setProvider(provider: LLMProvider<AnthropicLLMParams>) {\n providerRef = provider;\n },\n\n bind(modelId: string): BoundLLMModel<AnthropicLLMParams> {\n // Use the injected provider reference (set by createProvider)\n if (!providerRef) {\n throw new UPPError(\n 'Provider reference not set. Handler must be used with createProvider().',\n 'INVALID_REQUEST',\n 'anthropic',\n 'llm'\n );\n }\n\n const model: BoundLLMModel<AnthropicLLMParams> = {\n modelId,\n capabilities: ANTHROPIC_CAPABILITIES,\n\n get provider(): LLMProvider<AnthropicLLMParams> {\n return providerRef!;\n },\n\n async complete(request: LLMRequest<AnthropicLLMParams>): Promise<LLMResponse> {\n const apiKey = await resolveApiKey(\n request.config,\n 'ANTHROPIC_API_KEY',\n 'anthropic',\n 'llm'\n );\n\n const baseUrl = request.config.baseUrl ?? ANTHROPIC_API_URL;\n const body = transformRequest(request, modelId);\n\n const response = await doFetch(\n baseUrl,\n {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n 'x-api-key': apiKey,\n 'anthropic-version': request.config.apiVersion ?? ANTHROPIC_VERSION,\n },\n body: JSON.stringify(body),\n signal: request.signal,\n },\n request.config,\n 'anthropic',\n 'llm'\n );\n\n const data = (await response.json()) as AnthropicResponse;\n return transformResponse(data);\n },\n\n stream(request: LLMRequest<AnthropicLLMParams>): LLMStreamResult {\n const state = createStreamState();\n let responseResolve: (value: LLMResponse) => void;\n let responseReject: (error: Error) => void;\n\n const responsePromise = new Promise<LLMResponse>((resolve, reject) => {\n responseResolve = resolve;\n responseReject = reject;\n });\n\n async function* generateEvents(): AsyncGenerator<StreamEvent, void, unknown> {\n try {\n const apiKey = await resolveApiKey(\n request.config,\n 'ANTHROPIC_API_KEY',\n 'anthropic',\n 'llm'\n );\n\n const baseUrl = request.config.baseUrl ?? ANTHROPIC_API_URL;\n const body = transformRequest(request, modelId);\n body.stream = true;\n\n const response = await doStreamFetch(\n baseUrl,\n {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n 'x-api-key': apiKey,\n 'anthropic-version': request.config.apiVersion ?? ANTHROPIC_VERSION,\n },\n body: JSON.stringify(body),\n signal: request.signal,\n },\n request.config,\n 'anthropic',\n 'llm'\n );\n\n if (!response.ok) {\n const error = await normalizeHttpError(response, 'anthropic', 'llm');\n responseReject(error);\n throw error;\n }\n\n if (!response.body) {\n const error = new UPPError(\n 'No response body for streaming request',\n 'PROVIDER_ERROR',\n 'anthropic',\n 'llm'\n );\n responseReject(error);\n throw error;\n }\n\n for await (const data of parseSSEStream(response.body)) {\n // Check for Anthropic error event\n if (typeof data === 'object' && data !== null && 'type' in data) {\n const event = data as AnthropicStreamEvent;\n\n if (event.type === 'error') {\n const error = new UPPError(\n event.error.message,\n 'PROVIDER_ERROR',\n 'anthropic',\n 'llm'\n );\n responseReject(error);\n throw error;\n }\n\n const uppEvent = transformStreamEvent(event, state);\n if (uppEvent) {\n yield uppEvent;\n }\n }\n }\n\n // Build final response\n responseResolve(buildResponseFromState(state));\n } catch (error) {\n responseReject(error as Error);\n throw error;\n }\n }\n\n return {\n [Symbol.asyncIterator]() {\n return generateEvents();\n },\n response: responsePromise,\n };\n },\n };\n\n return model;\n },\n };\n}\n","import { createProvider } from '../../core/provider.ts';\nimport { createLLMHandler } from './llm.ts';\n\n/**\n * Anthropic provider\n * Supports LLM modality with Claude models\n */\nexport const anthropic = createProvider({\n name: 'anthropic',\n version: '1.0.0',\n modalities: {\n llm: createLLMHandler(),\n },\n});\n\n// Re-export types\nexport type { AnthropicLLMParams } from './types.ts';\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AA4BO,SAAS,iBACd,SACA,SACkB;AAClB,QAAM,SAAU,QAAQ,UAAU,CAAC;AAEnC,QAAM,mBAAqC;A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UAAU;AACZ,0BAAM;AAAA,kBACR;AAAA,gBACF;AAAA,cACF;AAGA,8BAAgB,uBAAuB,KAAK,CAAC;AAAA,YAC/C,SAAS,OAAO;AACd,6BAAe,KAAc;AAC7B,oBAAM;AAAA,YACR;AAAA,UACF;AAEA,iBAAO;AAAA,YACL,CAAC,OAAO,aAAa,IAAI;AACvB,qBAAO,eAAe;AAAA,YACxB;AAAA,YACA,UAAU;AAAA,UACZ;AAAA,QACF;AAAA,MACF;AAEA,aAAO;AAAA,IACT;AAAA,EACF;AACF;;;AC5LO,IAAM,YAAY,eAAe;AAAA,EACtC,MAAM;AAAA,EACN,SAAS;AAAA,EACT,YAAY;AAAA,IACV,KAAK,iBAAiB;AAAA,EACxB;AACF,CAAC;","names":[]}
@@ -0,0 +1,235 @@
1
+ import { b as Provider, M as ModelReference, a as LLMHandler } from '../provider-CUJWjgNl.js';
2
+
3
+ /**
4
+ * OpenRouter-specific LLM parameters
5
+ * These are passed through to the OpenRouter APIs
6
+ */
7
+ interface OpenRouterLLMParams {
8
+ /** Maximum number of tokens to generate */
9
+ max_tokens?: number;
10
+ /** Maximum output tokens (Responses API) */
11
+ max_output_tokens?: number;
12
+ /** Temperature for randomness (0.0 - 2.0) */
13
+ temperature?: number;
14
+ /** Top-p (nucleus) sampling (0.0 - 1.0) */
15
+ top_p?: number;
16
+ /** Top-k sampling (not available for OpenAI models) */
17
+ top_k?: number;
18
+ /** Frequency penalty (-2.0 - 2.0) */
19
+ frequency_penalty?: number;
20
+ /** Presence penalty (-2.0 - 2.0) */
21
+ presence_penalty?: number;
22
+ /** Repetition penalty (0.0 - 2.0) */
23
+ repetition_penalty?: number;
24
+ /** Custom stop sequences */
25
+ stop?: string | string[];
26
+ /** Seed for deterministic sampling */
27
+ seed?: number;
28
+ /** User identifier for abuse detection */
29
+ user?: string;
30
+ /** Enable logprobs */
31
+ logprobs?: boolean;
32
+ /** Number of top logprobs to return */
33
+ top_logprobs?: number;
34
+ /** Logit bias map */
35
+ logit_bias?: Record<number, number>;
36
+ /** Minimum probability threshold (0.0 - 1.0) */
37
+ min_p?: number;
38
+ /** Top-a sampling threshold (0.0 - 1.0) */
39
+ top_a?: number;
40
+ /** Whether to enable parallel tool calls */
41
+ parallel_tool_calls?: boolean;
42
+ /** Response format for structured output (Chat Completions API only) */
43
+ response_format?: OpenRouterResponseFormat;
44
+ /**
45
+ * Prompt transforms to apply
46
+ * See: https://openrouter.ai/docs/guides/features/message-transforms
47
+ */
48
+ transforms?: string[];
49
+ /**
50
+ * Multiple models for routing
51
+ * See: https://openrouter.ai/docs/guides/features/model-routing
52
+ */
53
+ models?: string[];
54
+ /**
55
+ * Routing strategy (e.g., 'fallback')
56
+ */
57
+ route?: 'fallback';
58
+ /**
59
+ * Provider routing preferences
60
+ * See: https://openrouter.ai/docs/guides/routing/provider-selection
61
+ */
62
+ provider?: OpenRouterProviderPreferences;
63
+ /**
64
+ * Predicted output for latency optimization
65
+ * https://platform.openai.com/docs/guides/latency-optimization#use-predicted-outputs
66
+ */
67
+ prediction?: {
68
+ type: 'content';
69
+ content: string;
70
+ };
71
+ /**
72
+ * Debug options (streaming only)
73
+ */
74
+ debug?: {
75
+ /** If true, returns the transformed request body sent to the provider */
76
+ echo_upstream_body?: boolean;
77
+ };
78
+ /**
79
+ * Reasoning configuration (Responses API)
80
+ */
81
+ reasoning?: {
82
+ effort?: 'low' | 'medium' | 'high';
83
+ };
84
+ }
85
+ /**
86
+ * API mode for OpenRouter provider
87
+ */
88
+ type OpenRouterAPIMode = 'completions' | 'responses';
89
+ /**
90
+ * Model options when creating a model reference
91
+ */
92
+ interface OpenRouterModelOptions {
93
+ /** Which API to use */
94
+ api?: OpenRouterAPIMode;
95
+ }
96
+ /**
97
+ * Model reference with OpenRouter-specific options
98
+ */
99
+ interface OpenRouterModelReference {
100
+ modelId: string;
101
+ options?: OpenRouterModelOptions;
102
+ }
103
+ /**
104
+ * OpenRouter provider configuration
105
+ */
106
+ interface OpenRouterConfig {
107
+ /** Which API to use: 'completions' (default) or 'responses' (beta) */
108
+ api?: 'completions' | 'responses';
109
+ }
110
+ /**
111
+ * Provider routing preferences
112
+ */
113
+ interface OpenRouterProviderPreferences {
114
+ /** Allow fallback to other providers */
115
+ allow_fallbacks?: boolean;
116
+ /** Require specific parameters to be supported */
117
+ require_parameters?: boolean;
118
+ /** Data collection policy */
119
+ data_collection?: 'allow' | 'deny';
120
+ /** Order of provider preference */
121
+ order?: string[];
122
+ /** Ignore specific providers */
123
+ ignore?: string[];
124
+ /** Quantization preferences */
125
+ quantizations?: string[];
126
+ }
127
+ /**
128
+ * Response format
129
+ */
130
+ type OpenRouterResponseFormat = {
131
+ type: 'text';
132
+ } | {
133
+ type: 'json_object';
134
+ } | {
135
+ type: 'json_schema';
136
+ json_schema: {
137
+ name: string;
138
+ description?: string;
139
+ schema: Record<string, unknown>;
140
+ strict?: boolean;
141
+ };
142
+ };
143
+
144
+ /**
145
+ * OpenRouter provider options
146
+ */
147
+ interface OpenRouterProviderOptions {
148
+ /**
149
+ * Which API to use:
150
+ * - 'completions': Chat Completions API (default, recommended)
151
+ * - 'responses': Responses API (beta)
152
+ */
153
+ api?: 'completions' | 'responses';
154
+ }
155
+ /**
156
+ * OpenRouter provider with configurable API mode
157
+ *
158
+ * @example
159
+ * // Using the Chat Completions API (default)
160
+ * const model = openrouter('openai/gpt-4o');
161
+ *
162
+ * @example
163
+ * // Using the Responses API (beta)
164
+ * const model = openrouter('openai/gpt-4o', { api: 'responses' });
165
+ *
166
+ * @example
167
+ * // Explicit Completions API
168
+ * const model = openrouter('anthropic/claude-3.5-sonnet', { api: 'completions' });
169
+ */
170
+ interface OpenRouterProvider extends Provider<OpenRouterProviderOptions> {
171
+ /**
172
+ * Create a model reference
173
+ * @param modelId - The model identifier (e.g., 'openai/gpt-4o', 'anthropic/claude-3.5-sonnet', 'meta-llama/llama-3.1-70b-instruct')
174
+ * @param options - Provider options including API selection
175
+ */
176
+ (modelId: string, options?: OpenRouterProviderOptions): ModelReference<OpenRouterProviderOptions>;
177
+ /** Provider name */
178
+ readonly name: 'openrouter';
179
+ /** Provider version */
180
+ readonly version: string;
181
+ /** Supported modalities */
182
+ readonly modalities: {
183
+ llm: LLMHandler<OpenRouterLLMParams>;
184
+ };
185
+ }
186
+ /**
187
+ * OpenRouter provider
188
+ *
189
+ * Supports both the Chat Completions API (default) and Responses API (beta).
190
+ *
191
+ * OpenRouter is a unified API that provides access to hundreds of AI models
192
+ * through a single endpoint, including models from OpenAI, Anthropic, Google,
193
+ * Meta, Mistral, and many others.
194
+ *
195
+ * @example
196
+ * ```ts
197
+ * import { openrouter } from './providers/openrouter';
198
+ * import { llm } from './core/llm';
199
+ *
200
+ * // Using Chat Completions API (default, recommended)
201
+ * const model = llm({
202
+ * model: openrouter('openai/gpt-4o'),
203
+ * params: { max_tokens: 1000 }
204
+ * });
205
+ *
206
+ * // Using Responses API (beta)
207
+ * const betaModel = llm({
208
+ * model: openrouter('openai/gpt-4o', { api: 'responses' }),
209
+ * params: { max_output_tokens: 1000 }
210
+ * });
211
+ *
212
+ * // Using OpenRouter-specific features
213
+ * const routedModel = llm({
214
+ * model: openrouter('openai/gpt-4o'),
215
+ * params: {
216
+ * max_tokens: 1000,
217
+ * // Fallback routing
218
+ * models: ['openai/gpt-4o', 'anthropic/claude-3.5-sonnet'],
219
+ * route: 'fallback',
220
+ * // Provider preferences
221
+ * provider: {
222
+ * allow_fallbacks: true,
223
+ * require_parameters: true,
224
+ * },
225
+ * }
226
+ * });
227
+ *
228
+ * // Generate
229
+ * const turn = await model.generate('Hello!');
230
+ * console.log(turn.response.text);
231
+ * ```
232
+ */
233
+ declare const openrouter: OpenRouterProvider;
234
+
235
+ export { type OpenRouterAPIMode, type OpenRouterConfig, type OpenRouterLLMParams, type OpenRouterModelOptions, type OpenRouterModelReference, type OpenRouterProviderPreferences, openrouter };