@providerprotocol/ai 0.0.10 → 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 (64) hide show
  1. package/dist/index.d.ts +7 -1
  2. package/dist/index.js +37 -9
  3. package/dist/index.js.map +1 -1
  4. package/package.json +1 -10
  5. package/src/anthropic/index.ts +0 -3
  6. package/src/core/image.ts +0 -188
  7. package/src/core/llm.ts +0 -624
  8. package/src/core/provider.ts +0 -92
  9. package/src/google/index.ts +0 -3
  10. package/src/http/errors.ts +0 -112
  11. package/src/http/fetch.ts +0 -210
  12. package/src/http/index.ts +0 -31
  13. package/src/http/keys.ts +0 -136
  14. package/src/http/retry.ts +0 -205
  15. package/src/http/sse.ts +0 -136
  16. package/src/index.ts +0 -32
  17. package/src/ollama/index.ts +0 -3
  18. package/src/openai/index.ts +0 -39
  19. package/src/openrouter/index.ts +0 -11
  20. package/src/providers/anthropic/index.ts +0 -17
  21. package/src/providers/anthropic/llm.ts +0 -196
  22. package/src/providers/anthropic/transform.ts +0 -434
  23. package/src/providers/anthropic/types.ts +0 -213
  24. package/src/providers/google/index.ts +0 -17
  25. package/src/providers/google/llm.ts +0 -203
  26. package/src/providers/google/transform.ts +0 -447
  27. package/src/providers/google/types.ts +0 -214
  28. package/src/providers/ollama/index.ts +0 -43
  29. package/src/providers/ollama/llm.ts +0 -272
  30. package/src/providers/ollama/transform.ts +0 -434
  31. package/src/providers/ollama/types.ts +0 -260
  32. package/src/providers/openai/index.ts +0 -186
  33. package/src/providers/openai/llm.completions.ts +0 -201
  34. package/src/providers/openai/llm.responses.ts +0 -211
  35. package/src/providers/openai/transform.completions.ts +0 -561
  36. package/src/providers/openai/transform.responses.ts +0 -708
  37. package/src/providers/openai/types.ts +0 -1249
  38. package/src/providers/openrouter/index.ts +0 -177
  39. package/src/providers/openrouter/llm.completions.ts +0 -201
  40. package/src/providers/openrouter/llm.responses.ts +0 -211
  41. package/src/providers/openrouter/transform.completions.ts +0 -538
  42. package/src/providers/openrouter/transform.responses.ts +0 -742
  43. package/src/providers/openrouter/types.ts +0 -717
  44. package/src/providers/xai/index.ts +0 -223
  45. package/src/providers/xai/llm.completions.ts +0 -201
  46. package/src/providers/xai/llm.messages.ts +0 -195
  47. package/src/providers/xai/llm.responses.ts +0 -211
  48. package/src/providers/xai/transform.completions.ts +0 -565
  49. package/src/providers/xai/transform.messages.ts +0 -448
  50. package/src/providers/xai/transform.responses.ts +0 -678
  51. package/src/providers/xai/types.ts +0 -938
  52. package/src/types/content.ts +0 -133
  53. package/src/types/errors.ts +0 -85
  54. package/src/types/index.ts +0 -105
  55. package/src/types/llm.ts +0 -211
  56. package/src/types/messages.ts +0 -205
  57. package/src/types/provider.ts +0 -195
  58. package/src/types/schema.ts +0 -58
  59. package/src/types/stream.ts +0 -146
  60. package/src/types/thread.ts +0 -226
  61. package/src/types/tool.ts +0 -88
  62. package/src/types/turn.ts +0 -118
  63. package/src/utils/id.ts +0 -28
  64. package/src/xai/index.ts +0 -41
