Sam 2c6459429f
DEV: use a proper object for tool definition (#1337)
* DEV: use a proper object for tool definition

This moves away from using a loose hash to define tools, which
is error prone.

Instead given a proper object we will also be able to coerce the
return values to match tool definition correctly

* fix xml tools

* fix anthropic tools

* fix specs... a few more to go

* specs are passing

* FIX: coerce values for XML tool calls

* Update spec/lib/completions/tool_definition_spec.rb

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-05-15 17:32:39 +10:00

65 lines
1.6 KiB
Ruby

# frozen_string_literal: true
module DiscourseAi
module Completions
module Dialects
class ClaudeTools
def initialize(tools)
@raw_tools = tools
end
def translated_tools
raw_tools.map do |t|
{ name: t.name, description: t.description, input_schema: t.parameters_json_schema }
end
end
def instructions
""
end
def from_raw_tool_call(raw_message)
call_details = JSON.parse(raw_message[:content], symbolize_names: true)
result = []
if raw_message[:thinking] || raw_message[:redacted_thinking_signature]
if raw_message[:thinking]
result << {
type: "thinking",
thinking: raw_message[:thinking],
signature: raw_message[:thinking_signature],
}
end
if raw_message[:redacted_thinking_signature]
result << {
type: "redacted_thinking",
data: raw_message[:redacted_thinking_signature],
}
end
end
tool_call_id = raw_message[:id]
result << {
type: "tool_use",
id: tool_call_id,
name: raw_message[:name],
input: call_details[:arguments],
}
result
end
def from_raw_tool(raw_message)
[{ type: "tool_result", tool_use_id: raw_message[:id], content: raw_message[:content] }]
end
private
attr_reader :raw_tools
end
end
end
end