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
Daniel35290d904d12
diff --git a/kernel/agent/agent.go b/kernel/agent/agent.goindex 5e70347426..9fd47758c9 100644--- a/kernel/agent/agent.go+++ b/kernel/agent/agent.go@@ -533,7 +533,22 @@ func handleQuestion(argsJSON string, ch chan<- AgentEvent, timeout time.Duration}func buildMessages(history []UserMessage, language string, references []Reference) []openai.ChatCompletionMessage {- var prompt = systemPrompt + "\n\n<env>\nWorkspace: " + util.WorkspaceDir + "\nVersion: " + util.Ver + "\nToday's date: " + time.Now().Format("2006-01-02 Mon") + "\nContainer: " + util.Container + "\n</env>\n\nReply in " + langName(language) + "."+ var prompt = systemPrompt + "\n\n<env>\nWorkspace: " + util.WorkspaceDir + "\nVersion: " + util.Ver + "\nToday's date: " + time.Now().Format("2006-01-02 Mon") + "\nContainer: " + util.Container + "\n</env>"++ skills := kernelModel.DiscoverAgentSkills()+ if len(skills) > 0 {+ prompt += "\n\n<available_skills>\n"+ for _, s := range skills {+ prompt += " <skill>\n"+ prompt += " <name>" + s.Name + "</name>\n"+ prompt += " <description>" + s.Description + "</description>\n"+ prompt += " </skill>\n"+ }+ prompt += "</available_skills>\n\n"+ prompt += "Use the skill tool to load a skill when a task matches its description."+ }++ prompt += "\n\nReply in " + langName(language) + "."if len(references) > 0 {prompt += "\n\nThe user has referenced the following content blocks:\n"for _, ref := range references {diff --git a/kernel/mcp/tools/skill.go b/kernel/mcp/tools/skill.gonew file mode 100644index 0000000000..9d6b295778--- /dev/null+++ b/kernel/mcp/tools/skill.go@@ -0,0 +1,63 @@+// 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 tools++import (+ "fmt"++ "github.com/siyuan-note/siyuan/kernel/model"+)++var SkillTool = &Tool{+ Name: "skill",+ Description: "Load a specialized skill when the task at hand matches one of the skills listed in the available skills. The skill name must match one of the skills listed in the system prompt.\n- name: The skill name to load.",+ InputSchema: ToolSchema{+ Type: "object",+ Properties: map[string]Property{+ "name": {Type: "string", Description: "The name of the skill to load"},+ },+ Required: []string{"name"},+ },+ Handler: skillHandler,+}++func init() {+ register(SkillTool)+}++func skillHandler(args map[string]interface{}) (CallToolResult, error) {+ name, _ := args["name"].(string)+ if name == "" {+ return CallToolResult{+ Content: []ContentItem{{Type: "text", Text: "skill name is required"}},+ IsError: true,+ }, nil+ }++ content := model.LoadAgentSkillContent(name)+ if content == "" {+ return CallToolResult{+ Content: []ContentItem{{Type: "text", Text: fmt.Sprintf("skill not found: %s", name)}},+ IsError: true,+ }, nil+ }++ result := "<skill_content name=\"" + name + "\">\n\n" + content + "\n\n</skill_content>"+ return CallToolResult{+ Content: []ContentItem{{Type: "text", Text: result}},+ }, nil+}diff --git a/kernel/model/skill.go b/kernel/model/skill.gonew file mode 100644index 0000000000..e7a3467ce8--- /dev/null+++ b/kernel/model/skill.go@@ -0,0 +1,137 @@+// 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 model++import (+ "strings"++ "github.com/siyuan-note/siyuan/kernel/treenode"+)++const skillsNotebookName = "Skills"++type AgentSkillInfo struct {+ Name string+ Description string+ ID string+}++func DiscoverAgentSkills() []AgentSkillInfo {+ notebooks, err := ListNotebooks()+ if err != nil {+ return nil+ }++ var skillsNBID string+ for _, nb := range notebooks {+ if nb.Name == skillsNotebookName {+ skillsNBID = nb.ID+ break+ }+ }+ if skillsNBID == "" {+ return nil+ }++ docs, _, err := ListDocTree(skillsNBID, "/", 0, false, false, 128)+ if err != nil {+ return nil+ }++ var skills []AgentSkillInfo+ for _, doc := range docs {+ desc := firstBlockText(doc.ID)+ skills = append(skills, AgentSkillInfo{+ Name: doc.Name,+ Description: desc,+ ID: doc.ID,+ })+ }+ return skills+}++func LoadAgentSkillContent(name string) string {+ notebooks, err := ListNotebooks()+ if err != nil {+ return ""+ }+ var skillsNBID string+ for _, nb := range notebooks {+ if nb.Name == skillsNotebookName {+ skillsNBID = nb.ID+ break+ }+ }+ if skillsNBID == "" {+ return ""+ }++ docs, _, err := ListDocTree(skillsNBID, "/", 0, false, false, 128)+ if err != nil {+ return ""+ }++ for _, doc := range docs {+ if strings.EqualFold(doc.Name, name) {+ return readDocMarkdown(doc.ID)+ }+ }++ results := SearchDocs(name, false, nil)+ for _, r := range results {+ if strings.EqualFold(r["box"], skillsNBID) {+ return readDocMarkdown(r["id"])+ }+ }+ return ""+}++func firstBlockText(docID string) string {+ tree, err := LoadTreeByBlockID(docID)+ if err != nil {+ return ""+ }+ root := tree.Root+ if root == nil {+ return ""+ }+ for child := root.FirstChild; child != nil; child = child.Next {+ typ := treenode.TypeAbbr(child.Type.String())+ if typ == "p" || typ == "h1" || typ == "h2" {+ text := strings.TrimSpace(child.Text())+ if text != "" {+ if len(text) > 200 {+ text = text[:200] + "..."+ }+ return text+ }+ }+ }+ return ""+}++func readDocMarkdown(docID string) string {+ tree, err := LoadTreeByBlockID(docID)+ if err != nil {+ return ""+ }+ b, _ := GetBlock(docID, tree)+ if b == nil {+ return ""+ }+ return b.Markdown+}