|
| 1 | +"""LangGraph integration helpers. |
| 2 | +
|
| 3 | +These utilities convert StackOne tools into LangGraph prebuilt components. |
| 4 | +
|
| 5 | +Usage: |
| 6 | + from stackone_ai import StackOneToolSet |
| 7 | + from stackone_ai.integrations.langgraph import to_tool_node |
| 8 | +
|
| 9 | + toolset = StackOneToolSet() |
| 10 | + tools = toolset.get_tools("hris_*", account_id="...") |
| 11 | + node = to_tool_node(tools) # langgraph.prebuilt.ToolNode |
| 12 | +""" |
| 13 | + |
| 14 | +from __future__ import annotations |
| 15 | + |
| 16 | +from collections.abc import Sequence |
| 17 | +from typing import TYPE_CHECKING, Any |
| 18 | + |
| 19 | +from langchain_core.tools import BaseTool |
| 20 | + |
| 21 | +from stackone_ai.models import Tools |
| 22 | + |
| 23 | +if TYPE_CHECKING: # pragma: no cover - only for typing |
| 24 | + try: |
| 25 | + from langgraph.prebuilt import ToolNode |
| 26 | + except Exception: # pragma: no cover |
| 27 | + |
| 28 | + class ToolNode: # type: ignore[no-redef] |
| 29 | + pass |
| 30 | + |
| 31 | + |
| 32 | +def _ensure_langgraph() -> None: |
| 33 | + try: |
| 34 | + from langgraph import prebuilt as _ # noqa: F401 |
| 35 | + except Exception as e: # pragma: no cover |
| 36 | + raise ImportError( |
| 37 | + "LangGraph is not installed. Install with `pip install langgraph` or " |
| 38 | + "`pip install 'stackone-ai[examples]'`" |
| 39 | + ) from e |
| 40 | + |
| 41 | + |
| 42 | +def _to_langchain_tools(tools: Tools | Sequence[BaseTool]) -> Sequence[BaseTool]: |
| 43 | + if isinstance(tools, Tools): |
| 44 | + return tools.to_langchain() |
| 45 | + return tools |
| 46 | + |
| 47 | + |
| 48 | +def to_tool_node(tools: Tools | Sequence[BaseTool], **kwargs: Any) -> Any: |
| 49 | + """Create a LangGraph `ToolNode` from StackOne tools or LangChain tools. |
| 50 | +
|
| 51 | + Accepts either a `Tools` collection from this SDK or an existing sequence of |
| 52 | + LangChain `BaseTool` instances and returns a LangGraph `ToolNode` suitable |
| 53 | + for inclusion in a graph. |
| 54 | + """ |
| 55 | + _ensure_langgraph() |
| 56 | + from langgraph.prebuilt import ToolNode # local import with helpful error |
| 57 | + |
| 58 | + langchain_tools = _to_langchain_tools(tools) |
| 59 | + return ToolNode(langchain_tools, **kwargs) |
| 60 | + |
| 61 | + |
| 62 | +def to_tool_executor(tools: Tools | Sequence[BaseTool], **kwargs: Any) -> Any: |
| 63 | + """Create a LangGraph `ToolNode` from StackOne tools or LangChain tools. |
| 64 | +
|
| 65 | + Note: ToolExecutor has been deprecated in favor of ToolNode. |
| 66 | + This function now returns a ToolNode for compatibility. |
| 67 | + """ |
| 68 | + _ensure_langgraph() |
| 69 | + from langgraph.prebuilt import ToolNode # local import with helpful error |
| 70 | + |
| 71 | + langchain_tools = _to_langchain_tools(tools) |
| 72 | + return ToolNode(langchain_tools, **kwargs) |
| 73 | + |
| 74 | + |
| 75 | +def bind_model_with_tools(model: Any, tools: Tools | Sequence[BaseTool]) -> Any: |
| 76 | + """Bind tools to an LLM that supports LangChain's `.bind_tools()` API. |
| 77 | +
|
| 78 | + This is a tiny helper that converts a `Tools` collection to LangChain tools |
| 79 | + and calls `model.bind_tools(...)`. |
| 80 | + """ |
| 81 | + langchain_tools = _to_langchain_tools(tools) |
| 82 | + return model.bind_tools(langchain_tools) |
| 83 | + |
| 84 | + |
| 85 | +def create_react_agent(llm: Any, tools: Tools | Sequence[BaseTool], **kwargs: Any) -> Any: |
| 86 | + """Create a LangGraph ReAct agent using StackOne tools. |
| 87 | +
|
| 88 | + Thin wrapper around `langgraph.prebuilt.create_react_agent` that accepts a |
| 89 | + `Tools` collection from this SDK. |
| 90 | + """ |
| 91 | + _ensure_langgraph() |
| 92 | + from langgraph.prebuilt import create_react_agent as _create |
| 93 | + |
| 94 | + return _create(llm, _to_langchain_tools(tools), **kwargs) |
0 commit comments