runAgent method

  1. @override
Future<DVAIAgentResult> runAgent(
  1. DVAIAgentRequest request
)
override

Fallback used when the provider has no native tool calling, or when the request names no registered tools: run the allowed tools up front and put their results in the prompt.

Implementation

@override
Future<DVAIAgentResult> runAgent(DVAIAgentRequest request) async {
  const registry = DVAIToolRegistry();
  final usedTools = <String>[];
  final data = <String, DVJsonValue>{};
  for (final toolName in request.tools) {
    if (!registry.contains(toolName)) continue;
    usedTools.add(toolName);
    data[toolName] = await registry.call(toolName, request.context);
  }

  final prompt = StringBuffer(agentPrompt(request));
  if (data.isNotEmpty) {
    prompt
      ..writeln()
      ..writeln()
      ..writeln('Tool results:')
      ..writeln(jsonEncode(DVJsonCodec.toJsonObject(data)));
  }

  return DVAIAgentResult(
    output: await chat(prompt.toString(), provider: providerName),
    data: Map<String, DVJsonValue>.unmodifiable(data),
    usedTools: List<String>.unmodifiable(usedTools),
  );
}