An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Prevent MCP previews from splitting UTF-8 characters https://github.com/siyuan-note/siyuan/issues/19106
Daniel6b30b8321340
diff --git a/kernel/mcp/tools/block.go b/kernel/mcp/tools/block.goindex 99554faa00..13d2b7295c 100644--- a/kernel/mcp/tools/block.go+++ b/kernel/mcp/tools/block.go@@ -166,9 +166,7 @@ func blockGetChildren(args map[string]any) (CallToolResult, error) {if content == "" {content = c.Content}- if len(content) > 200 {- content = content[:200] + "..."- }+ content = truncateText(content, 200)sb.WriteString(fmt.Sprintf("- [%s] %s (%s)\n", c.Type, content, c.ID))}return CallToolResult{Content: []ContentItem{{Type: "text", Text: sb.String()}}}, nildiff --git a/kernel/mcp/tools/document.go b/kernel/mcp/tools/document.goindex 9244fb21d1..171159a8d6 100644--- a/kernel/mcp/tools/document.go+++ b/kernel/mcp/tools/document.go@@ -306,9 +306,7 @@ func documentInfo(args map[string]any) (CallToolResult, error) {if len(info.IAL) > 0 {sb.WriteString("\nIAL:")for k, v := range info.IAL {- if len(v) > 100 {- v = v[:100] + "..."- }+ v = truncateText(v, 100)sb.WriteString(fmt.Sprintf("\n %s: %s", k, v))}}diff --git a/kernel/mcp/tools/search.go b/kernel/mcp/tools/search.goindex 681fdc7892..9a7955e9e0 100644--- a/kernel/mcp/tools/search.go+++ b/kernel/mcp/tools/search.go@@ -149,9 +149,7 @@ func fulltextSearch(args map[string]any) (CallToolResult, error) {if content == "" {content = b.Content}- if len(content) > 200 {- content = content[:200] + "..."- }+ content = truncateText(content, 200)sb.WriteString(fmt.Sprintf("- [%s] %s\n %s\n id: %s\n\n", b.HPath, b.Type, content, b.ID))}if docMode {@@ -205,9 +203,7 @@ func semanticSearch(args map[string]any) (CallToolResult, error) {if content == "" {content = b.Content}- if len(content) > 200 {- content = content[:200] + "..."- }+ content = truncateText(content, 200)sb.WriteString(fmt.Sprintf("- [%s] %s\n %s\n id: %s\n\n", b.HPath, b.Type, content, b.ID))}if matchedRootCount > 0 {@@ -264,9 +260,7 @@ func assetSearch(args map[string]any) (CallToolResult, error) {sb.WriteString(fmt.Sprintf("Found %d asset matches (page %d/%d):\n\n", matchedAssetCount, page, pageCount))for _, a := range assetContents {content := a.Content- if len(content) > 200 {- content = content[:200] + "..."- }+ content = truncateText(content, 200)sb.WriteString(fmt.Sprintf("- [%s] %s\n", a.Ext, a.Name))sb.WriteString(fmt.Sprintf(" path: %s\n", a.Path))sb.WriteString(fmt.Sprintf(" size: %s\n", a.HSize))diff --git a/kernel/mcp/tools/string.go b/kernel/mcp/tools/string.gonew file mode 100644index 0000000000..5540da0489--- /dev/null+++ b/kernel/mcp/tools/string.go@@ -0,0 +1,30 @@+// SiYuan - From thought to insight, with agents+// Copyright (c) 2020-present, b3log.org+//+// This program is free software: you can redistribute it and/or modify+// it under the terms of the GNU Affero General Public License as published by+// the Free Software Foundation, either version 3 of the License, or+// (at your option) any later version.+//+// This program is distributed in the hope that it will be useful,+// but WITHOUT ANY WARRANTY; without even the implied warranty of+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+// GNU Affero General Public License for more details.+//+// You should have received a copy of the GNU Affero General Public License+// along with this program. If not, see <https://www.gnu.org/licenses/>.++package tools++import (+ "unicode/utf8"++ "github.com/88250/gulu"+)++func truncateText(text string, maxLen int) string {+ if utf8.RuneCountInString(text) <= maxLen {+ return text+ }+ return gulu.Str.SubStr(text, maxLen) + "..."+}diff --git a/kernel/mcp/tools/string_test.go b/kernel/mcp/tools/string_test.gonew file mode 100644index 0000000000..5238bad7b6--- /dev/null+++ b/kernel/mcp/tools/string_test.go@@ -0,0 +1,51 @@+// SiYuan - From thought to insight, with agents+// Copyright (c) 2020-present, b3log.org+//+// This program is free software: you can redistribute it and/or modify+// it under the terms of the GNU Affero General Public License as published by+// the Free Software Foundation, either version 3 of the License, or+// (at your option) any later version.+//+// This program is distributed in the hope that it will be useful,+// but WITHOUT ANY WARRANTY; without even the implied warranty of+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+// GNU Affero General Public License for more details.+//+// You should have received a copy of the GNU Affero General Public License+// along with this program. If not, see <https://www.gnu.org/licenses/>.++package tools++import (+ "strings"+ "testing"+ "unicode/utf8"+)++func TestTruncateText(t *testing.T) {+ tests := []struct {+ name string+ text string+ maxLen int+ want string+ }{+ {name: "empty", text: "", maxLen: 200, want: ""},+ {name: "ASCII exact", text: strings.Repeat("a", 200), maxLen: 200, want: strings.Repeat("a", 200)},+ {name: "ASCII truncated", text: strings.Repeat("a", 201), maxLen: 200, want: strings.Repeat("a", 200) + "..."},+ {name: "Chinese below limit", text: strings.Repeat("中", 80), maxLen: 200, want: strings.Repeat("中", 80)},+ {name: "Chinese truncated", text: strings.Repeat("中", 201), maxLen: 200, want: strings.Repeat("中", 200) + "..."},+ {name: "emoji truncated", text: strings.Repeat("😀", 201), maxLen: 200, want: strings.Repeat("😀", 200) + "..."},+ }++ for _, test := range tests {+ t.Run(test.name, func(t *testing.T) {+ got := truncateText(test.text, test.maxLen)+ if got != test.want {+ t.Fatalf("truncateText() = %q, want %q", got, test.want)+ }+ if !utf8.ValidString(got) {+ t.Fatalf("truncateText() returned invalid UTF-8: %q", got)+ }+ })+ }+}