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: Render Agent tag names as tags https://github.com/siyuan-note/siyuan/issues/18872
Daniel76655a0d3346
diff --git a/app/src/assets/scss/business/_ai_agent.scss b/app/src/assets/scss/business/_ai_agent.scssindex e3f6faf247..25671865ae 100644--- a/app/src/assets/scss/business/_ai_agent.scss+++ b/app/src/assets/scss/business/_ai_agent.scss@@ -284,6 +284,19 @@.agent-chat__body.b3-typography,.agent-chat__thinking-chat.b3-typography {font-size: var(--b3-font-size);++ span[data-type~="tag"] {+ padding-left: 1px;+ padding-right: 1px;+ border-bottom: 1px solid;+ color: var(--b3-protyle-inline-tag-color);+ transition: var(--b3-transition);+ cursor: pointer;++ &:hover {+ background-color: var(--b3-theme-surface-lighter);+ }+ }}// 消息正文复用全局 b3-typography 排版(与导出预览/编辑器预览一致),diff --git a/kernel/agent/agent.go b/kernel/agent/agent.goindex 430fc3eec4..3afcb86b43 100644--- a/kernel/agent/agent.go+++ b/kernel/agent/agent.go@@ -70,6 +70,7 @@ const systemPrompt = `You are a SiYuan AI assistant. You help users manage their## Response Guidelines- Reply in the language configured in SiYuan's appearance settings.- When mentioning documents/blocks the user can open, format them as markdown links: [title](siyuan://blocks/<blockID>). Only use block IDs actually returned by a tool call (block.get/get_children/breadcrumb/batch_get/search); never fabricate IDs. For general mentions without a specific block, plain text is fine.+- When displaying a SiYuan tag name in chat, render its exact label as <span data-type="tag">label</span>. Keep every label character, including a leading $, inside the span and HTML-escape the label text. Never prefix the label with # or use #label# in chat.- Be concise: summarize rather than repeat large content.- For choices (which notebook/document/action), use the question tool — never a plain text list.- Use markdown; for code blocks always specify the language (e.g. python, go); use $...$ for inline and $...$ for block formulas.diff --git a/kernel/agent/prompt_test.go b/kernel/agent/prompt_test.goindex 928f51c00f..42d8228c3e 100644--- a/kernel/agent/prompt_test.go+++ b/kernel/agent/prompt_test.go@@ -66,6 +66,18 @@ func TestSystemPromptDocumentsBlockReferenceSyntax(t *testing.T) {}}+func TestSystemPromptDocumentsTagRendering(t *testing.T) {+ for _, instruction := range []string{+ `render its exact label as <span data-type="tag">label</span>`,+ `including a leading $, inside the span`,+ `Never prefix the label with # or use #label# in chat`,+ } {+ if !strings.Contains(systemPrompt, instruction) {+ t.Fatalf("system prompt is missing the tag rendering instruction %q", instruction)+ }+ }+}+func TestSystemPromptOmitsUnavailableSkillInstructions(t *testing.T) {capabilities := &capabilitySet{registrations: map[string]*capabilityRegistration{}}prompt := buildSystemPrompt("English", capabilities)diff --git a/kernel/mcp/tools/tag.go b/kernel/mcp/tools/tag.goindex b50c878b4f..aa885c9182 100644--- a/kernel/mcp/tools/tag.go+++ b/kernel/mcp/tools/tag.go@@ -18,6 +18,7 @@ package toolsimport ("fmt"+ "html""strings""github.com/siyuan-note/siyuan/kernel/model"@@ -71,11 +72,17 @@ func tagList(args map[string]any) (CallToolResult, error) {var sb strings.Buildersb.WriteString(fmt.Sprintf("Tags (%d):\n\n", len(tags)))for _, t := range tags {- sb.WriteString(fmt.Sprintf("- #%s\n", t))+ sb.WriteString("- ")+ sb.WriteString(formatTagTextMark(t))+ sb.WriteByte('\n')}return CallToolResult{Content: []ContentItem{{Type: "text", Text: sb.String()}}}, nil}+func formatTagTextMark(label string) string {+ return `<span data-type="tag">` + html.EscapeString(label) + `</span>`+}+func tagRename(args map[string]any) (CallToolResult, error) {oldLabel, _ := args["old"].(string)newLabel, _ := args["new"].(string)@@ -88,7 +95,10 @@ func tagRename(args map[string]any) (CallToolResult, error) {}model.ReloadTag()- return CallToolResult{Content: []ContentItem{{Type: "text", Text: "tag renamed: #" + oldLabel + " -> #" + newLabel}}}, nil+ return CallToolResult{Content: []ContentItem{{+ Type: "text",+ Text: "tag renamed: " + formatTagTextMark(oldLabel) + " -> " + formatTagTextMark(newLabel),+ }}}, nil}func tagRemove(args map[string]any) (CallToolResult, error) {@@ -102,5 +112,5 @@ func tagRemove(args map[string]any) (CallToolResult, error) {}model.ReloadTag()- return CallToolResult{Content: []ContentItem{{Type: "text", Text: "tag removed: #" + label}}}, nil+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "tag removed: " + formatTagTextMark(label)}}}, nil}diff --git a/kernel/mcp/tools/tag_test.go b/kernel/mcp/tools/tag_test.gonew file mode 100644index 0000000000..f441b4e6d7--- /dev/null+++ b/kernel/mcp/tools/tag_test.go@@ -0,0 +1,38 @@+// 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.++package tools++import "testing"++func TestFormatTagTextMark(t *testing.T) {+ tests := []struct {+ name string+ label string+ expected string+ }{+ {+ name: "leading dollar",+ label: "$01want",+ expected: `<span data-type="tag">$01want</span>`,+ },+ {+ name: "HTML characters",+ label: `<script>&"'`,+ expected: `<span data-type="tag"><script>&"'</span>`,+ },+ }++ for _, test := range tests {+ t.Run(test.name, func(t *testing.T) {+ if actual := formatTagTextMark(test.label); actual != test.expected {+ t.Fatalf("unexpected tag text mark: %q", actual)+ }+ })+ }+}