fix: fix DeepAgent context from default to DeepContext

This commit is contained in:
notts1215 2026-05-31 22:39:12 +08:00
parent c77311afe7
commit cd6949c7fd
3 changed files with 42 additions and 4 deletions

View File

@ -21,13 +21,14 @@ from yuxi.services.mcp_service import get_tools_from_all_servers
from yuxi.services.subagent_service import get_subagents_from_names
from yuxi.utils import logger
from .prompt import DEEP_PROMPT
from .context import DeepContext
class DeepAgent(BaseAgent):
name = "深度分析"
description = "具备规划、深度分析和子智能体协作能力的智能体,可以处理复杂的多步骤任务"
capabilities = ["file_upload", "files"] # 支持文件上传功能
context_schema = DeepContext
metadata = {"examples": ["调研一下多模态 GraphRAG 的相关论文"]}
def __init__(self, **kwargs):
@ -52,7 +53,6 @@ class DeepAgent(BaseAgent):
async def get_graph(self, context=None, **kwargs):
context = context or self.context_schema() # 获取上下文配置
system_prompt = f"{DEEP_PROMPT.strip()}\n\n{context.system_prompt or ''}"
model = load_chat_model(context.model)
sub_model = load_chat_model(context.subagents_model)
@ -93,7 +93,8 @@ class DeepAgent(BaseAgent):
# 使用 create_deep_agent 创建深度智能体
graph = create_agent(
model=model,
system_prompt=system_prompt,
# RuntimeConfigMiddleware injects context.system_prompt before each model call.
system_prompt="",
middleware=[
FilesystemMiddleware(backend=create_agent_composite_backend), # 文件系统后端
RuntimeConfigMiddleware(extra_tools=all_mcp_tools),

View File

@ -0,0 +1,37 @@
import ast
from pathlib import Path
def test_deep_agent_uses_deep_context_schema():
graph_path = (
Path(__file__).parents[3] / "package" / "yuxi" / "agents" / "buildin" / "deep_agent" / "graph.py"
)
module = ast.parse(graph_path.read_text(encoding="utf-8"))
deep_agent = next(
node for node in module.body if isinstance(node, ast.ClassDef) and node.name == "DeepAgent"
)
context_schema = next(
node
for node in deep_agent.body
if isinstance(node, ast.Assign)
and any(isinstance(target, ast.Name) and target.id == "context_schema" for target in node.targets)
)
assert isinstance(context_schema.value, ast.Name)
assert context_schema.value.id == "DeepContext"
def test_deep_agent_does_not_prepend_context_system_prompt_before_runtime_middleware():
graph_path = (
Path(__file__).parents[3] / "package" / "yuxi" / "agents" / "buildin" / "deep_agent" / "graph.py"
)
module = ast.parse(graph_path.read_text(encoding="utf-8"))
create_agent = next(
node
for node in ast.walk(module)
if isinstance(node, ast.Call) and isinstance(node.func, ast.Name) and node.func.id == "create_agent"
)
system_prompt = next(keyword.value for keyword in create_agent.keywords if keyword.arg == "system_prompt")
assert isinstance(system_prompt, ast.Constant)
assert system_prompt.value == ""

View File

@ -36,7 +36,7 @@
### 0.6.3 开发记录
<!-- 0.6.3 的内容请放在这里 -->
- 修复 DeepAgent 未绑定 `DeepContext`,导致深度分析专用系统提示词和子智能体默认模型配置未生效的问题;同时避免运行时重复注入默认提示词。
---