Class: RubyLLM::Registry::Adapters::SQLite

Inherits:
Base
  • Object
show all
Defined in:
lib/ruby_llm/registry/adapters/sqlite.rb

Overview

SQLite-backed prompt repository.

Instance Method Summary collapse

Methods inherited from Base

#export, #import

Constructor Details

#initialize(path:, table_name: "ruby_llm_registry_prompts") ⇒ SQLite

Returns a new instance of SQLite.



11
12
13
14
15
16
17
# File 'lib/ruby_llm/registry/adapters/sqlite.rb', line 11

def initialize(path:, table_name: "ruby_llm_registry_prompts")
  require_sqlite3!
  @database = SQLite3::Database.new(path)
  @database.results_as_hash = true
  @table_name = table_name
  ensure_schema!
end

Instance Method Details

#available_versions(path) ⇒ Object



37
38
39
# File 'lib/ruby_llm/registry/adapters/sqlite.rb', line 37

def available_versions(path)
  rows_for_path(path).map { |row| Version.parse(row["version"]) }.sort
end

#get(path, version: nil, label: nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ruby_llm/registry/adapters/sqlite.rb', line 19

def get(path, version: nil, label: nil)
  rows = rows_for_path(path)
  raise PromptNotFoundError, "Prompt path not found: #{path}" if rows.empty?

  row = if version
          rows.find { |record| record["version"] == Version.parse(version).to_s }
        elsif label
          label = label.to_sym
          rows.find { |record| labels_for(record).include?(label) } || rows.find { |record| record["version"] == label.to_s }
        else
          rows.max_by { |record| Version.parse(record["version"]) }
        end

  raise PromptNotFoundError, "Prompt not found: #{path}" unless row

  prompt_from_row(row)
end

#store(prompt, overwrite: false) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ruby_llm/registry/adapters/sqlite.rb', line 41

def store(prompt, overwrite: false)
  existing = rows_for_path(prompt.path).find { |row| row["version"] == prompt.version.to_s }
  if existing && !overwrite
    raise Error, "Prompt #{prompt.path}@#{prompt.version} already exists"
  end

  data = serialize_prompt(prompt)
  if existing
    update_row(data)
  else
    insert_row(data)
  end
  prompt
end