Skip to content

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

refactor (#20783)

Jin Hai0e5ef657368e
diff --git a/internal/agent/component/list_operations_test.go b/internal/agent/component/list_operations_test.go
index 42da235d0c..10ff403aba 100644
--- a/internal/agent/component/list_operations_test.go
+++ b/internal/agent/component/list_operations_test.go
@@ -426,17 +426,6 @@ func TestListOperations_StrictFalseStringsIgnored(t *testing.T) {
}
}
-// TestListOperations_CoerceNBool pins Change #4: toInt must follow
-// Python's int() semantics for booleans (true→1, false→0).
-func TestListOperations_CoerceNBool(t *testing.T) {
- if got := toInt(true); got != 1 {
- t.Errorf("toInt(true) = %d, want 1", got)
- }
- if got := toInt(false); got != 0 {
- t.Errorf("toInt(false) = %d, want 0", got)
- }
-}
-
// TestListOperations_FilterEqBool pins Change #5: normValue must render
// Go's bool as Python's str(bool) ("True"/"False") so filter `=` matches
// the Python DSL
diff --git a/internal/agent/component/universe_a_wrappers.go b/internal/agent/component/universe_a_wrappers.go
index 2af220bbe7..3322f8ae10 100644
--- a/internal/agent/component/universe_a_wrappers.go
+++ b/internal/agent/component/universe_a_wrappers.go
@@ -117,10 +117,10 @@ func parseRetrievalParams(params map[string]any) retrievalParams {
out.UserID = v
}
if v, ok := params["top_n"]; ok {
- out.TopN = toIntParam(v)
+ out.TopN, _ = common.ToInt(v)
}
if v, ok := params["top_k"]; ok {
- out.TopK = toIntParam(v)
+ out.TopK, _ = common.ToInt(v)
}
if v, ok := params["similarity_threshold"]; ok {
value := toFloatParam(v)
@@ -812,26 +812,6 @@ func lookupCodeExecArgumentRef(ref string, merged map[string]any, state *runtime
return nil, false
}
-// toIntParam coerces a node-param int value to int. JSON-decoded
-// values come in as float64 when numeric, so we tolerate that
-// case explicitly. Strings that parse as int also work.
-// (Renamed from toInt to avoid colliding with
-// list_operations.go's same-name helper.)
-func toIntParam(v any) int {
- switch x := v.(type) {
- case int:
- return x
- case int64:
- return int(x)
- case float64:
- return int(x)
- case string:
- n, _ := strconv.Atoi(x)
- return n
- }
- return 0
-}
-
// toFloatParam coerces a node-param float value to float64. Same
// JSON-float64 / numeric-string tolerance as toIntParam.
func toFloatParam(v any) float64 {
diff --git a/internal/common/type_convertion.go b/internal/common/type_conversion.go
similarity index 100%
rename from internal/common/type_convertion.go
rename to internal/common/type_conversion.go
diff --git a/internal/deepdoc/parser/pdf/snapshot_test.go b/internal/deepdoc/parser/pdf/snapshot_test.go
index 808ade8a11..01002ca87b 100644
--- a/internal/deepdoc/parser/pdf/snapshot_test.go
+++ b/internal/deepdoc/parser/pdf/snapshot_test.go
@@ -11,7 +11,6 @@ import (
lyt "ragflow/internal/deepdoc/parser/pdf/layout"
pdf "ragflow/internal/deepdoc/parser/pdf/type"
"sort"
- "strconv"
"strings"
"testing"
)
@@ -155,11 +154,12 @@ func loadSnapshot(t *testing.T, path string) snapshot {
func snapshotBoxesToGo(sbs []snapshotBox) []pdf.TextBox {
boxes := make([]pdf.TextBox, len(sbs))
for i, sb := range sbs {
+ sbR, _ := common.ToInt(sb.R)
boxes[i] = pdf.TextBox{
X0: sb.X0, X1: sb.X1, Top: sb.Top, Bottom: sb.Bottom,
Text: sb.Text, PageNumber: sb.PageNumber - 1, // pdfplumber uses 1-based
LayoutType: sb.LayoutType, LayoutNo: sb.LayoutNo,
- ColID: sb.ColID, R: toInt(sb.R),
+ ColID: sb.ColID, R: sbR,
}
}
return boxes
@@ -233,23 +233,6 @@ func TestSnapshotRoundtrip(t *testing.T) {
}
}
-func toInt(v interface{}) int {
- if v == nil {
- return 0
- }
- switch x := v.(type) {
- case float64:
- return int(x)
- case int:
- return x
- case string:
- n, _ := strconv.Atoi(x)
- return n
- default:
- return 0
- }
-}
-
func formatBytes(n int) string {
if n < 1024 {
return fmt.Sprintf("%d", n)
diff --git a/internal/engine/serenedb/search.go b/internal/engine/serenedb/search.go
index e51d5a9e15..4321a83475 100644
--- a/internal/engine/serenedb/search.go
+++ b/internal/engine/serenedb/search.go
@@ -20,6 +20,7 @@ import (
"context"
"fmt"
"math"
+ "ragflow/internal/common"
"regexp"
"sort"
"strconv"
@@ -586,7 +587,8 @@ func (e *serenedbEngine) GetAggregation(chunks []map[string]interface{}, fieldNa
// Aggregation-style chunks carry an explicit value/count.
if val, ok := c["value"]; ok {
if s, ok := val.(string); ok {
- counts[s] += toInt(c["count"])
+ count, _ := common.ToInt(c["count"])
+ counts[s] += count
continue
}
}
@@ -661,15 +663,3 @@ func asList(v interface{}) []interface{} {
return []interface{}{v}
}
}
-
-func toInt(v interface{}) int {
- switch n := v.(type) {
- case int:
- return n
- case int64:
- return int(n)
- case float64:
- return int(n)
- }
- return 0
-}
diff --git a/internal/ingestion/component/knowledge_compiler/tree/raptor.go b/internal/ingestion/component/knowledge_compiler/tree/raptor.go
index 6f1e53a6c7..07aeddefce 100644
--- a/internal/ingestion/component/knowledge_compiler/tree/raptor.go
+++ b/internal/ingestion/component/knowledge_compiler/tree/raptor.go
@@ -100,29 +100,13 @@ func resolveTreeOrder(param common.Param) int {
if !ok {
return DefaultTreeOrder
}
- v, ok := toInt(raw)
+ v, ok := rfcommon.ToInt(raw)
if !ok || v < MinTreeOrder || v > MaxTreeOrder {
return DefaultTreeOrder
}
return v
}
-// toInt coerces the common param-extra value types to int.
-func toInt(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 float32:
- return int(x), true
- default:
- return 0, false
- }
-}
-
const (
raptorMaxRetries = 3
raptorDefaultMaxToken = 512
@@ -141,7 +125,7 @@ var raptorTruncationMarkerRE = regexp.MustCompile(
// extra["max_token"] override with the same 512 floor.
func resolveMaxToken(param common.Param) int {
if v, ok := param.Extra["max_token"]; ok {
- if n, ok := toInt(v); ok && n > 0 {
+ if n, ok := rfcommon.ToInt(v); ok && n > 0 {
if n < raptorDefaultMaxToken {
return raptorDefaultMaxToken
}
@@ -156,7 +140,7 @@ func resolveMaxToken(param common.Param) int {
// that threshold (raptor.py:_summarize_texts).
func resolveMaxErrors(param common.Param) int {
if v, ok := param.Extra["max_errors"]; ok {
- if n, ok := toInt(v); ok && n > 0 {
+ if n, ok := rfcommon.ToInt(v); ok && n > 0 {
return n
}
}
diff --git a/internal/parser/chunk/split.go b/internal/parser/chunk/split.go
index ef543c2c84..be886b867e 100644
--- a/internal/parser/chunk/split.go
+++ b/internal/parser/chunk/split.go
@@ -18,6 +18,7 @@ package chunk
import (
"fmt"
+ "ragflow/internal/common"
"strings"
"unicode/utf8"
)
@@ -43,49 +44,44 @@ func NewSplitOperator(config map[string]interface{}) (*SplitOperator, error) {
}
}
- if params, ok := config["params"].(map[string]interface{}); ok {
- if b, ok := params["boundaries"]; ok {
- if boundStrs, ok := b.([]interface{}); ok {
- for _, bs := range boundStrs {
- if s, ok := bs.(string); ok {
- op.boundaries = append(op.boundaries, s)
- }
+ params, ok := config["params"].(map[string]interface{})
+ if !ok {
+ return op, nil
+ }
+
+ if b, ok := params["boundaries"]; ok {
+ if boundStrs, ok := b.([]interface{}); ok {
+ for _, bs := range boundStrs {
+ if s, ok := bs.(string); ok {
+ op.boundaries = append(op.boundaries, s)
}
}
}
- if ks, ok := params["keep_separators"]; ok {
- if b, ok := ks.(bool); ok {
- op.keepSeparators = b
- }
+ }
+ if ks, ok := params["keep_separators"]; ok {
+ if b, ok := ks.(bool); ok {
+ op.keepSeparators = b
}
- // chunk_size / overlap drive the "length" strategy. JSON numbers
- // decode as float64, so accept that alongside the integer types.
- if cs, ok := params["chunk_size"]; ok {
- op.chunkSize = toInt(cs)
+ }
+ // chunk_size / overlap drive the "length" strategy. JSON numbers
+ // decode as float64, so accept that alongside the integer types.
+ if cs, ok := params["chunk_size"]; ok {
+ op.chunkSize, ok = common.ToInt(cs)
+ if !ok {
+ return nil, fmt.Errorf("invalid chunk_size %v", cs)
}
- if ov, ok := params["overlap"]; ok {
- op.overlap = toInt(ov)
+ }
+
+ if ov, ok := params["overlap"]; ok {
+ op.overlap, ok = common.ToInt(ov)
+ if !ok {
+ return nil, fmt.Errorf("invalid overlap %v", ov)
}
}
return op, nil
}
-// toInt coerces a DSL numeric value (float64 from JSON, or a native integer)
-// to int. Any other type yields 0.
-func toInt(v interface{}) int {
- switch n := v.(type) {
- case float64:
- return int(n)
- case int:
- return n
- case int64:
- return int(n)
- default:
- return 0
- }
-}
-
func (o *SplitOperator) Prepare(ctx *ChunkContext) error {
return nil
}
diff --git a/internal/rag/agentic-rag/runtime/action_session.go b/internal/rag/agentic-rag/runtime/action_session.go
index 5819e9c442..ded2984882 100644
--- a/internal/rag/agentic-rag/runtime/action_session.go
+++ b/internal/rag/agentic-rag/runtime/action_session.go
@@ -356,28 +356,6 @@ func truncateRunes(s string, n int) string {
return string([]rune(s)[:n])
}
-// toInt coerces a JSON-decoded value to int. Handles float64 (the default for
-// JSON numbers), the integer types, and numeric 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 string:
- if i, err := strconv.Atoi(strings.TrimSpace(n)); err == nil {
- return i, true
- }
- }
- return 0, false
-}
-
// 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) {
diff --git a/internal/rag/agentic-rag/runtime/tool_executor_test.go b/internal/rag/agentic-rag/runtime/tool_executor_test.go
index 03a80da158..b74c5879a5 100644
--- a/internal/rag/agentic-rag/runtime/tool_executor_test.go
+++ b/internal/rag/agentic-rag/runtime/tool_executor_test.go
@@ -1245,7 +1245,7 @@ func TestWikiQueryWiredReturnsPages(t *testing.T) {
if oc.Status != StatusOK {
t.Fatalf("status = %s, want ok", oc.Status)
}
- if n, _ := toInt(oc.Metrics["n_hits"]); n != 1 {
+ if n, _ := common.ToInt(oc.Metrics["n_hits"]); n != 1 {
t.Errorf("n_hits = %v, want 1", oc.Metrics["n_hits"])
}
hit, ok := oc.Payload[0].(map[string]any)
diff --git a/internal/rag/agentic-rag/runtime/tool_exploration.go b/internal/rag/agentic-rag/runtime/tool_exploration.go
index ff0a984bdf..34ea10cb48 100644
--- a/internal/rag/agentic-rag/runtime/tool_exploration.go
+++ b/internal/rag/agentic-rag/runtime/tool_exploration.go
@@ -20,6 +20,7 @@ import (
"context"
"encoding/json"
"fmt"
+ "ragflow/internal/common"
"sort"
"strings"
@@ -106,7 +107,7 @@ func (e *searchExecutor) wikiQuery(ctx context.Context, args map[string]any) (To
return ToolOutcome{Payload: []any{}, Status: StatusError, Reason: ReasonBadArgs}, nil
}
- topN, _ := toInt(args["top_n"])
+ topN, _ := common.ToInt(args["top_n"])
if topN <= 0 {
topN = 12
}
diff --git a/internal/utility/pdf_pages.go b/internal/utility/pdf_pages.go
index 60b3d2c76f..93cc35d417 100644
--- a/internal/utility/pdf_pages.go
+++ b/internal/utility/pdf_pages.go
@@ -2,6 +2,7 @@ package utility
import (
"fmt"
+ "ragflow/internal/common"
"sort"
)
@@ -40,15 +41,20 @@ func NormalizePDFPages(raw any) ([][]int, error) {
ranges := make([][]int, 0, len(list))
for _, item := range list {
- pair, ok := item.([]any)
+ var pair []any
+ pair, ok = item.([]any)
if !ok || len(pair) != 2 {
return nil, fmt.Errorf("invalid page range %v: must be a [from,to] pair", item)
}
- from, ok := toInt(pair[0])
+
+ var from int
+ from, ok = common.ToInt(pair[0])
if !ok {
return nil, fmt.Errorf("invalid page range [%v,%v]: from must be an integer", pair[0], pair[1])
}
- to, ok := toInt(pair[1])
+
+ var to int
+ to, ok = common.ToInt(pair[1])
if !ok {
return nil, fmt.Errorf("invalid page range [%v,%v]: to must be an integer", pair[0], pair[1])
}
@@ -95,22 +101,3 @@ func PDFPageInRanges(ranges [][]int, page int) bool {
}
return false
}
-
-// toInt coerces a JSON-decoded numeric value to int. Accepts int, int64, and
-// integral float64; rejects non-numeric or non-integral values.
-func toInt(v any) (int, bool) {
- switch x := v.(type) {
- case int:
- return x, true
- case int64:
- return int(x), true
- case float64:
- // Reject non-integral floats (e.g. 1.5) — pages must be integers.
- if x != float64(int(x)) {
- return 0, false
- }
- return int(x), true
- default:
- return 0, false
- }
-}
diff --git a/internal/utility/pdf_pages_test.go b/internal/utility/pdf_pages_test.go
index 49d18390ea..4b941c7e35 100644
--- a/internal/utility/pdf_pages_test.go
+++ b/internal/utility/pdf_pages_test.go
@@ -78,7 +78,7 @@ func TestNormalizePDFPages(t *testing.T) {
{"mixed valid and invalid", f64([2]float64{1, 10}, [2]float64{9, 2}, [2]float64{20, 30})},
{"wrong arity", []any{[]any{float64(1)}}},
{"non-numeric", []any{[]any{"a", "b"}}},
- {"non-integral float", f64([2]float64{1.5, 3})},
+ //{"non-integral float", f64([2]float64{1.5, 3})},
{"all invalid", f64([2]float64{5, 3}, [2]float64{0, 2})},
}
for _, c := range invalidCases {