Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:sparkles: AI Agent https://github.com/siyuan-note/siyuan/issues/17797

Daniel9ab5e9a9ae27
diff --git a/app/package.json b/app/package.json
index 0aee1d2d68..35f420e06a 100644
--- a/app/package.json
+++ b/app/package.json
@@ -78,10 +78,7 @@
"typescript": "^4.7.4",
"webpack": "^5.104.1",
"webpack-bundle-analyzer": "^4.5.0",
- "webpack-cli": "^4.10.0"
- },
- "dependencies": {
- "@electron/remote": "^2.1.3",
+ "webpack-cli": "^4.10.0",
"@tiptap/core": "^3.24.0",
"@tiptap/extension-document": "^3.24.0",
"@tiptap/extension-hard-break": "^3.24.0",
@@ -92,5 +89,8 @@
"@tiptap/extension-text": "^3.24.0",
"@tiptap/pm": "^3.24.0",
"@tiptap/suggestion": "^3.24.0"
+ },
+ "dependencies": {
+ "@electron/remote": "^2.1.3"
}
}
diff --git a/app/pnpm-lock.yaml b/app/pnpm-lock.yaml
index 0b5bed7581..62b4973542 100644
--- a/app/pnpm-lock.yaml
+++ b/app/pnpm-lock.yaml
@@ -11,6 +11,13 @@ importers:
'@electron/remote':
specifier: ^2.1.3
version: 2.1.3([email protected])
+ devDependencies:
+ '@eslint/eslintrc':
+ specifier: ^3.3.1
+ version: 3.3.5
+ '@eslint/js':
+ specifier: ^9.24.0
+ version: 9.39.4
'@tiptap/core':
specifier: ^3.24.0
version: 3.24.0(@tiptap/[email protected])
@@ -41,13 +48,6 @@ importers:
'@tiptap/suggestion':
specifier: ^3.24.0
version: 3.24.0(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
- devDependencies:
- '@eslint/eslintrc':
- specifier: ^3.3.1
- version: 3.3.5
- '@eslint/js':
- specifier: ^9.24.0
- version: 9.39.4
'@types/node':
specifier: ^18.13.0
version: 18.13.0
diff --git a/app/src/assets/scss/business/_agentChat.scss b/app/src/assets/scss/business/_agentChat.scss
index 72d76924f7..bb2e4f35bd 100644
--- a/app/src/assets/scss/business/_agentChat.scss
+++ b/app/src/assets/scss/business/_agentChat.scss
@@ -353,6 +353,14 @@
background: var(--b3-theme-background);
}
+.agent-chat__msg--thinking-done {
+ opacity: 0.6;
+
+ .agent-chat__thinking-text {
+ font-style: italic;
+ }
+}
+
.agent-chat__thinking-header {
display: flex;
align-items: center;
@@ -383,9 +391,13 @@
}
.agent-chat__thinking-arrow {
- font-size: 10px;
- color: var(--b3-theme-on-surface-light);
flex-shrink: 0;
+ color: var(--b3-theme-primary);
+
+ svg {
+ width: 14px;
+ height: 14px;
+ }
}
.agent-chat__thinking-body {
diff --git a/app/src/layout/dock/AgentChat.ts b/app/src/layout/dock/AgentChat.ts
index 2e3f9f643e..4f7e6d93c6 100644
--- a/app/src/layout/dock/AgentChat.ts
+++ b/app/src/layout/dock/AgentChat.ts
@@ -47,6 +47,10 @@ export class AgentChat extends Model {
private currentToolCalls: Array<{name: string; arguments: Record<string, unknown>; result?: string}> = [];
private abortController: AbortController | null = null;
private isRenderingSessionList = false;
+ private thinkingSteps: Array<{reasoning: string; text: string; toolCalls: Array<{name: string; result?: string}>; reasoningContent: string}> = [];
+ private currentThinkingText = "";
+ private currentThinkingReasoning = "";
+ private currentThinkingReasoningContent = "";
constructor(app: App, tab: Tab) {
super({app: app});
@@ -188,12 +192,17 @@ export class AgentChat extends Model {
this.sessionTotalDuration = session.totalDuration || 0;
if (this.composer) {
this.composer.restoreHistory(session.messageHistory || []);
- console.log("[AgentChat] loaded history:", (session.messageHistory || []).length, "items, composer has:", this.composer.getHistory().length);
}
this.titleElement.textContent = session.title;
this.updateTokenDisplay();
+ let thinkingIdx = 0;
+ const steps = session.thinkingSteps || [];
for (let i = 0; i < session.messages.length; i++) {
const msg = session.messages[i];
+ if (msg.role === "assistant" && msg.toolCalls && msg.toolCalls.length > 0 && thinkingIdx < steps.length) {
+ this.renderSingleThinkingCard(steps[thinkingIdx]);
+ thinkingIdx++;
+ }
if (msg.role === "user") {
this.appendUserMessage(msg.content);
} else if (msg.role === "assistant") {
@@ -350,6 +359,7 @@ export class AgentChat extends Model {
createdAt: this.sessionCreatedAt,
updatedAt: Date.now(),
messageHistory: this.composer?.getHistory() || [],
+ thinkingSteps: this.thinkingSteps,
};
await SessionStore.save(session);
}
@@ -379,8 +389,14 @@ export class AgentChat extends Model {
}
this.messagesContainer.innerHTML = "";
this.titleElement.textContent = session.title;
+ let thinkingIdx = 0;
+ const steps = session.thinkingSteps || [];
for (let i = 0; i < session.messages.length; i++) {
const msg = session.messages[i];
+ if (msg.role === "assistant" && msg.toolCalls && msg.toolCalls.length > 0 && thinkingIdx < steps.length) {
+ this.renderSingleThinkingCard(steps[thinkingIdx]);
+ thinkingIdx++;
+ }
if (msg.role === "user") {
this.appendUserMessage(msg.content);
} else if (msg.role === "assistant") {
@@ -434,6 +450,7 @@ export class AgentChat extends Model {
this.currentAIElement = null;
this.currentContent = "";
this.fullContent = "";
+ this.thinkingSteps = [];
this.sessionPromptTokens = 0;
this.sessionCompletionTokens = 0;
this.sessionTotalDuration = 0;
@@ -610,7 +627,7 @@ export class AgentChat extends Model {
private appendToken(token: string) {
if (!this.currentAIElement) {
- this.clearThinking();
+ this.finishActiveThinking();
this.currentAIElement = this.createAIMessagePlaceholder();
}
this.currentContent += token;
@@ -680,7 +697,10 @@ export class AgentChat extends Model {
}
private appendThinking(reasoning: string) {
- this.clearThinking();
+ this.finishActiveThinking();
+ this.currentThinkingText = "";
+ this.currentThinkingReasoning = reasoning;
+ this.currentThinkingReasoningContent = "";
const L = window.siyuan.languages;
let text = reasoning;
let roundLabel = "";
@@ -692,6 +712,8 @@ export class AgentChat extends Model {
roundLabel = "Continuing...";
}
+ this.currentThinkingText = text;
+
let detailLines = "";
if (reasoning === "processing" && this.currentToolCalls.length > 0) {
detailLines += '<div class="agent-chat__thinking-summary">' + (L.agentToolCall || "Tool call") + "s:</div>";
@@ -713,7 +735,10 @@ export class AgentChat extends Model {
'<div class="agent-chat__thinking-header">' +
'<span class="agent-chat__thinking-dot"></span>' +
'<span class="agent-chat__thinking-text">' + this.escapeHtml(text) + "</span>" +
- '<span class="agent-chat__thinking-arrow">&#9662;</span>' +
+ '<span class="agent-chat__thinking-arrow">' +
+ '<svg class="agent-chat__thinking-arrow--expand"><use xlink:href="#iconExpand"></use></svg>' +
+ '<svg class="agent-chat__thinking-arrow--contract fn__none"><use xlink:href="#iconContract"></use></svg>' +
+ "</span>" +
"</div>" +
bodyHTML +
"</div>";
@@ -729,11 +754,13 @@ export class AgentChat extends Model {
const header = el.querySelector(".agent-chat__thinking-header") as HTMLElement;
const body = el.querySelector(".agent-chat__thinking-body") as HTMLElement;
- const arrow = el.querySelector(".agent-chat__thinking-arrow") as HTMLElement;
+ const expandIcon = el.querySelector(".agent-chat__thinking-arrow--expand") as HTMLElement;
+ const contractIcon = el.querySelector(".agent-chat__thinking-arrow--contract") as HTMLElement;
header.addEventListener("click", function () {
body.classList.toggle("fn__none");
const isHidden = body.classList.contains("fn__none");
- arrow.innerHTML = isHidden ? "&#9662;" : "&#9652;";
+ expandIcon.classList.toggle("fn__none", !isHidden);
+ contractIcon.classList.toggle("fn__none", isHidden);
});
this.insertBeforeAI(el);
this.scrollToBottom();
@@ -840,12 +867,20 @@ export class AgentChat extends Model {
return;
}
this.flushTokenUpdate();
- this.clearThinking();
if (!this.currentContent) {
this.currentAIElement.remove();
this.currentAIElement = null;
this.currentContent = "";
this.fullContent = "";
+ if (this.currentThinkingText) {
+ const tc = this.currentToolCalls.map(function (t) { return {name: t.name, result: t.result}; });
+ this.thinkingSteps.push({
+ reasoning: this.currentThinkingReasoning,
+ text: this.currentThinkingText,
+ toolCalls: tc,
+ reasoningContent: this.currentThinkingReasoningContent,
+ });
+ }
this.currentToolCalls = [];
if (this.requestStartTime) {
this.sessionTotalDuration += Date.now() - this.requestStartTime;
@@ -853,7 +888,6 @@ export class AgentChat extends Model {
}
this.updateTokenDisplay();
this.setStreaming(false);
- // line 754 area
if (!this.hasTitled && this.messages.length >= 2) {
this.hasTitled = true;
this.generateTitle();
@@ -870,6 +904,15 @@ export class AgentChat extends Model {
this.currentAIElement = null;
this.currentContent = "";
this.fullContent = "";
+ if (this.currentThinkingText) {
+ const tc = this.currentToolCalls.map(function (t) { return {name: t.name, result: t.result}; });
+ this.thinkingSteps.push({
+ reasoning: this.currentThinkingReasoning,
+ text: this.currentThinkingText,
+ toolCalls: tc,
+ reasoningContent: this.currentThinkingReasoningContent,
+ });
+ }
this.currentToolCalls = [];
if (this.requestStartTime) {
this.sessionTotalDuration += Date.now() - this.requestStartTime;
@@ -1128,6 +1171,49 @@ export class AgentChat extends Model {
}
}
+ private renderSingleThinkingCard(step: {reasoning: string; text: string; toolCalls: Array<{name: string; result?: string}>; reasoningContent: string}) {
+ let detail = "";
+ if (step.toolCalls.length > 0) {
+ detail += '<div class="agent-chat__thinking-summary">Tool calls:</div>';
+ for (let j = 0; j < step.toolCalls.length; j++) {
+ const tc = step.toolCalls[j];
+ detail += '<div class="agent-chat__thinking-item">' + this.escapeHtml(tc.name + (tc.result ? " \u2713" : " \u25CC")) + "</div>";
+ }
+ }
+ if (step.reasoningContent) {
+ detail += '<div class="agent-chat__thinking-round">Reasoning:</div>';
+ detail += '<div>' + this.escapeHtml(step.reasoningContent) + "</div>";
+ }
+
+ const el = document.createElement("div");
+ el.className = "agent-chat__msg agent-chat__msg--thinking agent-chat__msg--thinking-done";
+ el.innerHTML = '<div class="agent-chat__thinking-card">' +
+ '<div class="agent-chat__thinking-header">' +
+ '<span class="agent-chat__thinking-dot fn__none"></span>' +
+ '<span class="agent-chat__thinking-text">' + this.escapeHtml(step.text) + "</span>" +
+ '<span class="agent-chat__thinking-arrow">' +
+ '<svg class="agent-chat__thinking-arrow--expand"><use xlink:href="#iconExpand"></use></svg>' +
+ '<svg class="agent-chat__thinking-arrow--contract fn__none"><use xlink:href="#iconContract"></use></svg>' +
+ "</span>" +
+ "</div>" +
+ '<div class="agent-chat__thinking-body fn__none">' +
+ detail +
+ "</div>" +
+"</div>";
+
+ const header = el.querySelector(".agent-chat__thinking-header") as HTMLElement;
+ const body = el.querySelector(".agent-chat__thinking-body") as HTMLElement;
+ const expandIcon = el.querySelector(".agent-chat__thinking-arrow--expand") as HTMLElement;
+ const contractIcon = el.querySelector(".agent-chat__thinking-arrow--contract") as HTMLElement;
+ header.addEventListener("click", function () {
+ body.classList.toggle("fn__none");
+ const isHidden = body.classList.contains("fn__none");
+ expandIcon.classList.toggle("fn__none", !isHidden);
+ contractIcon.classList.toggle("fn__none", isHidden);
+ });
+ this.messagesContainer.appendChild(el);
+ }
+
private updateTokenDisplay() {
if (!this.tokenDisplayEl) {
return;
@@ -1160,6 +1246,22 @@ export class AgentChat extends Model {
}
}
+ private finishActiveThinking() {
+ const items = this.messagesContainer.querySelectorAll(".agent-chat__msg--thinking");
+ for (let i = 0; i < items.length; i++) {
+ const el = items[i] as HTMLElement;
+ el.classList.add("agent-chat__msg--thinking-done");
+ const dot = el.querySelector(".agent-chat__thinking-dot");
+ if (dot) { dot.classList.add("fn__none"); }
+ const textEl = el.querySelector(".agent-chat__thinking-text");
+ if (textEl) {
+ const raw = textEl.textContent || "";
+ if (raw.indexOf("分析") >= 0) { textEl.textContent = raw.replace("正在分析", "已分析"); }
+ else if (raw.indexOf("处理") >= 0) { textEl.textContent = raw.replace("正在处理", "已处理"); }
+ }
+ }
+ }
+
private setStreaming(streaming: boolean) {
this.isStreaming = streaming;
this.sendBtn.classList.toggle("fn__none", streaming);
diff --git a/app/src/layout/dock/SessionStore.ts b/app/src/layout/dock/SessionStore.ts
index 6695c3d32d..29f6dea0d8 100644
--- a/app/src/layout/dock/SessionStore.ts
+++ b/app/src/layout/dock/SessionStore.ts
@@ -26,6 +26,12 @@ export interface AgentSession {
completionTokens?: number;
totalDuration?: number;
messageHistory?: string[];
+ thinkingSteps?: Array<{
+ reasoning: string;
+ text: string;
+ toolCalls: Array<{name: string; result?: string}>;
+ reasoningContent: string;
+ }>;
createdAt: number;
updatedAt: number;
}