Skip to content

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

fix(agent): make user added emppty entries fail to save (#18979)

Sbaaoui Idriss52fe959e7ffe
diff --git a/internal/agent/component/dynamic_params.go b/internal/agent/component/dynamic_params.go
new file mode 100644
index 0000000000..cb059a9b9e
--- /dev/null
+++ b/internal/agent/component/dynamic_params.go
@@ -0,0 +1,177 @@
+package component
+
+import (
+ "fmt"
+ "strings"
+)
+
+// ValidateDynamicEntries rejects blank values in repeatable Agent parameters.
+func ValidateDynamicEntries(dsl map[string]any) error {
+ return validateDynamicEntries(dsl)
+}
+
+func validateDynamicEntries(value any) error {
+ switch node := value.(type) {
+ case map[string]any:
+ if name, ok := node["component_name"].(string); ok {
+ if params, ok := node["params"].(map[string]any); ok {
+ if err := validateDynamicParams(name, params); err != nil {
+ return err
+ }
+ }
+ }
+ for _, child := range node {
+ if err := validateDynamicEntries(child); err != nil {
+ return err
+ }
+ }
+ case []any:
+ for _, child := range node {
+ if err := validateDynamicEntries(child); err != nil {
+ return err
+ }
+ }
+ }
+ return nil
+}
+
+func validateDynamicParams(component string, params map[string]any) error {
+ for _, field := range []string{
+ "include_domains", "exclude_domains", "site_include", "site_exclude",
+ "country_include", "language_include", "delimiters", "children_delimiters",
+ } {
+ if values, ok := params[field].([]any); ok && containsBlank(values) {
+ return fmt.Errorf("[%s] %s does not support empty entries", component, field)
+ }
+ }
+ switch strings.ToLower(component) {
+ case "dataoperations":
+ for _, field := range []string{"select_keys", "remove_keys"} {
+ if values, ok := params[field].([]any); ok && containsBlank(values) {
+ return fmt.Errorf("[%s] %s does not support empty entries", component, field)
+ }
+ }
+ for field, required := range map[string][]string{
+ "updates": {"key", "value"}, "rename_keys": {"old_key", "new_key"},
+ "filter_values": {"key", "operator", "value"},
+ } {
+ if err := validateRows(component, params, field, required); err != nil {
+ return err
+ }
+ }
+ case "agent":
+ if values, ok := params["tools"].([]any); ok && containsBlank(values) {
+ 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 {
+ return err
+ }
+ case "loop":
+ if err := validateRows(component, params, "loop_variables", []string{"variable"}); err != nil {
+ return err
+ }
+ if err := validateRows(component, params, "loop_termination_condition", []string{"variable", "operator", "value"}); err != nil {
+ return err
+ }
+ case "iteration":
+ return validateIterationOutputs(component, params)
+ case "variableaggregator":
+ return validateVariableAggregatorGroups(component, params)
+ case "userfillup":
+ return validateInputOptions(component, params)
+ }
+ return nil
+}
+
+func validateRows(component string, params map[string]any, field string, required []string) error {
+ rows, ok := params[field].([]any)
+ if !ok {
+ return nil
+ }
+ for i, raw := range rows {
+ row, ok := raw.(map[string]any)
+ if !ok {
+ continue
+ }
+ for _, key := range required {
+ if isBlank(row[key]) {
+ return fmt.Errorf("[%s] %s[%d] is incomplete", component, field, i)
+ }
+ }
+ }
+ return nil
+}
+
+func validateVariableAggregatorGroups(component string, params map[string]any) error {
+ if groups, ok := params["groups"].([]any); ok {
+ for i, rawGroup := range groups {
+ group, ok := rawGroup.(map[string]any)
+ if !ok || group == nil {
+ return fmt.Errorf("[%s] groups[%d] is incomplete", component, i)
+ }
+ if !isNonBlankString(group["group_name"]) {
+ return fmt.Errorf("[%s] groups[%d] is incomplete", component, i)
+ }
+ variables, ok := group["variables"].([]any)
+ if !ok {
+ return fmt.Errorf("[%s] groups[%d].variables is incomplete", component, i)
+ }
+ for j, rawVariable := range variables {
+ variable, ok := rawVariable.(map[string]any)
+ if !ok || variable == nil || !isNonBlankString(variable["value"]) {
+ return fmt.Errorf("[%s] groups[%d].variables[%d] is incomplete", component, i, j)
+ }
+ }
+ }
+ }
+ return nil
+}
+
+func validateIterationOutputs(component string, params map[string]any) error {
+ outputs, ok := params["outputs"].(map[string]any)
+ if !ok {
+ return nil
+ }
+ for name, rawOutput := range outputs {
+ output, ok := rawOutput.(map[string]any)
+ if isBlank(name) || !ok || output == nil || !isNonBlankString(output["ref"]) {
+ return fmt.Errorf("[%s] outputs.%s is incomplete", component, name)
+ }
+ }
+ return nil
+}
+
+func validateInputOptions(component string, params map[string]any) error {
+ if inputs, ok := params["inputs"].(map[string]any); ok {
+ for key, raw := range inputs {
+ entry, ok := raw.(map[string]any)
+ if !ok {
+ continue
+ }
+ if values, ok := entry["options"].([]any); ok && containsBlank(values) {
+ return fmt.Errorf("[%s] inputs.%s.options does not support empty entries", component, key)
+ }
+ }
+ }
+ return nil
+}
+
+func isBlank(value any) bool {
+ s, ok := value.(string)
+ return ok && strings.TrimSpace(s) == ""
+}
+
+func isNonBlankString(value any) bool {
+ s, ok := value.(string)
+ return ok && strings.TrimSpace(s) != ""
+}
+
+func containsBlank(values []any) bool {
+ for _, value := range values {
+ if isBlank(value) {
+ return true
+ }
+ }
+ return false
+}
diff --git a/internal/agent/component/dynamic_params_test.go b/internal/agent/component/dynamic_params_test.go
new file mode 100644
index 0000000000..bdd737aba5
--- /dev/null
+++ b/internal/agent/component/dynamic_params_test.go
@@ -0,0 +1,75 @@
+package component
+
+import (
+ "strings"
+ "testing"
+)
+
+func TestValidateDynamicEntries(t *testing.T) {
+ valid := dslWithComponents(
+ componentDSL("DataOperations", map[string]any{
+ "select_keys": []any{"title"},
+ "remove_keys": []any{"internal"},
+ "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"}}}),
+ componentDSL("VariableAggregator", map[string]any{"groups": []any{map[string]any{
+ "group_name": "answer",
+ "variables": []any{map[string]any{"value": "LLM@content"}},
+ }}}),
+ componentDSL("Loop", map[string]any{
+ "loop_variables": []any{map[string]any{"variable": "count", "value": 0}},
+ "loop_termination_condition": []any{map[string]any{"variable": "count", "operator": "gte", "value": 3}},
+ }),
+ componentDSL("UserFillUp", map[string]any{"inputs": map[string]any{
+ "choice": map[string]any{"options": []any{"one", "two"}},
+ }}),
+ componentDSL("Iteration", map[string]any{"outputs": map[string]any{
+ "result": map[string]any{"type": "string", "ref": "Iteration@result"},
+ }}),
+ )
+
+ if err := ValidateDynamicEntries(valid); err != nil {
+ t.Fatalf("valid DSL rejected: %v", err)
+ }
+
+ tests := []struct {
+ name string
+ component map[string]any
+ want string
+ }{
+ {"string array", componentDSL("DataOperations", map[string]any{"select_keys": []any{"title", " "}}), "select_keys"},
+ {"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]"},
+ {"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]"},
+ {"loop variable", componentDSL("Loop", map[string]any{"loop_variables": []any{map[string]any{"variable": ""}}}), "loop_variables[0]"},
+ {"loop operator", componentDSL("Loop", map[string]any{"loop_termination_condition": []any{map[string]any{"variable": "count", "operator": "", "value": 3}}}), "loop_termination_condition[0]"},
+ {"iteration output", componentDSL("Iteration", map[string]any{"outputs": map[string]any{"result": map[string]any{"ref": ""}}}), "outputs.result"},
+ {"input option", componentDSL("UserFillUp", map[string]any{"inputs": map[string]any{"choice": map[string]any{"options": []any{"one", ""}}}}), "inputs.choice.options"},
+ }
+
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ err := ValidateDynamicEntries(dslWithComponents(test.component))
+ if err == nil || !strings.Contains(err.Error(), test.want) {
+ t.Fatalf("ValidateDynamicEntries() error = %v, want %q", err, test.want)
+ }
+ })
+ }
+}
+
+func dslWithComponents(components ...map[string]any) map[string]any {
+ items := make(map[string]any, len(components))
+ for i, component := range components {
+ items[string(rune('a'+i))] = map[string]any{"obj": component}
+ }
+ return map[string]any{"components": items}
+}
+
+func componentDSL(name string, params map[string]any) map[string]any {
+ return map[string]any{"component_name": name, "params": params}
+}
diff --git a/internal/service/agent.go b/internal/service/agent.go
index e54582adef..ad0589ea9b 100644
--- a/internal/service/agent.go
+++ b/internal/service/agent.go
@@ -974,6 +974,9 @@ func (s *AgentService) CreateAgent(ctx context.Context, req *CreateAgentRequest)
if err := component.ValidateIntegerParameters(req.DSL); err != nil {
return nil, common.CodeArgumentError, fmt.Errorf("create agent: %w", err)
}
+ if err := component.ValidateDynamicEntries(req.DSL); err != nil {
+ return nil, common.CodeArgumentError, fmt.Errorf("create agent: %w", err)
+ }
// Normalize legacy v1 / Go-v2 payloads to a React-Flow-shaped graph so
// the front-end can render the canvas without a migration. Idempotent;
// no-op when graph.nodes is already non-empty.
@@ -1162,6 +1165,9 @@ func (s *AgentService) UpdateAgent(ctx context.Context, userID, canvasID string,
if err := component.ValidateIntegerParameters(dslMap); err != nil {
return fmt.Errorf("update agent %s: %w", canvasID, err)
}
+ if err := component.ValidateDynamicEntries(dslMap); err != nil {
+ return fmt.Errorf("update agent %s: %w", canvasID, err)
+ }
updates["dsl"] = entity.JSONMap(dslpkg.NormalizeForCanvas(dslMap))
}
@@ -1299,6 +1305,9 @@ func (s *AgentService) PublishAgent(ctx context.Context, userID, canvasID string
if err := component.ValidateIntegerParameters(req.DSL); err != nil {
return nil, fmt.Errorf("publish agent %s: %w", canvasID, err)
}
+ if err := component.ValidateDynamicEntries(req.DSL); err != nil {
+ return nil, fmt.Errorf("publish agent %s: %w", canvasID, err)
+ }
dsl = dslpkg.NormalizeForCanvas(req.DSL)
}
if req.Title != nil {