@providerprotocol/ai 0.0.20 → 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 (45) hide show
  1. package/dist/anthropic/index.d.ts +184 -14
  2. package/dist/anthropic/index.js +210 -82
  3. package/dist/anthropic/index.js.map +1 -1
  4. package/dist/{chunk-U3FZWV4U.js → chunk-EDENPF3E.js} +5 -2
  5. package/dist/{chunk-U3FZWV4U.js.map → chunk-EDENPF3E.js.map} +1 -1
  6. package/dist/{chunk-UMKWXGO3.js → chunk-M4BMM5IB.js} +86 -2
  7. package/dist/chunk-M4BMM5IB.js.map +1 -0
  8. package/dist/{chunk-P5IRTEM5.js → chunk-Y3GBJNA2.js} +2 -2
  9. package/dist/{chunk-U4JJC2YX.js → chunk-Z4ILICF5.js} +2 -2
  10. package/dist/chunk-Z4ILICF5.js.map +1 -0
  11. package/dist/google/index.d.ts +16 -19
  12. package/dist/google/index.js +14 -36
  13. package/dist/google/index.js.map +1 -1
  14. package/dist/http/index.d.ts +2 -2
  15. package/dist/http/index.js +3 -3
  16. package/dist/index.d.ts +101 -38
  17. package/dist/index.js +69 -43
  18. package/dist/index.js.map +1 -1
  19. package/dist/ollama/index.d.ts +14 -16
  20. package/dist/ollama/index.js +5 -7
  21. package/dist/ollama/index.js.map +1 -1
  22. package/dist/openai/index.d.ts +25 -133
  23. package/dist/openai/index.js +27 -81
  24. package/dist/openai/index.js.map +1 -1
  25. package/dist/openrouter/index.d.ts +28 -53
  26. package/dist/openrouter/index.js +20 -43
  27. package/dist/openrouter/index.js.map +1 -1
  28. package/dist/provider-DGQHYE6I.d.ts +1319 -0
  29. package/dist/proxy/index.d.ts +2 -3
  30. package/dist/proxy/index.js +5 -7
  31. package/dist/proxy/index.js.map +1 -1
  32. package/dist/{retry-DR7YRJDz.d.ts → retry-Pcs3hnbu.d.ts} +2 -2
  33. package/dist/{stream-DRHy6q1a.d.ts → stream-Di9acos2.d.ts} +1 -1
  34. package/dist/xai/index.d.ts +16 -88
  35. package/dist/xai/index.js +30 -58
  36. package/dist/xai/index.js.map +1 -1
  37. package/package.json +4 -1
  38. package/dist/chunk-MSR5P65T.js +0 -39
  39. package/dist/chunk-MSR5P65T.js.map +0 -1
  40. package/dist/chunk-U4JJC2YX.js.map +0 -1
  41. package/dist/chunk-UMKWXGO3.js.map +0 -1
  42. package/dist/content-DEl3z_W2.d.ts +0 -276
  43. package/dist/image-Dhq-Yuq4.d.ts +0 -456
  44. package/dist/provider-BBMBZuGn.d.ts +0 -570
  45. /package/dist/{chunk-P5IRTEM5.js.map → chunk-Y3GBJNA2.js.map} +0 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@providerprotocol/ai",
3
- "version": "0.0.20",
3
+ "version": "0.0.21",
4
4
  "description": "UPP: Unified Provider Protocol for AI inference",
5
5
  "license": "MIT",
6
6
  "homepage": "https://providerprotocol.org",
@@ -100,6 +100,9 @@
100
100
  "openrouter",
101
101
  "xai",
102
102
  "grok",
103
+ "vertex",
104
+ "deepseek",
105
+ "mistral",
103
106
  "provider",
104
107
  "protocol",
105
108
  "unified"
