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
Daniela904a408913c
diff --git a/app/appearance/icons/index.html b/app/appearance/icons/index.htmlindex 5592f69ca1..2e4c766e7d 100644--- a/app/appearance/icons/index.html+++ b/app/appearance/icons/index.html@@ -28,6 +28,12 @@<body><h2>SiYuan</h2><div class="fn__clear">+ <div>+ <svg>+ <use xlink:href="#iconCheck2"></use>+ </svg>+ iconCheck2+ </div><div><svg><use xlink:href="#iconCirclePlay"></use>diff --git a/app/appearance/icons/litheness/icon.js b/app/appearance/icons/litheness/icon.jsindex a078bd7e11..3298e40826 100644--- a/app/appearance/icons/litheness/icon.js+++ b/app/appearance/icons/litheness/icon.js@@ -1,5 +1,8 @@document.body.insertAdjacentHTML('afterbegin', `<svg id="iconsLitheness" style="position: absolute; width: 0; height: 0; overflow: hidden;"><defs>+ <symbol id="iconCheck2" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round">+ <path d="M20 6 9 17l-5-5"/>+ </symbol><symbol id="iconCirclePlay" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round"><path d="M9 9.003a1 1 0 0 1 1.517-.859l4.997 2.997a1 1 0 0 1 0 1.718l-4.997 2.997A1 1 0 0 1 9 14.996z"/><circle cx="12" cy="12" r="10"/></symbol>diff --git a/app/src/assets/scss/business/_agentChat.scss b/app/src/assets/scss/business/_agentChat.scssindex 087669075a..d84345924b 100644--- a/app/src/assets/scss/business/_agentChat.scss+++ b/app/src/assets/scss/business/_agentChat.scss@@ -617,6 +617,56 @@margin-top: 8px;}+.agent-chat__model-trigger {+ display: flex; align-items: center; gap: 2px;+ font-size: 13px; padding: 0 8px; height: 24px;+ border-radius: var(--b3-border-radius);+ background: transparent; cursor: pointer;+ white-space: nowrap;+ position: relative;+ outline: none;++ svg { width: 12px; height: 12px; flex-shrink: 0; }++ &:hover, &:focus-visible { background: var(--b3-theme-surface-light); }+}++.agent-chat__model-menu {+ position: absolute; bottom: 100%; left: 0;+ min-width: 160px; max-height: 240px; overflow-y: auto;+ background: var(--b3-theme-background);+ border: 1px solid var(--b3-border-color);+ border-radius: var(--b3-border-radius-b);+ box-shadow: var(--b3-dialog-shadow);+ z-index: 9999;+ padding: 4px 0;+}++.agent-chat__model-item {+ display: flex; align-items: center;+ padding: 2px 12px; font-size: 13px; line-height: 1.5;+ cursor: pointer;+ color: var(--b3-theme-on-background);++ &:hover { background: var(--b3-list-hover); }++ &--current {+ background: var(--b3-theme-surface-light);+ }+}++.agent-chat__model-check {+ display: none;+ width: 14px; height: 14px;+ margin-left: auto;+ flex-shrink: 0;+ color: var(--b3-theme-on-surface-light);+}++.agent-chat__model-item--current .agent-chat__model-check {+ display: block;+}+.agent-session-popup {position: fixed;z-index: 9999;diff --git a/app/src/layout/dock/AgentChat.ts b/app/src/layout/dock/AgentChat.tsindex c576e0d3e2..66f890e28f 100644--- a/app/src/layout/dock/AgentChat.ts+++ b/app/src/layout/dock/AgentChat.ts@@ -86,7 +86,11 @@ export class AgentChat extends Model {private pendingConfirms: SessionEntry[] = [];private renderedToolNames: Record<string, boolean> = {};private hasInterveningCard = false;- private modelSelect: HTMLSelectElement;+ private modelTrigger: HTMLElement;+ private selectedModel: string;+ private modelMenu: HTMLElement | null = null;+ private modelMenuIndex = 0;+ private modelOptions: Array<{ id: string; name: string }> = [];private userScrolledUp = false;private scrollBottomBtn: HTMLElement;@@ -129,7 +133,7 @@ export class AgentChat extends Model {'<div class="agent-chat__input-area">' +'<div class="agent-chat__composer-host"></div>' +'<div class="agent-chat__buttons">' +- '<select class="agent-chat__model b3-select"></select>' ++ '<span class="agent-chat__model-trigger" tabindex="0"><span class="agent-chat__model-label"></span><svg><use xlink:href="#iconUp"></use></svg></span>' +'<span class="agent-chat__tokens fn__none"></span>' +'<span class="fn__flex-1"></span>' +'<button class="agent-chat__send b3-button b3-button--text b3-tooltips b3-tooltips__n" aria-label="' + (L.agentSend || "Send") + '"><svg><use xlink:href="#iconCirclePlay"></use></svg></button>' +@@ -146,7 +150,7 @@ export class AgentChat extends Model {this.sessionMenuBtn = panel.querySelector('.block__icon[data-type="session-menu"]') as HTMLElement;this.titleElement = panel.querySelector(".agent-chat__title") as HTMLElement;this.tokenDisplayEl = panel.querySelector(".agent-chat__tokens") as HTMLElement;- this.modelSelect = panel.querySelector(".agent-chat__model") as HTMLSelectElement;+ this.modelTrigger = panel.querySelector(".agent-chat__model-trigger") as HTMLElement;this.scrollBottomBtn = panel.querySelector(".agent-chat__scroll-bottom") as HTMLElement;this.messagesContainer.addEventListener("scroll", () => {const { scrollTop, scrollHeight, clientHeight } = this.messagesContainer;@@ -181,20 +185,105 @@ export class AgentChat extends Model {private initModelSelect() {const aiConfig = window.siyuan.config.ai;- const mainName = aiConfig.openAI.name || aiConfig.openAI.apiModel;- const providers = aiConfig.providers || [];- let html = '<option value="' + aiConfig.openAI.id + '">' + escapeHtml(mainName) + "</option>";- for (const p of providers) {- if (p.enabled === false) {- continue;+ const displayName = aiConfig.openAI.name || aiConfig.openAI.apiModel;+ this.modelOptions = [{ id: aiConfig.openAI.id, name: displayName }];+ for (const p of aiConfig.providers || []) {+ if (p.enabled === false) { continue; }+ this.modelOptions.push({ id: p.id, name: p.name || p.apiModel || p.id });+ }+ this.selectedModel = aiConfig.openAI.id;+ this.updateModelLabel();+ this.modelTrigger.addEventListener("click", (e: MouseEvent) => {+ e.stopPropagation();+ if (this.modelMenu) {+ this.closeModelMenu();+ } else {+ this.openModelMenu();+ }+ });+ this.modelTrigger.addEventListener("keydown", (e: KeyboardEvent) => {+ if (e.key === "Enter" || e.key === " ") {+ e.preventDefault();+ if (this.modelMenu) { this.closeModelMenu(); } else { this.openModelMenu(); }+ } else if (e.key === "ArrowDown") {+ e.preventDefault();+ if (!this.modelMenu) { this.openModelMenu(); return; }+ this.modelMenuIndex = (this.modelMenuIndex + 1) % this.modelOptions.length;+ this.updateModelMenuHighlight();+ } else if (e.key === "ArrowUp") {+ e.preventDefault();+ if (!this.modelMenu) { this.openModelMenu(); return; }+ this.modelMenuIndex = (this.modelMenuIndex - 1 + this.modelOptions.length) % this.modelOptions.length;+ this.updateModelMenuHighlight();+ } else if (e.key === "Escape" && this.modelMenu) {+ this.closeModelMenu();+ }+ });+ }++ private updateModelLabel() {+ const label = this.modelTrigger.querySelector(".agent-chat__model-label") as HTMLElement;+ const option = this.modelOptions.find((o) => o.id === this.selectedModel);+ if (label && option) { label.textContent = option.name; }+ }++ private openModelMenu() {+ this.closeModelMenu();+ this.modelMenuIndex = this.modelOptions.findIndex((o) => o.id === this.selectedModel);+ if (this.modelMenuIndex < 0) { this.modelMenuIndex = 0; }+ const menu = document.createElement("div");+ menu.className = "agent-chat__model-menu";+ let html = "";+ for (let i = 0; i < this.modelOptions.length; i++) {+ const o = this.modelOptions[i];+ const isSelected = o.id === this.selectedModel;+ html += '<div class="agent-chat__model-item' + (isSelected ? " agent-chat__model-item--current" : "") + '" data-i="' + i + '" data-id="' + o.id + '">' ++ '<span>' + escapeHtml(o.name) + "</span>" ++ '<svg class="agent-chat__model-check"><use xlink:href="#iconCheck2"></use></svg>' ++ "</div>";+ }+ menu.innerHTML = html;+ this.modelTrigger.appendChild(menu);+ this.modelMenu = menu;+ this.updateModelMenuHighlight();+ menu.addEventListener("click", (e: MouseEvent) => {+ const item = (e.target as HTMLElement).closest(".agent-chat__model-item") as HTMLElement;+ if (item) {+ this.selectedModel = item.getAttribute("data-id") || this.selectedModel;+ this.updateModelLabel();+ this.closeModelMenu();}- html += '<option value="' + p.id + '">' + escapeHtml(p.name || p.apiModel) + "</option>";+ });+ setTimeout(() => {+ document.addEventListener("click", this.closeModelMenuHandler);+ }, 10);+ }++ private closeModelMenuHandler = () => {+ this.closeModelMenu();+ document.removeEventListener("click", this.closeModelMenuHandler);+ };++ private closeModelMenu() {+ if (this.modelMenu) {+ this.modelMenu.remove();+ this.modelMenu = null;+ }+ document.removeEventListener("click", this.closeModelMenuHandler);+ }++ private updateModelMenuHighlight() {+ if (!this.modelMenu) { return; }+ const items = this.modelMenu.querySelectorAll(".agent-chat__model-item");+ for (let i = 0; i < items.length; i++) {+ items[i].classList.toggle("agent-chat__model-item--current", i === this.modelMenuIndex);}- this.modelSelect.innerHTML = html;+ const current = items[this.modelMenuIndex] as HTMLElement;+ if (current) { current.scrollIntoView({ block: "nearest" }); }}private getSelectedModel(): string {- return this.modelSelect.value;+ return this.selectedModel;}private showWelcome() {@@ -274,7 +363,7 @@ export class AgentChat extends Model {getDockByType("agentChat").toggleModel("agentChat", false, true);return;}- if (t.closest(".agent-chat__model")) {+ if (t.closest(".agent-chat__model-trigger") || t.closest(".agent-chat__model-menu")) {return;}if (this.composer) {@@ -302,7 +391,8 @@ export class AgentChat extends Model {this.sessionCompletionTokens = session.completionTokens || 0;this.sessionTotalDuration = session.totalDuration || 0;if (session.model) {- this.modelSelect.value = session.model;+ this.selectedModel = session.model;+ this.updateModelLabel();}if (this.composer) {this.composer.restoreHistory(session.messageHistory || []);@@ -367,7 +457,8 @@ export class AgentChat extends Model {this.sessionCompletionTokens = session.completionTokens || 0;this.sessionTotalDuration = session.totalDuration || 0;if (session.model) {- this.modelSelect.value = session.model;+ this.selectedModel = session.model;+ this.updateModelLabel();}if (this.tokenDisplayEl) {this.updateTokenDisplay();