Module: LLM::Sequel::Plugin::Utils Private

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

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Shared helper methods for the ORM wrapper.

These utilities keep persistence plumbing out of the wrapped model’s method namespace so the injected surface stays focused on the runtime API itself.

Class Method Summary collapse

Class Method Details

.columns(options) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Maps wrapper options onto the record’s storage columns.

Returns:

  • (Hash)


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

def self.columns(options)
  {
    data_column: options[:data_column]
  }.freeze
end

.resolve_option(obj, option) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Resolves a single configured option against a model instance.

Returns:



32
33
34
35
36
37
38
39
# File 'lib/llm/sequel/plugin.rb', line 32

def self.resolve_option(obj, option)
  case option
  when Proc then obj.instance_exec(&option)
  when Symbol then obj.send(option)
  when Hash then option.dup
  else option
  end
end

.resolve_options(obj, option, empty_hash) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Resolves hash-like wrapper options against a model instance.

Returns:

  • (Hash)


44
45
46
47
48
49
# File 'lib/llm/sequel/plugin.rb', line 44

def self.resolve_options(obj, option, empty_hash)
  case option
  when Proc, Symbol, Hash then resolve_option(obj, option)
  else empty_hash.dup
  end
end

.resolve_provider(obj, options, empty_hash) ⇒ LLM::Provider

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Resolves the provider runtime for a record.

Returns:

Raises:

  • (ArgumentError)


74
75
76
77
78
# File 'lib/llm/sequel/plugin.rb', line 74

def self.resolve_provider(obj, options, empty_hash)
  provider = resolve_option(obj, options[:provider])
  return provider if LLM::Provider === provider
  raise ArgumentError, "provider: must resolve to an LLM::Provider instance"
end

.save(obj, ctx, options) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Persists the runtime state and usage columns back onto the record.



83
84
85
86
87
88
# File 'lib/llm/sequel/plugin.rb', line 83

def self.save(obj, ctx, options)
  columns = self.columns(options)
  payload = serialize_context(ctx, options[:format])
  payload = wrap_json_payload(payload, options[:format])
  obj.update(columns[:data_column] => payload)
end

.serialize_context(ctx, format) ⇒ String, Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Serializes the runtime into the configured storage format.

Returns:

  • (String, Hash)


54
55
56
57
58
59
60
# File 'lib/llm/sequel/plugin.rb', line 54

def self.serialize_context(ctx, format)
  case format
  when :string then ctx.to_json
  when :json, :jsonb then ctx.to_h
  else raise ArgumentError, "Unknown format: #{format.inspect}"
  end
end

.wrap_json_payload(payload, format) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Wraps JSON payloads for Sequel PostgreSQL adapters when needed.

Returns:



93
94
95
96
97
98
99
# File 'lib/llm/sequel/plugin.rb', line 93

def self.wrap_json_payload(payload, format)
  case format
  when :json then Sequel.pg_json_wrap(payload)
  when :jsonb then Sequel.pg_jsonb_wrap(payload)
  else payload
  end
end