Class: Fastlane::Actions::OpenaiAskAction

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/wpmreleasetoolkit/actions/common/openai_ask_action.rb

Constant Summary collapse

OPENAI_API_ENDPOINT =
URI('https://api.openai.com/v1/chat/completions').freeze
DEFAULT_MAX_COMPLETION_TOKENS =

Preserve the previous ‘max_tokens` ceiling while using the current API field.

2048
DEFAULT_MAX_TOOL_ITERATIONS =
5
DEFAULT_MODEL =
'gpt-4.1'
PREDEFINED_PROMPTS =
{
  release_notes: <<~PROMPT
    Act like a mobile app marketer who wants to prepare release notes for Google Play and App Store.
    Do not write it point by point and keep it under 350 characters. It should be a unique paragraph.

    When provided a list, use the number of any potential "*" in brackets at the start of each item as indicator of importance.
    Ignore items starting with "[Internal]", and ignore links to GitHub.
  PROMPT
}.freeze

Documentation collapse

Class Method Summary collapse

Class Method Details

.authorsObject



303
304
305
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/openai_ask_action.rb', line 303

def self.authors
  ['Automattic']
end

.available_optionsObject



372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/openai_ask_action.rb', line 372

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :prompt,
                                 description: 'The internal top-level instructions to give to the model to tell it how to behave. ' \
                                  + "Use a Ruby Symbol from one of [#{available_prompt_symbols}] to use a predefined prompt instead of writing your own",
                                 optional: true,
                                 default_value: nil,
                                 type: String,
                                 skip_type_validation: true,
                                 verify_block: proc do |value|
                                   next if value.is_a?(String)
                                   next if PREDEFINED_PROMPTS.include?(value)

                                   UI.user_error!("Parameter `prompt` can only be a String or one of the following Symbols: [#{available_prompt_symbols}]")
                                 end),
    FastlaneCore::ConfigItem.new(key: :question,
                                 description: 'The user message to ask the question to the OpenAI model',
                                 optional: false,
                                 default_value: nil,
                                 type: String),
    FastlaneCore::ConfigItem.new(key: :api_token,
                                 description: 'The OpenAI API Token to use for the request',
                                 env_name: 'OPENAI_API_TOKEN',
                                 optional: false,
                                 sensitive: true,
                                 type: String),
    FastlaneCore::ConfigItem.new(key: :model,
                                 description: 'The OpenAI model to send the request to (e.g. `gpt-4.1`, `gpt-4.1-mini`, `gpt-4o`). ' \
                                              "Defaults to `#{DEFAULT_MODEL}`",
                                 optional: true,
                                 default_value: DEFAULT_MODEL,
                                 type: String),
    FastlaneCore::ConfigItem.new(key: :tools,
                                 description: 'Optional array of OpenAI function tool definitions. Each definition must have a non-empty `function.name`. ' \
                                              'When provided, the action runs a tool-use loop',
                                 optional: true,
                                 default_value: nil,
                                 type: Array,
                                 verify_block: proc do |value|
                                   validate_tools_array!(value)
                                   validate_tools!(value)
                                 end),
    FastlaneCore::ConfigItem.new(key: :tool_handlers,
                                 description: 'Hash of tool name to a callable (e.g. a Proc) invoked when the model calls that tool. ' \
                                              'The callable receives the parsed arguments Hash and must return a JSON-serializable value, ' \
                                              'which is sent back to the model as the tool result',
                                 optional: true,
                                 default_value: nil,
                                 type: Hash,
                                 verify_block: proc do |value|
                                   non_callable = value.reject { |_k, v| v.respond_to?(:call) }
                                   UI.user_error!("Parameter `tool_handlers` values must respond to :call. Non-callable handlers: #{non_callable.keys}") if non_callable.any?
                                 end),
    FastlaneCore::ConfigItem.new(key: :max_tool_iterations,
                                 description: 'Maximum number of local tool execution rounds before the action fails. ' \
                                              'The model can receive one final API turn to answer after the last permitted tool result. ' \
                                              'Only used when `tools` are provided',
                                 optional: true,
                                 default_value: DEFAULT_MAX_TOOL_ITERATIONS,
                                 type: Integer,
                                 verify_block: proc do |value|
                                   validate_max_tool_iterations!(value)
                                 end),
  ]
end

.available_prompt_symbolsObject



368
369
370
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/openai_ask_action.rb', line 368

def self.available_prompt_symbols
  PREDEFINED_PROMPTS.keys.map { |v| "`:#{v}`" }.join(',')
end

.descriptionObject



299
300
301
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/openai_ask_action.rb', line 299

def self.description
  'Use OpenAI API to generate response to a prompt'
end

.detailsObject



