ai 4.1.54 → 4.1.56
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 +17 -0
- package/dist/index.d.mts +843 -27
- package/dist/index.d.ts +843 -27
- package/dist/index.js +1013 -169
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +964 -126
- 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.js
CHANGED
@@ -1,11 +1,13 @@
|
|
1
1
|
"use strict";
|
2
|
+
var __create = Object.create;
|
2
3
|
var __defProp = Object.defineProperty;
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
4
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
5
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
6
8
|
var __export = (target, all) => {
|
7
|
-
for (var
|
8
|
-
__defProp(target,
|
9
|
+
for (var name17 in all)
|
10
|
+
__defProp(target, name17, { get: all[name17], enumerable: true });
|
9
11
|
};
|
10
12
|
var __copyProps = (to, from, except, desc) => {
|
11
13
|
if (from && typeof from === "object" || typeof from === "function") {
|
@@ -15,33 +17,42 @@ var __copyProps = (to, from, except, desc) => {
|
|
15
17
|
}
|
16
18
|
return to;
|
17
19
|
};
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
26
|
+
mod
|
27
|
+
));
|
18
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
19
29
|
|
20
30
|
// streams/index.ts
|
21
31
|
var streams_exports = {};
|
22
32
|
__export(streams_exports, {
|
23
|
-
AISDKError: () =>
|
24
|
-
APICallError: () =>
|
33
|
+
AISDKError: () => import_provider20.AISDKError,
|
34
|
+
APICallError: () => import_provider20.APICallError,
|
25
35
|
AssistantResponse: () => AssistantResponse,
|
26
36
|
DownloadError: () => DownloadError,
|
27
|
-
EmptyResponseBodyError: () =>
|
37
|
+
EmptyResponseBodyError: () => import_provider20.EmptyResponseBodyError,
|
28
38
|
InvalidArgumentError: () => InvalidArgumentError,
|
29
39
|
InvalidDataContentError: () => InvalidDataContentError,
|
30
40
|
InvalidMessageRoleError: () => InvalidMessageRoleError,
|
31
|
-
InvalidPromptError: () =>
|
32
|
-
InvalidResponseDataError: () =>
|
41
|
+
InvalidPromptError: () => import_provider20.InvalidPromptError,
|
42
|
+
InvalidResponseDataError: () => import_provider20.InvalidResponseDataError,
|
33
43
|
InvalidStreamPartError: () => InvalidStreamPartError,
|
34
44
|
InvalidToolArgumentsError: () => InvalidToolArgumentsError,
|
35
|
-
JSONParseError: () =>
|
45
|
+
JSONParseError: () => import_provider20.JSONParseError,
|
36
46
|
LangChainAdapter: () => langchain_adapter_exports,
|
37
47
|
LlamaIndexAdapter: () => llamaindex_adapter_exports,
|
38
|
-
LoadAPIKeyError: () =>
|
48
|
+
LoadAPIKeyError: () => import_provider20.LoadAPIKeyError,
|
49
|
+
MCPClientError: () => MCPClientError,
|
39
50
|
MessageConversionError: () => MessageConversionError,
|
40
|
-
NoContentGeneratedError: () =>
|
51
|
+
NoContentGeneratedError: () => import_provider20.NoContentGeneratedError,
|
41
52
|
NoImageGeneratedError: () => NoImageGeneratedError,
|
42
53
|
NoObjectGeneratedError: () => NoObjectGeneratedError,
|
43
54
|
NoOutputSpecifiedError: () => NoOutputSpecifiedError,
|
44
|
-
NoSuchModelError: () =>
|
55
|
+
NoSuchModelError: () => import_provider20.NoSuchModelError,
|
45
56
|
NoSuchProviderError: () => NoSuchProviderError,
|
46
57
|
NoSuchToolError: () => NoSuchToolError,
|
47
58
|
Output: () => output_exports,
|
@@ -49,8 +60,8 @@ __export(streams_exports, {
|
|
49
60
|
StreamData: () => StreamData,
|
50
61
|
ToolCallRepairError: () => ToolCallRepairError,
|
51
62
|
ToolExecutionError: () => ToolExecutionError,
|
52
|
-
TypeValidationError: () =>
|
53
|
-
UnsupportedFunctionalityError: () =>
|
63
|
+
TypeValidationError: () => import_provider20.TypeValidationError,
|
64
|
+
UnsupportedFunctionalityError: () => import_provider20.UnsupportedFunctionalityError,
|
54
65
|
appendClientMessage: () => appendClientMessage,
|
55
66
|
appendResponseMessages: () => appendResponseMessages,
|
56
67
|
convertToCoreMessages: () => convertToCoreMessages,
|
@@ -66,35 +77,37 @@ __export(streams_exports, {
|
|
66
77
|
customProvider: () => customProvider,
|
67
78
|
embed: () => embed,
|
68
79
|
embedMany: () => embedMany,
|
80
|
+
experimental_createMCPClient: () => createMCPClient,
|
69
81
|
experimental_createProviderRegistry: () => experimental_createProviderRegistry,
|
70
82
|
experimental_customProvider: () => experimental_customProvider,
|
71
83
|
experimental_generateImage: () => generateImage,
|
72
84
|
experimental_wrapLanguageModel: () => experimental_wrapLanguageModel,
|
73
85
|
extractReasoningMiddleware: () => extractReasoningMiddleware,
|
74
|
-
formatAssistantStreamPart: () =>
|
75
|
-
formatDataStreamPart: () =>
|
86
|
+
formatAssistantStreamPart: () => import_ui_utils11.formatAssistantStreamPart,
|
87
|
+
formatDataStreamPart: () => import_ui_utils11.formatDataStreamPart,
|
76
88
|
generateId: () => import_provider_utils14.generateId,
|
77
89
|
generateObject: () => generateObject,
|
78
90
|
generateText: () => generateText,
|
79
|
-
jsonSchema: () =>
|
80
|
-
parseAssistantStreamPart: () =>
|
81
|
-
parseDataStreamPart: () =>
|
91
|
+
jsonSchema: () => import_ui_utils11.jsonSchema,
|
92
|
+
parseAssistantStreamPart: () => import_ui_utils11.parseAssistantStreamPart,
|
93
|
+
parseDataStreamPart: () => import_ui_utils11.parseDataStreamPart,
|
82
94
|
pipeDataStreamToResponse: () => pipeDataStreamToResponse,
|
83
|
-
processDataStream: () =>
|
84
|
-
processTextStream: () =>
|
95
|
+
processDataStream: () => import_ui_utils11.processDataStream,
|
96
|
+
processTextStream: () => import_ui_utils11.processTextStream,
|
85
97
|
simulateReadableStream: () => simulateReadableStream,
|
98
|
+
simulateStreamingMiddleware: () => simulateStreamingMiddleware,
|
86
99
|
smoothStream: () => smoothStream,
|
87
100
|
streamObject: () => streamObject,
|
88
101
|
streamText: () => streamText,
|
89
102
|
tool: () => tool,
|
90
103
|
wrapLanguageModel: () => wrapLanguageModel,
|
91
|
-
zodSchema: () =>
|
104
|
+
zodSchema: () => import_ui_utils11.zodSchema
|
92
105
|
});
|
93
106
|
module.exports = __toCommonJS(streams_exports);
|
94
107
|
|
95
108
|
// core/index.ts
|
96
109
|
var import_provider_utils14 = require("@ai-sdk/provider-utils");
|
97
|
-
var
|
110
|
+
var import_ui_utils11 = require("@ai-sdk/ui-utils");
|
98
111
|
|
99
112
|
// core/data-stream/create-data-stream.ts
|
100
113
|
var import_ui_utils = require("@ai-sdk/ui-utils");
|
@@ -433,7 +446,7 @@ function getBaseTelemetryAttributes({
|
|
433
446
|
telemetry,
|
434
447
|
headers
|
435
448
|
}) {
|
436
|
-
var
|
449
|
+
var _a17;
|
437
450
|
return {
|
438
451
|
"ai.model.provider": model.provider,
|
439
452
|
"ai.model.id": model.modelId,
|
@@ -443,7 +456,7 @@ function getBaseTelemetryAttributes({
|
|
443
456
|
return attributes;
|
444
457
|
}, {}),
|
445
458
|
// add metadata as attributes:
|
446
|
-
...Object.entries((
|
459
|
+
...Object.entries((_a17 = telemetry == null ? void 0 : telemetry.metadata) != null ? _a17 : {}).reduce(
|
447
460
|
(attributes, [key, value]) => {
|
448
461
|
attributes[`ai.telemetry.metadata.${key}`] = value;
|
449
462
|
return attributes;
|
@@ -468,7 +481,7 @@ var noopTracer = {
|
|
468
481
|
startSpan() {
|
469
482
|
return noopSpan;
|
470
483
|
},
|
471
|
-
startActiveSpan(
|
484
|
+
startActiveSpan(name17, arg1, arg2, arg3) {
|
472
485
|
if (typeof arg1 === "function") {
|
473
486
|
return arg1(noopSpan);
|
474
487
|
}
|
@@ -538,13 +551,13 @@ function getTracer({
|
|
538
551
|
// core/telemetry/record-span.ts
|
539
552
|
var import_api2 = require("@opentelemetry/api");
|
540
553
|
function recordSpan({
|
541
|
-
name:
|
554
|
+
name: name17,
|
542
555
|
tracer,
|
543
556
|
attributes,
|
544
557
|
fn,
|
545
558
|
endWhenDone = true
|
546
559
|
}) {
|
547
|
-
return tracer.startActiveSpan(
|
560
|
+
return tracer.startActiveSpan(name17, { attributes }, async (span) => {
|
548
561
|
try {
|
549
562
|
const result = await fn(span);
|
550
563
|
if (endWhenDone) {
|
@@ -652,14 +665,14 @@ async function embed({
|
|
652
665
|
}),
|
653
666
|
tracer,
|
654
667
|
fn: async (doEmbedSpan) => {
|
655
|
-
var
|
668
|
+
var _a17;
|
656
669
|
const modelResponse = await model.doEmbed({
|
657
670
|
values: [value],
|
658
671
|
abortSignal,
|
659
672
|
headers
|
660
673
|
});
|
661
674
|
const embedding2 = modelResponse.embeddings[0];
|
662
|
-
const usage2 = (
|
675
|
+
const usage2 = (_a17 = modelResponse.usage) != null ? _a17 : { tokens: NaN };
|
663
676
|
doEmbedSpan.setAttributes(
|
664
677
|
selectTelemetryAttributes({
|
665
678
|
telemetry,
|
@@ -769,14 +782,14 @@ async function embedMany({
|
|
769
782
|
}),
|
770
783
|
tracer,
|
771
784
|
fn: async (doEmbedSpan) => {
|
772
|
-
var
|
785
|
+
var _a17;
|
773
786
|
const modelResponse = await model.doEmbed({
|
774
787
|
values,
|
775
788
|
abortSignal,
|
776
789
|
headers
|
777
790
|
});
|
778
791
|
const embeddings3 = modelResponse.embeddings;
|
779
|
-
const usage2 = (
|
792
|
+
const usage2 = (_a17 = modelResponse.usage) != null ? _a17 : { tokens: NaN };
|
780
793
|
doEmbedSpan.setAttributes(
|
781
794
|
selectTelemetryAttributes({
|
782
795
|
telemetry,
|
@@ -828,14 +841,14 @@ async function embedMany({
|
|
828
841
|
}),
|
829
842
|
tracer,
|
830
843
|
fn: async (doEmbedSpan) => {
|
831
|
-
var
|
844
|
+
var _a17;
|
832
845
|
const modelResponse = await model.doEmbed({
|
833
846
|
values: chunk,
|
834
847
|
abortSignal,
|
835
848
|
headers
|
836
849
|
});
|
837
850
|
const embeddings2 = modelResponse.embeddings;
|
838
|
-
const usage2 = (
|
851
|
+
const usage2 = (_a17 = modelResponse.usage) != null ? _a17 : { tokens: NaN };
|
839
852
|
doEmbedSpan.setAttributes(
|
840
853
|
selectTelemetryAttributes({
|
841
854
|
telemetry,
|
@@ -922,9 +935,9 @@ async function generateImage({
|
|
922
935
|
currentDate: () => /* @__PURE__ */ new Date()
|
923
936
|
}
|
924
937
|
}) {
|
925
|
-
var
|
938
|
+
var _a17;
|
926
939
|
const { retry } = prepareRetries({ maxRetries: maxRetriesArg });
|
927
|
-
const maxImagesPerCall = (
|
940
|
+
const maxImagesPerCall = (_a17 = model.maxImagesPerCall) != null ? _a17 : 1;
|
928
941
|
const callCount = Math.ceil(n / maxImagesPerCall);
|
929
942
|
const callImageCounts = Array.from({ length: callCount }, (_, i) => {
|
930
943
|
if (i < callCount - 1) {
|
@@ -1057,7 +1070,7 @@ async function download({
|
|
1057
1070
|
url,
|
1058
1071
|
fetchImplementation = fetch
|
1059
1072
|
}) {
|
1060
|
-
var
|
1073
|
+
var _a17;
|
1061
1074
|
const urlText = url.toString();
|
1062
1075
|
try {
|
1063
1076
|
const response = await fetchImplementation(urlText);
|
@@ -1070,7 +1083,7 @@ async function download({
|
|
1070
1083
|
}
|
1071
1084
|
return {
|
1072
1085
|
data: new Uint8Array(await response.arrayBuffer()),
|
1073
|
-
mimeType: (
|
1086
|
+
mimeType: (_a17 = response.headers.get("content-type")) != null ? _a17 : void 0
|
1074
1087
|
};
|
1075
1088
|
} catch (error) {
|
1076
1089
|
if (DownloadError.isInstance(error)) {
|
@@ -1130,8 +1143,8 @@ var dataContentSchema = import_zod.z.union([
|
|
1130
1143
|
import_zod.z.custom(
|
1131
1144
|
// Buffer might not be available in some environments such as CloudFlare:
|
1132
1145
|
(value) => {
|
1133
|
-
var
|
1134
|
-
return (_b = (
|
1146
|
+
var _a17, _b;
|
1147
|
+
return (_b = (_a17 = globalThis.Buffer) == null ? void 0 : _a17.isBuffer(value)) != null ? _b : false;
|
1135
1148
|
},
|
1136
1149
|
{ message: "Must be a Buffer" }
|
1137
1150
|
)
|
@@ -1231,14 +1244,14 @@ async function convertToLanguageModelPrompt({
|
|
1231
1244
|
];
|
1232
1245
|
}
|
1233
1246
|
function convertToLanguageModelMessage(message, downloadedAssets) {
|
1234
|
-
var
|
1247
|
+
var _a17, _b, _c, _d, _e, _f;
|
1235
1248
|
const role = message.role;
|
1236
1249
|
switch (role) {
|
1237
1250
|
case "system": {
|
1238
1251
|
return {
|
1239
1252
|
role: "system",
|
1240
1253
|
content: message.content,
|
1241
|
-
providerMetadata: (
|
1254
|
+
providerMetadata: (_a17 = message.providerOptions) != null ? _a17 : message.experimental_providerMetadata
|
1242
1255
|
};
|
1243
1256
|
}
|
1244
1257
|
case "user": {
|
@@ -1282,7 +1295,7 @@ function convertToLanguageModelMessage(message, downloadedAssets) {
|
|
1282
1295
|
return {
|
1283
1296
|
role: "tool",
|
1284
1297
|
content: message.content.map((part) => {
|
1285
|
-
var
|
1298
|
+
var _a18;
|
1286
1299
|
return {
|
1287
1300
|
type: "tool-result",
|
1288
1301
|
toolCallId: part.toolCallId,
|
@@ -1290,7 +1303,7 @@ function convertToLanguageModelMessage(message, downloadedAssets) {
|
|
1290
1303
|
result: part.result,
|
1291
1304
|
content: part.experimental_content,
|
1292
1305
|
isError: part.isError,
|
1293
|
-
providerMetadata: (
|
1306
|
+
providerMetadata: (_a18 = part.providerOptions) != null ? _a18 : part.experimental_providerMetadata
|
1294
1307
|
};
|
1295
1308
|
}),
|
1296
1309
|
providerMetadata: (_f = message.providerOptions) != null ? _f : message.experimental_providerMetadata
|
@@ -1326,12 +1339,12 @@ async function downloadAssets(messages, downloadImplementation, modelSupportsIma
|
|
1326
1339
|
);
|
1327
1340
|
}
|
1328
1341
|
function convertPartToLanguageModelPart(part, downloadedAssets) {
|
1329
|
-
var
|
1342
|
+
var _a17, _b, _c, _d;
|
1330
1343
|
if (part.type === "text") {
|
1331
1344
|
return {
|
1332
1345
|
type: "text",
|
1333
1346
|
text: part.text,
|
1334
|
-
providerMetadata: (
|
1347
|
+
providerMetadata: (_a17 = part.providerOptions) != null ? _a17 : part.experimental_providerMetadata
|
1335
1348
|
};
|
1336
1349
|
}
|
1337
1350
|
let mimeType = part.mimeType;
|
@@ -1485,6 +1498,7 @@ function prepareCallSettings({
|
|
1485
1498
|
}
|
1486
1499
|
return {
|
1487
1500
|
maxTokens,
|
1501
|
+
// TODO v5 remove default 0 for temperature
|
1488
1502
|
temperature: temperature != null ? temperature : 0,
|
1489
1503
|
topP,
|
1490
1504
|
topK,
|
@@ -1502,7 +1516,7 @@ var import_zod7 = require("zod");
|
|
1502
1516
|
|
1503
1517
|
// core/prompt/attachments-to-parts.ts
|
1504
1518
|
function attachmentsToParts(attachments) {
|
1505
|
-
var
|
1519
|
+
var _a17, _b, _c;
|
1506
1520
|
const parts = [];
|
1507
1521
|
for (const attachment of attachments) {
|
1508
1522
|
let url;
|
@@ -1514,7 +1528,7 @@ function attachmentsToParts(attachments) {
|
|
1514
1528
|
switch (url.protocol) {
|
1515
1529
|
case "http:":
|
1516
1530
|
case "https:": {
|
1517
|
-
if ((
|
1531
|
+
if ((_a17 = attachment.contentType) == null ? void 0 : _a17.startsWith("image/")) {
|
1518
1532
|
parts.push({ type: "image", image: url });
|
1519
1533
|
} else {
|
1520
1534
|
if (!attachment.contentType) {
|
@@ -1600,8 +1614,8 @@ _a8 = symbol8;
|
|
1600
1614
|
|
1601
1615
|
// core/prompt/convert-to-core-messages.ts
|
1602
1616
|
function convertToCoreMessages(messages, options) {
|
1603
|
-
var
|
1604
|
-
const tools = (
|
1617
|
+
var _a17, _b;
|
1618
|
+
const tools = (_a17 = options == null ? void 0 : options.tools) != null ? _a17 : {};
|
1605
1619
|
const coreMessages = [];
|
1606
1620
|
for (let i = 0; i < messages.length; i++) {
|
1607
1621
|
const message = messages[i];
|
@@ -1746,14 +1760,14 @@ function convertToCoreMessages(messages, options) {
|
|
1746
1760
|
break;
|
1747
1761
|
}
|
1748
1762
|
const maxStep = toolInvocations.reduce((max, toolInvocation) => {
|
1749
|
-
var
|
1750
|
-
return Math.max(max, (
|
1763
|
+
var _a18;
|
1764
|
+
return Math.max(max, (_a18 = toolInvocation.step) != null ? _a18 : 0);
|
1751
1765
|
}, 0);
|
1752
1766
|
for (let i2 = 0; i2 <= maxStep; i2++) {
|
1753
1767
|
const stepInvocations = toolInvocations.filter(
|
1754
1768
|
(toolInvocation) => {
|
1755
|
-
var
|
1756
|
-
return ((
|
1769
|
+
var _a18;
|
1770
|
+
return ((_a18 = toolInvocation.step) != null ? _a18 : 0) === i2;
|
1757
1771
|
}
|
1758
1772
|
);
|
1759
1773
|
if (stepInvocations.length === 0) {
|
@@ -2186,7 +2200,7 @@ var arrayOutputStrategy = (schema) => {
|
|
2186
2200
|
additionalProperties: false
|
2187
2201
|
},
|
2188
2202
|
validatePartialResult({ value, latestObject, isFirstDelta, isFinalDelta }) {
|
2189
|
-
var
|
2203
|
+
var _a17;
|
2190
2204
|
if (!(0, import_provider11.isJSONObject)(value) || !(0, import_provider11.isJSONArray)(value.elements)) {
|
2191
2205
|
return {
|
2192
2206
|
success: false,
|
@@ -2209,7 +2223,7 @@ var arrayOutputStrategy = (schema) => {
|
|
2209
2223
|
}
|
2210
2224
|
resultArray.push(result.value);
|
2211
2225
|
}
|
2212
|
-
const publishedElementCount = (
|
2226
|
+
const publishedElementCount = (_a17 = latestObject == null ? void 0 : latestObject.length) != null ? _a17 : 0;
|
2213
2227
|
let textDelta = "";
|
2214
2228
|
if (isFirstDelta) {
|
2215
2229
|
textDelta += "[";
|
@@ -2547,7 +2561,7 @@ async function generateObject({
|
|
2547
2561
|
}),
|
2548
2562
|
tracer,
|
2549
2563
|
fn: async (span) => {
|
2550
|
-
var
|
2564
|
+
var _a17, _b, _c, _d;
|
2551
2565
|
if (mode === "auto" || mode == null) {
|
2552
2566
|
mode = model.defaultObjectGenerationMode;
|
2553
2567
|
}
|
@@ -2576,7 +2590,7 @@ async function generateObject({
|
|
2576
2590
|
const promptMessages = await convertToLanguageModelPrompt({
|
2577
2591
|
prompt: standardizedPrompt,
|
2578
2592
|
modelSupportsImageUrls: model.supportsImageUrls,
|
2579
|
-
modelSupportsUrl: (
|
2593
|
+
modelSupportsUrl: (_a17 = model.supportsUrl) == null ? void 0 : _a17.bind(model)
|
2580
2594
|
// support 'this' context
|
2581
2595
|
});
|
2582
2596
|
const generateResult = await retry(
|
@@ -2610,7 +2624,7 @@ async function generateObject({
|
|
2610
2624
|
}),
|
2611
2625
|
tracer,
|
2612
2626
|
fn: async (span2) => {
|
2613
|
-
var
|
2627
|
+
var _a18, _b2, _c2, _d2, _e, _f;
|
2614
2628
|
const result2 = await model.doGenerate({
|
2615
2629
|
mode: {
|
2616
2630
|
type: "object-json",
|
@@ -2626,7 +2640,7 @@ async function generateObject({
|
|
2626
2640
|
headers
|
2627
2641
|
});
|
2628
2642
|
const responseData = {
|
2629
|
-
id: (_b2 = (
|
2643
|
+
id: (_b2 = (_a18 = result2.response) == null ? void 0 : _a18.id) != null ? _b2 : generateId3(),
|
2630
2644
|
timestamp: (_d2 = (_c2 = result2.response) == null ? void 0 : _c2.timestamp) != null ? _d2 : currentDate(),
|
2631
2645
|
modelId: (_f = (_e = result2.response) == null ? void 0 : _e.modelId) != null ? _f : model.modelId
|
2632
2646
|
};
|
@@ -2715,7 +2729,7 @@ async function generateObject({
|
|
2715
2729
|
}),
|
2716
2730
|
tracer,
|
2717
2731
|
fn: async (span2) => {
|
2718
|
-
var
|
2732
|
+
var _a18, _b2, _c2, _d2, _e, _f, _g, _h;
|
2719
2733
|
const result2 = await model.doGenerate({
|
2720
2734
|
mode: {
|
2721
2735
|
type: "object-tool",
|
@@ -2733,7 +2747,7 @@ async function generateObject({
|
|
2733
2747
|
abortSignal,
|
2734
2748
|
headers
|
2735
2749
|
});
|
2736
|
-
const objectText = (_b2 = (
|
2750
|
+
const objectText = (_b2 = (_a18 = result2.toolCalls) == null ? void 0 : _a18[0]) == null ? void 0 : _b2.args;
|
2737
2751
|
const responseData = {
|
2738
2752
|
id: (_d2 = (_c2 = result2.response) == null ? void 0 : _c2.id) != null ? _d2 : generateId3(),
|
2739
2753
|
timestamp: (_f = (_e = result2.response) == null ? void 0 : _e.timestamp) != null ? _f : currentDate(),
|
@@ -2881,9 +2895,9 @@ var DefaultGenerateObjectResult = class {
|
|
2881
2895
|
this.logprobs = options.logprobs;
|
2882
2896
|
}
|
2883
2897
|
toJsonResponse(init) {
|
2884
|
-
var
|
2898
|
+
var _a17;
|
2885
2899
|
return new Response(JSON.stringify(this.object), {
|
2886
|
-
status: (
|
2900
|
+
status: (_a17 = init == null ? void 0 : init.status) != null ? _a17 : 200,
|
2887
2901
|
headers: prepareResponseHeaders(init == null ? void 0 : init.headers, {
|
2888
2902
|
contentType: "application/json; charset=utf-8"
|
2889
2903
|
})
|
@@ -2918,17 +2932,17 @@ var DelayedPromise = class {
|
|
2918
2932
|
return this.promise;
|
2919
2933
|
}
|
2920
2934
|
resolve(value) {
|
2921
|
-
var
|
2935
|
+
var _a17;
|
2922
2936
|
this.status = { type: "resolved", value };
|
2923
2937
|
if (this.promise) {
|
2924
|
-
(
|
2938
|
+
(_a17 = this._resolve) == null ? void 0 : _a17.call(this, value);
|
2925
2939
|
}
|
2926
2940
|
}
|
2927
2941
|
reject(error) {
|
2928
|
-
var
|
2942
|
+
var _a17;
|
2929
2943
|
this.status = { type: "rejected", error };
|
2930
2944
|
if (this.promise) {
|
2931
|
-
(
|
2945
|
+
(_a17 = this._reject) == null ? void 0 : _a17.call(this, error);
|
2932
2946
|
}
|
2933
2947
|
}
|
2934
2948
|
};
|
@@ -3032,8 +3046,8 @@ function createStitchableStream() {
|
|
3032
3046
|
|
3033
3047
|
// core/util/now.ts
|
3034
3048
|
function now() {
|
3035
|
-
var
|
3036
|
-
return (_b = (
|
3049
|
+
var _a17, _b;
|
3050
|
+
return (_b = (_a17 = globalThis == null ? void 0 : globalThis.performance) == null ? void 0 : _a17.now()) != null ? _b : Date.now();
|
3037
3051
|
}
|
3038
3052
|
|
3039
3053
|
// core/generate-object/stream-object.ts
|
@@ -3169,7 +3183,7 @@ var DefaultStreamObjectResult = class {
|
|
3169
3183
|
tracer,
|
3170
3184
|
endWhenDone: false,
|
3171
3185
|
fn: async (rootSpan) => {
|
3172
|
-
var
|
3186
|
+
var _a17, _b;
|
3173
3187
|
if (mode === "auto" || mode == null) {
|
3174
3188
|
mode = model.defaultObjectGenerationMode;
|
3175
3189
|
}
|
@@ -3200,7 +3214,7 @@ var DefaultStreamObjectResult = class {
|
|
3200
3214
|
prompt: await convertToLanguageModelPrompt({
|
3201
3215
|
prompt: standardizedPrompt,
|
3202
3216
|
modelSupportsImageUrls: model.supportsImageUrls,
|
3203
|
-
modelSupportsUrl: (
|
3217
|
+
modelSupportsUrl: (_a17 = model.supportsUrl) == null ? void 0 : _a17.bind(model)
|
3204
3218
|
// support 'this' context
|
3205
3219
|
}),
|
3206
3220
|
providerMetadata: providerOptions,
|
@@ -3338,7 +3352,7 @@ var DefaultStreamObjectResult = class {
|
|
3338
3352
|
const transformedStream = stream.pipeThrough(new TransformStream(transformer)).pipeThrough(
|
3339
3353
|
new TransformStream({
|
3340
3354
|
async transform(chunk, controller) {
|
3341
|
-
var
|
3355
|
+
var _a18, _b2, _c;
|
3342
3356
|
if (isFirstChunk) {
|
3343
3357
|
const msToFirstChunk = now2() - startTimestampMs;
|
3344
3358
|
isFirstChunk = false;
|
@@ -3384,7 +3398,7 @@ var DefaultStreamObjectResult = class {
|
|
3384
3398
|
switch (chunk.type) {
|
3385
3399
|
case "response-metadata": {
|
3386
3400
|
response = {
|
3387
|
-
id: (
|
3401
|
+
id: (_a18 = chunk.id) != null ? _a18 : response.id,
|
3388
3402
|
timestamp: (_b2 = chunk.timestamp) != null ? _b2 : response.timestamp,
|
3389
3403
|
modelId: (_c = chunk.modelId) != null ? _c : response.modelId
|
3390
3404
|
};
|
@@ -3597,9 +3611,9 @@ var DefaultStreamObjectResult = class {
|
|
3597
3611
|
});
|
3598
3612
|
}
|
3599
3613
|
toTextStreamResponse(init) {
|
3600
|
-
var
|
3614
|
+
var _a17;
|
3601
3615
|
return new Response(this.textStream.pipeThrough(new TextEncoderStream()), {
|
3602
|
-
status: (
|
3616
|
+
status: (_a17 = init == null ? void 0 : init.status) != null ? _a17 : 200,
|
3603
3617
|
headers: prepareResponseHeaders(init == null ? void 0 : init.headers, {
|
3604
3618
|
contentType: "text/plain; charset=utf-8"
|
3605
3619
|
})
|
@@ -3675,24 +3689,24 @@ function prepareToolsAndToolChoice({
|
|
3675
3689
|
};
|
3676
3690
|
}
|
3677
3691
|
const filteredTools = activeTools != null ? Object.entries(tools).filter(
|
3678
|
-
([
|
3692
|
+
([name17]) => activeTools.includes(name17)
|
3679
3693
|
) : Object.entries(tools);
|
3680
3694
|
return {
|
3681
|
-
tools: filteredTools.map(([
|
3695
|
+
tools: filteredTools.map(([name17, tool2]) => {
|
3682
3696
|
const toolType = tool2.type;
|
3683
3697
|
switch (toolType) {
|
3684
3698
|
case void 0:
|
3685
3699
|
case "function":
|
3686
3700
|
return {
|
3687
3701
|
type: "function",
|
3688
|
-
name:
|
3702
|
+
name: name17,
|
3689
3703
|
description: tool2.description,
|
3690
3704
|
parameters: (0, import_ui_utils4.asSchema)(tool2.parameters).jsonSchema
|
3691
3705
|
};
|
3692
3706
|
case "provider-defined":
|
3693
3707
|
return {
|
3694
3708
|
type: "provider-defined",
|
3695
|
-
name:
|
3709
|
+
name: name17,
|
3696
3710
|
id: tool2.id,
|
3697
3711
|
args: tool2.args
|
3698
3712
|
};
|
@@ -3862,6 +3876,12 @@ async function doParseToolCall({
|
|
3862
3876
|
};
|
3863
3877
|
}
|
3864
3878
|
|
3879
|
+
// core/generate-text/reasoning-detail.ts
|
3880
|
+
function asReasoningText(reasoning) {
|
3881
|
+
const reasoningText = reasoning.filter((part) => part.type === "text").map((part) => part.text).join("");
|
3882
|
+
return reasoningText.length > 0 ? reasoningText : void 0;
|
3883
|
+
}
|
3884
|
+
|
3865
3885
|
// core/generate-text/to-response-messages.ts
|
3866
3886
|
function toResponseMessages({
|
3867
3887
|
text: text2 = "",
|
@@ -3910,12 +3930,6 @@ function toResponseMessages({
|
|
3910
3930
|
return responseMessages;
|
3911
3931
|
}
|
3912
3932
|
|
3913
|
-
// core/generate-text/reasoning-detail.ts
|
3914
|
-
function asReasoningText(reasoning) {
|
3915
|
-
const reasoningText = reasoning.filter((part) => part.type === "text").map((part) => part.text).join("");
|
3916
|
-
return reasoningText.length > 0 ? reasoningText : void 0;
|
3917
|
-
}
|
3918
|
-
|
3919
3933
|
// core/generate-text/generate-text.ts
|
3920
3934
|
var originalGenerateId3 = (0, import_provider_utils9.createIdGenerator)({
|
3921
3935
|
prefix: "aitxt",
|
@@ -3951,7 +3965,7 @@ async function generateText({
|
|
3951
3965
|
onStepFinish,
|
3952
3966
|
...settings
|
3953
3967
|
}) {
|
3954
|
-
var
|
3968
|
+
var _a17;
|
3955
3969
|
if (maxSteps < 1) {
|
3956
3970
|
throw new InvalidArgumentError({
|
3957
3971
|
parameter: "maxSteps",
|
@@ -3968,7 +3982,7 @@ async function generateText({
|
|
3968
3982
|
});
|
3969
3983
|
const initialPrompt = standardizePrompt({
|
3970
3984
|
prompt: {
|
3971
|
-
system: (
|
3985
|
+
system: (_a17 = output == null ? void 0 : output.injectIntoSystemPrompt({ system, model })) != null ? _a17 : system,
|
3972
3986
|
prompt,
|
3973
3987
|
messages
|
3974
3988
|
},
|
@@ -3994,7 +4008,7 @@ async function generateText({
|
|
3994
4008
|
}),
|
3995
4009
|
tracer,
|
3996
4010
|
fn: async (span) => {
|
3997
|
-
var
|
4011
|
+
var _a18, _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
3998
4012
|
const mode = {
|
3999
4013
|
type: "regular",
|
4000
4014
|
...prepareToolsAndToolChoice({ tools, toolChoice, activeTools })
|
@@ -4028,7 +4042,7 @@ async function generateText({
|
|
4028
4042
|
messages: stepInputMessages
|
4029
4043
|
},
|
4030
4044
|
modelSupportsImageUrls: model.supportsImageUrls,
|
4031
|
-
modelSupportsUrl: (
|
4045
|
+
modelSupportsUrl: (_a18 = model.supportsUrl) == null ? void 0 : _a18.bind(model)
|
4032
4046
|
// support 'this' context
|
4033
4047
|
});
|
4034
4048
|
currentModelResponse = await retry(
|
@@ -4049,8 +4063,8 @@ async function generateText({
|
|
4049
4063
|
"ai.prompt.tools": {
|
4050
4064
|
// convert the language model level tools:
|
4051
4065
|
input: () => {
|
4052
|
-
var
|
4053
|
-
return (
|
4066
|
+
var _a19;
|
4067
|
+
return (_a19 = mode.tools) == null ? void 0 : _a19.map((tool2) => JSON.stringify(tool2));
|
4054
4068
|
}
|
4055
4069
|
},
|
4056
4070
|
"ai.prompt.toolChoice": {
|
@@ -4070,7 +4084,7 @@ async function generateText({
|
|
4070
4084
|
}),
|
4071
4085
|
tracer,
|
4072
4086
|
fn: async (span2) => {
|
4073
|
-
var
|
4087
|
+
var _a19, _b2, _c2, _d2, _e2, _f2;
|
4074
4088
|
const result = await model.doGenerate({
|
4075
4089
|
mode,
|
4076
4090
|
...callSettings,
|
@@ -4082,7 +4096,7 @@ async function generateText({
|
|
4082
4096
|
headers
|
4083
4097
|
});
|
4084
4098
|
const responseData = {
|
4085
|
-
id: (_b2 = (
|
4099
|
+
id: (_b2 = (_a19 = result.response) == null ? void 0 : _a19.id) != null ? _b2 : generateId3(),
|
4086
4100
|
timestamp: (_d2 = (_c2 = result.response) == null ? void 0 : _c2.timestamp) != null ? _d2 : currentDate(),
|
4087
4101
|
modelId: (_f2 = (_e2 = result.response) == null ? void 0 : _e2.modelId) != null ? _f2 : model.modelId
|
4088
4102
|
};
|
@@ -4378,7 +4392,7 @@ var import_provider_utils10 = require("@ai-sdk/provider-utils");
|
|
4378
4392
|
var import_ui_utils6 = require("@ai-sdk/ui-utils");
|
4379
4393
|
|
4380
4394
|
// errors/index.ts
|
4381
|
-
var
|
4395
|
+
var import_provider20 = require("@ai-sdk/provider");
|
4382
4396
|
|
4383
4397
|
// errors/invalid-stream-part-error.ts
|
4384
4398
|
var import_provider18 = require("@ai-sdk/provider");
|
@@ -4401,6 +4415,27 @@ var InvalidStreamPartError = class extends import_provider18.AISDKError {
|
|
4401
4415
|
};
|
4402
4416
|
_a14 = symbol14;
|
4403
4417
|
|
4418
|
+
// errors/mcp-client-error.ts
|
4419
|
+
var import_provider19 = require("@ai-sdk/provider");
|
4420
|
+
var name15 = "AI_MCPClientError";
|
4421
|
+
var marker15 = `vercel.ai.error.${name15}`;
|
4422
|
+
var symbol15 = Symbol.for(marker15);
|
4423
|
+
var _a15;
|
4424
|
+
var MCPClientError = class extends import_provider19.AISDKError {
|
4425
|
+
constructor({
|
4426
|
+
name: name17 = "MCPClientError",
|
4427
|
+
message,
|
4428
|
+
cause
|
4429
|
+
}) {
|
4430
|
+
super({ name: name17, message, cause });
|
4431
|
+
this[_a15] = true;
|
4432
|
+
}
|
4433
|
+
static isInstance(error) {
|
4434
|
+
return import_provider19.AISDKError.hasMarker(error, marker15);
|
4435
|
+
}
|
4436
|
+
};
|
4437
|
+
_a15 = symbol15;
|
4438
|
+
|
4404
4439
|
// core/generate-text/output.ts
|
4405
4440
|
var text = () => ({
|
4406
4441
|
type: "text",
|
@@ -4479,7 +4514,7 @@ var object = ({
|
|
4479
4514
|
};
|
4480
4515
|
|
4481
4516
|
// core/generate-text/smooth-stream.ts
|
4482
|
-
var
|
4517
|
+
var import_provider21 = require("@ai-sdk/provider");
|
4483
4518
|
var import_provider_utils11 = require("@ai-sdk/provider-utils");
|
4484
4519
|
var CHUNKING_REGEXPS = {
|
4485
4520
|
word: /\s*\S+\s+/m,
|
@@ -4492,7 +4527,7 @@ function smoothStream({
|
|
4492
4527
|
} = {}) {
|
4493
4528
|
const chunkingRegexp = typeof chunking === "string" ? CHUNKING_REGEXPS[chunking] : chunking;
|
4494
4529
|
if (chunkingRegexp == null) {
|
4495
|
-
throw new
|
4530
|
+
throw new import_provider21.InvalidArgumentError({
|
4496
4531
|
argument: "chunking",
|
4497
4532
|
message: `Chunking must be "word" or "line" or a RegExp. Received: ${chunking}`
|
4498
4533
|
});
|
@@ -4527,7 +4562,7 @@ function smoothStream({
|
|
4527
4562
|
}
|
4528
4563
|
|
4529
4564
|
// core/generate-text/stream-text.ts
|
4530
|
-
var
|
4565
|
+
var import_provider22 = require("@ai-sdk/provider");
|
4531
4566
|
var import_provider_utils12 = require("@ai-sdk/provider-utils");
|
4532
4567
|
var import_ui_utils8 = require("@ai-sdk/ui-utils");
|
4533
4568
|
|
@@ -4981,7 +5016,7 @@ var DefaultStreamTextResult = class {
|
|
4981
5016
|
this.requestPromise = new DelayedPromise();
|
4982
5017
|
this.responsePromise = new DelayedPromise();
|
4983
5018
|
this.stepsPromise = new DelayedPromise();
|
4984
|
-
var
|
5019
|
+
var _a17;
|
4985
5020
|
if (maxSteps < 1) {
|
4986
5021
|
throw new InvalidArgumentError({
|
4987
5022
|
parameter: "maxSteps",
|
@@ -5035,7 +5070,7 @@ var DefaultStreamTextResult = class {
|
|
5035
5070
|
}
|
5036
5071
|
if (part.type === "reasoning-signature") {
|
5037
5072
|
if (activeReasoningText == null) {
|
5038
|
-
throw new
|
5073
|
+
throw new import_provider22.AISDKError({
|
5039
5074
|
name: "InvalidStreamPart",
|
5040
5075
|
message: "reasoning-signature without reasoning"
|
5041
5076
|
});
|
@@ -5127,7 +5162,7 @@ var DefaultStreamTextResult = class {
|
|
5127
5162
|
}
|
5128
5163
|
},
|
5129
5164
|
async flush(controller) {
|
5130
|
-
var
|
5165
|
+
var _a18;
|
5131
5166
|
try {
|
5132
5167
|
if (recordedSteps.length === 0) {
|
5133
5168
|
return;
|
@@ -5164,7 +5199,7 @@ var DefaultStreamTextResult = class {
|
|
5164
5199
|
sources: lastStep.sources,
|
5165
5200
|
toolCalls: lastStep.toolCalls,
|
5166
5201
|
toolResults: lastStep.toolResults,
|
5167
|
-
request: (
|
5202
|
+
request: (_a18 = lastStep.request) != null ? _a18 : {},
|
5168
5203
|
response: lastStep.response,
|
5169
5204
|
warnings: lastStep.warnings,
|
5170
5205
|
providerMetadata: lastStep.providerMetadata,
|
@@ -5179,8 +5214,8 @@ var DefaultStreamTextResult = class {
|
|
5179
5214
|
"ai.response.text": { output: () => recordedFullText },
|
5180
5215
|
"ai.response.toolCalls": {
|
5181
5216
|
output: () => {
|
5182
|
-
var
|
5183
|
-
return ((
|
5217
|
+
var _a19;
|
5218
|
+
return ((_a19 = lastStep.toolCalls) == null ? void 0 : _a19.length) ? JSON.stringify(lastStep.toolCalls) : void 0;
|
5184
5219
|
}
|
5185
5220
|
},
|
5186
5221
|
"ai.usage.promptTokens": usage.promptTokens,
|
@@ -5222,7 +5257,7 @@ var DefaultStreamTextResult = class {
|
|
5222
5257
|
});
|
5223
5258
|
const initialPrompt = standardizePrompt({
|
5224
5259
|
prompt: {
|
5225
|
-
system: (
|
5260
|
+
system: (_a17 = output == null ? void 0 : output.injectIntoSystemPrompt({ system, model })) != null ? _a17 : system,
|
5226
5261
|
prompt,
|
5227
5262
|
messages
|
5228
5263
|
},
|
@@ -5256,7 +5291,7 @@ var DefaultStreamTextResult = class {
|
|
5256
5291
|
hasLeadingWhitespace,
|
5257
5292
|
messageId
|
5258
5293
|
}) {
|
5259
|
-
var
|
5294
|
+
var _a18;
|
5260
5295
|
const promptFormat = responseMessages.length === 0 ? initialPrompt.type : "messages";
|
5261
5296
|
const stepInputMessages = [
|
5262
5297
|
...initialPrompt.messages,
|
@@ -5269,7 +5304,7 @@ var DefaultStreamTextResult = class {
|
|
5269
5304
|
messages: stepInputMessages
|
5270
5305
|
},
|
5271
5306
|
modelSupportsImageUrls: model.supportsImageUrls,
|
5272
|
-
modelSupportsUrl: (
|
5307
|
+
modelSupportsUrl: (_a18 = model.supportsUrl) == null ? void 0 : _a18.bind(model)
|
5273
5308
|
// support 'this' context
|
5274
5309
|
});
|
5275
5310
|
const mode = {
|
@@ -5300,8 +5335,8 @@ var DefaultStreamTextResult = class {
|
|
5300
5335
|
"ai.prompt.tools": {
|
5301
5336
|
// convert the language model level tools:
|
5302
5337
|
input: () => {
|
5303
|
-
var
|
5304
|
-
return (
|
5338
|
+
var _a19;
|
5339
|
+
return (_a19 = mode.tools) == null ? void 0 : _a19.map((tool2) => JSON.stringify(tool2));
|
5305
5340
|
}
|
5306
5341
|
},
|
5307
5342
|
"ai.prompt.toolChoice": {
|
@@ -5388,7 +5423,7 @@ var DefaultStreamTextResult = class {
|
|
5388
5423
|
transformedStream.pipeThrough(
|
5389
5424
|
new TransformStream({
|
5390
5425
|
async transform(chunk, controller) {
|
5391
|
-
var
|
5426
|
+
var _a19, _b, _c;
|
5392
5427
|
if (stepFirstChunk) {
|
5393
5428
|
const msToFirstChunk = now2() - startTimestampMs;
|
5394
5429
|
stepFirstChunk = false;
|
@@ -5479,7 +5514,7 @@ var DefaultStreamTextResult = class {
|
|
5479
5514
|
}
|
5480
5515
|
case "response-metadata": {
|
5481
5516
|
stepResponse = {
|
5482
|
-
id: (
|
5517
|
+
id: (_a19 = chunk.id) != null ? _a19 : stepResponse.id,
|
5483
5518
|
timestamp: (_b = chunk.timestamp) != null ? _b : stepResponse.timestamp,
|
5484
5519
|
modelId: (_c = chunk.modelId) != null ? _c : stepResponse.modelId
|
5485
5520
|
};
|
@@ -6002,9 +6037,9 @@ var DefaultStreamTextResult = class {
|
|
6002
6037
|
);
|
6003
6038
|
}
|
6004
6039
|
toTextStreamResponse(init) {
|
6005
|
-
var
|
6040
|
+
var _a17;
|
6006
6041
|
return new Response(this.textStream.pipeThrough(new TextEncoderStream()), {
|
6007
|
-
status: (
|
6042
|
+
status: (_a17 = init == null ? void 0 : init.status) != null ? _a17 : 200,
|
6008
6043
|
headers: prepareResponseHeaders(init == null ? void 0 : init.headers, {
|
6009
6044
|
contentType: "text/plain; charset=utf-8"
|
6010
6045
|
})
|
@@ -6122,6 +6157,89 @@ function extractReasoningMiddleware({
|
|
6122
6157
|
};
|
6123
6158
|
}
|
6124
6159
|
|
6160
|
+
// core/middleware/simulate-streaming-middleware.ts
|
6161
|
+
function simulateStreamingMiddleware() {
|
6162
|
+
return {
|
6163
|
+
middlewareVersion: "v1",
|
6164
|
+
wrapStream: async ({ doGenerate }) => {
|
6165
|
+
const result = await doGenerate();
|
6166
|
+
const simulatedStream = new ReadableStream({
|
6167
|
+
start(controller) {
|
6168
|
+
controller.enqueue({ type: "response-metadata", ...result.response });
|
6169
|
+
if (result.reasoning) {
|
6170
|
+
if (typeof result.reasoning === "string") {
|
6171
|
+
controller.enqueue({
|
6172
|
+
type: "reasoning",
|
6173
|
+
textDelta: result.reasoning
|
6174
|
+
});
|
6175
|
+
} else {
|
6176
|
+
for (const reasoning of result.reasoning) {
|
6177
|
+
switch (reasoning.type) {
|
6178
|
+
case "text": {
|
6179
|
+
controller.enqueue({
|
6180
|
+
type: "reasoning",
|
6181
|
+
textDelta: reasoning.text
|
6182
|
+
});
|
6183
|
+
if (reasoning.signature != null) {
|
6184
|
+
controller.enqueue({
|
6185
|
+
type: "reasoning-signature",
|
6186
|
+
signature: reasoning.signature
|
6187
|
+
});
|
6188
|
+
}
|
6189
|
+
break;
|
6190
|
+
}
|
6191
|
+
case "redacted": {
|
6192
|
+
controller.enqueue({
|
6193
|
+
type: "redacted-reasoning",
|
6194
|
+
data: reasoning.data
|
6195
|
+
});
|
6196
|
+
break;
|
6197
|
+
}
|
6198
|
+
}
|
6199
|
+
}
|
6200
|
+
}
|
6201
|
+
}
|
6202
|
+
if (result.text) {
|
6203
|
+
controller.enqueue({
|
6204
|
+
type: "text-delta",
|
6205
|
+
textDelta: result.text
|
6206
|
+
});
|
6207
|
+
}
|
6208
|
+
if (result.toolCalls) {
|
6209
|
+
for (const toolCall of result.toolCalls) {
|
6210
|
+
controller.enqueue({
|
6211
|
+
type: "tool-call-delta",
|
6212
|
+
toolCallType: "function",
|
6213
|
+
toolCallId: toolCall.toolCallId,
|
6214
|
+
toolName: toolCall.toolName,
|
6215
|
+
argsTextDelta: toolCall.args
|
6216
|
+
});
|
6217
|
+
controller.enqueue({
|
6218
|
+
type: "tool-call",
|
6219
|
+
...toolCall
|
6220
|
+
});
|
6221
|
+
}
|
6222
|
+
}
|
6223
|
+
controller.enqueue({
|
6224
|
+
type: "finish",
|
6225
|
+
finishReason: result.finishReason,
|
6226
|
+
usage: result.usage,
|
6227
|
+
logprobs: result.logprobs,
|
6228
|
+
providerMetadata: result.providerMetadata
|
6229
|
+
});
|
6230
|
+
controller.close();
|
6231
|
+
}
|
6232
|
+
});
|
6233
|
+
return {
|
6234
|
+
stream: simulatedStream,
|
6235
|
+
rawCall: result.rawCall,
|
6236
|
+
rawResponse: result.rawResponse,
|
6237
|
+
warnings: result.warnings
|
6238
|
+
};
|
6239
|
+
}
|
6240
|
+
};
|
6241
|
+
}
|
6242
|
+
|
6125
6243
|
// core/middleware/wrap-language-model.ts
|
6126
6244
|
var wrapLanguageModel = ({
|
6127
6245
|
model,
|
@@ -6139,7 +6257,7 @@ var doWrap = ({
|
|
6139
6257
|
modelId,
|
6140
6258
|
providerId
|
6141
6259
|
}) => {
|
6142
|
-
var
|
6260
|
+
var _a17;
|
6143
6261
|
async function doTransform({
|
6144
6262
|
params,
|
6145
6263
|
type
|
@@ -6152,17 +6270,24 @@ var doWrap = ({
|
|
6152
6270
|
modelId: modelId != null ? modelId : model.modelId,
|
6153
6271
|
defaultObjectGenerationMode: model.defaultObjectGenerationMode,
|
6154
6272
|
supportsImageUrls: model.supportsImageUrls,
|
6155
|
-
supportsUrl: (
|
6273
|
+
supportsUrl: (_a17 = model.supportsUrl) == null ? void 0 : _a17.bind(model),
|
6156
6274
|
supportsStructuredOutputs: model.supportsStructuredOutputs,
|
6157
6275
|
async doGenerate(params) {
|
6158
6276
|
const transformedParams = await doTransform({ params, type: "generate" });
|
6159
6277
|
const doGenerate = async () => model.doGenerate(transformedParams);
|
6160
|
-
|
6278
|
+
const doStream = async () => model.doStream(transformedParams);
|
6279
|
+
return wrapGenerate ? wrapGenerate({
|
6280
|
+
doGenerate,
|
6281
|
+
doStream,
|
6282
|
+
params: transformedParams,
|
6283
|
+
model
|
6284
|
+
}) : doGenerate();
|
6161
6285
|
},
|
6162
6286
|
async doStream(params) {
|
6163
6287
|
const transformedParams = await doTransform({ params, type: "stream" });
|
6288
|
+
const doGenerate = async () => model.doGenerate(transformedParams);
|
6164
6289
|
const doStream = async () => model.doStream(transformedParams);
|
6165
|
-
return wrapStream ? wrapStream({ doStream, params: transformedParams, model }) : doStream();
|
6290
|
+
return wrapStream ? wrapStream({ doGenerate, doStream, params: transformedParams, model }) : doStream();
|
6166
6291
|
}
|
6167
6292
|
};
|
6168
6293
|
};
|
@@ -6186,7 +6311,7 @@ function appendResponseMessages({
|
|
6186
6311
|
responseMessages,
|
6187
6312
|
_internal: { currentDate = () => /* @__PURE__ */ new Date() } = {}
|
6188
6313
|
}) {
|
6189
|
-
var
|
6314
|
+
var _a17, _b, _c, _d;
|
6190
6315
|
const clonedMessages = structuredClone(messages);
|
6191
6316
|
for (const message of responseMessages) {
|
6192
6317
|
const role = message.role;
|
@@ -6268,7 +6393,7 @@ function appendResponseMessages({
|
|
6268
6393
|
const maxStep = (0, import_ui_utils9.extractMaxToolInvocationStep)(
|
6269
6394
|
lastMessage.toolInvocations
|
6270
6395
|
);
|
6271
|
-
(
|
6396
|
+
(_a17 = lastMessage.parts) != null ? _a17 : lastMessage.parts = [];
|
6272
6397
|
lastMessage.content = textContent;
|
6273
6398
|
lastMessage.reasoning = reasoningTextContent;
|
6274
6399
|
lastMessage.parts.push(...parts);
|
@@ -6344,7 +6469,7 @@ function appendResponseMessages({
|
|
6344
6469
|
}
|
6345
6470
|
|
6346
6471
|
// core/registry/custom-provider.ts
|
6347
|
-
var
|
6472
|
+
var import_provider23 = require("@ai-sdk/provider");
|
6348
6473
|
function customProvider({
|
6349
6474
|
languageModels,
|
6350
6475
|
textEmbeddingModels,
|
@@ -6359,7 +6484,7 @@ function customProvider({
|
|
6359
6484
|
if (fallbackProvider) {
|
6360
6485
|
return fallbackProvider.languageModel(modelId);
|
6361
6486
|
}
|
6362
|
-
throw new
|
6487
|
+
throw new import_provider23.NoSuchModelError({ modelId, modelType: "languageModel" });
|
6363
6488
|
},
|
6364
6489
|
textEmbeddingModel(modelId) {
|
6365
6490
|
if (textEmbeddingModels != null && modelId in textEmbeddingModels) {
|
@@ -6368,7 +6493,7 @@ function customProvider({
|
|
6368
6493
|
if (fallbackProvider) {
|
6369
6494
|
return fallbackProvider.textEmbeddingModel(modelId);
|
6370
6495
|
}
|
6371
|
-
throw new
|
6496
|
+
throw new import_provider23.NoSuchModelError({ modelId, modelType: "textEmbeddingModel" });
|
6372
6497
|
},
|
6373
6498
|
imageModel(modelId) {
|
6374
6499
|
if (imageModels != null && modelId in imageModels) {
|
@@ -6377,19 +6502,19 @@ function customProvider({
|
|
6377
6502
|
if (fallbackProvider == null ? void 0 : fallbackProvider.imageModel) {
|
6378
6503
|
return fallbackProvider.imageModel(modelId);
|
6379
6504
|
}
|
6380
|
-
throw new
|
6505
|
+
throw new import_provider23.NoSuchModelError({ modelId, modelType: "imageModel" });
|
6381
6506
|
}
|
6382
6507
|
};
|
6383
6508
|
}
|
6384
6509
|
var experimental_customProvider = customProvider;
|
6385
6510
|
|
6386
6511
|
// core/registry/no-such-provider-error.ts
|
6387
|
-
var
|
6388
|
-
var
|
6389
|
-
var
|
6390
|
-
var
|
6391
|
-
var
|
6392
|
-
var NoSuchProviderError = class extends
|
6512
|
+
var import_provider24 = require("@ai-sdk/provider");
|
6513
|
+
var name16 = "AI_NoSuchProviderError";
|
6514
|
+
var marker16 = `vercel.ai.error.${name16}`;
|
6515
|
+
var symbol16 = Symbol.for(marker16);
|
6516
|
+
var _a16;
|
6517
|
+
var NoSuchProviderError = class extends import_provider24.NoSuchModelError {
|
6393
6518
|
constructor({
|
6394
6519
|
modelId,
|
6395
6520
|
modelType,
|
@@ -6397,19 +6522,19 @@ var NoSuchProviderError = class extends import_provider23.NoSuchModelError {
|
|
6397
6522
|
availableProviders,
|
6398
6523
|
message = `No such provider: ${providerId} (available providers: ${availableProviders.join()})`
|
6399
6524
|
}) {
|
6400
|
-
super({ errorName:
|
6401
|
-
this[
|
6525
|
+
super({ errorName: name16, modelId, modelType, message });
|
6526
|
+
this[_a16] = true;
|
6402
6527
|
this.providerId = providerId;
|
6403
6528
|
this.availableProviders = availableProviders;
|
6404
6529
|
}
|
6405
6530
|
static isInstance(error) {
|
6406
|
-
return
|
6531
|
+
return import_provider24.AISDKError.hasMarker(error, marker16);
|
6407
6532
|
}
|
6408
6533
|
};
|
6409
|
-
|
6534
|
+
_a16 = symbol16;
|
6410
6535
|
|
6411
6536
|
// core/registry/provider-registry.ts
|
6412
|
-
var
|
6537
|
+
var import_provider25 = require("@ai-sdk/provider");
|
6413
6538
|
function experimental_createProviderRegistry(providers) {
|
6414
6539
|
const registry = new DefaultProviderRegistry();
|
6415
6540
|
for (const [id, provider] of Object.entries(providers)) {
|
@@ -6442,7 +6567,7 @@ var DefaultProviderRegistry = class {
|
|
6442
6567
|
splitId(id, modelType) {
|
6443
6568
|
const index = id.indexOf(":");
|
6444
6569
|
if (index === -1) {
|
6445
|
-
throw new
|
6570
|
+
throw new import_provider25.NoSuchModelError({
|
6446
6571
|
modelId: id,
|
6447
6572
|
modelType,
|
6448
6573
|
message: `Invalid ${modelType} id for registry: ${id} (must be in the format "providerId:modelId")`
|
@@ -6451,21 +6576,21 @@ var DefaultProviderRegistry = class {
|
|
6451
6576
|
return [id.slice(0, index), id.slice(index + 1)];
|
6452
6577
|
}
|
6453
6578
|
languageModel(id) {
|
6454
|
-
var
|
6579
|
+
var _a17, _b;
|
6455
6580
|
const [providerId, modelId] = this.splitId(id, "languageModel");
|
6456
|
-
const model = (_b = (
|
6581
|
+
const model = (_b = (_a17 = this.getProvider(providerId)).languageModel) == null ? void 0 : _b.call(_a17, modelId);
|
6457
6582
|
if (model == null) {
|
6458
|
-
throw new
|
6583
|
+
throw new import_provider25.NoSuchModelError({ modelId: id, modelType: "languageModel" });
|
6459
6584
|
}
|
6460
6585
|
return model;
|
6461
6586
|
}
|
6462
6587
|
textEmbeddingModel(id) {
|
6463
|
-
var
|
6588
|
+
var _a17;
|
6464
6589
|
const [providerId, modelId] = this.splitId(id, "textEmbeddingModel");
|
6465
6590
|
const provider = this.getProvider(providerId);
|
6466
|
-
const model = (
|
6591
|
+
const model = (_a17 = provider.textEmbeddingModel) == null ? void 0 : _a17.call(provider, modelId);
|
6467
6592
|
if (model == null) {
|
6468
|
-
throw new
|
6593
|
+
throw new import_provider25.NoSuchModelError({
|
6469
6594
|
modelId: id,
|
6470
6595
|
modelType: "textEmbeddingModel"
|
6471
6596
|
});
|
@@ -6473,12 +6598,12 @@ var DefaultProviderRegistry = class {
|
|
6473
6598
|
return model;
|
6474
6599
|
}
|
6475
6600
|
imageModel(id) {
|
6476
|
-
var
|
6601
|
+
var _a17;
|
6477
6602
|
const [providerId, modelId] = this.splitId(id, "imageModel");
|
6478
6603
|
const provider = this.getProvider(providerId);
|
6479
|
-
const model = (
|
6604
|
+
const model = (_a17 = provider.imageModel) == null ? void 0 : _a17.call(provider, modelId);
|
6480
6605
|
if (model == null) {
|
6481
|
-
throw new
|
6606
|
+
throw new import_provider25.NoSuchModelError({ modelId: id, modelType: "imageModel" });
|
6482
6607
|
}
|
6483
6608
|
return model;
|
6484
6609
|
}
|
@@ -6490,11 +6615,727 @@ var DefaultProviderRegistry = class {
|
|
6490
6615
|
}
|
6491
6616
|
};
|
6492
6617
|
|
6618
|
+
// core/tool/mcp/mcp-client.ts
|
6619
|
+
var import_ui_utils10 = require("@ai-sdk/ui-utils");
|
6620
|
+
|
6493
6621
|
// core/tool/tool.ts
|
6494
6622
|
function tool(tool2) {
|
6495
6623
|
return tool2;
|
6496
6624
|
}
|
6497
6625
|
|
6626
|
+
// core/tool/mcp/types.ts
|
6627
|
+
var import_zod8 = require("zod");
|
6628
|
+
var LATEST_PROTOCOL_VERSION = "2024-11-05";
|
6629
|
+
var SUPPORTED_PROTOCOL_VERSIONS = [
|
6630
|
+
LATEST_PROTOCOL_VERSION,
|
6631
|
+
"2024-10-07"
|
6632
|
+
];
|
6633
|
+
var JSONRPC_VERSION = "2.0";
|
6634
|
+
var ClientOrServerImplementationSchema = import_zod8.z.object({
|
6635
|
+
name: import_zod8.z.string(),
|
6636
|
+
version: import_zod8.z.string()
|
6637
|
+
}).passthrough();
|
6638
|
+
var BaseParamsSchema = import_zod8.z.object({
|
6639
|
+
_meta: import_zod8.z.optional(import_zod8.z.object({}).passthrough())
|
6640
|
+
}).passthrough();
|
6641
|
+
var RequestSchema = import_zod8.z.object({
|
6642
|
+
method: import_zod8.z.string(),
|
6643
|
+
params: import_zod8.z.optional(BaseParamsSchema)
|
6644
|
+
});
|
6645
|
+
var ResultSchema = BaseParamsSchema;
|
6646
|
+
var NotificationSchema = import_zod8.z.object({
|
6647
|
+
method: import_zod8.z.string(),
|
6648
|
+
params: import_zod8.z.optional(BaseParamsSchema)
|
6649
|
+
});
|
6650
|
+
var RequestIdSchema = import_zod8.z.union([import_zod8.z.string(), import_zod8.z.number().int()]);
|
6651
|
+
var JSONRPCRequestSchema = import_zod8.z.object({
|
6652
|
+
jsonrpc: import_zod8.z.literal(JSONRPC_VERSION),
|
6653
|
+
id: RequestIdSchema
|
6654
|
+
}).merge(RequestSchema).strict();
|
6655
|
+
var JSONRPCResponseSchema = import_zod8.z.object({
|
6656
|
+
jsonrpc: import_zod8.z.literal(JSONRPC_VERSION),
|
6657
|
+
id: RequestIdSchema,
|
6658
|
+
result: ResultSchema
|
6659
|
+
}).strict();
|
6660
|
+
var JSONRPCErrorSchema = import_zod8.z.object({
|
6661
|
+
jsonrpc: import_zod8.z.literal(JSONRPC_VERSION),
|
6662
|
+
id: RequestIdSchema,
|
6663
|
+
error: import_zod8.z.object({
|
6664
|
+
code: import_zod8.z.number().int(),
|
6665
|
+
message: import_zod8.z.string(),
|
6666
|
+
data: import_zod8.z.optional(import_zod8.z.unknown())
|
6667
|
+
})
|
6668
|
+
}).strict();
|
6669
|
+
var JSONRPCNotificationSchema = import_zod8.z.object({
|
6670
|
+
jsonrpc: import_zod8.z.literal(JSONRPC_VERSION)
|
6671
|
+
}).merge(NotificationSchema).strict();
|
6672
|
+
var JSONRPCMessageSchema = import_zod8.z.union([
|
6673
|
+
JSONRPCRequestSchema,
|
6674
|
+
JSONRPCNotificationSchema,
|
6675
|
+
JSONRPCResponseSchema,
|
6676
|
+
JSONRPCErrorSchema
|
6677
|
+
]);
|
6678
|
+
var ServerCapabilitiesSchema = import_zod8.z.object({
|
6679
|
+
experimental: import_zod8.z.optional(import_zod8.z.object({}).passthrough()),
|
6680
|
+
logging: import_zod8.z.optional(import_zod8.z.object({}).passthrough()),
|
6681
|
+
prompts: import_zod8.z.optional(
|
6682
|
+
import_zod8.z.object({
|
6683
|
+
listChanged: import_zod8.z.optional(import_zod8.z.boolean())
|
6684
|
+
}).passthrough()
|
6685
|
+
),
|
6686
|
+
resources: import_zod8.z.optional(
|
6687
|
+
import_zod8.z.object({
|
6688
|
+
subscribe: import_zod8.z.optional(import_zod8.z.boolean()),
|
6689
|
+
listChanged: import_zod8.z.optional(import_zod8.z.boolean())
|
6690
|
+
}).passthrough()
|
6691
|
+
),
|
6692
|
+
tools: import_zod8.z.optional(
|
6693
|
+
import_zod8.z.object({
|
6694
|
+
listChanged: import_zod8.z.optional(import_zod8.z.boolean())
|
6695
|
+
}).passthrough()
|
6696
|
+
)
|
6697
|
+
}).passthrough();
|
6698
|
+
var InitializeResultSchema = ResultSchema.extend({
|
6699
|
+
protocolVersion: import_zod8.z.string(),
|
6700
|
+
capabilities: ServerCapabilitiesSchema,
|
6701
|
+
serverInfo: ClientOrServerImplementationSchema,
|
6702
|
+
instructions: import_zod8.z.optional(import_zod8.z.string())
|
6703
|
+
});
|
6704
|
+
var PaginatedResultSchema = ResultSchema.extend({
|
6705
|
+
nextCursor: import_zod8.z.optional(import_zod8.z.string())
|
6706
|
+
});
|
6707
|
+
var ToolSchema = import_zod8.z.object({
|
6708
|
+
name: import_zod8.z.string(),
|
6709
|
+
description: import_zod8.z.optional(import_zod8.z.string()),
|
6710
|
+
inputSchema: import_zod8.z.object({
|
6711
|
+
type: import_zod8.z.literal("object"),
|
6712
|
+
properties: import_zod8.z.optional(import_zod8.z.object({}).passthrough())
|
6713
|
+
}).passthrough()
|
6714
|
+
}).passthrough();
|
6715
|
+
var ListToolsResultSchema = PaginatedResultSchema.extend({
|
6716
|
+
tools: import_zod8.z.array(ToolSchema)
|
6717
|
+
});
|
6718
|
+
var TextContentSchema = import_zod8.z.object({
|
6719
|
+
type: import_zod8.z.literal("text"),
|
6720
|
+
text: import_zod8.z.string()
|
6721
|
+
}).passthrough();
|
6722
|
+
var ImageContentSchema = import_zod8.z.object({
|
6723
|
+
type: import_zod8.z.literal("image"),
|
6724
|
+
data: import_zod8.z.string().base64(),
|
6725
|
+
mimeType: import_zod8.z.string()
|
6726
|
+
}).passthrough();
|
6727
|
+
var ResourceContentsSchema = import_zod8.z.object({
|
6728
|
+
/**
|
6729
|
+
* The URI of this resource.
|
6730
|
+
*/
|
6731
|
+
uri: import_zod8.z.string(),
|
6732
|
+
/**
|
6733
|
+
* The MIME type of this resource, if known.
|
6734
|
+
*/
|
6735
|
+
mimeType: import_zod8.z.optional(import_zod8.z.string())
|
6736
|
+
}).passthrough();
|
6737
|
+
var TextResourceContentsSchema = ResourceContentsSchema.extend({
|
6738
|
+
text: import_zod8.z.string()
|
6739
|
+
});
|
6740
|
+
var BlobResourceContentsSchema = ResourceContentsSchema.extend({
|
6741
|
+
blob: import_zod8.z.string().base64()
|
6742
|
+
});
|
6743
|
+
var EmbeddedResourceSchema = import_zod8.z.object({
|
6744
|
+
type: import_zod8.z.literal("resource"),
|
6745
|
+
resource: import_zod8.z.union([TextResourceContentsSchema, BlobResourceContentsSchema])
|
6746
|
+
}).passthrough();
|
6747
|
+
var CallToolResultSchema = ResultSchema.extend({
|
6748
|
+
content: import_zod8.z.array(
|
6749
|
+
import_zod8.z.union([TextContentSchema, ImageContentSchema, EmbeddedResourceSchema])
|
6750
|
+
),
|
6751
|
+
isError: import_zod8.z.boolean().default(false).optional()
|
6752
|
+
}).or(
|
6753
|
+
ResultSchema.extend({
|
6754
|
+
toolResult: import_zod8.z.unknown()
|
6755
|
+
})
|
6756
|
+
);
|
6757
|
+
|
6758
|
+
// core/tool/mcp/utils.ts
|
6759
|
+
function detectRuntime() {
|
6760
|
+
var _a17, _b;
|
6761
|
+
if (typeof window !== "undefined") {
|
6762
|
+
return "browser";
|
6763
|
+
}
|
6764
|
+
if (((_b = (_a17 = globalThis.process) == null ? void 0 : _a17.release) == null ? void 0 : _b.name) === "node") {
|
6765
|
+
return "node";
|
6766
|
+
}
|
6767
|
+
return null;
|
6768
|
+
}
|
6769
|
+
async function createChildProcess(config, signal) {
|
6770
|
+
var _a17, _b, _c;
|
6771
|
+
const runtime = detectRuntime();
|
6772
|
+
if (runtime !== "node") {
|
6773
|
+
throw new MCPClientError({
|
6774
|
+
message: "Attempted to use child_process module outside of Node.js environment"
|
6775
|
+
});
|
6776
|
+
}
|
6777
|
+
let childProcess;
|
6778
|
+
try {
|
6779
|
+
childProcess = await import("child_process");
|
6780
|
+
} catch (error) {
|
6781
|
+
try {
|
6782
|
+
childProcess = require("child_process");
|
6783
|
+
} catch (innerError) {
|
6784
|
+
throw new MCPClientError({
|
6785
|
+
message: "Failed to load child_process module dynamically",
|
6786
|
+
cause: innerError
|
6787
|
+
});
|
6788
|
+
}
|
6789
|
+
}
|
6790
|
+
const { spawn } = childProcess;
|
6791
|
+
return spawn(config.command, (_a17 = config.args) != null ? _a17 : [], {
|
6792
|
+
env: (_b = config.env) != null ? _b : getDefaultEnvironment(),
|
6793
|
+
stdio: ["pipe", "pipe", (_c = config.stderr) != null ? _c : "inherit"],
|
6794
|
+
shell: false,
|
6795
|
+
signal,
|
6796
|
+
windowsHide: process.platform === "win32" && isElectron(),
|
6797
|
+
cwd: config.cwd
|
6798
|
+
});
|
6799
|
+
}
|
6800
|
+
var DEFAULT_INHERITED_ENV_VARS = process.platform === "win32" ? [
|
6801
|
+
"APPDATA",
|
6802
|
+
"HOMEDRIVE",
|
6803
|
+
"HOMEPATH",
|
6804
|
+
"LOCALAPPDATA",
|
6805
|
+
"PATH",
|
6806
|
+
"PROCESSOR_ARCHITECTURE",
|
6807
|
+
"SYSTEMDRIVE",
|
6808
|
+
"SYSTEMROOT",
|
6809
|
+
"TEMP",
|
6810
|
+
"USERNAME",
|
6811
|
+
"USERPROFILE"
|
6812
|
+
] : ["HOME", "LOGNAME", "PATH", "SHELL", "TERM", "USER"];
|
6813
|
+
function getDefaultEnvironment() {
|
6814
|
+
const env = {};
|
6815
|
+
for (const key of DEFAULT_INHERITED_ENV_VARS) {
|
6816
|
+
const value = process.env[key];
|
6817
|
+
if (value === void 0) {
|
6818
|
+
continue;
|
6819
|
+
}
|
6820
|
+
if (value.startsWith("()")) {
|
6821
|
+
continue;
|
6822
|
+
}
|
6823
|
+
env[key] = value;
|
6824
|
+
}
|
6825
|
+
return env;
|
6826
|
+
}
|
6827
|
+
function isElectron() {
|
6828
|
+
return "type" in process;
|
6829
|
+
}
|
6830
|
+
|
6831
|
+
// core/tool/mcp/mcp-stdio-transport.ts
|
6832
|
+
var StdioClientTransport = class {
|
6833
|
+
constructor(server) {
|
6834
|
+
this.abortController = new AbortController();
|
6835
|
+
this.readBuffer = new ReadBuffer();
|
6836
|
+
this.serverParams = server;
|
6837
|
+
}
|
6838
|
+
async start() {
|
6839
|
+
if (this.process) {
|
6840
|
+
throw new MCPClientError({
|
6841
|
+
message: "StdioClientTransport already started."
|
6842
|
+
});
|
6843
|
+
}
|
6844
|
+
return new Promise(async (resolve, reject) => {
|
6845
|
+
var _a17, _b, _c, _d;
|
6846
|
+
try {
|
6847
|
+
const process2 = await createChildProcess(
|
6848
|
+
this.serverParams,
|
6849
|
+
this.abortController.signal
|
6850
|
+
);
|
6851
|
+
this.process = process2;
|
6852
|
+
this.process.on("error", (error) => {
|
6853
|
+
var _a18, _b2;
|
6854
|
+
if (error.name === "AbortError") {
|
6855
|
+
(_a18 = this.onClose) == null ? void 0 : _a18.call(this);
|
6856
|
+
return;
|
6857
|
+
}
|
6858
|
+
reject(error);
|
6859
|
+
(_b2 = this.onError) == null ? void 0 : _b2.call(this, error);
|
6860
|
+
});
|
6861
|
+
this.process.on("spawn", () => {
|
6862
|
+
resolve();
|
6863
|
+
});
|
6864
|
+
this.process.on("close", (_code) => {
|
6865
|
+
var _a18;
|
6866
|
+
this.process = void 0;
|
6867
|
+
(_a18 = this.onClose) == null ? void 0 : _a18.call(this);
|
6868
|
+
});
|
6869
|
+
(_a17 = this.process.stdin) == null ? void 0 : _a17.on("error", (error) => {
|
6870
|
+
var _a18;
|
6871
|
+
(_a18 = this.onError) == null ? void 0 : _a18.call(this, error);
|
6872
|
+
});
|
6873
|
+
(_b = this.process.stdout) == null ? void 0 : _b.on("data", (chunk) => {
|
6874
|
+
this.readBuffer.append(chunk);
|
6875
|
+
this.processReadBuffer();
|
6876
|
+
});
|
6877
|
+
(_c = this.process.stdout) == null ? void 0 : _c.on("error", (error) => {
|
6878
|
+
var _a18;
|
6879
|
+
(_a18 = this.onError) == null ? void 0 : _a18.call(this, error);
|
6880
|
+
});
|
6881
|
+
} catch (error) {
|
6882
|
+
reject(error);
|
6883
|
+
(_d = this.onError) == null ? void 0 : _d.call(this, error);
|
6884
|
+
}
|
6885
|
+
});
|
6886
|
+
}
|
6887
|
+
processReadBuffer() {
|
6888
|
+
var _a17, _b;
|
6889
|
+
while (true) {
|
6890
|
+
try {
|
6891
|
+
const message = this.readBuffer.readMessage();
|
6892
|
+
if (message === null) {
|
6893
|
+
break;
|
6894
|
+
}
|
6895
|
+
(_a17 = this.onMessage) == null ? void 0 : _a17.call(this, message);
|
6896
|
+
} catch (error) {
|
6897
|
+
(_b = this.onError) == null ? void 0 : _b.call(this, error);
|
6898
|
+
}
|
6899
|
+
}
|
6900
|
+
}
|
6901
|
+
async close() {
|
6902
|
+
this.abortController.abort();
|
6903
|
+
this.process = void 0;
|
6904
|
+
this.readBuffer.clear();
|
6905
|
+
}
|
6906
|
+
send(message) {
|
6907
|
+
return new Promise((resolve) => {
|
6908
|
+
var _a17;
|
6909
|
+
if (!((_a17 = this.process) == null ? void 0 : _a17.stdin)) {
|
6910
|
+
throw new MCPClientError({
|
6911
|
+
message: "StdioClientTransport not connected"
|
6912
|
+
});
|
6913
|
+
}
|
6914
|
+
const json = serializeMessage(message);
|
6915
|
+
if (this.process.stdin.write(json)) {
|
6916
|
+
resolve();
|
6917
|
+
} else {
|
6918
|
+
this.process.stdin.once("drain", resolve);
|
6919
|
+
}
|
6920
|
+
});
|
6921
|
+
}
|
6922
|
+
};
|
6923
|
+
var ReadBuffer = class {
|
6924
|
+
append(chunk) {
|
6925
|
+
this.buffer = this.buffer ? Buffer.concat([this.buffer, chunk]) : chunk;
|
6926
|
+
}
|
6927
|
+
readMessage() {
|
6928
|
+
if (!this.buffer)
|
6929
|
+
return null;
|
6930
|
+
const index = this.buffer.indexOf("\n");
|
6931
|
+
if (index === -1) {
|
6932
|
+
return null;
|
6933
|
+
}
|
6934
|
+
const line = this.buffer.toString("utf8", 0, index);
|
6935
|
+
this.buffer = this.buffer.subarray(index + 1);
|
6936
|
+
return deserializeMessage(line);
|
6937
|
+
}
|
6938
|
+
clear() {
|
6939
|
+
this.buffer = void 0;
|
6940
|
+
}
|
6941
|
+
};
|
6942
|
+
function serializeMessage(message) {
|
6943
|
+
return JSON.stringify(message) + "\n";
|
6944
|
+
}
|
6945
|
+
function deserializeMessage(line) {
|
6946
|
+
return JSONRPCMessageSchema.parse(JSON.parse(line));
|
6947
|
+
}
|
6948
|
+
|
6949
|
+
// core/tool/mcp/mcp-sse-transport.ts
|
6950
|
+
var import_stream = require("eventsource-parser/stream");
|
6951
|
+
var SSEClientTransport = class {
|
6952
|
+
constructor({ url }) {
|
6953
|
+
this.connected = false;
|
6954
|
+
this.url = new URL(url);
|
6955
|
+
}
|
6956
|
+
async start() {
|
6957
|
+
return new Promise((resolve, reject) => {
|
6958
|
+
if (this.connected) {
|
6959
|
+
return resolve();
|
6960
|
+
}
|
6961
|
+
this.abortController = new AbortController();
|
6962
|
+
const establishConnection = async () => {
|
6963
|
+
var _a17, _b, _c;
|
6964
|
+
try {
|
6965
|
+
const response = await fetch(this.url.href, {
|
6966
|
+
headers: {
|
6967
|
+
Accept: "text/event-stream"
|
6968
|
+
},
|
6969
|
+
signal: (_a17 = this.abortController) == null ? void 0 : _a17.signal
|
6970
|
+
});
|
6971
|
+
if (!response.ok || !response.body) {
|
6972
|
+
const error = new MCPClientError({
|
6973
|
+
message: `MCP SSE Transport Error: ${response.status} ${response.statusText}`
|
6974
|
+
});
|
6975
|
+
(_b = this.onError) == null ? void 0 : _b.call(this, error);
|
6976
|
+
return reject(error);
|
6977
|
+
}
|
6978
|
+
const stream = response.body.pipeThrough(new TextDecoderStream()).pipeThrough(new import_stream.EventSourceParserStream());
|
6979
|
+
const reader = stream.getReader();
|
6980
|
+
const processEvents = async () => {
|
6981
|
+
var _a18, _b2, _c2;
|
6982
|
+
try {
|
6983
|
+
while (true) {
|
6984
|
+
const { done, value } = await reader.read();
|
6985
|
+
if (done) {
|
6986
|
+
if (this.connected) {
|
6987
|
+
this.connected = false;
|
6988
|
+
throw new MCPClientError({
|
6989
|
+
message: "MCP SSE Transport Error: Connection closed unexpectedly"
|
6990
|
+
});
|
6991
|
+
}
|
6992
|
+
return;
|
6993
|
+
}
|
6994
|
+
const { event, data } = value;
|
6995
|
+
if (event === "endpoint") {
|
6996
|
+
this.endpoint = new URL(data, this.url);
|
6997
|
+
if (this.endpoint.origin !== this.url.origin) {
|
6998
|
+
throw new MCPClientError({
|
6999
|
+
message: `MCP SSE Transport Error: Endpoint origin does not match connection origin: ${this.endpoint.origin}`
|
7000
|
+
});
|
7001
|
+
}
|
7002
|
+
this.connected = true;
|
7003
|
+
resolve();
|
7004
|
+
} else if (event === "message") {
|
7005
|
+
try {
|
7006
|
+
const message = JSONRPCMessageSchema.parse(
|
7007
|
+
JSON.parse(data)
|
7008
|
+
);
|
7009
|
+
(_a18 = this.onMessage) == null ? void 0 : _a18.call(this, message);
|
7010
|
+
} catch (error) {
|
7011
|
+
const e = new MCPClientError({
|
7012
|
+
message: "MCP SSE Transport Error: Failed to parse message",
|
7013
|
+
cause: error
|
7014
|
+
});
|
7015
|
+
(_b2 = this.onError) == null ? void 0 : _b2.call(this, e);
|
7016
|
+
}
|
7017
|
+
}
|
7018
|
+
}
|
7019
|
+
} catch (error) {
|
7020
|
+
if (error instanceof Error && error.name === "AbortError") {
|
7021
|
+
return;
|
7022
|
+
}
|
7023
|
+
(_c2 = this.onError) == null ? void 0 : _c2.call(this, error);
|
7024
|
+
reject(error);
|
7025
|
+
}
|
7026
|
+
};
|
7027
|
+
this.sseConnection = {
|
7028
|
+
close: () => reader.cancel()
|
7029
|
+
};
|
7030
|
+
processEvents();
|
7031
|
+
} catch (error) {
|
7032
|
+
if (error instanceof Error && error.name === "AbortError") {
|
7033
|
+
return;
|
7034
|
+
}
|
7035
|
+
(_c = this.onError) == null ? void 0 : _c.call(this, error);
|
7036
|
+
reject(error);
|
7037
|
+
}
|
7038
|
+
};
|
7039
|
+
establishConnection();
|
7040
|
+
});
|
7041
|
+
}
|
7042
|
+
async close() {
|
7043
|
+
var _a17, _b, _c;
|
7044
|
+
this.connected = false;
|
7045
|
+
(_a17 = this.sseConnection) == null ? void 0 : _a17.close();
|
7046
|
+
(_b = this.abortController) == null ? void 0 : _b.abort();
|
7047
|
+
(_c = this.onClose) == null ? void 0 : _c.call(this);
|
7048
|
+
}
|
7049
|
+
async send(message) {
|
7050
|
+
var _a17, _b, _c;
|
7051
|
+
if (!this.endpoint || !this.connected) {
|
7052
|
+
throw new MCPClientError({
|
7053
|
+
message: "MCP SSE Transport Error: Not connected"
|
7054
|
+
});
|
7055
|
+
}
|
7056
|
+
try {
|
7057
|
+
const headers = new Headers();
|
7058
|
+
headers.set("Content-Type", "application/json");
|
7059
|
+
const init = {
|
7060
|
+
method: "POST",
|
7061
|
+
headers,
|
7062
|
+
body: JSON.stringify(message),
|
7063
|
+
signal: (_a17 = this.abortController) == null ? void 0 : _a17.signal
|
7064
|
+
};
|
7065
|
+
const response = await fetch(this.endpoint, init);
|
7066
|
+
if (!response.ok) {
|
7067
|
+
const text2 = await response.text().catch(() => null);
|
7068
|
+
const error = new MCPClientError({
|
7069
|
+
message: `MCP SSE Transport Error: POSTing to endpoint (HTTP ${response.status}): ${text2}`
|
7070
|
+
});
|
7071
|
+
(_b = this.onError) == null ? void 0 : _b.call(this, error);
|
7072
|
+
return;
|
7073
|
+
}
|
7074
|
+
} catch (error) {
|
7075
|
+
(_c = this.onError) == null ? void 0 : _c.call(this, error);
|
7076
|
+
return;
|
7077
|
+
}
|
7078
|
+
}
|
7079
|
+
};
|
7080
|
+
|
7081
|
+
// core/tool/mcp/mcp-transport.ts
|
7082
|
+
function createMcpTransport(config) {
|
7083
|
+
return config.type === "stdio" ? new StdioClientTransport(config) : new SSEClientTransport(config);
|
7084
|
+
}
|
7085
|
+
|
7086
|
+
// core/tool/mcp/mcp-client.ts
|
7087
|
+
var CLIENT_VERSION = "1.0.0";
|
7088
|
+
async function createMCPClient(config) {
|
7089
|
+
const client = new MCPClient(config);
|
7090
|
+
await client.init();
|
7091
|
+
return client;
|
7092
|
+
}
|
7093
|
+
var MCPClient = class {
|
7094
|
+
constructor({
|
7095
|
+
transport: transportConfig,
|
7096
|
+
name: name17 = "ai-sdk-mcp-client",
|
7097
|
+
onUncaughtError
|
7098
|
+
}) {
|
7099
|
+
this.requestMessageId = 0;
|
7100
|
+
this.responseHandlers = /* @__PURE__ */ new Map();
|
7101
|
+
this.serverCapabilities = {};
|
7102
|
+
this.isClosed = true;
|
7103
|
+
this.onUncaughtError = onUncaughtError;
|
7104
|
+
this.transport = createMcpTransport(transportConfig);
|
7105
|
+
this.transport.onClose = () => this.onClose();
|
7106
|
+
this.transport.onError = (error) => this.onError(error);
|
7107
|
+
this.transport.onMessage = (message) => {
|
7108
|
+
if ("method" in message) {
|
7109
|
+
this.onError(
|
7110
|
+
new MCPClientError({
|
7111
|
+
message: "Unsupported message type"
|
7112
|
+
})
|
7113
|
+
);
|
7114
|
+
return;
|
7115
|
+
}
|
7116
|
+
this.onResponse(message);
|
7117
|
+
};
|
7118
|
+
this.clientInfo = {
|
7119
|
+
name: name17,
|
7120
|
+
version: CLIENT_VERSION
|
7121
|
+
};
|
7122
|
+
}
|
7123
|
+
async init() {
|
7124
|
+
try {
|
7125
|
+
await this.transport.start();
|
7126
|
+
this.isClosed = false;
|
7127
|
+
const result = await this.request({
|
7128
|
+
request: {
|
7129
|
+
method: "initialize",
|
7130
|
+
params: {
|
7131
|
+
protocolVersion: LATEST_PROTOCOL_VERSION,
|
7132
|
+
capabilities: {},
|
7133
|
+
clientInfo: this.clientInfo
|
7134
|
+
}
|
7135
|
+
},
|
7136
|
+
resultSchema: InitializeResultSchema
|
7137
|
+
});
|
7138
|
+
if (result === void 0) {
|
7139
|
+
throw new MCPClientError({
|
7140
|
+
message: "Server sent invalid initialize result"
|
7141
|
+
});
|
7142
|
+
}
|
7143
|
+
if (!SUPPORTED_PROTOCOL_VERSIONS.includes(result.protocolVersion)) {
|
7144
|
+
throw new MCPClientError({
|
7145
|
+
message: `Server's protocol version is not supported: ${result.protocolVersion}`
|
7146
|
+
});
|
7147
|
+
}
|
7148
|
+
this.serverCapabilities = result.capabilities;
|
7149
|
+
await this.notification({
|
7150
|
+
method: "notifications/initialized"
|
7151
|
+
});
|
7152
|
+
return this;
|
7153
|
+
} catch (error) {
|
7154
|
+
await this.close();
|
7155
|
+
throw error;
|
7156
|
+
}
|
7157
|
+
}
|
7158
|
+
async close() {
|
7159
|
+
var _a17;
|
7160
|
+
if (this.isClosed)
|
7161
|
+
return;
|
7162
|
+
await ((_a17 = this.transport) == null ? void 0 : _a17.close());
|
7163
|
+
this.onClose();
|
7164
|
+
}
|
7165
|
+
async request({
|
7166
|
+
request,
|
7167
|
+
resultSchema,
|
7168
|
+
options
|
7169
|
+
}) {
|
7170
|
+
return new Promise((resolve, reject) => {
|
7171
|
+
if (this.isClosed) {
|
7172
|
+
return reject(
|
7173
|
+
new MCPClientError({
|
7174
|
+
message: "Attempted to send a request from a closed client"
|
7175
|
+
})
|
7176
|
+
);
|
7177
|
+
}
|
7178
|
+
const signal = options == null ? void 0 : options.signal;
|
7179
|
+
signal == null ? void 0 : signal.throwIfAborted();
|
7180
|
+
const messageId = this.requestMessageId++;
|
7181
|
+
const jsonrpcRequest = {
|
7182
|
+
...request,
|
7183
|
+
jsonrpc: "2.0",
|
7184
|
+
id: messageId
|
7185
|
+
};
|
7186
|
+
const cleanup = () => {
|
7187
|
+
this.responseHandlers.delete(messageId);
|
7188
|
+
};
|
7189
|
+
this.responseHandlers.set(messageId, (response) => {
|
7190
|
+
if (signal == null ? void 0 : signal.aborted) {
|
7191
|
+
return reject(
|
7192
|
+
new MCPClientError({
|
7193
|
+
message: "Request was aborted",
|
7194
|
+
cause: signal.reason
|
7195
|
+
})
|
7196
|
+
);
|
7197
|
+
}
|
7198
|
+
if (response instanceof Error) {
|
7199
|
+
return reject(response);
|
7200
|
+
}
|
7201
|
+
try {
|
7202
|
+
const result = resultSchema.parse(response.result);
|
7203
|
+
resolve(result);
|
7204
|
+
} catch (error) {
|
7205
|
+
const parseError = new MCPClientError({
|
7206
|
+
message: "Failed to parse server initialization result",
|
7207
|
+
cause: error
|
7208
|
+
});
|
7209
|
+
reject(parseError);
|
7210
|
+
}
|
7211
|
+
});
|
7212
|
+
this.transport.send(jsonrpcRequest).catch((error) => {
|
7213
|
+
cleanup();
|
7214
|
+
reject(error);
|
7215
|
+
});
|
7216
|
+
});
|
7217
|
+
}
|
7218
|
+
async listTools({
|
7219
|
+
params,
|
7220
|
+
options
|
7221
|
+
} = {}) {
|
7222
|
+
if (!this.serverCapabilities.tools) {
|
7223
|
+
throw new MCPClientError({
|
7224
|
+
message: `Server does not support tools`
|
7225
|
+
});
|
7226
|
+
}
|
7227
|
+
try {
|
7228
|
+
return this.request({
|
7229
|
+
request: { method: "tools/list", params },
|
7230
|
+
resultSchema: ListToolsResultSchema,
|
7231
|
+
options
|
7232
|
+
});
|
7233
|
+
} catch (error) {
|
7234
|
+
throw error;
|
7235
|
+
}
|
7236
|
+
}
|
7237
|
+
async callTool({
|
7238
|
+
name: name17,
|
7239
|
+
args,
|
7240
|
+
options
|
7241
|
+
}) {
|
7242
|
+
if (!this.serverCapabilities.tools) {
|
7243
|
+
throw new MCPClientError({
|
7244
|
+
message: `Server does not support tools`
|
7245
|
+
});
|
7246
|
+
}
|
7247
|
+
try {
|
7248
|
+
return this.request({
|
7249
|
+
request: { method: "tools/call", params: { name: name17, arguments: args } },
|
7250
|
+
resultSchema: CallToolResultSchema,
|
7251
|
+
options: {
|
7252
|
+
signal: options == null ? void 0 : options.abortSignal
|
7253
|
+
}
|
7254
|
+
});
|
7255
|
+
} catch (error) {
|
7256
|
+
throw error;
|
7257
|
+
}
|
7258
|
+
}
|
7259
|
+
async notification(notification) {
|
7260
|
+
const jsonrpcNotification = {
|
7261
|
+
...notification,
|
7262
|
+
jsonrpc: "2.0"
|
7263
|
+
};
|
7264
|
+
await this.transport.send(jsonrpcNotification);
|
7265
|
+
}
|
7266
|
+
/**
|
7267
|
+
* Returns a set of AI SDK tools from the MCP server
|
7268
|
+
* @returns A record of tool names to their implementations
|
7269
|
+
*/
|
7270
|
+
async tools({
|
7271
|
+
schemas = "automatic"
|
7272
|
+
} = {}) {
|
7273
|
+
const tools = {};
|
7274
|
+
try {
|
7275
|
+
const listToolsResult = await this.listTools();
|
7276
|
+
for (const { name: name17, description, inputSchema } of listToolsResult.tools) {
|
7277
|
+
if (schemas !== "automatic" && !(name17 in schemas)) {
|
7278
|
+
continue;
|
7279
|
+
}
|
7280
|
+
const parameters = schemas === "automatic" ? (0, import_ui_utils10.jsonSchema)(inputSchema) : schemas[name17].parameters;
|
7281
|
+
const self = this;
|
7282
|
+
const toolWithExecute = tool({
|
7283
|
+
description,
|
7284
|
+
parameters,
|
7285
|
+
execute: async (args, options) => {
|
7286
|
+
var _a17;
|
7287
|
+
(_a17 = options == null ? void 0 : options.abortSignal) == null ? void 0 : _a17.throwIfAborted();
|
7288
|
+
return self.callTool({
|
7289
|
+
name: name17,
|
7290
|
+
args,
|
7291
|
+
options
|
7292
|
+
});
|
7293
|
+
}
|
7294
|
+
});
|
7295
|
+
tools[name17] = toolWithExecute;
|
7296
|
+
}
|
7297
|
+
return tools;
|
7298
|
+
} catch (error) {
|
7299
|
+
throw error;
|
7300
|
+
}
|
7301
|
+
}
|
7302
|
+
onClose() {
|
7303
|
+
if (this.isClosed)
|
7304
|
+
return;
|
7305
|
+
this.isClosed = true;
|
7306
|
+
const error = new MCPClientError({
|
7307
|
+
message: "Connection closed"
|
7308
|
+
});
|
7309
|
+
for (const handler of this.responseHandlers.values()) {
|
7310
|
+
handler(error);
|
7311
|
+
}
|
7312
|
+
this.responseHandlers.clear();
|
7313
|
+
}
|
7314
|
+
onError(error) {
|
7315
|
+
if (this.onUncaughtError) {
|
7316
|
+
this.onUncaughtError(error);
|
7317
|
+
}
|
7318
|
+
}
|
7319
|
+
onResponse(response) {
|
7320
|
+
const messageId = Number(response.id);
|
7321
|
+
const handler = this.responseHandlers.get(messageId);
|
7322
|
+
if (handler === void 0) {
|
7323
|
+
throw new MCPClientError({
|
7324
|
+
message: `Protocol error: Received a response for an unknown message ID: ${JSON.stringify(
|
7325
|
+
response
|
7326
|
+
)}`
|
7327
|
+
});
|
7328
|
+
}
|
7329
|
+
this.responseHandlers.delete(messageId);
|
7330
|
+
handler(
|
7331
|
+
"result" in response ? response : new MCPClientError({
|
7332
|
+
message: response.error.message,
|
7333
|
+
cause: response.error
|
7334
|
+
})
|
7335
|
+
);
|
7336
|
+
}
|
7337
|
+
};
|
7338
|
+
|
6498
7339
|
// core/util/cosine-similarity.ts
|
6499
7340
|
function cosineSimilarity(vector1, vector2, options = {
|
6500
7341
|
throwErrorForEmptyVectors: false
|
@@ -6537,8 +7378,8 @@ function simulateReadableStream({
|
|
6537
7378
|
chunkDelayInMs = 0,
|
6538
7379
|
_internal
|
6539
7380
|
}) {
|
6540
|
-
var
|
6541
|
-
const delay2 = (
|
7381
|
+
var _a17;
|
7382
|
+
const delay2 = (_a17 = _internal == null ? void 0 : _internal.delay) != null ? _a17 : import_provider_utils13.delay;
|
6542
7383
|
let index = 0;
|
6543
7384
|
return new ReadableStream({
|
6544
7385
|
async pull(controller) {
|
@@ -6553,40 +7394,40 @@ function simulateReadableStream({
|
|
6553
7394
|
}
|
6554
7395
|
|
6555
7396
|
// streams/assistant-response.ts
|
6556
|
-
var
|
7397
|
+
var import_ui_utils12 = require("@ai-sdk/ui-utils");
|
6557
7398
|
function AssistantResponse({ threadId, messageId }, process2) {
|
6558
7399
|
const stream = new ReadableStream({
|
6559
7400
|
async start(controller) {
|
6560
|
-
var
|
7401
|
+
var _a17;
|
6561
7402
|
const textEncoder = new TextEncoder();
|
6562
7403
|
const sendMessage = (message) => {
|
6563
7404
|
controller.enqueue(
|
6564
7405
|
textEncoder.encode(
|
6565
|
-
(0,
|
7406
|
+
(0, import_ui_utils12.formatAssistantStreamPart)("assistant_message", message)
|
6566
7407
|
)
|
6567
7408
|
);
|
6568
7409
|
};
|
6569
7410
|
const sendDataMessage = (message) => {
|
6570
7411
|
controller.enqueue(
|
6571
7412
|
textEncoder.encode(
|
6572
|
-
(0,
|
7413
|
+
(0, import_ui_utils12.formatAssistantStreamPart)("data_message", message)
|
6573
7414
|
)
|
6574
7415
|
);
|
6575
7416
|
};
|
6576
7417
|
const sendError = (errorMessage) => {
|
6577
7418
|
controller.enqueue(
|
6578
|
-
textEncoder.encode((0,
|
7419
|
+
textEncoder.encode((0, import_ui_utils12.formatAssistantStreamPart)("error", errorMessage))
|
6579
7420
|
);
|
6580
7421
|
};
|
6581
7422
|
const forwardStream = async (stream2) => {
|
6582
|
-
var
|
7423
|
+
var _a18, _b;
|
6583
7424
|
let result = void 0;
|
6584
7425
|
for await (const value of stream2) {
|
6585
7426
|
switch (value.event) {
|
6586
7427
|
case "thread.message.created": {
|
6587
7428
|
controller.enqueue(
|
6588
7429
|
textEncoder.encode(
|
6589
|
-
(0,
|
7430
|
+
(0, import_ui_utils12.formatAssistantStreamPart)("assistant_message", {
|
6590
7431
|
id: value.data.id,
|
6591
7432
|
role: "assistant",
|
6592
7433
|
content: [{ type: "text", text: { value: "" } }]
|
@@ -6596,11 +7437,11 @@ function AssistantResponse({ threadId, messageId }, process2) {
|
|
6596
7437
|
break;
|
6597
7438
|
}
|
6598
7439
|
case "thread.message.delta": {
|
6599
|
-
const content = (
|
7440
|
+
const content = (_a18 = value.data.delta.content) == null ? void 0 : _a18[0];
|
6600
7441
|
if ((content == null ? void 0 : content.type) === "text" && ((_b = content.text) == null ? void 0 : _b.value) != null) {
|
6601
7442
|
controller.enqueue(
|
6602
7443
|
textEncoder.encode(
|
6603
|
-
(0,
|
7444
|
+
(0, import_ui_utils12.formatAssistantStreamPart)("text", content.text.value)
|
6604
7445
|
)
|
6605
7446
|
);
|
6606
7447
|
}
|
@@ -6617,7 +7458,7 @@ function AssistantResponse({ threadId, messageId }, process2) {
|
|
6617
7458
|
};
|
6618
7459
|
controller.enqueue(
|
6619
7460
|
textEncoder.encode(
|
6620
|
-
(0,
|
7461
|
+
(0, import_ui_utils12.formatAssistantStreamPart)("assistant_control_data", {
|
6621
7462
|
threadId,
|
6622
7463
|
messageId
|
6623
7464
|
})
|
@@ -6630,7 +7471,7 @@ function AssistantResponse({ threadId, messageId }, process2) {
|
|
6630
7471
|
forwardStream
|
6631
7472
|
});
|
6632
7473
|
} catch (error) {
|
6633
|
-
sendError((
|
7474
|
+
sendError((_a17 = error.message) != null ? _a17 : `${error}`);
|
6634
7475
|
} finally {
|
6635
7476
|
controller.close();
|
6636
7477
|
}
|
@@ -6655,7 +7496,7 @@ __export(langchain_adapter_exports, {
|
|
6655
7496
|
toDataStream: () => toDataStream,
|
6656
7497
|
toDataStreamResponse: () => toDataStreamResponse
|
6657
7498
|
});
|
6658
|
-
var
|
7499
|
+
var import_ui_utils13 = require("@ai-sdk/ui-utils");
|
6659
7500
|
|
6660
7501
|
// streams/stream-callbacks.ts
|
6661
7502
|
function createCallbacksTransformer(callbacks = {}) {
|
@@ -6691,7 +7532,7 @@ function toDataStreamInternal(stream, callbacks) {
|
|
6691
7532
|
return stream.pipeThrough(
|
6692
7533
|
new TransformStream({
|
6693
7534
|
transform: async (value, controller) => {
|
6694
|
-
var
|
7535
|
+
var _a17;
|
6695
7536
|
if (typeof value === "string") {
|
6696
7537
|
controller.enqueue(value);
|
6697
7538
|
return;
|
@@ -6699,7 +7540,7 @@ function toDataStreamInternal(stream, callbacks) {
|
|
6699
7540
|
if ("event" in value) {
|
6700
7541
|
if (value.event === "on_chat_model_stream") {
|
6701
7542
|
forwardAIMessageChunk(
|
6702
|
-
(
|
7543
|
+
(_a17 = value.data) == null ? void 0 : _a17.chunk,
|
6703
7544
|
controller
|
6704
7545
|
);
|
6705
7546
|
}
|
@@ -6711,7 +7552,7 @@ function toDataStreamInternal(stream, callbacks) {
|
|
6711
7552
|
).pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(new TextDecoderStream()).pipeThrough(
|
6712
7553
|
new TransformStream({
|
6713
7554
|
transform: async (chunk, controller) => {
|
6714
|
-
controller.enqueue((0,
|
7555
|
+
controller.enqueue((0, import_ui_utils13.formatDataStreamPart)("text", chunk));
|
6715
7556
|
}
|
6716
7557
|
})
|
6717
7558
|
);
|
@@ -6722,7 +7563,7 @@ function toDataStream(stream, callbacks) {
|
|
6722
7563
|
);
|
6723
7564
|
}
|
6724
7565
|
function toDataStreamResponse(stream, options) {
|
6725
|
-
var
|
7566
|
+
var _a17;
|
6726
7567
|
const dataStream = toDataStreamInternal(
|
6727
7568
|
stream,
|
6728
7569
|
options == null ? void 0 : options.callbacks
|
@@ -6731,7 +7572,7 @@ function toDataStreamResponse(stream, options) {
|
|
6731
7572
|
const init = options == null ? void 0 : options.init;
|
6732
7573
|
const responseStream = data ? mergeStreams(data.stream, dataStream) : dataStream;
|
6733
7574
|
return new Response(responseStream, {
|
6734
|
-
status: (
|
7575
|
+
status: (_a17 = init == null ? void 0 : init.status) != null ? _a17 : 200,
|
6735
7576
|
statusText: init == null ? void 0 : init.statusText,
|
6736
7577
|
headers: prepareResponseHeaders(init == null ? void 0 : init.headers, {
|
6737
7578
|
contentType: "text/plain; charset=utf-8",
|
@@ -6763,7 +7604,7 @@ __export(llamaindex_adapter_exports, {
|
|
6763
7604
|
toDataStreamResponse: () => toDataStreamResponse2
|
6764
7605
|
});
|
6765
7606
|
var import_provider_utils15 = require("@ai-sdk/provider-utils");
|
6766
|
-
var
|
7607
|
+
var import_ui_utils14 = require("@ai-sdk/ui-utils");
|
6767
7608
|
function toDataStreamInternal2(stream, callbacks) {
|
6768
7609
|
const trimStart = trimStartOfStream();
|
6769
7610
|
return (0, import_provider_utils15.convertAsyncIteratorToReadableStream)(stream[Symbol.asyncIterator]()).pipeThrough(
|
@@ -6775,7 +7616,7 @@ function toDataStreamInternal2(stream, callbacks) {
|
|
6775
7616
|
).pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(new TextDecoderStream()).pipeThrough(
|
6776
7617
|
new TransformStream({
|
6777
7618
|
transform: async (chunk, controller) => {
|
6778
|
-
controller.enqueue((0,
|
7619
|
+
controller.enqueue((0, import_ui_utils14.formatDataStreamPart)("text", chunk));
|
6779
7620
|
}
|
6780
7621
|
})
|
6781
7622
|
);
|
@@ -6786,14 +7627,14 @@ function toDataStream2(stream, callbacks) {
|
|
6786
7627
|
);
|
6787
7628
|
}
|
6788
7629
|
function toDataStreamResponse2(stream, options = {}) {
|
6789
|
-
var
|
7630
|
+
var _a17;
|
6790
7631
|
const { init, data, callbacks } = options;
|
6791
7632
|
const dataStream = toDataStreamInternal2(stream, callbacks).pipeThrough(
|
6792
7633
|
new TextEncoderStream()
|
6793
7634
|
);
|
6794
7635
|
const responseStream = data ? mergeStreams(data.stream, dataStream) : dataStream;
|
6795
7636
|
return new Response(responseStream, {
|
6796
|
-
status: (
|
7637
|
+
status: (_a17 = init == null ? void 0 : init.status) != null ? _a17 : 200,
|
6797
7638
|
statusText: init == null ? void 0 : init.statusText,
|
6798
7639
|
headers: prepareResponseHeaders(init == null ? void 0 : init.headers, {
|
6799
7640
|
contentType: "text/plain; charset=utf-8",
|
@@ -6817,7 +7658,7 @@ function trimStartOfStream() {
|
|
6817
7658
|
}
|
6818
7659
|
|
6819
7660
|
// streams/stream-data.ts
|
6820
|
-
var
|
7661
|
+
var import_ui_utils15 = require("@ai-sdk/ui-utils");
|
6821
7662
|
|
6822
7663
|
// util/constants.ts
|
6823
7664
|
var HANGING_STREAM_WARNING_TIME_MS = 15 * 1e3;
|
@@ -6869,7 +7710,7 @@ var StreamData = class {
|
|
6869
7710
|
throw new Error("Stream controller is not initialized.");
|
6870
7711
|
}
|
6871
7712
|
this.controller.enqueue(
|
6872
|
-
this.encoder.encode((0,
|
7713
|
+
this.encoder.encode((0, import_ui_utils15.formatDataStreamPart)("data", [value]))
|
6873
7714
|
);
|
6874
7715
|
}
|
6875
7716
|
appendMessageAnnotation(value) {
|
@@ -6880,7 +7721,7 @@ var StreamData = class {
|
|
6880
7721
|
throw new Error("Stream controller is not initialized.");
|
6881
7722
|
}
|
6882
7723
|
this.controller.enqueue(
|
6883
|
-
this.encoder.encode((0,
|
7724
|
+
this.encoder.encode((0, import_ui_utils15.formatDataStreamPart)("message_annotations", [value]))
|
6884
7725
|
);
|
6885
7726
|
}
|
6886
7727
|
};
|
@@ -6902,6 +7743,7 @@ var StreamData = class {
|
|
6902
7743
|
LangChainAdapter,
|
6903
7744
|
LlamaIndexAdapter,
|
6904
7745
|
LoadAPIKeyError,
|
7746
|
+
MCPClientError,
|
6905
7747
|
MessageConversionError,
|
6906
7748
|
NoContentGeneratedError,
|
6907
7749
|
NoImageGeneratedError,
|
@@ -6932,6 +7774,7 @@ var StreamData = class {
|
|
6932
7774
|
customProvider,
|
6933
7775
|
embed,
|
6934
7776
|
embedMany,
|
7777
|
+
experimental_createMCPClient,
|
6935
7778
|
experimental_createProviderRegistry,
|
6936
7779
|
experimental_customProvider,
|
6937
7780
|
experimental_generateImage,
|
@@ -6949,6 +7792,7 @@ var StreamData = class {
|
|
6949
7792
|
processDataStream,
|
6950
7793
|
processTextStream,
|
6951
7794
|
simulateReadableStream,
|
7795
|
+
simulateStreamingMiddleware,
|
6952
7796
|
smoothStream,
|
6953
7797
|
streamObject,
|
6954
7798
|
streamText,
|