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
Daniel9a063b0bce4c
diff --git a/app/appearance/icons/index.html b/app/appearance/icons/index.htmlindex 5322875168..9c8b6f7c8f 100644--- a/app/appearance/icons/index.html+++ b/app/appearance/icons/index.html@@ -28,6 +28,18 @@<body><h2>SiYuan</h2><div class="fn__clear">+ <div>+ <svg>+ <use xlink:href="#iconSquareStop"></use>+ </svg>+ iconSquareStop+ </div>+ <div>+ <svg>+ <use xlink:href="#iconSend"></use>+ </svg>+ iconSend+ </div><div><svg><use xlink:href="#iconLayoutGrid"></use>diff --git a/app/appearance/icons/litheness/icon.js b/app/appearance/icons/litheness/icon.jsindex 0dd0e0011c..20faf15209 100644--- a/app/appearance/icons/litheness/icon.js+++ b/app/appearance/icons/litheness/icon.js@@ -1,5 +1,11 @@document.body.insertAdjacentHTML('afterbegin', `<svg id="iconsLitheness" style="position: absolute; width: 0; height: 0; overflow: hidden;"><defs>+ <symbol id="iconSquareStop" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round">+ <rect width="18" height="18" x="3" y="3" rx="2"/><rect x="9" y="9" width="6" height="6" rx="1"/>+ </symbol>+ <symbol id="iconSend" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round">+ <path d="M14.536 21.686a.5.5 0 0 0 .937-.024l6.5-19a.496.496 0 0 0-.635-.635l-19 6.5a.5.5 0 0 0-.024.937l7.93 3.18a2 2 0 0 1 1.112 1.11z"/><path d="m21.854 2.147-10.94 10.939"/>+ </symbol><symbol id="iconLayoutGrid" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round"><rect width="7" height="7" x="3" y="3" rx="1"/><rect width="7" height="7" x="14" y="3" rx="1"/><rect width="7" height="7" x="14" y="14" rx="1"/><rect width="7" height="7" x="3" y="14" rx="1"/></symbol>diff --git a/app/src/assets/scss/business/_agentChat.scss b/app/src/assets/scss/business/_agentChat.scssindex ed9fdd6794..0f6734fda1 100644--- a/app/src/assets/scss/business/_agentChat.scss+++ b/app/src/assets/scss/business/_agentChat.scss@@ -5,10 +5,59 @@}.agent-chat__tokens {- font-size: 12px;- color: var(--b3-theme-on-surface-light);+ display: inline-flex;+ align-items: center;+ justify-content: center;flex-shrink: 0;- align-self: center;+ width: 24px;+ height: 24px;+ border-radius: var(--b3-border-radius);+ cursor: pointer;+ color: var(--b3-theme-on-surface-light);++ &:hover {+ background: var(--b3-list-hover);+ }++ svg {+ width: 18px;+ height: 18px;+ }++ // 圆环轨道(背景灰圈)。+ .agent-chat__tokens-track {+ fill: none;+ stroke: var(--b3-border-color);+ }++ // 圆环弧度(按占用率描边),rotate -90 让起点在顶部。+ .agent-chat__tokens-arc {+ fill: none;+ stroke-linecap: round;+ transform: rotate(-90deg);+ transform-origin: 50% 50%;+ }++ // 描边颜色按占用率分档:绿(<30%)、黄(30-45%)、橙(45-85%)、红(≥85%)、未知上限(灰)。+ &.agent-chat__tokens--low .agent-chat__tokens-arc {+ stroke: var(--b3-theme-success);+ }++ &.agent-chat__tokens--mid .agent-chat__tokens-arc {+ stroke: var(--b3-graph-todo-point);+ }++ &.agent-chat__tokens--warn .agent-chat__tokens-arc {+ stroke: var(--b3-theme-secondary);+ }++ &.agent-chat__tokens--high .agent-chat__tokens-arc {+ stroke: var(--b3-theme-error);+ }++ &.agent-chat__tokens--unknown .agent-chat__tokens-arc {+ stroke: var(--b3-theme-on-surface-light);+ }}.agent-chat__messages-wrap {@@ -886,6 +935,23 @@ pre:hover .agent-chat__copy-wrap {height: 18px;}+// 发送/停止按钮:与上下文用量圆环保持一致的 24×24 正方形背景,hover 同色。+.agent-chat__send,+.agent-chat__stop {+ display: inline-flex;+ align-items: center;+ justify-content: center;+ width: 24px;+ height: 24px;+ padding: 0;+ flex-shrink: 0;+ border-radius: var(--b3-border-radius);++ &:hover {+ background: var(--b3-list-hover);+ }+}+// 模型选择器:原生 select,复用 b3-select 基础样式,此处只补充尺寸约束。// height 与 line-height 一致以保证文字垂直居中(b3-select 默认 28/20,这里压到 24/24)。.agent-chat__model-select {@@ -961,6 +1027,38 @@ pre:hover .agent-chat__copy-wrap {background: var(--b3-border-color);}+// 第一行下方的上下文占用横条:总长 = 模型上限,填充宽度 = 已用占比,+// 填充色按占用率分档,与底部圆环颜色算法一致;未填充为轨道灰。+.agent-token-popup__bar {+ height: 4px;+ margin: 6px 10px 4px;+ border-radius: 2px;+ background: var(--b3-border-color);+ overflow: hidden;++ & > span {+ display: block;+ height: 100%;+ border-radius: 2px;+ }++ &.agent-token-popup__bar--low > span {+ background: var(--b3-theme-success);+ }++ &.agent-token-popup__bar--mid > span {+ background: var(--b3-graph-todo-point);+ }++ &.agent-token-popup__bar--warn > span {+ background: var(--b3-theme-secondary);+ }++ &.agent-token-popup__bar--high > span {+ background: var(--b3-theme-error);+ }+}+.agent-session-popup {position: fixed;width: 280px;diff --git a/app/src/layout/dock/AgentChat.ts b/app/src/layout/dock/AgentChat.tsindex cd5fdbc9a0..77b672530c 100644--- a/app/src/layout/dock/AgentChat.ts+++ b/app/src/layout/dock/AgentChat.ts@@ -223,9 +223,14 @@ export class AgentChat extends Model {'<div class="agent-chat__buttons">' +'<select class="agent-chat__model-select b3-select" tabindex="0"></select>' +'<span class="fn__flex-1"></span>' +- '<span class="agent-chat__tokens fn__none"></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>' +- '<button class="agent-chat__stop b3-button b3-button--cancel fn__none b3-tooltips b3-tooltips__n" aria-label="' + (L.agentStop || "Stop") + '"><svg><use xlink:href="#iconCircleStop"></use></svg></button>' ++ '<span class="agent-chat__tokens fn__none" aria-label="' + (L.tokenUsage || "Context Usage") + '">' ++ '<svg viewBox="0 0 24 24">' ++ '<circle class="agent-chat__tokens-track" cx="12" cy="12" r="9" stroke-width="2.5"></circle>' ++ '<circle class="agent-chat__tokens-arc" cx="12" cy="12" r="9" stroke-width="2.5" stroke-dasharray="0 56.55"></circle>' ++ "</svg>" ++ "</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="#iconSend"></use></svg></button>' ++ '<button class="agent-chat__stop b3-button b3-button--cancel fn__none b3-tooltips b3-tooltips__n" aria-label="' + (L.agentStop || "Stop") + '"><svg><use xlink:href="#iconSquareStop"></use></svg></button>' +"</div>" +"</div>" +'<div class="agent-chat__preview-notice">' + (L.featurePreview || "") + "</div>" +@@ -265,6 +270,9 @@ export class AgentChat extends Model {this.composer = mountComposer(this.composerHost, () => {this.sendMessage();+ }, () => {+ // 内容变化时刷新发送按钮可用性(含用户输入、IME、程序化 clear 等所有 doc 变更)。+ this.updateSendButtonState();});this.sessionPanel = new AgentSessionPanel(this.sessionMenuBtn,@@ -1107,6 +1115,8 @@ export class AgentChat extends Model {if (this.composer) {this.composer.focus();}+ // clear() 是程序化清空,不触发原生 input 事件,需显式刷新发送按钮(空输入 → 禁用)。+ this.updateSendButtonState();this.showWelcome();this.scrollToBottom(true);}@@ -2444,26 +2454,39 @@ export class AgentChat extends Model {return L.agentThinking || "Thinking";}- // 刷新底部「当前上下文 tokens」显示。无值时隐藏。+ // 刷新底部 token 圆环显示。contextTokens 为 0 时隐藏(含切换到无统计的旧会话场景)。private updateTokenDisplay() {if (!this.tokenDisplayEl) {return;}- // contextTokens 为 0 时隐藏显示(含切换到无统计的旧会话场景),避免残留上一会话的数字。if (this.contextTokens === 0) {this.tokenDisplayEl.classList.add("fn__none");return;}this.tokenDisplayEl.classList.remove("fn__none");- const used = this.contextTokens >= 1000- ? (this.contextTokens / 1000).toFixed(1) + "k"- : this.contextTokens.toString();- // 已知模型上限时显示「已用 / 上限」,未知时仅显示已用。- const text = this.contextLimit > 0- ? used + " / " + this.formatTokenCount(this.contextLimit)- : used;- this.tokenDisplayEl.textContent = text;- this.tokenDisplayEl.classList.remove("fn__none");+ const arc = this.tokenDisplayEl.querySelector(".agent-chat__tokens-arc") as SVGCircleElement | null;+ if (!arc) {+ return;+ }+ const circumference = 2 * Math.PI * 9; // r=9 → ≈56.55+ const tokens = this.contextTokens;+ const limit = this.contextLimit;+ // 颜色档位 class:未知上限 → unknown;按占用率分 low/mid/warn/high(与横条共用算法)。+ const levelClass = limit > 0+ ? "agent-chat__tokens--" + this.contextUsageLevel(tokens / limit)+ : "agent-chat__tokens--unknown";+ this.tokenDisplayEl.classList.remove(+ "agent-chat__tokens--low",+ "agent-chat__tokens--mid",+ "agent-chat__tokens--warn",+ "agent-chat__tokens--high",+ "agent-chat__tokens--unknown"+ );+ this.tokenDisplayEl.classList.add(levelClass);+ // 弧长:未知上限(limit=0)时画一个小占位弧(约 15%),表示有用量但无上限。+ const ratio = limit > 0 ? Math.min(tokens / limit, 1) : 0.15;+ const filled = circumference * ratio;+ arc.setAttribute("stroke-dasharray", filled.toFixed(2) + " " + circumference.toFixed(2));}// 记录最近一轮的 prompt tokens(= 当前上下文已用)+ 分类明细 + 缓存命中 + 模型上限,覆盖式更新而非累加。@@ -2485,13 +2508,25 @@ export class AgentChat extends Model {const popup = document.createElement("div");popup.className = "agent-token-popup b3-menu";let html = '<div class="b3-menu__items">';- // 第一行:上下文用量(总数)。+ // 第一行:已用 / 上限 · 占用百分比;未知上限时仅显示已用。+ const limitLine = this.contextLimit > 0+ ? this.formatTokenCount(this.contextTokens) + " / " + this.formatTokenCount(this.contextLimit) + " · " + Math.round(this.contextTokens / this.contextLimit * 100) + "%"+ : this.formatTokenCount(this.contextTokens);html += '<div class="agent-token-popup__total">' +'<span class="agent-token-popup__label">' + (L.tokenUsage || "Context Usage") + "</span>" +- '<span class="agent-token-popup__value">' + this.formatTokenCount(this.contextTokens) + "</span>" ++ '<span class="agent-token-popup__value">' + limitLine + "</span>" +"</div>";- // 分隔线。- html += '<div class="agent-token-popup__divider"></div>';+ // 第一行下方的占用横条:总长=上限,填充=已用占比,颜色档位与圆环一致。+ // 未知上限(contextLimit=0)时不画横条(无总长基线)。+ if (this.contextLimit > 0) {+ const ratio = Math.min(this.contextTokens / this.contextLimit, 1);+ const barLevel = this.contextUsageLevel(ratio);+ html += '<div class="agent-token-popup__bar agent-token-popup__bar--' + barLevel + '">' ++ '<span style="width:' + (ratio * 100).toFixed(1) + '%"></span>' ++ "</div>";+ } else {+ html += '<div class="agent-token-popup__divider"></div>';+ }// 各分类(0 值跳过),百分比格式。for (const row of this.formatTokenBreakdown()) {html += '<div class="agent-token-popup__row">' +@@ -2512,9 +2547,12 @@ export class AgentChat extends Model {popup.innerHTML = html;document.body.appendChild(popup);popup.style.zIndex = (++window.siyuan.zIndex).toString();- // 定位在 tokens 数字上方。+ // 定位:右对齐到 trigger 右边缘并向左延伸。trigger(圆环)只有 24px 宽且紧贴发送按钮,+ // 左对齐会让 popup 向右溢出后挤到屏幕右边缘、首行末尾贴边,故改用右对齐。const rect = this.tokenDisplayEl.getBoundingClientRect();- setPosition(popup, rect.left, rect.top, rect.height, rect.width);+ const popupWidth = popup.offsetWidth;+ const rightAlignedLeft = rect.right - popupWidth;+ setPosition(popup, rightAlignedLeft, rect.top, rect.height);// popup 自身 hover 保持显示(鼠标移入时取消关闭计时,移出时关闭)。popup.addEventListener("mouseenter", () => {window.clearTimeout(this.tokenPopupHideTimer);@@ -2592,9 +2630,39 @@ export class AgentChat extends Model {return result;}- // token 数 k 格式化(>=1000 显示为 1.2k)。+ // 上下文占用率 → 颜色档位名称(low/mid/warn/high)。圆环弧度与弹层横条共用此算法,保证两者配色一致。+ // 阈值:green(<30%) / yellow(30-45%) / orange(45-85%) / red(≥85%)。+ private contextUsageLevel(ratio: number): string {+ if (ratio >= 0.85) { return "high"; }+ if (ratio >= 0.45) { return "warn"; }+ if (ratio >= 0.3) { return "mid"; }+ return "low";+ }++ // token 数格式化:1024 进制值(2^N,如 131072=128×1024)转成业界惯称(128k、1M),+ // 仅当「能整除 1024 且商在白名单」时才用 1024 进制,避免 256000 这种 1000 进制值被误判为 250k。+ // 其余按 1000 进制(200000→200k、1048576→1.0M)。private formatTokenCount(n: number): string {- return n >= 1000 ? (n / 1000).toFixed(1) + "k" : String(n);+ if (n <= 0) {+ return String(n);+ }+ // 白名单:业界常见的 2^N 商(8k/16k/32k/64k/128k/256k/512k/1M)+ 200(200k=204800 少见但存在)。+ const niceMultiples = new Set([8, 16, 32, 64, 128, 200, 256, 512, 1024]);+ if (n >= 1024 && n % 1024 === 0 && niceMultiples.has(n / 1024)) {+ const quotient = n / 1024;+ if (quotient >= 1024) {+ return (quotient / 1024) + "M";+ }+ return quotient + "k";+ }+ // 1000 进制或其他:除以 1000 / 1000000,整除时省略小数(200000→200k,3500→3.5k)。+ if (n >= 1000000) {+ return (n / 1000000).toFixed(n % 1000000 === 0 ? 0 : 1) + "M";+ }+ if (n >= 1000) {+ return (n / 1000).toFixed(n % 1000 === 0 ? 0 : 1) + "k";+ }+ return String(n);}private clearThinking() {@@ -2671,10 +2739,10 @@ export class AgentChat extends Model {this.updateSendButtonState();}- // 根据"是否流式中"与"是否有可用模型"综合决定发送按钮与输入框可用性。+ // 根据"是否流式中"、"是否有可用模型"、"输入框是否有内容"综合决定发送按钮与输入框可用性。// 无模型时一并禁用发送按钮与输入框(attr disabled + 灰样式 + composer-host 禁用态),从源头阻止无效请求。private updateSendButtonState() {- const disabled = this.isStreaming || this.modelOptions.length === 0;+ const disabled = this.isStreaming || this.modelOptions.length === 0 || !this.hasComposerInput();if (disabled) {this.sendBtn.setAttribute("disabled", "disabled");this.sendBtn.classList.add("agent-chat__send--disabled");@@ -2684,8 +2752,18 @@ export class AgentChat extends Model {}if (this.composerHost) {// 复用流式时已有的禁用态样式(灰显 + 阻止交互)。- this.composerHost.classList.toggle("agent-chat__composer-host--disabled", disabled);+ // 注意:仅流式 / 无模型时禁用 composer;输入为空不禁用 composer(用户仍可正常编辑)。+ const composerDisabled = this.isStreaming || this.modelOptions.length === 0;+ this.composerHost.classList.toggle("agent-chat__composer-host--disabled", composerDisabled);+ }+ }++ // 输入框当前是否有可发送内容(含 @引用也算)。无 composer 时返回 false。+ private hasComposerInput(): boolean {+ if (!this.composer) {+ return false;}+ return this.composer.getSendData().text.length > 0;}private scrollToBottom(force = false, smooth = false) {diff --git a/app/src/layout/dock/AgentComposer.ts b/app/src/layout/dock/AgentComposer.tsindex 9ba58821a5..335a69e173 100644--- a/app/src/layout/dock/AgentComposer.ts+++ b/app/src/layout/dock/AgentComposer.ts@@ -27,7 +27,11 @@ interface ComposerHandle {restoreHistory: (h: string[]) => void;}-export function mountComposer(host: HTMLElement, onSend: () => void): ComposerHandle {+// 内容变化回调(含用户输入、IME、程序化 clearContent 等所有 doc 变更)。+// 用于发送按钮启用/禁用等需要感知输入框内容的外部逻辑。+type OnChangeCallback = () => void;++export function mountComposer(host: HTMLElement, onSend: () => void, onChange?: OnChangeCallback): ComposerHandle {const L = window.siyuan.languages;let suggestionMenu: HTMLElement | null = null;@@ -311,6 +315,8 @@ export function mountComposer(host: HTMLElement, onSend: () => void): ComposerHa});editor.on("update", function () {+ // 通知外部内容已变更(无论是否涉及 slash 命令处理)。+ if (onChange) { onChange(); }if (suggestionMenu && !slashActive) { return; }const {$from} = editor.state.selection;const textBefore = $from.parent.textBetween(0, $from.parentOffset);