Module: Legion::LLM::API::Namespaces::OpenAI::Audio::Speech

Extended by:
Legion::Logging::Helper, Sinatra::Extension
Defined in:
lib/legion/llm/api/namespaces/openai/audio/speech.rb

Constant Summary collapse

AUDIO_CONTENT_TYPES =
{
  'mp3'  => 'audio/mpeg',
  'opus' => 'audio/opus',
  'aac'  => 'audio/aac',
  'flac' => 'audio/flac',
  'wav'  => 'audio/wav',
  'pcm'  => 'audio/pcm'
}.freeze

Class Method Summary collapse

Class Method Details

.capable_provider_available?Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/legion/llm/api/namespaces/openai/audio/speech.rb', line 36

def self.capable_provider_available?
  instances = begin
    Legion::LLM::Call::Registry.all_instances
  rescue StandardError
    []
  end
  instances.any? do |entry|
    caps = entry[:capabilities] || entry['capabilities'] || []
    syms = caps.map(&:to_sym)
    syms.include?(:text_to_speech) || syms.include?(:audio_speech) || syms.include?(:tts)
  end
rescue StandardError => e
  log.warn("[llm][api][openai][audio][speech] action=capability_check error=#{e.message}")
  false
end

.extract_audio_bytes(pipeline_response, response_format:) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/legion/llm/api/namespaces/openai/audio/speech.rb', line 52

def self.extract_audio_bytes(pipeline_response, response_format:)
  _ = response_format # unused, retained for contract stability
  raw_msg = pipeline_response.message
  msg = raw_msg.respond_to?(:transform_keys) ? raw_msg.transform_keys(&:to_sym) : {}

  audio_binary = msg[:audio_binary]
  return audio_binary if audio_binary.is_a?(String) && !audio_binary.empty?

  audio_b64 = msg[:audio_b64]
  return Base64.strict_decode64(audio_b64) if audio_b64.is_a?(String) && !audio_b64.empty?

  # Fallback: provider returned text content
  content = msg[:content].to_s
  content.empty? ? '' : content.encode('BINARY', invalid: :replace, undef: :replace)
end

.resolve_audio_format(pipeline_response, requested_format:) ⇒ Object



68
69
70
71
72
73
# File 'lib/legion/llm/api/namespaces/openai/audio/speech.rb', line 68

def self.resolve_audio_format(pipeline_response, requested_format:)
  raw_msg = pipeline_response.message
  msg = raw_msg.respond_to?(:transform_keys) ? raw_msg.transform_keys(&:to_sym) : {}
  returned_fmt = (msg[:audio_format] || msg[:format]).to_s.downcase
  VALID_FORMATS.include?(returned_fmt) ? returned_fmt : requested_format
end