ai 5.0.218 → 5.0.219

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,17 @@
1
1
  # ai
2
2
 
3
+ ## 5.0.219
4
+
5
+ ### Patch Changes
6
+
7
+ - 649bd0f: fix(ai): bound media-type sniffing decode for ID3-prefixed input
8
+
9
+ Media-type detection stripped ID3 tags before the ~18-byte prefix cap, decoding the entire base64 attachment (plus a full-size copy) whenever the data began with `ID3`/`SUQz`. This turned the intended O(1) sniff into an O(N) decode of the whole attachment. Detection now decodes at most a bounded prefix and skips the ID3 tag within that bound, keeping cost O(1) in input size on both image and audio paths.
10
+
11
+ - 965545b: Return response piping promises so callers can catch stream read and write errors.
12
+ - Updated dependencies [fb4c0d7]
13
+ - @ai-sdk/gateway@2.0.118
14
+
3
15
  ## 5.0.218
4
16
 
5
17
  ### Patch Changes
package/dist/index.d.mts CHANGED
@@ -2108,7 +2108,7 @@ interface StreamTextResult<TOOLS extends ToolSet, PARTIAL_OUTPUT> {
2108
2108
  /**
2109
2109
  *Writes UI message stream output to a Node.js response-like object.
2110
2110
  */
2111
- pipeUIMessageStreamToResponse<UI_MESSAGE extends UIMessage>(response: ServerResponse, options?: UIMessageStreamResponseInit & UIMessageStreamOptions<UI_MESSAGE>): void;
2111
+ pipeUIMessageStreamToResponse<UI_MESSAGE extends UIMessage>(response: ServerResponse, options?: UIMessageStreamResponseInit & UIMessageStreamOptions<UI_MESSAGE>): Promise<void>;
2112
2112
  /**
2113
2113
  Writes text delta output to a Node.js response-like object.
2114
2114
  It sets a `Content-Type` header to `text/plain; charset=utf-8` and
@@ -2117,7 +2117,7 @@ interface StreamTextResult<TOOLS extends ToolSet, PARTIAL_OUTPUT> {
2117
2117
  @param response A Node.js response-like object (ServerResponse).
2118
2118
  @param init Optional headers, status code, and status text.
2119
2119
  */
2120
- pipeTextStreamToResponse(response: ServerResponse, init?: ResponseInit): void;
2120
+ pipeTextStreamToResponse(response: ServerResponse, init?: ResponseInit): Promise<void>;
2121
2121
  /**
2122
2122
  Converts the result to a streamed response object with a stream data part stream.
2123
2123
 
@@ -3238,7 +3238,7 @@ interface StreamObjectResult<PARTIAL, RESULT, ELEMENT_STREAM> {
3238
3238
  @param response A Node.js response-like object (ServerResponse).
3239
3239
  @param init Optional headers, status code, and status text.
3240
3240
  */
3241
- pipeTextStreamToResponse(response: ServerResponse$1, init?: ResponseInit): void;
3241
+ pipeTextStreamToResponse(response: ServerResponse$1, init?: ResponseInit): Promise<void>;
3242
3242
  /**
3243
3243
  Creates a simple text stream response.
3244
3244
  The response has a `Content-Type` header set to `text/plain; charset=utf-8`.
@@ -3713,7 +3713,7 @@ declare function createTextStreamResponse({ status, statusText, headers, textStr
3713
3713
  declare function pipeTextStreamToResponse({ response, status, statusText, headers, textStream, }: {
3714
3714
  response: ServerResponse;
3715
3715
  textStream: ReadableStream<string>;
3716
- } & ResponseInit): void;
3716
+ } & ResponseInit): Promise<void>;
3717
3717
 
3718
3718
  /**
3719
3719
  The result of a `transcribe` call.
@@ -3893,7 +3893,7 @@ declare class JsonToSseTransformStream extends TransformStream<unknown, string>
3893
3893
  declare function pipeUIMessageStreamToResponse({ response, status, statusText, headers, stream, consumeSseStream, }: {
3894
3894
  response: ServerResponse;
3895
3895
  stream: ReadableStream<UIMessageChunk>;
3896
- } & UIMessageStreamResponseInit): void;
3896
+ } & UIMessageStreamResponseInit): Promise<void>;
3897
3897
 
3898
3898
  /**
3899
3899
  * Transforms a stream of `UIMessageChunk`s into an `AsyncIterableStream` of `UIMessage`s.
package/dist/index.d.ts CHANGED
@@ -2108,7 +2108,7 @@ interface StreamTextResult<TOOLS extends ToolSet, PARTIAL_OUTPUT> {
2108
2108
  /**
2109
2109
  *Writes UI message stream output to a Node.js response-like object.
2110
2110
  */
2111
- pipeUIMessageStreamToResponse<UI_MESSAGE extends UIMessage>(response: ServerResponse, options?: UIMessageStreamResponseInit & UIMessageStreamOptions<UI_MESSAGE>): void;
2111
+ pipeUIMessageStreamToResponse<UI_MESSAGE extends UIMessage>(response: ServerResponse, options?: UIMessageStreamResponseInit & UIMessageStreamOptions<UI_MESSAGE>): Promise<void>;
2112
2112
  /**
2113
2113
  Writes text delta output to a Node.js response-like object.
2114
2114
  It sets a `Content-Type` header to `text/plain; charset=utf-8` and
@@ -2117,7 +2117,7 @@ interface StreamTextResult<TOOLS extends ToolSet, PARTIAL_OUTPUT> {
2117
2117
  @param response A Node.js response-like object (ServerResponse).
2118
2118
  @param init Optional headers, status code, and status text.
2119
2119
  */
2120
- pipeTextStreamToResponse(response: ServerResponse, init?: ResponseInit): void;
2120
+ pipeTextStreamToResponse(response: ServerResponse, init?: ResponseInit): Promise<void>;
2121
2121
  /**
2122
2122
  Converts the result to a streamed response object with a stream data part stream.
2123
2123
 
@@ -3238,7 +3238,7 @@ interface StreamObjectResult<PARTIAL, RESULT, ELEMENT_STREAM> {
3238
3238
  @param response A Node.js response-like object (ServerResponse).
3239
3239
  @param init Optional headers, status code, and status text.
3240
3240
  */
3241
- pipeTextStreamToResponse(response: ServerResponse$1, init?: ResponseInit): void;
3241
+ pipeTextStreamToResponse(response: ServerResponse$1, init?: ResponseInit): Promise<void>;
3242
3242
  /**
3243
3243
  Creates a simple text stream response.
3244
3244
  The response has a `Content-Type` header set to `text/plain; charset=utf-8`.
@@ -3713,7 +3713,7 @@ declare function createTextStreamResponse({ status, statusText, headers, textStr
3713
3713
  declare function pipeTextStreamToResponse({ response, status, statusText, headers, textStream, }: {
3714
3714
  response: ServerResponse;
3715
3715
  textStream: ReadableStream<string>;
3716
- } & ResponseInit): void;
3716
+ } & ResponseInit): Promise<void>;
3717
3717
 
3718
3718
  /**
3719
3719
  The result of a `transcribe` call.
@@ -3893,7 +3893,7 @@ declare class JsonToSseTransformStream extends TransformStream<unknown, string>
3893
3893
  declare function pipeUIMessageStreamToResponse({ response, status, statusText, headers, stream, consumeSseStream, }: {
3894
3894
  response: ServerResponse;
3895
3895
  stream: ReadableStream<UIMessageChunk>;
3896
- } & UIMessageStreamResponseInit): void;
3896
+ } & UIMessageStreamResponseInit): Promise<void>;
3897
3897
 
3898
3898
  /**
3899
3899
  * Transforms a stream of `UIMessageChunk`s into an `AsyncIterableStream` of `UIMessage`s.
package/dist/index.js CHANGED
@@ -755,25 +755,37 @@ var audioMediaTypeSignatures = [
755
755
  bytesPrefix: [26, 69, 223, 163]
756
756
  }
757
757
  ];
758
- var stripID3 = (data) => {
759
- const bytes = typeof data === "string" ? (0, import_provider_utils.convertBase64ToUint8Array)(data) : data;
758
+ var DEFAULT_SNIFF_BYTES = 18;
759
+ var MAX_SIGNATURE_BYTES = 12;
760
+ var MAX_ID3_TAG_BYTES = 128 * 1024;
761
+ var ID3_SCAN_BYTES = MAX_ID3_TAG_BYTES + MAX_SIGNATURE_BYTES;
762
+ function decodePrefix(data, maxBytes) {
763
+ if (typeof data !== "string") {
764
+ return data.length > maxBytes ? data.subarray(0, maxBytes) : data;
765
+ }
766
+ const maxChars = Math.ceil(maxBytes / 3) * 4;
767
+ const bytes = (0, import_provider_utils.convertBase64ToUint8Array)(
768
+ data.substring(0, Math.min(data.length, maxChars))
769
+ );
770
+ return bytes.length > maxBytes ? bytes.subarray(0, maxBytes) : bytes;
771
+ }
772
+ function hasID3(bytes) {
773
+ return bytes.length > 10 && bytes[0] === 73 && // 'I'
774
+ bytes[1] === 68 && // 'D'
775
+ bytes[2] === 51;
776
+ }
777
+ var stripID3 = (bytes) => {
760
778
  const id3Size = (bytes[6] & 127) << 21 | (bytes[7] & 127) << 14 | (bytes[8] & 127) << 7 | bytes[9] & 127;
761
- return bytes.slice(id3Size + 10);
779
+ return bytes.subarray(id3Size + 10);
762
780
  };
763
- function stripID3TagsIfPresent(data) {
764
- const hasId3 = typeof data === "string" && data.startsWith("SUQz") || typeof data !== "string" && data.length > 10 && data[0] === 73 && // 'I'
765
- data[1] === 68 && // 'D'
766
- data[2] === 51;
767
- return hasId3 ? stripID3(data) : data;
768
- }
769
781
  function detectMediaType({
770
782
  data,
771
783
  signatures
772
784
  }) {
773
- const processedData = stripID3TagsIfPresent(data);
774
- const bytes = typeof processedData === "string" ? (0, import_provider_utils.convertBase64ToUint8Array)(
775
- processedData.substring(0, Math.min(processedData.length, 24))
776
- ) : processedData;
785
+ let bytes = decodePrefix(data, DEFAULT_SNIFF_BYTES);
786
+ if (hasID3(bytes)) {
787
+ bytes = stripID3(decodePrefix(data, ID3_SCAN_BYTES));
788
+ }
777
789
  for (const signature of signatures) {
778
790
  if (bytes.length >= signature.bytesPrefix.length && signature.bytesPrefix.every(
779
791
  (byte, index) => byte === null || bytes[index] === byte
@@ -788,7 +800,7 @@ function detectMediaType({
788
800
  var import_provider_utils2 = require("@ai-sdk/provider-utils");
789
801
 
790
802
  // src/version.ts
791
- var VERSION = true ? "5.0.218" : "0.0.0-test";
803
+ var VERSION = true ? "5.0.219" : "0.0.0-test";
792
804
 
793
805
  // src/util/download/download.ts
794
806
  var download = async ({
@@ -2942,7 +2954,7 @@ function writeToServerResponse({
2942
2954
  response.end();
2943
2955
  }
2944
2956
  };
2945
- read();
2957
+ return read();
2946
2958
  }
2947
2959
 
2948
2960
  // src/text-stream/pipe-text-stream-to-response.ts
@@ -2953,7 +2965,7 @@ function pipeTextStreamToResponse({
2953
2965
  headers,
2954
2966
  textStream
2955
2967
  }) {
2956
- writeToServerResponse({
2968
+ return writeToServerResponse({
2957
2969
  response,
2958
2970
  status,
2959
2971
  statusText,
@@ -4188,7 +4200,7 @@ function pipeUIMessageStreamToResponse({
4188
4200
  sseStream = stream1;
4189
4201
  consumeSseStream({ stream: stream2 });
4190
4202
  }
4191
- writeToServerResponse({
4203
+ return writeToServerResponse({
4192
4204
  response,
4193
4205
  status,
4194
4206
  statusText,
@@ -5935,7 +5947,7 @@ var DefaultStreamTextResult = class {
5935
5947
  onError,
5936
5948
  ...init
5937
5949
  } = {}) {
5938
- pipeUIMessageStreamToResponse({
5950
+ return pipeUIMessageStreamToResponse({
5939
5951
  response,
5940
5952
  stream: this.toUIMessageStream({
5941
5953
  originalMessages,
@@ -5952,7 +5964,7 @@ var DefaultStreamTextResult = class {
5952
5964
  });
5953
5965
  }
5954
5966
  pipeTextStreamToResponse(response, init) {
5955
- pipeTextStreamToResponse({
5967
+ return pipeTextStreamToResponse({
5956
5968
  response,
5957
5969
  textStream: this.textStream,
5958
5970
  ...init
@@ -8078,7 +8090,7 @@ var DefaultStreamObjectResult = class {
8078
8090
  return createAsyncIterableStream(this.baseStream);
8079
8091
  }
8080
8092
  pipeTextStreamToResponse(response, init) {
8081
- pipeTextStreamToResponse({
8093
+ return pipeTextStreamToResponse({
8082
8094
  response,
8083
8095
  textStream: this.textStream,
8084
8096
  ...init