From 0c929e62eb2aa2dc99892ca2f3cc13df8ff04e02 Mon Sep 17 00:00:00 2001 From: Nuno Campos Date: Mon, 1 Sep 2025 09:57:52 +0100 Subject: [PATCH] Rename to AgentJump --- libs/langgraph/langgraph/agent/__init__.py | 6 ++-- .../agent/middleware/human_in_the_loop.py | 36 ++++++++++--------- libs/langgraph/langgraph/agent/types.py | 6 ++-- libs/langgraph/tests/test_agent.py | 4 +-- 4 files changed, 28 insertions(+), 24 deletions(-) diff --git a/libs/langgraph/langgraph/agent/__init__.py b/libs/langgraph/langgraph/agent/__init__.py index 7b4c5dccf..b665dc26e 100644 --- a/libs/langgraph/langgraph/agent/__init__.py +++ b/libs/langgraph/langgraph/agent/__init__.py @@ -7,7 +7,7 @@ from langchain_core.messages import AIMessage, SystemMessage from langchain_core.tools import BaseTool from langgraph.agent.types import ( - AgentGoTo, + AgentJump, AgentMiddleware, AgentState, AgentUpdate, @@ -233,13 +233,13 @@ def _make_tools_to_model_edge( def _add_middleware_edge( graph: StateGraph, - method: Callable[[AgentState], AgentUpdate | AgentGoTo | None], + method: Callable[[AgentState], AgentUpdate | AgentJump | None], name: str, default_destination: str, model_destination: str, ) -> None: sig = signature(method) - uses_jump = sig.return_annotation is AgentGoTo or AgentGoTo in getattr( + uses_jump = sig.return_annotation is AgentJump or AgentJump in getattr( sig.return_annotation, "__args__", () ) diff --git a/libs/langgraph/langgraph/agent/middleware/human_in_the_loop.py b/libs/langgraph/langgraph/agent/middleware/human_in_the_loop.py index 08fc80596..927973441 100644 --- a/libs/langgraph/langgraph/agent/middleware/human_in_the_loop.py +++ b/libs/langgraph/langgraph/agent/middleware/human_in_the_loop.py @@ -1,25 +1,26 @@ -from langgraph.agent.types import AgentMiddleware, AgentState, AgentUpdate, AgentGoTo -from typing import Dict, Any, List, Optional, Union -from langgraph.types import interrupt +from langgraph.agent.types import AgentJump, AgentMiddleware, AgentState, AgentUpdate from langgraph.prebuilt.interrupt import ( - HumanInterruptConfig, ActionRequest, HumanInterrupt, + HumanInterruptConfig, HumanResponse, ) +from langgraph.types import interrupt + +ToolInterruptConfig = dict[str, HumanInterruptConfig] -ToolInterruptConfig = Dict[str, HumanInterruptConfig] class HumanInTheLoopMiddleware(AgentMiddleware): - - def __init__(self, - tool_configs: ToolInterruptConfig, - message_prefix: str = "Tool execution requires approval",): + def __init__( + self, + tool_configs: ToolInterruptConfig, + message_prefix: str = "Tool execution requires approval", + ): super().__init__() self.tool_configs = tool_configs self.message_prefix = message_prefix - def after_model(self, state: AgentState) -> AgentUpdate | AgentGoTo | None: + def after_model(self, state: AgentState) -> AgentUpdate | AgentJump | None: messages = state.messages if not messages: return @@ -52,7 +53,9 @@ class HumanInTheLoopMiddleware(AgentMiddleware): for tool_call in interrupt_tool_calls: tool_name = tool_call["name"] tool_args = tool_call["args"] - description = f"{self.message_prefix}\n\nTool: {tool_name}\nArgs: {tool_args}" + description = ( + f"{self.message_prefix}\n\nTool: {tool_name}\nArgs: {tool_args}" + ) tool_config = self.tool_configs[tool_name] request: HumanInterrupt = { @@ -65,7 +68,7 @@ class HumanInTheLoopMiddleware(AgentMiddleware): } requests.append(request) - responses: List[HumanResponse] = interrupt(requests) + responses: list[HumanResponse] = interrupt(requests) for i, response in enumerate(responses): tool_call = interrupt_tool_calls[i] @@ -85,11 +88,12 @@ class HumanInTheLoopMiddleware(AgentMiddleware): return {"goto": "__end__"} elif response["type"] == "response": # NOTE: does not work with multiple interrupts - tool_message = {"role": "tool", "tool_call_id": tool_call["id"], "content": response["args"]} - return { - "messages": [tool_message], - "goto": "model" + tool_message = { + "role": "tool", + "tool_call_id": tool_call["id"], + "content": response["args"], } + return {"messages": [tool_message], "goto": "model"} else: raise ValueError(f"Unknown response type: {response['type']}") diff --git a/libs/langgraph/langgraph/agent/types.py b/libs/langgraph/langgraph/agent/types.py index 8f828e953..a1ce5ce27 100644 --- a/libs/langgraph/langgraph/agent/types.py +++ b/libs/langgraph/langgraph/agent/types.py @@ -34,7 +34,7 @@ class AgentMiddleware: def __copy__(self) -> Self: return self.__class__(**self.__dict__) - def before_model(self, state: AgentState) -> AgentUpdate | AgentGoTo | None: + def before_model(self, state: AgentState) -> AgentUpdate | AgentJump | None: pass def modify_model_request( @@ -42,7 +42,7 @@ class AgentMiddleware: ) -> ModelRequest: return request - def after_model(self, state: AgentState) -> AgentUpdate | AgentGoTo | None: + def after_model(self, state: AgentState) -> AgentUpdate | AgentJump | None: pass @@ -51,7 +51,7 @@ class AgentUpdate(TypedDict, total=False): response: dict -class AgentGoTo(TypedDict, total=False): +class AgentJump(TypedDict, total=False): messages: Messages jump_to: JumpTo diff --git a/libs/langgraph/tests/test_agent.py b/libs/langgraph/tests/test_agent.py index 33bab2b27..4a2006fcb 100644 --- a/libs/langgraph/tests/test_agent.py +++ b/libs/langgraph/tests/test_agent.py @@ -2,7 +2,7 @@ from langchain_core.messages import AIMessage, ToolCall from syrupy import SnapshotAssertion from langgraph.agent import create_agent -from langgraph.agent.types import AgentGoTo, AgentMiddleware +from langgraph.agent.types import AgentJump, AgentMiddleware from langgraph.checkpoint.base import BaseCheckpointSaver from langgraph.checkpoint.memory import InMemorySaver from langgraph.constants import END @@ -258,7 +258,7 @@ def test_create_agent_jump( calls.append("NoopSeven.after_model") class NoopEight(AgentMiddleware): - def before_model(self, state) -> AgentGoTo: + def before_model(self, state) -> AgentJump: calls.append("NoopEight.before_model") return {"jump_to": END}