312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/openai_ask_action.rb', line 312

def self.details
  <<~DETAILS
    Uses the OpenAI API to generate response to a prompt.
    Can be used to e.g. ask it to generate Release Notes based on a bullet point technical changelog or similar.

    When `tools` and `tool_handlers` are provided, the action runs a tool-use (function-calling) loop:
    on each turn, if the model calls one or more tools, the corresponding handler is invoked locally
    and its return value is sent back to the model as a `role: tool` message. The loop ends when the
    model returns a plain text response, or before executing tool calls beyond `max_tool_iterations`.
    The model gets one final API turn to answer after the last permitted local tool execution round.
  DETAILS
end

.examplesObject



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/openai_ask_action.rb', line 325

def self.examples
  [
    <<~'EXAMPLE',
    <<~'EXAMPLE',
      # Tool-use loop: the model proposes release notes via a tool call; the handler validates
      # length locally and rejects until the model produces text under the limit.
      notes = openai_ask(
        prompt: :release_notes,
        question: "Write release notes for: #{items}. Call the validate_length tool with your draft and iterate until it accepts.",
        api_token: get_required_env('OPENAI_API_TOKEN'),
        tools: [{
          type: 'function',
          function: {
            name: 'validate_length',
            description: 'Validates the length of the proposed release notes against a 350-character budget. ' \
                         'Returns `{ ok: true, length: }` if the text fits, or `{ ok: false, length:, max: }` otherwise. ' \
                         'Call repeatedly with shorter drafts until it returns ok: true.',
            parameters: {
              type: 'object',
              properties: { text: { type: 'string' } },
              required: ['text']
            }
          }
        }],
        tool_handlers: {
          'validate_length' => ->(args) {
            len = args['text'].length
            len <= 350 ? { ok: true, length: len } : { ok: false, length: len, max: 350 }
          }
        }
      )
    EXAMPLE
  ]
end

.execute_tool_call(tool_call, tool_handlers) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/openai_ask_action.rb', line 196

def self.execute_tool_call(tool_call, tool_handlers)
  return unsupported_tool_call_result(tool_call) unless function_tool_call?(tool_call)

  name = tool_call.dig('function', 'name').to_s
  raw_args = tool_call.dig('function', 'arguments') || '{}'

  result =
    begin
      args = JSON.parse(raw_args)
      invoke_tool_handler(name: name, handler: tool_handlers[name], args: args)
    rescue JSON::ParserError
      # Short-circuit: the handler never sees malformed args. Tell the model the
      # tool-call payload was invalid so it can retry with valid JSON, and log the
      # local failure without recording raw arguments that might contain secrets.
      UI.error("Invalid JSON arguments for tool '#{name}' in tool call '#{tool_call['id']}'. Raw payload omitted because it may contain secrets.")
      { error: "Invalid JSON arguments for tool '#{name}' — payload could not be parsed. Retry with valid JSON." }
    end

  {
    role: 'tool',
    tool_call_id: tool_call['id'],
    content: serialize_tool_result(name: name, result: result)
  }
end

.format_message(role:, text:) ⇒ Object



128
129
130
131
132
133
134
135
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/openai_ask_action.rb', line 128

def self.format_message(role:, text:)
  return nil if text.nil? || text.empty?

  {
    role: role,
    content: [{ type: 'text', text: text }]
  }
end

.function_tool_call?(tool_call) ⇒ Boolean

Returns:

  • (Boolean)


221
222
223
224
225
226
227
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/openai_ask_action.rb', line 221

def self.function_tool_call?(tool_call)
  return false unless tool_call['type'] == 'function'
  return false unless tool_call['function'].is_a?(Hash)

  name = tool_call.dig('function', 'name')
  valid_tool_name?(name)
end

.invoke_tool_handler(name:, handler:, args:) ⇒ Object

