Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit c69a51b

Browse files
committed
handle thinking in tool implementation
1 parent 4d36638 commit c69a51b

File tree

2 files changed

+62
-10
lines changed

2 files changed

+62
-10
lines changed

lib/ai_bot/bot.rb

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ def reply(context, &update_blk)
112112

113113
allow_partial_tool_calls = persona.allow_partial_tool_calls?
114114
existing_tools = Set.new
115+
current_thinking = []
115116

116117
result =
117118
llm.generate(
@@ -149,7 +150,17 @@ def reply(context, &update_blk)
149150
needs_newlines = false
150151
end
151152

152-
process_tool(tool, raw_context, llm, cancel, update_blk, prompt, context)
153+
process_tool(
154+
tool: tool,
155+
raw_context: raw_context,
156+
llm: llm,
157+
cancel: cancel,
158+
update_blk: update_blk,
159+
prompt: prompt,
160+
context: context,
161+
current_thinking: current_thinking,
162+
)
163+
153164
tools_ran += 1
154165
ongoing_chain &&= tool.chain_next_response?
155166

@@ -167,6 +178,7 @@ def reply(context, &update_blk)
167178
if !partial.partial?
168179
# this will be dealt with later
169180
raw_context << partial
181+
current_thinking << partial
170182
end
171183
else
172184
update_blk.call(partial, cancel)
@@ -222,7 +234,16 @@ def embed_thinking(raw_context)
222234
embedded_thinking
223235
end
224236

225-
def process_tool(tool, raw_context, llm, cancel, update_blk, prompt, context)
237+
def process_tool(
238+
tool:,
239+
raw_context:,
240+
llm:,
241+
cancel:,
242+
update_blk:,
243+
prompt:,
244+
context:,
245+
current_thinking:
246+
)
226247
tool_call_id = tool.tool_call_id
227248
invocation_result_json = invoke_tool(tool, llm, cancel, context, &update_blk).to_json
228249

@@ -233,6 +254,17 @@ def process_tool(tool, raw_context, llm, cancel, update_blk, prompt, context)
233254
name: tool.name,
234255
}
235256

257+
if current_thinking.present?
258+
current_thinking.each do |thinking|
259+
if thinking.redacted
260+
tool_call_message[:redacted_thinking_signature] = thinking.signature
261+
else
262+
tool_call_message[:thinking] = thinking.message
263+
tool_call_message[:thinking_signature] = thinking.signature
264+
end
265+
end
266+
end
267+
236268
tool_message = {
237269
type: :tool,
238270
id: tool_call_id,

lib/completions/dialects/claude_tools.rb

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,35 @@ def instructions
4545

4646
def from_raw_tool_call(raw_message)
4747
call_details = JSON.parse(raw_message[:content], symbolize_names: true)
48+
result = []
49+
50+
if raw_message[:thinking] || raw_message[:redacted_thinking_signature]
51+
if raw_message[:thinking]
52+
result << {
53+
type: "thinking",
54+
thinking: raw_message[:thinking],
55+
signature: raw_message[:thinking_signature],
56+
}
57+
end
58+
59+
if raw_message[:redacted_thinking_signature]
60+
result << {
61+
type: "redacted_thinking",
62+
data: raw_message[:redacted_thinking_signature],
63+
}
64+
end
65+
end
66+
4867
tool_call_id = raw_message[:id]
49-
[
50-
{
51-
type: "tool_use",
52-
id: tool_call_id,
53-
name: raw_message[:name],
54-
input: call_details[:arguments],
55-
},
56-
]
68+
69+
result << {
70+
type: "tool_use",
71+
id: tool_call_id,
72+
name: raw_message[:name],
73+
input: call_details[:arguments],
74+
}
75+
76+
result
5777
end
5878

5979
def from_raw_tool(raw_message)

0 commit comments

Comments
 (0)