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:
wizardchen
2026-05-26 20:57:50 +08:00
committed by lyingbug
parent 11c3236e52
commit e6ee87759d
2 changed files with 106 additions and 10 deletions
@@ -187,16 +187,7 @@ func (p *PluginQueryUnderstand) OnEvent(ctx context.Context,
// --- Apply intent-specific system prompt override ---
if !chatManage.NeedsRetrieval() {
intentKey := string(chatManage.Intent)
if chatManage.IntentPromptOverrides != nil {
chatManage.SystemPromptOverride = strings.TrimSpace(chatManage.IntentPromptOverrides[intentKey])
}
if chatManage.SystemPromptOverride == "" {
if prompt, ok := p.config.Conversation.IntentSystemPrompts[intentKey]; ok {
chatManage.SystemPromptOverride = prompt
}
}
if chatManage.SystemPromptOverride != "" {
if applyIntentPromptOverride(chatManage, p.config.Conversation.IntentSystemPrompts) {
pipelineInfo(ctx, "QueryUnderstand", "prompt_override", map[string]interface{}{
"session_id": chatManage.SessionID,
"intent": chatManage.Intent,
@@ -477,6 +468,24 @@ func mergeImageDescAndOCR(desc, ocr string) (string, bool) {
return desc + "\n\n[OCR]\n" + ocr, true
}
// applyIntentPromptOverride resolves the system-prompt override for the current
// non-retrieval intent. Agent-level overrides take precedence; otherwise the
// tenant/global IntentSystemPrompts map is consulted. Whitespace-only agent
// overrides are treated as unset and fall through to the global default. Returns
// true when a non-empty override was applied.
func applyIntentPromptOverride(chatManage *types.ChatManage, globalPrompts map[string]string) bool {
intentKey := string(chatManage.Intent)
if raw, ok := chatManage.IntentPromptOverrides[intentKey]; ok && strings.TrimSpace(raw) != "" {
chatManage.SystemPromptOverride = raw
}
if chatManage.SystemPromptOverride == "" {
if prompt, ok := globalPrompts[intentKey]; ok {
chatManage.SystemPromptOverride = prompt
}
}
return chatManage.SystemPromptOverride != ""
}
// formatConversationHistory formats conversation history for prompt template.
func formatConversationHistory(historyList []*types.History) string {
if len(historyList) == 0 {