fix: align agent no-hit handling

This commit is contained in:
2026-07-30 14:36:09 +08:00
parent d1f573108e
commit 77a899e24c
5 changed files with 71 additions and 25 deletions

View File

@@ -16,6 +16,7 @@ from app.schemas.admin import AgentDebugRequest, AgentRuntimeConfigSaveRequest
from app.services.agent_debug_service import AgentDebugService
from app.services.model_stream_service import (
AsyncStreamingModelResponse,
ModelStreamService,
_openai_stream_payload,
_stream_configured_model_async,
)
@@ -194,12 +195,71 @@ def test_debug_preview_passes_conversation_history_and_replaces_saved_prompt():
kwargs = build_result.await_args.kwargs
assert [item.content for item in kwargs["history"]] == ["最开始的问题", "第一次回答"]
assert kwargs["preview_knowledge_ids"] is None
assert kwargs["version_overrides"] is None
assert kwargs["prompt_override"] == "调试主提示词"
assert kwargs["response_depth"] == 35
assert result.messages == rag_result.messages
assert result.prompt == "调试提示词渲染结果"
def test_agent_debug_empty_knowledge_selection_uses_formal_open_catalog():
payload = AgentDebugRequest(
promptContent="调试主提示词",
modelId=1,
knowledgeIds=[],
knowledgeVersions={},
question="测试问题",
)
rag_result = RagResult(
question="测试问题",
knowledge_scopes=[],
chunks=[],
prompt="测试",
allow_general_knowledge=True,
)
with _database() as db:
build_result = AsyncMock(return_value=rag_result)
with patch("app.services.agent_debug_service.KnowledgeAgentService.build_result", build_result):
asyncio.run(AgentDebugService.build_result(db, payload))
kwargs = build_result.await_args.kwargs
assert kwargs["preview_knowledge_ids"] is None
assert kwargs["version_overrides"] is None
def test_user_stream_no_hit_still_calls_model_for_cautious_answer():
async def configured_chunks(_model, _rag_result):
yield "我暂时没有可靠课程依据,但可以先帮你整理需要确认的点。"
async def collect():
with _database() as db:
model = _model()
db.add_all([
model,
SystemConfig(config_key="mock_model_enabled", config_value="false"),
])
db.commit()
rag_result = RagResult(
question="课程里的特殊练习怎么做?",
knowledge_scopes=[],
chunks=[],
prompt="本轮没有可靠的正式知识章节。",
allow_general_knowledge=False,
)
with patch(
"app.services.model_stream_service._stream_configured_model_async",
configured_chunks,
):
response = ModelStreamService.stream_async(db, rag_result)
return [chunk async for chunk in response.chunks]
chunks = asyncio.run(collect())
assert chunks == ["我暂时没有可靠课程依据,但可以先帮你整理需要确认的点。"]
def test_prompt_includes_response_depth_instruction():
with _database() as db:
db.add(SystemConfig(config_key="agent_response_depth", config_value="20"))