@providerprotocol/ai 0.0.19 → 0.0.21

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.
Files changed (49) hide show
  1. package/README.md +82 -9
  2. package/dist/anthropic/index.d.ts +184 -14
  3. package/dist/anthropic/index.js +214 -86
  4. package/dist/anthropic/index.js.map +1 -1
  5. package/dist/{chunk-5FEAOEXV.js → chunk-EDENPF3E.js} +57 -103
  6. package/dist/chunk-EDENPF3E.js.map +1 -0
  7. package/dist/{chunk-UMKWXGO3.js → chunk-M4BMM5IB.js} +86 -2
  8. package/dist/chunk-M4BMM5IB.js.map +1 -0
  9. package/dist/chunk-Y3GBJNA2.js +120 -0
  10. package/dist/chunk-Y3GBJNA2.js.map +1 -0
  11. package/dist/{chunk-U4JJC2YX.js → chunk-Z4ILICF5.js} +2 -2
  12. package/dist/chunk-Z4ILICF5.js.map +1 -0
  13. package/dist/google/index.d.ts +16 -19
  14. package/dist/google/index.js +18 -40
  15. package/dist/google/index.js.map +1 -1
  16. package/dist/http/index.d.ts +2 -2
  17. package/dist/http/index.js +5 -4
  18. package/dist/index.d.ts +101 -38
  19. package/dist/index.js +69 -43
  20. package/dist/index.js.map +1 -1
  21. package/dist/ollama/index.d.ts +14 -16
  22. package/dist/ollama/index.js +9 -11
  23. package/dist/ollama/index.js.map +1 -1
  24. package/dist/openai/index.d.ts +25 -133
  25. package/dist/openai/index.js +31 -85
  26. package/dist/openai/index.js.map +1 -1
  27. package/dist/openrouter/index.d.ts +28 -53
  28. package/dist/openrouter/index.js +24 -47
  29. package/dist/openrouter/index.js.map +1 -1
  30. package/dist/provider-DGQHYE6I.d.ts +1319 -0
  31. package/dist/proxy/index.d.ts +194 -12
  32. package/dist/proxy/index.js +37 -65
  33. package/dist/proxy/index.js.map +1 -1
  34. package/dist/{retry-DR7YRJDz.d.ts → retry-Pcs3hnbu.d.ts} +2 -2
  35. package/dist/{stream-DRHy6q1a.d.ts → stream-Di9acos2.d.ts} +1 -1
  36. package/dist/xai/index.d.ts +16 -88
  37. package/dist/xai/index.js +34 -62
  38. package/dist/xai/index.js.map +1 -1
  39. package/package.json +4 -1
  40. package/dist/chunk-5FEAOEXV.js.map +0 -1
  41. package/dist/chunk-DZQHVGNV.js +0 -71
  42. package/dist/chunk-DZQHVGNV.js.map +0 -1
  43. package/dist/chunk-MSR5P65T.js +0 -39
  44. package/dist/chunk-MSR5P65T.js.map +0 -1
  45. package/dist/chunk-U4JJC2YX.js.map +0 -1
  46. package/dist/chunk-UMKWXGO3.js.map +0 -1
  47. package/dist/content-DEl3z_W2.d.ts +0 -276
  48. package/dist/image-Dhq-Yuq4.d.ts +0 -456
  49. package/dist/provider-BBMBZuGn.d.ts +0 -570
