feat: Phase 3 - parallel execution, progress reporting, result caching + AgentChat bug fixes
Phase 3 能力: - DAG 并行执行 (workflow_engine): asyncio.gather 并行执行就绪节点 - Debate 并行 (orchestrator): for 循环改为 asyncio.gather - 粒度进度上报 (workflow_engine + tasks + websocket): Redis 推送 + DB 降级 - 工具结果缓存 (tool_manager): 确定性工具默认开启缓存 - LLM 响应缓存 (core): messages[-4:] + model 哈希,5min TTL AgentChat bug 修复 (Gitea #1-#5): - #1 SSE 降级重复空消息: fallback POST 前移除占位消息 - #2 streamTimeout 泄漏: while 正常退出后 clearTimeout - #3 loading 闪烁: final/error 事件中提前设 loading=false - #4 SSE 事件类型对齐: 确认匹配,未知类型加 console.warn - #5 retryMessage 流式残留: 重试时清理占位消息 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -10,6 +10,7 @@ Agent Runtime 核心 —— 自主 ReAct 循环。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
import logging
|
||||
import time
|
||||
@@ -98,6 +99,9 @@ class AgentRuntime:
|
||||
self.tool_manager = tool_manager or AgentToolManager(
|
||||
include_tools=self.config.tools.include_tools,
|
||||
exclude_tools=self.config.tools.exclude_tools,
|
||||
cache_enabled=self.config.tools.cache_enabled,
|
||||
cache_tool_whitelist=self.config.tools.cache_tool_whitelist,
|
||||
cache_ttl_ms=self.config.tools.cache_ttl_ms,
|
||||
)
|
||||
self.execution_logger = execution_logger
|
||||
self.on_tool_executed = on_tool_executed
|
||||
@@ -912,6 +916,32 @@ class AgentRuntime:
|
||||
return any(kw in err_lower for kw in _RETRYABLE_ERRORS)
|
||||
|
||||
|
||||
# LLM 缓存辅助
|
||||
def _llm_cache_key(messages: list, model: str) -> str:
|
||||
import hashlib
|
||||
raw = json.dumps({"msgs": messages[-4:], "model": model}, sort_keys=True, ensure_ascii=False)
|
||||
return f"llm:{model}:{hashlib.sha256(raw.encode()).hexdigest()[:16]}"
|
||||
|
||||
async def _llm_cache_get(key: str) -> Optional[str]:
|
||||
try:
|
||||
from app.core.redis_client import get_redis_client
|
||||
redis = get_redis_client()
|
||||
if redis:
|
||||
return await redis.get(key)
|
||||
except Exception:
|
||||
pass
|
||||
return None
|
||||
|
||||
async def _llm_cache_set(key: str, value: str, ttl_ms: int):
|
||||
try:
|
||||
from app.core.redis_client import get_redis_client
|
||||
redis = get_redis_client()
|
||||
if redis:
|
||||
await redis.setex(key, max(1, int(ttl_ms / 1000)), value)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
class _LLMClient:
|
||||
"""轻量 LLM 客户端包装,复用已有 LLMService 能力。"""
|
||||
|
||||
@@ -984,12 +1014,29 @@ class _LLMClient:
|
||||
kwargs["tools"] = normalized
|
||||
kwargs["tool_choice"] = "auto"
|
||||
|
||||
# LLM 响应缓存(仅不用工具时缓存,避免复杂序列化)
|
||||
if self._config.cache_enabled and not tools:
|
||||
cache_key = _llm_cache_key(kwargs.get("messages", []), kwargs.get("model", ""))
|
||||
cached = await _llm_cache_get(cache_key)
|
||||
if cached is not None:
|
||||
logger.info("LLM 响应命中缓存: model=%s", kwargs.get("model"))
|
||||
# 构造简易 message 对象(含 content 字段即可)
|
||||
class _CachedMsg:
|
||||
content = cached
|
||||
tool_calls = None
|
||||
return _CachedMsg()
|
||||
|
||||
start_time = time.perf_counter()
|
||||
try:
|
||||
response = await client.chat.completions.create(**kwargs)
|
||||
latency_ms = int((time.perf_counter() - start_time) * 1000)
|
||||
message = response.choices[0].message
|
||||
|
||||
# 缓存写入(仅不用工具时)
|
||||
if self._config.cache_enabled and not tools and message.content:
|
||||
ck = _llm_cache_key(kwargs.get("messages", []), kwargs.get("model", ""))
|
||||
await _llm_cache_set(ck, message.content, self._config.cache_ttl_ms)
|
||||
|
||||
# 提取 token 用量
|
||||
usage = getattr(response, "usage", None)
|
||||
prompt_tokens = usage.prompt_tokens if usage else 0
|
||||
|
||||
@@ -9,6 +9,7 @@ Agent Orchestrator — 多 Agent 编排引擎。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
import uuid
|
||||
@@ -347,9 +348,10 @@ class AgentOrchestrator:
|
||||
steps: List[OrchestratorStep] = []
|
||||
agent_outputs: List[Dict[str, Any]] = []
|
||||
|
||||
# 第一阶段:所有 Agent 独立回答
|
||||
# 第一阶段:所有 Agent 并行独立回答
|
||||
runtimes = []
|
||||
for agent_cfg in agents:
|
||||
runtime = AgentRuntime(
|
||||
runtimes.append(AgentRuntime(
|
||||
AgentConfig(
|
||||
name=agent_cfg.name,
|
||||
system_prompt=agent_cfg.system_prompt,
|
||||
@@ -364,8 +366,30 @@ class AgentOrchestrator:
|
||||
),
|
||||
),
|
||||
on_llm_call=on_llm_call,
|
||||
)
|
||||
result = await runtime.run(question)
|
||||
))
|
||||
|
||||
results = await asyncio.gather(
|
||||
*[rt.run(question) for rt in runtimes],
|
||||
return_exceptions=True,
|
||||
)
|
||||
|
||||
for i, agent_cfg in enumerate(agents):
|
||||
result = results[i]
|
||||
if isinstance(result, BaseException):
|
||||
step = OrchestratorStep(
|
||||
agent_id=agent_cfg.id,
|
||||
agent_name=agent_cfg.name,
|
||||
input=question,
|
||||
output="",
|
||||
error=str(result),
|
||||
)
|
||||
steps.append(step)
|
||||
agent_outputs.append({
|
||||
"agent_id": agent_cfg.id,
|
||||
"agent_name": agent_cfg.name,
|
||||
"output": f"[错误] {result}",
|
||||
})
|
||||
continue
|
||||
|
||||
step = OrchestratorStep(
|
||||
agent_id=agent_cfg.id,
|
||||
|
||||
@@ -15,6 +15,10 @@ class AgentToolConfig(BaseModel):
|
||||
require_approval: List[str] = Field(default_factory=list, description="需要人工审批的工具名列表")
|
||||
approval_timeout_ms: int = Field(default=60000, description="审批超时(毫秒),超时使用默认策略")
|
||||
approval_default: str = Field(default="deny", description="超时默认策略: approve | deny | skip")
|
||||
# 结果缓存
|
||||
cache_enabled: bool = Field(default=True, description="是否启用工具结果缓存(确定性工具默认开启)")
|
||||
cache_tool_whitelist: List[str] = Field(default_factory=list, description="启用缓存的工具名(空=确定性工具默认)")
|
||||
cache_ttl_ms: int = Field(default=3600000, description="缓存 TTL(毫秒),默认 1 小时")
|
||||
|
||||
|
||||
class AgentMemoryConfig(BaseModel):
|
||||
@@ -40,6 +44,8 @@ class AgentLLMConfig(BaseModel):
|
||||
request_timeout: float = 120.0
|
||||
extra_body: Optional[Dict[str, Any]] = None
|
||||
self_review_threshold: float = 0.6 # self-review 通过阈值(0-1)
|
||||
cache_enabled: bool = False # LLM 响应缓存(默认关闭,语义缓存有风险)
|
||||
cache_ttl_ms: int = 300000 # LLM 缓存 TTL,默认 5 分钟
|
||||
|
||||
|
||||
class AgentBudgetConfig(BaseModel):
|
||||
|
||||
@@ -3,6 +3,8 @@ Agent 工具管理器:包装已有 ToolRegistry,提供 Agent 需要的工具
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
import logging
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
@@ -10,6 +12,12 @@ from app.services.tool_registry import tool_registry
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# 默认确定性工具(结果可缓存)
|
||||
_DETERMINISTIC_TOOLS = {
|
||||
"file_read", "math_calculate", "database_query",
|
||||
"json", "text", "csv", "excel", "pdf", "image",
|
||||
}
|
||||
|
||||
|
||||
class AgentToolManager:
|
||||
"""
|
||||
@@ -17,12 +25,54 @@ class AgentToolManager:
|
||||
- 将 ToolRegistry 的工具 schema 转为 OpenAI Function Calling 格式
|
||||
- 按 Agent 配置过滤(白名单/黑名单)
|
||||
- 执行工具调用并返回结果字符串
|
||||
- 工具结果缓存(Redis / 内存 fallback)
|
||||
"""
|
||||
|
||||
def __init__(self, include_tools: Optional[List[str]] = None,
|
||||
exclude_tools: Optional[List[str]] = None):
|
||||
exclude_tools: Optional[List[str]] = None,
|
||||
cache_enabled: bool = True,
|
||||
cache_tool_whitelist: Optional[List[str]] = None,
|
||||
cache_ttl_ms: int = 3600000):
|
||||
self._include_tools: set = set(include_tools or [])
|
||||
self._exclude_tools: set = set(exclude_tools or [])
|
||||
self._cache_enabled = cache_enabled
|
||||
self._cache_whitelist: set = set(cache_tool_whitelist or [])
|
||||
self._cache_ttl_s = max(1, int(cache_ttl_ms / 1000))
|
||||
self._cache_store: Dict[str, str] = {} # 内存 fallback
|
||||
|
||||
def _is_cacheable(self, tool_name: str) -> bool:
|
||||
"""判断工具结果是否可缓存。"""
|
||||
if not self._cache_enabled:
|
||||
return False
|
||||
if self._cache_whitelist:
|
||||
return tool_name in self._cache_whitelist
|
||||
return tool_name in _DETERMINISTIC_TOOLS
|
||||
|
||||
@staticmethod
|
||||
def _cache_key(name: str, args: Dict[str, Any]) -> str:
|
||||
raw = json.dumps([name, args], sort_keys=True, ensure_ascii=False)
|
||||
return f"tool:{name}:{hashlib.sha256(raw.encode()).hexdigest()[:16]}"
|
||||
|
||||
async def _cache_get(self, key: str) -> Optional[str]:
|
||||
try:
|
||||
from app.core.redis_client import get_redis_client
|
||||
redis = get_redis_client()
|
||||
if redis:
|
||||
return await redis.get(key)
|
||||
except Exception:
|
||||
pass
|
||||
return self._cache_store.get(key)
|
||||
|
||||
async def _cache_set(self, key: str, value: str):
|
||||
try:
|
||||
from app.core.redis_client import get_redis_client
|
||||
redis = get_redis_client()
|
||||
if redis:
|
||||
await redis.setex(key, self._cache_ttl_s, value)
|
||||
return
|
||||
except Exception:
|
||||
pass
|
||||
self._cache_store[key] = value
|
||||
|
||||
def get_tool_schemas(self) -> List[Dict[str, Any]]:
|
||||
"""获取 Agent 可用的工具定义列表(OpenAI Function Calling 格式)。"""
|
||||
@@ -55,7 +105,7 @@ class AgentToolManager:
|
||||
|
||||
async def execute(self, name: str, args: Dict[str, Any]) -> str:
|
||||
"""
|
||||
执行工具调用。
|
||||
执行工具调用(带缓存)。
|
||||
|
||||
优先查找内置工具,其次查找数据库自定义工具(HTTP / Code)。
|
||||
|
||||
@@ -66,8 +116,24 @@ class AgentToolManager:
|
||||
Returns:
|
||||
工具执行结果的字符串表示
|
||||
"""
|
||||
# 缓存检查
|
||||
if self._is_cacheable(name):
|
||||
ck = self._cache_key(name, args)
|
||||
cached = await self._cache_get(ck)
|
||||
if cached is not None:
|
||||
logger.info("Agent 工具命中缓存: %s", name)
|
||||
return cached
|
||||
|
||||
logger.info("Agent 执行工具: %s", name)
|
||||
return await tool_registry.execute_tool(name, args)
|
||||
result = await tool_registry.execute_tool(name, args)
|
||||
|
||||
# 缓存写入
|
||||
if self._is_cacheable(name):
|
||||
ck = self._cache_key(name, args)
|
||||
await self._cache_set(ck, result)
|
||||
logger.debug("Agent 工具结果已缓存: %s", name)
|
||||
|
||||
return result
|
||||
|
||||
@staticmethod
|
||||
def _extract_tool_name(schema: Dict[str, Any]) -> Optional[str]:
|
||||
|
||||
Reference in New Issue
Block a user