Class: LLMDB::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/llmdb/configuration.rb

Constant Summary collapse

SUPPORTED_ADAPTERS =
%i[postgresql mysql oracle].freeze
PERMISSION_MODES =
%i[read_only ask full].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Configuration

Returns a new instance of Configuration.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/llmdb/configuration.rb', line 29

def initialize(opts = {})
  @host     = opts[:host]
  @port     = opts[:port]
  @database = opts[:database]
  @username = opts[:username]
  @password = opts[:password]
  @adapter  = opts[:adapter]&.to_sym

  @llm_provider            = opts[:llm_provider]&.to_sym
  @llm_model               = opts[:llm_model]
  @llm_api_base            = opts[:llm_api_base]
  @llm_api_key             = opts[:llm_api_key]
  @llm_assume_model_exists = opts.fetch(:llm_assume_model_exists, false)

  @permission_mode       = (opts[:permission_mode] || :read_only).to_sym
  @confirm_callback      = opts[:confirm_callback]
  @write_classifier      = opts[:write_classifier]
  @max_rows              = opts[:max_rows] || 500
  @system_prompt         = opts[:system_prompt]
  @default_system_prompt = opts[:default_system_prompt]
end

Instance Attribute Details

#adapterObject

Database connection



7
8
9
# File 'lib/llmdb/configuration.rb', line 7

def adapter
  @adapter
end

#confirm_callbackObject



51
52
53
# File 'lib/llmdb/configuration.rb', line 51

def confirm_callback
  @confirm_callback || self.class.default_confirm_callback
end

#databaseObject

Database connection



7
8
9
# File 'lib/llmdb/configuration.rb', line 7

def database
  @database
end

#default_system_promptObject

Agent behaviour

  • permission_mode: one of :read_only (default — only SELECT/WITH/EXPLAIN), :ask (write queries require confirm_callback to return truthy), or :full (no restrictions). The check is best-effort and based on the first SQL token; for hard isolation use a read-only DB user.

  • confirm_callback: a callable taking the SQL string and returning true/false. Used only when permission_mode == :ask. Defaults to a built-in TTY prompt; override for non-interactive contexts (web apps, job queues, etc.).

  • max_rows: cap on rows returned to the LLM per query.

  • system_prompt: project-specific context appended on top of the default.

  • default_system_prompt: replaces the built-in tool/schema-discovery instructions. Rarely needed.



26
27
28
# File 'lib/llmdb/configuration.rb', line 26

def default_system_prompt
  @default_system_prompt
end

#hostObject

Database connection



7
8
9
# File 'lib/llmdb/configuration.rb', line 7

def host
  @host
end

#llm_api_baseObject

LLM (provider-agnostic — works with Ollama, LM Studio, OpenAI, Anthropic, Gemini, etc.)



10
11
12
# File 'lib/llmdb/configuration.rb', line 10

def llm_api_base
  @llm_api_base
end

#llm_api_keyObject

LLM (provider-agnostic — works with Ollama, LM Studio, OpenAI, Anthropic, Gemini, etc.)



10
11
12
# File 'lib/llmdb/configuration.rb', line 10

def llm_api_key
  @llm_api_key
end

#llm_assume_model_existsObject

LLM (provider-agnostic — works with Ollama, LM Studio, OpenAI, Anthropic, Gemini, etc.)



10
11
12
# File 'lib/llmdb/configuration.rb', line 10

def llm_assume_model_exists
  @llm_assume_model_exists
end

#llm_modelObject

LLM (provider-agnostic — works with Ollama, LM Studio, OpenAI, Anthropic, Gemini, etc.)



10
11
12
# File 'lib/llmdb/configuration.rb', line 10

def llm_model
  @llm_model
end

#llm_providerObject

LLM (provider-agnostic — works with Ollama, LM Studio, OpenAI, Anthropic, Gemini, etc.)



10
11
12
# File 'lib/llmdb/configuration.rb', line 10

def llm_provider
  @llm_provider
end

#max_rowsObject

