feat: Agent 运行时、对话 API、作业助手与引擎修复及前端执行超时
- agent_runtime 模块与 agent_chat API,前端 AgentChat 视图与路由对接 - workflow_engine: code 节点命名空间与 json 引用修复 - llm_service: 工具调用 extra_body(如 DeepSeek) - create_homework_manager_agent / _3 脚本与测试脚本扩展 - frontend: WORKFLOW_EXECUTION_HTTP_TIMEOUT_MS、AgentChatPreview/MainLayout 等 - 文档:架构说明与自主 Agent 改造完成情况 Made-with: Cursor
This commit is contained in:
32
backend/app/agent_runtime/__init__.py
Normal file
32
backend/app/agent_runtime/__init__.py
Normal file
@@ -0,0 +1,32 @@
|
||||
"""
|
||||
Agent Runtime — 自主 AI Agent 核心运行时。
|
||||
|
||||
提供 ReAct 循环驱动的自主 Agent,支持:
|
||||
- 工具调用(复用已有 ToolRegistry)
|
||||
- 分层记忆(工作记忆 + 长期记忆)
|
||||
- 多模型(OpenAI / DeepSeek)
|
||||
- 可嵌入工作流节点或独立运行
|
||||
"""
|
||||
from app.agent_runtime.core import AgentRuntime
|
||||
from app.agent_runtime.schemas import (
|
||||
AgentConfig,
|
||||
AgentResult,
|
||||
AgentLLMConfig,
|
||||
AgentToolConfig,
|
||||
AgentMemoryConfig,
|
||||
)
|
||||
from app.agent_runtime.context import AgentContext
|
||||
from app.agent_runtime.memory import AgentMemory
|
||||
from app.agent_runtime.tool_manager import AgentToolManager
|
||||
|
||||
__all__ = [
|
||||
"AgentRuntime",
|
||||
"AgentConfig",
|
||||
"AgentResult",
|
||||
"AgentLLMConfig",
|
||||
"AgentToolConfig",
|
||||
"AgentMemoryConfig",
|
||||
"AgentContext",
|
||||
"AgentMemory",
|
||||
"AgentToolManager",
|
||||
]
|
||||
87
backend/app/agent_runtime/context.py
Normal file
87
backend/app/agent_runtime/context.py
Normal file
@@ -0,0 +1,87 @@
|
||||
"""
|
||||
Agent 会话上下文管理:维护消息历史、状态追踪。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import uuid
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
|
||||
class AgentContext:
|
||||
"""
|
||||
Agent 会话上下文:
|
||||
|
||||
- 消息历史(messages 列表,OpenAI 格式)
|
||||
- 会话元信息(session_id, user_id 等)
|
||||
- 执行追踪(iteration 计数, 工具调用统计)
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
system_prompt: str = "你是一个有用的AI助手。",
|
||||
user_id: Optional[str] = None,
|
||||
session_id: Optional[str] = None,
|
||||
):
|
||||
self.session_id = session_id or str(uuid.uuid4())
|
||||
self.user_id = user_id
|
||||
self._messages: List[Dict[str, Any]] = []
|
||||
self._system_prompt = system_prompt
|
||||
# 执行状态
|
||||
self.iteration = 0
|
||||
self.tool_calls_made = 0
|
||||
|
||||
@property
|
||||
def messages(self) -> List[Dict[str, Any]]:
|
||||
"""获取完整消息列表(含 system prompt)。"""
|
||||
if self._system_prompt:
|
||||
# 确保 system prompt 始终在第一条
|
||||
has_system = (
|
||||
len(self._messages) > 0
|
||||
and self._messages[0].get("role") == "system"
|
||||
)
|
||||
if not has_system:
|
||||
return [
|
||||
{"role": "system", "content": self._system_prompt},
|
||||
*self._messages,
|
||||
]
|
||||
return self._messages
|
||||
|
||||
def add_user_message(self, content: str) -> None:
|
||||
"""添加用户消息。"""
|
||||
self._messages.append({"role": "user", "content": content})
|
||||
|
||||
def add_assistant_message(
|
||||
self,
|
||||
content: str,
|
||||
tool_calls: Optional[List[Dict[str, Any]]] = None,
|
||||
reasoning_content: Optional[str] = None,
|
||||
) -> None:
|
||||
"""添加助手回复。"""
|
||||
msg: Dict[str, Any] = {"role": "assistant", "content": content or ""}
|
||||
if tool_calls:
|
||||
msg["tool_calls"] = tool_calls
|
||||
if reasoning_content:
|
||||
msg["reasoning_content"] = reasoning_content
|
||||
self._messages.append(msg)
|
||||
|
||||
def add_tool_result(
|
||||
self, tool_call_id: str, tool_name: str, result: str
|
||||
) -> None:
|
||||
"""添加工具执行结果。"""
|
||||
self._messages.append({
|
||||
"role": "tool",
|
||||
"tool_call_id": tool_call_id,
|
||||
"content": result,
|
||||
"name": tool_name,
|
||||
})
|
||||
|
||||
def set_system_prompt(self, prompt: str) -> None:
|
||||
"""更新 system prompt(仅在未发送过消息时有效)。"""
|
||||
if not self._messages:
|
||||
self._system_prompt = prompt
|
||||
|
||||
def reset(self) -> None:
|
||||
"""重置上下文(保留 system prompt 和 session_id)。"""
|
||||
self._messages = []
|
||||
self.iteration = 0
|
||||
self.tool_calls_made = 0
|
||||
330
backend/app/agent_runtime/core.py
Normal file
330
backend/app/agent_runtime/core.py
Normal file
@@ -0,0 +1,330 @@
|
||||
"""
|
||||
Agent Runtime 核心 —— 自主 ReAct 循环。
|
||||
|
||||
流程:
|
||||
1. 接收用户输入 → 追加到消息列表
|
||||
2. 调用 LLM(携带 tools schema)
|
||||
3. 如果 LLM 返回工具调用 → 执行工具 → 结果追加到消息列表 → 回到 2
|
||||
4. 如果 LLM 返回文本 → 作为最终回答返回
|
||||
5. 超过 max_iterations → 强制终止
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
from typing import Any, Callable, Dict, List, Optional
|
||||
|
||||
from app.agent_runtime.schemas import (
|
||||
AgentConfig,
|
||||
AgentResult,
|
||||
)
|
||||
from app.agent_runtime.context import AgentContext
|
||||
from app.agent_runtime.memory import AgentMemory
|
||||
from app.agent_runtime.tool_manager import AgentToolManager
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# 可重试的 API 异常
|
||||
_RETRYABLE_ERRORS = (
|
||||
"timed out",
|
||||
"timeout",
|
||||
"connection error",
|
||||
"temporarily unavailable",
|
||||
"server disconnected",
|
||||
"rate limit",
|
||||
"too many requests",
|
||||
"internal server error",
|
||||
"service unavailable",
|
||||
)
|
||||
|
||||
|
||||
class AgentRuntime:
|
||||
"""
|
||||
自主 Agent 运行时。
|
||||
|
||||
用法:
|
||||
runtime = AgentRuntime(config)
|
||||
result = await runtime.run("帮我写个Python脚本")
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
config: Optional[AgentConfig] = None,
|
||||
context: Optional[AgentContext] = None,
|
||||
memory: Optional[AgentMemory] = None,
|
||||
tool_manager: Optional[AgentToolManager] = None,
|
||||
execution_logger: Optional[Any] = None,
|
||||
on_tool_executed: Optional[Callable[[str], Any]] = None,
|
||||
):
|
||||
self.config = config or AgentConfig()
|
||||
self.context = context or AgentContext(
|
||||
system_prompt=self.config.system_prompt,
|
||||
user_id=self.config.user_id,
|
||||
)
|
||||
self.memory = memory or AgentMemory(
|
||||
scope_id=self.config.user_id or self.config.name,
|
||||
max_history=self.config.memory.max_history_messages,
|
||||
persist=self.config.memory.persist_to_db,
|
||||
)
|
||||
self.tool_manager = tool_manager or AgentToolManager(
|
||||
include_tools=self.config.tools.include_tools,
|
||||
exclude_tools=self.config.tools.exclude_tools,
|
||||
)
|
||||
self.execution_logger = execution_logger
|
||||
self.on_tool_executed = on_tool_executed
|
||||
self._memory_context_loaded = False
|
||||
|
||||
async def run(self, user_input: str) -> AgentResult:
|
||||
"""
|
||||
执行 Agent 单轮对话。
|
||||
|
||||
流程:加载记忆 → 追加用户消息 → ReAct 循环 → 保存记忆 → 返回结果。
|
||||
"""
|
||||
max_iter = max(1, self.config.llm.max_iterations)
|
||||
self.context.iteration = 0
|
||||
self.context.tool_calls_made = 0
|
||||
|
||||
# 1. 首次运行时加载长期记忆到 system prompt
|
||||
if not self._memory_context_loaded:
|
||||
await self._inject_memory_context()
|
||||
self._memory_context_loaded = True
|
||||
|
||||
# 2. 追加用户消息
|
||||
self.context.add_user_message(user_input)
|
||||
|
||||
# 3. ReAct 循环
|
||||
llm = _LLMClient(self.config.llm)
|
||||
tool_schemas = self.tool_manager.get_tool_schemas()
|
||||
has_tools = self.tool_manager.has_tools()
|
||||
|
||||
while self.context.iteration < max_iter:
|
||||
self.context.iteration += 1
|
||||
|
||||
# 裁剪过长历史
|
||||
messages = self.memory.trim_messages(self.context.messages)
|
||||
|
||||
# 调用 LLM
|
||||
try:
|
||||
response = await llm.chat(
|
||||
messages=messages,
|
||||
tools=tool_schemas if has_tools and self.context.iteration == 1 else
|
||||
(tool_schemas if has_tools else None),
|
||||
iteration=self.context.iteration,
|
||||
)
|
||||
except Exception as e:
|
||||
err_str = str(e)
|
||||
logger.error("LLM 调用失败 (iteration=%s): %s", self.context.iteration, err_str)
|
||||
if self.context.iteration < max_iter and self._is_retryable(err_str):
|
||||
continue
|
||||
return AgentResult(
|
||||
success=False,
|
||||
content=f"LLM 调用失败: {err_str}",
|
||||
iterations_used=self.context.iteration,
|
||||
tool_calls_made=self.context.tool_calls_made,
|
||||
error=err_str,
|
||||
)
|
||||
|
||||
# 解析工具调用
|
||||
tool_calls = self._extract_tool_calls(response)
|
||||
content = self._extract_content(response)
|
||||
|
||||
if not tool_calls:
|
||||
# LLM 直接返回文本 → 结束
|
||||
self.context.add_assistant_message(content)
|
||||
final_text = content or "(模型未返回有效内容)"
|
||||
# 保存记忆
|
||||
await self.memory.save_context(user_input, final_text)
|
||||
return AgentResult(
|
||||
success=True,
|
||||
content=final_text,
|
||||
iterations_used=self.context.iteration,
|
||||
tool_calls_made=self.context.tool_calls_made,
|
||||
)
|
||||
|
||||
# 有工具调用 → 先记录 assistant 消息(含 tool_calls + reasoning_content)
|
||||
reasoning = getattr(response, "reasoning_content", None) or (
|
||||
response.get("reasoning_content") if isinstance(response, dict) else None
|
||||
)
|
||||
self.context.add_assistant_message(content or "", tool_calls, reasoning)
|
||||
if self.execution_logger:
|
||||
self.execution_logger.info(
|
||||
f"Agent 调用 {len(tool_calls)} 个工具",
|
||||
data={"tool_calls": [tc["function"]["name"] for tc in tool_calls],
|
||||
"iteration": self.context.iteration},
|
||||
)
|
||||
|
||||
# 逐一执行工具
|
||||
for tc in tool_calls:
|
||||
tfn = tc.get("function", {})
|
||||
tname = tfn.get("name", "unknown")
|
||||
tcid = tc.get("id", f"call_{self.context.iteration}_{self.context.tool_calls_made}")
|
||||
|
||||
try:
|
||||
targs = json.loads(tfn.get("arguments", "{}"))
|
||||
except (json.JSONDecodeError, TypeError):
|
||||
targs = {}
|
||||
|
||||
logger.info("Agent 执行工具 [%s]: %s", tname, targs)
|
||||
result = await self.tool_manager.execute(tname, targs)
|
||||
|
||||
self.context.add_tool_result(tcid, tname, result)
|
||||
self.context.tool_calls_made += 1
|
||||
|
||||
if self.on_tool_executed:
|
||||
try:
|
||||
await self.on_tool_executed(tname)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if self.execution_logger:
|
||||
preview = result[:300] + "..." if len(result) > 300 else result
|
||||
self.execution_logger.info(
|
||||
f"工具 {tname} 执行完成",
|
||||
data={"tool_name": tname, "result_preview": preview},
|
||||
)
|
||||
|
||||
# 达到最大迭代次数
|
||||
last_content = ""
|
||||
for m in reversed(self.context.messages):
|
||||
if m.get("role") == "assistant" and m.get("content"):
|
||||
last_content = m["content"]
|
||||
break
|
||||
|
||||
logger.warning("Agent 达到最大迭代次数 (%s)", max_iter)
|
||||
await self.memory.save_context(user_input, last_content or "(已达最大迭代次数)")
|
||||
return AgentResult(
|
||||
success=True,
|
||||
content=last_content or "已达最大迭代次数,但模型未返回最终回答。",
|
||||
truncated=True,
|
||||
iterations_used=self.context.iteration,
|
||||
tool_calls_made=self.context.tool_calls_made,
|
||||
)
|
||||
|
||||
async def _inject_memory_context(self) -> None:
|
||||
"""加载长期记忆并注入 system prompt。"""
|
||||
mem_text = await self.memory.initialize()
|
||||
if mem_text:
|
||||
enriched = (
|
||||
self.config.system_prompt.rstrip("\n")
|
||||
+ "\n\n"
|
||||
+ mem_text
|
||||
)
|
||||
self.context.set_system_prompt(enriched)
|
||||
logger.info("Agent 已注入长期记忆上下文")
|
||||
|
||||
@staticmethod
|
||||
def _extract_tool_calls(response: Any) -> List[Dict[str, Any]]:
|
||||
"""从 LLM 响应中提取工具调用列表。"""
|
||||
if response is None:
|
||||
return []
|
||||
# OpenAI SDK 格式
|
||||
if hasattr(response, "tool_calls") and response.tool_calls:
|
||||
result = []
|
||||
for tc in response.tool_calls:
|
||||
result.append({
|
||||
"id": tc.id,
|
||||
"type": tc.type,
|
||||
"function": {
|
||||
"name": tc.function.name,
|
||||
"arguments": tc.function.arguments,
|
||||
},
|
||||
})
|
||||
return result
|
||||
# 字典格式
|
||||
if isinstance(response, dict):
|
||||
tc_list = response.get("tool_calls") or []
|
||||
if tc_list:
|
||||
return tc_list
|
||||
# 检查 content 中是否嵌入了 DSML
|
||||
content = response.get("content") or ""
|
||||
if "invoke" in content or "function_call" in content:
|
||||
from app.services.llm_service import _parse_dsml_tool_invocations
|
||||
dsml = _parse_dsml_tool_invocations(content)
|
||||
if dsml:
|
||||
return [
|
||||
{
|
||||
"id": f"dsml-{i}",
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": inv["name"],
|
||||
"arguments": json.dumps(inv["arguments"], ensure_ascii=False),
|
||||
},
|
||||
}
|
||||
for i, inv in enumerate(dsml)
|
||||
]
|
||||
return []
|
||||
|
||||
@staticmethod
|
||||
def _extract_content(response: Any) -> str:
|
||||
"""从 LLM 响应中提取文本内容。"""
|
||||
if response is None:
|
||||
return ""
|
||||
if hasattr(response, "content"):
|
||||
return response.content or ""
|
||||
if isinstance(response, dict):
|
||||
return response.get("content") or ""
|
||||
return str(response)
|
||||
|
||||
@staticmethod
|
||||
def _is_retryable(err_str: str) -> bool:
|
||||
"""判断错误是否可重试。"""
|
||||
err_lower = err_str.lower()
|
||||
return any(kw in err_lower for kw in _RETRYABLE_ERRORS)
|
||||
|
||||
|
||||
class _LLMClient:
|
||||
"""轻量 LLM 客户端包装,复用已有 LLMService 能力。"""
|
||||
|
||||
def __init__(self, config: Any):
|
||||
from app.services.llm_service import llm_service
|
||||
self._service = llm_service
|
||||
self._config = config
|
||||
|
||||
async def chat(
|
||||
self,
|
||||
messages: List[Dict[str, Any]],
|
||||
tools: Optional[List[Dict[str, Any]]] = None,
|
||||
iteration: int = 1,
|
||||
) -> Any:
|
||||
"""
|
||||
调用 LLM。
|
||||
优先使用 llm_service.call_openai_with_tools(支持 ReAct 的多次工具调用)。
|
||||
|
||||
但为避免外层 ReAct 与内部 ReAct 冲突:
|
||||
- 第 1 轮:使用标准 chat(无内部 ReAct),由外层 AgentRuntime 控制循环
|
||||
- 后续轮次:也使用标准 chat,仅追加工具结果
|
||||
"""
|
||||
# 直接用 OpenAI/DeepSeek SDK 调用,由 AgentRuntime 控制循环
|
||||
from openai import AsyncOpenAI
|
||||
from app.core.config import settings
|
||||
|
||||
# 优先从配置读取,其次从 settings(.env 加载),最后 os.environ
|
||||
api_key = self._config.api_key or settings.OPENAI_API_KEY or ""
|
||||
base_url = self._config.base_url or settings.OPENAI_BASE_URL or ""
|
||||
|
||||
if not api_key or api_key == "your-openai-api-key":
|
||||
# 尝试 DeepSeek
|
||||
api_key = self._config.api_key or settings.DEEPSEEK_API_KEY or ""
|
||||
base_url = self._config.base_url or settings.DEEPSEEK_BASE_URL or "https://api.deepseek.com"
|
||||
|
||||
if not api_key:
|
||||
raise ValueError("未配置 API Key")
|
||||
|
||||
client = AsyncOpenAI(api_key=api_key, base_url=base_url)
|
||||
|
||||
kwargs: Dict[str, Any] = {
|
||||
"model": self._config.model,
|
||||
"messages": messages,
|
||||
"temperature": self._config.temperature,
|
||||
"timeout": self._config.request_timeout,
|
||||
}
|
||||
if self._config.max_tokens:
|
||||
kwargs["max_tokens"] = self._config.max_tokens
|
||||
if self._config.extra_body:
|
||||
kwargs["extra_body"] = self._config.extra_body
|
||||
if tools:
|
||||
kwargs["tools"] = tools
|
||||
kwargs["tool_choice"] = "auto"
|
||||
|
||||
response = await client.chat.completions.create(**kwargs)
|
||||
return response.choices[0].message
|
||||
135
backend/app/agent_runtime/memory.py
Normal file
135
backend/app/agent_runtime/memory.py
Normal file
@@ -0,0 +1,135 @@
|
||||
"""
|
||||
Agent 记忆管理:包装已有 persistent_memory_service,提供会话级和长期记忆。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
from typing import Any, Dict, List, Optional
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.database import SessionLocal
|
||||
from app.services.persistent_memory_service import (
|
||||
load_persistent_memory,
|
||||
save_persistent_memory,
|
||||
persist_enabled,
|
||||
)
|
||||
from app.core.config import settings
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class AgentMemory:
|
||||
"""
|
||||
分层记忆管理器:
|
||||
|
||||
- 工作记忆:当前会话消息列表(由 AgentRuntime 直接管理)
|
||||
- 长期记忆:从 MySQL 加载/保存的用户画像和关键事实
|
||||
- 上下文压缩:对话过长时自动裁剪或总结
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
scope_kind: str = "agent",
|
||||
scope_id: Optional[str] = None,
|
||||
session_key: Optional[str] = None,
|
||||
persist: bool = True,
|
||||
max_history: int = 20,
|
||||
):
|
||||
self.scope_kind = scope_kind
|
||||
self.scope_id = scope_id or "default"
|
||||
self.session_key = session_key or "default_session"
|
||||
self.persist = persist and persist_enabled()
|
||||
self.max_history = max_history
|
||||
# 从长期记忆加载的上下文(启动时加载)
|
||||
self._long_term_context: Dict[str, Any] = {}
|
||||
|
||||
async def initialize(self) -> str:
|
||||
"""
|
||||
初始化记忆:从 DB/Redis 加载长期记忆,构造初始上下文文本。
|
||||
返回注入 system prompt 的记忆文本块。
|
||||
"""
|
||||
if not self.persist or not self.scope_id:
|
||||
return ""
|
||||
|
||||
db: Optional[Session] = None
|
||||
try:
|
||||
db = SessionLocal()
|
||||
payload = load_persistent_memory(
|
||||
db, self.scope_kind, self.scope_id, self.session_key
|
||||
)
|
||||
if payload and isinstance(payload, dict):
|
||||
self._long_term_context = payload
|
||||
# 构建注入 system prompt 的记忆文本
|
||||
parts = []
|
||||
profile = payload.get("user_profile")
|
||||
if profile and isinstance(profile, dict):
|
||||
profile_text = json.dumps(profile, ensure_ascii=False)
|
||||
parts.append(f"## 用户画像\n{profile_text}")
|
||||
|
||||
context = payload.get("context")
|
||||
if context and isinstance(context, dict):
|
||||
ctx_text = json.dumps(context, ensure_ascii=False)
|
||||
parts.append(f"## 上下文\n{ctx_text}")
|
||||
|
||||
history = payload.get("conversation_history")
|
||||
if history and isinstance(history, list) and len(history) > 0:
|
||||
summary = self._summarize_history(history)
|
||||
parts.append(f"## 历史对话摘要\n{summary}")
|
||||
|
||||
if parts:
|
||||
return "\n\n".join(parts)
|
||||
except Exception as e:
|
||||
logger.warning("加载长期记忆失败: %s", e)
|
||||
finally:
|
||||
if db:
|
||||
db.close()
|
||||
return ""
|
||||
|
||||
async def save_context(
|
||||
self, user_message: str, assistant_reply: str
|
||||
) -> None:
|
||||
"""将单轮对话保存到长期记忆。"""
|
||||
if not self.persist or not self.scope_id:
|
||||
return
|
||||
|
||||
# 更新上下文
|
||||
ctx = self._long_term_context.get("context", {})
|
||||
ctx["last_user_message"] = user_message[:500]
|
||||
ctx["last_assistant_reply"] = assistant_reply[:500]
|
||||
self._long_term_context["context"] = ctx
|
||||
|
||||
db: Optional[Session] = None
|
||||
try:
|
||||
db = SessionLocal()
|
||||
save_persistent_memory(
|
||||
db, self.scope_kind, self.scope_id,
|
||||
self.session_key, self._long_term_context,
|
||||
)
|
||||
except Exception as e:
|
||||
logger.warning("保存长期记忆失败: %s", e)
|
||||
finally:
|
||||
if db:
|
||||
db.close()
|
||||
|
||||
def trim_messages(self, messages: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
||||
"""
|
||||
裁剪消息列表:保留最近的 N 条,但始终保留第一条 system 消息。
|
||||
"""
|
||||
if len(messages) <= self.max_history:
|
||||
return messages
|
||||
|
||||
system_msgs = [m for m in messages if m.get("role") == "system"]
|
||||
other_msgs = [m for m in messages if m.get("role") != "system"]
|
||||
|
||||
trimmed = other_msgs[-(self.max_history - len(system_msgs)):]
|
||||
return system_msgs + trimmed
|
||||
|
||||
@staticmethod
|
||||
def _summarize_history(history: List[Dict[str, Any]]) -> str:
|
||||
"""简单汇总历史对话(不做 LLM 压缩,仅计数)。"""
|
||||
turns = 0
|
||||
for m in history:
|
||||
if m.get("role") == "user":
|
||||
turns += 1
|
||||
return f"共 {turns} 轮历史对话(详情已存入长期记忆)"
|
||||
64
backend/app/agent_runtime/schemas.py
Normal file
64
backend/app/agent_runtime/schemas.py
Normal file
@@ -0,0 +1,64 @@
|
||||
"""
|
||||
Agent Runtime 配置与数据结构 Schema
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Dict, List, Optional
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
|
||||
class AgentToolConfig(BaseModel):
|
||||
"""Agent 可用工具配置"""
|
||||
# 若为空列表则使用全部已注册工具
|
||||
include_tools: List[str] = Field(default_factory=list, description="允许的工具名称白名单")
|
||||
exclude_tools: List[str] = Field(default_factory=list, description="排除的工具名称黑名单")
|
||||
|
||||
|
||||
class AgentMemoryConfig(BaseModel):
|
||||
"""Agent 记忆配置"""
|
||||
enabled: bool = True
|
||||
max_history_messages: int = 20 # 注入 LLM 的上文最大消息数
|
||||
session_key: Optional[str] = None # 会话标识,默认自动生成
|
||||
persist_to_db: bool = True # 是否写入 MySQL 长期记忆
|
||||
|
||||
|
||||
class AgentLLMConfig(BaseModel):
|
||||
"""Agent 模型配置"""
|
||||
provider: str = "openai" # openai / deepseek
|
||||
model: str = "gpt-4o-mini"
|
||||
temperature: float = 0.7
|
||||
max_tokens: Optional[int] = None
|
||||
api_key: Optional[str] = None
|
||||
base_url: Optional[str] = None
|
||||
max_iterations: int = 10 # ReAct 循环最大步数
|
||||
request_timeout: float = 120.0
|
||||
extra_body: Optional[Dict[str, Any]] = None
|
||||
|
||||
|
||||
class AgentConfig(BaseModel):
|
||||
"""Agent 完整配置"""
|
||||
name: str = "default_agent"
|
||||
system_prompt: str = "你是一个有用的AI助手。请使用可用工具来帮助用户完成任务。"
|
||||
llm: AgentLLMConfig = Field(default_factory=AgentLLMConfig)
|
||||
tools: AgentToolConfig = Field(default_factory=AgentToolConfig)
|
||||
memory: AgentMemoryConfig = Field(default_factory=AgentMemoryConfig)
|
||||
user_id: Optional[str] = None
|
||||
|
||||
|
||||
class AgentMessage(BaseModel):
|
||||
"""Agent 对话消息"""
|
||||
role: str # user / assistant / tool
|
||||
content: str
|
||||
tool_calls: Optional[List[Dict[str, Any]]] = None
|
||||
tool_call_id: Optional[str] = None
|
||||
name: Optional[str] = None
|
||||
|
||||
|
||||
class AgentResult(BaseModel):
|
||||
"""Agent 执行结果"""
|
||||
success: bool = True
|
||||
content: str = ""
|
||||
truncated: bool = False
|
||||
iterations_used: int = 0
|
||||
tool_calls_made: int = 0
|
||||
error: Optional[str] = None
|
||||
94
backend/app/agent_runtime/tool_manager.py
Normal file
94
backend/app/agent_runtime/tool_manager.py
Normal file
@@ -0,0 +1,94 @@
|
||||
"""
|
||||
Agent 工具管理器:包装已有 ToolRegistry,提供 Agent 需要的工具格式转换和执行。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
from typing import Any, Callable, Dict, List, Optional
|
||||
|
||||
from app.services.tool_registry import tool_registry
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class AgentToolManager:
|
||||
"""
|
||||
为 Agent Runtime 管理工具:
|
||||
- 将 ToolRegistry 的工具 schema 转为 OpenAI Function Calling 格式
|
||||
- 按 Agent 配置过滤(白名单/黑名单)
|
||||
- 执行工具调用并返回结果字符串
|
||||
"""
|
||||
|
||||
def __init__(self, include_tools: Optional[List[str]] = None,
|
||||
exclude_tools: Optional[List[str]] = None):
|
||||
self._include_tools: set = set(include_tools or [])
|
||||
self._exclude_tools: set = set(exclude_tools or [])
|
||||
|
||||
def get_tool_schemas(self) -> List[Dict[str, Any]]:
|
||||
"""获取 Agent 可用的工具定义列表(OpenAI Function Calling 格式)。"""
|
||||
all_schemas = tool_registry.get_all_tool_schemas()
|
||||
if not self._include_tools and not self._exclude_tools:
|
||||
return all_schemas
|
||||
|
||||
filtered = []
|
||||
for schema in all_schemas:
|
||||
name = self._extract_tool_name(schema)
|
||||
if not name:
|
||||
continue
|
||||
if self._include_tools and name not in self._include_tools:
|
||||
continue
|
||||
if name in self._exclude_tools:
|
||||
continue
|
||||
filtered.append(schema)
|
||||
return filtered
|
||||
|
||||
def has_tools(self) -> bool:
|
||||
"""是否有可用工具。"""
|
||||
return len(self.get_tool_schemas()) > 0
|
||||
|
||||
def tool_names(self) -> List[str]:
|
||||
"""可用工具名称列表。"""
|
||||
return [
|
||||
self._extract_tool_name(s) or "?"
|
||||
for s in self.get_tool_schemas()
|
||||
]
|
||||
|
||||
async def execute(self, name: str, args: Dict[str, Any]) -> str:
|
||||
"""
|
||||
执行工具调用。
|
||||
|
||||
Args:
|
||||
name: 工具名称
|
||||
args: 工具参数字典
|
||||
|
||||
Returns:
|
||||
工具执行结果的字符串表示
|
||||
"""
|
||||
func: Optional[Callable] = tool_registry.get_tool_function(name)
|
||||
if not func:
|
||||
err = f"工具 '{name}' 不存在"
|
||||
logger.error(err)
|
||||
return json.dumps({"error": err}, ensure_ascii=False)
|
||||
|
||||
logger.info("Agent 执行工具: %s, 参数: %s", name, args)
|
||||
try:
|
||||
import asyncio
|
||||
if asyncio.iscoroutinefunction(func):
|
||||
result = await func(**args)
|
||||
else:
|
||||
result = func(**args)
|
||||
|
||||
if isinstance(result, (dict, list)):
|
||||
return json.dumps(result, ensure_ascii=False)
|
||||
return str(result)
|
||||
except Exception as e:
|
||||
err_msg = f"工具 '{name}' 执行失败: {e}"
|
||||
logger.error(err_msg, exc_info=True)
|
||||
return json.dumps({"error": err_msg}, ensure_ascii=False)
|
||||
|
||||
@staticmethod
|
||||
def _extract_tool_name(schema: Dict[str, Any]) -> Optional[str]:
|
||||
"""从工具 schema 中提取工具名称。"""
|
||||
fn = schema.get("function") or schema
|
||||
return fn.get("name") if isinstance(fn, dict) else None
|
||||
115
backend/app/agent_runtime/workflow_integration.py
Normal file
115
backend/app/agent_runtime/workflow_integration.py
Normal file
@@ -0,0 +1,115 @@
|
||||
"""
|
||||
Agent Runtime ⇄ WorkflowEngine 桥接。
|
||||
|
||||
让 workflow_engine.execute_node() 通过寥寥几行调用 Agent Runtime。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
from app.agent_runtime.core import AgentRuntime
|
||||
from app.agent_runtime.schemas import (
|
||||
AgentConfig,
|
||||
AgentLLMConfig,
|
||||
AgentToolConfig,
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def run_agent_node(
|
||||
node_data: Dict[str, Any],
|
||||
input_data: Dict[str, Any],
|
||||
execution_logger: Optional[Any] = None,
|
||||
user_id: Optional[str] = None,
|
||||
on_tool_executed: Optional[Any] = None,
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
在工作流中执行 Agent 节点。
|
||||
|
||||
node_data 支持的字段:
|
||||
system_prompt — Agent 人格/指令(支持 {{variable}} 模板)
|
||||
tools — 可选工具白名单,默认全部
|
||||
exclude_tools — 可选工具黑名单
|
||||
model — 模型名称
|
||||
provider — 提供商(openai/deepseek)
|
||||
temperature — 温度
|
||||
max_iterations — ReAct 最大步数
|
||||
memory — 是否启用长期记忆
|
||||
|
||||
input_data 中的 "query" 或 "input" 字段作为用户输入。
|
||||
"""
|
||||
# 1. 解析配置
|
||||
query = (
|
||||
input_data.get("query")
|
||||
or input_data.get("input")
|
||||
or input_data.get("text", "")
|
||||
)
|
||||
if not isinstance(query, str):
|
||||
query = str(query) if query else ""
|
||||
|
||||
if not query:
|
||||
return {"output": "错误:Agent 节点未收到用户输入", "status": "error"}
|
||||
|
||||
# 2. 解析 system_prompt(支持模板变量)
|
||||
raw_prompt = node_data.get("system_prompt", "你是一个有用的AI助手。")
|
||||
try:
|
||||
formatted_prompt = raw_prompt.format(**input_data)
|
||||
except (KeyError, ValueError):
|
||||
formatted_prompt = raw_prompt
|
||||
|
||||
# 3. 构建 Agent 配置
|
||||
llm_config = AgentLLMConfig(
|
||||
provider=node_data.get("provider", "openai"),
|
||||
model=node_data.get("model", "gpt-4o-mini"),
|
||||
temperature=float(node_data.get("temperature", 0.7)),
|
||||
max_iterations=int(node_data.get("max_iterations", 10)),
|
||||
)
|
||||
# 允许节点内联 api_key/base_url
|
||||
if node_data.get("api_key"):
|
||||
llm_config.api_key = node_data["api_key"]
|
||||
if node_data.get("base_url"):
|
||||
llm_config.base_url = node_data["base_url"]
|
||||
|
||||
agent_config = AgentConfig(
|
||||
name=node_data.get("label", "agent_node"),
|
||||
system_prompt=formatted_prompt,
|
||||
llm=llm_config,
|
||||
tools=AgentToolConfig(
|
||||
include_tools=node_data.get("tools", []),
|
||||
exclude_tools=node_data.get("exclude_tools", []),
|
||||
),
|
||||
memory={
|
||||
"enabled": node_data.get("memory", True),
|
||||
"persist_to_db": node_data.get("memory", True),
|
||||
},
|
||||
user_id=user_id,
|
||||
)
|
||||
|
||||
# 4. 执行 Agent
|
||||
runtime = AgentRuntime(
|
||||
config=agent_config,
|
||||
execution_logger=execution_logger,
|
||||
on_tool_executed=on_tool_executed,
|
||||
)
|
||||
|
||||
result = await runtime.run(query)
|
||||
|
||||
# 5. 返回结果(兼容工作流引擎的输出格式)
|
||||
if result.success:
|
||||
return {
|
||||
"output": result.content,
|
||||
"status": "success",
|
||||
"agent_meta": {
|
||||
"iterations": result.iterations_used,
|
||||
"tool_calls": result.tool_calls_made,
|
||||
"truncated": result.truncated,
|
||||
},
|
||||
}
|
||||
else:
|
||||
return {
|
||||
"output": result.content,
|
||||
"status": "error",
|
||||
"error": result.error,
|
||||
}
|
||||
Reference in New Issue
Block a user