|
151 | 151 | all-tools (f.tools/all-tools @db* config) |
152 | 152 | received-msgs* (atom "") |
153 | 153 | received-thinking* (atom "") |
154 | | - tool-call-args-by-id* (atom {}) |
| 154 | + tool-call-by-id* (atom {:args {}}) |
155 | 155 | add-to-history! (fn [msg] |
156 | 156 | (swap! db* update-in [:chats chat-id :messages] (fnil conj []) msg))] |
157 | 157 |
|
|
199 | 199 | (finish-chat-prompt! :idle chat-ctx)))) |
200 | 200 | :on-prepare-tool-call (fn [{:keys [id name arguments-text]}] |
201 | 201 | (assert-chat-not-stopped! chat-ctx) |
202 | | - (swap! tool-call-args-by-id* update id str arguments-text) |
| 202 | + (swap! tool-call-by-id* update-in [id :args] str arguments-text) |
203 | 203 | (send-content! chat-ctx :assistant |
204 | 204 | {:type :toolCallPrepare |
205 | 205 | :name name |
206 | 206 | :origin (tool-name->origin name all-tools) |
207 | | - :arguments-text (get @tool-call-args-by-id* id) |
| 207 | + :arguments-text (get-in @tool-call-by-id* [id :args]) |
208 | 208 | :id id |
209 | 209 | :manual-approval manual-approval?})) |
210 | 210 | :on-tool-called (fn [{:keys [id name arguments] :as tool-call}] |
211 | 211 | (assert-chat-not-stopped! chat-ctx) |
212 | | - (send-content! chat-ctx :assistant |
213 | | - (assoc-some |
214 | | - {:type :toolCallRun |
215 | | - :name name |
216 | | - :origin (tool-name->origin name all-tools) |
217 | | - :arguments arguments |
218 | | - :id id |
219 | | - :manual-approval manual-approval?} |
220 | | - :details (f.tools/get-tool-call-details name arguments))) |
221 | | - (let [approved?* (promise)] |
| 212 | + (let [approved?* (promise) |
| 213 | + details (f.tools/get-tool-call-details name arguments)] |
| 214 | + (send-content! chat-ctx :assistant |
| 215 | + (assoc-some |
| 216 | + {:type :toolCallRun |
| 217 | + :name name |
| 218 | + :origin (tool-name->origin name all-tools) |
| 219 | + :arguments arguments |
| 220 | + :id id |
| 221 | + :manual-approval manual-approval?} |
| 222 | + :details details)) |
222 | 223 | (swap! db* assoc-in [:chats chat-id :tool-calls id :approved?*] approved?*) |
223 | 224 | (when-not (string/blank? @received-msgs*) |
224 | 225 | (add-to-history! {:role "assistant" :content @received-msgs*}) |
|
234 | 235 | (let [result (f.tools/call-tool! name arguments @db* config)] |
235 | 236 | (add-to-history! {:role "tool_call" :content tool-call}) |
236 | 237 | (add-to-history! {:role "tool_call_output" :content (assoc tool-call :output result)}) |
237 | | - (swap! tool-call-args-by-id* dissoc id) |
238 | 238 | (send-content! chat-ctx :assistant |
239 | | - {:type :toolCalled |
240 | | - :origin (tool-name->origin name all-tools) |
241 | | - :name name |
242 | | - :arguments arguments |
243 | | - :error (:error result) |
244 | | - :id id |
245 | | - :outputs (:contents result)}) |
246 | | - {:new-messages (get-in @db* [:chats chat-id :messages])}) |
| 239 | + (assoc-some |
| 240 | + {:type :toolCalled |
| 241 | + :origin (tool-name->origin name all-tools) |
| 242 | + :name name |
| 243 | + :arguments arguments |
| 244 | + :error (:error result) |
| 245 | + :id id |
| 246 | + :outputs (:contents result)} |
| 247 | + :details details))) |
247 | 248 | (do |
248 | 249 | (add-to-history! {:role "tool_call" :content tool-call}) |
249 | 250 | (add-to-history! {:role "tool_call_output" :content (assoc tool-call :output {:contents [{:content "Tool call rejected by user" |
250 | 251 | :error true |
251 | 252 | :type :text}]})}) |
252 | | - (swap! tool-call-args-by-id* dissoc id) |
253 | | - (send-content! chat-ctx :system |
254 | | - {:type :progress |
255 | | - :state :running |
256 | | - :text "Generating"}) |
257 | 253 | (send-content! chat-ctx :assistant |
258 | | - {:type :toolCallRejected |
259 | | - :origin (tool-name->origin name all-tools) |
260 | | - :name name |
261 | | - :arguments arguments |
262 | | - :reason :user |
263 | | - :id id}) |
264 | | - {:new-messages (get-in @db* [:chats chat-id :messages])})))) |
| 254 | + (assoc-some |
| 255 | + {:type :toolCallRejected |
| 256 | + :origin (tool-name->origin name all-tools) |
| 257 | + :name name |
| 258 | + :arguments arguments |
| 259 | + :reason :user |
| 260 | + :id id} |
| 261 | + :details details)))) |
| 262 | + (swap! tool-call-by-id* dissoc id) |
| 263 | + (send-content! chat-ctx :system {:type :progress :state :running :text "Generating"}) |
| 264 | + {:new-messages (get-in @db* [:chats chat-id :messages])})) |
265 | 265 | :on-reason (fn [{:keys [status id text external-id]}] |
266 | 266 | (assert-chat-not-stopped! chat-ctx) |
267 | 267 | (case status |
|
0 commit comments