An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:sparkles: AI Agent https://github.com/siyuan-note/siyuan/issues/17797
Daniel7b3a4dccde9f
diff --git a/app/src/layout/dock/AgentChat.ts b/app/src/layout/dock/AgentChat.tsindex 67557f7bea..f2a0a441d4 100644--- a/app/src/layout/dock/AgentChat.ts+++ b/app/src/layout/dock/AgentChat.ts@@ -548,7 +548,7 @@ export class AgentChat extends Model {this.currentContent += token;this.fullContent += token;this.clearThinking();- bubble.innerHTML = this.lute.MarkdownStr("", this.currentContent);+ bubble.innerHTML = this.lute.MarkdownStr("", this.currentContent) || this.escapeHtml(this.currentContent);this.scrollToBottom();}@@ -635,9 +635,14 @@ export class AgentChat extends Model {bodyHTML +"</div>";- if (reasoning === "processing" && this.currentAIElement && this.currentContent) {- this.finalizeCurrentRound();- this.currentAIElement = this.createAIMessagePlaceholder();+ if (reasoning === "processing" && this.currentAIElement) {+ if (this.currentContent) {+ this.finalizeCurrentRound();+ this.currentAIElement = this.createAIMessagePlaceholder();+ } else {+ this.currentAIElement.remove();+ this.currentAIElement = null;+ }}const header = el.querySelector(".agent-chat__thinking-header") as HTMLElement;@@ -669,6 +674,24 @@ export class AgentChat extends Model {return;}this.clearThinking();+ if (!this.currentContent) {+ this.currentAIElement.remove();+ this.currentAIElement = null;+ this.currentContent = "";+ this.fullContent = "";+ this.currentToolCalls = [];+ if (this.requestStartTime) {+ this.sessionTotalDuration += Date.now() - this.requestStartTime;+ this.requestStartTime = 0;+ }+ this.setStreaming(false);+ if (!this.hasTitled && this.messages.length >= 2) {+ this.hasTitled = true;+ this.generateTitle();+ }+ this.saveSession();+ return;+ }const bubble = this.currentAIElement.querySelector(".agent-chat__bubble") as HTMLElement;if (bubble) {bubble.classList.remove("agent-chat__bubble--streaming");@@ -709,11 +732,14 @@ export class AgentChat extends Model {private appendError(message: string) {this.clearThinking();+ if (this.currentAIElement && !this.currentContent) {+ this.currentAIElement.remove();+ }+ this.currentAIElement = null;const el = document.createElement("div");el.className = "agent-chat__msg agent-chat__msg--error";el.innerHTML = '<div class="agent-chat__bubble agent-chat__bubble--error">' + this.escapeHtml(message) + "</div>";- this.insertBeforeAI(el);- this.currentAIElement = null;+ this.messagesContainer.appendChild(el);this.scrollToBottom();this.saveSession();}@@ -744,12 +770,13 @@ export class AgentChat extends Model {}if (this.fullContent) {this.messages.push({role: "assistant", content: this.fullContent || " ", toolCalls: this.currentToolCalls.length > 0 ? this.currentToolCalls.slice() : undefined});+ } else {+ this.currentAIElement.remove();}this.currentAIElement = null;this.currentContent = "";this.fullContent = "";this.currentToolCalls = [];- this.currentToolCalls = [];}if (this.requestStartTime) {this.sessionTotalDuration += Date.now() - this.requestStartTime;diff --git a/kernel/mcp/tools/asset.go b/kernel/mcp/tools/asset.goindex 531feb16ac..1ff7aca27e 100644--- a/kernel/mcp/tools/asset.go+++ b/kernel/mcp/tools/asset.go@@ -33,7 +33,7 @@ var AssetTool = &Tool{"action": {Type: "string", Description: "Operation", Enum: []string{"upload", "unused", "clean"}},"id": {Type: "string", Description: "Document block ID (for upload)"},"files": {Type: "string", Description: "Comma-separated absolute file paths (for upload)"},- "path": {Type: "string", Description: "Single unused asset path to remove (for clean, optional)"},+ "path": {Type: "string", Description: "Single unused asset path to remove, relative to data directory (for clean, optional). Use as returned by the unused action, e.g. assets/image/xxx.png."},},Required: []string{"action"},},diff --git a/kernel/mcp/tools/document.go b/kernel/mcp/tools/document.goindex 29ef65addf..42d39e7897 100644--- a/kernel/mcp/tools/document.go+++ b/kernel/mcp/tools/document.go@@ -18,23 +18,23 @@ package toolsimport ("fmt"- "path""strings""github.com/88250/lute/ast""github.com/siyuan-note/siyuan/kernel/model"+ "github.com/siyuan-note/siyuan/kernel/treenode")var DocumentTool = &Tool{Name: "document",- Description: "Document operations for SiYuan notebooks.\n- get: Get document content by ID. Requires: id.\n- create: Create a new document. Requires: notebook, path (e.g. /folder/doc), title. Optional: markdown.\n- list: List documents in a notebook path. Requires: notebook. Optional: path (default /).\n- delete: Delete a document by ID. Requires: id.\n- rename: Rename a document by ID. Requires: id, title.\n- move: Move a document to a different notebook/path. Requires: id, notebook, path.\n- duplicate: Duplicate a document by ID. Requires: id.\n- search_docs: Search documents by keyword. Requires: keyword.",+ Description: "Document operations for SiYuan notebooks.\n- get: Get document content by ID. Requires: id.\n- create: Create a new document. Requires: notebook, path (hPath, e.g. /folder/doc), title. Optional: markdown.\n- list: List documents at an hPath. Requires: notebook. Optional: path (hPath, default /).\n- delete: Delete a document by ID. Requires: id.\n- rename: Rename a document by ID. Requires: id, title.\n- move: Move a document to a different notebook/hPath. Requires: id, notebook, path (target hPath, e.g. /folder or / for root).\n- duplicate: Duplicate a document by ID. Requires: id.\n- search_docs: Search documents by keyword. Requires: keyword.",InputSchema: ToolSchema{Type: "object",Properties: map[string]Property{"action": {Type: "string", Description: "Operation", Enum: []string{"get", "create", "list", "delete", "rename", "move", "duplicate", "search_docs"}},"id": {Type: "string", Description: "Document block ID"},"title": {Type: "string", Description: "Document title (for create, rename)"},- "path": {Type: "string", Description: "Document path like /folder/doc (for create, list, move)"},+ "path": {Type: "string", Description: "Document hPath, the human-readable path shown in the document tree (e.g. /folder/doc). Used for create, list, move."},"markdown": {Type: "string", Description: "Initial markdown content (for create)"},"keyword": {Type: "string", Description: "Search keyword (for search_docs)"},"notebook": {Type: "string", Description: "Notebook ID (required for create, list, move)"},@@ -101,28 +101,46 @@ func documentCreate(args map[string]interface{}) (CallToolResult, error) {if notebook == "" {return CallToolResult{Content: []ContentItem{{Type: "text", Text: "notebook is required"}}, IsError: true}, nil}- p, _ := args["path"].(string)- if p == "" {+ hPath, _ := args["path"].(string)+ if hPath == "" {return CallToolResult{Content: []ContentItem{{Type: "text", Text: "path is required"}}, IsError: true}, nil}markdown, _ := args["markdown"].(string)title, _ := args["title"].(string)if title == "" {- title = p+ title = hPathif strings.Contains(title, "/") {parts := strings.Split(strings.TrimRight(title, "/"), "/")title = parts[len(parts)-1]}}+ parentPath := "/"+ parentDir := parentDir(hPath)+ if parentDir != "/" {+ bt := treenode.GetBlockTreeRootByHPath(notebook, parentDir)+ if bt == nil {+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "parent path not found: " + parentDir}}, IsError: true}, nil+ }+ parentPath = strings.TrimSuffix(bt.Path, ".sy")+ }+id := ast.NewNodeID()- docPath := path.Join(path.Dir(p), id+".sy")+ docPath := parentPath + "/" + id + ".sy"tree, err := model.CreateDocByMd(notebook, docPath, title, markdown, nil, nil)if err != nil {return CallToolResult{Content: []ContentItem{{Type: "text", Text: fmt.Sprintf("create doc failed: %s", err)}}, IsError: true}, nil}- return CallToolResult{Content: []ContentItem{{Type: "text", Text: fmt.Sprintf("document created: %s (path: %s)", tree.Root.ID, p)}}}, nil+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: fmt.Sprintf("document created: %s (hPath: %s)", tree.Root.ID, hPath)}}}, nil+}++func parentDir(p string) string {+ i := strings.LastIndex(p, "/")+ if i <= 0 {+ return "/"+ }+ return p[:i]}func documentList(args map[string]interface{}) (CallToolResult, error) {@@ -130,20 +148,29 @@ func documentList(args map[string]interface{}) (CallToolResult, error) {if notebook == "" {return CallToolResult{Content: []ContentItem{{Type: "text", Text: "notebook is required"}}, IsError: true}, nil}- path, _ := args["path"].(string)- if path == "" {- path = "/"+ hPath, _ := args["path"].(string)+ if hPath == "" {+ hPath = "/"}- files, _, err := model.ListDocTree(notebook, path, 0, false, false, 128)+ fsPath := hPath+ if hPath != "/" {+ bt := treenode.GetBlockTreeRootByHPath(notebook, hPath)+ if bt == nil {+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "target path not found: " + hPath}}, IsError: true}, nil+ }+ fsPath = bt.Path+ }++ files, _, err := model.ListDocTree(notebook, fsPath, 0, false, false, 128)if err != nil {return CallToolResult{Content: []ContentItem{{Type: "text", Text: fmt.Sprintf("list docs failed: %s", err)}}, IsError: true}, nil}var sb strings.Builder- sb.WriteString(fmt.Sprintf("Documents in %s%s:\n\n", notebook, path))+ sb.WriteString(fmt.Sprintf("Documents in %s (hPath: %s):\n\n", notebook, hPath))for _, f := range files {- sb.WriteString(fmt.Sprintf("- %s (id: %s, path: %s)\n", f.Name, f.ID, f.Path))+ sb.WriteString(fmt.Sprintf("- %s (id: %s, hPath: %s)\n", f.Name, f.ID, hPath+"/"+f.Name))}return CallToolResult{Content: []ContentItem{{Type: "text", Text: sb.String()}}}, nil}@@ -185,8 +212,8 @@ func documentRename(args map[string]interface{}) (CallToolResult, error) {func documentMove(args map[string]interface{}) (CallToolResult, error) {id, _ := args["id"].(string)notebook, _ := args["notebook"].(string)- path, _ := args["path"].(string)- if id == "" || notebook == "" || path == "" {+ hPath, _ := args["path"].(string)+ if id == "" || notebook == "" || hPath == "" {return CallToolResult{Content: []ContentItem{{Type: "text", Text: "id, notebook and path are required"}}, IsError: true}, nil}@@ -195,11 +222,20 @@ func documentMove(args map[string]interface{}) (CallToolResult, error) {return CallToolResult{Content: []ContentItem{{Type: "text", Text: fmt.Sprintf("load doc failed: %s", err)}}, IsError: true}, nil}- if err := model.MoveDocs([]string{tree.Path}, notebook, path, nil); err != nil {+ fsPath := hPath+ if hPath != "/" {+ bt := treenode.GetBlockTreeRootByHPath(notebook, hPath)+ if bt == nil {+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "target path not found: " + hPath}}, IsError: true}, nil+ }+ fsPath = bt.Path+ }++ if err := model.MoveDocs([]string{tree.Path}, notebook, fsPath, nil); err != nil {return CallToolResult{Content: []ContentItem{{Type: "text", Text: fmt.Sprintf("move doc failed: %s", err)}}, IsError: true}, nil}- return CallToolResult{Content: []ContentItem{{Type: "text", Text: fmt.Sprintf("document moved: %s -> %s%s", id, notebook, path)}}}, nil+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: fmt.Sprintf("document moved: %s -> %s (hPath: %s)", id, notebook, hPath)}}}, nil}func documentDuplicate(args map[string]interface{}) (CallToolResult, error) {diff --git a/kernel/mcp/tools/history.go b/kernel/mcp/tools/history.goindex 174f717214..206408cda5 100644--- a/kernel/mcp/tools/history.go+++ b/kernel/mcp/tools/history.go@@ -37,7 +37,7 @@ var HistoryTool = &Tool{"op": {Type: "string", Description: "Operation filter: delete/update/create (for list, search)"},"type": {Type: "number", Description: "Search type: 0=name,1=content,2=asset,3=docID,4=database (default 1)"},"page": {Type: "number", Description: "Page number (default 1)"},- "path": {Type: "string", Description: "History path (for get, rollback)"},+ "path": {Type: "string", Description: "History path relative to workspace directory (for get, rollback). Obtained from list/search output, e.g. history/2024-03-15-.../docid/..."},},Required: []string{"action"},},diff --git a/kernel/mcp/tools/import.go b/kernel/mcp/tools/import.goindex 8be07b18f9..40deff463a 100644--- a/kernel/mcp/tools/import.go+++ b/kernel/mcp/tools/import.go@@ -31,7 +31,7 @@ var ImportTool = &Tool{"action": {Type: "string", Description: "Operation", Enum: []string{"md", "sy", "data"}},"notebook": {Type: "string", Description: "Notebook ID (for md, sy)"},"path": {Type: "string", Description: "Absolute local file path"},- "targetPath": {Type: "string", Description: "Target internal path (for md, sy; default /)"},+ "targetPath": {Type: "string", Description: "Target internal filesystem path (for md, sy; default /). This is the ID-based path like /2022...ws.sy, not hPath."},},Required: []string{"action"},},