fix(agent): preserve agent intent prompt whitespace and add tests
The intent prompt override logic in query_understand applied strings.TrimSpace to the value before assigning it as the system prompt override, which silently stripped trailing newlines and intentional formatting from agent-supplied prompts. Use TrimSpace only to detect emptiness (so whitespace-only strings still fall back to the global default) while passing the raw string through verbatim. Extract the resolution into applyIntentPromptOverride and add unit tests covering agent-wins, whitespace preservation, blank fallback, no-config, and global-only paths.
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
package chatpipeline
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
)
|
||||
|
||||
func TestApplyIntentPromptOverride_AgentOverrideWins(t *testing.T) {
|
||||
cm := &types.ChatManage{
|
||||
PipelineRequest: types.PipelineRequest{
|
||||
IntentPromptOverrides: map[string]string{"chitchat": "agent prompt"},
|
||||
},
|
||||
PipelineState: types.PipelineState{Intent: types.IntentChitchat},
|
||||
}
|
||||
global := map[string]string{"chitchat": "global prompt"}
|
||||
|
||||
if !applyIntentPromptOverride(cm, global) {
|
||||
t.Fatal("expected applied=true")
|
||||
}
|
||||
if cm.SystemPromptOverride != "agent prompt" {
|
||||
t.Errorf("override: got %q, want %q", cm.SystemPromptOverride, "agent prompt")
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyIntentPromptOverride_PreservesAgentWhitespace(t *testing.T) {
|
||||
// Agent-supplied prompts with surrounding whitespace must reach the model
|
||||
// verbatim; trim is only used for emptiness detection.
|
||||
raw := " agent prompt with trailing newline\n"
|
||||
cm := &types.ChatManage{
|
||||
PipelineRequest: types.PipelineRequest{
|
||||
IntentPromptOverrides: map[string]string{"chitchat": raw},
|
||||
},
|
||||
PipelineState: types.PipelineState{Intent: types.IntentChitchat},
|
||||
}
|
||||
|
||||
if !applyIntentPromptOverride(cm, nil) {
|
||||
t.Fatal("expected applied=true")
|
||||
}
|
||||
if cm.SystemPromptOverride != raw {
|
||||
t.Errorf("override: got %q, want %q", cm.SystemPromptOverride, raw)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyIntentPromptOverride_BlankAgentFallsBackToGlobal(t *testing.T) {
|
||||
cm := &types.ChatManage{
|
||||
PipelineRequest: types.PipelineRequest{
|
||||
IntentPromptOverrides: map[string]string{"chitchat": " \n\t "},
|
||||
},
|
||||
PipelineState: types.PipelineState{Intent: types.IntentChitchat},
|
||||
}
|
||||
global := map[string]string{"chitchat": "global prompt"}
|
||||
|
||||
if !applyIntentPromptOverride(cm, global) {
|
||||
t.Fatal("expected applied=true")
|
||||
}
|
||||
if cm.SystemPromptOverride != "global prompt" {
|
||||
t.Errorf("override: got %q, want %q", cm.SystemPromptOverride, "global prompt")
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyIntentPromptOverride_NoOverrideAndNoGlobal(t *testing.T) {
|
||||
cm := &types.ChatManage{
|
||||
PipelineState: types.PipelineState{Intent: types.IntentChitchat},
|
||||
}
|
||||
|
||||
if applyIntentPromptOverride(cm, nil) {
|
||||
t.Fatal("expected applied=false")
|
||||
}
|
||||
if cm.SystemPromptOverride != "" {
|
||||
t.Errorf("override should remain empty, got %q", cm.SystemPromptOverride)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyIntentPromptOverride_GlobalOnly(t *testing.T) {
|
||||
cm := &types.ChatManage{
|
||||
PipelineState: types.PipelineState{Intent: types.IntentGreeting},
|
||||
}
|
||||
global := map[string]string{"greeting": "hi there"}
|
||||
|
||||
if !applyIntentPromptOverride(cm, global) {
|
||||
t.Fatal("expected applied=true")
|
||||
}
|
||||
if cm.SystemPromptOverride != "hi there" {
|
||||
t.Errorf("override: got %q, want %q", cm.SystemPromptOverride, "hi there")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user