@ai-sdk/provider-utils 5.0.82 → 5.0.84

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/provider-utils",
3
- "version": "5.0.82",
3
+ "version": "5.0.84",
4
4
  "type": "module",
5
5
  "license": "Apache-2.0",
6
6
  "sideEffects": false,
@@ -50,7 +50,7 @@
50
50
  "@ai-sdk/provider": "4.0.39",
51
51
  "@standard-schema/spec": "^1.1.0",
52
52
  "@workflow/serde": "4.1.0",
53
- "eventsource-parser": "^3.0.8"
53
+ "eventsource-parser": "^3.1.1"
54
54
  },
55
55
  "devDependencies": {
56
56
  "@types/node": "22.19.19",
@@ -84,10 +84,9 @@
84
84
  "ai"
85
85
  ],
86
86
  "scripts": {
87
- "build": "pnpm clean && tsdown",
88
- "build:watch": "pnpm clean && tsdown --watch",
89
- "clean": "del-cli *.tsbuildinfo",
90
- "type-check": "tsc --build",
87
+ "build": "tsdown",
88
+ "build:watch": "tsdown --watch",
89
+ "type-check": "tsc --noEmit",
91
90
  "test": "pnpm test:node && pnpm test:edge",
92
91
  "test:update": "pnpm test:node -u",
93
92
  "test:watch": "vitest --config vitest.node.config.js",
@@ -0,0 +1,35 @@
1
+ import { InvalidArgumentError } from '@ai-sdk/provider';
2
+
3
+ /**
4
+ * Keeps a prefix of a text stream while counting all UTF-16 code units, like
5
+ * `String.length`. Appending more output does not retain the discarded text.
6
+ */
7
+ export function createTextOutputCapture({ maxLength }: { maxLength: number }) {
8
+ if (!Number.isSafeInteger(maxLength) || maxLength < 1) {
9
+ throw new InvalidArgumentError({
10
+ argument: 'maxOutputLength',
11
+ message: 'Must be a positive safe integer.',
12
+ });
13
+ }
14
+
15
+ let text = '';
16
+ let length = 0;
17
+
18
+ return {
19
+ append(chunk: string): void {
20
+ length += chunk.length;
21
+ const remaining = maxLength - text.length;
22
+ if (remaining > 0) {
23
+ // A substring can keep its entire source string alive. Joining its
24
+ // code units copies the prefix, including unpaired surrogates.
25
+ text += chunk.slice(0, remaining).split('').join('');
26
+ }
27
+ },
28
+ get text(): string {
29
+ return text;
30
+ },
31
+ get length(): number {
32
+ return length;
33
+ },
34
+ };
35
+ }
package/src/index.ts CHANGED
@@ -12,6 +12,7 @@ export { convertJsonResponseToolStream } from './convert-json-response-tool-stre
12
12
  export { convertToFormData } from './convert-to-form-data';
13
13
  export { createLanguageModelResponseMetadata } from './create-language-model-response-metadata';
14
14
  export { createNullLanguageModelUsage } from './create-null-language-model-usage';
15
+ export { createTextOutputCapture } from './create-text-output-capture';
15
16
  export {
16
17
  createToolNameMapping,
17
18
  type ToolNameMapping,
@@ -5,21 +5,38 @@ import {
5
5
  import { safeParseJSON, type ParseResult } from './parse-json';
6
6
  import type { FlexibleSchema } from './schema';
7
7
 
8
+ /**
9
+ * Default maximum size of a single event: 64 * 1024 * 1024 characters.
10
+ *
11
+ * An event is buffered until it is complete, so a stream that never completes
12
+ * an event would grow the buffer until the process runs out of memory. With
13
+ * this limit, such a stream fails with an error that can be handled instead.
14
+ */
15
+ const DEFAULT_MAX_EVENT_SIZE = 64 * 1024 * 1024;
16
+
8
17
  /**
9
18
  * Parses a JSON event stream into a stream of parsed JSON objects.
10
19
  */
11
20
  export function parseJsonEventStream<T>({
12
21
  stream,
13
22
  schema,
23
+ maxEventSize = DEFAULT_MAX_EVENT_SIZE,
14
24
  }: {
15
25
  stream: ReadableStream<Uint8Array>;
16
26
  schema: FlexibleSchema<T>;
27
+
28
+ /**
29
+ * Maximum size of a single event in characters. The returned stream fails
30
+ * with an error when an event is larger. Defaults to 64 * 1024 * 1024
31
+ * characters.
32
+ */
33
+ maxEventSize?: number;
17
34
  }): ReadableStream<ParseResult<T>> {
18
35
  return stream
19
36
  .pipeThrough(
20
37
  new TextDecoderStream() as ReadableWritablePair<string, Uint8Array>,
21
38
  )
22
- .pipeThrough(new EventSourceParserStream())
39
+ .pipeThrough(new EventSourceParserStream({ maxBufferSize: maxEventSize }))
23
40
  .pipeThrough(
24
41
  new TransformStream<EventSourceMessage, ParseResult<T>>({
25
42
  async transform({ data }, controller) {
@@ -27,7 +27,38 @@ type SandboxProcessOptions = {
27
27
  abortSignal?: AbortSignal;
28
28
  };
29
29
 
30
- export type SandboxRunOptions = SandboxProcessOptions;
30
+ export type SandboxRunOptions = SandboxProcessOptions & {
31
+ /**
32
+ * Optional positive safe integer limiting each returned output stream to at
33
+ * most this many UTF-16 code units. The command still runs to completion and its
34
+ * remaining output is drained and discarded. Supporting implementations
35
+ * return `stdoutLength` and `stderrLength` with the original stream lengths.
36
+ * Older implementations may ignore this option and return the full output.
37
+ * Native sandbox runtimes may impose their own buffering and output limits.
38
+ */
39
+ maxOutputLength?: number;
40
+ };
41
+
42
+ type SandboxRunResult = {
43
+ /** Exit code returned by the command. */
44
+ exitCode: number;
45
+ /** Standard output produced by the command. */
46
+ stdout: string;
47
+ /** Standard error produced by the command. */
48
+ stderr: string;
49
+ /**
50
+ * Original stdout length in UTF-16 code units. Returned by implementations
51
+ * that support `maxOutputLength` when that option is set. If omitted,
52
+ * stdout contains the full output.
53
+ */
54
+ stdoutLength?: number;
55
+ /**
56
+ * Original stderr length in UTF-16 code units. Returned by implementations
57
+ * that support `maxOutputLength` when that option is set. If omitted,
58
+ * stderr contains the full output.
59
+ */
60
+ stderrLength?: number;
61
+ };
31
62
 
32
63
  export type SandboxSpawnOptions = SandboxProcessOptions & {
33
64
  stdin?: boolean;
@@ -278,22 +309,7 @@ export type SandboxSession = {
278
309
  /**
279
310
  * Run a command in the sandbox.
280
311
  */
281
- readonly run: (options: SandboxRunOptions) => PromiseLike<{
282
- /**
283
- * Exit code returned by the command.
284
- */
285
- exitCode: number;
286
-
287
- /**
288
- * Standard output produced by the command.
289
- */
290
- stdout: string;
291
-
292
- /**
293
- * Standard error produced by the command.
294
- */
295
- stderr: string;
296
- }>;
312
+ readonly run: (options: SandboxRunOptions) => PromiseLike<SandboxRunResult>;
297
313
  };
298
314
 
299
315
  /**