Module: OllamaChat::MessageOutput

Included in:
Chat
Defined in:
lib/ollama_chat/message_output.rb

Overview

A module that provides output functionality for chat messages.

This module encapsulates methods for piping assistant responses to command standard input and writing assistant responses to files. It handles the mechanics of sending output to external processes or saving content to disk while providing appropriate error handling and user message.

Examples:

Piping a response to a command

chat.pipe('cat > output.txt')

Writing a response to a file

chat.output('response.txt')

Instance Method Summary collapse

Instance Method Details

#attempt_to_write_file(filename, content) ⇒ TrueClass? (private)

The attempt_to_write_file method handles writing content to a file with overwrite confirmation.

This method checks if a file already exists and prompts the user for confirmation before overwriting it. If the user declines or if the file doesn't exist, the method returns early without writing. Otherwise, it opens the file in write mode and writes the message content to it.

Parameters:

  • filename (String)

    the path to the file where the content should be written

  • content (String)

    the actual text content to be written to the file

Returns:

  • (TrueClass)

    returns true if the file was successfully written

  • (nil)

    returns nil if the user chose not to overwrite or if an error occurred



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ollama_chat/message_output.rb', line 90

def attempt_to_write_file(filename, content)
  path = Pathname.new(filename.to_s).expand_path
  if !path.exist? ||
      confirm?(
        prompt: "🔔 File #{path.to_s.inspect} already exists, overwrite? (y/n) ",
        yes: /\Ay/i
      )
  then
    File.open(path, ?w) do |output|
      output.write(content)
    end
  else
    return
  end
  true
end

#output(filename, edit: false) ⇒ OllamaChat::Chat

The output method writes the last assistant message to a file.

Parameters:

  • filename (String)

    the path to the file where the last assistant message should be written

  • edit (truthy/falsy) (defaults to: false)

    whether to edit the content before saving (defaults to false)

Returns:



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ollama_chat/message_output.rb', line 55

def output(filename, edit: false)
  if message = @messages.last and message.role == 'assistant' and
    content = message.content
  then
    begin
      content = edit_text(content) if edit
      if attempt_to_write_file(filename, content)
        STDOUT.puts "Last response was written to #{filename.inspect}."
      end
      self
    rescue => e
      STDERR.puts "Writing to #{filename.inspect}, caused #{e.class}: #{e}."
    end
  else
    STDERR.puts "No response available to write to #{filename.inspect}."
  end
end

#pipe(cmd, edit: false) ⇒ OllamaChat::Chat?

The pipe method forwards the last assistant message to a command's standard input.

Parameters:

  • cmd (String)

    the command to which the output should be piped

  • edit (truthy/falsy) (defaults to: false)

    whether to edit the content before piping (defaults to false)

Returns:

  • (OllamaChat::Chat)

    returns self

  • (nil)

    returns nil if the command is not provided or if there is no assistant message



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ollama_chat/message_output.rb', line 23

def pipe(cmd, edit: false)
  cmd.present? or return
  if message = @messages.last and message.role == 'assistant' and
    content = message.content
  then
    content = edit_text(content) if edit
    begin
      IO.popen(cmd, ?w) do |output|
        output.write(content)
      end
      exit_code = $?&.exitstatus
      if exit_code == 0
        STDOUT.puts "Last response was piped to #{cmd.inspect}."
      else
        STDERR.puts "Executing #{cmd.inspect}, failed with exit code #{exit_code}."
      end
      self
    rescue => e
      STDERR.puts "Executing #{cmd.inspect}, caused #{e.class}: #{e}."
    end
  else
    STDERR.puts "No response available to output to pipe command #{cmd.inspect}."
  end
end