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: Hide the Agent scroll button when scrolling is unavailable https://github.com/siyuan-note/siyuan/issues/18768
Daniel1f728d523db1
diff --git a/app/src/layout/dock/agent/AgentChat.ts b/app/src/layout/dock/agent/AgentChat.tsindex e3d5b26d41..895b9cbebb 100644--- a/app/src/layout/dock/agent/AgentChat.ts+++ b/app/src/layout/dock/agent/AgentChat.ts@@ -45,6 +45,7 @@ import {AI_CONFIG_CHANGED_EVENT} from "../../../config/tabs/ai/aiRuntime";import {isEncryptedBox} from "../../../util/pathName";import {Menu} from "../../../plugin/Menu";import {getAgentDefaultModelID, getUsableAgentModels} from "./agentModel";+import {AgentScrollStateMode, resolveAgentScrollState} from "./AgentScrollState";// 限制注入用户轮次上下文的可见块 ID 数量,以控制 token 开销。// 与 kernel/agent/agent.go 中的 maxVisibleBlockIDs 保持一致。@@ -204,6 +205,7 @@ export class AgentChat extends Model {private permissionMode: AgentPermissionMode = "confirm";private userScrolledUp = false;private programmaticScroll = false;+ private programmaticScrollGeneration = 0;private stickResizeObserver: ResizeObserver | null = null;// 按会话保存的距底部距离(scrollHeight - scrollTop),用于切换会话与开关 dock 面板后恢复滚动位置。// 用距底距离而非绝对 scrollTop:dock 展开/折叠有宽高过渡,期间 scrollHeight 变化,@@ -367,17 +369,7 @@ export class AgentChat extends Model {if (this.programmaticScroll) {return;}- 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.syncScrollState("user");this.updateActiveMarker();}, {passive: true});@@ -421,9 +413,9 @@ export class AgentChat extends Model {!!this.host.mobile,);// 监听滚动容器尺寸:dock 面板折叠时容器尺寸归零、浏览器把 scrollTop 钳制到 0;- // 这里在面板重新展开后恢复当前会话的滚动位置。dock 展开/折叠有 CSS 宽高过渡(约 0.2s),- // 过渡期间 scrollHeight 随尺寸变化,故在折叠→展开转换后用 rAF 循环持续校正约 320ms,- // 覆盖过渡动画直到布局稳定。+ // 面板重新展开后恢复当前会话的滚动位置,普通尺寸变化则重新校验按钮状态。+ // dock 展开/折叠有 CSS 宽高过渡(约 0.2s),过渡期间 scrollHeight 随尺寸变化,+ // 故在折叠→展开转换后用 rAF 循环持续校正约 320ms,覆盖过渡动画直到布局稳定。this.layoutResizeObserver = new ResizeObserver(() => {const collapsed = this.messagesContainer.clientWidth === 0 || this.messagesContainer.clientHeight === 0;if (collapsed) {@@ -435,6 +427,10 @@ export class AgentChat extends Model {this.layoutVisible = true;const saved = this.scrollBottomBySession.get(this.sessionId) ?? 0;this.restoreScrollToBottom(saved);+ return;+ }+ if (!this.programmaticScroll) {+ this.syncScrollState("reconcile");}});this.layoutResizeObserver.observe(this.messagesContainer);@@ -893,9 +889,6 @@ export class AgentChat extends Model {}});this.scrollBottomBtn.addEventListener("click", () => {- // 程序化滚动期间会忽略 scroll 事件,点击时需主动退出“用户已向上滚动”状态并隐藏按钮。- this.userScrolledUp = false;- this.scrollBottomBtn.classList.remove("agent-chat__scroll-bottom--visible");this.scrollToBottom(true, true);});}@@ -1050,6 +1043,7 @@ export class AgentChat extends Model {if (this.mirrorPlaceholderEl) {this.mirrorPlaceholderEl.remove();this.mirrorPlaceholderEl = null;+ this.syncScrollState("reconcile");}}@@ -1100,7 +1094,7 @@ export class AgentChat extends Model {if (atBottom) {this.scrollToBottom(true);} else {- this.messagesContainer.scrollTop = savedScroll;+ this.restoreScrollTop(savedScroll);}// 重绘会清空 DOM(含占位条);若仍处于其他实例的流式中,重新显示占位条。if (this.mirrorLocked) {@@ -2671,7 +2665,7 @@ export class AgentChat extends Model {bodyHTML +"</div>";- bindThinkingCardToggle(el);+ bindThinkingCardToggle(el, () => this.syncScrollState("reconcile"));this.insertBeforeAI(el);this.scrollToBottom();this.observeStickTarget(el);@@ -3017,7 +3011,7 @@ export class AgentChat extends Model {if (atBottom) {this.scrollToBottom(true);} else {- this.messagesContainer.scrollTop = savedScroll;+ this.restoreScrollTop(savedScroll);}} else {await this.reloadFromDisk(true);@@ -3608,7 +3602,7 @@ export class AgentChat extends Model {reasoningContent: string}) {const el = createThinkingCardElement(step);- bindThinkingCardToggle(el);+ bindThinkingCardToggle(el, () => this.syncScrollState("reconcile"));this.messagesContainer.appendChild(el);}@@ -3659,7 +3653,7 @@ export class AgentChat extends Model {"</div>" +"</div>";- bindThinkingCardToggle(el);+ bindThinkingCardToggle(el, () => this.syncScrollState("reconcile"));this.messagesContainer.appendChild(el);postRender(el, this.app, this.host.onNavigate);}@@ -3872,6 +3866,9 @@ export class AgentChat extends Model {for (let i = 0; i < items.length; i++) {items[i].remove();}+ if (items.length > 0) {+ this.syncScrollState("reconcile");+ }}// 启动思考计时器,每 100ms 刷新所有未完成思考卡片的标题文本为「思考中... X.Xs」。@@ -3970,6 +3967,40 @@ export class AgentChat extends Model {return this.composer.getSendData().text.length > 0;}+ private syncScrollState(mode: AgentScrollStateMode) {+ const {scrollTop, scrollHeight, clientHeight} = this.messagesContainer;+ // dock 折叠期间无法得到有效视口尺寸,保留当前会话的用户滚动意图,待恢复完成后重新派生。+ if (!this.layoutVisible || clientHeight <= 0) {+ this.scrollBottomBtn.classList.remove("agent-chat__scroll-bottom--visible");+ return;+ }+ const state = resolveAgentScrollState({scrollTop, scrollHeight, clientHeight}, this.userScrolledUp, mode);+ this.userScrolledUp = state.userScrolledUp;+ this.scrollBottomBtn.classList.toggle("agent-chat__scroll-bottom--visible", state.buttonVisible);+ }++ private beginProgrammaticScroll(): number {+ this.programmaticScroll = true;+ this.programmaticScrollGeneration++;+ return this.programmaticScrollGeneration;+ }++ private finishProgrammaticScroll(generation: number, mode: AgentScrollStateMode = "reconcile") {+ if (generation !== this.programmaticScrollGeneration) {+ return;+ }+ this.programmaticScroll = false;+ this.syncScrollState(mode);+ }++ private restoreScrollTop(scrollTop: number) {+ const generation = this.beginProgrammaticScroll();+ this.messagesContainer.scrollTop = scrollTop;+ requestAnimationFrame(() => {+ this.finishProgrammaticScroll(generation, "restore");+ });+ }+// 持续校正滚动位置约 duration ms(覆盖 dock 宽高过渡 / 异步富渲染期间 scrollHeight 变化),// 使 scrollTop 落到距底部 scrollBottom 的位置。scrollBottom 为 0 即贴底。// 供开关面板(layoutVisible 恢复)与切换会话(renderLoadedSession 后)共用。@@ -3978,11 +4009,13 @@ export class AgentChat extends Model {return;}const startedAt = Date.now();- // 标记为程序化滚动,避免恢复期间触发 scroll 事件里的 userScrolledUp 翻转。- this.programmaticScroll = true;+ const generation = this.beginProgrammaticScroll();const tick = () => {+ if (generation !== this.programmaticScrollGeneration) {+ return;+ }if (!this.layoutVisible) {- this.programmaticScroll = false;+ this.finishProgrammaticScroll(generation);return;}const {scrollHeight} = this.messagesContainer;@@ -3994,7 +4027,7 @@ export class AgentChat extends Model {} else {// 多留一帧再清标志,确保最后一次 scroll 事件已被吞掉。requestAnimationFrame(() => {- this.programmaticScroll = false;+ this.finishProgrammaticScroll(generation, "restore");});}};@@ -4015,10 +4048,10 @@ export class AgentChat extends Model {// 卡片底部在文档中的位置 - 容器顶部在文档中的位置 + 当前已滚动量 = 卡片底部的 scrollTop 目标值。const target = this.messagesContainer.scrollTop + (cardRect.bottom - containerRect.top) + 8;const max = this.messagesContainer.scrollHeight - this.messagesContainer.clientHeight;- this.programmaticScroll = true;+ const generation = this.beginProgrammaticScroll();this.messagesContainer.scrollTop = Math.min(target, max);requestAnimationFrame(() => {- this.programmaticScroll = false;+ this.finishProgrammaticScroll(generation);});};if (delay > 0) {@@ -4031,48 +4064,47 @@ export class AgentChat extends Model {private scrollToBottom(force = false, smooth = 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;+ }+ if (force) {+ this.userScrolledUp = false;+ this.syncScrollState("reconcile");+ }+ // 递增序号保证只有最新滚动请求能够写入位置和解除保护,避免跨帧调用相互覆盖。+ const generation = this.beginProgrammaticScroll();requestAnimationFrame(() => {+ if (generation !== this.programmaticScrollGeneration) {+ return;+ }if (smooth) {- // Smooth scrolling fires scroll events asynchronously throughout the- // animation, so keep the guard raised until scrolling settles: on- // scrollend, on a 1s timeout fallback, or immediately if the user- // wheels/touches during the animation (counts as a user scroll).+ // 平滑滚动会持续异步触发 scroll,直至 scrollend、超时或用户滚轮操作后才解除保护。+ let timer = 0;const finish = () => {this.messagesContainer.removeEventListener("scrollend", finish);this.messagesContainer.removeEventListener("wheel", onWheel);clearTimeout(timer);- this.programmaticScroll = false;+ this.finishProgrammaticScroll(generation);};const onWheel = () => {this.messagesContainer.removeEventListener("scrollend", finish);clearTimeout(timer);- this.programmaticScroll = false;+ this.finishProgrammaticScroll(generation);};- const timer = window.setTimeout(finish, 1000);+ timer = window.setTimeout(finish, 1000);this.messagesContainer.addEventListener("scrollend", finish, {once: true});this.messagesContainer.addEventListener("wheel", onWheel, {once: true, passive: true});this.messagesContainer.scrollTo({top: this.messagesContainer.scrollHeight, behavior: "smooth"});} else {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).+ // 多留一帧等待本次写入产生的 scroll 事件分发完毕。requestAnimationFrame(() => {- this.programmaticScroll = false;+ this.finishProgrammaticScroll(generation);});}});}- // 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.+ // 观察流式消息卡片,让代码高亮、图片、图表和字体等异步内容增长也能在用户未向上滚动时保持贴底。+ // token 帧只会在新数据块到达时触发,此观察器用于覆盖两个数据块之间发生的尺寸变化。private observeStickTarget(el: HTMLElement | null) {if (this.stickResizeObserver) {this.stickResizeObserver.disconnect();@@ -4082,6 +4114,7 @@ export class AgentChat extends Model {return;}this.stickResizeObserver = new ResizeObserver(() => {+ this.syncScrollState("reconcile");if (!this.userScrolledUp) {this.scrollToBottom();}diff --git a/app/src/layout/dock/agent/AgentMessageRenderer.ts b/app/src/layout/dock/agent/AgentMessageRenderer.tsindex 5657b46ff9..c0a23bf18e 100644--- a/app/src/layout/dock/agent/AgentMessageRenderer.ts+++ b/app/src/layout/dock/agent/AgentMessageRenderer.ts@@ -179,7 +179,7 @@ export const createThinkingCardElement = (step: {return el;};-export const bindThinkingCardToggle = (el: HTMLElement): void => {+export const bindThinkingCardToggle = (el: HTMLElement, onLayoutChange?: () => void): void => {const header = el.querySelector(".agent-chat__thinking-header") as HTMLElement;const body = el.querySelector(".agent-chat__thinking-body") as HTMLElement;const expandIcon = el.querySelector(".agent-chat__thinking-arrow--expand") as HTMLElement;@@ -188,6 +188,13 @@ export const bindThinkingCardToggle = (el: HTMLElement): void => {if (!header || !body || !expandIcon || !contractIcon) {return;}+ if (onLayoutChange) {+ body.addEventListener("transitionend", (event) => {+ if (event.target === body && event.propertyName === "max-height") {+ onLayoutChange();+ }+ });+ }header.addEventListener("click", () => {el.setAttribute("data-user-interacted", "true");const isExpanded = body.classList.contains("agent-chat__thinking-body--expanded");@@ -226,6 +233,7 @@ export const bindThinkingCardToggle = (el: HTMLElement): void => {latestElement?.classList.add("fn__none");}}+ onLayoutChange?.();});};diff --git a/app/src/layout/dock/agent/AgentScrollState.test.ts b/app/src/layout/dock/agent/AgentScrollState.test.tsnew file mode 100644index 0000000000..855fadb4d6--- /dev/null+++ b/app/src/layout/dock/agent/AgentScrollState.test.ts@@ -0,0 +1,78 @@+import {describe, it} from "node:test";+import * as assert from "node:assert/strict";+import {resolveAgentScrollState} from "./AgentScrollState";++describe("AgentScrollState", () => {+ it("clears stale state when the content cannot scroll", () => {+ assert.deepEqual(resolveAgentScrollState({+ scrollTop: 0,+ scrollHeight: 600,+ clientHeight: 600,+ }, true, "reconcile"), {+ userScrolledUp: false,+ buttonVisible: false,+ });+ });++ it("enters and leaves the user-scrolled state with hysteresis", () => {+ const scrolledUp = resolveAgentScrollState({+ scrollTop: 340,+ scrollHeight: 1000,+ clientHeight: 600,+ }, false, "user");+ assert.deepEqual(scrolledUp, {+ userScrolledUp: true,+ buttonVisible: true,+ });++ const withinHysteresis = resolveAgentScrollState({+ scrollTop: 370,+ scrollHeight: 1000,+ clientHeight: 600,+ }, true, "user");+ assert.deepEqual(withinHysteresis, {+ userScrolledUp: true,+ buttonVisible: true,+ });++ const returnedToBottom = resolveAgentScrollState({+ scrollTop: 390,+ scrollHeight: 1000,+ clientHeight: 600,+ }, true, "user");+ assert.deepEqual(returnedToBottom, {+ userScrolledUp: false,+ buttonVisible: false,+ });+ });++ it("does not interpret content growth as a user scroll", () => {+ assert.deepEqual(resolveAgentScrollState({+ scrollTop: 400,+ scrollHeight: 1200,+ clientHeight: 600,+ }, false, "reconcile"), {+ userScrolledUp: false,+ buttonVisible: false,+ });+ });++ it("derives the state from a restored session position", () => {+ assert.deepEqual(resolveAgentScrollState({+ scrollTop: 200,+ scrollHeight: 1000,+ clientHeight: 600,+ }, false, "restore"), {+ userScrolledUp: true,+ buttonVisible: true,+ });+ assert.deepEqual(resolveAgentScrollState({+ scrollTop: 350,+ scrollHeight: 1000,+ clientHeight: 600,+ }, true, "restore"), {+ userScrolledUp: false,+ buttonVisible: false,+ });+ });+});diff --git a/app/src/layout/dock/agent/AgentScrollState.ts b/app/src/layout/dock/agent/AgentScrollState.tsnew file mode 100644index 0000000000..d45982b6b9--- /dev/null+++ b/app/src/layout/dock/agent/AgentScrollState.ts@@ -0,0 +1,41 @@+export type AgentScrollStateMode = "user" | "reconcile" | "restore";++export interface AgentScrollMetrics {+ scrollTop: number;+ scrollHeight: number;+ clientHeight: number;+}++export interface AgentScrollState {+ userScrolledUp: boolean;+ buttonVisible: boolean;+}++const scrollableThreshold = 1;+const leaveBottomThreshold = 60;+const returnBottomThreshold = 10;++export const resolveAgentScrollState = (+ metrics: AgentScrollMetrics,+ currentUserScrolledUp: boolean,+ mode: AgentScrollStateMode,+): AgentScrollState => {+ const maxScrollTop = Math.max(0, metrics.scrollHeight - metrics.clientHeight);+ const scrollTop = Math.min(maxScrollTop, Math.max(0, metrics.scrollTop));+ const distanceFromBottom = maxScrollTop - scrollTop;+ const scrollable = maxScrollTop > scrollableThreshold;+ let userScrolledUp = currentUserScrolledUp;++ if (!scrollable || distanceFromBottom <= returnBottomThreshold) {+ userScrolledUp = false;+ } else if (mode === "restore") {+ userScrolledUp = distanceFromBottom >= leaveBottomThreshold;+ } else if (mode === "user" && !userScrolledUp && distanceFromBottom >= leaveBottomThreshold) {+ userScrolledUp = true;+ }++ return {+ userScrolledUp,+ buttonVisible: userScrolledUp && scrollable && distanceFromBottom > returnBottomThreshold,+ };+};