Class: LLMDB::Configuration
- Inherits:
-
Object
- Object
- LLMDB::Configuration
- 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
-
#adapter ⇒ Object
Database connection.
- #confirm_callback ⇒ Object
-
#database ⇒ Object
Database connection.
-
#default_system_prompt ⇒ Object
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).
-
#host ⇒ Object
Database connection.
-
#llm_api_base ⇒ Object
LLM (provider-agnostic — works with Ollama, LM Studio, OpenAI, Anthropic, Gemini, etc.).
-
#llm_api_key ⇒ Object
LLM (provider-agnostic — works with Ollama, LM Studio, OpenAI, Anthropic, Gemini, etc.).
-
#llm_assume_model_exists ⇒ Object
LLM (provider-agnostic — works with Ollama, LM Studio, OpenAI, Anthropic, Gemini, etc.).
-
#llm_model ⇒ Object
LLM (provider-agnostic — works with Ollama, LM Studio, OpenAI, Anthropic, Gemini, etc.).
-
#llm_provider ⇒ Object
LLM (provider-agnostic — works with Ollama, LM Studio, OpenAI, Anthropic, Gemini, etc.).
-
#max_rows ⇒ Object
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).
-
#password ⇒ Object
Database connection.
-
#permission_mode ⇒ Object
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).
-
#port ⇒ Object
Database connection.
-
#system_prompt ⇒ Object
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).
-
#username ⇒ Object
Database connection.
-
#write_classifier ⇒ Object
Classifier used in :ask mode to decide whether a query writes.
Class Method Summary collapse
-
.default_confirm_callback ⇒ Object
Default TTY-based confirmation.
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ Configuration
constructor
A new instance of Configuration.
- #validate! ⇒ Object
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
#adapter ⇒ Object
Database connection
7 8 9 |
# File 'lib/llmdb/configuration.rb', line 7 def adapter @adapter end |
#confirm_callback ⇒ Object
51 52 53 |
# File 'lib/llmdb/configuration.rb', line 51 def confirm_callback @confirm_callback || self.class.default_confirm_callback end |
#database ⇒ Object
Database connection
7 8 9 |
# File 'lib/llmdb/configuration.rb', line 7 def database @database end |
#default_system_prompt ⇒ Object
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 |
#host ⇒ Object
Database connection
7 8 9 |
# File 'lib/llmdb/configuration.rb', line 7 def host @host end |
#llm_api_base ⇒ Object
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_key ⇒ Object
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_exists ⇒ Object
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_model ⇒ Object
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_provider ⇒ Object
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_rows ⇒ Object
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 |
#password ⇒ Object
Database connection
7 8 9 |
# File 'lib/llmdb/configuration.rb', line 7 def password @password end |
#permission_mode ⇒ Object
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 end |
#port ⇒ Object
Database connection
7 8 9 |
# File 'lib/llmdb/configuration.rb', line 7 def port @port end |
#system_prompt ⇒ Object
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 |
#username ⇒ Object
Database connection
7 8 9 |
# File 'lib/llmdb/configuration.rb', line 7 def username @username end |
#write_classifier ⇒ Object
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_callback ⇒ Object
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! end |