Class: LangGraphRB::Stores::JsonStore

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

Overview

JSON-based store

Instance Method Summary collapse

Constructor Details

#initialize(base_path) ⇒ JsonStore



188
189
190
191
# File 'lib/langgraph_rb/stores/memory.rb', line 188

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



250
251
252
253
# File 'lib/langgraph_rb/stores/memory.rb', line 250

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



255
256
257
258
259
260
261
262
# File 'lib/langgraph_rb/stores/memory.rb', line 255

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, "*.json")).map do |file|
    File.basename(file, '.json').to_i
  end.sort
end

#list_threadsObject



244
245
246
247
248
# File 'lib/langgraph_rb/stores/memory.rb', line 244

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



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/langgraph_rb/stores/memory.rb', line 208

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}.json")
    return nil unless File.exist?(checkpoint_file)

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

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

    step_num = File.basename(latest_file, '.json').to_i
    data = JSON.parse(File.read(latest_file))
    
    {
      state: State.new(data['state']),
      step_number: step_num,
      timestamp: Time.parse(data['timestamp']),
      metadata: data['metadata'] || {}
    }
  end
end

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



193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/langgraph_rb/stores/memory.rb', line 193

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}.json")
  
  data = {
    state: state.to_h,
    timestamp: Time.now.iso8601,
    metadata: 
  }

  File.write(checkpoint_file, JSON.pretty_generate(data))
end