Class: Raif::ModelToolInvocation
- Inherits:
-
ApplicationRecord
- Object
- ApplicationRecord
- Raif::ModelToolInvocation
- 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
- #admin_formatted_result ⇒ Object
-
#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.
- #admin_formatted_result_error ⇒ Object
-
#as_tool_call_message(assistant_message: nil) ⇒ Hash
Returns tool call in the format expected by LLM message formatting.
-
#as_tool_call_result_message(result: self.result) ⇒ Hash
Returns tool result in the format expected by LLM message formatting.
- #ensure_valid_tool_argument_schema ⇒ Object
-
#format_result_for_llm ⇒ Object
Instance-aware shims so callers (Raif::Conversation, Raif::ConversationEntry, etc.) ask the invocation itself, which forwards
selfto the tool class. - #to_partial_path ⇒ Object
- #tool ⇒ Object
-
#tool_arguments_schema ⇒ Object
Routes through
tool_arguments_schema_for_sourceso the invocation validates against the schema the model saw when making the call. - #triggers_immediate_llm_follow_up? ⇒ Boolean
Instance Method Details
#admin_formatted_result ⇒ Object
94 95 96 |
# File 'app/models/raif/model_tool_invocation.rb', line 94 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.
105 106 107 |
# File 'app/models/raif/model_tool_invocation.rb', line 105 def admin_formatted_result_available? admin_formatted_result.present? || admin_formatted_result_error.present? end |
#admin_formatted_result_error ⇒ Object
98 99 100 |
# File 'app/models/raif/model_tool_invocation.rb', line 98 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
71 72 73 74 75 76 77 78 |
# File 'app/models/raif/model_tool_invocation.rb', line 71 def (assistant_message: nil) Raif::Messages::ToolCall.new( provider_tool_call_id: provider_tool_call_id, name: tool_name, arguments: tool_arguments, 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
82 83 84 85 86 87 88 |
# File 'app/models/raif/model_tool_invocation.rb', line 82 def (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_schema ⇒ Object
109 110 111 112 113 |
# File 'app/models/raif/model_tool_invocation.rb', line 109 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_llm ⇒ Object
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 42 43 44 45 46 47 |
# File 'app/models/raif/model_tool_invocation.rb', line 39 def format_result_for_llm tool.format_result_for_llm(self) rescue StandardError => e # A formatter that raises must not fail the conversation or agent run that was only # trying to build a message. Returning nil lets callers fall back to the raw persisted # result, which is always sendable. Raif.logger.error("Raif::ModelToolInvocation##{id}: #{tool_type} formatter failed: #{e.class}: #{e.}") nil end |
#to_partial_path ⇒ Object
90 91 92 |
# File 'app/models/raif/model_tool_invocation.rb', line 90 def to_partial_path "raif/model_tool_invocations/#{tool.invocation_partial_name}" end |
#tool ⇒ Object
56 57 58 |
# File 'app/models/raif/model_tool_invocation.rb', line 56 def tool @tool ||= tool_type.constantize end |
#tool_arguments_schema ⇒ Object
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.
64 65 66 |
# File 'app/models/raif/model_tool_invocation.rb', line 64 def tool_arguments_schema tool.tool_arguments_schema_for_source(source) end |
#triggers_immediate_llm_follow_up? ⇒ Boolean
49 50 51 |
# File 'app/models/raif/model_tool_invocation.rb', line 49 def triggers_immediate_llm_follow_up? tool.triggers_immediate_llm_follow_up?(self) end |