Class: Protege::LoadRecordResolver

Inherits:
Resolver
  • Object
show all
Defined in:
app/resolvers/protege/load_record_resolver.rb

Overview

General-purpose resolver that loads ActiveRecord record(s) and contributes them as one context message — the database counterpart to LoadFileResolver. Like LoadFileResolver, it is deliberately not named for a use case: the developer decides what the records mean (account state, open invoices, reference data) and what message role carries them.

The single primitive is a loader block that returns record(s). It receives the resolver context, so loading can key off the inbound message — e.g. find the account that emailed in. A static query is just a block that ignores the context. We intentionally do not offer a +model:+/+where:+ DSL: it would be strictly less capable than the block (no scopes, joins, or find vs where) and would collide column names with resolver options.

Serialization is per-record and always produces text. By default each record is rendered as pretty-printed JSON; serialize: overrides that with a Symbol (a method on the record returning text) or a Proc (called with the record, returning text). The rendered records are joined into one message. A nil or empty result contributes nothing (returns nil), since "no matching rows" is a normal runtime state — unlike LoadFileResolver's missing-file misconfiguration.

resolvers do |chain|
chain.use(Protege::LoadRecordResolver) { |ctx| Account.find_by(email: ctx.message.from_address) }
chain.use(Protege::LoadRecordResolver, serialize: :to_llm_markdown) { Setting.active }
end

Constant Summary collapse

RECORD_SEPARATOR =

Separator between each record's text rendering when more than one is loaded.

"\n\n"

Instance Method Summary collapse

Constructor Details

#initialize(role: :system, serialize: nil) {|context| ... } ⇒ LoadRecordResolver

Returns a new instance of LoadRecordResolver.

Parameters:

  • role (Symbol) (defaults to: :system)

    role for the emitted message; defaults to :system

  • serialize (Symbol, Proc, nil) (defaults to: nil)

    per-record serializer returning text — a method name to call on each record, or a Proc receiving each record; defaults to pretty-printed JSON

Yield Parameters:

Yield Returns:

  • (ActiveRecord::Base, ActiveRecord::Relation, Array, nil)

    the record(s) to load

Raises:

  • (ArgumentError)

    when no loader block is given



35
36
37
38
39
40
41
42
# File 'app/resolvers/protege/load_record_resolver.rb', line 35

def initialize(role: :system, serialize: nil, &loader)
  super()
  raise ArgumentError, 'LoadRecordResolver requires a block returning record(s)' unless loader

  @role      = role
  @serialize = serialize
  @loader    = loader
end

Instance Method Details

#resolve(context:) ⇒ Protege::ModelMessage?

Run the loader and emit its record(s) as one message, or nil when nothing is loaded.

Parameters:

Returns:



48
49
50
51
52
53
54
# File 'app/resolvers/protege/load_record_resolver.rb', line 48

def resolve(context:)
  records = Array(@loader.call(context))
  return nil if records.empty?

  content = render(records)
  message(role: @role, content:)
end