@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,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,188 +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
- | 'tool_execution_start'
14
- | 'tool_execution_end'
15
- | 'message_start'
16
- | 'message_stop'
17
- | 'content_block_start'
18
- | 'content_block_stop';
19
-
20
- /**
21
- * Event delta data (type-specific)
22
- */
23
- export interface EventDelta {
24
- text?: string;
25
- data?: Uint8Array;
26
- toolCallId?: string;
27
- toolName?: string;
28
- argumentsJson?: string;
29
- /** Tool execution result (for tool_execution_end) */
30
- result?: unknown;
31
- /** Whether tool execution errored (for tool_execution_end) */
32
- isError?: boolean;
33
- /** Timestamp in ms (for tool_execution_start/end) */
34
- timestamp?: number;
35
- }
36
-
37
- /**
38
- * A streaming event
39
- */
40
- export interface StreamEvent {
41
- /** Event type */
42
- type: StreamEventType;
43
-
44
- /** Index of the content block this event belongs to */
45
- index: number;
46
-
47
- /** Event data (type-specific) */
48
- delta: EventDelta;
49
- }
50
-
51
- /**
52
- * Stream result - async iterable that also provides final turn
53
- */
54
- export interface StreamResult<TData = unknown>
55
- extends AsyncIterable<StreamEvent> {
56
- /**
57
- * Get the complete Turn after streaming finishes.
58
- * Resolves when the stream completes.
59
- */
60
- readonly turn: Promise<Turn<TData>>;
61
-
62
- /** Abort the stream */
63
- abort(): void;
64
- }
65
-
66
- /**
67
- * Create a stream result from an async generator and completion promise
68
- */
69
- export function createStreamResult<TData = unknown>(
70
- generator: AsyncGenerator<StreamEvent, void, unknown>,
71
- turnPromise: Promise<Turn<TData>>,
72
- abortController: AbortController
73
- ): StreamResult<TData> {
74
- return {
75
- [Symbol.asyncIterator]() {
76
- return generator;
77
- },
78
- turn: turnPromise,
79
- abort() {
80
- abortController.abort();
81
- },
82
- };
83
- }
84
-
85
- /**
86
- * Create a text delta event
87
- */
88
- export function textDelta(text: string, index = 0): StreamEvent {
89
- return {
90
- type: 'text_delta',
91
- index,
92
- delta: { text },
93
- };
94
- }
95
-
96
- /**
97
- * Create a tool call delta event
98
- */
99
- export function toolCallDelta(
100
- toolCallId: string,
101
- toolName: string,
102
- argumentsJson: string,
103
- index = 0
104
- ): StreamEvent {
105
- return {
106
- type: 'tool_call_delta',
107
- index,
108
- delta: { toolCallId, toolName, argumentsJson },
109
- };
110
- }
111
-
112
- /**
113
- * Create a message start event
114
- */
115
- export function messageStart(): StreamEvent {
116
- return {
117
- type: 'message_start',
118
- index: 0,
119
- delta: {},
120
- };
121
- }
122
-
123
- /**
124
- * Create a message stop event
125
- */
126
- export function messageStop(): StreamEvent {
127
- return {
128
- type: 'message_stop',
129
- index: 0,
130
- delta: {},
131
- };
132
- }
133
-
134
- /**
135
- * Create a content block start event
136
- */
137
- export function contentBlockStart(index: number): StreamEvent {
138
- return {
139
- type: 'content_block_start',
140
- index,
141
- delta: {},
142
- };
143
- }
144
-
145
- /**
146
- * Create a content block stop event
147
- */
148
- export function contentBlockStop(index: number): StreamEvent {
149
- return {
150
- type: 'content_block_stop',
151
- index,
152
- delta: {},
153
- };
154
- }
155
-
156
- /**
157
- * Create a tool execution start event
158
- */
159
- export function toolExecutionStart(
160
- toolCallId: string,
161
- toolName: string,
162
- timestamp: number,
163
- index = 0
164
- ): StreamEvent {
165
- return {
166
- type: 'tool_execution_start',
167
- index,
168
- delta: { toolCallId, toolName, timestamp },
169
- };
170
- }
171
-
172
- /**
173
- * Create a tool execution end event
174
- */
175
- export function toolExecutionEnd(
176
- toolCallId: string,
177
- toolName: string,
178
- result: unknown,
179
- isError: boolean,
180
- timestamp: number,
181
- index = 0
182
- ): StreamEvent {
183
- return {
184
- type: 'tool_execution_end',
185
- index,
186
- delta: { toolCallId, toolName, result, isError, timestamp },
187
- };
188
- }