From 6eaae0bfe70aedaa63671004a30c653025e60e03 Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Sat, 6 Jun 2026 14:24:11 +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 | 20 ++++ app/src/layout/dock/AgentChat.ts | 106 ++++--------------- app/src/layout/dock/SessionStore.ts | 1 + 3 files changed, 41 insertions(+), 86 deletions(-) diff --git a/app/src/assets/scss/business/_agentChat.scss b/app/src/assets/scss/business/_agentChat.scss index 6f1af446e..72d76924f 100644 --- a/app/src/assets/scss/business/_agentChat.scss +++ b/app/src/assets/scss/business/_agentChat.scss @@ -311,6 +311,26 @@ align-items: stretch; } +.agent-chat__tool-line { + font-size: 12px; + color: var(--b3-theme-on-surface-light); + padding: 2px 0; + cursor: pointer; + display: flex; + align-items: center; + gap: 4px; + + &:hover { + color: var(--b3-theme-on-surface); + } +} + +.agent-chat__tool-label { + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} + .agent-chat__tool-card { border: 1px solid var(--b3-border-color); border-radius: var(--b3-border-radius); diff --git a/app/src/layout/dock/AgentChat.ts b/app/src/layout/dock/AgentChat.ts index 0fea2aba1..7974d660c 100644 --- a/app/src/layout/dock/AgentChat.ts +++ b/app/src/layout/dock/AgentChat.ts @@ -47,7 +47,6 @@ export class AgentChat extends Model { private currentToolCalls: Array<{name: string; arguments: Record; result?: string}> = []; private abortController: AbortController | null = null; private isRenderingSessionList = false; - private messageHistory: Record = {}; constructor(app: App, tab: Tab) { super({app: app}); @@ -187,6 +186,9 @@ export class AgentChat extends Model { this.sessionPromptTokens = session.promptTokens || 0; this.sessionCompletionTokens = session.completionTokens || 0; this.sessionTotalDuration = session.totalDuration || 0; + if (this.composer) { + this.composer.restoreHistory(session.messageHistory || []); + } this.titleElement.textContent = session.title; this.updateTokenDisplay(); for (let i = 0; i < session.messages.length; i++) { @@ -346,22 +348,20 @@ export class AgentChat extends Model { totalDuration: this.sessionTotalDuration, createdAt: this.sessionCreatedAt, updatedAt: Date.now(), + messageHistory: this.composer?.getHistory() || [], }; await SessionStore.save(session); } private async switchSession(id: string) { this.setStreaming(false); - if (this.composer) { - this.messageHistory[this.sessionId] = this.composer.getHistory(); - } await this.saveSession(); const session = await SessionStore.load(id); if (!session) { return; } this.sessionId = session.id; if (this.composer) { this.composer.clearHistory(); - this.composer.restoreHistory(this.messageHistory[id] || []); + this.composer.restoreHistory(session.messageHistory || []); } this.sessionCreatedAt = session.createdAt || Date.now(); this.sessionTitle = session.title; @@ -402,45 +402,13 @@ export class AgentChat extends Model { } private appendPersistedToolCalls(content: string, toolCalls: Array<{name: string; arguments: Record; result?: string}>) { - const L = window.siyuan.languages; + // Only render todo_write cards, skip other tool calls for (let i = 0; i < toolCalls.length; i++) { const tc = toolCalls[i]; - const argsStr = JSON.stringify(tc.arguments, null, 2); - const el = document.createElement("div"); - el.className = "agent-chat__msg agent-chat__msg--tool"; - el.innerHTML = '
' + - '
' + - '🛠' + - '' + (L.agentToolCall || "Calling tool") + ": " + tc.name + "" + - "
" + - '
' + this.escapeHtml(argsStr) + "
" + -"
"; - const header = el.querySelector(".agent-chat__tool-header") as HTMLElement; - const body = el.querySelector(".agent-chat__tool-body") as HTMLElement; - header.addEventListener("click", function () { - body.classList.toggle("fn__none"); - }); - this.messagesContainer.appendChild(el); - - if (tc.result) { + if (tc.result && tc.name === "todo_write") { const rel = document.createElement("div"); rel.className = "agent-chat__msg agent-chat__msg--tool"; - if (tc.name === "todo_write") { - rel.innerHTML = this.renderTodoList(tc.result); - } else { - rel.innerHTML = '
' + - '
' + - '📄' + - '' + (L.agentToolResult || "Tool result") + ": " + tc.name + "" + - "
" + - '
' + this.escapeHtml(tc.result) + "
" + -"
"; - const rheader = rel.querySelector(".agent-chat__tool-header") as HTMLElement; - const rbody = rel.querySelector(".agent-chat__tool-body") as HTMLElement; - rheader.addEventListener("click", function () { - rbody.classList.toggle("fn__none"); - }); - } + rel.innerHTML = this.renderTodoList(tc.result); this.messagesContainer.appendChild(rel); } } @@ -458,6 +426,7 @@ export class AgentChat extends Model { await this.saveSession(); this.sessionId = SessionStore.newSessionId(); this.sessionCreatedAt = Date.now(); + if (this.composer) { this.composer.clearHistory(); } this.sessionTitle = this.defaultTitle; this.messages = []; this.hasTitled = false; @@ -668,47 +637,15 @@ export class AgentChat extends Model { } private appendToolCall(name: string, args: Record) { - const el = document.createElement("div"); - el.className = "agent-chat__msg agent-chat__msg--tool"; - const argsStr = JSON.stringify(args, null, 2); - const L = window.siyuan.languages; - el.innerHTML = '
' + - '
' + - '🛠' + - '' + (L.agentToolCall || "Calling tool") + ": " + name + "" + - "
" + - '
' + this.escapeHtml(argsStr) + "
" + -"
"; - const header = el.querySelector(".agent-chat__tool-header") as HTMLElement; - const body = el.querySelector(".agent-chat__tool-body") as HTMLElement; - header.addEventListener("click", function () { - body.classList.toggle("fn__none"); - }); - this.insertBeforeAI(el); - this.scrollToBottom(); + // Tool calls are tracked in currentToolCalls, no DOM needed } private appendToolResult(name: string, result: string) { + if (name !== "todo_write") { return; } + const el = document.createElement("div"); el.className = "agent-chat__msg agent-chat__msg--tool"; - const L = window.siyuan.languages; - - if (name === "todo_write") { - el.innerHTML = this.renderTodoList(result); - } else { - el.innerHTML = '
' + - '
' + - '📄' + - '' + (L.agentToolResult || "Tool result") + ": " + name + "" + - "
" + - '
' + this.escapeHtml(result) + "
" + -"
"; - const header = el.querySelector(".agent-chat__tool-header") as HTMLElement; - const body = el.querySelector(".agent-chat__tool-body") as HTMLElement; - header.addEventListener("click", function () { - body.classList.toggle("fn__none"); - }); - } + el.innerHTML = this.renderTodoList(result); this.insertBeforeAI(el); this.scrollToBottom(); } @@ -738,6 +675,7 @@ export class AgentChat extends Model { } private appendThinking(reasoning: string) { + this.clearThinking(); const L = window.siyuan.languages; let text = reasoning; let roundLabel = ""; @@ -750,16 +688,12 @@ export class AgentChat extends Model { } let detailLines = ""; - if (reasoning === "processing") { - const toolCards = this.messagesContainer.querySelectorAll(".agent-chat__tool-card--call"); - if (toolCards.length > 0) { - detailLines += '
' + (L.agentToolCall || "Tool call") + "s:
"; - for (let i = 0; i < toolCards.length; i++) { - const name = toolCards[i].getAttribute("data-tool") || ""; - if (name) { - detailLines += '
' + this.escapeHtml(name) + "
"; - } - } + if (reasoning === "processing" && this.currentToolCalls.length > 0) { + detailLines += '
' + (L.agentToolCall || "Tool call") + "s:
"; + for (let i = 0; i < this.currentToolCalls.length; i++) { + const tc = this.currentToolCalls[i]; + const name = tc.name + (tc.result ? " \u2713" : " \u25CC"); + detailLines += '
' + this.escapeHtml(name) + "
"; } } diff --git a/app/src/layout/dock/SessionStore.ts b/app/src/layout/dock/SessionStore.ts index f707432b4..6695c3d32 100644 --- a/app/src/layout/dock/SessionStore.ts +++ b/app/src/layout/dock/SessionStore.ts @@ -25,6 +25,7 @@ export interface AgentSession { promptTokens?: number; completionTokens?: number; totalDuration?: number; + messageHistory?: string[]; createdAt: number; updatedAt: number; }