ai 4.0.8 → 4.0.9
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 +6 -0
- package/dist/index.d.mts +66 -16
- package/dist/index.d.ts +66 -16
- package/dist/index.js +334 -191
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +302 -161
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
@@ -7,7 +7,7 @@ var __export = (target, all) => {
|
|
7
7
|
// streams/index.ts
|
8
8
|
import {
|
9
9
|
formatAssistantStreamPart as formatAssistantStreamPart2,
|
10
|
-
formatDataStreamPart as
|
10
|
+
formatDataStreamPart as formatDataStreamPart6,
|
11
11
|
parseAssistantStreamPart,
|
12
12
|
parseDataStreamPart,
|
13
13
|
processDataStream,
|
@@ -18,6 +18,172 @@ import { generateId as generateId2 } from "@ai-sdk/provider-utils";
|
|
18
18
|
// core/index.ts
|
19
19
|
import { jsonSchema } from "@ai-sdk/ui-utils";
|
20
20
|
|
21
|
+
// core/data-stream/create-data-stream.ts
|
22
|
+
import { formatDataStreamPart } from "@ai-sdk/ui-utils";
|
23
|
+
function createDataStream({
|
24
|
+
execute,
|
25
|
+
onError = () => "An error occurred."
|
26
|
+
// mask error messages for safety by default
|
27
|
+
}) {
|
28
|
+
let controller;
|
29
|
+
const ongoingStreamPromises = [];
|
30
|
+
const stream = new ReadableStream({
|
31
|
+
start(controllerArg) {
|
32
|
+
controller = controllerArg;
|
33
|
+
}
|
34
|
+
});
|
35
|
+
try {
|
36
|
+
const result = execute({
|
37
|
+
writeData(data) {
|
38
|
+
controller.enqueue(formatDataStreamPart("data", [data]));
|
39
|
+
},
|
40
|
+
writeMessageAnnotation(annotation) {
|
41
|
+
controller.enqueue(
|
42
|
+
formatDataStreamPart("message_annotations", [annotation])
|
43
|
+
);
|
44
|
+
},
|
45
|
+
merge(streamArg) {
|
46
|
+
ongoingStreamPromises.push(
|
47
|
+
(async () => {
|
48
|
+
const reader = streamArg.getReader();
|
49
|
+
while (true) {
|
50
|
+
const { done, value } = await reader.read();
|
51
|
+
if (done)
|
52
|
+
break;
|
53
|
+
controller.enqueue(value);
|
54
|
+
}
|
55
|
+
})().catch((error) => {
|
56
|
+
controller.enqueue(formatDataStreamPart("error", onError(error)));
|
57
|
+
})
|
58
|
+
);
|
59
|
+
},
|
60
|
+
onError
|
61
|
+
});
|
62
|
+
if (result) {
|
63
|
+
result.catch((error) => {
|
64
|
+
controller.enqueue(formatDataStreamPart("error", onError(error)));
|
65
|
+
});
|
66
|
+
}
|
67
|
+
} catch (error) {
|
68
|
+
controller.enqueue(formatDataStreamPart("error", onError(error)));
|
69
|
+
}
|
70
|
+
const waitForStreams = new Promise(async (resolve) => {
|
71
|
+
while (ongoingStreamPromises.length > 0) {
|
72
|
+
await ongoingStreamPromises.shift();
|
73
|
+
}
|
74
|
+
resolve();
|
75
|
+
});
|
76
|
+
waitForStreams.finally(() => {
|
77
|
+
controller.close();
|
78
|
+
});
|
79
|
+
return stream;
|
80
|
+
}
|
81
|
+
|
82
|
+
// core/util/prepare-response-headers.ts
|
83
|
+
function prepareResponseHeaders(headers, {
|
84
|
+
contentType,
|
85
|
+
dataStreamVersion
|
86
|
+
}) {
|
87
|
+
const responseHeaders = new Headers(headers != null ? headers : {});
|
88
|
+
if (!responseHeaders.has("Content-Type")) {
|
89
|
+
responseHeaders.set("Content-Type", contentType);
|
90
|
+
}
|
91
|
+
if (dataStreamVersion !== void 0) {
|
92
|
+
responseHeaders.set("X-Vercel-AI-Data-Stream", dataStreamVersion);
|
93
|
+
}
|
94
|
+
return responseHeaders;
|
95
|
+
}
|
96
|
+
|
97
|
+
// core/data-stream/create-data-stream-response.ts
|
98
|
+
function createDataStreamResponse({
|
99
|
+
status,
|
100
|
+
statusText,
|
101
|
+
headers,
|
102
|
+
execute,
|
103
|
+
onError
|
104
|
+
}) {
|
105
|
+
return new Response(
|
106
|
+
createDataStream({ execute, onError }).pipeThrough(new TextEncoderStream()),
|
107
|
+
{
|
108
|
+
status,
|
109
|
+
statusText,
|
110
|
+
headers: prepareResponseHeaders(headers, {
|
111
|
+
contentType: "text/plain; charset=utf-8",
|
112
|
+
dataStreamVersion: "v1"
|
113
|
+
})
|
114
|
+
}
|
115
|
+
);
|
116
|
+
}
|
117
|
+
|
118
|
+
// core/util/prepare-outgoing-http-headers.ts
|
119
|
+
function prepareOutgoingHttpHeaders(headers, {
|
120
|
+
contentType,
|
121
|
+
dataStreamVersion
|
122
|
+
}) {
|
123
|
+
const outgoingHeaders = {};
|
124
|
+
if (headers != null) {
|
125
|
+
for (const [key, value] of Object.entries(headers)) {
|
126
|
+
outgoingHeaders[key] = value;
|
127
|
+
}
|
128
|
+
}
|
129
|
+
if (outgoingHeaders["Content-Type"] == null) {
|
130
|
+
outgoingHeaders["Content-Type"] = contentType;
|
131
|
+
}
|
132
|
+
if (dataStreamVersion !== void 0) {
|
133
|
+
outgoingHeaders["X-Vercel-AI-Data-Stream"] = dataStreamVersion;
|
134
|
+
}
|
135
|
+
return outgoingHeaders;
|
136
|
+
}
|
137
|
+
|
138
|
+
// core/util/write-to-server-response.ts
|
139
|
+
function writeToServerResponse({
|
140
|
+
response,
|
141
|
+
status,
|
142
|
+
statusText,
|
143
|
+
headers,
|
144
|
+
stream
|
145
|
+
}) {
|
146
|
+
response.writeHead(status != null ? status : 200, statusText, headers);
|
147
|
+
const reader = stream.getReader();
|
148
|
+
const read = async () => {
|
149
|
+
try {
|
150
|
+
while (true) {
|
151
|
+
const { done, value } = await reader.read();
|
152
|
+
if (done)
|
153
|
+
break;
|
154
|
+
response.write(value);
|
155
|
+
}
|
156
|
+
} catch (error) {
|
157
|
+
throw error;
|
158
|
+
} finally {
|
159
|
+
response.end();
|
160
|
+
}
|
161
|
+
};
|
162
|
+
read();
|
163
|
+
}
|
164
|
+
|
165
|
+
// core/data-stream/pipe-data-stream-to-response.ts
|
166
|
+
function pipeDataStreamToResponse(response, {
|
167
|
+
status,
|
168
|
+
statusText,
|
169
|
+
headers,
|
170
|
+
execute,
|
171
|
+
onError
|
172
|
+
}) {
|
173
|
+
writeToServerResponse({
|
174
|
+
response,
|
175
|
+
status,
|
176
|
+
statusText,
|
177
|
+
headers: prepareOutgoingHttpHeaders(headers, {
|
178
|
+
contentType: "text/plain; charset=utf-8",
|
179
|
+
dataStreamVersion: "v1"
|
180
|
+
}),
|
181
|
+
stream: createDataStream({ execute, onError }).pipeThrough(
|
182
|
+
new TextEncoderStream()
|
183
|
+
)
|
184
|
+
});
|
185
|
+
}
|
186
|
+
|
21
187
|
// errors/invalid-argument-error.ts
|
22
188
|
import { AISDKError } from "@ai-sdk/provider";
|
23
189
|
var name = "AI_InvalidArgumentError";
|
@@ -1512,21 +1678,6 @@ function calculateLanguageModelUsage({
|
|
1512
1678
|
};
|
1513
1679
|
}
|
1514
1680
|
|
1515
|
-
// core/util/prepare-response-headers.ts
|
1516
|
-
function prepareResponseHeaders(headers, {
|
1517
|
-
contentType,
|
1518
|
-
dataStreamVersion
|
1519
|
-
}) {
|
1520
|
-
const responseHeaders = new Headers(headers != null ? headers : {});
|
1521
|
-
if (!responseHeaders.has("Content-Type")) {
|
1522
|
-
responseHeaders.set("Content-Type", contentType);
|
1523
|
-
}
|
1524
|
-
if (dataStreamVersion !== void 0) {
|
1525
|
-
responseHeaders.set("X-Vercel-AI-Data-Stream", dataStreamVersion);
|
1526
|
-
}
|
1527
|
-
return responseHeaders;
|
1528
|
-
}
|
1529
|
-
|
1530
1681
|
// core/generate-object/inject-json-instruction.ts
|
1531
1682
|
var DEFAULT_SCHEMA_PREFIX = "JSON schema:";
|
1532
1683
|
var DEFAULT_SCHEMA_SUFFIX = "You MUST answer with a JSON object that matches the JSON schema above.";
|
@@ -2430,53 +2581,6 @@ function now() {
|
|
2430
2581
|
return (_b = (_a11 = globalThis == null ? void 0 : globalThis.performance) == null ? void 0 : _a11.now()) != null ? _b : Date.now();
|
2431
2582
|
}
|
2432
2583
|
|
2433
|
-
// core/util/prepare-outgoing-http-headers.ts
|
2434
|
-
function prepareOutgoingHttpHeaders(headers, {
|
2435
|
-
contentType,
|
2436
|
-
dataStreamVersion
|
2437
|
-
}) {
|
2438
|
-
const outgoingHeaders = {};
|
2439
|
-
if (headers != null) {
|
2440
|
-
for (const [key, value] of Object.entries(headers)) {
|
2441
|
-
outgoingHeaders[key] = value;
|
2442
|
-
}
|
2443
|
-
}
|
2444
|
-
if (outgoingHeaders["Content-Type"] == null) {
|
2445
|
-
outgoingHeaders["Content-Type"] = contentType;
|
2446
|
-
}
|
2447
|
-
if (dataStreamVersion !== void 0) {
|
2448
|
-
outgoingHeaders["X-Vercel-AI-Data-Stream"] = dataStreamVersion;
|
2449
|
-
}
|
2450
|
-
return outgoingHeaders;
|
2451
|
-
}
|
2452
|
-
|
2453
|
-
// core/util/write-to-server-response.ts
|
2454
|
-
function writeToServerResponse({
|
2455
|
-
response,
|
2456
|
-
status,
|
2457
|
-
statusText,
|
2458
|
-
headers,
|
2459
|
-
stream
|
2460
|
-
}) {
|
2461
|
-
response.writeHead(status != null ? status : 200, statusText, headers);
|
2462
|
-
const reader = stream.getReader();
|
2463
|
-
const read = async () => {
|
2464
|
-
try {
|
2465
|
-
while (true) {
|
2466
|
-
const { done, value } = await reader.read();
|
2467
|
-
if (done)
|
2468
|
-
break;
|
2469
|
-
response.write(value);
|
2470
|
-
}
|
2471
|
-
} catch (error) {
|
2472
|
-
throw error;
|
2473
|
-
} finally {
|
2474
|
-
response.end();
|
2475
|
-
}
|
2476
|
-
};
|
2477
|
-
read();
|
2478
|
-
}
|
2479
|
-
|
2480
2584
|
// core/generate-object/stream-object.ts
|
2481
2585
|
var originalGenerateId2 = createIdGenerator2({ prefix: "aiobj", size: 24 });
|
2482
2586
|
function streamObject({
|
@@ -3605,7 +3709,7 @@ var DefaultGenerateTextResult = class {
|
|
3605
3709
|
|
3606
3710
|
// core/generate-text/stream-text.ts
|
3607
3711
|
import { createIdGenerator as createIdGenerator4 } from "@ai-sdk/provider-utils";
|
3608
|
-
import { formatDataStreamPart } from "@ai-sdk/ui-utils";
|
3712
|
+
import { formatDataStreamPart as formatDataStreamPart2 } from "@ai-sdk/ui-utils";
|
3609
3713
|
|
3610
3714
|
// core/util/merge-streams.ts
|
3611
3715
|
function mergeStreams(stream1, stream2) {
|
@@ -4544,12 +4648,12 @@ var DefaultStreamTextResult = class {
|
|
4544
4648
|
const chunkType = chunk.type;
|
4545
4649
|
switch (chunkType) {
|
4546
4650
|
case "text-delta": {
|
4547
|
-
controller.enqueue(
|
4651
|
+
controller.enqueue(formatDataStreamPart2("text", chunk.textDelta));
|
4548
4652
|
break;
|
4549
4653
|
}
|
4550
4654
|
case "tool-call-streaming-start": {
|
4551
4655
|
controller.enqueue(
|
4552
|
-
|
4656
|
+
formatDataStreamPart2("tool_call_streaming_start", {
|
4553
4657
|
toolCallId: chunk.toolCallId,
|
4554
4658
|
toolName: chunk.toolName
|
4555
4659
|
})
|
@@ -4558,7 +4662,7 @@ var DefaultStreamTextResult = class {
|
|
4558
4662
|
}
|
4559
4663
|
case "tool-call-delta": {
|
4560
4664
|
controller.enqueue(
|
4561
|
-
|
4665
|
+
formatDataStreamPart2("tool_call_delta", {
|
4562
4666
|
toolCallId: chunk.toolCallId,
|
4563
4667
|
argsTextDelta: chunk.argsTextDelta
|
4564
4668
|
})
|
@@ -4567,7 +4671,7 @@ var DefaultStreamTextResult = class {
|
|
4567
4671
|
}
|
4568
4672
|
case "tool-call": {
|
4569
4673
|
controller.enqueue(
|
4570
|
-
|
4674
|
+
formatDataStreamPart2("tool_call", {
|
4571
4675
|
toolCallId: chunk.toolCallId,
|
4572
4676
|
toolName: chunk.toolName,
|
4573
4677
|
args: chunk.args
|
@@ -4577,7 +4681,7 @@ var DefaultStreamTextResult = class {
|
|
4577
4681
|
}
|
4578
4682
|
case "tool-result": {
|
4579
4683
|
controller.enqueue(
|
4580
|
-
|
4684
|
+
formatDataStreamPart2("tool_result", {
|
4581
4685
|
toolCallId: chunk.toolCallId,
|
4582
4686
|
result: chunk.result
|
4583
4687
|
})
|
@@ -4586,13 +4690,13 @@ var DefaultStreamTextResult = class {
|
|
4586
4690
|
}
|
4587
4691
|
case "error": {
|
4588
4692
|
controller.enqueue(
|
4589
|
-
|
4693
|
+
formatDataStreamPart2("error", getErrorMessage3(chunk.error))
|
4590
4694
|
);
|
4591
4695
|
break;
|
4592
4696
|
}
|
4593
4697
|
case "step-finish": {
|
4594
4698
|
controller.enqueue(
|
4595
|
-
|
4699
|
+
formatDataStreamPart2("finish_step", {
|
4596
4700
|
finishReason: chunk.finishReason,
|
4597
4701
|
usage: sendUsage ? {
|
4598
4702
|
promptTokens: chunk.usage.promptTokens,
|
@@ -4605,7 +4709,7 @@ var DefaultStreamTextResult = class {
|
|
4605
4709
|
}
|
4606
4710
|
case "finish": {
|
4607
4711
|
controller.enqueue(
|
4608
|
-
|
4712
|
+
formatDataStreamPart2("finish_message", {
|
4609
4713
|
finishReason: chunk.finishReason,
|
4610
4714
|
usage: sendUsage ? {
|
4611
4715
|
promptTokens: chunk.usage.promptTokens,
|
@@ -4622,7 +4726,7 @@ var DefaultStreamTextResult = class {
|
|
4622
4726
|
}
|
4623
4727
|
}
|
4624
4728
|
});
|
4625
|
-
return this.fullStream.pipeThrough(callbackTransformer).pipeThrough(streamPartsTransformer)
|
4729
|
+
return this.fullStream.pipeThrough(callbackTransformer).pipeThrough(streamPartsTransformer);
|
4626
4730
|
}
|
4627
4731
|
pipeDataStreamToResponse(response, {
|
4628
4732
|
status,
|
@@ -4654,13 +4758,21 @@ var DefaultStreamTextResult = class {
|
|
4654
4758
|
stream: this.textStream.pipeThrough(new TextEncoderStream())
|
4655
4759
|
});
|
4656
4760
|
}
|
4761
|
+
// TODO breaking change 5.0: remove pipeThrough(new TextEncoderStream())
|
4657
4762
|
toDataStream(options) {
|
4658
4763
|
const stream = this.toDataStreamInternal({
|
4659
4764
|
getErrorMessage: options == null ? void 0 : options.getErrorMessage,
|
4660
4765
|
sendUsage: options == null ? void 0 : options.sendUsage
|
4661
|
-
});
|
4766
|
+
}).pipeThrough(new TextEncoderStream());
|
4662
4767
|
return (options == null ? void 0 : options.data) ? mergeStreams(options == null ? void 0 : options.data.stream, stream) : stream;
|
4663
4768
|
}
|
4769
|
+
mergeIntoDataStream(writer) {
|
4770
|
+
writer.merge(
|
4771
|
+
this.toDataStreamInternal({
|
4772
|
+
getErrorMessage: writer.onError
|
4773
|
+
})
|
4774
|
+
);
|
4775
|
+
}
|
4664
4776
|
toDataStreamResponse({
|
4665
4777
|
headers,
|
4666
4778
|
status,
|
@@ -5014,9 +5126,11 @@ function AssistantResponse({ threadId, messageId }, process2) {
|
|
5014
5126
|
// streams/langchain-adapter.ts
|
5015
5127
|
var langchain_adapter_exports = {};
|
5016
5128
|
__export(langchain_adapter_exports, {
|
5129
|
+
mergeIntoDataStream: () => mergeIntoDataStream,
|
5017
5130
|
toDataStream: () => toDataStream,
|
5018
5131
|
toDataStreamResponse: () => toDataStreamResponse
|
5019
5132
|
});
|
5133
|
+
import { formatDataStreamPart as formatDataStreamPart3 } from "@ai-sdk/ui-utils";
|
5020
5134
|
|
5021
5135
|
// streams/stream-callbacks.ts
|
5022
5136
|
function createCallbacksTransformer(callbacks = {}) {
|
@@ -5047,87 +5161,8 @@ function createCallbacksTransformer(callbacks = {}) {
|
|
5047
5161
|
});
|
5048
5162
|
}
|
5049
5163
|
|
5050
|
-
// streams/stream-data.ts
|
5051
|
-
import { formatDataStreamPart as formatDataStreamPart2 } from "@ai-sdk/ui-utils";
|
5052
|
-
|
5053
|
-
// util/constants.ts
|
5054
|
-
var HANGING_STREAM_WARNING_TIME_MS = 15 * 1e3;
|
5055
|
-
|
5056
|
-
// streams/stream-data.ts
|
5057
|
-
var StreamData = class {
|
5058
|
-
constructor() {
|
5059
|
-
this.encoder = new TextEncoder();
|
5060
|
-
this.controller = null;
|
5061
|
-
this.isClosed = false;
|
5062
|
-
this.warningTimeout = null;
|
5063
|
-
const self = this;
|
5064
|
-
this.stream = new ReadableStream({
|
5065
|
-
start: async (controller) => {
|
5066
|
-
self.controller = controller;
|
5067
|
-
if (process.env.NODE_ENV === "development") {
|
5068
|
-
self.warningTimeout = setTimeout(() => {
|
5069
|
-
console.warn(
|
5070
|
-
"The data stream is hanging. Did you forget to close it with `data.close()`?"
|
5071
|
-
);
|
5072
|
-
}, HANGING_STREAM_WARNING_TIME_MS);
|
5073
|
-
}
|
5074
|
-
},
|
5075
|
-
pull: (controller) => {
|
5076
|
-
},
|
5077
|
-
cancel: (reason) => {
|
5078
|
-
this.isClosed = true;
|
5079
|
-
}
|
5080
|
-
});
|
5081
|
-
}
|
5082
|
-
async close() {
|
5083
|
-
if (this.isClosed) {
|
5084
|
-
throw new Error("Data Stream has already been closed.");
|
5085
|
-
}
|
5086
|
-
if (!this.controller) {
|
5087
|
-
throw new Error("Stream controller is not initialized.");
|
5088
|
-
}
|
5089
|
-
this.controller.close();
|
5090
|
-
this.isClosed = true;
|
5091
|
-
if (this.warningTimeout) {
|
5092
|
-
clearTimeout(this.warningTimeout);
|
5093
|
-
}
|
5094
|
-
}
|
5095
|
-
append(value) {
|
5096
|
-
if (this.isClosed) {
|
5097
|
-
throw new Error("Data Stream has already been closed.");
|
5098
|
-
}
|
5099
|
-
if (!this.controller) {
|
5100
|
-
throw new Error("Stream controller is not initialized.");
|
5101
|
-
}
|
5102
|
-
this.controller.enqueue(
|
5103
|
-
this.encoder.encode(formatDataStreamPart2("data", [value]))
|
5104
|
-
);
|
5105
|
-
}
|
5106
|
-
appendMessageAnnotation(value) {
|
5107
|
-
if (this.isClosed) {
|
5108
|
-
throw new Error("Data Stream has already been closed.");
|
5109
|
-
}
|
5110
|
-
if (!this.controller) {
|
5111
|
-
throw new Error("Stream controller is not initialized.");
|
5112
|
-
}
|
5113
|
-
this.controller.enqueue(
|
5114
|
-
this.encoder.encode(formatDataStreamPart2("message_annotations", [value]))
|
5115
|
-
);
|
5116
|
-
}
|
5117
|
-
};
|
5118
|
-
function createStreamDataTransformer() {
|
5119
|
-
const encoder = new TextEncoder();
|
5120
|
-
const decoder = new TextDecoder();
|
5121
|
-
return new TransformStream({
|
5122
|
-
transform: async (chunk, controller) => {
|
5123
|
-
const message = decoder.decode(chunk);
|
5124
|
-
controller.enqueue(encoder.encode(formatDataStreamPart2("text", message)));
|
5125
|
-
}
|
5126
|
-
});
|
5127
|
-
}
|
5128
|
-
|
5129
5164
|
// streams/langchain-adapter.ts
|
5130
|
-
function
|
5165
|
+
function toDataStreamInternal(stream, callbacks) {
|
5131
5166
|
return stream.pipeThrough(
|
5132
5167
|
new TransformStream({
|
5133
5168
|
transform: async (value, controller) => {
|
@@ -5148,11 +5183,25 @@ function toDataStream(stream, callbacks) {
|
|
5148
5183
|
forwardAIMessageChunk(value, controller);
|
5149
5184
|
}
|
5150
5185
|
})
|
5151
|
-
).pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(
|
5186
|
+
).pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(new TextDecoderStream()).pipeThrough(
|
5187
|
+
new TransformStream({
|
5188
|
+
transform: async (chunk, controller) => {
|
5189
|
+
controller.enqueue(formatDataStreamPart3("text", chunk));
|
5190
|
+
}
|
5191
|
+
})
|
5192
|
+
);
|
5193
|
+
}
|
5194
|
+
function toDataStream(stream, callbacks) {
|
5195
|
+
return toDataStreamInternal(stream, callbacks).pipeThrough(
|
5196
|
+
new TextEncoderStream()
|
5197
|
+
);
|
5152
5198
|
}
|
5153
5199
|
function toDataStreamResponse(stream, options) {
|
5154
5200
|
var _a11;
|
5155
|
-
const dataStream =
|
5201
|
+
const dataStream = toDataStreamInternal(
|
5202
|
+
stream,
|
5203
|
+
options == null ? void 0 : options.callbacks
|
5204
|
+
).pipeThrough(new TextEncoderStream());
|
5156
5205
|
const data = options == null ? void 0 : options.data;
|
5157
5206
|
const init = options == null ? void 0 : options.init;
|
5158
5207
|
const responseStream = data ? mergeStreams(data.stream, dataStream) : dataStream;
|
@@ -5165,6 +5214,9 @@ function toDataStreamResponse(stream, options) {
|
|
5165
5214
|
})
|
5166
5215
|
});
|
5167
5216
|
}
|
5217
|
+
function mergeIntoDataStream(stream, options) {
|
5218
|
+
options.dataStream.merge(toDataStreamInternal(stream, options.callbacks));
|
5219
|
+
}
|
5168
5220
|
function forwardAIMessageChunk(chunk, controller) {
|
5169
5221
|
if (typeof chunk.content === "string") {
|
5170
5222
|
controller.enqueue(chunk.content);
|
@@ -5181,11 +5233,13 @@ function forwardAIMessageChunk(chunk, controller) {
|
|
5181
5233
|
// streams/llamaindex-adapter.ts
|
5182
5234
|
var llamaindex_adapter_exports = {};
|
5183
5235
|
__export(llamaindex_adapter_exports, {
|
5236
|
+
mergeIntoDataStream: () => mergeIntoDataStream2,
|
5184
5237
|
toDataStream: () => toDataStream2,
|
5185
5238
|
toDataStreamResponse: () => toDataStreamResponse2
|
5186
5239
|
});
|
5187
5240
|
import { convertAsyncIteratorToReadableStream } from "@ai-sdk/provider-utils";
|
5188
|
-
|
5241
|
+
import { formatDataStreamPart as formatDataStreamPart4 } from "@ai-sdk/ui-utils";
|
5242
|
+
function toDataStreamInternal2(stream, callbacks) {
|
5189
5243
|
const trimStart = trimStartOfStream();
|
5190
5244
|
return convertAsyncIteratorToReadableStream(stream[Symbol.asyncIterator]()).pipeThrough(
|
5191
5245
|
new TransformStream({
|
@@ -5193,12 +5247,25 @@ function toDataStream2(stream, callbacks) {
|
|
5193
5247
|
controller.enqueue(trimStart(message.delta));
|
5194
5248
|
}
|
5195
5249
|
})
|
5196
|
-
).pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(
|
5250
|
+
).pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(new TextDecoderStream()).pipeThrough(
|
5251
|
+
new TransformStream({
|
5252
|
+
transform: async (chunk, controller) => {
|
5253
|
+
controller.enqueue(formatDataStreamPart4("text", chunk));
|
5254
|
+
}
|
5255
|
+
})
|
5256
|
+
);
|
5257
|
+
}
|
5258
|
+
function toDataStream2(stream, callbacks) {
|
5259
|
+
return toDataStreamInternal2(stream, callbacks).pipeThrough(
|
5260
|
+
new TextEncoderStream()
|
5261
|
+
);
|
5197
5262
|
}
|
5198
5263
|
function toDataStreamResponse2(stream, options = {}) {
|
5199
5264
|
var _a11;
|
5200
5265
|
const { init, data, callbacks } = options;
|
5201
|
-
const dataStream =
|
5266
|
+
const dataStream = toDataStreamInternal2(stream, callbacks).pipeThrough(
|
5267
|
+
new TextEncoderStream()
|
5268
|
+
);
|
5202
5269
|
const responseStream = data ? mergeStreams(data.stream, dataStream) : dataStream;
|
5203
5270
|
return new Response(responseStream, {
|
5204
5271
|
status: (_a11 = init == null ? void 0 : init.status) != null ? _a11 : 200,
|
@@ -5209,6 +5276,9 @@ function toDataStreamResponse2(stream, options = {}) {
|
|
5209
5276
|
})
|
5210
5277
|
});
|
5211
5278
|
}
|
5279
|
+
function mergeIntoDataStream2(stream, options) {
|
5280
|
+
options.dataStream.merge(toDataStreamInternal2(stream, options.callbacks));
|
5281
|
+
}
|
5212
5282
|
function trimStartOfStream() {
|
5213
5283
|
let isStreamStart = true;
|
5214
5284
|
return (text2) => {
|
@@ -5220,6 +5290,75 @@ function trimStartOfStream() {
|
|
5220
5290
|
return text2;
|
5221
5291
|
};
|
5222
5292
|
}
|
5293
|
+
|
5294
|
+
// streams/stream-data.ts
|
5295
|
+
import { formatDataStreamPart as formatDataStreamPart5 } from "@ai-sdk/ui-utils";
|
5296
|
+
|
5297
|
+
// util/constants.ts
|
5298
|
+
var HANGING_STREAM_WARNING_TIME_MS = 15 * 1e3;
|
5299
|
+
|
5300
|
+
// streams/stream-data.ts
|
5301
|
+
var StreamData = class {
|
5302
|
+
constructor() {
|
5303
|
+
this.encoder = new TextEncoder();
|
5304
|
+
this.controller = null;
|
5305
|
+
this.isClosed = false;
|
5306
|
+
this.warningTimeout = null;
|
5307
|
+
const self = this;
|
5308
|
+
this.stream = new ReadableStream({
|
5309
|
+
start: async (controller) => {
|
5310
|
+
self.controller = controller;
|
5311
|
+
if (process.env.NODE_ENV === "development") {
|
5312
|
+
self.warningTimeout = setTimeout(() => {
|
5313
|
+
console.warn(
|
5314
|
+
"The data stream is hanging. Did you forget to close it with `data.close()`?"
|
5315
|
+
);
|
5316
|
+
}, HANGING_STREAM_WARNING_TIME_MS);
|
5317
|
+
}
|
5318
|
+
},
|
5319
|
+
pull: (controller) => {
|
5320
|
+
},
|
5321
|
+
cancel: (reason) => {
|
5322
|
+
this.isClosed = true;
|
5323
|
+
}
|
5324
|
+
});
|
5325
|
+
}
|
5326
|
+
async close() {
|
5327
|
+
if (this.isClosed) {
|
5328
|
+
throw new Error("Data Stream has already been closed.");
|
5329
|
+
}
|
5330
|
+
if (!this.controller) {
|
5331
|
+
throw new Error("Stream controller is not initialized.");
|
5332
|
+
}
|
5333
|
+
this.controller.close();
|
5334
|
+
this.isClosed = true;
|
5335
|
+
if (this.warningTimeout) {
|
5336
|
+
clearTimeout(this.warningTimeout);
|
5337
|
+
}
|
5338
|
+
}
|
5339
|
+
append(value) {
|
5340
|
+
if (this.isClosed) {
|
5341
|
+
throw new Error("Data Stream has already been closed.");
|
5342
|
+
}
|
5343
|
+
if (!this.controller) {
|
5344
|
+
throw new Error("Stream controller is not initialized.");
|
5345
|
+
}
|
5346
|
+
this.controller.enqueue(
|
5347
|
+
this.encoder.encode(formatDataStreamPart5("data", [value]))
|
5348
|
+
);
|
5349
|
+
}
|
5350
|
+
appendMessageAnnotation(value) {
|
5351
|
+
if (this.isClosed) {
|
5352
|
+
throw new Error("Data Stream has already been closed.");
|
5353
|
+
}
|
5354
|
+
if (!this.controller) {
|
5355
|
+
throw new Error("Stream controller is not initialized.");
|
5356
|
+
}
|
5357
|
+
this.controller.enqueue(
|
5358
|
+
this.encoder.encode(formatDataStreamPart5("message_annotations", [value]))
|
5359
|
+
);
|
5360
|
+
}
|
5361
|
+
};
|
5223
5362
|
export {
|
5224
5363
|
AISDKError10 as AISDKError,
|
5225
5364
|
APICallError2 as APICallError,
|
@@ -5249,20 +5388,22 @@ export {
|
|
5249
5388
|
UnsupportedFunctionalityError2 as UnsupportedFunctionalityError,
|
5250
5389
|
convertToCoreMessages,
|
5251
5390
|
cosineSimilarity,
|
5252
|
-
|
5391
|
+
createDataStream,
|
5392
|
+
createDataStreamResponse,
|
5253
5393
|
embed,
|
5254
5394
|
embedMany,
|
5255
5395
|
experimental_createProviderRegistry,
|
5256
5396
|
experimental_customProvider,
|
5257
5397
|
experimental_wrapLanguageModel,
|
5258
5398
|
formatAssistantStreamPart2 as formatAssistantStreamPart,
|
5259
|
-
|
5399
|
+
formatDataStreamPart6 as formatDataStreamPart,
|
5260
5400
|
generateId2 as generateId,
|
5261
5401
|
generateObject,
|
5262
5402
|
generateText,
|
5263
5403
|
jsonSchema,
|
5264
5404
|
parseAssistantStreamPart,
|
5265
5405
|
parseDataStreamPart,
|
5406
|
+
pipeDataStreamToResponse,
|
5266
5407
|
processDataStream,
|
5267
5408
|
processTextStream,
|
5268
5409
|
streamObject,
|