"""DeepAgents integration — expose GOAP planning as a tool or subagent.
Two integration APIs for embedding LangGOAP inside a DeepAgents agent:
* :func:`create_goap_tool` — wraps a ``GoapGraph`` as a LangChain
:class:`~langchain_core.tools.BaseTool`. The main agent calls it via
the normal tool-calling flow.
* :func:`create_goap_subagent` — returns a
:class:`~deepagents.middleware.subagents.CompiledSubAgent`-compatible
dict whose ``runnable`` accepts a ``{messages: [HumanMessage(...)]}``
state and returns an ``{messages: [AIMessage(...)]}`` state after the
GOAP loop completes.
Both use :class:`~langgoap.interpreter.GoalInterpreter` to convert the
natural-language request to a :class:`~langgoap.goals.GoalSpec` before
running the planner.
No hard dependency on the ``deepagents`` package — everything is built
on ``langchain-core`` and ``langgraph`` primitives.
"""
from __future__ import annotations
from typing import Annotated, Any, TypedDict
from langchain_core.language_models import BaseChatModel
from langchain_core.messages import AIMessage, BaseMessage, HumanMessage
from langchain_core.runnables import RunnableLambda
from langchain_core.tools import StructuredTool
from langgraph.graph import StateGraph
from langgraph.graph.message import add_messages
from pydantic import BaseModel, Field
from langgoap.actions import ActionSpec
from langgoap.graph.builder import GoapGraph
__all__ = [
"create_goap_tool",
"create_goap_subagent",
"format_goap_result",
]
# ---------------------------------------------------------------------------
# Result formatting
# ---------------------------------------------------------------------------
#: Longest rendering of a single world-state value in a report.
_VALUE_CLIP = 120
#: Most world-state keys listed in a report.
_KEY_CAP = 40
def _clip(value: Any, limit: int = _VALUE_CLIP) -> str:
text = repr(value)
return text if len(text) <= limit else text[: limit - 1] + "…"
def _entry_field(entry: Any, name: str, default: Any = None) -> Any:
if isinstance(entry, dict):
return entry.get(name, default)
return getattr(entry, name, default)
# ---------------------------------------------------------------------------
# Tool input schema
# ---------------------------------------------------------------------------
class _GoapToolInput(BaseModel):
"""Input schema for the GOAP planning tool."""
request: str = Field(
description="Natural-language description of the goal to achieve."
)
# ---------------------------------------------------------------------------
# create_goap_tool
# ---------------------------------------------------------------------------
# ---------------------------------------------------------------------------
# create_goap_subagent (CompiledSubAgent-compatible dict)
# ---------------------------------------------------------------------------
class _BridgeState(TypedDict):
messages: Annotated[list[BaseMessage], add_messages]
[docs]
def create_goap_subagent(
actions: list[ActionSpec],
*,
llm: BaseChatModel,
name: str = "goap_planner",
description: str = "A GOAP (Goal-Oriented Action Planning) subagent that achieves goals described in natural language.",
world_state: dict[str, Any] | None = None,
graph_kwargs: dict[str, Any] | None = None,
) -> dict[str, Any]:
"""Create a ``CompiledSubAgent``-compatible dict for DeepAgents.
The returned dict has ``name``, ``description``, and ``runnable``
keys. The ``runnable`` is a compiled ``StateGraph`` whose state
includes a ``messages`` list — the DeepAgents subagent protocol.
The bridge graph has a single node that:
1. Extracts the task from the last ``HumanMessage``.
2. Runs ``GoapGraph.invoke_nl`` / ``ainvoke_nl``.
3. Returns an ``AIMessage`` with the formatted result.
Args:
actions: The action library for the planner.
llm: Chat model for natural-language goal interpretation.
name: Subagent name.
description: Subagent description for routing.
world_state: Default initial world state.
graph_kwargs: Extra keyword arguments forwarded to
:class:`~langgoap.graph.builder.GoapGraph`.
Returns:
A dict matching the ``CompiledSubAgent`` protocol:
``{"name": str, "description": str, "runnable": Runnable}``.
"""
gkw = dict(graph_kwargs or {})
graph = GoapGraph(actions=actions, **gkw)
default_ws = dict(world_state or {})
def _extract_task(messages: list[BaseMessage]) -> str:
"""Extract the task description from the last HumanMessage."""
for msg in reversed(messages):
if isinstance(msg, HumanMessage):
return str(msg.content)
return str(messages[-1].content) if messages else ""
def _goap_node(state: _BridgeState) -> dict[str, Any]:
task = _extract_task(state["messages"])
result = graph.invoke_nl(
task,
world_state=dict(default_ws),
llm=llm,
)
report = format_goap_result(dict(result))
return {"messages": [AIMessage(content=report)]}
async def _agoap_node(state: _BridgeState) -> dict[str, Any]:
task = _extract_task(state["messages"])
result = await graph.ainvoke_nl(
task,
world_state=dict(default_ws),
llm=llm,
)
report = format_goap_result(dict(result))
return {"messages": [AIMessage(content=report)]}
builder = StateGraph(_BridgeState)
builder.add_node(
"goap",
RunnableLambda(func=_goap_node, afunc=_agoap_node),
)
builder.set_entry_point("goap")
builder.set_finish_point("goap")
compiled = builder.compile()
return {
"name": name,
"description": description,
"runnable": compiled,
}