Class: Fastlane::Helper::Providers::OpenAIProvider

Inherits:
BaseProvider
  • Object
show all
Defined in:
lib/fastlane/plugin/translate_gpt_release_notes/helper/providers/openai_provider.rb

Overview

Provider implementation for OpenAI GPT translation API. Supports various GPT models including gpt-5.2 for translation tasks.

Constant Summary collapse

DEFAULT_MODEL =

Default model for OpenAI translations

'gpt-5.2'.freeze
DEFAULT_TEMPERATURE =

Default temperature for translation generation (0.5 = balanced creativity)

0.5
DEFAULT_TIMEOUT =

Default request timeout in seconds

30

Constants inherited from BaseProvider

BaseProvider::ANDROID_CHAR_LIMIT

Instance Attribute Summary

Attributes inherited from BaseProvider

#config_errors, #params

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseProvider

#valid?

Constructor Details

#initialize(params) ⇒ OpenAIProvider

Initializes the OpenAI provider with configuration parameters. Sets up the OpenAI::Client with appropriate credentials and timeout.

Parameters:

  • params (Hash)

    Configuration parameters for the provider



56
57
58
59
60
61
62
63
64
# File 'lib/fastlane/plugin/translate_gpt_release_notes/helper/providers/openai_provider.rb', line 56

def initialize(params)
  super

  timeout = normalized_timeout
  @client = OpenAI::Client.new(
    access_token: credential(:api_token),
    request_timeout: timeout
  )
end

Class Method Details

.display_nameString

Returns the human-readable display name for the provider.

Returns:

  • (String)

    Human-readable name



29
30
31
# File 'lib/fastlane/plugin/translate_gpt_release_notes/helper/providers/openai_provider.rb', line 29

def self.display_name
  'OpenAI GPT'
end

.optional_paramsHash

Returns a hash of optional parameter definitions for this provider.

Returns:

  • (Hash)

    Optional parameter definitions



43
44
45
46
47
48
49
50
# File 'lib/fastlane/plugin/translate_gpt_release_notes/helper/providers/openai_provider.rb', line 43

def self.optional_params
  {
    model_name: { default: DEFAULT_MODEL, description: 'OpenAI model to use' },
    temperature: { default: DEFAULT_TEMPERATURE, description: 'Sampling temperature (0-2)' },
    service_tier: { default: nil, description: 'Service tier (e.g., "flex")' },
    request_timeout: { default: DEFAULT_TIMEOUT, description: 'Request timeout in seconds' }
  }
end

.provider_nameString

Returns the provider identifier string.

Returns:

  • (String)

    Provider identifier



22
23
24
# File 'lib/fastlane/plugin/translate_gpt_release_notes/helper/providers/openai_provider.rb', line 22

def self.provider_name
  'openai'
end

.required_credentialsArray<Symbol>

Returns the list of required credential symbols for this provider.

Returns:

  • (Array<Symbol>)

    Array of required credential keys



36
37
38
# File 'lib/fastlane/plugin/translate_gpt_release_notes/helper/providers/openai_provider.rb', line 36

def self.required_credentials
  [:api_token]
end

Instance Method Details

#translate(text, source_locale, target_locale, glossary_terms: {}) ⇒ String?

Translates text from source locale to target locale using OpenAI’s API. Uses a system message for instructions and a user message for the text, which improves instruction following for glossary terms and output format.

Parameters:

  • text (String)

    The text to translate

  • source_locale (String)

    Source language code (e.g., ‘en’, ‘de’)

  • target_locale (String)

    Target language code (e.g., ‘es’, ‘fr’)

  • glossary_terms (Hash) (defaults to: {})

    Optional glossary { source_term => target_translation }

Returns:

  • (String, nil)

    Translated text or nil on error



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/fastlane/plugin/translate_gpt_release_notes/helper/providers/openai_provider.rb', line 83

def translate(text, source_locale, target_locale, glossary_terms: {})
  # Build system instruction and user content separately for better results
  system_instruction = build_system_instruction(
    source_locale, target_locale,
    glossary_terms: glossary_terms,
    platform: @params[:platform]
  )
  user_content = text

  # Build parameters hash with separate system and user messages
  parameters = {
    model: @params[:model_name] || DEFAULT_MODEL,
    messages: [
      { role: 'system', content: system_instruction },
      { role: 'user', content: user_content }
    ],
    temperature: (@params[:temperature] || DEFAULT_TEMPERATURE).to_f
  }

  # Add service_tier if present
  service_tier = @params[:service_tier].to_s.strip
  parameters[:service_tier] = service_tier unless service_tier.empty?

  # Make API call
  response = @client.chat(parameters: parameters)

  # Handle errors and extract text
  if (error = response.dig('error', 'message'))
    UI.error "OpenAI translation error: #{error}"
    nil
  else
    enforce_android_limit(response.dig('choices', 0, 'message', 'content')&.strip)
  end
rescue StandardError => e
  UI.error "OpenAI provider error: #{e.message}"
  nil
end

#validate_config!void

This method returns an undefined value.

Validates the provider configuration. Ensures that the required api_token credential is present.



70
71
72
# File 'lib/fastlane/plugin/translate_gpt_release_notes/helper/providers/openai_provider.rb', line 70

def validate_config!
  require_credential(:api_token)
end