Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions lib/ruby_llm/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@ def encoded
def save(path)
return unless io_like?

File.open(path, 'w') do |f|
f.puts(@source.read)
end
@source.rewind if @source.respond_to?(:rewind)
File.binwrite(path, @source.read)
end

def for_llm
Expand Down
15 changes: 15 additions & 0 deletions spec/ruby_llm/attachment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require 'spec_helper'
require 'open3'
require 'rbconfig'
require 'stringio'
require 'tempfile'

RSpec.describe RubyLLM::Attachment do
it 'supports path attachments from the public API' do
Expand All @@ -22,4 +24,17 @@
expect(status.success?).to be(true), stderr
expect(stdout.strip).to eq('ruby.txt,text/plain')
end

it 'saves io attachments without altering binary content' do
binary_content = "\x00\xFFbinary\npayload".b
io = StringIO.new(binary_content)
io.read(2)
attachment = described_class.new(io, filename: 'payload.bin')

Tempfile.create('attachment') do |file|
attachment.save(file.path)

expect(File.binread(file.path)).to eq(binary_content)
end
end
end