@ai-sdk/provider-utils 5.0.0-canary.41 → 5.0.0-canary.44

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,26 @@
1
1
  # @ai-sdk/provider-utils
2
2
 
3
+ ## 5.0.0-canary.44
4
+
5
+ ### Patch Changes
6
+
7
+ - 6c93e36: feat(provider-utils): add `spawnCommand` method to `Experimental_Sandbox` to allow for detached command execution
8
+ - f617ac2: feat(provider-utils): narrow `tool()` return type to `ExecutableTool<...>` when `execute` is provided
9
+
10
+ ## 5.0.0-canary.43
11
+
12
+ ### Patch Changes
13
+
14
+ - 7fc6bd6: Raise minimum supported Node.js version to 22. Supported versions: 22, 24, and 26.
15
+ - Updated dependencies [7fc6bd6]
16
+ - @ai-sdk/provider@4.0.0-canary.17
17
+
18
+ ## 5.0.0-canary.42
19
+
20
+ ### Patch Changes
21
+
22
+ - a6617c5: feat(provider-utils): add `readFile` and `writeFile` plus convenience wrappers to `Experimental_Sandbox` abstraction
23
+
3
24
  ## 5.0.0-canary.41
4
25
 
5
26
  ### Patch Changes
package/dist/index.d.ts CHANGED
@@ -738,6 +738,21 @@ declare class DownloadError extends AISDKError {
738
738
  static isInstance(error: unknown): error is DownloadError;
739
739
  }
740
740
 
741
+ /**
742
+ * Extracts a 1-based inclusive line range from `text`, auto-detecting the
743
+ * file's line ending (`\r\n`, `\n`, or `\r`, in that priority).
744
+ *
745
+ * Mixed line endings are not supported: detection picks one and uses it for
746
+ * both the split and the rejoin, so files that mix conventions will not slice
747
+ * cleanly. When neither `startLine` nor `endLine` is provided, the input is
748
+ * returned unchanged. `endLine` past EOF clamps to the last line.
749
+ */
750
+ declare function extractLines({ text, startLine, endLine, }: {
751
+ text: string;
752
+ startLine?: number;
753
+ endLine?: number;
754
+ }): string;
755
+
741
756
  /**
742
757
  * Extracts the headers from a response object and returns them as a key-value object.
743
758
  *
@@ -1156,6 +1171,17 @@ declare const postToApi: <T>({ url, headers, body, successfulResponseHandler, fa
1156
1171
  */
1157
1172
  type Context = Record<string, unknown>;
1158
1173
 
1174
+ /**
1175
+ * A tool that is guaranteed to expose an execute function.
1176
+ */
1177
+ type ExecutableTool<TOOL extends Tool = Tool> = TOOL & {
1178
+ execute: NonNullable<TOOL['execute']>;
1179
+ };
1180
+ /**
1181
+ * Checks whether a tool exposes an execute function.
1182
+ */
1183
+ declare function isExecutableTool<TOOL extends Tool>(tool: TOOL | undefined): tool is ExecutableTool<TOOL>;
1184
+
1159
1185
  type NeverOptional<N, T> = 0 extends 1 & N ? Partial<T> : [N] extends [never] ? Partial<Record<keyof T, undefined>> : T;
1160
1186
 
1161
1187
  /**
@@ -1283,7 +1309,7 @@ type UserContent = string | Array<TextPart | ImagePart | FilePart>;
1283
1309
  type ModelMessage = SystemModelMessage | UserModelMessage | AssistantModelMessage | ToolModelMessage;
1284
1310
 
1285
1311
  /**
1286
- * Sandbox environment that can execute commands.
1312
+ * Sandbox environment that can execute commands and read/write files.
1287
1313
  */
