|
371 | 371 | (let [db @db* |
372 | 372 | [provider model] (string/split full-model #"/" 2) |
373 | 373 | past-messages (get-in db [:chats chat-id :messages] []) |
| 374 | + model-capabilities (get-in db [:models full-model]) |
| 375 | + provider-auth (get-in @db* [:auth provider]) |
374 | 376 | all-tools (f.tools/all-tools chat-id behavior @db* config) |
375 | 377 | received-msgs* (atom "") |
376 | 378 | reasonings* (atom {}) |
377 | 379 | add-to-history! (fn [msg] |
378 | | - (swap! db* update-in [:chats chat-id :messages] (fnil conj []) msg))] |
| 380 | + (swap! db* update-in [:chats chat-id :messages] (fnil conj []) msg)) |
| 381 | + on-usage-updated (fn [usage] |
| 382 | + (when-let [usage (shared/usage-msg->usage usage full-model chat-ctx)] |
| 383 | + (send-content! chat-ctx :system |
| 384 | + (merge {:type :usage} |
| 385 | + usage))))] |
379 | 386 | (when-let [expires-at (get-in db [:auth provider :expires-at])] |
380 | 387 | (when (<= (long expires-at) (quot (System/currentTimeMillis) 1000)) |
381 | 388 | (send-content! chat-ctx :system {:type :progress |
|
388 | 395 | (finish-chat-prompt! :idle chat-ctx) |
389 | 396 | (throw (ex-info "Auth token renew failed" {})))}))) |
390 | 397 |
|
| 398 | + (when-not (get-in db [:chats chat-id :title]) |
| 399 | + (future |
| 400 | + (when-let [title @(llm-api/simple-prompt |
| 401 | + {:provider provider |
| 402 | + :model model |
| 403 | + :model-capabilities model-capabilities |
| 404 | + :instructions (f.prompt/title-prompt) |
| 405 | + :user-messages user-messages |
| 406 | + :config config |
| 407 | + :tools [] |
| 408 | + :provider-auth provider-auth |
| 409 | + :on-usage-updated on-usage-updated})] |
| 410 | + (swap! db* assoc-in [:chats chat-id :title] title) |
| 411 | + (send-content! chat-ctx :system (assoc-some |
| 412 | + {:type :metadata} |
| 413 | + :title title))))) |
391 | 414 | (send-content! chat-ctx :system {:type :progress |
392 | 415 | :state :running |
393 | 416 | :text "Waiting model"}) |
394 | 417 | (llm-api/complete! |
395 | 418 | {:model model |
396 | 419 | :provider provider |
397 | | - :model-capabilities (get-in db [:models full-model]) |
| 420 | + :model-capabilities model-capabilities |
398 | 421 | :user-messages user-messages |
399 | 422 | :instructions instructions |
400 | 423 | :past-messages past-messages |
401 | 424 | :config config |
402 | 425 | :tools all-tools |
403 | | - :provider-auth (get-in @db* [:auth provider]) |
| 426 | + :provider-auth provider-auth |
404 | 427 | :on-first-response-received (fn [& _] |
405 | 428 | (assert-chat-not-stopped! chat-ctx) |
406 | 429 | (doseq [message user-messages] |
407 | 430 | (add-to-history! message)) |
408 | 431 | (send-content! chat-ctx :system {:type :progress |
409 | 432 | :state :running |
410 | 433 | :text "Generating"})) |
411 | | - :on-usage-updated (fn [usage] |
412 | | - (when-let [usage (shared/usage-msg->usage usage full-model chat-ctx)] |
413 | | - (send-content! chat-ctx :system |
414 | | - (merge {:type :usage} |
415 | | - usage)))) |
| 434 | + :on-usage-updated on-usage-updated |
416 | 435 | :on-message-received (fn [{:keys [type] :as msg}] |
417 | 436 | (assert-chat-not-stopped! chat-ctx) |
418 | 437 | (case type |
|
0 commit comments