Module: Raif::Concerns::ProviderManagedToolCalls

Extended by:
ActiveSupport::Concern
Included in:
ModelCompletion
Defined in:
app/models/raif/concerns/provider_managed_tool_calls.rb

Instance Method Summary collapse

Instance Method Details

#provider_managed_image_output? ⇒ Boolean

True when a provider-managed tool produced a generated image. Unlike search or code execution, whose text follows in a message, the image is the response on its own. Not memoized: the blank-response guard re-checks the same record after each retry, and a memoized empty result would hide output from a later attempt.

Returns:

  • (Boolean)


31
32
33
34
35
# File 'app/models/raif/concerns/provider_managed_tool_calls.rb', line 31

def provider_managed_image_output?
  extract_provider_managed_tool_calls.any? do |tool_call|
    tool_call["tool_name"] == "image_generation" && tool_call.dig("raw_result", "result").present?
  end
end

#provider_managed_tool_calls ⇒ Object

Provider-managed tool data is not normalized by the provider SDKs the same way developer-managed tool calls are. This method smooths those differences into one admin-friendly structure for the model completion page.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/models/raif/concerns/provider_managed_tool_calls.rb', line 9

def provider_managed_tool_calls
  # Memoized for repeated reads during a request/render. This assumes the
  # completion's response payload is not mutated after first access.
  @provider_managed_tool_calls ||= begin
    tool_calls = extract_provider_managed_tool_calls
    tool_calls += inferred_provider_managed_tool_calls if tool_calls.none? { |call| call["tool_name"] == "web_search" }

    tool_calls.map do |tool_call|
      next tool_call unless tool_call["tool_name"] == "web_search"

      # Search sources can come from explicit provider result blocks
      # (Anthropic) or from top-level citations (OpenAI / Google), so we
      # merge both.
      tool_call.merge("sources" => merge_provider_managed_sources(tool_call["sources"], citations))
    end
  end
end

#sanitized_citations ⇒ Object

Returns citations with URLs sanitized to only allow http/https schemes.



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

def sanitized_citations
  @sanitized_citations ||= Array(citations).map do |citation|
    url = citation["url"]
    safe_url = url.present? && url.match?(%r{\Ahttps?://}i) ? url : nil
    citation.merge("url" => safe_url)
  end
end

#web_search_source_urls ⇒ Object

URLs of the pages a provider-managed web search returned, whether or not the response cites them. A model told to cite pages in a format of its own writes no provider citation, so its citations alone miss pages it did retrieve. Google stores only the cited pages, so for Google this returns the citations.



41
42
43
44
45
46
# File 'app/models/raif/concerns/provider_managed_tool_calls.rb', line 41

def web_search_source_urls
  provider_managed_tool_calls
    .select { |tool_call| tool_call["tool_name"] == "web_search" }
    .flat_map { |tool_call| tool_call["sources"].filter_map { |source| source["url"] } }
    .uniq
end