Class: PromptObjects::Repositories::EventRepository
- Inherits:
-
BaseRepository
- Object
- BaseRepository
- PromptObjects::Repositories::EventRepository
- Defined in:
- lib/prompt_objects/repositories/event_repository.rb
Instance Method Summary collapse
- #append_run_event(run_id:, kind:, data: {}, timestamp: Time.now.utc) ⇒ Object
- #current_cursor(workspace_session_id:) ⇒ Object
- #run_events(run_id:, after_sequence: 0) ⇒ Object
- #workspace_events(workspace_session_id:, after_cursor: 0, limit: 500) ⇒ Object
Methods inherited from BaseRepository
Constructor Details
This class inherits a constructor from PromptObjects::Repositories::BaseRepository
Instance Method Details
#append_run_event(run_id:, kind:, data: {}, timestamp: Time.now.utc) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/prompt_objects/repositories/event_repository.rb', line 6 def append_run_event(run_id:, kind:, data: {}, timestamp: Time.now.utc) validate_kind!(kind) transaction do |db| run = db.get_first_row("SELECT * FROM runs WHERE id = ?", [run_id]) raise ArgumentError, "unknown run: #{run_id}" unless run sequence = db.get_first_value( "SELECT COALESCE(MAX(sequence), 0) + 1 FROM run_events WHERE run_id = ?", [run_id] ) occurred_at = .iso8601 workspace_cursor = append_workspace_event( db, workspace_session_id: run["workspace_session_id"], event_type: "run_event", entity_type: "run", entity_id: run_id, payload: { run_id: run_id, sequence: sequence, kind: kind.to_s, data: data }, created_at: occurred_at ) params = [ run_id, sequence, workspace_cursor, run["workspace_session_id"], run["thread_id"], kind.to_s, occurred_at, JSON.generate(data) ] db.execute(<<~SQL, params) INSERT INTO run_events ( run_id, sequence, workspace_cursor, workspace_session_id, thread_id, kind, timestamp, data ) VALUES (?, ?, ?, ?, ?, ?, ?, ?) SQL parse_run_event(db.get_first_row("SELECT * FROM run_events WHERE id = last_insert_rowid()")) end end |
#current_cursor(workspace_session_id:) ⇒ Object
69 70 71 72 73 74 75 76 |
# File 'lib/prompt_objects/repositories/event_repository.rb', line 69 def current_cursor(workspace_session_id:) read do |db| db.get_first_value( "SELECT COALESCE(MAX(id), 0) FROM workspace_events WHERE workspace_session_id = ?", [workspace_session_id] ) end end |
#run_events(run_id:, after_sequence: 0) ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/prompt_objects/repositories/event_repository.rb', line 40 def run_events(run_id:, after_sequence: 0) rows = read do |db| db.execute( "SELECT * FROM run_events WHERE run_id = ? AND sequence > ? ORDER BY sequence ASC", [run_id, after_sequence] ) end rows.map { |row| parse_run_event(row) } end |
#workspace_events(workspace_session_id:, after_cursor: 0, limit: 500) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/prompt_objects/repositories/event_repository.rb', line 50 def workspace_events(workspace_session_id:, after_cursor: 0, limit: 500) read do |db| rows = db.execute(<<~SQL, [workspace_session_id, after_cursor, limit]) SELECT * FROM workspace_events WHERE workspace_session_id = ? AND id > ? ORDER BY id ASC LIMIT ? SQL rows.map do |row| { cursor: row["id"], workspace_session_id: row["workspace_session_id"], event_type: row["event_type"], entity_type: row["entity_type"], entity_id: row["entity_id"], payload: json(row["payload"]), created_at: (row["created_at"]) }.freeze end end end |