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
Daniel4edcd809b866
diff --git a/app/src/layout/dock/AgentChat.ts b/app/src/layout/dock/AgentChat.tsindex e690437393..b8761336a0 100644--- a/app/src/layout/dock/AgentChat.ts+++ b/app/src/layout/dock/AgentChat.ts@@ -25,6 +25,10 @@ import {renderWelcomeHTML} from "./AgentMessageRenderer";+// Limit on the number of visible block IDs injected into the system prompt to control token usage.+// Mirrors kernel/agent/agent.go maxVisibleBlockIDs.+const maxVisibleBlockIDs = 50;+type EntryBase = { id?: string };type SessionEntry =@@ -962,26 +966,63 @@ export class AgentChat extends Model {/// #endif}- private readEditorContext(editor: { protyle: { block?: { id?: string; rootID?: string }; wysiwyg?: { element?: HTMLElement } } }): IEditorContext | undefined {+ private readEditorContext(editor: {+ protyle: {+ block?: { id?: string; rootID?: string };+ wysiwyg?: { element?: HTMLElement };+ contentElement?: HTMLElement;+ notebookId?: string;+ title?: { editElement?: HTMLElement };+ };+ }): IEditorContext | undefined {const p = editor.protyle;if (!p) {return undefined;}const activeDocID = p.block?.rootID;const focusedBlockID = p.block?.id;+ const activeDocTitle = p.title?.editElement?.textContent?.trim() || undefined;+ const notebookID = p.notebookId || undefined;+const selectedBlockIDs: string[] = [];p.wysiwyg?.element?.querySelectorAll("[data-node-id].protyle-wysiwyg--select")?.forEach(el => {const id = (el as HTMLElement).getAttribute("data-node-id");if (id) { selectedBlockIDs.push(id); }});- if (!activeDocID && !focusedBlockID && selectedBlockIDs.length === 0) {++ // Visible blocks: top-level [data-node-id] children whose bounding rect intersects+ // the scroll container viewport. Long docs are lazily loaded, so wysiwyg.element's+ // children are already the loaded subset; this further narrows to what is on screen.+ const visibleBlockIDs: string[] = [];+ const scrollContainer = (p.contentElement || p.wysiwyg?.element?.parentElement as HTMLElement | undefined);+ if (scrollContainer && p.wysiwyg?.element) {+ const view = scrollContainer.getBoundingClientRect();+ const children = p.wysiwyg.element.children;+ for (let i = 0; i < children.length; i++) {+ const child = children[i] as HTMLElement;+ const id = child.getAttribute("data-node-id");+ if (!id) { continue; }+ const rect = child.getBoundingClientRect();+ if (rect.height === 0) { continue; }+ if (rect.bottom >= view.top && rect.top <= view.bottom) {+ visibleBlockIDs.push(id);+ }+ if (visibleBlockIDs.length >= maxVisibleBlockIDs) { break; }+ }+ }++ if (!activeDocID && !activeDocTitle && !notebookID &&+ !focusedBlockID && selectedBlockIDs.length === 0 && visibleBlockIDs.length === 0) {return undefined;}const ctx: IEditorContext = {};if (activeDocID) { ctx.activeDocID = activeDocID; }+ if (activeDocTitle) { ctx.activeDocTitle = activeDocTitle; }+ if (notebookID) { ctx.notebookID = notebookID; }if (focusedBlockID && focusedBlockID !== activeDocID) { ctx.focusedBlockID = focusedBlockID; }if (selectedBlockIDs.length > 0) { ctx.selectedBlockIDs = selectedBlockIDs; }+ if (visibleBlockIDs.length > 0) { ctx.visibleBlockIDs = visibleBlockIDs; }return ctx;}diff --git a/app/src/util/agentSSE.ts b/app/src/util/agentSSE.tsindex 664226dcf4..db5c86cb87 100644--- a/app/src/util/agentSSE.ts+++ b/app/src/util/agentSSE.ts@@ -44,8 +44,11 @@ export type ISSEResult = {export type IEditorContext = {activeDocID?: string;+ activeDocTitle?: string;+ notebookID?: string;focusedBlockID?: string;selectedBlockIDs?: string[];+ visibleBlockIDs?: string[];};export async function fetchAgentSSE(diff --git a/kernel/agent/agent.go b/kernel/agent/agent.goindex 344600c9a2..b134a321f2 100644--- a/kernel/agent/agent.go+++ b/kernel/agent/agent.go@@ -111,6 +111,9 @@ const systemPrompt = `You are a SiYuan AI assistant. You help users manage theirvar maxToolCallRounds = 64+// maxVisibleBlockIDs 限制注入到 system prompt 的"视口可见块"数量,控制 token 开销。+var maxVisibleBlockIDs = 50+type confirmResult struct {approved boolalways bool@@ -209,8 +212,11 @@ type Reference struct {// 与 Reference 的处理方式保持一致。type EditorContext struct {ActiveDocID string `json:"activeDocID,omitempty"` // 当前激活文档的 root block ID+ ActiveDocTitle string `json:"activeDocTitle,omitempty"` // 当前文档标题+ NotebookID string `json:"notebookID,omitempty"` // 当前文档所属笔记本 IDFocusedBlockID string `json:"focusedBlockID,omitempty"` // 光标/聚焦所在块 ID(editor.protyle.block.id)SelectedBlockIDs []string `json:"selectedBlockIDs,omitempty"` // 用户选中的块 ID 列表+ VisibleBlockIDs []string `json:"visibleBlockIDs,omitempty"` // 视口内可见块 ID 列表(已截断至上限)}type checkpointThinkingStep struct {@@ -825,12 +831,34 @@ func buildSystemPrompt(language string, references []Reference, editorCtx Editor}sb.WriteString("Use the block tools to fetch their actual content before responding.")}- if editorCtx.ActiveDocID != "" || len(editorCtx.SelectedBlockIDs) > 0 || editorCtx.FocusedBlockID != "" {+ if editorCtx.ActiveDocID != "" || editorCtx.ActiveDocTitle != "" || editorCtx.NotebookID != "" ||+ len(editorCtx.SelectedBlockIDs) > 0 || editorCtx.FocusedBlockID != "" || len(editorCtx.VisibleBlockIDs) > 0 {sb.WriteString("\n\n<editor_context>\n")sb.WriteString("This is the user's editor state at the moment they sent the message. It may be stale by now.\n")- if editorCtx.ActiveDocID != "" {- sb.WriteString("Active document root block id: ")- sb.WriteString(editorCtx.ActiveDocID)+ if editorCtx.ActiveDocID != "" || editorCtx.ActiveDocTitle != "" {+ sb.WriteString("Active document: ")+ if editorCtx.ActiveDocTitle != "" {+ sb.WriteString(editorCtx.ActiveDocTitle)+ } else {+ sb.WriteString("(untitled)")+ }+ if editorCtx.ActiveDocID != "" {+ sb.WriteString(" (root block id: ")+ sb.WriteString(editorCtx.ActiveDocID)+ if editorCtx.NotebookID != "" {+ sb.WriteString(", notebook: ")+ sb.WriteString(editorCtx.NotebookID)+ }+ sb.WriteString(")")+ } else if editorCtx.NotebookID != "" {+ sb.WriteString(" (notebook: ")+ sb.WriteString(editorCtx.NotebookID)+ sb.WriteString(")")+ }+ sb.WriteString("\n")+ } else if editorCtx.NotebookID != "" {+ sb.WriteString("Notebook: ")+ sb.WriteString(editorCtx.NotebookID)sb.WriteString("\n")}if editorCtx.FocusedBlockID != "" && editorCtx.FocusedBlockID != editorCtx.ActiveDocID {@@ -846,6 +874,27 @@ func buildSystemPrompt(language string, references []Reference, editorCtx Editorsb.WriteString("\n")}}+ if len(editorCtx.VisibleBlockIDs) > 0 {+ totalVisible := len(editorCtx.VisibleBlockIDs)+ if totalVisible > maxVisibleBlockIDs {+ sb.WriteString("Visible block ids (showing first ")+ sb.WriteString(fmt.Sprintf("%d", maxVisibleBlockIDs))+ sb.WriteString(" of ")+ sb.WriteString(fmt.Sprintf("%d", totalVisible))+ sb.WriteString("):\n")+ } else {+ sb.WriteString("Visible block ids:\n")+ }+ limit := totalVisible+ if limit > maxVisibleBlockIDs {+ limit = maxVisibleBlockIDs+ }+ for i := 0; i < limit; i++ {+ sb.WriteString("- ")+ sb.WriteString(editorCtx.VisibleBlockIDs[i])+ sb.WriteString("\n")+ }+ }sb.WriteString("Use the block tools (e.g. block with action \"get\") to fetch actual content before responding.")sb.WriteString("\n</editor_context>")}