"""LangGraph / LangChain adapter (scaffold). Wrap `Pipeline.run` as a LangGraph node or a LangChain tool. Safe to import even if LangGraph/LangChain aren't installed — the adapter is just a callable. """ from __future__ import annotations from typing import Any, Dict from ..core.config import Config from ..core.types import Query from ..pipeline import Pipeline _pipeline: Pipeline = Pipeline.from_config(Config.default()) def set_pipeline(pipe: Pipeline) -> None: global _pipeline _pipeline = pipe def tau_rag_node(state: Dict[str, Any]) -> Dict[str, Any]: """LangGraph node: reads `state['query']`, returns fields merged into state. Example: from langgraph.graph import StateGraph g = StateGraph(dict) g.add_node("rag", tau_rag_node) ... """ q_text: str = state.get("query", "") resp = _pipeline.run(Query(text=q_text, lang=state.get("lang", "he"))) return { **state, "answer": resp.answer, "sources": resp.sources, "omega": resp.signals.omega, "verification_passed": resp.verification.passed, "alerts": [a.__dict__ for a in resp.verification.alerts], } # ----------------------------------------------------------------------- LangChain def as_langchain_tool(): """Return a LangChain Tool if langchain is installed, else a ValueError. Usage: from tau_rag.api.langgraph_node import as_langchain_tool tool = as_langchain_tool() agent_executor = initialize_agent([tool], llm) """ try: from langchain.tools import Tool # type: ignore except Exception as e: raise RuntimeError( "LangChain not installed. `pip install langchain`." ) from e def _call(q: str) -> str: r = _pipeline.run(Query(text=q)) return r.answer return Tool( name="tau_rag", func=_call, description=( "Retrieval-augmented generation over the TAU-RAG knowledge base " "with structure-preservation guarantees." ), )