Class: Ligarb::ReviewStore
- Inherits:
-
Object
- Object
- Ligarb::ReviewStore
- Defined in:
- lib/ligarb/review_store.rb
Instance Method Summary collapse
- #add_message(id, role:, content:) ⇒ Object
- #create(context:, message:) ⇒ Object
- #delete(id) ⇒ Object
- #get(id) ⇒ Object
-
#initialize(base_dir) ⇒ ReviewStore
constructor
A new instance of ReviewStore.
- #list ⇒ Object
- #update_context_files(id, files) ⇒ Object
- #update_status(id, status) ⇒ Object
Constructor Details
#initialize(base_dir) ⇒ ReviewStore
Returns a new instance of ReviewStore.
10 11 12 13 14 |
# File 'lib/ligarb/review_store.rb', line 10 def initialize(base_dir) @dir = File.join(base_dir, ".ligarb", "reviews") @mutex = Mutex.new FileUtils.mkdir_p(@dir) end |
Instance Method Details
#add_message(id, role:, content:) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/ligarb/review_store.rb', line 51 def (id, role:, content:) @mutex.synchronize do review = get_unlocked(id) return nil unless review review["messages"] << { "role" => role, "content" => content, "timestamp" => Time.now.utc.iso8601 } write_json(id, review) review end end |
#create(context:, message:) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/ligarb/review_store.rb', line 31 def create(context:, message:) @mutex.synchronize do id = SecureRandom.uuid now = Time.now.utc.iso8601 review = { "id" => id, "status" => "open", "created_at" => now, "context" => context, "messages" => [ { "role" => "user", "content" => , "timestamp" => now } ] } write_json(id, review) review end end |
#delete(id) ⇒ Object
90 91 92 93 94 95 96 97 |
# File 'lib/ligarb/review_store.rb', line 90 def delete(id) @mutex.synchronize do path = file_path(id) return false unless File.exist?(path) File.delete(path) true end end |
#get(id) ⇒ Object
25 26 27 28 29 |
# File 'lib/ligarb/review_store.rb', line 25 def get(id) @mutex.synchronize do get_unlocked(id) end end |
#list ⇒ Object
16 17 18 19 20 21 22 23 |
# File 'lib/ligarb/review_store.rb', line 16 def list @mutex.synchronize do Dir.glob(File.join(@dir, "*.json")).map { |f| read_json(f) } .compact .sort_by { |r| r["created_at"] } .map { |r| summary(r) } end end |
#update_context_files(id, files) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/ligarb/review_store.rb', line 67 def update_context_files(id, files) @mutex.synchronize do review = get_unlocked(id) return nil unless review existing = review.dig("context", "uploaded_files") || [] review["context"]["uploaded_files"] = existing + files write_json(id, review) review end end |
#update_status(id, status) ⇒ Object
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/ligarb/review_store.rb', line 79 def update_status(id, status) @mutex.synchronize do review = get_unlocked(id) return nil unless review review["status"] = status write_json(id, review) review end end |