@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/CHANGELOG.md +6 -0
- package/dist/index.d.ts +36 -31
- package/dist/index.js +10 -15
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/get-error-message.ts +1 -15
- package/src/types/executable-tool.ts +17 -0
- package/src/types/execute-tool.ts +22 -23
- package/src/types/index.ts +1 -0
package/package.json
CHANGED
package/src/get-error-message.ts
CHANGED
|
@@ -1,15 +1 @@
|
|
|
1
|
-
export
|
|
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 {
|
|
3
|
-
import {
|
|
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
|
|
9
|
+
* Executes a tool function and normalizes its results into a stream of outputs.
|
|
7
10
|
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
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
|
-
* @
|
|
17
|
-
* @
|
|
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
|
|
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<
|
|
25
|
-
|
|
22
|
+
export async function* executeTool<TOOL extends Tool>({
|
|
23
|
+
tool,
|
|
26
24
|
input,
|
|
27
25
|
options,
|
|
28
26
|
}: {
|
|
29
|
-
|
|
30
|
-
input:
|
|
31
|
-
options: ToolExecutionOptions<
|
|
27
|
+
tool: ExecutableTool<TOOL>;
|
|
28
|
+
input: InferToolInput<TOOL>;
|
|
29
|
+
options: ToolExecutionOptions<InferToolContext<TOOL>>;
|
|
32
30
|
}): AsyncGenerator<
|
|
33
|
-
|
|
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:
|
|
37
|
+
let lastOutput: InferToolOutput<TOOL> | undefined;
|
|
39
38
|
for await (const output of result) {
|
|
40
39
|
lastOutput = output;
|
|
41
40
|
yield { type: 'preliminary', output };
|
package/src/types/index.ts
CHANGED
|
@@ -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';
|