@ai-sdk/provider-utils 5.0.0-beta.20 → 5.0.0-beta.22

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-sdk/provider-utils
2
2
 
3
+ ## 5.0.0-beta.22
4
+
5
+ ### Patch Changes
6
+
7
+ - 083947b: feat(ai): separate toolsContext from context
8
+
9
+ ## 5.0.0-beta.21
10
+
11
+ ### Patch Changes
12
+
13
+ - add1126: refactoring: executeTool uses tool as parameter
14
+
3
15
  ## 5.0.0-beta.20
4
16
 
5
17
  ### Patch Changes
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { ImageModelV4File, LanguageModelV4FunctionTool, LanguageModelV4ProviderTool, AISDKError, JSONSchema7, JSONParseError, TypeValidationError, JSONValue, APICallError, LanguageModelV4Prompt, LanguageModelV4FilePart, SharedV4ProviderReference, LanguageModelV4CallOptions, SharedV4Warning, SharedV4ProviderOptions, JSONObject, TypeValidationContext } from '@ai-sdk/provider';
2
+ export { getErrorMessage } from '@ai-sdk/provider';
2
3
  import { StandardSchemaV1, StandardJSONSchemaV1 } from '@standard-schema/spec';
3
4
  export * from '@standard-schema/spec';
4
5
  import * as z3 from 'zod/v3';
@@ -199,8 +200,6 @@ type IdGenerator = () => string;
199
200
  */
200
201
  declare const generateId: IdGenerator;
201
202
 
202
- declare function getErrorMessage(error: unknown | undefined): string;
203
-
204
203
  /**
205
204
  * Used to mark schemas so we can support both Zod and custom schemas.
206
205
  */
