diff --git a/internal/service/agent.go b/internal/service/agent.go index 514c0a6710..7a4fd9d8c2 100644 --- a/internal/service/agent.go +++ b/internal/service/agent.go @@ -2280,7 +2280,7 @@ func (s *AgentService) buildRunFunc(canvasID string, versionRow *entity.UserCanv attachment = v } } - referencePayload := agentRunReferencePayload(state, legacyReference) + referencePayload := agentRunReferencePayload(c, state, legacyReference) assistantOutput := terminalCanvasOutput(c, state, workflowOutput, answer, downloads, attachment) // Release any deferred Agent node that was not consumed because the // downstream Message was skipped by an exception/branch path. @@ -2651,20 +2651,39 @@ func buildPersistedAgentDSL(runDSL map[string]any, state *canvas.CanvasState) en return dsl } -func agentRunReferencePayload(state *canvas.CanvasState, legacyChunks []interface{}) map[string]interface{} { +func agentRunReferencePayload(c *canvas.Canvas, state *canvas.CanvasState, legacyChunks []interface{}) map[string]interface{} { + var reference map[string]interface{} if state != nil { - if reference := state.GetRetrievalReference(); len(reference) > 0 { - return reference + reference = state.GetRetrievalReference() + } + if len(reference) == 0 { + if len(legacyChunks) == 0 { + return nil + } + reference = map[string]interface{}{ + "chunks": legacyChunks, + "doc_aggs": []interface{}{}, + "total": len(legacyChunks), } } - if len(legacyChunks) == 0 { - return nil - } - return map[string]interface{}{ - "chunks": legacyChunks, - "doc_aggs": []interface{}{}, - "total": len(legacyChunks), + if c != nil && state != nil { + snapshot := state.Snapshot() + hasAgent, cite := false, false + for cpnID, component := range c.Components { + if !strings.EqualFold(component.Obj.ComponentName, "Agent") || len(snapshot[cpnID]) == 0 { + continue + } + hasAgent = true + if enabled, _ := component.Obj.Params["cite"].(bool); enabled { + cite = true + break + } + } + if hasAgent && !cite { + delete(reference, "doc_aggs") + } } + return reference } func stringifyAgentUserInput(userInput any) string {