1288
1314
  type Experimental_Sandbox = {
1289
1315
  /**
@@ -1295,7 +1321,7 @@ type Experimental_Sandbox = {
1295
1321
  /**
1296
1322
  * Run a command in the sandbox.
1297
1323
  */
1298
- readonly runCommand: (options: {
1324
+ readonly run: (options: {
1299
1325
  /**
1300
1326
  * Command to execute in the sandbox.
1301
1327
  */
@@ -1322,6 +1348,179 @@ type Experimental_Sandbox = {
1322
1348
  */
1323
1349
  stderr: string;
1324
1350
  }>;
1351
+ /**
1352
+ * Read one file from the sandbox as a stream of bytes. Resolves to `null`
1353
+ * when the file does not exist.
1354
+ *
1355
+ * Relative path handling is implementation-defined. This is the lowest-level
1356
+ * read primitive; prefer `readBinaryFile` or `readTextFile` unless you need
1357
+ * to stream bytes.
1358
+ */
1359
+ readonly readFile: (options: {
1360
+ /**
1361
+ * Path of the file to read.
1362
+ */
1363
+ path: string;
1364
+ /**
1365
+ * Signal that can be used to abort the read.
1366
+ */
1367
+ abortSignal?: AbortSignal;
1368
+ }) => PromiseLike<ReadableStream<Uint8Array> | null>;
1369
+ /**
1370
+ * Read one file from the sandbox as raw bytes. Resolves to `null` when the
1371
+ * file does not exist.
1372
+ */
1373
+ readonly readBinaryFile: (options: {
1374
+ /**
1375
+ * Path of the file to read.
1376
+ */
1377
+ path: string;
1378
+ /**
1379
+ * Signal that can be used to abort the read.
1380
+ */
1381
+ abortSignal?: AbortSignal;
1382
+ }) => PromiseLike<Uint8Array | null>;
1383
+ /**
1384
+ * Read one text file from the sandbox, decoded using the requested encoding.
1385
+ * Resolves to `null` when the file does not exist.
1386
+ *
1387
+ * Line ranges are 1-based and inclusive. When `endLine` is past EOF the read
1388
+ * returns through EOF without error.
1389
+ */
1390
+ readonly readTextFile: (options: {
1391
+ /**
1392
+ * Path of the file to read.
1393
+ */
1394
+ path: string;
1395
+ /**
1396
+ * Text encoding used to decode the file bytes. Defaults to `"utf-8"`.
1397
+ */
1398
+ encoding?: string;
1399
+ /**
1400
+ * 1-based inclusive start line. Defaults to 1.
1401
+ */
1402
+ startLine?: number;
1403
+ /**
1404
+ * 1-based inclusive end line. When past the file's line count, the read
1405
+ * returns through EOF without error.
1406
+ */
1407
+ endLine?: number;
1408
+ /**
1409
+ * Signal that can be used to abort the read.
1410
+ */
1411
+ abortSignal?: AbortSignal;
1412
+ }) => PromiseLike<string | null>;
1413
+ /**
1414
+ * Write one file to the sandbox from a stream of bytes. Creates parent
1415
+ * directories recursively and overwrites any existing file.
1416
+ *
1417
+ * This is the lowest-level write primitive; prefer `writeBinaryFile` or
1418
+ * `writeTextFile` when the full content is already materialized in memory.
1419
+ */
1420
+ readonly writeFile: (options: {
1421
+ /**
1422
+ * Path of the file to write.
1423
+ */
1424
+ path: string;
1425
+ /**
1426
+ * Stream of bytes to write.
1427
+ */
1428
+ content: ReadableStream<Uint8Array>;
1429
+ /**
1430
+ * Signal that can be used to abort the write.
1431
+ */
1432
+ abortSignal?: AbortSignal;
1433
+ }) => PromiseLike<void>;
1434
+ /**
1435
+ * Write one file to the sandbox from raw bytes. Creates parent directories
1436
+ * recursively and overwrites any existing file.
1437
+ */
1438
+ readonly writeBinaryFile: (options: {
1439
+ /**
1440
+ * Path of the file to write.
1441
+ */
1442
+ path: string;
1443
+ /**
1444
+ * Raw bytes to write.
1445
+ */
1446
+ content: Uint8Array;
1447
+ /**
1448
+ * Signal that can be used to abort the write.
1449
+ */
1450
+ abortSignal?: AbortSignal;
1451
+ }) => PromiseLike<void>;
1452
+ /**
1453
+ * Write one file to the sandbox from a string, encoded using the requested
1454
+ * encoding. Creates parent directories recursively and overwrites any
1455
+ * existing file.
1456
+ */
1457
+ readonly writeTextFile: (options: {
1458
+ /**
1459
+ * Path of the file to write.
1460
+ */
1461
+ path: string;
1462
+ /**
1463
+ * Text content to write.
1464
+ */
1465
+ content: string;
1466
+ /**
1467
+ * Text encoding used to encode the string to bytes. Defaults to `"utf-8"`.
1468
+ */
1469
+ encoding?: string;
1470
+ /**
1471
+ * Signal that can be used to abort the write.
1472
+ */
1473
+ abortSignal?: AbortSignal;
1474
+ }) => PromiseLike<void>;
1475
+ /**
1476
+ * Spawn a long-running process in the sandbox. Returns immediately with a
1477
+ * handle that streams stdout/stderr, can be waited on, and can be killed.
1478
+ *
1479
+ * `run` is conceptually a thin wrapper over this primitive: spawn,
1480
+ * collect both streams to strings, await `wait()`, return the result.
1481
+ */
1482
+ readonly spawn: (options: {
1483
+ /**
1484
+ * Command to execute in the sandbox.
1485
+ */
1486
+ command: string;
1487
+ /**
1488
+ * Working directory to execute the command in.
1489
+ */
1490
+ workingDirectory?: string;
1491
+ /**
1492
+ * Signal that can be used to abort the process. When aborted, the process
1493
+ * is killed and `wait()` rejects with the abort reason.
1494
+ */
1495
+ abortSignal?: AbortSignal;
1496
+ }) => PromiseLike<Experimental_SandboxProcess>;
1497
+ };
1498
+ /**
1499
+ * Handle to a long-running process started via `Experimental_Sandbox.spawn`.
1500
+ */
1501
+ type Experimental_SandboxProcess = {
1502
+ /**
1503
+ * Process identifier, if the sandbox implementation exposes one.
1504
+ */
1505
+ readonly pid?: number;
1506
+ /**
1507
+ * Stream of bytes written by the process to standard output.
1508
+ */
1509
+ readonly stdout: ReadableStream<Uint8Array>;
1510
+ /**
1511
+ * Stream of bytes written by the process to standard error.
1512
+ */
1513
+ readonly stderr: ReadableStream<Uint8Array>;
1514
+ /**
1515
+ * Resolve when the process exits, yielding its exit code.
1516
+ */
1517
+ wait(): PromiseLike<{
1518
+ exitCode: number;
1519
+ }>;
1520
+ /**
1521
+ * Terminate the process. Idempotent.
1522
+ */
1523
+ kill(): PromiseLike<void>;
1325
1524
  };
1326
1525
 
1327
1526
  /**
@@ -1629,7 +1828,14 @@ type Tool<INPUT extends JSONValue | unknown | never = any, OUTPUT extends JSONVa
1629
1828
  * Infer the tool type from a tool object.
1630
1829
  *
1631
1830
  * This is useful for type inference when working with tool objects.
1831
+ *
1832
+ * When the input has an `execute` function, the return type narrows to
1833
+ * `ExecutableTool<Tool<...>>` so that `.execute` is non-nullable without
1834
+ * needing `isExecutableTool` or a `!` assertion at the call site.
1632
1835
  */
1836
+ declare function tool<INPUT, OUTPUT, CONTEXT extends Context>(tool: Tool<INPUT, OUTPUT, CONTEXT> & {
1837
+ execute: ToolExecuteFunction<INPUT, OUTPUT, CONTEXT>;
1838
+ }): ExecutableTool<Tool<INPUT, OUTPUT, CONTEXT>>;
1633
1839
  declare function tool<INPUT, OUTPUT, CONTEXT extends Context>(tool: Tool<INPUT, OUTPUT, CONTEXT>): Tool<INPUT, OUTPUT, CONTEXT>;
1634
1840
  declare function tool<INPUT, CONTEXT extends Context>(tool: Tool<INPUT, never, CONTEXT>): Tool<INPUT, never, CONTEXT>;
1635
1841
  declare function tool<OUTPUT, CONTEXT extends Context>(tool: Tool<never, OUTPUT, CONTEXT>): Tool<never, OUTPUT, CONTEXT>;
@@ -1960,17 +2166,6 @@ declare function withUserAgentSuffix(headers: HeadersInit | Record<string, strin
1960
2166
 
1961
2167
  declare function withoutTrailingSlash(url: string | undefined): string | undefined;
1962
2168
 
1963
- /**
1964
- * A tool that is guaranteed to expose an execute function.
1965
- */
1966
- type ExecutableTool<TOOL extends Tool = Tool> = TOOL & {
1967
- execute: NonNullable<TOOL['execute']>;
1968
- };
1969
- /**
1970
- * Checks whether a tool exposes an execute function.
1971
- */
1972
- declare function isExecutableTool<TOOL extends Tool>(tool: TOOL | undefined): tool is ExecutableTool<TOOL>;
1973
-
1974
2169
  /**
1975
2170
  * Detects the `any` type so untyped tools can be treated as having no explicit
1976
2171
  * context type.
@@ -2122,4 +2317,4 @@ interface ToolResult<NAME extends string, INPUT, OUTPUT> {
2122
2317
  dynamic?: boolean;
2123
2318
  }
2124
2319
 
2125
- export { type Arrayable, type AssistantContent, type AssistantModelMessage, type Context, type CustomPart, DEFAULT_MAX_DOWNLOAD_SIZE, type DataContent, DelayedPromise, DownloadError, type DynamicTool, type ExecutableTool, type Experimental_Sandbox, type FetchFunction, type FileData, type FileDataData, type FileDataReference, type FileDataText, type FileDataUrl, type FilePart, type FlexibleSchema, type FunctionTool, type HasRequiredKey, type IdGenerator, type ImagePart, type InferSchema, type InferToolContext, type InferToolInput, type InferToolOutput, type InferToolSetContext, type LazySchema, type MaybePromiseLike, type ModelMessage, type ParseResult, type ProviderDefinedTool, type ProviderDefinedToolFactory, type ProviderDefinedToolFactoryWithOutputSchema, type ProviderExecutedTool, type ProviderExecutedToolFactory, type ProviderOptions, type ProviderReference, type ReasoningFilePart, type ReasoningPart, type Resolvable, type ResponseHandler, type Schema, type StreamingToolCallDelta, StreamingToolCallTracker, type StreamingToolCallTrackerOptions, type SystemModelMessage, type TextPart, type Tool, type ToolApprovalRequest, type ToolApprovalResponse, type ToolCall, type ToolCallPart, type ToolContent, type ToolExecuteFunction, type ToolExecutionOptions, type ToolModelMessage, type ToolNameMapping, type ToolNeedsApprovalFunction, type ToolResult, type ToolResultOutput, type ToolResultPart, type ToolSet, type UserContent, type UserModelMessage, VERSION, type ValidationResult, asArray, asSchema, combineHeaders, convertAsyncIteratorToReadableStream, convertBase64ToUint8Array, convertImageModelFileToDataUri, convertInlineFileDataToUint8Array, convertToBase64, convertToFormData, convertUint8ArrayToBase64, createBinaryResponseHandler, createEventSourceResponseHandler, createIdGenerator, createJsonErrorResponseHandler, createJsonResponseHandler, createProviderDefinedToolFactory, createProviderDefinedToolFactoryWithOutputSchema, createProviderExecutedToolFactory, createStatusCodeErrorResponseHandler, createToolNameMapping, delay, detectMediaType, downloadBlob, dynamicTool, executeTool, extractResponseHeaders, filterNullable, generateId, getFromApi, getRuntimeEnvironmentUserAgent, getTopLevelMediaType, injectJsonInstructionIntoMessages, isAbortError, isBuffer, isCustomReasoning, isExecutableTool, isFullMediaType, isNonNullable, isParsableJson, isProviderReference, isUrlSupported, jsonSchema, lazySchema, loadApiKey, loadOptionalSetting, loadSetting, mapReasoningToProviderBudget, mapReasoningToProviderEffort, mediaTypeToExtension, normalizeHeaders, parseJSON, parseJsonEventStream, parseProviderOptions, postFormDataToApi, postJsonToApi, postToApi, readResponseWithSizeLimit, removeUndefinedEntries, resolve, resolveFullMediaType, resolveProviderReference, safeParseJSON, safeValidateTypes, serializeModelOptions, stripFileExtension, tool, validateDownloadUrl, validateTypes, withUserAgentSuffix, withoutTrailingSlash, zodSchema };
2320
+ export { type Arrayable, type AssistantContent, type AssistantModelMessage, type Context, type CustomPart, DEFAULT_MAX_DOWNLOAD_SIZE, type DataContent, DelayedPromise, DownloadError, type DynamicTool, type ExecutableTool, type Experimental_Sandbox, type Experimental_SandboxProcess, type FetchFunction, type FileData, type FileDataData, type FileDataReference, type FileDataText, type FileDataUrl, type FilePart, type FlexibleSchema, type FunctionTool, type HasRequiredKey, type IdGenerator, type ImagePart, type InferSchema, type InferToolContext, type InferToolInput, type InferToolOutput, type InferToolSetContext, type LazySchema, type MaybePromiseLike, type ModelMessage, type ParseResult, type ProviderDefinedTool, type ProviderDefinedToolFactory, type ProviderDefinedToolFactoryWithOutputSchema, type ProviderExecutedTool, type ProviderExecutedToolFactory, type ProviderOptions, type ProviderReference, type ReasoningFilePart, type ReasoningPart, type Resolvable, type ResponseHandler, type Schema, type StreamingToolCallDelta, StreamingToolCallTracker, type StreamingToolCallTrackerOptions, type SystemModelMessage, type TextPart, type Tool, type ToolApprovalRequest, type ToolApprovalResponse, type ToolCall, type ToolCallPart, type ToolContent, type ToolExecuteFunction, type ToolExecutionOptions, type ToolModelMessage, type ToolNameMapping, type ToolNeedsApprovalFunction, type ToolResult, type ToolResultOutput, type ToolResultPart, type ToolSet, type UserContent, type UserModelMessage, VERSION, type ValidationResult, asArray, asSchema, combineHeaders, convertAsyncIteratorToReadableStream, convertBase64ToUint8Array, convertImageModelFileToDataUri, convertInlineFileDataToUint8Array, convertToBase64, convertToFormData, convertUint8ArrayToBase64, createBinaryResponseHandler, createEventSourceResponseHandler, createIdGenerator, createJsonErrorResponseHandler, createJsonResponseHandler, createProviderDefinedToolFactory, createProviderDefinedToolFactoryWithOutputSchema, createProviderExecutedToolFactory, createStatusCodeErrorResponseHandler, createToolNameMapping, delay, detectMediaType, downloadBlob, dynamicTool, executeTool, extractLines, extractResponseHeaders, filterNullable, generateId, getFromApi, getRuntimeEnvironmentUserAgent, getTopLevelMediaType, injectJsonInstructionIntoMessages, isAbortError, isBuffer, isCustomReasoning, isExecutableTool, isFullMediaType, isNonNullable, isParsableJson, isProviderReference, isUrlSupported, jsonSchema, lazySchema, loadApiKey, loadOptionalSetting, loadSetting, mapReasoningToProviderBudget, mapReasoningToProviderEffort, mediaTypeToExtension, normalizeHeaders, parseJSON, parseJsonEventStream, parseProviderOptions, postFormDataToApi, postJsonToApi, postToApi, readResponseWithSizeLimit, removeUndefinedEntries, resolve, resolveFullMediaType, resolveProviderReference, safeParseJSON, safeValidateTypes, serializeModelOptions, stripFileExtension, tool, validateDownloadUrl, validateTypes, withUserAgentSuffix, withoutTrailingSlash, zodSchema };
package/dist/index.js CHANGED
@@ -701,6 +701,20 @@ async function downloadBlob(url, options) {
701
701
  }
702
702
  }
703
703
 
704
+ // src/extract-lines.ts
705
+ function extractLines({
706
+ text,
707
+ startLine,
708
+ endLine
709
+ }) {
710
+ if (startLine == null && endLine == null) return text;
711
+ const lineEnding = text.includes("\r\n") ? "\r\n" : text.includes("\n") ? "\n" : text.includes("\r") ? "\r" : "\n";
712
+ const lines = text.split(lineEnding);
713
+ const start = Math.max(1, startLine != null ? startLine : 1) - 1;
714
+ const end = Math.min(lines.length, endLine != null ? endLine : lines.length);
715
+ return lines.slice(start, end).join(lineEnding);
716
+ }
717
+
704
718
  // src/extract-response-headers.ts
705
719
  function extractResponseHeaders(response) {
706
720
  return Object.fromEntries([...response.headers]);
@@ -862,7 +876,7 @@ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
862
876
  }
863
877
 
864
878
  // src/version.ts
865
- var VERSION = true ? "5.0.0-canary.41" : "0.0.0-test";
879
+ var VERSION = true ? "5.0.0-canary.44" : "0.0.0-test";
866
880
 
867
881
  // src/get-from-api.ts
868
882
  var getOriginalFetch = () => globalThis.fetch;
@@ -3349,6 +3363,7 @@ export {
3349
3363
  downloadBlob,
3350
3364
  dynamicTool,
3351
3365
  executeTool,
3366
+ extractLines,
3352
3367
  extractResponseHeaders,
3353
3368
  filterNullable,
3354
3369
  generateId,