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
15
16
17
18
19
20
21
22
23
# File 'lib/brute/session.rb', line 10

def initialize(path: nil)
  super()
  @path = path
  if @path
    FileUtils.mkdir_p(File.dirname(@path))
    if File.exist?(@path)
      File.foreach(@path) do |line|
        line.strip!
        # Use push to bypass append persistence (avoids re-writing existing lines)
        push(RubyLLM::Message.new(**JSON.parse(line, symbolize_names: true))) if line.present?
      end
    end
  end
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

Deprecated.

Use Session.new(path:) instead.



26
27
28
# File 'lib/brute/session.rb', line 26

def self.from_jsonl(path)
  new(path: path)
end

Instance Method Details

#<<(msg) ⇒ Object

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



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

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

#assistant(content) ⇒ Object



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

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

#system(content) ⇒ Object



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

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

#user(content) ⇒ Object



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

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