Invokes a tool handler safely. Returns a JSON-serializable value that will be sent back to the model as the ‘content` of a `role: tool` message (the value may be a Hash, Array, scalar, etc. — whatever the handler returns).

  • Missing or non-callable handler: structured ‘{ error: … }` so the model can recover.

  • Handler raised: structured ‘{ error:, exception: }` carrying only the exception class so the model can see the failure category and adjust. The exception message and backtrace are intentionally omitted from local logs and from the model response because tool results and CI logs can expose release secrets (tokens, file contents, internal API responses). The loop keeps going rather than aborting the lane mid-conversation — the model is the better judge of whether the failure is recoverable than a global `rescue` here.



283
284
285
286
287
288
289
290
291
292
293
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/openai_ask_action.rb', line 283

def self.invoke_tool_handler(name:, handler:, args:)
  return { error: "No handler defined for tool '#{name}'" } if handler.nil?
  return { error: "Handler for tool '#{name}' is not callable (got #{handler.class})" } unless handler.respond_to?(:call)

  begin
    handler.call(args)
  rescue StandardError => e
    UI.error("Handler for tool '#{name}' raised #{e.class}. Error message and backtrace omitted because they may contain secrets.")
    { error: "Handler for tool '#{name}' raised an exception", exception: e.class.name }
  end
end

.is_supported?(_platform) ⇒ Boolean

Returns:

  • (Boolean)


438
439
440
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/openai_ask_action.rb', line 438

def self.is_supported?(_platform)
  true
end

.parse_assistant_message(response) ⇒ Object



147
148
149
150
151
152
153
154
155
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/openai_ask_action.rb', line 147

def self.parse_assistant_message(response)
  case response
  when Net::HTTPOK
    json = JSON.parse(response.body)
    json['choices']&.first&.dig('message') || {}
  else
    UI.user_error!("Error in OpenAI API response: #{response}. #{response.body}")
  end
end

.parse_text_response(response) ⇒ Object



137
138
139
140
141
142
143
144
145
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/openai_ask_action.rb', line 137

def self.parse_text_response(response)
  case response
  when Net::HTTPOK
    json = JSON.parse(response.body)
    json['choices']&.first&.dig('message', 'content')
  else
    UI.user_error!("Error in OpenAI API response: #{response}. #{response.body}")
  end
end

.request_body(prompt:, question:, model: DEFAULT_MODEL) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/openai_ask_action.rb', line 100

def self.request_body(prompt:, question:, model: DEFAULT_MODEL)
  {
    model: model,
    store: false,
    response_format: { type: 'text' },
    temperature: 1,
    max_completion_tokens: DEFAULT_MAX_COMPLETION_TOKENS,
    top_p: 1,
    messages: [
      format_message(role: 'system', text: prompt),
      format_message(role: 'user', text: question),
    ].compact
  }.to_json
end

.request_body_with_messages(messages:, tools:, model: DEFAULT_MODEL) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/openai_ask_action.rb', line 115

def self.request_body_with_messages(messages:, tools:, model: DEFAULT_MODEL)
  {
    model: model,
    store: false,
    response_format: { type: 'text' },
    temperature: 1,
    max_completion_tokens: DEFAULT_MAX_COMPLETION_TOKENS,
    top_p: 1,
    messages: messages,
    tools: tools
  }.to_json
end

.return_valueObject



307
308
309
310
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/openai_ask_action.rb', line 307

def self.return_value
  'The response text from the prompt as returned by OpenAI API. ' \
    'When `tools` are provided, returns the assistant content from the first turn that produces a non-tool-call response.'
end

.run(params) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/openai_ask_action.rb', line 26

def self.run(params)
  api_token = params[:api_token]
  prompt = params[:prompt]
  prompt = PREDEFINED_PROMPTS[prompt] if PREDEFINED_PROMPTS.key?(prompt)
  question = params[:question]
  model = params[:model] || DEFAULT_MODEL
  tools = params[:tools]
  # Tool names from the OpenAI API are always JSON strings. Normalize handler keys so
  # callers can register handlers with either string or symbol keys without surprises.
  tool_handlers = (params[:tool_handlers] || {}).transform_keys(&:to_s)
  max_tool_iterations = params[:max_tool_iterations] || DEFAULT_MAX_TOOL_ITERATIONS

  headers = {
    'Content-Type': 'application/json',
    Authorization: "Bearer #{api_token}"
  }

  # Backwards-compatible single-shot path when no tools are provided.
  if tools.nil?
    body = request_body(prompt: prompt, question: question, model: model)
    response = Net::HTTP.post(OPENAI_API_ENDPOINT, body, headers)
    return parse_text_response(response)
  end

  validate_tools_array!(tools)
  validate_max_tool_iterations!(max_tool_iterations)
  validate_tools!(tools)

  run_with_tools(
    prompt: prompt,
    question: question,
    model: model,
    tools: tools,
    tool_handlers: tool_handlers,
    max_tool_iterations: max_tool_iterations,
    headers: headers
  )
end

.run_with_tools(prompt:, question:, model:, tools:, tool_handlers:, max_tool_iterations:, headers:) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/openai_ask_action.rb', line 65

def self.run_with_tools(prompt:, question:, model:, tools:, tool_handlers:, max_tool_iterations:, headers:)
  messages = [
    format_message(role: 'system', text: prompt),
    format_message(role: 'user', text: question),
  ].compact

  tool_iterations = 0

  loop do
    body = request_body_with_messages(messages: messages, tools: tools, model: model)
    response = Net::HTTP.post(OPENAI_API_ENDPOINT, body, headers)
    assistant_message = parse_assistant_message(response)
    tool_calls = assistant_message['tool_calls']

    # No tool calls — model produced a final answer.
    return assistant_message['content'] if tool_calls.nil? || tool_calls.empty?

    if tool_iterations >= max_tool_iterations
      UI.user_error!(
        "OpenAI tool-use loop did not produce a final answer after #{max_tool_iterations} tool iterations. " \
        'Refusing to execute additional tool calls. Increase `max_tool_iterations` or check that your prompt instructs the model to stop calling tools.'
      )
    end

    # Append the assistant's tool-call message verbatim, then run each handler
    # and append the corresponding `role: tool` results.
    messages << assistant_message
    tool_calls.each do |tool_call|
      messages << execute_tool_call(tool_call, tool_handlers)
    end

    tool_iterations += 1
  end
end

.serialize_tool_result(name:, result:) ⇒ Object

Serializes a tool result to a JSON string. Handlers are contracted to return JSON-serializable values, but a buggy handler might return something like a ‘Pathname`, `Proc`, or a custom object whose `to_json` raises. Failing the whole conversation over a serialization error is harsh — instead, log locally and send a structured `{ error: … }` back so the model can recover.

