6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'app/models/raif/concerns/llms/open_ai_responses/response_tool_calls.rb', line 6
def (resp)
return if resp["output"].blank?
tool_calls = []
preceding_reasoning = []
resp["output"].each do |output_item|
if output_item["type"] == "reasoning"
preceding_reasoning << output_item if output_item["encrypted_content"].present?
next
end
unless output_item["type"] == "function_call"
preceding_reasoning = []
next
end
tool_call = {
"provider_tool_call_id" => output_item["call_id"],
"name" => output_item["name"],
"arguments" => begin
JSON.parse(output_item["arguments"])
rescue JSON::ParserError
output_item["arguments"]
end
}
if preceding_reasoning.any?
tool_call["provider_metadata"] = {
"reasoning_items" => preceding_reasoning,
"item_id" => output_item["id"],
"item_status" => output_item["status"]
}.compact
end
preceding_reasoning = []
tool_calls << tool_call
end
tool_calls.any? ? tool_calls : nil
end
|