Class: OllamaChat::Say

Inherits:
Ollama::Handlers::Say
  • Object
show all
Includes:
Speaker
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

Methods included from Speaker

#cancel_speaking, #speak, #wait_for_speaker

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)



29
30
31
32
33
# File 'lib/ollama_chat/say.rb', line 29

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

Class Method Details

.voices(**_kwargs) ⇒ Array<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



21
22
23
# File 'lib/ollama_chat/say.rb', line 21

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