|
199 | 199 | (on-reason {:status :finished |
200 | 200 | :id (-> data :item :id) |
201 | 201 | :external-id (-> data :item :encrypted_content)})) |
| 202 | + "function_call" (swap! tool-call-by-item-id* update (-> data :item :id) |
| 203 | + assoc :arguments (-> data :item :arguments)) |
202 | 204 | "web_search_call" (on-server-web-search {:status :finished |
203 | 205 | :id (-> data :item :id) |
204 | 206 | :output nil}) |
|
246 | 248 | ;; done |
247 | 249 | "response.completed" |
248 | 250 | (let [response (:response data) |
249 | | - tool-calls (keep (fn [{:keys [id call_id name arguments] :as output}] |
250 | | - (when (= "function_call" (:type output)) |
251 | | - ;; Fallback case when the tool call was not prepared before when |
252 | | - ;; some models/apis respond only with response.completed (skipping streaming). |
253 | | - (when-not (get @tool-call-by-item-id* id) |
254 | | - (swap! tool-call-by-item-id* assoc id {:full-name name :id call_id}) |
255 | | - (on-prepare-tool-call {:id call_id |
256 | | - :full-name name |
257 | | - :arguments-text arguments})) |
258 | | - {:id call_id |
259 | | - :item-id id |
260 | | - :full-name name |
261 | | - :arguments (json/parse-string arguments)})) |
262 | | - (:output response))] |
| 251 | + tool-calls (or (seq (keep (fn [{:keys [id call_id name arguments] :as output}] |
| 252 | + (when (= "function_call" (:type output)) |
| 253 | + (when-not (get @tool-call-by-item-id* id) |
| 254 | + (swap! tool-call-by-item-id* assoc id {:full-name name :id call_id}) |
| 255 | + (on-prepare-tool-call {:id call_id |
| 256 | + :full-name name |
| 257 | + :arguments-text arguments})) |
| 258 | + {:id call_id |
| 259 | + :item-id id |
| 260 | + :full-name name |
| 261 | + :arguments (json/parse-string arguments)})) |
| 262 | + (:output response))) |
| 263 | + ;; Fallback: some models stream tool calls via events |
| 264 | + ;; but return empty :output in response.completed |
| 265 | + (seq (keep (fn [[item-id {:keys [full-name id arguments]}]] |
| 266 | + (when arguments |
| 267 | + {:id id |
| 268 | + :item-id item-id |
| 269 | + :full-name full-name |
| 270 | + :arguments (json/parse-string arguments)})) |
| 271 | + @tool-call-by-item-id*)))] |
263 | 272 | (on-usage-updated (let [input-cache-read-tokens (-> response :usage :input_tokens_details :cached_tokens)] |
264 | 273 | {:input-tokens (if input-cache-read-tokens |
265 | 274 | (- (-> response :usage :input_tokens) input-cache-read-tokens) |
|
0 commit comments