Class: LangGraphRB::Stores::FileStore

Inherits:
BaseStore
  • Object
show all
Defined in:
lib/langgraph_rb/stores/memory.rb

Overview

File-based store using YAML

Instance Method Summary collapse

Constructor Details

#initialize(base_path) ⇒ FileStore

Returns a new instance of FileStore.



109
110
111
112
# File 'lib/langgraph_rb/stores/memory.rb', line 109

def initialize(base_path)
  @base_path = base_path
  FileUtils.mkdir_p(@base_path) unless Dir.exist?(@base_path)
end

Instance Method Details

#delete(thread_id) ⇒ Object



171
172
173
174
# File 'lib/langgraph_rb/stores/memory.rb', line 171

def delete(thread_id)
  thread_dir = File.join(@base_path, thread_id.to_s)
  FileUtils.rm_rf(thread_dir) if Dir.exist?(thread_dir)
end

#list_steps(thread_id) ⇒ Object



176
177
178
179
180
181
182
183
# File 'lib/langgraph_rb/stores/memory.rb', line 176

def list_steps(thread_id)
  thread_dir = File.join(@base_path, thread_id.to_s)
  return [] unless Dir.exist?(thread_dir)

  Dir.glob(File.join(thread_dir, "*.yml")).map do |file|
    File.basename(file, '.yml').to_i
  end.sort
end

#list_threadsObject



165
166
167
168
169
# File 'lib/langgraph_rb/stores/memory.rb', line 165

def list_threads
  Dir.entries(@base_path).select do |entry|
    File.directory?(File.join(@base_path, entry)) && entry != '.' && entry != '..'
  end
end

#load(thread_id, step_number = nil) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/langgraph_rb/stores/memory.rb', line 129

def load(thread_id, step_number = nil)
  thread_dir = File.join(@base_path, thread_id.to_s)
  return nil unless Dir.exist?(thread_dir)

  if step_number
    checkpoint_file = File.join(thread_dir, "#{step_number}.yml")
    return nil unless File.exist?(checkpoint_file)

    data = YAML.load_file(checkpoint_file)
    {
      state: State.new(data['state']),
      step_number: step_number,
      timestamp: data['timestamp'],
      metadata: data['metadata'] || {}
    }
  else
    # Find latest checkpoint
    files = Dir.glob(File.join(thread_dir, "*.yml"))
    return nil if files.empty?

    latest_file = files.max_by do |file|
      File.basename(file, '.yml').to_i
    end

    step_num = File.basename(latest_file, '.yml').to_i
    data = YAML.load_file(latest_file)
    
    {
      state: State.new(data['state']),
      step_number: step_num,
      timestamp: data['timestamp'],
      metadata: data['metadata'] || {}
    }
  end
end

#save(thread_id, state, step_number, metadata = {}) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/langgraph_rb/stores/memory.rb', line 114

def save(thread_id, state, step_number,  = {})
  thread_dir = File.join(@base_path, thread_id.to_s)
  FileUtils.mkdir_p(thread_dir) unless Dir.exist?(thread_dir)

  checkpoint_file = File.join(thread_dir, "#{step_number}.yml")
  
  data = {
    state: state.to_h,
    timestamp: Time.now,
    metadata: 
  }

  File.write(checkpoint_file, YAML.dump(data))
end