|
6 | 6 | from langgraph.graph.state import CompiledStateGraph |
7 | 7 |
|
8 | 8 | try: |
9 | | - from langchain.schema import BaseMessage, SystemMessage |
| 9 | + from langchain.schema import BaseMessage, SystemMessage, ToolMessage |
10 | 10 | except ImportError: |
11 | 11 | # Langchain >= 1.0.0 |
12 | | - from langchain_core.messages import BaseMessage, SystemMessage |
| 12 | + from langchain_core.messages import BaseMessage, SystemMessage, ToolMessage |
13 | 13 |
|
14 | 14 | from langchain_core.runnables import RunnableConfig, ensure_config |
15 | 15 | from langchain_core.messages import HumanMessage |
@@ -682,6 +682,51 @@ async def _handle_single_event(self, event: Any, state: State) -> AsyncGenerator |
682 | 682 |
|
683 | 683 | elif event_type == LangGraphEventTypes.OnToolEnd: |
684 | 684 | tool_call_output = event["data"]["output"] |
| 685 | + |
| 686 | + if isinstance(tool_call_output, Command): |
| 687 | + # Extract ToolMessages from Command.update |
| 688 | + messages = tool_call_output.update.get('messages', []) |
| 689 | + tool_messages = [m for m in messages if isinstance(m, ToolMessage)] |
| 690 | + |
| 691 | + # Process each tool message |
| 692 | + for tool_msg in tool_messages: |
| 693 | + if not self.active_run["has_function_streaming"]: |
| 694 | + yield self._dispatch_event( |
| 695 | + ToolCallStartEvent( |
| 696 | + type=EventType.TOOL_CALL_START, |
| 697 | + tool_call_id=tool_msg.tool_call_id, |
| 698 | + tool_call_name=tool_msg.name, |
| 699 | + parent_message_id=tool_msg.id, |
| 700 | + raw_event=event, |
| 701 | + ) |
| 702 | + ) |
| 703 | + yield self._dispatch_event( |
| 704 | + ToolCallArgsEvent( |
| 705 | + type=EventType.TOOL_CALL_ARGS, |
| 706 | + tool_call_id=tool_msg.tool_call_id, |
| 707 | + delta=json.dumps(event["data"].get("input", {})), |
| 708 | + raw_event=event |
| 709 | + ) |
| 710 | + ) |
| 711 | + yield self._dispatch_event( |
| 712 | + ToolCallEndEvent( |
| 713 | + type=EventType.TOOL_CALL_END, |
| 714 | + tool_call_id=tool_msg.tool_call_id, |
| 715 | + raw_event=event |
| 716 | + ) |
| 717 | + ) |
| 718 | + |
| 719 | + yield self._dispatch_event( |
| 720 | + ToolCallResultEvent( |
| 721 | + type=EventType.TOOL_CALL_RESULT, |
| 722 | + tool_call_id=tool_msg.tool_call_id, |
| 723 | + message_id=str(uuid.uuid4()), |
| 724 | + content=tool_msg.content, |
| 725 | + role="tool" |
| 726 | + ) |
| 727 | + ) |
| 728 | + return |
| 729 | + |
685 | 730 | if not self.active_run["has_function_streaming"]: |
686 | 731 | yield self._dispatch_event( |
687 | 732 | ToolCallStartEvent( |
|
0 commit comments