Class: LanguageOperator::Agent::PromptBuilder

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/language_operator/agent/prompt_builder.rb

Overview

Dynamic Prompt Builder

Generates persona-driven system prompts by combining static persona configuration with dynamic agent metadata and operational context.

Supports multiple template styles and configurable levels of context injection. Falls back to static prompts for backward compatibility.

Examples:

Basic usage

builder = PromptBuilder.new(agent, chat_endpoint_config)
prompt = builder.build_system_prompt

With custom template

builder = PromptBuilder.new(agent, config, template: :detailed)
prompt = builder.build_system_prompt

Constant Summary collapse

TEMPLATE_LEVELS =

Template levels for different amounts of context injection

{
  minimal: :build_minimal_template,
  standard: :build_standard_template,
  detailed: :build_detailed_template,
  comprehensive: :build_comprehensive_template
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Loggable

#logger

Constructor Details

#initialize(agent, chat_config, **options) ⇒ PromptBuilder

Initialize prompt builder

Parameters:

  • The agent instance

  • Chat endpoint configuration

  • Additional options

Options Hash (**options):

  • :template (Symbol)

    Template level (:minimal, :standard, :detailed, :comprehensive)

  • :enable_identity_awareness (Boolean)

    Enable identity context injection



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/language_operator/agent/prompt_builder.rb', line 43

def initialize(agent, chat_config, **options)
  @agent = agent
  @chat_config = chat_config
  @options = options
  @metadata_collector = MetadataCollector.new(agent)

  # Configuration
  @template_level = options[:template] || chat_config&.prompt_template_level || :standard
  @identity_awareness_enabled = if options.key?(:enable_identity_awareness)
                                   options[:enable_identity_awareness]
                                 elsif chat_config&.identity_awareness_enabled.nil?
                                   true
                                 else
                                   chat_config.identity_awareness_enabled
                                 end
  @static_prompt = chat_config&.system_prompt
end

Instance Attribute Details

#agentObject (readonly)

Returns the value of attribute agent.



26
27
28
# File 'lib/language_operator/agent/prompt_builder.rb', line 26

def agent
  @agent
end

#chat_configObject (readonly)

Returns the value of attribute chat_config.



26
27
28
# File 'lib/language_operator/agent/prompt_builder.rb', line 26

def chat_config
  @chat_config
end

#metadata_collectorObject (readonly)

Returns the value of attribute metadata_collector.



26
27
28
# File 'lib/language_operator/agent/prompt_builder.rb', line 26

def 
  @metadata_collector
end

Instance Method Details

#build_conversation_contextString

Build prompt for conversation context (shorter version)

Returns:

  • Conversation context prompt



90
91
92
93
94
95
96
97
98
# File 'lib/language_operator/agent/prompt_builder.rb', line 90

def build_conversation_context
  return nil unless @identity_awareness_enabled

   = @metadata_collector.summary_for_prompt
  build_conversation_context_template()
rescue StandardError => e
  logger.error('Failed to build conversation context', error: e.message)
  nil
end

#build_system_promptString

Build complete system prompt with persona and context

Returns:

  • Generated system prompt



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/language_operator/agent/prompt_builder.rb', line 64

def build_system_prompt
  # Return static prompt if identity awareness is disabled
  unless @identity_awareness_enabled
    return @static_prompt || build_fallback_prompt
  end

  # Collect metadata for context injection
   = @metadata_collector.summary_for_prompt

  # Build dynamic prompt based on template level
  if TEMPLATE_LEVELS.key?(@template_level)
    method_name = TEMPLATE_LEVELS[@template_level]
    send(method_name, )
  else
    logger.warn("Unknown template level: #{@template_level}, falling back to standard")
    build_standard_template()
  end
rescue StandardError => e
  logger.error('Failed to build dynamic system prompt, falling back to static',
               error: e.message)
  @static_prompt || build_fallback_prompt
end