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)


70
71
72
73
74
75
76
77
78
79
80
# File 'lib/llm/sequel/plugin.rb', line 70

def self.columns(options)
  usage_columns = options[:usage_columns]
  {
    provider_column: options[:provider_column],
    model_column: options[:model_column],
    data_column: options[:data_column],
    input_tokens: usage_columns[:input_tokens],
    output_tokens: usage_columns[:output_tokens],
    total_tokens: usage_columns[:total_tokens]
  }.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:



37
38
39
40
41
42
43
44
# File 'lib/llm/sequel/plugin.rb', line 37

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)


49
50
51
52
53
54
# File 'lib/llm/sequel/plugin.rb', line 49

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

.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.



85
86
87
88
89
90
91
92
93
# File 'lib/llm/sequel/plugin.rb', line 85

def self.save(obj, ctx, options)
  columns = self.columns(options)
  obj.update(
    columns[:data_column] => serialize_context(ctx, options[:format]),
    columns[:input_tokens] => ctx.usage.input_tokens,
    columns[:output_tokens] => ctx.usage.output_tokens,
    columns[:total_tokens] => ctx.usage.total_tokens
  )
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)


59
60
61
62
63
64
65
# File 'lib/llm/sequel/plugin.rb', line 59

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