ai 3.3.28 → 3.3.29

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 CHANGED
@@ -1,5 +1,11 @@
1
1
  # ai
2
2
 
3
+ ## 3.3.29
4
+
5
+ ### Patch Changes
6
+
7
+ - 1e3dfd2: feat (ai/core): enhance pipeToData/TextStreamResponse methods
8
+
3
9
  ## 3.3.28
4
10
 
5
11
  ### 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 code.
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 code.
882
+ @param init Optional headers, status code, and status text.
886
883
  */
887
884
  toTextStreamResponse(init?: ResponseInit): Response;
888
885
  }
@@ -1541,15 +1538,15 @@ interface StreamTextResult<TOOLS extends Record<string, CoreTool>> {
1541
1538
  }): void;
1542
1539
  /**
1543
1540
  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
1541
 
1547
1542
  @param response A Node.js response-like object (ServerResponse).
1548
- @param init Optional headers and status code.
1543
+ @param options An object with an init property (ResponseInit) and a data property.
1544
+ You can also pass in a ResponseInit directly (deprecated).
1549
1545
  */
1550
- pipeDataStreamToResponse(response: ServerResponse$1, init?: {
1551
- headers?: Record<string, string>;
1552
- status?: number;
1546
+ pipeDataStreamToResponse(response: ServerResponse$1, options?: ResponseInit | {
1547
+ init?: ResponseInit;
1548
+ data?: StreamData;
1549
+ getErrorMessage?: (error: unknown) => string;
1553
1550
  }): void;
1554
1551
  /**
1555
1552
  Writes text delta output to a Node.js response-like object.
@@ -1557,12 +1554,9 @@ interface StreamTextResult<TOOLS extends Record<string, CoreTool>> {
1557
1554
  writes each text delta as a separate chunk.
1558
1555
 
1559
1556
  @param response A Node.js response-like object (ServerResponse).
1560
- @param init Optional headers and status code.
1557
+ @param init Optional headers, status code, and status text.
1561
1558
  */
1562
- pipeTextStreamToResponse(response: ServerResponse$1, init?: {
1563
- headers?: Record<string, string>;
1564
- status?: number;
1565
- }): void;
1559
+ pipeTextStreamToResponse(response: ServerResponse$1, init?: ResponseInit): void;
1566
1560
  /**
1567
1561
  Converts the result to a streamed response object with a stream data part stream.
1568
1562
  It can be used with the `useChat` and `useCompletion` hooks.
@@ -1597,7 +1591,7 @@ interface StreamTextResult<TOOLS extends Record<string, CoreTool>> {
1597
1591
  Each text delta is encoded as UTF-8 and sent as a separate chunk.
1598
1592
  Non-text-delta events are ignored.
1599
1593
 
1600
- @param init Optional headers and status code.
1594
+ @param init Optional headers, status code, and status text.
1601
1595
  */
1602
1596
  toTextStreamResponse(init?: ResponseInit): Response;
1603
1597
  }
@@ -2938,6 +2932,8 @@ declare function ReplicateStream(res: Prediction, cb?: AIStreamCallbacksAndOptio
2938
2932
 
2939
2933
  /**
2940
2934
  * A utility function to stream a ReadableStream to a Node.js response-like object.
2935
+ *
2936
+ * @deprecated Use `pipeDataStreamToResponse` (part of `StreamTextResult`) instead.
2941
2937
  */
2942
2938
  declare function streamToResponse(res: ReadableStream, response: ServerResponse$1, init?: {
2943
2939
  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 code.
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 code.
882
+ @param init Optional headers, status code, and status text.
886
883
  */
887
884
  toTextStreamResponse(init?: ResponseInit): Response;
888
885
  }
@@ -1541,15 +1538,15 @@ interface StreamTextResult<TOOLS extends Record<string, CoreTool>> {
1541
1538
  }): void;
1542
1539
  /**
1543
1540
  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
1541
 
1547
1542
  @param response A Node.js response-like object (ServerResponse).
1548
- @param init Optional headers and status code.
1543
+ @param options An object with an init property (ResponseInit) and a data property.
1544
+ You can also pass in a ResponseInit directly (deprecated).
1549
1545
  */
1550
- pipeDataStreamToResponse(response: ServerResponse$1, init?: {
1551
- headers?: Record<string, string>;
1552
- status?: number;
1546
+ pipeDataStreamToResponse(response: ServerResponse$1, options?: ResponseInit | {
1547
+ init?: ResponseInit;
1548
+ data?: StreamData;
1549
+ getErrorMessage?: (error: unknown) => string;
1553
1550
  }): void;
1554
1551
  /**
1555
1552
  Writes text delta output to a Node.js response-like object.
@@ -1557,12 +1554,9 @@ interface StreamTextResult<TOOLS extends Record<string, CoreTool>> {
1557
1554
  writes each text delta as a separate chunk.
1558
1555
 
1559
1556
  @param response A Node.js response-like object (ServerResponse).
1560
- @param init Optional headers and status code.
1557
+ @param init Optional headers, status code, and status text.
1561
1558
  */
1562
- pipeTextStreamToResponse(response: ServerResponse$1, init?: {
1563
- headers?: Record<string, string>;
1564
- status?: number;
1565
- }): void;
1559
+ pipeTextStreamToResponse(response: ServerResponse$1, init?: ResponseInit): void;
1566
1560
  /**
1567
1561
  Converts the result to a streamed response object with a stream data part stream.
1568
1562
  It can be used with the `useChat` and `useCompletion` hooks.
@@ -1597,7 +1591,7 @@ interface StreamTextResult<TOOLS extends Record<string, CoreTool>> {
1597
1591
  Each text delta is encoded as UTF-8 and sent as a separate chunk.
1598
1592
  Non-text-delta events are ignored.
1599
1593
 
1600
- @param init Optional headers and status code.
1594
+ @param init Optional headers, status code, and status text.
1601
1595
  */
1602
1596
  toTextStreamResponse(init?: ResponseInit): Response;
1603
1597
  }
@@ -2938,6 +2932,8 @@ declare function ReplicateStream(res: Prediction, cb?: AIStreamCallbacksAndOptio
2938
2932
 
2939
2933
  /**
2940
2934
  * A utility function to stream a ReadableStream to a Node.js response-like object.
2935
+ *
2936
+ * @deprecated Use `pipeDataStreamToResponse` (part of `StreamTextResult`) instead.
2941
2937
  */
2942
2938
  declare function streamToResponse(res: ReadableStream, response: ServerResponse$1, init?: {
2943
2939
  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
- var _a11;
2634
- response.writeHead((_a11 = init == null ? void 0 : init.status) != null ? _a11 : 200, {
2635
- "Content-Type": "text/plain; charset=utf-8",
2636
- ...init == null ? void 0 : init.headers
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;
@@ -4129,51 +4166,35 @@ var DefaultStreamTextResult = class {
4129
4166
  pipeAIStreamToResponse(response, init) {
4130
4167
  return this.pipeDataStreamToResponse(response, init);
4131
4168
  }
4132
- pipeDataStreamToResponse(response, init) {
4133
- var _a11;
4134
- response.writeHead((_a11 = init == null ? void 0 : init.status) != null ? _a11 : 200, {
4135
- "Content-Type": "text/plain; charset=utf-8",
4136
- ...init == null ? void 0 : init.headers
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
- read();
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: data ? mergeStreams(data.stream, this.toDataStream({ getErrorMessage: getErrorMessage4 })) : this.toDataStream({ getErrorMessage: getErrorMessage4 })
4186
+ });
4154
4187
  }
4155
4188
  pipeTextStreamToResponse(response, init) {
4156
- var _a11;
4157
- response.writeHead((_a11 = init == null ? void 0 : init.status) != null ? _a11 : 200, {
4158
- "Content-Type": "text/plain; charset=utf-8",
4159
- ...init == null ? void 0 : init.headers
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);