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/dist/index.mjs CHANGED
@@ -2079,6 +2079,55 @@ function now() {
2079
2079
 
2080
2080
  // core/generate-object/stream-object.ts
2081
2081
  import { createIdGenerator as createIdGenerator2 } from "@ai-sdk/provider-utils";
2082
+
2083
+ // core/util/prepare-outgoing-http-headers.ts
2084
+ function prepareOutgoingHttpHeaders(init, {
2085
+ contentType,
2086
+ dataStreamVersion
2087
+ }) {
2088
+ const headers = {};
2089
+ if ((init == null ? void 0 : init.headers) != null) {
2090
+ for (const [key, value] of Object.entries(init.headers)) {
2091
+ headers[key] = value;
2092
+ }
2093
+ }
2094
+ if (headers["Content-Type"] == null) {
2095
+ headers["Content-Type"] = contentType;
2096
+ }
2097
+ if (dataStreamVersion !== void 0) {
2098
+ headers["X-Vercel-AI-Data-Stream"] = dataStreamVersion;
2099
+ }
2100
+ return headers;
2101
+ }
2102
+
2103
+ // core/util/write-to-server-response.ts
2104
+ function writeToServerResponse({
2105
+ response,
2106
+ status,
2107
+ statusText,
2108
+ headers,
2109
+ stream
2110
+ }) {
2111
+ response.writeHead(status != null ? status : 200, statusText, headers);
2112
+ const reader = stream.getReader();
2113
+ const read = async () => {
2114
+ try {
2115
+ while (true) {
2116
+ const { done, value } = await reader.read();
2117
+ if (done)
2118
+ break;
2119
+ response.write(value);
2120
+ }
2121
+ } catch (error) {
2122
+ throw error;
2123
+ } finally {
2124
+ response.end();
2125
+ }
2126
+ };
2127
+ read();
2128
+ }
2129
+
2130
+ // core/generate-object/stream-object.ts
2082
2131
  var originalGenerateId2 = createIdGenerator2({ prefix: "aiobj-", length: 24 });
2083
2132
  async function streamObject({
2084
2133
  model,
@@ -2557,27 +2606,15 @@ var DefaultStreamObjectResult = class {
2557
2606
  });
2558
2607
  }
2559
2608
  pipeTextStreamToResponse(response, init) {
2560
- var _a11;
2561
- response.writeHead((_a11 = init == null ? void 0 : init.status) != null ? _a11 : 200, {
2562
- "Content-Type": "text/plain; charset=utf-8",
2563
- ...init == null ? void 0 : init.headers
2609
+ writeToServerResponse({
2610
+ response,
2611
+ status: init == null ? void 0 : init.status,
2612
+ statusText: init == null ? void 0 : init.statusText,
2613
+ headers: prepareOutgoingHttpHeaders(init, {
2614
+ contentType: "text/plain; charset=utf-8"
2615
+ }),
2616
+ stream: this.textStream.pipeThrough(new TextEncoderStream())
2564
2617
  });
2565
- const reader = this.textStream.pipeThrough(new TextEncoderStream()).getReader();
2566
- const read = async () => {
2567
- try {
2568
- while (true) {
2569
- const { done, value } = await reader.read();
2570
- if (done)
2571
- break;
2572
- response.write(value);
2573
- }
2574
- } catch (error) {
2575
- throw error;
2576
- } finally {
2577
- response.end();
2578
- }
2579
- };
2580
- read();
2581
2618
  }
2582
2619
  toTextStreamResponse(init) {
2583
2620
  var _a11;
@@ -3946,9 +3983,9 @@ var DefaultStreamTextResult = class {
3946
3983
  });
3947
3984
  }
3948
3985
  toAIStream(callbacks = {}) {
3949
- return this.toDataStream({ callbacks });
3986
+ return this.toDataStreamInternal({ callbacks });
3950
3987
  }
3951
- toDataStream({
3988
+ toDataStreamInternal({
3952
3989
  callbacks = {},
3953
3990
  getErrorMessage: getErrorMessage4 = () => ""
3954
3991
  // mask error messages for safety by default
@@ -4056,55 +4093,45 @@ var DefaultStreamTextResult = class {
4056
4093
  pipeAIStreamToResponse(response, init) {
4057
4094
  return this.pipeDataStreamToResponse(response, init);
4058
4095
  }
4059
- pipeDataStreamToResponse(response, init) {
4060
- var _a11;
4061
- response.writeHead((_a11 = init == null ? void 0 : init.status) != null ? _a11 : 200, {
4062
- "Content-Type": "text/plain; charset=utf-8",
4063
- ...init == null ? void 0 : init.headers
4064
- });
4065
- const reader = this.toDataStream().getReader();
4066
- const read = async () => {
4067
- try {
4068
- while (true) {
4069
- const { done, value } = await reader.read();
4070
- if (done)
4071
- break;
4072
- response.write(value);
4073
- }
4074
- } catch (error) {
4075
- throw error;
4076
- } finally {
4077
- response.end();
4078
- }
4096
+ pipeDataStreamToResponse(response, options) {
4097
+ const init = options == null ? void 0 : "init" in options ? options.init : {
4098
+ headers: "headers" in options ? options.headers : void 0,
4099
+ status: "status" in options ? options.status : void 0,
4100
+ statusText: "statusText" in options ? options.statusText : void 0
4079
4101
  };
4080
- read();
4102
+ const data = options == null ? void 0 : "data" in options ? options.data : void 0;
4103
+ const getErrorMessage4 = options == null ? void 0 : "getErrorMessage" in options ? options.getErrorMessage : void 0;
4104
+ writeToServerResponse({
4105
+ response,
4106
+ status: init == null ? void 0 : init.status,
4107
+ statusText: init == null ? void 0 : init.statusText,
4108
+ headers: prepareOutgoingHttpHeaders(init, {
4109
+ contentType: "text/plain; charset=utf-8",
4110
+ dataStreamVersion: "v1"
4111
+ }),
4112
+ stream: this.toDataStream({ data, getErrorMessage: getErrorMessage4 })
4113
+ });
4081
4114
  }
4082
4115
  pipeTextStreamToResponse(response, init) {
4083
- var _a11;
4084
- response.writeHead((_a11 = init == null ? void 0 : init.status) != null ? _a11 : 200, {
4085
- "Content-Type": "text/plain; charset=utf-8",
4086
- ...init == null ? void 0 : init.headers
4116
+ writeToServerResponse({
4117
+ response,
4118
+ status: init == null ? void 0 : init.status,
4119
+ statusText: init == null ? void 0 : init.statusText,
4120
+ headers: prepareOutgoingHttpHeaders(init, {
4121
+ contentType: "text/plain; charset=utf-8"
4122
+ }),
4123
+ stream: this.textStream.pipeThrough(new TextEncoderStream())
4087
4124
  });
4088
- const reader = this.textStream.pipeThrough(new TextEncoderStream()).getReader();
4089
- const read = async () => {
4090
- try {
4091
- while (true) {
4092
- const { done, value } = await reader.read();
4093
- if (done)
4094
- break;
4095
- response.write(value);
4096
- }
4097
- } catch (error) {
4098
- throw error;
4099
- } finally {
4100
- response.end();
4101
- }
4102
- };
4103
- read();
4104
4125
  }
4105
4126
  toAIStreamResponse(options) {
4106
4127
  return this.toDataStreamResponse(options);
4107
4128
  }
4129
+ toDataStream(options) {
4130
+ const stream = this.toDataStreamInternal({
4131
+ getErrorMessage: options == null ? void 0 : options.getErrorMessage
4132
+ });
4133
+ return (options == null ? void 0 : options.data) ? mergeStreams(options == null ? void 0 : options.data.stream, stream) : stream;
4134
+ }
4108
4135
  toDataStreamResponse(options) {
4109
4136
  var _a11;
4110
4137
  const init = options == null ? void 0 : "init" in options ? options.init : {
@@ -4114,8 +4141,7 @@ var DefaultStreamTextResult = class {
4114
4141
  };
4115
4142
  const data = options == null ? void 0 : "data" in options ? options.data : void 0;
4116
4143
  const getErrorMessage4 = options == null ? void 0 : "getErrorMessage" in options ? options.getErrorMessage : void 0;
4117
- const stream = data ? mergeStreams(data.stream, this.toDataStream({ getErrorMessage: getErrorMessage4 })) : this.toDataStream({ getErrorMessage: getErrorMessage4 });
4118
- return new Response(stream, {
4144
+ return new Response(this.toDataStream({ data, getErrorMessage: getErrorMessage4 }), {
4119
4145
  status: (_a11 = init == null ? void 0 : init.status) != null ? _a11 : 200,
4120
4146
  statusText: init == null ? void 0 : init.statusText,
4121
4147
  headers: prepareResponseHeaders(init, {