Class: Brute::Middleware::SessionLog

Inherits:
Object
  • 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)
...

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
# 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