@providerprotocol/ai 0.0.17 → 0.0.18
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/dist/anthropic/index.js +5 -3
- package/dist/anthropic/index.js.map +1 -1
- package/dist/{chunk-MOU4U3PO.js → chunk-5FEAOEXV.js} +4 -68
- package/dist/chunk-5FEAOEXV.js.map +1 -0
- package/dist/chunk-DZQHVGNV.js +71 -0
- package/dist/chunk-DZQHVGNV.js.map +1 -0
- package/dist/chunk-SKY2JLA7.js +59 -0
- package/dist/chunk-SKY2JLA7.js.map +1 -0
- package/dist/{chunk-SVYROCLD.js → chunk-UMKWXGO3.js} +1 -1
- package/dist/chunk-UMKWXGO3.js.map +1 -0
- package/dist/google/index.js +5 -3
- package/dist/google/index.js.map +1 -1
- package/dist/http/index.js +2 -1
- package/dist/index.d.ts +7 -1300
- package/dist/index.js +11 -57
- package/dist/index.js.map +1 -1
- package/dist/ollama/index.js +5 -3
- package/dist/ollama/index.js.map +1 -1
- package/dist/openai/index.js +5 -3
- package/dist/openai/index.js.map +1 -1
- package/dist/openrouter/index.js +5 -3
- package/dist/openrouter/index.js.map +1 -1
- package/dist/proxy/index.d.ts +611 -0
- package/dist/proxy/index.js +565 -0
- package/dist/proxy/index.js.map +1 -0
- package/dist/stream-BjyVzBxV.d.ts +1286 -0
- package/dist/xai/index.js +5 -3
- package/dist/xai/index.js.map +1 -1
- package/package.json +6 -1
- package/dist/chunk-MOU4U3PO.js.map +0 -1
- package/dist/chunk-SVYROCLD.js.map +0 -1
|
@@ -0,0 +1,611 @@
|
|
|
1
|
+
import { d as Provider, f as ModelReference } from '../provider-D5MO3-pS.js';
|
|
2
|
+
import { M as Message, b as MessageJSON, T as Turn, $ as TurnJSON, g as StreamEvent, S as StreamResult, J as JSONSchema, u as ToolMetadata, c as Tool } from '../stream-BjyVzBxV.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @fileoverview Proxy provider types.
|
|
6
|
+
*
|
|
7
|
+
* Defines the configuration options and parameters for the proxy provider,
|
|
8
|
+
* which transports PP requests over HTTP to a backend server.
|
|
9
|
+
*
|
|
10
|
+
* @module providers/proxy/types
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* Proxy-specific LLM parameters.
|
|
14
|
+
*
|
|
15
|
+
* These parameters are passed through to the backend server.
|
|
16
|
+
* The server decides how to interpret them based on its own provider.
|
|
17
|
+
*/
|
|
18
|
+
interface ProxyLLMParams {
|
|
19
|
+
/** Parameters are passed through to the backend */
|
|
20
|
+
[key: string]: unknown;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Configuration options for creating a proxy provider.
|
|
24
|
+
*/
|
|
25
|
+
interface ProxyProviderOptions {
|
|
26
|
+
/** The endpoint URL to proxy requests to */
|
|
27
|
+
endpoint: string;
|
|
28
|
+
/** Default headers to include in all requests */
|
|
29
|
+
headers?: Record<string, string>;
|
|
30
|
+
/** Custom fetch implementation */
|
|
31
|
+
fetch?: typeof fetch;
|
|
32
|
+
/** Request timeout in milliseconds (default: 120000) */
|
|
33
|
+
timeout?: number;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Per-request options for proxy calls.
|
|
37
|
+
*/
|
|
38
|
+
interface ProxyRequestOptions {
|
|
39
|
+
/** Additional headers for this request */
|
|
40
|
+
headers?: Record<string, string>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* @fileoverview Serialization utilities for proxy transport.
|
|
45
|
+
*
|
|
46
|
+
* Handles converting PP types to/from JSON for HTTP transport.
|
|
47
|
+
* These are pure functions with no side effects.
|
|
48
|
+
*
|
|
49
|
+
* @module providers/proxy/serialization
|
|
50
|
+
*/
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Convert a Message to MessageJSON format.
|
|
54
|
+
*/
|
|
55
|
+
declare function serializeMessage(m: Message): MessageJSON;
|
|
56
|
+
/**
|
|
57
|
+
* Reconstruct a Message from MessageJSON format.
|
|
58
|
+
*/
|
|
59
|
+
declare function deserializeMessage(json: MessageJSON): Message;
|
|
60
|
+
/**
|
|
61
|
+
* Serialize a Turn to JSON-transportable format.
|
|
62
|
+
*/
|
|
63
|
+
declare function serializeTurn(turn: Turn): TurnJSON;
|
|
64
|
+
/**
|
|
65
|
+
* Serialize a StreamEvent for JSON transport.
|
|
66
|
+
* Converts Uint8Array data to base64 string.
|
|
67
|
+
*/
|
|
68
|
+
declare function serializeStreamEvent(event: StreamEvent): StreamEvent;
|
|
69
|
+
/**
|
|
70
|
+
* Deserialize a StreamEvent from JSON transport.
|
|
71
|
+
* Converts base64 string data back to Uint8Array.
|
|
72
|
+
*/
|
|
73
|
+
declare function deserializeStreamEvent(event: StreamEvent): StreamEvent;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* @fileoverview H3/Nitro/Nuxt adapter for proxy server.
|
|
77
|
+
*
|
|
78
|
+
* Provides utilities for using PP proxy with H3-based servers
|
|
79
|
+
* (Nuxt, Nitro, or standalone H3).
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```typescript
|
|
83
|
+
* // Nuxt server route: server/api/ai.post.ts
|
|
84
|
+
* import { llm, anthropic } from '@providerprotocol/ai';
|
|
85
|
+
* import { parseBody } from '@providerprotocol/ai/proxy';
|
|
86
|
+
* import { h3 as h3Adapter } from '@providerprotocol/ai/proxy/server';
|
|
87
|
+
*
|
|
88
|
+
* export default defineEventHandler(async (event) => {
|
|
89
|
+
* const body = await readBody(event);
|
|
90
|
+
* const { messages, system, params } = parseBody(body);
|
|
91
|
+
* const instance = llm({ model: anthropic('claude-sonnet-4-20250514'), system });
|
|
92
|
+
*
|
|
93
|
+
* const wantsStream = getHeader(event, 'accept')?.includes('text/event-stream');
|
|
94
|
+
* if (wantsStream) {
|
|
95
|
+
* return h3Adapter.streamSSE(instance.stream(messages), event);
|
|
96
|
+
* } else {
|
|
97
|
+
* const turn = await instance.generate(messages);
|
|
98
|
+
* return h3Adapter.sendJSON(turn, event);
|
|
99
|
+
* }
|
|
100
|
+
* });
|
|
101
|
+
* ```
|
|
102
|
+
*
|
|
103
|
+
* @module providers/proxy/server/h3
|
|
104
|
+
*/
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* H3 Event interface (minimal type to avoid dependency).
|
|
108
|
+
*/
|
|
109
|
+
interface H3Event {
|
|
110
|
+
node: {
|
|
111
|
+
res: {
|
|
112
|
+
setHeader(name: string, value: string): void;
|
|
113
|
+
write(chunk: string): boolean;
|
|
114
|
+
end(): void;
|
|
115
|
+
};
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Send a Turn as JSON response.
|
|
120
|
+
*
|
|
121
|
+
* @param turn - The completed inference turn
|
|
122
|
+
* @param event - H3 event object
|
|
123
|
+
* @returns Serialized turn data
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* ```typescript
|
|
127
|
+
* const turn = await instance.generate(messages);
|
|
128
|
+
* return h3Adapter.sendJSON(turn, event);
|
|
129
|
+
* ```
|
|
130
|
+
*/
|
|
131
|
+
declare function sendJSON$2(turn: Turn, event: H3Event): unknown;
|
|
132
|
+
/**
|
|
133
|
+
* Stream a StreamResult as Server-Sent Events.
|
|
134
|
+
*
|
|
135
|
+
* @param stream - The StreamResult from instance.stream()
|
|
136
|
+
* @param event - H3 event object
|
|
137
|
+
*
|
|
138
|
+
* @example
|
|
139
|
+
* ```typescript
|
|
140
|
+
* const stream = instance.stream(messages);
|
|
141
|
+
* return h3Adapter.streamSSE(stream, event);
|
|
142
|
+
* ```
|
|
143
|
+
*/
|
|
144
|
+
declare function streamSSE$2(stream: StreamResult, event: H3Event): void;
|
|
145
|
+
/**
|
|
146
|
+
* Create a ReadableStream for H3's sendStream utility.
|
|
147
|
+
*
|
|
148
|
+
* Use this with H3's sendStream for better integration:
|
|
149
|
+
* ```typescript
|
|
150
|
+
* import { sendStream } from 'h3';
|
|
151
|
+
* return sendStream(event, h3Adapter.createSSEStream(stream));
|
|
152
|
+
* ```
|
|
153
|
+
*
|
|
154
|
+
* @param stream - The StreamResult from instance.stream()
|
|
155
|
+
* @returns A ReadableStream of SSE data
|
|
156
|
+
*/
|
|
157
|
+
declare function createSSEStream(stream: StreamResult): ReadableStream<Uint8Array>;
|
|
158
|
+
/**
|
|
159
|
+
* Send an error response.
|
|
160
|
+
*
|
|
161
|
+
* @param message - Error message
|
|
162
|
+
* @param status - HTTP status code
|
|
163
|
+
* @param event - H3 event object
|
|
164
|
+
* @returns Error object for H3 to serialize
|
|
165
|
+
*/
|
|
166
|
+
declare function sendError$2(message: string, status: number, event: H3Event): {
|
|
167
|
+
error: string;
|
|
168
|
+
statusCode: number;
|
|
169
|
+
};
|
|
170
|
+
/**
|
|
171
|
+
* H3/Nitro/Nuxt adapter utilities.
|
|
172
|
+
*/
|
|
173
|
+
declare const h3: {
|
|
174
|
+
sendJSON: typeof sendJSON$2;
|
|
175
|
+
streamSSE: typeof streamSSE$2;
|
|
176
|
+
createSSEStream: typeof createSSEStream;
|
|
177
|
+
sendError: typeof sendError$2;
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* @fileoverview Fastify adapter for proxy server.
|
|
182
|
+
*
|
|
183
|
+
* Provides utilities for using PP proxy with Fastify servers.
|
|
184
|
+
* These adapters convert PP types to Fastify-compatible responses.
|
|
185
|
+
*
|
|
186
|
+
* @example
|
|
187
|
+
* ```typescript
|
|
188
|
+
* import Fastify from 'fastify';
|
|
189
|
+
* import { llm, anthropic } from '@providerprotocol/ai';
|
|
190
|
+
* import { parseBody } from '@providerprotocol/ai/proxy';
|
|
191
|
+
* import { fastify as fastifyAdapter } from '@providerprotocol/ai/proxy/server';
|
|
192
|
+
*
|
|
193
|
+
* const app = Fastify();
|
|
194
|
+
*
|
|
195
|
+
* app.post('/api/ai', async (request, reply) => {
|
|
196
|
+
* const { messages, system, params } = parseBody(request.body);
|
|
197
|
+
* const instance = llm({ model: anthropic('claude-sonnet-4-20250514'), system });
|
|
198
|
+
*
|
|
199
|
+
* if (request.headers.accept?.includes('text/event-stream')) {
|
|
200
|
+
* return fastifyAdapter.streamSSE(instance.stream(messages), reply);
|
|
201
|
+
* } else {
|
|
202
|
+
* const turn = await instance.generate(messages);
|
|
203
|
+
* return fastifyAdapter.sendJSON(turn, reply);
|
|
204
|
+
* }
|
|
205
|
+
* });
|
|
206
|
+
* ```
|
|
207
|
+
*
|
|
208
|
+
* @module providers/proxy/server/fastify
|
|
209
|
+
*/
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Fastify Reply interface (minimal type to avoid dependency).
|
|
213
|
+
*/
|
|
214
|
+
interface FastifyReply {
|
|
215
|
+
header(name: string, value: string): FastifyReply;
|
|
216
|
+
status(code: number): FastifyReply;
|
|
217
|
+
send(payload: unknown): FastifyReply;
|
|
218
|
+
raw: {
|
|
219
|
+
write(chunk: string): boolean;
|
|
220
|
+
end(): void;
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Send a Turn as JSON response.
|
|
225
|
+
*
|
|
226
|
+
* @param turn - The completed inference turn
|
|
227
|
+
* @param reply - Fastify reply object
|
|
228
|
+
*
|
|
229
|
+
* @example
|
|
230
|
+
* ```typescript
|
|
231
|
+
* const turn = await instance.generate(messages);
|
|
232
|
+
* return fastifyAdapter.sendJSON(turn, reply);
|
|
233
|
+
* ```
|
|
234
|
+
*/
|
|
235
|
+
declare function sendJSON$1(turn: Turn, reply: FastifyReply): FastifyReply;
|
|
236
|
+
/**
|
|
237
|
+
* Stream a StreamResult as Server-Sent Events.
|
|
238
|
+
*
|
|
239
|
+
* @param stream - The StreamResult from instance.stream()
|
|
240
|
+
* @param reply - Fastify reply object
|
|
241
|
+
*
|
|
242
|
+
* @example
|
|
243
|
+
* ```typescript
|
|
244
|
+
* const stream = instance.stream(messages);
|
|
245
|
+
* return fastifyAdapter.streamSSE(stream, reply);
|
|
246
|
+
* ```
|
|
247
|
+
*/
|
|
248
|
+
declare function streamSSE$1(stream: StreamResult, reply: FastifyReply): FastifyReply;
|
|
249
|
+
/**
|
|
250
|
+
* Send an error response.
|
|
251
|
+
*
|
|
252
|
+
* @param message - Error message
|
|
253
|
+
* @param status - HTTP status code
|
|
254
|
+
* @param reply - Fastify reply object
|
|
255
|
+
*/
|
|
256
|
+
declare function sendError$1(message: string, status: number, reply: FastifyReply): FastifyReply;
|
|
257
|
+
/**
|
|
258
|
+
* Fastify adapter utilities.
|
|
259
|
+
*/
|
|
260
|
+
declare const fastify: {
|
|
261
|
+
sendJSON: typeof sendJSON$1;
|
|
262
|
+
streamSSE: typeof streamSSE$1;
|
|
263
|
+
sendError: typeof sendError$1;
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* @fileoverview Express/Connect adapter for proxy server.
|
|
268
|
+
*
|
|
269
|
+
* Provides utilities for using PP proxy with Express.js or Connect-based servers.
|
|
270
|
+
* These adapters convert PP types to Express-compatible responses.
|
|
271
|
+
*
|
|
272
|
+
* @example
|
|
273
|
+
* ```typescript
|
|
274
|
+
* import express from 'express';
|
|
275
|
+
* import { llm, anthropic } from '@providerprotocol/ai';
|
|
276
|
+
* import { parseBody, bindTools } from '@providerprotocol/ai/proxy';
|
|
277
|
+
* import { express as expressAdapter } from '@providerprotocol/ai/proxy/server';
|
|
278
|
+
*
|
|
279
|
+
* const app = express();
|
|
280
|
+
* app.use(express.json());
|
|
281
|
+
*
|
|
282
|
+
* app.post('/api/ai', async (req, res) => {
|
|
283
|
+
* const { messages, system, params } = parseBody(req.body);
|
|
284
|
+
* const instance = llm({ model: anthropic('claude-sonnet-4-20250514'), system });
|
|
285
|
+
*
|
|
286
|
+
* if (req.headers.accept?.includes('text/event-stream')) {
|
|
287
|
+
* expressAdapter.streamSSE(instance.stream(messages), res);
|
|
288
|
+
* } else {
|
|
289
|
+
* const turn = await instance.generate(messages);
|
|
290
|
+
* expressAdapter.sendJSON(turn, res);
|
|
291
|
+
* }
|
|
292
|
+
* });
|
|
293
|
+
* ```
|
|
294
|
+
*
|
|
295
|
+
* @module providers/proxy/server/express
|
|
296
|
+
*/
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Express Response interface (minimal type to avoid dependency).
|
|
300
|
+
*/
|
|
301
|
+
interface ExpressResponse {
|
|
302
|
+
setHeader(name: string, value: string): void;
|
|
303
|
+
status(code: number): ExpressResponse;
|
|
304
|
+
write(chunk: string): boolean;
|
|
305
|
+
end(): void;
|
|
306
|
+
json(body: unknown): void;
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* Send a Turn as JSON response.
|
|
310
|
+
*
|
|
311
|
+
* @param turn - The completed inference turn
|
|
312
|
+
* @param res - Express response object
|
|
313
|
+
*
|
|
314
|
+
* @example
|
|
315
|
+
* ```typescript
|
|
316
|
+
* const turn = await instance.generate(messages);
|
|
317
|
+
* expressAdapter.sendJSON(turn, res);
|
|
318
|
+
* ```
|
|
319
|
+
*/
|
|
320
|
+
declare function sendJSON(turn: Turn, res: ExpressResponse): void;
|
|
321
|
+
/**
|
|
322
|
+
* Stream a StreamResult as Server-Sent Events.
|
|
323
|
+
*
|
|
324
|
+
* @param stream - The StreamResult from instance.stream()
|
|
325
|
+
* @param res - Express response object
|
|
326
|
+
*
|
|
327
|
+
* @example
|
|
328
|
+
* ```typescript
|
|
329
|
+
* const stream = instance.stream(messages);
|
|
330
|
+
* expressAdapter.streamSSE(stream, res);
|
|
331
|
+
* ```
|
|
332
|
+
*/
|
|
333
|
+
declare function streamSSE(stream: StreamResult, res: ExpressResponse): void;
|
|
334
|
+
/**
|
|
335
|
+
* Send an error response.
|
|
336
|
+
*
|
|
337
|
+
* @param message - Error message
|
|
338
|
+
* @param status - HTTP status code
|
|
339
|
+
* @param res - Express response object
|
|
340
|
+
*/
|
|
341
|
+
declare function sendError(message: string, status: number, res: ExpressResponse): void;
|
|
342
|
+
/**
|
|
343
|
+
* Express adapter utilities.
|
|
344
|
+
*/
|
|
345
|
+
declare const express: {
|
|
346
|
+
sendJSON: typeof sendJSON;
|
|
347
|
+
streamSSE: typeof streamSSE;
|
|
348
|
+
sendError: typeof sendError;
|
|
349
|
+
};
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* @fileoverview Web API adapter for proxy server.
|
|
353
|
+
*
|
|
354
|
+
* Provides utilities for using PP proxy with Web API native frameworks
|
|
355
|
+
* (Bun, Deno, Next.js App Router, Cloudflare Workers).
|
|
356
|
+
*
|
|
357
|
+
* These utilities return standard Web API Response objects that work
|
|
358
|
+
* directly with modern runtimes.
|
|
359
|
+
*
|
|
360
|
+
* @example
|
|
361
|
+
* ```typescript
|
|
362
|
+
* import { llm, anthropic } from '@providerprotocol/ai';
|
|
363
|
+
* import { parseBody, toJSON, toSSE } from '@providerprotocol/ai/proxy';
|
|
364
|
+
*
|
|
365
|
+
* // Bun.serve / Deno.serve / Next.js App Router
|
|
366
|
+
* export async function POST(req: Request) {
|
|
367
|
+
* const { messages, system } = parseBody(await req.json());
|
|
368
|
+
* const instance = llm({ model: anthropic('claude-sonnet-4-20250514'), system });
|
|
369
|
+
*
|
|
370
|
+
* if (req.headers.get('accept')?.includes('text/event-stream')) {
|
|
371
|
+
* return toSSE(instance.stream(messages));
|
|
372
|
+
* }
|
|
373
|
+
* return toJSON(await instance.generate(messages));
|
|
374
|
+
* }
|
|
375
|
+
* ```
|
|
376
|
+
*
|
|
377
|
+
* @module providers/proxy/server/webapi
|
|
378
|
+
*/
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* Parsed request body from a proxy HTTP request.
|
|
382
|
+
* This is just the deserialized PP data from the request body.
|
|
383
|
+
*/
|
|
384
|
+
interface ParsedRequest {
|
|
385
|
+
messages: Message[];
|
|
386
|
+
system?: string | unknown[];
|
|
387
|
+
params?: Record<string, unknown>;
|
|
388
|
+
tools?: Array<{
|
|
389
|
+
name: string;
|
|
390
|
+
description: string;
|
|
391
|
+
parameters: JSONSchema;
|
|
392
|
+
metadata?: ToolMetadata;
|
|
393
|
+
}>;
|
|
394
|
+
structure?: JSONSchema;
|
|
395
|
+
}
|
|
396
|
+
/**
|
|
397
|
+
* Parse an HTTP request body into PP types.
|
|
398
|
+
*
|
|
399
|
+
* @param body - The JSON-parsed request body
|
|
400
|
+
* @returns Deserialized PP data
|
|
401
|
+
*
|
|
402
|
+
* @example
|
|
403
|
+
* ```typescript
|
|
404
|
+
* const body = await req.json();
|
|
405
|
+
* const { messages, system, params } = parseBody(body);
|
|
406
|
+
*
|
|
407
|
+
* const instance = llm({ model: anthropic('...'), system, params });
|
|
408
|
+
* const turn = await instance.generate(messages);
|
|
409
|
+
* ```
|
|
410
|
+
*/
|
|
411
|
+
declare function parseBody(body: unknown): ParsedRequest;
|
|
412
|
+
/**
|
|
413
|
+
* Create a JSON Response from a Turn.
|
|
414
|
+
*
|
|
415
|
+
* @param turn - The completed inference turn
|
|
416
|
+
* @returns HTTP Response with JSON body
|
|
417
|
+
*
|
|
418
|
+
* @example
|
|
419
|
+
* ```typescript
|
|
420
|
+
* const turn = await instance.generate(messages);
|
|
421
|
+
* return toJSON(turn);
|
|
422
|
+
* ```
|
|
423
|
+
*/
|
|
424
|
+
declare function toJSON(turn: Turn): Response;
|
|
425
|
+
/**
|
|
426
|
+
* Create an SSE Response from a StreamResult.
|
|
427
|
+
*
|
|
428
|
+
* Streams PP StreamEvents as SSE, then sends the final Turn data.
|
|
429
|
+
*
|
|
430
|
+
* @param stream - The StreamResult from instance.stream()
|
|
431
|
+
* @returns HTTP Response with SSE body
|
|
432
|
+
*
|
|
433
|
+
* @example
|
|
434
|
+
* ```typescript
|
|
435
|
+
* const stream = instance.stream(messages);
|
|
436
|
+
* return toSSE(stream);
|
|
437
|
+
* ```
|
|
438
|
+
*/
|
|
439
|
+
declare function toSSE(stream: StreamResult): Response;
|
|
440
|
+
/**
|
|
441
|
+
* Create an error Response.
|
|
442
|
+
*
|
|
443
|
+
* @param message - Error message
|
|
444
|
+
* @param status - HTTP status code (default: 500)
|
|
445
|
+
* @returns HTTP Response with error body
|
|
446
|
+
*/
|
|
447
|
+
declare function toError(message: string, status?: number): Response;
|
|
448
|
+
/**
|
|
449
|
+
* Bind tool schemas to implementation functions.
|
|
450
|
+
*
|
|
451
|
+
* Takes tool schemas from the request and binds them to your
|
|
452
|
+
* server-side implementations.
|
|
453
|
+
*
|
|
454
|
+
* @param schemas - Tool schemas from the request
|
|
455
|
+
* @param implementations - Map of tool name to implementation
|
|
456
|
+
* @returns Array of complete Tool objects
|
|
457
|
+
*
|
|
458
|
+
* @example
|
|
459
|
+
* ```typescript
|
|
460
|
+
* const { tools: schemas } = parseBody(body);
|
|
461
|
+
*
|
|
462
|
+
* const tools = bindTools(schemas, {
|
|
463
|
+
* get_weather: async ({ location }) => fetchWeather(location),
|
|
464
|
+
* search: async ({ query }) => searchDB(query),
|
|
465
|
+
* });
|
|
466
|
+
*
|
|
467
|
+
* const instance = llm({ model, tools });
|
|
468
|
+
* ```
|
|
469
|
+
*/
|
|
470
|
+
declare function bindTools(schemas: ParsedRequest['tools'], implementations: Record<string, (params: unknown) => unknown | Promise<unknown>>): Tool[];
|
|
471
|
+
/**
|
|
472
|
+
* Web API adapter utilities.
|
|
473
|
+
*
|
|
474
|
+
* For use with Bun, Deno, Next.js App Router, Cloudflare Workers,
|
|
475
|
+
* and other frameworks that support Web API Response.
|
|
476
|
+
*/
|
|
477
|
+
declare const webapi: {
|
|
478
|
+
parseBody: typeof parseBody;
|
|
479
|
+
toJSON: typeof toJSON;
|
|
480
|
+
toSSE: typeof toSSE;
|
|
481
|
+
toError: typeof toError;
|
|
482
|
+
bindTools: typeof bindTools;
|
|
483
|
+
};
|
|
484
|
+
|
|
485
|
+
/**
|
|
486
|
+
* @fileoverview Shared types for proxy server adapters.
|
|
487
|
+
*
|
|
488
|
+
* @module providers/proxy/server/types
|
|
489
|
+
*/
|
|
490
|
+
|
|
491
|
+
/**
|
|
492
|
+
* Parsed request body from a proxy HTTP request.
|
|
493
|
+
*/
|
|
494
|
+
interface ParsedBody {
|
|
495
|
+
messages: Message[];
|
|
496
|
+
system?: string | unknown[];
|
|
497
|
+
params?: Record<string, unknown>;
|
|
498
|
+
tools?: Array<{
|
|
499
|
+
name: string;
|
|
500
|
+
description: string;
|
|
501
|
+
parameters: JSONSchema;
|
|
502
|
+
metadata?: ToolMetadata;
|
|
503
|
+
}>;
|
|
504
|
+
structure?: JSONSchema;
|
|
505
|
+
}
|
|
506
|
+
/**
|
|
507
|
+
* Handler function signature for proxy endpoints.
|
|
508
|
+
* Takes parsed request data and returns either a Turn or StreamResult.
|
|
509
|
+
*/
|
|
510
|
+
type ProxyHandler = (body: ParsedBody, meta: RequestMeta) => Promise<Turn> | StreamResult | Promise<StreamResult>;
|
|
511
|
+
/**
|
|
512
|
+
* Metadata about the incoming request.
|
|
513
|
+
*/
|
|
514
|
+
interface RequestMeta {
|
|
515
|
+
/** Whether the client wants a streaming response */
|
|
516
|
+
wantsStream: boolean;
|
|
517
|
+
/** Raw headers from the request */
|
|
518
|
+
headers: Record<string, string | undefined>;
|
|
519
|
+
}
|
|
520
|
+
/**
|
|
521
|
+
* Options for adapter middleware.
|
|
522
|
+
*/
|
|
523
|
+
interface AdapterOptions {
|
|
524
|
+
/** Custom error handler */
|
|
525
|
+
onError?: (error: Error) => {
|
|
526
|
+
message: string;
|
|
527
|
+
status: number;
|
|
528
|
+
};
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
/**
|
|
532
|
+
* Server adapters namespace.
|
|
533
|
+
*
|
|
534
|
+
* Contains framework-specific adapters for Web API, Express, Fastify, and H3.
|
|
535
|
+
*/
|
|
536
|
+
declare const server: {
|
|
537
|
+
/** Web API adapter (Bun, Deno, Next.js, Workers) */
|
|
538
|
+
webapi: {
|
|
539
|
+
parseBody: typeof parseBody;
|
|
540
|
+
toJSON: typeof toJSON;
|
|
541
|
+
toSSE: typeof toSSE;
|
|
542
|
+
toError: typeof toError;
|
|
543
|
+
bindTools: typeof bindTools;
|
|
544
|
+
};
|
|
545
|
+
/** Express/Connect adapter */
|
|
546
|
+
express: {
|
|
547
|
+
sendJSON: typeof sendJSON;
|
|
548
|
+
streamSSE: typeof streamSSE;
|
|
549
|
+
sendError: typeof sendError;
|
|
550
|
+
};
|
|
551
|
+
/** Fastify adapter */
|
|
552
|
+
fastify: {
|
|
553
|
+
sendJSON: typeof sendJSON$1;
|
|
554
|
+
streamSSE: typeof streamSSE$1;
|
|
555
|
+
sendError: typeof sendError$1;
|
|
556
|
+
};
|
|
557
|
+
/** H3/Nitro/Nuxt adapter */
|
|
558
|
+
h3: {
|
|
559
|
+
sendJSON: typeof sendJSON$2;
|
|
560
|
+
streamSSE: typeof streamSSE$2;
|
|
561
|
+
createSSEStream: typeof createSSEStream;
|
|
562
|
+
sendError: typeof sendError$2;
|
|
563
|
+
};
|
|
564
|
+
};
|
|
565
|
+
|
|
566
|
+
/**
|
|
567
|
+
* Creates a proxy provider that transports PP requests over HTTP to a backend server.
|
|
568
|
+
*
|
|
569
|
+
* The proxy acts as a pure transport layer - PP types go in, PP types come out.
|
|
570
|
+
* The modelId is passed through to the backend, which decides which actual model to use.
|
|
571
|
+
*
|
|
572
|
+
* @param options - Configuration for the proxy endpoint
|
|
573
|
+
* @returns A provider that can be used with llm()
|
|
574
|
+
*
|
|
575
|
+
* @example
|
|
576
|
+
* ```typescript
|
|
577
|
+
* import { proxy } from './providers/proxy';
|
|
578
|
+
* import { llm } from './core/llm';
|
|
579
|
+
*
|
|
580
|
+
* const backend = proxy({ endpoint: '/api/ai' });
|
|
581
|
+
*
|
|
582
|
+
* const model = llm({
|
|
583
|
+
* model: backend('gpt-4o'),
|
|
584
|
+
* system: 'You are a helpful assistant.',
|
|
585
|
+
* });
|
|
586
|
+
*
|
|
587
|
+
* const turn = await model.generate('Hello!');
|
|
588
|
+
* ```
|
|
589
|
+
*/
|
|
590
|
+
declare function proxy(options: ProxyProviderOptions): Provider<ProxyRequestOptions>;
|
|
591
|
+
/**
|
|
592
|
+
* Shorthand for creating a proxy model reference with default model ID.
|
|
593
|
+
*
|
|
594
|
+
* Creates a proxy provider and immediately returns a model reference using
|
|
595
|
+
* 'default' as the model identifier. Useful for simple single-endpoint setups.
|
|
596
|
+
*
|
|
597
|
+
* @param endpoint - The URL to proxy requests to
|
|
598
|
+
* @returns A model reference for use with llm()
|
|
599
|
+
*
|
|
600
|
+
* @example
|
|
601
|
+
* ```typescript
|
|
602
|
+
* import { proxyModel } from './providers/proxy';
|
|
603
|
+
* import { llm } from './core/llm';
|
|
604
|
+
*
|
|
605
|
+
* const model = llm({ model: proxyModel('/api/ai') });
|
|
606
|
+
* const turn = await model.generate('Hello!');
|
|
607
|
+
* ```
|
|
608
|
+
*/
|
|
609
|
+
declare function proxyModel(endpoint: string): ModelReference<ProxyRequestOptions>;
|
|
610
|
+
|
|
611
|
+
export { type AdapterOptions, type ParsedBody, type ParsedRequest, type ProxyHandler, type ProxyLLMParams, type ProxyProviderOptions, type ProxyRequestOptions, type RequestMeta, TurnJSON, bindTools, deserializeMessage, deserializeStreamEvent, express, fastify, h3, parseBody, proxy, proxyModel, serializeMessage, serializeStreamEvent, serializeTurn, server, toError, toJSON, toSSE, webapi };
|