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): truncate tool-call hints by runes, not bytes (#4673)

ydflow68361299e1ff
diff --git a/internal/agent/act.go b/internal/agent/act.go
index 82d73bf46..3f209e08c 100644
--- a/internal/agent/act.go
+++ b/internal/agent/act.go
@@ -212,9 +212,7 @@ func formatToolHint(name string, args map[string]any) string {
}
for _, v := range args {
if s, ok := v.(string); ok {
- if len(s) > 40 {
- s = s[:40] + "…"
- }
+ s = truncateForLangfuse(s, 40)
return fmt.Sprintf(`%s("%s")`, displayName, s)
}
}
diff --git a/internal/agent/act_tool_hint_test.go b/internal/agent/act_tool_hint_test.go
new file mode 100644
index 000000000..239e5c5a9
--- /dev/null
+++ b/internal/agent/act_tool_hint_test.go
@@ -0,0 +1,33 @@
+package agent
+
+import (
+ "strings"
+ "testing"
+ "unicode/utf8"
+
+ agenttools "github.com/Tencent/WeKnora/internal/agent/tools"
+ "github.com/stretchr/testify/require"
+)
+
+// The tool-call hint is rendered in the UI's tool-call chip. Truncating it by
+// bytes splits multi-byte CJK arguments mid-rune and ships invalid UTF-8 in
+// the EventAgentToolCall hint, so truncation must be rune-based — the same
+// rule truncateForLangfuse already documents for Langfuse previews.
+func TestFormatToolHintTruncatesByRunes(t *testing.T) {
+ // 30 CJK runes (90 bytes): within the 40-rune budget, must survive intact.
+ short := formatToolHint(agenttools.ToolSearchKnowledge,
+ map[string]any{"query": strings.Repeat("数", 30)})
+ require.True(t, utf8.ValidString(short), "hint must stay valid UTF-8, got %q", short)
+ require.Equal(t, `检索知识库("`+strings.Repeat("数", 30)+`")`, short)
+
+ // 60 CJK runes (180 bytes): truncated at the 40th rune, never mid-rune.
+ long := formatToolHint(agenttools.ToolSearchKnowledge,
+ map[string]any{"query": strings.Repeat("数", 60)})
+ require.True(t, utf8.ValidString(long), "hint must stay valid UTF-8, got %q", long)
+ require.Equal(t, `检索知识库("`+strings.Repeat("数", 40)+`…")`, long)
+
+ // ASCII longer than the budget keeps its byte-oriented behavior.
+ ascii := formatToolHint(agenttools.ToolSearchKnowledge,
+ map[string]any{"query": strings.Repeat("a", 50)})
+ require.Equal(t, `检索知识库("`+strings.Repeat("a", 40)+`…")`, ascii)
+}