Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Avoid accidental agent navigation jumps https://github.com/siyuan-note/siyuan/issues/21024

Danielb8bb0cadfb82
diff --git a/app/src/assets/scss/business/_ai_agent.scss b/app/src/assets/scss/business/_ai_agent.scss
index af1c78fd45..8d57875c2d 100644
--- a/app/src/assets/scss/business/_ai_agent.scss
+++ b/app/src/assets/scss/business/_ai_agent.scss
@@ -1132,39 +1132,37 @@
.agent-chat__nav-rail {
position: absolute;
- right: 4px;
+ right: 12px;
top: 8px;
+ box-sizing: border-box;
+ width: 24px;
+ max-height: calc(100% - 56px);
+ overflow-y: auto;
+ scrollbar-width: none;
display: flex;
flex-direction: column;
align-items: flex-end;
gap: var(--nav-gap, 3px);
z-index: 6;
- border: 1px solid transparent;
- border-radius: var(--b3-border-radius-b);
- padding: 6px 4px 6px 12px;
- transition: background 200ms ease-out, box-shadow 200ms ease-out, border-color 200ms ease-out, gap 200ms ease-out;
+ padding: 4px 2px;
}
.agent-chat__nav-rail-marker {
box-sizing: content-box;
width: 10px;
height: 2px;
- padding-left: 10px;
+ flex-shrink: 0;
+ padding: 3px 5px;
background-clip: content-box;
background-color: var(--b3-theme-on-surface);
opacity: 0.3;
cursor: pointer;
- overflow: hidden;
- white-space: nowrap;
- text-overflow: ellipsis;
- font-size: 12px;
- line-height: 20px;
- color: transparent;
border-radius: var(--b3-border-radius-b);
- transition: width 200ms ease-out, height 200ms ease-out,
- opacity 200ms ease-out, padding 200ms ease-out,
- background 200ms ease-out, color 200ms ease-out,
- line-height 200ms ease-out, border-radius 200ms ease-out;
+ transition: opacity 150ms ease-out;
+
+ &:hover {
+ opacity: 1;
+ }
}
.agent-chat__nav-rail-marker--active {
@@ -1172,36 +1170,10 @@
opacity: 0.7;
}
-.agent-chat__nav-rail--expanded {
- background: var(--b3-menu-background);
- box-shadow: var(--b3-dialog-shadow);
- border: 1px solid var(--b3-theme-surface-lighter);
- gap: 0;
- padding: 8px;
+.tooltip--agent-nav {
+ pointer-events: none;
+ max-height: min(160px, 90vh);
overflow: hidden;
-
- .agent-chat__nav-rail-marker {
- width: 160px;
- height: 28px;
- line-height: 28px;
- opacity: 1;
- padding: 0 8px;
- margin: 1px 0;
- background: transparent;
- background-clip: padding-box;
- color: var(--b3-theme-on-background);
- border-radius: var(--b3-border-radius);
- transition: none;
-
- &:hover {
- background: var(--b3-list-hover);
- color: var(--b3-theme-on-background);
- }
- }
-
- .agent-chat__nav-rail-marker--active {
- background: var(--b3-list-hover);
- }
}
.agent-chat__msg--jumped .agent-chat__body {
diff --git a/app/src/layout/dock/agent/AgentChat.navigation.test.ts b/app/src/layout/dock/agent/AgentChat.navigation.test.ts
new file mode 100644
index 0000000000..8a369c232d
--- /dev/null
+++ b/app/src/layout/dock/agent/AgentChat.navigation.test.ts
@@ -0,0 +1,190 @@
+import * as assert from "node:assert/strict";
+import {execFile} from "node:child_process";
+import {mkdtempSync, readFileSync, rmSync, writeFileSync} from "node:fs";
+import {tmpdir} from "node:os";
+import * as path from "node:path";
+import {test} from "node:test";
+import {promisify} from "node:util";
+import {compile, Logger} from "sass";
+import {createSourceFile, forEachChild, isMethodDeclaration, ModuleKind, ScriptTarget, transpileModule} from "typescript";
+
+const browserCases = async (sources: Record<string, string>, css: string, mobile: boolean) => {
+ const check: typeof assert = require("node:assert/strict");
+ document.head.innerHTML = `<style>${css}</style>`;
+ document.body.innerHTML = '<div class="sy__agentChat' + (mobile ? " sy__agentChat--mobile" : "") +
+ '" style="position:absolute;left:20px;top:20px;width:360px;height:400px">' +
+ '<div class="agent-chat__messages-wrap" style="height:100%">' +
+ '<div class="agent-chat__messages"><button id="copy" style="position:absolute">Copy</button></div>' +
+ '<span class="agent-chat__scroll-bottom agent-chat__scroll-bottom--visible"></span></div></div>' +
+ '<div id="tooltip" class="fn__none"></div>';
+ window.DOMPurify = {sanitize: (value: string) => value} as typeof window.DOMPurify;
+ const input = {} as typeof import("../../../util/hoverInput");
+ new Function("exports", "require", sources.hoverInput)(input, () => ({isInIOS: () => false}));
+ const tooltip = {} as typeof import("../../../dialog/tooltip");
+ new Function("exports", "require", sources.tooltip)(tooltip, (id: string) => {
+ if (id === "../util/hoverInput") return input;
+ return {isMobile: () => false, emitToPlugins() {}, forEachPluginSubscriber() {}};
+ });
+ const escape = {} as typeof import("../../../util/escape");
+ new Function("exports", sources.escape)(escape);
+ tooltip.initTooltips();
+ const Harness = new Function("showTooltip", "hideTooltip", "escapeHtmlTextAndAttr", sources.chat)(
+ tooltip.showTooltip, tooltip.hideTooltip, escape.escapeHtmlTextAndAttr);
+ const chat = new Harness() as {
+ navRail: HTMLElement;
+ entries: Array<{type: string, id: string, content: string}>;
+ messagesContainer: HTMLElement;
+ initNavRail: (wrap: HTMLElement) => void;
+ rebuildNavMarkers: () => void;
+ updateActiveMarker: () => void;
+ };
+ const panel = document.querySelector<HTMLElement>(".sy__agentChat");
+ const wrap = document.querySelector<HTMLElement>(".agent-chat__messages-wrap");
+ chat.messagesContainer = document.querySelector(".agent-chat__messages");
+ chat.initNavRail(wrap);
+ chat.entries = [
+ {type: "user", id: "first", content: 'First <img src=x> & "quoted" %2F'},
+ {type: "assistant", id: "answer", content: "Answer"},
+ {type: "user", id: "second", content: "Second question"},
+ ];
+ for (const id of ["first", "second"]) {
+ const message = document.createElement("div");
+ message.className = "agent-chat__msg agent-chat__msg--user";
+ message.dataset.messageId = id;
+ message.style.cssText = "min-height:600px;flex-shrink:0";
+ chat.messagesContainer.append(message);
+ }
+ chat.rebuildNavMarkers();
+ const rail = chat.navRail;
+ const markers = rail.querySelectorAll<HTMLElement>(".agent-chat__nav-rail-marker");
+ check.equal(markers.length, 2, "only user messages appear in navigation");
+ if (mobile) {
+ check.equal(getComputedStyle(rail).display, "none", "mobile keeps its existing hidden navigation");
+ return "Navigation cases passed";
+ }
+ const tick = () => new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)));
+ const move = async (x: number, y: number) => {
+ await require("electron").ipcRenderer.invoke("navigation-input", "mouseMoved", x, y);
+ await tick();
+ };
+ const click = async (x: number, y: number) => {
+ await move(x, y);
+ for (const type of ["mousePressed", "mouseReleased"]) {
+ await require("electron").ipcRenderer.invoke("navigation-input", type, x, y);
+ }
+ await tick();
+ };
+ const tip = document.getElementById("tooltip");
+ const initialWidth = rail.getBoundingClientRect().width;
+ const first = markers[0].getBoundingClientRect();
+ check.ok(first.height >= 8, "markers have a usable click height");
+ await move(first.left + first.width / 2, first.top + first.height / 2);
+ await new Promise(resolve => setTimeout(resolve, 500));
+ check.equal(rail.getBoundingClientRect().width, initialWidth, "hover never expands the clickable rail");
+ check.ok(initialWidth <= 24);
+ check.equal(chat.messagesContainer.scrollTop, 0, "hover never jumps");
+ check.equal(tip.classList.contains("fn__none"), false);
+ check.equal(tip.textContent, chat.entries[0].content, "preview preserves literal HTML, quotes and percent sequences");
+ check.equal(tip.children.length, 0, "message content cannot create interactive preview elements");
+ check.equal(getComputedStyle(tip).pointerEvents, "none", "preview cannot intercept clicks");
+ const preview = tip.getBoundingClientRect();
+ const copy = document.getElementById("copy");
+ copy.style.left = `${preview.left - wrap.getBoundingClientRect().left + 5}px`;
+ copy.style.top = `${preview.top - wrap.getBoundingClientRect().top + 5}px`;
+ let copied = 0;
+ copy.addEventListener("click", () => copied++);
+ const copyRect = copy.getBoundingClientRect();
+ check.ok(copyRect.left < first.left, "copy target is outside the narrow rail");
+ check.equal(document.elementFromPoint(copyRect.left + 5, copyRect.top + 5), copy,
+ "the copy control remains the hit target beneath the visible preview");
+ await click(copyRect.left + 5, copyRect.top + 5);
+ check.equal(copied, 1, "copying beside navigation still works");
+ check.equal(chat.messagesContainer.scrollTop, 0, "copying never jumps");
+ const second = markers[1].getBoundingClientRect();
+ await move(second.left + 10, second.top + 4);
+ check.equal(tip.textContent, "Second question", "hover updates only the selected message preview");
+ await click(second.left + 10, second.top + 4);
+ await new Promise(resolve => setTimeout(resolve, 800));
+ check.ok(chat.messagesContainer.scrollTop > 0, "clicking a marker scrolls to its message");
+ check.ok(chat.messagesContainer.querySelector('[data-message-id="second"]').classList.contains("agent-chat__msg--jumped"));
+ chat.updateActiveMarker();
+ check.equal(markers[1].classList.contains("agent-chat__nav-rail-marker--active"), true);
+ chat.entries = Array.from({length: 150}, (_, i) => ({type: "user", id: `message-${i}`, content: `Question ${i}`}));
+ chat.rebuildNavMarkers();
+ for (const width of [240, 640]) {
+ panel.style.width = `${width}px`;
+ await tick();
+ check.ok(rail.scrollHeight > rail.clientHeight, "long navigation remains scrollable");
+ const bounds = rail.getBoundingClientRect();
+ const bottom = panel.querySelector(".agent-chat__scroll-bottom").getBoundingClientRect();
+ check.ok(bounds.bottom < bottom.top, "navigation leaves the return-to-bottom control clear");
+ check.ok(bounds.right <= wrap.getBoundingClientRect().right - 10, "navigation leaves the message scrollbar clear");
+ rail.scrollTop = rail.scrollHeight;
+ await tick();
+ const last = rail.lastElementChild.getBoundingClientRect();
+ check.ok(last.top >= bounds.top && last.bottom <= bounds.bottom, "the last item remains reachable");
+ }
+ chat.entries = [];
+ chat.rebuildNavMarkers();
+ check.equal(rail.children.length, 0, "switching to an empty conversation removes stale markers");
+ return "Navigation cases passed";
+};
+
+test("agent navigation keeps previews outside its click targets on desktop and stays hidden on mobile", {
+ skip: process.platform === "linux" && !process.env.DISPLAY && !process.env.WAYLAND_DISPLAY,
+ timeout: 60000,
+}, async () => {
+ const source = createSourceFile("AgentChat.ts", readFileSync(path.join(__dirname, "AgentChat.ts"), "utf8"),
+ ScriptTarget.Latest, true);
+ const methods: string[] = [];
+ const names = new Set(["initNavRail", "rebuildNavMarkers", "updateActiveMarker", "jumpToMessage"]);
+ const visit = (node: import("typescript").Node) => {
+ if (isMethodDeclaration(node) && names.has(node.name.getText(source))) methods.push(node.getText(source));
+ forEachChild(node, visit);
+ };
+ visit(source);
+ assert.equal(methods.length, names.size);
+ const transpile = (text: string) => transpileModule(text, {
+ compilerOptions: {module: ModuleKind.CommonJS, target: ScriptTarget.ES2021},
+ }).outputText;
+ const sources: Record<string, string> = {chat: transpile(`class Harness {${methods.join("\n")}}\nreturn Harness;`)};
+ for (const [name, file] of Object.entries({hoverInput: "util/hoverInput.ts", tooltip: "dialog/tooltip.ts", escape: "util/escape.ts"})) {
+ sources[name] = transpile(readFileSync(path.resolve(__dirname, "../../..", file), "utf8"));
+ }
+ const sheets = ["base", "mobile"].map(name => ({name, css: compile(path.resolve(__dirname,
+ `../../../assets/scss/${name}.scss`), {logger: Logger.silent}).css}));
+ const temporary = mkdtempSync(path.join(tmpdir(), "siyuan-agent-navigation-"));
+ const script = path.join(temporary, "run.cjs");
+ writeFileSync(script, `const {app, BrowserWindow, ipcMain} = require("electron");
+app.setPath("userData", ${JSON.stringify(path.join(temporary, "profile"))});
+app.whenReady().then(async () => {
+ const win = new BrowserWindow({show:false,width:900,height:700,
+ webPreferences:{nodeIntegration:true,contextIsolation:false,offscreen:true,backgroundThrottling:false}});
+ win.webContents.debugger.attach("1.3");
+ ipcMain.handle("navigation-input", (_event,type,x,y) => win.webContents.debugger.sendCommand("Input.dispatchMouseEvent",
+ {type,x,y,button:type === "mouseMoved" ? "none" : "left",clickCount:1}));
+ try {
+ for (const sheet of ${JSON.stringify(sheets)}) {
+ for (const theme of ["daylight", "midnight"]) {
+ await win.loadURL("data:text/html,<html><body></body></html>");
+ const css = require("node:fs").readFileSync(${JSON.stringify(path.resolve(__dirname, "../../../../appearance/themes"))} +
+ "/" + theme + "/theme.css", "utf8") + sheet.css;
+ console.log(await win.webContents.executeJavaScript("const __name = value => value; (" +
+ ${JSON.stringify(browserCases.toString())} + ")(" + JSON.stringify(${JSON.stringify(sources)}) + "," +
+ JSON.stringify(css) + "," + (sheet.name === "mobile") + ")"));
+ }
+ }
+ win.destroy();app.exit(0);
+ } catch(error) {console.error(error);win.destroy();app.exit(1);}
+});`, "utf8");
+ const env = {...process.env};
+ delete env.ELECTRON_RUN_AS_NODE;
+ try {
+ const result = await promisify(execFile)(require("electron"), [script], {env, windowsHide: true, timeout: 50000});
+ assert.equal(result.stdout.match(/Navigation cases passed/g)?.length, 4);
+ } finally {
+ assert.equal(path.dirname(path.resolve(temporary)), path.resolve(tmpdir()));
+ assert.ok(path.basename(temporary).startsWith("siyuan-agent-navigation-"));
+ rmSync(temporary, {recursive: true, force: true, maxRetries: 10, retryDelay: 100});
+ }
+});
diff --git a/app/src/layout/dock/agent/AgentChat.ts b/app/src/layout/dock/agent/AgentChat.ts
index d3148ce88e..eb2c88a73f 100644
--- a/app/src/layout/dock/agent/AgentChat.ts
+++ b/app/src/layout/dock/agent/AgentChat.ts
@@ -14,7 +14,8 @@ import {genAgentConfirmActionButtons} from "./agentConfirmActions";
import {AgentSessionPanel} from "./AgentSessionPanel";
import {updateHotkeyAfterTip} from "../../../protyle/util/compatibility";
import {getAgentLute} from "../../../protyle/render/setLute";
-import {escapeAriaLabel, escapeHtml} from "../../../util/escape";
+import {escapeAriaLabel, escapeHtml, escapeHtmlTextAndAttr} from "../../../util/escape";
+import {hideTooltip, showTooltip} from "../../../dialog/tooltip";
import {setPosition} from "../../../util/setPosition";
import {fetchPost} from "../../../util/fetch";
import {confirmDialog} from "../../../dialog/confirmDialog";
@@ -291,7 +292,6 @@ export class AgentChat extends Model {
private settingDialogObserver: MutationObserver | null = null;
private scrollBottomBtn: HTMLElement;
private navRail: HTMLElement;
- private navExpandTimer = 0;
// 镜像态:当前会话正由其他实例流式对话,本实例处于只读占位锁定,期间不重绘当前视图。
// 由 ws 的 streamStart/streamEnd 事件驱动,与发起者的 isStreaming 互斥(发起者走 SSE)。
private mirrorLocked = false;
@@ -925,15 +925,17 @@ export class AgentChat extends Model {
this.navRail = document.createElement("div");
this.navRail.className = "agent-chat__nav-rail";
- this.navRail.addEventListener("mouseenter", () => {
- this.navExpandTimer = window.setTimeout(() => {
- this.navRail.classList.add("agent-chat__nav-rail--expanded");
- }, 200);
- });
- this.navRail.addEventListener("mouseleave", () => {
- clearTimeout(this.navExpandTimer);
- this.navRail.classList.remove("agent-chat__nav-rail--expanded");
+ this.navRail.addEventListener("mouseover", (event: MouseEvent) => {
+ const marker = (event.target as HTMLElement).closest(".agent-chat__nav-rail-marker");
+ if (!marker) {
+ return;
+ }
+ // 单条消息预览复用提示框,不扩大导航的点击区域。
+ event.stopPropagation();
+ showTooltip(escapeHtmlTextAndAttr(marker.getAttribute("aria-label") || ""), marker, "agent-nav", event);
});
+ this.navRail.addEventListener("mouseleave", hideTooltip);
+ this.navRail.addEventListener("scroll", hideTooltip, {passive: true});
this.navRail.addEventListener("click", (e: MouseEvent) => {
const marker = (e.target as HTMLElement).closest(".agent-chat__nav-rail-marker") as HTMLElement;
if (!marker) {
@@ -946,6 +948,9 @@ export class AgentChat extends Model {
}
private rebuildNavMarkers() {
+ if (this.navRail.childElementCount > 0 && this.navRail.matches(":hover")) {
+ hideTooltip();
+ }
this.navRail.innerHTML = "";
const userEntries = this.entries.filter((entry): entry is UserEntry => entry.type === "user");
if (userEntries.length === 0) {
@@ -957,11 +962,10 @@ export class AgentChat extends Model {
for (const entry of userEntries) {
const marker = document.createElement("div");
- marker.className = "agent-chat__nav-rail-marker ariaLabel";
+ marker.className = "agent-chat__nav-rail-marker";
marker.dataset.messageId = entry.id || "";
marker.setAttribute("data-position", "west");
- marker.setAttribute("aria-label", escapeAriaLabel(escapeHtml(entry.content)));
- marker.textContent = entry.content.slice(0, 120);
+ marker.setAttribute("aria-label", entry.content);
this.navRail.appendChild(marker);
}
this.updateActiveMarker();