Class: Raif::ModelToolInvocation

Inherits:
ApplicationRecord
  • Object
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

Instance Method Details

#admin_observationObject



78
79
80
# File 'app/models/raif/model_tool_invocation.rb', line 78

def admin_observation
  admin_observation_result[:observation]
end

#admin_observation_available?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'app/models/raif/model_tool_invocation.rb', line 86

def admin_observation_available?
  admin_observation.present? || admin_observation_error.present?
end

#admin_observation_errorObject



82
83
84
# File 'app/models/raif/model_tool_invocation.rb', line 82

def admin_observation_error
  admin_observation_result[:error]
end

#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



55
56
57
58
59
60
61
62
# File 'app/models/raif/model_tool_invocation.rb', line 55

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_message(result: self.result) ⇒ Hash

Returns tool result in the format expected by LLM message formatting

Returns:

  • (Hash)

    Hash representation for JSONB storage and LLM APIs



66
67
68
69
70
71
72
# File 'app/models/raif/model_tool_invocation.rb', line 66

def as_tool_call_result_message(result: self.result)
  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



90
91
92
93
94
# File 'app/models/raif/model_tool_invocation.rb', line 90

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



74
75
76
# File 'app/models/raif/model_tool_invocation.rb', line 74

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

#toolObject



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

def tool
  @tool ||= tool_type.constantize
end

#tool_arguments_schemaObject

Routes through tool_arguments_schema_for_source so the invocation validates against the schema the model saw when making the call. The helper forwards source: only to tools whose tool_arguments_schema accepts it, keeping existing overrides that predate the keyword working.



48
49
50
# File 'app/models/raif/model_tool_invocation.rb', line 48

def tool_arguments_schema
  tool.tool_arguments_schema_for_source(source)
end