package/README.md CHANGED
@@ -314,43 +314,116 @@ try {
314
314
 
315
315
  **Error Codes:** `AUTHENTICATION_FAILED`, `RATE_LIMITED`, `CONTEXT_LENGTH_EXCEEDED`, `MODEL_NOT_FOUND`, `INVALID_REQUEST`, `INVALID_RESPONSE`, `CONTENT_FILTERED`, `QUOTA_EXCEEDED`, `PROVIDER_ERROR`, `NETWORK_ERROR`, `TIMEOUT`, `CANCELLED`
316
316
 
317
- ## Proxy Server
317
+ ## API Gateway / Proxy
318
318
 
319
- Build backend proxies to hide API keys from clients.
319
+ Build AI API gateways with your own authentication. Users authenticate with your platform - AI provider keys stay hidden on the server.
320
320
 
321
- ### Server
321
+ > **Security Note:** The proxy works without any configuration, but this means **no authentication by default**. Always add your own auth layer in production - the examples below show how.
322
+
323
+ ### Server (Bun/Deno/Cloudflare Workers)
322
324
 
323
325
  ```typescript
324
326
  import { llm } from '@providerprotocol/ai';
325
327
  import { anthropic } from '@providerprotocol/ai/anthropic';
326
- import { webapi, parseBody, toJSON, toSSE } from '@providerprotocol/ai/proxy';
328
+ import { ExponentialBackoff, RoundRobinKeys } from '@providerprotocol/ai/http';
329
+ import { parseBody, toJSON, toSSE, toError } from '@providerprotocol/ai/proxy';
327
330
 
328
- const claude = llm({ model: anthropic('claude-sonnet-4-20250514') });
331
+ // Server manages AI provider keys - users never see them
332
+ const claude = llm({
333
+ model: anthropic('claude-sonnet-4-20250514'),
334
+ config: {
335
+ apiKey: new RoundRobinKeys([process.env.ANTHROPIC_KEY_1!, process.env.ANTHROPIC_KEY_2!]),
336
+ retryStrategy: new ExponentialBackoff({ maxAttempts: 3 }),
337
+ },
338
+ });
329
339
 
330
340
  Bun.serve({
331
341
  port: 3000,
332
- fetch: webapi(async (req) => {
342
+ async fetch(req) {
343
+ // Authenticate with YOUR platform credentials
344
+ const token = req.headers.get('Authorization')?.replace('Bearer ', '');
345
+ const user = await validatePlatformToken(token ?? '');
346
+ if (!user) return toError('Unauthorized', 401);
347
+
348
+ // Rate limit, track usage, bill user, etc.
349
+ await trackUsage(user.id);
350
+
333
351
  const { messages, system, params } = parseBody(await req.json());
334
352
 
335
353
  if (params?.stream) {
336
354
  return toSSE(claude.stream(messages, { system }));
337
355
  }
338
-
339
356
  return toJSON(await claude.generate(messages, { system }));
340
- }),
357
+ },
341
358
  });
342
359
  ```
343
360
 
344
361
  ### Client
345
362
 
363
+ Clients authenticate with your platform token. They get automatic retry on network failures to your proxy.
364
+
346
365
  ```typescript
347
366
  import { llm } from '@providerprotocol/ai';
348
367
  import { proxy } from '@providerprotocol/ai/proxy';
368
+ import { ExponentialBackoff } from '@providerprotocol/ai/http';
369
+
370
+ const claude = llm({
371
+ model: proxy('https://api.yourplatform.com/ai'),
372
+ config: {
373
+ headers: { 'Authorization': 'Bearer user-platform-token' },
374
+ retryStrategy: new ExponentialBackoff({ maxAttempts: 3 }),
375
+ timeout: 30000,
376
+ },
377
+ });
349
378
 
350
- const claude = llm({ model: proxy('http://localhost:3000') });
351
379
  const turn = await claude.generate('Hello!');
352
380
  ```
353
381
 
382
+ ### Framework Adapters
383
+
384
+ Server adapters for Express, Fastify, and Nuxt/H3:
385
+
386
+ ```typescript
387
+ // Express
388
+ import { express as expressAdapter } from '@providerprotocol/ai/proxy/server';
389
+ app.post('/ai', authMiddleware, async (req, res) => {
390
+ const { messages, system, params } = parseBody(req.body);
391
+ if (params?.stream) {
392
+ expressAdapter.streamSSE(claude.stream(messages, { system }), res);
393
+ } else {
394
+ expressAdapter.sendJSON(await claude.generate(messages, { system }), res);
395
+ }
396
+ });
397
+
398
+ // Fastify
399
+ import { fastify as fastifyAdapter } from '@providerprotocol/ai/proxy/server';
400
+ app.post('/ai', async (request, reply) => {
401
+ const { messages, system, params } = parseBody(request.body);
402
+ if (params?.stream) {
403
+ return fastifyAdapter.streamSSE(claude.stream(messages, { system }), reply);
404
+ }
405
+ return fastifyAdapter.sendJSON(await claude.generate(messages, { system }), reply);
406
+ });
407
+
408
+ // Nuxt/H3 (server/api/ai.post.ts)
409
+ import { h3 as h3Adapter } from '@providerprotocol/ai/proxy/server';
410
+ export default defineEventHandler(async (event) => {
411
+ const { messages, system, params } = parseBody(await readBody(event));
412
+ if (params?.stream) {
413
+ return h3Adapter.streamSSE(claude.stream(messages, { system }), event);
414
+ }
415
+ return h3Adapter.sendJSON(await claude.generate(messages, { system }), event);
416
+ });
417
+ ```
418
+
419
+ **What this enables:**
420
+ - Users auth with your platform credentials (JWT, API keys, sessions)
421
+ - You manage/rotate AI provider keys centrally
422
+ - Per-user rate limiting, usage tracking, billing
423
+ - Model access control (different users get different models)
424
+ - Request/response logging, content filtering
425
+ - Double-layer retry: client retries to proxy, server retries to AI provider
426
+
354
427
  ## xAI API Modes
355
428
 
356
429
  xAI supports multiple API compatibility modes:
@@ -1,4 +1,4 @@
1
- import { d as Provider } from '../provider-BBMBZuGn.js';
1
+ import { g as Provider } from '../provider-DGQHYE6I.js';
2
2
 
3
3
  /**
4
4
  * @fileoverview Anthropic API type definitions.
@@ -6,6 +6,83 @@ import { d as Provider } from '../provider-BBMBZuGn.js';
6
6
  * Contains TypeScript interfaces for Anthropic's Messages API request/response
7
7
  * structures, streaming events, and provider-specific parameters.
8
8
  */
9
+ /**
10
+ * Known Anthropic beta header values.
11
+ *
12
+ * Beta features are enabled by passing these values in the `betas` config option
13
+ * or via the `anthropic-beta` HTTP header. Multiple betas can be enabled simultaneously.
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * import { anthropic, betas } from 'provider-protocol/anthropic';
18
+ *
19
+ * // Using the betas config option (recommended)
20
+ * const provider = anthropic('claude-sonnet-4-20250514', {
21
+ * betas: [betas.structuredOutputs, betas.interleavedThinking],
22
+ * });
23
+ *
24
+ * // Or use string values directly for new/unlisted betas
25
+ * const provider = anthropic('claude-sonnet-4-20250514', {
26
+ * betas: ['new-beta-2025-12-01'],
27
+ * });
28
+ * ```
29
+ */
30
+ declare const betas: {
31
+ /** Guaranteed JSON schema conformance for responses. Available for Claude Sonnet 4.5+. */
32
+ readonly structuredOutputs: "structured-outputs-2025-11-13";
33
+ /** Enables Claude to think between tool calls in Claude 4 models. */
34
+ readonly interleavedThinking: "interleaved-thinking-2025-05-14";
35
+ /** Developer mode for full thinking output visibility. */
36
+ readonly devFullThinking: "dev-full-thinking-2025-05-14";
37
+ /** Effort parameter for Claude Opus 4.5 - controls response thoroughness vs efficiency. */
38
+ readonly effort: "effort-2025-11-24";
39
+ /** Legacy computer use tool (Claude 3.x models). */
40
+ readonly computerUseLegacy: "computer-use-2024-10-22";
41
+ /** Computer use tool for Claude 4 models (mouse, keyboard, screenshots). */
42
+ readonly computerUse: "computer-use-2025-01-24";
43
+ /** Computer use tool for Claude Opus 4.5 with additional commands. */
44
+ readonly computerUseOpus: "computer-use-2025-11-24";
45
+ /** Enables up to 8,192 output tokens from Claude Sonnet 3.5. */
46
+ readonly maxTokens35Sonnet: "max-tokens-3-5-sonnet-2024-07-15";
47
+ /** Enables 128K token output length. */
48
+ readonly output128k: "output-128k-2025-02-19";
49
+ /** Enables 1 million token context window for Claude Sonnet 4. */
50
+ readonly context1m: "context-1m-2025-08-07";
51
+ /** Reduces output token consumption by up to 70% for tool calls. */
52
+ readonly tokenEfficientTools: "token-efficient-tools-2025-02-19";
53
+ /** Streams tool use parameters without buffering/JSON validation. */
54
+ readonly fineGrainedToolStreaming: "fine-grained-tool-streaming-2025-05-14";
55
+ /** Code execution tool for running Python/Bash in secure sandbox. */
56
+ readonly codeExecution: "code-execution-2025-08-25";
57
+ /** Advanced tool use: Tool Search, Programmatic Tool Calling, Tool Use Examples. */
58
+ readonly advancedToolUse: "advanced-tool-use-2025-11-20";
59
+ /** Files API for uploading and managing files. */
60
+ readonly filesApi: "files-api-2025-04-14";
61
+ /** PDF document support. */
62
+ readonly pdfs: "pdfs-2024-09-25";
63
+ /** MCP connector to connect to remote MCP servers. */
64
+ readonly mcpClient: "mcp-client-2025-04-04";
65
+ /** Updated MCP client. */
66
+ readonly mcpClientLatest: "mcp-client-2025-11-20";
67
+ /** Prompt caching for reduced latency and costs. Now works automatically with cache_control. */
68
+ readonly promptCaching: "prompt-caching-2024-07-31";
69
+ /** Enables 1-hour cache TTL (vs default 5-minute). */
70
+ readonly extendedCacheTtl: "extended-cache-ttl-2025-04-11";
71
+ /** Automatic tool call clearing for context management. */
72
+ readonly contextManagement: "context-management-2025-06-27";
73
+ /** Handling for when model context window is exceeded. */
74
+ readonly modelContextWindowExceeded: "model-context-window-exceeded-2025-08-26";
75
+ /** Message Batches API for async processing at 50% cost. */
76
+ readonly messageBatches: "message-batches-2024-09-24";
77
+ /** Token counting endpoint. */
78
+ readonly tokenCounting: "token-counting-2024-11-01";
79
+ /** Agent Skills for specialized tasks (PowerPoint, Excel, Word, PDF). */
80
+ readonly skills: "skills-2025-10-02";
81
+ };
82
+ /** Type representing any valid beta key from the betas object. */
83
+ type BetaKey = keyof typeof betas;
84
+ /** Type representing a beta value (either a known constant or arbitrary string). */
85
+ type BetaValue = (typeof betas)[BetaKey] | string;
9
86
  /**
10
87
  * Provider-specific parameters for Anthropic Claude models.
11
88
  *
@@ -68,7 +145,7 @@ interface AnthropicLLMParams {
68
145
  * const model = llm({
69
146
  * model: anthropic('claude-sonnet-4-20250514'),
70
147
  * params: {
71
- * builtInTools: [
148
+ * tools: [
72
149
  * tools.webSearch({ max_uses: 5 }),
73
150
  * tools.codeExecution(),
74
151
  * ],
@@ -76,13 +153,56 @@ interface AnthropicLLMParams {
76
153
  * });
77
154
  * ```
78
155
  */
79
- builtInTools?: AnthropicBuiltInTool[];
156
+ tools?: AnthropicBuiltInTool[];
80
157
  /**
81
158
  * Container ID for code execution tool reuse.
82
159
  * Pass the container ID from a previous response to reuse the same environment.
83
160
  */
84
161
  container?: string;
85
162
  }