@@ -1,39 +0,0 @@
1
- // src/core/provider.ts
2
- function createProvider(options) {
3
- const fn = function(modelId, _options) {
4
- return { modelId, provider };
5
- };
6
- Object.defineProperties(fn, {
7
- name: {
8
- value: options.name,
9
- writable: false,
10
- configurable: true
11
- },
12
- version: {
13
- value: options.version,
14
- writable: false,
15
- configurable: true
16
- },
17
- modalities: {
18
- value: options.modalities,
19
- writable: false,
20
- configurable: true
21
- }
22
- });
23
- const provider = fn;
24
- if (options.modalities.llm?._setProvider) {
25
- options.modalities.llm._setProvider(provider);
26
- }
27
- if (options.modalities.embedding?._setProvider) {
28
- options.modalities.embedding._setProvider(provider);
29
- }
30
- if (options.modalities.image?._setProvider) {
31
- options.modalities.image._setProvider(provider);
32
- }
33
- return provider;
34
- }
35
-
36
- export {
37
- createProvider
38
- };
39
- //# sourceMappingURL=chunk-MSR5P65T.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/core/provider.ts"],"sourcesContent":["/**\n * @fileoverview Base provider interface and factory for the Universal Provider Protocol.\n *\n * This module provides the foundation for creating AI providers that conform to the\n * UPP specification. Providers are callable functions that create model references\n * and expose modality handlers (LLM, embedding, image).\n *\n * @module core/provider\n */\n\nimport type {\n Provider,\n ModelReference,\n LLMHandler,\n EmbeddingHandler,\n ImageHandler,\n LLMProvider,\n EmbeddingProvider,\n ImageProvider,\n} from '../types/provider.ts';\n\n/**\n * Configuration options for creating a new provider.\n *\n * @example\n * ```typescript\n * const options: CreateProviderOptions = {\n * name: 'my-provider',\n * version: '1.0.0',\n * modalities: {\n * llm: createLLMHandler(),\n * embedding: createEmbeddingHandler(),\n * },\n * };\n * ```\n */\nexport interface CreateProviderOptions {\n /** Unique identifier for the provider */\n name: string;\n /** Semantic version string for the provider implementation */\n version: string;\n /** Handlers for supported modalities (LLM, embedding, image generation) */\n modalities: {\n /** Handler for language model completions */\n llm?: LLMHandler;\n /** Handler for text embeddings */\n embedding?: EmbeddingHandler;\n /** Handler for image generation */\n image?: ImageHandler;\n };\n}\n\n/**\n * Creates a provider factory function with attached modality handlers.\n *\n * The returned provider is a callable function that creates model references\n * when invoked with a model ID. It also exposes `name`, `version`, and\n * `modalities` properties for introspection.\n *\n * @typeParam TOptions - Provider-specific options type (defaults to unknown)\n * @param options - Provider configuration including name, version, and handlers\n * @returns A callable Provider with modalities attached\n *\n * @example\n * ```typescript\n * // Create a basic provider\n * const anthropic = createProvider({\n * name: 'anthropic',\n * version: '1.0.0',\n * modalities: { llm: createLLMHandler() },\n * });\n *\n * // Use the provider to create a model reference\n * const model = anthropic('claude-sonnet-4-20250514');\n *\n * // Provider with custom options type\n * interface MyOptions { apiVersion?: 'v1' | 'v2' }\n * const myProvider = createProvider<MyOptions>({\n * name: 'my-provider',\n * version: '1.0.0',\n * modalities: { llm: handler },\n * });\n * ```\n */\nexport function createProvider<TOptions = unknown>(\n options: CreateProviderOptions\n): Provider<TOptions> {\n const fn = function (modelId: string, _options?: TOptions): ModelReference<TOptions> {\n return { modelId, provider };\n };\n\n Object.defineProperties(fn, {\n name: {\n value: options.name,\n writable: false,\n configurable: true,\n },\n version: {\n value: options.version,\n writable: false,\n configurable: true,\n },\n modalities: {\n value: options.modalities,\n writable: false,\n configurable: true,\n },\n });\n\n const provider = fn as Provider<TOptions>;\n\n if (options.modalities.llm?._setProvider) {\n options.modalities.llm._setProvider(provider as unknown as LLMProvider);\n }\n if (options.modalities.embedding?._setProvider) {\n options.modalities.embedding._setProvider(provider as unknown as EmbeddingProvider);\n }\n if (options.modalities.image?._setProvider) {\n options.modalities.image._setProvider(provider as unknown as ImageProvider);\n }\n\n return provider;\n}\n"],"mappings":";AAoFO,SAAS,eACd,SACoB;AACpB,QAAM,KAAK,SAAU,SAAiB,UAA+C;AACnF,WAAO,EAAE,SAAS,SAAS;AAAA,EAC7B;AAEA,SAAO,iBAAiB,IAAI;AAAA,IAC1B,MAAM;AAAA,MACJ,OAAO,QAAQ;AAAA,MACf,UAAU;AAAA,MACV,cAAc;AAAA,IAChB;AAAA,IACA,SAAS;AAAA,MACP,OAAO,QAAQ;AAAA,MACf,UAAU;AAAA,MACV,cAAc;AAAA,IAChB;AAAA,IACA,YAAY;AAAA,MACV,OAAO,QAAQ;AAAA,MACf,UAAU;AAAA,MACV,cAAc;AAAA,IAChB;AAAA,EACF,CAAC;AAED,QAAM,WAAW;AAEjB,MAAI,QAAQ,WAAW,KAAK,cAAc;AACxC,YAAQ,WAAW,IAAI,aAAa,QAAkC;AAAA,EACxE;AACA,MAAI,QAAQ,WAAW,WAAW,cAAc;AAC9C,YAAQ,WAAW,UAAU,aAAa,QAAwC;AAAA,EACpF;AACA,MAAI,QAAQ,WAAW,OAAO,cAAc;AAC1C,YAAQ,WAAW,MAAM,aAAa,QAAoC;AAAA,EAC5E;AAEA,SAAO;AACT;","names":[]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/http/retry.ts"],"sourcesContent":["/**\n * Retry strategies for handling transient failures in HTTP requests.\n * @module http/retry\n */\n\nimport type { RetryStrategy } from '../types/provider.ts';\nimport type { UPPError } from '../types/errors.ts';\n\n/**\n * Implements exponential backoff with optional jitter for retry delays.\n *\n * The delay between retries doubles with each attempt, helping to:\n * - Avoid overwhelming servers during outages\n * - Reduce thundering herd effects when many clients retry simultaneously\n * - Give transient issues time to resolve\n *\n * Delay formula: min(baseDelay * 2^(attempt-1), maxDelay)\n * With jitter: delay * random(0.5, 1.0)\n *\n * Only retries on transient errors: RATE_LIMITED, NETWORK_ERROR, TIMEOUT, PROVIDER_ERROR\n *\n * @implements {RetryStrategy}\n *\n * @example\n * ```typescript\n * // Default configuration (3 retries, 1s base, 30s max, jitter enabled)\n * const retry = new ExponentialBackoff();\n *\n * // Custom configuration\n * const customRetry = new ExponentialBackoff({\n * maxAttempts: 5, // Up to 5 retry attempts\n * baseDelay: 500, // Start with 500ms delay\n * maxDelay: 60000, // Cap at 60 seconds\n * jitter: false // Disable random jitter\n * });\n *\n * // Use with provider\n * const provider = createOpenAI({\n * retryStrategy: customRetry\n * });\n * ```\n */\nexport class ExponentialBackoff implements RetryStrategy {\n private maxAttempts: number;\n private baseDelay: number;\n private maxDelay: number;\n private jitter: boolean;\n\n /**\n * Creates a new ExponentialBackoff instance.\n *\n * @param options - Configuration options\n * @param options.maxAttempts - Maximum number of retry attempts (default: 3)\n * @param options.baseDelay - Initial delay in milliseconds (default: 1000)\n * @param options.maxDelay - Maximum delay cap in milliseconds (default: 30000)\n * @param options.jitter - Whether to add random jitter to delays (default: true)\n */\n constructor(options: {\n maxAttempts?: number;\n baseDelay?: number;\n maxDelay?: number;\n jitter?: boolean;\n } = {}) {\n this.maxAttempts = options.maxAttempts ?? 3;\n this.baseDelay = options.baseDelay ?? 1000;\n this.maxDelay = options.maxDelay ?? 30000;\n this.jitter = options.jitter ?? true;\n }\n\n /**\n * Determines whether to retry and calculates the delay.\n *\n * @param error - The error that triggered the retry\n * @param attempt - Current attempt number (1-indexed)\n * @returns Delay in milliseconds before next retry, or null to stop retrying\n */\n onRetry(error: UPPError, attempt: number): number | null {\n if (attempt > this.maxAttempts) {\n return null;\n }\n\n if (!this.isRetryable(error)) {\n return null;\n }\n\n let delay = this.baseDelay * Math.pow(2, attempt - 1);\n delay = Math.min(delay, this.maxDelay);\n\n if (this.jitter) {\n delay = delay * (0.5 + Math.random());\n }\n\n return Math.floor(delay);\n }\n\n /**\n * Checks if an error is eligible for retry.\n *\n * @param error - The error to evaluate\n * @returns True if the error is transient and retryable\n */\n private isRetryable(error: UPPError): boolean {\n return (\n error.code === 'RATE_LIMITED' ||\n error.code === 'NETWORK_ERROR' ||\n error.code === 'TIMEOUT' ||\n error.code === 'PROVIDER_ERROR'\n );\n }\n}\n\n/**\n * Implements linear backoff where delays increase proportionally with each attempt.\n *\n * Unlike exponential backoff, linear backoff increases delays at a constant rate:\n * - Attempt 1: delay * 1 (e.g., 1000ms)\n * - Attempt 2: delay * 2 (e.g., 2000ms)\n * - Attempt 3: delay * 3 (e.g., 3000ms)\n *\n * This strategy is simpler and more predictable than exponential backoff,\n * suitable for scenarios where gradual delay increase is preferred over\n * aggressive backoff.\n *\n * Only retries on transient errors: RATE_LIMITED, NETWORK_ERROR, TIMEOUT, PROVIDER_ERROR\n *\n * @implements {RetryStrategy}\n *\n * @example\n * ```typescript\n * // Default configuration (3 retries, 1s delay increment)\n * const retry = new LinearBackoff();\n *\n * // Custom configuration\n * const customRetry = new LinearBackoff({\n * maxAttempts: 4, // Up to 4 retry attempts\n * delay: 2000 // 2s, 4s, 6s, 8s delays\n * });\n *\n * // Use with provider\n * const provider = createAnthropic({\n * retryStrategy: customRetry\n * });\n * ```\n */\nexport class LinearBackoff implements RetryStrategy {\n private maxAttempts: number;\n private delay: number;\n\n /**\n * Creates a new LinearBackoff instance.\n *\n * @param options - Configuration options\n * @param options.maxAttempts - Maximum number of retry attempts (default: 3)\n * @param options.delay - Base delay multiplier in milliseconds (default: 1000)\n */\n constructor(options: {\n maxAttempts?: number;\n delay?: number;\n } = {}) {\n this.maxAttempts = options.maxAttempts ?? 3;\n this.delay = options.delay ?? 1000;\n }\n\n /**\n * Determines whether to retry and calculates the linear delay.\n *\n * @param error - The error that triggered the retry\n * @param attempt - Current attempt number (1-indexed)\n * @returns Delay in milliseconds (delay * attempt), or null to stop retrying\n */\n onRetry(error: UPPError, attempt: number): number | null {\n if (attempt > this.maxAttempts) {\n return null;\n }\n\n if (!this.isRetryable(error)) {\n return null;\n }\n\n return this.delay * attempt;\n }\n\n /**\n * Checks if an error is eligible for retry.\n *\n * @param error - The error to evaluate\n * @returns True if the error is transient and retryable\n */\n private isRetryable(error: UPPError): boolean {\n return (\n error.code === 'RATE_LIMITED' ||\n error.code === 'NETWORK_ERROR' ||\n error.code === 'TIMEOUT' ||\n error.code === 'PROVIDER_ERROR'\n );\n }\n}\n\n/**\n * Disables all retry behavior, failing immediately on any error.\n *\n * Use this strategy when:\n * - Retries are handled at a higher level in your application\n * - You want immediate failure feedback\n * - The operation is not idempotent\n * - Time sensitivity requires fast failure\n *\n * @implements {RetryStrategy}\n *\n * @example\n * ```typescript\n * // Disable retries for time-sensitive operations\n * const provider = createOpenAI({\n * retryStrategy: new NoRetry()\n * });\n * ```\n */\nexport class NoRetry implements RetryStrategy {\n /**\n * Always returns null to indicate no retry should be attempted.\n *\n * @returns Always returns null\n */\n onRetry(): null {\n return null;\n }\n}\n\n/**\n * Implements token bucket rate limiting with automatic refill.\n *\n * The token bucket algorithm provides smooth rate limiting by:\n * - Maintaining a bucket of tokens that replenish over time\n * - Consuming one token per request\n * - Delaying requests when the bucket is empty\n * - Allowing burst traffic up to the bucket capacity\n *\n * This is particularly useful for:\n * - Client-side rate limiting to avoid hitting API rate limits\n * - Smoothing request patterns to maintain consistent throughput\n * - Preventing accidental API abuse\n *\n * Unlike other retry strategies, TokenBucket implements {@link beforeRequest}\n * to proactively delay requests before they are made.\n *\n * @implements {RetryStrategy}\n *\n * @example\n * ```typescript\n * // Allow 10 requests burst, refill 1 token per second\n * const bucket = new TokenBucket({\n * maxTokens: 10, // Burst capacity\n * refillRate: 1, // Tokens per second\n * maxAttempts: 3 // Retry attempts on rate limit\n * });\n *\n * // Aggressive rate limiting: 5 req/s sustained\n * const strictBucket = new TokenBucket({\n * maxTokens: 5,\n * refillRate: 5\n * });\n *\n * // Use with provider\n * const provider = createOpenAI({\n * retryStrategy: bucket\n * });\n * ```\n */\nexport class TokenBucket implements RetryStrategy {\n private tokens: number;\n private maxTokens: number;\n private refillRate: number;\n private lastRefill: number;\n private maxAttempts: number;\n\n /**\n * Creates a new TokenBucket instance.\n *\n * @param options - Configuration options\n * @param options.maxTokens - Maximum bucket capacity (default: 10)\n * @param options.refillRate - Tokens added per second (default: 1)\n * @param options.maxAttempts - Maximum retry attempts on rate limit (default: 3)\n */\n constructor(options: {\n maxTokens?: number;\n refillRate?: number;\n maxAttempts?: number;\n } = {}) {\n this.maxTokens = options.maxTokens ?? 10;\n this.refillRate = options.refillRate ?? 1;\n this.maxAttempts = options.maxAttempts ?? 3;\n this.tokens = this.maxTokens;\n this.lastRefill = Date.now();\n }\n\n /**\n * Called before each request to consume a token or calculate wait time.\n *\n * Refills the bucket based on elapsed time, then either:\n * - Returns 0 if a token is available (consumed immediately)\n * - Returns the wait time in milliseconds until the next token\n *\n * @returns Delay in milliseconds before the request can proceed\n */\n beforeRequest(): number {\n this.refill();\n\n if (this.tokens >= 1) {\n this.tokens -= 1;\n return 0;\n }\n\n const msPerToken = 1000 / this.refillRate;\n return Math.ceil(msPerToken);\n }\n\n /**\n * Handles retry logic for rate-limited requests.\n *\n * Only retries on RATE_LIMITED errors, waiting for bucket refill.\n *\n * @param error - The error that triggered the retry\n * @param attempt - Current attempt number (1-indexed)\n * @returns Delay in milliseconds (time for 2 tokens), or null to stop\n */\n onRetry(error: UPPError, attempt: number): number | null {\n if (attempt > this.maxAttempts) {\n return null;\n }\n\n if (error.code !== 'RATE_LIMITED') {\n return null;\n }\n\n const msPerToken = 1000 / this.refillRate;\n return Math.ceil(msPerToken * 2);\n }\n\n /**\n * Resets the bucket to full capacity.\n *\n * Called automatically on successful requests to restore available tokens.\n */\n reset(): void {\n this.tokens = this.maxTokens;\n this.lastRefill = Date.now();\n }\n\n /**\n * Refills the bucket based on elapsed time since last refill.\n */\n private refill(): void {\n const now = Date.now();\n const elapsed = (now - this.lastRefill) / 1000;\n const newTokens = elapsed * this.refillRate;\n\n this.tokens = Math.min(this.maxTokens, this.tokens + newTokens);\n this.lastRefill = now;\n }\n}\n\n/**\n * Respects server-provided Retry-After headers for optimal retry timing.\n *\n * When servers return a 429 (Too Many Requests) response, they often include\n * a Retry-After header indicating when the client should retry. This strategy\n * uses that information for precise retry timing.\n *\n * Benefits over fixed backoff strategies:\n * - Follows server recommendations for optimal retry timing\n * - Avoids retrying too early and wasting requests\n * - Adapts to dynamic rate limit windows\n *\n * If no Retry-After header is provided, falls back to a configurable delay.\n * Only retries on RATE_LIMITED errors.\n *\n * @implements {RetryStrategy}\n *\n * @example\n * ```typescript\n * // Use server-recommended retry timing\n * const retryAfter = new RetryAfterStrategy({\n * maxAttempts: 5, // Retry up to 5 times\n * fallbackDelay: 10000 // 10s fallback if no header\n * });\n *\n * // The doFetch function automatically calls setRetryAfter\n * // when a Retry-After header is present in the response\n *\n * const provider = createOpenAI({\n * retryStrategy: retryAfter\n * });\n * ```\n */\nexport class RetryAfterStrategy implements RetryStrategy {\n private maxAttempts: number;\n private fallbackDelay: number;\n private lastRetryAfter?: number;\n\n /**\n * Creates a new RetryAfterStrategy instance.\n *\n * @param options - Configuration options\n * @param options.maxAttempts - Maximum number of retry attempts (default: 3)\n * @param options.fallbackDelay - Delay in ms when no Retry-After header (default: 5000)\n */\n constructor(options: {\n maxAttempts?: number;\n fallbackDelay?: number;\n } = {}) {\n this.maxAttempts = options.maxAttempts ?? 3;\n this.fallbackDelay = options.fallbackDelay ?? 5000;\n }\n\n /**\n * Sets the retry delay from a Retry-After header value.\n *\n * Called by doFetch when a Retry-After header is present in the response.\n * The value is used for the next onRetry call and then cleared.\n *\n * @param seconds - The Retry-After value in seconds\n */\n setRetryAfter(seconds: number): void {\n this.lastRetryAfter = seconds * 1000;\n }\n\n /**\n * Determines retry delay using Retry-After header or fallback.\n *\n * @param error - The error that triggered the retry\n * @param attempt - Current attempt number (1-indexed)\n * @returns Delay from Retry-After header or fallback, null to stop\n */\n onRetry(error: UPPError, attempt: number): number | null {\n if (attempt > this.maxAttempts) {\n return null;\n }\n\n if (error.code !== 'RATE_LIMITED') {\n return null;\n }\n\n const delay = this.lastRetryAfter ?? this.fallbackDelay;\n this.lastRetryAfter = undefined;\n return delay;\n }\n}\n"],"mappings":";AA0CO,IAAM,qBAAN,MAAkD;AAAA,EAC/C;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWR,YAAY,UAKR,CAAC,GAAG;AACN,SAAK,cAAc,QAAQ,eAAe;AAC1C,SAAK,YAAY,QAAQ,aAAa;AACtC,SAAK,WAAW,QAAQ,YAAY;AACpC,SAAK,SAAS,QAAQ,UAAU;AAAA,EAClC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,QAAQ,OAAiB,SAAgC;AACvD,QAAI,UAAU,KAAK,aAAa;AAC9B,aAAO;AAAA,IACT;AAEA,QAAI,CAAC,KAAK,YAAY,KAAK,GAAG;AAC5B,aAAO;AAAA,IACT;AAEA,QAAI,QAAQ,KAAK,YAAY,KAAK,IAAI,GAAG,UAAU,CAAC;AACpD,YAAQ,KAAK,IAAI,OAAO,KAAK,QAAQ;AAErC,QAAI,KAAK,QAAQ;AACf,cAAQ,SAAS,MAAM,KAAK,OAAO;AAAA,IACrC;AAEA,WAAO,KAAK,MAAM,KAAK;AAAA,EACzB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQQ,YAAY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AAM,SAAS,gBAAgB;AACjC,aAAO;AAAA,IACT;AAEA,UAAM,QAAQ,KAAK,kBAAkB,KAAK;AAC1C,SAAK,iBAAiB;AACtB,WAAO;AAAA,EACT;AACF;","names":[]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/utils/id.ts","../src/types/messages.ts"],"sourcesContent":["/**\n * @fileoverview ID generation utilities for the Universal Provider Protocol.\n *\n * Provides functions for generating unique identifiers used throughout UPP,\n * including message IDs, tool call IDs, and other internal references.\n *\n * @module utils/id\n */\n\n/**\n * Generates a unique UUID v4 identifier.\n *\n * Uses the native `crypto.randomUUID()` when available for cryptographically\n * secure randomness. Falls back to a Math.random-based implementation for\n * environments without Web Crypto API support.\n *\n * @returns A UUID v4 string in the format `xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx`\n *\n * @example\n * ```typescript\n * const messageId = generateId();\n * // => \"f47ac10b-58cc-4372-a567-0e02b2c3d479\"\n * ```\n */\nexport function generateId(): string {\n if (typeof crypto !== 'undefined' && crypto.randomUUID) {\n return crypto.randomUUID();\n }\n\n return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {\n const r = (Math.random() * 16) | 0;\n const v = c === 'x' ? r : (r & 0x3) | 0x8;\n return v.toString(16);\n });\n}\n\n/**\n * Generates a short alphanumeric identifier.\n *\n * Creates a 12-character random string using alphanumeric characters (a-z, A-Z, 0-9).\n * Useful for tool call IDs and other cases where a full UUID is not required.\n *\n * @param prefix - Optional prefix to prepend to the generated ID\n * @returns A string containing the prefix followed by 12 random alphanumeric characters\n *\n * @example\n * ```typescript\n * const toolCallId = generateShortId('call_');\n * // => \"call_aB3xY9mK2pQr\"\n *\n * const simpleId = generateShortId();\n * // => \"Tz4wN8vL1sHj\"\n * ```\n */\nexport function generateShortId(prefix = ''): string {\n const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';\n let result = prefix;\n for (let i = 0; i < 12; i++) {\n result += chars.charAt(Math.floor(Math.random() * chars.length));\n }\n return result;\n}\n","/**\n * @fileoverview Message types for conversation history.\n *\n * Defines the message classes used to represent conversation turns\n * between users and assistants, including support for multimodal\n * content and tool calls.\n *\n * @module types/messages\n */\n\nimport { generateId } from '../utils/id.ts';\nimport type {\n ContentBlock,\n TextBlock,\n ImageBlock,\n AudioBlock,\n VideoBlock,\n UserContent,\n AssistantContent,\n} from './content.ts';\nimport type { ToolCall, ToolResult } from './tool.ts';\n\n/**\n * Message serialized to JSON format.\n * Picks common fields from Message, converts timestamp to string.\n */\nexport type MessageJSON = Pick<Message, 'id' | 'type' | 'metadata'> & {\n timestamp: string;\n content: ContentBlock[];\n toolCalls?: ToolCall[];\n results?: ToolResult[];\n};\n\n/**\n * Message type discriminator.\n *\n * Used to distinguish between different message types in a conversation.\n */\nexport type MessageType = 'user' | 'assistant' | 'tool_result';\n\n/**\n * Provider-namespaced metadata for messages.\n *\n * Each provider can attach its own metadata under its namespace,\n * preventing conflicts between different providers.\n *\n * @example\n * ```typescript\n * const metadata: MessageMetadata = {\n * openai: { model: 'gpt-4', finishReason: 'stop' },\n * anthropic: { model: 'claude-3', stopReason: 'end_turn' }\n * };\n * ```\n */\nexport interface MessageMetadata {\n [provider: string]: Record<string, unknown> | undefined;\n}\n\n/**\n * Options for constructing messages.\n */\nexport interface MessageOptions {\n /** Custom message ID (auto-generated if not provided) */\n id?: string;\n\n /** Provider-specific metadata */\n metadata?: MessageMetadata;\n}\n\n/**\n * Abstract base class for all message types.\n *\n * Provides common functionality for user, assistant, and tool result\n * messages, including content accessors and metadata handling.\n *\n * @example\n * ```typescript\n * // Access text content from any message\n * const text = message.text;\n *\n * // Access images\n * const images = message.images;\n * ```\n */\nexport abstract class Message {\n /** Unique message identifier */\n readonly id: string;\n\n /** Timestamp when the message was created */\n readonly timestamp: Date;\n\n /** Provider-specific metadata, namespaced by provider name */\n readonly metadata?: MessageMetadata;\n\n /** Message type discriminator (implemented by subclasses) */\n abstract readonly type: MessageType;\n\n /**\n * Returns the content blocks for this message.\n * Implemented by subclasses to provide type-specific content.\n */\n protected abstract getContent(): ContentBlock[];\n\n /**\n * Creates a new message instance.\n *\n * @param options - Optional message ID and metadata\n */\n constructor(options?: MessageOptions) {\n this.id = options?.id ?? generateId();\n this.timestamp = new Date();\n this.metadata = options?.metadata;\n }\n\n /**\n * Concatenated text content from all text blocks.\n * Blocks are joined with double newlines.\n */\n get text(): string {\n return this.getContent()\n .filter((block): block is TextBlock => block.type === 'text')\n .map((block) => block.text)\n .join('\\n\\n');\n }\n\n /**\n * All image content blocks in this message.\n */\n get images(): ImageBlock[] {\n return this.getContent().filter((block): block is ImageBlock => block.type === 'image');\n }\n\n /**\n * All audio content blocks in this message.\n */\n get audio(): AudioBlock[] {\n return this.getContent().filter((block): block is AudioBlock => block.type === 'audio');\n }\n\n /**\n * All video content blocks in this message.\n */\n get video(): VideoBlock[] {\n return this.getContent().filter((block): block is VideoBlock => block.type === 'video');\n }\n}\n\n/**\n * User input message.\n *\n * Represents a message from the user, which can contain text and/or\n * multimodal content like images, audio, or video.\n *\n * @example\n * ```typescript\n * // Simple text message\n * const msg = new UserMessage('Hello, world!');\n *\n * // Multimodal message\n * const msg = new UserMessage([\n * { type: 'text', text: 'What is in this image?' },\n * { type: 'image', source: { type: 'url', url: '...' }, mimeType: 'image/png' }\n * ]);\n * ```\n */\nexport class UserMessage extends Message {\n /** Message type discriminator */\n readonly type = 'user' as const;\n\n /** Content blocks in this message */\n readonly content: UserContent[];\n\n /**\n * Creates a new user message.\n *\n * @param content - String (converted to TextBlock) or array of content blocks\n * @param options - Optional message ID and metadata\n */\n constructor(content: string | UserContent[], options?: MessageOptions) {\n super(options);\n if (typeof content === 'string') {\n this.content = [{ type: 'text', text: content }];\n } else {\n this.content = content;\n }\n }\n\n protected getContent(): ContentBlock[] {\n return this.content;\n }\n}\n\n/**\n * Assistant response message.\n *\n * Represents a response from the AI assistant, which may contain\n * text, media content, and/or tool call requests.\n *\n * @example\n * ```typescript\n * // Simple text response\n * const msg = new AssistantMessage('Hello! How can I help?');\n *\n * // Response with tool calls\n * const msg = new AssistantMessage(\n * 'Let me check the weather...',\n * [{ toolCallId: 'call_1', toolName: 'get_weather', arguments: { location: 'NYC' } }]\n * );\n * ```\n */\nexport class AssistantMessage extends Message {\n /** Message type discriminator */\n readonly type = 'assistant' as const;\n\n /** Content blocks in this message */\n readonly content: AssistantContent[];\n\n /** Tool calls requested by the model (if any) */\n readonly toolCalls?: ToolCall[];\n\n /**\n * Creates a new assistant message.\n *\n * @param content - String (converted to TextBlock) or array of content blocks\n * @param toolCalls - Tool calls requested by the model\n * @param options - Optional message ID and metadata\n */\n constructor(\n content: string | AssistantContent[],\n toolCalls?: ToolCall[],\n options?: MessageOptions\n ) {\n super(options);\n if (typeof content === 'string') {\n this.content = [{ type: 'text', text: content }];\n } else {\n this.content = content;\n }\n this.toolCalls = toolCalls;\n }\n\n protected getContent(): ContentBlock[] {\n return this.content;\n }\n\n /**\n * Whether this message contains tool call requests.\n */\n get hasToolCalls(): boolean {\n return this.toolCalls !== undefined && this.toolCalls.length > 0;\n }\n}\n\n/**\n * Tool execution result message.\n *\n * Contains the results of executing one or more tool calls,\n * sent back to the model for further processing.\n *\n * @example\n * ```typescript\n * const msg = new ToolResultMessage([\n * { toolCallId: 'call_1', result: { temperature: 72, conditions: 'sunny' } },\n * { toolCallId: 'call_2', result: 'File not found', isError: true }\n * ]);\n * ```\n */\nexport class ToolResultMessage extends Message {\n /** Message type discriminator */\n readonly type = 'tool_result' as const;\n\n /** Results from tool executions */\n readonly results: ToolResult[];\n\n /**\n * Creates a new tool result message.\n *\n * @param results - Array of tool execution results\n * @param options - Optional message ID and metadata\n */\n constructor(results: ToolResult[], options?: MessageOptions) {\n super(options);\n this.results = results;\n }\n\n protected getContent(): ContentBlock[] {\n return this.results.map((result) => ({\n type: 'text' as const,\n text:\n typeof result.result === 'string'\n ? result.result\n : JSON.stringify(result.result),\n }));\n }\n}\n\n/**\n * Type guard for UserMessage.\n *\n * @param msg - The message to check\n * @returns True if the message is a UserMessage\n *\n * @example\n * ```typescript\n * if (isUserMessage(msg)) {\n * console.log('User said:', msg.text);\n * }\n * ```\n */\nexport function isUserMessage(msg: Message): msg is UserMessage {\n return msg.type === 'user';\n}\n\n/**\n * Type guard for AssistantMessage.\n *\n * @param msg - The message to check\n * @returns True if the message is an AssistantMessage\n *\n * @example\n * ```typescript\n * if (isAssistantMessage(msg)) {\n * console.log('Assistant said:', msg.text);\n * if (msg.hasToolCalls) {\n * console.log('Tool calls:', msg.toolCalls);\n * }\n * }\n * ```\n */\nexport function isAssistantMessage(msg: Message): msg is AssistantMessage {\n return msg.type === 'assistant';\n}\n\n/**\n * Type guard for ToolResultMessage.\n *\n * @param msg - The message to check\n * @returns True if the message is a ToolResultMessage\n *\n * @example\n * ```typescript\n * if (isToolResultMessage(msg)) {\n * for (const result of msg.results) {\n * console.log(`Tool ${result.toolCallId}:`, result.result);\n * }\n * }\n * ```\n */\nexport function isToolResultMessage(msg: Message): msg is ToolResultMessage {\n return msg.type === 'tool_result';\n}\n"],"mappings":";AAwBO,SAAS,aAAqB;AACnC,MAAI,OAAO,WAAW,eAAe,OAAO,YAAY;AACtD,WAAO,OAAO,WAAW;AAAA,EAC3B;AAEA,SAAO,uCAAuC,QAAQ,SAAS,CAAC,MAAM;AACpE,UAAM,IAAK,KAAK,OAAO,IAAI,KAAM;AACjC,UAAM,IAAI,MAAM,MAAM,IAAK,IAAI,IAAO;AACtC,WAAO,EAAE,SAAS,EAAE;AAAA,EACtB,CAAC;AACH;;;ACkDO,IAAe,UAAf,MAAuB;AAAA;AAAA,EAEnB;AAAA;AAAA,EAGA;AAAA;AAAA,EAGA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAgBT,YAAY,SAA0B;AACpC,SAAK,KAAK,SAAS,MAAM,WAAW;AACpC,SAAK,YAAY,oBAAI,KAAK;AAC1B,SAAK,WAAW,SAAS;A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ACL,WAAK,UAAU;AAAA,IACjB;AACA,SAAK,YAAY;AAAA,EACnB;AAAA,EAEU,aAA6B;AACrC,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA,EAKA,IAAI,eAAwB;AAC1B,WAAO,KAAK,cAAc,UAAa,KAAK,UAAU,SAAS;AAAA,EACjE;AACF;AAgBO,IAAM,oBAAN,cAAgC,QAAQ;AAAA;AAAA,EAEpC,OAAO;AAAA;AAAA,EAGP;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQT,YAAY,SAAuB,SAA0B;AAC3D,UAAM,OAAO;AACb,SAAK,UAAU;AAAA,EACjB;AAAA,EAEU,aAA6B;AACrC,WAAO,KAAK,QAAQ,IAAI,CAAC,YAAY;AAAA,MACnC,MAAM;AAAA,MACN,MACE,OAAO,OAAO,WAAW,WACrB,OAAO,SACP,KAAK,UAAU,OAAO,MAAM;AAAA,IACpC,EAAE;AAAA,EACJ;AACF;AAeO,SAAS,cAAc,KAAkC;AAC9D,SAAO,IAAI,SAAS;AACtB;AAkBO,SAAS,mBAAmB,KAAuC;AACxE,SAAO,IAAI,SAAS;AACtB;AAiBO,SAAS,oBAAoB,KAAwC;AAC1E,SAAO,IAAI,SAAS;AACtB;","names":[]}
@@ -1,276 +0,0 @@
1
- /**
2
- * @fileoverview Content block types for multimodal messages.
3
- *
4
- * Defines the various content block types that can be included in
5
- * user and assistant messages, supporting text, images, audio, video,
6
- * and arbitrary binary data.
7
- *
8
- * @module types/content
9
- */
10
- /**
11
- * Image source variants for ImageBlock.
12
- *
13
- * Images can be provided as base64-encoded strings, URLs, or raw bytes.
14
- *
15
- * @example
16
- * ```typescript
17
- * // Base64 encoded image
18
- * const base64Source: ImageSource = {
19
- * type: 'base64',
20
- * data: 'iVBORw0KGgo...'
21
- * };
22
- *
23
- * // URL reference
24
- * const urlSource: ImageSource = {
25
- * type: 'url',
26
- * url: 'https://example.com/image.png'
27
- * };
28
- *
29
- * // Raw bytes
30
- * const bytesSource: ImageSource = {
31
- * type: 'bytes',
32
- * data: new Uint8Array([...])
33
- * };
34
- * ```
35
- */
36
- type ImageSource = {
37
- type: 'base64';
38
- data: string;
39
- } | {
40
- type: 'url';
41
- url: string;
42
- } | {
43
- type: 'bytes';
44
- data: Uint8Array;
45
- };
46
- /**
47
- * Text content block.
48
- *
49
- * The most common content block type, containing plain text content.
50
- *
51
- * @example
52
- * ```typescript
53
- * const textBlock: TextBlock = {
54
- * type: 'text',
55
- * text: 'Hello, world!'
56
- * };
57
- * ```
58
- */
59
- interface TextBlock {
60
- /** Discriminator for text blocks */
61
- type: 'text';
62
- /** The text content */
63
- text: string;
64
- }
65
- /**
66
- * Image content block.
67
- *
68
- * Contains an image with its source data and metadata.
69
- *
70
- * @example
71
- * ```typescript
72
- * const imageBlock: ImageBlock = {
73
- * type: 'image',
74
- * source: { type: 'url', url: 'https://example.com/photo.jpg' },
75
- * mimeType: 'image/jpeg',
76
- * width: 1920,
77
- * height: 1080
78
- * };
79
- * ```
80
- */
81
- interface ImageBlock {
82
- /** Discriminator for image blocks */
83
- type: 'image';
84
- /** The image data source */
85
- source: ImageSource;
86
- /** MIME type of the image (e.g., 'image/png', 'image/jpeg') */
87
- mimeType: string;
88
- /** Image width in pixels */
89
- width?: number;
90
- /** Image height in pixels */
91
- height?: number;
92
- }
93
- /**
94
- * Audio content block.
95
- *
96
- * Contains audio data with its metadata.
97
- *
98
- * @example
99
- * ```typescript
100
- * const audioBlock: AudioBlock = {
101
- * type: 'audio',
102
- * data: audioBytes,
103
- * mimeType: 'audio/mp3',
104
- * duration: 120.5
105
- * };
106
- * ```
107
- */
108
- interface AudioBlock {
109
- /** Discriminator for audio blocks */
110
- type: 'audio';
111
- /** Raw audio data */
112
- data: Uint8Array;
113
- /** MIME type of the audio (e.g., 'audio/mp3', 'audio/wav') */
114
- mimeType: string;
115
- /** Duration in seconds */
116
- duration?: number;
117
- }
118
- /**
119
- * Video content block.
120
- *
121
- * Contains video data with its metadata.
122
- *
123
- * @example
124
- * ```typescript
125
- * const videoBlock: VideoBlock = {
126
- * type: 'video',
127
- * data: videoBytes,
128
- * mimeType: 'video/mp4',
129
- * duration: 30,
130
- * width: 1920,
131
- * height: 1080
132
- * };
133
- * ```
134
- */
135
- interface VideoBlock {
136
- /** Discriminator for video blocks */
137
- type: 'video';
138
- /** Raw video data */
139
- data: Uint8Array;
140
- /** MIME type of the video (e.g., 'video/mp4', 'video/webm') */
141
- mimeType: string;
142
- /** Duration in seconds */
143
- duration?: number;
144
- /** Video width in pixels */
145
- width?: number;
146
- /** Video height in pixels */
147
- height?: number;
148
- }
149
- /**
150
- * Binary content block for arbitrary data.
151
- *
152
- * A generic block type for data that doesn't fit other categories.
153
- *
154
- * @example
155
- * ```typescript
156
- * const binaryBlock: BinaryBlock = {
157
- * type: 'binary',
158
- * data: pdfBytes,
159
- * mimeType: 'application/pdf',
160
- * metadata: { filename: 'document.pdf', pages: 10 }
161
- * };
162
- * ```
163
- */
164
- interface BinaryBlock {
165
- /** Discriminator for binary blocks */
166
- type: 'binary';
167
- /** Raw binary data */
168
- data: Uint8Array;
169
- /** MIME type of the data */
170
- mimeType: string;
171
- /** Additional metadata about the binary content */
172
- metadata?: Record<string, unknown>;
173
- }
174
- /**
175
- * Union of all content block types.
176
- *
177
- * Used when a function or property can accept any type of content block.
178
- */
179
- type ContentBlock = TextBlock | ImageBlock | AudioBlock | VideoBlock | BinaryBlock;
180
- /**
181
- * Content types allowed in user messages.
182
- *
183
- * Users can send any type of content block including binary data.
184
- */
185
- type UserContent = TextBlock | ImageBlock | AudioBlock | VideoBlock | BinaryBlock;
186
- /**
187
- * Content types allowed in assistant messages.
188
- *
189
- * Assistants can generate text and media but not arbitrary binary data.
190
- */
191
- type AssistantContent = TextBlock | ImageBlock | AudioBlock | VideoBlock;
192
- /**
193
- * Creates a text content block from a string.
194
- *
195
- * @param content - The text content
196
- * @returns A TextBlock containing the provided text
197
- *
198
- * @example
199
- * ```typescript
200
- * const block = text('Hello, world!');
201
- * // { type: 'text', text: 'Hello, world!' }
202
- * ```
203
- */
204
- declare function text(content: string): TextBlock;
205
- /**
206
- * Type guard for TextBlock.
207
- *
208
- * @param block - The content block to check
209
- * @returns True if the block is a TextBlock
210
- *
211
- * @example
212
- * ```typescript
213
- * if (isTextBlock(block)) {
214
- * console.log(block.text);
215
- * }
216
- * ```
217
- */
218
- declare function isTextBlock(block: ContentBlock): block is TextBlock;
219
- /**
220
- * Type guard for ImageBlock.
221
- *
222
- * @param block - The content block to check
223
- * @returns True if the block is an ImageBlock
224
- *
225
- * @example
226
- * ```typescript
227
- * if (isImageBlock(block)) {
228
- * console.log(block.mimeType, block.width, block.height);
229
- * }
230
- * ```
231
- */
232
- declare function isImageBlock(block: ContentBlock): block is ImageBlock;
233
- /**
234
- * Type guard for AudioBlock.
235
- *
236
- * @param block - The content block to check
237
- * @returns True if the block is an AudioBlock
238
- *
239
- * @example
240
- * ```typescript
241
- * if (isAudioBlock(block)) {
242
- * console.log(block.mimeType, block.duration);
243
- * }
244
- * ```
245
- */
246
- declare function isAudioBlock(block: ContentBlock): block is AudioBlock;
247
- /**
248
- * Type guard for VideoBlock.
249
- *
250
- * @param block - The content block to check
251
- * @returns True if the block is a VideoBlock
252
- *
253
- * @example
254
- * ```typescript
255
- * if (isVideoBlock(block)) {
256
- * console.log(block.mimeType, block.duration);
257
- * }
258
- * ```
259
- */
260
- declare function isVideoBlock(block: ContentBlock): block is VideoBlock;
261
- /**
262
- * Type guard for BinaryBlock.
263
- *
264
- * @param block - The content block to check
265
- * @returns True if the block is a BinaryBlock
266
- *
267
- * @example
268
- * ```typescript
269
- * if (isBinaryBlock(block)) {
270
- * console.log(block.mimeType, block.metadata);
271
- * }
272
- * ```
273
- */
274
- declare function isBinaryBlock(block: ContentBlock): block is BinaryBlock;
275
-
276
- export { type AssistantContent as A, type BinaryBlock as B, type ContentBlock as C, type ImageBlock as I, type TextBlock as T, type UserContent as U, type VideoBlock as V, type AudioBlock as a, type ImageSource as b, isImageBlock as c, isAudioBlock as d, isVideoBlock as e, isBinaryBlock as f, isTextBlock as i, text as t };