Class: DSPy::Storage::ProgramStorage

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/dspy/storage/program_storage.rb

Overview

Storage system for saving and loading optimized DSPy programs Handles serialization of optimization results, program state, and history tracking

Defined Under Namespace

Classes: SavedProgram

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(storage_path: "./dspy_storage", create_directories: true) ⇒ ProgramStorage

Returns a new instance of ProgramStorage.



161
162
163
164
165
166
# File 'lib/dspy/storage/program_storage.rb', line 161

def initialize(storage_path: "./dspy_storage", create_directories: true)
  @storage_path = File.expand_path(storage_path)
  @create_directories = create_directories
  
  setup_storage_directory if @create_directories
end

Instance Attribute Details

#create_directoriesObject (readonly)

Returns the value of attribute create_directories.



153
154
155
# File 'lib/dspy/storage/program_storage.rb', line 153

def create_directories
  @create_directories
end

#storage_pathObject (readonly)

Returns the value of attribute storage_path.



150
151
152
# File 'lib/dspy/storage/program_storage.rb', line 150

def storage_path
  @storage_path
end

Instance Method Details

#delete_program(program_id) ⇒ Object



253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/dspy/storage/program_storage.rb', line 253

def delete_program(program_id)
  file_path = program_file_path(program_id)
  
  if File.exist?(file_path)
    File.delete(file_path)
    remove_from_history(program_id)
    emit_delete_event(program_id)
    true
  else
    false
  end
end

#export_programs(program_ids, export_path) ⇒ Object



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/dspy/storage/program_storage.rb', line 268

def export_programs(program_ids, export_path)
  programs = program_ids.map { |id| load_program(id) }.compact
  
  dspy_version = begin
    DSPy::VERSION
  rescue
    "unknown"
  end
  
  export_data = {
    exported_at: Time.now.iso8601,
    dspy_version: dspy_version,
    programs: programs.map(&:to_h)
  }
  
  File.write(export_path, JSON.pretty_generate(export_data))
  emit_export_event(export_path, programs.size)
end

#get_historyObject



244
245
246
247
248
249
# File 'lib/dspy/storage/program_storage.rb', line 244

def get_history
  history_path = File.join(@storage_path, "history.json")
  return { programs: [], summary: {} } unless File.exist?(history_path)
  
  JSON.parse(File.read(history_path), symbolize_names: true)
end

#import_programs(import_path) ⇒ Object



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/dspy/storage/program_storage.rb', line 289

def import_programs(import_path)
  data = JSON.parse(File.read(import_path), symbolize_names: true)
  imported = []
  
  data[:programs].each do |program_data|
    saved_program = SavedProgram.from_h(program_data)
    
    # Save with new timestamp but preserve original ID
    file_path = program_file_path(saved_program.program_id)
    File.write(file_path, JSON.pretty_generate(saved_program.to_h))
    
    update_history(saved_program)
    imported << saved_program
  end
  
  emit_import_event(import_path, imported.size)
  imported
end

#list_programsObject



234
235
236
237
238
239
240
# File 'lib/dspy/storage/program_storage.rb', line 234

def list_programs
  history_path = File.join(@storage_path, "history.json")
  return [] unless File.exist?(history_path)
  
  history_data = JSON.parse(File.read(history_path), symbolize_names: true)
  history_data[:programs] || []
end

#load_program(program_id) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/dspy/storage/program_storage.rb', line 209

def load_program(program_id)
  emit_load_start_event(program_id)
  
  begin
    file_path = program_file_path(program_id)
    
    unless File.exist?(file_path)
      emit_load_error_event(program_id, "Program not found: #{program_id}")
      return nil
    end
    
    data = JSON.parse(File.read(file_path), symbolize_names: true)
    saved_program = SavedProgram.from_h(data)
    
    emit_load_complete_event(saved_program)
    saved_program
    
  rescue => error
    emit_load_error_event(program_id, error)
    nil
  end
end

#save_program(program, optimization_result, program_id: nil, metadata: {}) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/dspy/storage/program_storage.rb', line 177

def save_program(program, optimization_result, program_id: nil, metadata: {})
  emit_save_start_event(program_id)
  
  begin
    # Convert optimization result to hash if it's an object
    result_hash = optimization_result.respond_to?(:to_h) ? optimization_result.to_h : optimization_result
    
    saved_program = SavedProgram.new(
      program: program,
      optimization_result: result_hash,
      metadata: ,
      program_id: program_id
    )
    
    # Write to file
    file_path = program_file_path(saved_program.program_id)
    File.write(file_path, JSON.pretty_generate(saved_program.to_h))
    
    # Update history
    update_history(saved_program)
    
    emit_save_complete_event(saved_program)
    saved_program
    
  rescue => error
    emit_save_error_event(program_id, error)
    raise
  end
end