call method
Calls the method behind tool with the agent's JSON arguments and
returns its result as JSON.
The agent list is read again first, because an earlier call of the same
run may have changed it. Throws a ToolCallRefused for a tool that is
not listed now, that needs approval when approved is false, and for
arguments of the wrong type or with a key the schema does not have. The
method runs in none of these cases.
Implementation
Future<Object?> call(
String tool,
Map<String, Object?> arguments, {
bool approved = false,
}) async {
final (target, capability) = switch ((
_tools[tool],
_currentCapabilityFor(tool),
)) {
(final target?, final capability?) => (target, capability),
_ => throw ToolCallRefused('The agent cannot call $tool now.'),
};
if (capability.needsApproval && !approved) {
throw ToolCallRefused('$tool runs only after the user approves it.');
}
_rejectUnknown(arguments, target.parameters, '');
return await target.invoke(ToolArgs(arguments));
}