|
69 | 69 | :max_uses 10 |
70 | 70 | :cache_control {:type "ephemeral"}}))) |
71 | 71 |
|
72 | | -(defn ^:private base-request! [{:keys [rid body api-url api-key auth-type url-relative-path content-block* on-error on-stream http-client extra-headers cancelled?]}] |
| 72 | +(defn ^:private base-request! [{:keys [rid body api-url api-key auth-type url-relative-path content-block* on-error on-stream http-client extra-headers cancelled? stream-idle-timeout-seconds]}] |
73 | 73 | (let [url (join-api-url api-url (or url-relative-path messages-path)) |
74 | 74 | reason-id* (atom (str (random-uuid))) |
75 | 75 | oauth? (= :auth/oauth auth-type) |
|
105 | 105 | :body body-str})) |
106 | 106 | (if on-stream |
107 | 107 | (let [{:keys [touch-fn set-reading-fn stop-fn reason*]} |
108 | | - (llm-util/start-stream-watchdog! body cancelled? {}) |
| 108 | + (llm-util/start-stream-watchdog! body cancelled? |
| 109 | + (when stream-idle-timeout-seconds |
| 110 | + {:idle-timeout-ms (* 1000 stream-idle-timeout-seconds)})) |
109 | 111 | completed?* (atom false)] |
110 | 112 | (try |
111 | 113 | (with-open [rdr (io/reader body)] |
|
134 | 136 | (throw (ex-info "Stream cancelled" {:silent? true})) |
135 | 137 |
|
136 | 138 | (= :idle-timeout reason) |
137 | | - (on-error {:message "Stream idle timeout: no data received for 2 minutes" |
| 139 | + (on-error {:message (format "Stream idle timeout: no data received for %d seconds" |
| 140 | + (or stream-idle-timeout-seconds 120)) |
138 | 141 | :exception e}) |
139 | 142 |
|
140 | 143 | :else |
|
291 | 294 | (defn chat! |
292 | 295 | [{:keys [model user-messages instructions max-output-tokens |
293 | 296 | api-url api-key auth-type url-relative-path reason? past-messages |
294 | | - tools web-search extra-payload extra-headers supports-image? http-client cancelled?]} |
| 297 | + tools web-search extra-payload extra-headers supports-image? http-client cancelled? |
| 298 | + stream-idle-timeout-seconds]} |
295 | 299 | {:keys [on-message-received on-error on-reason on-prepare-tool-call on-tools-called on-usage-updated on-server-web-search] :as callbacks}] |
296 | 300 | (let [messages (-> (concat past-messages (fix-non-thinking-assistant-messages user-messages)) |
297 | 301 | group-parallel-tool-calls |
|
438 | 442 | :content-block* (atom nil) |
439 | 443 | :cancelled? cancelled? |
440 | 444 | :on-error on-error |
441 | | - :on-stream handle-stream})))) |
| 445 | + :on-stream handle-stream |
| 446 | + :stream-idle-timeout-seconds stream-idle-timeout-seconds})))) |
442 | 447 | "end_turn" (if @has-content?* |
443 | 448 | (do |
444 | 449 | (reset! content-block* {}) |
|
470 | 475 | :content-block* (atom nil) |
471 | 476 | :cancelled? cancelled? |
472 | 477 | :on-error on-error |
473 | | - :on-stream on-stream-fn}))) |
| 478 | + :on-stream on-stream-fn |
| 479 | + :stream-idle-timeout-seconds stream-idle-timeout-seconds}))) |
474 | 480 |
|
475 | 481 | (def ^:private client-id "9d1c250a-e61b-44d9-88ed-5944d1962f5e") |
476 | 482 |
|
|
0 commit comments