Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:sparkles: AI Agent https://github.com/siyuan-note/siyuan/issues/17797

Daniel3be15c8af0e0
diff --git a/kernel/agent/agent.go b/kernel/agent/agent.go
index bd099d19ae..9fff0ffd26 100644
--- a/kernel/agent/agent.go
+++ b/kernel/agent/agent.go
@@ -372,6 +372,8 @@ func AgentChat(ctx context.Context, client *openai.Client, model string, session
resultStr = executeTool(tc)
}
+ resultStr = util.TruncateToolOutput(resultStr, sessionID)
+
ch <- AgentEvent{
Type: "tool_result",
Name: tc.Function.Name,
diff --git a/kernel/util/tooloutput.go b/kernel/util/tooloutput.go
new file mode 100644
index 0000000000..81238426ba
--- /dev/null
+++ b/kernel/util/tooloutput.go
@@ -0,0 +1,48 @@
+// SiYuan - Refactor your thinking
+// 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 util
+
+import (
+ "os"
+ "path/filepath"
+
+ "github.com/88250/lute/ast"
+ "github.com/siyuan-note/filelock"
+)
+
+const MaxToolOutputChars = 40000
+
+func TruncateToolOutput(text, sessionID string) string {
+ runes := []rune(text)
+ if len(runes) <= MaxToolOutputChars {
+ return text
+ }
+
+ outputDir := filepath.Join(DataDir, "storage", "ai", "agent", "sessions", sessionID, "output")
+ if err := os.MkdirAll(outputDir, 0755); err != nil {
+ return text
+ }
+
+ name := ast.NewNodeID() + ".txt"
+ path := filepath.Join(outputDir, name)
+ if err := filelock.WriteFile(path, []byte(text)); err != nil {
+ return text
+ }
+
+ relPath := filepath.Join("data", "storage", "ai", "agent", "sessions", sessionID, "output", name)
+ return string(runes[:MaxToolOutputChars]) + "\n\n...content truncated. Full output: " + relPath
+}