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

Danield15652747eb1
diff --git a/kernel/agent/agent.go b/kernel/agent/agent.go
index 9fd47758c9..390ee08ee7 100644
--- a/kernel/agent/agent.go
+++ b/kernel/agent/agent.go
@@ -535,7 +535,7 @@ 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>"
- skills := kernelModel.DiscoverAgentSkills()
+ skills := util.DiscoverSkills()
if len(skills) > 0 {
prompt += "\n\n<available_skills>\n"
for _, s := range skills {
diff --git a/kernel/mcp/tools/skill.go b/kernel/mcp/tools/skill.go
index 9d6b295778..525b361553 100644
--- a/kernel/mcp/tools/skill.go
+++ b/kernel/mcp/tools/skill.go
@@ -18,13 +18,14 @@ package tools
import (
"fmt"
+ "strings"
- "github.com/siyuan-note/siyuan/kernel/model"
+ "github.com/siyuan-note/siyuan/kernel/util"
)
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.",
+ Description: "Load a specialized skill. Available skills are listed below.\n\n" + skillListDesc() + "\n\nThe skill tool loads the full instructions and resources for a skill when invoked. The skill name must match one of the listed skills.",
InputSchema: ToolSchema{
Type: "object",
Properties: map[string]Property{
@@ -48,7 +49,7 @@ func skillHandler(args map[string]interface{}) (CallToolResult, error) {
}, nil
}
- content := model.LoadAgentSkillContent(name)
+ content := util.LoadSkillContent(name)
if content == "" {
return CallToolResult{
Content: []ContentItem{{Type: "text", Text: fmt.Sprintf("skill not found: %s", name)}},
@@ -61,3 +62,16 @@ func skillHandler(args map[string]interface{}) (CallToolResult, error) {
Content: []ContentItem{{Type: "text", Text: result}},
}, nil
}
+
+func skillListDesc() string {
+ skills := util.DiscoverSkills()
+ if len(skills) == 0 {
+ return "No skills are currently available."
+ }
+ var sb strings.Builder
+ sb.WriteString("Available skills:\n")
+ for _, s := range skills {
+ sb.WriteString("- **" + s.Name + "**: " + s.Description + "\n")
+ }
+ return sb.String()
+}
diff --git a/kernel/model/conf.go b/kernel/model/conf.go
index 9137a16bc4..1f6cb0e83b 100644
--- a/kernel/model/conf.go
+++ b/kernel/model/conf.go
@@ -1213,6 +1213,7 @@ func clearWorkspaceTemp() {
// v3.7.0-dev 开发版数据索引队列,后面改成 index.queue 了
os.RemoveAll(filepath.Join(util.TempDir, "queue.wal"))
os.RemoveAll(filepath.Join(util.TempDir, "queue.wal.lock"))
+ os.RemoveAll(filepath.Join(util.DataDir, "storage", "ai", "agent", "todos"))
logging.LogInfof("cleared workspace temp")
}
diff --git a/kernel/model/skill.go b/kernel/model/skill.go
deleted file mode 100644
index e7a3467ce8..0000000000
--- a/kernel/model/skill.go
+++ /dev/null
@@ -1,137 +0,0 @@
-// 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
-}
diff --git a/kernel/model/todo.go b/kernel/model/todo.go
index 66bf3ed3fd..4ff02e9a03 100644
--- a/kernel/model/todo.go
+++ b/kernel/model/todo.go
@@ -35,16 +35,12 @@ type AgentTodoList struct {
Todos []AgentTodoItem `json:"todos"`
}
-func agentTodosDir() string {
- return filepath.Join(util.DataDir, "storage", "ai", "agent", "todos")
-}
-
func agentTodosPath(sessionID string) string {
- return filepath.Join(agentTodosDir(), sessionID+".json")
+ return filepath.Join(util.DataDir, "storage", "ai", "agent", "sessions", sessionID, "todos.json")
}
func SaveAgentTodos(sessionID string, todos []AgentTodoItem) error {
- dir := agentTodosDir()
+ dir := filepath.Join(util.DataDir, "storage", "ai", "agent", "sessions", sessionID)
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}
diff --git a/kernel/util/skill.go b/kernel/util/skill.go
new file mode 100644
index 0000000000..f2deaa34ba
--- /dev/null
+++ b/kernel/util/skill.go
@@ -0,0 +1,141 @@
+// 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"
+ "strings"
+
+ "github.com/siyuan-note/filelock"
+)
+
+type SkillInfo struct {
+ Name string
+ Description string
+}
+
+func SkillsDir() string {
+ return filepath.Join(DataDir, "storage", "ai", "agent", "skills")
+}
+
+func DiscoverSkills() []SkillInfo {
+ dir := SkillsDir()
+ entries, err := os.ReadDir(dir)
+ if err != nil {
+ return nil
+ }
+
+ var skills []SkillInfo
+ for _, e := range entries {
+ if !e.IsDir() {
+ continue
+ }
+ skillDir := e.Name()
+ skillMdPath := filepath.Join(dir, skillDir, "SKILL.md")
+ b, err := filelock.ReadFile(skillMdPath)
+ if err != nil {
+ continue
+ }
+ fm, body := parseSkillFrontmatter(string(b))
+ name := fm["name"]
+ if name == "" {
+ name = skillDir
+ }
+ desc := fm["description"]
+ if desc == "" {
+ desc = firstLine(body)
+ }
+ skills = append(skills, SkillInfo{
+ Name: name,
+ Description: desc,
+ })
+ }
+ return skills
+}
+
+func LoadSkillContent(name string) string {
+ dir := SkillsDir()
+ entries, err := os.ReadDir(dir)
+ if err != nil {
+ return ""
+ }
+ for _, e := range entries {
+ if !e.IsDir() {
+ continue
+ }
+ skillMdPath := filepath.Join(dir, e.Name(), "SKILL.md")
+ b, err := filelock.ReadFile(skillMdPath)
+ if err != nil {
+ continue
+ }
+ fm, body := parseSkillFrontmatter(string(b))
+ skillName := fm["name"]
+ if skillName == "" {
+ skillName = e.Name()
+ }
+ if strings.EqualFold(skillName, name) || strings.EqualFold(e.Name(), name) {
+ return body
+ }
+ }
+ return ""
+}
+
+func parseSkillFrontmatter(text string) (fm map[string]string, body string) {
+ fm = map[string]string{}
+ text = strings.TrimSpace(text)
+ if !strings.HasPrefix(text, "---") {
+ return fm, text
+ }
+ end := strings.Index(text[3:], "\n---")
+ if end < 0 {
+ return fm, text
+ }
+ raw := text[3 : 3+end]
+ body = strings.TrimSpace(text[3+end+4:])
+ for _, line := range strings.Split(raw, "\n") {
+ line = strings.TrimSpace(line)
+ if line == "" || strings.HasPrefix(line, "#") {
+ continue
+ }
+ parts := strings.SplitN(line, ":", 2)
+ if len(parts) != 2 {
+ continue
+ }
+ key := strings.TrimSpace(parts[0])
+ val := strings.TrimSpace(parts[1])
+ if key == "name" || key == "description" {
+ fm[key] = val
+ }
+ }
+ return fm, body
+}
+
+func firstLine(text string) string {
+ text = strings.TrimSpace(text)
+ if text == "" {
+ return ""
+ }
+ idx := strings.IndexAny(text, "\n\r")
+ if idx > 0 {
+ text = text[:idx]
+ }
+ if len(text) > 200 {
+ text = text[:200] + "..."
+ }
+ return text
+}