Module: RubyPi::Context::Transform

Defined in:
lib/ruby_pi/context/transform.rb

Overview

Factory methods for building transform_context callables. Each method returns a Proc that accepts an Agent::State and mutates it. Use ‘compose` to chain multiple transforms.

Examples:

Composing transforms

transform = RubyPi::Context::Transform.compose(
  RubyPi::Context::Transform.inject_datetime,
  RubyPi::Context::Transform.inject_user_preferences { |state| state.user_data[:prefs] }
)
agent = RubyPi::Agent.new(transform_context: transform, ...)

Class Method Summary collapse

Class Method Details

.compose(*transforms) ⇒ Proc

Chains multiple transform callables into a single callable that executes them in order. Each transform receives the same State object and can mutate it freely.

Examples:

combined = Transform.compose(transform_a, transform_b, transform_c)
combined.call(state) # runs a, then b, then c

Parameters:

  • transforms (Array<Proc>)

    transform callables to chain

Returns:

  • (Proc)

    a single callable that runs all transforms in sequence



38
39
40
41
42
# File 'lib/ruby_pi/context/transform.rb', line 38

def compose(*transforms)
  ->(state) do
    transforms.each { |t| t.call(state) }
  end
end

.inject_datetimeProc

Returns a transform that appends the current date and time to the system prompt. Useful for giving the LLM temporal awareness.

Examples:

transform = Transform.inject_datetime
# Appends: "\n\nCurrent date and time: 2025-01-15 14:30:00 UTC"

Returns:

  • (Proc)

    transform callable



52
53
54
55
56
57
# File 'lib/ruby_pi/context/transform.rb', line 52

def inject_datetime
  ->(state) do
    timestamp = Time.now.utc.strftime("%Y-%m-%d %H:%M:%S UTC")
    state.system_prompt += "\n\nCurrent date and time: #{timestamp}"
  end
end

.inject_user_preferences {|state| ... } ⇒ Proc

Returns a transform that appends user preferences to the system prompt. The block is called with the state and should return a string or hash of preferences. If nil is returned, nothing is appended.

Examples:

transform = Transform.inject_user_preferences { |s| s.user_data[:prefs] }

Yields:

  • (state)

    block that extracts preferences from the state

Yield Parameters:

Yield Returns:

  • (String, Hash, nil)

    preferences to inject

Returns:

  • (Proc)

    transform callable



71
72
73
74
75
76
77
78
79
# File 'lib/ruby_pi/context/transform.rb', line 71

def inject_user_preferences(&block)
  ->(state) do
    preferences = block.call(state)
    return if preferences.nil?

    prefs_text = preferences.is_a?(Hash) ? format_hash(preferences) : preferences.to_s
    state.system_prompt += "\n\n[User Preferences]\n#{prefs_text}"
  end
end

.inject_workspace_context {|state| ... } ⇒ Proc

Returns a transform that appends workspace context to the system prompt. The block is called with the state and should return contextual information about the current workspace/project.

Examples:

transform = Transform.inject_workspace_context { |s| s.user_data[:workspace] }

Yields:

  • (state)

    block that extracts workspace context from the state

Yield Parameters:

Yield Returns:

  • (String, Hash, nil)

    workspace context to inject

Returns:

  • (Proc)

    transform callable



92
93
94
95
96
97
98
99
100
# File 'lib/ruby_pi/context/transform.rb', line 92

def inject_workspace_context(&block)
  ->(state) do
    context = block.call(state)
    return if context.nil?

    ctx_text = context.is_a?(Hash) ? format_hash(context) : context.to_s
    state.system_prompt += "\n\n[Workspace Context]\n#{ctx_text}"
  end
end