Refine knowledge brain workflow
Align the brain prompts, graph view, and startup defaults with the latest phase 1 flow so local runs and navigation stay consistent.
This commit is contained in:
102
backend/tests/backend/app/agents/test_graph.py
Normal file
102
backend/tests/backend/app/agents/test_graph.py
Normal file
@@ -0,0 +1,102 @@
|
||||
from langchain_core.messages import HumanMessage
|
||||
|
||||
from app.agents.graph import master_node
|
||||
from app.agents.state import AgentRole
|
||||
|
||||
|
||||
class FailIfCalledLLM:
|
||||
async def ainvoke(self, messages):
|
||||
raise AssertionError('LLM should not be called for simple greetings')
|
||||
|
||||
|
||||
async def test_master_node_returns_stable_reply_for_simple_greeting(monkeypatch):
|
||||
monkeypatch.setattr('app.agents.graph._get_llm_for_state', lambda state: FailIfCalledLLM())
|
||||
|
||||
state = {
|
||||
'messages': [HumanMessage(content='你好')],
|
||||
'user_id': 'u1',
|
||||
'conversation_id': 'c1',
|
||||
'current_agent': AgentRole.MASTER,
|
||||
'active_agents': [AgentRole.MASTER],
|
||||
'pending_tasks': [],
|
||||
'completed_tasks': [],
|
||||
'tool_calls': [],
|
||||
'last_tool_result': None,
|
||||
'knowledge_context': None,
|
||||
'graph_context': None,
|
||||
'plan': None,
|
||||
'plan_steps': [],
|
||||
'analysis_report': None,
|
||||
'final_response': None,
|
||||
'should_respond': True,
|
||||
'memory_context': None,
|
||||
'user_llm_config': None,
|
||||
}
|
||||
|
||||
result = await master_node(state)
|
||||
|
||||
assert result['final_response'] == '您好。我在。\n\n您把问题给我,我先帮您收束重点,再往下推。'
|
||||
assert result['current_agent'] == AgentRole.MASTER
|
||||
assert result['active_agents'] == [AgentRole.MASTER]
|
||||
|
||||
|
||||
async def test_master_node_returns_stable_reply_for_identity_question(monkeypatch):
|
||||
monkeypatch.setattr('app.agents.graph._get_llm_for_state', lambda state: FailIfCalledLLM())
|
||||
|
||||
state = {
|
||||
'messages': [HumanMessage(content='你是谁')],
|
||||
'user_id': 'u1',
|
||||
'conversation_id': 'c1',
|
||||
'current_agent': AgentRole.MASTER,
|
||||
'active_agents': [AgentRole.MASTER],
|
||||
'pending_tasks': [],
|
||||
'completed_tasks': [],
|
||||
'tool_calls': [],
|
||||
'last_tool_result': None,
|
||||
'knowledge_context': None,
|
||||
'graph_context': None,
|
||||
'plan': None,
|
||||
'plan_steps': [],
|
||||
'analysis_report': None,
|
||||
'final_response': None,
|
||||
'should_respond': True,
|
||||
'memory_context': None,
|
||||
'user_llm_config': None,
|
||||
}
|
||||
|
||||
result = await master_node(state)
|
||||
|
||||
assert result['final_response'] == '我是 Jarvis。\n\n比起做一个泛泛的助手,我更像您的判断型协作伙伴:帮您看清问题、压缩路径、把事情往前推进。'
|
||||
assert result['current_agent'] == AgentRole.MASTER
|
||||
assert result['active_agents'] == [AgentRole.MASTER]
|
||||
|
||||
|
||||
async def test_master_node_returns_stable_reply_for_capability_question(monkeypatch):
|
||||
monkeypatch.setattr('app.agents.graph._get_llm_for_state', lambda state: FailIfCalledLLM())
|
||||
|
||||
state = {
|
||||
'messages': [HumanMessage(content='你能做什么')],
|
||||
'user_id': 'u1',
|
||||
'conversation_id': 'c1',
|
||||
'current_agent': AgentRole.MASTER,
|
||||
'active_agents': [AgentRole.MASTER],
|
||||
'pending_tasks': [],
|
||||
'completed_tasks': [],
|
||||
'tool_calls': [],
|
||||
'last_tool_result': None,
|
||||
'knowledge_context': None,
|
||||
'graph_context': None,
|
||||
'plan': None,
|
||||
'plan_steps': [],
|
||||
'analysis_report': None,
|
||||
'final_response': None,
|
||||
'should_respond': True,
|
||||
'memory_context': None,
|
||||
'user_llm_config': None,
|
||||
}
|
||||
|
||||
result = await master_node(state)
|
||||
|
||||
assert result['final_response'] == '主要做三件事。\n- 帮您判断:看问题本质、梳理取舍、给出方向\n- 帮您收束:把复杂内容理顺,把重点拎出来\n- 帮您推进:拆任务、定步骤、把下一步变清楚\n\n如果您现在有具体目标,我可以直接进入处理。'
|
||||
assert result['current_agent'] == AgentRole.MASTER
|
||||
assert result['active_agents'] == [AgentRole.MASTER]
|
||||
12
backend/tests/backend/app/agents/test_prompts.py
Normal file
12
backend/tests/backend/app/agents/test_prompts.py
Normal file
@@ -0,0 +1,12 @@
|
||||
from app.agents.prompts import MASTER_SYSTEM_PROMPT
|
||||
|
||||
|
||||
def test_master_prompt_forbids_subagent_rollcall_in_simple_greetings():
|
||||
assert '当用户只是打招呼(如“你好”“您好”“早”“在吗”)时:不要介绍 4 个子Agent' in MASTER_SYSTEM_PROMPT
|
||||
assert '只做一个自然、简短、有推进感的回应' in MASTER_SYSTEM_PROMPT
|
||||
|
||||
|
||||
def test_master_prompt_does_not_include_full_canned_answers_for_greetings_or_identity():
|
||||
assert 'Jarvis:您好。我在。' not in MASTER_SYSTEM_PROMPT
|
||||
assert 'Jarvis:我是 Jarvis。' not in MASTER_SYSTEM_PROMPT
|
||||
assert 'Jarvis:主要做三件事。' not in MASTER_SYSTEM_PROMPT
|
||||
Reference in New Issue
Block a user