|
25 | 25 |
|
26 | 26 | (set! *warn-on-reflection* true) |
27 | 27 |
|
| 28 | +(defn ^:private fork-title [title] |
| 29 | + (let [title (or title "Untitled")] |
| 30 | + (if-let [[_ base n] (re-matches #"(.+?)\s*\((\d+)\)\s*$" title)] |
| 31 | + (str base " (" (inc (parse-long n)) ")") |
| 32 | + (str title " (2)")))) |
| 33 | + |
28 | 34 | (defn ^:private normalize-command-name [f] |
29 | 35 | (string/lower-case (fs/strip-ext (fs/file-name f)))) |
30 | 36 |
|
|
113 | 119 | :type :native |
114 | 120 | :description "Summarize the chat so far cleaning previous chat history to reduce context." |
115 | 121 | :arguments [{:name "additional-input"}]} |
| 122 | + {:name "fork" |
| 123 | + :type :native |
| 124 | + :description "Fork current chat into a new chat with the same history and settings." |
| 125 | + :arguments []} |
116 | 126 | {:name "resume" |
117 | 127 | :type :native |
118 | 128 | :description "Resume the specified chat-id. Blank to list chats or 'latest'." |
|
326 | 336 | metrics) |
327 | 337 | {:type :new-chat-status |
328 | 338 | :status :login}) |
| 339 | + "fork" (let [chat (get-in db [:chats chat-id]) |
| 340 | + new-id (str (random-uuid)) |
| 341 | + now (System/currentTimeMillis) |
| 342 | + new-title (fork-title (:title chat)) |
| 343 | + new-chat {:id new-id |
| 344 | + :title new-title |
| 345 | + :status :idle |
| 346 | + :created-at now |
| 347 | + :updated-at now |
| 348 | + :model (:model chat) |
| 349 | + :last-api (:last-api chat) |
| 350 | + :messages (vec (:messages chat)) |
| 351 | + :prompt-finished? true}] |
| 352 | + (swap! db* assoc-in [:chats new-id] new-chat) |
| 353 | + (db/update-workspaces-cache! @db* metrics) |
| 354 | + (messenger/chat-opened messenger {:chat-id new-id :title new-title}) |
| 355 | + {:type :chat-messages |
| 356 | + :chats {new-id {:messages (:messages chat) |
| 357 | + :title new-title} |
| 358 | + chat-id {:messages [{:role "system" |
| 359 | + :content [{:type :text |
| 360 | + :text (str "Chat forked to: " new-title)}]}]}}}) |
329 | 361 | "resume" (let [chats (into {} |
330 | 362 | (filter #(and (not= chat-id (first %)) |
331 | 363 | (not (:subagent (second %))))) |
|
0 commit comments