Class: Brute::Middleware::SystemPrompt

Inherits:
Object
  • Object
show all
Defined in:
lib/brute/middleware/020_system_prompt.rb

Overview

Prepends a system message to env before passing control down the middleware chain.

By default, uses Brute::SystemPrompt.default which assembles a provider-specific prompt stack (Identity, ToneAndStyle, ToolUsage, etc.) from the Brute::Prompts modules and text files.

Pass a custom Brute::SystemPrompt instance to override — useful for SubAgents that need a specialized prompt (e.g. the explore agent prompt):

use Brute::Middleware::SystemPrompt,
    system_prompt: Brute::SystemPrompt.build { |p, _ctx|
      p << Brute::Prompts.agent_prompt("explore")
    }

Skips injection when env already contains a :system message (e.g. from session.system(…)), so manually-set system prompts are respected.

Instance Method Summary collapse

Constructor Details

#initialize(app, system_prompt: Brute::SystemPrompt.default) ⇒ SystemPrompt

Returns a new instance of SystemPrompt.



29
30
31
32
# File 'lib/brute/middleware/020_system_prompt.rb', line 29

def initialize(app, system_prompt: Brute::SystemPrompt.default)
  @app = app
  @system_prompt = system_prompt
end

Instance Method Details

#call(env) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/brute/middleware/020_system_prompt.rb', line 34

def call(env)
  unless env[:messages].any? { |m| m.role == :system }
    ctx = build_context(env)
    result = @system_prompt.prepare(ctx)
    unless result.empty?
      env[:messages].unshift(
        RubyLLM::Message.new(role: :system, content: result.to_s)
      )
    end
  end

  @app.call(env)
end