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
Danielf4acec573b2c
diff --git a/app/src/layout/dock/AgentChat.ts b/app/src/layout/dock/AgentChat.tsindex 394607ef39..c551714664 100644--- a/app/src/layout/dock/AgentChat.ts+++ b/app/src/layout/dock/AgentChat.ts@@ -186,6 +186,7 @@ export class AgentChat extends Model {this.sessionCreatedAt = session.createdAt || Date.now();this.sessionTitle = session.title;this.messages = session.messages as IAgentMessage[];+ this.thinkingSteps = session.thinkingSteps || [];this.hasTitled = true;this.sessionPromptTokens = session.promptTokens || 0;this.sessionCompletionTokens = session.completionTokens || 0;@@ -195,24 +196,7 @@ export class AgentChat extends Model {}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") {- if (msg.toolCalls && msg.toolCalls.length > 0) {- this.appendPersistedToolCalls(msg.content, msg.toolCalls);- } else {- this.appendPersistedAssistant(msg.content);- }- }- }+ this.renderLoadedSession(session);this.scrollToBottom();return;}@@ -377,6 +361,7 @@ export class AgentChat extends Model {this.sessionCreatedAt = session.createdAt || Date.now();this.sessionTitle = session.title;this.messages = session.messages as IAgentMessage[];+ this.thinkingSteps = session.thinkingSteps || [];this.hasTitled = true;this.currentAIElement = null;this.currentContent = "";@@ -389,24 +374,7 @@ 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") {- if (msg.toolCalls && msg.toolCalls.length > 0) {- this.appendPersistedToolCalls(msg.content, msg.toolCalls);- } else {- this.appendPersistedAssistant(msg.content);- }- }- }+ this.renderLoadedSession(session);this.scrollToBottom();}@@ -434,6 +402,49 @@ export class AgentChat extends Model {}}+ private renderLoadedSession(session: AgentSession) {+ const messages = session.messages as IAgentMessage[];+ const steps = session.thinkingSteps || [];+ const groups: Array<Array<typeof steps[0]>> = [];+ let cur: Array<typeof steps[0]> = [];+ for (let s = 0; s < steps.length; s++) {+ const step = steps[s];+ if (step.reasoning === "analyzing" && cur.length > 0) {+ groups.push(cur);+ cur = [];+ }+ cur.push(step);+ }+ if (cur.length > 0) {+ groups.push(cur);+ }+ let groupIdx = 0;+ for (let i = 0; i < messages.length; i++) {+ const msg = messages[i];+ if (msg.role === "user") {+ this.appendUserMessage(msg.content);+ } else if (msg.role === "assistant") {+ if (groupIdx < groups.length) {+ for (let s = 0; s < groups[groupIdx].length; s++) {+ this.renderSingleThinkingCard(groups[groupIdx][s]);+ }+ groupIdx++;+ }+ if (msg.toolCalls && msg.toolCalls.length > 0) {+ this.appendPersistedToolCalls(msg.content, msg.toolCalls);+ } else {+ this.appendPersistedAssistant(msg.content);+ }+ }+ }+ while (groupIdx < groups.length) {+ for (let s = 0; s < groups[groupIdx].length; s++) {+ this.renderSingleThinkingCard(groups[groupIdx][s]);+ }+ groupIdx++;+ }+ }+private async createSession() {if (this.abortController) {this.abortController.abort();@@ -523,6 +534,11 @@ export class AgentChat extends Model {this.appendUserMessage(text);if (this.composer) { this.composer.pushHistory(text); }+ if (!this.hasTitled && this.messages.length === 1) {+ this.hasTitled = true;+ this.generateTitle();+ }+this.requestStartTime = Date.now();const apiMessages = this.messages.map(function (m) { return {role: m.role as "user" | "assistant", content: m.content}; });@@ -872,6 +888,20 @@ export class AgentChat extends Model {private finishResponse() {if (!this.currentAIElement) {+ if (this.currentToolCalls.length === 0) {+ this.setStreaming(false);+ return;+ }+ this.flushThinkingStep();+ this.messages.push({role: "assistant", content: "", toolCalls: this.currentToolCalls.slice()});+ this.currentToolCalls = [];+ if (this.requestStartTime) {+ this.sessionTotalDuration += Date.now() - this.requestStartTime;+ this.requestStartTime = 0;+ }+ this.updateTokenDisplay();+ this.setStreaming(false);+ this.saveSession();return;}this.flushTokenUpdate();@@ -880,6 +910,10 @@ export class AgentChat extends Model {this.currentAIElement = null;this.currentContent = "";this.fullContent = "";+ if (this.currentToolCalls.length > 0) {+ this.flushThinkingStep();+ this.messages.push({role: "assistant", content: "", toolCalls: this.currentToolCalls.slice()});+ }this.currentToolCalls = [];if (this.requestStartTime) {this.sessionTotalDuration += Date.now() - this.requestStartTime;@@ -887,10 +921,6 @@ export class AgentChat extends Model {}this.updateTokenDisplay();this.setStreaming(false);- if (!this.hasTitled && this.messages.length >= 2) {- this.hasTitled = true;- this.generateTitle();- }this.saveSession();return;}@@ -903,15 +933,7 @@ 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.flushThinkingStep();this.currentToolCalls = [];if (this.requestStartTime) {this.sessionTotalDuration += Date.now() - this.requestStartTime;@@ -920,13 +942,23 @@ export class AgentChat extends Model {this.updateTokenDisplay();this.setStreaming(false);- if (!this.hasTitled && this.messages.length >= 2) {- this.hasTitled = true;- this.generateTitle();- }this.saveSession();}+ private flushThinkingStep() {+ if (!this.currentThinkingText) {+ return;+ }+ 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.currentThinkingText = "";+ }+private generateTitle() {const firstMsg = this.messages[0].content.slice(0, 500);const self = this;diff --git a/kernel/agent/agent.go b/kernel/agent/agent.goindex eed5e211ed..eaa18466c9 100644--- a/kernel/agent/agent.go+++ b/kernel/agent/agent.go@@ -159,15 +159,29 @@ type Reference struct {Title string `json:"title"`}+type checkpointThinkingStep struct {+ Reasoning string `json:"reasoning"`+ Text string `json:"text"`+ ToolCalls []checkpointBriefTC `json:"toolCalls"`+ ReasoningContent string `json:"reasoningContent"`+}++type checkpointBriefTC struct {+ Name string `json:"name"`+ Result string `json:"result,omitempty"`+}+type agentCheckpoint struct {- ID string `json:"id"`- Title string `json:"title"`- Messages []AgentMessage `json:"messages"`- PromptTokens int `json:"promptTokens"`- CompletionTokens int `json:"completionTokens"`- TotalDuration int64 `json:"totalDuration"`- CreatedAt int64 `json:"createdAt"`- UpdatedAt int64 `json:"updatedAt"`+ ID string `json:"id"`+ Title string `json:"title"`+ Messages []AgentMessage `json:"messages"`+ PromptTokens int `json:"promptTokens"`+ CompletionTokens int `json:"completionTokens"`+ TotalDuration int64 `json:"totalDuration"`+ CreatedAt int64 `json:"createdAt"`+ UpdatedAt int64 `json:"updatedAt"`+ MessageHistory []string `json:"messageHistory,omitempty"`+ ThinkingSteps []checkpointThinkingStep `json:"thinkingSteps,omitempty"`}func AgentChat(ctx context.Context, client *openai.Client, model string, sessionID string, history []UserMessage, language string, references []Reference, confirmTimeout time.Duration, maxRetries int) <-chan AgentEvent {@@ -656,6 +670,12 @@ func saveCheckpoint(sessionID string, messages []AgentMessage, promptTokens int,if old.CreatedAt > 0 {cp.CreatedAt = old.CreatedAt}+ if len(old.MessageHistory) > 0 {+ cp.MessageHistory = old.MessageHistory+ }+ if len(old.ThinkingSteps) > 0 {+ cp.ThinkingSteps = old.ThinkingSteps+ }}data, err := json.MarshalIndent(cp, "", " ")