invictatill-ai / agents /langgraph_wrapper.py
CI Bot
deploy: clean deploy with LFS binary tracking
550cb8d
Raw
History Blame Contribute Delete
2.96 kB
"""
Stub LangGraph wrapper that routes workflow tasks through Brain._call_llm().
"""
from typing import Dict, Any, Tuple, Optional
def _build_messages(brain, query, context, system_prefix=""):
"""Build messages and call LLM directly, bypassing orchestrator to avoid loops."""
history = (context or {}).get("history", [])
user_profile = (context or {}).get("profile", {})
user_model = (context or {}).get("user_model")
profile_context = brain._format_profile(user_profile) if hasattr(brain, '_format_profile') else ""
context_snippets, sources, topic = brain._assemble_context(query) if hasattr(brain, '_assemble_context') else ([], [], query)
system_content = brain._build_system(profile_context, context_snippets, user_model) if hasattr(brain, '_build_system') else ""
if system_prefix:
system_content = system_prefix + "\n\n" + system_content
messages = [{"role": "system", "content": system_content}]
if history:
formatted = brain._format_history(history) if hasattr(brain, '_format_history') else []
messages.extend(formatted)
messages.append({"role": "user", "content": str(query)})
return messages
class LangGraphWrapper:
"""Stub: routes workflow tasks through Brain._call_llm()."""
def __init__(self, brain=None):
self.brain = brain
def run(self, query: str, context: Dict[str, Any] = None) -> Optional[str]:
if not self.brain:
return f"[LangGraph stub] Workflow: {query[:100]}..."
prefix = (
"You are an expert systems architect. Break down the following workflow "
"into clear, numbered steps. Provide the implementation or configuration "
"for each step.\n\n"
)
messages = _build_messages(self.brain, query, context, prefix)
try:
response = self.brain._call_llm(messages, stream=False)
return response.choices[0].message.content
except Exception as e:
return f"[LangGraph error: {e}]"
def run_stream(self, query: str, context: Dict[str, Any] = None):
if not self.brain:
yield f"[LangGraph stub] Workflow: {query[:100]}...", None
return
prefix = (
"You are an expert systems architect. Break down the following workflow "
"into clear, numbered steps. Provide the implementation or configuration "
"for each step.\n\n"
)
messages = _build_messages(self.brain, query, context, prefix)
try:
stream = self.brain._call_llm(messages, stream=True)
for chunk in stream:
if hasattr(chunk, 'choices') and chunk.choices:
delta = chunk.choices[0].delta
if delta and delta.content:
yield delta.content, None
yield "", None
except Exception as e:
yield f"[LangGraph error: {e}]", None