@gram-ai/elements 1.16.3 → 1.16.5
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/contexts/ElementsProvider.d.ts +1 -1
- package/dist/elements.cjs +120 -33
- package/dist/elements.css +1 -1
- package/dist/elements.js +28646 -8262
- package/dist/hooks/useMCPTools.d.ts +11 -0
- package/dist/hooks/useSession.d.ts +13 -0
- package/dist/{index-SAJg5Cz7.js → index-Bjdy_BPv.js} +378 -385
- package/dist/{index-C6vUoNWt.cjs → index-CqzaiyM-.cjs} +2 -2
- package/dist/lib/tools.d.ts +6 -0
- package/dist/lib/utils.d.ts +1 -1
- package/dist/plugins.cjs +1 -1
- package/dist/plugins.js +1 -1
- package/dist/server.cjs +1 -29
- package/dist/server.d.ts +32 -4
- package/dist/server.js +21 -4069
- package/dist/types/index.d.ts +23 -11
- package/package.json +3 -1
- package/dist/index-BqoFmyxX.cjs +0 -60
- package/dist/index-CVZt-xeH.js +0 -14913
package/dist/server.js
CHANGED
|
@@ -1,4077 +1,29 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
{
|
|
5
|
-
...n.description ? { description: n.description } : void 0,
|
|
6
|
-
inputSchema: Rn(n.parameters)
|
|
7
|
-
}
|
|
8
|
-
]));
|
|
9
|
-
var Un = ({
|
|
10
|
-
prefix: e,
|
|
11
|
-
size: t = 16,
|
|
12
|
-
alphabet: n = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
|
13
|
-
separator: r = "-"
|
|
14
|
-
} = {}) => {
|
|
15
|
-
const o = () => {
|
|
16
|
-
const a = n.length, i = new Array(t);
|
|
17
|
-
for (let c = 0; c < t; c++)
|
|
18
|
-
i[c] = n[Math.random() * a | 0];
|
|
19
|
-
return i.join("");
|
|
20
|
-
};
|
|
21
|
-
if (e == null)
|
|
22
|
-
return o;
|
|
23
|
-
if (n.includes(r))
|
|
24
|
-
throw new En({
|
|
25
|
-
argument: "separator",
|
|
26
|
-
message: `The separator "${r}" must not be part of the alphabet "${n}".`
|
|
27
|
-
});
|
|
28
|
-
return () => `${e}${r}${o()}`;
|
|
29
|
-
};
|
|
30
|
-
Un();
|
|
31
|
-
function _t(e = globalThis) {
|
|
32
|
-
var t, n, r;
|
|
33
|
-
return e.window ? "runtime/browser" : (t = e.navigator) != null && t.userAgent ? `runtime/${e.navigator.userAgent.toLowerCase()}` : (r = (n = e.process) == null ? void 0 : n.versions) != null && r.node ? `runtime/node.js/${e.process.version.substring(0)}` : e.EdgeRuntime ? "runtime/vercel-edge" : "runtime/unknown";
|
|
34
|
-
}
|
|
35
|
-
function Mn(e) {
|
|
36
|
-
if (e == null)
|
|
37
|
-
return {};
|
|
38
|
-
const t = {};
|
|
39
|
-
if (e instanceof Headers)
|
|
40
|
-
e.forEach((n, r) => {
|
|
41
|
-
t[r.toLowerCase()] = n;
|
|
42
|
-
});
|
|
43
|
-
else {
|
|
44
|
-
Array.isArray(e) || (e = Object.entries(e));
|
|
45
|
-
for (const [n, r] of e)
|
|
46
|
-
r != null && (t[n.toLowerCase()] = r);
|
|
47
|
-
}
|
|
48
|
-
return t;
|
|
49
|
-
}
|
|
50
|
-
function vt(e, ...t) {
|
|
51
|
-
const n = new Headers(Mn(e)), r = n.get("user-agent") || "";
|
|
52
|
-
return n.set(
|
|
53
|
-
"user-agent",
|
|
54
|
-
[r, ...t].filter(Boolean).join(" ")
|
|
55
|
-
), Object.fromEntries(n.entries());
|
|
56
|
-
}
|
|
57
|
-
var Nn = Symbol.for("vercel.ai.validator");
|
|
58
|
-
function jn(e) {
|
|
59
|
-
return { ...e, type: "dynamic" };
|
|
60
|
-
}
|
|
61
|
-
new Set(
|
|
62
|
-
"ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvxyz0123456789"
|
|
63
|
-
);
|
|
64
|
-
var Dn = Symbol.for("vercel.ai.schema");
|
|
65
|
-
function Hn(e, {
|
|
66
|
-
validate: t
|
|
67
|
-
} = {}) {
|
|
68
|
-
return {
|
|
69
|
-
[Dn]: !0,
|
|
70
|
-
_type: void 0,
|
|
71
|
-
// should never be used directly
|
|
72
|
-
[Nn]: !0,
|
|
73
|
-
get jsonSchema() {
|
|
74
|
-
return typeof e == "function" && (e = e()), e;
|
|
75
|
-
},
|
|
76
|
-
validate: t
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
let We;
|
|
80
|
-
We = globalThis.crypto;
|
|
81
|
-
async function Ln(e) {
|
|
82
|
-
return (await We).getRandomValues(new Uint8Array(e));
|
|
83
|
-
}
|
|
84
|
-
async function qn(e) {
|
|
85
|
-
const t = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~";
|
|
86
|
-
let n = "";
|
|
87
|
-
const r = await Ln(e);
|
|
88
|
-
for (let o = 0; o < e; o++) {
|
|
89
|
-
const a = r[o] % t.length;
|
|
90
|
-
n += t[a];
|
|
91
|
-
}
|
|
92
|
-
return n;
|
|
93
|
-
}
|
|
94
|
-
async function Vn(e) {
|
|
95
|
-
return await qn(e);
|
|
96
|
-
}
|
|
97
|
-
async function Bn(e) {
|
|
98
|
-
const t = await (await We).subtle.digest("SHA-256", new TextEncoder().encode(e));
|
|
99
|
-
return btoa(String.fromCharCode(...new Uint8Array(t))).replace(/\//g, "_").replace(/\+/g, "-").replace(/=/g, "");
|
|
100
|
-
}
|
|
101
|
-
async function Jn(e) {
|
|
102
|
-
if (e || (e = 43), e < 43 || e > 128)
|
|
103
|
-
throw `Expected a length between 43 and 128. Received ${e}.`;
|
|
104
|
-
const t = await Vn(e), n = await Bn(t);
|
|
105
|
-
return {
|
|
106
|
-
code_verifier: t,
|
|
107
|
-
code_challenge: n
|
|
108
|
-
};
|
|
109
|
-
}
|
|
110
|
-
var zn = "AI_MCPClientError", yt = `vercel.ai.error.${zn}`, Gn = Symbol.for(yt), rt, ot, O = class extends (ot = be, rt = Gn, ot) {
|
|
111
|
-
constructor({
|
|
112
|
-
name: e = "MCPClientError",
|
|
113
|
-
message: t,
|
|
114
|
-
cause: n,
|
|
115
|
-
data: r,
|
|
116
|
-
code: o
|
|
117
|
-
}) {
|
|
118
|
-
super({ name: e, message: t, cause: n }), this[rt] = !0, this.data = r, this.code = o;
|
|
119
|
-
}
|
|
120
|
-
static isInstance(e) {
|
|
121
|
-
return be.hasMarker(e, yt);
|
|
122
|
-
}
|
|
123
|
-
}, ue = "2025-06-18", Wn = [
|
|
124
|
-
ue,
|
|
125
|
-
"2025-03-26",
|
|
126
|
-
"2024-11-05"
|
|
127
|
-
], Fn = le({
|
|
128
|
-
name: s(),
|
|
129
|
-
version: s()
|
|
130
|
-
}), Pe = le({
|
|
131
|
-
_meta: E(d({}).loose())
|
|
132
|
-
}), Y = Pe, wt = d({
|
|
133
|
-
method: s(),
|
|
134
|
-
params: E(Pe)
|
|
135
|
-
}), bt = d({
|
|
136
|
-
applyDefaults: E(X())
|
|
137
|
-
}).loose(), Kn = le({
|
|
138
|
-
experimental: E(d({}).loose()),
|
|
139
|
-
logging: E(d({}).loose()),
|
|
140
|
-
prompts: E(
|
|
141
|
-
le({
|
|
142
|
-
listChanged: E(X())
|
|
143
|
-
})
|
|
144
|
-
),
|
|
145
|
-
resources: E(
|
|
146
|
-
le({
|
|
147
|
-
subscribe: E(X()),
|
|
148
|
-
listChanged: E(X())
|
|
149
|
-
})
|
|
150
|
-
),
|
|
151
|
-
tools: E(
|
|
152
|
-
le({
|
|
153
|
-
listChanged: E(X())
|
|
154
|
-
})
|
|
155
|
-
),
|
|
156
|
-
elicitation: E(bt)
|
|
157
|
-
});
|
|
158
|
-
d({
|
|
159
|
-
elicitation: E(bt)
|
|
160
|
-
}).loose();
|
|
161
|
-
var Zn = Y.extend({
|
|
162
|
-
protocolVersion: s(),
|
|
163
|
-
capabilities: Kn,
|
|
164
|
-
serverInfo: Fn,
|
|
165
|
-
instructions: E(s())
|
|
166
|
-
}), Fe = Y.extend({
|
|
167
|
-
nextCursor: E(s())
|
|
168
|
-
}), Xn = d({
|
|
169
|
-
name: s(),
|
|
170
|
-
description: E(s()),
|
|
171
|
-
inputSchema: d({
|
|
172
|
-
type: x("object"),
|
|
173
|
-
properties: E(d({}).loose())
|
|
174
|
-
}).loose()
|
|
175
|
-
}).loose(), Yn = Fe.extend({
|
|
176
|
-
tools: y(Xn)
|
|
177
|
-
}), kt = d({
|
|
178
|
-
type: x("text"),
|
|
179
|
-
text: s()
|
|
180
|
-
}).loose(), Tt = d({
|
|
181
|
-
type: x("image"),
|
|
182
|
-
data: gt(),
|
|
183
|
-
mimeType: s()
|
|
184
|
-
}).loose(), Qn = d({
|
|
185
|
-
uri: s(),
|
|
186
|
-
name: s(),
|
|
187
|
-
title: E(s()),
|
|
188
|
-
description: E(s()),
|
|
189
|
-
mimeType: E(s()),
|
|
190
|
-
size: E(k())
|
|
191
|
-
}).loose(), er = Fe.extend({
|
|
192
|
-
resources: y(Qn)
|
|
193
|
-
}), St = d({
|
|
194
|
-
/**
|
|
195
|
-
* The URI of this resource.
|
|
196
|
-
*/
|
|
197
|
-
uri: s(),
|
|
198
|
-
/**
|
|
199
|
-
* Optional display name of the resource content.
|
|
200
|
-
*/
|
|
201
|
-
name: E(s()),
|
|
202
|
-
/**
|
|
203
|
-
* Optional human readable title.
|
|
204
|
-
*/
|
|
205
|
-
title: E(s()),
|
|
206
|
-
/**
|
|
207
|
-
* The MIME type of this resource, if known.
|
|
208
|
-
*/
|
|
209
|
-
mimeType: E(s())
|
|
210
|
-
}).loose(), Rt = St.extend({
|
|
211
|
-
text: s()
|
|
212
|
-
}), Et = St.extend({
|
|
213
|
-
blob: gt()
|
|
214
|
-
}), Ct = d({
|
|
215
|
-
type: x("resource"),
|
|
216
|
-
resource: j([Rt, Et])
|
|
217
|
-
}).loose(), tr = Y.extend({
|
|
218
|
-
content: y(
|
|
219
|
-
j([kt, Tt, Ct])
|
|
220
|
-
),
|
|
221
|
-
isError: X().default(!1).optional()
|
|
222
|
-
}).or(
|
|
223
|
-
Y.extend({
|
|
224
|
-
toolResult: oe()
|
|
225
|
-
})
|
|
226
|
-
), nr = d({
|
|
227
|
-
uriTemplate: s(),
|
|
228
|
-
name: s(),
|
|
229
|
-
title: E(s()),
|
|
230
|
-
description: E(s()),
|
|
231
|
-
mimeType: E(s())
|
|
232
|
-
}).loose(), rr = Y.extend({
|
|
233
|
-
resourceTemplates: y(nr)
|
|
234
|
-
}), or = Y.extend({
|
|
235
|
-
contents: y(
|
|
236
|
-
j([Rt, Et])
|
|
237
|
-
)
|
|
238
|
-
}), sr = d({
|
|
239
|
-
name: s(),
|
|
240
|
-
description: E(s()),
|
|
241
|
-
required: E(X())
|
|
242
|
-
}).loose(), ar = d({
|
|
243
|
-
name: s(),
|
|
244
|
-
title: E(s()),
|
|
245
|
-
description: E(s()),
|
|
246
|
-
arguments: E(y(sr))
|
|
247
|
-
}).loose(), ir = Fe.extend({
|
|
248
|
-
prompts: y(ar)
|
|
249
|
-
}), lr = d({
|
|
250
|
-
role: j([x("user"), x("assistant")]),
|
|
251
|
-
content: j([
|
|
252
|
-
kt,
|
|
253
|
-
Tt,
|
|
254
|
-
Ct
|
|
255
|
-
])
|
|
256
|
-
}).loose(), cr = Y.extend({
|
|
257
|
-
description: E(s()),
|
|
258
|
-
messages: y(lr)
|
|
259
|
-
}), ur = Pe.extend({
|
|
260
|
-
message: s(),
|
|
261
|
-
requestedSchema: oe()
|
|
262
|
-
}), st = wt.extend({
|
|
263
|
-
method: x("elicitation/create"),
|
|
264
|
-
params: ur
|
|
265
|
-
}), dr = Y.extend({
|
|
266
|
-
action: j([
|
|
267
|
-
x("accept"),
|
|
268
|
-
x("decline"),
|
|
269
|
-
x("cancel")
|
|
270
|
-
]),
|
|
271
|
-
content: E(ze(s(), oe()))
|
|
272
|
-
}), Ae = "2.0", pr = d({
|
|
273
|
-
jsonrpc: x(Ae),
|
|
274
|
-
id: j([s(), k().int()])
|
|
275
|
-
}).merge(wt).strict(), hr = d({
|
|
276
|
-
jsonrpc: x(Ae),
|
|
277
|
-
id: j([s(), k().int()]),
|
|
278
|
-
result: Y
|
|
279
|
-
}).strict(), mr = d({
|
|
280
|
-
jsonrpc: x(Ae),
|
|
281
|
-
id: j([s(), k().int()]),
|
|
282
|
-
error: d({
|
|
283
|
-
code: k().int(),
|
|
284
|
-
message: s(),
|
|
285
|
-
data: E(oe())
|
|
286
|
-
})
|
|
287
|
-
}).strict(), fr = d({
|
|
288
|
-
jsonrpc: x(Ae)
|
|
289
|
-
}).merge(
|
|
290
|
-
d({
|
|
291
|
-
method: s(),
|
|
292
|
-
params: E(Pe)
|
|
293
|
-
})
|
|
294
|
-
).strict(), pe = j([
|
|
295
|
-
pr,
|
|
296
|
-
fr,
|
|
297
|
-
hr,
|
|
298
|
-
mr
|
|
299
|
-
]), xt = typeof __PACKAGE_VERSION__ < "u" ? __PACKAGE_VERSION__ : "0.0.0-test", It = d({
|
|
300
|
-
access_token: s(),
|
|
301
|
-
id_token: s().optional(),
|
|
302
|
-
// Optional for OAuth 2.1, but necessary in OpenID Connect
|
|
303
|
-
token_type: s(),
|
|
304
|
-
expires_in: k().optional(),
|
|
305
|
-
scope: s().optional(),
|
|
306
|
-
refresh_token: s().optional()
|
|
307
|
-
}).strip(), B = s().url().superRefine((e, t) => {
|
|
308
|
-
if (!URL.canParse(e))
|
|
309
|
-
return t.addIssue({
|
|
310
|
-
code: Cn.custom,
|
|
311
|
-
message: "URL must be parseable",
|
|
312
|
-
fatal: !0
|
|
313
|
-
}), xn;
|
|
314
|
-
}).refine(
|
|
315
|
-
(e) => {
|
|
316
|
-
const t = new URL(e);
|
|
317
|
-
return t.protocol !== "javascript:" && t.protocol !== "data:" && t.protocol !== "vbscript:";
|
|
318
|
-
},
|
|
319
|
-
{ message: "URL cannot use javascript:, data:, or vbscript: scheme" }
|
|
320
|
-
), gr = d({
|
|
321
|
-
resource: s().url(),
|
|
322
|
-
authorization_servers: y(B).optional(),
|
|
323
|
-
jwks_uri: s().url().optional(),
|
|
324
|
-
scopes_supported: y(s()).optional(),
|
|
325
|
-
bearer_methods_supported: y(s()).optional(),
|
|
326
|
-
resource_signing_alg_values_supported: y(s()).optional(),
|
|
327
|
-
resource_name: s().optional(),
|
|
328
|
-
resource_documentation: s().optional(),
|
|
329
|
-
resource_policy_uri: s().url().optional(),
|
|
330
|
-
resource_tos_uri: s().url().optional(),
|
|
331
|
-
tls_client_certificate_bound_access_tokens: X().optional(),
|
|
332
|
-
authorization_details_types_supported: y(s()).optional(),
|
|
333
|
-
dpop_signing_alg_values_supported: y(s()).optional(),
|
|
334
|
-
dpop_bound_access_tokens_required: X().optional()
|
|
335
|
-
}).passthrough(), Ot = d({
|
|
336
|
-
issuer: s(),
|
|
337
|
-
authorization_endpoint: B,
|
|
338
|
-
token_endpoint: B,
|
|
339
|
-
registration_endpoint: B.optional(),
|
|
340
|
-
scopes_supported: y(s()).optional(),
|
|
341
|
-
response_types_supported: y(s()),
|
|
342
|
-
grant_types_supported: y(s()).optional(),
|
|
343
|
-
code_challenge_methods_supported: y(s()),
|
|
344
|
-
token_endpoint_auth_methods_supported: y(s()).optional(),
|
|
345
|
-
token_endpoint_auth_signing_alg_values_supported: y(s()).optional()
|
|
346
|
-
}).passthrough(), _r = d({
|
|
347
|
-
issuer: s(),
|
|
348
|
-
authorization_endpoint: B,
|
|
349
|
-
token_endpoint: B,
|
|
350
|
-
userinfo_endpoint: B.optional(),
|
|
351
|
-
jwks_uri: B,
|
|
352
|
-
registration_endpoint: B.optional(),
|
|
353
|
-
scopes_supported: y(s()).optional(),
|
|
354
|
-
response_types_supported: y(s()),
|
|
355
|
-
grant_types_supported: y(s()).optional(),
|
|
356
|
-
subject_types_supported: y(s()),
|
|
357
|
-
id_token_signing_alg_values_supported: y(s()),
|
|
358
|
-
claims_supported: y(s()).optional(),
|
|
359
|
-
token_endpoint_auth_methods_supported: y(s()).optional()
|
|
360
|
-
}).passthrough(), vr = _r.merge(
|
|
361
|
-
Ot.pick({
|
|
362
|
-
code_challenge_methods_supported: !0
|
|
363
|
-
})
|
|
364
|
-
), yr = d({
|
|
365
|
-
client_id: s(),
|
|
366
|
-
client_secret: s().optional(),
|
|
367
|
-
client_id_issued_at: k().optional(),
|
|
368
|
-
client_secret_expires_at: k().optional()
|
|
369
|
-
}).strip(), wr = d({
|
|
370
|
-
redirect_uris: y(B),
|
|
371
|
-
token_endpoint_auth_method: s().optional(),
|
|
372
|
-
grant_types: y(s()).optional(),
|
|
373
|
-
response_types: y(s()).optional(),
|
|
374
|
-
client_name: s().optional(),
|
|
375
|
-
client_uri: B.optional(),
|
|
376
|
-
logo_uri: B.optional(),
|
|
377
|
-
scope: s().optional(),
|
|
378
|
-
contacts: y(s()).optional(),
|
|
379
|
-
tos_uri: B.optional(),
|
|
380
|
-
policy_uri: s().optional(),
|
|
381
|
-
jwks_uri: B.optional(),
|
|
382
|
-
jwks: K().optional(),
|
|
383
|
-
software_id: s().optional(),
|
|
384
|
-
software_version: s().optional(),
|
|
385
|
-
software_statement: s().optional()
|
|
386
|
-
}).strip(), br = d({
|
|
387
|
-
error: s(),
|
|
388
|
-
error_description: s().optional(),
|
|
389
|
-
error_uri: s().optional()
|
|
390
|
-
}), kr = wr.merge(
|
|
391
|
-
yr
|
|
392
|
-
), Tr = "AI_MCPClientOAuthError", Pt = `vercel.ai.error.${Tr}`, Sr = Symbol.for(Pt), at, it, _e = class extends (it = be, at = Sr, it) {
|
|
393
|
-
constructor({
|
|
394
|
-
name: e = "MCPClientOAuthError",
|
|
395
|
-
message: t,
|
|
396
|
-
cause: n
|
|
397
|
-
}) {
|
|
398
|
-
super({ name: e, message: t, cause: n }), this[at] = !0;
|
|
399
|
-
}
|
|
400
|
-
static isInstance(e) {
|
|
401
|
-
return be.hasMarker(e, Pt);
|
|
402
|
-
}
|
|
403
|
-
}, ce = class extends _e {
|
|
404
|
-
};
|
|
405
|
-
ce.errorCode = "server_error";
|
|
406
|
-
var ke = class extends _e {
|
|
407
|
-
};
|
|
408
|
-
ke.errorCode = "invalid_client";
|
|
409
|
-
var Te = class extends _e {
|
|
410
|
-
};
|
|
411
|
-
Te.errorCode = "invalid_grant";
|
|
412
|
-
var Se = class extends _e {
|
|
413
|
-
};
|
|
414
|
-
Se.errorCode = "unauthorized_client";
|
|
415
|
-
var Rr = {
|
|
416
|
-
[ce.errorCode]: ce,
|
|
417
|
-
[ke.errorCode]: ke,
|
|
418
|
-
[Te.errorCode]: Te,
|
|
419
|
-
[Se.errorCode]: Se
|
|
420
|
-
};
|
|
421
|
-
function Er(e) {
|
|
422
|
-
const t = typeof e == "string" ? new URL(e) : new URL(e.href);
|
|
423
|
-
return t.hash = "", t;
|
|
424
|
-
}
|
|
425
|
-
function Cr({
|
|
426
|
-
requestedResource: e,
|
|
427
|
-
configuredResource: t
|
|
428
|
-
}) {
|
|
429
|
-
const n = typeof e == "string" ? new URL(e) : new URL(e.href), r = typeof t == "string" ? new URL(t) : new URL(t.href);
|
|
430
|
-
if (n.origin !== r.origin || n.pathname.length < r.pathname.length)
|
|
431
|
-
return !1;
|
|
432
|
-
const o = n.pathname.endsWith("/") ? n.pathname : n.pathname + "/", a = r.pathname.endsWith("/") ? r.pathname : r.pathname + "/";
|
|
433
|
-
return o.startsWith(a);
|
|
434
|
-
}
|
|
435
|
-
var Re = class extends Error {
|
|
436
|
-
constructor(e = "Unauthorized") {
|
|
437
|
-
super(e), this.name = "UnauthorizedError";
|
|
438
|
-
}
|
|
439
|
-
};
|
|
440
|
-
function Ee(e) {
|
|
441
|
-
var t;
|
|
442
|
-
const n = (t = e.headers.get("www-authenticate")) != null ? t : e.headers.get("WWW-Authenticate");
|
|
443
|
-
if (!n)
|
|
444
|
-
return;
|
|
445
|
-
const [r, o] = n.split(" ");
|
|
446
|
-
if (r.toLowerCase() !== "bearer" || !o)
|
|
447
|
-
return;
|
|
448
|
-
const a = /resource_metadata="([^"]*)"/, i = n.match(a);
|
|
449
|
-
if (i)
|
|
450
|
-
try {
|
|
451
|
-
return new URL(i[1]);
|
|
452
|
-
} catch {
|
|
453
|
-
return;
|
|
454
|
-
}
|
|
455
|
-
}
|
|
456
|
-
function xr(e, t = "", n = {}) {
|
|
457
|
-
return t.endsWith("/") && (t = t.slice(0, -1)), n.prependPathname ? `${t}/.well-known/${e}` : `/.well-known/${e}${t}`;
|
|
458
|
-
}
|
|
459
|
-
async function Ke(e, t, n = fetch) {
|
|
460
|
-
try {
|
|
461
|
-
return await n(e, { headers: t });
|
|
462
|
-
} catch (r) {
|
|
463
|
-
if (r instanceof TypeError)
|
|
464
|
-
return t ? Ke(e, void 0, n) : void 0;
|
|
465
|
-
throw r;
|
|
466
|
-
}
|
|
467
|
-
}
|
|
468
|
-
async function lt(e, t, n = fetch) {
|
|
469
|
-
return await Ke(e, {
|
|
470
|
-
"MCP-Protocol-Version": t
|
|
471
|
-
}, n);
|
|
472
|
-
}
|
|
473
|
-
function Ir(e, t) {
|
|
474
|
-
return !e || e.status >= 400 && e.status < 500 && t !== "/";
|
|
475
|
-
}
|
|
476
|
-
async function Or(e, t, n, r) {
|
|
477
|
-
var o, a;
|
|
478
|
-
const i = new URL(e), c = (o = r?.protocolVersion) != null ? o : ue;
|
|
479
|
-
let l;
|
|
480
|
-
if (r?.metadataUrl)
|
|
481
|
-
l = new URL(r.metadataUrl);
|
|
482
|
-
else {
|
|
483
|
-
const h = xr(t, i.pathname);
|
|
484
|
-
l = new URL(h, (a = r?.metadataServerUrl) != null ? a : i), l.search = i.search;
|
|
485
|
-
}
|
|
486
|
-
let u = await lt(l, c, n);
|
|
487
|
-
if (!r?.metadataUrl && Ir(u, i.pathname)) {
|
|
488
|
-
const h = new URL(`/.well-known/${t}`, i);
|
|
489
|
-
u = await lt(h, c, n);
|
|
490
|
-
}
|
|
491
|
-
return u;
|
|
492
|
-
}
|
|
493
|
-
async function Pr(e, t, n = fetch) {
|
|
494
|
-
const r = await Or(
|
|
495
|
-
e,
|
|
496
|
-
"oauth-protected-resource",
|
|
497
|
-
n,
|
|
498
|
-
{
|
|
499
|
-
protocolVersion: t?.protocolVersion,
|
|
500
|
-
metadataUrl: t?.resourceMetadataUrl
|
|
501
|
-
}
|
|
502
|
-
);
|
|
503
|
-
if (!r || r.status === 404)
|
|
504
|
-
throw new Error(
|
|
505
|
-
"Resource server does not implement OAuth 2.0 Protected Resource Metadata."
|
|
506
|
-
);
|
|
507
|
-
if (!r.ok)
|
|
508
|
-
throw new Error(
|
|
509
|
-
`HTTP ${r.status} trying to load well-known OAuth protected resource metadata.`
|
|
510
|
-
);
|
|
511
|
-
return gr.parse(await r.json());
|
|
512
|
-
}
|
|
513
|
-
function Ar(e) {
|
|
514
|
-
const t = typeof e == "string" ? new URL(e) : e, n = t.pathname !== "/", r = [];
|
|
515
|
-
if (!n)
|
|
516
|
-
return r.push({
|
|
517
|
-
url: new URL("/.well-known/oauth-authorization-server", t.origin),
|
|
518
|
-
type: "oauth"
|
|
519
|
-
}), r.push({
|
|
520
|
-
url: new URL("/.well-known/openid-configuration", t.origin),
|
|
521
|
-
type: "oidc"
|
|
522
|
-
}), r;
|
|
523
|
-
let o = t.pathname;
|
|
524
|
-
return o.endsWith("/") && (o = o.slice(0, -1)), r.push({
|
|
525
|
-
url: new URL(
|
|
526
|
-
`/.well-known/oauth-authorization-server${o}`,
|
|
527
|
-
t.origin
|
|
528
|
-
),
|
|
529
|
-
type: "oauth"
|
|
530
|
-
}), r.push({
|
|
531
|
-
url: new URL("/.well-known/oauth-authorization-server", t.origin),
|
|
532
|
-
type: "oauth"
|
|
533
|
-
}), r.push({
|
|
534
|
-
url: new URL(`/.well-known/openid-configuration${o}`, t.origin),
|
|
535
|
-
type: "oidc"
|
|
536
|
-
}), r.push({
|
|
537
|
-
url: new URL(`${o}/.well-known/openid-configuration`, t.origin),
|
|
538
|
-
type: "oidc"
|
|
539
|
-
}), r;
|
|
540
|
-
}
|
|
541
|
-
async function $r(e, {
|
|
542
|
-
fetchFn: t = fetch,
|
|
543
|
-
protocolVersion: n = ue
|
|
544
|
-
} = {}) {
|
|
545
|
-
var r;
|
|
546
|
-
const o = { "MCP-Protocol-Version": n }, a = Ar(e);
|
|
547
|
-
for (const { url: i, type: c } of a) {
|
|
548
|
-
const l = await Ke(i, o, t);
|
|
549
|
-
if (l) {
|
|
550
|
-
if (!l.ok) {
|
|
551
|
-
if (l.status >= 400 && l.status < 500)
|
|
552
|
-
continue;
|
|
553
|
-
throw new Error(
|
|
554
|
-
`HTTP ${l.status} trying to load ${c === "oauth" ? "OAuth" : "OpenID provider"} metadata from ${i}`
|
|
555
|
-
);
|
|
556
|
-
}
|
|
557
|
-
if (c === "oauth")
|
|
558
|
-
return Ot.parse(await l.json());
|
|
559
|
-
{
|
|
560
|
-
const u = vr.parse(
|
|
561
|
-
await l.json()
|
|
562
|
-
);
|
|
563
|
-
if (!((r = u.code_challenge_methods_supported) != null && r.includes("S256")))
|
|
564
|
-
throw new Error(
|
|
565
|
-
`Incompatible OIDC provider at ${i}: does not support S256 code challenge method required by MCP specification`
|
|
566
|
-
);
|
|
567
|
-
return u;
|
|
568
|
-
}
|
|
569
|
-
}
|
|
570
|
-
}
|
|
571
|
-
}
|
|
572
|
-
async function Ur(e, {
|
|
573
|
-
metadata: t,
|
|
574
|
-
clientInformation: n,
|
|
575
|
-
redirectUrl: r,
|
|
576
|
-
scope: o,
|
|
577
|
-
state: a,
|
|
578
|
-
resource: i
|
|
579
|
-
}) {
|
|
580
|
-
const c = "code", l = "S256";
|
|
581
|
-
let u;
|
|
582
|
-
if (t) {
|
|
583
|
-
if (u = new URL(t.authorization_endpoint), !t.response_types_supported.includes(c))
|
|
584
|
-
throw new Error(
|
|
585
|
-
`Incompatible auth server: does not support response type ${c}`
|
|
586
|
-
);
|
|
587
|
-
if (!t.code_challenge_methods_supported || !t.code_challenge_methods_supported.includes(l))
|
|
588
|
-
throw new Error(
|
|
589
|
-
`Incompatible auth server: does not support code challenge method ${l}`
|
|
590
|
-
);
|
|
591
|
-
} else
|
|
592
|
-
u = new URL("/authorize", e);
|
|
593
|
-
const h = await Jn(), g = h.code_verifier, f = h.code_challenge;
|
|
594
|
-
return u.searchParams.set("response_type", c), u.searchParams.set("client_id", n.client_id), u.searchParams.set("code_challenge", f), u.searchParams.set(
|
|
595
|
-
"code_challenge_method",
|
|
596
|
-
l
|
|
597
|
-
), u.searchParams.set("redirect_uri", String(r)), a && u.searchParams.set("state", a), o && u.searchParams.set("scope", o), o?.includes("offline_access") && u.searchParams.append("prompt", "consent"), i && u.searchParams.set("resource", i.href), { authorizationUrl: u, codeVerifier: g };
|
|
598
|
-
}
|
|
599
|
-
function At(e, t) {
|
|
600
|
-
const n = e.client_secret !== void 0;
|
|
601
|
-
return t.length === 0 ? n ? "client_secret_post" : "none" : n && t.includes("client_secret_basic") ? "client_secret_basic" : n && t.includes("client_secret_post") ? "client_secret_post" : t.includes("none") ? "none" : n ? "client_secret_post" : "none";
|
|
602
|
-
}
|
|
603
|
-
function $t(e, t, n, r) {
|
|
604
|
-
const { client_id: o, client_secret: a } = t;
|
|
605
|
-
switch (e) {
|
|
606
|
-
case "client_secret_basic":
|
|
607
|
-
Mr(o, a, n);
|
|
608
|
-
return;
|
|
609
|
-
case "client_secret_post":
|
|
610
|
-
Nr(o, a, r);
|
|
611
|
-
return;
|
|
612
|
-
case "none":
|
|
613
|
-
jr(o, r);
|
|
614
|
-
return;
|
|
615
|
-
default:
|
|
616
|
-
throw new Error(`Unsupported client authentication method: ${e}`);
|
|
617
|
-
}
|
|
618
|
-
}
|
|
619
|
-
function Mr(e, t, n) {
|
|
620
|
-
if (!t)
|
|
621
|
-
throw new Error(
|
|
622
|
-
"client_secret_basic authentication requires a client_secret"
|
|
623
|
-
);
|
|
624
|
-
const r = btoa(`${e}:${t}`);
|
|
625
|
-
n.set("Authorization", `Basic ${r}`);
|
|
626
|
-
}
|
|
627
|
-
function Nr(e, t, n) {
|
|
628
|
-
n.set("client_id", e), t && n.set("client_secret", t);
|
|
629
|
-
}
|
|
630
|
-
function jr(e, t) {
|
|
631
|
-
t.set("client_id", e);
|
|
632
|
-
}
|
|
633
|
-
async function Ze(e) {
|
|
634
|
-
const t = e instanceof Response ? e.status : void 0, n = e instanceof Response ? await e.text() : e;
|
|
635
|
-
try {
|
|
636
|
-
const r = br.parse(JSON.parse(n)), { error: o, error_description: a, error_uri: i } = r, c = Rr[o] || ce;
|
|
637
|
-
return new c({
|
|
638
|
-
message: a || "",
|
|
639
|
-
cause: i
|
|
640
|
-
});
|
|
641
|
-
} catch (r) {
|
|
642
|
-
const o = `${t ? `HTTP ${t}: ` : ""}Invalid OAuth error response: ${r}. Raw body: ${n}`;
|
|
643
|
-
return new ce({ message: o });
|
|
644
|
-
}
|
|
645
|
-
}
|
|
646
|
-
async function Dr(e, {
|
|
647
|
-
metadata: t,
|
|
648
|
-
clientInformation: n,
|
|
649
|
-
authorizationCode: r,
|
|
650
|
-
codeVerifier: o,
|
|
651
|
-
redirectUri: a,
|
|
652
|
-
resource: i,
|
|
653
|
-
addClientAuthentication: c,
|
|
654
|
-
fetchFn: l
|
|
655
|
-
}) {
|
|
656
|
-
var u;
|
|
657
|
-
const h = "authorization_code", g = t?.token_endpoint ? new URL(t.token_endpoint) : new URL("/token", e);
|
|
658
|
-
if (t?.grant_types_supported && !t.grant_types_supported.includes(h))
|
|
659
|
-
throw new Error(
|
|
660
|
-
`Incompatible auth server: does not support grant type ${h}`
|
|
661
|
-
);
|
|
662
|
-
const f = new Headers({
|
|
663
|
-
"Content-Type": "application/x-www-form-urlencoded",
|
|
664
|
-
Accept: "application/json"
|
|
665
|
-
}), v = new URLSearchParams({
|
|
666
|
-
grant_type: h,
|
|
667
|
-
code: r,
|
|
668
|
-
code_verifier: o,
|
|
669
|
-
redirect_uri: String(a)
|
|
670
|
-
});
|
|
671
|
-
if (c)
|
|
672
|
-
c(f, v, e, t);
|
|
673
|
-
else {
|
|
674
|
-
const p = (u = t?.token_endpoint_auth_methods_supported) != null ? u : [], w = At(
|
|
675
|
-
n,
|
|
676
|
-
p
|
|
677
|
-
);
|
|
678
|
-
$t(w, n, f, v);
|
|
679
|
-
}
|
|
680
|
-
i && v.set("resource", i.href);
|
|
681
|
-
const m = await (l ?? fetch)(g, {
|
|
682
|
-
method: "POST",
|
|
683
|
-
headers: f,
|
|
684
|
-
body: v
|
|
685
|
-
});
|
|
686
|
-
if (!m.ok)
|
|
687
|
-
throw await Ze(m);
|
|
688
|
-
return It.parse(await m.json());
|
|
689
|
-
}
|
|
690
|
-
async function Hr(e, {
|
|
691
|
-
metadata: t,
|
|
692
|
-
clientInformation: n,
|
|
693
|
-
refreshToken: r,
|
|
694
|
-
resource: o,
|
|
695
|
-
addClientAuthentication: a,
|
|
696
|
-
fetchFn: i
|
|
697
|
-
}) {
|
|
698
|
-
var c;
|
|
699
|
-
const l = "refresh_token";
|
|
700
|
-
let u;
|
|
701
|
-
if (t) {
|
|
702
|
-
if (u = new URL(t.token_endpoint), t.grant_types_supported && !t.grant_types_supported.includes(l))
|
|
703
|
-
throw new Error(
|
|
704
|
-
`Incompatible auth server: does not support grant type ${l}`
|
|
705
|
-
);
|
|
706
|
-
} else
|
|
707
|
-
u = new URL("/token", e);
|
|
708
|
-
const h = new Headers({
|
|
709
|
-
"Content-Type": "application/x-www-form-urlencoded"
|
|
710
|
-
}), g = new URLSearchParams({
|
|
711
|
-
grant_type: l,
|
|
712
|
-
refresh_token: r
|
|
713
|
-
});
|
|
714
|
-
if (a)
|
|
715
|
-
a(h, g, e, t);
|
|
716
|
-
else {
|
|
717
|
-
const v = (c = t?.token_endpoint_auth_methods_supported) != null ? c : [], m = At(
|
|
718
|
-
n,
|
|
719
|
-
v
|
|
720
|
-
);
|
|
721
|
-
$t(m, n, h, g);
|
|
722
|
-
}
|
|
723
|
-
o && g.set("resource", o.href);
|
|
724
|
-
const f = await (i ?? fetch)(u, {
|
|
725
|
-
method: "POST",
|
|
726
|
-
headers: h,
|
|
727
|
-
body: g
|
|
728
|
-
});
|
|
729
|
-
if (!f.ok)
|
|
730
|
-
throw await Ze(f);
|
|
731
|
-
return It.parse({
|
|
732
|
-
refresh_token: r,
|
|
733
|
-
...await f.json()
|
|
734
|
-
});
|
|
735
|
-
}
|
|
736
|
-
async function Lr(e, {
|
|
737
|
-
metadata: t,
|
|
738
|
-
clientMetadata: n,
|
|
739
|
-
fetchFn: r
|
|
740
|
-
}) {
|
|
741
|
-
let o;
|
|
742
|
-
if (t) {
|
|
743
|
-
if (!t.registration_endpoint)
|
|
744
|
-
throw new Error(
|
|
745
|
-
"Incompatible auth server: does not support dynamic client registration"
|
|
746
|
-
);
|
|
747
|
-
o = new URL(t.registration_endpoint);
|
|
748
|
-
} else
|
|
749
|
-
o = new URL("/register", e);
|
|
750
|
-
const a = await (r ?? fetch)(o, {
|
|
1
|
+
const i = "https://app.getgram.ai", o = () => ({
|
|
2
|
+
session: r
|
|
3
|
+
}), r = async (a, t, n) => {
|
|
4
|
+
a.method === "POST" && fetch(i + "/rpc/chatSessions.create", {
|
|
751
5
|
method: "POST",
|
|
6
|
+
body: JSON.stringify({
|
|
7
|
+
embed_origin: n?.embedOrigin,
|
|
8
|
+
user_identifier: n?.userIdentifier,
|
|
9
|
+
expires_after: n?.expiresAfter
|
|
10
|
+
}),
|
|
752
11
|
headers: {
|
|
753
|
-
"Content-Type": "application/json"
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
}
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
return await Me(e, t);
|
|
765
|
-
} catch (o) {
|
|
766
|
-
if (o instanceof ke || o instanceof Se)
|
|
767
|
-
return await ((n = e.invalidateCredentials) == null ? void 0 : n.call(e, "all")), await Me(e, t);
|
|
768
|
-
if (o instanceof Te)
|
|
769
|
-
return await ((r = e.invalidateCredentials) == null ? void 0 : r.call(e, "tokens")), await Me(e, t);
|
|
770
|
-
throw o;
|
|
771
|
-
}
|
|
772
|
-
}
|
|
773
|
-
async function qr(e, t, n) {
|
|
774
|
-
const r = Er(e);
|
|
775
|
-
if (t.validateResourceURL)
|
|
776
|
-
return await t.validateResourceURL(
|
|
777
|
-
r,
|
|
778
|
-
n?.resource
|
|
779
|
-
);
|
|
780
|
-
if (n) {
|
|
781
|
-
if (!Cr({
|
|
782
|
-
requestedResource: r,
|
|
783
|
-
configuredResource: n.resource
|
|
784
|
-
}))
|
|
785
|
-
throw new Error(
|
|
786
|
-
`Protected resource ${n.resource} does not match expected ${r} (or origin)`
|
|
787
|
-
);
|
|
788
|
-
return new URL(n.resource);
|
|
789
|
-
}
|
|
790
|
-
}
|
|
791
|
-
async function Me(e, {
|
|
792
|
-
serverUrl: t,
|
|
793
|
-
authorizationCode: n,
|
|
794
|
-
scope: r,
|
|
795
|
-
resourceMetadataUrl: o,
|
|
796
|
-
fetchFn: a
|
|
797
|
-
}) {
|
|
798
|
-
let i, c;
|
|
799
|
-
try {
|
|
800
|
-
i = await Pr(
|
|
801
|
-
t,
|
|
802
|
-
{ resourceMetadataUrl: o },
|
|
803
|
-
a
|
|
804
|
-
), i.authorization_servers && i.authorization_servers.length > 0 && (c = i.authorization_servers[0]);
|
|
805
|
-
} catch {
|
|
806
|
-
}
|
|
807
|
-
c || (c = t);
|
|
808
|
-
const l = await qr(
|
|
809
|
-
t,
|
|
810
|
-
e,
|
|
811
|
-
i
|
|
812
|
-
), u = await $r(
|
|
813
|
-
c,
|
|
814
|
-
{
|
|
815
|
-
fetchFn: a
|
|
816
|
-
}
|
|
817
|
-
);
|
|
818
|
-
let h = await Promise.resolve(e.clientInformation());
|
|
819
|
-
if (!h) {
|
|
820
|
-
if (n !== void 0)
|
|
821
|
-
throw new Error(
|
|
822
|
-
"Existing OAuth client information is required when exchanging an authorization code"
|
|
823
|
-
);
|
|
824
|
-
if (!e.saveClientInformation)
|
|
825
|
-
throw new Error(
|
|
826
|
-
"OAuth client information must be saveable for dynamic registration"
|
|
827
|
-
);
|
|
828
|
-
const p = await Lr(c, {
|
|
829
|
-
metadata: u,
|
|
830
|
-
clientMetadata: e.clientMetadata,
|
|
831
|
-
fetchFn: a
|
|
832
|
-
});
|
|
833
|
-
await e.saveClientInformation(p), h = p;
|
|
834
|
-
}
|
|
835
|
-
if (n !== void 0) {
|
|
836
|
-
const p = await e.codeVerifier(), w = await Dr(c, {
|
|
837
|
-
metadata: u,
|
|
838
|
-
clientInformation: h,
|
|
839
|
-
authorizationCode: n,
|
|
840
|
-
codeVerifier: p,
|
|
841
|
-
redirectUri: e.redirectUrl,
|
|
842
|
-
resource: l,
|
|
843
|
-
addClientAuthentication: e.addClientAuthentication,
|
|
844
|
-
fetchFn: a
|
|
845
|
-
});
|
|
846
|
-
return await e.saveTokens(w), "AUTHORIZED";
|
|
847
|
-
}
|
|
848
|
-
const g = await e.tokens();
|
|
849
|
-
if (g?.refresh_token)
|
|
850
|
-
try {
|
|
851
|
-
const p = await Hr(c, {
|
|
852
|
-
metadata: u,
|
|
853
|
-
clientInformation: h,
|
|
854
|
-
refreshToken: g.refresh_token,
|
|
855
|
-
resource: l,
|
|
856
|
-
addClientAuthentication: e.addClientAuthentication,
|
|
857
|
-
fetchFn: a
|
|
858
|
-
});
|
|
859
|
-
return await e.saveTokens(p), "AUTHORIZED";
|
|
860
|
-
} catch (p) {
|
|
861
|
-
if (
|
|
862
|
-
// If this is a ServerError, or an unknown type, log it out and try to continue. Otherwise, escalate so we can fix things and retry.
|
|
863
|
-
!(!(p instanceof _e) || p instanceof ce)
|
|
864
|
-
) throw p;
|
|
865
|
-
}
|
|
866
|
-
const f = e.state ? await e.state() : void 0, { authorizationUrl: v, codeVerifier: m } = await Ur(
|
|
867
|
-
c,
|
|
868
|
-
{
|
|
869
|
-
metadata: u,
|
|
870
|
-
clientInformation: h,
|
|
871
|
-
state: f,
|
|
872
|
-
redirectUrl: e.redirectUrl,
|
|
873
|
-
scope: r || e.clientMetadata.scope,
|
|
874
|
-
resource: l
|
|
875
|
-
}
|
|
876
|
-
);
|
|
877
|
-
return await e.saveCodeVerifier(m), await e.redirectToAuthorization(v), "REDIRECT";
|
|
878
|
-
}
|
|
879
|
-
var Vr = class {
|
|
880
|
-
constructor({
|
|
881
|
-
url: e,
|
|
882
|
-
headers: t,
|
|
883
|
-
authProvider: n
|
|
884
|
-
}) {
|
|
885
|
-
this.connected = !1, this.url = new URL(e), this.headers = t, this.authProvider = n;
|
|
886
|
-
}
|
|
887
|
-
async commonHeaders(e) {
|
|
888
|
-
const t = {
|
|
889
|
-
...this.headers,
|
|
890
|
-
...e,
|
|
891
|
-
"mcp-protocol-version": ue
|
|
892
|
-
};
|
|
893
|
-
if (this.authProvider) {
|
|
894
|
-
const n = await this.authProvider.tokens();
|
|
895
|
-
n?.access_token && (t.Authorization = `Bearer ${n.access_token}`);
|
|
896
|
-
}
|
|
897
|
-
return vt(
|
|
898
|
-
t,
|
|
899
|
-
`ai-sdk/${xt}`,
|
|
900
|
-
_t()
|
|
901
|
-
);
|
|
902
|
-
}
|
|
903
|
-
async start() {
|
|
904
|
-
return new Promise((e, t) => {
|
|
905
|
-
if (this.connected)
|
|
906
|
-
return e();
|
|
907
|
-
this.abortController = new AbortController();
|
|
908
|
-
const n = async (r = !1) => {
|
|
909
|
-
var o, a, i, c, l;
|
|
910
|
-
try {
|
|
911
|
-
const u = await this.commonHeaders({
|
|
912
|
-
Accept: "text/event-stream"
|
|
913
|
-
}), h = await fetch(this.url.href, {
|
|
914
|
-
headers: u,
|
|
915
|
-
signal: (o = this.abortController) == null ? void 0 : o.signal
|
|
916
|
-
});
|
|
917
|
-
if (h.status === 401 && this.authProvider && !r) {
|
|
918
|
-
this.resourceMetadataUrl = Ee(h);
|
|
919
|
-
try {
|
|
920
|
-
if (await Ce(this.authProvider, {
|
|
921
|
-
serverUrl: this.url,
|
|
922
|
-
resourceMetadataUrl: this.resourceMetadataUrl
|
|
923
|
-
}) !== "AUTHORIZED") {
|
|
924
|
-
const p = new Re();
|
|
925
|
-
return (a = this.onerror) == null || a.call(this, p), t(p);
|
|
926
|
-
}
|
|
927
|
-
} catch (m) {
|
|
928
|
-
return (i = this.onerror) == null || i.call(this, m), t(m);
|
|
929
|
-
}
|
|
930
|
-
return n(!0);
|
|
931
|
-
}
|
|
932
|
-
if (!h.ok || !h.body) {
|
|
933
|
-
let m = `MCP SSE Transport Error: ${h.status} ${h.statusText}`;
|
|
934
|
-
h.status === 405 && (m += ". This server does not support SSE transport. Try using `http` transport instead");
|
|
935
|
-
const p = new O({
|
|
936
|
-
message: m
|
|
937
|
-
});
|
|
938
|
-
return (c = this.onerror) == null || c.call(this, p), t(p);
|
|
939
|
-
}
|
|
940
|
-
const f = h.body.pipeThrough(new TextDecoderStream()).pipeThrough(new He()).getReader(), v = async () => {
|
|
941
|
-
var m, p, w;
|
|
942
|
-
try {
|
|
943
|
-
for (; ; ) {
|
|
944
|
-
const { done: b, value: R } = await f.read();
|
|
945
|
-
if (b) {
|
|
946
|
-
if (this.connected)
|
|
947
|
-
throw this.connected = !1, new O({
|
|
948
|
-
message: "MCP SSE Transport Error: Connection closed unexpectedly"
|
|
949
|
-
});
|
|
950
|
-
return;
|
|
951
|
-
}
|
|
952
|
-
const { event: T, data: S } = R;
|
|
953
|
-
if (T === "endpoint") {
|
|
954
|
-
if (this.endpoint = new URL(S, this.url), this.endpoint.origin !== this.url.origin)
|
|
955
|
-
throw new O({
|
|
956
|
-
message: `MCP SSE Transport Error: Endpoint origin does not match connection origin: ${this.endpoint.origin}`
|
|
957
|
-
});
|
|
958
|
-
this.connected = !0, e();
|
|
959
|
-
} else if (T === "message")
|
|
960
|
-
try {
|
|
961
|
-
const M = pe.parse(
|
|
962
|
-
JSON.parse(S)
|
|
963
|
-
);
|
|
964
|
-
(m = this.onmessage) == null || m.call(this, M);
|
|
965
|
-
} catch (M) {
|
|
966
|
-
const $ = new O({
|
|
967
|
-
message: "MCP SSE Transport Error: Failed to parse message",
|
|
968
|
-
cause: M
|
|
969
|
-
});
|
|
970
|
-
(p = this.onerror) == null || p.call(this, $);
|
|
971
|
-
}
|
|
972
|
-
}
|
|
973
|
-
} catch (b) {
|
|
974
|
-
if (b instanceof Error && b.name === "AbortError")
|
|
975
|
-
return;
|
|
976
|
-
(w = this.onerror) == null || w.call(this, b), t(b);
|
|
977
|
-
}
|
|
978
|
-
};
|
|
979
|
-
this.sseConnection = {
|
|
980
|
-
close: () => f.cancel()
|
|
981
|
-
}, v();
|
|
982
|
-
} catch (u) {
|
|
983
|
-
if (u instanceof Error && u.name === "AbortError")
|
|
984
|
-
return;
|
|
985
|
-
(l = this.onerror) == null || l.call(this, u), t(u);
|
|
986
|
-
}
|
|
987
|
-
};
|
|
988
|
-
n();
|
|
989
|
-
});
|
|
990
|
-
}
|
|
991
|
-
async close() {
|
|
992
|
-
var e, t, n;
|
|
993
|
-
this.connected = !1, (e = this.sseConnection) == null || e.close(), (t = this.abortController) == null || t.abort(), (n = this.onclose) == null || n.call(this);
|
|
994
|
-
}
|
|
995
|
-
async send(e) {
|
|
996
|
-
if (!this.endpoint || !this.connected)
|
|
997
|
-
throw new O({
|
|
998
|
-
message: "MCP SSE Transport Error: Not connected"
|
|
999
|
-
});
|
|
1000
|
-
const t = this.endpoint, n = async (r = !1) => {
|
|
1001
|
-
var o, a, i, c, l;
|
|
1002
|
-
try {
|
|
1003
|
-
const h = {
|
|
1004
|
-
method: "POST",
|
|
1005
|
-
headers: await this.commonHeaders({
|
|
1006
|
-
"Content-Type": "application/json"
|
|
1007
|
-
}),
|
|
1008
|
-
body: JSON.stringify(e),
|
|
1009
|
-
signal: (o = this.abortController) == null ? void 0 : o.signal
|
|
1010
|
-
}, g = await fetch(t, h);
|
|
1011
|
-
if (g.status === 401 && this.authProvider && !r) {
|
|
1012
|
-
this.resourceMetadataUrl = Ee(g);
|
|
1013
|
-
try {
|
|
1014
|
-
if (await Ce(this.authProvider, {
|
|
1015
|
-
serverUrl: this.url,
|
|
1016
|
-
resourceMetadataUrl: this.resourceMetadataUrl
|
|
1017
|
-
}) !== "AUTHORIZED") {
|
|
1018
|
-
const v = new Re();
|
|
1019
|
-
(a = this.onerror) == null || a.call(this, v);
|
|
1020
|
-
return;
|
|
1021
|
-
}
|
|
1022
|
-
} catch (f) {
|
|
1023
|
-
(i = this.onerror) == null || i.call(this, f);
|
|
1024
|
-
return;
|
|
1025
|
-
}
|
|
1026
|
-
return n(!0);
|
|
1027
|
-
}
|
|
1028
|
-
if (!g.ok) {
|
|
1029
|
-
const f = await g.text().catch(() => null), v = new O({
|
|
1030
|
-
message: `MCP SSE Transport Error: POSTing to endpoint (HTTP ${g.status}): ${f}`
|
|
1031
|
-
});
|
|
1032
|
-
(c = this.onerror) == null || c.call(this, v);
|
|
1033
|
-
return;
|
|
1034
|
-
}
|
|
1035
|
-
} catch (u) {
|
|
1036
|
-
(l = this.onerror) == null || l.call(this, u);
|
|
1037
|
-
return;
|
|
1038
|
-
}
|
|
1039
|
-
};
|
|
1040
|
-
await n();
|
|
1041
|
-
}
|
|
1042
|
-
}, Br = class {
|
|
1043
|
-
constructor({
|
|
1044
|
-
url: e,
|
|
1045
|
-
headers: t,
|
|
1046
|
-
authProvider: n
|
|
1047
|
-
}) {
|
|
1048
|
-
this.inboundReconnectAttempts = 0, this.reconnectionOptions = {
|
|
1049
|
-
initialReconnectionDelay: 1e3,
|
|
1050
|
-
maxReconnectionDelay: 3e4,
|
|
1051
|
-
reconnectionDelayGrowFactor: 1.5,
|
|
1052
|
-
maxRetries: 2
|
|
1053
|
-
}, this.url = new URL(e), this.headers = t, this.authProvider = n;
|
|
1054
|
-
}
|
|
1055
|
-
async commonHeaders(e) {
|
|
1056
|
-
const t = {
|
|
1057
|
-
...this.headers,
|
|
1058
|
-
...e,
|
|
1059
|
-
"mcp-protocol-version": ue
|
|
1060
|
-
};
|
|
1061
|
-
if (this.sessionId && (t["mcp-session-id"] = this.sessionId), this.authProvider) {
|
|
1062
|
-
const n = await this.authProvider.tokens();
|
|
1063
|
-
n?.access_token && (t.Authorization = `Bearer ${n.access_token}`);
|
|
1064
|
-
}
|
|
1065
|
-
return vt(
|
|
1066
|
-
t,
|
|
1067
|
-
`ai-sdk/${xt}`,
|
|
1068
|
-
_t()
|
|
1069
|
-
);
|
|
1070
|
-
}
|
|
1071
|
-
async start() {
|
|
1072
|
-
if (this.abortController)
|
|
1073
|
-
throw new O({
|
|
1074
|
-
message: "MCP HTTP Transport Error: Transport already started. Note: client.connect() calls start() automatically."
|
|
1075
|
-
});
|
|
1076
|
-
this.abortController = new AbortController(), this.openInboundSse();
|
|
1077
|
-
}
|
|
1078
|
-
async close() {
|
|
1079
|
-
var e, t, n;
|
|
1080
|
-
(e = this.inboundSseConnection) == null || e.close();
|
|
1081
|
-
try {
|
|
1082
|
-
if (this.sessionId && this.abortController && !this.abortController.signal.aborted) {
|
|
1083
|
-
const r = await this.commonHeaders({});
|
|
1084
|
-
await fetch(this.url, {
|
|
1085
|
-
method: "DELETE",
|
|
1086
|
-
headers: r,
|
|
1087
|
-
signal: this.abortController.signal
|
|
1088
|
-
}).catch(() => {
|
|
1089
|
-
});
|
|
1090
|
-
}
|
|
1091
|
-
} catch {
|
|
1092
|
-
}
|
|
1093
|
-
(t = this.abortController) == null || t.abort(), (n = this.onclose) == null || n.call(this);
|
|
1094
|
-
}
|
|
1095
|
-
async send(e) {
|
|
1096
|
-
const t = async (n = !1) => {
|
|
1097
|
-
var r, o, a, i, c, l, u;
|
|
1098
|
-
try {
|
|
1099
|
-
const g = {
|
|
1100
|
-
method: "POST",
|
|
1101
|
-
headers: await this.commonHeaders({
|
|
1102
|
-
"Content-Type": "application/json",
|
|
1103
|
-
Accept: "application/json, text/event-stream"
|
|
1104
|
-
}),
|
|
1105
|
-
body: JSON.stringify(e),
|
|
1106
|
-
signal: (r = this.abortController) == null ? void 0 : r.signal
|
|
1107
|
-
}, f = await fetch(this.url, g), v = f.headers.get("mcp-session-id");
|
|
1108
|
-
if (v && (this.sessionId = v), f.status === 401 && this.authProvider && !n) {
|
|
1109
|
-
this.resourceMetadataUrl = Ee(f);
|
|
1110
|
-
try {
|
|
1111
|
-
if (await Ce(this.authProvider, {
|
|
1112
|
-
serverUrl: this.url,
|
|
1113
|
-
resourceMetadataUrl: this.resourceMetadataUrl
|
|
1114
|
-
}) !== "AUTHORIZED")
|
|
1115
|
-
throw new Re();
|
|
1116
|
-
} catch (w) {
|
|
1117
|
-
throw (o = this.onerror) == null || o.call(this, w), w;
|
|
1118
|
-
}
|
|
1119
|
-
return t(!0);
|
|
1120
|
-
}
|
|
1121
|
-
if (f.status === 202) {
|
|
1122
|
-
this.inboundSseConnection || this.openInboundSse();
|
|
1123
|
-
return;
|
|
1124
|
-
}
|
|
1125
|
-
if (!f.ok) {
|
|
1126
|
-
const w = await f.text().catch(() => null);
|
|
1127
|
-
let b = `MCP HTTP Transport Error: POSTing to endpoint (HTTP ${f.status}): ${w}`;
|
|
1128
|
-
f.status === 404 && (b += ". This server does not support HTTP transport. Try using `sse` transport instead");
|
|
1129
|
-
const R = new O({
|
|
1130
|
-
message: b
|
|
1131
|
-
});
|
|
1132
|
-
throw (a = this.onerror) == null || a.call(this, R), R;
|
|
1133
|
-
}
|
|
1134
|
-
const m = f.headers.get("content-type") || "";
|
|
1135
|
-
if (m.includes("application/json")) {
|
|
1136
|
-
const w = await f.json(), b = Array.isArray(w) ? w.map((R) => pe.parse(R)) : [pe.parse(w)];
|
|
1137
|
-
for (const R of b) (i = this.onmessage) == null || i.call(this, R);
|
|
1138
|
-
return;
|
|
1139
|
-
}
|
|
1140
|
-
if (m.includes("text/event-stream")) {
|
|
1141
|
-
if (!f.body) {
|
|
1142
|
-
const T = new O({
|
|
1143
|
-
message: "MCP HTTP Transport Error: text/event-stream response without body"
|
|
1144
|
-
});
|
|
1145
|
-
throw (c = this.onerror) == null || c.call(this, T), T;
|
|
1146
|
-
}
|
|
1147
|
-
const b = f.body.pipeThrough(new TextDecoderStream()).pipeThrough(new He()).getReader();
|
|
1148
|
-
(async () => {
|
|
1149
|
-
var T, S, M;
|
|
1150
|
-
try {
|
|
1151
|
-
for (; ; ) {
|
|
1152
|
-
const { done: $, value: A } = await b.read();
|
|
1153
|
-
if ($) return;
|
|
1154
|
-
const { event: I, data: z } = A;
|
|
1155
|
-
if (I === "message")
|
|
1156
|
-
try {
|
|
1157
|
-
const J = pe.parse(JSON.parse(z));
|
|
1158
|
-
(T = this.onmessage) == null || T.call(this, J);
|
|
1159
|
-
} catch (J) {
|
|
1160
|
-
const G = new O({
|
|
1161
|
-
message: "MCP HTTP Transport Error: Failed to parse message",
|
|
1162
|
-
cause: J
|
|
1163
|
-
});
|
|
1164
|
-
(S = this.onerror) == null || S.call(this, G);
|
|
1165
|
-
}
|
|
1166
|
-
}
|
|
1167
|
-
} catch ($) {
|
|
1168
|
-
if ($ instanceof Error && $.name === "AbortError")
|
|
1169
|
-
return;
|
|
1170
|
-
(M = this.onerror) == null || M.call(this, $);
|
|
1171
|
-
}
|
|
1172
|
-
})();
|
|
1173
|
-
return;
|
|
1174
|
-
}
|
|
1175
|
-
const p = new O({
|
|
1176
|
-
message: `MCP HTTP Transport Error: Unexpected content type: ${m}`
|
|
1177
|
-
});
|
|
1178
|
-
throw (l = this.onerror) == null || l.call(this, p), p;
|
|
1179
|
-
} catch (h) {
|
|
1180
|
-
throw (u = this.onerror) == null || u.call(this, h), h;
|
|
1181
|
-
}
|
|
1182
|
-
};
|
|
1183
|
-
await t();
|
|
1184
|
-
}
|
|
1185
|
-
getNextReconnectionDelay(e) {
|
|
1186
|
-
const {
|
|
1187
|
-
initialReconnectionDelay: t,
|
|
1188
|
-
reconnectionDelayGrowFactor: n,
|
|
1189
|
-
maxReconnectionDelay: r
|
|
1190
|
-
} = this.reconnectionOptions;
|
|
1191
|
-
return Math.min(
|
|
1192
|
-
t * Math.pow(n, e),
|
|
1193
|
-
r
|
|
1194
|
-
);
|
|
1195
|
-
}
|
|
1196
|
-
scheduleInboundSseReconnection() {
|
|
1197
|
-
var e;
|
|
1198
|
-
const { maxRetries: t } = this.reconnectionOptions;
|
|
1199
|
-
if (t > 0 && this.inboundReconnectAttempts >= t) {
|
|
1200
|
-
(e = this.onerror) == null || e.call(
|
|
1201
|
-
this,
|
|
1202
|
-
new O({
|
|
1203
|
-
message: `MCP HTTP Transport Error: Maximum reconnection attempts (${t}) exceeded.`
|
|
1204
|
-
})
|
|
1205
|
-
);
|
|
1206
|
-
return;
|
|
1207
|
-
}
|
|
1208
|
-
const n = this.getNextReconnectionDelay(this.inboundReconnectAttempts);
|
|
1209
|
-
this.inboundReconnectAttempts += 1, setTimeout(async () => {
|
|
1210
|
-
var r;
|
|
1211
|
-
(r = this.abortController) != null && r.signal.aborted || await this.openInboundSse(!1, this.lastInboundEventId);
|
|
1212
|
-
}, n);
|
|
1213
|
-
}
|
|
1214
|
-
// Open optional inbound SSE stream; best-effort and resumable
|
|
1215
|
-
async openInboundSse(e = !1, t) {
|
|
1216
|
-
var n, r, o, a, i, c;
|
|
1217
|
-
try {
|
|
1218
|
-
const l = await this.commonHeaders({
|
|
1219
|
-
Accept: "text/event-stream"
|
|
1220
|
-
});
|
|
1221
|
-
t && (l["last-event-id"] = t);
|
|
1222
|
-
const u = await fetch(this.url.href, {
|
|
1223
|
-
method: "GET",
|
|
1224
|
-
headers: l,
|
|
1225
|
-
signal: (n = this.abortController) == null ? void 0 : n.signal
|
|
1226
|
-
}), h = u.headers.get("mcp-session-id");
|
|
1227
|
-
if (h && (this.sessionId = h), u.status === 401 && this.authProvider && !e) {
|
|
1228
|
-
this.resourceMetadataUrl = Ee(u);
|
|
1229
|
-
try {
|
|
1230
|
-
if (await Ce(this.authProvider, {
|
|
1231
|
-
serverUrl: this.url,
|
|
1232
|
-
resourceMetadataUrl: this.resourceMetadataUrl
|
|
1233
|
-
}) !== "AUTHORIZED") {
|
|
1234
|
-
const p = new Re();
|
|
1235
|
-
(r = this.onerror) == null || r.call(this, p);
|
|
1236
|
-
return;
|
|
1237
|
-
}
|
|
1238
|
-
} catch (m) {
|
|
1239
|
-
(o = this.onerror) == null || o.call(this, m);
|
|
1240
|
-
return;
|
|
1241
|
-
}
|
|
1242
|
-
return this.openInboundSse(!0, t);
|
|
1243
|
-
}
|
|
1244
|
-
if (u.status === 405)
|
|
1245
|
-
return;
|
|
1246
|
-
if (!u.ok || !u.body) {
|
|
1247
|
-
const m = new O({
|
|
1248
|
-
message: `MCP HTTP Transport Error: GET SSE failed: ${u.status} ${u.statusText}`
|
|
1249
|
-
});
|
|
1250
|
-
(a = this.onerror) == null || a.call(this, m);
|
|
1251
|
-
return;
|
|
1252
|
-
}
|
|
1253
|
-
const f = u.body.pipeThrough(new TextDecoderStream()).pipeThrough(new He()).getReader(), v = async () => {
|
|
1254
|
-
var m, p, w, b;
|
|
1255
|
-
try {
|
|
1256
|
-
for (; ; ) {
|
|
1257
|
-
const { done: R, value: T } = await f.read();
|
|
1258
|
-
if (R) return;
|
|
1259
|
-
const { event: S, data: M, id: $ } = T;
|
|
1260
|
-
if ($ && (this.lastInboundEventId = $), S === "message")
|
|
1261
|
-
try {
|
|
1262
|
-
const A = pe.parse(JSON.parse(M));
|
|
1263
|
-
(m = this.onmessage) == null || m.call(this, A);
|
|
1264
|
-
} catch (A) {
|
|
1265
|
-
const I = new O({
|
|
1266
|
-
message: "MCP HTTP Transport Error: Failed to parse message",
|
|
1267
|
-
cause: A
|
|
1268
|
-
});
|
|
1269
|
-
(p = this.onerror) == null || p.call(this, I);
|
|
1270
|
-
}
|
|
1271
|
-
}
|
|
1272
|
-
} catch (R) {
|
|
1273
|
-
if (R instanceof Error && R.name === "AbortError")
|
|
1274
|
-
return;
|
|
1275
|
-
(w = this.onerror) == null || w.call(this, R), (b = this.abortController) != null && b.signal.aborted || this.scheduleInboundSseReconnection();
|
|
1276
|
-
}
|
|
1277
|
-
};
|
|
1278
|
-
this.inboundSseConnection = {
|
|
1279
|
-
close: () => f.cancel()
|
|
1280
|
-
}, this.inboundReconnectAttempts = 0, v();
|
|
1281
|
-
} catch (l) {
|
|
1282
|
-
if (l instanceof Error && l.name === "AbortError")
|
|
1283
|
-
return;
|
|
1284
|
-
(i = this.onerror) == null || i.call(this, l), (c = this.abortController) != null && c.signal.aborted || this.scheduleInboundSseReconnection();
|
|
1285
|
-
}
|
|
1286
|
-
}
|
|
1287
|
-
};
|
|
1288
|
-
function Jr(e) {
|
|
1289
|
-
switch (e.type) {
|
|
1290
|
-
case "sse":
|
|
1291
|
-
return new Vr(e);
|
|
1292
|
-
case "http":
|
|
1293
|
-
return new Br(e);
|
|
1294
|
-
default:
|
|
1295
|
-
throw new O({
|
|
1296
|
-
message: "Unsupported or invalid transport configuration. If you are using a custom transport, make sure it implements the MCPTransport interface."
|
|
1297
|
-
});
|
|
1298
|
-
}
|
|
1299
|
-
}
|
|
1300
|
-
function zr(e) {
|
|
1301
|
-
return "start" in e && typeof e.start == "function" && "send" in e && typeof e.send == "function" && "close" in e && typeof e.close == "function";
|
|
1302
|
-
}
|
|
1303
|
-
var Gr = "1.0.0";
|
|
1304
|
-
async function Wr(e) {
|
|
1305
|
-
const t = new Fr(e);
|
|
1306
|
-
return await t.init(), t;
|
|
1307
|
-
}
|
|
1308
|
-
var Fr = class {
|
|
1309
|
-
constructor({
|
|
1310
|
-
transport: e,
|
|
1311
|
-
name: t = "ai-sdk-mcp-client",
|
|
1312
|
-
version: n = Gr,
|
|
1313
|
-
onUncaughtError: r,
|
|
1314
|
-
capabilities: o
|
|
1315
|
-
}) {
|
|
1316
|
-
this.requestMessageId = 0, this.responseHandlers = /* @__PURE__ */ new Map(), this.serverCapabilities = {}, this.isClosed = !0, this.onUncaughtError = r, this.clientCapabilities = o ?? {}, zr(e) ? this.transport = e : this.transport = Jr(e), this.transport.onclose = () => this.onClose(), this.transport.onerror = (a) => this.onError(a), this.transport.onmessage = (a) => {
|
|
1317
|
-
if ("method" in a) {
|
|
1318
|
-
"id" in a ? this.onRequestMessage(a) : this.onError(
|
|
1319
|
-
new O({
|
|
1320
|
-
message: "Unsupported message type"
|
|
1321
|
-
})
|
|
1322
|
-
);
|
|
1323
|
-
return;
|
|
1324
|
-
}
|
|
1325
|
-
this.onResponse(a);
|
|
1326
|
-
}, this.clientInfo = {
|
|
1327
|
-
name: t,
|
|
1328
|
-
version: n
|
|
1329
|
-
};
|
|
1330
|
-
}
|
|
1331
|
-
async init() {
|
|
1332
|
-
try {
|
|
1333
|
-
await this.transport.start(), this.isClosed = !1;
|
|
1334
|
-
const e = await this.request({
|
|
1335
|
-
request: {
|
|
1336
|
-
method: "initialize",
|
|
1337
|
-
params: {
|
|
1338
|
-
protocolVersion: ue,
|
|
1339
|
-
capabilities: this.clientCapabilities,
|
|
1340
|
-
clientInfo: this.clientInfo
|
|
1341
|
-
}
|
|
1342
|
-
},
|
|
1343
|
-
resultSchema: Zn
|
|
1344
|
-
});
|
|
1345
|
-
if (e === void 0)
|
|
1346
|
-
throw new O({
|
|
1347
|
-
message: "Server sent invalid initialize result"
|
|
1348
|
-
});
|
|
1349
|
-
if (!Wn.includes(e.protocolVersion))
|
|
1350
|
-
throw new O({
|
|
1351
|
-
message: `Server's protocol version is not supported: ${e.protocolVersion}`
|
|
1352
|
-
});
|
|
1353
|
-
return this.serverCapabilities = e.capabilities, await this.notification({
|
|
1354
|
-
method: "notifications/initialized"
|
|
1355
|
-
}), this;
|
|
1356
|
-
} catch (e) {
|
|
1357
|
-
throw await this.close(), e;
|
|
1358
|
-
}
|
|
1359
|
-
}
|
|
1360
|
-
async close() {
|
|
1361
|
-
var e;
|
|
1362
|
-
this.isClosed || (await ((e = this.transport) == null ? void 0 : e.close()), this.onClose());
|
|
1363
|
-
}
|
|
1364
|
-
assertCapability(e) {
|
|
1365
|
-
switch (e) {
|
|
1366
|
-
case "initialize":
|
|
1367
|
-
break;
|
|
1368
|
-
case "tools/list":
|
|
1369
|
-
case "tools/call":
|
|
1370
|
-
if (!this.serverCapabilities.tools)
|
|
1371
|
-
throw new O({
|
|
1372
|
-
message: "Server does not support tools"
|
|
1373
|
-
});
|
|
1374
|
-
break;
|
|
1375
|
-
case "resources/list":
|
|
1376
|
-
case "resources/read":
|
|
1377
|
-
case "resources/templates/list":
|
|
1378
|
-
if (!this.serverCapabilities.resources)
|
|
1379
|
-
throw new O({
|
|
1380
|
-
message: "Server does not support resources"
|
|
1381
|
-
});
|
|
1382
|
-
break;
|
|
1383
|
-
case "prompts/list":
|
|
1384
|
-
case "prompts/get":
|
|
1385
|
-
if (!this.serverCapabilities.prompts)
|
|
1386
|
-
throw new O({
|
|
1387
|
-
message: "Server does not support prompts"
|
|
1388
|
-
});
|
|
1389
|
-
break;
|
|
1390
|
-
default:
|
|
1391
|
-
throw new O({
|
|
1392
|
-
message: `Unsupported method: ${e}`
|
|
1393
|
-
});
|
|
1394
|
-
}
|
|
1395
|
-
}
|
|
1396
|
-
async request({
|
|
1397
|
-
request: e,
|
|
1398
|
-
resultSchema: t,
|
|
1399
|
-
options: n
|
|
1400
|
-
}) {
|
|
1401
|
-
return new Promise((r, o) => {
|
|
1402
|
-
if (this.isClosed)
|
|
1403
|
-
return o(
|
|
1404
|
-
new O({
|
|
1405
|
-
message: "Attempted to send a request from a closed client"
|
|
1406
|
-
})
|
|
1407
|
-
);
|
|
1408
|
-
this.assertCapability(e.method);
|
|
1409
|
-
const a = n?.signal;
|
|
1410
|
-
a?.throwIfAborted();
|
|
1411
|
-
const i = this.requestMessageId++, c = {
|
|
1412
|
-
...e,
|
|
1413
|
-
jsonrpc: "2.0",
|
|
1414
|
-
id: i
|
|
1415
|
-
}, l = () => {
|
|
1416
|
-
this.responseHandlers.delete(i);
|
|
1417
|
-
};
|
|
1418
|
-
this.responseHandlers.set(i, (u) => {
|
|
1419
|
-
if (a?.aborted)
|
|
1420
|
-
return o(
|
|
1421
|
-
new O({
|
|
1422
|
-
message: "Request was aborted",
|
|
1423
|
-
cause: a.reason
|
|
1424
|
-
})
|
|
1425
|
-
);
|
|
1426
|
-
if (u instanceof Error)
|
|
1427
|
-
return o(u);
|
|
1428
|
-
try {
|
|
1429
|
-
const h = t.parse(u.result);
|
|
1430
|
-
r(h);
|
|
1431
|
-
} catch (h) {
|
|
1432
|
-
const g = new O({
|
|
1433
|
-
message: "Failed to parse server response",
|
|
1434
|
-
cause: h
|
|
1435
|
-
});
|
|
1436
|
-
o(g);
|
|
1437
|
-
}
|
|
1438
|
-
}), this.transport.send(c).catch((u) => {
|
|
1439
|
-
l(), o(u);
|
|
1440
|
-
});
|
|
1441
|
-
});
|
|
1442
|
-
}
|
|
1443
|
-
async listTools({
|
|
1444
|
-
params: e,
|
|
1445
|
-
options: t
|
|
1446
|
-
} = {}) {
|
|
1447
|
-
try {
|
|
1448
|
-
return this.request({
|
|
1449
|
-
request: { method: "tools/list", params: e },
|
|
1450
|
-
resultSchema: Yn,
|
|
1451
|
-
options: t
|
|
1452
|
-
});
|
|
1453
|
-
} catch (n) {
|
|
1454
|
-
throw n;
|
|
1455
|
-
}
|
|
1456
|
-
}
|
|
1457
|
-
async callTool({
|
|
1458
|
-
name: e,
|
|
1459
|
-
args: t,
|
|
1460
|
-
options: n
|
|
1461
|
-
}) {
|
|
1462
|
-
try {
|
|
1463
|
-
return this.request({
|
|
1464
|
-
request: { method: "tools/call", params: { name: e, arguments: t } },
|
|
1465
|
-
resultSchema: tr,
|
|
1466
|
-
options: {
|
|
1467
|
-
signal: n?.abortSignal
|
|
1468
|
-
}
|
|
1469
|
-
});
|
|
1470
|
-
} catch (r) {
|
|
1471
|
-
throw r;
|
|
1472
|
-
}
|
|
1473
|
-
}
|
|
1474
|
-
async listResourcesInternal({
|
|
1475
|
-
params: e,
|
|
1476
|
-
options: t
|
|
1477
|
-
} = {}) {
|
|
1478
|
-
try {
|
|
1479
|
-
return this.request({
|
|
1480
|
-
request: { method: "resources/list", params: e },
|
|
1481
|
-
resultSchema: er,
|
|
1482
|
-
options: t
|
|
1483
|
-
});
|
|
1484
|
-
} catch (n) {
|
|
1485
|
-
throw n;
|
|
1486
|
-
}
|
|
1487
|
-
}
|
|
1488
|
-
async readResourceInternal({
|
|
1489
|
-
uri: e,
|
|
1490
|
-
options: t
|
|
1491
|
-
}) {
|
|
1492
|
-
try {
|
|
1493
|
-
return this.request({
|
|
1494
|
-
request: { method: "resources/read", params: { uri: e } },
|
|
1495
|
-
resultSchema: or,
|
|
1496
|
-
options: t
|
|
1497
|
-
});
|
|
1498
|
-
} catch (n) {
|
|
1499
|
-
throw n;
|
|
1500
|
-
}
|
|
1501
|
-
}
|
|
1502
|
-
async listResourceTemplatesInternal({
|
|
1503
|
-
options: e
|
|
1504
|
-
} = {}) {
|
|
1505
|
-
try {
|
|
1506
|
-
return this.request({
|
|
1507
|
-
request: { method: "resources/templates/list" },
|
|
1508
|
-
resultSchema: rr,
|
|
1509
|
-
options: e
|
|
1510
|
-
});
|
|
1511
|
-
} catch (t) {
|
|
1512
|
-
throw t;
|
|
1513
|
-
}
|
|
1514
|
-
}
|
|
1515
|
-
async listPromptsInternal({
|
|
1516
|
-
params: e,
|
|
1517
|
-
options: t
|
|
1518
|
-
} = {}) {
|
|
1519
|
-
try {
|
|
1520
|
-
return this.request({
|
|
1521
|
-
request: { method: "prompts/list", params: e },
|
|
1522
|
-
resultSchema: ir,
|
|
1523
|
-
options: t
|
|
1524
|
-
});
|
|
1525
|
-
} catch (n) {
|
|
1526
|
-
throw n;
|
|
1527
|
-
}
|
|
1528
|
-
}
|
|
1529
|
-
async getPromptInternal({
|
|
1530
|
-
name: e,
|
|
1531
|
-
args: t,
|
|
1532
|
-
options: n
|
|
1533
|
-
}) {
|
|
1534
|
-
try {
|
|
1535
|
-
return this.request({
|
|
1536
|
-
request: { method: "prompts/get", params: { name: e, arguments: t } },
|
|
1537
|
-
resultSchema: cr,
|
|
1538
|
-
options: n
|
|
1539
|
-
});
|
|
1540
|
-
} catch (r) {
|
|
1541
|
-
throw r;
|
|
1542
|
-
}
|
|
1543
|
-
}
|
|
1544
|
-
async notification(e) {
|
|
1545
|
-
const t = {
|
|
1546
|
-
...e,
|
|
1547
|
-
jsonrpc: "2.0"
|
|
1548
|
-
};
|
|
1549
|
-
await this.transport.send(t);
|
|
1550
|
-
}
|
|
1551
|
-
/**
|
|
1552
|
-
* Returns a set of AI SDK tools from the MCP server
|
|
1553
|
-
* @returns A record of tool names to their implementations
|
|
1554
|
-
*/
|
|
1555
|
-
async tools({
|
|
1556
|
-
schemas: e = "automatic"
|
|
1557
|
-
} = {}) {
|
|
1558
|
-
var t;
|
|
1559
|
-
const n = {};
|
|
1560
|
-
try {
|
|
1561
|
-
const r = await this.listTools();
|
|
1562
|
-
for (const { name: o, description: a, inputSchema: i } of r.tools) {
|
|
1563
|
-
if (e !== "automatic" && !(o in e))
|
|
1564
|
-
continue;
|
|
1565
|
-
const c = this, l = async (h, g) => {
|
|
1566
|
-
var f;
|
|
1567
|
-
return (f = g?.abortSignal) == null || f.throwIfAborted(), c.callTool({ name: o, args: h, options: g });
|
|
1568
|
-
}, u = e === "automatic" ? jn({
|
|
1569
|
-
description: a,
|
|
1570
|
-
inputSchema: Hn({
|
|
1571
|
-
...i,
|
|
1572
|
-
properties: (t = i.properties) != null ? t : {},
|
|
1573
|
-
additionalProperties: !1
|
|
1574
|
-
}),
|
|
1575
|
-
execute: l
|
|
1576
|
-
}) : {
|
|
1577
|
-
description: a,
|
|
1578
|
-
inputSchema: e[o].inputSchema,
|
|
1579
|
-
execute: l
|
|
1580
|
-
};
|
|
1581
|
-
n[o] = u;
|
|
1582
|
-
}
|
|
1583
|
-
return n;
|
|
1584
|
-
} catch (r) {
|
|
1585
|
-
throw r;
|
|
1586
|
-
}
|
|
1587
|
-
}
|
|
1588
|
-
listResources({
|
|
1589
|
-
params: e,
|
|
1590
|
-
options: t
|
|
1591
|
-
} = {}) {
|
|
1592
|
-
return this.listResourcesInternal({ params: e, options: t });
|
|
1593
|
-
}
|
|
1594
|
-
readResource({
|
|
1595
|
-
uri: e,
|
|
1596
|
-
options: t
|
|
1597
|
-
}) {
|
|
1598
|
-
return this.readResourceInternal({ uri: e, options: t });
|
|
1599
|
-
}
|
|
1600
|
-
listResourceTemplates({
|
|
1601
|
-
options: e
|
|
1602
|
-
} = {}) {
|
|
1603
|
-
return this.listResourceTemplatesInternal({ options: e });
|
|
1604
|
-
}
|
|
1605
|
-
listPrompts({
|
|
1606
|
-
params: e,
|
|
1607
|
-
options: t
|
|
1608
|
-
} = {}) {
|
|
1609
|
-
return this.listPromptsInternal({ params: e, options: t });
|
|
1610
|
-
}
|
|
1611
|
-
getPrompt({
|
|
1612
|
-
name: e,
|
|
1613
|
-
arguments: t,
|
|
1614
|
-
options: n
|
|
1615
|
-
}) {
|
|
1616
|
-
return this.getPromptInternal({ name: e, args: t, options: n });
|
|
1617
|
-
}
|
|
1618
|
-
onElicitationRequest(e, t) {
|
|
1619
|
-
if (e !== st)
|
|
1620
|
-
throw new O({
|
|
1621
|
-
message: "Unsupported request schema. Only ElicitationRequestSchema is supported."
|
|
1622
|
-
});
|
|
1623
|
-
this.elicitationRequestHandler = t;
|
|
1624
|
-
}
|
|
1625
|
-
async onRequestMessage(e) {
|
|
1626
|
-
try {
|
|
1627
|
-
if (e.method !== "elicitation/create") {
|
|
1628
|
-
await this.transport.send({
|
|
1629
|
-
jsonrpc: "2.0",
|
|
1630
|
-
id: e.id,
|
|
1631
|
-
error: {
|
|
1632
|
-
code: -32601,
|
|
1633
|
-
message: `Unsupported request method: ${e.method}`
|
|
1634
|
-
}
|
|
1635
|
-
});
|
|
1636
|
-
return;
|
|
1637
|
-
}
|
|
1638
|
-
if (!this.elicitationRequestHandler) {
|
|
1639
|
-
await this.transport.send({
|
|
1640
|
-
jsonrpc: "2.0",
|
|
1641
|
-
id: e.id,
|
|
1642
|
-
error: {
|
|
1643
|
-
code: -32601,
|
|
1644
|
-
message: "No elicitation handler registered on client"
|
|
1645
|
-
}
|
|
1646
|
-
});
|
|
1647
|
-
return;
|
|
1648
|
-
}
|
|
1649
|
-
const t = st.safeParse({
|
|
1650
|
-
method: e.method,
|
|
1651
|
-
params: e.params
|
|
1652
|
-
});
|
|
1653
|
-
if (!t.success) {
|
|
1654
|
-
await this.transport.send({
|
|
1655
|
-
jsonrpc: "2.0",
|
|
1656
|
-
id: e.id,
|
|
1657
|
-
error: {
|
|
1658
|
-
code: -32602,
|
|
1659
|
-
message: `Invalid elicitation request: ${t.error.message}`,
|
|
1660
|
-
data: t.error.issues
|
|
1661
|
-
}
|
|
1662
|
-
});
|
|
1663
|
-
return;
|
|
1664
|
-
}
|
|
1665
|
-
try {
|
|
1666
|
-
const n = await this.elicitationRequestHandler(t.data), r = dr.parse(n);
|
|
1667
|
-
await this.transport.send({
|
|
1668
|
-
jsonrpc: "2.0",
|
|
1669
|
-
id: e.id,
|
|
1670
|
-
result: r
|
|
1671
|
-
});
|
|
1672
|
-
} catch (n) {
|
|
1673
|
-
await this.transport.send({
|
|
1674
|
-
jsonrpc: "2.0",
|
|
1675
|
-
id: e.id,
|
|
1676
|
-
error: {
|
|
1677
|
-
code: -32603,
|
|
1678
|
-
message: n instanceof Error ? n.message : "Failed to handle elicitation request"
|
|
1679
|
-
}
|
|
1680
|
-
}), this.onError(n);
|
|
1681
|
-
}
|
|
1682
|
-
} catch (t) {
|
|
1683
|
-
this.onError(t);
|
|
1684
|
-
}
|
|
1685
|
-
}
|
|
1686
|
-
onClose() {
|
|
1687
|
-
if (this.isClosed) return;
|
|
1688
|
-
this.isClosed = !0;
|
|
1689
|
-
const e = new O({
|
|
1690
|
-
message: "Connection closed"
|
|
1691
|
-
});
|
|
1692
|
-
for (const t of this.responseHandlers.values())
|
|
1693
|
-
t(e);
|
|
1694
|
-
this.responseHandlers.clear();
|
|
1695
|
-
}
|
|
1696
|
-
onError(e) {
|
|
1697
|
-
this.onUncaughtError && this.onUncaughtError(e);
|
|
1698
|
-
}
|
|
1699
|
-
onResponse(e) {
|
|
1700
|
-
const t = Number(e.id), n = this.responseHandlers.get(t);
|
|
1701
|
-
if (n === void 0)
|
|
1702
|
-
throw new O({
|
|
1703
|
-
message: `Protocol error: Received a response for an unknown message ID: ${JSON.stringify(
|
|
1704
|
-
e
|
|
1705
|
-
)}`
|
|
1706
|
-
});
|
|
1707
|
-
this.responseHandlers.delete(t), n(
|
|
1708
|
-
"result" in e ? e : new O({
|
|
1709
|
-
message: e.error.message,
|
|
1710
|
-
code: e.error.code,
|
|
1711
|
-
data: e.error.data,
|
|
1712
|
-
cause: e.error
|
|
12
|
+
"Content-Type": "application/json",
|
|
13
|
+
"Gram-Project": "default",
|
|
14
|
+
"Gram-Key": process.env.GRAM_API_KEY ?? ""
|
|
15
|
+
}
|
|
16
|
+
}).then(async (e) => {
|
|
17
|
+
const s = await e.text();
|
|
18
|
+
t.writeHead(e.status, { "Content-Type": "application/json" }), t.end(s);
|
|
19
|
+
}).catch((e) => {
|
|
20
|
+
console.error("Failed to create chat session:", e), t.writeHead(500, { "Content-Type": "application/json" }), t.end(
|
|
21
|
+
JSON.stringify({
|
|
22
|
+
error: "Failed to create chat session: " + e.message
|
|
1713
23
|
})
|
|
1714
24
|
);
|
|
1715
|
-
}
|
|
1716
|
-
}, Kr = Object.defineProperty, Zr = Object.defineProperties, Xr = Object.getOwnPropertyDescriptors, ct = Object.getOwnPropertySymbols, Yr = Object.prototype.hasOwnProperty, Qr = Object.prototype.propertyIsEnumerable, ut = (e, t, n) => t in e ? Kr(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n, P = (e, t) => {
|
|
1717
|
-
for (var n in t || (t = {}))
|
|
1718
|
-
Yr.call(t, n) && ut(e, n, t[n]);
|
|
1719
|
-
if (ct)
|
|
1720
|
-
for (var n of ct(t))
|
|
1721
|
-
Qr.call(t, n) && ut(e, n, t[n]);
|
|
1722
|
-
return e;
|
|
1723
|
-
}, xe = (e, t) => Zr(e, Xr(t)), Ut = "vercel.ai.error", eo = Symbol.for(Ut), Mt, to = class Nt extends Error {
|
|
1724
|
-
/**
|
|
1725
|
-
* Creates an AI SDK Error.
|
|
1726
|
-
*
|
|
1727
|
-
* @param {Object} params - The parameters for creating the error.
|
|
1728
|
-
* @param {string} params.name - The name of the error.
|
|
1729
|
-
* @param {string} params.message - The error message.
|
|
1730
|
-
* @param {unknown} [params.cause] - The underlying cause of the error.
|
|
1731
|
-
*/
|
|
1732
|
-
constructor({
|
|
1733
|
-
name: t,
|
|
1734
|
-
message: n,
|
|
1735
|
-
cause: r
|
|
1736
|
-
}) {
|
|
1737
|
-
super(n), this[Mt] = !0, this.name = t, this.cause = r;
|
|
1738
|
-
}
|
|
1739
|
-
/**
|
|
1740
|
-
* Checks if the given error is an AI SDK Error.
|
|
1741
|
-
* @param {unknown} error - The error to check.
|
|
1742
|
-
* @returns {boolean} True if the error is an AI SDK Error, false otherwise.
|
|
1743
|
-
*/
|
|
1744
|
-
static isInstance(t) {
|
|
1745
|
-
return Nt.hasMarker(t, Ut);
|
|
1746
|
-
}
|
|
1747
|
-
static hasMarker(t, n) {
|
|
1748
|
-
const r = Symbol.for(n);
|
|
1749
|
-
return t != null && typeof t == "object" && r in t && typeof t[r] == "boolean" && t[r] === !0;
|
|
1750
|
-
}
|
|
1751
|
-
};
|
|
1752
|
-
Mt = eo;
|
|
1753
|
-
var L = to, jt = "AI_APICallError", Dt = `vercel.ai.error.${jt}`, no = Symbol.for(Dt), Ht, F = class extends L {
|
|
1754
|
-
constructor({
|
|
1755
|
-
message: e,
|
|
1756
|
-
url: t,
|
|
1757
|
-
requestBodyValues: n,
|
|
1758
|
-
statusCode: r,
|
|
1759
|
-
responseHeaders: o,
|
|
1760
|
-
responseBody: a,
|
|
1761
|
-
cause: i,
|
|
1762
|
-
isRetryable: c = r != null && (r === 408 || // request timeout
|
|
1763
|
-
r === 409 || // conflict
|
|
1764
|
-
r === 429 || // too many requests
|
|
1765
|
-
r >= 500),
|
|
1766
|
-
// server error
|
|
1767
|
-
data: l
|
|
1768
|
-
}) {
|
|
1769
|
-
super({ name: jt, message: e, cause: i }), this[Ht] = !0, this.url = t, this.requestBodyValues = n, this.statusCode = r, this.responseHeaders = o, this.responseBody = a, this.isRetryable = c, this.data = l;
|
|
1770
|
-
}
|
|
1771
|
-
static isInstance(e) {
|
|
1772
|
-
return L.hasMarker(e, Dt);
|
|
1773
|
-
}
|
|
1774
|
-
};
|
|
1775
|
-
Ht = no;
|
|
1776
|
-
var Lt = "AI_EmptyResponseBodyError", qt = `vercel.ai.error.${Lt}`, ro = Symbol.for(qt), Vt, oo = class extends L {
|
|
1777
|
-
// used in isInstance
|
|
1778
|
-
constructor({ message: e = "Empty response body" } = {}) {
|
|
1779
|
-
super({ name: Lt, message: e }), this[Vt] = !0;
|
|
1780
|
-
}
|
|
1781
|
-
static isInstance(e) {
|
|
1782
|
-
return L.hasMarker(e, qt);
|
|
1783
|
-
}
|
|
1784
|
-
};
|
|
1785
|
-
Vt = ro;
|
|
1786
|
-
function Bt(e) {
|
|
1787
|
-
return e == null ? "unknown error" : typeof e == "string" ? e : e instanceof Error ? e.message : JSON.stringify(e);
|
|
1788
|
-
}
|
|
1789
|
-
var Jt = "AI_InvalidArgumentError", zt = `vercel.ai.error.${Jt}`, so = Symbol.for(zt), Gt, Wt = class extends L {
|
|
1790
|
-
constructor({
|
|
1791
|
-
message: e,
|
|
1792
|
-
cause: t,
|
|
1793
|
-
argument: n
|
|
1794
|
-
}) {
|
|
1795
|
-
super({ name: Jt, message: e, cause: t }), this[Gt] = !0, this.argument = n;
|
|
1796
|
-
}
|
|
1797
|
-
static isInstance(e) {
|
|
1798
|
-
return L.hasMarker(e, zt);
|
|
1799
|
-
}
|
|
1800
|
-
};
|
|
1801
|
-
Gt = so;
|
|
1802
|
-
var Ft = "AI_InvalidPromptError", Kt = `vercel.ai.error.${Ft}`, ao = Symbol.for(Kt), Zt, io = class extends L {
|
|
1803
|
-
constructor({
|
|
1804
|
-
prompt: e,
|
|
1805
|
-
message: t,
|
|
1806
|
-
cause: n
|
|
1807
|
-
}) {
|
|
1808
|
-
super({ name: Ft, message: `Invalid prompt: ${t}`, cause: n }), this[Zt] = !0, this.prompt = e;
|
|
1809
|
-
}
|
|
1810
|
-
static isInstance(e) {
|
|
1811
|
-
return L.hasMarker(e, Kt);
|
|
1812
|
-
}
|
|
1813
|
-
};
|
|
1814
|
-
Zt = ao;
|
|
1815
|
-
var Xt = "AI_InvalidResponseDataError", Yt = `vercel.ai.error.${Xt}`, lo = Symbol.for(Yt), Qt, de = class extends L {
|
|
1816
|
-
constructor({
|
|
1817
|
-
data: e,
|
|
1818
|
-
message: t = `Invalid response data: ${JSON.stringify(e)}.`
|
|
1819
|
-
}) {
|
|
1820
|
-
super({ name: Xt, message: t }), this[Qt] = !0, this.data = e;
|
|
1821
|
-
}
|
|
1822
|
-
static isInstance(e) {
|
|
1823
|
-
return L.hasMarker(e, Yt);
|
|
1824
|
-
}
|
|
1825
|
-
};
|
|
1826
|
-
Qt = lo;
|
|
1827
|
-
var en = "AI_JSONParseError", tn = `vercel.ai.error.${en}`, co = Symbol.for(tn), nn, Ie = class extends L {
|
|
1828
|
-
constructor({ text: e, cause: t }) {
|
|
1829
|
-
super({
|
|
1830
|
-
name: en,
|
|
1831
|
-
message: `JSON parsing failed: Text: ${e}.
|
|
1832
|
-
Error message: ${Bt(t)}`,
|
|
1833
|
-
cause: t
|
|
1834
|
-
}), this[nn] = !0, this.text = e;
|
|
1835
|
-
}
|
|
1836
|
-
static isInstance(e) {
|
|
1837
|
-
return L.hasMarker(e, tn);
|
|
1838
|
-
}
|
|
1839
|
-
};
|
|
1840
|
-
nn = co;
|
|
1841
|
-
var rn = "AI_LoadAPIKeyError", on = `vercel.ai.error.${rn}`, uo = Symbol.for(on), sn, ye = class extends L {
|
|
1842
|
-
// used in isInstance
|
|
1843
|
-
constructor({ message: e }) {
|
|
1844
|
-
super({ name: rn, message: e }), this[sn] = !0;
|
|
1845
|
-
}
|
|
1846
|
-
static isInstance(e) {
|
|
1847
|
-
return L.hasMarker(e, on);
|
|
1848
|
-
}
|
|
1849
|
-
};
|
|
1850
|
-
sn = uo;
|
|
1851
|
-
var an = "AI_NoContentGeneratedError", ln = `vercel.ai.error.${an}`, po = Symbol.for(ln), cn, un = class extends L {
|
|
1852
|
-
// used in isInstance
|
|
1853
|
-
constructor({
|
|
1854
|
-
message: e = "No content generated."
|
|
1855
|
-
} = {}) {
|
|
1856
|
-
super({ name: an, message: e }), this[cn] = !0;
|
|
1857
|
-
}
|
|
1858
|
-
static isInstance(e) {
|
|
1859
|
-
return L.hasMarker(e, ln);
|
|
1860
|
-
}
|
|
1861
|
-
};
|
|
1862
|
-
cn = po;
|
|
1863
|
-
var dn = "AI_TypeValidationError", pn = `vercel.ai.error.${dn}`, ho = Symbol.for(pn), hn, mo = class Le extends L {
|
|
1864
|
-
constructor({ value: t, cause: n }) {
|
|
1865
|
-
super({
|
|
1866
|
-
name: dn,
|
|
1867
|
-
message: `Type validation failed: Value: ${JSON.stringify(t)}.
|
|
1868
|
-
Error message: ${Bt(n)}`,
|
|
1869
|
-
cause: n
|
|
1870
|
-
}), this[hn] = !0, this.value = t;
|
|
1871
|
-
}
|
|
1872
|
-
static isInstance(t) {
|
|
1873
|
-
return L.hasMarker(t, pn);
|
|
1874
|
-
}
|
|
1875
|
-
/**
|
|
1876
|
-
* Wraps an error into a TypeValidationError.
|
|
1877
|
-
* If the cause is already a TypeValidationError with the same value, it returns the cause.
|
|
1878
|
-
* Otherwise, it creates a new TypeValidationError.
|
|
1879
|
-
*
|
|
1880
|
-
* @param {Object} params - The parameters for wrapping the error.
|
|
1881
|
-
* @param {unknown} params.value - The value that failed validation.
|
|
1882
|
-
* @param {unknown} params.cause - The original error or cause of the validation failure.
|
|
1883
|
-
* @returns {TypeValidationError} A TypeValidationError instance.
|
|
1884
|
-
*/
|
|
1885
|
-
static wrap({
|
|
1886
|
-
value: t,
|
|
1887
|
-
cause: n
|
|
1888
|
-
}) {
|
|
1889
|
-
return Le.isInstance(n) && n.value === t ? n : new Le({ value: t, cause: n });
|
|
1890
|
-
}
|
|
1891
|
-
};
|
|
1892
|
-
hn = ho;
|
|
1893
|
-
var he = mo, mn = "AI_UnsupportedFunctionalityError", fn = `vercel.ai.error.${mn}`, fo = Symbol.for(fn), gn, te = class extends L {
|
|
1894
|
-
constructor({
|
|
1895
|
-
functionality: e,
|
|
1896
|
-
message: t = `'${e}' functionality not supported.`
|
|
1897
|
-
}) {
|
|
1898
|
-
super({ name: mn, message: t }), this[gn] = !0, this.functionality = e;
|
|
1899
|
-
}
|
|
1900
|
-
static isInstance(e) {
|
|
1901
|
-
return L.hasMarker(e, fn);
|
|
1902
|
-
}
|
|
1903
|
-
};
|
|
1904
|
-
gn = fo;
|
|
1905
|
-
var dt = class extends Error {
|
|
1906
|
-
constructor(e, t) {
|
|
1907
|
-
super(e), this.name = "ParseError", this.type = t.type, this.field = t.field, this.value = t.value, this.line = t.line;
|
|
1908
|
-
}
|
|
1909
|
-
};
|
|
1910
|
-
function Ne(e) {
|
|
1911
|
-
}
|
|
1912
|
-
function go(e) {
|
|
1913
|
-
if (typeof e == "function")
|
|
1914
|
-
throw new TypeError(
|
|
1915
|
-
"`callbacks` must be an object, got a function instead. Did you mean `{onEvent: fn}`?"
|
|
1916
|
-
);
|
|
1917
|
-
const { onEvent: t = Ne, onError: n = Ne, onRetry: r = Ne, onComment: o } = e;
|
|
1918
|
-
let a = "", i = !0, c, l = "", u = "";
|
|
1919
|
-
function h(p) {
|
|
1920
|
-
const w = i ? p.replace(/^\xEF\xBB\xBF/, "") : p, [b, R] = _o(`${a}${w}`);
|
|
1921
|
-
for (const T of b)
|
|
1922
|
-
g(T);
|
|
1923
|
-
a = R, i = !1;
|
|
1924
|
-
}
|
|
1925
|
-
function g(p) {
|
|
1926
|
-
if (p === "") {
|
|
1927
|
-
v();
|
|
1928
|
-
return;
|
|
1929
|
-
}
|
|
1930
|
-
if (p.startsWith(":")) {
|
|
1931
|
-
o && o(p.slice(p.startsWith(": ") ? 2 : 1));
|
|
1932
|
-
return;
|
|
1933
|
-
}
|
|
1934
|
-
const w = p.indexOf(":");
|
|
1935
|
-
if (w !== -1) {
|
|
1936
|
-
const b = p.slice(0, w), R = p[w + 1] === " " ? 2 : 1, T = p.slice(w + R);
|
|
1937
|
-
f(b, T, p);
|
|
1938
|
-
return;
|
|
1939
|
-
}
|
|
1940
|
-
f(p, "", p);
|
|
1941
|
-
}
|
|
1942
|
-
function f(p, w, b) {
|
|
1943
|
-
switch (p) {
|
|
1944
|
-
case "event":
|
|
1945
|
-
u = w;
|
|
1946
|
-
break;
|
|
1947
|
-
case "data":
|
|
1948
|
-
l = `${l}${w}
|
|
1949
|
-
`;
|
|
1950
|
-
break;
|
|
1951
|
-
case "id":
|
|
1952
|
-
c = w.includes("\0") ? void 0 : w;
|
|
1953
|
-
break;
|
|
1954
|
-
case "retry":
|
|
1955
|
-
/^\d+$/.test(w) ? r(parseInt(w, 10)) : n(
|
|
1956
|
-
new dt(`Invalid \`retry\` value: "${w}"`, {
|
|
1957
|
-
type: "invalid-retry",
|
|
1958
|
-
value: w,
|
|
1959
|
-
line: b
|
|
1960
|
-
})
|
|
1961
|
-
);
|
|
1962
|
-
break;
|
|
1963
|
-
default:
|
|
1964
|
-
n(
|
|
1965
|
-
new dt(
|
|
1966
|
-
`Unknown field "${p.length > 20 ? `${p.slice(0, 20)}…` : p}"`,
|
|
1967
|
-
{ type: "unknown-field", field: p, value: w, line: b }
|
|
1968
|
-
)
|
|
1969
|
-
);
|
|
1970
|
-
break;
|
|
1971
|
-
}
|
|
1972
|
-
}
|
|
1973
|
-
function v() {
|
|
1974
|
-
l.length > 0 && t({
|
|
1975
|
-
id: c,
|
|
1976
|
-
event: u || void 0,
|
|
1977
|
-
// If the data buffer's last character is a U+000A LINE FEED (LF) character,
|
|
1978
|
-
// then remove the last character from the data buffer.
|
|
1979
|
-
data: l.endsWith(`
|
|
1980
|
-
`) ? l.slice(0, -1) : l
|
|
1981
|
-
}), c = void 0, l = "", u = "";
|
|
1982
|
-
}
|
|
1983
|
-
function m(p = {}) {
|
|
1984
|
-
a && p.consume && g(a), i = !0, c = void 0, l = "", u = "", a = "";
|
|
1985
|
-
}
|
|
1986
|
-
return { feed: h, reset: m };
|
|
1987
|
-
}
|
|
1988
|
-
function _o(e) {
|
|
1989
|
-
const t = [];
|
|
1990
|
-
let n = "", r = 0;
|
|
1991
|
-
for (; r < e.length; ) {
|
|
1992
|
-
const o = e.indexOf("\r", r), a = e.indexOf(`
|
|
1993
|
-
`, r);
|
|
1994
|
-
let i = -1;
|
|
1995
|
-
if (o !== -1 && a !== -1 ? i = Math.min(o, a) : o !== -1 ? i = o : a !== -1 && (i = a), i === -1) {
|
|
1996
|
-
n = e.slice(r);
|
|
1997
|
-
break;
|
|
1998
|
-
} else {
|
|
1999
|
-
const c = e.slice(r, i);
|
|
2000
|
-
t.push(c), r = i + 1, e[r - 1] === "\r" && e[r] === `
|
|
2001
|
-
` && r++;
|
|
2002
|
-
}
|
|
2003
|
-
}
|
|
2004
|
-
return [t, n];
|
|
2005
|
-
}
|
|
2006
|
-
var vo = class extends TransformStream {
|
|
2007
|
-
constructor({ onError: e, onRetry: t, onComment: n } = {}) {
|
|
2008
|
-
let r;
|
|
2009
|
-
super({
|
|
2010
|
-
start(o) {
|
|
2011
|
-
r = go({
|
|
2012
|
-
onEvent: (a) => {
|
|
2013
|
-
o.enqueue(a);
|
|
2014
|
-
},
|
|
2015
|
-
onError(a) {
|
|
2016
|
-
e === "terminate" ? o.error(a) : typeof e == "function" && e(a);
|
|
2017
|
-
},
|
|
2018
|
-
onRetry: t,
|
|
2019
|
-
onComment: n
|
|
2020
|
-
});
|
|
2021
|
-
},
|
|
2022
|
-
transform(o) {
|
|
2023
|
-
r.feed(o);
|
|
2024
|
-
}
|
|
2025
|
-
});
|
|
2026
|
-
}
|
|
2027
|
-
};
|
|
2028
|
-
new Set("ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvxyz0123456789");
|
|
2029
|
-
function me(...e) {
|
|
2030
|
-
return e.reduce(
|
|
2031
|
-
(t, n) => P(P({}, t), n ?? {}),
|
|
2032
|
-
{}
|
|
2033
|
-
);
|
|
2034
|
-
}
|
|
2035
|
-
function $e(e) {
|
|
2036
|
-
return Object.fromEntries([...e.headers]);
|
|
2037
|
-
}
|
|
2038
|
-
var yo = ({
|
|
2039
|
-
prefix: e,
|
|
2040
|
-
size: t = 16,
|
|
2041
|
-
alphabet: n = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
|
2042
|
-
separator: r = "-"
|
|
2043
|
-
} = {}) => {
|
|
2044
|
-
const o = () => {
|
|
2045
|
-
const a = n.length, i = new Array(t);
|
|
2046
|
-
for (let c = 0; c < t; c++)
|
|
2047
|
-
i[c] = n[Math.random() * a | 0];
|
|
2048
|
-
return i.join("");
|
|
2049
|
-
};
|
|
2050
|
-
if (e == null)
|
|
2051
|
-
return o;
|
|
2052
|
-
if (n.includes(r))
|
|
2053
|
-
throw new Wt({
|
|
2054
|
-
argument: "separator",
|
|
2055
|
-
message: `The separator "${r}" must not be part of the alphabet "${n}".`
|
|
2056
|
-
});
|
|
2057
|
-
return () => `${e}${r}${o()}`;
|
|
2058
|
-
}, W = yo();
|
|
2059
|
-
function qe(e) {
|
|
2060
|
-
return (e instanceof Error || e instanceof DOMException) && (e.name === "AbortError" || e.name === "ResponseAborted" || // Next.js
|
|
2061
|
-
e.name === "TimeoutError");
|
|
2062
|
-
}
|
|
2063
|
-
var wo = ["fetch failed", "failed to fetch"];
|
|
2064
|
-
function bo({
|
|
2065
|
-
error: e,
|
|
2066
|
-
url: t,
|
|
2067
|
-
requestBodyValues: n
|
|
2068
|
-
}) {
|
|
2069
|
-
if (qe(e))
|
|
2070
|
-
return e;
|
|
2071
|
-
if (e instanceof TypeError && wo.includes(e.message.toLowerCase())) {
|
|
2072
|
-
const r = e.cause;
|
|
2073
|
-
if (r != null)
|
|
2074
|
-
return new F({
|
|
2075
|
-
message: `Cannot connect to API: ${r.message}`,
|
|
2076
|
-
cause: r,
|
|
2077
|
-
url: t,
|
|
2078
|
-
requestBodyValues: n,
|
|
2079
|
-
isRetryable: !0
|
|
2080
|
-
// retry when network error
|
|
2081
|
-
});
|
|
2082
|
-
}
|
|
2083
|
-
return e;
|
|
2084
|
-
}
|
|
2085
|
-
function ko(e) {
|
|
2086
|
-
return Object.fromEntries(
|
|
2087
|
-
Object.entries(e).filter(([t, n]) => n != null)
|
|
2088
|
-
);
|
|
2089
|
-
}
|
|
2090
|
-
function To({
|
|
2091
|
-
apiKey: e,
|
|
2092
|
-
environmentVariableName: t,
|
|
2093
|
-
apiKeyParameterName: n = "apiKey",
|
|
2094
|
-
description: r
|
|
2095
|
-
}) {
|
|
2096
|
-
if (typeof e == "string")
|
|
2097
|
-
return e;
|
|
2098
|
-
if (e != null)
|
|
2099
|
-
throw new ye({
|
|
2100
|
-
message: `${r} API key must be a string.`
|
|
2101
|
-
});
|
|
2102
|
-
if (typeof process > "u")
|
|
2103
|
-
throw new ye({
|
|
2104
|
-
message: `${r} API key is missing. Pass it using the '${n}' parameter. Environment variables is not supported in this environment.`
|
|
2105
|
-
});
|
|
2106
|
-
if (e = process.env[t], e == null)
|
|
2107
|
-
throw new ye({
|
|
2108
|
-
message: `${r} API key is missing. Pass it using the '${n}' parameter or the ${t} environment variable.`
|
|
2109
|
-
});
|
|
2110
|
-
if (typeof e != "string")
|
|
2111
|
-
throw new ye({
|
|
2112
|
-
message: `${r} API key must be a string. The value of the ${t} environment variable is not a string.`
|
|
2113
|
-
});
|
|
2114
|
-
return e;
|
|
2115
|
-
}
|
|
2116
|
-
var So = /"__proto__"\s*:/, Ro = /"constructor"\s*:/;
|
|
2117
|
-
function Eo(e) {
|
|
2118
|
-
const t = JSON.parse(e);
|
|
2119
|
-
return t === null || typeof t != "object" || So.test(e) === !1 && Ro.test(e) === !1 ? t : Co(t);
|
|
2120
|
-
}
|
|
2121
|
-
function Co(e) {
|
|
2122
|
-
let t = [e];
|
|
2123
|
-
for (; t.length; ) {
|
|
2124
|
-
const n = t;
|
|
2125
|
-
t = [];
|
|
2126
|
-
for (const r of n) {
|
|
2127
|
-
if (Object.prototype.hasOwnProperty.call(r, "__proto__"))
|
|
2128
|
-
throw new SyntaxError("Object contains forbidden prototype property");
|
|
2129
|
-
if (Object.prototype.hasOwnProperty.call(r, "constructor") && Object.prototype.hasOwnProperty.call(r.constructor, "prototype"))
|
|
2130
|
-
throw new SyntaxError("Object contains forbidden prototype property");
|
|
2131
|
-
for (const o in r) {
|
|
2132
|
-
const a = r[o];
|
|
2133
|
-
a && typeof a == "object" && t.push(a);
|
|
2134
|
-
}
|
|
2135
|
-
}
|
|
2136
|
-
}
|
|
2137
|
-
return e;
|
|
2138
|
-
}
|
|
2139
|
-
function Xe(e) {
|
|
2140
|
-
const { stackTraceLimit: t } = Error;
|
|
2141
|
-
Error.stackTraceLimit = 0;
|
|
2142
|
-
try {
|
|
2143
|
-
return Eo(e);
|
|
2144
|
-
} finally {
|
|
2145
|
-
Error.stackTraceLimit = t;
|
|
2146
|
-
}
|
|
2147
|
-
}
|
|
2148
|
-
var Ve = Symbol.for("vercel.ai.validator");
|
|
2149
|
-
function xo(e) {
|
|
2150
|
-
return { [Ve]: !0, validate: e };
|
|
2151
|
-
}
|
|
2152
|
-
function Io(e) {
|
|
2153
|
-
return typeof e == "object" && e !== null && Ve in e && e[Ve] === !0 && "validate" in e;
|
|
2154
|
-
}
|
|
2155
|
-
function Oo(e) {
|
|
2156
|
-
return Io(e) ? e : Po(e);
|
|
2157
|
-
}
|
|
2158
|
-
function Po(e) {
|
|
2159
|
-
return xo(async (t) => {
|
|
2160
|
-
const n = await e["~standard"].validate(t);
|
|
2161
|
-
return n.issues == null ? { success: !0, value: n.value } : {
|
|
2162
|
-
success: !1,
|
|
2163
|
-
error: new he({
|
|
2164
|
-
value: t,
|
|
2165
|
-
cause: n.issues
|
|
2166
|
-
})
|
|
2167
|
-
};
|
|
2168
|
-
});
|
|
2169
|
-
}
|
|
2170
|
-
async function Ao({
|
|
2171
|
-
value: e,
|
|
2172
|
-
schema: t
|
|
2173
|
-
}) {
|
|
2174
|
-
const n = await _n({ value: e, schema: t });
|
|
2175
|
-
if (!n.success)
|
|
2176
|
-
throw he.wrap({ value: e, cause: n.error });
|
|
2177
|
-
return n.value;
|
|
2178
|
-
}
|
|
2179
|
-
async function _n({
|
|
2180
|
-
value: e,
|
|
2181
|
-
schema: t
|
|
2182
|
-
}) {
|
|
2183
|
-
const n = Oo(t);
|
|
2184
|
-
try {
|
|
2185
|
-
if (n.validate == null)
|
|
2186
|
-
return { success: !0, value: e, rawValue: e };
|
|
2187
|
-
const r = await n.validate(e);
|
|
2188
|
-
return r.success ? { success: !0, value: r.value, rawValue: e } : {
|
|
2189
|
-
success: !1,
|
|
2190
|
-
error: he.wrap({ value: e, cause: r.error }),
|
|
2191
|
-
rawValue: e
|
|
2192
|
-
};
|
|
2193
|
-
} catch (r) {
|
|
2194
|
-
return {
|
|
2195
|
-
success: !1,
|
|
2196
|
-
error: he.wrap({ value: e, cause: r }),
|
|
2197
|
-
rawValue: e
|
|
2198
|
-
};
|
|
2199
|
-
}
|
|
2200
|
-
}
|
|
2201
|
-
async function $o({
|
|
2202
|
-
text: e,
|
|
2203
|
-
schema: t
|
|
2204
|
-
}) {
|
|
2205
|
-
try {
|
|
2206
|
-
const n = Xe(e);
|
|
2207
|
-
return t == null ? n : Ao({ value: n, schema: t });
|
|
2208
|
-
} catch (n) {
|
|
2209
|
-
throw Ie.isInstance(n) || he.isInstance(n) ? n : new Ie({ text: e, cause: n });
|
|
2210
|
-
}
|
|
2211
|
-
}
|
|
2212
|
-
async function vn({
|
|
2213
|
-
text: e,
|
|
2214
|
-
schema: t
|
|
2215
|
-
}) {
|
|
2216
|
-
try {
|
|
2217
|
-
const n = Xe(e);
|
|
2218
|
-
return t == null ? { success: !0, value: n, rawValue: n } : await _n({ value: n, schema: t });
|
|
2219
|
-
} catch (n) {
|
|
2220
|
-
return {
|
|
2221
|
-
success: !1,
|
|
2222
|
-
error: Ie.isInstance(n) ? n : new Ie({ text: e, cause: n }),
|
|
2223
|
-
rawValue: void 0
|
|
2224
|
-
};
|
|
2225
|
-
}
|
|
2226
|
-
}
|
|
2227
|
-
function je(e) {
|
|
2228
|
-
try {
|
|
2229
|
-
return Xe(e), !0;
|
|
2230
|
-
} catch {
|
|
2231
|
-
return !1;
|
|
2232
|
-
}
|
|
2233
|
-
}
|
|
2234
|
-
function Uo({
|
|
2235
|
-
stream: e,
|
|
2236
|
-
schema: t
|
|
2237
|
-
}) {
|
|
2238
|
-
return e.pipeThrough(new TextDecoderStream()).pipeThrough(new vo()).pipeThrough(
|
|
2239
|
-
new TransformStream({
|
|
2240
|
-
async transform({ data: n }, r) {
|
|
2241
|
-
n !== "[DONE]" && r.enqueue(await vn({ text: n, schema: t }));
|
|
2242
|
-
}
|
|
2243
|
-
})
|
|
2244
|
-
);
|
|
2245
|
-
}
|
|
2246
|
-
var Mo = () => globalThis.fetch, fe = async ({
|
|
2247
|
-
url: e,
|
|
2248
|
-
headers: t,
|
|
2249
|
-
body: n,
|
|
2250
|
-
failedResponseHandler: r,
|
|
2251
|
-
successfulResponseHandler: o,
|
|
2252
|
-
abortSignal: a,
|
|
2253
|
-
fetch: i
|
|
2254
|
-
}) => No({
|
|
2255
|
-
url: e,
|
|
2256
|
-
headers: P({
|
|
2257
|
-
"Content-Type": "application/json"
|
|
2258
|
-
}, t),
|
|
2259
|
-
body: {
|
|
2260
|
-
content: JSON.stringify(n),
|
|
2261
|
-
values: n
|
|
2262
|
-
},
|
|
2263
|
-
failedResponseHandler: r,
|
|
2264
|
-
successfulResponseHandler: o,
|
|
2265
|
-
abortSignal: a,
|
|
2266
|
-
fetch: i
|
|
2267
|
-
}), No = async ({
|
|
2268
|
-
url: e,
|
|
2269
|
-
headers: t = {},
|
|
2270
|
-
body: n,
|
|
2271
|
-
successfulResponseHandler: r,
|
|
2272
|
-
failedResponseHandler: o,
|
|
2273
|
-
abortSignal: a,
|
|
2274
|
-
fetch: i = Mo()
|
|
2275
|
-
}) => {
|
|
2276
|
-
try {
|
|
2277
|
-
const c = await i(e, {
|
|
2278
|
-
method: "POST",
|
|
2279
|
-
headers: ko(t),
|
|
2280
|
-
body: n.content,
|
|
2281
|
-
signal: a
|
|
2282
|
-
}), l = $e(c);
|
|
2283
|
-
if (!c.ok) {
|
|
2284
|
-
let u;
|
|
2285
|
-
try {
|
|
2286
|
-
u = await o({
|
|
2287
|
-
response: c,
|
|
2288
|
-
url: e,
|
|
2289
|
-
requestBodyValues: n.values
|
|
2290
|
-
});
|
|
2291
|
-
} catch (h) {
|
|
2292
|
-
throw qe(h) || F.isInstance(h) ? h : new F({
|
|
2293
|
-
message: "Failed to process error response",
|
|
2294
|
-
cause: h,
|
|
2295
|
-
statusCode: c.status,
|
|
2296
|
-
url: e,
|
|
2297
|
-
responseHeaders: l,
|
|
2298
|
-
requestBodyValues: n.values
|
|
2299
|
-
});
|
|
2300
|
-
}
|
|
2301
|
-
throw u.value;
|
|
2302
|
-
}
|
|
2303
|
-
try {
|
|
2304
|
-
return await r({
|
|
2305
|
-
response: c,
|
|
2306
|
-
url: e,
|
|
2307
|
-
requestBodyValues: n.values
|
|
2308
|
-
});
|
|
2309
|
-
} catch (u) {
|
|
2310
|
-
throw u instanceof Error && (qe(u) || F.isInstance(u)) ? u : new F({
|
|
2311
|
-
message: "Failed to process successful response",
|
|
2312
|
-
cause: u,
|
|
2313
|
-
statusCode: c.status,
|
|
2314
|
-
url: e,
|
|
2315
|
-
responseHeaders: l,
|
|
2316
|
-
requestBodyValues: n.values
|
|
2317
|
-
});
|
|
2318
|
-
}
|
|
2319
|
-
} catch (c) {
|
|
2320
|
-
throw bo({ error: c, url: e, requestBodyValues: n.values });
|
|
2321
|
-
}
|
|
2322
|
-
}, jo = ({
|
|
2323
|
-
errorSchema: e,
|
|
2324
|
-
errorToMessage: t,
|
|
2325
|
-
isRetryable: n
|
|
2326
|
-
}) => async ({ response: r, url: o, requestBodyValues: a }) => {
|
|
2327
|
-
const i = await r.text(), c = $e(r);
|
|
2328
|
-
if (i.trim() === "")
|
|
2329
|
-
return {
|
|
2330
|
-
responseHeaders: c,
|
|
2331
|
-
value: new F({
|
|
2332
|
-
message: r.statusText,
|
|
2333
|
-
url: o,
|
|
2334
|
-
requestBodyValues: a,
|
|
2335
|
-
statusCode: r.status,
|
|
2336
|
-
responseHeaders: c,
|
|
2337
|
-
responseBody: i,
|
|
2338
|
-
isRetryable: n?.(r)
|
|
2339
|
-
})
|
|
2340
|
-
};
|
|
2341
|
-
try {
|
|
2342
|
-
const l = await $o({
|
|
2343
|
-
text: i,
|
|
2344
|
-
schema: e
|
|
2345
|
-
});
|
|
2346
|
-
return {
|
|
2347
|
-
responseHeaders: c,
|
|
2348
|
-
value: new F({
|
|
2349
|
-
message: t(l),
|
|
2350
|
-
url: o,
|
|
2351
|
-
requestBodyValues: a,
|
|
2352
|
-
statusCode: r.status,
|
|
2353
|
-
responseHeaders: c,
|
|
2354
|
-
responseBody: i,
|
|
2355
|
-
data: l,
|
|
2356
|
-
isRetryable: n?.(r, l)
|
|
2357
|
-
})
|
|
2358
|
-
};
|
|
2359
|
-
} catch {
|
|
2360
|
-
return {
|
|
2361
|
-
responseHeaders: c,
|
|
2362
|
-
value: new F({
|
|
2363
|
-
message: r.statusText,
|
|
2364
|
-
url: o,
|
|
2365
|
-
requestBodyValues: a,
|
|
2366
|
-
statusCode: r.status,
|
|
2367
|
-
responseHeaders: c,
|
|
2368
|
-
responseBody: i,
|
|
2369
|
-
isRetryable: n?.(r)
|
|
2370
|
-
})
|
|
2371
|
-
};
|
|
2372
|
-
}
|
|
2373
|
-
}, yn = (e) => async ({ response: t }) => {
|
|
2374
|
-
const n = $e(t);
|
|
2375
|
-
if (t.body == null)
|
|
2376
|
-
throw new oo({});
|
|
2377
|
-
return {
|
|
2378
|
-
responseHeaders: n,
|
|
2379
|
-
value: Uo({
|
|
2380
|
-
stream: t.body,
|
|
2381
|
-
schema: e
|
|
2382
|
-
})
|
|
2383
|
-
};
|
|
2384
|
-
}, Ye = (e) => async ({ response: t, url: n, requestBodyValues: r }) => {
|
|
2385
|
-
const o = await t.text(), a = await vn({
|
|
2386
|
-
text: o,
|
|
2387
|
-
schema: e
|
|
2388
|
-
}), i = $e(t);
|
|
2389
|
-
if (!a.success)
|
|
2390
|
-
throw new F({
|
|
2391
|
-
message: "Invalid JSON response",
|
|
2392
|
-
cause: a.error,
|
|
2393
|
-
statusCode: t.status,
|
|
2394
|
-
responseHeaders: i,
|
|
2395
|
-
responseBody: o,
|
|
2396
|
-
url: n,
|
|
2397
|
-
requestBodyValues: r
|
|
2398
|
-
});
|
|
2399
|
-
return {
|
|
2400
|
-
responseHeaders: i,
|
|
2401
|
-
value: a.value,
|
|
2402
|
-
rawValue: a.rawValue
|
|
2403
|
-
};
|
|
2404
|
-
}, { btoa: Do } = globalThis;
|
|
2405
|
-
function Ho(e) {
|
|
2406
|
-
let t = "";
|
|
2407
|
-
for (let n = 0; n < e.length; n++)
|
|
2408
|
-
t += String.fromCodePoint(e[n]);
|
|
2409
|
-
return Do(t);
|
|
2410
|
-
}
|
|
2411
|
-
function Lo(e) {
|
|
2412
|
-
return e?.replace(/\/$/, "");
|
|
2413
|
-
}
|
|
2414
|
-
function De(e) {
|
|
2415
|
-
return e != null;
|
|
2416
|
-
}
|
|
2417
|
-
var wn = /* @__PURE__ */ ((e) => (e.Unknown = "unknown", e.OpenAIResponsesV1 = "openai-responses-v1", e.XAIResponsesV1 = "xai-responses-v1", e.AnthropicClaudeV1 = "anthropic-claude-v1", e.GoogleGeminiV1 = "google-gemini-v1", e))(wn || {}), Qe = d({
|
|
2418
|
-
id: s().nullish(),
|
|
2419
|
-
format: Ge(wn).nullish(),
|
|
2420
|
-
index: k().optional()
|
|
2421
|
-
}).loose(), qo = d({
|
|
2422
|
-
type: x(
|
|
2423
|
-
"reasoning.summary"
|
|
2424
|
-
/* Summary */
|
|
2425
|
-
),
|
|
2426
|
-
summary: s()
|
|
2427
|
-
}).extend(Qe.shape), Vo = d({
|
|
2428
|
-
type: x(
|
|
2429
|
-
"reasoning.encrypted"
|
|
2430
|
-
/* Encrypted */
|
|
2431
|
-
),
|
|
2432
|
-
data: s()
|
|
2433
|
-
}).extend(Qe.shape), Bo = d({
|
|
2434
|
-
type: x(
|
|
2435
|
-
"reasoning.text"
|
|
2436
|
-
/* Text */
|
|
2437
|
-
),
|
|
2438
|
-
text: s().nullish(),
|
|
2439
|
-
signature: s().nullish()
|
|
2440
|
-
}).extend(Qe.shape), et = j([
|
|
2441
|
-
qo,
|
|
2442
|
-
Vo,
|
|
2443
|
-
Bo
|
|
2444
|
-
]), we = j([
|
|
2445
|
-
et,
|
|
2446
|
-
oe().transform(() => null)
|
|
2447
|
-
]), tt = y(we).transform((e) => e.filter((t) => !!t));
|
|
2448
|
-
j([
|
|
2449
|
-
d({
|
|
2450
|
-
delta: d({
|
|
2451
|
-
reasoning_details: y(we)
|
|
2452
|
-
})
|
|
2453
|
-
}).transform(
|
|
2454
|
-
(e) => e.delta.reasoning_details.filter(De)
|
|
2455
|
-
),
|
|
2456
|
-
d({
|
|
2457
|
-
message: d({
|
|
2458
|
-
reasoning_details: y(we)
|
|
2459
|
-
})
|
|
2460
|
-
}).transform(
|
|
2461
|
-
(e) => e.message.reasoning_details.filter(De)
|
|
2462
|
-
),
|
|
2463
|
-
d({
|
|
2464
|
-
text: s(),
|
|
2465
|
-
reasoning_details: y(we)
|
|
2466
|
-
}).transform((e) => e.reasoning_details.filter(De))
|
|
2467
|
-
]);
|
|
2468
|
-
var Ue = d({
|
|
2469
|
-
error: d({
|
|
2470
|
-
code: j([s(), k()]).nullable().optional().default(null),
|
|
2471
|
-
message: s(),
|
|
2472
|
-
type: s().nullable().optional().default(null),
|
|
2473
|
-
param: K().nullable().optional().default(null)
|
|
2474
|
-
}).passthrough()
|
|
2475
|
-
}).passthrough(), ge = jo({
|
|
2476
|
-
errorSchema: Ue,
|
|
2477
|
-
errorToMessage: (e) => e.error.message
|
|
2478
|
-
}), bn = d({
|
|
2479
|
-
type: x("file"),
|
|
2480
|
-
file: d({
|
|
2481
|
-
hash: s(),
|
|
2482
|
-
name: s(),
|
|
2483
|
-
content: y(
|
|
2484
|
-
d({
|
|
2485
|
-
type: s(),
|
|
2486
|
-
text: s().optional()
|
|
2487
|
-
}).catchall(K())
|
|
2488
|
-
).optional()
|
|
2489
|
-
}).catchall(K())
|
|
2490
|
-
}).catchall(K()), Jo = d({
|
|
2491
|
-
provider: s(),
|
|
2492
|
-
reasoning_details: y(et).optional(),
|
|
2493
|
-
annotations: y(bn).optional(),
|
|
2494
|
-
usage: d({
|
|
2495
|
-
promptTokens: k(),
|
|
2496
|
-
promptTokensDetails: d({
|
|
2497
|
-
cachedTokens: k()
|
|
2498
|
-
}).catchall(K()).optional(),
|
|
2499
|
-
completionTokens: k(),
|
|
2500
|
-
completionTokensDetails: d({
|
|
2501
|
-
reasoningTokens: k()
|
|
2502
|
-
}).catchall(K()).optional(),
|
|
2503
|
-
totalTokens: k(),
|
|
2504
|
-
cost: k().optional(),
|
|
2505
|
-
costDetails: d({
|
|
2506
|
-
upstreamInferenceCost: k()
|
|
2507
|
-
}).catchall(K()).optional()
|
|
2508
|
-
}).catchall(K())
|
|
2509
|
-
}).catchall(K()), pt = d({
|
|
2510
|
-
openrouter: d({
|
|
2511
|
-
reasoning_details: y(et).optional(),
|
|
2512
|
-
annotations: y(bn).optional()
|
|
2513
|
-
}).optional()
|
|
2514
|
-
}).optional();
|
|
2515
|
-
function Oe(e) {
|
|
2516
|
-
switch (e) {
|
|
2517
|
-
case "stop":
|
|
2518
|
-
return "stop";
|
|
2519
|
-
case "length":
|
|
2520
|
-
return "length";
|
|
2521
|
-
case "content_filter":
|
|
2522
|
-
return "content-filter";
|
|
2523
|
-
case "function_call":
|
|
2524
|
-
case "tool_calls":
|
|
2525
|
-
return "tool-calls";
|
|
2526
|
-
default:
|
|
2527
|
-
return "unknown";
|
|
2528
|
-
}
|
|
2529
|
-
}
|
|
2530
|
-
var zo = [
|
|
2531
|
-
"wav",
|
|
2532
|
-
"mp3",
|
|
2533
|
-
"aiff",
|
|
2534
|
-
"aac",
|
|
2535
|
-
"ogg",
|
|
2536
|
-
"flac",
|
|
2537
|
-
"m4a",
|
|
2538
|
-
"pcm16",
|
|
2539
|
-
"pcm24"
|
|
2540
|
-
];
|
|
2541
|
-
function nt({
|
|
2542
|
-
url: e,
|
|
2543
|
-
protocols: t
|
|
2544
|
-
}) {
|
|
2545
|
-
try {
|
|
2546
|
-
const n = new URL(e);
|
|
2547
|
-
return t.has(n.protocol);
|
|
2548
|
-
} catch {
|
|
2549
|
-
return !1;
|
|
2550
|
-
}
|
|
2551
|
-
}
|
|
2552
|
-
function Be({
|
|
2553
|
-
part: e,
|
|
2554
|
-
defaultMediaType: t
|
|
2555
|
-
}) {
|
|
2556
|
-
var n, r;
|
|
2557
|
-
if (e.data instanceof Uint8Array) {
|
|
2558
|
-
const a = Ho(e.data);
|
|
2559
|
-
return `data:${(n = e.mediaType) != null ? n : t};base64,${a}`;
|
|
2560
|
-
}
|
|
2561
|
-
const o = e.data.toString();
|
|
2562
|
-
return nt({
|
|
2563
|
-
url: o,
|
|
2564
|
-
protocols: /* @__PURE__ */ new Set(["http:", "https:"])
|
|
2565
|
-
}) || o.startsWith("data:") ? o : `data:${(r = e.mediaType) != null ? r : t};base64,${o}`;
|
|
2566
|
-
}
|
|
2567
|
-
function ht(e, t) {
|
|
2568
|
-
var n;
|
|
2569
|
-
const r = e.match(/^data:([^;]+)/);
|
|
2570
|
-
return r && (n = r[1]) != null ? n : t;
|
|
2571
|
-
}
|
|
2572
|
-
function Je(e) {
|
|
2573
|
-
const t = e.match(/^data:[^;]*;base64,(.+)$/);
|
|
2574
|
-
return t ? t[1] : e;
|
|
2575
|
-
}
|
|
2576
|
-
var Go = {
|
|
2577
|
-
// MP3 variants
|
|
2578
|
-
mpeg: "mp3",
|
|
2579
|
-
mp3: "mp3",
|
|
2580
|
-
// WAV variants
|
|
2581
|
-
"x-wav": "wav",
|
|
2582
|
-
wave: "wav",
|
|
2583
|
-
wav: "wav",
|
|
2584
|
-
// OGG variants
|
|
2585
|
-
ogg: "ogg",
|
|
2586
|
-
vorbis: "ogg",
|
|
2587
|
-
// AAC variants
|
|
2588
|
-
aac: "aac",
|
|
2589
|
-
"x-aac": "aac",
|
|
2590
|
-
// M4A variants
|
|
2591
|
-
m4a: "m4a",
|
|
2592
|
-
"x-m4a": "m4a",
|
|
2593
|
-
mp4: "m4a",
|
|
2594
|
-
// AIFF variants
|
|
2595
|
-
aiff: "aiff",
|
|
2596
|
-
"x-aiff": "aiff",
|
|
2597
|
-
// FLAC
|
|
2598
|
-
flac: "flac",
|
|
2599
|
-
"x-flac": "flac",
|
|
2600
|
-
// PCM variants
|
|
2601
|
-
pcm16: "pcm16",
|
|
2602
|
-
pcm24: "pcm24"
|
|
2603
|
-
};
|
|
2604
|
-
function Wo(e) {
|
|
2605
|
-
const t = Be({
|
|
2606
|
-
part: e,
|
|
2607
|
-
defaultMediaType: "audio/mpeg"
|
|
2608
25
|
});
|
|
2609
|
-
if (nt({
|
|
2610
|
-
url: t,
|
|
2611
|
-
protocols: /* @__PURE__ */ new Set(["http:", "https:"])
|
|
2612
|
-
}))
|
|
2613
|
-
throw new Error(
|
|
2614
|
-
`Audio files cannot be provided as URLs.
|
|
2615
|
-
|
|
2616
|
-
OpenRouter requires audio to be base64-encoded. Please:
|
|
2617
|
-
1. Download the audio file locally
|
|
2618
|
-
2. Read it as a Buffer or Uint8Array
|
|
2619
|
-
3. Pass it as the data parameter
|
|
2620
|
-
|
|
2621
|
-
The AI SDK will automatically handle base64 encoding.
|
|
2622
|
-
|
|
2623
|
-
Learn more: https://openrouter.ai/docs/features/multimodal/audio`
|
|
2624
|
-
);
|
|
2625
|
-
const n = Je(t), r = e.mediaType || "audio/mpeg", o = r.replace("audio/", ""), a = Go[o];
|
|
2626
|
-
if (a === void 0) {
|
|
2627
|
-
const i = zo.join(", ");
|
|
2628
|
-
throw new Error(
|
|
2629
|
-
`Unsupported audio format: "${r}"
|
|
2630
|
-
|
|
2631
|
-
OpenRouter supports the following audio formats: ${i}
|
|
2632
|
-
|
|
2633
|
-
Learn more: https://openrouter.ai/docs/features/multimodal/audio`
|
|
2634
|
-
);
|
|
2635
|
-
}
|
|
2636
|
-
return { data: n, format: a };
|
|
2637
|
-
}
|
|
2638
|
-
function ee(e) {
|
|
2639
|
-
var t, n, r;
|
|
2640
|
-
const o = e?.anthropic, a = e?.openrouter;
|
|
2641
|
-
return (r = (n = (t = a?.cacheControl) != null ? t : a?.cache_control) != null ? n : o?.cacheControl) != null ? r : o?.cache_control;
|
|
2642
|
-
}
|
|
2643
|
-
function Fo(e) {
|
|
2644
|
-
var t, n, r, o, a, i, c, l, u, h;
|
|
2645
|
-
const g = [];
|
|
2646
|
-
for (const { role: f, content: v, providerOptions: m } of e)
|
|
2647
|
-
switch (f) {
|
|
2648
|
-
case "system": {
|
|
2649
|
-
g.push({
|
|
2650
|
-
role: "system",
|
|
2651
|
-
content: v,
|
|
2652
|
-
cache_control: ee(m)
|
|
2653
|
-
});
|
|
2654
|
-
break;
|
|
2655
|
-
}
|
|
2656
|
-
case "user": {
|
|
2657
|
-
if (v.length === 1 && ((t = v[0]) == null ? void 0 : t.type) === "text") {
|
|
2658
|
-
const b = (n = ee(m)) != null ? n : ee(v[0].providerOptions), R = b ? [
|
|
2659
|
-
{
|
|
2660
|
-
type: "text",
|
|
2661
|
-
text: v[0].text,
|
|
2662
|
-
cache_control: b
|
|
2663
|
-
}
|
|
2664
|
-
] : v[0].text;
|
|
2665
|
-
g.push({
|
|
2666
|
-
role: "user",
|
|
2667
|
-
content: R
|
|
2668
|
-
});
|
|
2669
|
-
break;
|
|
2670
|
-
}
|
|
2671
|
-
const p = ee(m), w = v.map(
|
|
2672
|
-
(b) => {
|
|
2673
|
-
var R, T, S, M, $, A, I;
|
|
2674
|
-
const z = (R = ee(b.providerOptions)) != null ? R : p;
|
|
2675
|
-
switch (b.type) {
|
|
2676
|
-
case "text":
|
|
2677
|
-
return {
|
|
2678
|
-
type: "text",
|
|
2679
|
-
text: b.text,
|
|
2680
|
-
// For text parts, only use part-specific cache control
|
|
2681
|
-
cache_control: z
|
|
2682
|
-
};
|
|
2683
|
-
case "file": {
|
|
2684
|
-
if ((T = b.mediaType) != null && T.startsWith("image/"))
|
|
2685
|
-
return {
|
|
2686
|
-
type: "image_url",
|
|
2687
|
-
image_url: {
|
|
2688
|
-
url: Be({
|
|
2689
|
-
part: b,
|
|
2690
|
-
defaultMediaType: "image/jpeg"
|
|
2691
|
-
})
|
|
2692
|
-
},
|
|
2693
|
-
// For image parts, use part-specific or message-level cache control
|
|
2694
|
-
cache_control: z
|
|
2695
|
-
};
|
|
2696
|
-
if ((S = b.mediaType) != null && S.startsWith("audio/"))
|
|
2697
|
-
return {
|
|
2698
|
-
type: "input_audio",
|
|
2699
|
-
input_audio: Wo(b),
|
|
2700
|
-
cache_control: z
|
|
2701
|
-
};
|
|
2702
|
-
const J = String(
|
|
2703
|
-
(I = (A = ($ = (M = b.providerOptions) == null ? void 0 : M.openrouter) == null ? void 0 : $.filename) != null ? A : b.filename) != null ? I : ""
|
|
2704
|
-
), G = Be({
|
|
2705
|
-
part: b,
|
|
2706
|
-
defaultMediaType: "application/pdf"
|
|
2707
|
-
});
|
|
2708
|
-
return nt({
|
|
2709
|
-
url: G,
|
|
2710
|
-
protocols: /* @__PURE__ */ new Set(["http:", "https:"])
|
|
2711
|
-
}) ? {
|
|
2712
|
-
type: "file",
|
|
2713
|
-
file: {
|
|
2714
|
-
filename: J,
|
|
2715
|
-
file_data: G
|
|
2716
|
-
}
|
|
2717
|
-
} : {
|
|
2718
|
-
type: "file",
|
|
2719
|
-
file: {
|
|
2720
|
-
filename: J,
|
|
2721
|
-
file_data: G
|
|
2722
|
-
},
|
|
2723
|
-
cache_control: z
|
|
2724
|
-
};
|
|
2725
|
-
}
|
|
2726
|
-
default:
|
|
2727
|
-
return {
|
|
2728
|
-
type: "text",
|
|
2729
|
-
text: "",
|
|
2730
|
-
cache_control: z
|
|
2731
|
-
};
|
|
2732
|
-
}
|
|
2733
|
-
}
|
|
2734
|
-
);
|
|
2735
|
-
g.push({
|
|
2736
|
-
role: "user",
|
|
2737
|
-
content: w
|
|
2738
|
-
});
|
|
2739
|
-
break;
|
|
2740
|
-
}
|
|
2741
|
-
case "assistant": {
|
|
2742
|
-
let p = "", w = "";
|
|
2743
|
-
const b = [], R = [];
|
|
2744
|
-
for (const A of v)
|
|
2745
|
-
switch (A.type) {
|
|
2746
|
-
case "text": {
|
|
2747
|
-
p += A.text;
|
|
2748
|
-
break;
|
|
2749
|
-
}
|
|
2750
|
-
case "tool-call": {
|
|
2751
|
-
const I = (r = A.providerOptions) == null ? void 0 : r.openrouter;
|
|
2752
|
-
I?.reasoning_details && Array.isArray(I.reasoning_details) && R.push(
|
|
2753
|
-
...I.reasoning_details
|
|
2754
|
-
), b.push({
|
|
2755
|
-
id: A.toolCallId,
|
|
2756
|
-
type: "function",
|
|
2757
|
-
function: {
|
|
2758
|
-
name: A.toolName,
|
|
2759
|
-
arguments: JSON.stringify(A.input)
|
|
2760
|
-
}
|
|
2761
|
-
});
|
|
2762
|
-
break;
|
|
2763
|
-
}
|
|
2764
|
-
case "reasoning": {
|
|
2765
|
-
w += A.text;
|
|
2766
|
-
const I = pt.safeParse(A.providerOptions);
|
|
2767
|
-
I.success && ((a = (o = I.data) == null ? void 0 : o.openrouter) != null && a.reasoning_details) && R.push(
|
|
2768
|
-
...I.data.openrouter.reasoning_details
|
|
2769
|
-
);
|
|
2770
|
-
break;
|
|
2771
|
-
}
|
|
2772
|
-
}
|
|
2773
|
-
const T = pt.safeParse(m), S = T.success ? (c = (i = T.data) == null ? void 0 : i.openrouter) == null ? void 0 : c.reasoning_details : void 0, M = T.success ? (u = (l = T.data) == null ? void 0 : l.openrouter) == null ? void 0 : u.annotations : void 0, $ = S && Array.isArray(S) && S.length > 0 ? S : R.length > 0 ? R : void 0;
|
|
2774
|
-
g.push({
|
|
2775
|
-
role: "assistant",
|
|
2776
|
-
content: p,
|
|
2777
|
-
tool_calls: b.length > 0 ? b : void 0,
|
|
2778
|
-
reasoning: w || void 0,
|
|
2779
|
-
reasoning_details: $,
|
|
2780
|
-
annotations: M,
|
|
2781
|
-
cache_control: ee(m)
|
|
2782
|
-
});
|
|
2783
|
-
break;
|
|
2784
|
-
}
|
|
2785
|
-
case "tool": {
|
|
2786
|
-
for (const p of v) {
|
|
2787
|
-
const w = Ko(p);
|
|
2788
|
-
g.push({
|
|
2789
|
-
role: "tool",
|
|
2790
|
-
tool_call_id: p.toolCallId,
|
|
2791
|
-
content: w,
|
|
2792
|
-
cache_control: (h = ee(m)) != null ? h : ee(p.providerOptions)
|
|
2793
|
-
});
|
|
2794
|
-
}
|
|
2795
|
-
break;
|
|
2796
|
-
}
|
|
2797
|
-
}
|
|
2798
|
-
return g;
|
|
2799
|
-
}
|
|
2800
|
-
function Ko(e) {
|
|
2801
|
-
return e.output.type === "text" ? e.output.value : JSON.stringify(e.output.value);
|
|
2802
|
-
}
|
|
2803
|
-
j([
|
|
2804
|
-
x("auto"),
|
|
2805
|
-
x("none"),
|
|
2806
|
-
x("required"),
|
|
2807
|
-
d({
|
|
2808
|
-
type: x("function"),
|
|
2809
|
-
function: d({
|
|
2810
|
-
name: s()
|
|
2811
|
-
})
|
|
2812
|
-
})
|
|
2813
|
-
]);
|
|
2814
|
-
function Zo(e) {
|
|
2815
|
-
switch (e.type) {
|
|
2816
|
-
case "auto":
|
|
2817
|
-
case "none":
|
|
2818
|
-
case "required":
|
|
2819
|
-
return e.type;
|
|
2820
|
-
case "tool":
|
|
2821
|
-
return {
|
|
2822
|
-
type: "function",
|
|
2823
|
-
function: { name: e.toolName }
|
|
2824
|
-
};
|
|
2825
|
-
default:
|
|
2826
|
-
throw new Wt({
|
|
2827
|
-
argument: "toolChoice",
|
|
2828
|
-
message: `Invalid tool choice type: ${JSON.stringify(e)}`
|
|
2829
|
-
});
|
|
2830
|
-
}
|
|
2831
|
-
}
|
|
2832
|
-
var Xo = d({
|
|
2833
|
-
type: x("image_url"),
|
|
2834
|
-
image_url: d({
|
|
2835
|
-
url: s()
|
|
2836
|
-
}).passthrough()
|
|
2837
|
-
}).passthrough(), Yo = j([
|
|
2838
|
-
Xo,
|
|
2839
|
-
oe().transform(() => null)
|
|
2840
|
-
]), kn = y(Yo).transform((e) => e.filter((t) => !!t)), Tn = d({
|
|
2841
|
-
id: s().optional(),
|
|
2842
|
-
model: s().optional(),
|
|
2843
|
-
provider: s().optional(),
|
|
2844
|
-
usage: d({
|
|
2845
|
-
prompt_tokens: k(),
|
|
2846
|
-
prompt_tokens_details: d({
|
|
2847
|
-
cached_tokens: k()
|
|
2848
|
-
}).passthrough().nullish(),
|
|
2849
|
-
completion_tokens: k(),
|
|
2850
|
-
completion_tokens_details: d({
|
|
2851
|
-
reasoning_tokens: k()
|
|
2852
|
-
}).passthrough().nullish(),
|
|
2853
|
-
total_tokens: k(),
|
|
2854
|
-
cost: k().optional(),
|
|
2855
|
-
cost_details: d({
|
|
2856
|
-
upstream_inference_cost: k().nullish()
|
|
2857
|
-
}).passthrough().nullish()
|
|
2858
|
-
}).passthrough().nullish()
|
|
2859
|
-
}).passthrough(), Qo = j([
|
|
2860
|
-
// Success response with choices
|
|
2861
|
-
Tn.extend({
|
|
2862
|
-
choices: y(
|
|
2863
|
-
d({
|
|
2864
|
-
message: d({
|
|
2865
|
-
role: x("assistant"),
|
|
2866
|
-
content: s().nullable().optional(),
|
|
2867
|
-
reasoning: s().nullable().optional(),
|
|
2868
|
-
reasoning_details: tt.nullish(),
|
|
2869
|
-
images: kn.nullish(),
|
|
2870
|
-
tool_calls: y(
|
|
2871
|
-
d({
|
|
2872
|
-
id: s().optional().nullable(),
|
|
2873
|
-
type: x("function"),
|
|
2874
|
-
function: d({
|
|
2875
|
-
name: s(),
|
|
2876
|
-
arguments: s()
|
|
2877
|
-
}).passthrough()
|
|
2878
|
-
}).passthrough()
|
|
2879
|
-
).optional(),
|
|
2880
|
-
annotations: y(
|
|
2881
|
-
j([
|
|
2882
|
-
// URL citation from web search
|
|
2883
|
-
d({
|
|
2884
|
-
type: x("url_citation"),
|
|
2885
|
-
url_citation: d({
|
|
2886
|
-
end_index: k(),
|
|
2887
|
-
start_index: k(),
|
|
2888
|
-
title: s(),
|
|
2889
|
-
url: s(),
|
|
2890
|
-
content: s().optional()
|
|
2891
|
-
}).passthrough()
|
|
2892
|
-
}).passthrough(),
|
|
2893
|
-
// File annotation from FileParserPlugin (old format)
|
|
2894
|
-
d({
|
|
2895
|
-
type: x("file_annotation"),
|
|
2896
|
-
file_annotation: d({
|
|
2897
|
-
file_id: s(),
|
|
2898
|
-
quote: s().optional()
|
|
2899
|
-
}).passthrough()
|
|
2900
|
-
}).passthrough(),
|
|
2901
|
-
// File annotation from FileParserPlugin (new format)
|
|
2902
|
-
d({
|
|
2903
|
-
type: x("file"),
|
|
2904
|
-
file: d({
|
|
2905
|
-
hash: s(),
|
|
2906
|
-
name: s(),
|
|
2907
|
-
content: y(
|
|
2908
|
-
d({
|
|
2909
|
-
type: s(),
|
|
2910
|
-
text: s().optional()
|
|
2911
|
-
}).passthrough()
|
|
2912
|
-
).optional()
|
|
2913
|
-
}).passthrough()
|
|
2914
|
-
}).passthrough()
|
|
2915
|
-
])
|
|
2916
|
-
).nullish()
|
|
2917
|
-
}).passthrough(),
|
|
2918
|
-
index: k().nullish(),
|
|
2919
|
-
logprobs: d({
|
|
2920
|
-
content: y(
|
|
2921
|
-
d({
|
|
2922
|
-
token: s(),
|
|
2923
|
-
logprob: k(),
|
|
2924
|
-
top_logprobs: y(
|
|
2925
|
-
d({
|
|
2926
|
-
token: s(),
|
|
2927
|
-
logprob: k()
|
|
2928
|
-
}).passthrough()
|
|
2929
|
-
)
|
|
2930
|
-
}).passthrough()
|
|
2931
|
-
).nullable()
|
|
2932
|
-
}).passthrough().nullable().optional(),
|
|
2933
|
-
finish_reason: s().optional().nullable()
|
|
2934
|
-
}).passthrough()
|
|
2935
|
-
)
|
|
2936
|
-
}),
|
|
2937
|
-
// Error response (HTTP 200 with error payload)
|
|
2938
|
-
Ue.extend({
|
|
2939
|
-
user_id: s().optional()
|
|
2940
|
-
})
|
|
2941
|
-
]), es = j([
|
|
2942
|
-
Tn.extend({
|
|
2943
|
-
choices: y(
|
|
2944
|
-
d({
|
|
2945
|
-
delta: d({
|
|
2946
|
-
role: Ge(["assistant"]).optional(),
|
|
2947
|
-
content: s().nullish(),
|
|
2948
|
-
reasoning: s().nullish().optional(),
|
|
2949
|
-
reasoning_details: tt.nullish(),
|
|
2950
|
-
images: kn.nullish(),
|
|
2951
|
-
tool_calls: y(
|
|
2952
|
-
d({
|
|
2953
|
-
index: k().nullish(),
|
|
2954
|
-
id: s().nullish(),
|
|
2955
|
-
type: x("function").optional(),
|
|
2956
|
-
function: d({
|
|
2957
|
-
name: s().nullish(),
|
|
2958
|
-
arguments: s().nullish()
|
|
2959
|
-
}).passthrough()
|
|
2960
|
-
}).passthrough()
|
|
2961
|
-
).nullish(),
|
|
2962
|
-
annotations: y(
|
|
2963
|
-
j([
|
|
2964
|
-
// URL citation from web search
|
|
2965
|
-
d({
|
|
2966
|
-
type: x("url_citation"),
|
|
2967
|
-
url_citation: d({
|
|
2968
|
-
end_index: k(),
|
|
2969
|
-
start_index: k(),
|
|
2970
|
-
title: s(),
|
|
2971
|
-
url: s(),
|
|
2972
|
-
content: s().optional()
|
|
2973
|
-
}).passthrough()
|
|
2974
|
-
}).passthrough(),
|
|
2975
|
-
// File annotation from FileParserPlugin (old format)
|
|
2976
|
-
d({
|
|
2977
|
-
type: x("file_annotation"),
|
|
2978
|
-
file_annotation: d({
|
|
2979
|
-
file_id: s(),
|
|
2980
|
-
quote: s().optional()
|
|
2981
|
-
}).passthrough()
|
|
2982
|
-
}).passthrough(),
|
|
2983
|
-
// File annotation from FileParserPlugin (new format)
|
|
2984
|
-
d({
|
|
2985
|
-
type: x("file"),
|
|
2986
|
-
file: d({
|
|
2987
|
-
hash: s(),
|
|
2988
|
-
name: s(),
|
|
2989
|
-
content: y(
|
|
2990
|
-
d({
|
|
2991
|
-
type: s(),
|
|
2992
|
-
text: s().optional()
|
|
2993
|
-
}).passthrough()
|
|
2994
|
-
).optional()
|
|
2995
|
-
}).passthrough()
|
|
2996
|
-
}).passthrough()
|
|
2997
|
-
])
|
|
2998
|
-
).nullish()
|
|
2999
|
-
}).passthrough().nullish(),
|
|
3000
|
-
logprobs: d({
|
|
3001
|
-
content: y(
|
|
3002
|
-
d({
|
|
3003
|
-
token: s(),
|
|
3004
|
-
logprob: k(),
|
|
3005
|
-
top_logprobs: y(
|
|
3006
|
-
d({
|
|
3007
|
-
token: s(),
|
|
3008
|
-
logprob: k()
|
|
3009
|
-
}).passthrough()
|
|
3010
|
-
)
|
|
3011
|
-
}).passthrough()
|
|
3012
|
-
).nullable()
|
|
3013
|
-
}).passthrough().nullish(),
|
|
3014
|
-
finish_reason: s().nullable().optional(),
|
|
3015
|
-
index: k().nullish()
|
|
3016
|
-
}).passthrough()
|
|
3017
|
-
)
|
|
3018
|
-
}),
|
|
3019
|
-
Ue
|
|
3020
|
-
]), ts = class {
|
|
3021
|
-
constructor(e, t, n) {
|
|
3022
|
-
this.specificationVersion = "v2", this.provider = "openrouter", this.defaultObjectGenerationMode = "tool", this.supportsImageUrls = !0, this.supportedUrls = {
|
|
3023
|
-
"image/*": [
|
|
3024
|
-
/^data:image\/[a-zA-Z]+;base64,/,
|
|
3025
|
-
/^https?:\/\/.+\.(jpg|jpeg|png|gif|webp)$/i
|
|
3026
|
-
],
|
|
3027
|
-
// 'text/*': [/^data:text\//, /^https?:\/\/.+$/],
|
|
3028
|
-
"application/*": [/^data:application\//, /^https?:\/\/.+$/]
|
|
3029
|
-
}, this.modelId = e, this.settings = t, this.config = n;
|
|
3030
|
-
}
|
|
3031
|
-
getArgs({
|
|
3032
|
-
prompt: e,
|
|
3033
|
-
maxOutputTokens: t,
|
|
3034
|
-
temperature: n,
|
|
3035
|
-
topP: r,
|
|
3036
|
-
frequencyPenalty: o,
|
|
3037
|
-
presencePenalty: a,
|
|
3038
|
-
seed: i,
|
|
3039
|
-
stopSequences: c,
|
|
3040
|
-
responseFormat: l,
|
|
3041
|
-
topK: u,
|
|
3042
|
-
tools: h,
|
|
3043
|
-
toolChoice: g
|
|
3044
|
-
}) {
|
|
3045
|
-
var f;
|
|
3046
|
-
const v = P(P({
|
|
3047
|
-
// model id:
|
|
3048
|
-
model: this.modelId,
|
|
3049
|
-
models: this.settings.models,
|
|
3050
|
-
// model specific settings:
|
|
3051
|
-
logit_bias: this.settings.logitBias,
|
|
3052
|
-
logprobs: this.settings.logprobs === !0 || typeof this.settings.logprobs == "number" ? !0 : void 0,
|
|
3053
|
-
top_logprobs: typeof this.settings.logprobs == "number" ? this.settings.logprobs : typeof this.settings.logprobs == "boolean" && this.settings.logprobs ? 0 : void 0,
|
|
3054
|
-
user: this.settings.user,
|
|
3055
|
-
parallel_tool_calls: this.settings.parallelToolCalls,
|
|
3056
|
-
// standardized settings:
|
|
3057
|
-
max_tokens: t,
|
|
3058
|
-
temperature: n,
|
|
3059
|
-
top_p: r,
|
|
3060
|
-
frequency_penalty: o,
|
|
3061
|
-
presence_penalty: a,
|
|
3062
|
-
seed: i,
|
|
3063
|
-
stop: c,
|
|
3064
|
-
response_format: l?.type === "json" ? l.schema != null ? {
|
|
3065
|
-
type: "json_schema",
|
|
3066
|
-
json_schema: P({
|
|
3067
|
-
schema: l.schema,
|
|
3068
|
-
strict: !0,
|
|
3069
|
-
name: (f = l.name) != null ? f : "response"
|
|
3070
|
-
}, l.description && {
|
|
3071
|
-
description: l.description
|
|
3072
|
-
})
|
|
3073
|
-
} : { type: "json_object" } : void 0,
|
|
3074
|
-
top_k: u,
|
|
3075
|
-
// messages:
|
|
3076
|
-
messages: Fo(e),
|
|
3077
|
-
// OpenRouter specific settings:
|
|
3078
|
-
include_reasoning: this.settings.includeReasoning,
|
|
3079
|
-
reasoning: this.settings.reasoning,
|
|
3080
|
-
usage: this.settings.usage,
|
|
3081
|
-
// Web search settings:
|
|
3082
|
-
plugins: this.settings.plugins,
|
|
3083
|
-
web_search_options: this.settings.web_search_options,
|
|
3084
|
-
// Provider routing settings:
|
|
3085
|
-
provider: this.settings.provider,
|
|
3086
|
-
// Debug settings:
|
|
3087
|
-
debug: this.settings.debug
|
|
3088
|
-
}, this.config.extraBody), this.settings.extraBody);
|
|
3089
|
-
if (h && h.length > 0) {
|
|
3090
|
-
const m = h.filter(
|
|
3091
|
-
(p) => p.type === "function"
|
|
3092
|
-
).map((p) => ({
|
|
3093
|
-
type: "function",
|
|
3094
|
-
function: {
|
|
3095
|
-
name: p.name,
|
|
3096
|
-
description: p.description,
|
|
3097
|
-
parameters: p.inputSchema
|
|
3098
|
-
}
|
|
3099
|
-
}));
|
|
3100
|
-
return xe(P({}, v), {
|
|
3101
|
-
tools: m,
|
|
3102
|
-
tool_choice: g ? Zo(g) : void 0
|
|
3103
|
-
});
|
|
3104
|
-
}
|
|
3105
|
-
return v;
|
|
3106
|
-
}
|
|
3107
|
-
async doGenerate(e) {
|
|
3108
|
-
var t, n, r, o, a, i, c, l, u, h, g, f, v, m, p, w, b, R, T, S, M, $, A;
|
|
3109
|
-
const z = (e.providerOptions || {}).openrouter || {}, J = P(P({}, this.getArgs(e)), z), { value: G, responseHeaders: se } = await fe({
|
|
3110
|
-
url: this.config.url({
|
|
3111
|
-
path: "/chat/completions",
|
|
3112
|
-
modelId: this.modelId
|
|
3113
|
-
}),
|
|
3114
|
-
headers: me(this.config.headers(), e.headers),
|
|
3115
|
-
body: J,
|
|
3116
|
-
failedResponseHandler: ge,
|
|
3117
|
-
successfulResponseHandler: Ye(
|
|
3118
|
-
Qo
|
|
3119
|
-
),
|
|
3120
|
-
abortSignal: e.abortSignal,
|
|
3121
|
-
fetch: this.config.fetch
|
|
3122
|
-
});
|
|
3123
|
-
if ("error" in G) {
|
|
3124
|
-
const _ = G.error;
|
|
3125
|
-
throw new F({
|
|
3126
|
-
message: _.message,
|
|
3127
|
-
url: this.config.url({
|
|
3128
|
-
path: "/chat/completions",
|
|
3129
|
-
modelId: this.modelId
|
|
3130
|
-
}),
|
|
3131
|
-
requestBodyValues: J,
|
|
3132
|
-
statusCode: 200,
|
|
3133
|
-
responseHeaders: se,
|
|
3134
|
-
data: _
|
|
3135
|
-
});
|
|
3136
|
-
}
|
|
3137
|
-
const D = G, H = D.choices[0];
|
|
3138
|
-
if (!H)
|
|
3139
|
-
throw new un({
|
|
3140
|
-
message: "No choice in response"
|
|
3141
|
-
});
|
|
3142
|
-
const ne = D.usage ? {
|
|
3143
|
-
inputTokens: (t = D.usage.prompt_tokens) != null ? t : 0,
|
|
3144
|
-
outputTokens: (n = D.usage.completion_tokens) != null ? n : 0,
|
|
3145
|
-
totalTokens: ((r = D.usage.prompt_tokens) != null ? r : 0) + ((o = D.usage.completion_tokens) != null ? o : 0),
|
|
3146
|
-
reasoningTokens: (i = (a = D.usage.completion_tokens_details) == null ? void 0 : a.reasoning_tokens) != null ? i : 0,
|
|
3147
|
-
cachedInputTokens: (l = (c = D.usage.prompt_tokens_details) == null ? void 0 : c.cached_tokens) != null ? l : 0
|
|
3148
|
-
} : {
|
|
3149
|
-
inputTokens: 0,
|
|
3150
|
-
outputTokens: 0,
|
|
3151
|
-
totalTokens: 0,
|
|
3152
|
-
reasoningTokens: 0,
|
|
3153
|
-
cachedInputTokens: 0
|
|
3154
|
-
}, re = (u = H.message.reasoning_details) != null ? u : [], ve = re.length > 0 ? re.map((_) => {
|
|
3155
|
-
switch (_.type) {
|
|
3156
|
-
case "reasoning.text": {
|
|
3157
|
-
if (_.text)
|
|
3158
|
-
return {
|
|
3159
|
-
type: "reasoning",
|
|
3160
|
-
text: _.text,
|
|
3161
|
-
providerMetadata: {
|
|
3162
|
-
openrouter: {
|
|
3163
|
-
reasoning_details: [_]
|
|
3164
|
-
}
|
|
3165
|
-
}
|
|
3166
|
-
};
|
|
3167
|
-
break;
|
|
3168
|
-
}
|
|
3169
|
-
case "reasoning.summary": {
|
|
3170
|
-
if (_.summary)
|
|
3171
|
-
return {
|
|
3172
|
-
type: "reasoning",
|
|
3173
|
-
text: _.summary,
|
|
3174
|
-
providerMetadata: {
|
|
3175
|
-
openrouter: {
|
|
3176
|
-
reasoning_details: [_]
|
|
3177
|
-
}
|
|
3178
|
-
}
|
|
3179
|
-
};
|
|
3180
|
-
break;
|
|
3181
|
-
}
|
|
3182
|
-
case "reasoning.encrypted": {
|
|
3183
|
-
if (_.data)
|
|
3184
|
-
return {
|
|
3185
|
-
type: "reasoning",
|
|
3186
|
-
text: "[REDACTED]",
|
|
3187
|
-
providerMetadata: {
|
|
3188
|
-
openrouter: {
|
|
3189
|
-
reasoning_details: [_]
|
|
3190
|
-
}
|
|
3191
|
-
}
|
|
3192
|
-
};
|
|
3193
|
-
break;
|
|
3194
|
-
}
|
|
3195
|
-
}
|
|
3196
|
-
return null;
|
|
3197
|
-
}).filter((_) => _ !== null) : H.message.reasoning ? [
|
|
3198
|
-
{
|
|
3199
|
-
type: "reasoning",
|
|
3200
|
-
text: H.message.reasoning
|
|
3201
|
-
}
|
|
3202
|
-
] : [], Z = [];
|
|
3203
|
-
if (Z.push(...ve), H.message.content && Z.push({
|
|
3204
|
-
type: "text",
|
|
3205
|
-
text: H.message.content
|
|
3206
|
-
}), H.message.tool_calls)
|
|
3207
|
-
for (const _ of H.message.tool_calls)
|
|
3208
|
-
Z.push({
|
|
3209
|
-
type: "tool-call",
|
|
3210
|
-
toolCallId: (h = _.id) != null ? h : W(),
|
|
3211
|
-
toolName: _.function.name,
|
|
3212
|
-
input: _.function.arguments,
|
|
3213
|
-
providerMetadata: {
|
|
3214
|
-
openrouter: {
|
|
3215
|
-
reasoning_details: re
|
|
3216
|
-
}
|
|
3217
|
-
}
|
|
3218
|
-
});
|
|
3219
|
-
if (H.message.images)
|
|
3220
|
-
for (const _ of H.message.images)
|
|
3221
|
-
Z.push({
|
|
3222
|
-
type: "file",
|
|
3223
|
-
mediaType: ht(_.image_url.url, "image/jpeg"),
|
|
3224
|
-
data: Je(_.image_url.url)
|
|
3225
|
-
});
|
|
3226
|
-
if (H.message.annotations)
|
|
3227
|
-
for (const _ of H.message.annotations)
|
|
3228
|
-
_.type === "url_citation" && Z.push({
|
|
3229
|
-
type: "source",
|
|
3230
|
-
sourceType: "url",
|
|
3231
|
-
id: _.url_citation.url,
|
|
3232
|
-
url: _.url_citation.url,
|
|
3233
|
-
title: _.url_citation.title,
|
|
3234
|
-
providerMetadata: {
|
|
3235
|
-
openrouter: {
|
|
3236
|
-
content: _.url_citation.content || ""
|
|
3237
|
-
}
|
|
3238
|
-
}
|
|
3239
|
-
});
|
|
3240
|
-
const ae = (g = H.message.annotations) == null ? void 0 : g.filter(
|
|
3241
|
-
(_) => _.type === "file"
|
|
3242
|
-
), U = H.message.tool_calls && H.message.tool_calls.length > 0, Q = re.some(
|
|
3243
|
-
(_) => _.type === "reasoning.encrypted" && _.data
|
|
3244
|
-
), ie = U && Q && H.finish_reason === "stop" ? "tool-calls" : Oe(H.finish_reason);
|
|
3245
|
-
return {
|
|
3246
|
-
content: Z,
|
|
3247
|
-
finishReason: ie,
|
|
3248
|
-
usage: ne,
|
|
3249
|
-
warnings: [],
|
|
3250
|
-
providerMetadata: {
|
|
3251
|
-
openrouter: Jo.parse({
|
|
3252
|
-
provider: (f = D.provider) != null ? f : "",
|
|
3253
|
-
reasoning_details: (v = H.message.reasoning_details) != null ? v : [],
|
|
3254
|
-
annotations: ae && ae.length > 0 ? ae : void 0,
|
|
3255
|
-
usage: P(P(P({
|
|
3256
|
-
promptTokens: (m = ne.inputTokens) != null ? m : 0,
|
|
3257
|
-
completionTokens: (p = ne.outputTokens) != null ? p : 0,
|
|
3258
|
-
totalTokens: (w = ne.totalTokens) != null ? w : 0,
|
|
3259
|
-
cost: (b = D.usage) == null ? void 0 : b.cost
|
|
3260
|
-
}, ((T = (R = D.usage) == null ? void 0 : R.prompt_tokens_details) == null ? void 0 : T.cached_tokens) != null ? {
|
|
3261
|
-
promptTokensDetails: {
|
|
3262
|
-
cachedTokens: D.usage.prompt_tokens_details.cached_tokens
|
|
3263
|
-
}
|
|
3264
|
-
} : {}), ((M = (S = D.usage) == null ? void 0 : S.completion_tokens_details) == null ? void 0 : M.reasoning_tokens) != null ? {
|
|
3265
|
-
completionTokensDetails: {
|
|
3266
|
-
reasoningTokens: D.usage.completion_tokens_details.reasoning_tokens
|
|
3267
|
-
}
|
|
3268
|
-
} : {}), ((A = ($ = D.usage) == null ? void 0 : $.cost_details) == null ? void 0 : A.upstream_inference_cost) != null ? {
|
|
3269
|
-
costDetails: {
|
|
3270
|
-
upstreamInferenceCost: D.usage.cost_details.upstream_inference_cost
|
|
3271
|
-
}
|
|
3272
|
-
} : {})
|
|
3273
|
-
})
|
|
3274
|
-
},
|
|
3275
|
-
request: { body: J },
|
|
3276
|
-
response: {
|
|
3277
|
-
id: D.id,
|
|
3278
|
-
modelId: D.model,
|
|
3279
|
-
headers: se
|
|
3280
|
-
}
|
|
3281
|
-
};
|
|
3282
|
-
}
|
|
3283
|
-
async doStream(e) {
|
|
3284
|
-
var t;
|
|
3285
|
-
const r = (e.providerOptions || {}).openrouter || {}, o = P(P({}, this.getArgs(e)), r), { value: a, responseHeaders: i } = await fe({
|
|
3286
|
-
url: this.config.url({
|
|
3287
|
-
path: "/chat/completions",
|
|
3288
|
-
modelId: this.modelId
|
|
3289
|
-
}),
|
|
3290
|
-
headers: me(this.config.headers(), e.headers),
|
|
3291
|
-
body: xe(P({}, o), {
|
|
3292
|
-
stream: !0,
|
|
3293
|
-
// only include stream_options when in strict compatibility mode:
|
|
3294
|
-
stream_options: this.config.compatibility === "strict" ? P({
|
|
3295
|
-
include_usage: !0
|
|
3296
|
-
}, (t = this.settings.usage) != null && t.include ? { include_usage: !0 } : {}) : void 0
|
|
3297
|
-
}),
|
|
3298
|
-
failedResponseHandler: ge,
|
|
3299
|
-
successfulResponseHandler: yn(
|
|
3300
|
-
es
|
|
3301
|
-
),
|
|
3302
|
-
abortSignal: e.abortSignal,
|
|
3303
|
-
fetch: this.config.fetch
|
|
3304
|
-
}), c = [];
|
|
3305
|
-
let l = "other";
|
|
3306
|
-
const u = {
|
|
3307
|
-
inputTokens: Number.NaN,
|
|
3308
|
-
outputTokens: Number.NaN,
|
|
3309
|
-
totalTokens: Number.NaN,
|
|
3310
|
-
reasoningTokens: Number.NaN,
|
|
3311
|
-
cachedInputTokens: Number.NaN
|
|
3312
|
-
}, h = {}, g = [], f = [];
|
|
3313
|
-
let v = !1, m = !1, p, w, b, R;
|
|
3314
|
-
return {
|
|
3315
|
-
stream: a.pipeThrough(
|
|
3316
|
-
new TransformStream({
|
|
3317
|
-
transform(T, S) {
|
|
3318
|
-
var M, $, A, I, z, J, G, se, D, H, ne, re, ve, Z, ae;
|
|
3319
|
-
if (!T.success) {
|
|
3320
|
-
l = "error", S.enqueue({ type: "error", error: T.error });
|
|
3321
|
-
return;
|
|
3322
|
-
}
|
|
3323
|
-
const U = T.value;
|
|
3324
|
-
if ("error" in U) {
|
|
3325
|
-
l = "error", S.enqueue({ type: "error", error: U.error });
|
|
3326
|
-
return;
|
|
3327
|
-
}
|
|
3328
|
-
if (U.provider && (R = U.provider), U.id && (b = U.id, S.enqueue({
|
|
3329
|
-
type: "response-metadata",
|
|
3330
|
-
id: U.id
|
|
3331
|
-
})), U.model && S.enqueue({
|
|
3332
|
-
type: "response-metadata",
|
|
3333
|
-
modelId: U.model
|
|
3334
|
-
}), U.usage != null) {
|
|
3335
|
-
if (u.inputTokens = U.usage.prompt_tokens, u.outputTokens = U.usage.completion_tokens, u.totalTokens = U.usage.prompt_tokens + U.usage.completion_tokens, h.promptTokens = U.usage.prompt_tokens, U.usage.prompt_tokens_details) {
|
|
3336
|
-
const C = (M = U.usage.prompt_tokens_details.cached_tokens) != null ? M : 0;
|
|
3337
|
-
u.cachedInputTokens = C, h.promptTokensDetails = {
|
|
3338
|
-
cachedTokens: C
|
|
3339
|
-
};
|
|
3340
|
-
}
|
|
3341
|
-
if (h.completionTokens = U.usage.completion_tokens, U.usage.completion_tokens_details) {
|
|
3342
|
-
const C = ($ = U.usage.completion_tokens_details.reasoning_tokens) != null ? $ : 0;
|
|
3343
|
-
u.reasoningTokens = C, h.completionTokensDetails = {
|
|
3344
|
-
reasoningTokens: C
|
|
3345
|
-
};
|
|
3346
|
-
}
|
|
3347
|
-
h.cost = U.usage.cost, h.totalTokens = U.usage.total_tokens;
|
|
3348
|
-
const _ = (A = U.usage.cost_details) == null ? void 0 : A.upstream_inference_cost;
|
|
3349
|
-
_ != null && (h.costDetails = {
|
|
3350
|
-
upstreamInferenceCost: _
|
|
3351
|
-
});
|
|
3352
|
-
}
|
|
3353
|
-
const Q = U.choices[0];
|
|
3354
|
-
if (Q?.finish_reason != null && (l = Oe(Q.finish_reason)), Q?.delta == null)
|
|
3355
|
-
return;
|
|
3356
|
-
const q = Q.delta, ie = (_, C) => {
|
|
3357
|
-
m || (w = b || W(), S.enqueue({
|
|
3358
|
-
providerMetadata: C,
|
|
3359
|
-
type: "reasoning-start",
|
|
3360
|
-
id: w
|
|
3361
|
-
}), m = !0), S.enqueue({
|
|
3362
|
-
providerMetadata: C,
|
|
3363
|
-
type: "reasoning-delta",
|
|
3364
|
-
delta: _,
|
|
3365
|
-
id: w || W()
|
|
3366
|
-
});
|
|
3367
|
-
};
|
|
3368
|
-
if (q.reasoning_details && q.reasoning_details.length > 0) {
|
|
3369
|
-
for (const C of q.reasoning_details)
|
|
3370
|
-
if (C.type === "reasoning.text") {
|
|
3371
|
-
const N = g[g.length - 1];
|
|
3372
|
-
N?.type === "reasoning.text" ? (N.text = (N.text || "") + (C.text || ""), N.signature = N.signature || C.signature, N.format = N.format || C.format) : g.push(P({}, C));
|
|
3373
|
-
} else
|
|
3374
|
-
g.push(C);
|
|
3375
|
-
const _ = {
|
|
3376
|
-
openrouter: {
|
|
3377
|
-
reasoning_details: q.reasoning_details
|
|
3378
|
-
}
|
|
3379
|
-
};
|
|
3380
|
-
for (const C of q.reasoning_details)
|
|
3381
|
-
switch (C.type) {
|
|
3382
|
-
case "reasoning.text": {
|
|
3383
|
-
C.text && ie(C.text, _);
|
|
3384
|
-
break;
|
|
3385
|
-
}
|
|
3386
|
-
case "reasoning.encrypted": {
|
|
3387
|
-
C.data && ie("[REDACTED]", _);
|
|
3388
|
-
break;
|
|
3389
|
-
}
|
|
3390
|
-
case "reasoning.summary": {
|
|
3391
|
-
C.summary && ie(C.summary, _);
|
|
3392
|
-
break;
|
|
3393
|
-
}
|
|
3394
|
-
}
|
|
3395
|
-
} else q.reasoning && ie(q.reasoning);
|
|
3396
|
-
if (q.content && (m && !v && (S.enqueue({
|
|
3397
|
-
type: "reasoning-end",
|
|
3398
|
-
id: w || W()
|
|
3399
|
-
}), m = !1), v || (p = b || W(), S.enqueue({
|
|
3400
|
-
type: "text-start",
|
|
3401
|
-
id: p
|
|
3402
|
-
}), v = !0), S.enqueue({
|
|
3403
|
-
type: "text-delta",
|
|
3404
|
-
delta: q.content,
|
|
3405
|
-
id: p || W()
|
|
3406
|
-
})), q.annotations) {
|
|
3407
|
-
for (const _ of q.annotations)
|
|
3408
|
-
if (_.type === "url_citation")
|
|
3409
|
-
S.enqueue({
|
|
3410
|
-
type: "source",
|
|
3411
|
-
sourceType: "url",
|
|
3412
|
-
id: _.url_citation.url,
|
|
3413
|
-
url: _.url_citation.url,
|
|
3414
|
-
title: _.url_citation.title,
|
|
3415
|
-
providerMetadata: {
|
|
3416
|
-
openrouter: {
|
|
3417
|
-
content: _.url_citation.content || ""
|
|
3418
|
-
}
|
|
3419
|
-
}
|
|
3420
|
-
});
|
|
3421
|
-
else if (_.type === "file") {
|
|
3422
|
-
const C = _.file;
|
|
3423
|
-
C && typeof C == "object" && "hash" in C && "name" in C && f.push(
|
|
3424
|
-
_
|
|
3425
|
-
);
|
|
3426
|
-
}
|
|
3427
|
-
}
|
|
3428
|
-
if (q.tool_calls != null)
|
|
3429
|
-
for (const _ of q.tool_calls) {
|
|
3430
|
-
const C = (I = _.index) != null ? I : c.length - 1;
|
|
3431
|
-
if (c[C] == null) {
|
|
3432
|
-
if (_.type !== "function")
|
|
3433
|
-
throw new de({
|
|
3434
|
-
data: _,
|
|
3435
|
-
message: "Expected 'function' type."
|
|
3436
|
-
});
|
|
3437
|
-
if (_.id == null)
|
|
3438
|
-
throw new de({
|
|
3439
|
-
data: _,
|
|
3440
|
-
message: "Expected 'id' to be a string."
|
|
3441
|
-
});
|
|
3442
|
-
if (((z = _.function) == null ? void 0 : z.name) == null)
|
|
3443
|
-
throw new de({
|
|
3444
|
-
data: _,
|
|
3445
|
-
message: "Expected 'function.name' to be a string."
|
|
3446
|
-
});
|
|
3447
|
-
c[C] = {
|
|
3448
|
-
id: _.id,
|
|
3449
|
-
type: "function",
|
|
3450
|
-
function: {
|
|
3451
|
-
name: _.function.name,
|
|
3452
|
-
arguments: (J = _.function.arguments) != null ? J : ""
|
|
3453
|
-
},
|
|
3454
|
-
inputStarted: !1,
|
|
3455
|
-
sent: !1
|
|
3456
|
-
};
|
|
3457
|
-
const V = c[C];
|
|
3458
|
-
if (V == null)
|
|
3459
|
-
throw new de({
|
|
3460
|
-
data: { index: C, toolCallsLength: c.length },
|
|
3461
|
-
message: `Tool call at index ${C} is missing after creation.`
|
|
3462
|
-
});
|
|
3463
|
-
((G = V.function) == null ? void 0 : G.name) != null && ((se = V.function) == null ? void 0 : se.arguments) != null && je(V.function.arguments) && (V.inputStarted = !0, S.enqueue({
|
|
3464
|
-
type: "tool-input-start",
|
|
3465
|
-
id: V.id,
|
|
3466
|
-
toolName: V.function.name
|
|
3467
|
-
}), S.enqueue({
|
|
3468
|
-
type: "tool-input-delta",
|
|
3469
|
-
id: V.id,
|
|
3470
|
-
delta: V.function.arguments
|
|
3471
|
-
}), S.enqueue({
|
|
3472
|
-
type: "tool-input-end",
|
|
3473
|
-
id: V.id
|
|
3474
|
-
}), S.enqueue({
|
|
3475
|
-
type: "tool-call",
|
|
3476
|
-
toolCallId: V.id,
|
|
3477
|
-
toolName: V.function.name,
|
|
3478
|
-
input: V.function.arguments,
|
|
3479
|
-
providerMetadata: {
|
|
3480
|
-
openrouter: {
|
|
3481
|
-
reasoning_details: g
|
|
3482
|
-
}
|
|
3483
|
-
}
|
|
3484
|
-
}), V.sent = !0);
|
|
3485
|
-
continue;
|
|
3486
|
-
}
|
|
3487
|
-
const N = c[C];
|
|
3488
|
-
if (N == null)
|
|
3489
|
-
throw new de({
|
|
3490
|
-
data: {
|
|
3491
|
-
index: C,
|
|
3492
|
-
toolCallsLength: c.length,
|
|
3493
|
-
toolCallDelta: _
|
|
3494
|
-
},
|
|
3495
|
-
message: `Tool call at index ${C} is missing during merge.`
|
|
3496
|
-
});
|
|
3497
|
-
N.inputStarted || (N.inputStarted = !0, S.enqueue({
|
|
3498
|
-
type: "tool-input-start",
|
|
3499
|
-
id: N.id,
|
|
3500
|
-
toolName: N.function.name
|
|
3501
|
-
})), ((D = _.function) == null ? void 0 : D.arguments) != null && (N.function.arguments += (ne = (H = _.function) == null ? void 0 : H.arguments) != null ? ne : ""), S.enqueue({
|
|
3502
|
-
type: "tool-input-delta",
|
|
3503
|
-
id: N.id,
|
|
3504
|
-
delta: (re = _.function.arguments) != null ? re : ""
|
|
3505
|
-
}), ((ve = N.function) == null ? void 0 : ve.name) != null && ((Z = N.function) == null ? void 0 : Z.arguments) != null && je(N.function.arguments) && (S.enqueue({
|
|
3506
|
-
type: "tool-call",
|
|
3507
|
-
toolCallId: (ae = N.id) != null ? ae : W(),
|
|
3508
|
-
toolName: N.function.name,
|
|
3509
|
-
input: N.function.arguments,
|
|
3510
|
-
providerMetadata: {
|
|
3511
|
-
openrouter: {
|
|
3512
|
-
reasoning_details: g
|
|
3513
|
-
}
|
|
3514
|
-
}
|
|
3515
|
-
}), N.sent = !0);
|
|
3516
|
-
}
|
|
3517
|
-
if (q.images != null)
|
|
3518
|
-
for (const _ of q.images)
|
|
3519
|
-
S.enqueue({
|
|
3520
|
-
type: "file",
|
|
3521
|
-
mediaType: ht(_.image_url.url, "image/jpeg"),
|
|
3522
|
-
data: Je(_.image_url.url)
|
|
3523
|
-
});
|
|
3524
|
-
},
|
|
3525
|
-
flush(T) {
|
|
3526
|
-
var S;
|
|
3527
|
-
const M = c.length > 0, $ = g.some(
|
|
3528
|
-
(I) => I.type === "reasoning.encrypted" && I.data
|
|
3529
|
-
);
|
|
3530
|
-
if (M && $ && l === "stop" && (l = "tool-calls"), l === "tool-calls")
|
|
3531
|
-
for (const I of c)
|
|
3532
|
-
I && !I.sent && (T.enqueue({
|
|
3533
|
-
type: "tool-call",
|
|
3534
|
-
toolCallId: (S = I.id) != null ? S : W(),
|
|
3535
|
-
toolName: I.function.name,
|
|
3536
|
-
// Coerce invalid arguments to an empty JSON object
|
|
3537
|
-
input: je(I.function.arguments) ? I.function.arguments : "{}",
|
|
3538
|
-
providerMetadata: {
|
|
3539
|
-
openrouter: {
|
|
3540
|
-
reasoning_details: g
|
|
3541
|
-
}
|
|
3542
|
-
}
|
|
3543
|
-
}), I.sent = !0);
|
|
3544
|
-
m && T.enqueue({
|
|
3545
|
-
type: "reasoning-end",
|
|
3546
|
-
id: w || W()
|
|
3547
|
-
}), v && T.enqueue({
|
|
3548
|
-
type: "text-end",
|
|
3549
|
-
id: p || W()
|
|
3550
|
-
});
|
|
3551
|
-
const A = {
|
|
3552
|
-
usage: h
|
|
3553
|
-
};
|
|
3554
|
-
R !== void 0 && (A.provider = R), g.length > 0 && (A.reasoning_details = g), f.length > 0 && (A.annotations = f), T.enqueue({
|
|
3555
|
-
type: "finish",
|
|
3556
|
-
finishReason: l,
|
|
3557
|
-
usage: u,
|
|
3558
|
-
providerMetadata: {
|
|
3559
|
-
openrouter: A
|
|
3560
|
-
}
|
|
3561
|
-
});
|
|
3562
|
-
}
|
|
3563
|
-
})
|
|
3564
|
-
),
|
|
3565
|
-
warnings: [],
|
|
3566
|
-
request: { body: o },
|
|
3567
|
-
response: { headers: i }
|
|
3568
|
-
};
|
|
3569
|
-
}
|
|
3570
26
|
};
|
|
3571
|
-
function ns({
|
|
3572
|
-
prompt: e,
|
|
3573
|
-
inputFormat: t,
|
|
3574
|
-
user: n = "user",
|
|
3575
|
-
assistant: r = "assistant"
|
|
3576
|
-
}) {
|
|
3577
|
-
if (e.length === 1 && e[0] && e[0].role === "user" && e[0].content.length === 1 && e[0].content[0] && e[0].content[0].type === "text")
|
|
3578
|
-
return { prompt: e[0].content[0].text };
|
|
3579
|
-
let o = "";
|
|
3580
|
-
e[0] && e[0].role === "system" && (o += `${e[0].content}
|
|
3581
|
-
|
|
3582
|
-
`, e = e.slice(1));
|
|
3583
|
-
for (const { role: a, content: i } of e)
|
|
3584
|
-
switch (a) {
|
|
3585
|
-
case "system":
|
|
3586
|
-
throw new io({
|
|
3587
|
-
message: `Unexpected system message in prompt: ${i}`,
|
|
3588
|
-
prompt: e
|
|
3589
|
-
});
|
|
3590
|
-
case "user": {
|
|
3591
|
-
const c = i.map((l) => {
|
|
3592
|
-
switch (l.type) {
|
|
3593
|
-
case "text":
|
|
3594
|
-
return l.text;
|
|
3595
|
-
case "file":
|
|
3596
|
-
throw new te({
|
|
3597
|
-
functionality: "file attachments"
|
|
3598
|
-
});
|
|
3599
|
-
default:
|
|
3600
|
-
return "";
|
|
3601
|
-
}
|
|
3602
|
-
}).join("");
|
|
3603
|
-
o += `${n}:
|
|
3604
|
-
${c}
|
|
3605
|
-
|
|
3606
|
-
`;
|
|
3607
|
-
break;
|
|
3608
|
-
}
|
|
3609
|
-
case "assistant": {
|
|
3610
|
-
const c = i.map(
|
|
3611
|
-
(l) => {
|
|
3612
|
-
switch (l.type) {
|
|
3613
|
-
case "text":
|
|
3614
|
-
return l.text;
|
|
3615
|
-
case "tool-call":
|
|
3616
|
-
throw new te({
|
|
3617
|
-
functionality: "tool-call messages"
|
|
3618
|
-
});
|
|
3619
|
-
case "tool-result":
|
|
3620
|
-
throw new te({
|
|
3621
|
-
functionality: "tool-result messages"
|
|
3622
|
-
});
|
|
3623
|
-
case "reasoning":
|
|
3624
|
-
throw new te({
|
|
3625
|
-
functionality: "reasoning messages"
|
|
3626
|
-
});
|
|
3627
|
-
case "file":
|
|
3628
|
-
throw new te({
|
|
3629
|
-
functionality: "file attachments"
|
|
3630
|
-
});
|
|
3631
|
-
default:
|
|
3632
|
-
return "";
|
|
3633
|
-
}
|
|
3634
|
-
}
|
|
3635
|
-
).join("");
|
|
3636
|
-
o += `${r}:
|
|
3637
|
-
${c}
|
|
3638
|
-
|
|
3639
|
-
`;
|
|
3640
|
-
break;
|
|
3641
|
-
}
|
|
3642
|
-
case "tool":
|
|
3643
|
-
throw new te({
|
|
3644
|
-
functionality: "tool messages"
|
|
3645
|
-
});
|
|
3646
|
-
}
|
|
3647
|
-
return o += `${r}:
|
|
3648
|
-
`, {
|
|
3649
|
-
prompt: o
|
|
3650
|
-
};
|
|
3651
|
-
}
|
|
3652
|
-
var mt = j([
|
|
3653
|
-
d({
|
|
3654
|
-
id: s().optional(),
|
|
3655
|
-
model: s().optional(),
|
|
3656
|
-
choices: y(
|
|
3657
|
-
d({
|
|
3658
|
-
text: s(),
|
|
3659
|
-
reasoning: s().nullish().optional(),
|
|
3660
|
-
reasoning_details: tt.nullish(),
|
|
3661
|
-
finish_reason: s().nullish(),
|
|
3662
|
-
index: k().nullish(),
|
|
3663
|
-
logprobs: d({
|
|
3664
|
-
tokens: y(s()),
|
|
3665
|
-
token_logprobs: y(k()),
|
|
3666
|
-
top_logprobs: y(ze(s(), k())).nullable()
|
|
3667
|
-
}).passthrough().nullable().optional()
|
|
3668
|
-
}).passthrough()
|
|
3669
|
-
),
|
|
3670
|
-
usage: d({
|
|
3671
|
-
prompt_tokens: k(),
|
|
3672
|
-
prompt_tokens_details: d({
|
|
3673
|
-
cached_tokens: k()
|
|
3674
|
-
}).passthrough().nullish(),
|
|
3675
|
-
completion_tokens: k(),
|
|
3676
|
-
completion_tokens_details: d({
|
|
3677
|
-
reasoning_tokens: k()
|
|
3678
|
-
}).passthrough().nullish(),
|
|
3679
|
-
total_tokens: k(),
|
|
3680
|
-
cost: k().optional(),
|
|
3681
|
-
cost_details: d({
|
|
3682
|
-
upstream_inference_cost: k().nullish()
|
|
3683
|
-
}).passthrough().nullish()
|
|
3684
|
-
}).passthrough().nullish()
|
|
3685
|
-
}).passthrough(),
|
|
3686
|
-
Ue
|
|
3687
|
-
]), rs = class {
|
|
3688
|
-
constructor(e, t, n) {
|
|
3689
|
-
this.specificationVersion = "v2", this.provider = "openrouter", this.supportsImageUrls = !0, this.supportedUrls = {
|
|
3690
|
-
"image/*": [
|
|
3691
|
-
/^data:image\/[a-zA-Z]+;base64,/,
|
|
3692
|
-
/^https?:\/\/.+\.(jpg|jpeg|png|gif|webp)$/i
|
|
3693
|
-
],
|
|
3694
|
-
"text/*": [/^data:text\//, /^https?:\/\/.+$/],
|
|
3695
|
-
"application/*": [/^data:application\//, /^https?:\/\/.+$/]
|
|
3696
|
-
}, this.defaultObjectGenerationMode = void 0, this.modelId = e, this.settings = t, this.config = n;
|
|
3697
|
-
}
|
|
3698
|
-
getArgs({
|
|
3699
|
-
prompt: e,
|
|
3700
|
-
maxOutputTokens: t,
|
|
3701
|
-
temperature: n,
|
|
3702
|
-
topP: r,
|
|
3703
|
-
frequencyPenalty: o,
|
|
3704
|
-
presencePenalty: a,
|
|
3705
|
-
seed: i,
|
|
3706
|
-
responseFormat: c,
|
|
3707
|
-
topK: l,
|
|
3708
|
-
stopSequences: u,
|
|
3709
|
-
tools: h,
|
|
3710
|
-
toolChoice: g
|
|
3711
|
-
}) {
|
|
3712
|
-
const { prompt: f } = ns({
|
|
3713
|
-
prompt: e,
|
|
3714
|
-
inputFormat: "prompt"
|
|
3715
|
-
});
|
|
3716
|
-
if (h?.length)
|
|
3717
|
-
throw new te({
|
|
3718
|
-
functionality: "tools"
|
|
3719
|
-
});
|
|
3720
|
-
if (g)
|
|
3721
|
-
throw new te({
|
|
3722
|
-
functionality: "toolChoice"
|
|
3723
|
-
});
|
|
3724
|
-
return P(P({
|
|
3725
|
-
// model id:
|
|
3726
|
-
model: this.modelId,
|
|
3727
|
-
models: this.settings.models,
|
|
3728
|
-
// model specific settings:
|
|
3729
|
-
logit_bias: this.settings.logitBias,
|
|
3730
|
-
logprobs: typeof this.settings.logprobs == "number" ? this.settings.logprobs : typeof this.settings.logprobs == "boolean" && this.settings.logprobs ? 0 : void 0,
|
|
3731
|
-
suffix: this.settings.suffix,
|
|
3732
|
-
user: this.settings.user,
|
|
3733
|
-
// standardized settings:
|
|
3734
|
-
max_tokens: t,
|
|
3735
|
-
temperature: n,
|
|
3736
|
-
top_p: r,
|
|
3737
|
-
frequency_penalty: o,
|
|
3738
|
-
presence_penalty: a,
|
|
3739
|
-
seed: i,
|
|
3740
|
-
stop: u,
|
|
3741
|
-
response_format: c,
|
|
3742
|
-
top_k: l,
|
|
3743
|
-
// prompt:
|
|
3744
|
-
prompt: f,
|
|
3745
|
-
// OpenRouter specific settings:
|
|
3746
|
-
include_reasoning: this.settings.includeReasoning,
|
|
3747
|
-
reasoning: this.settings.reasoning
|
|
3748
|
-
}, this.config.extraBody), this.settings.extraBody);
|
|
3749
|
-
}
|
|
3750
|
-
async doGenerate(e) {
|
|
3751
|
-
var t, n, r, o, a, i, c, l, u, h, g, f, v, m, p;
|
|
3752
|
-
const b = (e.providerOptions || {}).openrouter || {}, R = P(P({}, this.getArgs(e)), b), { value: T, responseHeaders: S } = await fe({
|
|
3753
|
-
url: this.config.url({
|
|
3754
|
-
path: "/completions",
|
|
3755
|
-
modelId: this.modelId
|
|
3756
|
-
}),
|
|
3757
|
-
headers: me(this.config.headers(), e.headers),
|
|
3758
|
-
body: R,
|
|
3759
|
-
failedResponseHandler: ge,
|
|
3760
|
-
successfulResponseHandler: Ye(
|
|
3761
|
-
mt
|
|
3762
|
-
),
|
|
3763
|
-
abortSignal: e.abortSignal,
|
|
3764
|
-
fetch: this.config.fetch
|
|
3765
|
-
});
|
|
3766
|
-
if ("error" in T) {
|
|
3767
|
-
const $ = T.error;
|
|
3768
|
-
throw new F({
|
|
3769
|
-
message: $.message,
|
|
3770
|
-
url: this.config.url({
|
|
3771
|
-
path: "/completions",
|
|
3772
|
-
modelId: this.modelId
|
|
3773
|
-
}),
|
|
3774
|
-
requestBodyValues: R,
|
|
3775
|
-
statusCode: 200,
|
|
3776
|
-
responseHeaders: S,
|
|
3777
|
-
data: $
|
|
3778
|
-
});
|
|
3779
|
-
}
|
|
3780
|
-
const M = T.choices[0];
|
|
3781
|
-
if (!M)
|
|
3782
|
-
throw new un({
|
|
3783
|
-
message: "No choice in OpenRouter completion response"
|
|
3784
|
-
});
|
|
3785
|
-
return {
|
|
3786
|
-
content: [
|
|
3787
|
-
{
|
|
3788
|
-
type: "text",
|
|
3789
|
-
text: (t = M.text) != null ? t : ""
|
|
3790
|
-
}
|
|
3791
|
-
],
|
|
3792
|
-
finishReason: Oe(M.finish_reason),
|
|
3793
|
-
usage: {
|
|
3794
|
-
inputTokens: (r = (n = T.usage) == null ? void 0 : n.prompt_tokens) != null ? r : 0,
|
|
3795
|
-
outputTokens: (a = (o = T.usage) == null ? void 0 : o.completion_tokens) != null ? a : 0,
|
|
3796
|
-
totalTokens: ((c = (i = T.usage) == null ? void 0 : i.prompt_tokens) != null ? c : 0) + ((u = (l = T.usage) == null ? void 0 : l.completion_tokens) != null ? u : 0),
|
|
3797
|
-
reasoningTokens: (f = (g = (h = T.usage) == null ? void 0 : h.completion_tokens_details) == null ? void 0 : g.reasoning_tokens) != null ? f : 0,
|
|
3798
|
-
cachedInputTokens: (p = (m = (v = T.usage) == null ? void 0 : v.prompt_tokens_details) == null ? void 0 : m.cached_tokens) != null ? p : 0
|
|
3799
|
-
},
|
|
3800
|
-
warnings: [],
|
|
3801
|
-
response: {
|
|
3802
|
-
headers: S
|
|
3803
|
-
}
|
|
3804
|
-
};
|
|
3805
|
-
}
|
|
3806
|
-
async doStream(e) {
|
|
3807
|
-
const n = (e.providerOptions || {}).openrouter || {}, r = P(P({}, this.getArgs(e)), n), { value: o, responseHeaders: a } = await fe({
|
|
3808
|
-
url: this.config.url({
|
|
3809
|
-
path: "/completions",
|
|
3810
|
-
modelId: this.modelId
|
|
3811
|
-
}),
|
|
3812
|
-
headers: me(this.config.headers(), e.headers),
|
|
3813
|
-
body: xe(P({}, r), {
|
|
3814
|
-
stream: !0,
|
|
3815
|
-
// only include stream_options when in strict compatibility mode:
|
|
3816
|
-
stream_options: this.config.compatibility === "strict" ? { include_usage: !0 } : void 0
|
|
3817
|
-
}),
|
|
3818
|
-
failedResponseHandler: ge,
|
|
3819
|
-
successfulResponseHandler: yn(
|
|
3820
|
-
mt
|
|
3821
|
-
),
|
|
3822
|
-
abortSignal: e.abortSignal,
|
|
3823
|
-
fetch: this.config.fetch
|
|
3824
|
-
});
|
|
3825
|
-
let i = "other";
|
|
3826
|
-
const c = {
|
|
3827
|
-
inputTokens: Number.NaN,
|
|
3828
|
-
outputTokens: Number.NaN,
|
|
3829
|
-
totalTokens: Number.NaN,
|
|
3830
|
-
reasoningTokens: Number.NaN,
|
|
3831
|
-
cachedInputTokens: Number.NaN
|
|
3832
|
-
}, l = {};
|
|
3833
|
-
return {
|
|
3834
|
-
stream: o.pipeThrough(
|
|
3835
|
-
new TransformStream({
|
|
3836
|
-
transform(u, h) {
|
|
3837
|
-
var g, f, v;
|
|
3838
|
-
if (!u.success) {
|
|
3839
|
-
i = "error", h.enqueue({ type: "error", error: u.error });
|
|
3840
|
-
return;
|
|
3841
|
-
}
|
|
3842
|
-
const m = u.value;
|
|
3843
|
-
if ("error" in m) {
|
|
3844
|
-
i = "error", h.enqueue({ type: "error", error: m.error });
|
|
3845
|
-
return;
|
|
3846
|
-
}
|
|
3847
|
-
if (m.usage != null) {
|
|
3848
|
-
if (c.inputTokens = m.usage.prompt_tokens, c.outputTokens = m.usage.completion_tokens, c.totalTokens = m.usage.prompt_tokens + m.usage.completion_tokens, l.promptTokens = m.usage.prompt_tokens, m.usage.prompt_tokens_details) {
|
|
3849
|
-
const b = (g = m.usage.prompt_tokens_details.cached_tokens) != null ? g : 0;
|
|
3850
|
-
c.cachedInputTokens = b, l.promptTokensDetails = {
|
|
3851
|
-
cachedTokens: b
|
|
3852
|
-
};
|
|
3853
|
-
}
|
|
3854
|
-
if (l.completionTokens = m.usage.completion_tokens, m.usage.completion_tokens_details) {
|
|
3855
|
-
const b = (f = m.usage.completion_tokens_details.reasoning_tokens) != null ? f : 0;
|
|
3856
|
-
c.reasoningTokens = b, l.completionTokensDetails = {
|
|
3857
|
-
reasoningTokens: b
|
|
3858
|
-
};
|
|
3859
|
-
}
|
|
3860
|
-
l.cost = m.usage.cost, l.totalTokens = m.usage.total_tokens;
|
|
3861
|
-
const w = (v = m.usage.cost_details) == null ? void 0 : v.upstream_inference_cost;
|
|
3862
|
-
w != null && (l.costDetails = {
|
|
3863
|
-
upstreamInferenceCost: w
|
|
3864
|
-
});
|
|
3865
|
-
}
|
|
3866
|
-
const p = m.choices[0];
|
|
3867
|
-
p?.finish_reason != null && (i = Oe(p.finish_reason)), p?.text != null && h.enqueue({
|
|
3868
|
-
type: "text-delta",
|
|
3869
|
-
delta: p.text,
|
|
3870
|
-
id: W()
|
|
3871
|
-
});
|
|
3872
|
-
},
|
|
3873
|
-
flush(u) {
|
|
3874
|
-
u.enqueue({
|
|
3875
|
-
type: "finish",
|
|
3876
|
-
finishReason: i,
|
|
3877
|
-
usage: c,
|
|
3878
|
-
providerMetadata: {
|
|
3879
|
-
openrouter: {
|
|
3880
|
-
usage: l
|
|
3881
|
-
}
|
|
3882
|
-
}
|
|
3883
|
-
});
|
|
3884
|
-
}
|
|
3885
|
-
})
|
|
3886
|
-
),
|
|
3887
|
-
response: {
|
|
3888
|
-
headers: a
|
|
3889
|
-
}
|
|
3890
|
-
};
|
|
3891
|
-
}
|
|
3892
|
-
}, os = d({
|
|
3893
|
-
prompt_tokens: k(),
|
|
3894
|
-
total_tokens: k(),
|
|
3895
|
-
cost: k().optional()
|
|
3896
|
-
}), ss = d({
|
|
3897
|
-
object: x("embedding"),
|
|
3898
|
-
embedding: y(k()),
|
|
3899
|
-
index: k().optional()
|
|
3900
|
-
}), as = d({
|
|
3901
|
-
id: s().optional(),
|
|
3902
|
-
object: x("list"),
|
|
3903
|
-
data: y(ss),
|
|
3904
|
-
model: s(),
|
|
3905
|
-
usage: os.optional()
|
|
3906
|
-
}), is = class {
|
|
3907
|
-
constructor(e, t, n) {
|
|
3908
|
-
this.specificationVersion = "v2", this.provider = "openrouter", this.maxEmbeddingsPerCall = void 0, this.supportsParallelCalls = !0, this.modelId = e, this.settings = t, this.config = n;
|
|
3909
|
-
}
|
|
3910
|
-
async doEmbed(e) {
|
|
3911
|
-
var t;
|
|
3912
|
-
const { values: n, abortSignal: r, headers: o } = e, a = P(P({
|
|
3913
|
-
model: this.modelId,
|
|
3914
|
-
input: n,
|
|
3915
|
-
user: this.settings.user,
|
|
3916
|
-
provider: this.settings.provider
|
|
3917
|
-
}, this.config.extraBody), this.settings.extraBody), { value: i, responseHeaders: c } = await fe({
|
|
3918
|
-
url: this.config.url({
|
|
3919
|
-
path: "/embeddings",
|
|
3920
|
-
modelId: this.modelId
|
|
3921
|
-
}),
|
|
3922
|
-
headers: me(this.config.headers(), o),
|
|
3923
|
-
body: a,
|
|
3924
|
-
failedResponseHandler: ge,
|
|
3925
|
-
successfulResponseHandler: Ye(
|
|
3926
|
-
as
|
|
3927
|
-
),
|
|
3928
|
-
abortSignal: r,
|
|
3929
|
-
fetch: this.config.fetch
|
|
3930
|
-
});
|
|
3931
|
-
return {
|
|
3932
|
-
embeddings: i.data.map((l) => l.embedding),
|
|
3933
|
-
usage: i.usage ? { tokens: i.usage.prompt_tokens } : void 0,
|
|
3934
|
-
providerMetadata: (t = i.usage) != null && t.cost ? {
|
|
3935
|
-
openrouter: {
|
|
3936
|
-
usage: {
|
|
3937
|
-
cost: i.usage.cost
|
|
3938
|
-
}
|
|
3939
|
-
}
|
|
3940
|
-
} : void 0,
|
|
3941
|
-
response: {
|
|
3942
|
-
headers: c,
|
|
3943
|
-
body: i
|
|
3944
|
-
}
|
|
3945
|
-
};
|
|
3946
|
-
}
|
|
3947
|
-
};
|
|
3948
|
-
function ls(e) {
|
|
3949
|
-
return Object.fromEntries(
|
|
3950
|
-
Object.entries(e).filter(([, t]) => t !== null)
|
|
3951
|
-
);
|
|
3952
|
-
}
|
|
3953
|
-
function cs(e, ...t) {
|
|
3954
|
-
const n = ls(
|
|
3955
|
-
e ?? {}
|
|
3956
|
-
), o = [n["user-agent"] || "", ...t].filter(Boolean).join(" ");
|
|
3957
|
-
return xe(P({}, n), {
|
|
3958
|
-
"user-agent": o
|
|
3959
|
-
});
|
|
3960
|
-
}
|
|
3961
|
-
var us = "1.5.4";
|
|
3962
|
-
function Sn(e = {}) {
|
|
3963
|
-
var t, n, r;
|
|
3964
|
-
const o = (n = Lo((t = e.baseURL) != null ? t : e.baseUrl)) != null ? n : "https://openrouter.ai/api/v1", a = (r = e.compatibility) != null ? r : "compatible", i = () => cs(
|
|
3965
|
-
P(P({
|
|
3966
|
-
Authorization: `Bearer ${To({
|
|
3967
|
-
apiKey: e.apiKey,
|
|
3968
|
-
environmentVariableName: "OPENROUTER_API_KEY",
|
|
3969
|
-
description: "OpenRouter"
|
|
3970
|
-
})}`
|
|
3971
|
-
}, e.headers), e.api_keys && Object.keys(e.api_keys).length > 0 && {
|
|
3972
|
-
"X-Provider-API-Keys": JSON.stringify(e.api_keys)
|
|
3973
|
-
}),
|
|
3974
|
-
`ai-sdk/openrouter/${us}`
|
|
3975
|
-
), c = (f, v = {}) => new ts(f, v, {
|
|
3976
|
-
provider: "openrouter.chat",
|
|
3977
|
-
url: ({ path: m }) => `${o}${m}`,
|
|
3978
|
-
headers: i,
|
|
3979
|
-
compatibility: a,
|
|
3980
|
-
fetch: e.fetch,
|
|
3981
|
-
extraBody: e.extraBody
|
|
3982
|
-
}), l = (f, v = {}) => new rs(f, v, {
|
|
3983
|
-
provider: "openrouter.completion",
|
|
3984
|
-
url: ({ path: m }) => `${o}${m}`,
|
|
3985
|
-
headers: i,
|
|
3986
|
-
compatibility: a,
|
|
3987
|
-
fetch: e.fetch,
|
|
3988
|
-
extraBody: e.extraBody
|
|
3989
|
-
}), u = (f, v = {}) => new is(f, v, {
|
|
3990
|
-
provider: "openrouter.embedding",
|
|
3991
|
-
url: ({ path: m }) => `${o}${m}`,
|
|
3992
|
-
headers: i,
|
|
3993
|
-
fetch: e.fetch,
|
|
3994
|
-
extraBody: e.extraBody
|
|
3995
|
-
}), h = (f, v) => {
|
|
3996
|
-
if (new.target)
|
|
3997
|
-
throw new Error(
|
|
3998
|
-
"The OpenRouter model function cannot be called with the new keyword."
|
|
3999
|
-
);
|
|
4000
|
-
return f === "openai/gpt-3.5-turbo-instruct" ? l(
|
|
4001
|
-
f,
|
|
4002
|
-
v
|
|
4003
|
-
) : c(f, v);
|
|
4004
|
-
}, g = (f, v) => h(f, v);
|
|
4005
|
-
return g.languageModel = h, g.chat = c, g.completion = l, g.textEmbeddingModel = u, g.embedding = u, g;
|
|
4006
|
-
}
|
|
4007
|
-
Sn({
|
|
4008
|
-
compatibility: "strict"
|
|
4009
|
-
// strict for OpenRouter API
|
|
4010
|
-
});
|
|
4011
|
-
const ds = "https://app.getgram.ai", gs = () => ({
|
|
4012
|
-
chat: hs
|
|
4013
|
-
}), ps = d({
|
|
4014
|
-
// currently only supports serverURL, however we will support other config shapes for this via a union type in the future
|
|
4015
|
-
mcp: j([s()]),
|
|
4016
|
-
environment: ze(s(), oe()).optional(),
|
|
4017
|
-
projectSlug: s(),
|
|
4018
|
-
model: Ge(An)
|
|
4019
|
-
}), hs = async (e, t) => {
|
|
4020
|
-
if (e.method === "POST")
|
|
4021
|
-
try {
|
|
4022
|
-
const n = [];
|
|
4023
|
-
for await (const v of e)
|
|
4024
|
-
n.push(v);
|
|
4025
|
-
const r = Buffer.concat(n).toString(), { messages: o, config: a, system: i, tools: c } = JSON.parse(r), l = ps.parse(a), h = await (await Wr({
|
|
4026
|
-
transport: {
|
|
4027
|
-
type: "http",
|
|
4028
|
-
url: l.mcp,
|
|
4029
|
-
headers: {
|
|
4030
|
-
...ms(l.environment ?? {}),
|
|
4031
|
-
// Always send the Gram-Key header last so that it isn't clobbered by the environment variables.
|
|
4032
|
-
Authorization: `Bearer ${process.env.GRAM_API_KEY ?? ""}`
|
|
4033
|
-
}
|
|
4034
|
-
}
|
|
4035
|
-
})).tools(), g = Sn({
|
|
4036
|
-
baseURL: ds,
|
|
4037
|
-
// We do not use the apiKey field as the requests are proxied through the Gram API.
|
|
4038
|
-
// Instead we use the Gram-Key header to authenticate the request.
|
|
4039
|
-
apiKey: "must be set",
|
|
4040
|
-
headers: {
|
|
4041
|
-
"Gram-Project": l.projectSlug,
|
|
4042
|
-
"Gram-Key": process.env.GRAM_API_KEY ?? ""
|
|
4043
|
-
}
|
|
4044
|
-
}), f = In({
|
|
4045
|
-
system: i,
|
|
4046
|
-
model: g.chat(l.model),
|
|
4047
|
-
messages: Pn(o),
|
|
4048
|
-
tools: {
|
|
4049
|
-
...h,
|
|
4050
|
-
...$n(c)
|
|
4051
|
-
},
|
|
4052
|
-
stopWhen: On(10)
|
|
4053
|
-
});
|
|
4054
|
-
t.setHeader("Content-Type", "text/event-stream"), t.setHeader("Cache-Control", "no-cache"), t.setHeader("Connection", "keep-alive"), t.setHeader("Transfer-Encoding", "chunked"), t.statusCode = 200, f.pipeUIMessageStreamToResponse(t, {
|
|
4055
|
-
sendReasoning: !0,
|
|
4056
|
-
sendSources: !0
|
|
4057
|
-
});
|
|
4058
|
-
} catch (n) {
|
|
4059
|
-
t.statusCode = 500, t.end(
|
|
4060
|
-
JSON.stringify({
|
|
4061
|
-
error: n instanceof Error ? n.message : "Unknown error"
|
|
4062
|
-
})
|
|
4063
|
-
);
|
|
4064
|
-
}
|
|
4065
|
-
}, ft = "MCP-";
|
|
4066
|
-
function ms(e) {
|
|
4067
|
-
return typeof e != "object" || e === null ? {} : Object.entries(e).reduce(
|
|
4068
|
-
(t, [n, r]) => {
|
|
4069
|
-
const o = n.replace(/_/g, "-"), a = o.startsWith(ft) ? o : `${ft}${o}`;
|
|
4070
|
-
return t[a] = r, t;
|
|
4071
|
-
},
|
|
4072
|
-
{}
|
|
4073
|
-
);
|
|
4074
|
-
}
|
|
4075
27
|
export {
|
|
4076
|
-
|
|
28
|
+
o as createElementsServerHandlers
|
|
4077
29
|
};
|