@@ -1549,35 +1548,15 @@ declare function withUserAgentSuffix(headers: HeadersInit | Record<string, strin
1549
1548
  declare function withoutTrailingSlash(url: string | undefined): string | undefined;
1550
1549
 
1551
1550
  /**
1552
- * Executes a tool function, supporting both synchronous and streaming/asynchronous results.
1553
- *
1554
- * This generator yields intermediate ("preliminary") outputs as they're produced, allowing callers
1555
- * to stream partial tool results before completion. When execution is finished, it yields a final output,
1556
- * ensuring all consumers receive a conclusive result.
1557
- *
1558
- * - If the tool's `execute` function returns an `AsyncIterable`, all intermediate values are yielded
1559
- * as `{ type: "preliminary", output }` except the last, which is yielded as `{ type: "final", output }`.
1560
- * - If the tool returns a direct value or Promise, a single `{ type: "final", output }` is yielded.
1561
- *
1562
- * @template INPUT Input type for the tool execution.
1563
- * @template OUTPUT Output type for the tool execution.
1564
- * @template CONTEXT Context object extension for execution (extends Context).
1565
- * @param params.execute The tool execute function.
1566
- * @param params.input Input value to pass to the execute function.
1567
- * @param params.options Additional options for tool execution.
1568
- * @yields An object containing either a preliminary or final output from the tool.
1551
+ * A tool that is guaranteed to expose an execute function.
1569
1552
  */
1570
- declare function executeTool<INPUT, OUTPUT, CONTEXT extends Context>({ execute, input, options, }: {
1571
- execute: ToolExecuteFunction<INPUT, OUTPUT, CONTEXT>;
1572
- input: INPUT;
1573
- options: ToolExecutionOptions<NoInfer<CONTEXT>>;
1574
- }): AsyncGenerator<{
1575
- type: 'preliminary';
1576
- output: OUTPUT;
1577
- } | {
1578
- type: 'final';
1579
- output: OUTPUT;
1580
- }>;
1553
+ type ExecutableTool<TOOL extends Tool = Tool> = TOOL & {
1554
+ execute: NonNullable<TOOL['execute']>;
1555
+ };
1556
+ /**
1557
+ * Checks whether a tool exposes an execute function.
1558
+ */
1559
+ declare function isExecutableTool<TOOL extends Tool>(tool: TOOL | undefined): tool is ExecutableTool<TOOL>;
1581
1560
 
1582
1561
  /**
1583
1562
  * Infer the context type of a tool.
@@ -1595,35 +1574,46 @@ type InferToolInput<TOOL extends Tool<any, any, any>> = TOOL extends Tool<infer
1595
1574
  type InferToolOutput<TOOL extends Tool<any, any, any>> = TOOL extends Tool<any, infer OUTPUT, any> ? OUTPUT : never;
1596
1575
 
1597
1576
  /**
1598
- * A mapping of tool names to tool definitions.
1577
+ * Executes a tool function and normalizes its results into a stream of outputs.
1578
+ *
1579
+ * - If the tool's `execute` function returns an `AsyncIterable`, each yielded value is emitted as
1580
+ * `{ type: "preliminary", output }`. After iteration completes, the last yielded value is emitted
1581
+ * again as `{ type: "final", output }`.
1582
+ * - If the tool returns a direct value or Promise, a single `{ type: "final", output }` is yielded.
1583
+ *
1584
+ * @param params.tool The tool whose `execute` function should be invoked.
1585
+ * @param params.input The input value to pass to the tool.
1586
+ * @param params.options Additional options for tool execution.
1587
+ * @yields A preliminary output for each streamed value, followed by a final output, or a single final
1588
+ * output for non-streaming tools.
1599
1589
  */
1600
- type ToolSet = Record<string, (Tool<never, never, any> | Tool<any, any, any> | Tool<any, never, any> | Tool<never, any, any>) & Pick<Tool<any, any, any>, 'execute' | 'onInputAvailable' | 'onInputStart' | 'onInputDelta' | 'needsApproval'>>;
1590
+ declare function executeTool<TOOL extends Tool>({ tool, input, options, }: {
1591
+ tool: ExecutableTool<TOOL>;
1592
+ input: InferToolInput<TOOL>;
1593
+ options: ToolExecutionOptions<InferToolContext<TOOL>>;
1594
+ }): AsyncGenerator<{
1595
+ type: 'preliminary';
1596
+ output: InferToolOutput<TOOL>;
1597
+ } | {
1598
+ type: 'final';
1599
+ output: InferToolOutput<TOOL>;
1600
+ }>;
1601
1601
 
1602
1602
  /**
1603
- * Converts a union type `U` into an intersection type.
1604
- *
1605
- * For example:
1606
- * type A = { a: number };
1607
- * type B = { b: string };
1608
- * type Union = A | B;
1609
- * type Intersection = UnionToIntersection<Union>;
1610
- * // Intersection is: { a: number } & { b: string }
1611
- *
1612
- * This is useful when you have a union of object types and need a type with all possible properties.
1603
+ * A mapping of tool names to tool definitions.
1613
1604
  */
1614
- type UnionToIntersection<U> = (U extends unknown ? (arg: U) => void : never) extends (arg: infer I) => void ? I : never;
1605
+ type ToolSet = Record<string, (Tool<never, never, any> | Tool<any, any, any> | Tool<any, never, any> | Tool<never, any, any>) & Pick<Tool<any, any, any>, 'execute' | 'onInputAvailable' | 'onInputStart' | 'onInputDelta' | 'needsApproval'>>;
1615
1606
 
1616
1607
  /**
1617
1608
  * Infer the context type for a tool set.
1618
1609
  *
1619
- * The inferred type contains all properties required by the contexts of the
1620
- * tools in the set.
1610
+ * The inferred type maps each tool name to its required context type.
1621
1611
  *
1622
- * If there are incompatible properties, they will be of type `never`.
1612
+ * Tools without required context properties are omitted from the result.
1623
1613
  */
1624
- type InferToolSetContext<TOOLS extends ToolSet> = UnionToIntersection<{
1625
- [K in keyof TOOLS]: InferToolContext<NoInfer<TOOLS[K]>>;
1626
- }[keyof TOOLS]>;
1614
+ type InferToolSetContext<TOOLS extends ToolSet> = {
1615
+ [K in keyof TOOLS as InferToolContext<NoInfer<TOOLS[K]>> extends never ? never : K]: InferToolContext<NoInfer<TOOLS[K]>>;
1616
+ };
1627
1617
 
1628
1618
  /**
1629
1619
  * Typed tool call that is returned by generateText and streamText.
@@ -1684,4 +1674,4 @@ interface ToolResult<NAME extends string, INPUT, OUTPUT> {
1684
1674
  dynamic?: boolean;
1685
1675
  }
1686
1676
 
1687
- export { type AssistantContent, type AssistantModelMessage, type Context, type CustomPart, DEFAULT_MAX_DOWNLOAD_SIZE, type DataContent, DelayedPromise, DownloadError, type FetchFunction, type FilePart, type FlexibleSchema, 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 ProviderOptions, type ProviderReference, type ProviderToolFactory, type ProviderToolFactoryWithOutputSchema, type ReasoningFilePart, type ReasoningPart, type Resolvable, type ResponseHandler, type Schema, 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, asSchema, combineHeaders, convertAsyncIteratorToReadableStream, convertBase64ToUint8Array, convertImageModelFileToDataUri, convertToBase64, convertToFormData, convertUint8ArrayToBase64, createBinaryResponseHandler, createEventSourceResponseHandler, createIdGenerator, createJsonErrorResponseHandler, createJsonResponseHandler, createProviderToolFactory, createProviderToolFactoryWithOutputSchema, createStatusCodeErrorResponseHandler, createToolNameMapping, delay, downloadBlob, dynamicTool, executeTool, extractResponseHeaders, generateId, getErrorMessage, getFromApi, getRuntimeEnvironmentUserAgent, injectJsonInstructionIntoMessages, isAbortError, isCustomReasoning, isNonNullable, isParsableJson, isProviderReference, isUrlSupported, jsonSchema, lazySchema, loadApiKey, loadOptionalSetting, loadSetting, mapReasoningToProviderBudget, mapReasoningToProviderEffort, mediaTypeToExtension, normalizeHeaders, parseJSON, parseJsonEventStream, parseProviderOptions, postFormDataToApi, postJsonToApi, postToApi, readResponseWithSizeLimit, removeUndefinedEntries, resolve, resolveProviderReference, safeParseJSON, safeValidateTypes, serializeModelOptions, stripFileExtension, tool, validateDownloadUrl, validateTypes, withUserAgentSuffix, withoutTrailingSlash, zodSchema };
1677
+ export { type AssistantContent, type AssistantModelMessage, type Context, type CustomPart, DEFAULT_MAX_DOWNLOAD_SIZE, type DataContent, DelayedPromise, DownloadError, type ExecutableTool, type FetchFunction, type FilePart, type FlexibleSchema, 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 ProviderOptions, type ProviderReference, type ProviderToolFactory, type ProviderToolFactoryWithOutputSchema, type ReasoningFilePart, type ReasoningPart, type Resolvable, type ResponseHandler, type Schema, 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, asSchema, combineHeaders, convertAsyncIteratorToReadableStream, convertBase64ToUint8Array, convertImageModelFileToDataUri, convertToBase64, convertToFormData, convertUint8ArrayToBase64, createBinaryResponseHandler, createEventSourceResponseHandler, createIdGenerator, createJsonErrorResponseHandler, createJsonResponseHandler, createProviderToolFactory, createProviderToolFactoryWithOutputSchema, createStatusCodeErrorResponseHandler, createToolNameMapping, delay, downloadBlob, dynamicTool, executeTool, extractResponseHeaders, generateId, getFromApi, getRuntimeEnvironmentUserAgent, injectJsonInstructionIntoMessages, isAbortError, isCustomReasoning, isExecutableTool, isNonNullable, isParsableJson, isProviderReference, isUrlSupported, jsonSchema, lazySchema, loadApiKey, loadOptionalSetting, loadSetting, mapReasoningToProviderBudget, mapReasoningToProviderEffort, mediaTypeToExtension, normalizeHeaders, parseJSON, parseJsonEventStream, parseProviderOptions, postFormDataToApi, postJsonToApi, postToApi, readResponseWithSizeLimit, removeUndefinedEntries, resolve, resolveProviderReference, safeParseJSON, safeValidateTypes, serializeModelOptions, stripFileExtension, tool, validateDownloadUrl, validateTypes, withUserAgentSuffix, withoutTrailingSlash, zodSchema };
package/dist/index.js CHANGED
@@ -445,18 +445,7 @@ var createIdGenerator = ({
445
445
  var generateId = createIdGenerator();
446
446
 
447
447
  // src/get-error-message.ts
448
- function getErrorMessage(error) {
449
- if (error == null) {
450
- return "unknown error";
451
- }
452
- if (typeof error === "string") {
453
- return error;
454
- }
455
- if (error instanceof Error) {
456
- return error.toString();
457
- }
458
- return JSON.stringify(error);
459
- }
448
+ import { getErrorMessage } from "@ai-sdk/provider";
460
449
 
461
450
  // src/get-from-api.ts
462
451
  import { APICallError as APICallError2 } from "@ai-sdk/provider";
@@ -577,7 +566,7 @@ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
577
566
  }
578
567
 
579
568
  // src/version.ts
580
- var VERSION = true ? "5.0.0-beta.20" : "0.0.0-test";
569
+ var VERSION = true ? "5.0.0-beta.22" : "0.0.0-test";
581
570
 
582
571
  // src/get-from-api.ts
583
572
  var getOriginalFetch = () => globalThis.fetch;
@@ -2794,11 +2783,11 @@ function isAsyncIterable(obj) {
2794
2783
 
2795
2784
  // src/types/execute-tool.ts
2796
2785
  async function* executeTool({
2797
- execute,
2786
+ tool: tool2,
2798
2787
  input,
2799
2788
  options
2800
2789
  }) {
2801
- const result = execute(input, options);
2790
+ const result = tool2.execute(input, options);
2802
2791
  if (isAsyncIterable(result)) {
2803
2792
  let lastOutput;
2804
2793
  for await (const output of result) {
@@ -2811,6 +2800,11 @@ async function* executeTool({
2811
2800
  }
2812
2801
  }
2813
2802
 
2803
+ // src/types/executable-tool.ts
2804
+ function isExecutableTool(tool2) {
2805
+ return tool2 != null && typeof tool2.execute === "function";
2806
+ }
2807
+
2814
2808
  // src/index.ts
2815
2809
  import { WORKFLOW_DESERIALIZE, WORKFLOW_SERIALIZE } from "@workflow/serde";
2816
2810
  import {
@@ -2853,6 +2847,7 @@ export {
2853
2847
  injectJsonInstructionIntoMessages,
2854
2848
  isAbortError,
2855
2849
  isCustomReasoning,
2850
+ isExecutableTool,
2856
2851
  isNonNullable,
2857
2852
  isParsableJson,
2858
2853
  isProviderReference,