Files
ai-agent/internal/ai/runtime/executor/knowledge_guard_test.go
T
mlogclub c84870b968 feat: add fallback mode for AI agents and update knowledge base handling
- Introduced AIAgentFallbackMode with options for NoAnswer and SuggestRetry.
- Updated AI agent edit component to include fallback mode selection.
- Modified API types and payloads to accommodate fallback mode.
- Removed deprecated KnowledgeFallbackMode and related logic from knowledge base.
- Adjusted knowledge retrieval and response handling to utilize new fallback logic.
- Updated tests to reflect changes in fallback mode handling for AI agents.
2026-04-18 13:36:44 +08:00

70 lines
2.3 KiB
Go

package executor
import (
"strings"
"testing"
"cs-agent/internal/ai/rag"
"cs-agent/internal/ai/runtime/internal/impl/retrievers"
"cs-agent/internal/models"
"cs-agent/internal/pkg/enums"
)
func TestBuildKnowledgeGuardDecisionFallsBackWhenKnowledgeMisses(t *testing.T) {
agent := newKnowledgeGuardAgentFixture()
agent.FallbackMode = enums.AIAgentFallbackModeSuggestRetry
decision := buildKnowledgeGuardDecision(agent, &retrievers.KnowledgeRetrieveResult{
KnowledgeBaseIDs: []int64{1},
})
if decision.FallbackReply != "当前知识库里没有找到足够明确的信息,你可以换个更具体的问法再试一次。" {
t.Fatalf("unexpected fallback reply: %q", decision.FallbackReply)
}
if len(decision.Instructions) != 0 {
t.Fatalf("expected no instructions on miss, got %d", len(decision.Instructions))
}
}
func TestBuildKnowledgeGuardDecisionUsesAgentFallbackMessage(t *testing.T) {
agent := newKnowledgeGuardAgentFixture()
agent.FallbackMessage = "请联系人工客服"
decision := buildKnowledgeGuardDecision(agent, &retrievers.KnowledgeRetrieveResult{
KnowledgeBaseIDs: []int64{1},
})
if decision.FallbackReply != "请联系人工客服" {
t.Fatalf("expected agent fallback message, got %q", decision.FallbackReply)
}
}
func TestBuildKnowledgeGuardDecisionInjectsStrictInstructionOnHit(t *testing.T) {
agent := newKnowledgeGuardAgentFixture()
decision := buildKnowledgeGuardDecision(agent, &retrievers.KnowledgeRetrieveResult{
KnowledgeBaseIDs: []int64{1},
Hits: []rag.RetrieveResult{
{KnowledgeBaseID: 1, Score: 0.88},
},
AnswerMode: enums.KnowledgeAnswerModeStrict,
})
if decision.FallbackReply != "" {
t.Fatalf("expected no fallback reply on hit, got %q", decision.FallbackReply)
}
if len(decision.Instructions) != 1 {
t.Fatalf("expected one instruction, got %d", len(decision.Instructions))
}
content := decision.Instructions[0].Content
if !strings.Contains(content, "只能依据后续提供的知识片段回答") {
t.Fatalf("unexpected strict instruction: %q", content)
}
if !strings.Contains(content, "当前知识库暂无明确信息。") {
t.Fatalf("expected fallback text in instruction, got %q", content)
}
}
func newKnowledgeGuardAgentFixture() models.AIAgent {
return models.AIAgent{
FallbackMode: enums.AIAgentFallbackModeNoAnswer,
}
}