@ai-sdk/provider-utils 5.0.0-canary.43 → 5.0.0-canary.45

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,19 @@
1
1
  # @ai-sdk/provider-utils
2
2
 
3
+ ## 5.0.0-canary.45
4
+
5
+ ### Patch Changes
6
+
7
+ - ee798eb: chore(provider-utils): rename `Experimental_Sandbox` to `Experimental_SandboxSession`
8
+ - daf6637: feat(provider-utils): add `env` option to `spawn` and `run` methods of `Experimental_SandboxSession`
9
+
10
+ ## 5.0.0-canary.44
11
+
12
+ ### Patch Changes
13
+
14
+ - 6c93e36: feat(provider-utils): add `spawnCommand` method to `Experimental_Sandbox` to allow for detached command execution
15
+ - f617ac2: feat(provider-utils): narrow `tool()` return type to `ExecutableTool<...>` when `execute` is provided
16
+
3
17
  ## 5.0.0-canary.43
4
18
 
5
19
  ### Patch Changes
package/dist/index.d.ts CHANGED
@@ -1171,6 +1171,17 @@ declare const postToApi: <T>({ url, headers, body, successfulResponseHandler, fa
1171
1171
  */
1172
1172
  type Context = Record<string, unknown>;
1173
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
+
1174
1185
  type NeverOptional<N, T> = 0 extends 1 & N ? Partial<T> : [N] extends [never] ? Partial<Record<keyof T, undefined>> : T;
1175
1186
 
1176
1187
  /**
@@ -1298,45 +1309,71 @@ type UserContent = string | Array<TextPart | ImagePart | FilePart>;
1298
1309
  type ModelMessage = SystemModelMessage | UserModelMessage | AssistantModelMessage | ToolModelMessage;
1299
1310
 
1300
1311
  /**
1301
- * Sandbox environment that can execute commands and read/write files.
1312
+ * Options for executing a command in the sandbox via `run` or `spawn`.
1313
+ */
1314
+ type SandboxProcessOptions = {
1315
+ /**
1316
+ * Command to execute in the sandbox.
1317
+ */
1318
+ command: string;
1319
+ /**
1320
+ * Working directory to execute the command in.
1321
+ */
1322
+ workingDirectory?: string;
1323
+ /**
1324
+ * Environment variables to set for this command. Merged with the
1325
+ * sandbox's default environment; values here take precedence.
1326
+ * Supporting environment variables as an option is preferable from a
1327
+ * security perspective, e.g. to avoid them leaking in logs.
1328
+ */
1329
+ env?: Record<string, string>;
1330
+ /**
1331
+ * Signal that can be used to abort the command. When aborted, the running
1332
+ * process is killed; for `spawn`, `wait()` rejects with the abort reason.
1333
+ */
1334
+ abortSignal?: AbortSignal;
1335
+ };
1336
+ /**
1337
+ * Options for reading a file from the sandbox.
1338
+ */
1339
+ type ReadFileOptions = {
1340
+ /**
1341
+ * Path of the file to read.
1342
+ */
1343
+ path: string;
1344
+ /**
1345
+ * Signal that can be used to abort the read.
1346
+ */
1347
+ abortSignal?: AbortSignal;
1348
+ };
1349
+ /**
1350
+ * Options for writing a file to the sandbox. `CONTENT` is the payload written
1351
+ * to the file: a byte stream, raw bytes, or a string.
1352
+ */
1353
+ type WriteFileOptions<CONTENT> = {
1354
+ /**
1355
+ * Path of the file to write.
1356
+ */
1357
+ path: string;
1358
+ /**
1359
+ * Content to write to the file.
1360
+ */
1361
+ content: CONTENT;
1362
+ /**
1363
+ * Signal that can be used to abort the write.
1364
+ */
1365
+ abortSignal?: AbortSignal;
1366
+ };
1367
+ /**
1368
+ * Sandbox session that can execute commands and read/write files.
1302
1369
  */
1303
- type Experimental_Sandbox = {
1370
+ type SandboxSession = {
1304
1371
  /**
1305
1372
  * Description of the sandbox environment that can be added to the agent's instructions
1306
1373
  * so that the agent knows about relevant details such as the root directory, exposed
1307
1374
  * ports, the public hostname, etc.
1308
1375
  */
1309
1376
  readonly description: string;
1310
- /**
1311
- * Run a command in the sandbox.
1312
- */
1313
- readonly runCommand: (options: {
1314
- /**
1315
- * Command to execute in the sandbox.
1316
- */
1317
- command: string;
1318
- /**
1319
- * Working directory to execute the command in.
1320
- */
1321
- workingDirectory?: string;
1322
- /**
1323
- * Signal that can be used to abort the command.
1324
- */
1325
- abortSignal?: AbortSignal;
1326
- }) => PromiseLike<{
1327
- /**
1328
- * Exit code returned by the command.
1329
- */
1330
- exitCode: number;
1331
- /**
1332
- * Standard output produced by the command.
1333
- */
1334
- stdout: string;
1335
- /**
1336
- * Standard error produced by the command.
1337
- */
1338
- stderr: string;
1339
- }>;
1340
1377
  /**
1341
1378
  * Read one file from the sandbox as a stream of bytes. Resolves to `null`
1342
1379
  * when the file does not exist.
@@ -1345,30 +1382,12 @@ type Experimental_Sandbox = {
1345
1382
  * read primitive; prefer `readBinaryFile` or `readTextFile` unless you need
1346
1383
  * to stream bytes.
1347
1384
  */
1348
- readonly readFile: (options: {
1349
- /**
1350
- * Path of the file to read.
1351
- */
1352
- path: string;
1353
- /**
1354
- * Signal that can be used to abort the read.
1355
- */
1356
- abortSignal?: AbortSignal;
1357
- }) => PromiseLike<ReadableStream<Uint8Array> | null>;
1385
+ readonly readFile: (options: ReadFileOptions) => PromiseLike<ReadableStream<Uint8Array> | null>;
1358
1386
  /**
1359
1387
  * Read one file from the sandbox as raw bytes. Resolves to `null` when the
1360
1388
  * file does not exist.
1361
1389
  */
1362
- readonly readBinaryFile: (options: {
1363
- /**
1364
- * Path of the file to read.
1365
- */
1366
- path: string;
1367
- /**
1368
- * Signal that can be used to abort the read.
1369
- */
1370
- abortSignal?: AbortSignal;
1371
- }) => PromiseLike<Uint8Array | null>;
1390
+ readonly readBinaryFile: (options: ReadFileOptions) => PromiseLike<Uint8Array | null>;
1372
1391
  /**
1373
1392
  * Read one text file from the sandbox, decoded using the requested encoding.
1374
1393
  * Resolves to `null` when the file does not exist.
@@ -1376,11 +1395,7 @@ type Experimental_Sandbox = {
1376
1395
  * Line ranges are 1-based and inclusive. When `endLine` is past EOF the read
1377
1396
  * returns through EOF without error.
1378
1397
  */
1379
- readonly readTextFile: (options: {
1380
- /**
1381
- * Path of the file to read.
1382
- */
1383
- path: string;
1398
+ readonly readTextFile: (options: ReadFileOptions & {
1384
1399
  /**
1385
1400
  * Text encoding used to decode the file bytes. Defaults to `"utf-8"`.
1386
1401
  */
@@ -1394,10 +1409,6 @@ type Experimental_Sandbox = {
1394
1409
  * returns through EOF without error.
1395
1410
  */
1396
1411
  endLine?: number;
1397
- /**
1398
- * Signal that can be used to abort the read.
1399
- */
1400
- abortSignal?: AbortSignal;
1401
1412
  }) => PromiseLike<string | null>;
1402
1413
  /**
1403
1414
  * Write one file to the sandbox from a stream of bytes. Creates parent
@@ -1406,61 +1417,75 @@ type Experimental_Sandbox = {
1406
1417
  * This is the lowest-level write primitive; prefer `writeBinaryFile` or
1407
1418
  * `writeTextFile` when the full content is already materialized in memory.
1408
1419
  */
1409
- readonly writeFile: (options: {
1410
- /**
1411
- * Path of the file to write.
1412
- */
1413
- path: string;
1414
- /**
1415
- * Stream of bytes to write.
1416
- */
1417
- content: ReadableStream<Uint8Array>;
1418
- /**
1419
- * Signal that can be used to abort the write.
1420
- */
1421
- abortSignal?: AbortSignal;
1422
- }) => PromiseLike<void>;
1420
+ readonly writeFile: (options: WriteFileOptions<ReadableStream<Uint8Array>>) => PromiseLike<void>;
1423
1421
  /**
1424
1422
  * Write one file to the sandbox from raw bytes. Creates parent directories
1425
1423
  * recursively and overwrites any existing file.
1426
1424
  */
1427
- readonly writeBinaryFile: (options: {
1428
- /**
1429
- * Path of the file to write.
1430
- */
1431
- path: string;
1432
- /**
1433
- * Raw bytes to write.
1434
- */
1435
- content: Uint8Array;
1436
- /**
1437
- * Signal that can be used to abort the write.
1438
- */
1439
- abortSignal?: AbortSignal;
1440
- }) => PromiseLike<void>;
1425
+ readonly writeBinaryFile: (options: WriteFileOptions<Uint8Array>) => PromiseLike<void>;
1441
1426
  /**
1442
1427
  * Write one file to the sandbox from a string, encoded using the requested
1443
1428
  * encoding. Creates parent directories recursively and overwrites any
1444
1429
  * existing file.
1445
1430
  */
1446
- readonly writeTextFile: (options: {
1431
+ readonly writeTextFile: (options: WriteFileOptions<string> & {
1447
1432
  /**
1448
- * Path of the file to write.
1433
+ * Text encoding used to encode the string to bytes. Defaults to `"utf-8"`.
1449
1434
  */
1450
- path: string;
1435
+ encoding?: string;
1436
+ }) => PromiseLike<void>;
1437
+ /**
1438
+ * Spawn a long-running process in the sandbox. Returns immediately with a
1439
+ * handle that streams stdout/stderr, can be waited on, and can be killed.
1440
+ *
1441
+ * `run` is conceptually a thin wrapper over this primitive: spawn,
1442
+ * collect both streams to strings, await `wait()`, return the result.
1443
+ */
1444
+ readonly spawn: (options: SandboxProcessOptions) => PromiseLike<SandboxProcess>;
1445
+ /**
1446
+ * Run a command in the sandbox.
1447
+ */
1448
+ readonly run: (options: SandboxProcessOptions) => PromiseLike<{
1451
1449
  /**
1452
- * Text content to write.
1450
+ * Exit code returned by the command.
1453
1451
  */
1454
- content: string;
1452
+ exitCode: number;
1455
1453
  /**
1456
- * Text encoding used to encode the string to bytes. Defaults to `"utf-8"`.
1454
+ * Standard output produced by the command.
1457
1455
  */
1458
- encoding?: string;
1456
+ stdout: string;
1459
1457
  /**
1460
- * Signal that can be used to abort the write.
1458
+ * Standard error produced by the command.
1461
1459
  */
1462
- abortSignal?: AbortSignal;
1463
- }) => PromiseLike<void>;
1460
+ stderr: string;
1461
+ }>;
1462
+ };
1463
+ /**
1464
+ * Handle to a long-running process started via `SandboxSession.spawn`.
1465
+ */
1466
+ type SandboxProcess = {
1467
+ /**
1468
+ * Process identifier, if the sandbox implementation exposes one.
1469
+ */
1470
+ readonly pid?: number;
1471
+ /**
1472
+ * Stream of bytes written by the process to standard output.
1473
+ */
1474
+ readonly stdout: ReadableStream<Uint8Array>;
1475
+ /**
1476
+ * Stream of bytes written by the process to standard error.
1477
+ */
1478
+ readonly stderr: ReadableStream<Uint8Array>;
1479
+ /**
1480
+ * Resolve when the process exits, yielding its exit code.
1481
+ */
1482
+ wait(): PromiseLike<{
1483
+ exitCode: number;
1484
+ }>;
1485
+ /**
1486
+ * Terminate the process. Idempotent.
1487
+ */
1488
+ kill(): PromiseLike<void>;
1464
1489
  };
1465
1490
 
1466
1491
  /**
@@ -1495,7 +1520,7 @@ interface ToolExecutionOptions<CONTEXT extends Context | unknown | never> {
1495
1520
  /**
1496
1521
  * The sandbox environment that the tool is operating in.
1497
1522
  */
1498
- experimental_sandbox?: Experimental_Sandbox;
1523
+ experimental_sandbox?: SandboxSession;
1499
1524
  }
1500
1525
  /**
1501
1526
  * Function that executes the tool and returns either a single result or a stream of results.
@@ -1663,7 +1688,7 @@ type BaseFunctionTool<INPUT extends JSONValue | unknown | never = any, OUTPUT ex
1663
1688
  */
1664
1689
  description?: string | ((options: {
1665
1690
  context: NoInfer<CONTEXT>;
1666
- experimental_sandbox?: Experimental_Sandbox;
1691
+ experimental_sandbox?: SandboxSession;
1667
1692
  }) => string);
1668
1693
  /**
1669
1694
  * Strict mode setting for the tool.
@@ -1768,7 +1793,14 @@ type Tool<INPUT extends JSONValue | unknown | never = any, OUTPUT extends JSONVa
1768
1793
  * Infer the tool type from a tool object.
1769
1794
  *
1770
1795
  * This is useful for type inference when working with tool objects.
1796
+ *
1797
+ * When the input has an `execute` function, the return type narrows to
1798
+ * `ExecutableTool<Tool<...>>` so that `.execute` is non-nullable without
1799
+ * needing `isExecutableTool` or a `!` assertion at the call site.
1771
1800
  */
1801
+ declare function tool<INPUT, OUTPUT, CONTEXT extends Context>(tool: Tool<INPUT, OUTPUT, CONTEXT> & {
1802
+ execute: ToolExecuteFunction<INPUT, OUTPUT, CONTEXT>;
1803
+ }): ExecutableTool<Tool<INPUT, OUTPUT, CONTEXT>>;
1772
1804
  declare function tool<INPUT, OUTPUT, CONTEXT extends Context>(tool: Tool<INPUT, OUTPUT, CONTEXT>): Tool<INPUT, OUTPUT, CONTEXT>;
1773
1805
  declare function tool<INPUT, CONTEXT extends Context>(tool: Tool<INPUT, never, CONTEXT>): Tool<INPUT, never, CONTEXT>;
1774
1806
  declare function tool<OUTPUT, CONTEXT extends Context>(tool: Tool<never, OUTPUT, CONTEXT>): Tool<never, OUTPUT, CONTEXT>;
@@ -2099,17 +2131,6 @@ declare function withUserAgentSuffix(headers: HeadersInit | Record<string, strin
2099
2131
 
2100
2132
  declare function withoutTrailingSlash(url: string | undefined): string | undefined;
2101
2133
 
2102
- /**
2103
- * A tool that is guaranteed to expose an execute function.
2104
- */
2105
- type ExecutableTool<TOOL extends Tool = Tool> = TOOL & {
2106
- execute: NonNullable<TOOL['execute']>;
2107
- };
2108
- /**
2109
- * Checks whether a tool exposes an execute function.
2110
- */
2111
- declare function isExecutableTool<TOOL extends Tool>(tool: TOOL | undefined): tool is ExecutableTool<TOOL>;
2112
-
2113
2134
  /**
2114
2135
  * Detects the `any` type so untyped tools can be treated as having no explicit
2115
2136
  * context type.
@@ -2261,4 +2282,4 @@ interface ToolResult<NAME extends string, INPUT, OUTPUT> {
2261
2282
  dynamic?: boolean;
2262
2283
  }
2263
2284
 
2264
- 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, 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 };
2285
+ export { type Arrayable, type AssistantContent, type AssistantModelMessage, type Context, type CustomPart, DEFAULT_MAX_DOWNLOAD_SIZE, type DataContent, DelayedPromise, DownloadError, type DynamicTool, type ExecutableTool, type SandboxProcess as Experimental_SandboxProcess, type SandboxSession as Experimental_SandboxSession, 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
@@ -876,7 +876,7 @@ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
876
876
  }
877
877
 
878
878
  // src/version.ts
879
- var VERSION = true ? "5.0.0-canary.43" : "0.0.0-test";
879
+ var VERSION = true ? "5.0.0-canary.45" : "0.0.0-test";
880
880
 
881
881
  // src/get-from-api.ts
882
882
  var getOriginalFetch = () => globalThis.fetch;
@@ -2148,16 +2148,18 @@ var parsePipelineDef = (def, refs) => {
2148
2148
  } else if (refs.pipeStrategy === "output") {
2149
2149
  return parseDef(def.out._def, refs);
2150
2150
  }
2151
- const a = parseDef(def.in._def, {
2151
+ const inputSchema = parseDef(def.in._def, {
2152
2152
  ...refs,
2153
2153
  currentPath: [...refs.currentPath, "allOf", "0"]
2154
2154
  });
2155
- const b = parseDef(def.out._def, {
2155
+ const outputSchema = parseDef(def.out._def, {
2156
2156
  ...refs,
2157
- currentPath: [...refs.currentPath, "allOf", a ? "1" : "0"]
2157
+ currentPath: [...refs.currentPath, "allOf", inputSchema ? "1" : "0"]
2158
2158
  });
2159
2159
  return {
2160
- allOf: [a, b].filter((x) => x !== void 0)
2160
+ allOf: [inputSchema, outputSchema].filter(
2161
+ (schema) => schema !== void 0
2162
+ )
2161
2163
  };
2162
2164
  };
2163
2165