Skip to content

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

fix: preserve named Begin inputs in agent bot runs (#21014)

buua43663235983ab8b
diff --git a/internal/handler/agent.go b/internal/handler/agent.go
index b03b008c05..8116ffff9e 100644
--- a/internal/handler/agent.go
+++ b/internal/handler/agent.go
@@ -1108,39 +1108,6 @@ func extractLastUserContent(messages []map[string]interface{}) string {
return content
}
-// extractUserInputFromFormInputs mirrors the front-end's wait-for-user submit
-// shape: `inputs` is an object keyed by form field name, and each entry carries
-// a nested `value`. The current chat-completion resume path consumes a single
-// string payload, so we lift the first field's value and stringify it.
-func extractUserInputFromFormInputs(inputs map[string]interface{}) interface{} {
- if len(inputs) == 0 {
- return nil
- }
-
- out := make(map[string]any, len(inputs))
- for name, raw := range inputs {
- out[name] = unwrapFormInput(raw)
- }
- return out
-}
-
-func unwrapFormInput(raw any) any {
- field, ok := raw.(map[string]interface{})
- if !ok {
- return raw
- }
- if value, exists := field["value"]; exists {
- return value
- }
- // Frontend form state can omit an untouched optional value. A field
- // descriptor without a submitted value represents nil, not the
- // descriptor itself (which would make Switch's `empty` fail).
- if _, descriptor := field["type"]; descriptor {
- return nil
- }
- return raw
-}
-
func countInputValues(inputs map[string]interface{}) int {
count := 0
for _, raw := range inputs {
@@ -1254,15 +1221,9 @@ func (h *AgentHandler) AgentChatCompletions(c *gin.Context) {
// Real canvas run. The editor sends the conversational query alongside
// named Begin inputs; preserve both so a custom Starter field cannot be
// replaced by sys.query before it reaches the runtime.
- var userInput any
- if req.Query != "" && len(req.Inputs) > 0 {
- userInput = extractUserInputWithQuery(req.Inputs, req.Query)
- } else if req.Query != "" {
- userInput = req.Query
- } else {
- if extracted := extractUserInputFromFormInputs(req.Inputs); extracted != nil {
- userInput = extracted
- } else if extracted := extractLastUserContent(req.Messages); extracted != "" {
+ userInput := service.NormalizeAgentInput(req.Query, req.Inputs)
+ if userInput == nil {
+ if extracted := extractLastUserContent(req.Messages); extracted != "" {
userInput = extracted
}
}
@@ -1479,15 +1440,6 @@ func (h *AgentHandler) AgentChatCompletions(c *gin.Context) {
common.SuccessWithData(c, result, "success")
}
-func extractUserInputWithQuery(inputs map[string]interface{}, query string) map[string]any {
- values := make(map[string]any, len(inputs)+1)
- for name, raw := range inputs {
- values[name] = unwrapFormInput(raw)
- }
- values["query"] = query
- return values
-}
-
// TestDBConnection POST /api/v1/agents/test_db_connection
func (h *AgentHandler) TestDBConnection(c *gin.Context) {
user, code, msg := GetUser(c)
diff --git a/internal/service/agent_input.go b/internal/service/agent_input.go
new file mode 100644
index 0000000000..ae2b4185e7
--- /dev/null
+++ b/internal/service/agent_input.go
@@ -0,0 +1,34 @@
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+// SPDX-License-Identifier: Apache-2.0
+
+package service
+
+// NormalizeAgentInput preserves named Begin fields alongside the conversational
+// query. Form descriptors contribute their submitted value, including false
+// and zero, rather than the descriptor object itself.
+func NormalizeAgentInput(query string, inputs map[string]any) any {
+ if len(inputs) == 0 {
+ if query == "" {
+ return nil
+ }
+ return query
+ }
+ values := make(map[string]any, len(inputs)+1)
+ for name, raw := range inputs {
+ if field, ok := raw.(map[string]any); ok {
+ if value, exists := field["value"]; exists {
+ values[name] = value
+ continue
+ }
+ if _, descriptor := field["type"]; descriptor {
+ values[name] = nil
+ continue
+ }
+ }
+ values[name] = raw
+ }
+ if query != "" {
+ values["query"] = query
+ }
+ return values
+}
diff --git a/internal/service/agent_input_test.go b/internal/service/agent_input_test.go
new file mode 100644
index 0000000000..612dadf3c7
--- /dev/null
+++ b/internal/service/agent_input_test.go
@@ -0,0 +1,84 @@
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+// SPDX-License-Identifier: Apache-2.0
+
+package service
+
+import (
+ "encoding/json"
+ "reflect"
+ "strconv"
+ "testing"
+
+ "ragflow/internal/common"
+ "ragflow/internal/entity"
+)
+
+func TestNormalizeAgentInput(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ query string
+ inputs map[string]any
+ want any
+ }{
+ {name: "empty request"},
+ {name: "query only", query: "1", want: "1"},
+ {name: "true alongside query", query: "1", inputs: map[string]any{"bool": map[string]any{"type": "boolean", "value": true}}, want: map[string]any{"query": "1", "bool": true}},
+ {name: "false without query", inputs: map[string]any{"bool": map[string]any{"type": "boolean", "value": false}}, want: map[string]any{"bool": false}},
+ {name: "zero and empty values", inputs: map[string]any{"count": map[string]any{"value": 0}, "text": map[string]any{"value": ""}}, want: map[string]any{"count": 0, "text": ""}},
+ {name: "unset optional field", inputs: map[string]any{"text": map[string]any{"type": "line", "optional": true}}, want: map[string]any{"text": nil}},
+ {name: "raw object", inputs: map[string]any{"data": map[string]any{"enabled": false}}, want: map[string]any{"data": map[string]any{"enabled": false}}},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ got := NormalizeAgentInput(tc.query, tc.inputs)
+ if !reflect.DeepEqual(got, tc.want) {
+ t.Fatalf("normalized input = %#v, want %#v", got, tc.want)
+ }
+ })
+ }
+}
+
+func TestAgentbotCompletionPreservesBooleanBeginInput(t *testing.T) {
+ db := setupServiceTestDB(t)
+ if err := db.AutoMigrate(&entity.UserCanvasVersion{}); err != nil {
+ t.Fatalf("migrate canvas versions: %v", err)
+ }
+ pushServiceDB(t, db)
+ dsl := map[string]any{
+ "components": map[string]any{
+ "begin": map[string]any{
+ "obj": map[string]any{"component_name": "Begin", "params": map[string]any{
+ "inputs": map[string]any{"bool": map[string]any{"name": "bool", "type": "boolean"}},
+ }},
+ "downstream": []any{"message"},
+ },
+ "message": map[string]any{
+ "obj": map[string]any{"component_name": "Message", "params": map[string]any{"text": "{{begin@bool}}|{{sys.query}}"}},
+ "upstream": []any{"begin"},
+ },
+ },
+ "path": []any{"begin", "message"},
+ }
+ makeCanvasWithDSL(t, "canvas-bool", "user-1", "tenant-1", "version-bool", dsl)
+ svc := NewBotService(NewAgentServiceWithOptions(nil, nil, nil))
+ for _, value := range []bool{true, false} {
+ t.Run(strconv.FormatBool(value), func(t *testing.T) {
+ body := `{"stream":true,"running_hint_text":"正在运行中...🕞","openai-compatible":false,"query":"1","inputs":{"bool":{"name":"bool","optional":false,"options":[],"type":"boolean","value":` + strconv.FormatBool(value) + `}},"files":[],"session_id":null}`
+ var req AgentbotCompletionRequest
+ if err := json.Unmarshal([]byte(body), &req); err != nil {
+ t.Fatal(err)
+ }
+ events, code, err := svc.AgentbotCompletion(t.Context(), "user-1", "canvas-bool", req)
+ if err != nil || code != common.CodeSuccess {
+ t.Fatalf("AgentbotCompletion = (%d, %v)", code, err)
+ }
+ messages, waiting, errs, _, done := drainAgentEvents(t, events)
+ if len(errs) != 0 || len(waiting) != 0 || !done {
+ t.Fatalf("unexpected run result: errors=%+v waiting=%+v done=%v", errs, waiting, done)
+ }
+ want := strconv.FormatBool(value) + "|1"
+ if len(messages) != 1 || messages[0].Content != want {
+ t.Fatalf("messages = %+v, want content %q", messages, want)
+ }
+ })
+ }
+}
diff --git a/internal/service/bot.go b/internal/service/bot.go
index 2d161f8498..6b216bcca6 100644
--- a/internal/service/bot.go
+++ b/internal/service/bot.go
@@ -153,16 +153,7 @@ func (s *BotService) AgentbotCompletion(
if _, err := s.loadCanvas(ctx, userID, agentID, permission.OperationRun); err != nil {
return nil, common.CodeDataError, err
}
- // Compose the canvas user input the same way Python's
- // canvas_service.completion does: `query` first (falling back to
- // `question`), with the begin-form `inputs` dict only as a
- // last resort when no free-text question is present. Passing the
- // `inputs` map itself as the user input breaks the run — the
- // canvas seeds sys.query / Begin's `query` output with it, and
- // downstream Retrieval nodes then fail to unmarshal an object
- // into a string field. Files remain a separate RunAgent argument
- // so they can populate sys.files.
- userInput := agentbotUserInput(req)
+ userInput := NormalizeAgentInput(question, req.UserInput)
var ch <-chan canvas.RunEvent
if req.Release != nil && *req.Release {
ch, err = s.agentService.runReleasedAgent(ctx, userID, agentID,
@@ -220,43 +211,6 @@ type AgentbotCompletionRequest struct {
Release *bool `json:"release,omitempty"`
}
-// agentbotUserInput uses the resolved question, falling back to Begin form inputs.
-// A single form field lifts its value; multiple fields collapse to a name/value map.
-func agentbotUserInput(req AgentbotCompletionRequest) any {
- query := req.Question
- if query == "" {
- query = req.Query
- }
- if query != "" {
- return query
- }
- inputs := req.UserInput
- if len(inputs) == 0 {
- return nil
- }
- if len(inputs) == 1 {
- for _, raw := range inputs {
- if field, ok := raw.(map[string]any); ok {
- if v, ok := field["value"]; ok {
- return v
- }
- }
- return raw
- }
- }
- out := make(map[string]any, len(inputs))
- for name, raw := range inputs {
- if field, ok := raw.(map[string]any); ok {
- if v, ok := field["value"]; ok {
- out[name] = v
- continue
- }
- }
- out[name] = raw
- }
- return out
-}
-
// ChatbotCompletionRequest is the request body for
// /api/v1/chatbots/<dialog_id>/completions. Mirrors the python
// `async_iframe_completion` body shape (session_id, question,