@providerprotocol/ai 0.0.11 → 0.0.12

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 (63) hide show
  1. package/dist/index.js +3 -3
  2. package/dist/index.js.map +1 -1
  3. package/package.json +1 -10
  4. package/src/anthropic/index.ts +0 -3
  5. package/src/core/image.ts +0 -188
  6. package/src/core/llm.ts +0 -650
  7. package/src/core/provider.ts +0 -92
  8. package/src/google/index.ts +0 -3
  9. package/src/http/errors.ts +0 -112
  10. package/src/http/fetch.ts +0 -210
  11. package/src/http/index.ts +0 -31
  12. package/src/http/keys.ts +0 -136
  13. package/src/http/retry.ts +0 -205
  14. package/src/http/sse.ts +0 -136
  15. package/src/index.ts +0 -32
  16. package/src/ollama/index.ts +0 -3
  17. package/src/openai/index.ts +0 -39
  18. package/src/openrouter/index.ts +0 -11
  19. package/src/providers/anthropic/index.ts +0 -17
  20. package/src/providers/anthropic/llm.ts +0 -196
  21. package/src/providers/anthropic/transform.ts +0 -434
  22. package/src/providers/anthropic/types.ts +0 -213
  23. package/src/providers/google/index.ts +0 -17
  24. package/src/providers/google/llm.ts +0 -203
  25. package/src/providers/google/transform.ts +0 -447
  26. package/src/providers/google/types.ts +0 -214
  27. package/src/providers/ollama/index.ts +0 -43
  28. package/src/providers/ollama/llm.ts +0 -272
  29. package/src/providers/ollama/transform.ts +0 -434
  30. package/src/providers/ollama/types.ts +0 -260
  31. package/src/providers/openai/index.ts +0 -186
  32. package/src/providers/openai/llm.completions.ts +0 -201
  33. package/src/providers/openai/llm.responses.ts +0 -211
  34. package/src/providers/openai/transform.completions.ts +0 -561
  35. package/src/providers/openai/transform.responses.ts +0 -708
  36. package/src/providers/openai/types.ts +0 -1249
  37. package/src/providers/openrouter/index.ts +0 -177
  38. package/src/providers/openrouter/llm.completions.ts +0 -201
  39. package/src/providers/openrouter/llm.responses.ts +0 -211
  40. package/src/providers/openrouter/transform.completions.ts +0 -538
  41. package/src/providers/openrouter/transform.responses.ts +0 -742
  42. package/src/providers/openrouter/types.ts +0 -717
  43. package/src/providers/xai/index.ts +0 -223
  44. package/src/providers/xai/llm.completions.ts +0 -201
  45. package/src/providers/xai/llm.messages.ts +0 -195
  46. package/src/providers/xai/llm.responses.ts +0 -211
  47. package/src/providers/xai/transform.completions.ts +0 -565
  48. package/src/providers/xai/transform.messages.ts +0 -448
  49. package/src/providers/xai/transform.responses.ts +0 -678
  50. package/src/providers/xai/types.ts +0 -938
  51. package/src/types/content.ts +0 -133
  52. package/src/types/errors.ts +0 -85
  53. package/src/types/index.ts +0 -105
  54. package/src/types/llm.ts +0 -211
  55. package/src/types/messages.ts +0 -205
  56. package/src/types/provider.ts +0 -195
  57. package/src/types/schema.ts +0 -58
  58. package/src/types/stream.ts +0 -188
  59. package/src/types/thread.ts +0 -226
  60. package/src/types/tool.ts +0 -88
  61. package/src/types/turn.ts +0 -118
  62. package/src/utils/id.ts +0 -28
  63. package/src/xai/index.ts +0 -41
