Module: OmniAgent::Eval::Cache

Defined in:
lib/omni_agent/eval/cache.rb

Class Method Summary collapse

Class Method Details

.clear!Object



29
30
31
# File 'lib/omni_agent/eval/cache.rb', line 29

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

.fetch(key) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/omni_agent/eval/cache.rb', line 8

def self.fetch(key)
  return yield unless OmniAgent.configuration.eval_cache_enabled

  store = read_store
  return store[key] if store.key?(key)

  result = yield
  store[key] = result
  write_store(store)
  result
end

.key_for(agent_class:, run_alias:, input:, context:) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/omni_agent/eval/cache.rb', line 20

def self.key_for(agent_class:, run_alias:, input:, context:)
  Digest::SHA256.hexdigest(JSON.generate(
    agent: agent_class.name,
    run_alias: run_alias,
    input: input,
    context: context
  ))
end

.pathObject



33
34
35
# File 'lib/omni_agent/eval/cache.rb', line 33

def self.path
  OmniAgent.configuration.eval_cache_path
end

.read_storeObject



37
38
39
40
41
42
43
# File 'lib/omni_agent/eval/cache.rb', line 37

def self.read_store
  return {} unless File.exist?(path)

  JSON.parse(File.read(path))
rescue JSON::ParserError
  {}
end

.write_store(store) ⇒ Object



45
46
47
48
# File 'lib/omni_agent/eval/cache.rb', line 45

def self.write_store(store)
  FileUtils.mkdir_p(File.dirname(path))
  File.write(path, JSON.generate(store))
end