Skip to content

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

Go: use unified ToInt (#20772)

Jin Haib45dfd927f7b
diff --git a/internal/agent/canvas/node_body.go b/internal/agent/canvas/node_body.go
index a8bb9a9305..f37fd8ee42 100644
--- a/internal/agent/canvas/node_body.go
+++ b/internal/agent/canvas/node_body.go
@@ -83,7 +83,7 @@ type nodeBodyFn = func(ctx context.Context, in map[string]any) (map[string]any,
// (componentFactoryFromContext): the override is threaded through
// canvas.Compile → BuildWorkflow → buildNodeBody without the canvas
// package ever importing the ingestion layer.
-const ctxKeyOverrideParams ctxKey = "canvas_override_params"
+const ctxKeyOverrideParams = "canvas_override_params"
// withOverrideParams attaches a run-level override map to ctx. It is
// a no-op when m is nil so callers can pass a possibly-nil run parameter
diff --git a/internal/agent/canvas/scheduler.go b/internal/agent/canvas/scheduler.go
index af5a58d06d..e44f1fb83e 100644
--- a/internal/agent/canvas/scheduler.go
+++ b/internal/agent/canvas/scheduler.go
@@ -37,9 +37,7 @@ import (
// (events channel, message/session ids) so the statePre/statePost
// wrappers can emit node_started/node_finished without depending on
// the service package.
-type ctxKey string
-
-const ctxKeyRunMeta ctxKey = "canvas_run_meta"
+const ctxKeyRunMeta = "canvas_run_meta"
const terminalMergeNodeID = "__canvas_terminal_merge__"
// RunMeta carries the per-run metadata that node lifecycle hooks need.
diff --git a/internal/agent/runtime/helpers_test.go b/internal/agent/runtime/helpers_test.go
index 4bd265d331..abfa7141e3 100644
--- a/internal/agent/runtime/helpers_test.go
+++ b/internal/agent/runtime/helpers_test.go
@@ -227,8 +227,7 @@ func TestWithTimeout_ParentCancellation(t *testing.T) {
// which is the documented contract of context.WithTimeout, not a
// helper bug.
func TestWithTimeout_PassesContextToFn(t *testing.T) {
- type ctxKey struct{}
- parent := context.WithValue(t.Context(), ctxKey{}, "v")
+ parent := context.WithValue(t.Context(), struct{}{}, "v")
type captured struct {
ctx context.Context
errInFlight error
@@ -251,7 +250,7 @@ func TestWithTimeout_PassesContextToFn(t *testing.T) {
if cap.ctx == parent {
t.Fatal("fn received the parent ctx directly — expected a derived child ctx")
}
- if v, _ := cap.ctx.Value(ctxKey{}).(string); v != "v" {
+ if v, _ := cap.ctx.Value(struct{}{}).(string); v != "v" {
t.Errorf("child ctx did not carry parent's Value(): got %q, want %q", v, "v")
}
if cap.errInFlight != nil {
diff --git a/internal/common/logger.go b/internal/common/logger.go
index 58fb439309..b6cd18033c 100644
--- a/internal/common/logger.go
+++ b/internal/common/logger.go
@@ -363,9 +363,7 @@ func (stdLogRouter) Write(p []byte) (int, error) {
// log helpers below lets a single `grep session_id=<id>` reconstruct one
// turn's full trail (the q71 postmortem had to reconstruct it from timestamps).
-type ctxKey int
-
-const sessionIDCtxKey ctxKey = iota
+const sessionIDCtxKey int = iota
// WithSessionID returns a context that tags every Ctx-variant log call with
// the conversation turn's session id. Empty ids are a no-op so callers do not
diff --git a/internal/common/type_convertion.go b/internal/common/type_convertion.go
new file mode 100644
index 0000000000..831ac4d275
--- /dev/null
+++ b/internal/common/type_convertion.go
@@ -0,0 +1,47 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+package common
+
+import (
+ "strconv"
+ "strings"
+)
+
+func ToInt(v any) (int, bool) {
+ switch n := v.(type) {
+ case int:
+ return n, true
+ case int32:
+ return int(n), true
+ case int64:
+ return int(n), true
+ case float32:
+ return int(n), true
+ case float64:
+ return int(n), true
+ case bool:
+ if v.(bool) {
+ return 1, true
+ }
+ return 0, true
+ case string:
+ if i, err := strconv.Atoi(strings.TrimSpace(n)); err == nil {
+ return i, true
+ }
+ }
+ return 0, false
+}
diff --git a/internal/handler/agent.go b/internal/handler/agent.go
index c6f3a04b85..0d519c22b5 100644
--- a/internal/handler/agent.go
+++ b/internal/handler/agent.go
@@ -348,9 +348,6 @@ type agentDetailResponse struct {
LastPublishTime *int64 `json:"last_publish_time,omitempty"`
}
-// updateAgentRequest is the wire shape for PUT /api/v1/agents/:canvas_id.
-type updateAgentRequest map[string]interface{}
-
func decodeJSONWithNumber(body io.Reader, target any) error {
decoder := json.NewDecoder(body)
decoder.UseNumber()
@@ -373,13 +370,13 @@ func (h *AgentHandler) UpdateAgent(c *gin.Context) {
return
}
canvasID := c.Param("canvas_id")
- var req updateAgentRequest
+ var req map[string]interface{}
if err := decodeJSONWithNumber(c.Request.Body, &req); err != nil && !errors.Is(err, io.EOF) {
common.ResponseWithCodeData(c, common.CodeArgumentError, nil, "Invalid request: "+err.Error())
return
}
if req == nil {
- req = updateAgentRequest{}
+ req = map[string]interface{}{}
}
if err := h.agentService.UpdateAgent(c.Request.Context(), user.ID, canvasID, req); err != nil {
ec, em := mapAgentError(err)
diff --git a/internal/ingestion/task/pipeline_executor_test.go b/internal/ingestion/task/pipeline_executor_test.go
index 5759e8fedf..ffe8e85bba 100644
--- a/internal/ingestion/task/pipeline_executor_test.go
+++ b/internal/ingestion/task/pipeline_executor_test.go
@@ -991,8 +991,7 @@ func TestPipelineExecutor_Execute_DoesNotLogCanceledRun(t *testing.T) {
// Execute is the ctx received by runPipelineFunc - the task context must flow
// through to the pipeline run.
func TestPipelineExecutor_Execute_PropagatesContext(t *testing.T) {
- type ctxKey string
- const key ctxKey = "trace"
+ const key string = "trace"
taskCtx := makeTaskCtx()
ctx := t.Context()
taskCtx.Ctx = context.WithValue(ctx, key, "task-ctx")