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_formatted_resultObject



88
89
90
# File 'app/models/raif/model_tool_invocation.rb', line 88

def admin_formatted_result
  admin_formatted_result_attempt[:formatted]
end

#admin_formatted_result_available?Boolean

True when admin should show the formatted result block — either we have something to display (the formatted payload or its raw-result fallback) or we have a formatter error to surface.

Returns:

  • (Boolean)


99
100
101
# File 'app/models/raif/model_tool_invocation.rb', line 99

def admin_formatted_result_available?
  admin_formatted_result.present? || admin_formatted_result_error.present?
end

#admin_formatted_result_errorObject



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

def admin_formatted_result_error
  admin_formatted_result_attempt[: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



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

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



76
77
78
79
80
81
82
# File 'app/models/raif/model_tool_invocation.rb', line 76

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



103
104
105
106
107
# File 'app/models/raif/model_tool_invocation.rb', line 103

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

#format_result_for_llmObject

Instance-aware shims so callers (Raif::Conversation, Raif::ConversationEntry, etc.) ask the invocation itself, which forwards self to the tool class. Lets tools decide per-invocation rather than at the class level.



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

def format_result_for_llm
  tool.format_result_for_llm(self)
end

#to_partial_pathObject



84
85
86
# File 'app/models/raif/model_tool_invocation.rb', line 84

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

#toolObject



50
51
52
# File 'app/models/raif/model_tool_invocation.rb', line 50

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.



58
59
60
# File 'app/models/raif/model_tool_invocation.rb', line 58

def tool_arguments_schema
  tool.tool_arguments_schema_for_source(source)
end

#triggers_immediate_llm_follow_up?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'app/models/raif/model_tool_invocation.rb', line 43

def triggers_immediate_llm_follow_up?
  tool.triggers_immediate_llm_follow_up?(self)
end