ai 3.3.28 → 3.3.30
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 +12 -0
- package/dist/index.d.mts +27 -19
- package/dist/index.d.ts +27 -19
- package/dist/index.js +91 -65
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +91 -65
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
@@ -1,5 +1,17 @@
|
|
1
1
|
# ai
|
2
2
|
|
3
|
+
## 3.3.30
|
4
|
+
|
5
|
+
### Patch Changes
|
6
|
+
|
7
|
+
- 6ee1f8e: feat (ai/core): add toDataStream to streamText result
|
8
|
+
|
9
|
+
## 3.3.29
|
10
|
+
|
11
|
+
### Patch Changes
|
12
|
+
|
13
|
+
- 1e3dfd2: feat (ai/core): enhance pipeToData/TextStreamResponse methods
|
14
|
+
|
3
15
|
## 3.3.28
|
4
16
|
|
5
17
|
### Patch Changes
|
package/dist/index.d.mts
CHANGED
@@ -870,19 +870,16 @@ interface StreamObjectResult<PARTIAL, RESULT, ELEMENT_STREAM> {
|
|
870
870
|
writes each text delta as a separate chunk.
|
871
871
|
|
872
872
|
@param response A Node.js response-like object (ServerResponse).
|
873
|
-
@param init Optional headers and status
|
873
|
+
@param init Optional headers, status code, and status text.
|
874
874
|
*/
|
875
|
-
pipeTextStreamToResponse(response: ServerResponse, init?:
|
876
|
-
headers?: Record<string, string>;
|
877
|
-
status?: number;
|
878
|
-
}): void;
|
875
|
+
pipeTextStreamToResponse(response: ServerResponse, init?: ResponseInit): void;
|
879
876
|
/**
|
880
877
|
Creates a simple text stream response.
|
881
878
|
The response has a `Content-Type` header set to `text/plain; charset=utf-8`.
|
882
879
|
Each text delta is encoded as UTF-8 and sent as a separate chunk.
|
883
880
|
Non-text-delta events are ignored.
|
884
881
|
|
885
|
-
@param init Optional headers and status
|
882
|
+
@param init Optional headers, status code, and status text.
|
886
883
|
*/
|
887
884
|
toTextStreamResponse(init?: ResponseInit): Response;
|
888
885
|
}
|
@@ -1522,10 +1519,22 @@ interface StreamTextResult<TOOLS extends Record<string, CoreTool>> {
|
|
1522
1519
|
|
1523
1520
|
@returns A data stream.
|
1524
1521
|
|
1525
|
-
@deprecated Use `
|
1522
|
+
@deprecated Use `toDataStream` instead.
|
1526
1523
|
*/
|
1527
1524
|
toAIStream(callbacks?: AIStreamCallbacksAndOptions): ReadableStream<Uint8Array>;
|
1528
1525
|
/**
|
1526
|
+
Converts the result to a data stream.
|
1527
|
+
|
1528
|
+
@param data an optional StreamData object that will be merged into the stream.
|
1529
|
+
@param getErrorMessage an optional function that converts an error to an error message.
|
1530
|
+
|
1531
|
+
@return A data stream.
|
1532
|
+
*/
|
1533
|
+
toDataStream(options?: {
|
1534
|
+
data?: StreamData;
|
1535
|
+
getErrorMessage?: (error: unknown) => string;
|
1536
|
+
}): ReadableStream<Uint8Array>;
|
1537
|
+
/**
|
1529
1538
|
Writes stream data output to a Node.js response-like object.
|
1530
1539
|
It sets a `Content-Type` header to `text/plain; charset=utf-8` and
|
1531
1540
|
writes each stream data part as a separate chunk.
|
@@ -1541,15 +1550,15 @@ interface StreamTextResult<TOOLS extends Record<string, CoreTool>> {
|
|
1541
1550
|
}): void;
|
1542
1551
|
/**
|
1543
1552
|
Writes data stream output to a Node.js response-like object.
|
1544
|
-
It sets a `Content-Type` header to `text/plain; charset=utf-8` and
|
1545
|
-
writes each data stream part as a separate chunk.
|
1546
1553
|
|
1547
1554
|
@param response A Node.js response-like object (ServerResponse).
|
1548
|
-
@param init
|
1555
|
+
@param options An object with an init property (ResponseInit) and a data property.
|
1556
|
+
You can also pass in a ResponseInit directly (deprecated).
|
1549
1557
|
*/
|
1550
|
-
pipeDataStreamToResponse(response: ServerResponse$1,
|
1551
|
-
|
1552
|
-
|
1558
|
+
pipeDataStreamToResponse(response: ServerResponse$1, options?: ResponseInit | {
|
1559
|
+
init?: ResponseInit;
|
1560
|
+
data?: StreamData;
|
1561
|
+
getErrorMessage?: (error: unknown) => string;
|
1553
1562
|
}): void;
|
1554
1563
|
/**
|
1555
1564
|
Writes text delta output to a Node.js response-like object.
|
@@ -1557,12 +1566,9 @@ interface StreamTextResult<TOOLS extends Record<string, CoreTool>> {
|
|
1557
1566
|
writes each text delta as a separate chunk.
|
1558
1567
|
|
1559
1568
|
@param response A Node.js response-like object (ServerResponse).
|
1560
|
-
@param init Optional headers and status
|
1569
|
+
@param init Optional headers, status code, and status text.
|
1561
1570
|
*/
|
1562
|
-
pipeTextStreamToResponse(response: ServerResponse$1, init?:
|
1563
|
-
headers?: Record<string, string>;
|
1564
|
-
status?: number;
|
1565
|
-
}): void;
|
1571
|
+
pipeTextStreamToResponse(response: ServerResponse$1, init?: ResponseInit): void;
|
1566
1572
|
/**
|
1567
1573
|
Converts the result to a streamed response object with a stream data part stream.
|
1568
1574
|
It can be used with the `useChat` and `useCompletion` hooks.
|
@@ -1597,7 +1603,7 @@ interface StreamTextResult<TOOLS extends Record<string, CoreTool>> {
|
|
1597
1603
|
Each text delta is encoded as UTF-8 and sent as a separate chunk.
|
1598
1604
|
Non-text-delta events are ignored.
|
1599
1605
|
|
1600
|
-
@param init Optional headers and status
|
1606
|
+
@param init Optional headers, status code, and status text.
|
1601
1607
|
*/
|
1602
1608
|
toTextStreamResponse(init?: ResponseInit): Response;
|
1603
1609
|
}
|
@@ -2938,6 +2944,8 @@ declare function ReplicateStream(res: Prediction, cb?: AIStreamCallbacksAndOptio
|
|
2938
2944
|
|
2939
2945
|
/**
|
2940
2946
|
* A utility function to stream a ReadableStream to a Node.js response-like object.
|
2947
|
+
*
|
2948
|
+
* @deprecated Use `pipeDataStreamToResponse` (part of `StreamTextResult`) instead.
|
2941
2949
|
*/
|
2942
2950
|
declare function streamToResponse(res: ReadableStream, response: ServerResponse$1, init?: {
|
2943
2951
|
headers?: Record<string, string>;
|
package/dist/index.d.ts
CHANGED
@@ -870,19 +870,16 @@ interface StreamObjectResult<PARTIAL, RESULT, ELEMENT_STREAM> {
|
|
870
870
|
writes each text delta as a separate chunk.
|
871
871
|
|
872
872
|
@param response A Node.js response-like object (ServerResponse).
|
873
|
-
@param init Optional headers and status
|
873
|
+
@param init Optional headers, status code, and status text.
|
874
874
|
*/
|
875
|
-
pipeTextStreamToResponse(response: ServerResponse, init?:
|
876
|
-
headers?: Record<string, string>;
|
877
|
-
status?: number;
|
878
|
-
}): void;
|
875
|
+
pipeTextStreamToResponse(response: ServerResponse, init?: ResponseInit): void;
|
879
876
|
/**
|
880
877
|
Creates a simple text stream response.
|
881
878
|
The response has a `Content-Type` header set to `text/plain; charset=utf-8`.
|
882
879
|
Each text delta is encoded as UTF-8 and sent as a separate chunk.
|
883
880
|
Non-text-delta events are ignored.
|
884
881
|
|
885
|
-
@param init Optional headers and status
|
882
|
+
@param init Optional headers, status code, and status text.
|
886
883
|
*/
|
887
884
|
toTextStreamResponse(init?: ResponseInit): Response;
|
888
885
|
}
|
@@ -1522,10 +1519,22 @@ interface StreamTextResult<TOOLS extends Record<string, CoreTool>> {
|
|
1522
1519
|
|
1523
1520
|
@returns A data stream.
|
1524
1521
|
|
1525
|
-
@deprecated Use `
|
1522
|
+
@deprecated Use `toDataStream` instead.
|
1526
1523
|
*/
|
1527
1524
|
toAIStream(callbacks?: AIStreamCallbacksAndOptions): ReadableStream<Uint8Array>;
|
1528
1525
|
/**
|
1526
|
+
Converts the result to a data stream.
|
1527
|
+
|
1528
|
+
@param data an optional StreamData object that will be merged into the stream.
|
1529
|
+
@param getErrorMessage an optional function that converts an error to an error message.
|
1530
|
+
|
1531
|
+
@return A data stream.
|
1532
|
+
*/
|
1533
|
+
toDataStream(options?: {
|
1534
|
+
data?: StreamData;
|
1535
|
+
getErrorMessage?: (error: unknown) => string;
|
1536
|
+
}): ReadableStream<Uint8Array>;
|
1537
|
+
/**
|
1529
1538
|
Writes stream data output to a Node.js response-like object.
|
1530
1539
|
It sets a `Content-Type` header to `text/plain; charset=utf-8` and
|
1531
1540
|
writes each stream data part as a separate chunk.
|
@@ -1541,15 +1550,15 @@ interface StreamTextResult<TOOLS extends Record<string, CoreTool>> {
|
|
1541
1550
|
}): void;
|
1542
1551
|
/**
|
1543
1552
|
Writes data stream output to a Node.js response-like object.
|
1544
|
-
It sets a `Content-Type` header to `text/plain; charset=utf-8` and
|
1545
|
-
writes each data stream part as a separate chunk.
|
1546
1553
|
|
1547
1554
|
@param response A Node.js response-like object (ServerResponse).
|
1548
|
-
@param init
|
1555
|
+
@param options An object with an init property (ResponseInit) and a data property.
|
1556
|
+
You can also pass in a ResponseInit directly (deprecated).
|
1549
1557
|
*/
|
1550
|
-
pipeDataStreamToResponse(response: ServerResponse$1,
|
1551
|
-
|
1552
|
-
|
1558
|
+
pipeDataStreamToResponse(response: ServerResponse$1, options?: ResponseInit | {
|
1559
|
+
init?: ResponseInit;
|
1560
|
+
data?: StreamData;
|
1561
|
+
getErrorMessage?: (error: unknown) => string;
|
1553
1562
|
}): void;
|
1554
1563
|
/**
|
1555
1564
|
Writes text delta output to a Node.js response-like object.
|
@@ -1557,12 +1566,9 @@ interface StreamTextResult<TOOLS extends Record<string, CoreTool>> {
|
|
1557
1566
|
writes each text delta as a separate chunk.
|
1558
1567
|
|
1559
1568
|
@param response A Node.js response-like object (ServerResponse).
|
1560
|
-
@param init Optional headers and status
|
1569
|
+
@param init Optional headers, status code, and status text.
|
1561
1570
|
*/
|
1562
|
-
pipeTextStreamToResponse(response: ServerResponse$1, init?:
|
1563
|
-
headers?: Record<string, string>;
|
1564
|
-
status?: number;
|
1565
|
-
}): void;
|
1571
|
+
pipeTextStreamToResponse(response: ServerResponse$1, init?: ResponseInit): void;
|
1566
1572
|
/**
|
1567
1573
|
Converts the result to a streamed response object with a stream data part stream.
|
1568
1574
|
It can be used with the `useChat` and `useCompletion` hooks.
|
@@ -1597,7 +1603,7 @@ interface StreamTextResult<TOOLS extends Record<string, CoreTool>> {
|
|
1597
1603
|
Each text delta is encoded as UTF-8 and sent as a separate chunk.
|
1598
1604
|
Non-text-delta events are ignored.
|
1599
1605
|
|
1600
|
-
@param init Optional headers and status
|
1606
|
+
@param init Optional headers, status code, and status text.
|
1601
1607
|
*/
|
1602
1608
|
toTextStreamResponse(init?: ResponseInit): Response;
|
1603
1609
|
}
|
@@ -2938,6 +2944,8 @@ declare function ReplicateStream(res: Prediction, cb?: AIStreamCallbacksAndOptio
|
|
2938
2944
|
|
2939
2945
|
/**
|
2940
2946
|
* A utility function to stream a ReadableStream to a Node.js response-like object.
|
2947
|
+
*
|
2948
|
+
* @deprecated Use `pipeDataStreamToResponse` (part of `StreamTextResult`) instead.
|
2941
2949
|
*/
|
2942
2950
|
declare function streamToResponse(res: ReadableStream, response: ServerResponse$1, init?: {
|
2943
2951
|
headers?: Record<string, string>;
|
package/dist/index.js
CHANGED
@@ -2152,6 +2152,55 @@ function now() {
|
|
2152
2152
|
|
2153
2153
|
// core/generate-object/stream-object.ts
|
2154
2154
|
var import_provider_utils7 = require("@ai-sdk/provider-utils");
|
2155
|
+
|
2156
|
+
// core/util/prepare-outgoing-http-headers.ts
|
2157
|
+
function prepareOutgoingHttpHeaders(init, {
|
2158
|
+
contentType,
|
2159
|
+
dataStreamVersion
|
2160
|
+
}) {
|
2161
|
+
const headers = {};
|
2162
|
+
if ((init == null ? void 0 : init.headers) != null) {
|
2163
|
+
for (const [key, value] of Object.entries(init.headers)) {
|
2164
|
+
headers[key] = value;
|
2165
|
+
}
|
2166
|
+
}
|
2167
|
+
if (headers["Content-Type"] == null) {
|
2168
|
+
headers["Content-Type"] = contentType;
|
2169
|
+
}
|
2170
|
+
if (dataStreamVersion !== void 0) {
|
2171
|
+
headers["X-Vercel-AI-Data-Stream"] = dataStreamVersion;
|
2172
|
+
}
|
2173
|
+
return headers;
|
2174
|
+
}
|
2175
|
+
|
2176
|
+
// core/util/write-to-server-response.ts
|
2177
|
+
function writeToServerResponse({
|
2178
|
+
response,
|
2179
|
+
status,
|
2180
|
+
statusText,
|
2181
|
+
headers,
|
2182
|
+
stream
|
2183
|
+
}) {
|
2184
|
+
response.writeHead(status != null ? status : 200, statusText, headers);
|
2185
|
+
const reader = stream.getReader();
|
2186
|
+
const read = async () => {
|
2187
|
+
try {
|
2188
|
+
while (true) {
|
2189
|
+
const { done, value } = await reader.read();
|
2190
|
+
if (done)
|
2191
|
+
break;
|
2192
|
+
response.write(value);
|
2193
|
+
}
|
2194
|
+
} catch (error) {
|
2195
|
+
throw error;
|
2196
|
+
} finally {
|
2197
|
+
response.end();
|
2198
|
+
}
|
2199
|
+
};
|
2200
|
+
read();
|
2201
|
+
}
|
2202
|
+
|
2203
|
+
// core/generate-object/stream-object.ts
|
2155
2204
|
var originalGenerateId2 = (0, import_provider_utils7.createIdGenerator)({ prefix: "aiobj-", length: 24 });
|
2156
2205
|
async function streamObject({
|
2157
2206
|
model,
|
@@ -2630,27 +2679,15 @@ var DefaultStreamObjectResult = class {
|
|
2630
2679
|
});
|
2631
2680
|
}
|
2632
2681
|
pipeTextStreamToResponse(response, init) {
|
2633
|
-
|
2634
|
-
|
2635
|
-
|
2636
|
-
|
2682
|
+
writeToServerResponse({
|
2683
|
+
response,
|
2684
|
+
status: init == null ? void 0 : init.status,
|
2685
|
+
statusText: init == null ? void 0 : init.statusText,
|
2686
|
+
headers: prepareOutgoingHttpHeaders(init, {
|
2687
|
+
contentType: "text/plain; charset=utf-8"
|
2688
|
+
}),
|
2689
|
+
stream: this.textStream.pipeThrough(new TextEncoderStream())
|
2637
2690
|
});
|
2638
|
-
const reader = this.textStream.pipeThrough(new TextEncoderStream()).getReader();
|
2639
|
-
const read = async () => {
|
2640
|
-
try {
|
2641
|
-
while (true) {
|
2642
|
-
const { done, value } = await reader.read();
|
2643
|
-
if (done)
|
2644
|
-
break;
|
2645
|
-
response.write(value);
|
2646
|
-
}
|
2647
|
-
} catch (error) {
|
2648
|
-
throw error;
|
2649
|
-
} finally {
|
2650
|
-
response.end();
|
2651
|
-
}
|
2652
|
-
};
|
2653
|
-
read();
|
2654
2691
|
}
|
2655
2692
|
toTextStreamResponse(init) {
|
2656
2693
|
var _a11;
|
@@ -4019,9 +4056,9 @@ var DefaultStreamTextResult = class {
|
|
4019
4056
|
});
|
4020
4057
|
}
|
4021
4058
|
toAIStream(callbacks = {}) {
|
4022
|
-
return this.
|
4059
|
+
return this.toDataStreamInternal({ callbacks });
|
4023
4060
|
}
|
4024
|
-
|
4061
|
+
toDataStreamInternal({
|
4025
4062
|
callbacks = {},
|
4026
4063
|
getErrorMessage: getErrorMessage4 = () => ""
|
4027
4064
|
// mask error messages for safety by default
|
@@ -4129,55 +4166,45 @@ var DefaultStreamTextResult = class {
|
|
4129
4166
|
pipeAIStreamToResponse(response, init) {
|
4130
4167
|
return this.pipeDataStreamToResponse(response, init);
|
4131
4168
|
}
|
4132
|
-
pipeDataStreamToResponse(response,
|
4133
|
-
|
4134
|
-
|
4135
|
-
"
|
4136
|
-
|
4137
|
-
});
|
4138
|
-
const reader = this.toDataStream().getReader();
|
4139
|
-
const read = async () => {
|
4140
|
-
try {
|
4141
|
-
while (true) {
|
4142
|
-
const { done, value } = await reader.read();
|
4143
|
-
if (done)
|
4144
|
-
break;
|
4145
|
-
response.write(value);
|
4146
|
-
}
|
4147
|
-
} catch (error) {
|
4148
|
-
throw error;
|
4149
|
-
} finally {
|
4150
|
-
response.end();
|
4151
|
-
}
|
4169
|
+
pipeDataStreamToResponse(response, options) {
|
4170
|
+
const init = options == null ? void 0 : "init" in options ? options.init : {
|
4171
|
+
headers: "headers" in options ? options.headers : void 0,
|
4172
|
+
status: "status" in options ? options.status : void 0,
|
4173
|
+
statusText: "statusText" in options ? options.statusText : void 0
|
4152
4174
|
};
|
4153
|
-
|
4175
|
+
const data = options == null ? void 0 : "data" in options ? options.data : void 0;
|
4176
|
+
const getErrorMessage4 = options == null ? void 0 : "getErrorMessage" in options ? options.getErrorMessage : void 0;
|
4177
|
+
writeToServerResponse({
|
4178
|
+
response,
|
4179
|
+
status: init == null ? void 0 : init.status,
|
4180
|
+
statusText: init == null ? void 0 : init.statusText,
|
4181
|
+
headers: prepareOutgoingHttpHeaders(init, {
|
4182
|
+
contentType: "text/plain; charset=utf-8",
|
4183
|
+
dataStreamVersion: "v1"
|
4184
|
+
}),
|
4185
|
+
stream: this.toDataStream({ data, getErrorMessage: getErrorMessage4 })
|
4186
|
+
});
|
4154
4187
|
}
|
4155
4188
|
pipeTextStreamToResponse(response, init) {
|
4156
|
-
|
4157
|
-
|
4158
|
-
|
4159
|
-
|
4189
|
+
writeToServerResponse({
|
4190
|
+
response,
|
4191
|
+
status: init == null ? void 0 : init.status,
|
4192
|
+
statusText: init == null ? void 0 : init.statusText,
|
4193
|
+
headers: prepareOutgoingHttpHeaders(init, {
|
4194
|
+
contentType: "text/plain; charset=utf-8"
|
4195
|
+
}),
|
4196
|
+
stream: this.textStream.pipeThrough(new TextEncoderStream())
|
4160
4197
|
});
|
4161
|
-
const reader = this.textStream.pipeThrough(new TextEncoderStream()).getReader();
|
4162
|
-
const read = async () => {
|
4163
|
-
try {
|
4164
|
-
while (true) {
|
4165
|
-
const { done, value } = await reader.read();
|
4166
|
-
if (done)
|
4167
|
-
break;
|
4168
|
-
response.write(value);
|
4169
|
-
}
|
4170
|
-
} catch (error) {
|
4171
|
-
throw error;
|
4172
|
-
} finally {
|
4173
|
-
response.end();
|
4174
|
-
}
|
4175
|
-
};
|
4176
|
-
read();
|
4177
4198
|
}
|
4178
4199
|
toAIStreamResponse(options) {
|
4179
4200
|
return this.toDataStreamResponse(options);
|
4180
4201
|
}
|
4202
|
+
toDataStream(options) {
|
4203
|
+
const stream = this.toDataStreamInternal({
|
4204
|
+
getErrorMessage: options == null ? void 0 : options.getErrorMessage
|
4205
|
+
});
|
4206
|
+
return (options == null ? void 0 : options.data) ? mergeStreams(options == null ? void 0 : options.data.stream, stream) : stream;
|
4207
|
+
}
|
4181
4208
|
toDataStreamResponse(options) {
|
4182
4209
|
var _a11;
|
4183
4210
|
const init = options == null ? void 0 : "init" in options ? options.init : {
|
@@ -4187,8 +4214,7 @@ var DefaultStreamTextResult = class {
|
|
4187
4214
|
};
|
4188
4215
|
const data = options == null ? void 0 : "data" in options ? options.data : void 0;
|
4189
4216
|
const getErrorMessage4 = options == null ? void 0 : "getErrorMessage" in options ? options.getErrorMessage : void 0;
|
4190
|
-
|
4191
|
-
return new Response(stream, {
|
4217
|
+
return new Response(this.toDataStream({ data, getErrorMessage: getErrorMessage4 }), {
|
4192
4218
|
status: (_a11 = init == null ? void 0 : init.status) != null ? _a11 : 200,
|
4193
4219
|
statusText: init == null ? void 0 : init.statusText,
|
4194
4220
|
headers: prepareResponseHeaders(init, {
|