Module: OpenAI::Helpers::Agents::Tools Private

Defined in:
lib/openai/helpers/agents/tools.rb

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Class Method Summary collapse

Class Method Details

.content_output?(parts) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/openai/helpers/agents/tools.rb', line 40

def self.content_output?(parts)
  parts.all? do |part|
    next false unless part.is_a?(Hash)

    case part["type"]
    when "input_text"
      part["text"].is_a?(String)
    when "input_image"
      part["image_url"].is_a?(String)
    else
      false
    end
  end
end

.pending_call_race?(error, call_id) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



55
56
57
58
59
60
61
# File 'lib/openai/helpers/agents/tools.rb', line 55

def self.pending_call_race?(error, call_id)
  body = error.body
  body = body[:error] if body.is_a?(Hash) && body[:error].is_a?(Hash)
  body.is_a?(Hash) &&
    body[:code] == "invalid_request_error" &&
    body[:message] == "Unknown pending tool call: #{call_id}"
end

.prepare(call, handler) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Capture routing and detached arguments before caller code sees the event, but defer the application callback until iteration resumes after yielding it.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/openai/helpers/agents/tools.rb', line 10

def self.prepare(call, handler)
  base = {type: :"agent.session.input.tool_result", turn_id: call.turn_id.dup, call_id: call.call_id.dup}
  arguments = begin
    JSON.parse(call.arguments.is_a?(String) ? call.arguments : JSON.generate(call.arguments))
  rescue StandardError
    nil
  end

  lambda do
    begin
      raise ArgumentError, "Function arguments must be a JSON object" unless arguments.is_a?(Hash)

      output = handler.call(arguments)
      output = JSON.generate(output) if output.is_a?(Hash)
      unless output.nil? || output.is_a?(String) || output.is_a?(Array)
        raise ArgumentError, "Tool output must be a string, object, content array, or nil"
      end

      serialized = JSON.generate(output)
      if output.is_a?(Array) && !content_output?(JSON.parse(serialized))
        raise ArgumentError, "Tool output array must contain input text or image content"
      end

      base.merge(success: true, output: output)
    rescue StandardError
      base.merge(success: false, error: "Tool handler failed.")
    end
  end
end