Skip to content

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

fix(agent): save incomplete SQL connections and gate Run (#21369)

Sbaaoui Idriss5850c68981a9
diff --git a/internal/agent/component/dynamic_params.go b/internal/agent/component/dynamic_params.go
index fd12877027..5231e86598 100644
--- a/internal/agent/component/dynamic_params.go
+++ b/internal/agent/component/dynamic_params.go
@@ -18,7 +18,7 @@ func ValidateDynamicEntries(dsl map[string]any) error {
// ValidateDraftDynamicEntries allows unfinished Message content, DataOperations
// entries, Invoke mappings, Compiler configuration, input options, Loop settings,
-// and Iteration outputs in saved drafts.
+// Iteration outputs, and SQL connections in saved drafts.
// All other checks remain unchanged.
func ValidateDraftDynamicEntries(dsl map[string]any) error {
return validateDynamicEntries(dsl, true)
@@ -115,7 +115,7 @@ func validateDynamicParams(component string, params map[string]any, draft bool)
case "userfillup":
return runtime.ValidateUserFillUpOptions(params, draft)
case "exesql":
- if err := validateExeSQLParams(params); err != nil {
+ if err := validateExeSQLParams(params, draft); err != nil {
return fmt.Errorf("[%s] %w", component, err)
}
case "compiler":
diff --git a/internal/agent/component/exesql_params.go b/internal/agent/component/exesql_params.go
index 2a1f5845b5..0540898e86 100644
--- a/internal/agent/component/exesql_params.go
+++ b/internal/agent/component/exesql_params.go
@@ -19,40 +19,25 @@ import (
"fmt"
"reflect"
"strings"
+
+ agenttool "ragflow/internal/agent/tool"
)
-// exeSQLDBTypes is the whitelist enforced at save time for an ExeSQL
-// component's db_type. It mirrors Python ExeSQLParam.check
-// (agent/tools/exesql.py), where ExeSQLParam defaults db_type to "mysql"
-// when the DSL omits it.
+// exeSQLDBTypes is the supported persisted connection type list.
var exeSQLDBTypes = []string{"mysql", "postgres", "mariadb", "mssql", "IBM DB2", "trino", "oceanbase"}
-// validateExeSQLParams enforces the required connection settings of an
-// ExeSQL component at save time. The Go agent PUT path validates DSL
-// component parameters through validateDynamicParams; Python performs the
-// same checks via Canvas.validate_component_parameters ->
-// ExeSQLParam.check. Without them a canvas can be persisted with a broken
-// DB tool that only fails at runtime.
-func validateExeSQLParams(params map[string]any) error {
+// validateExeSQLParams allows incomplete drafts but retains type, numeric, and security checks.
+func validateExeSQLParams(params map[string]any, draft bool) error {
dbType, explicit := params["db_type"]
if !explicit {
- // Python default when the DSL omits db_type.
+ // Runtime defaults to mysql when db_type is absent.
dbType = "mysql"
}
dbTypeString, ok := dbType.(string)
- if !ok || !exeSQLDBTypeAllowed(dbTypeString) {
+ if !ok || (!exeSQLDBTypeAllowed(dbTypeString) && !(draft && strings.TrimSpace(dbTypeString) == "")) {
return fmt.Errorf("Choose DB type %v is not supported, it should be in %v", dbType, exeSQLDBTypes)
}
- // Templates ship ExeSQL nodes whose connection fields are all blank (see
- // agent/templates/text2sql_data_expert.json): the node is an unconfigured
- // scaffold the user fills in on the canvas, not a broken configuration.
- // Requiring the fields here would make such a canvas impossible to create
- // or save, so only a node the user started configuring is checked.
- if !exeSQLConnectionStarted(params) {
- return nil
- }
-
// Connection identity fields have no Python defaults and must be present.
for _, field := range []struct {
key string
@@ -62,8 +47,13 @@ func validateExeSQLParams(params map[string]any) error {
{"username", "database username"},
{"host", "IP Address"},
} {
- if !isNonBlankString(params[field.key]) {
- return fmt.Errorf("%s does not support empty value", field.desc)
+ if value, present := params[field.key]; present {
+ if _, ok := value.(string); !ok {
+ return fmt.Errorf("%s must be a string", field.desc)
+ }
+ }
+ if !draft && !isNonBlankString(params[field.key]) {
+ return fmt.Errorf("%w: %s does not support empty value", agenttool.ErrExeSQLNoCredentials, field.desc)
}
}
@@ -75,8 +65,13 @@ func validateExeSQLParams(params map[string]any) error {
// Trino connects without credentials; every other supported DB type
// requires a password.
- if dbTypeString != "trino" && !isNonBlankString(params["password"]) {
- return fmt.Errorf("Database password does not support empty value")
+ if password, present := params["password"]; present {
+ if _, ok := password.(string); !ok {
+ return fmt.Errorf("Database password must be a string")
+ }
+ }
+ if !draft && dbTypeString != "trino" && !isNonBlankString(params["password"]) {
+ return fmt.Errorf("%w: Database password does not support empty value", agenttool.ErrExeSQLNoCredentials)
}
if maxRecords, present := params["max_records"]; present {
@@ -85,8 +80,7 @@ func validateExeSQLParams(params map[string]any) error {
}
}
- // Refuse configs aimed at RAGFlow's own metadata database, mirroring
- // Python ExeSQLParam.check's security guard.
+ // Never allow connections aimed at RAGFlow's own metadata database.
if trimmedDBString(params["database"]) == "rag_flow" &&
(trimmedDBString(params["host"]) == "ragflow-mysql" || trimmedDBString(params["password"]) == "infini_rag_flow") {
return fmt.Errorf("For the security reason, it does not support database named rag_flow.")
@@ -94,17 +88,6 @@ func validateExeSQLParams(params map[string]any) error {
return nil
}
-// exeSQLConnectionStarted reports whether the user filled in at least one
-// ExeSQL connection field. Blanks and absent keys both count as untouched.
-func exeSQLConnectionStarted(params map[string]any) bool {
- for _, key := range []string{"database", "username", "host", "password"} {
- if isNonBlankString(params[key]) {
- return true
- }
- }
- return false
-}
-
// exeSQLDBTypeAllowed reports whether dbType is one of exeSQLDBTypes.
func exeSQLDBTypeAllowed(dbType string) bool {
for _, candidate := range exeSQLDBTypes {
diff --git a/internal/agent/component/exesql_params_test.go b/internal/agent/component/exesql_params_test.go
index 26d3053598..6833227616 100644
--- a/internal/agent/component/exesql_params_test.go
+++ b/internal/agent/component/exesql_params_test.go
@@ -56,7 +56,7 @@ func TestValidateDynamicEntriesExeSQL(t *testing.T) {
"port": 3306,
"max_records": 1024,
}))
- if err := ValidateDynamicEntries(templateScaffold); err != nil {
+ if err := ValidateDraftDynamicEntries(templateScaffold); err != nil {
t.Fatalf("unconfigured ExeSQL scaffold rejected: %v", err)
}
@@ -68,7 +68,7 @@ func TestValidateDynamicEntriesExeSQL(t *testing.T) {
{
"scaffold with only host filled",
componentDSL("ExeSQL", map[string]any{"db_type": "mysql", "database": "", "username": "", "host": "10.0.0.5", "password": ""}),
- "[ExeSQL] Database name does not support empty value",
+ "Database name does not support empty value",
},
{
"unsupported db_type",
@@ -83,27 +83,27 @@ func TestValidateDynamicEntriesExeSQL(t *testing.T) {
{
"empty database",
componentDSL("ExeSQL", map[string]any{"db_type": "mysql", "database": "", "username": "u", "host": "h", "password": "p"}),
- "[ExeSQL] Database name does not support empty value",
+ "Database name does not support empty value",
},
{
"missing database",
componentDSL("ExeSQL", map[string]any{"db_type": "mysql", "username": "u", "host": "h", "password": "p"}),
- "[ExeSQL] Database name does not support empty value",
+ "Database name does not support empty value",
},
{
"empty username",
componentDSL("ExeSQL", map[string]any{"db_type": "mysql", "database": "d", "username": "", "host": "h", "password": "p"}),
- "[ExeSQL] database username does not support empty value",
+ "database username does not support empty value",
},
{
"empty host",
componentDSL("ExeSQL", map[string]any{"db_type": "mysql", "database": "d", "username": "u", "host": " ", "password": "p"}),
- "[ExeSQL] IP Address does not support empty value",
+ "IP Address does not support empty value",
},
{
"missing password",
componentDSL("ExeSQL", map[string]any{"db_type": "mysql", "database": "d", "username": "u", "host": "h"}),
- "[ExeSQL] Database password does not support empty value",
+ "Database password does not support empty value",
},
{
"zero port",
diff --git a/internal/agent/component/tool_component.go b/internal/agent/component/tool_component.go
index baaf784acc..5bfe61196b 100644
--- a/internal/agent/component/tool_component.go
+++ b/internal/agent/component/tool_component.go
@@ -19,6 +19,7 @@ package component
import (
"context"
"encoding/json"
+ "errors"
"fmt"
"io"
"strings"
@@ -41,6 +42,14 @@ const componentNameExeSQL = "ExeSQL"
func newToolComponentFactory(componentName, toolName string) Factory {
return func(params map[string]any) (Component, error) {
+ if componentName == componentNameExeSQL {
+ if err := validateExeSQLParams(params, false); err != nil {
+ if errors.Is(err, agenttool.ErrExeSQLNoCredentials) {
+ return nil, fmt.Errorf("%s: %w", componentName, err)
+ }
+ return nil, runtime.NewUserFacingError(fmt.Sprintf("%s: %s", componentName, err))
+ }
+ }
base, err := agenttool.BuildByName(toolName, params)
if err != nil {
return nil, err
diff --git a/web/src/pages/agent/form/exesql-form/use-submit-form.ts b/web/src/pages/agent/form/exesql-form/use-submit-form.ts
index a52d9cc43e..13852cd950 100644
--- a/web/src/pages/agent/form/exesql-form/use-submit-form.ts
+++ b/web/src/pages/agent/form/exesql-form/use-submit-form.ts
@@ -13,10 +13,10 @@ export const MinPort = 1;
export const MaxPort = 65535;
export const ExeSQLFormSchema = {
- db_type: z.string().min(1),
- database: z.string().min(1),
- username: z.string().min(1),
- host: z.string().min(1),
+ db_type: z.string().trim().min(1),
+ database: z.string().trim().min(1),
+ username: z.string().trim().min(1),
+ host: z.string().trim().min(1),
port: z.number().int().min(MinPort).max(MaxPort),
password: z.string().optional().or(z.literal('')),
max_records: z.number().int().min(MinMaxRecords).max(MaxMaxRecords),
diff --git a/web/src/pages/agent/utils/canvas-checklist.ts b/web/src/pages/agent/utils/canvas-checklist.ts
index 7be2000c9b..b4c23552a4 100644
--- a/web/src/pages/agent/utils/canvas-checklist.ts
+++ b/web/src/pages/agent/utils/canvas-checklist.ts
@@ -25,6 +25,7 @@ import {
RetrievalFrom,
VariableRegex,
} from '../constant';
+import { FormSchema as ExeSQLFormSchema } from '../form/exesql-form/use-submit-form';
import { FormSchema as ParserFormSchema } from '../form/parser-form/schema';
import { FormSchema as VariableAssignerFormSchema } from '../form/variable-assigner-form/schema';
import { isEmptyMessageContent } from '../utils';
@@ -625,6 +626,18 @@ export function collectCanvasIssues({
});
}
+ if (
+ label === Operator.ExeSQL &&
+ !ExeSQLFormSchema.safeParse(form).success
+ ) {
+ issues.push({
+ ...target,
+ type: CanvasIssueType.MissingRequired,
+ messageKey: 'flow.nodeFormInvalid',
+ blocksRun: true,
+ });
+ }
+
if (ApiKeyRequiredOperators.includes(label) && !form?.api_key) {
issues.push({
...target,