ai 4.1.53 → 4.1.55
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/CHANGELOG.md +16 -0
- package/dist/index.d.mts +833 -27
- package/dist/index.d.ts +833 -27
- package/dist/index.js +920 -168
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +872 -125
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -4
- package/rsc/dist/rsc-server.mjs +1 -0
- package/rsc/dist/rsc-server.mjs.map +1 -1
package/dist/index.mjs
CHANGED
@@ -1,7 +1,14 @@
|
|
1
1
|
var __defProp = Object.defineProperty;
|
2
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
3
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
4
|
+
}) : x)(function(x) {
|
5
|
+
if (typeof require !== "undefined")
|
6
|
+
return require.apply(this, arguments);
|
7
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
8
|
+
});
|
2
9
|
var __export = (target, all) => {
|
3
|
-
for (var
|
4
|
-
__defProp(target,
|
10
|
+
for (var name17 in all)
|
11
|
+
__defProp(target, name17, { get: all[name17], enumerable: true });
|
5
12
|
};
|
6
13
|
|
7
14
|
// core/index.ts
|
@@ -9,7 +16,7 @@ import { createIdGenerator as createIdGenerator5, generateId as generateId2 } fr
|
|
9
16
|
import {
|
10
17
|
formatAssistantStreamPart,
|
11
18
|
formatDataStreamPart as formatDataStreamPart3,
|
12
|
-
jsonSchema,
|
19
|
+
jsonSchema as jsonSchema2,
|
13
20
|
parseAssistantStreamPart,
|
14
21
|
parseDataStreamPart,
|
15
22
|
processDataStream,
|
@@ -354,7 +361,7 @@ function getBaseTelemetryAttributes({
|
|
354
361
|
telemetry,
|
355
362
|
headers
|
356
363
|
}) {
|
357
|
-
var
|
364
|
+
var _a17;
|
358
365
|
return {
|
359
366
|
"ai.model.provider": model.provider,
|
360
367
|
"ai.model.id": model.modelId,
|
@@ -364,7 +371,7 @@ function getBaseTelemetryAttributes({
|
|
364
371
|
return attributes;
|
365
372
|
}, {}),
|
366
373
|
// add metadata as attributes:
|
367
|
-
...Object.entries((
|
374
|
+
...Object.entries((_a17 = telemetry == null ? void 0 : telemetry.metadata) != null ? _a17 : {}).reduce(
|
368
375
|
(attributes, [key, value]) => {
|
369
376
|
attributes[`ai.telemetry.metadata.${key}`] = value;
|
370
377
|
return attributes;
|
@@ -389,7 +396,7 @@ var noopTracer = {
|
|
389
396
|
startSpan() {
|
390
397
|
return noopSpan;
|
391
398
|
},
|
392
|
-
startActiveSpan(
|
399
|
+
startActiveSpan(name17, arg1, arg2, arg3) {
|
393
400
|
if (typeof arg1 === "function") {
|
394
401
|
return arg1(noopSpan);
|
395
402
|
}
|
@@ -459,13 +466,13 @@ function getTracer({
|
|
459
466
|
// core/telemetry/record-span.ts
|
460
467
|
import { SpanStatusCode } from "@opentelemetry/api";
|
461
468
|
function recordSpan({
|
462
|
-
name:
|
469
|
+
name: name17,
|
463
470
|
tracer,
|
464
471
|
attributes,
|
465
472
|
fn,
|
466
473
|
endWhenDone = true
|
467
474
|
}) {
|
468
|
-
return tracer.startActiveSpan(
|
475
|
+
return tracer.startActiveSpan(name17, { attributes }, async (span) => {
|
469
476
|
try {
|
470
477
|
const result = await fn(span);
|
471
478
|
if (endWhenDone) {
|
@@ -573,14 +580,14 @@ async function embed({
|
|
573
580
|
}),
|
574
581
|
tracer,
|
575
582
|
fn: async (doEmbedSpan) => {
|
576
|
-
var
|
583
|
+
var _a17;
|
577
584
|
const modelResponse = await model.doEmbed({
|
578
585
|
values: [value],
|
579
586
|
abortSignal,
|
580
587
|
headers
|
581
588
|
});
|
582
589
|
const embedding2 = modelResponse.embeddings[0];
|
583
|
-
const usage2 = (
|
590
|
+
const usage2 = (_a17 = modelResponse.usage) != null ? _a17 : { tokens: NaN };
|
584
591
|
doEmbedSpan.setAttributes(
|
585
592
|
selectTelemetryAttributes({
|
586
593
|
telemetry,
|
@@ -690,14 +697,14 @@ async function embedMany({
|
|
690
697
|
}),
|
691
698
|
tracer,
|
692
699
|
fn: async (doEmbedSpan) => {
|
693
|
-
var
|
700
|
+
var _a17;
|
694
701
|
const modelResponse = await model.doEmbed({
|
695
702
|
values,
|
696
703
|
abortSignal,
|
697
704
|
headers
|
698
705
|
});
|
699
706
|
const embeddings3 = modelResponse.embeddings;
|
700
|
-
const usage2 = (
|
707
|
+
const usage2 = (_a17 = modelResponse.usage) != null ? _a17 : { tokens: NaN };
|
701
708
|
doEmbedSpan.setAttributes(
|
702
709
|
selectTelemetryAttributes({
|
703
710
|
telemetry,
|
@@ -749,14 +756,14 @@ async function embedMany({
|
|
749
756
|
}),
|
750
757
|
tracer,
|
751
758
|
fn: async (doEmbedSpan) => {
|
752
|
-
var
|
759
|
+
var _a17;
|
753
760
|
const modelResponse = await model.doEmbed({
|
754
761
|
values: chunk,
|
755
762
|
abortSignal,
|
756
763
|
headers
|
757
764
|
});
|
758
765
|
const embeddings2 = modelResponse.embeddings;
|
759
|
-
const usage2 = (
|
766
|
+
const usage2 = (_a17 = modelResponse.usage) != null ? _a17 : { tokens: NaN };
|
760
767
|
doEmbedSpan.setAttributes(
|
761
768
|
selectTelemetryAttributes({
|
762
769
|
telemetry,
|
@@ -846,9 +853,9 @@ async function generateImage({
|
|
846
853
|
currentDate: () => /* @__PURE__ */ new Date()
|
847
854
|
}
|
848
855
|
}) {
|
849
|
-
var
|
856
|
+
var _a17;
|
850
857
|
const { retry } = prepareRetries({ maxRetries: maxRetriesArg });
|
851
|
-
const maxImagesPerCall = (
|
858
|
+
const maxImagesPerCall = (_a17 = model.maxImagesPerCall) != null ? _a17 : 1;
|
852
859
|
const callCount = Math.ceil(n / maxImagesPerCall);
|
853
860
|
const callImageCounts = Array.from({ length: callCount }, (_, i) => {
|
854
861
|
if (i < callCount - 1) {
|
@@ -984,7 +991,7 @@ async function download({
|
|
984
991
|
url,
|
985
992
|
fetchImplementation = fetch
|
986
993
|
}) {
|
987
|
-
var
|
994
|
+
var _a17;
|
988
995
|
const urlText = url.toString();
|
989
996
|
try {
|
990
997
|
const response = await fetchImplementation(urlText);
|
@@ -997,7 +1004,7 @@ async function download({
|
|
997
1004
|
}
|
998
1005
|
return {
|
999
1006
|
data: new Uint8Array(await response.arrayBuffer()),
|
1000
|
-
mimeType: (
|
1007
|
+
mimeType: (_a17 = response.headers.get("content-type")) != null ? _a17 : void 0
|
1001
1008
|
};
|
1002
1009
|
} catch (error) {
|
1003
1010
|
if (DownloadError.isInstance(error)) {
|
@@ -1060,8 +1067,8 @@ var dataContentSchema = z.union([
|
|
1060
1067
|
z.custom(
|
1061
1068
|
// Buffer might not be available in some environments such as CloudFlare:
|
1062
1069
|
(value) => {
|
1063
|
-
var
|
1064
|
-
return (_b = (
|
1070
|
+
var _a17, _b;
|
1071
|
+
return (_b = (_a17 = globalThis.Buffer) == null ? void 0 : _a17.isBuffer(value)) != null ? _b : false;
|
1065
1072
|
},
|
1066
1073
|
{ message: "Must be a Buffer" }
|
1067
1074
|
)
|
@@ -1161,14 +1168,14 @@ async function convertToLanguageModelPrompt({
|
|
1161
1168
|
];
|
1162
1169
|
}
|
1163
1170
|
function convertToLanguageModelMessage(message, downloadedAssets) {
|
1164
|
-
var
|
1171
|
+
var _a17, _b, _c, _d, _e, _f;
|
1165
1172
|
const role = message.role;
|
1166
1173
|
switch (role) {
|
1167
1174
|
case "system": {
|
1168
1175
|
return {
|
1169
1176
|
role: "system",
|
1170
1177
|
content: message.content,
|
1171
|
-
providerMetadata: (
|
1178
|
+
providerMetadata: (_a17 = message.providerOptions) != null ? _a17 : message.experimental_providerMetadata
|
1172
1179
|
};
|
1173
1180
|
}
|
1174
1181
|
case "user": {
|
@@ -1212,7 +1219,7 @@ function convertToLanguageModelMessage(message, downloadedAssets) {
|
|
1212
1219
|
return {
|
1213
1220
|
role: "tool",
|
1214
1221
|
content: message.content.map((part) => {
|
1215
|
-
var
|
1222
|
+
var _a18;
|
1216
1223
|
return {
|
1217
1224
|
type: "tool-result",
|
1218
1225
|
toolCallId: part.toolCallId,
|
@@ -1220,7 +1227,7 @@ function convertToLanguageModelMessage(message, downloadedAssets) {
|
|
1220
1227
|
result: part.result,
|
1221
1228
|
content: part.experimental_content,
|
1222
1229
|
isError: part.isError,
|
1223
|
-
providerMetadata: (
|
1230
|
+
providerMetadata: (_a18 = part.providerOptions) != null ? _a18 : part.experimental_providerMetadata
|
1224
1231
|
};
|
1225
1232
|
}),
|
1226
1233
|
providerMetadata: (_f = message.providerOptions) != null ? _f : message.experimental_providerMetadata
|
@@ -1256,12 +1263,12 @@ async function downloadAssets(messages, downloadImplementation, modelSupportsIma
|
|
1256
1263
|
);
|
1257
1264
|
}
|
1258
1265
|
function convertPartToLanguageModelPart(part, downloadedAssets) {
|
1259
|
-
var
|
1266
|
+
var _a17, _b, _c, _d;
|
1260
1267
|
if (part.type === "text") {
|
1261
1268
|
return {
|
1262
1269
|
type: "text",
|
1263
1270
|
text: part.text,
|
1264
|
-
providerMetadata: (
|
1271
|
+
providerMetadata: (_a17 = part.providerOptions) != null ? _a17 : part.experimental_providerMetadata
|
1265
1272
|
};
|
1266
1273
|
}
|
1267
1274
|
let mimeType = part.mimeType;
|
@@ -1415,6 +1422,7 @@ function prepareCallSettings({
|
|
1415
1422
|
}
|
1416
1423
|
return {
|
1417
1424
|
maxTokens,
|
1425
|
+
// TODO v5 remove default 0 for temperature
|
1418
1426
|
temperature: temperature != null ? temperature : 0,
|
1419
1427
|
topP,
|
1420
1428
|
topK,
|
@@ -1432,7 +1440,7 @@ import { z as z7 } from "zod";
|
|
1432
1440
|
|
1433
1441
|
// core/prompt/attachments-to-parts.ts
|
1434
1442
|
function attachmentsToParts(attachments) {
|
1435
|
-
var
|
1443
|
+
var _a17, _b, _c;
|
1436
1444
|
const parts = [];
|
1437
1445
|
for (const attachment of attachments) {
|
1438
1446
|
let url;
|
@@ -1444,7 +1452,7 @@ function attachmentsToParts(attachments) {
|
|
1444
1452
|
switch (url.protocol) {
|
1445
1453
|
case "http:":
|
1446
1454
|
case "https:": {
|
1447
|
-
if ((
|
1455
|
+
if ((_a17 = attachment.contentType) == null ? void 0 : _a17.startsWith("image/")) {
|
1448
1456
|
parts.push({ type: "image", image: url });
|
1449
1457
|
} else {
|
1450
1458
|
if (!attachment.contentType) {
|
@@ -1530,8 +1538,8 @@ _a8 = symbol8;
|
|
1530
1538
|
|
1531
1539
|
// core/prompt/convert-to-core-messages.ts
|
1532
1540
|
function convertToCoreMessages(messages, options) {
|
1533
|
-
var
|
1534
|
-
const tools = (
|
1541
|
+
var _a17, _b;
|
1542
|
+
const tools = (_a17 = options == null ? void 0 : options.tools) != null ? _a17 : {};
|
1535
1543
|
const coreMessages = [];
|
1536
1544
|
for (let i = 0; i < messages.length; i++) {
|
1537
1545
|
const message = messages[i];
|
@@ -1676,14 +1684,14 @@ function convertToCoreMessages(messages, options) {
|
|
1676
1684
|
break;
|
1677
1685
|
}
|
1678
1686
|
const maxStep = toolInvocations.reduce((max, toolInvocation) => {
|
1679
|
-
var
|
1680
|
-
return Math.max(max, (
|
1687
|
+
var _a18;
|
1688
|
+
return Math.max(max, (_a18 = toolInvocation.step) != null ? _a18 : 0);
|
1681
1689
|
}, 0);
|
1682
1690
|
for (let i2 = 0; i2 <= maxStep; i2++) {
|
1683
1691
|
const stepInvocations = toolInvocations.filter(
|
1684
1692
|
(toolInvocation) => {
|
1685
|
-
var
|
1686
|
-
return ((
|
1693
|
+
var _a18;
|
1694
|
+
return ((_a18 = toolInvocation.step) != null ? _a18 : 0) === i2;
|
1687
1695
|
}
|
1688
1696
|
);
|
1689
1697
|
if (stepInvocations.length === 0) {
|
@@ -2121,7 +2129,7 @@ var arrayOutputStrategy = (schema) => {
|
|
2121
2129
|
additionalProperties: false
|
2122
2130
|
},
|
2123
2131
|
validatePartialResult({ value, latestObject, isFirstDelta, isFinalDelta }) {
|
2124
|
-
var
|
2132
|
+
var _a17;
|
2125
2133
|
if (!isJSONObject(value) || !isJSONArray(value.elements)) {
|
2126
2134
|
return {
|
2127
2135
|
success: false,
|
@@ -2144,7 +2152,7 @@ var arrayOutputStrategy = (schema) => {
|
|
2144
2152
|
}
|
2145
2153
|
resultArray.push(result.value);
|
2146
2154
|
}
|
2147
|
-
const publishedElementCount = (
|
2155
|
+
const publishedElementCount = (_a17 = latestObject == null ? void 0 : latestObject.length) != null ? _a17 : 0;
|
2148
2156
|
let textDelta = "";
|
2149
2157
|
if (isFirstDelta) {
|
2150
2158
|
textDelta += "[";
|
@@ -2482,7 +2490,7 @@ async function generateObject({
|
|
2482
2490
|
}),
|
2483
2491
|
tracer,
|
2484
2492
|
fn: async (span) => {
|
2485
|
-
var
|
2493
|
+
var _a17, _b, _c, _d;
|
2486
2494
|
if (mode === "auto" || mode == null) {
|
2487
2495
|
mode = model.defaultObjectGenerationMode;
|
2488
2496
|
}
|
@@ -2511,7 +2519,7 @@ async function generateObject({
|
|
2511
2519
|
const promptMessages = await convertToLanguageModelPrompt({
|
2512
2520
|
prompt: standardizedPrompt,
|
2513
2521
|
modelSupportsImageUrls: model.supportsImageUrls,
|
2514
|
-
modelSupportsUrl: (
|
2522
|
+
modelSupportsUrl: (_a17 = model.supportsUrl) == null ? void 0 : _a17.bind(model)
|
2515
2523
|
// support 'this' context
|
2516
2524
|
});
|
2517
2525
|
const generateResult = await retry(
|
@@ -2545,7 +2553,7 @@ async function generateObject({
|
|
2545
2553
|
}),
|
2546
2554
|
tracer,
|
2547
2555
|
fn: async (span2) => {
|
2548
|
-
var
|
2556
|
+
var _a18, _b2, _c2, _d2, _e, _f;
|
2549
2557
|
const result2 = await model.doGenerate({
|
2550
2558
|
mode: {
|
2551
2559
|
type: "object-json",
|
@@ -2561,7 +2569,7 @@ async function generateObject({
|
|
2561
2569
|
headers
|
2562
2570
|
});
|
2563
2571
|
const responseData = {
|
2564
|
-
id: (_b2 = (
|
2572
|
+
id: (_b2 = (_a18 = result2.response) == null ? void 0 : _a18.id) != null ? _b2 : generateId3(),
|
2565
2573
|
timestamp: (_d2 = (_c2 = result2.response) == null ? void 0 : _c2.timestamp) != null ? _d2 : currentDate(),
|
2566
2574
|
modelId: (_f = (_e = result2.response) == null ? void 0 : _e.modelId) != null ? _f : model.modelId
|
2567
2575
|
};
|
@@ -2650,7 +2658,7 @@ async function generateObject({
|
|
2650
2658
|
}),
|
2651
2659
|
tracer,
|
2652
2660
|
fn: async (span2) => {
|
2653
|
-
var
|
2661
|
+
var _a18, _b2, _c2, _d2, _e, _f, _g, _h;
|
2654
2662
|
const result2 = await model.doGenerate({
|
2655
2663
|
mode: {
|
2656
2664
|
type: "object-tool",
|
@@ -2668,7 +2676,7 @@ async function generateObject({
|
|
2668
2676
|
abortSignal,
|
2669
2677
|
headers
|
2670
2678
|
});
|
2671
|
-
const objectText = (_b2 = (
|
2679
|
+
const objectText = (_b2 = (_a18 = result2.toolCalls) == null ? void 0 : _a18[0]) == null ? void 0 : _b2.args;
|
2672
2680
|
const responseData = {
|
2673
2681
|
id: (_d2 = (_c2 = result2.response) == null ? void 0 : _c2.id) != null ? _d2 : generateId3(),
|
2674
2682
|
timestamp: (_f = (_e = result2.response) == null ? void 0 : _e.timestamp) != null ? _f : currentDate(),
|
@@ -2816,9 +2824,9 @@ var DefaultGenerateObjectResult = class {
|
|
2816
2824
|
this.logprobs = options.logprobs;
|
2817
2825
|
}
|
2818
2826
|
toJsonResponse(init) {
|
2819
|
-
var
|
2827
|
+
var _a17;
|
2820
2828
|
return new Response(JSON.stringify(this.object), {
|
2821
|
-
status: (
|
2829
|
+
status: (_a17 = init == null ? void 0 : init.status) != null ? _a17 : 200,
|
2822
2830
|
headers: prepareResponseHeaders(init == null ? void 0 : init.headers, {
|
2823
2831
|
contentType: "application/json; charset=utf-8"
|
2824
2832
|
})
|
@@ -2856,17 +2864,17 @@ var DelayedPromise = class {
|
|
2856
2864
|
return this.promise;
|
2857
2865
|
}
|
2858
2866
|
resolve(value) {
|
2859
|
-
var
|
2867
|
+
var _a17;
|
2860
2868
|
this.status = { type: "resolved", value };
|
2861
2869
|
if (this.promise) {
|
2862
|
-
(
|
2870
|
+
(_a17 = this._resolve) == null ? void 0 : _a17.call(this, value);
|
2863
2871
|
}
|
2864
2872
|
}
|
2865
2873
|
reject(error) {
|
2866
|
-
var
|
2874
|
+
var _a17;
|
2867
2875
|
this.status = { type: "rejected", error };
|
2868
2876
|
if (this.promise) {
|
2869
|
-
(
|
2877
|
+
(_a17 = this._reject) == null ? void 0 : _a17.call(this, error);
|
2870
2878
|
}
|
2871
2879
|
}
|
2872
2880
|
};
|
@@ -2970,8 +2978,8 @@ function createStitchableStream() {
|
|
2970
2978
|
|
2971
2979
|
// core/util/now.ts
|
2972
2980
|
function now() {
|
2973
|
-
var
|
2974
|
-
return (_b = (
|
2981
|
+
var _a17, _b;
|
2982
|
+
return (_b = (_a17 = globalThis == null ? void 0 : globalThis.performance) == null ? void 0 : _a17.now()) != null ? _b : Date.now();
|
2975
2983
|
}
|
2976
2984
|
|
2977
2985
|
// core/generate-object/stream-object.ts
|
@@ -3107,7 +3115,7 @@ var DefaultStreamObjectResult = class {
|
|
3107
3115
|
tracer,
|
3108
3116
|
endWhenDone: false,
|
3109
3117
|
fn: async (rootSpan) => {
|
3110
|
-
var
|
3118
|
+
var _a17, _b;
|
3111
3119
|
if (mode === "auto" || mode == null) {
|
3112
3120
|
mode = model.defaultObjectGenerationMode;
|
3113
3121
|
}
|
@@ -3138,7 +3146,7 @@ var DefaultStreamObjectResult = class {
|
|
3138
3146
|
prompt: await convertToLanguageModelPrompt({
|
3139
3147
|
prompt: standardizedPrompt,
|
3140
3148
|
modelSupportsImageUrls: model.supportsImageUrls,
|
3141
|
-
modelSupportsUrl: (
|
3149
|
+
modelSupportsUrl: (_a17 = model.supportsUrl) == null ? void 0 : _a17.bind(model)
|
3142
3150
|
// support 'this' context
|
3143
3151
|
}),
|
3144
3152
|
providerMetadata: providerOptions,
|
@@ -3276,7 +3284,7 @@ var DefaultStreamObjectResult = class {
|
|
3276
3284
|
const transformedStream = stream.pipeThrough(new TransformStream(transformer)).pipeThrough(
|
3277
3285
|
new TransformStream({
|
3278
3286
|
async transform(chunk, controller) {
|
3279
|
-
var
|
3287
|
+
var _a18, _b2, _c;
|
3280
3288
|
if (isFirstChunk) {
|
3281
3289
|
const msToFirstChunk = now2() - startTimestampMs;
|
3282
3290
|
isFirstChunk = false;
|
@@ -3322,7 +3330,7 @@ var DefaultStreamObjectResult = class {
|
|
3322
3330
|
switch (chunk.type) {
|
3323
3331
|
case "response-metadata": {
|
3324
3332
|
response = {
|
3325
|
-
id: (
|
3333
|
+
id: (_a18 = chunk.id) != null ? _a18 : response.id,
|
3326
3334
|
timestamp: (_b2 = chunk.timestamp) != null ? _b2 : response.timestamp,
|
3327
3335
|
modelId: (_c = chunk.modelId) != null ? _c : response.modelId
|
3328
3336
|
};
|
@@ -3535,9 +3543,9 @@ var DefaultStreamObjectResult = class {
|
|
3535
3543
|
});
|
3536
3544
|
}
|
3537
3545
|
toTextStreamResponse(init) {
|
3538
|
-
var
|
3546
|
+
var _a17;
|
3539
3547
|
return new Response(this.textStream.pipeThrough(new TextEncoderStream()), {
|
3540
|
-
status: (
|
3548
|
+
status: (_a17 = init == null ? void 0 : init.status) != null ? _a17 : 200,
|
3541
3549
|
headers: prepareResponseHeaders(init == null ? void 0 : init.headers, {
|
3542
3550
|
contentType: "text/plain; charset=utf-8"
|
3543
3551
|
})
|
@@ -3613,24 +3621,24 @@ function prepareToolsAndToolChoice({
|
|
3613
3621
|
};
|
3614
3622
|
}
|
3615
3623
|
const filteredTools = activeTools != null ? Object.entries(tools).filter(
|
3616
|
-
([
|
3624
|
+
([name17]) => activeTools.includes(name17)
|
3617
3625
|
) : Object.entries(tools);
|
3618
3626
|
return {
|
3619
|
-
tools: filteredTools.map(([
|
3627
|
+
tools: filteredTools.map(([name17, tool2]) => {
|
3620
3628
|
const toolType = tool2.type;
|
3621
3629
|
switch (toolType) {
|
3622
3630
|
case void 0:
|
3623
3631
|
case "function":
|
3624
3632
|
return {
|
3625
3633
|
type: "function",
|
3626
|
-
name:
|
3634
|
+
name: name17,
|
3627
3635
|
description: tool2.description,
|
3628
3636
|
parameters: asSchema2(tool2.parameters).jsonSchema
|
3629
3637
|
};
|
3630
3638
|
case "provider-defined":
|
3631
3639
|
return {
|
3632
3640
|
type: "provider-defined",
|
3633
|
-
name:
|
3641
|
+
name: name17,
|
3634
3642
|
id: tool2.id,
|
3635
3643
|
args: tool2.args
|
3636
3644
|
};
|
@@ -3800,6 +3808,12 @@ async function doParseToolCall({
|
|
3800
3808
|
};
|
3801
3809
|
}
|
3802
3810
|
|
3811
|
+
// core/generate-text/reasoning-detail.ts
|
3812
|
+
function asReasoningText(reasoning) {
|
3813
|
+
const reasoningText = reasoning.filter((part) => part.type === "text").map((part) => part.text).join("");
|
3814
|
+
return reasoningText.length > 0 ? reasoningText : void 0;
|
3815
|
+
}
|
3816
|
+
|
3803
3817
|
// core/generate-text/to-response-messages.ts
|
3804
3818
|
function toResponseMessages({
|
3805
3819
|
text: text2 = "",
|
@@ -3848,12 +3862,6 @@ function toResponseMessages({
|
|
3848
3862
|
return responseMessages;
|
3849
3863
|
}
|
3850
3864
|
|
3851
|
-
// core/generate-text/reasoning-detail.ts
|
3852
|
-
function asReasoningText(reasoning) {
|
3853
|
-
const reasoningText = reasoning.filter((part) => part.type === "text").map((part) => part.text).join("");
|
3854
|
-
return reasoningText.length > 0 ? reasoningText : void 0;
|
3855
|
-
}
|
3856
|
-
|
3857
3865
|
// core/generate-text/generate-text.ts
|
3858
3866
|
var originalGenerateId3 = createIdGenerator3({
|
3859
3867
|
prefix: "aitxt",
|
@@ -3889,7 +3897,7 @@ async function generateText({
|
|
3889
3897
|
onStepFinish,
|
3890
3898
|
...settings
|
3891
3899
|
}) {
|
3892
|
-
var
|
3900
|
+
var _a17;
|
3893
3901
|
if (maxSteps < 1) {
|
3894
3902
|
throw new InvalidArgumentError({
|
3895
3903
|
parameter: "maxSteps",
|
@@ -3906,7 +3914,7 @@ async function generateText({
|
|
3906
3914
|
});
|
3907
3915
|
const initialPrompt = standardizePrompt({
|
3908
3916
|
prompt: {
|
3909
|
-
system: (
|
3917
|
+
system: (_a17 = output == null ? void 0 : output.injectIntoSystemPrompt({ system, model })) != null ? _a17 : system,
|
3910
3918
|
prompt,
|
3911
3919
|
messages
|
3912
3920
|
},
|
@@ -3932,7 +3940,7 @@ async function generateText({
|
|
3932
3940
|
}),
|
3933
3941
|
tracer,
|
3934
3942
|
fn: async (span) => {
|
3935
|
-
var
|
3943
|
+
var _a18, _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
3936
3944
|
const mode = {
|
3937
3945
|
type: "regular",
|
3938
3946
|
...prepareToolsAndToolChoice({ tools, toolChoice, activeTools })
|
@@ -3966,7 +3974,7 @@ async function generateText({
|
|
3966
3974
|
messages: stepInputMessages
|
3967
3975
|
},
|
3968
3976
|
modelSupportsImageUrls: model.supportsImageUrls,
|
3969
|
-
modelSupportsUrl: (
|
3977
|
+
modelSupportsUrl: (_a18 = model.supportsUrl) == null ? void 0 : _a18.bind(model)
|
3970
3978
|
// support 'this' context
|
3971
3979
|
});
|
3972
3980
|
currentModelResponse = await retry(
|
@@ -3987,8 +3995,8 @@ async function generateText({
|
|
3987
3995
|
"ai.prompt.tools": {
|
3988
3996
|
// convert the language model level tools:
|
3989
3997
|
input: () => {
|
3990
|
-
var
|
3991
|
-
return (
|
3998
|
+
var _a19;
|
3999
|
+
return (_a19 = mode.tools) == null ? void 0 : _a19.map((tool2) => JSON.stringify(tool2));
|
3992
4000
|
}
|
3993
4001
|
},
|
3994
4002
|
"ai.prompt.toolChoice": {
|
@@ -4008,7 +4016,7 @@ async function generateText({
|
|
4008
4016
|
}),
|
4009
4017
|
tracer,
|
4010
4018
|
fn: async (span2) => {
|
4011
|
-
var
|
4019
|
+
var _a19, _b2, _c2, _d2, _e2, _f2;
|
4012
4020
|
const result = await model.doGenerate({
|
4013
4021
|
mode,
|
4014
4022
|
...callSettings,
|
@@ -4020,7 +4028,7 @@ async function generateText({
|
|
4020
4028
|
headers
|
4021
4029
|
});
|
4022
4030
|
const responseData = {
|
4023
|
-
id: (_b2 = (
|
4031
|
+
id: (_b2 = (_a19 = result.response) == null ? void 0 : _a19.id) != null ? _b2 : generateId3(),
|
4024
4032
|
timestamp: (_d2 = (_c2 = result.response) == null ? void 0 : _c2.timestamp) != null ? _d2 : currentDate(),
|
4025
4033
|
modelId: (_f2 = (_e2 = result.response) == null ? void 0 : _e2.modelId) != null ? _f2 : model.modelId
|
4026
4034
|
};
|
@@ -4320,7 +4328,7 @@ import {
|
|
4320
4328
|
|
4321
4329
|
// errors/index.ts
|
4322
4330
|
import {
|
4323
|
-
AISDKError as
|
4331
|
+
AISDKError as AISDKError16,
|
4324
4332
|
APICallError as APICallError2,
|
4325
4333
|
EmptyResponseBodyError,
|
4326
4334
|
InvalidPromptError as InvalidPromptError2,
|
@@ -4354,6 +4362,27 @@ var InvalidStreamPartError = class extends AISDKError14 {
|
|
4354
4362
|
};
|
4355
4363
|
_a14 = symbol14;
|
4356
4364
|
|
4365
|
+
// errors/mcp-client-error.ts
|
4366
|
+
import { AISDKError as AISDKError15 } from "@ai-sdk/provider";
|
4367
|
+
var name15 = "AI_MCPClientError";
|
4368
|
+
var marker15 = `vercel.ai.error.${name15}`;
|
4369
|
+
var symbol15 = Symbol.for(marker15);
|
4370
|
+
var _a15;
|
4371
|
+
var MCPClientError = class extends AISDKError15 {
|
4372
|
+
constructor({
|
4373
|
+
name: name17 = "MCPClientError",
|
4374
|
+
message,
|
4375
|
+
cause
|
4376
|
+
}) {
|
4377
|
+
super({ name: name17, message, cause });
|
4378
|
+
this[_a15] = true;
|
4379
|
+
}
|
4380
|
+
static isInstance(error) {
|
4381
|
+
return AISDKError15.hasMarker(error, marker15);
|
4382
|
+
}
|
4383
|
+
};
|
4384
|
+
_a15 = symbol15;
|
4385
|
+
|
4357
4386
|
// core/generate-text/output.ts
|
4358
4387
|
var text = () => ({
|
4359
4388
|
type: "text",
|
@@ -4480,7 +4509,7 @@ function smoothStream({
|
|
4480
4509
|
}
|
4481
4510
|
|
4482
4511
|
// core/generate-text/stream-text.ts
|
4483
|
-
import { AISDKError as
|
4512
|
+
import { AISDKError as AISDKError17 } from "@ai-sdk/provider";
|
4484
4513
|
import { createIdGenerator as createIdGenerator4 } from "@ai-sdk/provider-utils";
|
4485
4514
|
import { formatDataStreamPart as formatDataStreamPart2 } from "@ai-sdk/ui-utils";
|
4486
4515
|
|
@@ -4934,7 +4963,7 @@ var DefaultStreamTextResult = class {
|
|
4934
4963
|
this.requestPromise = new DelayedPromise();
|
4935
4964
|
this.responsePromise = new DelayedPromise();
|
4936
4965
|
this.stepsPromise = new DelayedPromise();
|
4937
|
-
var
|
4966
|
+
var _a17;
|
4938
4967
|
if (maxSteps < 1) {
|
4939
4968
|
throw new InvalidArgumentError({
|
4940
4969
|
parameter: "maxSteps",
|
@@ -4988,7 +5017,7 @@ var DefaultStreamTextResult = class {
|
|
4988
5017
|
}
|
4989
5018
|
if (part.type === "reasoning-signature") {
|
4990
5019
|
if (activeReasoningText == null) {
|
4991
|
-
throw new
|
5020
|
+
throw new AISDKError17({
|
4992
5021
|
name: "InvalidStreamPart",
|
4993
5022
|
message: "reasoning-signature without reasoning"
|
4994
5023
|
});
|
@@ -5080,7 +5109,7 @@ var DefaultStreamTextResult = class {
|
|
5080
5109
|
}
|
5081
5110
|
},
|
5082
5111
|
async flush(controller) {
|
5083
|
-
var
|
5112
|
+
var _a18;
|
5084
5113
|
try {
|
5085
5114
|
if (recordedSteps.length === 0) {
|
5086
5115
|
return;
|
@@ -5117,7 +5146,7 @@ var DefaultStreamTextResult = class {
|
|
5117
5146
|
sources: lastStep.sources,
|
5118
5147
|
toolCalls: lastStep.toolCalls,
|
5119
5148
|
toolResults: lastStep.toolResults,
|
5120
|
-
request: (
|
5149
|
+
request: (_a18 = lastStep.request) != null ? _a18 : {},
|
5121
5150
|
response: lastStep.response,
|
5122
5151
|
warnings: lastStep.warnings,
|
5123
5152
|
providerMetadata: lastStep.providerMetadata,
|
@@ -5132,8 +5161,8 @@ var DefaultStreamTextResult = class {
|
|
5132
5161
|
"ai.response.text": { output: () => recordedFullText },
|
5133
5162
|
"ai.response.toolCalls": {
|
5134
5163
|
output: () => {
|
5135
|
-
var
|
5136
|
-
return ((
|
5164
|
+
var _a19;
|
5165
|
+
return ((_a19 = lastStep.toolCalls) == null ? void 0 : _a19.length) ? JSON.stringify(lastStep.toolCalls) : void 0;
|
5137
5166
|
}
|
5138
5167
|
},
|
5139
5168
|
"ai.usage.promptTokens": usage.promptTokens,
|
@@ -5175,7 +5204,7 @@ var DefaultStreamTextResult = class {
|
|
5175
5204
|
});
|
5176
5205
|
const initialPrompt = standardizePrompt({
|
5177
5206
|
prompt: {
|
5178
|
-
system: (
|
5207
|
+
system: (_a17 = output == null ? void 0 : output.injectIntoSystemPrompt({ system, model })) != null ? _a17 : system,
|
5179
5208
|
prompt,
|
5180
5209
|
messages
|
5181
5210
|
},
|
@@ -5209,7 +5238,7 @@ var DefaultStreamTextResult = class {
|
|
5209
5238
|
hasLeadingWhitespace,
|
5210
5239
|
messageId
|
5211
5240
|
}) {
|
5212
|
-
var
|
5241
|
+
var _a18;
|
5213
5242
|
const promptFormat = responseMessages.length === 0 ? initialPrompt.type : "messages";
|
5214
5243
|
const stepInputMessages = [
|
5215
5244
|
...initialPrompt.messages,
|
@@ -5222,7 +5251,7 @@ var DefaultStreamTextResult = class {
|
|
5222
5251
|
messages: stepInputMessages
|
5223
5252
|
},
|
5224
5253
|
modelSupportsImageUrls: model.supportsImageUrls,
|
5225
|
-
modelSupportsUrl: (
|
5254
|
+
modelSupportsUrl: (_a18 = model.supportsUrl) == null ? void 0 : _a18.bind(model)
|
5226
5255
|
// support 'this' context
|
5227
5256
|
});
|
5228
5257
|
const mode = {
|
@@ -5253,8 +5282,8 @@ var DefaultStreamTextResult = class {
|
|
5253
5282
|
"ai.prompt.tools": {
|
5254
5283
|
// convert the language model level tools:
|
5255
5284
|
input: () => {
|
5256
|
-
var
|
5257
|
-
return (
|
5285
|
+
var _a19;
|
5286
|
+
return (_a19 = mode.tools) == null ? void 0 : _a19.map((tool2) => JSON.stringify(tool2));
|
5258
5287
|
}
|
5259
5288
|
},
|
5260
5289
|
"ai.prompt.toolChoice": {
|
@@ -5341,7 +5370,7 @@ var DefaultStreamTextResult = class {
|
|
5341
5370
|
transformedStream.pipeThrough(
|
5342
5371
|
new TransformStream({
|
5343
5372
|
async transform(chunk, controller) {
|
5344
|
-
var
|
5373
|
+
var _a19, _b, _c;
|
5345
5374
|
if (stepFirstChunk) {
|
5346
5375
|
const msToFirstChunk = now2() - startTimestampMs;
|
5347
5376
|
stepFirstChunk = false;
|
@@ -5432,7 +5461,7 @@ var DefaultStreamTextResult = class {
|
|
5432
5461
|
}
|
5433
5462
|
case "response-metadata": {
|
5434
5463
|
stepResponse = {
|
5435
|
-
id: (
|
5464
|
+
id: (_a19 = chunk.id) != null ? _a19 : stepResponse.id,
|
5436
5465
|
timestamp: (_b = chunk.timestamp) != null ? _b : stepResponse.timestamp,
|
5437
5466
|
modelId: (_c = chunk.modelId) != null ? _c : stepResponse.modelId
|
5438
5467
|
};
|
@@ -5955,9 +5984,9 @@ var DefaultStreamTextResult = class {
|
|
5955
5984
|
);
|
5956
5985
|
}
|
5957
5986
|
toTextStreamResponse(init) {
|
5958
|
-
var
|
5987
|
+
var _a17;
|
5959
5988
|
return new Response(this.textStream.pipeThrough(new TextEncoderStream()), {
|
5960
|
-
status: (
|
5989
|
+
status: (_a17 = init == null ? void 0 : init.status) != null ? _a17 : 200,
|
5961
5990
|
headers: prepareResponseHeaders(init == null ? void 0 : init.headers, {
|
5962
5991
|
contentType: "text/plain; charset=utf-8"
|
5963
5992
|
})
|
@@ -6014,7 +6043,7 @@ function extractReasoningMiddleware({
|
|
6014
6043
|
);
|
6015
6044
|
textWithoutReasoning = beforeMatch + (beforeMatch.length > 0 && afterMatch.length > 0 ? separator : "") + afterMatch;
|
6016
6045
|
}
|
6017
|
-
return { text: textWithoutReasoning, reasoning
|
6046
|
+
return { ...rest, text: textWithoutReasoning, reasoning };
|
6018
6047
|
},
|
6019
6048
|
wrapStream: async ({ doStream }) => {
|
6020
6049
|
const { stream, ...rest } = await doStream();
|
@@ -6092,7 +6121,7 @@ var doWrap = ({
|
|
6092
6121
|
modelId,
|
6093
6122
|
providerId
|
6094
6123
|
}) => {
|
6095
|
-
var
|
6124
|
+
var _a17;
|
6096
6125
|
async function doTransform({
|
6097
6126
|
params,
|
6098
6127
|
type
|
@@ -6105,7 +6134,7 @@ var doWrap = ({
|
|
6105
6134
|
modelId: modelId != null ? modelId : model.modelId,
|
6106
6135
|
defaultObjectGenerationMode: model.defaultObjectGenerationMode,
|
6107
6136
|
supportsImageUrls: model.supportsImageUrls,
|
6108
|
-
supportsUrl: (
|
6137
|
+
supportsUrl: (_a17 = model.supportsUrl) == null ? void 0 : _a17.bind(model),
|
6109
6138
|
supportsStructuredOutputs: model.supportsStructuredOutputs,
|
6110
6139
|
async doGenerate(params) {
|
6111
6140
|
const transformedParams = await doTransform({ params, type: "generate" });
|
@@ -6141,7 +6170,7 @@ function appendResponseMessages({
|
|
6141
6170
|
responseMessages,
|
6142
6171
|
_internal: { currentDate = () => /* @__PURE__ */ new Date() } = {}
|
6143
6172
|
}) {
|
6144
|
-
var
|
6173
|
+
var _a17, _b, _c, _d;
|
6145
6174
|
const clonedMessages = structuredClone(messages);
|
6146
6175
|
for (const message of responseMessages) {
|
6147
6176
|
const role = message.role;
|
@@ -6223,7 +6252,7 @@ function appendResponseMessages({
|
|
6223
6252
|
const maxStep = extractMaxToolInvocationStep(
|
6224
6253
|
lastMessage.toolInvocations
|
6225
6254
|
);
|
6226
|
-
(
|
6255
|
+
(_a17 = lastMessage.parts) != null ? _a17 : lastMessage.parts = [];
|
6227
6256
|
lastMessage.content = textContent;
|
6228
6257
|
lastMessage.reasoning = reasoningTextContent;
|
6229
6258
|
lastMessage.parts.push(...parts);
|
@@ -6339,11 +6368,11 @@ function customProvider({
|
|
6339
6368
|
var experimental_customProvider = customProvider;
|
6340
6369
|
|
6341
6370
|
// core/registry/no-such-provider-error.ts
|
6342
|
-
import { AISDKError as
|
6343
|
-
var
|
6344
|
-
var
|
6345
|
-
var
|
6346
|
-
var
|
6371
|
+
import { AISDKError as AISDKError18, NoSuchModelError as NoSuchModelError3 } from "@ai-sdk/provider";
|
6372
|
+
var name16 = "AI_NoSuchProviderError";
|
6373
|
+
var marker16 = `vercel.ai.error.${name16}`;
|
6374
|
+
var symbol16 = Symbol.for(marker16);
|
6375
|
+
var _a16;
|
6347
6376
|
var NoSuchProviderError = class extends NoSuchModelError3 {
|
6348
6377
|
constructor({
|
6349
6378
|
modelId,
|
@@ -6352,16 +6381,16 @@ var NoSuchProviderError = class extends NoSuchModelError3 {
|
|
6352
6381
|
availableProviders,
|
6353
6382
|
message = `No such provider: ${providerId} (available providers: ${availableProviders.join()})`
|
6354
6383
|
}) {
|
6355
|
-
super({ errorName:
|
6356
|
-
this[
|
6384
|
+
super({ errorName: name16, modelId, modelType, message });
|
6385
|
+
this[_a16] = true;
|
6357
6386
|
this.providerId = providerId;
|
6358
6387
|
this.availableProviders = availableProviders;
|
6359
6388
|
}
|
6360
6389
|
static isInstance(error) {
|
6361
|
-
return
|
6390
|
+
return AISDKError18.hasMarker(error, marker16);
|
6362
6391
|
}
|
6363
6392
|
};
|
6364
|
-
|
6393
|
+
_a16 = symbol16;
|
6365
6394
|
|
6366
6395
|
// core/registry/provider-registry.ts
|
6367
6396
|
import { NoSuchModelError as NoSuchModelError4 } from "@ai-sdk/provider";
|
@@ -6406,19 +6435,19 @@ var DefaultProviderRegistry = class {
|
|
6406
6435
|
return [id.slice(0, index), id.slice(index + 1)];
|
6407
6436
|
}
|
6408
6437
|
languageModel(id) {
|
6409
|
-
var
|
6438
|
+
var _a17, _b;
|
6410
6439
|
const [providerId, modelId] = this.splitId(id, "languageModel");
|
6411
|
-
const model = (_b = (
|
6440
|
+
const model = (_b = (_a17 = this.getProvider(providerId)).languageModel) == null ? void 0 : _b.call(_a17, modelId);
|
6412
6441
|
if (model == null) {
|
6413
6442
|
throw new NoSuchModelError4({ modelId: id, modelType: "languageModel" });
|
6414
6443
|
}
|
6415
6444
|
return model;
|
6416
6445
|
}
|
6417
6446
|
textEmbeddingModel(id) {
|
6418
|
-
var
|
6447
|
+
var _a17;
|
6419
6448
|
const [providerId, modelId] = this.splitId(id, "textEmbeddingModel");
|
6420
6449
|
const provider = this.getProvider(providerId);
|
6421
|
-
const model = (
|
6450
|
+
const model = (_a17 = provider.textEmbeddingModel) == null ? void 0 : _a17.call(provider, modelId);
|
6422
6451
|
if (model == null) {
|
6423
6452
|
throw new NoSuchModelError4({
|
6424
6453
|
modelId: id,
|
@@ -6428,10 +6457,10 @@ var DefaultProviderRegistry = class {
|
|
6428
6457
|
return model;
|
6429
6458
|
}
|
6430
6459
|
imageModel(id) {
|
6431
|
-
var
|
6460
|
+
var _a17;
|
6432
6461
|
const [providerId, modelId] = this.splitId(id, "imageModel");
|
6433
6462
|
const provider = this.getProvider(providerId);
|
6434
|
-
const model = (
|
6463
|
+
const model = (_a17 = provider.imageModel) == null ? void 0 : _a17.call(provider, modelId);
|
6435
6464
|
if (model == null) {
|
6436
6465
|
throw new NoSuchModelError4({ modelId: id, modelType: "imageModel" });
|
6437
6466
|
}
|
@@ -6445,11 +6474,727 @@ var DefaultProviderRegistry = class {
|
|
6445
6474
|
}
|
6446
6475
|
};
|
6447
6476
|
|
6477
|
+
// core/tool/mcp/mcp-client.ts
|
6478
|
+
import { jsonSchema } from "@ai-sdk/ui-utils";
|
6479
|
+
|
6448
6480
|
// core/tool/tool.ts
|
6449
6481
|
function tool(tool2) {
|
6450
6482
|
return tool2;
|
6451
6483
|
}
|
6452
6484
|
|
6485
|
+
// core/tool/mcp/types.ts
|
6486
|
+
import { z as z8 } from "zod";
|
6487
|
+
var LATEST_PROTOCOL_VERSION = "2024-11-05";
|
6488
|
+
var SUPPORTED_PROTOCOL_VERSIONS = [
|
6489
|
+
LATEST_PROTOCOL_VERSION,
|
6490
|
+
"2024-10-07"
|
6491
|
+
];
|
6492
|
+
var JSONRPC_VERSION = "2.0";
|
6493
|
+
var ClientOrServerImplementationSchema = z8.object({
|
6494
|
+
name: z8.string(),
|
6495
|
+
version: z8.string()
|
6496
|
+
}).passthrough();
|
6497
|
+
var BaseParamsSchema = z8.object({
|
6498
|
+
_meta: z8.optional(z8.object({}).passthrough())
|
6499
|
+
}).passthrough();
|
6500
|
+
var RequestSchema = z8.object({
|
6501
|
+
method: z8.string(),
|
6502
|
+
params: z8.optional(BaseParamsSchema)
|
6503
|
+
});
|
6504
|
+
var ResultSchema = BaseParamsSchema;
|
6505
|
+
var NotificationSchema = z8.object({
|
6506
|
+
method: z8.string(),
|
6507
|
+
params: z8.optional(BaseParamsSchema)
|
6508
|
+
});
|
6509
|
+
var RequestIdSchema = z8.union([z8.string(), z8.number().int()]);
|
6510
|
+
var JSONRPCRequestSchema = z8.object({
|
6511
|
+
jsonrpc: z8.literal(JSONRPC_VERSION),
|
6512
|
+
id: RequestIdSchema
|
6513
|
+
}).merge(RequestSchema).strict();
|
6514
|
+
var JSONRPCResponseSchema = z8.object({
|
6515
|
+
jsonrpc: z8.literal(JSONRPC_VERSION),
|
6516
|
+
id: RequestIdSchema,
|
6517
|
+
result: ResultSchema
|
6518
|
+
}).strict();
|
6519
|
+
var JSONRPCErrorSchema = z8.object({
|
6520
|
+
jsonrpc: z8.literal(JSONRPC_VERSION),
|
6521
|
+
id: RequestIdSchema,
|
6522
|
+
error: z8.object({
|
6523
|
+
code: z8.number().int(),
|
6524
|
+
message: z8.string(),
|
6525
|
+
data: z8.optional(z8.unknown())
|
6526
|
+
})
|
6527
|
+
}).strict();
|
6528
|
+
var JSONRPCNotificationSchema = z8.object({
|
6529
|
+
jsonrpc: z8.literal(JSONRPC_VERSION)
|
6530
|
+
}).merge(NotificationSchema).strict();
|
6531
|
+
var JSONRPCMessageSchema = z8.union([
|
6532
|
+
JSONRPCRequestSchema,
|
6533
|
+
JSONRPCNotificationSchema,
|
6534
|
+
JSONRPCResponseSchema,
|
6535
|
+
JSONRPCErrorSchema
|
6536
|
+
]);
|
6537
|
+
var ServerCapabilitiesSchema = z8.object({
|
6538
|
+
experimental: z8.optional(z8.object({}).passthrough()),
|
6539
|
+
logging: z8.optional(z8.object({}).passthrough()),
|
6540
|
+
prompts: z8.optional(
|
6541
|
+
z8.object({
|
6542
|
+
listChanged: z8.optional(z8.boolean())
|
6543
|
+
}).passthrough()
|
6544
|
+
),
|
6545
|
+
resources: z8.optional(
|
6546
|
+
z8.object({
|
6547
|
+
subscribe: z8.optional(z8.boolean()),
|
6548
|
+
listChanged: z8.optional(z8.boolean())
|
6549
|
+
}).passthrough()
|
6550
|
+
),
|
6551
|
+
tools: z8.optional(
|
6552
|
+
z8.object({
|
6553
|
+
listChanged: z8.optional(z8.boolean())
|
6554
|
+
}).passthrough()
|
6555
|
+
)
|
6556
|
+
}).passthrough();
|
6557
|
+
var InitializeResultSchema = ResultSchema.extend({
|
6558
|
+
protocolVersion: z8.string(),
|
6559
|
+
capabilities: ServerCapabilitiesSchema,
|
6560
|
+
serverInfo: ClientOrServerImplementationSchema,
|
6561
|
+
instructions: z8.optional(z8.string())
|
6562
|
+
});
|
6563
|
+
var PaginatedResultSchema = ResultSchema.extend({
|
6564
|
+
nextCursor: z8.optional(z8.string())
|
6565
|
+
});
|
6566
|
+
var ToolSchema = z8.object({
|
6567
|
+
name: z8.string(),
|
6568
|
+
description: z8.optional(z8.string()),
|
6569
|
+
inputSchema: z8.object({
|
6570
|
+
type: z8.literal("object"),
|
6571
|
+
properties: z8.optional(z8.object({}).passthrough())
|
6572
|
+
}).passthrough()
|
6573
|
+
}).passthrough();
|
6574
|
+
var ListToolsResultSchema = PaginatedResultSchema.extend({
|
6575
|
+
tools: z8.array(ToolSchema)
|
6576
|
+
});
|
6577
|
+
var TextContentSchema = z8.object({
|
6578
|
+
type: z8.literal("text"),
|
6579
|
+
text: z8.string()
|
6580
|
+
}).passthrough();
|
6581
|
+
var ImageContentSchema = z8.object({
|
6582
|
+
type: z8.literal("image"),
|
6583
|
+
data: z8.string().base64(),
|
6584
|
+
mimeType: z8.string()
|
6585
|
+
}).passthrough();
|
6586
|
+
var ResourceContentsSchema = z8.object({
|
6587
|
+
/**
|
6588
|
+
* The URI of this resource.
|
6589
|
+
*/
|
6590
|
+
uri: z8.string(),
|
6591
|
+
/**
|
6592
|
+
* The MIME type of this resource, if known.
|
6593
|
+
*/
|
6594
|
+
mimeType: z8.optional(z8.string())
|
6595
|
+
}).passthrough();
|
6596
|
+
var TextResourceContentsSchema = ResourceContentsSchema.extend({
|
6597
|
+
text: z8.string()
|
6598
|
+
});
|
6599
|
+
var BlobResourceContentsSchema = ResourceContentsSchema.extend({
|
6600
|
+
blob: z8.string().base64()
|
6601
|
+
});
|
6602
|
+
var EmbeddedResourceSchema = z8.object({
|
6603
|
+
type: z8.literal("resource"),
|
6604
|
+
resource: z8.union([TextResourceContentsSchema, BlobResourceContentsSchema])
|
6605
|
+
}).passthrough();
|
6606
|
+
var CallToolResultSchema = ResultSchema.extend({
|
6607
|
+
content: z8.array(
|
6608
|
+
z8.union([TextContentSchema, ImageContentSchema, EmbeddedResourceSchema])
|
6609
|
+
),
|
6610
|
+
isError: z8.boolean().default(false).optional()
|
6611
|
+
}).or(
|
6612
|
+
ResultSchema.extend({
|
6613
|
+
toolResult: z8.unknown()
|
6614
|
+
})
|
6615
|
+
);
|
6616
|
+
|
6617
|
+
// core/tool/mcp/utils.ts
|
6618
|
+
function detectRuntime() {
|
6619
|
+
var _a17, _b;
|
6620
|
+
if (typeof window !== "undefined") {
|
6621
|
+
return "browser";
|
6622
|
+
}
|
6623
|
+
if (((_b = (_a17 = globalThis.process) == null ? void 0 : _a17.release) == null ? void 0 : _b.name) === "node") {
|
6624
|
+
return "node";
|
6625
|
+
}
|
6626
|
+
return null;
|
6627
|
+
}
|
6628
|
+
async function createChildProcess(config, signal) {
|
6629
|
+
var _a17, _b, _c;
|
6630
|
+
const runtime = detectRuntime();
|
6631
|
+
if (runtime !== "node") {
|
6632
|
+
throw new MCPClientError({
|
6633
|
+
message: "Attempted to use child_process module outside of Node.js environment"
|
6634
|
+
});
|
6635
|
+
}
|
6636
|
+
let childProcess;
|
6637
|
+
try {
|
6638
|
+
childProcess = await import("child_process");
|
6639
|
+
} catch (error) {
|
6640
|
+
try {
|
6641
|
+
childProcess = __require("child_process");
|
6642
|
+
} catch (innerError) {
|
6643
|
+
throw new MCPClientError({
|
6644
|
+
message: "Failed to load child_process module dynamically",
|
6645
|
+
cause: innerError
|
6646
|
+
});
|
6647
|
+
}
|
6648
|
+
}
|
6649
|
+
const { spawn } = childProcess;
|
6650
|
+
return spawn(config.command, (_a17 = config.args) != null ? _a17 : [], {
|
6651
|
+
env: (_b = config.env) != null ? _b : getDefaultEnvironment(),
|
6652
|
+
stdio: ["pipe", "pipe", (_c = config.stderr) != null ? _c : "inherit"],
|
6653
|
+
shell: false,
|
6654
|
+
signal,
|
6655
|
+
windowsHide: process.platform === "win32" && isElectron(),
|
6656
|
+
cwd: config.cwd
|
6657
|
+
});
|
6658
|
+
}
|
6659
|
+
var DEFAULT_INHERITED_ENV_VARS = process.platform === "win32" ? [
|
6660
|
+
"APPDATA",
|
6661
|
+
"HOMEDRIVE",
|
6662
|
+
"HOMEPATH",
|
6663
|
+
"LOCALAPPDATA",
|
6664
|
+
"PATH",
|
6665
|
+
"PROCESSOR_ARCHITECTURE",
|
6666
|
+
"SYSTEMDRIVE",
|
6667
|
+
"SYSTEMROOT",
|
6668
|
+
"TEMP",
|
6669
|
+
"USERNAME",
|
6670
|
+
"USERPROFILE"
|
6671
|
+
] : ["HOME", "LOGNAME", "PATH", "SHELL", "TERM", "USER"];
|
6672
|
+
function getDefaultEnvironment() {
|
6673
|
+
const env = {};
|
6674
|
+
for (const key of DEFAULT_INHERITED_ENV_VARS) {
|
6675
|
+
const value = process.env[key];
|
6676
|
+
if (value === void 0) {
|
6677
|
+
continue;
|
6678
|
+
}
|
6679
|
+
if (value.startsWith("()")) {
|
6680
|
+
continue;
|
6681
|
+
}
|
6682
|
+
env[key] = value;
|
6683
|
+
}
|
6684
|
+
return env;
|
6685
|
+
}
|
6686
|
+
function isElectron() {
|
6687
|
+
return "type" in process;
|
6688
|
+
}
|
6689
|
+
|
6690
|
+
// core/tool/mcp/mcp-stdio-transport.ts
|
6691
|
+
var StdioClientTransport = class {
|
6692
|
+
constructor(server) {
|
6693
|
+
this.abortController = new AbortController();
|
6694
|
+
this.readBuffer = new ReadBuffer();
|
6695
|
+
this.serverParams = server;
|
6696
|
+
}
|
6697
|
+
async start() {
|
6698
|
+
if (this.process) {
|
6699
|
+
throw new MCPClientError({
|
6700
|
+
message: "StdioClientTransport already started."
|
6701
|
+
});
|
6702
|
+
}
|
6703
|
+
return new Promise(async (resolve, reject) => {
|
6704
|
+
var _a17, _b, _c, _d;
|
6705
|
+
try {
|
6706
|
+
const process2 = await createChildProcess(
|
6707
|
+
this.serverParams,
|
6708
|
+
this.abortController.signal
|
6709
|
+
);
|
6710
|
+
this.process = process2;
|
6711
|
+
this.process.on("error", (error) => {
|
6712
|
+
var _a18, _b2;
|
6713
|
+
if (error.name === "AbortError") {
|
6714
|
+
(_a18 = this.onClose) == null ? void 0 : _a18.call(this);
|
6715
|
+
return;
|
6716
|
+
}
|
6717
|
+
reject(error);
|
6718
|
+
(_b2 = this.onError) == null ? void 0 : _b2.call(this, error);
|
6719
|
+
});
|
6720
|
+
this.process.on("spawn", () => {
|
6721
|
+
resolve();
|
6722
|
+
});
|
6723
|
+
this.process.on("close", (_code) => {
|
6724
|
+
var _a18;
|
6725
|
+
this.process = void 0;
|
6726
|
+
(_a18 = this.onClose) == null ? void 0 : _a18.call(this);
|
6727
|
+
});
|
6728
|
+
(_a17 = this.process.stdin) == null ? void 0 : _a17.on("error", (error) => {
|
6729
|
+
var _a18;
|
6730
|
+
(_a18 = this.onError) == null ? void 0 : _a18.call(this, error);
|
6731
|
+
});
|
6732
|
+
(_b = this.process.stdout) == null ? void 0 : _b.on("data", (chunk) => {
|
6733
|
+
this.readBuffer.append(chunk);
|
6734
|
+
this.processReadBuffer();
|
6735
|
+
});
|
6736
|
+
(_c = this.process.stdout) == null ? void 0 : _c.on("error", (error) => {
|
6737
|
+
var _a18;
|
6738
|
+
(_a18 = this.onError) == null ? void 0 : _a18.call(this, error);
|
6739
|
+
});
|
6740
|
+
} catch (error) {
|
6741
|
+
reject(error);
|
6742
|
+
(_d = this.onError) == null ? void 0 : _d.call(this, error);
|
6743
|
+
}
|
6744
|
+
});
|
6745
|
+
}
|
6746
|
+
processReadBuffer() {
|
6747
|
+
var _a17, _b;
|
6748
|
+
while (true) {
|
6749
|
+
try {
|
6750
|
+
const message = this.readBuffer.readMessage();
|
6751
|
+
if (message === null) {
|
6752
|
+
break;
|
6753
|
+
}
|
6754
|
+
(_a17 = this.onMessage) == null ? void 0 : _a17.call(this, message);
|
6755
|
+
} catch (error) {
|
6756
|
+
(_b = this.onError) == null ? void 0 : _b.call(this, error);
|
6757
|
+
}
|
6758
|
+
}
|
6759
|
+
}
|
6760
|
+
async close() {
|
6761
|
+
this.abortController.abort();
|
6762
|
+
this.process = void 0;
|
6763
|
+
this.readBuffer.clear();
|
6764
|
+
}
|
6765
|
+
send(message) {
|
6766
|
+
return new Promise((resolve) => {
|
6767
|
+
var _a17;
|
6768
|
+
if (!((_a17 = this.process) == null ? void 0 : _a17.stdin)) {
|
6769
|
+
throw new MCPClientError({
|
6770
|
+
message: "StdioClientTransport not connected"
|
6771
|
+
});
|
6772
|
+
}
|
6773
|
+
const json = serializeMessage(message);
|
6774
|
+
if (this.process.stdin.write(json)) {
|
6775
|
+
resolve();
|
6776
|
+
} else {
|
6777
|
+
this.process.stdin.once("drain", resolve);
|
6778
|
+
}
|
6779
|
+
});
|
6780
|
+
}
|
6781
|
+
};
|
6782
|
+
var ReadBuffer = class {
|
6783
|
+
append(chunk) {
|
6784
|
+
this.buffer = this.buffer ? Buffer.concat([this.buffer, chunk]) : chunk;
|
6785
|
+
}
|
6786
|
+
readMessage() {
|
6787
|
+
if (!this.buffer)
|
6788
|
+
return null;
|
6789
|
+
const index = this.buffer.indexOf("\n");
|
6790
|
+
if (index === -1) {
|
6791
|
+
return null;
|
6792
|
+
}
|
6793
|
+
const line = this.buffer.toString("utf8", 0, index);
|
6794
|
+
this.buffer = this.buffer.subarray(index + 1);
|
6795
|
+
return deserializeMessage(line);
|
6796
|
+
}
|
6797
|
+
clear() {
|
6798
|
+
this.buffer = void 0;
|
6799
|
+
}
|
6800
|
+
};
|
6801
|
+
function serializeMessage(message) {
|
6802
|
+
return JSON.stringify(message) + "\n";
|
6803
|
+
}
|
6804
|
+
function deserializeMessage(line) {
|
6805
|
+
return JSONRPCMessageSchema.parse(JSON.parse(line));
|
6806
|
+
}
|
6807
|
+
|
6808
|
+
// core/tool/mcp/mcp-sse-transport.ts
|
6809
|
+
import { EventSourceParserStream } from "eventsource-parser/stream";
|
6810
|
+
var SSEClientTransport = class {
|
6811
|
+
constructor({ url }) {
|
6812
|
+
this.connected = false;
|
6813
|
+
this.url = new URL(url);
|
6814
|
+
}
|
6815
|
+
async start() {
|
6816
|
+
return new Promise((resolve, reject) => {
|
6817
|
+
if (this.connected) {
|
6818
|
+
return resolve();
|
6819
|
+
}
|
6820
|
+
this.abortController = new AbortController();
|
6821
|
+
const establishConnection = async () => {
|
6822
|
+
var _a17, _b, _c;
|
6823
|
+
try {
|
6824
|
+
const response = await fetch(this.url.href, {
|
6825
|
+
headers: {
|
6826
|
+
Accept: "text/event-stream"
|
6827
|
+
},
|
6828
|
+
signal: (_a17 = this.abortController) == null ? void 0 : _a17.signal
|
6829
|
+
});
|
6830
|
+
if (!response.ok || !response.body) {
|
6831
|
+
const error = new MCPClientError({
|
6832
|
+
message: `MCP SSE Transport Error: ${response.status} ${response.statusText}`
|
6833
|
+
});
|
6834
|
+
(_b = this.onError) == null ? void 0 : _b.call(this, error);
|
6835
|
+
return reject(error);
|
6836
|
+
}
|
6837
|
+
const stream = response.body.pipeThrough(new TextDecoderStream()).pipeThrough(new EventSourceParserStream());
|
6838
|
+
const reader = stream.getReader();
|
6839
|
+
const processEvents = async () => {
|
6840
|
+
var _a18, _b2, _c2;
|
6841
|
+
try {
|
6842
|
+
while (true) {
|
6843
|
+
const { done, value } = await reader.read();
|
6844
|
+
if (done) {
|
6845
|
+
if (this.connected) {
|
6846
|
+
this.connected = false;
|
6847
|
+
throw new MCPClientError({
|
6848
|
+
message: "MCP SSE Transport Error: Connection closed unexpectedly"
|
6849
|
+
});
|
6850
|
+
}
|
6851
|
+
return;
|
6852
|
+
}
|
6853
|
+
const { event, data } = value;
|
6854
|
+
if (event === "endpoint") {
|
6855
|
+
this.endpoint = new URL(data, this.url);
|
6856
|
+
if (this.endpoint.origin !== this.url.origin) {
|
6857
|
+
throw new MCPClientError({
|
6858
|
+
message: `MCP SSE Transport Error: Endpoint origin does not match connection origin: ${this.endpoint.origin}`
|
6859
|
+
});
|
6860
|
+
}
|
6861
|
+
this.connected = true;
|
6862
|
+
resolve();
|
6863
|
+
} else if (event === "message") {
|
6864
|
+
try {
|
6865
|
+
const message = JSONRPCMessageSchema.parse(
|
6866
|
+
JSON.parse(data)
|
6867
|
+
);
|
6868
|
+
(_a18 = this.onMessage) == null ? void 0 : _a18.call(this, message);
|
6869
|
+
} catch (error) {
|
6870
|
+
const e = new MCPClientError({
|
6871
|
+
message: "MCP SSE Transport Error: Failed to parse message",
|
6872
|
+
cause: error
|
6873
|
+
});
|
6874
|
+
(_b2 = this.onError) == null ? void 0 : _b2.call(this, e);
|
6875
|
+
}
|
6876
|
+
}
|
6877
|
+
}
|
6878
|
+
} catch (error) {
|
6879
|
+
if (error instanceof Error && error.name === "AbortError") {
|
6880
|
+
return;
|
6881
|
+
}
|
6882
|
+
(_c2 = this.onError) == null ? void 0 : _c2.call(this, error);
|
6883
|
+
reject(error);
|
6884
|
+
}
|
6885
|
+
};
|
6886
|
+
this.sseConnection = {
|
6887
|
+
close: () => reader.cancel()
|
6888
|
+
};
|
6889
|
+
processEvents();
|
6890
|
+
} catch (error) {
|
6891
|
+
if (error instanceof Error && error.name === "AbortError") {
|
6892
|
+
return;
|
6893
|
+
}
|
6894
|
+
(_c = this.onError) == null ? void 0 : _c.call(this, error);
|
6895
|
+
reject(error);
|
6896
|
+
}
|
6897
|
+
};
|
6898
|
+
establishConnection();
|
6899
|
+
});
|
6900
|
+
}
|
6901
|
+
async close() {
|
6902
|
+
var _a17, _b, _c;
|
6903
|
+
this.connected = false;
|
6904
|
+
(_a17 = this.sseConnection) == null ? void 0 : _a17.close();
|
6905
|
+
(_b = this.abortController) == null ? void 0 : _b.abort();
|
6906
|
+
(_c = this.onClose) == null ? void 0 : _c.call(this);
|
6907
|
+
}
|
6908
|
+
async send(message) {
|
6909
|
+
var _a17, _b, _c;
|
6910
|
+
if (!this.endpoint || !this.connected) {
|
6911
|
+
throw new MCPClientError({
|
6912
|
+
message: "MCP SSE Transport Error: Not connected"
|
6913
|
+
});
|
6914
|
+
}
|
6915
|
+
try {
|
6916
|
+
const headers = new Headers();
|
6917
|
+
headers.set("Content-Type", "application/json");
|
6918
|
+
const init = {
|
6919
|
+
method: "POST",
|
6920
|
+
headers,
|
6921
|
+
body: JSON.stringify(message),
|
6922
|
+
signal: (_a17 = this.abortController) == null ? void 0 : _a17.signal
|
6923
|
+
};
|
6924
|
+
const response = await fetch(this.endpoint, init);
|
6925
|
+
if (!response.ok) {
|
6926
|
+
const text2 = await response.text().catch(() => null);
|
6927
|
+
const error = new MCPClientError({
|
6928
|
+
message: `MCP SSE Transport Error: POSTing to endpoint (HTTP ${response.status}): ${text2}`
|
6929
|
+
});
|
6930
|
+
(_b = this.onError) == null ? void 0 : _b.call(this, error);
|
6931
|
+
return;
|
6932
|
+
}
|
6933
|
+
} catch (error) {
|
6934
|
+
(_c = this.onError) == null ? void 0 : _c.call(this, error);
|
6935
|
+
return;
|
6936
|
+
}
|
6937
|
+
}
|
6938
|
+
};
|
6939
|
+
|
6940
|
+
// core/tool/mcp/mcp-transport.ts
|
6941
|
+
function createMcpTransport(config) {
|
6942
|
+
return config.type === "stdio" ? new StdioClientTransport(config) : new SSEClientTransport(config);
|
6943
|
+
}
|
6944
|
+
|
6945
|
+
// core/tool/mcp/mcp-client.ts
|
6946
|
+
var CLIENT_VERSION = "1.0.0";
|
6947
|
+
async function createMCPClient(config) {
|
6948
|
+
const client = new MCPClient(config);
|
6949
|
+
await client.init();
|
6950
|
+
return client;
|
6951
|
+
}
|
6952
|
+
var MCPClient = class {
|
6953
|
+
constructor({
|
6954
|
+
transport: transportConfig,
|
6955
|
+
name: name17 = "ai-sdk-mcp-client",
|
6956
|
+
onUncaughtError
|
6957
|
+
}) {
|
6958
|
+
this.requestMessageId = 0;
|
6959
|
+
this.responseHandlers = /* @__PURE__ */ new Map();
|
6960
|
+
this.serverCapabilities = {};
|
6961
|
+
this.isClosed = true;
|
6962
|
+
this.onUncaughtError = onUncaughtError;
|
6963
|
+
this.transport = createMcpTransport(transportConfig);
|
6964
|
+
this.transport.onClose = () => this.onClose();
|
6965
|
+
this.transport.onError = (error) => this.onError(error);
|
6966
|
+
this.transport.onMessage = (message) => {
|
6967
|
+
if ("method" in message) {
|
6968
|
+
this.onError(
|
6969
|
+
new MCPClientError({
|
6970
|
+
message: "Unsupported message type"
|
6971
|
+
})
|
6972
|
+
);
|
6973
|
+
return;
|
6974
|
+
}
|
6975
|
+
this.onResponse(message);
|
6976
|
+
};
|
6977
|
+
this.clientInfo = {
|
6978
|
+
name: name17,
|
6979
|
+
version: CLIENT_VERSION
|
6980
|
+
};
|
6981
|
+
}
|
6982
|
+
async init() {
|
6983
|
+
try {
|
6984
|
+
await this.transport.start();
|
6985
|
+
this.isClosed = false;
|
6986
|
+
const result = await this.request({
|
6987
|
+
request: {
|
6988
|
+
method: "initialize",
|
6989
|
+
params: {
|
6990
|
+
protocolVersion: LATEST_PROTOCOL_VERSION,
|
6991
|
+
capabilities: {},
|
6992
|
+
clientInfo: this.clientInfo
|
6993
|
+
}
|
6994
|
+
},
|
6995
|
+
resultSchema: InitializeResultSchema
|
6996
|
+
});
|
6997
|
+
if (result === void 0) {
|
6998
|
+
throw new MCPClientError({
|
6999
|
+
message: "Server sent invalid initialize result"
|
7000
|
+
});
|
7001
|
+
}
|
7002
|
+
if (!SUPPORTED_PROTOCOL_VERSIONS.includes(result.protocolVersion)) {
|
7003
|
+
throw new MCPClientError({
|
7004
|
+
message: `Server's protocol version is not supported: ${result.protocolVersion}`
|
7005
|
+
});
|
7006
|
+
}
|
7007
|
+
this.serverCapabilities = result.capabilities;
|
7008
|
+
await this.notification({
|
7009
|
+
method: "notifications/initialized"
|
7010
|
+
});
|
7011
|
+
return this;
|
7012
|
+
} catch (error) {
|
7013
|
+
await this.close();
|
7014
|
+
throw error;
|
7015
|
+
}
|
7016
|
+
}
|
7017
|
+
async close() {
|
7018
|
+
var _a17;
|
7019
|
+
if (this.isClosed)
|
7020
|
+
return;
|
7021
|
+
await ((_a17 = this.transport) == null ? void 0 : _a17.close());
|
7022
|
+
this.onClose();
|
7023
|
+
}
|
7024
|
+
async request({
|
7025
|
+
request,
|
7026
|
+
resultSchema,
|
7027
|
+
options
|
7028
|
+
}) {
|
7029
|
+
return new Promise((resolve, reject) => {
|
7030
|
+
if (this.isClosed) {
|
7031
|
+
return reject(
|
7032
|
+
new MCPClientError({
|
7033
|
+
message: "Attempted to send a request from a closed client"
|
7034
|
+
})
|
7035
|
+
);
|
7036
|
+
}
|
7037
|
+
const signal = options == null ? void 0 : options.signal;
|
7038
|
+
signal == null ? void 0 : signal.throwIfAborted();
|
7039
|
+
const messageId = this.requestMessageId++;
|
7040
|
+
const jsonrpcRequest = {
|
7041
|
+
...request,
|
7042
|
+
jsonrpc: "2.0",
|
7043
|
+
id: messageId
|
7044
|
+
};
|
7045
|
+
const cleanup = () => {
|
7046
|
+
this.responseHandlers.delete(messageId);
|
7047
|
+
};
|
7048
|
+
this.responseHandlers.set(messageId, (response) => {
|
7049
|
+
if (signal == null ? void 0 : signal.aborted) {
|
7050
|
+
return reject(
|
7051
|
+
new MCPClientError({
|
7052
|
+
message: "Request was aborted",
|
7053
|
+
cause: signal.reason
|
7054
|
+
})
|
7055
|
+
);
|
7056
|
+
}
|
7057
|
+
if (response instanceof Error) {
|
7058
|
+
return reject(response);
|
7059
|
+
}
|
7060
|
+
try {
|
7061
|
+
const result = resultSchema.parse(response.result);
|
7062
|
+
resolve(result);
|
7063
|
+
} catch (error) {
|
7064
|
+
const parseError = new MCPClientError({
|
7065
|
+
message: "Failed to parse server initialization result",
|
7066
|
+
cause: error
|
7067
|
+
});
|
7068
|
+
reject(parseError);
|
7069
|
+
}
|
7070
|
+
});
|
7071
|
+
this.transport.send(jsonrpcRequest).catch((error) => {
|
7072
|
+
cleanup();
|
7073
|
+
reject(error);
|
7074
|
+
});
|
7075
|
+
});
|
7076
|
+
}
|
7077
|
+
async listTools({
|
7078
|
+
params,
|
7079
|
+
options
|
7080
|
+
} = {}) {
|
7081
|
+
if (!this.serverCapabilities.tools) {
|
7082
|
+
throw new MCPClientError({
|
7083
|
+
message: `Server does not support tools`
|
7084
|
+
});
|
7085
|
+
}
|
7086
|
+
try {
|
7087
|
+
return this.request({
|
7088
|
+
request: { method: "tools/list", params },
|
7089
|
+
resultSchema: ListToolsResultSchema,
|
7090
|
+
options
|
7091
|
+
});
|
7092
|
+
} catch (error) {
|
7093
|
+
throw error;
|
7094
|
+
}
|
7095
|
+
}
|
7096
|
+
async callTool({
|
7097
|
+
name: name17,
|
7098
|
+
args,
|
7099
|
+
options
|
7100
|
+
}) {
|
7101
|
+
if (!this.serverCapabilities.tools) {
|
7102
|
+
throw new MCPClientError({
|
7103
|
+
message: `Server does not support tools`
|
7104
|
+
});
|
7105
|
+
}
|
7106
|
+
try {
|
7107
|
+
return this.request({
|
7108
|
+
request: { method: "tools/call", params: { name: name17, arguments: args } },
|
7109
|
+
resultSchema: CallToolResultSchema,
|
7110
|
+
options: {
|
7111
|
+
signal: options == null ? void 0 : options.abortSignal
|
7112
|
+
}
|
7113
|
+
});
|
7114
|
+
} catch (error) {
|
7115
|
+
throw error;
|
7116
|
+
}
|
7117
|
+
}
|
7118
|
+
async notification(notification) {
|
7119
|
+
const jsonrpcNotification = {
|
7120
|
+
...notification,
|
7121
|
+
jsonrpc: "2.0"
|
7122
|
+
};
|
7123
|
+
await this.transport.send(jsonrpcNotification);
|
7124
|
+
}
|
7125
|
+
/**
|
7126
|
+
* Returns a set of AI SDK tools from the MCP server
|
7127
|
+
* @returns A record of tool names to their implementations
|
7128
|
+
*/
|
7129
|
+
async tools({
|
7130
|
+
schemas = "automatic"
|
7131
|
+
} = {}) {
|
7132
|
+
const tools = {};
|
7133
|
+
try {
|
7134
|
+
const listToolsResult = await this.listTools();
|
7135
|
+
for (const { name: name17, description, inputSchema } of listToolsResult.tools) {
|
7136
|
+
if (schemas !== "automatic" && !(name17 in schemas)) {
|
7137
|
+
continue;
|
7138
|
+
}
|
7139
|
+
const parameters = schemas === "automatic" ? jsonSchema(inputSchema) : schemas[name17].parameters;
|
7140
|
+
const self = this;
|
7141
|
+
const toolWithExecute = tool({
|
7142
|
+
description,
|
7143
|
+
parameters,
|
7144
|
+
execute: async (args, options) => {
|
7145
|
+
var _a17;
|
7146
|
+
(_a17 = options == null ? void 0 : options.abortSignal) == null ? void 0 : _a17.throwIfAborted();
|
7147
|
+
return self.callTool({
|
7148
|
+
name: name17,
|
7149
|
+
args,
|
7150
|
+
options
|
7151
|
+
});
|
7152
|
+
}
|
7153
|
+
});
|
7154
|
+
tools[name17] = toolWithExecute;
|
7155
|
+
}
|
7156
|
+
return tools;
|
7157
|
+
} catch (error) {
|
7158
|
+
throw error;
|
7159
|
+
}
|
7160
|
+
}
|
7161
|
+
onClose() {
|
7162
|
+
if (this.isClosed)
|
7163
|
+
return;
|
7164
|
+
this.isClosed = true;
|
7165
|
+
const error = new MCPClientError({
|
7166
|
+
message: "Connection closed"
|
7167
|
+
});
|
7168
|
+
for (const handler of this.responseHandlers.values()) {
|
7169
|
+
handler(error);
|
7170
|
+
}
|
7171
|
+
this.responseHandlers.clear();
|
7172
|
+
}
|
7173
|
+
onError(error) {
|
7174
|
+
if (this.onUncaughtError) {
|
7175
|
+
this.onUncaughtError(error);
|
7176
|
+
}
|
7177
|
+
}
|
7178
|
+
onResponse(response) {
|
7179
|
+
const messageId = Number(response.id);
|
7180
|
+
const handler = this.responseHandlers.get(messageId);
|
7181
|
+
if (handler === void 0) {
|
7182
|
+
throw new MCPClientError({
|
7183
|
+
message: `Protocol error: Received a response for an unknown message ID: ${JSON.stringify(
|
7184
|
+
response
|
7185
|
+
)}`
|
7186
|
+
});
|
7187
|
+
}
|
7188
|
+
this.responseHandlers.delete(messageId);
|
7189
|
+
handler(
|
7190
|
+
"result" in response ? response : new MCPClientError({
|
7191
|
+
message: response.error.message,
|
7192
|
+
cause: response.error
|
7193
|
+
})
|
7194
|
+
);
|
7195
|
+
}
|
7196
|
+
};
|
7197
|
+
|
6453
7198
|
// core/util/cosine-similarity.ts
|
6454
7199
|
function cosineSimilarity(vector1, vector2, options = {
|
6455
7200
|
throwErrorForEmptyVectors: false
|
@@ -6492,8 +7237,8 @@ function simulateReadableStream({
|
|
6492
7237
|
chunkDelayInMs = 0,
|
6493
7238
|
_internal
|
6494
7239
|
}) {
|
6495
|
-
var
|
6496
|
-
const delay2 = (
|
7240
|
+
var _a17;
|
7241
|
+
const delay2 = (_a17 = _internal == null ? void 0 : _internal.delay) != null ? _a17 : delayFunction;
|
6497
7242
|
let index = 0;
|
6498
7243
|
return new ReadableStream({
|
6499
7244
|
async pull(controller) {
|
@@ -6514,7 +7259,7 @@ import {
|
|
6514
7259
|
function AssistantResponse({ threadId, messageId }, process2) {
|
6515
7260
|
const stream = new ReadableStream({
|
6516
7261
|
async start(controller) {
|
6517
|
-
var
|
7262
|
+
var _a17;
|
6518
7263
|
const textEncoder = new TextEncoder();
|
6519
7264
|
const sendMessage = (message) => {
|
6520
7265
|
controller.enqueue(
|
@@ -6536,7 +7281,7 @@ function AssistantResponse({ threadId, messageId }, process2) {
|
|
6536
7281
|
);
|
6537
7282
|
};
|
6538
7283
|
const forwardStream = async (stream2) => {
|
6539
|
-
var
|
7284
|
+
var _a18, _b;
|
6540
7285
|
let result = void 0;
|
6541
7286
|
for await (const value of stream2) {
|
6542
7287
|
switch (value.event) {
|
@@ -6553,7 +7298,7 @@ function AssistantResponse({ threadId, messageId }, process2) {
|
|
6553
7298
|
break;
|
6554
7299
|
}
|
6555
7300
|
case "thread.message.delta": {
|
6556
|
-
const content = (
|
7301
|
+
const content = (_a18 = value.data.delta.content) == null ? void 0 : _a18[0];
|
6557
7302
|
if ((content == null ? void 0 : content.type) === "text" && ((_b = content.text) == null ? void 0 : _b.value) != null) {
|
6558
7303
|
controller.enqueue(
|
6559
7304
|
textEncoder.encode(
|
@@ -6587,7 +7332,7 @@ function AssistantResponse({ threadId, messageId }, process2) {
|
|
6587
7332
|
forwardStream
|
6588
7333
|
});
|
6589
7334
|
} catch (error) {
|
6590
|
-
sendError((
|
7335
|
+
sendError((_a17 = error.message) != null ? _a17 : `${error}`);
|
6591
7336
|
} finally {
|
6592
7337
|
controller.close();
|
6593
7338
|
}
|
@@ -6648,7 +7393,7 @@ function toDataStreamInternal(stream, callbacks) {
|
|
6648
7393
|
return stream.pipeThrough(
|
6649
7394
|
new TransformStream({
|
6650
7395
|
transform: async (value, controller) => {
|
6651
|
-
var
|
7396
|
+
var _a17;
|
6652
7397
|
if (typeof value === "string") {
|
6653
7398
|
controller.enqueue(value);
|
6654
7399
|
return;
|
@@ -6656,7 +7401,7 @@ function toDataStreamInternal(stream, callbacks) {
|
|
6656
7401
|
if ("event" in value) {
|
6657
7402
|
if (value.event === "on_chat_model_stream") {
|
6658
7403
|
forwardAIMessageChunk(
|
6659
|
-
(
|
7404
|
+
(_a17 = value.data) == null ? void 0 : _a17.chunk,
|
6660
7405
|
controller
|
6661
7406
|
);
|
6662
7407
|
}
|
@@ -6679,7 +7424,7 @@ function toDataStream(stream, callbacks) {
|
|
6679
7424
|
);
|
6680
7425
|
}
|
6681
7426
|
function toDataStreamResponse(stream, options) {
|
6682
|
-
var
|
7427
|
+
var _a17;
|
6683
7428
|
const dataStream = toDataStreamInternal(
|
6684
7429
|
stream,
|
6685
7430
|
options == null ? void 0 : options.callbacks
|
@@ -6688,7 +7433,7 @@ function toDataStreamResponse(stream, options) {
|
|
6688
7433
|
const init = options == null ? void 0 : options.init;
|
6689
7434
|
const responseStream = data ? mergeStreams(data.stream, dataStream) : dataStream;
|
6690
7435
|
return new Response(responseStream, {
|
6691
|
-
status: (
|
7436
|
+
status: (_a17 = init == null ? void 0 : init.status) != null ? _a17 : 200,
|
6692
7437
|
statusText: init == null ? void 0 : init.statusText,
|
6693
7438
|
headers: prepareResponseHeaders(init == null ? void 0 : init.headers, {
|
6694
7439
|
contentType: "text/plain; charset=utf-8",
|
@@ -6743,14 +7488,14 @@ function toDataStream2(stream, callbacks) {
|
|
6743
7488
|
);
|
6744
7489
|
}
|
6745
7490
|
function toDataStreamResponse2(stream, options = {}) {
|
6746
|
-
var
|
7491
|
+
var _a17;
|
6747
7492
|
const { init, data, callbacks } = options;
|
6748
7493
|
const dataStream = toDataStreamInternal2(stream, callbacks).pipeThrough(
|
6749
7494
|
new TextEncoderStream()
|
6750
7495
|
);
|
6751
7496
|
const responseStream = data ? mergeStreams(data.stream, dataStream) : dataStream;
|
6752
7497
|
return new Response(responseStream, {
|
6753
|
-
status: (
|
7498
|
+
status: (_a17 = init == null ? void 0 : init.status) != null ? _a17 : 200,
|
6754
7499
|
statusText: init == null ? void 0 : init.statusText,
|
6755
7500
|
headers: prepareResponseHeaders(init == null ? void 0 : init.headers, {
|
6756
7501
|
contentType: "text/plain; charset=utf-8",
|
@@ -6842,7 +7587,7 @@ var StreamData = class {
|
|
6842
7587
|
}
|
6843
7588
|
};
|
6844
7589
|
export {
|
6845
|
-
|
7590
|
+
AISDKError16 as AISDKError,
|
6846
7591
|
APICallError2 as APICallError,
|
6847
7592
|
AssistantResponse,
|
6848
7593
|
DownloadError,
|
@@ -6858,6 +7603,7 @@ export {
|
|
6858
7603
|
langchain_adapter_exports as LangChainAdapter,
|
6859
7604
|
llamaindex_adapter_exports as LlamaIndexAdapter,
|
6860
7605
|
LoadAPIKeyError,
|
7606
|
+
MCPClientError,
|
6861
7607
|
MessageConversionError,
|
6862
7608
|
NoContentGeneratedError,
|
6863
7609
|
NoImageGeneratedError,
|
@@ -6888,6 +7634,7 @@ export {
|
|
6888
7634
|
customProvider,
|
6889
7635
|
embed,
|
6890
7636
|
embedMany,
|
7637
|
+
createMCPClient as experimental_createMCPClient,
|
6891
7638
|
experimental_createProviderRegistry,
|
6892
7639
|
experimental_customProvider,
|
6893
7640
|
generateImage as experimental_generateImage,
|
@@ -6898,7 +7645,7 @@ export {
|
|
6898
7645
|
generateId2 as generateId,
|
6899
7646
|
generateObject,
|
6900
7647
|
generateText,
|
6901
|
-
jsonSchema,
|
7648
|
+
jsonSchema2 as jsonSchema,
|
6902
7649
|
parseAssistantStreamPart,
|
6903
7650
|
parseDataStreamPart,
|
6904
7651
|
pipeDataStreamToResponse,
|