ai 3.4.32 → 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 +21 -0
- package/dist/index.d.mts +6 -474
- package/dist/index.d.ts +6 -474
- package/dist/index.js +120 -783
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +107 -758
- 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
|
@@ -3395,7 +3380,7 @@ async function generateText({
|
|
3395
3380
|
}),
|
3396
3381
|
tracer,
|
3397
3382
|
fn: async (span) => {
|
3398
|
-
var _a11, _b, _c, _d, _e, _f
|
3383
|
+
var _a11, _b, _c, _d, _e, _f;
|
3399
3384
|
const retry = retryWithExponentialBackoff({ maxRetries });
|
3400
3385
|
const mode = {
|
3401
3386
|
type: "regular",
|
@@ -3549,12 +3534,15 @@ async function generateText({
|
|
3549
3534
|
nextStepType = "tool-result";
|
3550
3535
|
}
|
3551
3536
|
}
|
3552
|
-
const
|
3537
|
+
const originalText = (_b = currentModelResponse.text) != null ? _b : "";
|
3538
|
+
const stepTextLeadingWhitespaceTrimmed = stepType === "continue" && // only for continue steps
|
3539
|
+
text.trimEnd() !== text ? originalText.trimStart() : originalText;
|
3540
|
+
const stepText = nextStepType === "continue" ? removeTextAfterLastWhitespace(stepTextLeadingWhitespaceTrimmed) : stepTextLeadingWhitespaceTrimmed;
|
3553
3541
|
text = nextStepType === "continue" || stepType === "continue" ? text + stepText : stepText;
|
3554
3542
|
if (stepType === "continue") {
|
3555
3543
|
const lastMessage = responseMessages[responseMessages.length - 1];
|
3556
3544
|
if (typeof lastMessage.content === "string") {
|
3557
|
-
lastMessage.content
|
3545
|
+
lastMessage.content += stepText;
|
3558
3546
|
} else {
|
3559
3547
|
lastMessage.content.push({
|
3560
3548
|
text: stepText,
|
@@ -3580,10 +3568,10 @@ async function generateText({
|
|
3580
3568
|
usage: currentUsage,
|
3581
3569
|
warnings: currentModelResponse.warnings,
|
3582
3570
|
logprobs: currentModelResponse.logprobs,
|
3583
|
-
request: (
|
3571
|
+
request: (_c = currentModelResponse.request) != null ? _c : {},
|
3584
3572
|
response: {
|
3585
3573
|
...currentModelResponse.response,
|
3586
|
-
headers: (
|
3574
|
+
headers: (_d = currentModelResponse.rawResponse) == null ? void 0 : _d.headers,
|
3587
3575
|
// deep clone msgs to avoid mutating past messages in multi-step:
|
3588
3576
|
messages: JSON.parse(JSON.stringify(responseMessages))
|
3589
3577
|
},
|
@@ -3625,10 +3613,10 @@ async function generateText({
|
|
3625
3613
|
finishReason: currentModelResponse.finishReason,
|
3626
3614
|
usage,
|
3627
3615
|
warnings: currentModelResponse.warnings,
|
3628
|
-
request: (
|
3616
|
+
request: (_e = currentModelResponse.request) != null ? _e : {},
|
3629
3617
|
response: {
|
3630
3618
|
...currentModelResponse.response,
|
3631
|
-
headers: (
|
3619
|
+
headers: (_f = currentModelResponse.rawResponse) == null ? void 0 : _f.headers,
|
3632
3620
|
messages: responseMessages
|
3633
3621
|
},
|
3634
3622
|
logprobs: currentModelResponse.logprobs,
|
@@ -4328,7 +4316,8 @@ var DefaultStreamTextResult = class {
|
|
4328
4316
|
},
|
4329
4317
|
stepType,
|
4330
4318
|
previousStepText = "",
|
4331
|
-
stepRequest
|
4319
|
+
stepRequest,
|
4320
|
+
hasLeadingWhitespace
|
4332
4321
|
}) {
|
4333
4322
|
const stepToolCalls = [];
|
4334
4323
|
const stepToolResults = [];
|
@@ -4350,6 +4339,8 @@ var DefaultStreamTextResult = class {
|
|
4350
4339
|
};
|
4351
4340
|
let chunkBuffer = "";
|
4352
4341
|
let chunkTextPublished = false;
|
4342
|
+
let inWhitespacePrefix = true;
|
4343
|
+
let hasWhitespaceSuffix = false;
|
4353
4344
|
async function publishTextChunk({
|
4354
4345
|
controller,
|
4355
4346
|
chunk
|
@@ -4358,6 +4349,7 @@ var DefaultStreamTextResult = class {
|
|
4358
4349
|
stepText += chunk.textDelta;
|
4359
4350
|
fullStepText += chunk.textDelta;
|
4360
4351
|
chunkTextPublished = true;
|
4352
|
+
hasWhitespaceSuffix = chunk.textDelta.trimEnd() !== chunk.textDelta;
|
4361
4353
|
await (onChunk == null ? void 0 : onChunk({ chunk }));
|
4362
4354
|
}
|
4363
4355
|
addStream(
|
@@ -4386,7 +4378,12 @@ var DefaultStreamTextResult = class {
|
|
4386
4378
|
switch (chunkType) {
|
4387
4379
|
case "text-delta": {
|
4388
4380
|
if (continueSteps) {
|
4389
|
-
|
4381
|
+
const trimmedChunkText = inWhitespacePrefix && hasLeadingWhitespace ? chunk.textDelta.trimStart() : chunk.textDelta;
|
4382
|
+
if (trimmedChunkText.length === 0) {
|
4383
|
+
break;
|
4384
|
+
}
|
4385
|
+
inWhitespacePrefix = false;
|
4386
|
+
chunkBuffer += trimmedChunkText;
|
4390
4387
|
const split = splitOnLastWhitespace(chunkBuffer);
|
4391
4388
|
if (split != null) {
|
4392
4389
|
chunkBuffer = split.suffix;
|
@@ -4529,7 +4526,7 @@ var DefaultStreamTextResult = class {
|
|
4529
4526
|
if (stepType === "continue") {
|
4530
4527
|
const lastMessage = responseMessages[responseMessages.length - 1];
|
4531
4528
|
if (typeof lastMessage.content === "string") {
|
4532
|
-
lastMessage.content
|
4529
|
+
lastMessage.content += stepText;
|
4533
4530
|
} else {
|
4534
4531
|
lastMessage.content.push({
|
4535
4532
|
text: stepText,
|
@@ -4590,7 +4587,8 @@ var DefaultStreamTextResult = class {
|
|
4590
4587
|
usage: combinedUsage,
|
4591
4588
|
stepType: nextStepType,
|
4592
4589
|
previousStepText: fullStepText,
|
4593
|
-
stepRequest: result.request
|
4590
|
+
stepRequest: result.request,
|
4591
|
+
hasLeadingWhitespace: hasWhitespaceSuffix
|
4594
4592
|
});
|
4595
4593
|
return;
|
4596
4594
|
}
|
@@ -4681,7 +4679,8 @@ var DefaultStreamTextResult = class {
|
|
4681
4679
|
responseMessages: [],
|
4682
4680
|
usage: void 0,
|
4683
4681
|
stepType: "initial",
|
4684
|
-
stepRequest: request
|
4682
|
+
stepRequest: request,
|
4683
|
+
hasLeadingWhitespace: false
|
4685
4684
|
});
|
4686
4685
|
}
|
4687
4686
|
/**
|
@@ -4753,12 +4752,12 @@ var DefaultStreamTextResult = class {
|
|
4753
4752
|
const chunkType = chunk.type;
|
4754
4753
|
switch (chunkType) {
|
4755
4754
|
case "text-delta": {
|
4756
|
-
controller.enqueue((0,
|
4755
|
+
controller.enqueue((0, import_ui_utils9.formatStreamPart)("text", chunk.textDelta));
|
4757
4756
|
break;
|
4758
4757
|
}
|
4759
4758
|
case "tool-call-streaming-start": {
|
4760
4759
|
controller.enqueue(
|
4761
|
-
(0,
|
4760
|
+
(0, import_ui_utils9.formatStreamPart)("tool_call_streaming_start", {
|
4762
4761
|
toolCallId: chunk.toolCallId,
|
4763
4762
|
toolName: chunk.toolName
|
4764
4763
|
})
|
@@ -4767,7 +4766,7 @@ var DefaultStreamTextResult = class {
|
|
4767
4766
|
}
|
4768
4767
|
case "tool-call-delta": {
|
4769
4768
|
controller.enqueue(
|
4770
|
-
(0,
|
4769
|
+
(0, import_ui_utils9.formatStreamPart)("tool_call_delta", {
|
4771
4770
|
toolCallId: chunk.toolCallId,
|
4772
4771
|
argsTextDelta: chunk.argsTextDelta
|
4773
4772
|
})
|
@@ -4776,7 +4775,7 @@ var DefaultStreamTextResult = class {
|
|
4776
4775
|
}
|
4777
4776
|
case "tool-call": {
|
4778
4777
|
controller.enqueue(
|
4779
|
-
(0,
|
4778
|
+
(0, import_ui_utils9.formatStreamPart)("tool_call", {
|
4780
4779
|
toolCallId: chunk.toolCallId,
|
4781
4780
|
toolName: chunk.toolName,
|
4782
4781
|
args: chunk.args
|
@@ -4786,7 +4785,7 @@ var DefaultStreamTextResult = class {
|
|
4786
4785
|
}
|
4787
4786
|
case "tool-result": {
|
4788
4787
|
controller.enqueue(
|
4789
|
-
(0,
|
4788
|
+
(0, import_ui_utils9.formatStreamPart)("tool_result", {
|
4790
4789
|
toolCallId: chunk.toolCallId,
|
4791
4790
|
result: chunk.result
|
4792
4791
|
})
|
@@ -4795,13 +4794,13 @@ var DefaultStreamTextResult = class {
|
|
4795
4794
|
}
|
4796
4795
|
case "error": {
|
4797
4796
|
controller.enqueue(
|
4798
|
-
(0,
|
4797
|
+
(0, import_ui_utils9.formatStreamPart)("error", getErrorMessage3(chunk.error))
|
4799
4798
|
);
|
4800
4799
|
break;
|
4801
4800
|
}
|
4802
4801
|
case "step-finish": {
|
4803
4802
|
controller.enqueue(
|
4804
|
-
(0,
|
4803
|
+
(0, import_ui_utils9.formatStreamPart)("finish_step", {
|
4805
4804
|
finishReason: chunk.finishReason,
|
4806
4805
|
usage: sendUsage ? {
|
4807
4806
|
promptTokens: chunk.usage.promptTokens,
|
@@ -4814,7 +4813,7 @@ var DefaultStreamTextResult = class {
|
|
4814
4813
|
}
|
4815
4814
|
case "finish": {
|
4816
4815
|
controller.enqueue(
|
4817
|
-
(0,
|
4816
|
+
(0, import_ui_utils9.formatStreamPart)("finish_message", {
|
4818
4817
|
finishReason: chunk.finishReason,
|
4819
4818
|
usage: sendUsage ? {
|
4820
4819
|
promptTokens: chunk.usage.promptTokens,
|
@@ -5166,19 +5165,12 @@ function createCallbacksTransformer(cb) {
|
|
5166
5165
|
}
|
5167
5166
|
},
|
5168
5167
|
async flush() {
|
5169
|
-
const isOpenAICallbacks = isOfTypeOpenAIStreamCallbacks(callbacks);
|
5170
5168
|
if (callbacks.onCompletion) {
|
5171
5169
|
await callbacks.onCompletion(aggregatedResponse);
|
5172
5170
|
}
|
5173
|
-
if (callbacks.onFinal && !isOpenAICallbacks) {
|
5174
|
-
await callbacks.onFinal(aggregatedResponse);
|
5175
|
-
}
|
5176
5171
|
}
|
5177
5172
|
});
|
5178
5173
|
}
|
5179
|
-
function isOfTypeOpenAIStreamCallbacks(callbacks) {
|
5180
|
-
return "experimental_onFunctionCall" in callbacks;
|
5181
|
-
}
|
5182
5174
|
function trimStartOfStreamHelper() {
|
5183
5175
|
let isStreamStart = true;
|
5184
5176
|
return (text) => {
|
@@ -5238,135 +5230,8 @@ function readableFromAsyncIterable(iterable) {
|
|
5238
5230
|
});
|
5239
5231
|
}
|
5240
5232
|
|
5241
|
-
// streams/stream-data.ts
|
5242
|
-
var import_ui_utils7 = require("@ai-sdk/ui-utils");
|
5243
|
-
|
5244
|
-
// util/constants.ts
|
5245
|
-
var HANGING_STREAM_WARNING_TIME_MS = 15 * 1e3;
|
5246
|
-
|
5247
|
-
// streams/stream-data.ts
|
5248
|
-
var StreamData2 = class {
|
5249
|
-
constructor() {
|
5250
|
-
this.encoder = new TextEncoder();
|
5251
|
-
this.controller = null;
|
5252
|
-
this.isClosed = false;
|
5253
|
-
this.warningTimeout = null;
|
5254
|
-
const self = this;
|
5255
|
-
this.stream = new ReadableStream({
|
5256
|
-
start: async (controller) => {
|
5257
|
-
self.controller = controller;
|
5258
|
-
if (process.env.NODE_ENV === "development") {
|
5259
|
-
self.warningTimeout = setTimeout(() => {
|
5260
|
-
console.warn(
|
5261
|
-
"The data stream is hanging. Did you forget to close it with `data.close()`?"
|
5262
|
-
);
|
5263
|
-
}, HANGING_STREAM_WARNING_TIME_MS);
|
5264
|
-
}
|
5265
|
-
},
|
5266
|
-
pull: (controller) => {
|
5267
|
-
},
|
5268
|
-
cancel: (reason) => {
|
5269
|
-
this.isClosed = true;
|
5270
|
-
}
|
5271
|
-
});
|
5272
|
-
}
|
5273
|
-
async close() {
|
5274
|
-
if (this.isClosed) {
|
5275
|
-
throw new Error("Data Stream has already been closed.");
|
5276
|
-
}
|
5277
|
-
if (!this.controller) {
|
5278
|
-
throw new Error("Stream controller is not initialized.");
|
5279
|
-
}
|
5280
|
-
this.controller.close();
|
5281
|
-
this.isClosed = true;
|
5282
|
-
if (this.warningTimeout) {
|
5283
|
-
clearTimeout(this.warningTimeout);
|
5284
|
-
}
|
5285
|
-
}
|
5286
|
-
append(value) {
|
5287
|
-
if (this.isClosed) {
|
5288
|
-
throw new Error("Data Stream has already been closed.");
|
5289
|
-
}
|
5290
|
-
if (!this.controller) {
|
5291
|
-
throw new Error("Stream controller is not initialized.");
|
5292
|
-
}
|
5293
|
-
this.controller.enqueue(
|
5294
|
-
this.encoder.encode((0, import_ui_utils7.formatStreamPart)("data", [value]))
|
5295
|
-
);
|
5296
|
-
}
|
5297
|
-
appendMessageAnnotation(value) {
|
5298
|
-
if (this.isClosed) {
|
5299
|
-
throw new Error("Data Stream has already been closed.");
|
5300
|
-
}
|
5301
|
-
if (!this.controller) {
|
5302
|
-
throw new Error("Stream controller is not initialized.");
|
5303
|
-
}
|
5304
|
-
this.controller.enqueue(
|
5305
|
-
this.encoder.encode((0, import_ui_utils7.formatStreamPart)("message_annotations", [value]))
|
5306
|
-
);
|
5307
|
-
}
|
5308
|
-
};
|
5309
|
-
function createStreamDataTransformer() {
|
5310
|
-
const encoder = new TextEncoder();
|
5311
|
-
const decoder = new TextDecoder();
|
5312
|
-
return new TransformStream({
|
5313
|
-
transform: async (chunk, controller) => {
|
5314
|
-
const message = decoder.decode(chunk);
|
5315
|
-
controller.enqueue(encoder.encode((0, import_ui_utils7.formatStreamPart)("text", message)));
|
5316
|
-
}
|
5317
|
-
});
|
5318
|
-
}
|
5319
|
-
var experimental_StreamData = class extends StreamData2 {
|
5320
|
-
};
|
5321
|
-
|
5322
|
-
// streams/anthropic-stream.ts
|
5323
|
-
function parseAnthropicStream() {
|
5324
|
-
let previous = "";
|
5325
|
-
return (data) => {
|
5326
|
-
const json = JSON.parse(data);
|
5327
|
-
if ("error" in json) {
|
5328
|
-
throw new Error(`${json.error.type}: ${json.error.message}`);
|
5329
|
-
}
|
5330
|
-
if (!("completion" in json)) {
|
5331
|
-
return;
|
5332
|
-
}
|
5333
|
-
const text = json.completion;
|
5334
|
-
if (!previous || text.length > previous.length && text.startsWith(previous)) {
|
5335
|
-
const delta = text.slice(previous.length);
|
5336
|
-
previous = text;
|
5337
|
-
return delta;
|
5338
|
-
}
|
5339
|
-
return text;
|
5340
|
-
};
|
5341
|
-
}
|
5342
|
-
async function* streamable(stream) {
|
5343
|
-
for await (const chunk of stream) {
|
5344
|
-
if ("completion" in chunk) {
|
5345
|
-
const text = chunk.completion;
|
5346
|
-
if (text)
|
5347
|
-
yield text;
|
5348
|
-
} else if ("delta" in chunk) {
|
5349
|
-
const { delta } = chunk;
|
5350
|
-
if ("text" in delta) {
|
5351
|
-
const text = delta.text;
|
5352
|
-
if (text)
|
5353
|
-
yield text;
|
5354
|
-
}
|
5355
|
-
}
|
5356
|
-
}
|
5357
|
-
}
|
5358
|
-
function AnthropicStream(res, cb) {
|
5359
|
-
if (Symbol.asyncIterator in res) {
|
5360
|
-
return readableFromAsyncIterable(streamable(res)).pipeThrough(createCallbacksTransformer(cb)).pipeThrough(createStreamDataTransformer());
|
5361
|
-
} else {
|
5362
|
-
return AIStream(res, parseAnthropicStream(), cb).pipeThrough(
|
5363
|
-
createStreamDataTransformer()
|
5364
|
-
);
|
5365
|
-
}
|
5366
|
-
}
|
5367
|
-
|
5368
5233
|
// streams/assistant-response.ts
|
5369
|
-
var
|
5234
|
+
var import_ui_utils7 = require("@ai-sdk/ui-utils");
|
5370
5235
|
function AssistantResponse({ threadId, messageId }, process2) {
|
5371
5236
|
const stream = new ReadableStream({
|
5372
5237
|
async start(controller) {
|
@@ -5374,17 +5239,17 @@ function AssistantResponse({ threadId, messageId }, process2) {
|
|
5374
5239
|
const textEncoder = new TextEncoder();
|
5375
5240
|
const sendMessage = (message) => {
|
5376
5241
|
controller.enqueue(
|
5377
|
-
textEncoder.encode((0,
|
5242
|
+
textEncoder.encode((0, import_ui_utils7.formatStreamPart)("assistant_message", message))
|
5378
5243
|
);
|
5379
5244
|
};
|
5380
5245
|
const sendDataMessage = (message) => {
|
5381
5246
|
controller.enqueue(
|
5382
|
-
textEncoder.encode((0,
|
5247
|
+
textEncoder.encode((0, import_ui_utils7.formatStreamPart)("data_message", message))
|
5383
5248
|
);
|
5384
5249
|
};
|
5385
5250
|
const sendError = (errorMessage) => {
|
5386
5251
|
controller.enqueue(
|
5387
|
-
textEncoder.encode((0,
|
5252
|
+
textEncoder.encode((0, import_ui_utils7.formatStreamPart)("error", errorMessage))
|
5388
5253
|
);
|
5389
5254
|
};
|
5390
5255
|
const forwardStream = async (stream2) => {
|
@@ -5395,7 +5260,7 @@ function AssistantResponse({ threadId, messageId }, process2) {
|
|
5395
5260
|
case "thread.message.created": {
|
5396
5261
|
controller.enqueue(
|
5397
5262
|
textEncoder.encode(
|
5398
|
-
(0,
|
5263
|
+
(0, import_ui_utils7.formatStreamPart)("assistant_message", {
|
5399
5264
|
id: value.data.id,
|
5400
5265
|
role: "assistant",
|
5401
5266
|
content: [{ type: "text", text: { value: "" } }]
|
@@ -5409,7 +5274,7 @@ function AssistantResponse({ threadId, messageId }, process2) {
|
|
5409
5274
|
if ((content == null ? void 0 : content.type) === "text" && ((_b = content.text) == null ? void 0 : _b.value) != null) {
|
5410
5275
|
controller.enqueue(
|
5411
5276
|
textEncoder.encode(
|
5412
|
-
(0,
|
5277
|
+
(0, import_ui_utils7.formatStreamPart)("text", content.text.value)
|
5413
5278
|
)
|
5414
5279
|
);
|
5415
5280
|
}
|
@@ -5426,7 +5291,7 @@ function AssistantResponse({ threadId, messageId }, process2) {
|
|
5426
5291
|
};
|
5427
5292
|
controller.enqueue(
|
5428
5293
|
textEncoder.encode(
|
5429
|
-
(0,
|
5294
|
+
(0, import_ui_utils7.formatStreamPart)("assistant_control_data", {
|
5430
5295
|
threadId,
|
5431
5296
|
messageId
|
5432
5297
|
})
|
@@ -5460,192 +5325,96 @@ function AssistantResponse({ threadId, messageId }, process2) {
|
|
5460
5325
|
}
|
5461
5326
|
var experimental_AssistantResponse = AssistantResponse;
|
5462
5327
|
|
5463
|
-
// streams/
|
5464
|
-
|
5465
|
-
|
5466
|
-
|
5467
|
-
|
5468
|
-
|
5469
|
-
|
5470
|
-
|
5471
|
-
|
5472
|
-
|
5473
|
-
|
5474
|
-
|
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;
|
5475
5365
|
}
|
5476
|
-
}
|
5366
|
+
});
|
5477
5367
|
}
|
5478
|
-
|
5479
|
-
|
5480
|
-
|
5481
|
-
var _a11;
|
5482
|
-
return (_a11 = chunk.delta) == null ? void 0 : _a11.text;
|
5483
|
-
});
|
5484
|
-
}
|
5485
|
-
function AWSBedrockAnthropicStream(response, callbacks) {
|
5486
|
-
return AWSBedrockStream(response, callbacks, (chunk) => chunk.completion);
|
5487
|
-
}
|
5488
|
-
function AWSBedrockCohereStream(response, callbacks) {
|
5489
|
-
return AWSBedrockStream(response, callbacks, (chunk) => chunk == null ? void 0 : chunk.text);
|
5490
|
-
}
|
5491
|
-
function AWSBedrockLlama2Stream(response, callbacks) {
|
5492
|
-
return AWSBedrockStream(response, callbacks, (chunk) => chunk.generation);
|
5493
|
-
}
|
5494
|
-
function AWSBedrockStream(response, callbacks, extractTextDeltaFromChunk) {
|
5495
|
-
return readableFromAsyncIterable(
|
5496
|
-
asDeltaIterable(response, extractTextDeltaFromChunk)
|
5497
|
-
).pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(createStreamDataTransformer());
|
5498
|
-
}
|
5499
|
-
|
5500
|
-
// streams/cohere-stream.ts
|
5501
|
-
var utf8Decoder = new TextDecoder("utf-8");
|
5502
|
-
async function processLines(lines, controller) {
|
5503
|
-
for (const line of lines) {
|
5504
|
-
const { text, is_finished } = JSON.parse(line);
|
5505
|
-
if (!is_finished) {
|
5506
|
-
controller.enqueue(text);
|
5368
|
+
async close() {
|
5369
|
+
if (this.isClosed) {
|
5370
|
+
throw new Error("Data Stream has already been closed.");
|
5507
5371
|
}
|
5508
|
-
|
5509
|
-
|
5510
|
-
|
5511
|
-
|
5512
|
-
|
5513
|
-
|
5514
|
-
|
5515
|
-
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);
|
5516
5379
|
}
|
5517
|
-
segment += utf8Decoder.decode(chunk, { stream: true });
|
5518
|
-
const linesArray = segment.split(/\r\n|\n|\r/g);
|
5519
|
-
segment = linesArray.pop() || "";
|
5520
|
-
await processLines(linesArray, controller);
|
5521
|
-
}
|
5522
|
-
if (segment) {
|
5523
|
-
const linesArray = [segment];
|
5524
|
-
await processLines(linesArray, controller);
|
5525
5380
|
}
|
5526
|
-
|
5527
|
-
|
5528
|
-
|
5529
|
-
var _a11;
|
5530
|
-
const reader = (_a11 = res.body) == null ? void 0 : _a11.getReader();
|
5531
|
-
return new ReadableStream({
|
5532
|
-
async start(controller) {
|
5533
|
-
if (!reader) {
|
5534
|
-
controller.close();
|
5535
|
-
return;
|
5536
|
-
}
|
5537
|
-
await readAndProcessLines(reader, controller);
|
5381
|
+
append(value) {
|
5382
|
+
if (this.isClosed) {
|
5383
|
+
throw new Error("Data Stream has already been closed.");
|
5538
5384
|
}
|
5539
|
-
|
5540
|
-
|
5541
|
-
async function* streamable2(stream) {
|
5542
|
-
for await (const chunk of stream) {
|
5543
|
-
if (chunk.eventType === "text-generation") {
|
5544
|
-
const text = chunk.text;
|
5545
|
-
if (text)
|
5546
|
-
yield text;
|
5385
|
+
if (!this.controller) {
|
5386
|
+
throw new Error("Stream controller is not initialized.");
|
5547
5387
|
}
|
5388
|
+
this.controller.enqueue(
|
5389
|
+
this.encoder.encode((0, import_ui_utils8.formatStreamPart)("data", [value]))
|
5390
|
+
);
|
5548
5391
|
}
|
5549
|
-
|
5550
|
-
|
5551
|
-
|
5552
|
-
return readableFromAsyncIterable(streamable2(reader)).pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(createStreamDataTransformer());
|
5553
|
-
} else {
|
5554
|
-
return createParser2(reader).pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(createStreamDataTransformer());
|
5555
|
-
}
|
5556
|
-
}
|
5557
|
-
|
5558
|
-
// streams/google-generative-ai-stream.ts
|
5559
|
-
async function* streamable3(response) {
|
5560
|
-
var _a11, _b, _c;
|
5561
|
-
for await (const chunk of response.stream) {
|
5562
|
-
const parts = (_c = (_b = (_a11 = chunk.candidates) == null ? void 0 : _a11[0]) == null ? void 0 : _b.content) == null ? void 0 : _c.parts;
|
5563
|
-
if (parts === void 0) {
|
5564
|
-
continue;
|
5392
|
+
appendMessageAnnotation(value) {
|
5393
|
+
if (this.isClosed) {
|
5394
|
+
throw new Error("Data Stream has already been closed.");
|
5565
5395
|
}
|
5566
|
-
|
5567
|
-
|
5568
|
-
yield firstPart.text;
|
5396
|
+
if (!this.controller) {
|
5397
|
+
throw new Error("Stream controller is not initialized.");
|
5569
5398
|
}
|
5399
|
+
this.controller.enqueue(
|
5400
|
+
this.encoder.encode((0, import_ui_utils8.formatStreamPart)("message_annotations", [value]))
|
5401
|
+
);
|
5570
5402
|
}
|
5571
|
-
}
|
5572
|
-
function
|
5573
|
-
|
5574
|
-
|
5575
|
-
|
5576
|
-
|
5577
|
-
|
5578
|
-
|
5579
|
-
return new ReadableStream({
|
5580
|
-
async pull(controller) {
|
5581
|
-
var _a11, _b;
|
5582
|
-
const { value, done } = await res.next();
|
5583
|
-
if (done) {
|
5584
|
-
controller.close();
|
5585
|
-
return;
|
5586
|
-
}
|
5587
|
-
const text = trimStartOfStream((_b = (_a11 = value.token) == null ? void 0 : _a11.text) != null ? _b : "");
|
5588
|
-
if (!text)
|
5589
|
-
return;
|
5590
|
-
if (value.generated_text != null && value.generated_text.length > 0) {
|
5591
|
-
return;
|
5592
|
-
}
|
5593
|
-
if (text === "</s>" || text === "<|endoftext|>" || text === "<|end|>") {
|
5594
|
-
return;
|
5595
|
-
}
|
5596
|
-
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)));
|
5597
5411
|
}
|
5598
5412
|
});
|
5599
5413
|
}
|
5600
|
-
|
5601
|
-
|
5602
|
-
}
|
5603
|
-
|
5604
|
-
// streams/inkeep-stream.ts
|
5605
|
-
function InkeepStream(res, callbacks) {
|
5606
|
-
if (!res.body) {
|
5607
|
-
throw new Error("Response body is null");
|
5608
|
-
}
|
5609
|
-
let chat_session_id = "";
|
5610
|
-
let records_cited;
|
5611
|
-
const inkeepEventParser = (data, options) => {
|
5612
|
-
var _a11, _b;
|
5613
|
-
const { event } = options;
|
5614
|
-
if (event === "records_cited") {
|
5615
|
-
records_cited = JSON.parse(data);
|
5616
|
-
(_a11 = callbacks == null ? void 0 : callbacks.onRecordsCited) == null ? void 0 : _a11.call(callbacks, records_cited);
|
5617
|
-
}
|
5618
|
-
if (event === "message_chunk") {
|
5619
|
-
const inkeepMessageChunk = JSON.parse(data);
|
5620
|
-
chat_session_id = (_b = inkeepMessageChunk.chat_session_id) != null ? _b : chat_session_id;
|
5621
|
-
return inkeepMessageChunk.content_chunk;
|
5622
|
-
}
|
5623
|
-
return;
|
5624
|
-
};
|
5625
|
-
let { onRecordsCited, ...passThroughCallbacks } = callbacks || {};
|
5626
|
-
passThroughCallbacks = {
|
5627
|
-
...passThroughCallbacks,
|
5628
|
-
onFinal: (completion) => {
|
5629
|
-
var _a11;
|
5630
|
-
const inkeepOnFinalMetadata = {
|
5631
|
-
chat_session_id,
|
5632
|
-
records_cited
|
5633
|
-
};
|
5634
|
-
(_a11 = callbacks == null ? void 0 : callbacks.onFinal) == null ? void 0 : _a11.call(callbacks, completion, inkeepOnFinalMetadata);
|
5635
|
-
}
|
5636
|
-
};
|
5637
|
-
return AIStream(res, inkeepEventParser, passThroughCallbacks).pipeThrough(
|
5638
|
-
createStreamDataTransformer()
|
5639
|
-
);
|
5640
|
-
}
|
5414
|
+
var experimental_StreamData = class extends StreamData2 {
|
5415
|
+
};
|
5641
5416
|
|
5642
5417
|
// streams/langchain-adapter.ts
|
5643
|
-
var langchain_adapter_exports = {};
|
5644
|
-
__export(langchain_adapter_exports, {
|
5645
|
-
toAIStream: () => toAIStream,
|
5646
|
-
toDataStream: () => toDataStream,
|
5647
|
-
toDataStreamResponse: () => toDataStreamResponse
|
5648
|
-
});
|
5649
5418
|
function toAIStream(stream, callbacks) {
|
5650
5419
|
return toDataStream(stream, callbacks);
|
5651
5420
|
}
|
@@ -5742,422 +5511,6 @@ function toReadableStream(res) {
|
|
5742
5511
|
});
|
5743
5512
|
}
|
5744
5513
|
|
5745
|
-
// streams/langchain-stream.ts
|
5746
|
-
function LangChainStream(callbacks) {
|
5747
|
-
const stream = new TransformStream();
|
5748
|
-
const writer = stream.writable.getWriter();
|
5749
|
-
const runs = /* @__PURE__ */ new Set();
|
5750
|
-
const handleError = async (e, runId) => {
|
5751
|
-
runs.delete(runId);
|
5752
|
-
await writer.ready;
|
5753
|
-
await writer.abort(e);
|
5754
|
-
};
|
5755
|
-
const handleStart = async (runId) => {
|
5756
|
-
runs.add(runId);
|
5757
|
-
};
|
5758
|
-
const handleEnd = async (runId) => {
|
5759
|
-
runs.delete(runId);
|
5760
|
-
if (runs.size === 0) {
|
5761
|
-
await writer.ready;
|
5762
|
-
await writer.close();
|
5763
|
-
}
|
5764
|
-
};
|
5765
|
-
return {
|
5766
|
-
stream: stream.readable.pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(createStreamDataTransformer()),
|
5767
|
-
writer,
|
5768
|
-
handlers: {
|
5769
|
-
handleLLMNewToken: async (token) => {
|
5770
|
-
await writer.ready;
|
5771
|
-
await writer.write(token);
|
5772
|
-
},
|
5773
|
-
handleLLMStart: async (_llm, _prompts, runId) => {
|
5774
|
-
handleStart(runId);
|
5775
|
-
},
|
5776
|
-
handleLLMEnd: async (_output, runId) => {
|
5777
|
-
await handleEnd(runId);
|
5778
|
-
},
|
5779
|
-
handleLLMError: async (e, runId) => {
|
5780
|
-
await handleError(e, runId);
|
5781
|
-
},
|
5782
|
-
handleChainStart: async (_chain, _inputs, runId) => {
|
5783
|
-
handleStart(runId);
|
5784
|
-
},
|
5785
|
-
handleChainEnd: async (_outputs, runId) => {
|
5786
|
-
await handleEnd(runId);
|
5787
|
-
},
|
5788
|
-
handleChainError: async (e, runId) => {
|
5789
|
-
await handleError(e, runId);
|
5790
|
-
},
|
5791
|
-
handleToolStart: async (_tool, _input, runId) => {
|
5792
|
-
handleStart(runId);
|
5793
|
-
},
|
5794
|
-
handleToolEnd: async (_output, runId) => {
|
5795
|
-
await handleEnd(runId);
|
5796
|
-
},
|
5797
|
-
handleToolError: async (e, runId) => {
|
5798
|
-
await handleError(e, runId);
|
5799
|
-
}
|
5800
|
-
}
|
5801
|
-
};
|
5802
|
-
}
|
5803
|
-
|
5804
|
-
// streams/mistral-stream.ts
|
5805
|
-
async function* streamable4(stream) {
|
5806
|
-
var _a11, _b;
|
5807
|
-
for await (const chunk of stream) {
|
5808
|
-
const content = (_b = (_a11 = chunk.choices[0]) == null ? void 0 : _a11.delta) == null ? void 0 : _b.content;
|
5809
|
-
if (content === void 0 || content === "") {
|
5810
|
-
continue;
|
5811
|
-
}
|
5812
|
-
yield content;
|
5813
|
-
}
|
5814
|
-
}
|
5815
|
-
function MistralStream(response, callbacks) {
|
5816
|
-
const stream = readableFromAsyncIterable(streamable4(response));
|
5817
|
-
return stream.pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(createStreamDataTransformer());
|
5818
|
-
}
|
5819
|
-
|
5820
|
-
// streams/openai-stream.ts
|
5821
|
-
var import_ui_utils9 = require("@ai-sdk/ui-utils");
|
5822
|
-
function parseOpenAIStream() {
|
5823
|
-
const extract = chunkToText();
|
5824
|
-
return (data) => extract(JSON.parse(data));
|
5825
|
-
}
|
5826
|
-
async function* streamable5(stream) {
|
5827
|
-
const extract = chunkToText();
|
5828
|
-
for await (let chunk of stream) {
|
5829
|
-
if ("promptFilterResults" in chunk) {
|
5830
|
-
chunk = {
|
5831
|
-
id: chunk.id,
|
5832
|
-
created: chunk.created.getDate(),
|
5833
|
-
object: chunk.object,
|
5834
|
-
// not exposed by Azure API
|
5835
|
-
model: chunk.model,
|
5836
|
-
// not exposed by Azure API
|
5837
|
-
choices: chunk.choices.map((choice) => {
|
5838
|
-
var _a11, _b, _c, _d, _e, _f, _g;
|
5839
|
-
return {
|
5840
|
-
delta: {
|
5841
|
-
content: (_a11 = choice.delta) == null ? void 0 : _a11.content,
|
5842
|
-
function_call: (_b = choice.delta) == null ? void 0 : _b.functionCall,
|
5843
|
-
role: (_c = choice.delta) == null ? void 0 : _c.role,
|
5844
|
-
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) => ({
|
5845
|
-
index,
|
5846
|
-
id: toolCall.id,
|
5847
|
-
function: toolCall.function,
|
5848
|
-
type: toolCall.type
|
5849
|
-
})) : void 0
|
5850
|
-
},
|
5851
|
-
finish_reason: choice.finishReason,
|
5852
|
-
index: choice.index
|
5853
|
-
};
|
5854
|
-
})
|
5855
|
-
};
|
5856
|
-
}
|
5857
|
-
const text = extract(chunk);
|
5858
|
-
if (text)
|
5859
|
-
yield text;
|
5860
|
-
}
|
5861
|
-
}
|
5862
|
-
function chunkToText() {
|
5863
|
-
const trimStartOfStream = trimStartOfStreamHelper();
|
5864
|
-
let isFunctionStreamingIn;
|
5865
|
-
return (json) => {
|
5866
|
-
var _a11, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r;
|
5867
|
-
if (isChatCompletionChunk(json)) {
|
5868
|
-
const delta = (_a11 = json.choices[0]) == null ? void 0 : _a11.delta;
|
5869
|
-
if ((_b = delta.function_call) == null ? void 0 : _b.name) {
|
5870
|
-
isFunctionStreamingIn = true;
|
5871
|
-
return {
|
5872
|
-
isText: false,
|
5873
|
-
content: `{"function_call": {"name": "${delta.function_call.name}", "arguments": "`
|
5874
|
-
};
|
5875
|
-
} else if ((_e = (_d = (_c = delta.tool_calls) == null ? void 0 : _c[0]) == null ? void 0 : _d.function) == null ? void 0 : _e.name) {
|
5876
|
-
isFunctionStreamingIn = true;
|
5877
|
-
const toolCall = delta.tool_calls[0];
|
5878
|
-
if (toolCall.index === 0) {
|
5879
|
-
return {
|
5880
|
-
isText: false,
|
5881
|
-
content: `{"tool_calls":[ {"id": "${toolCall.id}", "type": "function", "function": {"name": "${(_f = toolCall.function) == null ? void 0 : _f.name}", "arguments": "`
|
5882
|
-
};
|
5883
|
-
} else {
|
5884
|
-
return {
|
5885
|
-
isText: false,
|
5886
|
-
content: `"}}, {"id": "${toolCall.id}", "type": "function", "function": {"name": "${(_g = toolCall.function) == null ? void 0 : _g.name}", "arguments": "`
|
5887
|
-
};
|
5888
|
-
}
|
5889
|
-
} else if ((_h = delta.function_call) == null ? void 0 : _h.arguments) {
|
5890
|
-
return {
|
5891
|
-
isText: false,
|
5892
|
-
content: cleanupArguments((_i = delta.function_call) == null ? void 0 : _i.arguments)
|
5893
|
-
};
|
5894
|
-
} else if ((_l = (_k = (_j = delta.tool_calls) == null ? void 0 : _j[0]) == null ? void 0 : _k.function) == null ? void 0 : _l.arguments) {
|
5895
|
-
return {
|
5896
|
-
isText: false,
|
5897
|
-
content: cleanupArguments((_o = (_n = (_m = delta.tool_calls) == null ? void 0 : _m[0]) == null ? void 0 : _n.function) == null ? void 0 : _o.arguments)
|
5898
|
-
};
|
5899
|
-
} 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")) {
|
5900
|
-
isFunctionStreamingIn = false;
|
5901
|
-
return {
|
5902
|
-
isText: false,
|
5903
|
-
content: '"}}'
|
5904
|
-
};
|
5905
|
-
} else if (isFunctionStreamingIn && ((_r = json.choices[0]) == null ? void 0 : _r.finish_reason) === "tool_calls") {
|
5906
|
-
isFunctionStreamingIn = false;
|
5907
|
-
return {
|
5908
|
-
isText: false,
|
5909
|
-
content: '"}}]}'
|
5910
|
-
};
|
5911
|
-
}
|
5912
|
-
}
|
5913
|
-
const text = trimStartOfStream(
|
5914
|
-
isChatCompletionChunk(json) && json.choices[0].delta.content ? json.choices[0].delta.content : isCompletion(json) ? json.choices[0].text : ""
|
5915
|
-
);
|
5916
|
-
return text;
|
5917
|
-
};
|
5918
|
-
function cleanupArguments(argumentChunk) {
|
5919
|
-
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");
|
5920
|
-
return `${escapedPartialJson}`;
|
5921
|
-
}
|
5922
|
-
}
|
5923
|
-
var __internal__OpenAIFnMessagesSymbol = Symbol(
|
5924
|
-
"internal_openai_fn_messages"
|
5925
|
-
);
|
5926
|
-
function isChatCompletionChunk(data) {
|
5927
|
-
return "choices" in data && data.choices && data.choices[0] && "delta" in data.choices[0];
|
5928
|
-
}
|
5929
|
-
function isCompletion(data) {
|
5930
|
-
return "choices" in data && data.choices && data.choices[0] && "text" in data.choices[0];
|
5931
|
-
}
|
5932
|
-
function OpenAIStream(res, callbacks) {
|
5933
|
-
const cb = callbacks;
|
5934
|
-
let stream;
|
5935
|
-
if (Symbol.asyncIterator in res) {
|
5936
|
-
stream = readableFromAsyncIterable(streamable5(res)).pipeThrough(
|
5937
|
-
createCallbacksTransformer(
|
5938
|
-
(cb == null ? void 0 : cb.experimental_onFunctionCall) || (cb == null ? void 0 : cb.experimental_onToolCall) ? {
|
5939
|
-
...cb,
|
5940
|
-
onFinal: void 0
|
5941
|
-
} : {
|
5942
|
-
...cb
|
5943
|
-
}
|
5944
|
-
)
|
5945
|
-
);
|
5946
|
-
} else {
|
5947
|
-
stream = AIStream(
|
5948
|
-
res,
|
5949
|
-
parseOpenAIStream(),
|
5950
|
-
(cb == null ? void 0 : cb.experimental_onFunctionCall) || (cb == null ? void 0 : cb.experimental_onToolCall) ? {
|
5951
|
-
...cb,
|
5952
|
-
onFinal: void 0
|
5953
|
-
} : {
|
5954
|
-
...cb
|
5955
|
-
}
|
5956
|
-
);
|
5957
|
-
}
|
5958
|
-
if (cb && (cb.experimental_onFunctionCall || cb.experimental_onToolCall)) {
|
5959
|
-
const functionCallTransformer = createFunctionCallTransformer(cb);
|
5960
|
-
return stream.pipeThrough(functionCallTransformer);
|
5961
|
-
} else {
|
5962
|
-
return stream.pipeThrough(createStreamDataTransformer());
|
5963
|
-
}
|
5964
|
-
}
|
5965
|
-
function createFunctionCallTransformer(callbacks) {
|
5966
|
-
const textEncoder = new TextEncoder();
|
5967
|
-
let isFirstChunk = true;
|
5968
|
-
let aggregatedResponse = "";
|
5969
|
-
let aggregatedFinalCompletionResponse = "";
|
5970
|
-
let isFunctionStreamingIn = false;
|
5971
|
-
let functionCallMessages = callbacks[__internal__OpenAIFnMessagesSymbol] || [];
|
5972
|
-
const decode = (0, import_ui_utils9.createChunkDecoder)();
|
5973
|
-
return new TransformStream({
|
5974
|
-
async transform(chunk, controller) {
|
5975
|
-
const message = decode(chunk);
|
5976
|
-
aggregatedFinalCompletionResponse += message;
|
5977
|
-
const shouldHandleAsFunction = isFirstChunk && (message.startsWith('{"function_call":') || message.startsWith('{"tool_calls":'));
|
5978
|
-
if (shouldHandleAsFunction) {
|
5979
|
-
isFunctionStreamingIn = true;
|
5980
|
-
aggregatedResponse += message;
|
5981
|
-
isFirstChunk = false;
|
5982
|
-
return;
|
5983
|
-
}
|
5984
|
-
if (!isFunctionStreamingIn) {
|
5985
|
-
controller.enqueue(
|
5986
|
-
textEncoder.encode((0, import_ui_utils9.formatStreamPart)("text", message))
|
5987
|
-
);
|
5988
|
-
return;
|
5989
|
-
} else {
|
5990
|
-
aggregatedResponse += message;
|
5991
|
-
}
|
5992
|
-
},
|
5993
|
-
async flush(controller) {
|
5994
|
-
try {
|
5995
|
-
if (!isFirstChunk && isFunctionStreamingIn && (callbacks.experimental_onFunctionCall || callbacks.experimental_onToolCall)) {
|
5996
|
-
isFunctionStreamingIn = false;
|
5997
|
-
const payload = JSON.parse(aggregatedResponse);
|
5998
|
-
let newFunctionCallMessages = [
|
5999
|
-
...functionCallMessages
|
6000
|
-
];
|
6001
|
-
let functionResponse = void 0;
|
6002
|
-
if (callbacks.experimental_onFunctionCall) {
|
6003
|
-
if (payload.function_call === void 0) {
|
6004
|
-
console.warn(
|
6005
|
-
"experimental_onFunctionCall should not be defined when using tools"
|
6006
|
-
);
|
6007
|
-
}
|
6008
|
-
const argumentsPayload = JSON.parse(
|
6009
|
-
payload.function_call.arguments
|
6010
|
-
);
|
6011
|
-
functionResponse = await callbacks.experimental_onFunctionCall(
|
6012
|
-
{
|
6013
|
-
name: payload.function_call.name,
|
6014
|
-
arguments: argumentsPayload
|
6015
|
-
},
|
6016
|
-
(result) => {
|
6017
|
-
newFunctionCallMessages = [
|
6018
|
-
...functionCallMessages,
|
6019
|
-
{
|
6020
|
-
role: "assistant",
|
6021
|
-
content: "",
|
6022
|
-
function_call: payload.function_call
|
6023
|
-
},
|
6024
|
-
{
|
6025
|
-
role: "function",
|
6026
|
-
name: payload.function_call.name,
|
6027
|
-
content: JSON.stringify(result)
|
6028
|
-
}
|
6029
|
-
];
|
6030
|
-
return newFunctionCallMessages;
|
6031
|
-
}
|
6032
|
-
);
|
6033
|
-
}
|
6034
|
-
if (callbacks.experimental_onToolCall) {
|
6035
|
-
const toolCalls = {
|
6036
|
-
tools: []
|
6037
|
-
};
|
6038
|
-
for (const tool2 of payload.tool_calls) {
|
6039
|
-
toolCalls.tools.push({
|
6040
|
-
id: tool2.id,
|
6041
|
-
type: "function",
|
6042
|
-
func: {
|
6043
|
-
name: tool2.function.name,
|
6044
|
-
arguments: JSON.parse(tool2.function.arguments)
|
6045
|
-
}
|
6046
|
-
});
|
6047
|
-
}
|
6048
|
-
let responseIndex = 0;
|
6049
|
-
try {
|
6050
|
-
functionResponse = await callbacks.experimental_onToolCall(
|
6051
|
-
toolCalls,
|
6052
|
-
(result) => {
|
6053
|
-
if (result) {
|
6054
|
-
const { tool_call_id, function_name, tool_call_result } = result;
|
6055
|
-
newFunctionCallMessages = [
|
6056
|
-
...newFunctionCallMessages,
|
6057
|
-
// Only append the assistant message if it's the first response
|
6058
|
-
...responseIndex === 0 ? [
|
6059
|
-
{
|
6060
|
-
role: "assistant",
|
6061
|
-
content: "",
|
6062
|
-
tool_calls: payload.tool_calls.map(
|
6063
|
-
(tc) => ({
|
6064
|
-
id: tc.id,
|
6065
|
-
type: "function",
|
6066
|
-
function: {
|
6067
|
-
name: tc.function.name,
|
6068
|
-
// we send the arguments an object to the user, but as the API expects a string, we need to stringify it
|
6069
|
-
arguments: JSON.stringify(
|
6070
|
-
tc.function.arguments
|
6071
|
-
)
|
6072
|
-
}
|
6073
|
-
})
|
6074
|
-
)
|
6075
|
-
}
|
6076
|
-
] : [],
|
6077
|
-
// Append the function call result message
|
6078
|
-
{
|
6079
|
-
role: "tool",
|
6080
|
-
tool_call_id,
|
6081
|
-
name: function_name,
|
6082
|
-
content: JSON.stringify(tool_call_result)
|
6083
|
-
}
|
6084
|
-
];
|
6085
|
-
responseIndex++;
|
6086
|
-
}
|
6087
|
-
return newFunctionCallMessages;
|
6088
|
-
}
|
6089
|
-
);
|
6090
|
-
} catch (e) {
|
6091
|
-
console.error("Error calling experimental_onToolCall:", e);
|
6092
|
-
}
|
6093
|
-
}
|
6094
|
-
if (!functionResponse) {
|
6095
|
-
controller.enqueue(
|
6096
|
-
textEncoder.encode(
|
6097
|
-
(0, import_ui_utils9.formatStreamPart)(
|
6098
|
-
payload.function_call ? "function_call" : "tool_calls",
|
6099
|
-
// parse to prevent double-encoding:
|
6100
|
-
JSON.parse(aggregatedResponse)
|
6101
|
-
)
|
6102
|
-
)
|
6103
|
-
);
|
6104
|
-
return;
|
6105
|
-
} else if (typeof functionResponse === "string") {
|
6106
|
-
controller.enqueue(
|
6107
|
-
textEncoder.encode((0, import_ui_utils9.formatStreamPart)("text", functionResponse))
|
6108
|
-
);
|
6109
|
-
aggregatedFinalCompletionResponse = functionResponse;
|
6110
|
-
return;
|
6111
|
-
}
|
6112
|
-
const filteredCallbacks = {
|
6113
|
-
...callbacks,
|
6114
|
-
onStart: void 0
|
6115
|
-
};
|
6116
|
-
callbacks.onFinal = void 0;
|
6117
|
-
const openAIStream = OpenAIStream(functionResponse, {
|
6118
|
-
...filteredCallbacks,
|
6119
|
-
[__internal__OpenAIFnMessagesSymbol]: newFunctionCallMessages
|
6120
|
-
});
|
6121
|
-
const reader = openAIStream.getReader();
|
6122
|
-
while (true) {
|
6123
|
-
const { done, value } = await reader.read();
|
6124
|
-
if (done) {
|
6125
|
-
break;
|
6126
|
-
}
|
6127
|
-
controller.enqueue(value);
|
6128
|
-
}
|
6129
|
-
}
|
6130
|
-
} finally {
|
6131
|
-
if (callbacks.onFinal && aggregatedFinalCompletionResponse) {
|
6132
|
-
await callbacks.onFinal(aggregatedFinalCompletionResponse);
|
6133
|
-
}
|
6134
|
-
}
|
6135
|
-
}
|
6136
|
-
});
|
6137
|
-
}
|
6138
|
-
|
6139
|
-
// streams/replicate-stream.ts
|
6140
|
-
async function ReplicateStream(res, cb, options) {
|
6141
|
-
var _a11;
|
6142
|
-
const url = (_a11 = res.urls) == null ? void 0 : _a11.stream;
|
6143
|
-
if (!url) {
|
6144
|
-
if (res.error)
|
6145
|
-
throw new Error(res.error);
|
6146
|
-
else
|
6147
|
-
throw new Error("Missing stream URL in Replicate response");
|
6148
|
-
}
|
6149
|
-
const eventStream = await fetch(url, {
|
6150
|
-
method: "GET",
|
6151
|
-
headers: {
|
6152
|
-
Accept: "text/event-stream",
|
6153
|
-
...options == null ? void 0 : options.headers
|
6154
|
-
}
|
6155
|
-
});
|
6156
|
-
return AIStream(eventStream, void 0, cb).pipeThrough(
|
6157
|
-
createStreamDataTransformer()
|
6158
|
-
);
|
6159
|
-
}
|
6160
|
-
|
6161
5514
|
// streams/stream-to-response.ts
|
6162
5515
|
function streamToResponse(res, response, init, data) {
|
6163
5516
|
var _a11;
|
@@ -6202,25 +5555,14 @@ var StreamingTextResponse = class extends Response {
|
|
6202
5555
|
|
6203
5556
|
// streams/index.ts
|
6204
5557
|
var generateId2 = import_provider_utils10.generateId;
|
6205
|
-
var nanoid = import_provider_utils10.generateId;
|
6206
5558
|
// Annotate the CommonJS export names for ESM import in node:
|
6207
5559
|
0 && (module.exports = {
|
6208
5560
|
AISDKError,
|
6209
5561
|
AIStream,
|
6210
5562
|
APICallError,
|
6211
|
-
AWSBedrockAnthropicMessagesStream,
|
6212
|
-
AWSBedrockAnthropicStream,
|
6213
|
-
AWSBedrockCohereStream,
|
6214
|
-
AWSBedrockLlama2Stream,
|
6215
|
-
AWSBedrockStream,
|
6216
|
-
AnthropicStream,
|
6217
5563
|
AssistantResponse,
|
6218
|
-
CohereStream,
|
6219
5564
|
DownloadError,
|
6220
5565
|
EmptyResponseBodyError,
|
6221
|
-
GoogleGenerativeAIStream,
|
6222
|
-
HuggingFaceStream,
|
6223
|
-
InkeepStream,
|
6224
5566
|
InvalidArgumentError,
|
6225
5567
|
InvalidDataContentError,
|
6226
5568
|
InvalidMessageRoleError,
|
@@ -6229,18 +5571,14 @@ var nanoid = import_provider_utils10.generateId;
|
|
6229
5571
|
InvalidToolArgumentsError,
|
6230
5572
|
JSONParseError,
|
6231
5573
|
LangChainAdapter,
|
6232
|
-
LangChainStream,
|
6233
5574
|
LlamaIndexAdapter,
|
6234
5575
|
LoadAPIKeyError,
|
6235
5576
|
MessageConversionError,
|
6236
|
-
MistralStream,
|
6237
5577
|
NoContentGeneratedError,
|
6238
5578
|
NoObjectGeneratedError,
|
6239
5579
|
NoSuchModelError,
|
6240
5580
|
NoSuchProviderError,
|
6241
5581
|
NoSuchToolError,
|
6242
|
-
OpenAIStream,
|
6243
|
-
ReplicateStream,
|
6244
5582
|
RetryError,
|
6245
5583
|
StreamData,
|
6246
5584
|
StreamingTextResponse,
|
@@ -6268,7 +5606,6 @@ var nanoid = import_provider_utils10.generateId;
|
|
6268
5606
|
generateObject,
|
6269
5607
|
generateText,
|
6270
5608
|
jsonSchema,
|
6271
|
-
nanoid,
|
6272
5609
|
parseStreamPart,
|
6273
5610
|
processDataProtocolResponse,
|
6274
5611
|
readDataStream,
|