fix: continue agent flow on knowledge retrieval errors

This commit is contained in:
mlogclub
2026-05-09 23:38:42 +08:00
parent bce4c18c10
commit af8ec71bb8
3 changed files with 56 additions and 4 deletions
@@ -180,7 +180,7 @@ func (g *KnowledgeAnswerabilityGate) retrieveKnowledge(ctx context.Context, stat
retrieveOptions.QueryPreview = preview(req.UserMessage.Content, 120)
result, err := retriever.RetrieveContextByOptions(ctx, retrieveOptions, query)
if err != nil {
state.FallbackReply = resolveKnowledgeHumanSupportFallback(req.AIAgent)
state.Decision = buildKnowledgeRetrievalErrorDecision(req.AIAgent, knowledgeIDs)
state.ErrorMessage = err.Error()
state.recordAnswerability(answerabilityStatusUnanswerable, "knowledge retrieval failed", err)
return state, nil
@@ -248,7 +248,7 @@ func TestKnowledgePolicyEvaluateSkipsRuntimeActionIntent(t *testing.T) {
}
}
func TestKnowledgePolicyEvaluateFallsBackOnRetrieverError(t *testing.T) {
func TestKnowledgePolicyEvaluateInjectsRetrievalErrorInstructionWithoutFallback(t *testing.T) {
collector := callbacks.NewRuntimeTraceCollector()
gate := newTestKnowledgePolicyGate(&fakeKnowledgeContextRetriever{
knowledgeBaseIDs: []int64{1},
@@ -263,8 +263,14 @@ func TestKnowledgePolicyEvaluateFallsBackOnRetrieverError(t *testing.T) {
t.Fatalf("Evaluate returned error: %v", err)
}
if !strings.Contains(state.FallbackReply, "我暂时没有找到足够准确的信息") {
t.Fatalf("expected configured fallback on retrieval error, got %q", state.FallbackReply)
if state.FallbackReply != "" {
t.Fatalf("expected no direct fallback on retrieval error, got %q", state.FallbackReply)
}
if len(state.Decision.Instructions) != 1 {
t.Fatalf("expected one retrieval-error instruction, got %d", len(state.Decision.Instructions))
}
if !strings.Contains(state.Decision.Instructions[0].Content, "知识库检索暂时不可用") {
t.Fatalf("unexpected retrieval-error instruction: %q", state.Decision.Instructions[0].Content)
}
if collector.Data.Answerability.Status != answerabilityStatusUnanswerable {
t.Fatalf("unexpected status: %q", collector.Data.Answerability.Status)
@@ -273,3 +279,23 @@ func TestKnowledgePolicyEvaluateFallsBackOnRetrieverError(t *testing.T) {
t.Fatalf("unexpected reason: %q", collector.Data.Answerability.Reason)
}
}
func TestBuildRunMessagesContinuesAgentFlowWhenRetrievalFails(t *testing.T) {
summary := &RunResult{}
gate := newTestKnowledgePolicyGate(&fakeKnowledgeContextRetriever{
knowledgeBaseIDs: []int64{1},
err: errors.New("vector store unavailable"),
})
messages := buildRunMessages(context.Background(), newKnowledgePolicyRunInput("你好", "1"), summary, nil, gate)
if summary.ReplyText != "" {
t.Fatalf("expected no early fallback reply, got %q", summary.ReplyText)
}
if !messagesContainContent(messages, "知识库检索暂时不可用") {
t.Fatalf("expected retrieval-error instruction in messages: %#v", messages)
}
if !messagesContainContent(messages, "你好") {
t.Fatalf("expected current user message to remain in messages: %#v", messages)
}
}
@@ -53,6 +53,19 @@ func buildKnowledgeNoContextDecision(aiAgent models.AIAgent, knowledgeBaseIDs []
}
}
func buildKnowledgeRetrievalErrorDecision(aiAgent models.AIAgent, knowledgeBaseIDs []int64) knowledgeGuardDecision {
if len(knowledgeBaseIDs) == 0 {
return knowledgeGuardDecision{}
}
instruction := buildKnowledgeRetrievalErrorInstruction(resolveKnowledgeFallbackReply(aiAgent))
if instruction == "" {
return knowledgeGuardDecision{}
}
return knowledgeGuardDecision{
Instructions: []*schema.Message{schema.SystemMessage(instruction)},
}
}
func resolveKnowledgeFallbackReply(aiAgent models.AIAgent) string {
if reply := strings.TrimSpace(aiAgent.FallbackMessage); reply != "" {
return reply
@@ -96,3 +109,16 @@ func buildKnowledgeNoContextInstruction(fallbackReply string) string {
"3. 如果用户询问业务事实、规则、价格、流程、配置、时效、承诺、售后、退款、权限或政策,不得编造答案,必须明确回复:" + fallbackReply + "\n" +
"4. 不得输出知识库未提供的具体事实、流程、承诺、价格、时效或政策。"
}
func buildKnowledgeRetrievalErrorInstruction(fallbackReply string) string {
fallbackReply = strings.TrimSpace(fallbackReply)
if fallbackReply == "" {
fallbackReply = "当前知识库暂无明确信息。"
}
return "知识库检索状态:知识库检索暂时不可用,当前没有可用的知识库资料。\n" +
"回复策略:\n" +
"1. 如果用户只是寒暄、问候、感谢、确认或结束语,可以自然、简短地回复,不要使用知识库兜底话术。\n" +
"2. 如果用户表达不清楚或缺少上下文,应追问具体场景、对象、报错信息或操作步骤。\n" +
"3. 如果用户询问业务事实、规则、价格、流程、配置、时效、承诺、售后、退款、权限或政策,不得编造答案,必须明确回复:" + fallbackReply + "\n" +
"4. 不得输出知识库未提供的具体事实、流程、承诺、价格、时效或政策。"
}