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

Daniel0537ff35e304
diff --git a/app/src/layout/dock/AgentChat.ts b/app/src/layout/dock/AgentChat.ts
index 711acdd193..e690437393 100644
--- a/app/src/layout/dock/AgentChat.ts
+++ b/app/src/layout/dock/AgentChat.ts
@@ -1,8 +1,9 @@
import {Tab} from "../Tab";
import {Model} from "../Model";
import {App} from "../../index";
-import {fetchAgentSSE, ISSEResult} from "../../util/agentSSE";
+import {fetchAgentSSE, IEditorContext, ISSEResult} from "../../util/agentSSE";
import {mountComposer} from "./AgentComposer";
+import {getAllEditor} from "../getAll";
import {AgentSession, SessionStore} from "./SessionStore";
import {AgentSessionPanel} from "./AgentSessionPanel";
import {getDockByType} from "../tabUtil";
@@ -102,6 +103,8 @@ export class AgentChat extends Model {
private modelMenuIndex = 0;
private modelOptions: Array<{ id: string; name: string }> = [];
private userScrolledUp = false;
+ private programmaticScroll = false;
+ private stickResizeObserver: ResizeObserver | null = null;
private scrollBottomBtn: HTMLElement;
private navRail: HTMLElement;
private navExpandTimer = 0;
@@ -165,11 +168,21 @@ export class AgentChat extends Model {
this.modelTrigger = panel.querySelector(".agent-chat__model-trigger") as HTMLElement;
this.scrollBottomBtn = panel.querySelector(".agent-chat__scroll-bottom") as HTMLElement;
this.messagesContainer.addEventListener("scroll", () => {
+ if (this.programmaticScroll) { return; }
const { scrollTop, scrollHeight, clientHeight } = this.messagesContainer;
- this.userScrolledUp = scrollHeight - scrollTop - clientHeight >= 20;
+ const distanceFromBottom = scrollHeight - scrollTop - clientHeight;
+ // Hysteresis: only mark as scrolled-up when clearly above bottom (>=60),
+ // and only return to sticky when really at the bottom (<=10).
+ // This prevents the follow state from rapidly toggling while streaming
+ // causes scrollHeight to grow asynchronously.
+ if (!this.userScrolledUp) {
+ this.userScrolledUp = distanceFromBottom >= 60;
+ } else if (distanceFromBottom <= 10) {
+ this.userScrolledUp = false;
+ }
this.scrollBottomBtn.classList.toggle("agent-chat__scroll-bottom--visible", this.userScrolledUp);
this.updateActiveMarker();
- });
+ }, {passive: true});
const messagesWrap = panel.querySelector(".agent-chat__messages-wrap") as HTMLElement;
this.initNavRail(messagesWrap);
@@ -816,6 +829,7 @@ export class AgentChat extends Model {
const sendData = this.composer.getSendData();
const text = sendData.text;
const refs = sendData.references;
+ const editorContext = this.captureEditorContext();
if (!text || this.isStreaming) {
return;
}
@@ -862,9 +876,115 @@ export class AgentChat extends Model {
this.abortController.signal,
this.sessionId,
this.getSelectedModel(),
+ undefined,
+ editorContext,
);
}
+ // Capture a read-only snapshot of the user's editor to inject into the system prompt.
+ // Strategy: scan ALL editors. Prefer one that (a) is visible and (b) has selected blocks;
+ // this directly targets "user selected blocks here" regardless of which window has focus.
+ // Falls back to the editor hosting the DOM selection, then the most-recently-activated tab.
+ private captureEditorContext(): IEditorContext | undefined {
+ /// #if MOBILE
+ const mobEditor = window.siyuan.mobile.editor || window.siyuan.mobile.popEditor;
+ if (mobEditor?.protyle && !mobEditor.protyle.element.classList.contains("fn__none")) {
+ return this.readEditorContext(mobEditor);
+ }
+ return undefined;
+ /// #else
+ const allEditor = getAllEditor();
+ if (!allEditor || allEditor.length === 0) {
+ return undefined;
+ }
+ const isEditable = (e: { protyle: { element: HTMLElement } }) =>
+ !e.protyle.element.classList.contains("fn__none") &&
+ e.protyle.element.closest(".layout__center") !== null;
+
+ // Aggregate selected block IDs across ALL editors (user may have selected blocks
+ // in one editor while a different one is "active").
+ let allSelected: string[] = [];
+ allEditor.forEach(e => {
+ e.protyle?.wysiwyg?.element?.querySelectorAll("[data-node-id].protyle-wysiwyg--select")
+ ?.forEach(el => {
+ const id = (el as HTMLElement).getAttribute("data-node-id");
+ if (id) { allSelected.push(id); }
+ });
+ });
+ allSelected = Array.from(new Set(allSelected));
+
+ // Candidate selection, in priority order:
+ let candidate: { protyle: { block?: { id?: string; rootID?: string }; wysiwyg?: { element?: HTMLElement }; element: HTMLElement; model?: { parent?: { headElement?: HTMLElement } } } } | undefined;
+
+ // 1) An editable editor that has its own selected blocks.
+ candidate = allEditor.find(e => isEditable(e) &&
+ !!e.protyle?.wysiwyg?.element?.querySelector(".protyle-wysiwyg--select"));
+ // 2) The editor hosting the current DOM selection.
+ if (!candidate) {
+ const domSel = window.getSelection();
+ const range = domSel && domSel.rangeCount > 0 ? domSel.getRangeAt(0) : null;
+ if (range) {
+ candidate = allEditor.find(e => e.protyle.element.contains(range.startContainer));
+ }
+ }
+ // 3) The most-recently-activated focused document tab (data-activetime).
+ if (!candidate) {
+ let activeTime = 0;
+ allEditor.forEach(e => {
+ let head = e.protyle.model?.parent?.headElement;
+ if (!head && e.protyle.element.getBoundingClientRect().height > 0) {
+ const tabBody = e.protyle.element.closest(".fn__flex-1[data-id]");
+ if (tabBody) {
+ head = document.querySelector(
+ `.layout-tab-bar .item[data-id="${tabBody.getAttribute("data-id")}"]`);
+ }
+ }
+ if (head && head.classList.contains("item--focus") &&
+ parseInt(head.dataset.activetime || "0") > activeTime) {
+ activeTime = parseInt(head.dataset.activetime || "0");
+ candidate = e;
+ }
+ });
+ }
+ // 4) Any visible (non-fn__none) editor.
+ if (!candidate) {
+ candidate = allEditor.find(e => !e.protyle.element.classList.contains("fn__none"));
+ }
+
+ const ctx = candidate ? this.readEditorContext(candidate) : undefined;
+ // Even if no candidate editor was located, surface the globally-collected selections.
+ if ((!ctx || !ctx.selectedBlockIDs || ctx.selectedBlockIDs.length === 0) && allSelected.length > 0) {
+ const merged: IEditorContext = ctx ? {...ctx} : {};
+ merged.selectedBlockIDs = allSelected;
+ return merged;
+ }
+ return ctx;
+ /// #endif
+ }
+
+ private readEditorContext(editor: { protyle: { block?: { id?: string; rootID?: string }; wysiwyg?: { element?: HTMLElement } } }): IEditorContext | undefined {
+ const p = editor.protyle;
+ if (!p) {
+ return undefined;
+ }
+ const activeDocID = p.block?.rootID;
+ const focusedBlockID = p.block?.id;
+ 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) {
+ return undefined;
+ }
+ const ctx: IEditorContext = {};
+ if (activeDocID) { ctx.activeDocID = activeDocID; }
+ if (focusedBlockID && focusedBlockID !== activeDocID) { ctx.focusedBlockID = focusedBlockID; }
+ if (selectedBlockIDs.length > 0) { ctx.selectedBlockIDs = selectedBlockIDs; }
+ return ctx;
+ }
+
private async handleSSEEvent(event: ISSEResult) {
try {
switch (event.type) {
@@ -963,6 +1083,7 @@ export class AgentChat extends Model {
el.innerHTML = '<div class="agent-chat__body agent-chat__body--streaming"></div>';
this.messagesContainer.appendChild(el);
this.scrollToBottom();
+ this.observeStickTarget(el);
return el;
}
@@ -983,9 +1104,7 @@ export class AgentChat extends Model {
}
chatEl.innerHTML = this.lute.MarkdownStr("", this.currentContent) || escapeHtml(this.currentContent);
postRender(chatEl);
- if (!this.userScrolledUp) {
- this.messagesContainer.scrollTop = this.messagesContainer.scrollHeight;
- }
+ this.scrollToBottom();
return;
}
@@ -1002,9 +1121,7 @@ export class AgentChat extends Model {
bodyEl.innerHTML = this.lute.MarkdownStr("", this.currentContent) || escapeHtml(this.currentContent);
postRender(bodyEl);
void bodyEl.offsetHeight; // force reflow
- if (!this.userScrolledUp) {
- this.messagesContainer.scrollTop = this.messagesContainer.scrollHeight;
- }
+ this.scrollToBottom();
}
});
}
@@ -1177,6 +1294,7 @@ export class AgentChat extends Model {
bindThinkingCardToggle(el);
this.insertBeforeAI(el);
this.scrollToBottom();
+ this.observeStickTarget(el);
}
private appendReasoning(token: string) {
@@ -1283,6 +1401,7 @@ export class AgentChat extends Model {
all[i].remove();
}
this.currentAIElement = null;
+ this.observeStickTarget(null);
this.currentContent = "";
this.fullContent = "";
this.currentToolCalls = [];
@@ -1375,6 +1494,7 @@ export class AgentChat extends Model {
this.entries.push({id: SessionStore.newSessionId(), type: "assistant", content: "", toolCalls: this.currentToolCalls.slice()});
}
this.currentAIElement = null;
+ this.observeStickTarget(null);
this.currentAssistantEntryId = "";
this.currentContent = "";
this.fullContent = "";
@@ -1560,6 +1680,7 @@ export class AgentChat extends Model {
});
}
this.currentAIElement = null;
+ this.observeStickTarget(null);
this.currentAssistantEntryId = "";
this.currentContent = "";
this.fullContent = "";
@@ -1900,9 +2021,38 @@ export class AgentChat extends Model {
private scrollToBottom(force = false) {
if (!force && this.userScrolledUp) { return; }
+ // Guard with a flag so the resulting scroll event can be told apart from
+ // a user-driven scroll. Without this, the programmatic stick-to-bottom
+ // write itself trips the scroll handler and, while streaming, flips
+ // userScrolledUp on transiently (scrollHeight keeps growing) which
+ // immediately breaks follow-scroll.
+ this.programmaticScroll = true;
requestAnimationFrame(() => {
this.messagesContainer.scrollTop = this.messagesContainer.scrollHeight;
+ // Reset the flag only after the scroll event caused by this write has
+ // been dispatched (a second RAF runs after layout/event delivery).
+ requestAnimationFrame(() => {
+ this.programmaticScroll = false;
+ });
+ });
+ }
+
+ // Observe a streaming message card so that asynchronous content growth
+ // (code highlighting, images, mermaid, fonts) keeps the view pinned to the
+ // bottom while the user has not scrolled up. token frames only fire when a
+ // chunk arrives; this closes the gap between chunks.
+ private observeStickTarget(el: HTMLElement | null) {
+ if (this.stickResizeObserver) {
+ this.stickResizeObserver.disconnect();
+ this.stickResizeObserver = null;
+ }
+ if (!el) { return; }
+ this.stickResizeObserver = new ResizeObserver(() => {
+ if (!this.userScrolledUp) {
+ this.scrollToBottom();
+ }
});
+ this.stickResizeObserver.observe(el);
}
private toolCategory(name: string): string {
diff --git a/app/src/util/agentSSE.ts b/app/src/util/agentSSE.ts
index 8e5ed50dbc..664226dcf4 100644
--- a/app/src/util/agentSSE.ts
+++ b/app/src/util/agentSSE.ts
@@ -42,6 +42,12 @@ export type ISSEResult = {
snapshotID: string;
};
+export type IEditorContext = {
+ activeDocID?: string;
+ focusedBlockID?: string;
+ selectedBlockIDs?: string[];
+};
+
export async function fetchAgentSSE(
message: string,
language: string,
@@ -52,12 +58,14 @@ export async function fetchAgentSSE(
sessionID?: string,
model?: string,
regenerate?: boolean,
+ editorContext?: IEditorContext,
): Promise<void> {
try {
const body: Record<string, unknown> = {message: message, language: language, references: references};
if (sessionID) { body.sessionID = sessionID; }
if (model) { body.model = model; }
if (regenerate) { body.regenerate = regenerate; }
+ if (editorContext) { body.editorContext = editorContext; }
const response = await fetch("/api/ai/agent/chat", {
method: "POST",
diff --git a/kernel/agent/agent.go b/kernel/agent/agent.go
index 252fef0360..344600c9a2 100644
--- a/kernel/agent/agent.go
+++ b/kernel/agent/agent.go
@@ -204,6 +204,15 @@ type Reference struct {
Title string `json:"title"`
}
+// EditorContext 是发送消息时前端编辑器的只读状态快照。
+// 字段有意只传 ID 而不传正文 —— system prompt 会指示 LLM 用 block 工具按需拉取内容,
+// 与 Reference 的处理方式保持一致。
+type EditorContext struct {
+ ActiveDocID string `json:"activeDocID,omitempty"` // 当前激活文档的 root block ID
+ FocusedBlockID string `json:"focusedBlockID,omitempty"` // 光标/聚焦所在块 ID(editor.protyle.block.id)
+ SelectedBlockIDs []string `json:"selectedBlockIDs,omitempty"` // 用户选中的块 ID 列表
+}
+
type checkpointThinkingStep struct {
Reasoning string `json:"reasoning"`
Text string `json:"text"`
@@ -233,7 +242,7 @@ type agentCheckpoint struct {
AlwaysAllow bool `json:"alwaysAllow,omitempty"`
}
-func AgentChat(ctx context.Context, client *openai.Client, model string, sessionID string, userMessage string, language string, references []Reference, regenerate bool, confirmTimeout time.Duration, maxRetries int) <-chan AgentEvent {
+func AgentChat(ctx context.Context, client *openai.Client, model string, sessionID string, userMessage string, language string, references []Reference, editorCtx EditorContext, regenerate bool, confirmTimeout time.Duration, maxRetries int) <-chan AgentEvent {
ch := make(chan AgentEvent, 100)
go func() {
@@ -281,14 +290,14 @@ func AgentChat(ctx context.Context, client *openai.Client, model string, session
}
checkpointMsgs = truncated
checkpointMsgs = append(checkpointMsgs, AgentMessage{Role: "user", Content: userMessage})
- messages = checkpointMessagesToOpenAI(checkpointMsgs, language, references)
+ messages = checkpointMessagesToOpenAI(checkpointMsgs, language, references, editorCtx)
}
}
}
if messages == nil {
checkpointMsgs = []AgentMessage{{Role: "user", Content: userMessage}}
- messages = buildInitialMessages(userMessage, language, references)
+ messages = buildInitialMessages(userMessage, language, references, editorCtx)
}
temperature := kernelModel.Conf.AI.Agent.Temperature
@@ -759,7 +768,7 @@ func handleQuestion(ctx context.Context, argsJSON string, ch chan<- AgentEvent,
return strings.Join(answer.Answers, ", ")
}
-func buildSystemPrompt(language string, references []Reference) string {
+func buildSystemPrompt(language string, references []Reference, editorCtx EditorContext) string {
var sb strings.Builder
sb.WriteString(systemPrompt)
sb.WriteString("\n\n<env>\nWorkspace: ")
@@ -816,12 +825,36 @@ func buildSystemPrompt(language string, references []Reference) string {
}
sb.WriteString("Use the block tools to fetch their actual content before responding.")
}
+ if editorCtx.ActiveDocID != "" || len(editorCtx.SelectedBlockIDs) > 0 || editorCtx.FocusedBlockID != "" {
+ 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)
+ sb.WriteString("\n")
+ }
+ if editorCtx.FocusedBlockID != "" && editorCtx.FocusedBlockID != editorCtx.ActiveDocID {
+ sb.WriteString("Cursor/focused block id: ")
+ sb.WriteString(editorCtx.FocusedBlockID)
+ sb.WriteString("\n")
+ }
+ if len(editorCtx.SelectedBlockIDs) > 0 {
+ sb.WriteString("Selected block ids:\n")
+ for _, id := range editorCtx.SelectedBlockIDs {
+ sb.WriteString("- ")
+ sb.WriteString(id)
+ 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>")
+ }
return sb.String()
}
-func buildInitialMessages(userMessage string, language string, references []Reference) []openai.ChatCompletionMessage {
+func buildInitialMessages(userMessage string, language string, references []Reference, editorCtx EditorContext) []openai.ChatCompletionMessage {
return []openai.ChatCompletionMessage{
- {Role: openai.ChatMessageRoleSystem, Content: buildSystemPrompt(language, references)},
+ {Role: openai.ChatMessageRoleSystem, Content: buildSystemPrompt(language, references, editorCtx)},
{Role: openai.ChatMessageRoleUser, Content: userMessage},
}
}
@@ -843,9 +876,9 @@ func loadCheckpoint(sessionID string) *agentCheckpoint {
return &cp
}
-func checkpointMessagesToOpenAI(checkpointMsgs []AgentMessage, language string, references []Reference) []openai.ChatCompletionMessage {
+func checkpointMessagesToOpenAI(checkpointMsgs []AgentMessage, language string, references []Reference, editorCtx EditorContext) []openai.ChatCompletionMessage {
msgs := []openai.ChatCompletionMessage{
- {Role: openai.ChatMessageRoleSystem, Content: buildSystemPrompt(language, references)},
+ {Role: openai.ChatMessageRoleSystem, Content: buildSystemPrompt(language, references, editorCtx)},
}
for cmi := range checkpointMsgs {
diff --git a/kernel/api/agent.go b/kernel/api/agent.go
index dcb1916c01..47aafb265c 100644
--- a/kernel/api/agent.go
+++ b/kernel/api/agent.go
@@ -34,12 +34,13 @@ import (
)
type agentChatReq struct {
- SessionID string `json:"sessionID"`
- Message string `json:"message"`
- Language string `json:"language"`
- References []agent.Reference `json:"references"`
- Model string `json:"model,omitempty"`
- Regenerate bool `json:"regenerate"`
+ SessionID string `json:"sessionID"`
+ Message string `json:"message"`
+ Language string `json:"language"`
+ References []agent.Reference `json:"references"`
+ EditorContext agent.EditorContext `json:"editorContext"`
+ Model string `json:"model,omitempty"`
+ Regenerate bool `json:"regenerate"`
}
type runningSession struct {
@@ -96,7 +97,7 @@ func agentChat(c *gin.Context) {
ctx, cancel := context.WithCancel(c.Request.Context())
defer cancel()
- eventCh = agent.AgentChat(ctx, client, selectedModel.Name, req.SessionID, req.Message, req.Language, req.References, req.Regenerate, confirmTimeout, maxRetries)
+ eventCh = agent.AgentChat(ctx, client, selectedModel.Name, req.SessionID, req.Message, req.Language, req.References, req.EditorContext, req.Regenerate, confirmTimeout, maxRetries)
sessionsMu.Lock()
runningSessions[req.SessionID] = &runningSession{eventCh: eventCh}
sessionsMu.Unlock()