Skip to content

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

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

Danielcf9c9307fe54
diff --git a/app/src/assets/scss/business/_agentChat.scss b/app/src/assets/scss/business/_agentChat.scss
index 3d7faed329..1752e3522e 100644
--- a/app/src/assets/scss/business/_agentChat.scss
+++ b/app/src/assets/scss/business/_agentChat.scss
@@ -6,6 +6,17 @@
color: var(--b3-theme-on-background);
}
+.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;
+}
+
.agent-chat__messages {
overflow-y: auto;
padding: 12px;
@@ -97,6 +108,124 @@
max-width: 85%;
word-break: break-word;
line-height: 1.5;
+
+ ol, ul {
+ padding-left: 1.5em;
+ }
+
+ pre, table {
+ max-width: 100%;
+ overflow-x: auto;
+ }
+
+ blockquote {
+ position: relative;
+ margin: 0;
+ padding-left: calc(0.25em + 6px);
+ color: var(--b3-theme-on-surface);
+ background-color: var(--b3-bq-background);
+ border-radius: var(--b3-border-radius);
+
+ &::before {
+ content: '';
+ background-color: var(--b3-theme-surface-lighter);
+ width: .25em;
+ border-radius: var(--b3-border-radius);
+ position: absolute;
+ left: 2px;
+ top: 4px;
+ bottom: 4px;
+ }
+ }
+
+ code:not(.hljs) {
+ padding: .2em .4em;
+ font-size: 85%;
+ border-radius: var(--b3-border-radius);
+ font-family: var(--b3-font-family-code);
+ background-color: var(--b3-protyle-code-background);
+ }
+
+ pre {
+ padding: 8px 12px;
+ font-size: 85%;
+ font-family: var(--b3-font-family-code);
+ border-radius: var(--b3-border-radius);
+ background-color: var(--b3-protyle-code-background);
+ }
+
+ h1, h2, h3, h4, h5, h6 {
+ font-weight: 600;
+ margin: 0.5em 0 0.2em;
+ }
+
+ h1 { font-size: 1.75em; }
+ h2 { font-size: 1.55em; }
+ h3 { font-size: 1.38em; }
+ h4 { font-size: 1.25em; }
+ h5 { font-size: 1.13em; }
+ h6 { font-size: 1em; }
+
+ table {
+ border-collapse: collapse;
+
+ td, th {
+ padding: 4px 8px;
+ border: 1px solid var(--b3-theme-surface-lighter);
+ }
+
+ thead th {
+ font-weight: 600;
+ }
+
+ tr:nth-child(even) td {
+ background-color: var(--b3-table-even-background);
+ }
+ }
+
+ a {
+ color: var(--b3-protyle-inline-link-color);
+
+ &:hover {
+ text-decoration: underline;
+ }
+ }
+
+ hr {
+ border: 0;
+ border-bottom: 1px solid var(--b3-theme-background-light);
+ margin: 0.5em 0;
+ }
+
+ .protyle-task {
+ list-style: none;
+
+ input {
+ position: absolute;
+ left: -1.5em;
+ }
+ }
+
+ .protyle-task--done {
+ color: var(--b3-theme-on-surface-light);
+ text-decoration: line-through;
+ }
+
+ > * + * {
+ margin-top: 0.5em;
+ }
+
+ & > *:last-child {
+ margin-bottom: 0;
+ }
+
+ p {
+ margin: 0 0 0.4em;
+ }
+
+ li {
+ margin-bottom: 0.2em;
+ }
}
.agent-chat__msg--ai {
@@ -111,6 +240,124 @@
max-width: 85%;
word-break: break-word;
line-height: 1.5;
+
+ ol, ul {
+ padding-left: 1.5em;
+ }
+
+ pre, table {
+ max-width: 100%;
+ overflow-x: auto;
+ }
+
+ blockquote {
+ position: relative;
+ margin: 0;
+ padding-left: calc(0.25em + 6px);
+ color: var(--b3-theme-on-surface);
+ background-color: var(--b3-bq-background);
+ border-radius: var(--b3-border-radius);
+
+ &::before {
+ content: '';
+ background-color: var(--b3-theme-surface-lighter);
+ width: .25em;
+ border-radius: var(--b3-border-radius);
+ position: absolute;
+ left: 2px;
+ top: 4px;
+ bottom: 4px;
+ }
+ }
+
+ code:not(.hljs) {
+ padding: .2em .4em;
+ font-size: 85%;
+ border-radius: var(--b3-border-radius);
+ font-family: var(--b3-font-family-code);
+ background-color: var(--b3-protyle-code-background);
+ }
+
+ pre {
+ padding: 8px 12px;
+ font-size: 85%;
+ font-family: var(--b3-font-family-code);
+ border-radius: var(--b3-border-radius);
+ background-color: var(--b3-protyle-code-background);
+ }
+
+ h1, h2, h3, h4, h5, h6 {
+ font-weight: 600;
+ margin: 0.5em 0 0.2em;
+ }
+
+ h1 { font-size: 1.75em; }
+ h2 { font-size: 1.55em; }
+ h3 { font-size: 1.38em; }
+ h4 { font-size: 1.25em; }
+ h5 { font-size: 1.13em; }
+ h6 { font-size: 1em; }
+
+ table {
+ border-collapse: collapse;
+
+ td, th {
+ padding: 4px 8px;
+ border: 1px solid var(--b3-theme-surface-lighter);
+ }
+
+ thead th {
+ font-weight: 600;
+ }
+
+ tr:nth-child(even) td {
+ background-color: var(--b3-table-even-background);
+ }
+ }
+
+ a {
+ color: var(--b3-protyle-inline-link-color);
+
+ &:hover {
+ text-decoration: underline;
+ }
+ }
+
+ hr {
+ border: 0;
+ border-bottom: 1px solid var(--b3-theme-background-light);
+ margin: 0.5em 0;
+ }
+
+ .protyle-task {
+ list-style: none;
+
+ input {
+ position: absolute;
+ left: -1.5em;
+ }
+ }
+
+ .protyle-task--done {
+ color: var(--b3-theme-on-surface-light);
+ text-decoration: line-through;
+ }
+
+ > * + * {
+ margin-top: 0.5em;
+ }
+
+ & > *:last-child {
+ margin-bottom: 0;
+ }
+
+ p {
+ margin: 0 0 0.4em;
+ }
+
+ li {
+ margin-bottom: 0.2em;
+ }
}
.agent-chat__bubble--streaming {
@@ -343,7 +590,7 @@
}
.agent-chat__input-area {
- border-top: 1px solid var(--b3-border-color);
+ background: var(--b3-theme-surface);
padding: 10px 12px;
flex-shrink: 0;
}
diff --git a/app/src/layout/dock/AgentChat.ts b/app/src/layout/dock/AgentChat.ts
index b41f05a06e..31b689fd9c 100644
--- a/app/src/layout/dock/AgentChat.ts
+++ b/app/src/layout/dock/AgentChat.ts
@@ -28,6 +28,10 @@ export class AgentChat extends Model {
private currentAIElement: HTMLElement | null = null;
private lute: Lute;
private currentContent = "";
+ private fullContent = "";
+ private sessionPromptTokens = 0;
+ private sessionCompletionTokens = 0;
+ private tokenDisplayEl: HTMLElement;
private defaultTitle = "";
constructor(app: App, tab: Tab) {
@@ -49,6 +53,7 @@ export class AgentChat extends Model {
panel.innerHTML = '<div class="agent-chat fn__flex-column fn__flex-1">' +
'<div class="block__icons">' +
'<div class="block__logo fn__flex-1 agent-chat__title">' + (L.agentChat || "Agent") + '</div>' +
+ '<span class="agent-chat__tokens fn__none"></span>' +
'<span data-type="new-session" class="block__icon ariaLabel" data-position="north" aria-label="' + (L.agentNewSession || "New Session") + '">' +
'<svg><use xlink:href="#iconAdd"></use></svg>' +
'</span>' +
@@ -78,6 +83,7 @@ export class AgentChat extends Model {
this.newSessionBtn = panel.querySelector('.block__icon[data-type="new-session"]') as HTMLElement;
this.sessionMenuBtn = panel.querySelector('.block__icon[data-type="session-menu"]') as HTMLElement;
this.titleElement = panel.querySelector(".agent-chat__title") as HTMLElement;
+ this.tokenDisplayEl = panel.querySelector(".agent-chat__tokens") as HTMLElement;
var self = this;
this.composer = mountComposer(this.composerHost, function () { self.sendMessage(); });
@@ -134,7 +140,6 @@ export class AgentChat extends Model {
var t = e.target as HTMLElement;
if (t.closest(".agent-chat__msg")) { return; }
if (t.closest(".agent-chat__header")) { return; }
- if (t.closest(".agent-chat__input-area")) { return; }
if (t.closest(".agent-session-popup")) { return; }
if (self.composer) { self.composer.focus(); }
});
@@ -309,6 +314,12 @@ export class AgentChat extends Model {
this.hasTitled = true;
this.currentAIElement = null;
this.currentContent = "";
+ this.fullContent = "";
+ this.sessionPromptTokens = 0;
+ this.sessionCompletionTokens = 0;
+ if (this.tokenDisplayEl) {
+ this.tokenDisplayEl.classList.add("fn__none");
+ }
this.messagesContainer.innerHTML = "";
this.titleElement.textContent = session.title;
for (var i = 0; i < session.messages.length; i++) {
@@ -337,6 +348,12 @@ export class AgentChat extends Model {
this.hasTitled = false;
this.currentAIElement = null;
this.currentContent = "";
+ this.fullContent = "";
+ this.sessionPromptTokens = 0;
+ this.sessionCompletionTokens = 0;
+ if (this.tokenDisplayEl) {
+ this.tokenDisplayEl.classList.add("fn__none");
+ }
this.messagesContainer.innerHTML = "";
this.titleElement.textContent = this.defaultTitle;
if (this.composer) { this.composer.clear(); }
@@ -398,8 +415,7 @@ export class AgentChat extends Model {
this.appendThinking(event.reasoning);
break;
case "tool_call":
- break;
- case "tool_result":
+ this.appendToolCall(event.name, event.arguments);
break;
case "confirm":
this.appendConfirm(event.name, event.arguments, event.confirmID);
@@ -452,6 +468,7 @@ export class AgentChat extends Model {
return;
}
this.currentContent += token;
+ this.fullContent += token;
this.clearThinking();
bubble.innerHTML = this.lute.MarkdownStr("", this.currentContent) + '<span class="agent-chat__cursor"></span>';
this.scrollToBottom();
@@ -539,6 +556,12 @@ export class AgentChat extends Model {
'</div>' +
bodyHTML +
'</div>';
+
+ if (reasoning === "processing" && this.currentAIElement && this.currentContent) {
+ this.finalizeCurrentRound();
+ this.currentAIElement = this.createAIMessagePlaceholder();
+ }
+
var header = el.querySelector(".agent-chat__thinking-header") as HTMLElement;
var body = el.querySelector(".agent-chat__thinking-body") as HTMLElement;
var arrow = el.querySelector(".agent-chat__thinking-arrow") as HTMLElement;
@@ -551,6 +574,22 @@ export class AgentChat extends Model {
this.scrollToBottom();
}
+ private finalizeCurrentRound() {
+ if (!this.currentAIElement) {
+ return;
+ }
+ 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;
+ this.currentContent = "";
+ }
+
private finishResponse() {
if (!this.currentAIElement) {
return;
@@ -564,9 +603,10 @@ export class AgentChat extends Model {
}
bubble.classList.remove("agent-chat__bubble--streaming");
}
- this.messages.push({role: "assistant", content: this.currentContent});
+ this.messages.push({role: "assistant", content: this.fullContent || " "});
this.currentAIElement = null;
this.currentContent = "";
+ this.fullContent = "";
this.setStreaming(false);
if (!this.hasTitled && this.messages.length >= 2) {
@@ -623,11 +663,12 @@ export class AgentChat extends Model {
}
bubble.classList.remove("agent-chat__bubble--streaming");
}
- if (this.currentContent) {
- this.messages.push({role: "assistant", content: this.currentContent});
+ if (this.fullContent) {
+ this.messages.push({role: "assistant", content: this.fullContent || " "});
}
this.currentAIElement = null;
this.currentContent = "";
+ this.fullContent = "";
}
this.setStreaming(false);
}
@@ -683,14 +724,13 @@ export class AgentChat extends Model {
}
private appendUsage(promptTokens: number, completionTokens: number) {
- var el = document.createElement("div");
- el.className = "agent-chat__msg agent-chat__msg--usage";
- var total = promptTokens + completionTokens;
- el.innerHTML = '<div class="agent-chat__usage">' +
- '<span>📊 ' + promptTokens + ' prompt + ' + completionTokens + ' completion = ' + total + ' tokens</span>' +
-'</div>';
- this.messagesContainer.appendChild(el);
- this.scrollToBottom();
+ 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");
+ }
}
private clearThinking() {
diff --git a/kernel/agent/agent.go b/kernel/agent/agent.go
index 8fce19fcf4..27ac87caf7 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 = 5
+const maxToolCallRounds = 30
var confirmChannels = make(map[string]chan bool)
@@ -266,7 +266,7 @@ func GenerateTitle(client *openai.Client, model string, msg string) string {
var safeActions = map[string]bool{
"get": true, "get_kramdown": true, "get_children": true, "breadcrumb": true,
"tree_stat": true,
- "list": true, "search_docs": true, "fulltext": true, "backlinks": true,
+ "list": true, "search_docs": true, "fulltext": true, "backlinks": true,
"mentions": true, "labels": true, "status": true, "version": true,
"current_time": true, "workspace": true, "md": true, "query": true,
}
@@ -297,9 +297,13 @@ func buildMessages(history []UserMessage, language string, references []Referenc
{Role: openai.ChatMessageRoleSystem, Content: prompt},
}
for _, msg := range history {
+ content := msg.Content
+ if content == "" {
+ content = " "
+ }
messages = append(messages, openai.ChatCompletionMessage{
Role: msg.Role,
- Content: msg.Content,
+ Content: content,
})
}
return messages