feat: implement configurable Xingbang risk agent on mini-openclaw core

This commit is contained in:
NathanAlex7
2026-09-08 01:08:34 +08:00
parent f2439ee5a3
commit 58fb2854dc
26 changed files with 1990 additions and 208 deletions
+27 -7
View File
@@ -46,20 +46,40 @@ class AgentManager:
def _build_agent(self):
"""Build a fresh agent with current system prompt (re-reads files each time)."""
from langchain.agents import create_agent
assert self._base_dir is not None
assert self._llm is not None
rag_mode = get_rag_mode()
system_prompt = build_system_prompt(self._base_dir, rag_mode=rag_mode)
agent = create_agent(
model=self._llm,
tools=self._tools,
system_prompt=system_prompt,
return self.build_task_agent(self._llm, self._tools, system_prompt)
def build_task_agent(self, model, tools: list, system_prompt: str):
"""Shared core factory. Scoped tasks supply explicit model, tools and prompt."""
from langchain.agents import create_agent
return create_agent(model=model, tools=tools, system_prompt=system_prompt)
def invoke_scoped(self, message: str, *, model, tools: list, system_prompt: str) -> dict:
"""Run an isolated task through the same core without shared chat memory.
No mutation of the default model/tools: concurrent projects cannot share state.
The caller stores the trace under its own authorization boundary.
"""
from langchain_core.messages import messages_to_dict
agent = self.build_task_agent(model, tools, system_prompt)
result = agent.invoke(
{"messages": self._build_messages(message, [])},
config={"recursion_limit": 16, "callbacks": []},
)
return agent
messages = result.get("messages", [])
if not messages or messages[-1].type != "ai" or getattr(messages[-1], "tool_calls", None):
raise ValueError("Agent 未完成工具调用循环")
content = messages[-1].content
if not isinstance(content, str):
content = "\n".join(b.get("text", "") for b in content if isinstance(b, dict) and b.get("type") == "text")
if not content.strip():
raise ValueError("Agent 未返回有效建议")
return {"text": content, "messages": messages_to_dict(messages), "engine": "mini-openclaw/AgentManager"}
def _build_messages(self, user_message: str, history: list[dict[str, Any]]) -> list:
"""Convert session history + new message into LangChain messages."""