* Process agent result and execute tools
({
runAgentResult,
agent,
}: {
runAgentResult: RunAgentResult;
agent: AbstractAgent;
})
| 408 | * Process agent result and execute tools |
| 409 | */ |
| 410 | private async processAgentResult({ |
| 411 | runAgentResult, |
| 412 | agent, |
| 413 | }: { |
| 414 | runAgentResult: RunAgentResult; |
| 415 | agent: AbstractAgent; |
| 416 | }): Promise<RunAgentResult> { |
| 417 | const { newMessages } = runAgentResult; |
| 418 | // Agent ID is guaranteed to be set by validateAndAssignAgentId |
| 419 | const agentId = agent.agentId!; |
| 420 | |
| 421 | let needsFollowUp = false; |
| 422 | |
| 423 | for (const message of newMessages) { |
| 424 | if (message.role === "assistant") { |
| 425 | for (const toolCall of message.toolCalls || []) { |
| 426 | if ( |
| 427 | newMessages.findIndex( |
| 428 | (m) => m.role === "tool" && m.toolCallId === toolCall.id, |
| 429 | ) === -1 |
| 430 | ) { |
| 431 | const tool = this.getTool({ |
| 432 | toolName: toolCall.function.name, |
| 433 | agentId: agent.agentId, |
| 434 | }); |
| 435 | if (tool) { |
| 436 | const followUp = await this.executeSpecificTool( |
| 437 | tool, |
| 438 | toolCall, |
| 439 | message, |
| 440 | agent, |
| 441 | agentId, |
| 442 | ); |
| 443 | if (followUp) { |
| 444 | needsFollowUp = true; |
| 445 | } |
| 446 | } else { |
| 447 | // Wildcard fallback for undefined tools |
| 448 | const wildcardTool = this.getTool({ |
| 449 | toolName: "*", |
| 450 | agentId: agent.agentId, |
| 451 | }); |
| 452 | if (wildcardTool) { |
| 453 | const followUp = await this.executeWildcardTool( |
| 454 | wildcardTool, |
| 455 | toolCall, |
| 456 | message, |
| 457 | agent, |
| 458 | agentId, |
| 459 | ); |
| 460 | if (followUp) { |
| 461 | needsFollowUp = true; |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 | } |
no test coverage detected