Class: Brute::Session

Inherits:
Array
  • Object
show all
Defined in:
lib/brute/session.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: nil) ⇒ Session

Returns a new instance of Session.



10
11
12
13
14
# File 'lib/brute/session.rb', line 10

def initialize(path: nil)
  super()
  @path = path
  FileUtils.mkdir_p(File.dirname(@path)) if @path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/brute/session.rb', line 8

def path
  @path
end

Class Method Details

.from_jsonl(path) ⇒ Object

Load a session from a JSONL file. Subsequent appends will persist back to the same file automatically.



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/brute/session.rb', line 18

def self.from_jsonl(path)
  new(path: path).tap do |session|
    if File.exist?(path)
      File.foreach(path).map(&:strip).each do |line|
        if line.present?
          # Use push to bypass << persistence (avoids re-writing existing lines)
          session.push(RubyLLM::Message.new(**JSON.parse(line, symbolize_names: true)))
        end
      end
    end
  end
end

Instance Method Details

#<<(msg) ⇒ Object

Append a message and persist it to disk if a path is set.



32
33
34
35
36
37
38
# File 'lib/brute/session.rb', line 32

def <<(msg)
  super
  if @path
    File.open(@path, "a") { |f| f.puts(JSON.generate(msg.to_h)) }
  end
  self
end

#assistant(content) ⇒ Object



44
45
46
# File 'lib/brute/session.rb', line 44

def assistant(content)
  self << RubyLLM::Message.new(role: :assistant, content: content)
end

#system(content) ⇒ Object



48
49
50
# File 'lib/brute/session.rb', line 48

def system(content)
  self << RubyLLM::Message.new(role: :system, content: content)
end

#user(content) ⇒ Object



40
41
42
# File 'lib/brute/session.rb', line 40

def user(content)
  self << RubyLLM::Message.new(role: :user, content: content)
end