Class: Brute::Middleware::SessionLog

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

Overview

The "session" is just a JSONL log of the conversation on disk. This middleware owns that log — there is no Session class.

in  -> if the file exists, prepend its messages to env[:messages] so
     this turn continues the prior conversation.
out <- write the log back to disk (skipping the :system message, which
     the SystemPrompt middleware re-adds each turn).

Put it near the top of the stack (outermost) so history is loaded before the rest of the middleware runs and the whole turn is persisted after:

Brute.agent
.use(Brute::Middleware::SessionLog, path: "tmp/session.jsonl")
.use(Brute::Middleware::SystemPrompt)
...

Constant Summary

Constants included from Hooks

Hooks::AFTER_LLM_EVENT, Hooks::AFTER_TOOL_EVENT, Hooks::APPROVE_TOOL_EVENT, Hooks::BEFORE_LLM_EVENT, Hooks::BEFORE_TOOL_EVENT, Hooks::COMPACTED_EVENT, Hooks::COMPACT_DURATION_EVENT, Hooks::DURATION_EVENT, Hooks::ENTER_EVENT, Hooks::EXIT_EVENT, Hooks::FARADAY_ERROR_EVENT, Hooks::LLM_DURATION_EVENT, Hooks::LLM_FAILURE_EVENT, Hooks::MIDDLEWARE_ADDED_EVENT, Hooks::OPEN_ROUTER_SERVER_ERROR_EVENT, Hooks::STANDARD_ERROR_EVENT, Hooks::TOOL_DURATION_EVENT, Hooks::TURN_DURATION_EVENT, Hooks::TURN_END_EVENT, Hooks::TURN_START_EVENT

Instance Method Summary collapse

Methods included from Hooks

new

Constructor Details

#initialize(app, path:) ⇒ SessionLog

Returns a new instance of SessionLog.



27
28
29
30
# File 'lib/brute/middleware/002_session_log.rb', line 27

def initialize(app, path:)
  @app  = app
  @path = path
end

Instance Method Details

#call(env) ⇒ Object



32
33
34
35
36
37
# File 'lib/brute/middleware/002_session_log.rb', line 32

def call(env)
  load_into(env[:messages]) if @path && File.exist?(@path)
  @app.call(env)
  persist(env[:messages]) if @path
  env
end