ai 3.4.33 → 4.0.0-canary.0
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 +15 -0
- package/dist/index.d.mts +6 -474
- package/dist/index.d.ts +6 -474
- package/dist/index.js +94 -771
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +81 -746
- package/dist/index.mjs.map +1 -1
- package/package.json +11 -49
- package/rsc/dist/index.d.ts +5 -67
- package/rsc/dist/rsc-server.d.mts +5 -67
- package/rsc/dist/rsc-server.mjs +19 -623
- package/rsc/dist/rsc-server.mjs.map +1 -1
- package/solid/dist/index.d.mts +0 -13
- package/solid/dist/index.d.ts +0 -13
- package/solid/dist/index.js +0 -35
- package/solid/dist/index.js.map +0 -1
- package/solid/dist/index.mjs +0 -12
- package/solid/dist/index.mjs.map +0 -1
- package/svelte/dist/index.d.mts +0 -175
- package/svelte/dist/index.d.ts +0 -175
- package/svelte/dist/index.js +0 -1161
- package/svelte/dist/index.js.map +0 -1
- package/svelte/dist/index.mjs +0 -1136
- package/svelte/dist/index.mjs.map +0 -1
- package/vue/dist/index.d.mts +0 -17
- package/vue/dist/index.d.ts +0 -17
- package/vue/dist/index.js +0 -38
- package/vue/dist/index.js.map +0 -1
- package/vue/dist/index.mjs +0 -15
- package/vue/dist/index.mjs.map +0 -1
package/dist/index.js
CHANGED
@@ -23,19 +23,9 @@ __export(streams_exports, {
|
|
23
23
|
AISDKError: () => import_provider13.AISDKError,
|
24
24
|
AIStream: () => AIStream,
|
25
25
|
APICallError: () => import_provider13.APICallError,
|
26
|
-
AWSBedrockAnthropicMessagesStream: () => AWSBedrockAnthropicMessagesStream,
|
27
|
-
AWSBedrockAnthropicStream: () => AWSBedrockAnthropicStream,
|
28
|
-
AWSBedrockCohereStream: () => AWSBedrockCohereStream,
|
29
|
-
AWSBedrockLlama2Stream: () => AWSBedrockLlama2Stream,
|
30
|
-
AWSBedrockStream: () => AWSBedrockStream,
|
31
|
-
AnthropicStream: () => AnthropicStream,
|
32
26
|
AssistantResponse: () => AssistantResponse,
|
33
|
-
CohereStream: () => CohereStream,
|
34
27
|
DownloadError: () => DownloadError,
|
35
28
|
EmptyResponseBodyError: () => import_provider13.EmptyResponseBodyError,
|
36
|
-
GoogleGenerativeAIStream: () => GoogleGenerativeAIStream,
|
37
|
-
HuggingFaceStream: () => HuggingFaceStream,
|
38
|
-
InkeepStream: () => InkeepStream,
|
39
29
|
InvalidArgumentError: () => InvalidArgumentError,
|
40
30
|
InvalidDataContentError: () => InvalidDataContentError,
|
41
31
|
InvalidMessageRoleError: () => InvalidMessageRoleError,
|
@@ -44,18 +34,14 @@ __export(streams_exports, {
|
|
44
34
|
InvalidToolArgumentsError: () => InvalidToolArgumentsError,
|
45
35
|
JSONParseError: () => import_provider13.JSONParseError,
|
46
36
|
LangChainAdapter: () => langchain_adapter_exports,
|
47
|
-
LangChainStream: () => LangChainStream,
|
48
37
|
LlamaIndexAdapter: () => llamaindex_adapter_exports,
|
49
38
|
LoadAPIKeyError: () => import_provider13.LoadAPIKeyError,
|
50
39
|
MessageConversionError: () => MessageConversionError,
|
51
|
-
MistralStream: () => MistralStream,
|
52
40
|
NoContentGeneratedError: () => import_provider13.NoContentGeneratedError,
|
53
41
|
NoObjectGeneratedError: () => NoObjectGeneratedError,
|
54
42
|
NoSuchModelError: () => import_provider13.NoSuchModelError,
|
55
43
|
NoSuchProviderError: () => NoSuchProviderError,
|
56
44
|
NoSuchToolError: () => NoSuchToolError,
|
57
|
-
OpenAIStream: () => OpenAIStream,
|
58
|
-
ReplicateStream: () => ReplicateStream,
|
59
45
|
RetryError: () => RetryError,
|
60
46
|
StreamData: () => StreamData2,
|
61
47
|
StreamingTextResponse: () => StreamingTextResponse,
|
@@ -78,15 +64,14 @@ __export(streams_exports, {
|
|
78
64
|
experimental_streamObject: () => experimental_streamObject,
|
79
65
|
experimental_streamText: () => experimental_streamText,
|
80
66
|
experimental_wrapLanguageModel: () => experimental_wrapLanguageModel,
|
81
|
-
formatStreamPart: () =>
|
67
|
+
formatStreamPart: () => import_ui_utils9.formatStreamPart,
|
82
68
|
generateId: () => generateId2,
|
83
69
|
generateObject: () => generateObject,
|
84
70
|
generateText: () => generateText,
|
85
71
|
jsonSchema: () => import_ui_utils6.jsonSchema,
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
readDataStream: () => import_ui_utils10.readDataStream,
|
72
|
+
parseStreamPart: () => import_ui_utils9.parseStreamPart,
|
73
|
+
processDataProtocolResponse: () => import_ui_utils9.processDataProtocolResponse,
|
74
|
+
readDataStream: () => import_ui_utils9.readDataStream,
|
90
75
|
readableFromAsyncIterable: () => readableFromAsyncIterable,
|
91
76
|
streamObject: () => streamObject,
|
92
77
|
streamText: () => streamText,
|
@@ -95,7 +80,7 @@ __export(streams_exports, {
|
|
95
80
|
trimStartOfStreamHelper: () => trimStartOfStreamHelper
|
96
81
|
});
|
97
82
|
module.exports = __toCommonJS(streams_exports);
|
98
|
-
var
|
83
|
+
var import_ui_utils9 = require("@ai-sdk/ui-utils");
|
99
84
|
var import_provider_utils10 = require("@ai-sdk/provider-utils");
|
100
85
|
|
101
86
|
// core/index.ts
|
@@ -4767,12 +4752,12 @@ var DefaultStreamTextResult = class {
|
|
4767
4752
|
const chunkType = chunk.type;
|
4768
4753
|
switch (chunkType) {
|
4769
4754
|
case "text-delta": {
|
4770
|
-
controller.enqueue((0,
|
4755
|
+
controller.enqueue((0, import_ui_utils9.formatStreamPart)("text", chunk.textDelta));
|
4771
4756
|
break;
|
4772
4757
|
}
|
4773
4758
|
case "tool-call-streaming-start": {
|
4774
4759
|
controller.enqueue(
|
4775
|
-
(0,
|
4760
|
+
(0, import_ui_utils9.formatStreamPart)("tool_call_streaming_start", {
|
4776
4761
|
toolCallId: chunk.toolCallId,
|
4777
4762
|
toolName: chunk.toolName
|
4778
4763
|
})
|
@@ -4781,7 +4766,7 @@ var DefaultStreamTextResult = class {
|
|
4781
4766
|
}
|
4782
4767
|
case "tool-call-delta": {
|
4783
4768
|
controller.enqueue(
|
4784
|
-
(0,
|
4769
|
+
(0, import_ui_utils9.formatStreamPart)("tool_call_delta", {
|
4785
4770
|
toolCallId: chunk.toolCallId,
|
4786
4771
|
argsTextDelta: chunk.argsTextDelta
|
4787
4772
|
})
|
@@ -4790,7 +4775,7 @@ var DefaultStreamTextResult = class {
|
|
4790
4775
|
}
|
4791
4776
|
case "tool-call": {
|
4792
4777
|
controller.enqueue(
|
4793
|
-
(0,
|
4778
|
+
(0, import_ui_utils9.formatStreamPart)("tool_call", {
|
4794
4779
|
toolCallId: chunk.toolCallId,
|
4795
4780
|
toolName: chunk.toolName,
|
4796
4781
|
args: chunk.args
|
@@ -4800,7 +4785,7 @@ var DefaultStreamTextResult = class {
|
|
4800
4785
|
}
|
4801
4786
|
case "tool-result": {
|
4802
4787
|
controller.enqueue(
|
4803
|
-
(0,
|
4788
|
+
(0, import_ui_utils9.formatStreamPart)("tool_result", {
|
4804
4789
|
toolCallId: chunk.toolCallId,
|
4805
4790
|
result: chunk.result
|
4806
4791
|
})
|
@@ -4809,13 +4794,13 @@ var DefaultStreamTextResult = class {
|
|
4809
4794
|
}
|
4810
4795
|
case "error": {
|
4811
4796
|
controller.enqueue(
|
4812
|
-
(0,
|
4797
|
+
(0, import_ui_utils9.formatStreamPart)("error", getErrorMessage3(chunk.error))
|
4813
4798
|
);
|
4814
4799
|
break;
|
4815
4800
|
}
|
4816
4801
|
case "step-finish": {
|
4817
4802
|
controller.enqueue(
|
4818
|
-
(0,
|
4803
|
+
(0, import_ui_utils9.formatStreamPart)("finish_step", {
|
4819
4804
|
finishReason: chunk.finishReason,
|
4820
4805
|
usage: sendUsage ? {
|
4821
4806
|
promptTokens: chunk.usage.promptTokens,
|
@@ -4828,7 +4813,7 @@ var DefaultStreamTextResult = class {
|
|
4828
4813
|
}
|
4829
4814
|
case "finish": {
|
4830
4815
|
controller.enqueue(
|
4831
|
-
(0,
|
4816
|
+
(0, import_ui_utils9.formatStreamPart)("finish_message", {
|
4832
4817
|
finishReason: chunk.finishReason,
|
4833
4818
|
usage: sendUsage ? {
|
4834
4819
|
promptTokens: chunk.usage.promptTokens,
|
@@ -5180,19 +5165,12 @@ function createCallbacksTransformer(cb) {
|
|
5180
5165
|
}
|
5181
5166
|
},
|
5182
5167
|
async flush() {
|
5183
|
-
const isOpenAICallbacks = isOfTypeOpenAIStreamCallbacks(callbacks);
|
5184
5168
|
if (callbacks.onCompletion) {
|
5185
5169
|
await callbacks.onCompletion(aggregatedResponse);
|
5186
5170
|
}
|
5187
|
-
if (callbacks.onFinal && !isOpenAICallbacks) {
|
5188
|
-
await callbacks.onFinal(aggregatedResponse);
|
5189
|
-
}
|
5190
5171
|
}
|
5191
5172
|
});
|
5192
5173
|
}
|
5193
|
-
function isOfTypeOpenAIStreamCallbacks(callbacks) {
|
5194
|
-
return "experimental_onFunctionCall" in callbacks;
|
5195
|
-
}
|
5196
5174
|
function trimStartOfStreamHelper() {
|
5197
5175
|
let isStreamStart = true;
|
5198
5176
|
return (text) => {
|
@@ -5252,135 +5230,8 @@ function readableFromAsyncIterable(iterable) {
|
|
5252
5230
|
});
|
5253
5231
|
}
|
5254
5232
|
|
5255
|
-
// streams/stream-data.ts
|
5256
|
-
var import_ui_utils7 = require("@ai-sdk/ui-utils");
|
5257
|
-
|
5258
|
-
// util/constants.ts
|
5259
|
-
var HANGING_STREAM_WARNING_TIME_MS = 15 * 1e3;
|
5260
|
-
|
5261
|
-
// streams/stream-data.ts
|
5262
|
-
var StreamData2 = class {
|
5263
|
-
constructor() {
|
5264
|
-
this.encoder = new TextEncoder();
|
5265
|
-
this.controller = null;
|
5266
|
-
this.isClosed = false;
|
5267
|
-
this.warningTimeout = null;
|
5268
|
-
const self = this;
|
5269
|
-
this.stream = new ReadableStream({
|
5270
|
-
start: async (controller) => {
|
5271
|
-
self.controller = controller;
|
5272
|
-
if (process.env.NODE_ENV === "development") {
|
5273
|
-
self.warningTimeout = setTimeout(() => {
|
5274
|
-
console.warn(
|
5275
|
-
"The data stream is hanging. Did you forget to close it with `data.close()`?"
|
5276
|
-
);
|
5277
|
-
}, HANGING_STREAM_WARNING_TIME_MS);
|
5278
|
-
}
|
5279
|
-
},
|
5280
|
-
pull: (controller) => {
|
5281
|
-
},
|
5282
|
-
cancel: (reason) => {
|
5283
|
-
this.isClosed = true;
|
5284
|
-
}
|
5285
|
-
});
|
5286
|
-
}
|
5287
|
-
async close() {
|
5288
|
-
if (this.isClosed) {
|
5289
|
-
throw new Error("Data Stream has already been closed.");
|
5290
|
-
}
|
5291
|
-
if (!this.controller) {
|
5292
|
-
throw new Error("Stream controller is not initialized.");
|
5293
|
-
}
|
5294
|
-
this.controller.close();
|
5295
|
-
this.isClosed = true;
|
5296
|
-
if (this.warningTimeout) {
|
5297
|
-
clearTimeout(this.warningTimeout);
|
5298
|
-
}
|
5299
|
-
}
|
5300
|
-
append(value) {
|
5301
|
-
if (this.isClosed) {
|
5302
|
-
throw new Error("Data Stream has already been closed.");
|
5303
|
-
}
|
5304
|
-
if (!this.controller) {
|
5305
|
-
throw new Error("Stream controller is not initialized.");
|
5306
|
-
}
|
5307
|
-
this.controller.enqueue(
|
5308
|
-
this.encoder.encode((0, import_ui_utils7.formatStreamPart)("data", [value]))
|
5309
|
-
);
|
5310
|
-
}
|
5311
|
-
appendMessageAnnotation(value) {
|
5312
|
-
if (this.isClosed) {
|
5313
|
-
throw new Error("Data Stream has already been closed.");
|
5314
|
-
}
|
5315
|
-
if (!this.controller) {
|
5316
|
-
throw new Error("Stream controller is not initialized.");
|
5317
|
-
}
|
5318
|
-
this.controller.enqueue(
|
5319
|
-
this.encoder.encode((0, import_ui_utils7.formatStreamPart)("message_annotations", [value]))
|
5320
|
-
);
|
5321
|
-
}
|
5322
|
-
};
|
5323
|
-
function createStreamDataTransformer() {
|
5324
|
-
const encoder = new TextEncoder();
|
5325
|
-
const decoder = new TextDecoder();
|
5326
|
-
return new TransformStream({
|
5327
|
-
transform: async (chunk, controller) => {
|
5328
|
-
const message = decoder.decode(chunk);
|
5329
|
-
controller.enqueue(encoder.encode((0, import_ui_utils7.formatStreamPart)("text", message)));
|
5330
|
-
}
|
5331
|
-
});
|
5332
|
-
}
|
5333
|
-
var experimental_StreamData = class extends StreamData2 {
|
5334
|
-
};
|
5335
|
-
|
5336
|
-
// streams/anthropic-stream.ts
|
5337
|
-
function parseAnthropicStream() {
|
5338
|
-
let previous = "";
|
5339
|
-
return (data) => {
|
5340
|
-
const json = JSON.parse(data);
|
5341
|
-
if ("error" in json) {
|
5342
|
-
throw new Error(`${json.error.type}: ${json.error.message}`);
|
5343
|
-
}
|
5344
|
-
if (!("completion" in json)) {
|
5345
|
-
return;
|
5346
|
-
}
|
5347
|
-
const text = json.completion;
|
5348
|
-
if (!previous || text.length > previous.length && text.startsWith(previous)) {
|
5349
|
-
const delta = text.slice(previous.length);
|
5350
|
-
previous = text;
|
5351
|
-
return delta;
|
5352
|
-
}
|
5353
|
-
return text;
|
5354
|
-
};
|
5355
|
-
}
|
5356
|
-
async function* streamable(stream) {
|
5357
|
-
for await (const chunk of stream) {
|
5358
|
-
if ("completion" in chunk) {
|
5359
|
-
const text = chunk.completion;
|
5360
|
-
if (text)
|
5361
|
-
yield text;
|
5362
|
-
} else if ("delta" in chunk) {
|
5363
|
-
const { delta } = chunk;
|
5364
|
-
if ("text" in delta) {
|
5365
|
-
const text = delta.text;
|
5366
|
-
if (text)
|
5367
|
-
yield text;
|
5368
|
-
}
|
5369
|
-
}
|
5370
|
-
}
|
5371
|
-
}
|
5372
|
-
function AnthropicStream(res, cb) {
|
5373
|
-
if (Symbol.asyncIterator in res) {
|
5374
|
-
return readableFromAsyncIterable(streamable(res)).pipeThrough(createCallbacksTransformer(cb)).pipeThrough(createStreamDataTransformer());
|
5375
|
-
} else {
|
5376
|
-
return AIStream(res, parseAnthropicStream(), cb).pipeThrough(
|
5377
|
-
createStreamDataTransformer()
|
5378
|
-
);
|
5379
|
-
}
|
5380
|
-
}
|
5381
|
-
|
5382
5233
|
// streams/assistant-response.ts
|
5383
|
-
var
|
5234
|
+
var import_ui_utils7 = require("@ai-sdk/ui-utils");
|
5384
5235
|
function AssistantResponse({ threadId, messageId }, process2) {
|
5385
5236
|
const stream = new ReadableStream({
|
5386
5237
|
async start(controller) {
|
@@ -5388,17 +5239,17 @@ function AssistantResponse({ threadId, messageId }, process2) {
|
|
5388
5239
|
const textEncoder = new TextEncoder();
|
5389
5240
|
const sendMessage = (message) => {
|
5390
5241
|
controller.enqueue(
|
5391
|
-
textEncoder.encode((0,
|
5242
|
+
textEncoder.encode((0, import_ui_utils7.formatStreamPart)("assistant_message", message))
|
5392
5243
|
);
|
5393
5244
|
};
|
5394
5245
|
const sendDataMessage = (message) => {
|
5395
5246
|
controller.enqueue(
|
5396
|
-
textEncoder.encode((0,
|
5247
|
+
textEncoder.encode((0, import_ui_utils7.formatStreamPart)("data_message", message))
|
5397
5248
|
);
|
5398
5249
|
};
|
5399
5250
|
const sendError = (errorMessage) => {
|
5400
5251
|
controller.enqueue(
|
5401
|
-
textEncoder.encode((0,
|
5252
|
+
textEncoder.encode((0, import_ui_utils7.formatStreamPart)("error", errorMessage))
|
5402
5253
|
);
|
5403
5254
|
};
|
5404
5255
|
const forwardStream = async (stream2) => {
|
@@ -5409,7 +5260,7 @@ function AssistantResponse({ threadId, messageId }, process2) {
|
|
5409
5260
|
case "thread.message.created": {
|
5410
5261
|
controller.enqueue(
|
5411
5262
|
textEncoder.encode(
|
5412
|
-
(0,
|
5263
|
+
(0, import_ui_utils7.formatStreamPart)("assistant_message", {
|
5413
5264
|
id: value.data.id,
|
5414
5265
|
role: "assistant",
|
5415
5266
|
content: [{ type: "text", text: { value: "" } }]
|
@@ -5423,7 +5274,7 @@ function AssistantResponse({ threadId, messageId }, process2) {
|
|
5423
5274
|
if ((content == null ? void 0 : content.type) === "text" && ((_b = content.text) == null ? void 0 : _b.value) != null) {
|
5424
5275
|
controller.enqueue(
|
5425
5276
|
textEncoder.encode(
|
5426
|
-
(0,
|
5277
|
+
(0, import_ui_utils7.formatStreamPart)("text", content.text.value)
|
5427
5278
|
)
|
5428
5279
|
);
|
5429
5280
|
}
|
@@ -5440,7 +5291,7 @@ function AssistantResponse({ threadId, messageId }, process2) {
|
|
5440
5291
|
};
|
5441
5292
|
controller.enqueue(
|
5442
5293
|
textEncoder.encode(
|
5443
|
-
(0,
|
5294
|
+
(0, import_ui_utils7.formatStreamPart)("assistant_control_data", {
|
5444
5295
|
threadId,
|
5445
5296
|
messageId
|
5446
5297
|
})
|
@@ -5474,192 +5325,96 @@ function AssistantResponse({ threadId, messageId }, process2) {
|
|
5474
5325
|
}
|
5475
5326
|
var experimental_AssistantResponse = AssistantResponse;
|
5476
5327
|
|
5477
|
-
// streams/
|
5478
|
-
|
5479
|
-
|
5480
|
-
|
5481
|
-
|
5482
|
-
|
5483
|
-
|
5484
|
-
|
5485
|
-
|
5486
|
-
|
5487
|
-
|
5488
|
-
|
5328
|
+
// streams/langchain-adapter.ts
|
5329
|
+
var langchain_adapter_exports = {};
|
5330
|
+
__export(langchain_adapter_exports, {
|
5331
|
+
toAIStream: () => toAIStream,
|
5332
|
+
toDataStream: () => toDataStream,
|
5333
|
+
toDataStreamResponse: () => toDataStreamResponse
|
5334
|
+
});
|
5335
|
+
|
5336
|
+
// streams/stream-data.ts
|
5337
|
+
var import_ui_utils8 = require("@ai-sdk/ui-utils");
|
5338
|
+
|
5339
|
+
// util/constants.ts
|
5340
|
+
var HANGING_STREAM_WARNING_TIME_MS = 15 * 1e3;
|
5341
|
+
|
5342
|
+
// streams/stream-data.ts
|
5343
|
+
var StreamData2 = class {
|
5344
|
+
constructor() {
|
5345
|
+
this.encoder = new TextEncoder();
|
5346
|
+
this.controller = null;
|
5347
|
+
this.isClosed = false;
|
5348
|
+
this.warningTimeout = null;
|
5349
|
+
const self = this;
|
5350
|
+
this.stream = new ReadableStream({
|
5351
|
+
start: async (controller) => {
|
5352
|
+
self.controller = controller;
|
5353
|
+
if (process.env.NODE_ENV === "development") {
|
5354
|
+
self.warningTimeout = setTimeout(() => {
|
5355
|
+
console.warn(
|
5356
|
+
"The data stream is hanging. Did you forget to close it with `data.close()`?"
|
5357
|
+
);
|
5358
|
+
}, HANGING_STREAM_WARNING_TIME_MS);
|
5359
|
+
}
|
5360
|
+
},
|
5361
|
+
pull: (controller) => {
|
5362
|
+
},
|
5363
|
+
cancel: (reason) => {
|
5364
|
+
this.isClosed = true;
|
5489
5365
|
}
|
5490
|
-
}
|
5366
|
+
});
|
5491
5367
|
}
|
5492
|
-
|
5493
|
-
|
5494
|
-
|
5495
|
-
var _a11;
|
5496
|
-
return (_a11 = chunk.delta) == null ? void 0 : _a11.text;
|
5497
|
-
});
|
5498
|
-
}
|
5499
|
-
function AWSBedrockAnthropicStream(response, callbacks) {
|
5500
|
-
return AWSBedrockStream(response, callbacks, (chunk) => chunk.completion);
|
5501
|
-
}
|
5502
|
-
function AWSBedrockCohereStream(response, callbacks) {
|
5503
|
-
return AWSBedrockStream(response, callbacks, (chunk) => chunk == null ? void 0 : chunk.text);
|
5504
|
-
}
|
5505
|
-
function AWSBedrockLlama2Stream(response, callbacks) {
|
5506
|
-
return AWSBedrockStream(response, callbacks, (chunk) => chunk.generation);
|
5507
|
-
}
|
5508
|
-
function AWSBedrockStream(response, callbacks, extractTextDeltaFromChunk) {
|
5509
|
-
return readableFromAsyncIterable(
|
5510
|
-
asDeltaIterable(response, extractTextDeltaFromChunk)
|
5511
|
-
).pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(createStreamDataTransformer());
|
5512
|
-
}
|
5513
|
-
|
5514
|
-
// streams/cohere-stream.ts
|
5515
|
-
var utf8Decoder = new TextDecoder("utf-8");
|
5516
|
-
async function processLines(lines, controller) {
|
5517
|
-
for (const line of lines) {
|
5518
|
-
const { text, is_finished } = JSON.parse(line);
|
5519
|
-
if (!is_finished) {
|
5520
|
-
controller.enqueue(text);
|
5368
|
+
async close() {
|
5369
|
+
if (this.isClosed) {
|
5370
|
+
throw new Error("Data Stream has already been closed.");
|
5521
5371
|
}
|
5522
|
-
|
5523
|
-
|
5524
|
-
|
5525
|
-
|
5526
|
-
|
5527
|
-
|
5528
|
-
|
5529
|
-
break;
|
5372
|
+
if (!this.controller) {
|
5373
|
+
throw new Error("Stream controller is not initialized.");
|
5374
|
+
}
|
5375
|
+
this.controller.close();
|
5376
|
+
this.isClosed = true;
|
5377
|
+
if (this.warningTimeout) {
|
5378
|
+
clearTimeout(this.warningTimeout);
|
5530
5379
|
}
|
5531
|
-
segment += utf8Decoder.decode(chunk, { stream: true });
|
5532
|
-
const linesArray = segment.split(/\r\n|\n|\r/g);
|
5533
|
-
segment = linesArray.pop() || "";
|
5534
|
-
await processLines(linesArray, controller);
|
5535
|
-
}
|
5536
|
-
if (segment) {
|
5537
|
-
const linesArray = [segment];
|
5538
|
-
await processLines(linesArray, controller);
|
5539
5380
|
}
|
5540
|
-
|
5541
|
-
|
5542
|
-
|
5543
|
-
var _a11;
|
5544
|
-
const reader = (_a11 = res.body) == null ? void 0 : _a11.getReader();
|
5545
|
-
return new ReadableStream({
|
5546
|
-
async start(controller) {
|
5547
|
-
if (!reader) {
|
5548
|
-
controller.close();
|
5549
|
-
return;
|
5550
|
-
}
|
5551
|
-
await readAndProcessLines(reader, controller);
|
5381
|
+
append(value) {
|
5382
|
+
if (this.isClosed) {
|
5383
|
+
throw new Error("Data Stream has already been closed.");
|
5552
5384
|
}
|
5553
|
-
|
5554
|
-
|
5555
|
-
async function* streamable2(stream) {
|
5556
|
-
for await (const chunk of stream) {
|
5557
|
-
if (chunk.eventType === "text-generation") {
|
5558
|
-
const text = chunk.text;
|
5559
|
-
if (text)
|
5560
|
-
yield text;
|
5385
|
+
if (!this.controller) {
|
5386
|
+
throw new Error("Stream controller is not initialized.");
|
5561
5387
|
}
|
5388
|
+
this.controller.enqueue(
|
5389
|
+
this.encoder.encode((0, import_ui_utils8.formatStreamPart)("data", [value]))
|
5390
|
+
);
|
5562
5391
|
}
|
5563
|
-
|
5564
|
-
|
5565
|
-
|
5566
|
-
return readableFromAsyncIterable(streamable2(reader)).pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(createStreamDataTransformer());
|
5567
|
-
} else {
|
5568
|
-
return createParser2(reader).pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(createStreamDataTransformer());
|
5569
|
-
}
|
5570
|
-
}
|
5571
|
-
|
5572
|
-
// streams/google-generative-ai-stream.ts
|
5573
|
-
async function* streamable3(response) {
|
5574
|
-
var _a11, _b, _c;
|
5575
|
-
for await (const chunk of response.stream) {
|
5576
|
-
const parts = (_c = (_b = (_a11 = chunk.candidates) == null ? void 0 : _a11[0]) == null ? void 0 : _b.content) == null ? void 0 : _c.parts;
|
5577
|
-
if (parts === void 0) {
|
5578
|
-
continue;
|
5392
|
+
appendMessageAnnotation(value) {
|
5393
|
+
if (this.isClosed) {
|
5394
|
+
throw new Error("Data Stream has already been closed.");
|
5579
5395
|
}
|
5580
|
-
|
5581
|
-
|
5582
|
-
yield firstPart.text;
|
5396
|
+
if (!this.controller) {
|
5397
|
+
throw new Error("Stream controller is not initialized.");
|
5583
5398
|
}
|
5399
|
+
this.controller.enqueue(
|
5400
|
+
this.encoder.encode((0, import_ui_utils8.formatStreamPart)("message_annotations", [value]))
|
5401
|
+
);
|
5584
5402
|
}
|
5585
|
-
}
|
5586
|
-
function
|
5587
|
-
|
5588
|
-
|
5589
|
-
|
5590
|
-
|
5591
|
-
|
5592
|
-
|
5593
|
-
return new ReadableStream({
|
5594
|
-
async pull(controller) {
|
5595
|
-
var _a11, _b;
|
5596
|
-
const { value, done } = await res.next();
|
5597
|
-
if (done) {
|
5598
|
-
controller.close();
|
5599
|
-
return;
|
5600
|
-
}
|
5601
|
-
const text = trimStartOfStream((_b = (_a11 = value.token) == null ? void 0 : _a11.text) != null ? _b : "");
|
5602
|
-
if (!text)
|
5603
|
-
return;
|
5604
|
-
if (value.generated_text != null && value.generated_text.length > 0) {
|
5605
|
-
return;
|
5606
|
-
}
|
5607
|
-
if (text === "</s>" || text === "<|endoftext|>" || text === "<|end|>") {
|
5608
|
-
return;
|
5609
|
-
}
|
5610
|
-
controller.enqueue(text);
|
5403
|
+
};
|
5404
|
+
function createStreamDataTransformer() {
|
5405
|
+
const encoder = new TextEncoder();
|
5406
|
+
const decoder = new TextDecoder();
|
5407
|
+
return new TransformStream({
|
5408
|
+
transform: async (chunk, controller) => {
|
5409
|
+
const message = decoder.decode(chunk);
|
5410
|
+
controller.enqueue(encoder.encode((0, import_ui_utils8.formatStreamPart)("text", message)));
|
5611
5411
|
}
|
5612
5412
|
});
|
5613
5413
|
}
|
5614
|
-
|
5615
|
-
|
5616
|
-
}
|
5617
|
-
|
5618
|
-
// streams/inkeep-stream.ts
|
5619
|
-
function InkeepStream(res, callbacks) {
|
5620
|
-
if (!res.body) {
|
5621
|
-
throw new Error("Response body is null");
|
5622
|
-
}
|
5623
|
-
let chat_session_id = "";
|
5624
|
-
let records_cited;
|
5625
|
-
const inkeepEventParser = (data, options) => {
|
5626
|
-
var _a11, _b;
|
5627
|
-
const { event } = options;
|
5628
|
-
if (event === "records_cited") {
|
5629
|
-
records_cited = JSON.parse(data);
|
5630
|
-
(_a11 = callbacks == null ? void 0 : callbacks.onRecordsCited) == null ? void 0 : _a11.call(callbacks, records_cited);
|
5631
|
-
}
|
5632
|
-
if (event === "message_chunk") {
|
5633
|
-
const inkeepMessageChunk = JSON.parse(data);
|
5634
|
-
chat_session_id = (_b = inkeepMessageChunk.chat_session_id) != null ? _b : chat_session_id;
|
5635
|
-
return inkeepMessageChunk.content_chunk;
|
5636
|
-
}
|
5637
|
-
return;
|
5638
|
-
};
|
5639
|
-
let { onRecordsCited, ...passThroughCallbacks } = callbacks || {};
|
5640
|
-
passThroughCallbacks = {
|
5641
|
-
...passThroughCallbacks,
|
5642
|
-
onFinal: (completion) => {
|
5643
|
-
var _a11;
|
5644
|
-
const inkeepOnFinalMetadata = {
|
5645
|
-
chat_session_id,
|
5646
|
-
records_cited
|
5647
|
-
};
|
5648
|
-
(_a11 = callbacks == null ? void 0 : callbacks.onFinal) == null ? void 0 : _a11.call(callbacks, completion, inkeepOnFinalMetadata);
|
5649
|
-
}
|
5650
|
-
};
|
5651
|
-
return AIStream(res, inkeepEventParser, passThroughCallbacks).pipeThrough(
|
5652
|
-
createStreamDataTransformer()
|
5653
|
-
);
|
5654
|
-
}
|
5414
|
+
var experimental_StreamData = class extends StreamData2 {
|
5415
|
+
};
|
5655
5416
|
|
5656
5417
|
// streams/langchain-adapter.ts
|
5657
|
-
var langchain_adapter_exports = {};
|
5658
|
-
__export(langchain_adapter_exports, {
|
5659
|
-
toAIStream: () => toAIStream,
|
5660
|
-
toDataStream: () => toDataStream,
|
5661
|
-
toDataStreamResponse: () => toDataStreamResponse
|
5662
|
-
});
|
5663
5418
|
function toAIStream(stream, callbacks) {
|
5664
5419
|
return toDataStream(stream, callbacks);
|
5665
5420
|
}
|
@@ -5756,422 +5511,6 @@ function toReadableStream(res) {
|
|
5756
5511
|
});
|
5757
5512
|
}
|
5758
5513
|
|
5759
|
-
// streams/langchain-stream.ts
|
5760
|
-
function LangChainStream(callbacks) {
|
5761
|
-
const stream = new TransformStream();
|
5762
|
-
const writer = stream.writable.getWriter();
|
5763
|
-
const runs = /* @__PURE__ */ new Set();
|
5764
|
-
const handleError = async (e, runId) => {
|
5765
|
-
runs.delete(runId);
|
5766
|
-
await writer.ready;
|
5767
|
-
await writer.abort(e);
|
5768
|
-
};
|
5769
|
-
const handleStart = async (runId) => {
|
5770
|
-
runs.add(runId);
|
5771
|
-
};
|
5772
|
-
const handleEnd = async (runId) => {
|
5773
|
-
runs.delete(runId);
|
5774
|
-
if (runs.size === 0) {
|
5775
|
-
await writer.ready;
|
5776
|
-
await writer.close();
|
5777
|
-
}
|
5778
|
-
};
|
5779
|
-
return {
|
5780
|
-
stream: stream.readable.pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(createStreamDataTransformer()),
|
5781
|
-
writer,
|
5782
|
-
handlers: {
|
5783
|
-
handleLLMNewToken: async (token) => {
|
5784
|
-
await writer.ready;
|
5785
|
-
await writer.write(token);
|
5786
|
-
},
|
5787
|
-
handleLLMStart: async (_llm, _prompts, runId) => {
|
5788
|
-
handleStart(runId);
|
5789
|
-
},
|
5790
|
-
handleLLMEnd: async (_output, runId) => {
|
5791
|
-
await handleEnd(runId);
|
5792
|
-
},
|
5793
|
-
handleLLMError: async (e, runId) => {
|
5794
|
-
await handleError(e, runId);
|
5795
|
-
},
|
5796
|
-
handleChainStart: async (_chain, _inputs, runId) => {
|
5797
|
-
handleStart(runId);
|
5798
|
-
},
|
5799
|
-
handleChainEnd: async (_outputs, runId) => {
|
5800
|
-
await handleEnd(runId);
|
5801
|
-
},
|
5802
|
-
handleChainError: async (e, runId) => {
|
5803
|
-
await handleError(e, runId);
|
5804
|
-
},
|
5805
|
-
handleToolStart: async (_tool, _input, runId) => {
|
5806
|
-
handleStart(runId);
|
5807
|
-
},
|
5808
|
-
handleToolEnd: async (_output, runId) => {
|
5809
|
-
await handleEnd(runId);
|
5810
|
-
},
|
5811
|
-
handleToolError: async (e, runId) => {
|
5812
|
-
await handleError(e, runId);
|
5813
|
-
}
|
5814
|
-
}
|
5815
|
-
};
|
5816
|
-
}
|
5817
|
-
|
5818
|
-
// streams/mistral-stream.ts
|
5819
|
-
async function* streamable4(stream) {
|
5820
|
-
var _a11, _b;
|
5821
|
-
for await (const chunk of stream) {
|
5822
|
-
const content = (_b = (_a11 = chunk.choices[0]) == null ? void 0 : _a11.delta) == null ? void 0 : _b.content;
|
5823
|
-
if (content === void 0 || content === "") {
|
5824
|
-
continue;
|
5825
|
-
}
|
5826
|
-
yield content;
|
5827
|
-
}
|
5828
|
-
}
|
5829
|
-
function MistralStream(response, callbacks) {
|
5830
|
-
const stream = readableFromAsyncIterable(streamable4(response));
|
5831
|
-
return stream.pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(createStreamDataTransformer());
|
5832
|
-
}
|
5833
|
-
|
5834
|
-
// streams/openai-stream.ts
|
5835
|
-
var import_ui_utils9 = require("@ai-sdk/ui-utils");
|
5836
|
-
function parseOpenAIStream() {
|
5837
|
-
const extract = chunkToText();
|
5838
|
-
return (data) => extract(JSON.parse(data));
|
5839
|
-
}
|
5840
|
-
async function* streamable5(stream) {
|
5841
|
-
const extract = chunkToText();
|
5842
|
-
for await (let chunk of stream) {
|
5843
|
-
if ("promptFilterResults" in chunk) {
|
5844
|
-
chunk = {
|
5845
|
-
id: chunk.id,
|
5846
|
-
created: chunk.created.getDate(),
|
5847
|
-
object: chunk.object,
|
5848
|
-
// not exposed by Azure API
|
5849
|
-
model: chunk.model,
|
5850
|
-
// not exposed by Azure API
|
5851
|
-
choices: chunk.choices.map((choice) => {
|
5852
|
-
var _a11, _b, _c, _d, _e, _f, _g;
|
5853
|
-
return {
|
5854
|
-
delta: {
|
5855
|
-
content: (_a11 = choice.delta) == null ? void 0 : _a11.content,
|
5856
|
-
function_call: (_b = choice.delta) == null ? void 0 : _b.functionCall,
|
5857
|
-
role: (_c = choice.delta) == null ? void 0 : _c.role,
|
5858
|
-
tool_calls: ((_e = (_d = choice.delta) == null ? void 0 : _d.toolCalls) == null ? void 0 : _e.length) ? (_g = (_f = choice.delta) == null ? void 0 : _f.toolCalls) == null ? void 0 : _g.map((toolCall, index) => ({
|
5859
|
-
index,
|
5860
|
-
id: toolCall.id,
|
5861
|
-
function: toolCall.function,
|
5862
|
-
type: toolCall.type
|
5863
|
-
})) : void 0
|
5864
|
-
},
|
5865
|
-
finish_reason: choice.finishReason,
|
5866
|
-
index: choice.index
|
5867
|
-
};
|
5868
|
-
})
|
5869
|
-
};
|
5870
|
-
}
|
5871
|
-
const text = extract(chunk);
|
5872
|
-
if (text)
|
5873
|
-
yield text;
|
5874
|
-
}
|
5875
|
-
}
|
5876
|
-
function chunkToText() {
|
5877
|
-
const trimStartOfStream = trimStartOfStreamHelper();
|
5878
|
-
let isFunctionStreamingIn;
|
5879
|
-
return (json) => {
|
5880
|
-
var _a11, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r;
|
5881
|
-
if (isChatCompletionChunk(json)) {
|
5882
|
-
const delta = (_a11 = json.choices[0]) == null ? void 0 : _a11.delta;
|
5883
|
-
if ((_b = delta.function_call) == null ? void 0 : _b.name) {
|
5884
|
-
isFunctionStreamingIn = true;
|
5885
|
-
return {
|
5886
|
-
isText: false,
|
5887
|
-
content: `{"function_call": {"name": "${delta.function_call.name}", "arguments": "`
|
5888
|
-
};
|
5889
|
-
} else if ((_e = (_d = (_c = delta.tool_calls) == null ? void 0 : _c[0]) == null ? void 0 : _d.function) == null ? void 0 : _e.name) {
|
5890
|
-
isFunctionStreamingIn = true;
|
5891
|
-
const toolCall = delta.tool_calls[0];
|
5892
|
-
if (toolCall.index === 0) {
|
5893
|
-
return {
|
5894
|
-
isText: false,
|
5895
|
-
content: `{"tool_calls":[ {"id": "${toolCall.id}", "type": "function", "function": {"name": "${(_f = toolCall.function) == null ? void 0 : _f.name}", "arguments": "`
|
5896
|
-
};
|
5897
|
-
} else {
|
5898
|
-
return {
|
5899
|
-
isText: false,
|
5900
|
-
content: `"}}, {"id": "${toolCall.id}", "type": "function", "function": {"name": "${(_g = toolCall.function) == null ? void 0 : _g.name}", "arguments": "`
|
5901
|
-
};
|
5902
|
-
}
|
5903
|
-
} else if ((_h = delta.function_call) == null ? void 0 : _h.arguments) {
|
5904
|
-
return {
|
5905
|
-
isText: false,
|
5906
|
-
content: cleanupArguments((_i = delta.function_call) == null ? void 0 : _i.arguments)
|
5907
|
-
};
|
5908
|
-
} else if ((_l = (_k = (_j = delta.tool_calls) == null ? void 0 : _j[0]) == null ? void 0 : _k.function) == null ? void 0 : _l.arguments) {
|
5909
|
-
return {
|
5910
|
-
isText: false,
|
5911
|
-
content: cleanupArguments((_o = (_n = (_m = delta.tool_calls) == null ? void 0 : _m[0]) == null ? void 0 : _n.function) == null ? void 0 : _o.arguments)
|
5912
|
-
};
|
5913
|
-
} else if (isFunctionStreamingIn && (((_p = json.choices[0]) == null ? void 0 : _p.finish_reason) === "function_call" || ((_q = json.choices[0]) == null ? void 0 : _q.finish_reason) === "stop")) {
|
5914
|
-
isFunctionStreamingIn = false;
|
5915
|
-
return {
|
5916
|
-
isText: false,
|
5917
|
-
content: '"}}'
|
5918
|
-
};
|
5919
|
-
} else if (isFunctionStreamingIn && ((_r = json.choices[0]) == null ? void 0 : _r.finish_reason) === "tool_calls") {
|
5920
|
-
isFunctionStreamingIn = false;
|
5921
|
-
return {
|
5922
|
-
isText: false,
|
5923
|
-
content: '"}}]}'
|
5924
|
-
};
|
5925
|
-
}
|
5926
|
-
}
|
5927
|
-
const text = trimStartOfStream(
|
5928
|
-
isChatCompletionChunk(json) && json.choices[0].delta.content ? json.choices[0].delta.content : isCompletion(json) ? json.choices[0].text : ""
|
5929
|
-
);
|
5930
|
-
return text;
|
5931
|
-
};
|
5932
|
-
function cleanupArguments(argumentChunk) {
|
5933
|
-
let escapedPartialJson = argumentChunk.replace(/\\/g, "\\\\").replace(/\//g, "\\/").replace(/"/g, '\\"').replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\t/g, "\\t").replace(/\f/g, "\\f");
|
5934
|
-
return `${escapedPartialJson}`;
|
5935
|
-
}
|
5936
|
-
}
|
5937
|
-
var __internal__OpenAIFnMessagesSymbol = Symbol(
|
5938
|
-
"internal_openai_fn_messages"
|
5939
|
-
);
|
5940
|
-
function isChatCompletionChunk(data) {
|
5941
|
-
return "choices" in data && data.choices && data.choices[0] && "delta" in data.choices[0];
|
5942
|
-
}
|
5943
|
-
function isCompletion(data) {
|
5944
|
-
return "choices" in data && data.choices && data.choices[0] && "text" in data.choices[0];
|
5945
|
-
}
|
5946
|
-
function OpenAIStream(res, callbacks) {
|
5947
|
-
const cb = callbacks;
|
5948
|
-
let stream;
|
5949
|
-
if (Symbol.asyncIterator in res) {
|
5950
|
-
stream = readableFromAsyncIterable(streamable5(res)).pipeThrough(
|
5951
|
-
createCallbacksTransformer(
|
5952
|
-
(cb == null ? void 0 : cb.experimental_onFunctionCall) || (cb == null ? void 0 : cb.experimental_onToolCall) ? {
|
5953
|
-
...cb,
|
5954
|
-
onFinal: void 0
|
5955
|
-
} : {
|
5956
|
-
...cb
|
5957
|
-
}
|
5958
|
-
)
|
5959
|
-
);
|
5960
|
-
} else {
|
5961
|
-
stream = AIStream(
|
5962
|
-
res,
|
5963
|
-
parseOpenAIStream(),
|
5964
|
-
(cb == null ? void 0 : cb.experimental_onFunctionCall) || (cb == null ? void 0 : cb.experimental_onToolCall) ? {
|
5965
|
-
...cb,
|
5966
|
-
onFinal: void 0
|
5967
|
-
} : {
|
5968
|
-
...cb
|
5969
|
-
}
|
5970
|
-
);
|
5971
|
-
}
|
5972
|
-
if (cb && (cb.experimental_onFunctionCall || cb.experimental_onToolCall)) {
|
5973
|
-
const functionCallTransformer = createFunctionCallTransformer(cb);
|
5974
|
-
return stream.pipeThrough(functionCallTransformer);
|
5975
|
-
} else {
|
5976
|
-
return stream.pipeThrough(createStreamDataTransformer());
|
5977
|
-
}
|
5978
|
-
}
|
5979
|
-
function createFunctionCallTransformer(callbacks) {
|
5980
|
-
const textEncoder = new TextEncoder();
|
5981
|
-
let isFirstChunk = true;
|
5982
|
-
let aggregatedResponse = "";
|
5983
|
-
let aggregatedFinalCompletionResponse = "";
|
5984
|
-
let isFunctionStreamingIn = false;
|
5985
|
-
let functionCallMessages = callbacks[__internal__OpenAIFnMessagesSymbol] || [];
|
5986
|
-
const decode = (0, import_ui_utils9.createChunkDecoder)();
|
5987
|
-
return new TransformStream({
|
5988
|
-
async transform(chunk, controller) {
|
5989
|
-
const message = decode(chunk);
|
5990
|
-
aggregatedFinalCompletionResponse += message;
|
5991
|
-
const shouldHandleAsFunction = isFirstChunk && (message.startsWith('{"function_call":') || message.startsWith('{"tool_calls":'));
|
5992
|
-
if (shouldHandleAsFunction) {
|
5993
|
-
isFunctionStreamingIn = true;
|
5994
|
-
aggregatedResponse += message;
|
5995
|
-
isFirstChunk = false;
|
5996
|
-
return;
|
5997
|
-
}
|
5998
|
-
if (!isFunctionStreamingIn) {
|
5999
|
-
controller.enqueue(
|
6000
|
-
textEncoder.encode((0, import_ui_utils9.formatStreamPart)("text", message))
|
6001
|
-
);
|
6002
|
-
return;
|
6003
|
-
} else {
|
6004
|
-
aggregatedResponse += message;
|
6005
|
-
}
|
6006
|
-
},
|
6007
|
-
async flush(controller) {
|
6008
|
-
try {
|
6009
|
-
if (!isFirstChunk && isFunctionStreamingIn && (callbacks.experimental_onFunctionCall || callbacks.experimental_onToolCall)) {
|
6010
|
-
isFunctionStreamingIn = false;
|
6011
|
-
const payload = JSON.parse(aggregatedResponse);
|
6012
|
-
let newFunctionCallMessages = [
|
6013
|
-
...functionCallMessages
|
6014
|
-
];
|
6015
|
-
let functionResponse = void 0;
|
6016
|
-
if (callbacks.experimental_onFunctionCall) {
|
6017
|
-
if (payload.function_call === void 0) {
|
6018
|
-
console.warn(
|
6019
|
-
"experimental_onFunctionCall should not be defined when using tools"
|
6020
|
-
);
|
6021
|
-
}
|
6022
|
-
const argumentsPayload = JSON.parse(
|
6023
|
-
payload.function_call.arguments
|
6024
|
-
);
|
6025
|
-
functionResponse = await callbacks.experimental_onFunctionCall(
|
6026
|
-
{
|
6027
|
-
name: payload.function_call.name,
|
6028
|
-
arguments: argumentsPayload
|
6029
|
-
},
|
6030
|
-
(result) => {
|
6031
|
-
newFunctionCallMessages = [
|
6032
|
-
...functionCallMessages,
|
6033
|
-
{
|
6034
|
-
role: "assistant",
|
6035
|
-
content: "",
|
6036
|
-
function_call: payload.function_call
|
6037
|
-
},
|
6038
|
-
{
|
6039
|
-
role: "function",
|
6040
|
-
name: payload.function_call.name,
|
6041
|
-
content: JSON.stringify(result)
|
6042
|
-
}
|
6043
|
-
];
|
6044
|
-
return newFunctionCallMessages;
|
6045
|
-
}
|
6046
|
-
);
|
6047
|
-
}
|
6048
|
-
if (callbacks.experimental_onToolCall) {
|
6049
|
-
const toolCalls = {
|
6050
|
-
tools: []
|
6051
|
-
};
|
6052
|
-
for (const tool2 of payload.tool_calls) {
|
6053
|
-
toolCalls.tools.push({
|
6054
|
-
id: tool2.id,
|
6055
|
-
type: "function",
|
6056
|
-
func: {
|
6057
|
-
name: tool2.function.name,
|
6058
|
-
arguments: JSON.parse(tool2.function.arguments)
|
6059
|
-
}
|
6060
|
-
});
|
6061
|
-
}
|
6062
|
-
let responseIndex = 0;
|
6063
|
-
try {
|
6064
|
-
functionResponse = await callbacks.experimental_onToolCall(
|
6065
|
-
toolCalls,
|
6066
|
-
(result) => {
|
6067
|
-
if (result) {
|
6068
|
-
const { tool_call_id, function_name, tool_call_result } = result;
|
6069
|
-
newFunctionCallMessages = [
|
6070
|
-
...newFunctionCallMessages,
|
6071
|
-
// Only append the assistant message if it's the first response
|
6072
|
-
...responseIndex === 0 ? [
|
6073
|
-
{
|
6074
|
-
role: "assistant",
|
6075
|
-
content: "",
|
6076
|
-
tool_calls: payload.tool_calls.map(
|
6077
|
-
(tc) => ({
|
6078
|
-
id: tc.id,
|
6079
|
-
type: "function",
|
6080
|
-
function: {
|
6081
|
-
name: tc.function.name,
|
6082
|
-
// we send the arguments an object to the user, but as the API expects a string, we need to stringify it
|
6083
|
-
arguments: JSON.stringify(
|
6084
|
-
tc.function.arguments
|
6085
|
-
)
|
6086
|
-
}
|
6087
|
-
})
|
6088
|
-
)
|
6089
|
-
}
|
6090
|
-
] : [],
|
6091
|
-
// Append the function call result message
|
6092
|
-
{
|
6093
|
-
role: "tool",
|
6094
|
-
tool_call_id,
|
6095
|
-
name: function_name,
|
6096
|
-
content: JSON.stringify(tool_call_result)
|
6097
|
-
}
|
6098
|
-
];
|
6099
|
-
responseIndex++;
|
6100
|
-
}
|
6101
|
-
return newFunctionCallMessages;
|
6102
|
-
}
|
6103
|
-
);
|
6104
|
-
} catch (e) {
|
6105
|
-
console.error("Error calling experimental_onToolCall:", e);
|
6106
|
-
}
|
6107
|
-
}
|
6108
|
-
if (!functionResponse) {
|
6109
|
-
controller.enqueue(
|
6110
|
-
textEncoder.encode(
|
6111
|
-
(0, import_ui_utils9.formatStreamPart)(
|
6112
|
-
payload.function_call ? "function_call" : "tool_calls",
|
6113
|
-
// parse to prevent double-encoding:
|
6114
|
-
JSON.parse(aggregatedResponse)
|
6115
|
-
)
|
6116
|
-
)
|
6117
|
-
);
|
6118
|
-
return;
|
6119
|
-
} else if (typeof functionResponse === "string") {
|
6120
|
-
controller.enqueue(
|
6121
|
-
textEncoder.encode((0, import_ui_utils9.formatStreamPart)("text", functionResponse))
|
6122
|
-
);
|
6123
|
-
aggregatedFinalCompletionResponse = functionResponse;
|
6124
|
-
return;
|
6125
|
-
}
|
6126
|
-
const filteredCallbacks = {
|
6127
|
-
...callbacks,
|
6128
|
-
onStart: void 0
|
6129
|
-
};
|
6130
|
-
callbacks.onFinal = void 0;
|
6131
|
-
const openAIStream = OpenAIStream(functionResponse, {
|
6132
|
-
...filteredCallbacks,
|
6133
|
-
[__internal__OpenAIFnMessagesSymbol]: newFunctionCallMessages
|
6134
|
-
});
|
6135
|
-
const reader = openAIStream.getReader();
|
6136
|
-
while (true) {
|
6137
|
-
const { done, value } = await reader.read();
|
6138
|
-
if (done) {
|
6139
|
-
break;
|
6140
|
-
}
|
6141
|
-
controller.enqueue(value);
|
6142
|
-
}
|
6143
|
-
}
|
6144
|
-
} finally {
|
6145
|
-
if (callbacks.onFinal && aggregatedFinalCompletionResponse) {
|
6146
|
-
await callbacks.onFinal(aggregatedFinalCompletionResponse);
|
6147
|
-
}
|
6148
|
-
}
|
6149
|
-
}
|
6150
|
-
});
|
6151
|
-
}
|
6152
|
-
|
6153
|
-
// streams/replicate-stream.ts
|
6154
|
-
async function ReplicateStream(res, cb, options) {
|
6155
|
-
var _a11;
|
6156
|
-
const url = (_a11 = res.urls) == null ? void 0 : _a11.stream;
|
6157
|
-
if (!url) {
|
6158
|
-
if (res.error)
|
6159
|
-
throw new Error(res.error);
|
6160
|
-
else
|
6161
|
-
throw new Error("Missing stream URL in Replicate response");
|
6162
|
-
}
|
6163
|
-
const eventStream = await fetch(url, {
|
6164
|
-
method: "GET",
|
6165
|
-
headers: {
|
6166
|
-
Accept: "text/event-stream",
|
6167
|
-
...options == null ? void 0 : options.headers
|
6168
|
-
}
|
6169
|
-
});
|
6170
|
-
return AIStream(eventStream, void 0, cb).pipeThrough(
|
6171
|
-
createStreamDataTransformer()
|
6172
|
-
);
|
6173
|
-
}
|
6174
|
-
|
6175
5514
|
// streams/stream-to-response.ts
|
6176
5515
|
function streamToResponse(res, response, init, data) {
|
6177
5516
|
var _a11;
|
@@ -6216,25 +5555,14 @@ var StreamingTextResponse = class extends Response {
|
|
6216
5555
|
|
6217
5556
|
// streams/index.ts
|
6218
5557
|
var generateId2 = import_provider_utils10.generateId;
|
6219
|
-
var nanoid = import_provider_utils10.generateId;
|
6220
5558
|
// Annotate the CommonJS export names for ESM import in node:
|
6221
5559
|
0 && (module.exports = {
|
6222
5560
|
AISDKError,
|
6223
5561
|
AIStream,
|
6224
5562
|
APICallError,
|
6225
|
-
AWSBedrockAnthropicMessagesStream,
|
6226
|
-
AWSBedrockAnthropicStream,
|
6227
|
-
AWSBedrockCohereStream,
|
6228
|
-
AWSBedrockLlama2Stream,
|
6229
|
-
AWSBedrockStream,
|
6230
|
-
AnthropicStream,
|
6231
5563
|
AssistantResponse,
|
6232
|
-
CohereStream,
|
6233
5564
|
DownloadError,
|
6234
5565
|
EmptyResponseBodyError,
|
6235
|
-
GoogleGenerativeAIStream,
|
6236
|
-
HuggingFaceStream,
|
6237
|
-
InkeepStream,
|
6238
5566
|
InvalidArgumentError,
|
6239
5567
|
InvalidDataContentError,
|
6240
5568
|
InvalidMessageRoleError,
|
@@ -6243,18 +5571,14 @@ var nanoid = import_provider_utils10.generateId;
|
|
6243
5571
|
InvalidToolArgumentsError,
|
6244
5572
|
JSONParseError,
|
6245
5573
|
LangChainAdapter,
|
6246
|
-
LangChainStream,
|
6247
5574
|
LlamaIndexAdapter,
|
6248
5575
|
LoadAPIKeyError,
|
6249
5576
|
MessageConversionError,
|
6250
|
-
MistralStream,
|
6251
5577
|
NoContentGeneratedError,
|
6252
5578
|
NoObjectGeneratedError,
|
6253
5579
|
NoSuchModelError,
|
6254
5580
|
NoSuchProviderError,
|
6255
5581
|
NoSuchToolError,
|
6256
|
-
OpenAIStream,
|
6257
|
-
ReplicateStream,
|
6258
5582
|
RetryError,
|
6259
5583
|
StreamData,
|
6260
5584
|
StreamingTextResponse,
|
@@ -6282,7 +5606,6 @@ var nanoid = import_provider_utils10.generateId;
|
|
6282
5606
|
generateObject,
|
6283
5607
|
generateText,
|
6284
5608
|
jsonSchema,
|
6285
|
-
nanoid,
|
6286
5609
|
parseStreamPart,
|
6287
5610
|
processDataProtocolResponse,
|
6288
5611
|
readDataStream,
|