@@ -1,205 +0,0 @@
1
- import { generateId } from '../utils/id.ts';
2
- import type {
3
- ContentBlock,
4
- TextBlock,
5
- ImageBlock,
6
- AudioBlock,
7
- VideoBlock,
8
- UserContent,
9
- AssistantContent,
10
- } from './content.ts';
11
- import type { ToolCall, ToolResult } from './tool.ts';
12
-
13
- /**
14
- * Message type discriminator
15
- */
16
- export type MessageType = 'user' | 'assistant' | 'tool_result';
17
-
18
- /**
19
- * Provider-namespaced metadata
20
- * Each provider uses its own namespace
21
- */
22
- export interface MessageMetadata {
23
- [provider: string]: Record<string, unknown> | undefined;
24
- }
25
-
26
- /**
27
- * Options for message construction
28
- */
29
- export interface MessageOptions {
30
- id?: string;
31
- metadata?: MessageMetadata;
32
- }
33
-
34
- /**
35
- * Base message class
36
- * All messages inherit from this
37
- */
38
- export abstract class Message {
39
- /** Unique message identifier */
40
- readonly id: string;
41
-
42
- /** Timestamp */
43
- readonly timestamp: Date;
44
-
45
- /** Provider-specific metadata, namespaced by provider */
46
- readonly metadata?: MessageMetadata;
47
-
48
- /** Message type discriminator */
49
- abstract readonly type: MessageType;
50
-
51
- /** Raw content - implemented by subclasses */
52
- protected abstract getContent(): ContentBlock[];
53
-
54
- constructor(options?: MessageOptions) {
55
- this.id = options?.id ?? generateId();
56
- this.timestamp = new Date();
57
- this.metadata = options?.metadata;
58
- }
59
-
60
- /**
61
- * Convenience accessor for text content
62
- * Concatenates all text blocks with '\n\n'
63
- */
64
- get text(): string {
65
- return this.getContent()
66
- .filter((block): block is TextBlock => block.type === 'text')
67
- .map((block) => block.text)
68
- .join('\n\n');
69
- }
70
-
71
- /**
72
- * Convenience accessor for image content blocks
73
- */
74
- get images(): ImageBlock[] {
75
- return this.getContent().filter((block): block is ImageBlock => block.type === 'image');
76
- }
77
-
78
- /**
79
- * Convenience accessor for audio content blocks
80
- */
81
- get audio(): AudioBlock[] {
82
- return this.getContent().filter((block): block is AudioBlock => block.type === 'audio');
83
- }
84
-
85
- /**
86
- * Convenience accessor for video content blocks
87
- */
88
- get video(): VideoBlock[] {
89
- return this.getContent().filter((block): block is VideoBlock => block.type === 'video');
90
- }
91
- }
92
-
93
- /**
94
- * User input message
95
- */
96
- export class UserMessage extends Message {
97
- readonly type = 'user' as const;
98
- readonly content: UserContent[];
99
-
100
- /**
101
- * @param content - String (converted to TextBlock) or array of content blocks
102
- */
103
- constructor(content: string | UserContent[], options?: MessageOptions) {
104
- super(options);
105
- if (typeof content === 'string') {
106
- this.content = [{ type: 'text', text: content }];
107
- } else {
108
- this.content = content;
109
- }
110
- }
111
-
112
- protected getContent(): ContentBlock[] {
113
- return this.content;
114
- }
115
- }
116
-
117
- /**
118
- * Assistant response message
119
- * May contain text, media, and/or tool calls
120
- */
121
- export class AssistantMessage extends Message {
122
- readonly type = 'assistant' as const;
123
- readonly content: AssistantContent[];
124
-
125
- /** Tool calls requested by the model (if any) */
126
- readonly toolCalls?: ToolCall[];
127
-
128
- /**
129
- * @param content - String (converted to TextBlock) or array of content blocks
130
- * @param toolCalls - Tool calls requested by the model
131
- * @param options - Message ID and metadata
132
- */
133
- constructor(
134
- content: string | AssistantContent[],
135
- toolCalls?: ToolCall[],
136
- options?: MessageOptions
137
- ) {
138
- super(options);
139
- if (typeof content === 'string') {
140
- this.content = [{ type: 'text', text: content }];
141
- } else {
142
- this.content = content;
143
- }
144
- this.toolCalls = toolCalls;
145
- }
146
-
147
- protected getContent(): ContentBlock[] {
148
- return this.content;
149
- }
150
-
151
- /** Check if this message requests tool execution */
152
- get hasToolCalls(): boolean {
153
- return this.toolCalls !== undefined && this.toolCalls.length > 0;
154
- }
155
- }
156
-
157
- /**
158
- * Result of tool execution (sent back to model)
159
- */
160
- export class ToolResultMessage extends Message {
161
- readonly type = 'tool_result' as const;
162
- readonly results: ToolResult[];
163
-
164
- /**
165
- * @param results - Array of tool execution results
166
- * @param options - Message ID and metadata
167
- */
168
- constructor(results: ToolResult[], options?: MessageOptions) {
169
- super(options);
170
- this.results = results;
171
- }
172
-
173
- protected getContent(): ContentBlock[] {
174
- // Tool results don't have traditional content blocks
175
- // Return text representations of results
176
- return this.results.map((result) => ({
177
- type: 'text' as const,
178
- text:
179
- typeof result.result === 'string'
180
- ? result.result
181
- : JSON.stringify(result.result),
182
- }));
183
- }
184
- }
185
-
186
- /**
187
- * Type guard for UserMessage
188
- */
189
- export function isUserMessage(msg: Message): msg is UserMessage {
190
- return msg.type === 'user';
191
- }
192
-
193
- /**
194
- * Type guard for AssistantMessage
195
- */
196
- export function isAssistantMessage(msg: Message): msg is AssistantMessage {
197
- return msg.type === 'assistant';
198
- }
199
-
200
- /**
201
- * Type guard for ToolResultMessage
202
- */
203
- export function isToolResultMessage(msg: Message): msg is ToolResultMessage {
204
- return msg.type === 'tool_result';
205
- }
@@ -1,195 +0,0 @@
1
- import type { UPPError } from './errors.ts';
2
-
3
- /**
4
- * API key strategy interface for managing multiple keys
5
- */
6
- export interface KeyStrategy {
7
- /** Get the next API key to use */
8
- getKey(): string | Promise<string>;
9
- }
10
-
11
- /**
12
- * Retry strategy interface
13
- */
14
- export interface RetryStrategy {
15
- /**
16
- * Called when a request fails with a retryable error.
17
- * @param error - The error that occurred
18
- * @param attempt - The attempt number (1 = first retry)
19
- * @returns Delay in ms before retrying, or null to stop retrying
20
- */
21
- onRetry(error: UPPError, attempt: number): number | null | Promise<number | null>;
22
-
23
- /**
24
- * Called before each request. Can be used to implement pre-emptive rate limiting.
25
- * Returns delay in ms to wait before making the request, or 0 to proceed immediately.
26
- */
27
- beforeRequest?(): number | Promise<number>;
28
-
29
- /**
30
- * Reset the strategy state (e.g., after a successful request)
31
- */
32
- reset?(): void;
33
- }
34
-
35
- /**
36
- * Provider configuration for infrastructure/connection settings
37
- */
38
- export interface ProviderConfig {
39
- /**
40
- * API key - string, async function, or key strategy
41
- * @example 'sk-xxx'
42
- * @example () => fetchKeyFromVault()
43
- * @example new RoundRobinKeys(['sk-1', 'sk-2'])
44
- */
45
- apiKey?: string | (() => string | Promise<string>) | KeyStrategy;
46
-
47
- /** Override the base API URL (for proxies, local models) */
48
- baseUrl?: string;
49
-
50
- /** Request timeout in milliseconds */
51
- timeout?: number;
52
-
53
- /** Custom fetch implementation (for logging, caching, custom TLS) */
54
- fetch?: typeof fetch;
55
-
56
- /** API version override */
57
- apiVersion?: string;
58
-
59
- /** Retry strategy for handling failures and rate limits */
60
- retryStrategy?: RetryStrategy;
61
- }
62
-
63
- /**
64
- * A reference to a model, created by a provider factory
65
- *
66
- * @typeParam TOptions - Provider-specific options type
67
- */
68
- export interface ModelReference<TOptions = unknown> {
69
- /** The model identifier */
70
- readonly modelId: string;
71
-
72
- /** The provider that created this reference */
73
- readonly provider: Provider<TOptions>;
74
- }
75
-
76
- // Forward declarations for handler types (defined in llm.ts)
77
- // We use 'any' here since the full types are circular
78
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
79
- export interface LLMHandler<TParams = any> {
80
- /** Bind model ID to create executable model */
81
- bind(modelId: string): BoundLLMModel<TParams>;
82
-
83
- /**
84
- * Internal: Set the parent provider reference.
85
- * Called by createProvider() after the provider is constructed.
86
- * This allows bind() to return models with the correct provider reference.
87
- * @internal
88
- */
89
- _setProvider?(provider: LLMProvider<TParams>): void;
90
- }
91
-
92
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
93
- export interface EmbeddingHandler<TParams = any> {
94
- /** Supported input types */
95
- readonly supportedInputs: ('text' | 'image')[];
96
- /** Bind model ID to create executable model */
97
- bind(modelId: string): BoundEmbeddingModel<TParams>;
98
-
99
- /**
100
- * Internal: Set the parent provider reference.
101
- * @internal
102
- */
103
- _setProvider?(provider: EmbeddingProvider<TParams>): void;
104
- }
105
-
106
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
107
- export interface ImageHandler<TParams = any> {
108
- /** Bind model ID to create executable model */
109
- bind(modelId: string): BoundImageModel<TParams>;
110
-
111
- /**
112
- * Internal: Set the parent provider reference.
113
- * @internal
114
- */
115
- _setProvider?(provider: ImageProvider<TParams>): void;
116
- }
117
-
118
- // Forward declarations for bound models
119
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
120
- export interface BoundLLMModel<TParams = any> {
121
- readonly modelId: string;
122
- readonly provider: LLMProvider<TParams>;
123
- // Methods defined in llm.ts
124
- }
125
-
126
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
127
- export interface BoundEmbeddingModel<TParams = any> {
128
- readonly modelId: string;
129
- readonly provider: EmbeddingProvider<TParams>;
130
- readonly maxBatchSize: number;
131
- readonly maxInputLength: number;
132
- readonly dimensions: number;
133
- }
134
-
135
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
136
- export interface BoundImageModel<TParams = any> {
137
- readonly modelId: string;
138
- readonly provider: ImageProvider<TParams>;
139
- }
140
-
141
- /**
142
- * A provider factory function with metadata and modality handlers.
143
- *
144
- * @typeParam TOptions - Provider-specific options passed to the factory function
145
- */
146
- export interface Provider<TOptions = unknown> {
147
- /** Create a model reference, optionally with provider-specific options */
148
- (modelId: string, options?: TOptions): ModelReference<TOptions>;
149
-
150
- /** Provider name */
151
- readonly name: string;
152
-
153
- /** Provider version */
154
- readonly version: string;
155
-
156
- /** Supported modalities */
157
- readonly modalities: {
158
- llm?: LLMHandler;
159
- embedding?: EmbeddingHandler;
160
- image?: ImageHandler;
161
- };
162
- }
163
-
164
- /**
165
- * Provider with LLM modality
166
- *
167
- * @typeParam TParams - Model-specific parameters type
168
- * @typeParam TOptions - Provider-specific options type
169
- */
170
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
171
- export type LLMProvider<TParams = any, TOptions = unknown> = Provider<TOptions> & {
172
- readonly modalities: { llm: LLMHandler<TParams> };
173
- };
174
-
175
- /**
176
- * Provider with Embedding modality
177
- *
178
- * @typeParam TParams - Model-specific parameters type
179
- * @typeParam TOptions - Provider-specific options type
180
- */
181
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
182
- export type EmbeddingProvider<TParams = any, TOptions = unknown> = Provider<TOptions> & {
183
- readonly modalities: { embedding: EmbeddingHandler<TParams> };
184
- };
185
-
186
- /**
187
- * Provider with Image modality
188
- *
189
- * @typeParam TParams - Model-specific parameters type
190
- * @typeParam TOptions - Provider-specific options type
191
- */
192
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
193
- export type ImageProvider<TParams = any, TOptions = unknown> = Provider<TOptions> & {
194
- readonly modalities: { image: ImageHandler<TParams> };
195
- };
@@ -1,58 +0,0 @@
1
- /**
2
- * JSON Schema types for tool parameters and structured outputs
3
- */
4
-
5
- export type JSONSchemaPropertyType =
6
- | 'string'
7
- | 'number'
8
- | 'integer'
9
- | 'boolean'
10
- | 'array'
11
- | 'object'
12
- | 'null';
13
-
14
- /**
15
- * JSON Schema property definition
16
- */
17
- export interface JSONSchemaProperty {
18
- type: JSONSchemaPropertyType;
19
- description?: string;
20
- enum?: unknown[];
21
- const?: unknown;
22
- default?: unknown;
23
-
24
- // String-specific
25
- minLength?: number;
26
- maxLength?: number;
27
- pattern?: string;
28
- format?: 'email' | 'uri' | 'date' | 'date-time' | 'uuid';
29
-
30
- // Number-specific
31
- minimum?: number;
32
- maximum?: number;
33
- exclusiveMinimum?: number;
34
- exclusiveMaximum?: number;
35
- multipleOf?: number;
36
-
37
- // Array-specific
38
- items?: JSONSchemaProperty;
39
- minItems?: number;
40
- maxItems?: number;
41
- uniqueItems?: boolean;
42
-
43
- // Object-specific
44
- properties?: Record<string, JSONSchemaProperty>;
45
- required?: string[];
46
- additionalProperties?: boolean;
47
- }
48
-
49
- /**
50
- * JSON Schema for tool parameters or structured outputs
51
- */
52
- export interface JSONSchema {
53
- type: 'object';
54
- properties: Record<string, JSONSchemaProperty>;
55
- required?: string[];
56
- additionalProperties?: boolean;
57
- description?: string;
58
- }
@@ -1,146 +0,0 @@
1
- import type { Turn } from './turn.ts';
2
-
3
- /**
4
- * Stream event types
5
- */
6
- export type StreamEventType =
7
- | 'text_delta'
8
- | 'reasoning_delta'
9
- | 'image_delta'
10
- | 'audio_delta'
11
- | 'video_delta'
12
- | 'tool_call_delta'
13
- | 'message_start'
14
- | 'message_stop'
15
- | 'content_block_start'
16
- | 'content_block_stop';
17
-
18
- /**
19
- * Event delta data (type-specific)
20
- */
21
- export interface EventDelta {
22
- text?: string;
23
- data?: Uint8Array;
24
- toolCallId?: string;
25
- toolName?: string;
26
- argumentsJson?: string;
27
- }
28
-
29
- /**
30
- * A streaming event
31
- */
32
- export interface StreamEvent {
33
- /** Event type */
34
- type: StreamEventType;
35
-
36
- /** Index of the content block this event belongs to */
37
- index: number;
38
-
39
- /** Event data (type-specific) */
40
- delta: EventDelta;
41
- }
42
-
43
- /**
44
- * Stream result - async iterable that also provides final turn
45
- */
46
- export interface StreamResult<TData = unknown>
47
- extends AsyncIterable<StreamEvent> {
48
- /**
49
- * Get the complete Turn after streaming finishes.
50
- * Resolves when the stream completes.
51
- */
52
- readonly turn: Promise<Turn<TData>>;
53
-
54
- /** Abort the stream */
55
- abort(): void;
56
- }
57
-
58
- /**
59
- * Create a stream result from an async generator and completion promise
60
- */
61
- export function createStreamResult<TData = unknown>(
62
- generator: AsyncGenerator<StreamEvent, void, unknown>,
63
- turnPromise: Promise<Turn<TData>>,
64
- abortController: AbortController
65
- ): StreamResult<TData> {
66
- return {
67
- [Symbol.asyncIterator]() {
68
- return generator;
69
- },
70
- turn: turnPromise,
71
- abort() {
72
- abortController.abort();
73
- },
74
- };
75
- }
76
-
77
- /**
78
- * Create a text delta event
79
- */
80
- export function textDelta(text: string, index = 0): StreamEvent {
81
- return {
82
- type: 'text_delta',
83
- index,
84
- delta: { text },
85
- };
86
- }
87
-
88
- /**
89
- * Create a tool call delta event
90
- */
91
- export function toolCallDelta(
92
- toolCallId: string,
93
- toolName: string,
94
- argumentsJson: string,
95
- index = 0
96
- ): StreamEvent {
97
- return {
98
- type: 'tool_call_delta',
99
- index,
100
- delta: { toolCallId, toolName, argumentsJson },
101
- };
102
- }
103
-
104
- /**
105
- * Create a message start event
106
- */
107
- export function messageStart(): StreamEvent {
108
- return {
109
- type: 'message_start',
110
- index: 0,
111
- delta: {},
112
- };
113
- }
114
-
115
- /**
116
- * Create a message stop event
117
- */
118
- export function messageStop(): StreamEvent {
119
- return {
120
- type: 'message_stop',
121
- index: 0,
122
- delta: {},
123
- };
124
- }
125
-
126
- /**
127
- * Create a content block start event
128
- */
129
- export function contentBlockStart(index: number): StreamEvent {
130
- return {
131
- type: 'content_block_start',
132
- index,
133
- delta: {},
134
- };
135
- }
136
-
137
- /**
138
- * Create a content block stop event
139
- */
140
- export function contentBlockStop(index: number): StreamEvent {
141
- return {
142
- type: 'content_block_stop',
143
- index,
144
- delta: {},
145
- };
146
- }