@providerprotocol/ai 0.0.1
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.
- package/LICENSE +21 -0
- package/README.md +84 -0
- package/dist/anthropic/index.d.ts +41 -0
- package/dist/anthropic/index.js +500 -0
- package/dist/anthropic/index.js.map +1 -0
- package/dist/chunk-CUCRF5W6.js +136 -0
- package/dist/chunk-CUCRF5W6.js.map +1 -0
- package/dist/chunk-FTFX2VET.js +424 -0
- package/dist/chunk-FTFX2VET.js.map +1 -0
- package/dist/chunk-QUUX4G7U.js +117 -0
- package/dist/chunk-QUUX4G7U.js.map +1 -0
- package/dist/chunk-Y6Q7JCNP.js +39 -0
- package/dist/chunk-Y6Q7JCNP.js.map +1 -0
- package/dist/google/index.d.ts +69 -0
- package/dist/google/index.js +517 -0
- package/dist/google/index.js.map +1 -0
- package/dist/http/index.d.ts +61 -0
- package/dist/http/index.js +43 -0
- package/dist/http/index.js.map +1 -0
- package/dist/index.d.ts +792 -0
- package/dist/index.js +898 -0
- package/dist/index.js.map +1 -0
- package/dist/openai/index.d.ts +204 -0
- package/dist/openai/index.js +1340 -0
- package/dist/openai/index.js.map +1 -0
- package/dist/provider-CUJWjgNl.d.ts +192 -0
- package/dist/retry-I2661_rv.d.ts +118 -0
- package/package.json +88 -0
- package/src/anthropic/index.ts +3 -0
- package/src/core/image.ts +188 -0
- package/src/core/llm.ts +619 -0
- package/src/core/provider.ts +92 -0
- package/src/google/index.ts +3 -0
- package/src/http/errors.ts +112 -0
- package/src/http/fetch.ts +210 -0
- package/src/http/index.ts +31 -0
- package/src/http/keys.ts +136 -0
- package/src/http/retry.ts +205 -0
- package/src/http/sse.ts +136 -0
- package/src/index.ts +32 -0
- package/src/openai/index.ts +9 -0
- package/src/providers/anthropic/index.ts +17 -0
- package/src/providers/anthropic/llm.ts +196 -0
- package/src/providers/anthropic/transform.ts +452 -0
- package/src/providers/anthropic/types.ts +213 -0
- package/src/providers/google/index.ts +17 -0
- package/src/providers/google/llm.ts +203 -0
- package/src/providers/google/transform.ts +487 -0
- package/src/providers/google/types.ts +214 -0
- package/src/providers/openai/index.ts +151 -0
- package/src/providers/openai/llm.completions.ts +201 -0
- package/src/providers/openai/llm.responses.ts +211 -0
- package/src/providers/openai/transform.completions.ts +628 -0
- package/src/providers/openai/transform.responses.ts +718 -0
- package/src/providers/openai/types.ts +711 -0
- package/src/types/content.ts +133 -0
- package/src/types/errors.ts +85 -0
- package/src/types/index.ts +105 -0
- package/src/types/llm.ts +211 -0
- package/src/types/messages.ts +182 -0
- package/src/types/provider.ts +195 -0
- package/src/types/schema.ts +58 -0
- package/src/types/stream.ts +146 -0
- package/src/types/thread.ts +226 -0
- package/src/types/tool.ts +88 -0
- package/src/types/turn.ts +118 -0
- package/src/utils/id.ts +28 -0
package/src/http/sse.ts
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server-Sent Events (SSE) stream parser
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Parse a Server-Sent Events stream into JSON objects
|
|
7
|
+
* Handles standard SSE format with "data:" prefix
|
|
8
|
+
* Yields parsed JSON for each event
|
|
9
|
+
* Terminates on "[DONE]" message (OpenAI style)
|
|
10
|
+
*
|
|
11
|
+
* @param body - ReadableStream from fetch response
|
|
12
|
+
*/
|
|
13
|
+
export async function* parseSSEStream(
|
|
14
|
+
body: ReadableStream<Uint8Array>
|
|
15
|
+
): AsyncGenerator<unknown, void, unknown> {
|
|
16
|
+
const reader = body.getReader();
|
|
17
|
+
const decoder = new TextDecoder();
|
|
18
|
+
let buffer = '';
|
|
19
|
+
|
|
20
|
+
try {
|
|
21
|
+
while (true) {
|
|
22
|
+
const { done, value } = await reader.read();
|
|
23
|
+
|
|
24
|
+
if (done) {
|
|
25
|
+
// Process any remaining data in buffer
|
|
26
|
+
if (buffer.trim()) {
|
|
27
|
+
const event = parseSSEEvent(buffer);
|
|
28
|
+
if (event !== null && event !== undefined) {
|
|
29
|
+
yield event;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
break;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
buffer += decoder.decode(value, { stream: true });
|
|
36
|
+
|
|
37
|
+
// Process complete events (separated by double newlines or \r\n\r\n)
|
|
38
|
+
const events = buffer.split(/\r?\n\r?\n/);
|
|
39
|
+
|
|
40
|
+
// Keep the last partial event in the buffer
|
|
41
|
+
buffer = events.pop() ?? '';
|
|
42
|
+
|
|
43
|
+
for (const eventText of events) {
|
|
44
|
+
if (!eventText.trim()) continue;
|
|
45
|
+
|
|
46
|
+
const event = parseSSEEvent(eventText);
|
|
47
|
+
if (event === 'DONE') {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
if (event !== null && event !== undefined) {
|
|
51
|
+
yield event;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
} finally {
|
|
56
|
+
reader.releaseLock();
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Parse a single SSE event
|
|
62
|
+
* Returns 'DONE' for [DONE] terminator
|
|
63
|
+
* Returns null for empty or unparseable events
|
|
64
|
+
* Returns parsed JSON otherwise
|
|
65
|
+
*/
|
|
66
|
+
function parseSSEEvent(eventText: string): unknown | 'DONE' | null {
|
|
67
|
+
const lines = eventText.split('\n');
|
|
68
|
+
let data = '';
|
|
69
|
+
let eventType = '';
|
|
70
|
+
|
|
71
|
+
for (const line of lines) {
|
|
72
|
+
const trimmedLine = line.trim();
|
|
73
|
+
if (trimmedLine.startsWith('event:')) {
|
|
74
|
+
eventType = trimmedLine.slice(6).trim();
|
|
75
|
+
} else if (trimmedLine.startsWith('data:')) {
|
|
76
|
+
// Append data (some providers send multi-line data)
|
|
77
|
+
const lineData = trimmedLine.slice(5).trim();
|
|
78
|
+
data += (data ? '\n' : '') + lineData;
|
|
79
|
+
} else if (trimmedLine.startsWith(':')) {
|
|
80
|
+
// Comment line, ignore (often used for keep-alive)
|
|
81
|
+
continue;
|
|
82
|
+
} else if (trimmedLine.startsWith('{') || trimmedLine.startsWith('[')) {
|
|
83
|
+
// Some providers (like Google) may send raw JSON without data: prefix
|
|
84
|
+
data += (data ? '\n' : '') + trimmedLine;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (!data) {
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Check for OpenAI-style termination
|
|
93
|
+
if (data === '[DONE]') {
|
|
94
|
+
return 'DONE';
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
try {
|
|
98
|
+
const parsed = JSON.parse(data);
|
|
99
|
+
|
|
100
|
+
// If we have an event type, include it
|
|
101
|
+
if (eventType) {
|
|
102
|
+
return { _eventType: eventType, ...parsed };
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return parsed;
|
|
106
|
+
} catch {
|
|
107
|
+
// Failed to parse JSON - could be a ping or malformed event
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Create a simple SSE reader that handles basic text streaming
|
|
114
|
+
* For providers that just stream text deltas
|
|
115
|
+
*/
|
|
116
|
+
export async function* parseSimpleTextStream(
|
|
117
|
+
body: ReadableStream<Uint8Array>
|
|
118
|
+
): AsyncGenerator<string, void, unknown> {
|
|
119
|
+
const reader = body.getReader();
|
|
120
|
+
const decoder = new TextDecoder();
|
|
121
|
+
|
|
122
|
+
try {
|
|
123
|
+
while (true) {
|
|
124
|
+
const { done, value } = await reader.read();
|
|
125
|
+
|
|
126
|
+
if (done) break;
|
|
127
|
+
|
|
128
|
+
const text = decoder.decode(value, { stream: true });
|
|
129
|
+
if (text) {
|
|
130
|
+
yield text;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
} finally {
|
|
134
|
+
reader.releaseLock();
|
|
135
|
+
}
|
|
136
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// Core entry points
|
|
2
|
+
export { llm } from './core/llm.ts';
|
|
3
|
+
export { createProvider } from './core/provider.ts';
|
|
4
|
+
export { Image } from './core/image.ts';
|
|
5
|
+
|
|
6
|
+
// Namespace object for alternative import style
|
|
7
|
+
import { llm } from './core/llm.ts';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* UPP namespace object
|
|
11
|
+
* Provides ai.llm(), ai.embedding(), ai.image() style access
|
|
12
|
+
*/
|
|
13
|
+
export const ai = {
|
|
14
|
+
llm,
|
|
15
|
+
// embedding, // Coming soon
|
|
16
|
+
// image, // Coming soon
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
// Re-export all types from types/index.ts
|
|
20
|
+
export * from './types/index.ts';
|
|
21
|
+
|
|
22
|
+
// Re-export HTTP utilities
|
|
23
|
+
export {
|
|
24
|
+
RoundRobinKeys,
|
|
25
|
+
WeightedKeys,
|
|
26
|
+
DynamicKey,
|
|
27
|
+
ExponentialBackoff,
|
|
28
|
+
LinearBackoff,
|
|
29
|
+
NoRetry,
|
|
30
|
+
TokenBucket,
|
|
31
|
+
RetryAfterStrategy,
|
|
32
|
+
} from './http/index.ts';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { createProvider } from '../../core/provider.ts';
|
|
2
|
+
import { createLLMHandler } from './llm.ts';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Anthropic provider
|
|
6
|
+
* Supports LLM modality with Claude models
|
|
7
|
+
*/
|
|
8
|
+
export const anthropic = createProvider({
|
|
9
|
+
name: 'anthropic',
|
|
10
|
+
version: '1.0.0',
|
|
11
|
+
modalities: {
|
|
12
|
+
llm: createLLMHandler(),
|
|
13
|
+
},
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
// Re-export types
|
|
17
|
+
export type { AnthropicLLMParams } from './types.ts';
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import type { LLMHandler, BoundLLMModel, LLMRequest, LLMResponse, LLMStreamResult, LLMCapabilities } from '../../types/llm.ts';
|
|
2
|
+
import type { StreamEvent } from '../../types/stream.ts';
|
|
3
|
+
import type { LLMProvider } from '../../types/provider.ts';
|
|
4
|
+
import { UPPError } from '../../types/errors.ts';
|
|
5
|
+
import { resolveApiKey } from '../../http/keys.ts';
|
|
6
|
+
import { doFetch, doStreamFetch } from '../../http/fetch.ts';
|
|
7
|
+
import { parseSSEStream } from '../../http/sse.ts';
|
|
8
|
+
import { normalizeHttpError } from '../../http/errors.ts';
|
|
9
|
+
import type { AnthropicLLMParams, AnthropicResponse, AnthropicStreamEvent } from './types.ts';
|
|
10
|
+
import {
|
|
11
|
+
transformRequest,
|
|
12
|
+
transformResponse,
|
|
13
|
+
transformStreamEvent,
|
|
14
|
+
createStreamState,
|
|
15
|
+
buildResponseFromState,
|
|
16
|
+
} from './transform.ts';
|
|
17
|
+
|
|
18
|
+
const ANTHROPIC_API_URL = 'https://api.anthropic.com/v1/messages';
|
|
19
|
+
const ANTHROPIC_VERSION = '2023-06-01';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Anthropic API capabilities
|
|
23
|
+
*/
|
|
24
|
+
const ANTHROPIC_CAPABILITIES: LLMCapabilities = {
|
|
25
|
+
streaming: true,
|
|
26
|
+
tools: true,
|
|
27
|
+
structuredOutput: true,
|
|
28
|
+
imageInput: true,
|
|
29
|
+
videoInput: false,
|
|
30
|
+
audioInput: false,
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Create Anthropic LLM handler
|
|
35
|
+
*/
|
|
36
|
+
export function createLLMHandler(): LLMHandler<AnthropicLLMParams> {
|
|
37
|
+
// Provider reference injected by createProvider() after construction
|
|
38
|
+
let providerRef: LLMProvider<AnthropicLLMParams> | null = null;
|
|
39
|
+
|
|
40
|
+
return {
|
|
41
|
+
_setProvider(provider: LLMProvider<AnthropicLLMParams>) {
|
|
42
|
+
providerRef = provider;
|
|
43
|
+
},
|
|
44
|
+
|
|
45
|
+
bind(modelId: string): BoundLLMModel<AnthropicLLMParams> {
|
|
46
|
+
// Use the injected provider reference (set by createProvider)
|
|
47
|
+
if (!providerRef) {
|
|
48
|
+
throw new UPPError(
|
|
49
|
+
'Provider reference not set. Handler must be used with createProvider().',
|
|
50
|
+
'INVALID_REQUEST',
|
|
51
|
+
'anthropic',
|
|
52
|
+
'llm'
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const model: BoundLLMModel<AnthropicLLMParams> = {
|
|
57
|
+
modelId,
|
|
58
|
+
capabilities: ANTHROPIC_CAPABILITIES,
|
|
59
|
+
|
|
60
|
+
get provider(): LLMProvider<AnthropicLLMParams> {
|
|
61
|
+
return providerRef!;
|
|
62
|
+
},
|
|
63
|
+
|
|
64
|
+
async complete(request: LLMRequest<AnthropicLLMParams>): Promise<LLMResponse> {
|
|
65
|
+
const apiKey = await resolveApiKey(
|
|
66
|
+
request.config,
|
|
67
|
+
'ANTHROPIC_API_KEY',
|
|
68
|
+
'anthropic',
|
|
69
|
+
'llm'
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
const baseUrl = request.config.baseUrl ?? ANTHROPIC_API_URL;
|
|
73
|
+
const body = transformRequest(request, modelId);
|
|
74
|
+
|
|
75
|
+
const response = await doFetch(
|
|
76
|
+
baseUrl,
|
|
77
|
+
{
|
|
78
|
+
method: 'POST',
|
|
79
|
+
headers: {
|
|
80
|
+
'Content-Type': 'application/json',
|
|
81
|
+
'x-api-key': apiKey,
|
|
82
|
+
'anthropic-version': request.config.apiVersion ?? ANTHROPIC_VERSION,
|
|
83
|
+
},
|
|
84
|
+
body: JSON.stringify(body),
|
|
85
|
+
signal: request.signal,
|
|
86
|
+
},
|
|
87
|
+
request.config,
|
|
88
|
+
'anthropic',
|
|
89
|
+
'llm'
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
const data = (await response.json()) as AnthropicResponse;
|
|
93
|
+
return transformResponse(data);
|
|
94
|
+
},
|
|
95
|
+
|
|
96
|
+
stream(request: LLMRequest<AnthropicLLMParams>): LLMStreamResult {
|
|
97
|
+
const state = createStreamState();
|
|
98
|
+
let responseResolve: (value: LLMResponse) => void;
|
|
99
|
+
let responseReject: (error: Error) => void;
|
|
100
|
+
|
|
101
|
+
const responsePromise = new Promise<LLMResponse>((resolve, reject) => {
|
|
102
|
+
responseResolve = resolve;
|
|
103
|
+
responseReject = reject;
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
async function* generateEvents(): AsyncGenerator<StreamEvent, void, unknown> {
|
|
107
|
+
try {
|
|
108
|
+
const apiKey = await resolveApiKey(
|
|
109
|
+
request.config,
|
|
110
|
+
'ANTHROPIC_API_KEY',
|
|
111
|
+
'anthropic',
|
|
112
|
+
'llm'
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
const baseUrl = request.config.baseUrl ?? ANTHROPIC_API_URL;
|
|
116
|
+
const body = transformRequest(request, modelId);
|
|
117
|
+
body.stream = true;
|
|
118
|
+
|
|
119
|
+
const response = await doStreamFetch(
|
|
120
|
+
baseUrl,
|
|
121
|
+
{
|
|
122
|
+
method: 'POST',
|
|
123
|
+
headers: {
|
|
124
|
+
'Content-Type': 'application/json',
|
|
125
|
+
'x-api-key': apiKey,
|
|
126
|
+
'anthropic-version': request.config.apiVersion ?? ANTHROPIC_VERSION,
|
|
127
|
+
},
|
|
128
|
+
body: JSON.stringify(body),
|
|
129
|
+
signal: request.signal,
|
|
130
|
+
},
|
|
131
|
+
request.config,
|
|
132
|
+
'anthropic',
|
|
133
|
+
'llm'
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
if (!response.ok) {
|
|
137
|
+
const error = await normalizeHttpError(response, 'anthropic', 'llm');
|
|
138
|
+
responseReject(error);
|
|
139
|
+
throw error;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (!response.body) {
|
|
143
|
+
const error = new UPPError(
|
|
144
|
+
'No response body for streaming request',
|
|
145
|
+
'PROVIDER_ERROR',
|
|
146
|
+
'anthropic',
|
|
147
|
+
'llm'
|
|
148
|
+
);
|
|
149
|
+
responseReject(error);
|
|
150
|
+
throw error;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
for await (const data of parseSSEStream(response.body)) {
|
|
154
|
+
// Check for Anthropic error event
|
|
155
|
+
if (typeof data === 'object' && data !== null && 'type' in data) {
|
|
156
|
+
const event = data as AnthropicStreamEvent;
|
|
157
|
+
|
|
158
|
+
if (event.type === 'error') {
|
|
159
|
+
const error = new UPPError(
|
|
160
|
+
event.error.message,
|
|
161
|
+
'PROVIDER_ERROR',
|
|
162
|
+
'anthropic',
|
|
163
|
+
'llm'
|
|
164
|
+
);
|
|
165
|
+
responseReject(error);
|
|
166
|
+
throw error;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const uppEvent = transformStreamEvent(event, state);
|
|
170
|
+
if (uppEvent) {
|
|
171
|
+
yield uppEvent;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Build final response
|
|
177
|
+
responseResolve(buildResponseFromState(state));
|
|
178
|
+
} catch (error) {
|
|
179
|
+
responseReject(error as Error);
|
|
180
|
+
throw error;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return {
|
|
185
|
+
[Symbol.asyncIterator]() {
|
|
186
|
+
return generateEvents();
|
|
187
|
+
},
|
|
188
|
+
response: responsePromise,
|
|
189
|
+
};
|
|
190
|
+
},
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
return model;
|
|
194
|
+
},
|
|
195
|
+
};
|
|
196
|
+
}
|