Sha256: c58099024de2b7ab8787ef0d653c3deb88c3008630205f4edb719367e2948c1b

Contents?: true

Size: 1.17 KB

Versions: 3

Compression:

Stored size: 1.17 KB

Contents

require 'shellwords'

class Ollama::Handlers::Say
  include Ollama::Handlers::Concern

  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

  attr_reader :voice

  attr_reader :interactive

  def call(response)
    if @output.closed?
      wait_output_pid
      @output     = open_output
      @output_pid = @output.pid
    end
    if content = response.response || response.message&.content
      @output.print content
    end
    response.done and @output.close
    self
  end

  private

  def open_output
    io = IO.popen(Shellwords.join(command(voice:, interactive:)), 'w')
    io.sync = true
    io
  end

  def wait_output_pid
    @output_pid or return
    Process.wait(@output_pid, Process::WNOHANG | Process::WUNTRACED)
  rescue Errno::ECHILD
  end

  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
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ollama-ruby-0.12.1 lib/ollama/handlers/say.rb
ollama-ruby-0.12.0 lib/ollama/handlers/say.rb
ollama-ruby-0.11.0 lib/ollama/handlers/say.rb