Class: OllamaChat::Say

Inherits:
Ollama::Handlers::Say
  • Object
show all
Defined in:
lib/ollama_chat/say.rb

Overview

A handler that uses the system's say command to speak response content.

This class extends Ollama::Handlers::Say to provide voice validation against the system's available voices. It ensures that only valid voices are used, falling back to nil if an invalid voice is specified.

Examples:

Using the Say handler

OllamaChat::Say.new(chat: chat, voice: 'Samantha')

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chat:, voice: nil) ⇒ Say

Initializes a new Say handler.

Parameters:

  • chat (OllamaChat::Chat)

    the chat instance

  • voice (String, nil) (defaults to: nil)

    the voice to use (must be in the list of available voices)



25
26
27
28
29
# File 'lib/ollama_chat/say.rb', line 25

def initialize(chat:, voice: nil)
  voice && self.class.voices.member?(voice) or voice = nil
  @chat  = chat
  super(voice:)
end

Class Method Details

.voicesArray<String>

Returns a list of available system voices.

This method queries the system's say command to retrieve the list of available voices. If running within RSpec, it returns an empty array to avoid side effects during testing.

Returns:

  • (Array<String>)

    a sorted list of unique voice identifiers



17
18
19
# File 'lib/ollama_chat/say.rb', line 17

def self.voices
  `say -v '?' 2>/dev/null`.lines.map { |l| l[/^(.+?)\s+[a-z]{2}_[a-zA-Z0-9]{2,}/, 1] }.uniq.sort
end