Class: Ralph::Storage::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/ralph/storage/context.rb

Overview

Represents the persistent AI context file for conversational continuity.

Always instantiate with Context.new — it represents the file on disk. Content is read lazily; the file is created on first write/append.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeContext

Returns a new instance of Context.



14
15
16
# File 'lib/ralph/storage/context.rb', line 14

def initialize
  FileUtils.mkdir_p(dir)
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



12
13
14
# File 'lib/ralph/storage/context.rb', line 12

def path
  @path
end

Class Method Details

.dirObject



18
19
20
# File 'lib/ralph/storage/context.rb', line 18

def self.dir
  @dir ||= File.join(Dir.pwd, ".ralph")
end

Instance Method Details

#append(text) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/ralph/storage/context.rb', line 39

def append(text)
  if File.exist?(path)
    write(File.read(path) + text)
  else
    write("# Ralph Loop Context\n#{text}")
  end
end

#clearObject



51
52
53
54
55
# File 'lib/ralph/storage/context.rb', line 51

def clear
  if File.exist?(path)
    File.delete(path) 
  end
end

#contentObject



28
29
30
31
32
33
34
35
# File 'lib/ralph/storage/context.rb', line 28

def content
  if File.exist?(path)
    text = File.read(path).strip
    text.empty? ? nil : text
  end
rescue StandardError
  nil
end

#dirObject



22
# File 'lib/ralph/storage/context.rb', line 22

def dir = self.class.dir

#present?Boolean

Returns:

  • (Boolean)


37
# File 'lib/ralph/storage/context.rb', line 37

def present? = !content.nil?

#write(text) ⇒ Object



47
48
49
# File 'lib/ralph/storage/context.rb', line 47

def write(text)
  File.write(path, text)
end