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::COMPACT_DURATION_EVENT, Hooks::COMPACT_END_EVENT, Hooks::COMPACT_FAILURE_EVENT, Hooks::COMPACT_START_EVENT, Hooks::CONTENT_EVENT, Hooks::FARADAY_ERROR_EVENT, Hooks::LLM_DURATION_EVENT, Hooks::LLM_END_EVENT, Hooks::LLM_FAILURE_EVENT, Hooks::LLM_START_EVENT, Hooks::MIDDLEWARE_ADDED_EVENT, Hooks::MIDDLEWARE_DURATION_EVENT, Hooks::MIDDLEWARE_END_EVENT, Hooks::MIDDLEWARE_FAILURE_EVENT, Hooks::MIDDLEWARE_START_EVENT, Hooks::OPEN_ROUTER_SERVER_ERROR_EVENT, Hooks::REASONING_EVENT, Hooks::STANDARD_ERROR_EVENT, Hooks::TOOL_APPROVE_EVENT, Hooks::TOOL_CALLS_EVENT, Hooks::TOOL_DURATION_EVENT, Hooks::TOOL_END_EVENT, Hooks::TOOL_FAILURE_EVENT, Hooks::TOOL_START_EVENT, Hooks::TURN_DURATION_EVENT, Hooks::TURN_END_EVENT, Hooks::TURN_FAILURE_EVENT, Hooks::TURN_START_EVENT

Instance Method Summary collapse

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
38
39
40
41
# File 'lib/brute/middleware/002_session_log.rb', line 32

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