Module: LLM::ActiveRecord::ActsAsAgent

Defined in:
lib/llm/active_record/acts_as_agent.rb

Overview

ActiveRecord integration for persisting LLM::Agent state.

This wrapper reuses the same record-backed runtime surface as ActsAsLLM, but builds an LLM::Agent instead of an LLM::Context. Agent defaults such as model, tools, schema, instructions, and concurrency are configured on the model class and forwarded to an internal agent subclass.

Defined Under Namespace

Modules: ClassMethods, Hooks, InstanceMethods

Constant Summary collapse

EMPTY_HASH =
LLM::ActiveRecord::ActsAsLLM::EMPTY_HASH
DEFAULTS =
LLM::ActiveRecord::ActsAsLLM::DEFAULTS
Utils =
LLM::ActiveRecord::ActsAsLLM::Utils

Instance Method Summary collapse

Instance Method Details

#acts_as_agent(options = EMPTY_HASH) { ... } ⇒ void

This method returns an undefined value.

Installs the acts_as_agent wrapper on an ActiveRecord model.

Parameters:

  • options (Hash) (defaults to: EMPTY_HASH)

Options Hash (options):

  • :format (Symbol)

    Storage format for the serialized agent state. Use :string for text columns, or :json / :jsonb for structured JSON columns with ActiveRecord JSON typecasting enabled.

  • :tracer (Proc, Symbol, LLM::Tracer, nil)

    Optional tracer, method name, or proc that resolves to one and is assigned through llm.tracer = ... on the resolved provider.

  • :provider (Proc, Symbol, LLM::Provider)

    Must resolve to an LLM::Provider instance for the current record.

Yields:

  • Evaluated in the model class after the wrapper is installed, so agent DSL methods such as model, tools, schema, instructions, and concurrency can be configured inline.



84
85
86
87
88
89
90
# File 'lib/llm/active_record/acts_as_agent.rb', line 84

def acts_as_agent(options = EMPTY_HASH, &block)
  options = DEFAULTS.merge(options)
  class_attribute :llm_plugin_options, instance_accessor: false, default: DEFAULTS unless respond_to?(:llm_plugin_options)
  self.llm_plugin_options = options.freeze
  extend Hooks
  class_exec(&block) if block
end