@ai-sdk/provider-utils
Version:
75 lines (72 loc) • 2.38 kB
text/typescript
import type { FlexibleSchema } from './schema';
import type { Context } from './types/context';
import { tool, type ProviderExecutedTool, type Tool } from './types/tool';
import { withTelemetryRedaction } from './telemetry-redaction';
/**
* A provider-executed tool is a tool for which the provider executes the tool.
*/
export type ProviderExecutedToolFactory<
INPUT,
OUTPUT,
ARGS extends object,
CONTEXT extends Context = {},
> = (
options: ARGS & {
onInputStart?: Tool<INPUT, OUTPUT, CONTEXT>['onInputStart'];
onInputDelta?: Tool<INPUT, OUTPUT, CONTEXT>['onInputDelta'];
onInputAvailable?: Tool<INPUT, OUTPUT, CONTEXT>['onInputAvailable'];
},
) => ProviderExecutedTool<INPUT, OUTPUT, CONTEXT>;
export function createProviderExecutedToolFactory<
INPUT,
OUTPUT,
ARGS extends object,
CONTEXT extends Context = {},
>({
id,
inputSchema,
outputSchema,
supportsDeferredResults,
redactArgs,
}: {
id: `${string}.${string}`;
inputSchema: FlexibleSchema<INPUT>;
outputSchema: FlexibleSchema<OUTPUT>;
/**
* Whether this provider-executed tool supports deferred results.
*
* When true, the tool result may not be returned in the same turn as the
* tool call (e.g., when using programmatic tool calling where a server tool
* triggers a client-executed tool, and the server tool's result is deferred
* until the client tool is resolved).
*
* @default false
*/
supportsDeferredResults?: boolean;
/** Provider-owned telemetry view of the tool configuration. */
redactArgs?: (args: Record<string, unknown>) => Record<string, unknown>;
}): ProviderExecutedToolFactory<INPUT, OUTPUT, ARGS, CONTEXT> {
return ({
onInputStart,
onInputDelta,
onInputAvailable,
...args
}: ARGS & {
onInputStart?: Tool<INPUT, OUTPUT, CONTEXT>['onInputStart'];
onInputDelta?: Tool<INPUT, OUTPUT, CONTEXT>['onInputDelta'];
onInputAvailable?: Tool<INPUT, OUTPUT, CONTEXT>['onInputAvailable'];
}): ProviderExecutedTool<INPUT, OUTPUT, CONTEXT> =>
tool({
type: 'provider',
isProviderExecuted: true,
id,
args:
redactArgs == null ? args : withTelemetryRedaction(args, redactArgs),
inputSchema,
outputSchema,
onInputStart,
onInputDelta,
onInputAvailable,
supportsDeferredResults,
}) as ProviderExecutedTool<INPUT, OUTPUT, CONTEXT>;
}