From dc7ec0619658e0ff7d9ad51ba79beaa7e8ecfd97 Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Thu, 4 Jun 2026 13:23:38 +0800 Subject: [PATCH] :sparkles: AI Agent https://github.com/siyuan-note/siyuan/issues/17797 Signed-off-by: Daniel <845765@qq.com> --- app/src/assets/scss/business/_agentChat.scss | 6 +- app/src/layout/dock/AgentChat.ts | 77 ++++++++++++-------- app/src/layout/dock/SessionStore.ts | 3 + kernel/agent/agent.go | 2 +- 4 files changed, 52 insertions(+), 36 deletions(-) diff --git a/app/src/assets/scss/business/_agentChat.scss b/app/src/assets/scss/business/_agentChat.scss index 1752e3522..e1d04b90d 100644 --- a/app/src/assets/scss/business/_agentChat.scss +++ b/app/src/assets/scss/business/_agentChat.scss @@ -9,12 +9,8 @@ .agent-chat__tokens { font-size: 11px; color: var(--b3-theme-on-surface-light); - background: var(--b3-theme-surface-light); - border-radius: 10px; - padding: 1px 8px; flex-shrink: 0; - margin-right: 4px; - line-height: 18px; + align-self: center; } .agent-chat__messages { diff --git a/app/src/layout/dock/AgentChat.ts b/app/src/layout/dock/AgentChat.ts index 31b689fd9..a8c76524e 100644 --- a/app/src/layout/dock/AgentChat.ts +++ b/app/src/layout/dock/AgentChat.ts @@ -31,6 +31,8 @@ export class AgentChat extends Model { private fullContent = ""; private sessionPromptTokens = 0; private sessionCompletionTokens = 0; + private sessionTotalDuration = 0; + private requestStartTime = 0; private tokenDisplayEl: HTMLElement; private defaultTitle = ""; @@ -53,7 +55,6 @@ export class AgentChat extends Model { panel.innerHTML = '
' + '
' + '' + - '' + '' + '' + '' + @@ -70,6 +71,7 @@ export class AgentChat extends Model { '
' + '
' + '
' + + '' + '' + '' + '
' + @@ -157,7 +159,11 @@ export class AgentChat extends Model { this.sessionTitle = session.title; this.messages = session.messages; this.hasTitled = true; + this.sessionPromptTokens = session.promptTokens || 0; + this.sessionCompletionTokens = session.completionTokens || 0; + this.sessionTotalDuration = session.totalDuration || 0; this.titleElement.textContent = session.title; + this.updateTokenDisplay(); for (var i = 0; i < session.messages.length; i++) { var msg = session.messages[i]; if (msg.role === "user") { @@ -298,6 +304,9 @@ export class AgentChat extends Model { id: this.sessionId, title: this.sessionTitle, messages: this.messages.slice(), + promptTokens: this.sessionPromptTokens, + completionTokens: this.sessionCompletionTokens, + totalDuration: this.sessionTotalDuration, createdAt: Date.now(), updatedAt: Date.now(), }; @@ -315,10 +324,11 @@ export class AgentChat extends Model { this.currentAIElement = null; this.currentContent = ""; this.fullContent = ""; - this.sessionPromptTokens = 0; - this.sessionCompletionTokens = 0; + this.sessionPromptTokens = session.promptTokens || 0; + this.sessionCompletionTokens = session.completionTokens || 0; + this.sessionTotalDuration = session.totalDuration || 0; if (this.tokenDisplayEl) { - this.tokenDisplayEl.classList.add("fn__none"); + this.updateTokenDisplay(); } this.messagesContainer.innerHTML = ""; this.titleElement.textContent = session.title; @@ -351,8 +361,9 @@ export class AgentChat extends Model { this.fullContent = ""; this.sessionPromptTokens = 0; this.sessionCompletionTokens = 0; + this.sessionTotalDuration = 0; if (this.tokenDisplayEl) { - this.tokenDisplayEl.classList.add("fn__none"); + this.tokenDisplayEl.style.display = "none"; } this.messagesContainer.innerHTML = ""; this.titleElement.textContent = this.defaultTitle; @@ -389,6 +400,7 @@ export class AgentChat extends Model { this.messages.push({role: "user", content: text}); this.appendUserMessage(text); + this.requestStartTime = Date.now(); this.currentAIElement = this.createAIMessagePlaceholder(); var apiMessages = this.messages.map(function (m) { return {role: m.role, content: m.content}; }); @@ -453,7 +465,7 @@ export class AgentChat extends Model { this.currentContent = ""; var el = document.createElement("div"); el.className = "agent-chat__msg agent-chat__msg--ai"; - el.innerHTML = '
'; + el.innerHTML = '
'; this.messagesContainer.appendChild(el); this.scrollToBottom(); return el; @@ -470,7 +482,7 @@ export class AgentChat extends Model { this.currentContent += token; this.fullContent += token; this.clearThinking(); - bubble.innerHTML = this.lute.MarkdownStr("", this.currentContent) + ''; + bubble.innerHTML = this.lute.MarkdownStr("", this.currentContent); this.scrollToBottom(); } @@ -580,10 +592,6 @@ export class AgentChat extends Model { } var bubble = this.currentAIElement.querySelector(".agent-chat__bubble") as HTMLElement; if (bubble) { - var cursor = bubble.querySelector(".agent-chat__cursor"); - if (cursor) { - cursor.remove(); - } bubble.classList.remove("agent-chat__bubble--streaming"); } this.currentAIElement = null; @@ -597,16 +605,16 @@ export class AgentChat extends Model { this.clearThinking(); var bubble = this.currentAIElement.querySelector(".agent-chat__bubble") as HTMLElement; if (bubble) { - var cursor = bubble.querySelector(".agent-chat__cursor"); - if (cursor) { - cursor.remove(); - } bubble.classList.remove("agent-chat__bubble--streaming"); } this.messages.push({role: "assistant", content: this.fullContent || " "}); this.currentAIElement = null; this.currentContent = ""; this.fullContent = ""; + if (this.requestStartTime) { + this.sessionTotalDuration += Date.now() - this.requestStartTime; + this.requestStartTime = 0; + } this.setStreaming(false); if (!this.hasTitled && this.messages.length >= 2) { @@ -638,12 +646,6 @@ export class AgentChat extends Model { el.className = "agent-chat__msg agent-chat__msg--error"; el.innerHTML = '
' + this.escapeHtml(message) + '
'; this.insertBeforeAI(el); - if (this.currentAIElement) { - var cursor = this.currentAIElement.querySelector(".agent-chat__cursor"); - if (cursor) { - cursor.remove(); - } - } this.currentAIElement = null; this.scrollToBottom(); this.saveSession(); @@ -657,10 +659,6 @@ export class AgentChat extends Model { if (this.currentAIElement) { var bubble = this.currentAIElement.querySelector(".agent-chat__bubble") as HTMLElement; if (bubble) { - var cursor = bubble.querySelector(".agent-chat__cursor"); - if (cursor) { - cursor.remove(); - } bubble.classList.remove("agent-chat__bubble--streaming"); } if (this.fullContent) { @@ -670,6 +668,10 @@ export class AgentChat extends Model { this.currentContent = ""; this.fullContent = ""; } + if (this.requestStartTime) { + this.sessionTotalDuration += Date.now() - this.requestStartTime; + this.requestStartTime = 0; + } this.setStreaming(false); } @@ -723,14 +725,29 @@ export class AgentChat extends Model { }); } + private updateTokenDisplay() { + if (!this.tokenDisplayEl) { + return; + } + var total = this.sessionPromptTokens + this.sessionCompletionTokens; + if (total === 0 && this.sessionTotalDuration === 0) { + return; + } + var text = total >= 1000 ? (total / 1000).toFixed(1) + "k" : total.toString(); + if (this.sessionTotalDuration > 0) { + var seconds = Math.floor(this.sessionTotalDuration / 1000); + var minutes = Math.floor(seconds / 60); + seconds = seconds % 60; + text += " \u00B7 " + (minutes > 0 ? minutes + "m" : "") + seconds + "s"; + } + this.tokenDisplayEl.textContent = text; + this.tokenDisplayEl.style.display = ""; + } + private appendUsage(promptTokens: number, completionTokens: number) { this.sessionPromptTokens += promptTokens; this.sessionCompletionTokens += completionTokens; - var total = this.sessionPromptTokens + this.sessionCompletionTokens; - if (this.tokenDisplayEl) { - this.tokenDisplayEl.textContent = total >= 1000 ? (total / 1000).toFixed(1) + "k" : total.toString(); - this.tokenDisplayEl.classList.remove("fn__none"); - } + this.updateTokenDisplay(); } private clearThinking() { diff --git a/app/src/layout/dock/SessionStore.ts b/app/src/layout/dock/SessionStore.ts index 1b88b4dd6..48a6139e5 100644 --- a/app/src/layout/dock/SessionStore.ts +++ b/app/src/layout/dock/SessionStore.ts @@ -14,6 +14,9 @@ export interface AgentSession { id: string; title: string; messages: Array<{role: string; content: string}>; + promptTokens?: number; + completionTokens?: number; + totalDuration?: number; createdAt: number; updatedAt: number; } diff --git a/kernel/agent/agent.go b/kernel/agent/agent.go index 27ac87caf..82de4769d 100644 --- a/kernel/agent/agent.go +++ b/kernel/agent/agent.go @@ -31,7 +31,7 @@ Rules: 2. Summarize key points instead of listing everything when there are many search results 3. Ask for confirmation before deleting anything` -const maxToolCallRounds = 30 +const maxToolCallRounds = 64 var confirmChannels = make(map[string]chan bool)