@providerprotocol/ai 0.0.33 → 0.0.35
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/README.md +542 -3
- package/dist/anthropic/index.d.ts +2 -1
- package/dist/anthropic/index.js +151 -145
- package/dist/anthropic/index.js.map +1 -1
- package/dist/cerebras/index.d.ts +392 -0
- package/dist/cerebras/index.js +648 -0
- package/dist/cerebras/index.js.map +1 -0
- package/dist/chunk-3GWM5GR3.js +153 -0
- package/dist/chunk-3GWM5GR3.js.map +1 -0
- package/dist/chunk-4OGB7JZA.js +157 -0
- package/dist/chunk-4OGB7JZA.js.map +1 -0
- package/dist/chunk-7DXVRILR.js +49 -0
- package/dist/chunk-7DXVRILR.js.map +1 -0
- package/dist/{chunk-3C7O2RNO.js → chunk-A2IM7PGT.js} +6 -4
- package/dist/{chunk-3C7O2RNO.js.map → chunk-A2IM7PGT.js.map} +1 -1
- package/dist/{chunk-3D6XGGVG.js → chunk-ARVM24K2.js} +2 -2
- package/dist/{chunk-4J6OFUKX.js → chunk-AY55T37A.js} +70 -162
- package/dist/chunk-AY55T37A.js.map +1 -0
- package/dist/{chunk-ILR2D5PN.js → chunk-BRP5XJ6Q.js} +2 -86
- package/dist/chunk-BRP5XJ6Q.js.map +1 -0
- package/dist/chunk-C4JP64VW.js +298 -0
- package/dist/chunk-C4JP64VW.js.map +1 -0
- package/dist/chunk-COS4ON4G.js +111 -0
- package/dist/chunk-COS4ON4G.js.map +1 -0
- package/dist/chunk-ETBFOLQN.js +34 -0
- package/dist/chunk-ETBFOLQN.js.map +1 -0
- package/dist/chunk-HB4ZIH3T.js +31 -0
- package/dist/chunk-HB4ZIH3T.js.map +1 -0
- package/dist/chunk-I53CI6ZZ.js +142 -0
- package/dist/chunk-I53CI6ZZ.js.map +1 -0
- package/dist/chunk-IDZOVWP3.js +29 -0
- package/dist/chunk-IDZOVWP3.js.map +1 -0
- package/dist/chunk-JA3UZALR.js +88 -0
- package/dist/chunk-JA3UZALR.js.map +1 -0
- package/dist/{chunk-WAKD3OO5.js → chunk-N5DX5JW3.js} +31 -31
- package/dist/chunk-N5DX5JW3.js.map +1 -0
- package/dist/chunk-OIEWDFQU.js +97 -0
- package/dist/chunk-OIEWDFQU.js.map +1 -0
- package/dist/{chunk-TOJCZMVU.js → chunk-PMK5LZ5Z.js} +40 -40
- package/dist/chunk-PMK5LZ5Z.js.map +1 -0
- package/dist/chunk-UFFJDYCE.js +94 -0
- package/dist/chunk-UFFJDYCE.js.map +1 -0
- package/dist/chunk-VGKZIGVI.js +222 -0
- package/dist/chunk-VGKZIGVI.js.map +1 -0
- package/dist/chunk-VOEWHQUB.js +31 -0
- package/dist/chunk-VOEWHQUB.js.map +1 -0
- package/dist/{chunk-KUPF5KHT.js → chunk-Y5H7C5J4.js} +2 -2
- package/dist/chunk-ZI67WIQS.js +30 -0
- package/dist/chunk-ZI67WIQS.js.map +1 -0
- package/dist/{embedding-D2BYIehX.d.ts → embedding-CW6SaOOz.d.ts} +1 -1
- package/dist/google/index.d.ts +2 -1
- package/dist/google/index.js +202 -199
- package/dist/google/index.js.map +1 -1
- package/dist/groq/index.d.ts +410 -0
- package/dist/groq/index.js +649 -0
- package/dist/groq/index.js.map +1 -0
- package/dist/http/index.d.ts +3 -2
- package/dist/http/index.js +5 -4
- package/dist/image-stream-C0ciACM2.d.ts +11 -0
- package/dist/index.d.ts +8 -118
- package/dist/index.js +518 -767
- package/dist/index.js.map +1 -1
- package/dist/{llm-BQJZj3cD.d.ts → llm-DwbUK7un.d.ts} +12 -1632
- package/dist/middleware/logging/index.d.ts +76 -0
- package/dist/middleware/logging/index.js +74 -0
- package/dist/middleware/logging/index.js.map +1 -0
- package/dist/middleware/parsed-object/index.d.ts +45 -0
- package/dist/middleware/parsed-object/index.js +73 -0
- package/dist/middleware/parsed-object/index.js.map +1 -0
- package/dist/middleware/pubsub/index.d.ts +104 -0
- package/dist/middleware/pubsub/index.js +230 -0
- package/dist/middleware/pubsub/index.js.map +1 -0
- package/dist/middleware/pubsub/server/express/index.d.ts +52 -0
- package/dist/middleware/pubsub/server/express/index.js +11 -0
- package/dist/middleware/pubsub/server/express/index.js.map +1 -0
- package/dist/middleware/pubsub/server/fastify/index.d.ts +53 -0
- package/dist/middleware/pubsub/server/fastify/index.js +11 -0
- package/dist/middleware/pubsub/server/fastify/index.js.map +1 -0
- package/dist/middleware/pubsub/server/h3/index.d.ts +56 -0
- package/dist/middleware/pubsub/server/h3/index.js +11 -0
- package/dist/middleware/pubsub/server/h3/index.js.map +1 -0
- package/dist/middleware/pubsub/server/index.d.ts +78 -0
- package/dist/middleware/pubsub/server/index.js +34 -0
- package/dist/middleware/pubsub/server/index.js.map +1 -0
- package/dist/middleware/pubsub/server/webapi/index.d.ts +53 -0
- package/dist/middleware/pubsub/server/webapi/index.js +11 -0
- package/dist/middleware/pubsub/server/webapi/index.js.map +1 -0
- package/dist/ollama/index.d.ts +2 -1
- package/dist/ollama/index.js +48 -45
- package/dist/ollama/index.js.map +1 -1
- package/dist/openai/index.d.ts +2 -1
- package/dist/openai/index.js +319 -313
- package/dist/openai/index.js.map +1 -1
- package/dist/openrouter/index.d.ts +2 -1
- package/dist/openrouter/index.js +379 -383
- package/dist/openrouter/index.js.map +1 -1
- package/dist/proxy/index.d.ts +10 -914
- package/dist/proxy/index.js +275 -1007
- package/dist/proxy/index.js.map +1 -1
- package/dist/proxy/server/express/index.d.ts +161 -0
- package/dist/proxy/server/express/index.js +24 -0
- package/dist/proxy/server/express/index.js.map +1 -0
- package/dist/proxy/server/fastify/index.d.ts +162 -0
- package/dist/proxy/server/fastify/index.js +24 -0
- package/dist/proxy/server/fastify/index.js.map +1 -0
- package/dist/proxy/server/h3/index.d.ts +189 -0
- package/dist/proxy/server/h3/index.js +28 -0
- package/dist/proxy/server/h3/index.js.map +1 -0
- package/dist/proxy/server/index.d.ts +151 -0
- package/dist/proxy/server/index.js +48 -0
- package/dist/proxy/server/index.js.map +1 -0
- package/dist/proxy/server/webapi/index.d.ts +278 -0
- package/dist/proxy/server/webapi/index.js +32 -0
- package/dist/proxy/server/webapi/index.js.map +1 -0
- package/dist/responses/index.d.ts +650 -0
- package/dist/responses/index.js +930 -0
- package/dist/responses/index.js.map +1 -0
- package/dist/{retry-8Ch-WWgX.d.ts → retry-YayV42GV.d.ts} +1 -1
- package/dist/stream-CecfVCPO.d.ts +1632 -0
- package/dist/types-C8Gciizr.d.ts +168 -0
- package/dist/utils/index.d.ts +53 -0
- package/dist/utils/index.js +7 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/xai/index.d.ts +2 -1
- package/dist/xai/index.js +310 -310
- package/dist/xai/index.js.map +1 -1
- package/package.json +94 -4
- package/dist/chunk-4J6OFUKX.js.map +0 -1
- package/dist/chunk-ILR2D5PN.js.map +0 -1
- package/dist/chunk-TOJCZMVU.js.map +0 -1
- package/dist/chunk-WAKD3OO5.js.map +0 -1
- /package/dist/{chunk-3D6XGGVG.js.map → chunk-ARVM24K2.js.map} +0 -0
- /package/dist/{chunk-KUPF5KHT.js.map → chunk-Y5H7C5J4.js.map} +0 -0
package/dist/proxy/index.d.ts
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { d as Provider, g as ModelReference } from '../llm-DwbUK7un.js';
|
|
2
|
+
export { AdapterOptions, ParsedBody, ProxyHandler, RequestMeta, server } from './server/index.js';
|
|
3
|
+
import { M as Message, a as MessageJSON, T as Turn, b as TurnJSON, S as StreamEvent } from '../stream-CecfVCPO.js';
|
|
4
|
+
export { ParsedEmbeddingRequest, ParsedImageRequest, ParsedRequest, bindTools, parseBody, parseEmbeddingBody, parseImageBody, toEmbeddingJSON, toError, toImageJSON, toImageSSE, toJSON, toSSE, webapi } from './server/webapi/index.js';
|
|
5
|
+
export { express } from './server/express/index.js';
|
|
6
|
+
export { fastify } from './server/fastify/index.js';
|
|
7
|
+
export { h3 } from './server/h3/index.js';
|
|
8
|
+
import '../embedding-CW6SaOOz.js';
|
|
9
|
+
import '../image-stream-C0ciACM2.js';
|
|
3
10
|
|
|
4
11
|
/**
|
|
5
12
|
* @fileoverview Proxy provider types.
|
|
@@ -92,917 +99,6 @@ declare function serializeStreamEvent(event: StreamEvent): StreamEvent;
|
|
|
92
99
|
*/
|
|
93
100
|
declare function deserializeStreamEvent(event: StreamEvent): StreamEvent;
|
|
94
101
|
|
|
95
|
-
/**
|
|
96
|
-
* @fileoverview Image stream helpers for proxy server adapters.
|
|
97
|
-
*
|
|
98
|
-
* @module providers/proxy/server/image-stream
|
|
99
|
-
*/
|
|
100
|
-
|
|
101
|
-
type ImageStreamLike = ImageStreamResult | ImageProviderStreamResult;
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* @fileoverview H3/Nitro/Nuxt adapter for proxy server.
|
|
105
|
-
*
|
|
106
|
-
* Provides utilities for using PP proxy with H3-based servers
|
|
107
|
-
* (Nuxt, Nitro, or standalone H3).
|
|
108
|
-
*
|
|
109
|
-
* @module providers/proxy/server/h3
|
|
110
|
-
*/
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* H3 Event interface (minimal type to avoid dependency).
|
|
114
|
-
*/
|
|
115
|
-
interface H3Event {
|
|
116
|
-
node: {
|
|
117
|
-
res: {
|
|
118
|
-
setHeader(name: string, value: string): void;
|
|
119
|
-
write(chunk: string): boolean;
|
|
120
|
-
end(): void;
|
|
121
|
-
};
|
|
122
|
-
};
|
|
123
|
-
}
|
|
124
|
-
/**
|
|
125
|
-
* Send a Turn as JSON response.
|
|
126
|
-
*
|
|
127
|
-
* @param turn - The completed inference turn
|
|
128
|
-
* @param event - H3 event object
|
|
129
|
-
* @returns Serialized turn data
|
|
130
|
-
*
|
|
131
|
-
* @example
|
|
132
|
-
* ```typescript
|
|
133
|
-
* const turn = await instance.generate(messages);
|
|
134
|
-
* return h3Adapter.sendJSON(turn, event);
|
|
135
|
-
* ```
|
|
136
|
-
*/
|
|
137
|
-
declare function sendJSON$2(turn: Turn, event: H3Event): unknown;
|
|
138
|
-
/**
|
|
139
|
-
* Send an EmbeddingResult as JSON response.
|
|
140
|
-
*
|
|
141
|
-
* @param result - The embedding result
|
|
142
|
-
* @param event - H3 event object
|
|
143
|
-
* @returns Serialized result data
|
|
144
|
-
*/
|
|
145
|
-
declare function sendEmbeddingJSON$2(result: EmbeddingResult, event: H3Event): unknown;
|
|
146
|
-
/**
|
|
147
|
-
* Send an ImageResult as JSON response.
|
|
148
|
-
*
|
|
149
|
-
* @param result - The image result
|
|
150
|
-
* @param event - H3 event object
|
|
151
|
-
* @returns Serialized image result data
|
|
152
|
-
*/
|
|
153
|
-
declare function sendImageJSON$2(result: ImageResult, event: H3Event): unknown;
|
|
154
|
-
/**
|
|
155
|
-
* Stream a StreamResult as Server-Sent Events.
|
|
156
|
-
*
|
|
157
|
-
* @param stream - The StreamResult from instance.stream()
|
|
158
|
-
* @param event - H3 event object
|
|
159
|
-
*
|
|
160
|
-
* @example
|
|
161
|
-
* ```typescript
|
|
162
|
-
* const stream = instance.stream(messages);
|
|
163
|
-
* return h3Adapter.streamSSE(stream, event);
|
|
164
|
-
* ```
|
|
165
|
-
*/
|
|
166
|
-
declare function streamSSE$2(stream: StreamResult, event: H3Event): void;
|
|
167
|
-
/**
|
|
168
|
-
* Stream an ImageStreamResult as Server-Sent Events.
|
|
169
|
-
*
|
|
170
|
-
* @param stream - The ImageStreamResult or ImageProviderStreamResult from image().stream()
|
|
171
|
-
* @param event - H3 event object
|
|
172
|
-
*/
|
|
173
|
-
declare function streamImageSSE$2(stream: ImageStreamLike, event: H3Event): void;
|
|
174
|
-
/**
|
|
175
|
-
* Create a ReadableStream for H3's sendStream utility.
|
|
176
|
-
*
|
|
177
|
-
* Use this with H3's sendStream for better integration:
|
|
178
|
-
* ```typescript
|
|
179
|
-
* import { sendStream } from 'h3';
|
|
180
|
-
* return sendStream(event, h3Adapter.createSSEStream(stream));
|
|
181
|
-
* ```
|
|
182
|
-
*
|
|
183
|
-
* @param stream - The StreamResult from instance.stream()
|
|
184
|
-
* @returns A ReadableStream of SSE data
|
|
185
|
-
*/
|
|
186
|
-
declare function createSSEStream(stream: StreamResult): ReadableStream<Uint8Array>;
|
|
187
|
-
/**
|
|
188
|
-
* Create a ReadableStream for image SSE data.
|
|
189
|
-
*
|
|
190
|
-
* @param stream - The ImageStreamResult or ImageProviderStreamResult from image().stream()
|
|
191
|
-
* @returns A ReadableStream of SSE data
|
|
192
|
-
*/
|
|
193
|
-
declare function createImageSSEStream(stream: ImageStreamLike): ReadableStream<Uint8Array>;
|
|
194
|
-
/**
|
|
195
|
-
* Send an error response.
|
|
196
|
-
*
|
|
197
|
-
* @param message - Error message
|
|
198
|
-
* @param status - HTTP status code
|
|
199
|
-
* @param event - H3 event object
|
|
200
|
-
* @returns Error object for H3 to serialize
|
|
201
|
-
*/
|
|
202
|
-
declare function sendError$2(message: string, status: number, event: H3Event): {
|
|
203
|
-
error: string;
|
|
204
|
-
statusCode: number;
|
|
205
|
-
};
|
|
206
|
-
/**
|
|
207
|
-
* H3/Nitro/Nuxt adapter utilities.
|
|
208
|
-
*
|
|
209
|
-
* @example Basic usage
|
|
210
|
-
* ```typescript
|
|
211
|
-
* // Nuxt server route: server/api/ai.post.ts
|
|
212
|
-
* import { llm } from '@providerprotocol/ai';
|
|
213
|
-
* import { anthropic } from '@providerprotocol/ai/anthropic';
|
|
214
|
-
* import { parseBody } from '@providerprotocol/ai/proxy';
|
|
215
|
-
* import { h3 as h3Adapter } from '@providerprotocol/ai/proxy/server';
|
|
216
|
-
*
|
|
217
|
-
* export default defineEventHandler(async (event) => {
|
|
218
|
-
* const body = await readBody(event);
|
|
219
|
-
* const { messages, system, params } = parseBody(body);
|
|
220
|
-
* const instance = llm({ model: anthropic('claude-sonnet-4-20250514'), system });
|
|
221
|
-
*
|
|
222
|
-
* const wantsStream = getHeader(event, 'accept')?.includes('text/event-stream');
|
|
223
|
-
* if (wantsStream) {
|
|
224
|
-
* return h3Adapter.streamSSE(instance.stream(messages), event);
|
|
225
|
-
* } else {
|
|
226
|
-
* const turn = await instance.generate(messages);
|
|
227
|
-
* return h3Adapter.sendJSON(turn, event);
|
|
228
|
-
* }
|
|
229
|
-
* });
|
|
230
|
-
* ```
|
|
231
|
-
*
|
|
232
|
-
* @example API Gateway with authentication (Nuxt)
|
|
233
|
-
* ```typescript
|
|
234
|
-
* // server/api/ai.post.ts
|
|
235
|
-
* import { llm } from '@providerprotocol/ai';
|
|
236
|
-
* import { anthropic } from '@providerprotocol/ai/anthropic';
|
|
237
|
-
* import { ExponentialBackoff, RoundRobinKeys } from '@providerprotocol/ai/http';
|
|
238
|
-
* import { parseBody } from '@providerprotocol/ai/proxy';
|
|
239
|
-
* import { h3 as h3Adapter } from '@providerprotocol/ai/proxy/server';
|
|
240
|
-
*
|
|
241
|
-
* // Server manages AI provider keys - users never see them
|
|
242
|
-
* const claude = llm({
|
|
243
|
-
* model: anthropic('claude-sonnet-4-20250514'),
|
|
244
|
-
* config: {
|
|
245
|
-
* apiKey: new RoundRobinKeys([
|
|
246
|
-
* process.env.ANTHROPIC_KEY_1!,
|
|
247
|
-
* process.env.ANTHROPIC_KEY_2!,
|
|
248
|
-
* ]),
|
|
249
|
-
* retryStrategy: new ExponentialBackoff({ maxAttempts: 3 }),
|
|
250
|
-
* },
|
|
251
|
-
* });
|
|
252
|
-
*
|
|
253
|
-
* export default defineEventHandler(async (event) => {
|
|
254
|
-
* // Authenticate with your platform credentials
|
|
255
|
-
* const token = getHeader(event, 'authorization')?.replace('Bearer ', '');
|
|
256
|
-
* const user = await validatePlatformToken(token);
|
|
257
|
-
* if (!user) {
|
|
258
|
-
* throw createError({ statusCode: 401, message: 'Unauthorized' });
|
|
259
|
-
* }
|
|
260
|
-
*
|
|
261
|
-
* // Track usage per user
|
|
262
|
-
* // await trackUsage(user.id);
|
|
263
|
-
*
|
|
264
|
-
* const body = await readBody(event);
|
|
265
|
-
* const { messages, system, params } = parseBody(body);
|
|
266
|
-
*
|
|
267
|
-
* if (params?.stream) {
|
|
268
|
-
* return h3Adapter.streamSSE(claude.stream(messages, { system }), event);
|
|
269
|
-
* }
|
|
270
|
-
* const turn = await claude.generate(messages, { system });
|
|
271
|
-
* return h3Adapter.sendJSON(turn, event);
|
|
272
|
-
* });
|
|
273
|
-
* ```
|
|
274
|
-
*/
|
|
275
|
-
declare const h3: {
|
|
276
|
-
sendJSON: typeof sendJSON$2;
|
|
277
|
-
sendEmbeddingJSON: typeof sendEmbeddingJSON$2;
|
|
278
|
-
sendImageJSON: typeof sendImageJSON$2;
|
|
279
|
-
streamSSE: typeof streamSSE$2;
|
|
280
|
-
streamImageSSE: typeof streamImageSSE$2;
|
|
281
|
-
createSSEStream: typeof createSSEStream;
|
|
282
|
-
createImageSSEStream: typeof createImageSSEStream;
|
|
283
|
-
sendError: typeof sendError$2;
|
|
284
|
-
};
|
|
285
|
-
|
|
286
|
-
/**
|
|
287
|
-
* @fileoverview Fastify adapter for proxy server.
|
|
288
|
-
*
|
|
289
|
-
* Provides utilities for using PP proxy with Fastify servers.
|
|
290
|
-
* These adapters convert PP types to Fastify-compatible responses.
|
|
291
|
-
*
|
|
292
|
-
* @module providers/proxy/server/fastify
|
|
293
|
-
*/
|
|
294
|
-
|
|
295
|
-
/**
|
|
296
|
-
* Fastify Reply interface (minimal type to avoid dependency).
|
|
297
|
-
*/
|
|
298
|
-
interface FastifyReply {
|
|
299
|
-
header(name: string, value: string): FastifyReply;
|
|
300
|
-
status(code: number): FastifyReply;
|
|
301
|
-
send(payload: unknown): FastifyReply;
|
|
302
|
-
raw: {
|
|
303
|
-
write(chunk: string): boolean;
|
|
304
|
-
end(): void;
|
|
305
|
-
};
|
|
306
|
-
}
|
|
307
|
-
/**
|
|
308
|
-
* Send a Turn as JSON response.
|
|
309
|
-
*
|
|
310
|
-
* @param turn - The completed inference turn
|
|
311
|
-
* @param reply - Fastify reply object
|
|
312
|
-
*
|
|
313
|
-
* @example
|
|
314
|
-
* ```typescript
|
|
315
|
-
* const turn = await instance.generate(messages);
|
|
316
|
-
* return fastifyAdapter.sendJSON(turn, reply);
|
|
317
|
-
* ```
|
|
318
|
-
*/
|
|
319
|
-
declare function sendJSON$1(turn: Turn, reply: FastifyReply): FastifyReply;
|
|
320
|
-
/**
|
|
321
|
-
* Send an EmbeddingResult as JSON response.
|
|
322
|
-
*
|
|
323
|
-
* @param result - The embedding result
|
|
324
|
-
* @param reply - Fastify reply object
|
|
325
|
-
*/
|
|
326
|
-
declare function sendEmbeddingJSON$1(result: EmbeddingResult, reply: FastifyReply): FastifyReply;
|
|
327
|
-
/**
|
|
328
|
-
* Send an ImageResult as JSON response.
|
|
329
|
-
*
|
|
330
|
-
* @param result - The image result
|
|
331
|
-
* @param reply - Fastify reply object
|
|
332
|
-
*/
|
|
333
|
-
declare function sendImageJSON$1(result: ImageResult, reply: FastifyReply): FastifyReply;
|
|
334
|
-
/**
|
|
335
|
-
* Stream a StreamResult as Server-Sent Events.
|
|
336
|
-
*
|
|
337
|
-
* @param stream - The StreamResult from instance.stream()
|
|
338
|
-
* @param reply - Fastify reply object
|
|
339
|
-
*
|
|
340
|
-
* @example
|
|
341
|
-
* ```typescript
|
|
342
|
-
* const stream = instance.stream(messages);
|
|
343
|
-
* return fastifyAdapter.streamSSE(stream, reply);
|
|
344
|
-
* ```
|
|
345
|
-
*/
|
|
346
|
-
declare function streamSSE$1(stream: StreamResult, reply: FastifyReply): FastifyReply;
|
|
347
|
-
/**
|
|
348
|
-
* Stream an ImageStreamResult as Server-Sent Events.
|
|
349
|
-
*
|
|
350
|
-
* @param stream - The ImageStreamResult or ImageProviderStreamResult from image().stream()
|
|
351
|
-
* @param reply - Fastify reply object
|
|
352
|
-
*/
|
|
353
|
-
declare function streamImageSSE$1(stream: ImageStreamLike, reply: FastifyReply): FastifyReply;
|
|
354
|
-
/**
|
|
355
|
-
* Send an error response.
|
|
356
|
-
*
|
|
357
|
-
* @param message - Error message
|
|
358
|
-
* @param status - HTTP status code
|
|
359
|
-
* @param reply - Fastify reply object
|
|
360
|
-
*/
|
|
361
|
-
declare function sendError$1(message: string, status: number, reply: FastifyReply): FastifyReply;
|
|
362
|
-
/**
|
|
363
|
-
* Fastify adapter utilities.
|
|
364
|
-
*
|
|
365
|
-
* @example Basic usage
|
|
366
|
-
* ```typescript
|
|
367
|
-
* import Fastify from 'fastify';
|
|
368
|
-
* import { llm } from '@providerprotocol/ai';
|
|
369
|
-
* import { anthropic } from '@providerprotocol/ai/anthropic';
|
|
370
|
-
* import { parseBody } from '@providerprotocol/ai/proxy';
|
|
371
|
-
* import { fastify as fastifyAdapter } from '@providerprotocol/ai/proxy/server';
|
|
372
|
-
*
|
|
373
|
-
* const app = Fastify();
|
|
374
|
-
*
|
|
375
|
-
* app.post('/api/ai', async (request, reply) => {
|
|
376
|
-
* const { messages, system, params } = parseBody(request.body);
|
|
377
|
-
* const instance = llm({ model: anthropic('claude-sonnet-4-20250514'), system });
|
|
378
|
-
*
|
|
379
|
-
* if (request.headers.accept?.includes('text/event-stream')) {
|
|
380
|
-
* return fastifyAdapter.streamSSE(instance.stream(messages), reply);
|
|
381
|
-
* } else {
|
|
382
|
-
* const turn = await instance.generate(messages);
|
|
383
|
-
* return fastifyAdapter.sendJSON(turn, reply);
|
|
384
|
-
* }
|
|
385
|
-
* });
|
|
386
|
-
* ```
|
|
387
|
-
*
|
|
388
|
-
* @example API Gateway with authentication
|
|
389
|
-
* ```typescript
|
|
390
|
-
* import Fastify from 'fastify';
|
|
391
|
-
* import { llm } from '@providerprotocol/ai';
|
|
392
|
-
* import { anthropic } from '@providerprotocol/ai/anthropic';
|
|
393
|
-
* import { ExponentialBackoff, RoundRobinKeys } from '@providerprotocol/ai/http';
|
|
394
|
-
* import { parseBody } from '@providerprotocol/ai/proxy';
|
|
395
|
-
* import { fastify as fastifyAdapter } from '@providerprotocol/ai/proxy/server';
|
|
396
|
-
*
|
|
397
|
-
* const app = Fastify();
|
|
398
|
-
*
|
|
399
|
-
* // Server manages AI provider keys - users never see them
|
|
400
|
-
* const claude = llm({
|
|
401
|
-
* model: anthropic('claude-sonnet-4-20250514'),
|
|
402
|
-
* config: {
|
|
403
|
-
* apiKey: new RoundRobinKeys([process.env.ANTHROPIC_KEY_1!, process.env.ANTHROPIC_KEY_2!]),
|
|
404
|
-
* retryStrategy: new ExponentialBackoff({ maxAttempts: 3 }),
|
|
405
|
-
* },
|
|
406
|
-
* });
|
|
407
|
-
*
|
|
408
|
-
* // Auth hook for your platform
|
|
409
|
-
* app.addHook('preHandler', async (request, reply) => {
|
|
410
|
-
* const token = request.headers.authorization?.replace('Bearer ', '');
|
|
411
|
-
* const user = await validatePlatformToken(token);
|
|
412
|
-
* if (!user) {
|
|
413
|
-
* reply.status(401).send({ error: 'Unauthorized' });
|
|
414
|
-
* return;
|
|
415
|
-
* }
|
|
416
|
-
* request.user = user;
|
|
417
|
-
* });
|
|
418
|
-
*
|
|
419
|
-
* app.post('/api/ai', async (request, reply) => {
|
|
420
|
-
* // Track usage per user
|
|
421
|
-
* // await trackUsage(request.user.id);
|
|
422
|
-
*
|
|
423
|
-
* const { messages, system, params } = parseBody(request.body);
|
|
424
|
-
*
|
|
425
|
-
* if (params?.stream) {
|
|
426
|
-
* return fastifyAdapter.streamSSE(claude.stream(messages, { system }), reply);
|
|
427
|
-
* }
|
|
428
|
-
* const turn = await claude.generate(messages, { system });
|
|
429
|
-
* return fastifyAdapter.sendJSON(turn, reply);
|
|
430
|
-
* });
|
|
431
|
-
* ```
|
|
432
|
-
*/
|
|
433
|
-
declare const fastify: {
|
|
434
|
-
sendJSON: typeof sendJSON$1;
|
|
435
|
-
sendEmbeddingJSON: typeof sendEmbeddingJSON$1;
|
|
436
|
-
sendImageJSON: typeof sendImageJSON$1;
|
|
437
|
-
streamSSE: typeof streamSSE$1;
|
|
438
|
-
streamImageSSE: typeof streamImageSSE$1;
|
|
439
|
-
sendError: typeof sendError$1;
|
|
440
|
-
};
|
|
441
|
-
|
|
442
|
-
/**
|
|
443
|
-
* @fileoverview Express/Connect adapter for proxy server.
|
|
444
|
-
*
|
|
445
|
-
* Provides utilities for using PP proxy with Express.js or Connect-based servers.
|
|
446
|
-
* These adapters convert PP types to Express-compatible responses.
|
|
447
|
-
*
|
|
448
|
-
* @module providers/proxy/server/express
|
|
449
|
-
*/
|
|
450
|
-
|
|
451
|
-
/**
|
|
452
|
-
* Express Response interface (minimal type to avoid dependency).
|
|
453
|
-
*/
|
|
454
|
-
interface ExpressResponse {
|
|
455
|
-
setHeader(name: string, value: string): void;
|
|
456
|
-
status(code: number): ExpressResponse;
|
|
457
|
-
write(chunk: string): boolean;
|
|
458
|
-
end(): void;
|
|
459
|
-
json(body: unknown): void;
|
|
460
|
-
}
|
|
461
|
-
/**
|
|
462
|
-
* Send a Turn as JSON response.
|
|
463
|
-
*
|
|
464
|
-
* @param turn - The completed inference turn
|
|
465
|
-
* @param res - Express response object
|
|
466
|
-
*
|
|
467
|
-
* @example
|
|
468
|
-
* ```typescript
|
|
469
|
-
* const turn = await instance.generate(messages);
|
|
470
|
-
* expressAdapter.sendJSON(turn, res);
|
|
471
|
-
* ```
|
|
472
|
-
*/
|
|
473
|
-
declare function sendJSON(turn: Turn, res: ExpressResponse): void;
|
|
474
|
-
/**
|
|
475
|
-
* Send an EmbeddingResult as JSON response.
|
|
476
|
-
*
|
|
477
|
-
* @param result - The embedding result
|
|
478
|
-
* @param res - Express response object
|
|
479
|
-
*/
|
|
480
|
-
declare function sendEmbeddingJSON(result: EmbeddingResult, res: ExpressResponse): void;
|
|
481
|
-
/**
|
|
482
|
-
* Send an ImageResult as JSON response.
|
|
483
|
-
*
|
|
484
|
-
* @param result - The image result
|
|
485
|
-
* @param res - Express response object
|
|
486
|
-
*/
|
|
487
|
-
declare function sendImageJSON(result: ImageResult, res: ExpressResponse): void;
|
|
488
|
-
/**
|
|
489
|
-
* Stream a StreamResult as Server-Sent Events.
|
|
490
|
-
*
|
|
491
|
-
* @param stream - The StreamResult from instance.stream()
|
|
492
|
-
* @param res - Express response object
|
|
493
|
-
*
|
|
494
|
-
* @example
|
|
495
|
-
* ```typescript
|
|
496
|
-
* const stream = instance.stream(messages);
|
|
497
|
-
* expressAdapter.streamSSE(stream, res);
|
|
498
|
-
* ```
|
|
499
|
-
*/
|
|
500
|
-
declare function streamSSE(stream: StreamResult, res: ExpressResponse): void;
|
|
501
|
-
/**
|
|
502
|
-
* Stream an ImageStreamResult as Server-Sent Events.
|
|
503
|
-
*
|
|
504
|
-
* @param stream - The ImageStreamResult or ImageProviderStreamResult from image().stream()
|
|
505
|
-
* @param res - Express response object
|
|
506
|
-
*/
|
|
507
|
-
declare function streamImageSSE(stream: ImageStreamLike, res: ExpressResponse): void;
|
|
508
|
-
/**
|
|
509
|
-
* Send an error response.
|
|
510
|
-
*
|
|
511
|
-
* @param message - Error message
|
|
512
|
-
* @param status - HTTP status code
|
|
513
|
-
* @param res - Express response object
|
|
514
|
-
*/
|
|
515
|
-
declare function sendError(message: string, status: number, res: ExpressResponse): void;
|
|
516
|
-
/**
|
|
517
|
-
* Express adapter utilities.
|
|
518
|
-
*
|
|
519
|
-
* @example Basic usage
|
|
520
|
-
* ```typescript
|
|
521
|
-
* import express from 'express';
|
|
522
|
-
* import { llm } from '@providerprotocol/ai';
|
|
523
|
-
* import { anthropic } from '@providerprotocol/ai/anthropic';
|
|
524
|
-
* import { parseBody } from '@providerprotocol/ai/proxy';
|
|
525
|
-
* import { express as expressAdapter } from '@providerprotocol/ai/proxy/server';
|
|
526
|
-
*
|
|
527
|
-
* const app = express();
|
|
528
|
-
* app.use(express.json());
|
|
529
|
-
*
|
|
530
|
-
* app.post('/api/ai', async (req, res) => {
|
|
531
|
-
* const { messages, system, params } = parseBody(req.body);
|
|
532
|
-
* const instance = llm({ model: anthropic('claude-sonnet-4-20250514'), system });
|
|
533
|
-
*
|
|
534
|
-
* if (req.headers.accept?.includes('text/event-stream')) {
|
|
535
|
-
* expressAdapter.streamSSE(instance.stream(messages), res);
|
|
536
|
-
* } else {
|
|
537
|
-
* const turn = await instance.generate(messages);
|
|
538
|
-
* expressAdapter.sendJSON(turn, res);
|
|
539
|
-
* }
|
|
540
|
-
* });
|
|
541
|
-
* ```
|
|
542
|
-
*
|
|
543
|
-
* @example API Gateway with authentication
|
|
544
|
-
* ```typescript
|
|
545
|
-
* import express from 'express';
|
|
546
|
-
* import { llm } from '@providerprotocol/ai';
|
|
547
|
-
* import { anthropic } from '@providerprotocol/ai/anthropic';
|
|
548
|
-
* import { ExponentialBackoff, RoundRobinKeys } from '@providerprotocol/ai/http';
|
|
549
|
-
* import { parseBody } from '@providerprotocol/ai/proxy';
|
|
550
|
-
* import { express as expressAdapter } from '@providerprotocol/ai/proxy/server';
|
|
551
|
-
*
|
|
552
|
-
* const app = express();
|
|
553
|
-
* app.use(express.json());
|
|
554
|
-
*
|
|
555
|
-
* // Your platform's auth middleware
|
|
556
|
-
* async function authMiddleware(req, res, next) {
|
|
557
|
-
* const token = req.headers.authorization?.replace('Bearer ', '');
|
|
558
|
-
* const user = await validatePlatformToken(token);
|
|
559
|
-
* if (!user) return res.status(401).json({ error: 'Unauthorized' });
|
|
560
|
-
* req.user = user;
|
|
561
|
-
* next();
|
|
562
|
-
* }
|
|
563
|
-
*
|
|
564
|
-
* // Server manages AI provider keys - users never see them
|
|
565
|
-
* const claude = llm({
|
|
566
|
-
* model: anthropic('claude-sonnet-4-20250514'),
|
|
567
|
-
* config: {
|
|
568
|
-
* apiKey: new RoundRobinKeys([process.env.ANTHROPIC_KEY_1!, process.env.ANTHROPIC_KEY_2!]),
|
|
569
|
-
* retryStrategy: new ExponentialBackoff({ maxAttempts: 3 }),
|
|
570
|
-
* },
|
|
571
|
-
* });
|
|
572
|
-
*
|
|
573
|
-
* app.post('/api/ai', authMiddleware, async (req, res) => {
|
|
574
|
-
* // Track usage per user
|
|
575
|
-
* // await trackUsage(req.user.id);
|
|
576
|
-
*
|
|
577
|
-
* const { messages, system, params } = parseBody(req.body);
|
|
578
|
-
*
|
|
579
|
-
* if (params?.stream) {
|
|
580
|
-
* expressAdapter.streamSSE(claude.stream(messages, { system }), res);
|
|
581
|
-
* } else {
|
|
582
|
-
* const turn = await claude.generate(messages, { system });
|
|
583
|
-
* expressAdapter.sendJSON(turn, res);
|
|
584
|
-
* }
|
|
585
|
-
* });
|
|
586
|
-
* ```
|
|
587
|
-
*/
|
|
588
|
-
declare const express: {
|
|
589
|
-
sendJSON: typeof sendJSON;
|
|
590
|
-
sendEmbeddingJSON: typeof sendEmbeddingJSON;
|
|
591
|
-
sendImageJSON: typeof sendImageJSON;
|
|
592
|
-
streamSSE: typeof streamSSE;
|
|
593
|
-
streamImageSSE: typeof streamImageSSE;
|
|
594
|
-
sendError: typeof sendError;
|
|
595
|
-
};
|
|
596
|
-
|
|
597
|
-
/**
|
|
598
|
-
* @fileoverview Media serialization utilities for proxy transport.
|
|
599
|
-
*
|
|
600
|
-
* Handles converting embedding inputs and image results/events to/from JSON
|
|
601
|
-
* for HTTP transport. These are pure functions with no side effects.
|
|
602
|
-
*
|
|
603
|
-
* @module providers/proxy/serialization.media
|
|
604
|
-
*/
|
|
605
|
-
|
|
606
|
-
type SerializedImageSource = {
|
|
607
|
-
type: 'base64';
|
|
608
|
-
data: string;
|
|
609
|
-
} | {
|
|
610
|
-
type: 'url';
|
|
611
|
-
url: string;
|
|
612
|
-
} | {
|
|
613
|
-
type: 'bytes';
|
|
614
|
-
data: number[] | string;
|
|
615
|
-
};
|
|
616
|
-
interface SerializedImage {
|
|
617
|
-
source: SerializedImageSource;
|
|
618
|
-
mimeType: string;
|
|
619
|
-
width?: number;
|
|
620
|
-
height?: number;
|
|
621
|
-
}
|
|
622
|
-
/**
|
|
623
|
-
* Deserialize an Image from JSON transport.
|
|
624
|
-
*/
|
|
625
|
-
declare function deserializeImage(image: SerializedImage): Image;
|
|
626
|
-
|
|
627
|
-
/**
|
|
628
|
-
* @fileoverview Web API adapter for proxy server.
|
|
629
|
-
*
|
|
630
|
-
* Provides utilities for using PP proxy with Web API native frameworks
|
|
631
|
-
* (Bun, Deno, Next.js App Router, Cloudflare Workers).
|
|
632
|
-
*
|
|
633
|
-
* These utilities return standard Web API Response objects that work
|
|
634
|
-
* directly with modern runtimes.
|
|
635
|
-
*
|
|
636
|
-
* @module providers/proxy/server/webapi
|
|
637
|
-
*/
|
|
638
|
-
|
|
639
|
-
/**
|
|
640
|
-
* Parsed request body from a proxy HTTP request.
|
|
641
|
-
* This is just the deserialized PP data from the request body.
|
|
642
|
-
*/
|
|
643
|
-
interface ParsedRequest {
|
|
644
|
-
messages: Message[];
|
|
645
|
-
system?: string | unknown[];
|
|
646
|
-
params?: Record<string, unknown>;
|
|
647
|
-
model?: string;
|
|
648
|
-
tools?: Array<{
|
|
649
|
-
name: string;
|
|
650
|
-
description: string;
|
|
651
|
-
parameters: JSONSchema;
|
|
652
|
-
metadata?: ToolMetadata;
|
|
653
|
-
}>;
|
|
654
|
-
structure?: JSONSchema;
|
|
655
|
-
}
|
|
656
|
-
/**
|
|
657
|
-
* Parsed request body for embedding endpoints.
|
|
658
|
-
*/
|
|
659
|
-
interface ParsedEmbeddingRequest {
|
|
660
|
-
inputs: EmbeddingInput[];
|
|
661
|
-
params?: Record<string, unknown>;
|
|
662
|
-
model?: string;
|
|
663
|
-
}
|
|
664
|
-
/**
|
|
665
|
-
* Parsed request body for image endpoints.
|
|
666
|
-
*/
|
|
667
|
-
interface ParsedImageRequest {
|
|
668
|
-
prompt: string;
|
|
669
|
-
params?: Record<string, unknown>;
|
|
670
|
-
model?: string;
|
|
671
|
-
image?: ReturnType<typeof deserializeImage>;
|
|
672
|
-
mask?: ReturnType<typeof deserializeImage>;
|
|
673
|
-
}
|
|
674
|
-
/**
|
|
675
|
-
* Parse an HTTP request body into PP types.
|
|
676
|
-
*
|
|
677
|
-
* @param body - The JSON-parsed request body
|
|
678
|
-
* @returns Deserialized PP data
|
|
679
|
-
*
|
|
680
|
-
* @example
|
|
681
|
-
* ```typescript
|
|
682
|
-
* const body = await req.json();
|
|
683
|
-
* const { messages, system, params } = parseBody(body);
|
|
684
|
-
*
|
|
685
|
-
* const instance = llm({ model: anthropic('...'), system, params });
|
|
686
|
-
* const turn = await instance.generate(messages);
|
|
687
|
-
* ```
|
|
688
|
-
*/
|
|
689
|
-
declare function parseBody(body: unknown): ParsedRequest;
|
|
690
|
-
/**
|
|
691
|
-
* Parse an HTTP request body into embedding inputs.
|
|
692
|
-
*
|
|
693
|
-
* @param body - The JSON-parsed request body
|
|
694
|
-
* @returns Parsed embedding request data
|
|
695
|
-
*/
|
|
696
|
-
declare function parseEmbeddingBody(body: unknown): ParsedEmbeddingRequest;
|
|
697
|
-
/**
|
|
698
|
-
* Parse an HTTP request body into image request data.
|
|
699
|
-
*
|
|
700
|
-
* @param body - The JSON-parsed request body
|
|
701
|
-
* @returns Parsed image request data
|
|
702
|
-
*/
|
|
703
|
-
declare function parseImageBody(body: unknown): ParsedImageRequest;
|
|
704
|
-
/**
|
|
705
|
-
* Create a JSON Response from a Turn.
|
|
706
|
-
*
|
|
707
|
-
* @param turn - The completed inference turn
|
|
708
|
-
* @returns HTTP Response with JSON body
|
|
709
|
-
*
|
|
710
|
-
* @example
|
|
711
|
-
* ```typescript
|
|
712
|
-
* const turn = await instance.generate(messages);
|
|
713
|
-
* return toJSON(turn);
|
|
714
|
-
* ```
|
|
715
|
-
*/
|
|
716
|
-
declare function toJSON(turn: Turn): Response;
|
|
717
|
-
/**
|
|
718
|
-
* Create a JSON Response from an embedding result.
|
|
719
|
-
*
|
|
720
|
-
* @param result - The embedding result
|
|
721
|
-
* @returns HTTP Response with JSON body
|
|
722
|
-
*/
|
|
723
|
-
declare function toEmbeddingJSON(result: EmbeddingResult): Response;
|
|
724
|
-
/**
|
|
725
|
-
* Create a JSON Response from an image result.
|
|
726
|
-
*
|
|
727
|
-
* @param result - The image result
|
|
728
|
-
* @returns HTTP Response with JSON body
|
|
729
|
-
*/
|
|
730
|
-
declare function toImageJSON(result: ImageResult): Response;
|
|
731
|
-
/**
|
|
732
|
-
* Create an SSE Response from a StreamResult.
|
|
733
|
-
*
|
|
734
|
-
* Streams PP StreamEvents as SSE, then sends the final Turn data.
|
|
735
|
-
*
|
|
736
|
-
* @param stream - The StreamResult from instance.stream()
|
|
737
|
-
* @returns HTTP Response with SSE body
|
|
738
|
-
*
|
|
739
|
-
* @example
|
|
740
|
-
* ```typescript
|
|
741
|
-
* const stream = instance.stream(messages);
|
|
742
|
-
* return toSSE(stream);
|
|
743
|
-
* ```
|
|
744
|
-
*/
|
|
745
|
-
declare function toSSE(stream: StreamResult): Response;
|
|
746
|
-
/**
|
|
747
|
-
* Create an SSE Response from an ImageStreamResult.
|
|
748
|
-
*
|
|
749
|
-
* Streams image events as SSE, then sends the final image result.
|
|
750
|
-
*
|
|
751
|
-
* @param stream - The ImageStreamResult or ImageProviderStreamResult from image().stream()
|
|
752
|
-
* @returns HTTP Response with SSE body
|
|
753
|
-
*/
|
|
754
|
-
declare function toImageSSE(stream: ImageStreamLike): Response;
|
|
755
|
-
/**
|
|
756
|
-
* Create an error Response.
|
|
757
|
-
*
|
|
758
|
-
* @param message - Error message
|
|
759
|
-
* @param status - HTTP status code (default: 500)
|
|
760
|
-
* @returns HTTP Response with error body
|
|
761
|
-
*/
|
|
762
|
-
declare function toError(message: string, status?: number): Response;
|
|
763
|
-
/**
|
|
764
|
-
* Bind tool schemas to implementation functions.
|
|
765
|
-
*
|
|
766
|
-
* Takes tool schemas from the request and binds them to your
|
|
767
|
-
* server-side implementations.
|
|
768
|
-
*
|
|
769
|
-
* @param schemas - Tool schemas from the request
|
|
770
|
-
* @param implementations - Map of tool name to implementation
|
|
771
|
-
* @returns Array of complete Tool objects
|
|
772
|
-
*
|
|
773
|
-
* @example
|
|
774
|
-
* ```typescript
|
|
775
|
-
* const { tools: schemas } = parseBody(body);
|
|
776
|
-
*
|
|
777
|
-
* const tools = bindTools(schemas, {
|
|
778
|
-
* get_weather: async ({ location }) => fetchWeather(location),
|
|
779
|
-
* search: async ({ query }) => searchDB(query),
|
|
780
|
-
* });
|
|
781
|
-
*
|
|
782
|
-
* const instance = llm({ model, tools });
|
|
783
|
-
* ```
|
|
784
|
-
*/
|
|
785
|
-
declare function bindTools(schemas: ParsedRequest['tools'], implementations: Record<string, (params: unknown) => unknown | Promise<unknown>>): Tool[];
|
|
786
|
-
/**
|
|
787
|
-
* Web API adapter utilities.
|
|
788
|
-
*
|
|
789
|
-
* For use with Bun, Deno, Next.js App Router, Cloudflare Workers,
|
|
790
|
-
* and other frameworks that support Web API Response.
|
|
791
|
-
*
|
|
792
|
-
* **Security Note:** The proxy works without configuration, meaning no
|
|
793
|
-
* authentication by default. Always add your own auth layer in production.
|
|
794
|
-
*
|
|
795
|
-
* @example Basic usage
|
|
796
|
-
* ```typescript
|
|
797
|
-
* import { llm } from '@providerprotocol/ai';
|
|
798
|
-
* import { anthropic } from '@providerprotocol/ai/anthropic';
|
|
799
|
-
* import { parseBody, toJSON, toSSE } from '@providerprotocol/ai/proxy';
|
|
800
|
-
*
|
|
801
|
-
* // Bun.serve / Deno.serve / Next.js App Router
|
|
802
|
-
* export async function POST(req: Request) {
|
|
803
|
-
* const { messages, system } = parseBody(await req.json());
|
|
804
|
-
* const instance = llm({ model: anthropic('claude-sonnet-4-20250514'), system });
|
|
805
|
-
*
|
|
806
|
-
* if (req.headers.get('accept')?.includes('text/event-stream')) {
|
|
807
|
-
* return toSSE(instance.stream(messages));
|
|
808
|
-
* }
|
|
809
|
-
* return toJSON(await instance.generate(messages));
|
|
810
|
-
* }
|
|
811
|
-
* ```
|
|
812
|
-
*
|
|
813
|
-
* @example API Gateway with authentication
|
|
814
|
-
* ```typescript
|
|
815
|
-
* import { llm } from '@providerprotocol/ai';
|
|
816
|
-
* import { anthropic } from '@providerprotocol/ai/anthropic';
|
|
817
|
-
* import { ExponentialBackoff, RoundRobinKeys } from '@providerprotocol/ai/http';
|
|
818
|
-
* import { parseBody, toJSON, toSSE, toError } from '@providerprotocol/ai/proxy';
|
|
819
|
-
*
|
|
820
|
-
* // Your platform's user validation
|
|
821
|
-
* async function validateToken(token: string): Promise<{ id: string } | null> {
|
|
822
|
-
* // Verify JWT, check database, etc.
|
|
823
|
-
* return token ? { id: 'user-123' } : null;
|
|
824
|
-
* }
|
|
825
|
-
*
|
|
826
|
-
* // Server manages AI provider keys - users never see them
|
|
827
|
-
* const claude = llm({
|
|
828
|
-
* model: anthropic('claude-sonnet-4-20250514'),
|
|
829
|
-
* config: {
|
|
830
|
-
* apiKey: new RoundRobinKeys([process.env.ANTHROPIC_KEY_1!, process.env.ANTHROPIC_KEY_2!]),
|
|
831
|
-
* retryStrategy: new ExponentialBackoff({ maxAttempts: 3 }),
|
|
832
|
-
* },
|
|
833
|
-
* });
|
|
834
|
-
*
|
|
835
|
-
* Bun.serve({
|
|
836
|
-
* port: 3000,
|
|
837
|
-
* async fetch(req) {
|
|
838
|
-
* // Authenticate with YOUR platform credentials
|
|
839
|
-
* const token = req.headers.get('Authorization')?.replace('Bearer ', '');
|
|
840
|
-
* const user = await validateToken(token ?? '');
|
|
841
|
-
* if (!user) return toError('Unauthorized', 401);
|
|
842
|
-
*
|
|
843
|
-
* // Rate limit, track usage, bill user, etc.
|
|
844
|
-
* // await trackUsage(user.id);
|
|
845
|
-
*
|
|
846
|
-
* const { messages, system, params } = parseBody(await req.json());
|
|
847
|
-
*
|
|
848
|
-
* if (params?.stream) {
|
|
849
|
-
* return toSSE(claude.stream(messages, { system }));
|
|
850
|
-
* }
|
|
851
|
-
* return toJSON(await claude.generate(messages, { system }));
|
|
852
|
-
* },
|
|
853
|
-
* });
|
|
854
|
-
* ```
|
|
855
|
-
*/
|
|
856
|
-
declare const webapi: {
|
|
857
|
-
parseBody: typeof parseBody;
|
|
858
|
-
parseEmbeddingBody: typeof parseEmbeddingBody;
|
|
859
|
-
parseImageBody: typeof parseImageBody;
|
|
860
|
-
toJSON: typeof toJSON;
|
|
861
|
-
toEmbeddingJSON: typeof toEmbeddingJSON;
|
|
862
|
-
toImageJSON: typeof toImageJSON;
|
|
863
|
-
toSSE: typeof toSSE;
|
|
864
|
-
toImageSSE: typeof toImageSSE;
|
|
865
|
-
toError: typeof toError;
|
|
866
|
-
bindTools: typeof bindTools;
|
|
867
|
-
};
|
|
868
|
-
|
|
869
|
-
/**
|
|
870
|
-
* @fileoverview Shared types for proxy server adapters.
|
|
871
|
-
*
|
|
872
|
-
* @module providers/proxy/server/types
|
|
873
|
-
*/
|
|
874
|
-
|
|
875
|
-
/**
|
|
876
|
-
* Parsed request body from a proxy HTTP request.
|
|
877
|
-
*/
|
|
878
|
-
interface ParsedBody {
|
|
879
|
-
messages: Message[];
|
|
880
|
-
system?: string | unknown[];
|
|
881
|
-
params?: Record<string, unknown>;
|
|
882
|
-
model?: string;
|
|
883
|
-
tools?: Array<{
|
|
884
|
-
name: string;
|
|
885
|
-
description: string;
|
|
886
|
-
parameters: JSONSchema;
|
|
887
|
-
metadata?: ToolMetadata;
|
|
888
|
-
}>;
|
|
889
|
-
structure?: JSONSchema;
|
|
890
|
-
}
|
|
891
|
-
/**
|
|
892
|
-
* Handler function signature for proxy endpoints.
|
|
893
|
-
* Takes parsed request data and returns either a Turn or StreamResult.
|
|
894
|
-
*/
|
|
895
|
-
type ProxyHandler = (body: ParsedBody, meta: RequestMeta) => Promise<Turn> | StreamResult | Promise<StreamResult>;
|
|
896
|
-
/**
|
|
897
|
-
* Metadata about the incoming request.
|
|
898
|
-
*/
|
|
899
|
-
interface RequestMeta {
|
|
900
|
-
/** Whether the client wants a streaming response */
|
|
901
|
-
wantsStream: boolean;
|
|
902
|
-
/** Raw headers from the request */
|
|
903
|
-
headers: Record<string, string | undefined>;
|
|
904
|
-
}
|
|
905
|
-
/**
|
|
906
|
-
* Options for adapter middleware.
|
|
907
|
-
*/
|
|
908
|
-
interface AdapterOptions {
|
|
909
|
-
/** Custom error handler */
|
|
910
|
-
onError?: (error: Error) => {
|
|
911
|
-
message: string;
|
|
912
|
-
status: number;
|
|
913
|
-
};
|
|
914
|
-
}
|
|
915
|
-
|
|
916
|
-
/**
|
|
917
|
-
* Server adapters namespace.
|
|
918
|
-
*
|
|
919
|
-
* Contains framework-specific adapters for Web API, Express, Fastify, and H3.
|
|
920
|
-
*
|
|
921
|
-
* @example Express
|
|
922
|
-
* ```typescript
|
|
923
|
-
* import { express } from '@providerprotocol/ai/proxy/server';
|
|
924
|
-
*
|
|
925
|
-
* app.post('/api/ai', async (req, res) => {
|
|
926
|
-
* const { messages } = parseBody(req.body);
|
|
927
|
-
* if (req.headers.accept?.includes('text/event-stream')) {
|
|
928
|
-
* express.streamSSE(instance.stream(messages), res);
|
|
929
|
-
* } else {
|
|
930
|
-
* express.sendJSON(await instance.generate(messages), res);
|
|
931
|
-
* }
|
|
932
|
-
* });
|
|
933
|
-
* ```
|
|
934
|
-
*
|
|
935
|
-
* @example Fastify
|
|
936
|
-
* ```typescript
|
|
937
|
-
* import { fastify } from '@providerprotocol/ai/proxy/server';
|
|
938
|
-
*
|
|
939
|
-
* app.post('/api/ai', async (request, reply) => {
|
|
940
|
-
* const { messages } = parseBody(request.body);
|
|
941
|
-
* if (request.headers.accept?.includes('text/event-stream')) {
|
|
942
|
-
* return fastify.streamSSE(instance.stream(messages), reply);
|
|
943
|
-
* }
|
|
944
|
-
* return fastify.sendJSON(await instance.generate(messages), reply);
|
|
945
|
-
* });
|
|
946
|
-
* ```
|
|
947
|
-
*
|
|
948
|
-
* @example H3/Nuxt
|
|
949
|
-
* ```typescript
|
|
950
|
-
* import { h3 } from '@providerprotocol/ai/proxy/server';
|
|
951
|
-
*
|
|
952
|
-
* export default defineEventHandler(async (event) => {
|
|
953
|
-
* const { messages } = parseBody(await readBody(event));
|
|
954
|
-
* if (getHeader(event, 'accept')?.includes('text/event-stream')) {
|
|
955
|
-
* return h3.streamSSE(instance.stream(messages), event);
|
|
956
|
-
* }
|
|
957
|
-
* return h3.sendJSON(await instance.generate(messages), event);
|
|
958
|
-
* });
|
|
959
|
-
* ```
|
|
960
|
-
*/
|
|
961
|
-
declare const server: {
|
|
962
|
-
/** Web API adapter (Bun, Deno, Next.js, Workers) */
|
|
963
|
-
webapi: {
|
|
964
|
-
parseBody: typeof parseBody;
|
|
965
|
-
parseEmbeddingBody: typeof parseEmbeddingBody;
|
|
966
|
-
parseImageBody: typeof parseImageBody;
|
|
967
|
-
toJSON: typeof toJSON;
|
|
968
|
-
toEmbeddingJSON: typeof toEmbeddingJSON;
|
|
969
|
-
toImageJSON: typeof toImageJSON;
|
|
970
|
-
toSSE: typeof toSSE;
|
|
971
|
-
toImageSSE: typeof toImageSSE;
|
|
972
|
-
toError: typeof toError;
|
|
973
|
-
bindTools: typeof bindTools;
|
|
974
|
-
};
|
|
975
|
-
/** Express/Connect adapter */
|
|
976
|
-
express: {
|
|
977
|
-
sendJSON: typeof sendJSON;
|
|
978
|
-
sendEmbeddingJSON: typeof sendEmbeddingJSON;
|
|
979
|
-
sendImageJSON: typeof sendImageJSON;
|
|
980
|
-
streamSSE: typeof streamSSE;
|
|
981
|
-
streamImageSSE: typeof streamImageSSE;
|
|
982
|
-
sendError: typeof sendError;
|
|
983
|
-
};
|
|
984
|
-
/** Fastify adapter */
|
|
985
|
-
fastify: {
|
|
986
|
-
sendJSON: typeof sendJSON$1;
|
|
987
|
-
sendEmbeddingJSON: typeof sendEmbeddingJSON$1;
|
|
988
|
-
sendImageJSON: typeof sendImageJSON$1;
|
|
989
|
-
streamSSE: typeof streamSSE$1;
|
|
990
|
-
streamImageSSE: typeof streamImageSSE$1;
|
|
991
|
-
sendError: typeof sendError$1;
|
|
992
|
-
};
|
|
993
|
-
/** H3/Nitro/Nuxt adapter */
|
|
994
|
-
h3: {
|
|
995
|
-
sendJSON: typeof sendJSON$2;
|
|
996
|
-
sendEmbeddingJSON: typeof sendEmbeddingJSON$2;
|
|
997
|
-
sendImageJSON: typeof sendImageJSON$2;
|
|
998
|
-
streamSSE: typeof streamSSE$2;
|
|
999
|
-
streamImageSSE: typeof streamImageSSE$2;
|
|
1000
|
-
createSSEStream: typeof createSSEStream;
|
|
1001
|
-
createImageSSEStream: typeof createImageSSEStream;
|
|
1002
|
-
sendError: typeof sendError$2;
|
|
1003
|
-
};
|
|
1004
|
-
};
|
|
1005
|
-
|
|
1006
102
|
/**
|
|
1007
103
|
* Creates a proxy provider that transports PP requests over HTTP to a backend server.
|
|
1008
104
|
*
|
|
@@ -1048,4 +144,4 @@ declare function proxy(options: ProxyProviderOptions): Provider<ProxyRequestOpti
|
|
|
1048
144
|
*/
|
|
1049
145
|
declare function proxyModel(endpoint: string): ModelReference<ProxyRequestOptions>;
|
|
1050
146
|
|
|
1051
|
-
export { type
|
|
147
|
+
export { type ProxyEmbeddingParams, type ProxyImageParams, type ProxyLLMParams, type ProxyProviderOptions, type ProxyRequestOptions, TurnJSON, deserializeMessage, deserializeStreamEvent, proxy, proxyModel, serializeMessage, serializeStreamEvent, serializeTurn };
|