Class: Raif::ModelToolInvocation

Inherits:
ApplicationRecord show all
Defined in:
app/models/raif/model_tool_invocation.rb

Overview

Schema Information

Table name: raif_model_tool_invocations

id                    :bigint           not null, primary key
completed_at          :datetime
failed_at             :datetime
result                :jsonb            not null
source_type           :string           not null
tool_arguments        :jsonb            not null
tool_type             :string           not null
created_at            :datetime         not null
updated_at            :datetime         not null
provider_tool_call_id :string
source_id             :bigint           not null

Indexes

index_raif_model_tool_invocations_on_source  (source_type,source_id)

Instance Method Summary collapse

Methods inherited from ApplicationRecord

table_name_prefix, where_json_not_blank

Instance Method Details

#as_tool_call_message(assistant_message: nil) ⇒ Hash

Returns tool call in the format expected by LLM message formatting

Parameters:

  • assistant_message (String, nil) (defaults to: nil)

    Optional assistant message accompanying the tool call

Returns:

  • (Hash)

    Hash representation for JSONB storage and LLM APIs



48
49
50
51
52
53
54
55
# File 'app/models/raif/model_tool_invocation.rb', line 48

def as_tool_call_message(assistant_message: nil)
  Raif::Messages::ToolCall.new(
    provider_tool_call_id: provider_tool_call_id,
    name: tool_name,
    arguments: tool_arguments,
    assistant_message: assistant_message
  ).to_h
end

#as_tool_call_result_messageHash

Returns tool result in the format expected by LLM message formatting

Returns:

  • (Hash)

    Hash representation for JSONB storage and LLM APIs



59
60
61
62
63
64
65
# File 'app/models/raif/model_tool_invocation.rb', line 59

def as_tool_call_result_message
  Raif::Messages::ToolCallResult.new(
    provider_tool_call_id: provider_tool_call_id,
    name: tool_name,
    result: result
  ).to_h
end

#ensure_valid_tool_argument_schemaObject



71
72
73
74
75
# File 'app/models/raif/model_tool_invocation.rb', line 71

def ensure_valid_tool_argument_schema
  unless JSON::Validator.validate(tool_arguments_schema, tool_arguments)
    errors.add(:tool_arguments, "does not match schema")
  end
end

#to_partial_pathObject



67
68
69
# File 'app/models/raif/model_tool_invocation.rb', line 67

def to_partial_path
  "raif/model_tool_invocations/#{tool.invocation_partial_name}"
end

#toolObject



41
42
43
# File 'app/models/raif/model_tool_invocation.rb', line 41

def tool
  @tool ||= tool_type.constantize
end