feat: Add toolcall callbacks to agent workflows (#2137)

This commit is contained in:
Marcus Schiesser
2025-07-24 15:37:14 +08:00
committed by GitHub
parent 9bca30620b
commit f29799e385
3 changed files with 25 additions and 9 deletions
+6
View File
@@ -0,0 +1,6 @@
---
"@llamaindex/workflow": patch
"@llamaindex/core": patch
---
Add toolcall callbacks to agent workflows
+2
View File
@@ -15,6 +15,7 @@ import type {
} from "../llms";
import { baseToolWithCallSchema } from "../schema";
import {
assertIsJSONValue,
isAsyncIterable,
prettifyError,
stringifyJSONToMessageContent,
@@ -227,6 +228,7 @@ export async function callTool(
`Tool ${tool.metadata.name} (remote:${toolCall.name}) succeeded.`,
);
logger.log(`Output: ${JSON.stringify(output)}`);
assertIsJSONValue(output);
const toolOutput: ToolOutput = {
tool,
input,
+17 -9
View File
@@ -1,12 +1,10 @@
import { callTool } from "@llamaindex/core/agent";
import type { JSONValue } from "@llamaindex/core/global";
import type { ChatMessage, MessageContent } from "@llamaindex/core/llms";
import { createMemory, Memory } from "@llamaindex/core/memory";
import { PromptTemplate } from "@llamaindex/core/prompts";
import { tool } from "@llamaindex/core/tools";
import {
assertIsJSONValue,
stringifyJSONToMessageContent,
} from "@llamaindex/core/utils";
import { stringifyJSONToMessageContent } from "@llamaindex/core/utils";
import { consoleLogger, emptyLogger, type Logger } from "@llamaindex/env";
import {
createWorkflow,
@@ -600,12 +598,22 @@ export class AgentWorkflow implements Workflow {
const tool = this.agents
.get(toolCall.agentName)
?.tools.find((t) => t.metadata.name === toolCall.toolName);
if (!tool) {
throw new Error(`Tool ${toolCall.toolName} not found`);
const toolOutput = await callTool(
tool,
{
name: toolCall.toolName,
input: toolCall.toolKwargs,
id: toolCall.toolId,
},
this.logger,
);
if (toolOutput.isError) {
throw new Error(String(toolOutput.output));
}
const output = await tool.call(toolCall.toolKwargs);
assertIsJSONValue(output);
return output;
return toolOutput.output;
}
private createInitialState(): AgentWorkflowState {