Skip to content

Commit d057493

Browse files
committed
Support rollback only messages, tool call changes or both in chat/rollback.
1 parent abd127e commit d057493

File tree

4 files changed

+30
-14
lines changed

4 files changed

+30
-14
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
- Fix custom tools to support argument numbers.
77
- Improve read_file summary to mention offset being read.
8+
- Enhanced hooks documentation with new types (sessionStart, sessionEnd, chatStart, chatEnd), JSON input/output schemas, execution options (timeout)
9+
- Support rollback only messages, tool call changes or both in `chat/rollback`.
810

911
## 0.84.2
1012

docs/protocol.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,7 +1254,14 @@ interface ChatRollbackParams {
12541254
* The message content id.
12551255
*/
12561256
contentId: string;
1257+
1258+
/**
1259+
* The types of rollbacks to include, allowing to rollback one or more types.
1260+
*/
1261+
include: ChatRollbackInclude[];
12571262
}
1263+
1264+
type ChatRollbackInclude = 'messages' | 'tools';
12581265
```
12591266

12601267
_Response:_

src/eca/features/chat.clj

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,11 +1439,14 @@
14391439
(defn rollback-chat
14401440
"Remove messages from chat in db until content-id matches.
14411441
Then notify to clear chat and then the kept messages."
1442-
[{:keys [chat-id content-id]} db* messenger]
1443-
(let [all-messages (get-in @db* [:chats chat-id :messages])
1442+
[{:keys [chat-id content-id include]} db* messenger]
1443+
(let [include (set include)
1444+
all-messages (get-in @db* [:chats chat-id :messages])
14441445
tool-calls (get-in @db* [:chats chat-id :tool-calls])
1445-
new-messages (vec (take-while #(not= (:content-id %) content-id) all-messages))
1446-
removed-messages (vec (drop-while #(not= (:content-id %) content-id) all-messages))
1446+
new-messages (when (contains? include "messages")
1447+
(vec (take-while #(not= (:content-id %) content-id) all-messages)))
1448+
removed-messages (when (contains? include "tools")
1449+
(vec (drop-while #(not= (:content-id %) content-id) all-messages)))
14471450
rollback-changes (->> removed-messages
14481451
(filter #(= "tool_call_output" (:role %)))
14491452
(keep #(get-in tool-calls [(:id (:content %)) :rollback-changes]))
@@ -1454,13 +1457,14 @@
14541457
(if content
14551458
(spit path content)
14561459
(io/delete-file path true)))
1457-
(swap! db* assoc-in [:chats chat-id :messages] new-messages)
1458-
(messenger/chat-cleared
1459-
messenger
1460-
{:chat-id chat-id
1461-
:messages true})
1462-
(send-chat-contents!
1463-
new-messages
1464-
{:chat-id chat-id
1465-
:messenger messenger})
1460+
(when new-messages
1461+
(swap! db* assoc-in [:chats chat-id :messages] new-messages)
1462+
(messenger/chat-cleared
1463+
messenger
1464+
{:chat-id chat-id
1465+
:messages true})
1466+
(send-chat-contents!
1467+
new-messages
1468+
{:chat-id chat-id
1469+
:messenger messenger}))
14661470
{}))

test/eca/features/chat_test.clj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,10 @@
584584

585585
;; Rollback to second message (keep first 2 messages, remove last 2)
586586
(h/reset-messenger!)
587-
(is (= {} (f.chat/rollback-chat {:chat-id chat-id :content-id second-content-id} (h/db*) (h/messenger))))
587+
(is (= {} (f.chat/rollback-chat
588+
{:chat-id chat-id
589+
:include ["messages" "tools"]
590+
:content-id second-content-id} (h/db*) (h/messenger))))
588591

589592
;; Verify messages after content-id are removed (keeps messages before content-id)
590593
(is (match?

0 commit comments

Comments
 (0)