Skip to content

Commit cceae1f

Browse files
committed
Fix thinking for claude
1 parent eb175b8 commit cceae1f

File tree

3 files changed

+33
-20
lines changed

3 files changed

+33
-20
lines changed

src/eca/features/chat.clj

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
user-prompt message
102102
all-tools (f.tools/all-tools @db* config)
103103
received-msgs* (atom "")
104+
received-thinking* (atom "")
104105
tool-call-args-by-id* (atom {})
105106
add-to-history! (fn [msg]
106107
(swap! db* update-in [:chats chat-id :messages] (fnil conj []) msg))
@@ -218,19 +219,24 @@
218219
:reason :user
219220
:id id})
220221
{:new-messages (get-in @db* [:chats chat-id :messages])}))))
221-
:on-reason (fn [{:keys [status id text]}]
222+
:on-reason (fn [{:keys [status id text external-id]}]
222223
(assert-chat-not-stopped! chat-ctx)
223224
(case status
224225
:started (send-content! chat-ctx :assistant
225226
{:type :reasonStarted
226227
:id id})
227-
:thinking (send-content! chat-ctx :assistant
228-
{:type :reasonText
229-
:id id
230-
:text text})
231-
:finished (send-content! chat-ctx :assistant
232-
{:type :reasonFinished
233-
:id id})
228+
:thinking (do
229+
(swap! received-thinking* str text)
230+
(send-content! chat-ctx :assistant
231+
{:type :reasonText
232+
:id id
233+
:text text}))
234+
:finished (do
235+
(add-to-history! {:role "reason" :content {:external-id external-id
236+
:text @received-thinking*}})
237+
(send-content! chat-ctx :assistant
238+
{:type :reasonFinished
239+
:id id}))
234240
nil))
235241
:on-error (fn [{:keys [message exception]}]
236242
(send-content! chat-ctx :system

src/eca/llm_api.clj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@
7676
on-message-received-wrapper (fn [& args]
7777
(apply emit-first-message-fn args)
7878
(apply on-message-received args))
79+
on-reason-wrapper (fn [& args]
80+
(apply emit-first-message-fn args)
81+
(apply on-reason args))
7982
on-prepare-tool-call-wrapper (fn [& args]
8083
(apply emit-first-message-fn args)
8184
(apply on-prepare-tool-call args))
@@ -96,7 +99,7 @@
9699
:on-error on-error-wrapper
97100
:on-prepare-tool-call on-prepare-tool-call-wrapper
98101
:on-tool-called on-tool-called
99-
:on-reason on-reason}]
102+
:on-reason on-reason-wrapper}]
100103
(cond
101104
(contains? #{"o4-mini"
102105
"o3"

src/eca/llm_providers/anthropic.clj

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@
6767
:content [{:type "tool_result"
6868
:tool_use_id (:id content)
6969
:content (llm-util/stringfy-tool-result content)}]}
70+
"reason"
71+
{:role "assistant"
72+
:content [{:type "thinking"
73+
:signature (:external-id content)
74+
:thinking (:text content)}]}
7075
msg))
7176
past-messages))
7277

@@ -111,12 +116,6 @@
111116
(swap! content-block* assoc (:index data) (:content_block data)))
112117

113118
nil)
114-
"content_block_stop" (when-let [content-block (get @content-block* (:index data))]
115-
(case (:type content-block)
116-
"thinking" (on-reason {:status :finished
117-
:id reason-id})
118-
nil)
119-
(swap! content-block* dissoc (:index data)))
120119
"content_block_delta" (case (-> data :delta :type)
121120
"text_delta" (on-message-received {:type :text
122121
:text (-> data :delta :text)})
@@ -135,7 +134,10 @@
135134
"thinking_delta" (on-reason {:status :thinking
136135
:id reason-id
137136
:text (-> data :delta :thinking)})
138-
(logger/warn "Unkown response delta type" (-> data :delta :type)))
137+
"signature_delta" (on-reason {:status :finished
138+
:external-id (-> data :delta :signature)
139+
:id reason-id})
140+
nil)
139141
"message_delta" (case (-> data :delta :stop_reason)
140142
"tool_use" (doseq [content-block (vals @content-block*)]
141143
(when (= "tool_use" (:type content-block))
@@ -154,10 +156,12 @@
154156
:content-block* (atom nil)
155157
:on-error on-error
156158
:on-response handle-response}))))
157-
"end_turn" (on-message-received {:type :finish
158-
:usage {:input-tokens (-> data :usage :input_tokens)
159-
:output-tokens (-> data :usage :output_tokens)}
160-
:finish-reason (-> data :delta :stop_reason)})
159+
"end_turn" (do
160+
(reset! content-block* {})
161+
(on-message-received {:type :finish
162+
:usage {:input-tokens (-> data :usage :input_tokens)
163+
:output-tokens (-> data :usage :output_tokens)}
164+
:finish-reason (-> data :delta :stop_reason)}))
161165
"max_tokens" (on-message-received {:type :limit-reached
162166
:tokens (:usage data)})
163167
nil)

0 commit comments

Comments
 (0)