Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix(agent): allow Invoke variables to use a reference or a literal (#20634)

Wangshu67cbeca2e996
diff --git a/internal/agent/component/dynamic_params.go b/internal/agent/component/dynamic_params.go
index c09e86a3a3..d549267402 100644
--- a/internal/agent/component/dynamic_params.go
+++ b/internal/agent/component/dynamic_params.go
@@ -81,7 +81,7 @@ func validateDynamicParams(component string, params map[string]any) error {
return fmt.Errorf("[%s] tools does not support empty entries", component)
}
case "invoke":
- if err := validateRows(component, params, "variables", []string{"key", "ref", "value"}); err != nil {
+ if err := validateInvokeVariables(component, params); err != nil {
return err
}
case "loop":
@@ -165,6 +165,41 @@ func validateRows(component string, params map[string]any, field string, require
return nil
}
+// validateInvokeVariables requires every Invoke variables row to carry a
+// nonblank key plus one value source: an upstream reference (ref) or a
+// literal (value). The two sources are alternatives — a reference row has
+// no literal and a literal row has no reference — so a row with neither
+// is still rejected. Omitted and null variables carry no rows to check;
+// any other non-array value is malformed and rejected.
+func validateInvokeVariables(component string, params map[string]any) error {
+ raw, present := params["variables"]
+ if !present || raw == nil {
+ return nil
+ }
+ rows, ok := raw.([]any)
+ if !ok {
+ return fmt.Errorf("[%s] variables must be an array", component)
+ }
+ for i, raw := range rows {
+ row, ok := raw.(map[string]any)
+ if !ok || !isNonBlankString(row["key"]) || !invokeRowHasSource(row) {
+ return fmt.Errorf("[%s] variables[%d] is incomplete", component, i)
+ }
+ }
+ return nil
+}
+
+// invokeRowHasSource reports whether an Invoke variables row supplies a
+// reference or a literal. Non-string literals (numbers, booleans) count;
+// nil does not.
+func invokeRowHasSource(row map[string]any) bool {
+ if isNonBlankString(row["ref"]) {
+ return true
+ }
+ value := row["value"]
+ return value != nil && !isBlank(value)
+}
+
func validateVariableAggregatorGroups(component string, params map[string]any) error {
if groups, ok := params["groups"].([]any); ok {
for i, rawGroup := range groups {
diff --git a/internal/agent/component/dynamic_params_test.go b/internal/agent/component/dynamic_params_test.go
index 9a4802edde..05a0994a2d 100644
--- a/internal/agent/component/dynamic_params_test.go
+++ b/internal/agent/component/dynamic_params_test.go
@@ -13,7 +13,17 @@ func TestValidateDynamicEntries(t *testing.T) {
"filter_values": []any{map[string]any{"key": "status", "operator": "eq", "value": "published"}},
}),
componentDSL("Agent", map[string]any{"tools": []any{"web_search"}}),
- componentDSL("Invoke", map[string]any{"variables": []any{map[string]any{"key": "query", "ref": "Begin@query", "value": "query"}}}),
+ // Invoke rows take their value from either a reference or a
+ // literal, so a row may carry only one of the two.
+ componentDSL("Invoke", map[string]any{"variables": []any{
+ map[string]any{"key": "query", "ref": "Begin@query", "value": "query"},
+ map[string]any{"key": "question", "ref": "Begin@query", "value": ""},
+ map[string]any{"key": "top_k", "ref": "", "value": "12"},
+ map[string]any{"key": "limit", "ref": "", "value": 10},
+ map[string]any{"key": "enabled", "ref": "", "value": false},
+ }}),
+ // A null variables value carries no rows, same as an omitted one.
+ componentDSL("Invoke", map[string]any{"variables": nil}),
componentDSL("VariableAggregator", map[string]any{"groups": []any{map[string]any{
"group_name": "answer",
"variables": []any{map[string]any{"value": "LLM@content"}},
@@ -54,6 +64,18 @@ func TestValidateDynamicEntries(t *testing.T) {
{"tool name", componentDSL("Agent", map[string]any{"tools": []any{"web_search", ""}}), "tools"},
{"filter operator", componentDSL("DataOperations", map[string]any{"filter_values": []any{map[string]any{"key": "status", "operator": "", "value": "published"}}}), "filter_values[0]"},
{"invoke variable", componentDSL("Invoke", map[string]any{"variables": []any{map[string]any{"key": "", "ref": "Begin@query", "value": "query"}}}), "variables[0]"},
+ {"invoke variable without source", componentDSL("Invoke", map[string]any{"variables": []any{map[string]any{"key": "query", "ref": "", "value": ""}}}), "variables[0]"},
+ {"invoke variable with nil literal", componentDSL("Invoke", map[string]any{"variables": []any{map[string]any{"key": "query", "ref": "", "value": nil}}}), "variables[0]"},
+ {"invoke variable row not a map", componentDSL("Invoke", map[string]any{"variables": []any{"query"}}), "variables[0]"},
+ {"invoke variable nil row", componentDSL("Invoke", map[string]any{"variables": []any{nil}}), "variables[0]"},
+ {"invoke variable without key", componentDSL("Invoke", map[string]any{"variables": []any{map[string]any{"ref": "Begin@query", "value": ""}}}), "variables[0]"},
+ {"invoke variable with numeric key", componentDSL("Invoke", map[string]any{"variables": []any{map[string]any{"key": 42, "ref": "Begin@query", "value": ""}}}), "variables[0]"},
+ {"invoke variables not an array", componentDSL("Invoke", map[string]any{"variables": "query"}), "variables must be an array"},
+ {"invoke variables object", componentDSL("Invoke", map[string]any{"variables": map[string]any{"key": "query"}}), "variables must be an array"},
+ {"invoke variable without source after valid row", componentDSL("Invoke", map[string]any{"variables": []any{
+ map[string]any{"key": "query", "ref": "Begin@query", "value": ""},
+ map[string]any{"key": "top_k", "ref": "", "value": " "},
+ }}), "variables[1]"},
{"malformed group", componentDSL("VariableAggregator", map[string]any{"groups": []any{"not a group"}}), "groups[0]"},
{"aggregated variable", componentDSL("VariableAggregator", map[string]any{"groups": []any{map[string]any{"group_name": "answer", "variables": []any{map[string]any{"value": ""}}}}}), "groups[0].variables[0]"},
{"malformed aggregated variable", componentDSL("VariableAggregator", map[string]any{"groups": []any{map[string]any{"group_name": "answer", "variables": []any{nil}}}}), "groups[0].variables[0]"},