Class: ActiveHarness::Memory::Sqlite
- Inherits:
-
ActiveHarness::Memory
- Object
- ActiveHarness::Memory
- ActiveHarness::Memory::Sqlite
- Defined in:
- lib/active_harness/memory/adapter/sqlite.rb
Overview
Convenience Memory subclass for SQLite-backed storage.
Usage — plain Ruby (adapter owns the connection):
mem = ActiveHarness::Memory::Sqlite.new(
session_id: "user_42",
database: "storage/ai/memory.sqlite3",
depth: 10
)
mem.load
# ... use ...
mem.close
Usage — Rails (borrow AR raw connection for SQLite3 adapter):
mem = ActiveHarness::Memory::Sqlite.new(
session_id: "user_42",
connection: ActiveRecord::Base.connection.raw_connection
)
Plain Ruby schema setup (run once before first use):
ActiveHarness::Memory::Sqlite.create_schema!("storage/ai/memory.sqlite3")
Constant Summary collapse
- SCHEMA_SQL =
SQL to create the schema — run once before first use in plain Ruby.
<<~SQL.freeze CREATE TABLE IF NOT EXISTS active_harness_memory_turns ( id INTEGER PRIMARY KEY AUTOINCREMENT, session_id TEXT NOT NULL, namespace TEXT, request TEXT NOT NULL, response TEXT NOT NULL, meta TEXT NOT NULL DEFAULT '{}', created_at TEXT NOT NULL DEFAULT (datetime('now')) ); CREATE INDEX IF NOT EXISTS idx_ah_memory_turns_session ON active_harness_memory_turns (session_id, namespace, id); SQL
Constants inherited from ActiveHarness::Memory
Instance Attribute Summary
Attributes inherited from ActiveHarness::Memory
Class Method Summary collapse
-
.create_schema!(db_or_path) ⇒ Object
Create the schema on an existing database or a file path.
Instance Method Summary collapse
-
#initialize(session_id:, namespace: nil, on_trim: nil, **opts) ⇒ Sqlite
constructor
A new instance of Sqlite.
Methods inherited from ActiveHarness::Memory
#clear, #close, #delete, #load, #record, #size, #to_messages, #turns
Constructor Details
#initialize(session_id:, namespace: nil, on_trim: nil, **opts) ⇒ Sqlite
Returns a new instance of Sqlite.
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/active_harness/memory/adapter/sqlite.rb', line 215 def initialize(session_id:, namespace: nil, on_trim: nil, **opts) mem_keys = %i[depth enabled read_only async] mem_opts = opts.slice(*mem_keys) sq_opts = opts.reject { |k, _| mem_keys.include?(k) } sq_opts[:namespace] = namespace if namespace sq_opts[:on_trim] = on_trim if on_trim super( session_id: session_id, adapter: Adapter::Sqlite.new(sq_opts), namespace: namespace, on_trim: on_trim, **mem_opts ) end |
Class Method Details
.create_schema!(db_or_path) ⇒ Object
Create the schema on an existing database or a file path. Safe to call multiple times — uses CREATE TABLE IF NOT EXISTS.
206 207 208 209 210 211 212 213 |
# File 'lib/active_harness/memory/adapter/sqlite.rb', line 206 def self.create_schema!(db_or_path) require "sqlite3" conn = db_or_path.is_a?(String) ? SQLite3::Database.new(db_or_path) : db_or_path SCHEMA_SQL.split(";").map(&:strip).reject(&:empty?).each { |sql| conn.execute(sql) } rescue LoadError raise LoadError, "The 'sqlite3' gem is required. Add it to your Gemfile: gem 'sqlite3'" end |