mirror of
https://github.com/langchain-ai/langgraph.git
synced 2026-08-23 08:02:23 +02:00
Rename to AgentJump
This commit is contained in:
@@ -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__", ()
|
||||
)
|
||||
|
||||
|
||||
@@ -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']}")
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user