Class: Spurline::Memory::LongTerm::Postgres
- Defined in:
- lib/spurline/memory/long_term/postgres.rb
Constant Summary collapse
- TABLE_NAME =
"spurline_memories"
Instance Method Summary collapse
- #clear! ⇒ Object
- #create_table! ⇒ Object
-
#initialize(connection_string:, embedder:) ⇒ Postgres
constructor
A new instance of Postgres.
- #retrieve(query:, limit: 5) ⇒ Object
- #store(content:, metadata: {}) ⇒ Object
Constructor Details
#initialize(connection_string:, embedder:) ⇒ Postgres
Returns a new instance of Postgres.
11 12 13 14 15 |
# File 'lib/spurline/memory/long_term/postgres.rb', line 11 def initialize(connection_string:, embedder:) @connection_string = connection_string = @connection = nil end |
Instance Method Details
#clear! ⇒ Object
60 61 62 63 64 |
# File 'lib/spurline/memory/long_term/postgres.rb', line 60 def clear! connection.exec("DELETE FROM #{TABLE_NAME}") rescue StandardError => e raise Spurline::LongTermMemoryError, "Failed clearing long-term memory: #{e.message}" end |
#create_table! ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/spurline/memory/long_term/postgres.rb', line 66 def create_table! dim = .dimensions connection.exec("CREATE EXTENSION IF NOT EXISTS vector") connection.exec(" CREATE TABLE IF NOT EXISTS \#{TABLE_NAME} (\n id BIGSERIAL PRIMARY KEY,\n session_id TEXT,\n content TEXT NOT NULL,\n embedding vector(\#{dim}) NOT NULL,\n metadata JSONB DEFAULT '{}',\n created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()\n )\n SQL\n connection.exec(<<~SQL)\n CREATE INDEX IF NOT EXISTS idx_\#{TABLE_NAME}_session_id\n ON \#{TABLE_NAME} (session_id)\n SQL\nrescue StandardError => e\n raise Spurline::LongTermMemoryError, \"Failed creating long-term memory schema: \#{e.message}\"\nend\n") |
#retrieve(query:, limit: 5) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/spurline/memory/long_term/postgres.rb', line 37 def retrieve(query:, limit: 5) = .(query) sql = " SELECT content, metadata\n FROM \#{TABLE_NAME}\n ORDER BY embedding <-> $1::vector\n LIMIT $2\n SQL\n params = [vector_literal(query_embedding), limit]\n # ASYNC-READY: Database reads are synchronous in v1 at this boundary.\n result = connection.exec_params(sql, params)\n\n result.map do |row|\n Security::Content.new(\n text: row[\"content\"],\n trust: :operator,\n source: \"memory:long_term\"\n )\n end\nrescue StandardError => e\n raise Spurline::LongTermMemoryError, \"Failed retrieving long-term memory: \#{e.message}\"\nend\n" |
#store(content:, metadata: {}) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/spurline/memory/long_term/postgres.rb', line 17 def store(content:, metadata: {}) = .(content) session_id = [:session_id] || ["session_id"] sql = " INSERT INTO \#{TABLE_NAME} (session_id, content, embedding, metadata)\n VALUES ($1, $2, $3::vector, $4::jsonb)\n SQL\n params = [\n session_id,\n content,\n vector_literal(embedding),\n JSON.generate(metadata),\n ]\n\n # ASYNC-READY: Database writes are synchronous in v1 at this boundary.\n connection.exec_params(sql, params)\nrescue StandardError => e\n raise Spurline::LongTermMemoryError, \"Failed storing long-term memory: \#{e.message}\"\nend\n" |