The handler’s class name is exposed (handler authorship is local, not secret) but the exception’s message is NOT forwarded — same reasoning as ‘invoke_tool_handler`: handler-returned objects can carry secrets.



264
265
266
267
268
269
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/openai_ask_action.rb', line 264

def self.serialize_tool_result(name:, result:)
  JSON.generate(result)
rescue StandardError => e
  UI.error("Could not serialize tool result for '#{name}': #{e.class}. Result class: #{result.class}. Error message omitted because it may contain secrets.")
  JSON.generate({ error: "Tool result for '#{name}' could not be serialized to JSON. Returned class: #{result.class}." })
end

.tool_type(tool) ⇒ Object



186
187
188
189
190
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/openai_ask_action.rb', line 186

def self.tool_type(tool)
  return nil unless tool.is_a?(Hash)

  (tool[:type] || tool['type'])&.to_s
end

.unsupported_tool_call_result(tool_call) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/openai_ask_action.rb', line 229

def self.unsupported_tool_call_result(tool_call)
  type = tool_call['type'] || '<missing>'
  error =
    if type == 'function'
      'Function tool call is missing a non-empty function.name.'
    else
      "Unsupported tool call type '#{type}'. Only function tool calls are supported."
    end
  log_message =
    if type == 'function'
      "Invalid OpenAI function tool call '#{tool_call['id']}': missing a non-empty function.name."
    else
      "Unsupported OpenAI tool call type '#{type}' in tool call '#{tool_call['id']}'. Only function tool calls are supported."
    end
  UI.error(log_message)

  {
    role: 'tool',
    tool_call_id: tool_call['id'],
    content: serialize_tool_result(
      name: type,
      result: { error: error }
    )
  }
end

.valid_tool_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


192
193
194
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/openai_ask_action.rb', line 192

def self.valid_tool_name?(name)
  (name.is_a?(String) || name.is_a?(Symbol)) && !name.to_s.empty?
end

.validate_max_tool_iterations!(max_tool_iterations) ⇒ Object



157
158
159
160
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/openai_ask_action.rb', line 157

def self.validate_max_tool_iterations!(max_tool_iterations)
  UI.user_error!("Parameter `max_tool_iterations` must be an Integer (got #{max_tool_iterations.class})") unless max_tool_iterations.is_a?(Integer)
  UI.user_error!("Parameter `max_tool_iterations` must be >= 1 (got #{max_tool_iterations})") if max_tool_iterations < 1
end

.validate_tools!(tools) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/openai_ask_action.rb', line 166

def self.validate_tools!(tools)
  invalid_tools = tools.each_with_index.filter_map do |tool, index|
    type = tool_type(tool)
    next "tools[#{index}] type #{type.nil? ? '<missing>' : type.inspect}" unless type == 'function'

    function = tool[:function] || tool['function']
    name = function[:name] || function['name'] if function.is_a?(Hash)
    next if valid_tool_name?(name)

    "tools[#{index}] missing function.name"
  end

  return if invalid_tools.empty?

  UI.user_error!(
    'Parameter `tools` only supports OpenAI function tools with a non-empty `function.name`. ' \
    "Invalid tool definitions: #{invalid_tools.join(', ')}"
  )
end

.validate_tools_array!(tools) ⇒ Object



162
163
164
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/openai_ask_action.rb', line 162

def self.validate_tools_array!(tools)
  UI.user_error!('Parameter `tools` must be a non-empty Array when provided') unless tools.is_a?(Array) && !tools.empty?
end