ai 5.0.0-canary.21 → 5.0.0-canary.22
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 +9 -0
- package/README.md +1 -1
- package/dist/index.d.mts +574 -593
- package/dist/index.d.ts +574 -593
- package/dist/index.js +190 -202
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +187 -199
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
@@ -12,172 +12,6 @@ import {
|
|
12
12
|
jsonSchema as jsonSchema2
|
13
13
|
} from "@ai-sdk/provider-utils";
|
14
14
|
|
15
|
-
// src/data-stream/create-data-stream.ts
|
16
|
-
function createDataStream({
|
17
|
-
execute,
|
18
|
-
onError = () => "An error occurred."
|
19
|
-
// mask error messages for safety by default
|
20
|
-
}) {
|
21
|
-
let controller;
|
22
|
-
const ongoingStreamPromises = [];
|
23
|
-
const stream = new ReadableStream({
|
24
|
-
start(controllerArg) {
|
25
|
-
controller = controllerArg;
|
26
|
-
}
|
27
|
-
});
|
28
|
-
function safeEnqueue(data) {
|
29
|
-
try {
|
30
|
-
controller.enqueue(data);
|
31
|
-
} catch (error) {
|
32
|
-
}
|
33
|
-
}
|
34
|
-
try {
|
35
|
-
const result = execute({
|
36
|
-
write(part) {
|
37
|
-
safeEnqueue(part);
|
38
|
-
},
|
39
|
-
merge(streamArg) {
|
40
|
-
ongoingStreamPromises.push(
|
41
|
-
(async () => {
|
42
|
-
const reader = streamArg.getReader();
|
43
|
-
while (true) {
|
44
|
-
const { done, value } = await reader.read();
|
45
|
-
if (done)
|
46
|
-
break;
|
47
|
-
safeEnqueue(value);
|
48
|
-
}
|
49
|
-
})().catch((error) => {
|
50
|
-
safeEnqueue({ type: "error", value: onError(error) });
|
51
|
-
})
|
52
|
-
);
|
53
|
-
},
|
54
|
-
onError
|
55
|
-
});
|
56
|
-
if (result) {
|
57
|
-
ongoingStreamPromises.push(
|
58
|
-
result.catch((error) => {
|
59
|
-
safeEnqueue({ type: "error", value: onError(error) });
|
60
|
-
})
|
61
|
-
);
|
62
|
-
}
|
63
|
-
} catch (error) {
|
64
|
-
safeEnqueue({ type: "error", value: onError(error) });
|
65
|
-
}
|
66
|
-
const waitForStreams = new Promise(async (resolve) => {
|
67
|
-
while (ongoingStreamPromises.length > 0) {
|
68
|
-
await ongoingStreamPromises.shift();
|
69
|
-
}
|
70
|
-
resolve();
|
71
|
-
});
|
72
|
-
waitForStreams.finally(() => {
|
73
|
-
try {
|
74
|
-
controller.close();
|
75
|
-
} catch (error) {
|
76
|
-
}
|
77
|
-
});
|
78
|
-
return stream;
|
79
|
-
}
|
80
|
-
|
81
|
-
// src/util/prepare-headers.ts
|
82
|
-
function prepareHeaders(headers, defaultHeaders) {
|
83
|
-
const responseHeaders = new Headers(headers != null ? headers : {});
|
84
|
-
for (const [key, value] of Object.entries(defaultHeaders)) {
|
85
|
-
if (!responseHeaders.has(key)) {
|
86
|
-
responseHeaders.set(key, value);
|
87
|
-
}
|
88
|
-
}
|
89
|
-
return responseHeaders;
|
90
|
-
}
|
91
|
-
|
92
|
-
// src/data-stream/data-stream-headers.ts
|
93
|
-
var dataStreamHeaders = {
|
94
|
-
"content-type": "text/event-stream",
|
95
|
-
"cache-control": "no-cache",
|
96
|
-
connection: "keep-alive",
|
97
|
-
"x-vercel-ai-data-stream": "v2",
|
98
|
-
"x-accel-buffering": "no"
|
99
|
-
// disable nginx buffering
|
100
|
-
};
|
101
|
-
|
102
|
-
// src/data-stream/json-to-sse-transform-stream.ts
|
103
|
-
var JsonToSseTransformStream = class extends TransformStream {
|
104
|
-
constructor() {
|
105
|
-
super({
|
106
|
-
transform(part, controller) {
|
107
|
-
controller.enqueue(`data: ${JSON.stringify(part)}
|
108
|
-
|
109
|
-
`);
|
110
|
-
},
|
111
|
-
flush(controller) {
|
112
|
-
controller.enqueue("data: [DONE]\n\n");
|
113
|
-
}
|
114
|
-
});
|
115
|
-
}
|
116
|
-
};
|
117
|
-
|
118
|
-
// src/data-stream/create-data-stream-response.ts
|
119
|
-
function createDataStreamResponse({
|
120
|
-
status,
|
121
|
-
statusText,
|
122
|
-
headers,
|
123
|
-
dataStream
|
124
|
-
}) {
|
125
|
-
return new Response(
|
126
|
-
dataStream.pipeThrough(new JsonToSseTransformStream()).pipeThrough(new TextEncoderStream()),
|
127
|
-
{
|
128
|
-
status,
|
129
|
-
statusText,
|
130
|
-
headers: prepareHeaders(headers, dataStreamHeaders)
|
131
|
-
}
|
132
|
-
);
|
133
|
-
}
|
134
|
-
|
135
|
-
// src/util/write-to-server-response.ts
|
136
|
-
function writeToServerResponse({
|
137
|
-
response,
|
138
|
-
status,
|
139
|
-
statusText,
|
140
|
-
headers,
|
141
|
-
stream
|
142
|
-
}) {
|
143
|
-
response.writeHead(status != null ? status : 200, statusText, headers);
|
144
|
-
const reader = stream.getReader();
|
145
|
-
const read = async () => {
|
146
|
-
try {
|
147
|
-
while (true) {
|
148
|
-
const { done, value } = await reader.read();
|
149
|
-
if (done)
|
150
|
-
break;
|
151
|
-
response.write(value);
|
152
|
-
}
|
153
|
-
} catch (error) {
|
154
|
-
throw error;
|
155
|
-
} finally {
|
156
|
-
response.end();
|
157
|
-
}
|
158
|
-
};
|
159
|
-
read();
|
160
|
-
}
|
161
|
-
|
162
|
-
// src/data-stream/pipe-data-stream-to-response.ts
|
163
|
-
function pipeDataStreamToResponse({
|
164
|
-
response,
|
165
|
-
status,
|
166
|
-
statusText,
|
167
|
-
headers,
|
168
|
-
dataStream
|
169
|
-
}) {
|
170
|
-
writeToServerResponse({
|
171
|
-
response,
|
172
|
-
status,
|
173
|
-
statusText,
|
174
|
-
headers: Object.fromEntries(
|
175
|
-
prepareHeaders(headers, dataStreamHeaders).entries()
|
176
|
-
),
|
177
|
-
stream: dataStream.pipeThrough(new JsonToSseTransformStream()).pipeThrough(new TextEncoderStream())
|
178
|
-
});
|
179
|
-
}
|
180
|
-
|
181
15
|
// src/error/index.ts
|
182
16
|
import {
|
183
17
|
AISDKError as AISDKError16,
|
@@ -540,6 +374,17 @@ var RetryError = class extends AISDKError15 {
|
|
540
374
|
};
|
541
375
|
_a15 = symbol15;
|
542
376
|
|
377
|
+
// src/util/prepare-headers.ts
|
378
|
+
function prepareHeaders(headers, defaultHeaders) {
|
379
|
+
const responseHeaders = new Headers(headers != null ? headers : {});
|
380
|
+
for (const [key, value] of Object.entries(defaultHeaders)) {
|
381
|
+
if (!responseHeaders.has(key)) {
|
382
|
+
responseHeaders.set(key, value);
|
383
|
+
}
|
384
|
+
}
|
385
|
+
return responseHeaders;
|
386
|
+
}
|
387
|
+
|
543
388
|
// src/text-stream/create-text-stream-response.ts
|
544
389
|
function createTextStreamResponse({
|
545
390
|
status,
|
@@ -556,6 +401,33 @@ function createTextStreamResponse({
|
|
556
401
|
});
|
557
402
|
}
|
558
403
|
|
404
|
+
// src/util/write-to-server-response.ts
|
405
|
+
function writeToServerResponse({
|
406
|
+
response,
|
407
|
+
status,
|
408
|
+
statusText,
|
409
|
+
headers,
|
410
|
+
stream
|
411
|
+
}) {
|
412
|
+
response.writeHead(status != null ? status : 200, statusText, headers);
|
413
|
+
const reader = stream.getReader();
|
414
|
+
const read = async () => {
|
415
|
+
try {
|
416
|
+
while (true) {
|
417
|
+
const { done, value } = await reader.read();
|
418
|
+
if (done)
|
419
|
+
break;
|
420
|
+
response.write(value);
|
421
|
+
}
|
422
|
+
} catch (error) {
|
423
|
+
throw error;
|
424
|
+
} finally {
|
425
|
+
response.end();
|
426
|
+
}
|
427
|
+
};
|
428
|
+
read();
|
429
|
+
}
|
430
|
+
|
559
431
|
// src/text-stream/pipe-text-stream-to-response.ts
|
560
432
|
function pipeTextStreamToResponse({
|
561
433
|
response,
|
@@ -593,7 +465,7 @@ import {
|
|
593
465
|
parseJsonEventStream
|
594
466
|
} from "@ai-sdk/provider-utils";
|
595
467
|
|
596
|
-
// src/
|
468
|
+
// src/ui-message-stream/ui-message-stream-parts.ts
|
597
469
|
import { z } from "zod";
|
598
470
|
var toolCallSchema = z.object({
|
599
471
|
toolCallId: z.string(),
|
@@ -614,7 +486,7 @@ var sourceSchema = z.object({
|
|
614
486
|
providerMetadata: z.any().optional()
|
615
487
|
// Use z.any() for generic metadata
|
616
488
|
});
|
617
|
-
var
|
489
|
+
var uiMessageStreamPartSchema = z.discriminatedUnion("type", [
|
618
490
|
z.object({
|
619
491
|
type: z.literal("text"),
|
620
492
|
value: z.string()
|
@@ -713,7 +585,7 @@ async function consumeStream({
|
|
713
585
|
}
|
714
586
|
}
|
715
587
|
|
716
|
-
// src/ui/process-
|
588
|
+
// src/ui/process-ui-message-stream.ts
|
717
589
|
import { validateTypes } from "@ai-sdk/provider-utils";
|
718
590
|
|
719
591
|
// src/util/merge-objects.ts
|
@@ -1101,8 +973,8 @@ function getToolInvocations(message) {
|
|
1101
973
|
).map((part) => part.toolInvocation);
|
1102
974
|
}
|
1103
975
|
|
1104
|
-
// src/ui/process-
|
1105
|
-
function
|
976
|
+
// src/ui/process-ui-message-stream.ts
|
977
|
+
function processUIMessageStream({
|
1106
978
|
stream,
|
1107
979
|
onUpdate,
|
1108
980
|
onToolCall,
|
@@ -1387,11 +1259,10 @@ var getOriginalFetch = () => fetch;
|
|
1387
1259
|
async function callChatApi({
|
1388
1260
|
api,
|
1389
1261
|
body,
|
1390
|
-
streamProtocol = "
|
1262
|
+
streamProtocol = "ui-message",
|
1391
1263
|
credentials,
|
1392
1264
|
headers,
|
1393
1265
|
abortController,
|
1394
|
-
onResponse,
|
1395
1266
|
onUpdate,
|
1396
1267
|
onFinish,
|
1397
1268
|
onToolCall,
|
@@ -1420,9 +1291,6 @@ async function callChatApi({
|
|
1420
1291
|
signal: (_b = abortController == null ? void 0 : abortController()) == null ? void 0 : _b.signal,
|
1421
1292
|
credentials
|
1422
1293
|
});
|
1423
|
-
if (onResponse != null) {
|
1424
|
-
await onResponse(response);
|
1425
|
-
}
|
1426
1294
|
if (!response.ok) {
|
1427
1295
|
throw new Error(
|
1428
1296
|
(_c = await response.text()) != null ? _c : "Failed to fetch the chat response."
|
@@ -1441,12 +1309,12 @@ async function callChatApi({
|
|
1441
1309
|
});
|
1442
1310
|
return;
|
1443
1311
|
}
|
1444
|
-
case "
|
1312
|
+
case "ui-message": {
|
1445
1313
|
await consumeStream({
|
1446
|
-
stream:
|
1314
|
+
stream: processUIMessageStream({
|
1447
1315
|
stream: parseJsonEventStream({
|
1448
1316
|
stream: response.body,
|
1449
|
-
schema:
|
1317
|
+
schema: uiMessageStreamPartSchema
|
1450
1318
|
}).pipeThrough(
|
1451
1319
|
new TransformStream({
|
1452
1320
|
async transform(part, controller) {
|
@@ -1504,7 +1372,6 @@ async function callCompletionApi({
|
|
1504
1372
|
setLoading,
|
1505
1373
|
setError,
|
1506
1374
|
setAbortController,
|
1507
|
-
onResponse,
|
1508
1375
|
onFinish,
|
1509
1376
|
onError,
|
1510
1377
|
fetch: fetch2 = getOriginalFetch2()
|
@@ -1531,13 +1398,6 @@ async function callCompletionApi({
|
|
1531
1398
|
}).catch((err) => {
|
1532
1399
|
throw err;
|
1533
1400
|
});
|
1534
|
-
if (onResponse) {
|
1535
|
-
try {
|
1536
|
-
await onResponse(response);
|
1537
|
-
} catch (err) {
|
1538
|
-
throw err;
|
1539
|
-
}
|
1540
|
-
}
|
1541
1401
|
if (!response.ok) {
|
1542
1402
|
throw new Error(
|
1543
1403
|
(_a17 = await response.text()) != null ? _a17 : "Failed to fetch the chat response."
|
@@ -1562,7 +1422,7 @@ async function callCompletionApi({
|
|
1562
1422
|
await consumeStream({
|
1563
1423
|
stream: parseJsonEventStream2({
|
1564
1424
|
stream: response.body,
|
1565
|
-
schema:
|
1425
|
+
schema: uiMessageStreamPartSchema
|
1566
1426
|
}).pipeThrough(
|
1567
1427
|
new TransformStream({
|
1568
1428
|
async transform(part) {
|
@@ -1846,6 +1706,134 @@ function updateToolCallResult({
|
|
1846
1706
|
};
|
1847
1707
|
}
|
1848
1708
|
|
1709
|
+
// src/ui-message-stream/create-ui-message-stream.ts
|
1710
|
+
function createUIMessageStream({
|
1711
|
+
execute,
|
1712
|
+
onError = () => "An error occurred."
|
1713
|
+
// mask error messages for safety by default
|
1714
|
+
}) {
|
1715
|
+
let controller;
|
1716
|
+
const ongoingStreamPromises = [];
|
1717
|
+
const stream = new ReadableStream({
|
1718
|
+
start(controllerArg) {
|
1719
|
+
controller = controllerArg;
|
1720
|
+
}
|
1721
|
+
});
|
1722
|
+
function safeEnqueue(data) {
|
1723
|
+
try {
|
1724
|
+
controller.enqueue(data);
|
1725
|
+
} catch (error) {
|
1726
|
+
}
|
1727
|
+
}
|
1728
|
+
try {
|
1729
|
+
const result = execute({
|
1730
|
+
write(part) {
|
1731
|
+
safeEnqueue(part);
|
1732
|
+
},
|
1733
|
+
merge(streamArg) {
|
1734
|
+
ongoingStreamPromises.push(
|
1735
|
+
(async () => {
|
1736
|
+
const reader = streamArg.getReader();
|
1737
|
+
while (true) {
|
1738
|
+
const { done, value } = await reader.read();
|
1739
|
+
if (done)
|
1740
|
+
break;
|
1741
|
+
safeEnqueue(value);
|
1742
|
+
}
|
1743
|
+
})().catch((error) => {
|
1744
|
+
safeEnqueue({ type: "error", value: onError(error) });
|
1745
|
+
})
|
1746
|
+
);
|
1747
|
+
},
|
1748
|
+
onError
|
1749
|
+
});
|
1750
|
+
if (result) {
|
1751
|
+
ongoingStreamPromises.push(
|
1752
|
+
result.catch((error) => {
|
1753
|
+
safeEnqueue({ type: "error", value: onError(error) });
|
1754
|
+
})
|
1755
|
+
);
|
1756
|
+
}
|
1757
|
+
} catch (error) {
|
1758
|
+
safeEnqueue({ type: "error", value: onError(error) });
|
1759
|
+
}
|
1760
|
+
const waitForStreams = new Promise(async (resolve) => {
|
1761
|
+
while (ongoingStreamPromises.length > 0) {
|
1762
|
+
await ongoingStreamPromises.shift();
|
1763
|
+
}
|
1764
|
+
resolve();
|
1765
|
+
});
|
1766
|
+
waitForStreams.finally(() => {
|
1767
|
+
try {
|
1768
|
+
controller.close();
|
1769
|
+
} catch (error) {
|
1770
|
+
}
|
1771
|
+
});
|
1772
|
+
return stream;
|
1773
|
+
}
|
1774
|
+
|
1775
|
+
// src/ui-message-stream/ui-message-stream-headers.ts
|
1776
|
+
var uiMessageStreamHeaders = {
|
1777
|
+
"content-type": "text/event-stream",
|
1778
|
+
"cache-control": "no-cache",
|
1779
|
+
connection: "keep-alive",
|
1780
|
+
"x-vercel-ai-ui-message-stream": "v1",
|
1781
|
+
"x-accel-buffering": "no"
|
1782
|
+
// disable nginx buffering
|
1783
|
+
};
|
1784
|
+
|
1785
|
+
// src/ui-message-stream/json-to-sse-transform-stream.ts
|
1786
|
+
var JsonToSseTransformStream = class extends TransformStream {
|
1787
|
+
constructor() {
|
1788
|
+
super({
|
1789
|
+
transform(part, controller) {
|
1790
|
+
controller.enqueue(`data: ${JSON.stringify(part)}
|
1791
|
+
|
1792
|
+
`);
|
1793
|
+
},
|
1794
|
+
flush(controller) {
|
1795
|
+
controller.enqueue("data: [DONE]\n\n");
|
1796
|
+
}
|
1797
|
+
});
|
1798
|
+
}
|
1799
|
+
};
|
1800
|
+
|
1801
|
+
// src/ui-message-stream/create-ui-message-stream-response.ts
|
1802
|
+
function createUIMessageStreamResponse({
|
1803
|
+
status,
|
1804
|
+
statusText,
|
1805
|
+
headers,
|
1806
|
+
stream
|
1807
|
+
}) {
|
1808
|
+
return new Response(
|
1809
|
+
stream.pipeThrough(new JsonToSseTransformStream()).pipeThrough(new TextEncoderStream()),
|
1810
|
+
{
|
1811
|
+
status,
|
1812
|
+
statusText,
|
1813
|
+
headers: prepareHeaders(headers, uiMessageStreamHeaders)
|
1814
|
+
}
|
1815
|
+
);
|
1816
|
+
}
|
1817
|
+
|
1818
|
+
// src/ui-message-stream/pipe-ui-message-stream-to-response.ts
|
1819
|
+
function pipeUIMessageStreamToResponse({
|
1820
|
+
response,
|
1821
|
+
status,
|
1822
|
+
statusText,
|
1823
|
+
headers,
|
1824
|
+
stream
|
1825
|
+
}) {
|
1826
|
+
writeToServerResponse({
|
1827
|
+
response,
|
1828
|
+
status,
|
1829
|
+
statusText,
|
1830
|
+
headers: Object.fromEntries(
|
1831
|
+
prepareHeaders(headers, uiMessageStreamHeaders).entries()
|
1832
|
+
),
|
1833
|
+
stream: stream.pipeThrough(new JsonToSseTransformStream()).pipeThrough(new TextEncoderStream())
|
1834
|
+
});
|
1835
|
+
}
|
1836
|
+
|
1849
1837
|
// src/util/data-url.ts
|
1850
1838
|
function getTextFromDataUrl(dataUrl) {
|
1851
1839
|
const [header, base64Content] = dataUrl.split(",");
|
@@ -6709,7 +6697,7 @@ var DefaultStreamTextResult = class {
|
|
6709
6697
|
)
|
6710
6698
|
);
|
6711
6699
|
}
|
6712
|
-
|
6700
|
+
toUIMessageStream({
|
6713
6701
|
newMessageId,
|
6714
6702
|
originalMessages = [],
|
6715
6703
|
onFinish,
|
@@ -6861,7 +6849,7 @@ var DefaultStreamTextResult = class {
|
|
6861
6849
|
}
|
6862
6850
|
})
|
6863
6851
|
);
|
6864
|
-
return onFinish == null ? baseStream :
|
6852
|
+
return onFinish == null ? baseStream : processUIMessageStream({
|
6865
6853
|
stream: baseStream,
|
6866
6854
|
lastMessage,
|
6867
6855
|
newMessageId: messageId != null ? messageId : this.generateId(),
|
@@ -6878,7 +6866,7 @@ var DefaultStreamTextResult = class {
|
|
6878
6866
|
}
|
6879
6867
|
});
|
6880
6868
|
}
|
6881
|
-
|
6869
|
+
pipeUIMessageStreamToResponse(response, {
|
6882
6870
|
newMessageId,
|
6883
6871
|
originalMessages,
|
6884
6872
|
onFinish,
|
@@ -6890,9 +6878,9 @@ var DefaultStreamTextResult = class {
|
|
6890
6878
|
onError,
|
6891
6879
|
...init
|
6892
6880
|
} = {}) {
|
6893
|
-
|
6881
|
+
pipeUIMessageStreamToResponse({
|
6894
6882
|
response,
|
6895
|
-
|
6883
|
+
stream: this.toUIMessageStream({
|
6896
6884
|
newMessageId,
|
6897
6885
|
originalMessages,
|
6898
6886
|
onFinish,
|
@@ -6913,7 +6901,7 @@ var DefaultStreamTextResult = class {
|
|
6913
6901
|
...init
|
6914
6902
|
});
|
6915
6903
|
}
|
6916
|
-
|
6904
|
+
toUIMessageStreamResponse({
|
6917
6905
|
newMessageId,
|
6918
6906
|
originalMessages,
|
6919
6907
|
onFinish,
|
@@ -6925,8 +6913,8 @@ var DefaultStreamTextResult = class {
|
|
6925
6913
|
onError,
|
6926
6914
|
...init
|
6927
6915
|
} = {}) {
|
6928
|
-
return
|
6929
|
-
|
6916
|
+
return createUIMessageStreamResponse({
|
6917
|
+
stream: this.toUIMessageStream({
|
6930
6918
|
newMessageId,
|
6931
6919
|
originalMessages,
|
6932
6920
|
onFinish,
|
@@ -7997,11 +7985,11 @@ export {
|
|
7997
7985
|
coreToolMessageSchema,
|
7998
7986
|
coreUserMessageSchema,
|
7999
7987
|
cosineSimilarity,
|
8000
|
-
createDataStream,
|
8001
|
-
createDataStreamResponse,
|
8002
7988
|
createIdGenerator5 as createIdGenerator,
|
8003
7989
|
createProviderRegistry,
|
8004
7990
|
createTextStreamResponse,
|
7991
|
+
createUIMessageStream,
|
7992
|
+
createUIMessageStreamResponse,
|
8005
7993
|
customProvider,
|
8006
7994
|
defaultSettingsMiddleware,
|
8007
7995
|
embed,
|
@@ -8024,8 +8012,8 @@ export {
|
|
8024
8012
|
jsonSchema2 as jsonSchema,
|
8025
8013
|
modelMessageSchema,
|
8026
8014
|
parsePartialJson,
|
8027
|
-
pipeDataStreamToResponse,
|
8028
8015
|
pipeTextStreamToResponse,
|
8016
|
+
pipeUIMessageStreamToResponse,
|
8029
8017
|
processTextStream,
|
8030
8018
|
shouldResubmitMessages,
|
8031
8019
|
simulateReadableStream,
|