WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion packages/ai/src/generate-text/parse-tool-call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import { InvalidToolInputError } from '../error/invalid-tool-input-error';
import { NoSuchToolError } from '../error/no-such-tool-error';
import { ToolCallRepairError } from '../error/tool-call-repair-error';
import { TypedToolCall } from './tool-call';
import { DynamicToolCall, TypedToolCall } from './tool-call';
import { ToolCallRepairFunction } from './tool-call-repair-function';
import { ToolSet } from './tool-set';

Expand All @@ -27,6 +27,11 @@ export async function parseToolCall<TOOLS extends ToolSet>({
}): Promise<TypedToolCall<TOOLS>> {
try {
if (tools == null) {
// provider-executed dynamic tools are not part of our list of tools:
if (toolCall.providerExecuted && toolCall.dynamic) {
return await parseProviderExecutedDynamicToolCall(toolCall);
}

throw new NoSuchToolError({ toolName: toolCall.toolName });
}

Expand Down Expand Up @@ -90,6 +95,33 @@ export async function parseToolCall<TOOLS extends ToolSet>({
}
}

async function parseProviderExecutedDynamicToolCall(
toolCall: LanguageModelV2ToolCall,
): Promise<DynamicToolCall> {
const parseResult =
toolCall.input.trim() === ''
? { success: true as const, value: {} }
: await safeParseJSON({ text: toolCall.input });

if (parseResult.success === false) {
throw new InvalidToolInputError({
toolName: toolCall.toolName,
toolInput: toolCall.input,
cause: parseResult.error,
});
}

return {
type: 'tool-call',
toolCallId: toolCall.toolCallId,
toolName: toolCall.toolName,
input: parseResult.value,
providerExecuted: true,
dynamic: true,
providerMetadata: toolCall.providerMetadata,
};
}

async function doParseToolCall<TOOLS extends ToolSet>({
toolCall,
tools,
Expand All @@ -102,6 +134,11 @@ async function doParseToolCall<TOOLS extends ToolSet>({
const tool = tools[toolName];

if (tool == null) {
// provider-executed dynamic tools are not part of our list of tools:
if (toolCall.providerExecuted && toolCall.dynamic) {
return await parseProviderExecutedDynamicToolCall(toolCall);
}

throw new NoSuchToolError({
toolName: toolCall.toolName,
availableTools: Object.keys(tools),
Expand Down