Class: PromptObjects::Repositories::ArtifactRepository
- Inherits:
-
BaseRepository
- Object
- BaseRepository
- PromptObjects::Repositories::ArtifactRepository
- Defined in:
- lib/prompt_objects/repositories/artifact_repository.rb
Defined Under Namespace
Classes: Mutation
Instance Method Summary collapse
- #delete(id) ⇒ Object
- #get(id) ⇒ Object
- #list(workspace_session_id:) ⇒ Object
- #upsert(**attributes) ⇒ Object
- #upsert_with_cursor(workspace_session_id:, owner_po:, owner_thread_id:, producing_run_id:, title:, html:, mode: "replace", metadata: {}, id: nil) ⇒ Object
Methods inherited from BaseRepository
Constructor Details
This class inherits a constructor from PromptObjects::Repositories::BaseRepository
Instance Method Details
#delete(id) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/prompt_objects/repositories/artifact_repository.rb', line 70 def delete(id) transaction do |db| artifact = db.get_first_row("SELECT * FROM artifacts WHERE id = ?", [id]) next false unless artifact db.execute("DELETE FROM artifacts WHERE id = ?", [id]) now = Time.now.utc.iso8601 db.execute(<<~SQL, [artifact["workspace_session_id"], id, now]) INSERT INTO workspace_events ( workspace_session_id, event_type, entity_type, entity_id, payload, created_at ) VALUES (?, 'artifact_removed', 'artifact', ?, '{}', ?) SQL true end end |
#get(id) ⇒ Object
56 57 58 59 |
# File 'lib/prompt_objects/repositories/artifact_repository.rb', line 56 def get(id) row = read { |db| db.get_first_row("SELECT * FROM artifacts WHERE id = ?", [id]) } row && parse(row) end |
#list(workspace_session_id:) ⇒ Object
61 62 63 64 65 66 67 68 |
# File 'lib/prompt_objects/repositories/artifact_repository.rb', line 61 def list(workspace_session_id:) read do |db| db.execute( "SELECT * FROM artifacts WHERE workspace_session_id = ? ORDER BY updated_at ASC, id ASC", [workspace_session_id] ).map { |row| parse(row) } end end |
#upsert(**attributes) ⇒ Object
8 9 10 |
# File 'lib/prompt_objects/repositories/artifact_repository.rb', line 8 def upsert(**attributes) upsert_with_cursor(**attributes).artifact end |
#upsert_with_cursor(workspace_session_id:, owner_po:, owner_thread_id:, producing_run_id:, title:, html:, mode: "replace", metadata: {}, id: nil) ⇒ Object
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/prompt_objects/repositories/artifact_repository.rb', line 12 def upsert_with_cursor(workspace_session_id:, owner_po:, owner_thread_id:, producing_run_id:, title:, html:, mode: "replace", metadata: {}, id: nil) raise ArgumentError, "unknown artifact mode: #{mode}" unless %w[replace version].include?(mode.to_s) transaction do |db| existing = if mode.to_s == "replace" db.get_first_row(<<~SQL, [workspace_session_id, title]) SELECT * FROM artifacts WHERE workspace_session_id = ? AND title = ? AND mode = 'replace' ORDER BY updated_at DESC LIMIT 1 SQL end artifact_id = existing ? existing["id"] : (id || "artifact_#{SecureRandom.uuid}") revision = existing ? existing["revision"].to_i + 1 : 1 now = Time.now.utc.iso8601 created_at = existing ? existing["created_at"] : now params = [ artifact_id, workspace_session_id, owner_po, owner_thread_id, producing_run_id, title, html, revision, mode.to_s, created_at, now, JSON.generate() ] db.execute(<<~SQL, params) INSERT INTO artifacts ( id, workspace_session_id, owner_po, owner_thread_id, producing_run_id, title, html, revision, mode, created_at, updated_at, metadata ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT(id) DO UPDATE SET owner_po = excluded.owner_po, owner_thread_id = excluded.owner_thread_id, producing_run_id = excluded.producing_run_id, title = excluded.title, html = excluded.html, revision = excluded.revision, mode = excluded.mode, updated_at = excluded.updated_at, metadata = excluded.metadata SQL row = db.get_first_row("SELECT * FROM artifacts WHERE id = ?", [artifact_id]) artifact = parse(row) cursor = append_workspace_event(db, artifact) Mutation.new(artifact: artifact, cursor: cursor) end end |