|
68 | 68 | content)) |
69 | 69 |
|
70 | 70 | (defn finish-chat-prompt! [status {:keys [message chat-id db* messenger metrics config on-finished-side-effect prompt-id] :as chat-ctx}] |
71 | | - (when-not (and prompt-id (not= prompt-id (get-in @db* [:chats chat-id :prompt-id]))) |
72 | | - (when-not (get-in @db* [:chats chat-id :auto-compacting?]) |
73 | | - (swap! db* assoc-in [:chats chat-id :status] status) |
74 | | - (messenger/chat-status-changed messenger {:chat-id chat-id :status status}) |
75 | | - (let [db @db* |
76 | | - subagent? (some? (get-in db [:chats chat-id :subagent])) |
77 | | - hook-type (if subagent? :subagentPostRequest :postRequest) |
78 | | - hook-data (cond-> (merge (f.hooks/chat-hook-data db chat-id (:agent chat-ctx)) |
79 | | - {:prompt message}) |
80 | | - subagent? (assoc :parent-chat-id (get-in db [:chats chat-id :parent-chat-id])))] |
81 | | - (f.hooks/trigger-if-matches! hook-type |
82 | | - hook-data |
83 | | - {:on-before-action (partial notify-before-hook-action! chat-ctx) |
84 | | - :on-after-action (partial notify-after-hook-action! chat-ctx)} |
85 | | - db |
86 | | - config)) |
87 | | - (send-content! chat-ctx :system |
88 | | - {:type :progress |
89 | | - :state :finished}) |
90 | | - (when-not (get-in @db* [:chats chat-id :created-at]) |
91 | | - (swap! db* assoc-in [:chats chat-id :created-at] (System/currentTimeMillis))) |
92 | | - (swap! db* assoc-in [:chats chat-id :prompt-finished?] true)) |
93 | | - (when (and on-finished-side-effect |
94 | | - (not (identical? :stopping (get-in @db* [:chats chat-id :status])))) |
95 | | - (on-finished-side-effect)) |
96 | | - (db/update-workspaces-cache! @db* metrics))) |
| 71 | + (when-not (get-in @db* [:chats chat-id :prompt-finished?]) |
| 72 | + (when-not (and prompt-id (not= prompt-id (get-in @db* [:chats chat-id :prompt-id]))) |
| 73 | + (when-not (get-in @db* [:chats chat-id :auto-compacting?]) |
| 74 | + (swap! db* assoc-in [:chats chat-id :prompt-finished?] true) |
| 75 | + (swap! db* assoc-in [:chats chat-id :status] status) |
| 76 | + (messenger/chat-status-changed messenger {:chat-id chat-id :status status}) |
| 77 | + (let [db @db* |
| 78 | + subagent? (some? (get-in db [:chats chat-id :subagent])) |
| 79 | + hook-type (if subagent? :subagentPostRequest :postRequest) |
| 80 | + hook-data (cond-> (merge (f.hooks/chat-hook-data db chat-id (:agent chat-ctx)) |
| 81 | + {:prompt message}) |
| 82 | + subagent? (assoc :parent-chat-id (get-in db [:chats chat-id :parent-chat-id])))] |
| 83 | + (f.hooks/trigger-if-matches! hook-type |
| 84 | + hook-data |
| 85 | + {:on-before-action (partial notify-before-hook-action! chat-ctx) |
| 86 | + :on-after-action (partial notify-after-hook-action! chat-ctx)} |
| 87 | + db |
| 88 | + config)) |
| 89 | + (send-content! chat-ctx :system |
| 90 | + {:type :progress |
| 91 | + :state :finished}) |
| 92 | + (when-not (get-in @db* [:chats chat-id :created-at]) |
| 93 | + (swap! db* assoc-in [:chats chat-id :created-at] (System/currentTimeMillis)))) |
| 94 | + (when (and on-finished-side-effect |
| 95 | + (not (identical? :stopping (get-in @db* [:chats chat-id :status])))) |
| 96 | + (on-finished-side-effect)) |
| 97 | + (db/update-workspaces-cache! @db* metrics)))) |
97 | 98 |
|
98 | 99 | (defn maybe-renew-auth-token [chat-ctx] |
99 | 100 | (f.login/maybe-renew-auth-token! |
|
111 | 112 | (defn assert-chat-not-stopped! [{:keys [chat-id db* prompt-id] :as chat-ctx}] |
112 | 113 | (let [chat (get-in @db* [:chats chat-id]) |
113 | 114 | superseded? (and prompt-id (not= prompt-id (:prompt-id chat))) |
114 | | - stopped? (or (identical? :stopping (:status chat)) superseded?)] |
| 115 | + stopped? (or (identical? :stopping (:status chat)) |
| 116 | + (:prompt-finished? chat) |
| 117 | + superseded?)] |
115 | 118 | (when stopped? |
116 | 119 | (finish-chat-prompt! :idle (dissoc chat-ctx :on-finished-side-effect)) |
117 | 120 | (logger/info logger-tag "Chat prompt stopped:" chat-id (when superseded? "(superseded)")) |
|
0 commit comments