Class: Protege::LoadTextResolver

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

Overview

General-purpose resolver that runs a block and contributes its returned String as one context message — the computed-text sibling of LoadFileResolver (text from a file) and LoadRecordResolver (text from records). The block receives the resolver context, so the text can be derived from the persona or the inbound message. The canonical use is a persona's system prompt held in a column:

resolvers do |chain|
chain.use(Protege::LoadTextResolver, role: :system) { |ctx| ctx.persona.instructions }
end

role chooses how the text enters the conversation (+:system+ for a prompt, +:user+/+:assistant+ to seed turns) and defaults to :system. A nil or blank result contributes nothing (returns nil), so an unset column simply adds no message.

Instance Method Summary collapse

Constructor Details

#initialize(role: :system) {|context| ... } ⇒ LoadTextResolver

Returns a new instance of LoadTextResolver.

Parameters:

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

    role for the emitted message; defaults to :system

Yield Parameters:

Yield Returns:

  • (String, nil)

    the text to contribute

Raises:

  • (ArgumentError)

    when no block is given



21
22
23
24
25
26
27
# File 'app/resolvers/protege/load_text_resolver.rb', line 21

def initialize(role: :system, &block)
  super()
  raise ArgumentError, 'LoadTextResolver requires a block returning text' unless block

  @role  = role
  @block = block
end

Instance Method Details

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

Run the block and emit its text as a single message, or nil when the result is blank.

Parameters:

Returns:



33
34
35
36
37
38
# File 'app/resolvers/protege/load_text_resolver.rb', line 33

def resolve(context:)
  content = @block.call(context).to_s.strip
  return nil if content.empty?

  message(role: @role, content:)
end