Agent behaviour

  • permission_mode: one of :read_only (default — only SELECT/WITH/EXPLAIN), :ask (write queries require confirm_callback to return truthy), or :full (no restrictions). The check is best-effort and based on the first SQL token; for hard isolation use a read-only DB user.

  • confirm_callback: a callable taking the SQL string and returning true/false. Used only when permission_mode == :ask. Defaults to a built-in TTY prompt; override for non-interactive contexts (web apps, job queues, etc.).

  • max_rows: cap on rows returned to the LLM per query.

  • system_prompt: project-specific context appended on top of the default.

  • default_system_prompt: replaces the built-in tool/schema-discovery instructions. Rarely needed.



26
27
28
# File 'lib/llmdb/configuration.rb', line 26

def max_rows
  @max_rows
end

#passwordObject

Database connection



7
8
9
# File 'lib/llmdb/configuration.rb', line 7

def password
  @password
end

#permission_modeObject

Agent behaviour

  • permission_mode: one of :read_only (default — only SELECT/WITH/EXPLAIN), :ask (write queries require confirm_callback to return truthy), or :full (no restrictions). The check is best-effort and based on the first SQL token; for hard isolation use a read-only DB user.

  • confirm_callback: a callable taking the SQL string and returning true/false. Used only when permission_mode == :ask. Defaults to a built-in TTY prompt; override for non-interactive contexts (web apps, job queues, etc.).

  • max_rows: cap on rows returned to the LLM per query.

  • system_prompt: project-specific context appended on top of the default.

  • default_system_prompt: replaces the built-in tool/schema-discovery instructions. Rarely needed.



26
27
28
# File 'lib/llmdb/configuration.rb', line 26

def permission_mode
  @permission_mode
end

#portObject

Database connection



7
8
9
# File 'lib/llmdb/configuration.rb', line 7

def port
  @port
end

#system_promptObject

Agent behaviour

  • permission_mode: one of :read_only (default — only SELECT/WITH/EXPLAIN), :ask (write queries require confirm_callback to return truthy), or :full (no restrictions). The check is best-effort and based on the first SQL token; for hard isolation use a read-only DB user.

  • confirm_callback: a callable taking the SQL string and returning true/false. Used only when permission_mode == :ask. Defaults to a built-in TTY prompt; override for non-interactive contexts (web apps, job queues, etc.).

  • max_rows: cap on rows returned to the LLM per query.

  • system_prompt: project-specific context appended on top of the default.

  • default_system_prompt: replaces the built-in tool/schema-discovery instructions. Rarely needed.



26
27
28
# File 'lib/llmdb/configuration.rb', line 26

def system_prompt
  @system_prompt
end

#usernameObject

Database connection



7
8
9
# File 'lib/llmdb/configuration.rb', line 7

def username
  @username
end

#write_classifierObject

Classifier used in :ask mode to decide whether a query writes. Default is an LLM-judge using the same model as the agent. Override with any callable taking a SQL string and returning a boolean for custom logic (e.g. a deterministic SQL parser, a smaller dedicated model, regex).



59
60
61
# File 'lib/llmdb/configuration.rb', line 59

def write_classifier
  @write_classifier ||= WriteClassifier.new(self)
end

Class Method Details

.default_confirm_callbackObject

Default TTY-based confirmation. Returns false when STDIN isn’t interactive, which causes the adapter to refuse the write — fail-safe behaviour.



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/llmdb/configuration.rb', line 65

def self.default_confirm_callback
  lambda do |sql|
    return false unless $stdin.tty?

    $stderr.puts "\n[LLMDB] Pending non-read-only query:"
    sql.each_line { |line| $stderr.puts "  | #{line.chomp}" }
    $stderr.print "[LLMDB] Execute? [y/N] "
    answer = $stdin.gets&.strip&.downcase
    %w[y yes].include?(answer)
  end
end

Instance Method Details

#validate!Object



77
78
79
80
81
# File 'lib/llmdb/configuration.rb', line 77

def validate!
  validate_database!
  validate_llm!
  validate_permission_mode!
end