Module: DebugAgent::MiddlewareCore

Defined in:
lib/debug_agent/middleware.rb

Overview

Shared routing logic used by both RackMiddleware class and Middleware lambda

Constant Summary collapse

CORS_HEADERS =
{
  'Access-Control-Allow-Origin' => '*',
  'Access-Control-Allow-Methods' => 'GET, POST, OPTIONS',
  'Access-Control-Allow-Headers' => 'Content-Type, Authorization',
}.freeze

Class Method Summary collapse

Class Method Details

.call(env, app, engine, config) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/debug_agent/middleware.rb', line 99

def self.call(env, app, engine, config)
  path = env['PATH_INFO']
  method = env['REQUEST_METHOD']
  base = config.base_path

  # CORS preflight for all agent routes
  if path.start_with?(base) && method == 'OPTIONS'
    return [204, CORS_HEADERS.merge('Content-Length' => '0'), []]
  end

  # Chat UI
  if path == base || path == "#{base}/"
    if method == 'GET'
      html = ChatPage.render(base)
      return [200, { 'Content-Type' => 'text/html; charset=utf-8' }.merge(CORS_HEADERS), [html]]
    end
  end

  # SSE streaming chat — real-time streaming via Rack response body
  if path == "#{base}/api/chat" && method == 'POST'
    body = JSON.parse(env['rack.input'].read)
    message = body['message'] || ''
    session_id = body['sessionId'] || "session-#{Time.now.to_i}"

    cb = SseCallback.new

    # Run engine in a background thread for real-time streaming
    Thread.new do
      begin
        engine.chat(message, session_id, cb)
      rescue => e
        cb.on_error("Internal error: #{e.message}")
      end
    end

    # Return a streaming response body (Rack 2+ / Rack 3 compatible)
    streaming_body = cb.streaming_enum

    return [200, {
      'Content-Type' => 'text/event-stream',
      'Cache-Control' => 'no-cache',
      'Connection' => 'keep-alive',
    }.merge(CORS_HEADERS), streaming_body]
  end

  # Clear conversation
  if path == "#{base}/api/clear" && method == 'POST'
    body = JSON.parse(env['rack.input'].read)
    session_id = body['sessionId'] || ''
    engine.clear_session(session_id) if session_id && !session_id.empty?
    return [200, { 'Content-Type' => 'application/json' }.merge(CORS_HEADERS), [JSON.generate({ status: 'cleared' })]]
  end

  # Health check
  if path == "#{base}/api/health" && method == 'GET'
    return [200, { 'Content-Type' => 'application/json' }.merge(CORS_HEADERS), [JSON.generate({ status: 'ok', agent: 'ruby-debug-agent' })]]
  end

  # List tools
  if path == "#{base}/api/tools" && method == 'GET'
    return [200, { 'Content-Type' => 'application/json' }.merge(CORS_HEADERS), [JSON.generate({ tools: engine.tools.all_schemas })]]
  end

  # Pass through to the wrapped app
  if app
    app.call(env)
  else
    [404, { 'Content-Type' => 'text/plain' }, ['Not Found']]
  end
end