Class: Ollama::Handlers::Say
- Inherits:
- 
      Object
      
        - Object
- Ollama::Handlers::Say
 
- Includes:
- Concern
- Defined in:
- lib/ollama/handlers/say.rb
Overview
A handler that uses the system’s say command to speak response content.
The Say handler is designed to convert text responses from Ollama API commands into audible speech using the operating system’s native text-to-speech capabilities. It supports customization of voice and interactive modes, making it suitable for applications where audio feedback is preferred over visual display.
Instance Attribute Summary collapse
- 
  
    
      #interactive  ⇒ TrueClass, ... 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    The interactive attribute reader returns the interactive mode setting associated with the object. 
- 
  
    
      #voice  ⇒ String 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    The voice attribute reader returns the voice associated with the object. 
Attributes included from Concern
Instance Method Summary collapse
- 
  
    
      #call(response)  ⇒ self 
    
    
  
  
  
  
  
  
  
  
  
    The call method processes a response by printing its content to the output stream. 
- 
  
    
      #command(voice:, interactive:)  ⇒ Array<String> 
    
    
  
  
  
  
  private
  
  
  
  
    The command method constructs a say command array with specified voice and interactive options. 
- 
  
    
      #initialize(output: nil, voice: 'Samantha', interactive: nil)  ⇒ Say 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    The initialize method sets up a new handler instance with the specified output destination and configures voice and interactive settings for text-to-speech functionality. 
- 
  
    
      #open_output  ⇒ IO 
    
    
  
  
  
  
  private
  
  
  
  
    The open_output method creates a new IO object for handling speech output. 
- 
  
    
      #wait_output_pid  ⇒ Object 
    
    
  
  
  
  
  private
  
  
  
  
    The wait_output_pid method waits for the output process to complete. 
Methods included from Concern
Constructor Details
#initialize(output: nil, voice: 'Samantha', interactive: nil) ⇒ Say
The initialize method sets up a new handler instance with the specified output destination and configures voice and interactive settings for text-to-speech functionality.
mode setting for speech synthesis, defaults to nil
| 27 28 29 30 31 32 33 34 35 | # File 'lib/ollama/handlers/say.rb', line 27 def initialize(output: nil, voice: 'Samantha', interactive: nil) @voice = voice @interactive = interactive super(output:) unless output @output = open_output @output_pid = @output.pid end end | 
Instance Attribute Details
#interactive ⇒ TrueClass, ... (readonly)
The interactive attribute reader returns the interactive mode setting associated with the object.
stored in the instance variable
| 47 48 49 | # File 'lib/ollama/handlers/say.rb', line 47 def interactive @interactive end | 
#voice ⇒ String (readonly)
The voice attribute reader returns the voice associated with the object.
| 40 41 42 | # File 'lib/ollama/handlers/say.rb', line 40 def voice @voice end | 
Instance Method Details
#call(response) ⇒ self
The call method processes a response by printing its content to the output stream.
This method handles the display of response content by extracting text from the response object and writing it to the configured output stream. It manages the output stream state, reopening it if necessary when it has been closed, and ensures proper handling of streaming responses by closing the output stream when the response indicates completion.
| 59 60 61 62 63 64 65 66 67 68 69 70 | # File 'lib/ollama/handlers/say.rb', line 59 def call(response) if @output.closed? wait_output_pid @output = open_output @output_pid = @output.pid end if content = response.response || response.&.content @output.print content end response.done and @output.close self end | 
#command(voice:, interactive:) ⇒ Array<String> (private)
The command method constructs a say command array with specified voice and interactive options.
This method builds an array representing a system command for the ‘say’ utility, incorporating the provided voice and interactive settings to configure text-to-speech behavior.
mode setting for speech synthesis
| 113 114 115 116 117 118 119 120 121 122 123 | # File 'lib/ollama/handlers/say.rb', line 113 def command(voice:, interactive:) command = [ 'say' ] voice and command.concat([ '-v', voice ]) case interactive when true command << '-i' when String command << '--interactive=%s' % interactive end command end | 
#open_output ⇒ IO (private)
The open_output method creates a new IO object for handling speech output.
This method initializes a pipe to the system’s say command, configuring it with the specified voice and interactive settings. It returns an IO object that can be used to write text content which will be converted to speech by the operating system.
| 82 83 84 85 86 | # File 'lib/ollama/handlers/say.rb', line 82 def open_output io = IO.popen(Shellwords.join(command(voice:, interactive:)), 'w') io.sync = true io end | 
#wait_output_pid ⇒ Object (private)
The wait_output_pid method waits for the output process to complete.
This method checks if there is an active output process ID and waits for it to finish execution. It uses non-blocking wait to avoid hanging the main thread. If the process has already terminated, it handles the Errno::ECHILD exception gracefully without raising an error.
| 95 96 97 98 99 | # File 'lib/ollama/handlers/say.rb', line 95 def wait_output_pid @output_pid or return Process.wait(@output_pid, Process::WNOHANG | Process::WUNTRACED) rescue Errno::ECHILD end |