163
+ /**
164
+ * Request body structure for Anthropic's Messages API.
165
+ *
166
+ * This interface represents the full request payload sent to the
167
+ * `/v1/messages` endpoint.
168
+ */
169
+ /**
170
+ * Native structured output format configuration.
171
+ *
172
+ * When provided, Claude's response will be constrained to match the
173
+ * specified JSON schema. Requires the beta header `structured-outputs-2025-11-13`.
174
+ *
175
+ * @example
176
+ * ```typescript
177
+ * const outputFormat: AnthropicOutputFormat = {
178
+ * type: 'json_schema',
179
+ * schema: {
180
+ * type: 'object',
181
+ * properties: {
182
+ * name: { type: 'string' },
183
+ * age: { type: 'integer' },
184
+ * },
185
+ * required: ['name', 'age'],
186
+ * additionalProperties: false,
187
+ * },
188
+ * };
189
+ * ```
190
+ */
191
+ interface AnthropicOutputFormat {
192
+ /** Output format type - currently only 'json_schema' is supported. */
193
+ type: 'json_schema';
194
+ /** JSON Schema defining the expected response structure. */
195
+ schema: {
196
+ /** Schema type (always 'object' for structured outputs). */
197
+ type: 'object';
198
+ /** Property definitions for each field. */
199
+ properties: Record<string, unknown>;
200
+ /** List of required property names. */
201
+ required?: string[];
202
+ /** Must be false for structured outputs. */
203
+ additionalProperties?: false;
204
+ };
205
+ }
86
206
  /**
87
207
  * Anthropic-specific HTTP headers for API requests.
88
208
  *
@@ -454,7 +574,7 @@ declare function toolSearchTool(options?: {
454
574
  * const model = llm({
455
575
  * model: anthropic('claude-sonnet-4-20250514'),
456
576
  * params: {
457
- * builtInTools: [
577
+ * tools: [
458
578
  * tools.webSearch({ max_uses: 5 }),
459
579
  * tools.codeExecution(),
460
580
  * ],
@@ -478,25 +598,75 @@ declare const tools: {
478
598
  };
479
599
 
480
600
  /**
481
- * Anthropic provider instance for the Universal Provider Protocol.
601
+ * Options for configuring an Anthropic model reference.
602
+ *
603
+ * @example
604
+ * ```typescript
605
+ * import { anthropic, betas } from 'provider-protocol/anthropic';
606
+ *
607
+ * // Enable structured outputs beta
608
+ * const model = anthropic('claude-sonnet-4-20250514', {
609
+ * betas: [betas.structuredOutputs],
610
+ * });
611
+ *
612
+ * // Enable multiple betas
613
+ * const modelWithBetas = anthropic('claude-sonnet-4-20250514', {
614
+ * betas: [betas.structuredOutputs, betas.interleavedThinking],
615
+ * });
616
+ *
617
+ * // Use string values for new/unlisted betas
618
+ * const modelWithCustomBeta = anthropic('claude-sonnet-4-20250514', {
619
+ * betas: ['new-beta-2025-12-01'],
620
+ * });
621
+ * ```
622
+ */
623
+ interface AnthropicModelOptions {
624
+ /**
625
+ * Beta features to enable for this model.
626
+ *
627
+ * Use values from the `betas` export or pass arbitrary strings for new betas.
628
+ * Multiple betas are combined into a comma-separated `anthropic-beta` header.
629
+ */
630
+ betas?: BetaValue[];
631
+ }
632
+ /**
633
+ * Anthropic provider for the Universal Provider Protocol.
482
634
  *
483
635
  * Provides access to Claude language models through a unified interface.
484
- * Currently supports the LLM modality with full streaming, tool use,
485
- * structured output, and image input capabilities.
636
+ * Supports LLM modality with streaming, tool use, structured output,
637
+ * and image input capabilities.
638
+ *
639
+ * @param modelId - The model identifier (e.g., 'claude-sonnet-4-20250514')
640
+ * @param options - Optional configuration including beta features
641
+ * @returns A model reference for use with `llm()`
486
642
  *
487
643
  * @example
488
644
  * ```typescript
489
- * import { anthropic } from './providers/anthropic';
645
+ * import { anthropic, betas } from 'provider-protocol/anthropic';
646
+ * import { llm } from 'provider-protocol';
647
+ *
648
+ * // Basic usage
649
+ * const model = llm({ model: anthropic('claude-sonnet-4-20250514') });
650
+ *
651
+ * // With structured outputs beta
652
+ * const modelWithBetas = llm({
653
+ * model: anthropic('claude-sonnet-4-20250514', {
654
+ * betas: [betas.structuredOutputs],
655
+ * }),
656
+ * structure: { properties: { name: { type: 'string' } } },
657
+ * });
490
658
  *
491
- * const claude = anthropic.llm.bind('claude-sonnet-4-20250514');
492
- * const response = await claude.complete({
493
- * messages: [new UserMessage([{ type: 'text', text: 'Hello!' }])],
494
- * config: { apiKey: 'sk-...' },
659
+ * // With multiple betas
660
+ * const advancedModel = llm({
661
+ * model: anthropic('claude-sonnet-4-20250514', {
662
+ * betas: [betas.structuredOutputs, betas.tokenEfficientTools],
663
+ * }),
495
664
  * });
496
665
  * ```
497
666
  *
667
+ * @see {@link betas} for available beta features
498
668
  * @see {@link AnthropicLLMParams} for provider-specific parameters
499
669
  */
500
- declare const anthropic: Provider<unknown>;
670
+ declare const anthropic: Provider<AnthropicModelOptions>;
501
671
 
502
- export { type AnthropicBashTool, type AnthropicBuiltInTool, type AnthropicCodeExecutionTool, type AnthropicComputerTool, type AnthropicHeaders, type AnthropicLLMParams, type AnthropicTextEditorTool, type AnthropicToolSearchTool, type AnthropicUserLocation, type AnthropicWebSearchTool, anthropic, tools };
672
+ export { type AnthropicBashTool, type AnthropicBuiltInTool, type AnthropicCodeExecutionTool, type AnthropicComputerTool, type AnthropicHeaders, type AnthropicLLMParams, type AnthropicModelOptions, type AnthropicOutputFormat, type AnthropicTextEditorTool, type AnthropicToolSearchTool, type AnthropicUserLocation, type AnthropicWebSearchTool, type BetaKey, type BetaValue, anthropic, betas, tools };