feat(agent): add per-agent citation output toggle

Move citation protocol injection to the system layer so custom prompts no
longer own citation syntax. Agents can disable inline citations while source
aliases still encode and decode tool arguments correctly.
This commit is contained in:
wizardchen
2026-07-16 21:46:52 +08:00
committed by lyingbug
parent 014164ae44
commit b862c2e45d
26 changed files with 292 additions and 78 deletions
+2 -2
View File
@@ -88,7 +88,7 @@ func NewAgentEngine(
systemPromptTemplate: systemPromptTemplate,
tokenEstimator: tokenEst,
resourceRefs: llmresource.NewRegistry(),
sourceRefs: llmreference.NewRegistry(),
sourceRefs: llmreference.NewRegistry(config.CitationsEnabled()),
}
// Initialize memory consolidator if context window management is configured
@@ -125,7 +125,7 @@ func (e *AgentEngine) buildSystemPrompt(ctx context.Context) string {
e.systemPromptOptions(ctx),
e.systemPromptTemplate,
)
return strings.TrimRight(prompt, " \t\r\n") + llmreference.ProtocolPrompt
return strings.TrimRight(prompt, " \t\r\n") + llmreference.ProtocolPrompt(e.config.CitationsEnabled())
}
// NewAgentEngineWithSkills creates a new agent engine with skills support
+17
View File
@@ -105,6 +105,23 @@ func withMaxIterations(n int) testEngineOption {
}
}
func withCitationsEnabled(enabled bool) testEngineOption {
return func(cfg *types.AgentConfig) {
cfg.CitationEnabled = &enabled
}
}
func TestBuildSystemPromptUsesInternalCitationSetting(t *testing.T) {
model := &mockChat{}
enabledEngine := newTestEngine(t, model)
require.Contains(t, enabledEngine.buildSystemPrompt(context.Background()), "Source citations are enabled")
disabledEngine := newTestEngine(t, model, withCitationsEnabled(false))
prompt := disabledEngine.buildSystemPrompt(context.Background())
require.Contains(t, prompt, "Source citations are disabled")
require.NotContains(t, prompt, "Source citations are enabled")
}
func newTestEngine(t *testing.T, chatModel chat.Chat, opts ...testEngineOption) *AgentEngine {
t.Helper()
cfg := &types.AgentConfig{
+5 -6
View File
@@ -19,8 +19,8 @@ func finalAnswerImageRequirement(hasRetrievedImage bool) string {
return ""
}
return `
6. Retrieved tool results contain Markdown images. Unless the user explicitly requested text-only output or every image is clearly unrelated, the final answer MUST include at least one relevant Markdown image copied verbatim from the tool results. Preserve its complete URL exactly. Use ASCII half-width parentheses exactly as ![alt](url) and never use full-width ( or ). Place the image immediately after the paragraph it supports. When multiple images support different sections, distribute them across those sections instead of stopping after the first image.
7. Before finishing, silently verify that the answer contains a Markdown image when requirement 6 applies.`
5. Retrieved tool results contain Markdown images. Unless the user explicitly requested text-only output or every image is clearly unrelated, the final answer MUST include at least one relevant Markdown image copied verbatim from the tool results. Preserve its complete URL exactly. Use ASCII half-width parentheses exactly as ![alt](url) and never use full-width ( or ). Place the image immediately after the paragraph it supports. When multiple images support different sections, distribute them across those sections instead of stopping after the first image.
6. Before finishing, silently verify that the answer contains a Markdown image when requirement 5 applies.`
}
// streamFinalAnswerToEventBus streams the final answer generation through EventBus
@@ -80,10 +80,9 @@ User question: %s
Requirements:
1. Answer based on the actually retrieved content
2. Cite supporting knowledge chunks and web pages only with their supplied compact form: <ref id="cN"/> or <ref id="wN"/>
3. Organize the answer in a structured format
4. If information is insufficient, honestly state so
5. IMPORTANT: Respond in the same language as the user's question
2. Organize the answer in a structured format
3. If information is insufficient, honestly state so
4. IMPORTANT: Respond in the same language as the user's question
%s
Now generate the final answer:`, query, imageRequirement)
+1 -1
View File
@@ -75,7 +75,7 @@ Avoid:
## Output
Returns chunks ranked by semantic similarity, reranked when applicable.
Each chunk has a short cN source ID and belongs to a dN document ID. Results represent conceptual relevance, not literal keyword overlap. Use dN for document-level follow-up tool calls and cN for chunk citations.`,
Each chunk has a short cN source ID and belongs to a dN document ID. Results represent conceptual relevance, not literal keyword overlap. Use dN for document-level follow-up tool calls.`,
schema: json.RawMessage(`{
"type": "object",
"properties": {