|
19 | 19 | [eca.logger :as logger] |
20 | 20 | [eca.messenger :as messenger] |
21 | 21 | [eca.metrics :as metrics] |
22 | | - [eca.shared :as shared :refer [assoc-some future*]])) |
| 22 | + [eca.shared :as shared :refer [assoc-some future*]] |
| 23 | + [eca.llm-util :as llm-util])) |
23 | 24 |
|
24 | 25 | (set! *warn-on-reflection* true) |
25 | 26 |
|
|
81 | 82 | (let [entry {:type :text :text (wrap-additional-context hook-name additional-context)}] |
82 | 83 | (swap! db* update-in [:chats chat-id :messages] |
83 | 84 | ;; Optimized: Scans messages backwards since the tool output is likely one of the last items. |
84 | | - #(let [idx (loop [i (dec (count %))] |
85 | | - (when (>= i 0) |
86 | | - (let [msg (nth % i)] |
87 | | - (if (and (= "tool_call_output" (:role msg)) |
88 | | - (= tool-call-id (get-in msg [:content :id]))) |
89 | | - i |
90 | | - (recur (dec i))))))] |
| 85 | + #(let [idx (llm-util/find-last-msg-idx |
| 86 | + (fn [msg] |
| 87 | + (and (= "tool_call_output" (:role msg)) |
| 88 | + (= tool-call-id (get-in msg [:content :id])))) |
| 89 | + %)] |
91 | 90 | (if idx |
92 | 91 | (update-in % [idx :content :output :contents] conj entry) |
93 | 92 | %)))))) |
|
811 | 810 | hook-rejected? (assoc :hook-continue hook-continue |
812 | 811 | :hook-stop-reason hook-stop-reason)))) |
813 | 812 |
|
814 | | -(defn ^:private find-last-user-msg-idx [messages] |
815 | | - ;; Returns the index of the last :role "user" message, or nil if none. |
816 | | - (last (keep-indexed (fn [i m] (when (= "user" (:role m)) i)) messages))) |
817 | | - |
818 | 813 | (defn ^:private on-tools-called! [{:keys [db* config chat-id behavior messenger metrics] :as chat-ctx} |
819 | 814 | received-msgs* add-to-history!] |
820 | 815 | (let [all-tools (f.tools/all-tools chat-id behavior @db* config)] |
|
1026 | 1021 | (run-pre-request-hooks! (assoc chat-ctx :message original-text))] |
1027 | 1022 | (cond |
1028 | 1023 | stop? (do (finish-chat-prompt! :idle chat-ctx) nil) |
1029 | | - :else (let [last-user-idx (or (find-last-user-msg-idx user-messages) |
| 1024 | + :else (let [last-user-idx (or (llm-util/find-last-user-msg-idx user-messages) |
1030 | 1025 | (dec (count user-messages))) |
1031 | 1026 | rewritten (if (and modify-allowed? |
1032 | 1027 | last-user-idx |
|
1135 | 1130 | :arguments-text arguments-text |
1136 | 1131 | :summary (f.tools/tool-call-summary all-tools full-name nil config)}))) |
1137 | 1132 | :on-tools-called (on-tools-called! chat-ctx received-msgs* add-to-history!) |
1138 | | - :on-reason (fn [{:keys [status id text external-id]}] |
| 1133 | + :on-reason (fn [{:keys [status id text external-id delta-reasoning?]}] |
1139 | 1134 | (assert-chat-not-stopped! chat-ctx) |
1140 | 1135 | (case status |
1141 | 1136 | :started (do (swap! reasonings* assoc-in [id :start-time] (System/currentTimeMillis)) |
1142 | 1137 | (send-content! chat-ctx :assistant {:type :reasonStarted :id id})) |
1143 | 1138 | :thinking (do (swap! reasonings* update-in [id :text] str text) |
1144 | 1139 | (send-content! chat-ctx :assistant {:type :reasonText :id id :text text})) |
1145 | | - :finished (let [total-time-ms (- (System/currentTimeMillis) (get-in @reasonings* [id :start-time]))] |
1146 | | - (add-to-history! {:role "reason" |
1147 | | - :content {:id id |
1148 | | - :external-id external-id |
1149 | | - :total-time-ms total-time-ms |
1150 | | - :text (get-in @reasonings* [id :text])}}) |
1151 | | - (send-content! chat-ctx :assistant {:type :reasonFinished :total-time-ms total-time-ms :id id})) |
| 1140 | + :finished (when-let [start-time (get-in @reasonings* [id :start-time])] |
| 1141 | + (let [total-time-ms (- (System/currentTimeMillis) start-time)] |
| 1142 | + (add-to-history! {:role "reason" |
| 1143 | + :content {:id id |
| 1144 | + :external-id external-id |
| 1145 | + :delta-reasoning? delta-reasoning? |
| 1146 | + :total-time-ms total-time-ms |
| 1147 | + :text (get-in @reasonings* [id :text])}}) |
| 1148 | + (send-content! chat-ctx :assistant {:type :reasonFinished :total-time-ms total-time-ms :id id}))) |
1152 | 1149 | nil)) |
1153 | 1150 | :on-error (fn [{:keys [message exception]}] |
1154 | 1151 | (send-content! chat-ctx :system {:type :text :text (or message (str "Error: " (ex-message exception)))}) |
|
0 commit comments