fix: preserve agent debug conversation context
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import asyncio
|
||||
from decimal import Decimal
|
||||
import json
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
from sqlalchemy import create_engine
|
||||
@@ -164,6 +164,37 @@ def test_debug_stream_setting_overrides_model_without_changing_it():
|
||||
assert AgentDebugService.overrides(payload)["stream_enabled"] == 0
|
||||
|
||||
|
||||
def test_debug_preview_passes_conversation_history_and_replaces_saved_prompt():
|
||||
rag_result = RagResult(
|
||||
question="再回答上面的问题",
|
||||
knowledge_scopes=[],
|
||||
chunks=[],
|
||||
prompt="调试提示词渲染结果",
|
||||
allow_general_knowledge=True,
|
||||
messages=[{"role": "system", "content": "调试主提示词"}],
|
||||
)
|
||||
payload = AgentDebugRequest(
|
||||
promptContent="调试主提示词",
|
||||
modelId=1,
|
||||
question="再回答上面的问题",
|
||||
history=[
|
||||
{"role": "user", "content": "最开始的问题"},
|
||||
{"role": "assistant", "content": "第一次回答"},
|
||||
],
|
||||
)
|
||||
|
||||
with _database() as db:
|
||||
build_result = AsyncMock(return_value=rag_result)
|
||||
with patch("app.services.agent_debug_service.KnowledgeAgentService.build_result", build_result):
|
||||
result = asyncio.run(AgentDebugService.build_result(db, payload))
|
||||
|
||||
kwargs = build_result.await_args.kwargs
|
||||
assert [item.content for item in kwargs["history"]] == ["最开始的问题", "第一次回答"]
|
||||
assert kwargs["prompt_override"] == "调试主提示词"
|
||||
assert result.messages == rag_result.messages
|
||||
assert result.prompt == "调试提示词渲染结果"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("reasoning_visible", [True, False])
|
||||
def test_agent_debug_stream_respects_reasoning_visibility(reasoning_visible):
|
||||
async def chunks():
|
||||
|
||||
@@ -269,10 +269,30 @@ def test_contextual_follow_up_is_rewritten_before_agent_decision():
|
||||
rewrite = next(item for item in result.tool_trace if item["tool"] == "rewrite_contextual_question")
|
||||
decision = next(item for item in result.tool_trace if item["tool"] == "agent_decision")
|
||||
|
||||
assert rewrite["rewrittenQuestion"] == "关于“课程退款条件有哪些?”的追问:那第二种情况呢?"
|
||||
assert rewrite["rewrittenQuestion"] == (
|
||||
"结合以下历史问题回答当前追问:\n"
|
||||
"历史问题1:课程退款条件有哪些?\n"
|
||||
"当前追问:那第二种情况呢?"
|
||||
)
|
||||
assert decision["request"]["question"] == rewrite["rewrittenQuestion"]
|
||||
|
||||
|
||||
def test_long_follow_up_reference_is_detected_and_keeps_multiple_user_questions():
|
||||
history = [
|
||||
ChatMessage(id=1, session_id=1, user_id=1, role="user", content="觉式练习和慧氏练习分别是什么,区别是什么?"),
|
||||
ChatMessage(id=2, session_id=1, user_id=1, role="assistant", content="暂时无法确认。"),
|
||||
ChatMessage(id=3, session_id=1, user_id=1, role="user", content="阴影人格练习步骤是什么?"),
|
||||
ChatMessage(id=4, session_id=1, user_id=1, role="assistant", content="先稳定状态,再进行觉察。"),
|
||||
]
|
||||
question = "你能根据这个练习的基本方向再来回答一下我上面的问题吗?"
|
||||
|
||||
assert KnowledgeAgentService._needs_contextual_rewrite(question) is True
|
||||
rewritten = KnowledgeAgentService._fallback_rewrite(question, history)
|
||||
assert "觉式练习和慧氏练习" in rewritten
|
||||
assert "阴影人格练习步骤" in rewritten
|
||||
assert question in rewritten
|
||||
|
||||
|
||||
def test_homework_overview_expands_practice_terms_and_section_limit():
|
||||
terms = KnowledgeAgentService._query_terms("合一的作业是什么?")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user