|
465 | 465 | (logger/info logger-tag "Superseding active prompt" {:chat-id chat-id |
466 | 466 | :status (get-in @db* [:chats chat-id :status])})) |
467 | 467 | (swap! db* assoc-in [:chats chat-id :status] :running) |
| 468 | + (swap! db* update-in [:chats chat-id] dissoc :prompt-finished?) |
468 | 469 | (swap! db* assoc-in [:chats chat-id :updated-at] (System/currentTimeMillis)) |
469 | 470 | (messenger/chat-status-changed messenger {:chat-id chat-id :status :running}) |
470 | 471 | (swap! db* assoc-in [:chats chat-id :prompt-id] prompt-id) |
|
772 | 773 | (when (and (= prompt-id (get-in @db* [:chats chat-id :prompt-id])) |
773 | 774 | (contains? #{:stopping :running} (get-in @db* [:chats chat-id :status]))) |
774 | 775 | (swap! db* assoc-in [:chats chat-id :status] :idle) |
775 | | - (messenger/chat-status-changed (:messenger chat-ctx) {:chat-id chat-id :status :idle}) |
| 776 | + ;; Only notify client if finish-chat-prompt! hasn't already run, |
| 777 | + ;; otherwise the belated statusChanged causes duplicate finished handling. |
| 778 | + (when-not (get-in @db* [:chats chat-id :prompt-finished?]) |
| 779 | + (messenger/chat-status-changed (:messenger chat-ctx) {:chat-id chat-id :status :idle})) |
776 | 780 | (db/update-workspaces-cache! @db* metrics)))))))))) |
777 | 781 |
|
778 | 782 | (defn ^:private send-mcp-prompt! |
|
1021 | 1025 | :commands commands})) |
1022 | 1026 |
|
1023 | 1027 | (defn prompt-stop |
1024 | | - [{:keys [chat-id]} db* messenger metrics] |
1025 | | - (when (identical? :running (get-in @db* [:chats chat-id :status])) |
1026 | | - ;; Set :stopping immediately to prevent race with stream callbacks |
1027 | | - ;; that check status via assert-chat-not-stopped! or cancelled? |
1028 | | - (swap! db* assoc-in [:chats chat-id :status] :stopping) |
1029 | | - (let [chat-ctx {:chat-id chat-id |
1030 | | - :db* db* |
1031 | | - :metrics metrics |
1032 | | - :messenger messenger |
1033 | | - :parent-chat-id (get-in @db* [:chats chat-id :parent-chat-id])}] |
1034 | | - (lifecycle/send-content! chat-ctx :system {:type :text |
1035 | | - :text "\nPrompt stopped"}) |
| 1028 | + ([params db* messenger metrics] |
| 1029 | + (prompt-stop params db* messenger metrics {})) |
| 1030 | + ([{:keys [chat-id]} db* messenger metrics {:keys [silent?]}] |
| 1031 | + (when (identical? :running (get-in @db* [:chats chat-id :status])) |
| 1032 | + ;; Set :stopping immediately to prevent race with stream callbacks |
| 1033 | + ;; that check status via assert-chat-not-stopped! or cancelled? |
| 1034 | + (swap! db* assoc-in [:chats chat-id :status] :stopping) |
| 1035 | + (let [chat-ctx {:chat-id chat-id |
| 1036 | + :db* db* |
| 1037 | + :metrics metrics |
| 1038 | + :messenger messenger |
| 1039 | + :parent-chat-id (get-in @db* [:chats chat-id :parent-chat-id])}] |
| 1040 | + (when-not silent? |
| 1041 | + (lifecycle/send-content! chat-ctx :system {:type :text |
| 1042 | + :text "\nPrompt stopped"})) |
1036 | 1043 |
|
1037 | | - ;; Handle each active tool call |
1038 | | - (doseq [[tool-call-id _] (tc/get-active-tool-calls @db* chat-id)] |
1039 | | - (tc/transition-tool-call! db* chat-ctx tool-call-id :stop-requested |
1040 | | - {:reason {:code :user-prompt-stop |
1041 | | - :text "Tool call rejected because of user prompt stop"}})) |
1042 | | - (lifecycle/finish-chat-prompt! :stopping (dissoc chat-ctx :on-finished-side-effect))))) |
| 1044 | + ;; Handle each active tool call |
| 1045 | + (doseq [[tool-call-id _] (tc/get-active-tool-calls @db* chat-id)] |
| 1046 | + (tc/transition-tool-call! db* chat-ctx tool-call-id :stop-requested |
| 1047 | + {:reason {:code :user-prompt-stop |
| 1048 | + :text "Tool call rejected because of user prompt stop"}})) |
| 1049 | + (lifecycle/finish-chat-prompt! :stopping (dissoc chat-ctx :on-finished-side-effect)))))) |
1043 | 1050 |
|
1044 | 1051 | (defn delete-chat |
1045 | 1052 | [{:keys [chat-id]} db* messenger config metrics] |
|
0 commit comments