Skip to content

Commit f29799e

Browse files
feat: Add toolcall callbacks to agent workflows (#2137)
1 parent 9bca306 commit f29799e

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

.changeset/floppy-kiwis-play.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@llamaindex/workflow": patch
3+
"@llamaindex/core": patch
4+
---
5+
6+
Add toolcall callbacks to agent workflows

packages/core/src/agent/utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type {
1515
} from "../llms";
1616
import { baseToolWithCallSchema } from "../schema";
1717
import {
18+
assertIsJSONValue,
1819
isAsyncIterable,
1920
prettifyError,
2021
stringifyJSONToMessageContent,
@@ -227,6 +228,7 @@ export async function callTool(
227228
`Tool ${tool.metadata.name} (remote:${toolCall.name}) succeeded.`,
228229
);
229230
logger.log(`Output: ${JSON.stringify(output)}`);
231+
assertIsJSONValue(output);
230232
const toolOutput: ToolOutput = {
231233
tool,
232234
input,

packages/workflow/src/agent/agent-workflow.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1+
import { callTool } from "@llamaindex/core/agent";
12
import type { JSONValue } from "@llamaindex/core/global";
23
import type { ChatMessage, MessageContent } from "@llamaindex/core/llms";
34
import { createMemory, Memory } from "@llamaindex/core/memory";
45
import { PromptTemplate } from "@llamaindex/core/prompts";
56
import { tool } from "@llamaindex/core/tools";
6-
import {
7-
assertIsJSONValue,
8-
stringifyJSONToMessageContent,
9-
} from "@llamaindex/core/utils";
7+
import { stringifyJSONToMessageContent } from "@llamaindex/core/utils";
108
import { consoleLogger, emptyLogger, type Logger } from "@llamaindex/env";
119
import {
1210
createWorkflow,
@@ -600,12 +598,22 @@ export class AgentWorkflow implements Workflow {
600598
const tool = this.agents
601599
.get(toolCall.agentName)
602600
?.tools.find((t) => t.metadata.name === toolCall.toolName);
603-
if (!tool) {
604-
throw new Error(`Tool ${toolCall.toolName} not found`);
601+
602+
const toolOutput = await callTool(
603+
tool,
604+
{
605+
name: toolCall.toolName,
606+
input: toolCall.toolKwargs,
607+
id: toolCall.toolId,
608+
},
609+
this.logger,
610+
);
611+
612+
if (toolOutput.isError) {
613+
throw new Error(String(toolOutput.output));
605614
}
606-
const output = await tool.call(toolCall.toolKwargs);
607-
assertIsJSONValue(output);
608-
return output;
615+
616+
return toolOutput.output;
609617
}
610618

611619
private createInitialState(): AgentWorkflowState {

0 commit comments

Comments
 (0)