Skip to content

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

Go: refactor (#20795)

Jin Haib9cc99433adc
diff --git a/internal/agent/component/list_operations.go b/internal/agent/component/list_operations.go
index 8be3874cdf..5d90cc8bda 100644
--- a/internal/agent/component/list_operations.go
+++ b/internal/agent/component/list_operations.go
@@ -37,6 +37,7 @@ import (
"context"
"encoding/json"
"fmt"
+ "ragflow/internal/common"
"sort"
"strings"
@@ -105,7 +106,14 @@ func (p *listOperationsParam) Update(conf map[string]any) error {
p.Operations = "nth"
}
p.Operations = normalizeListOperationName(p.Operations)
- p.N = toInt(conf["n"])
+ var ok bool
+
+ p.N, ok = common.ToInt(conf["n"])
+ if !ok {
+ common.Error("n must be an integer", nil)
+ return &ParamError{Field: "n", Reason: "must be an integer"}
+ }
+
if v, ok := conf["strict"]; ok {
p.Strict = coerceBool(v)
}
@@ -211,31 +219,6 @@ func parseSortByFieldList(v any) []string {
return out
}
-// toInt coerces a value to int. Floats are truncated; strings parsed
-// via Atoi; bools follow Python's int() semantics (true → 1, false → 0);
-// everything else falls back to 0. Mirrors Python's _coerce_n in
-// agent/component/list_operations.py:73-77.
-func toInt(v any) int {
- switch x := v.(type) {
- case int:
- return x
- case int64:
- return int(x)
- case float64:
- return int(x)
- case string:
- var n int
- fmt.Sscanf(x, "%d", &n)
- return n
- case bool:
- if x {
- return 1
- }
- return 0
- }
- return 0
-}
-
// ListOperationsComponent implements the 6 list transforms.
type ListOperationsComponent struct {
name string
diff --git a/internal/common/type_conversion.go b/internal/common/type_conversion.go
index 831ac4d275..cbc485685a 100644
--- a/internal/common/type_conversion.go
+++ b/internal/common/type_conversion.go
@@ -22,6 +22,10 @@ import (
)
func ToInt(v any) (int, bool) {
+ if v == nil {
+ return 0, true
+ }
+
switch n := v.(type) {
case int:
return n, true
@@ -45,3 +49,19 @@ func ToInt(v any) (int, bool) {
}
return 0, false
}
+
+func ToIntNum(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
+ }
+ return 0, false
+}
diff --git a/internal/rag/agentic-rag/graph_sca.go b/internal/rag/agentic-rag/graph_sca.go
index 130b97168a..1259f98875 100644
--- a/internal/rag/agentic-rag/graph_sca.go
+++ b/internal/rag/agentic-rag/graph_sca.go
@@ -20,6 +20,7 @@ import (
"context"
"fmt"
"log"
+ "ragflow/internal/common"
"sort"
"strconv"
"strings"
@@ -500,12 +501,12 @@ func renderResearchContext(st *AgenticState) string {
// A research round's row carries what ITS session bound. There is no
// per-session "new to the pool" number to report: a round's sessions run
// concurrently over ONE pool (see RunSlotResearchPass).
- if n, ok := toIntStrict(e["bound"]); ok {
+ if n, ok := common.ToInt(e["bound"]); ok {
outcome = fmt.Sprintf("%d passage(s) bound", n)
}
default:
// A prefetch/rewrite row: those DO know how many passages the fetch added.
- if n, ok := toIntStrict(e["new"]); ok && n != 0 {
+ if n, ok := common.ToInt(e["new"]); ok && n != 0 {
outcome = fmt.Sprintf("%d new passage(s)", n)
}
}
diff --git a/internal/rag/agentic-rag/graph_util.go b/internal/rag/agentic-rag/graph_util.go
index 76d8c809cd..8f0c4b669a 100644
--- a/internal/rag/agentic-rag/graph_util.go
+++ b/internal/rag/agentic-rag/graph_util.go
@@ -137,24 +137,6 @@ func asSliceOfAny(v any) []any {
}
}
-// toIntStrict parses an int-like value (JSON numbers arrive as float64).
-func toIntStrict(v any) (int, bool) {
- switch x := v.(type) {
- case int:
- return x, true
- case int64:
- return int(x), true
- case float64:
- return int(x), true
- case string:
- var n int
- if _, err := fmt.Sscanf(x, "%d", &n); err == nil {
- return n, true
- }
- }
- return 0, false
-}
-
// toFloat parses a numeric value.
func toFloat(v any) (float64, bool) {
switch x := v.(type) {
diff --git a/internal/rag/agentic-rag/runtime/action_session.go b/internal/rag/agentic-rag/runtime/action_session.go
index ded2984882..fff1a393d3 100644
--- a/internal/rag/agentic-rag/runtime/action_session.go
+++ b/internal/rag/agentic-rag/runtime/action_session.go
@@ -272,7 +272,7 @@ func ApplyPatch(base State, branchPatches []map[string]any) *State {
// and is skipped. Only JSON numbers match, which decode as float64 — accept those,
// and reject anything else so the behaviour is strict rather than silently more
// permissive.
- want, ok := toIntStrict(rawID)
+ want, ok := common.ToIntNum(rawID)
if !ok {
continue
}
@@ -356,24 +356,6 @@ func truncateRunes(s string, n int) string {
return string([]rune(s)[:n])
}
-// toIntStrict converts JSON NUMBER types only. Unlike toInt it does NOT parse strings: a
-// patch whose id is "1" must match nothing.
-func toIntStrict(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
- }
- return 0, false
-}
-
// toFloat coerces a JSON-decoded value to float64.
func toFloat(v any) (float64, bool) {
switch n := v.(type) {
diff --git a/internal/rag/agentic-rag/runtime/arithmetic.go b/internal/rag/agentic-rag/runtime/arithmetic.go
index 53a29f101d..278f7cb8ac 100644
--- a/internal/rag/agentic-rag/runtime/arithmetic.go
+++ b/internal/rag/agentic-rag/runtime/arithmetic.go
@@ -20,6 +20,7 @@ import (
"context"
"fmt"
"math"
+ "ragflow/internal/common"
"strconv"
"strings"
"time"
@@ -1548,7 +1549,7 @@ func ComputeFromFacts(ctx context.Context, model SessionModel, question string,
uses := make([]int, 0, 4)
if raw, ok := data["uses"].([]any); ok {
for _, n := range raw {
- if v, ok := toIntStrict(n); ok {
+ if v, ok := common.ToIntNum(n); ok {
uses = append(uses, v)
}
}