Class: SolidLoop::Middlewares::EventLogging

Inherits:
Object
  • Object
show all
Defined in:
app/services/solid_loop/middlewares/event_logging.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ EventLogging

Returns a new instance of EventLogging.



4
5
6
# File 'app/services/solid_loop/middlewares/event_logging.rb', line 4

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/services/solid_loop/middlewares/event_logging.rb', line 8

def call(env)
  headers = { "Content-Type" => "application/json" }
  headers["Authorization"] = "Bearer #{env.model_config[:api_token]}" if env.model_config[:api_token].present?

  safe_headers = headers.dup
  safe_headers["Authorization"] = "[REDACTED]" if safe_headers.key?("Authorization")

  env.event = SolidLoop::Event.new(
    eventable: env.triggering_message || env.loop,
    loop_id: env.loop.id,
    message_id: env.triggering_message&.id,
    name: "llm_completion",
    http_method: "POST",
    url: env.model_config[:base_url].to_s,
    request_data: env.payload,
    headers: safe_headers,
    log: "Requesting LLM (#{env.agent.streaming? ? 'Streaming' : 'Sync'})..."
  )

  # We save before request if configured for slow models debugging
  env.event.save! if SolidLoop::LlmCompletionJob::EVENTS_SAVE_BEFORE_REQUEST

  # Set up log buffers for the network layer to use
  env.log_buffer ||= StringIO.new
  env.debug_output ||= StringIO.new

  begin
    env.mark_request_started!
    @app.call(env)
  ensure
    finalize_event(env)
  end
end