Skip to content

Commit f69411a

Browse files
authored
feat: report tool call result at the end of a tool call (#496)
1 parent 5d1055a commit f69411a

File tree

2 files changed

+58
-38
lines changed

2 files changed

+58
-38
lines changed

typescript-sdk/integrations/langgraph/python/ag_ui_langgraph/agent.py

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
ToolCallArgsEvent,
5353
ToolCallEndEvent,
5454
ToolCallStartEvent,
55+
ToolCallResultEvent,
5556
ThinkingTextMessageStartEvent,
5657
ThinkingTextMessageContentEvent,
5758
ThinkingTextMessageEndEvent,
@@ -674,31 +675,40 @@ async def _handle_single_event(self, event: Any, state: State) -> AsyncGenerator
674675
)
675676

676677
elif event_type == LangGraphEventTypes.OnToolEnd:
677-
if self.active_run["has_function_streaming"]:
678-
return
679678
tool_call_output = event["data"]["output"]
680-
yield self._dispatch_event(
681-
ToolCallStartEvent(
682-
type=EventType.TOOL_CALL_START,
683-
tool_call_id=tool_call_output.tool_call_id,
684-
tool_call_name=tool_call_output.name,
685-
parent_message_id=tool_call_output.id,
686-
raw_event=event,
679+
if not self.active_run["has_function_streaming"]:
680+
yield self._dispatch_event(
681+
ToolCallStartEvent(
682+
type=EventType.TOOL_CALL_START,
683+
tool_call_id=tool_call_output.tool_call_id,
684+
tool_call_name=tool_call_output.name,
685+
parent_message_id=tool_call_output.id,
686+
raw_event=event,
687+
)
687688
)
688-
)
689-
yield self._dispatch_event(
690-
ToolCallArgsEvent(
691-
type=EventType.TOOL_CALL_ARGS,
692-
tool_call_id=tool_call_output.tool_call_id,
693-
delta=json.dumps(event["data"]["input"]),
694-
raw_event=event
689+
yield self._dispatch_event(
690+
ToolCallArgsEvent(
691+
type=EventType.TOOL_CALL_ARGS,
692+
tool_call_id=tool_call_output.tool_call_id,
693+
delta=json.dumps(event["data"]["input"]),
694+
raw_event=event
695+
)
695696
)
696-
)
697+
yield self._dispatch_event(
698+
ToolCallEndEvent(
699+
type=EventType.TOOL_CALL_END,
700+
tool_call_id=tool_call_output.tool_call_id,
701+
raw_event=event
702+
)
703+
)
704+
697705
yield self._dispatch_event(
698-
ToolCallEndEvent(
699-
type=EventType.TOOL_CALL_END,
706+
ToolCallResultEvent(
707+
type=EventType.TOOL_CALL_RESULT,
700708
tool_call_id=tool_call_output.tool_call_id,
701-
raw_event=event
709+
message_id=uuid.uuid4(),
710+
content=tool_call_output.content,
711+
role="tool"
702712
)
703713
)
704714

typescript-sdk/integrations/langgraph/src/agent.ts

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ import {
4848
ToolCallArgsEvent,
4949
ToolCallEndEvent,
5050
ToolCallStartEvent,
51+
ToolCallResultEvent,
5152
ThinkingTextMessageStartEvent,
5253
ThinkingTextMessageContentEvent,
5354
ThinkingTextMessageEndEvent,
5455
ThinkingStartEvent,
5556
ThinkingEndEvent,
56-
Message as AGUIMessage,
5757
} from "@ag-ui/client";
5858
import { RunsStreamPayload } from "@langchain/langgraph-sdk/dist/types";
5959
import {
@@ -76,6 +76,7 @@ export type ProcessedEvents =
7676
| ToolCallStartEvent
7777
| ToolCallArgsEvent
7878
| ToolCallEndEvent
79+
| ToolCallResultEvent
7980
| ThinkingStartEvent
8081
| ThinkingEndEvent
8182
| StateSnapshotEvent
@@ -774,26 +775,35 @@ export class LangGraphAgent extends AbstractAgent {
774775
});
775776
break;
776777
case LangGraphEventTypes.OnToolEnd:
777-
if (this.activeRun!.hasFunctionStreaming) break;
778-
const toolCallOutput = event.data.output
778+
const toolCallOutput = event.data?.output
779+
if (!this.activeRun!.hasFunctionStreaming) {
780+
this.dispatchEvent({
781+
type: EventType.TOOL_CALL_START,
782+
toolCallId: toolCallOutput.tool_call_id,
783+
toolCallName: toolCallOutput.name,
784+
parentMessageId: toolCallOutput.id,
785+
rawEvent: event,
786+
})
787+
this.dispatchEvent({
788+
type: EventType.TOOL_CALL_ARGS,
789+
toolCallId: toolCallOutput.tool_call_id,
790+
delta: JSON.stringify(event.data.input),
791+
rawEvent: event,
792+
});
793+
this.dispatchEvent({
794+
type: EventType.TOOL_CALL_END,
795+
toolCallId: toolCallOutput.tool_call_id,
796+
rawEvent: event,
797+
});
798+
}
779799
this.dispatchEvent({
780-
type: EventType.TOOL_CALL_START,
800+
type: EventType.TOOL_CALL_RESULT,
781801
toolCallId: toolCallOutput.tool_call_id,
782-
toolCallName: toolCallOutput.name,
783-
parentMessageId: toolCallOutput.id,
784-
rawEvent: event,
802+
content: toolCallOutput?.content,
803+
messageId: randomUUID(),
804+
role: "tool",
785805
})
786-
this.dispatchEvent({
787-
type: EventType.TOOL_CALL_ARGS,
788-
toolCallId: toolCallOutput.tool_call_id,
789-
delta: JSON.stringify(event.data.input),
790-
rawEvent: event,
791-
});
792-
this.dispatchEvent({
793-
type: EventType.TOOL_CALL_END,
794-
toolCallId: toolCallOutput.tool_call_id,
795-
rawEvent: event,
796-
});
806+
break;
797807
}
798808
}
799809

0 commit comments

Comments
 (0)