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

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.0-beta.20",
3
+ "version": "5.0.0-beta.21",
4
4
  "type": "module",
5
5
  "license": "Apache-2.0",
6
6
  "sideEffects": false,
@@ -1,15 +1 @@
1
- export function getErrorMessage(error: unknown | undefined) {
2
- if (error == null) {
3
- return 'unknown error';
4
- }
5
-
6
- if (typeof error === 'string') {
7
- return error;
8
- }
9
-
10
- if (error instanceof Error) {
11
- return error.toString();
12
- }
13
-
14
- return JSON.stringify(error);
15
- }
1
+ export { getErrorMessage } from '@ai-sdk/provider';
@@ -0,0 +1,17 @@
1
+ import { Tool } from './tool';
2
+
3
+ /**
4
+ * A tool that is guaranteed to expose an execute function.
5
+ */
6
+ export type ExecutableTool<TOOL extends Tool = Tool> = TOOL & {
7
+ execute: NonNullable<TOOL['execute']>;
8
+ };
9
+
10
+ /**
11
+ * Checks whether a tool exposes an execute function.
12
+ */
13
+ export function isExecutableTool<TOOL extends Tool>(
14
+ tool: TOOL | undefined,
15
+ ): tool is ExecutableTool<TOOL> {
16
+ return tool != null && typeof tool.execute === 'function';
17
+ }
@@ -1,41 +1,40 @@
1
1
  import { isAsyncIterable } from '../is-async-iterable';
2
- import { Context } from './context';
3
- import { ToolExecuteFunction, ToolExecutionOptions } from './tool';
2
+ import { ExecutableTool } from './executable-tool';
3
+ import { InferToolContext } from './infer-tool-context';
4
+ import { InferToolInput } from './infer-tool-input';
5
+ import { InferToolOutput } from './infer-tool-output';
6
+ import { Tool, ToolExecutionOptions } from './tool';
4
7
 
5
8
  /**
6
- * Executes a tool function, supporting both synchronous and streaming/asynchronous results.
9
+ * Executes a tool function and normalizes its results into a stream of outputs.
7
10
  *
8
- * This generator yields intermediate ("preliminary") outputs as they're produced, allowing callers
9
- * to stream partial tool results before completion. When execution is finished, it yields a final output,
10
- * ensuring all consumers receive a conclusive result.
11
- *
12
- * - If the tool's `execute` function returns an `AsyncIterable`, all intermediate values are yielded
13
- * as `{ type: "preliminary", output }` except the last, which is yielded as `{ type: "final", output }`.
11
+ * - If the tool's `execute` function returns an `AsyncIterable`, each yielded value is emitted as
12
+ * `{ type: "preliminary", output }`. After iteration completes, the last yielded value is emitted
13
+ * again as `{ type: "final", output }`.
14
14
  * - If the tool returns a direct value or Promise, a single `{ type: "final", output }` is yielded.
15
15
  *
16
- * @template INPUT Input type for the tool execution.
17
- * @template OUTPUT Output type for the tool execution.
18
- * @template CONTEXT Context object extension for execution (extends Context).
19
- * @param params.execute The tool execute function.
20
- * @param params.input Input value to pass to the execute function.
16
+ * @param params.tool The tool whose `execute` function should be invoked.
17
+ * @param params.input The input value to pass to the tool.
21
18
  * @param params.options Additional options for tool execution.
22
- * @yields An object containing either a preliminary or final output from the tool.
19
+ * @yields A preliminary output for each streamed value, followed by a final output, or a single final
20
+ * output for non-streaming tools.
23
21
  */
24
- export async function* executeTool<INPUT, OUTPUT, CONTEXT extends Context>({
25
- execute,
22
+ export async function* executeTool<TOOL extends Tool>({
23
+ tool,
26
24
  input,
27
25
  options,
28
26
  }: {
29
- execute: ToolExecuteFunction<INPUT, OUTPUT, CONTEXT>;
30
- input: INPUT;
31
- options: ToolExecutionOptions<NoInfer<CONTEXT>>;
27
+ tool: ExecutableTool<TOOL>;
28
+ input: InferToolInput<TOOL>;
29
+ options: ToolExecutionOptions<InferToolContext<TOOL>>;
32
30
  }): AsyncGenerator<
33
- { type: 'preliminary'; output: OUTPUT } | { type: 'final'; output: OUTPUT }
31
+ | { type: 'preliminary'; output: InferToolOutput<TOOL> }
32
+ | { type: 'final'; output: InferToolOutput<TOOL> }
34
33
  > {
35
- const result = execute(input, options);
34
+ const result = tool.execute(input, options);
36
35
 
37
36
  if (isAsyncIterable(result)) {
38
- let lastOutput: OUTPUT | undefined;
37
+ let lastOutput: InferToolOutput<TOOL> | undefined;
39
38
  for await (const output of result) {
40
39
  lastOutput = output;
41
40
  yield { type: 'preliminary', output };
@@ -16,6 +16,7 @@ export type {
16
16
  export type { Context } from './context';
17
17
  export type { DataContent } from './data-content';
18
18
  export { executeTool } from './execute-tool';
19
+ export { isExecutableTool, type ExecutableTool } from './executable-tool';
19
20
  export type { InferToolContext } from './infer-tool-context';
20
21
  export type { InferToolInput } from './infer-tool-input';
21
22
  export type { InferToolOutput } from './infer-tool-output';