Module: Legion::LLM::Cache::Response
- Extended by:
- Cache::Helper, Legion::Logging::Helper
- Defined in:
- lib/legion/llm/cache/response.rb
Class Method Summary collapse
- .cache_namespace ⇒ Object
-
.cleanup(request_id) ⇒ Object
Removes all cache keys for a request (and any spool file).
- .complete(request_id, response:, meta:, ttl: default_ttl) ⇒ Object
-
.error(request_id) ⇒ Object
Returns { code:, message: } hash, or nil.
- .fail_request(request_id, code:, message:, ttl: default_ttl) ⇒ Object
- .init_request(request_id, ttl: default_ttl) ⇒ Object
-
.meta(request_id) ⇒ Object
Returns meta hash with symbolized keys, or nil.
- .poll(request_id, timeout: default_ttl, interval: 0.1) ⇒ Object
-
.response(request_id) ⇒ Object
Returns the response string (handles spool overflow transparently).
-
.status(request_id) ⇒ Object
Returns :pending, :done, :error, or nil.
Class Method Details
.cache_namespace ⇒ Object
17 |
# File 'lib/legion/llm/cache/response.rb', line 17 def cache_namespace = '' |
.cleanup(request_id) ⇒ Object
Removes all cache keys for a request (and any spool file).
88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/legion/llm/cache/response.rb', line 88 def cleanup(request_id) raw = cache_read(response_key(request_id)) if raw&.start_with?('spool:') path = raw.delete_prefix('spool:') FileUtils.rm_f(path) end cache_remove(status_key(request_id)) cache_remove(response_key(request_id)) cache_remove((request_id)) cache_remove(error_key(request_id)) end |
.complete(request_id, response:, meta:, ttl: default_ttl) ⇒ Object
23 24 25 26 27 |
# File 'lib/legion/llm/cache/response.rb', line 23 def complete(request_id, response:, meta:, ttl: default_ttl) write_response(request_id, response, ttl) cache_write((request_id), Legion::JSON.dump(), ttl) cache_write(status_key(request_id), 'done', ttl) end |
.error(request_id) ⇒ Object
Returns { code:, message: } hash, or nil.
60 61 62 63 64 65 |
# File 'lib/legion/llm/cache/response.rb', line 60 def error(request_id) raw = cache_read(error_key(request_id)) return nil if raw.nil? Legion::JSON.load(raw) end |
.fail_request(request_id, code:, message:, ttl: default_ttl) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/legion/llm/cache/response.rb', line 29 def fail_request(request_id, code:, message:, ttl: default_ttl) log.warn("ResponseCache fail_request request_id=#{request_id} code=#{code} message=#{}") payload = Legion::JSON.dump({ code: code, message: }) cache_write(error_key(request_id), payload, ttl) cache_write(status_key(request_id), 'error', ttl) end |
.init_request(request_id, ttl: default_ttl) ⇒ Object
19 20 21 |
# File 'lib/legion/llm/cache/response.rb', line 19 def init_request(request_id, ttl: default_ttl) cache_write(status_key(request_id), 'pending', ttl) end |
.meta(request_id) ⇒ Object
Returns meta hash with symbolized keys, or nil.
52 53 54 55 56 57 |
# File 'lib/legion/llm/cache/response.rb', line 52 def (request_id) raw = cache_read((request_id)) return nil if raw.nil? Legion::JSON.load(raw) end |
.poll(request_id, timeout: default_ttl, interval: 0.1) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/legion/llm/cache/response.rb', line 67 def poll(request_id, timeout: default_ttl, interval: 0.1) deadline = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC) + timeout loop do current = status(request_id) log.debug("ResponseCache poll request_id=#{request_id} status=#{current}") case current when :done return { status: :done, response: response(request_id), meta: (request_id) } when :error return { status: :error, error: error(request_id) } end return { status: :timeout } if ::Process.clock_gettime(::Process::CLOCK_MONOTONIC) >= deadline sleep interval end end |
.response(request_id) ⇒ Object
Returns the response string (handles spool overflow transparently).
43 44 45 46 47 48 49 |
# File 'lib/legion/llm/cache/response.rb', line 43 def response(request_id) raw = cache_read(response_key(request_id)) return nil if raw.nil? return File.read(raw.delete_prefix('spool:')) if raw.start_with?('spool:') raw end |
.status(request_id) ⇒ Object
Returns :pending, :done, :error, or nil.
37 38 39 40 |
# File 'lib/legion/llm/cache/response.rb', line 37 def status(request_id) raw = cache_read(status_key(request_id)) raw&.to_sym end |