Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

fix(agent): preserve native integer literals during parameter casting (#4483)

澍f1e1e44f0756
diff --git a/internal/agent/tools/param_cast.go b/internal/agent/tools/param_cast.go
index a77a1d005..a7ca4b4a8 100644
--- a/internal/agent/tools/param_cast.go
+++ b/internal/agent/tools/param_cast.go
@@ -52,6 +52,13 @@ func CastParams(args json.RawMessage, schema json.RawMessage) json.RawMessage {
if err := json.Unmarshal(rawVal, &val); err != nil {
continue
}
+ // Integer literals already have the requested JSON representation. A
+ // float64-to-int64 round trip would corrupt values above 2^53. Keep
+ // decimal/exponent notation on the existing normalization path.
+ if _, isNumber := val.(float64); isNumber && targetType == "integer" &&
+ !strings.ContainsAny(string(rawVal), ".eE") {
+ continue
+ }
newVal, didCast := castValue(val, targetType)
if didCast {
converted, err := json.Marshal(newVal)
diff --git a/internal/agent/tools/param_cast_integer_test.go b/internal/agent/tools/param_cast_integer_test.go
new file mode 100644
index 000000000..8692337b1
--- /dev/null
+++ b/internal/agent/tools/param_cast_integer_test.go
@@ -0,0 +1,54 @@
+package tools
+
+import (
+ "encoding/json"
+ "fmt"
+ "testing"
+)
+
+func TestCastParamsPreservesIntegerLiterals(t *testing.T) {
+ schema := json.RawMessage(`{"properties":{"id":{"type":"integer"},"enabled":{"type":"boolean"}}}`)
+ for _, number := range []string{
+ "0", "-0", "42", "9007199254740993", "-9007199254740993",
+ "9223372036854775807", "-9223372036854775808", "18446744073709551615",
+ } {
+ for _, enabled := range []string{`true`, `"true"`} {
+ t.Run(number+enabled, func(t *testing.T) {
+ args := json.RawMessage(fmt.Sprintf(`{ "id": %s, "enabled": %s }`, number, enabled))
+ result := CastParams(args, schema)
+ var fields map[string]json.RawMessage
+ if err := json.Unmarshal(result, &fields); err != nil {
+ t.Fatal(err)
+ }
+ if string(fields["id"]) != number || string(fields["enabled"]) != "true" {
+ t.Fatalf("expected id %s and true, got %s", number, result)
+ }
+ if enabled == "true" && string(result) != string(args) {
+ t.Fatalf("already typed arguments should remain unchanged: %s", result)
+ }
+ })
+ }
+ }
+}
+
+func TestCastParamsIntegerConversionControls(t *testing.T) {
+ schema := json.RawMessage(`{"properties":{"id":{"type":"integer"}}}`)
+ for _, tc := range []struct{ input, want string }{
+ {`"9007199254740993"`, "9007199254740993"},
+ {"42.0", "42"},
+ {"4.2e1", "42"},
+ {"1.5", "1.5"},
+ {`"invalid"`, `"invalid"`},
+ {"null", "null"},
+ {"true", "true"},
+ } {
+ result := CastParams(json.RawMessage(`{"id":`+tc.input+`}`), schema)
+ var fields map[string]json.RawMessage
+ if err := json.Unmarshal(result, &fields); err != nil {
+ t.Fatal(err)
+ }
+ if string(fields["id"]) != tc.want {
+ t.Errorf("%s: want %s, got %s", tc.input, tc.want, result)
+ }
+ }
+}