An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:art: https://github.com/siyuan-note/siyuan/issues/17797
Vanessaf03e2608981a
diff --git a/app/src/assets/scss/business/_ai_agent.scss b/app/src/assets/scss/business/_ai_agent.scssindex 3844d9b78a..fb8abebdb7 100644--- a/app/src/assets/scss/business/_ai_agent.scss+++ b/app/src/assets/scss/business/_ai_agent.scss@@ -669,71 +669,14 @@}.agent-session-popup {- position: fixed;width: 280px;max-height: 360px;display: flex;flex-direction: column;- overflow: hidden;-- .b3-menu__items {- flex: 1;- min-height: 0;- }-- .b3-menu__item:hover {- background: var(--b3-list-hover);- }-- .b3-menu__label {- overflow: hidden;- text-overflow: ellipsis;- white-space: nowrap;- }}.agent-session-popup__search {- width: calc(100% - 16px);margin: 0 8px 4px;- box-sizing: border-box;- flex-shrink: 0;-}--.agent-session-popup__actions {- display: flex;- align-items: center;- gap: 8px;- flex-shrink: 0;-}--.agent-session-popup__delete {- width: 12px;- height: 12px;- color: var(--b3-theme-error);-- &:hover, &:focus-visible {- opacity: 0.7;- }-}--.agent-session-popup__rename {- width: 12px;- height: 12px;-- &:hover, &:focus-visible {- opacity: 0.7;- }-}--.agent-session-popup__rename-input {- flex: 1;- font-size: 13px;- padding: 2px 4px;- border: 1px solid var(--b3-theme-primary);- border-radius: var(--b3-border-radius);- outline: none;- background: var(--b3-theme-background);- color: var(--b3-theme-on-background);}// Todo list stylesdiff --git a/app/src/layout/dock/agent/AgentSessionPanel.ts b/app/src/layout/dock/agent/AgentSessionPanel.tsindex badeb83531..ed12186298 100644--- a/app/src/layout/dock/agent/AgentSessionPanel.ts+++ b/app/src/layout/dock/agent/AgentSessionPanel.ts@@ -2,6 +2,7 @@ import {SessionIndexItem, SessionStore} from "./SessionStore";import {escapeHtml} from "../../../util/escape";import {setPosition} from "../../../util/setPosition";import {hasClosestByClassName} from "../../../protyle/util/hasClosest";+import {upDownHint} from "../../../util/upDownHint";export class AgentSessionPanel {private popup: HTMLElement | null = null;@@ -23,10 +24,13 @@ export class AgentSessionPanel {onDelete: (id: string) => Promise<void>;onRename: (id: string, title: string) => Promise<void>;}- ) {}+ ) {+ }toggle() {- if (this.isRendering) { return; }+ if (this.isRendering) {+ return;+ }if (this.popup) {this.close();return;@@ -35,13 +39,18 @@ export class AgentSessionPanel {}close() {- document.querySelectorAll(".agent-session-popup").forEach(function (el) { el.remove(); });+ document.querySelectorAll(".agent-session-popup").forEach(function (el) {+ el.remove();+ });this.popup = null;this.searchKeyword = "";this.items = [];this.total = 0;this.page = 0;- if (this.searchTimer !== null) { clearTimeout(this.searchTimer); this.searchTimer = null; }+ if (this.searchTimer !== null) {+ clearTimeout(this.searchTimer);+ this.searchTimer = null;+ }}destroy() {@@ -63,23 +72,36 @@ export class AgentSessionPanel {const L = window.siyuan.languages;let html = '<input class="b3-text-field agent-session-popup__search" placeholder="' + L.agentSessionSearch + '">';- html += '<div class="b3-menu__items"></div>';+ html += '<div class="b3-list b3-list--background fn__flex-1"></div>';this.popup.innerHTML = html;- const itemsContainer = this.popup.querySelector(".b3-menu__items") as HTMLElement;+ const itemsContainer = this.popup.querySelector(".b3-list") as HTMLElement;this.renderItems(itemsContainer, result.sessions, false);const searchInput = this.popup.querySelector(".agent-session-popup__search") as HTMLInputElement;- searchInput.addEventListener("input", () => {+ searchInput.addEventListener("input", (event: InputEvent) => {+ event.stopPropagation();+ if (event.isComposing) {+ return;+ }this.filter(searchInput.value, itemsContainer);});- searchInput.addEventListener("click", (e: MouseEvent) => {- e.stopPropagation();+ searchInput.addEventListener("compositionend", () => {+ this.filter(searchInput.value, itemsContainer);+ });+ searchInput.addEventListener("keydown", (event) => {+ if (event.isComposing) {+ return;+ }+ upDownHint(itemsContainer, event);});-itemsContainer.addEventListener("scroll", () => {- if (this.isLoadingMore) { return; }- if (this.items.length >= this.total) { return; }+ if (this.isLoadingMore) {+ return;+ }+ if (this.items.length >= this.total) {+ return;+ }if (itemsContainer.scrollHeight - itemsContainer.scrollTop - itemsContainer.clientHeight <= 30) {this.loadMore(itemsContainer);}@@ -94,7 +116,9 @@ export class AgentSessionPanel {this.popup.addEventListener("click", (e: MouseEvent) => {e.stopPropagation();});- const onResize = () => { this.close(); };+ const onResize = () => {+ this.close();+ };window.addEventListener("resize", onResize);const closeOut = () => {this.close();@@ -104,6 +128,7 @@ export class AgentSessionPanel {setTimeout(() => {document.addEventListener("click", closeOut);}, 10);+ searchInput.focus();} finally {this.isRendering = false;}@@ -112,19 +137,17 @@ export class AgentSessionPanel {private renderItems(container: HTMLElement, listItems: SessionIndexItem[], append: boolean) {let html = "";if (listItems.length === 0 && !append) {- html += '<div class="b3-menu__item"><span class="b3-menu__label" style="text-align:center;color:var(--b3-theme-on-surface-light)">' + (window.siyuan.languages.emptyContent || "No sessions") + "</span></div>";+ html += '<div class="b3-list--empty"><span class="b3-list-item__text">' + (window.siyuan.languages.emptyContent) + "</span></div>";} else {const currentId = this.getCurrentSessionId();const defaultTitle = this.getDefaultTitle();for (let i = 0; i < listItems.length; i++) {const s = listItems[i];const isActive = s.id === currentId;- html += '<div class="b3-menu__item' + (isActive ? " b3-menu__item--current" : "") + '" data-id="' + s.id + '">' +- '<span class="b3-menu__label ariaLabel" data-position="east" aria-label="' + escapeHtml(s.title || defaultTitle) + '">' + escapeHtml(s.title || defaultTitle) + "</span>" +- '<span class="agent-session-popup__actions">' +- '<svg class="agent-session-popup__rename ariaLabel" data-position="north" data-id="' + s.id + '" aria-label="' + window.siyuan.languages.rename + '"><use xlink:href="#iconEdit"></use></svg>' +- '<svg class="agent-session-popup__delete ariaLabel" data-position="north" data-id="' + s.id + '" aria-label="' + window.siyuan.languages.delete + '"><use xlink:href="#iconTrashcan"></use></svg>' +- "</span>" ++ html += '<div class="b3-list-item b3-list-item--hide-action' + (isActive ? " b3-list-item--focus" : "") + '" data-id="' + s.id + '">' ++ '<span class="b3-list-item__text ariaLabel" data-position="parentW" aria-label="' + escapeHtml(s.title || defaultTitle) + '">' + escapeHtml(s.title || defaultTitle) + "</span>" ++ '<span class="b3-list-item__action b3-tooltips b3-tooltips__nw" data-id="' + s.id + '" aria-label="' + window.siyuan.languages.rename + '"><svg><use xlink:href="#iconEdit"></use></svg></span>' ++ '<span class="b3-list-item__action b3-list-item__action--warning b3-tooltips b3-tooltips__nw" data-id="' + s.id + '" aria-label="' + window.siyuan.languages.delete + '"><svg><use xlink:href="#iconTrashcan"></use></svg></span>' +"</div>";}}@@ -133,41 +156,49 @@ export class AgentSessionPanel {} else {container.innerHTML = html;}+ if (!container.querySelector(".b3-list-item--focus, .b3-list--empty")) {+ container.firstElementChild.classList.add("b3-list-item--focus");+ }this.bindEvents(container);+ this.highlightCurrent();}private bindEvents(container: HTMLElement) {- if (container.dataset.eventsBound) { return; }+ if (container.dataset.eventsBound) {+ return;+ }container.dataset.eventsBound = "1";container.addEventListener("click", (e: MouseEvent) => {const target = e.target as HTMLElement;- const deleteBtn = hasClosestByClassName(target, "agent-session-popup__delete");+ const deleteBtn = hasClosestByClassName(target, "b3-list-item__action--warning");if (deleteBtn) {e.stopPropagation();const id = (deleteBtn as HTMLElement).getAttribute("data-id") || "";- if (id) { this.callbacks.onDelete(id).then(() => { this.refresh(); }); }+ if (id) {+ this.callbacks.onDelete(id).then(() => {+ this.refresh();+ });+ }return;}- const renameBtn = hasClosestByClassName(target, "agent-session-popup__rename");+ const renameBtn = hasClosestByClassName(target, "b3-list-item__action");if (renameBtn) {e.stopPropagation();const id = (renameBtn as HTMLElement).getAttribute("data-id") || "";if (id) {- const parent = (renameBtn as HTMLElement).parentElement;- const row = parent ? parent.parentElement as HTMLElement : null;- if (row) { this.startRename(id, row); }+ this.startRename(id, renameBtn.parentElement);}return;}- if (hasClosestByClassName(target, "agent-session-popup__rename-input")) {+ if (hasClosestByClassName(target, "b3-text-field")) {return;}- const item = hasClosestByClassName(target, "b3-menu__item");+ const item = hasClosestByClassName(target, "b3-list-item");if (item) {const id = (item as HTMLElement).getAttribute("data-id") || "";if (id && id !== this.getCurrentSessionId()) {@@ -179,7 +210,9 @@ export class AgentSessionPanel {}private async loadMore(container: HTMLElement) {- if (this.isLoadingMore) { return; }+ if (this.isLoadingMore) {+ return;+ }this.isLoadingMore = true;try {const result = await SessionStore.list({page: this.page + 1, pageSize: 30, keyword: this.searchKeyword});@@ -194,7 +227,9 @@ export class AgentSessionPanel {private filter(keyword: string, container: HTMLElement) {this.searchKeyword = keyword.trim();- if (this.searchTimer !== null) { clearTimeout(this.searchTimer); }+ if (this.searchTimer !== null) {+ clearTimeout(this.searchTimer);+ }this.searchTimer = window.setTimeout(async () => {const result = await SessionStore.list({page: 1, pageSize: 30, keyword: this.searchKeyword});this.items = result.sessions;@@ -208,19 +243,26 @@ export class AgentSessionPanel {}private startRename(id: string, rowEl: HTMLElement) {- const titleEl = rowEl.querySelector(".b3-menu__label") as HTMLElement;+ const titleEl = rowEl.querySelector(".b3-list-item__text") as HTMLElement;const oldTitle = titleEl.textContent || "";const input = document.createElement("input");input.type = "text";input.value = oldTitle;- input.className = "agent-session-popup__rename-input";+ input.className = "b3-text-field b3-text-field--small fn__flex-1";titleEl.replaceWith(input);input.focus();input.select();- input.addEventListener("blur", () => { this.finishRename(id, input.value, input, titleEl); });+ input.addEventListener("blur", () => {+ this.finishRename(id, input.value, input, titleEl);+ });input.addEventListener("keydown", (e: KeyboardEvent) => {- if (e.key === "Enter") { input.blur(); }- if (e.key === "Escape") { input.value = oldTitle; input.blur(); }+ if (e.key === "Enter") {+ input.blur();+ }+ if (e.key === "Escape") {+ input.value = oldTitle;+ input.blur();+ }});}@@ -230,25 +272,33 @@ export class AgentSessionPanel {titleEl.textContent = title;await this.callbacks.onRename(id, title);const listItem = this.items.find((s) => s.id === id);- if (listItem) { listItem.title = title; }+ if (listItem) {+ listItem.title = title;+ }}private highlightCurrent() {- const items = this.popup?.querySelectorAll(".b3-menu__item");- if (!items) { return; }+ const items = this.popup?.querySelectorAll(".b3-list-item");+ if (!items) {+ return;+ }+ this.popup.querySelector(".b3-menu__checked")?.remove();const currentId = this.getCurrentSessionId();for (let i = 0; i < items.length; i++) {- const item = items[i] as HTMLElement;- const sid = item.getAttribute("data-id");- item.classList.toggle("b3-menu__item--current", sid === currentId);+ if (items[i].getAttribute("data-id") === currentId) {+ items[i].insertAdjacentHTML("beforeend", '<svg class="b3-menu__checked"><use xlink:href="#iconSelect"></use></svg>');+ break;+ }}}// 跨实例会话变更(ws agentSessionChanged)时由 AgentChat.onWsMessage 调用,刷新已打开的列表。// popup 未打开时直接返回(下次 toggle/render 会拉取最新数据),避免无谓请求。async refresh() {- const itemsContainer = this.popup?.querySelector(".b3-menu__items") as HTMLElement;- if (!itemsContainer) { return; }+ const itemsContainer = this.popup?.querySelector(".b3-list") as HTMLElement;+ if (!itemsContainer) {+ return;+ }const result = await SessionStore.list({page: 1, pageSize: 30, keyword: this.searchKeyword});this.items = result.sessions;this.total = result.total;