@@ -1,92 +0,0 @@
1
- import type {
2
- Provider,
3
- ModelReference,
4
- LLMHandler,
5
- EmbeddingHandler,
6
- ImageHandler,
7
- LLMProvider,
8
- EmbeddingProvider,
9
- ImageProvider,
10
- } from '../types/provider.ts';
11
-
12
- /**
13
- * Options for creating a provider
14
- */
15
- export interface CreateProviderOptions {
16
- name: string;
17
- version: string;
18
- modalities: {
19
- llm?: LLMHandler;
20
- embedding?: EmbeddingHandler;
21
- image?: ImageHandler;
22
- };
23
- }
24
-
25
- /**
26
- * Create a provider factory function
27
- *
28
- * @typeParam TOptions - Provider-specific options type (defaults to unknown)
29
- * @param options - Provider configuration
30
- * @returns Provider function with modalities attached
31
- *
32
- * @example
33
- * ```ts
34
- * // Basic provider without options
35
- * const anthropic = createProvider({
36
- * name: 'anthropic',
37
- * version: '1.0.0',
38
- * modalities: { llm: createLLMHandler() },
39
- * });
40
- *
41
- * // Provider with custom options (typically needs custom factory)
42
- * interface MyProviderOptions { api?: 'v1' | 'v2' }
43
- * const myProvider = createProvider<MyProviderOptions>({
44
- * name: 'my-provider',
45
- * version: '1.0.0',
46
- * modalities: { llm: createLLMHandler() },
47
- * });
48
- * ```
49
- */
50
- export function createProvider<TOptions = unknown>(
51
- options: CreateProviderOptions
52
- ): Provider<TOptions> {
53
- // Create the base function that accepts optional provider-specific options
54
- const fn = function (modelId: string, _options?: TOptions): ModelReference<TOptions> {
55
- return { modelId, provider };
56
- };
57
-
58
- // Define properties, including overriding the read-only 'name' property
59
- Object.defineProperties(fn, {
60
- name: {
61
- value: options.name,
62
- writable: false,
63
- configurable: true,
64
- },
65
- version: {
66
- value: options.version,
67
- writable: false,
68
- configurable: true,
69
- },
70
- modalities: {
71
- value: options.modalities,
72
- writable: false,
73
- configurable: true,
74
- },
75
- });
76
-
77
- const provider = fn as Provider<TOptions>;
78
-
79
- // Inject provider reference into handlers so bind() can return
80
- // models with the correct provider reference (spec compliance)
81
- if (options.modalities.llm?._setProvider) {
82
- options.modalities.llm._setProvider(provider as unknown as LLMProvider);
83
- }
84
- if (options.modalities.embedding?._setProvider) {
85
- options.modalities.embedding._setProvider(provider as unknown as EmbeddingProvider);
86
- }
87
- if (options.modalities.image?._setProvider) {
88
- options.modalities.image._setProvider(provider as unknown as ImageProvider);
89
- }
90
-
91
- return provider;
92
- }
@@ -1,3 +0,0 @@
1
- // Re-export from providers/google
2
- export { google } from '../providers/google/index.ts';
3
- export type { GoogleLLMParams } from '../providers/google/index.ts';
@@ -1,112 +0,0 @@
1
- import { UPPError, type ErrorCode, type Modality } from '../types/errors.ts';
2
-
3
- /**
4
- * Map HTTP status codes to UPP error codes
5
- */
6
- export function statusToErrorCode(status: number): ErrorCode {
7
- switch (status) {
8
- case 400:
9
- return 'INVALID_REQUEST';
10
- case 401:
11
- case 403:
12
- return 'AUTHENTICATION_FAILED';
13
- case 404:
14
- return 'MODEL_NOT_FOUND';
15
- case 408:
16
- return 'TIMEOUT';
17
- case 413:
18
- return 'CONTEXT_LENGTH_EXCEEDED';
19
- case 429:
20
- return 'RATE_LIMITED';
21
- case 500:
22
- case 502:
23
- case 503:
24
- case 504:
25
- return 'PROVIDER_ERROR';
26
- default:
27
- return 'PROVIDER_ERROR';
28
- }
29
- }
30
-
31
- /**
32
- * Normalize HTTP error responses to UPPError
33
- * Maps HTTP status codes to appropriate ErrorCode values
34
- * Extracts error message from response body when available
35
- */
36
- export async function normalizeHttpError(
37
- response: Response,
38
- provider: string,
39
- modality: Modality
40
- ): Promise<UPPError> {
41
- const code = statusToErrorCode(response.status);
42
- let message = `HTTP ${response.status}: ${response.statusText}`;
43
-
44
- try {
45
- const body = await response.text();
46
- if (body) {
47
- try {
48
- const json = JSON.parse(body);
49
- // Common error message locations across providers
50
- const extractedMessage =
51
- json.error?.message ||
52
- json.message ||
53
- json.error?.error?.message ||
54
- json.detail;
55
-
56
- if (extractedMessage) {
57
- message = extractedMessage;
58
- }
59
- } catch {
60
- // Body is not JSON, use raw text if short
61
- if (body.length < 200) {
62
- message = body;
63
- }
64
- }
65
- }
66
- } catch {
67
- // Failed to read body, use default message
68
- }
69
-
70
- return new UPPError(message, code, provider, modality, response.status);
71
- }
72
-
73
- /**
74
- * Create a network error
75
- */
76
- export function networkError(
77
- error: Error,
78
- provider: string,
79
- modality: Modality
80
- ): UPPError {
81
- return new UPPError(
82
- `Network error: ${error.message}`,
83
- 'NETWORK_ERROR',
84
- provider,
85
- modality,
86
- undefined,
87
- error
88
- );
89
- }
90
-
91
- /**
92
- * Create a timeout error
93
- */
94
- export function timeoutError(
95
- timeout: number,
96
- provider: string,
97
- modality: Modality
98
- ): UPPError {
99
- return new UPPError(
100
- `Request timed out after ${timeout}ms`,
101
- 'TIMEOUT',
102
- provider,
103
- modality
104
- );
105
- }
106
-
107
- /**
108
- * Create a cancelled error
109
- */
110
- export function cancelledError(provider: string, modality: Modality): UPPError {
111
- return new UPPError('Request was cancelled', 'CANCELLED', provider, modality);
112
- }
package/src/http/fetch.ts DELETED
@@ -1,210 +0,0 @@
1
- import type { ProviderConfig } from '../types/provider.ts';
2
- import type { Modality } from '../types/errors.ts';
3
- import { UPPError } from '../types/errors.ts';
4
- import {
5
- normalizeHttpError,
6
- networkError,
7
- timeoutError,
8
- cancelledError,
9
- } from './errors.ts';
10
-
11
- /**
12
- * Default timeout in milliseconds
13
- */
14
- const DEFAULT_TIMEOUT = 120000; // 2 minutes
15
-
16
- /**
17
- * Execute fetch with retry, timeout, and error normalization
18
- *
19
- * @param url - Request URL
20
- * @param init - Fetch init options
21
- * @param config - Provider config
22
- * @param provider - Provider name for error messages
23
- * @param modality - Modality for error messages
24
- */
25
- export async function doFetch(
26
- url: string,
27
- init: RequestInit,
28
- config: ProviderConfig,
29
- provider: string,
30
- modality: Modality
31
- ): Promise<Response> {
32
- const fetchFn = config.fetch ?? fetch;
33
- const timeout = config.timeout ?? DEFAULT_TIMEOUT;
34
- const strategy = config.retryStrategy;
35
-
36
- // Pre-request delay (e.g., token bucket)
37
- if (strategy?.beforeRequest) {
38
- const delay = await strategy.beforeRequest();
39
- if (delay > 0) {
40
- await sleep(delay);
41
- }
42
- }
43
-
44
- let lastError: UPPError | undefined;
45
- let attempt = 0;
46
-
47
- while (true) {
48
- attempt++;
49
-
50
- try {
51
- const response = await fetchWithTimeout(
52
- fetchFn,
53
- url,
54
- init,
55
- timeout,
56
- provider,
57
- modality
58
- );
59
-
60
- // Check for HTTP errors
61
- if (!response.ok) {
62
- const error = await normalizeHttpError(response, provider, modality);
63
-
64
- // Check for Retry-After header
65
- const retryAfter = response.headers.get('Retry-After');
66
- if (retryAfter && strategy) {
67
- const seconds = parseInt(retryAfter, 10);
68
- if (!isNaN(seconds) && 'setRetryAfter' in strategy) {
69
- (strategy as { setRetryAfter: (s: number) => void }).setRetryAfter(
70
- seconds
71
- );
72
- }
73
- }
74
-
75
- // Try to retry
76
- if (strategy) {
77
- const delay = await strategy.onRetry(error, attempt);
78
- if (delay !== null) {
79
- await sleep(delay);
80
- lastError = error;
81
- continue;
82
- }
83
- }
84
-
85
- throw error;
86
- }
87
-
88
- // Success - reset strategy state
89
- strategy?.reset?.();
90
-
91
- return response;
92
- } catch (error) {
93
- // Already a UPPError, handle retry
94
- if (error instanceof UPPError) {
95
- if (strategy) {
96
- const delay = await strategy.onRetry(error, attempt);
97
- if (delay !== null) {
98
- await sleep(delay);
99
- lastError = error;
100
- continue;
101
- }
102
- }
103
- throw error;
104
- }
105
-
106
- // Network error
107
- const uppError = networkError(error as Error, provider, modality);
108
-
109
- if (strategy) {
110
- const delay = await strategy.onRetry(uppError, attempt);
111
- if (delay !== null) {
112
- await sleep(delay);
113
- lastError = uppError;
114
- continue;
115
- }
116
- }
117
-
118
- throw uppError;
119
- }
120
- }
121
- }
122
-
123
- /**
124
- * Fetch with timeout
125
- */
126
- async function fetchWithTimeout(
127
- fetchFn: typeof fetch,
128
- url: string,
129
- init: RequestInit,
130
- timeout: number,
131
- provider: string,
132
- modality: Modality
133
- ): Promise<Response> {
134
- const controller = new AbortController();
135
- const timeoutId = setTimeout(() => controller.abort(), timeout);
136
-
137
- // Merge abort signals if one was provided
138
- const existingSignal = init.signal;
139
- if (existingSignal) {
140
- existingSignal.addEventListener('abort', () => controller.abort());
141
- }
142
-
143
- try {
144
- const response = await fetchFn(url, {
145
- ...init,
146
- signal: controller.signal,
147
- });
148
- return response;
149
- } catch (error) {
150
- if ((error as Error).name === 'AbortError') {
151
- // Check if it was the user's signal or our timeout
152
- if (existingSignal?.aborted) {
153
- throw cancelledError(provider, modality);
154
- }
155
- throw timeoutError(timeout, provider, modality);
156
- }
157
- throw error;
158
- } finally {
159
- clearTimeout(timeoutId);
160
- }
161
- }
162
-
163
- /**
164
- * Sleep for a given number of milliseconds
165
- */
166
- function sleep(ms: number): Promise<void> {
167
- return new Promise((resolve) => setTimeout(resolve, ms));
168
- }
169
-
170
- /**
171
- * Streaming fetch - returns response without checking ok status
172
- * Used when we need to read the stream for SSE
173
- */
174
- export async function doStreamFetch(
175
- url: string,
176
- init: RequestInit,
177
- config: ProviderConfig,
178
- provider: string,
179
- modality: Modality
180
- ): Promise<Response> {
181
- const fetchFn = config.fetch ?? fetch;
182
- const timeout = config.timeout ?? DEFAULT_TIMEOUT;
183
- const strategy = config.retryStrategy;
184
-
185
- // Pre-request delay
186
- if (strategy?.beforeRequest) {
187
- const delay = await strategy.beforeRequest();
188
- if (delay > 0) {
189
- await sleep(delay);
190
- }
191
- }
192
-
193
- // For streaming, we don't retry - the consumer handles errors
194
- try {
195
- const response = await fetchWithTimeout(
196
- fetchFn,
197
- url,
198
- init,
199
- timeout,
200
- provider,
201
- modality
202
- );
203
- return response;
204
- } catch (error) {
205
- if (error instanceof UPPError) {
206
- throw error;
207
- }
208
- throw networkError(error as Error, provider, modality);
209
- }
210
- }
package/src/http/index.ts DELETED
@@ -1,31 +0,0 @@
1
- // Key management
2
- export {
3
- resolveApiKey,
4
- RoundRobinKeys,
5
- WeightedKeys,
6
- DynamicKey,
7
- } from './keys.ts';
8
-
9
- // Retry strategies
10
- export {
11
- ExponentialBackoff,
12
- LinearBackoff,
13
- NoRetry,
14
- TokenBucket,
15
- RetryAfterStrategy,
16
- } from './retry.ts';
17
-
18
- // HTTP fetch
19
- export { doFetch, doStreamFetch } from './fetch.ts';
20
-
21
- // SSE parsing
22
- export { parseSSEStream, parseSimpleTextStream } from './sse.ts';
23
-
24
- // Error utilities
25
- export {
26
- normalizeHttpError,
27
- networkError,
28
- timeoutError,
29
- cancelledError,
30
- statusToErrorCode,
31
- } from './errors.ts';
package/src/http/keys.ts DELETED
@@ -1,136 +0,0 @@
1
- import type { ProviderConfig, KeyStrategy } from '../types/provider.ts';
2
- import { UPPError, type Modality } from '../types/errors.ts';
3
-
4
- /**
5
- * Round-robin through a list of API keys
6
- */
7
- export class RoundRobinKeys implements KeyStrategy {
8
- private keys: string[];
9
- private index = 0;
10
-
11
- constructor(keys: string[]) {
12
- if (keys.length === 0) {
13
- throw new Error('RoundRobinKeys requires at least one key');
14
- }
15
- this.keys = keys;
16
- }
17
-
18
- getKey(): string {
19
- const key = this.keys[this.index]!;
20
- this.index = (this.index + 1) % this.keys.length;
21
- return key;
22
- }
23
- }
24
-
25
- /**
26
- * Weighted random selection of API keys
27
- */
28
- export class WeightedKeys implements KeyStrategy {
29
- private entries: Array<{ key: string; weight: number }>;
30
- private totalWeight: number;
31
-
32
- constructor(keys: Array<{ key: string; weight: number }>) {
33
- if (keys.length === 0) {
34
- throw new Error('WeightedKeys requires at least one key');
35
- }
36
- this.entries = keys;
37
- this.totalWeight = keys.reduce((sum, k) => sum + k.weight, 0);
38
- }
39
-
40
- getKey(): string {
41
- const random = Math.random() * this.totalWeight;
42
- let cumulative = 0;
43
-
44
- for (const entry of this.entries) {
45
- cumulative += entry.weight;
46
- if (random <= cumulative) {
47
- return entry.key;
48
- }
49
- }
50
-
51
- // Fallback to last key (shouldn't happen)
52
- return this.entries[this.entries.length - 1]!.key;
53
- }
54
- }
55
-
56
- /**
57
- * Dynamic key selection based on custom logic
58
- */
59
- export class DynamicKey implements KeyStrategy {
60
- private selector: () => string | Promise<string>;
61
-
62
- constructor(selector: () => string | Promise<string>) {
63
- this.selector = selector;
64
- }
65
-
66
- async getKey(): Promise<string> {
67
- return this.selector();
68
- }
69
- }
70
-
71
- /**
72
- * Check if a value is a KeyStrategy
73
- */
74
- function isKeyStrategy(value: unknown): value is KeyStrategy {
75
- return (
76
- typeof value === 'object' &&
77
- value !== null &&
78
- 'getKey' in value &&
79
- typeof (value as KeyStrategy).getKey === 'function'
80
- );
81
- }
82
-
83
- /**
84
- * Resolve API key from ProviderConfig
85
- * Falls back to environment variable if provided and config.apiKey is not set
86
- * Throws UPPError with AUTHENTICATION_FAILED if no key is available
87
- *
88
- * @param config - Provider configuration
89
- * @param envVar - Environment variable name to check as fallback
90
- * @param provider - Provider name for error messages
91
- * @param modality - Modality for error messages
92
- */
93
- export async function resolveApiKey(
94
- config: ProviderConfig,
95
- envVar?: string,
96
- provider = 'unknown',
97
- modality: Modality = 'llm'
98
- ): Promise<string> {
99
- const { apiKey } = config;
100
-
101
- // If apiKey is provided in config
102
- if (apiKey !== undefined) {
103
- // String
104
- if (typeof apiKey === 'string') {
105
- return apiKey;
106
- }
107
-
108
- // Function
109
- if (typeof apiKey === 'function') {
110
- return apiKey();
111
- }
112
-
113
- // KeyStrategy
114
- if (isKeyStrategy(apiKey)) {
115
- return apiKey.getKey();
116
- }
117
- }
118
-
119
- // Try environment variable
120
- if (envVar) {
121
- const envValue = process.env[envVar];
122
- if (envValue) {
123
- return envValue;
124
- }
125
- }
126
-
127
- // No key found
128
- throw new UPPError(
129
- envVar
130
- ? `API key not found. Set ${envVar} environment variable or provide apiKey in config.`
131
- : 'API key not found. Provide apiKey in config.',
132
- 'AUTHENTICATION_FAILED',
133
- provider,
134
- modality
135
- );
136
- }