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

Daniel97b72ad9c70a
diff --git a/app/src/layout/dock/AgentChat.ts b/app/src/layout/dock/AgentChat.ts
index 604468047e..db33405a0b 100644
--- a/app/src/layout/dock/AgentChat.ts
+++ b/app/src/layout/dock/AgentChat.ts
@@ -623,7 +623,7 @@ export class AgentChat extends Model {
if (line.startsWith("✅")) {
html += '<div class="agent-chat__todo-item agent-chat__todo-item--completed"><span class="agent-chat__todo-status">✅</span>' + this.escapeHtml(line.substring(1).trim()) + "</div>";
} else if (line.startsWith("🔄")) {
- html += '<div class="agent-chat__todo-item agent-chat__todo-item--in-progress"><span class="agent-chat__todo-status">🔄</span>' + this.escapeHtml(line.substring(1).trim()) + "</div>";
+ html += '<div class="agent-chat__todo-item agent-chat__todo-item--in-progress"><span class="agent-chat__todo-status">🔄</span>' + this.escapeHtml(line.slice(2).trim()) + "</div>";
} else if (line.startsWith("❌")) {
html += '<div class="agent-chat__todo-item agent-chat__todo-item--cancelled"><span class="agent-chat__todo-status">❌</span>' + this.escapeHtml(line.substring(1).trim()) + "</div>";
} else if (line.startsWith("○")) {
@@ -847,6 +847,7 @@ export class AgentChat extends Model {
'<div class="agent-chat__confirm-actions">' +
'<button class="b3-button b3-button--cancel agent-chat__confirm-reject">' + (L.agentConfirmReject || "Reject") + "</button>" +
'<button class="b3-button b3-button--text agent-chat__confirm-approve">' + (L.agentConfirmApprove || "Approve") + "</button>" +
+ '<button class="b3-button b3-button--text agent-chat__confirm-always">' + (L.agentConfirmAlways || "Session Allow") + "</button>" +
"</div>" +
"</div>";
const approveBtn = el.querySelector(".agent-chat__confirm-approve");
@@ -863,15 +864,26 @@ export class AgentChat extends Model {
if (btns) { btns.innerHTML = '<span class="agent-chat__confirm-done">' + (L.agentConfirmReject || "Rejected") + "</span>"; }
self.postConfirm(confirmID, false);
}); }
+ const alwaysBtn = el.querySelector(".agent-chat__confirm-always");
+ if (alwaysBtn) { alwaysBtn.addEventListener("click", function () {
+ el.classList.add("agent-chat__msg--confirmed");
+ const btns = el.querySelector(".agent-chat__confirm-actions") as HTMLElement;
+ if (btns) { btns.innerHTML = '<span class="agent-chat__confirm-done">' + (L.agentConfirmAlways || "Session Allow") + "</span>"; }
+ self.postConfirm(confirmID, true, true);
+ }); }
this.insertBeforeAI(el);
this.scrollToBottom();
}
- private postConfirm(confirmID: string, approved: boolean) {
+ private postConfirm(confirmID: string, approved: boolean, always?: boolean) {
+ const body: Record<string, unknown> = {confirmID: confirmID, approved: approved};
+ if (always) {
+ body.always = true;
+ }
fetch("/api/ai/agent/confirm", {
method: "POST",
headers: {"Content-Type": "application/json"},
- body: JSON.stringify({confirmID: confirmID, approved: approved}),
+ body: JSON.stringify(body),
});
}
diff --git a/kernel/agent/agent.go b/kernel/agent/agent.go
index 2ec9b37f23..07e370ed98 100644
--- a/kernel/agent/agent.go
+++ b/kernel/agent/agent.go
@@ -68,12 +68,17 @@ const systemPrompt = `You are a SiYuan AI assistant. You help users manage their
const maxToolCallRounds = 64
-var confirmChannels = make(map[string]chan bool)
+type confirmResult struct {
+ approved bool
+ always bool
+}
+
+var confirmChannels = make(map[string]chan confirmResult)
-func ConfirmSession(id string, approved bool) {
+func ConfirmSession(id string, approved bool, always bool) {
ch, ok := confirmChannels[id]
if ok {
- ch <- approved
+ ch <- confirmResult{approved: approved, always: always}
}
}
@@ -135,6 +140,7 @@ func AgentChat(ctx context.Context, client *openai.Client, model string, session
messages := buildMessages(history, language, references)
var totalPrompt, totalCompletion int
startTime := time.Now().UnixMilli()
+ alwaysAllow := map[string]bool{}
checkpointMsgs := make([]AgentMessage, 0, len(history))
for _, h := range history {
@@ -254,17 +260,17 @@ func AgentChat(ctx context.Context, client *openai.Client, model string, session
Arguments: args,
}
- if needsConfirm(tc.Function.Name, action) {
+ if needsConfirm(tc.Function.Name, action, alwaysAllow) {
confirmID := tc.ID
ch <- AgentEvent{Type: "confirm", Name: tc.Function.Name, Arguments: args, ConfirmID: confirmID}
- ch2 := make(chan bool, 1)
+ ch2 := make(chan confirmResult, 1)
confirmChannels[confirmID] = ch2
- var confirmResult string
- var approved bool
+ var rejectionMsg string
+ var result confirmResult
timedOut := false
select {
- case approved = <-ch2:
+ case result = <-ch2:
delete(confirmChannels, confirmID)
case <-ctx.Done():
delete(confirmChannels, confirmID)
@@ -272,36 +278,40 @@ func AgentChat(ctx context.Context, client *openai.Client, model string, session
case <-time.After(confirmTimeout):
delete(confirmChannels, confirmID)
timedOut = true
- confirmResult = "Confirmation timed out, operation skipped automatically"
- ch <- AgentEvent{Type: "tool_result", Name: tc.Function.Name, Result: confirmResult}
+ rejectionMsg = "Confirmation timed out, operation skipped automatically"
+ ch <- AgentEvent{Type: "tool_result", Name: tc.Function.Name, Result: rejectionMsg}
}
- if timedOut || !approved {
- if confirmResult == "" {
- confirmResult = "User rejected this operation"
- ch <- AgentEvent{Type: "tool_result", Name: tc.Function.Name, Result: confirmResult}
+ if timedOut || !result.approved {
+ if rejectionMsg == "" {
+ rejectionMsg = "User rejected this operation"
+ ch <- AgentEvent{Type: "tool_result", Name: tc.Function.Name, Result: rejectionMsg}
}
messages = append(messages, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleTool,
- Content: confirmResult,
+ Content: rejectionMsg,
ToolCallID: tc.ID,
})
- checkpointMsgs[assistantIdx].ToolCalls[i].Result = confirmResult
+ checkpointMsgs[assistantIdx].ToolCalls[i].Result = rejectionMsg
saveCheckpoint(sessionID, checkpointMsgs, totalPrompt, totalCompletion, startTime)
continue
}
+
+ if result.always {
+ alwaysAllow[tc.Function.Name+"::"+action] = true
+ }
}
setCurrentTodoSession(sessionID)
result := executeTool(tc)
ch <- AgentEvent{
- Type: "tool_result",
- Name: tc.Function.Name,
- Result: result,
- }
+ Type: "tool_result",
+ Name: tc.Function.Name,
+ Result: result,
+ }
- messages = append(messages, openai.ChatCompletionMessage{
+ messages = append(messages, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleTool,
Content: result,
ToolCallID: tc.ID,
@@ -375,10 +385,13 @@ var safeActions = map[string]bool{
"current_time": true, "workspace": true, "md": true, "query": true,
}
-func needsConfirm(toolName string, action string) bool {
+func needsConfirm(toolName string, action string, alwaysAllow map[string]bool) bool {
if action == "" {
return false
}
+ if alwaysAllow[toolName+"::"+action] {
+ return false
+ }
if safeActions[action] {
return false
}
diff --git a/kernel/api/agent.go b/kernel/api/agent.go
index 6e56bcca8d..4c9aee1b1e 100644
--- a/kernel/api/agent.go
+++ b/kernel/api/agent.go
@@ -128,6 +128,7 @@ func agentChat(c *gin.Context) {
type agentConfirmReq struct {
ConfirmID string `json:"confirmID"`
Approved bool `json:"approved"`
+ Always bool `json:"always"`
}
func agentChatConfirm(c *gin.Context) {
@@ -139,7 +140,7 @@ func agentChatConfirm(c *gin.Context) {
c.JSON(http.StatusOK, ret)
return
}
- agent.ConfirmSession(req.ConfirmID, req.Approved)
+ agent.ConfirmSession(req.ConfirmID, req.Approved, req.Always)
ret := gulu.Ret.NewResult()
c.JSON(http.StatusOK, ret)
}