Class: Brute::Middleware::MessageTracking

Inherits:
Base
  • Object
show all
Defined in:
lib/brute/middleware/message_tracking.rb

Overview

Records every LLM exchange into a MessageStore in the OpenCode parts format so sessions can be viewed later.

Lifecycle per pipeline call:

1. PRE-CALL  — if this is the first call of a turn (env[:tool_results]
   is nil), record the user message.
2. POST-CALL — record the assistant message: text content as a "text"
   part, each tool call as a "tool" part in "running" state.
3. When the pipeline is called again with tool results, update the
   corresponding tool parts to "completed" (or "error").

The middleware also stores itself in env so the orchestrator can access the current assistant message ID for callbacks.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, store:) ⇒ MessageTracking

Returns a new instance of MessageTracking.



28
29
30
31
32
33
# File 'lib/brute/middleware/message_tracking.rb', line 28

def initialize(app, store:)
  super(app)
  @store = store
  @current_user_id = nil
  @current_assistant_id = nil
end

Instance Attribute Details

#storeObject (readonly)

Returns the value of attribute store.



26
27
28
# File 'lib/brute/middleware/message_tracking.rb', line 26

def store
  @store
end

Instance Method Details

#call(env) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/brute/middleware/message_tracking.rb', line 35

def call(env)
  env[:message_tracking] = self

  # ── Pre-call: record user message or update tool results ──
  if env[:tool_results].nil?
    # New turn — record the user message
    record_user_message(env)
  else
    # Tool results coming back — complete the tool parts
    complete_tool_parts(env)
  end

  # ── LLM call ──
  response = @app.call(env)

  # ── Post-call: record assistant message ──
  record_assistant_message(env, response)

  response
end

#current_assistant_idObject

The current assistant message ID (used by external callbacks).



57
58
59
# File 'lib/brute/middleware/message_tracking.rb', line 57

def current_assistant_id
  @current_assistant_id
end