Skip to content

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

:bug: Update edited agent message send time https://github.com/siyuan-note/siyuan/issues/20680

Daniel91fd9d60709d
diff --git a/app/src/layout/dock/agent/AgentHistory.test.ts b/app/src/layout/dock/agent/AgentHistory.test.ts
index a4eee71444..60e580cb14 100644
--- a/app/src/layout/dock/agent/AgentHistory.test.ts
+++ b/app/src/layout/dock/agent/AgentHistory.test.ts
@@ -127,9 +127,12 @@ describe("AgentHistory", () => {
assert.equal(isAgentRegenerateStateCurrent("session-1", "session-1", 2, 2, false, true), false);
});
- it("updates rich user message data together", () => {
+ it("updates rich user message data and send time together", (t) => {
+ const timestamp = 1792300000000;
+ t.mock.method(Date, "now", () => timestamp);
const entry = {
content: "Old content",
+ timestamp: 1,
blockHTML: "<div>Old content</div>",
references: [{id: "block-1", title: "Old reference"}],
};
@@ -141,6 +144,7 @@ describe("AgentHistory", () => {
});
assert.deepEqual(entry, {
content: "| A | B |",
+ timestamp,
blockHTML: '<div data-type="NodeTable">table</div>',
references,
});
diff --git a/app/src/layout/dock/agent/AgentHistory.ts b/app/src/layout/dock/agent/AgentHistory.ts
index f0930b75ff..080e3e5155 100644
--- a/app/src/layout/dock/agent/AgentHistory.ts
+++ b/app/src/layout/dock/agent/AgentHistory.ts
@@ -48,6 +48,7 @@ export type AgentHistoryReference = { id: string; title: string };
export type AgentHistoryUserEntry = {
content: string;
+ timestamp?: number;
blockHTML?: string;
references?: AgentHistoryReference[];
};
@@ -60,6 +61,7 @@ export type AgentHistoryEditData = {
export const applyAgentUserEdit = (entry: AgentHistoryUserEntry, data: AgentHistoryEditData) => {
entry.content = data.text;
+ entry.timestamp = Date.now();
entry.blockHTML = data.blockHTML;
entry.references = data.references.length > 0 ? data.references.slice() : undefined;
};
diff --git a/kernel/agent/runtime.go b/kernel/agent/runtime.go
index e85aee64a3..ea8691ff21 100644
--- a/kernel/agent/runtime.go
+++ b/kernel/agent/runtime.go
@@ -156,6 +156,7 @@ type agentRuntimeTurn struct {
UserEntryID string `json:"userEntryID"`
TargetUserEntryID string `json:"targetUserEntryID,omitempty"`
UserContent string `json:"userContent,omitempty"`
+ UserTimestamp int64 `json:"userTimestamp,omitempty"`
UserBlockHTML *string `json:"userBlockHTML,omitempty"`
UserReferences *[]Reference `json:"userReferences,omitempty"`
UserEditorContext *EditorContext `json:"userEditorContext,omitempty"`
@@ -293,6 +294,10 @@ func beginRuntimeTurn(sessionID string, turn *agentRuntimeTurn) error {
if findRuntimeUserAnchor(session, turn.UserEntryID) < 0 {
return fmt.Errorf("agent runtime user entry not found")
}
+ // 编辑重发的消息时间固定为本轮开始时间,不随后续检查点和恢复操作改变。
+ if turn.Mode == "regenerate" && turn.UserBlockHTML != nil && turn.UserTimestamp == 0 {
+ turn.UserTimestamp = time.Now().UnixMilli()
+ }
runtime.ActiveTurn = turn
return writeRuntimeLocked(sessionID, runtime)
}
@@ -467,6 +472,9 @@ func applyRuntimeTurnToSessionLocked(session map[string]any, turn *agentRuntimeT
if turn.Mode == "regenerate" && turn.UserContent != "" {
entry, _ := entries[anchor].(map[string]any)
entry["content"] = turn.UserContent
+ if turn.UserTimestamp > 0 {
+ entry["timestamp"] = turn.UserTimestamp
+ }
if turn.UserBlockHTML != nil {
if *turn.UserBlockHTML != "" {
entry["blockHTML"] = *turn.UserBlockHTML
diff --git a/kernel/agent/session_test.go b/kernel/agent/session_test.go
index 5dcf3de2fe..43392b8886 100644
--- a/kernel/agent/session_test.go
+++ b/kernel/agent/session_test.go
@@ -14,6 +14,7 @@ import (
"os"
"path/filepath"
"testing"
+ "time"
"github.com/88250/gulu"
"github.com/siyuan-note/siyuan/kernel/util"
@@ -418,6 +419,7 @@ func TestRegenerateRuntimeRecoveryKeepsEditedUserContent(t *testing.T) {
"references": []any{map[string]any{"id": "block-1", "title": "First block"}},
"editorContext": map[string]any{"activeDocID": "old-doc"},
"blockHTML": `<div data-node-id="original">original prompt</div>`,
+ "timestamp": int64(1),
},
map[string]any{"id": "assistant-1", "type": "assistant", "content": "old answer"},
map[string]any{"id": "user-2", "type": "user", "content": "later prompt"},
@@ -445,9 +447,14 @@ func TestRegenerateRuntimeRecoveryKeepsEditedUserContent(t *testing.T) {
turn.UserBlockHTML = new(editedBlockHTML)
turn.UserReferences = &emptyReferences
turn.UserEditorContext = &EditorContext{ActiveDocID: "new-doc"}
+ startedAt := time.Now().UnixMilli()
if err := beginRuntimeTurn(testSessionID, turn); err != nil {
t.Fatal(err)
}
+ if turn.UserTimestamp < startedAt || turn.UserTimestamp > time.Now().UnixMilli() {
+ t.Fatalf("edited user timestamp was not initialized at turn start: %d", turn.UserTimestamp)
+ }
+ userTimestamp := turn.UserTimestamp
turn.State = "interrupted"
if err := saveRuntimeTurn(testSessionID, turn); err != nil {
t.Fatal(err)
@@ -458,6 +465,9 @@ func TestRegenerateRuntimeRecoveryKeepsEditedUserContent(t *testing.T) {
t.Fatal(err)
}
entries := recovered["entries"].([]any)
+ if timestamp := numberToInt64(entries[0].(map[string]any)["timestamp"]); timestamp != userTimestamp {
+ t.Fatalf("recovered edit lost its send time: %d, want %d", timestamp, userTimestamp)
+ }
if len(entries) != 2 || entries[0].(map[string]any)["content"] != "edited prompt" ||
entries[1].(map[string]any)["content"] != "new answer" {
t.Fatalf("regenerated runtime was not recovered consistently: %#v", entries)
@@ -478,6 +488,9 @@ func TestRegenerateRuntimeRecoveryKeepsEditedUserContent(t *testing.T) {
t.Fatalf("commit recovered regenerate turn failed: revision=%d, err=%v", revision, err)
}
committedEntries := canonical["entries"].([]any)
+ if timestamp := numberToInt64(committedEntries[0].(map[string]any)["timestamp"]); timestamp != userTimestamp {
+ t.Fatalf("committed edit lost its send time: %d, want %d", timestamp, userTimestamp)
+ }
if len(committedEntries) != 2 || committedEntries[0].(map[string]any)["content"] != "edited prompt" {
t.Fatalf("committed regenerate turn lost edited content: %#v", committedEntries)
}
@@ -493,6 +506,9 @@ func TestRegenerateRuntimeRecoveryKeepsEditedUserContent(t *testing.T) {
t.Fatal(err)
}
persistedEntries := persisted["entries"].([]any)
+ if timestamp := numberToInt64(persistedEntries[0].(map[string]any)["timestamp"]); timestamp != userTimestamp {
+ t.Fatalf("persisted edit lost its send time: %d, want %d", timestamp, userTimestamp)
+ }
if blockHTML := persistedEntries[0].(map[string]any)["blockHTML"]; blockHTML != editedBlockHTML {
t.Fatalf("persisted regenerate turn lost edited block HTML: %#v", persistedEntries[0])
}
@@ -822,6 +838,7 @@ func TestApplyRegenerateRuntimeReplacesUserContent(t *testing.T) {
map[string]any{
"id": "user-1", "type": "user", "content": "original prompt",
"blockHTML": `<div data-node-id="original">original prompt</div>`,
+ "timestamp": int64(42),
},
map[string]any{"id": "assistant-1", "type": "assistant", "content": "old answer"},
map[string]any{"id": "user-2", "type": "user", "content": "later prompt"},
@@ -847,6 +864,9 @@ func TestApplyRegenerateRuntimeReplacesUserContent(t *testing.T) {
if len(entries) != 2 {
t.Fatalf("regenerated history was not truncated: %#v", entries)
}
+ if timestamp := numberToInt64(entries[0].(map[string]any)["timestamp"]); timestamp != 42 {
+ t.Fatalf("legacy runtime without a send time changed user timestamp: %d", timestamp)
+ }
if content := entries[0].(map[string]any)["content"]; content != "edited prompt" {
t.Fatalf("edited user content was not restored: %v", content)
}
@@ -865,6 +885,7 @@ func TestApplyRegenerateRuntimePreservesUneditedUserBlockHTML(t *testing.T) {
map[string]any{
"id": "user-1", "type": "user", "content": "original prompt",
"blockHTML": originalBlockHTML,
+ "timestamp": int64(42),
},
},
}
@@ -879,6 +900,9 @@ func TestApplyRegenerateRuntimePreservesUneditedUserBlockHTML(t *testing.T) {
t.Fatal(err)
}
entry := session["entries"].([]any)[0].(map[string]any)
+ if timestamp := numberToInt64(entry["timestamp"]); timestamp != 42 {
+ t.Fatalf("regenerate without an edit changed user send time: %d", timestamp)
+ }
if blockHTML := entry["blockHTML"]; blockHTML != originalBlockHTML {
t.Fatalf("regenerate without an edit changed user block HTML: %#v", entry)
}