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 feedback.
Instance Method Summary collapse
-
#attempt_to_write_file(filename, message) ⇒ TrueClass?
private
The attempt_to_write_file method handles writing content to a file with overwrite confirmation.
-
#output(filename) ⇒ OllamaChat::Chat
The output method writes the last assistant message to a file.
-
#pipe(cmd) ⇒ OllamaChat::Chat?
The pipe method forwards the last assistant message to a command’s standard input.
Instance Method Details
#attempt_to_write_file(filename, message) ⇒ 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.
80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/ollama_chat/message_output.rb', line 80 def attempt_to_write_file(filename, ) if !File.exist?(filename) || ask?(prompt: "File #{filename.inspect} already exists, overwrite? (y/n) ") =~ /\Ay/i then File.open(filename, ?w) do |output| output.write(.content) end else return end true end |
#output(filename) ⇒ OllamaChat::Chat
The output method writes the last assistant message to a file.
message should be written
50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/ollama_chat/message_output.rb', line 50 def output(filename) if = @messages.last and .role == 'assistant' begin if attempt_to_write_file(filename, ) 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) ⇒ OllamaChat::Chat?
The pipe method forwards the last assistant message to a command’s standard input.
no assistant message
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/ollama_chat/message_output.rb', line 22 def pipe(cmd) cmd.present? or return if = @messages.last and .role == 'assistant' 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 |