Module: LLM::Sequel::Plugin

Defined in:
lib/llm/sequel/plugin.rb

Overview

Sequel plugin for persisting LLM::Context state.

This plugin maps model columns onto provider selection, model selection, usage accounting, and serialized context data while leaving application-specific concerns such as credentials, associations, and UI shaping to the host app.

Context state can be stored as a JSON string (‘format: :string`, the default) or as a structured object (`format: :json` / `:jsonb`) for databases such as PostgreSQL that can persist JSON natively. `:json` and `:jsonb` expect a real JSON column type with Sequel handling JSON typecasting for the model.

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Constant Summary collapse

EMPTY_HASH =
{}.freeze
DEFAULT_USAGE_COLUMNS =
{
  input_tokens: :input_tokens,
  output_tokens: :output_tokens,
  total_tokens: :total_tokens
}.freeze
DEFAULTS =
{
  provider_column: :provider,
  model_column: :model,
  data_column: :data,
  format: :string,
  usage_columns: DEFAULT_USAGE_COLUMNS,
  provider: EMPTY_HASH,
  context: EMPTY_HASH
}.freeze

Class Method Summary collapse

Class Method Details

.apply(model) ⇒ void

This method returns an undefined value.

Called by Sequel when the plugin is first applied to a model class.

This hook installs the plugin’s class- and instance-level behavior on the target model. It runs before configure, so it should only attach methods and not depend on per-model plugin options.

Parameters:

  • model (Class)


43
44
45
46
# File 'lib/llm/sequel/plugin.rb', line 43

def self.apply(model, **)
  model.extend ClassMethods
  model.include InstanceMethods
end

.configure(model, options = EMPTY_HASH) ⇒ void

This method returns an undefined value.

Called by Sequel after apply with the options passed to ‘plugin :llm, …`.

This hook merges plugin defaults with the model’s explicit settings and stores the resolved configuration on the model class for later use by instance methods such as LLM::Sequel::Plugin::InstanceMethods#llm and LLM::Sequel::Plugin::InstanceMethods#ctx.

Parameters:

  • model (Class)
  • options (Hash) (defaults to: EMPTY_HASH)

Options Hash (options):

  • :format (Symbol)

    Storage format for the serialized context. Use ‘:string` for text columns, or `:json` / `:jsonb` for structured JSON columns with Sequel JSON typecasting enabled.



63
64
65
66
67
68
69
70
# File 'lib/llm/sequel/plugin.rb', line 63

def self.configure(model, options = EMPTY_HASH)
  options = DEFAULTS.merge(options)
  usage_columns = DEFAULT_USAGE_COLUMNS.merge(options[:usage_columns] || EMPTY_HASH)
  model.instance_variable_set(
    :@llm_plugin_options,
    options.merge(usage_columns: usage_columns.freeze).freeze
  )
end