File size: 26,670 Bytes
332826f
 
 
 
 
 
 
 
 
 
 
6379bd0
acdc6c1
332826f
 
 
 
 
 
6379bd0
 
 
 
677456b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e8080f5
 
 
 
 
 
 
 
 
 
 
677456b
 
 
e8080f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6379bd0
 
 
 
 
 
 
 
 
 
677456b
6379bd0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
470e737
 
6379bd0
 
 
 
 
 
 
 
 
 
 
470e737
 
 
 
6379bd0
 
 
 
332826f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
470e737
 
 
 
 
 
 
 
 
 
 
332826f
 
 
 
 
 
 
 
 
 
7caa6ba
 
 
 
 
 
 
332826f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
677456b
332826f
 
 
 
 
677456b
 
 
 
6379bd0
 
 
 
 
332826f
677456b
 
 
332826f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6379bd0
 
332826f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e8080f5
 
 
 
6379bd0
 
677456b
 
 
6379bd0
332826f
 
 
 
 
 
 
 
 
 
 
 
 
7caa6ba
 
 
 
332826f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7caa6ba
 
 
 
332826f
 
 
 
 
7caa6ba
332826f
7caa6ba
 
 
332826f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7caa6ba
 
332826f
7caa6ba
332826f
 
1
2
3
4
5
6
7
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
#include "server.h"

#include <boost/beast/core.hpp>

#include "http_helpers.h"
#include "model_manager.h"
#include "request_parsing.h"
#include "runtime_components.h"

#include <algorithm>
#include <atomic>
#include <sstream>
#include <utility>

namespace beast = boost::beast;
namespace http = beast::http;

static std::atomic<uint64_t> g_req_id{1};

static std::string build_sse_event(const json &payload) {
    return "data: " + payload.dump() + "\n\n";
}

static std::string extract_chat_text(const json &completion) {
    if (!completion.is_object()) return "";
    if (!completion.contains("choices") || !completion["choices"].is_array() || completion["choices"].empty()) {
        return "";
    }
    const auto &choice = completion["choices"][0];
    if (!choice.is_object()) return "";
    if (choice.contains("message") && choice["message"].is_object()) {
        const auto &message = choice["message"];
        if (message.contains("content") && message["content"].is_string()) {
            return message["content"].get<std::string>();
        }
    }
    if (choice.contains("text") && choice["text"].is_string()) {
        return choice["text"].get<std::string>();
    }
    return "";
}

static json completion_payload_to_chat_payload(const json &payload, const LimitsConfig &limits) {
    json chat_payload = json::object();
    chat_payload["messages"] = json::array();

    if (payload.contains("prompt")) {
        if (payload["prompt"].is_string()) {
            chat_payload["messages"].push_back({
                {"role", "user"},
                {"content", payload["prompt"].get<std::string>()}
            });
        } else if (payload["prompt"].is_array()) {
            std::string joined_prompt;
            bool first = true;
            for (const auto &item : payload["prompt"]) {
                if (!item.is_string()) continue;
                if (!first) joined_prompt += "\n";
                joined_prompt += item.get<std::string>();
                first = false;
            }
            chat_payload["messages"].push_back({
                {"role", "user"},
                {"content", joined_prompt}
            });
        }
    }

    int normalized_max_tokens = limits.default_max_tokens;
    if (payload.contains("n_predict") && payload["n_predict"].is_number_integer()) {
        normalized_max_tokens = payload["n_predict"].get<int>();
    } else if (payload.contains("max_tokens") && payload["max_tokens"].is_number_integer()) {
        normalized_max_tokens = payload["max_tokens"].get<int>();
    }
    if (normalized_max_tokens <= 0) {
        normalized_max_tokens = limits.default_max_tokens;
    }
    chat_payload["max_tokens"] = normalized_max_tokens;

    if (chat_payload["messages"].empty()) {
        chat_payload["messages"].push_back({
            {"role", "user"},
            {"content", ""}
        });
    }

    if (payload.contains("temperature")) chat_payload["temperature"] = payload["temperature"];
    if (payload.contains("top_p")) chat_payload["top_p"] = payload["top_p"];
    if (payload.contains("top_k")) chat_payload["top_k"] = payload["top_k"];
    if (payload.contains("stop")) chat_payload["stop"] = payload["stop"];
    if (payload.contains("stream")) chat_payload["stream"] = payload["stream"];
    return chat_payload;
}

static std::string build_completion_compat_response(const std::string &completion_body) {
    json completion = json::parse(completion_body, nullptr, false);
    if (completion.is_discarded() || !completion.is_object()) {
        return completion_body;
    }

    json out = {
        {"content", extract_chat_text(completion)}
    };

    if (completion.contains("stop")) out["stop"] = completion["stop"];
    if (completion.contains("stopped_eos")) out["stopped_eos"] = completion["stopped_eos"];
    if (completion.contains("stopped_limit")) out["stopped_limit"] = completion["stopped_limit"];
    if (completion.contains("tokens_predicted")) out["tokens_predicted"] = completion["tokens_predicted"];
    if (completion.contains("tokens_evaluated")) out["tokens_evaluated"] = completion["tokens_evaluated"];
    if (completion.contains("timings")) out["timings"] = completion["timings"];

    // Map OpenAI usage fields to native llama.cpp field names if not already set
    if (completion.contains("usage") && completion["usage"].is_object()) {
        const auto &usage = completion["usage"];
        if (!out.contains("tokens_predicted") && usage.contains("completion_tokens"))
            out["tokens_predicted"] = usage["completion_tokens"];
        if (!out.contains("tokens_evaluated") && usage.contains("prompt_tokens"))
            out["tokens_evaluated"] = usage["prompt_tokens"];
    }

    return out.dump();
}

// Emits native llama.cpp SSE format for the /completion + stream=true case.
// The web UI expects {"content":"...","stop":false} chunks followed by a final
// stop=true chunk that includes timings so it can display generation stats.
static std::string build_completion_buffered_stream_response(const std::string &completion_body) {
    json completion = json::parse(completion_body, nullptr, false);
    if (completion.is_discarded() || !completion.is_object()) {
        return "data: [DONE]\n\n";
    }

    const std::string assistant_content = extract_chat_text(completion);
    std::ostringstream oss;

    if (!assistant_content.empty()) {
        oss << "data: " << json({{"content", assistant_content}, {"stop", false}}).dump() << "\n\n";
    }

    // Final event — includes timings and token counts so the UI can render stats
    json final_event = {{"content", ""}, {"stop", true}};

    if (completion.contains("timings"))
        final_event["timings"] = completion["timings"];

    if (completion.contains("usage") && completion["usage"].is_object()) {
        const auto &usage = completion["usage"];
        if (usage.contains("completion_tokens")) final_event["tokens_predicted"] = usage["completion_tokens"];
        if (usage.contains("prompt_tokens"))     final_event["tokens_evaluated"] = usage["prompt_tokens"];
    }
    if (completion.contains("tokens_predicted")) final_event["tokens_predicted"] = completion["tokens_predicted"];
    if (completion.contains("tokens_evaluated")) final_event["tokens_evaluated"] = completion["tokens_evaluated"];

    if (completion.contains("choices") && completion["choices"].is_array() && !completion["choices"].empty()) {
        const auto &choice = completion["choices"][0];
        if (choice.contains("finish_reason") && choice["finish_reason"].is_string()) {
            const std::string reason = choice["finish_reason"].get<std::string>();
            if (reason == "stop")   final_event["stop_type"] = "eos";
            else if (reason == "length") final_event["stop_type"] = "limit";
        }
    }

    oss << "data: " << final_event.dump() << "\n\n";
    return oss.str();
}

static std::string build_buffered_stream_response(const std::string &completion_body) {
    json completion = json::parse(completion_body, nullptr, false);
    if (completion.is_discarded() || !completion.is_object()) {
        return "data: [DONE]\n\n";
    }

    const std::string id = completion.value("id", "chatcmpl-buffered");
    const std::string model = completion.value("model", "");
    const auto created = completion.value("created", 0);

    const std::string assistant_content = extract_chat_text(completion);

    std::ostringstream oss;
    oss << build_sse_event({
        {"id", id},
        {"object", "chat.completion.chunk"},
        {"created", created},
        {"model", model},
        {"choices", json::array({
            {
                {"index", 0},
                {"delta", {{"role", "assistant"}}},
                {"finish_reason", nullptr}
            }
        })}
    });

    if (!assistant_content.empty()) {
        oss << build_sse_event({
            {"id", id},
            {"object", "chat.completion.chunk"},
            {"created", created},
            {"model", model},
            {"choices", json::array({
                {
                    {"index", 0},
                    {"delta", {{"content", assistant_content}}},
                    {"finish_reason", nullptr}
                }
            })}
        });
    }

    // Final chunk: include usage and timings so the web UI can display generation stats
    json final_chunk = {
        {"id", id},
        {"object", "chat.completion.chunk"},
        {"created", created},
        {"model", model},
        {"choices", json::array({
            {
                {"index", 0},
                {"delta", json::object()},
                {"finish_reason", "stop"}
            }
        })}
    };
    if (completion.contains("usage"))   final_chunk["usage"]   = completion["usage"];
    if (completion.contains("timings")) final_chunk["timings"] = completion["timings"];
    oss << build_sse_event(final_chunk);
    oss << "data: [DONE]\n\n";
    return oss.str();
}

http::response<http::string_body> handle_request(
    ModelManager &manager,
    const ManagerConfig &config,
    const ApiKeyAuth &auth,
    RateLimiterStore &rate_limiter,
    RequestRegistry &registry,
    MetricsRegistry &metrics,
    Scheduler &scheduler,
    http::request<http::string_body> &&req) {
    const auto start = std::chrono::steady_clock::now();
    const auto req_id_num = g_req_id.fetch_add(1);
    const std::string request_id = std::to_string(req_id_num);
    const std::string target = req.target().to_string();
    const std::string method = req.method_string().to_string();
    const std::string path = target.substr(0, target.find('?'));
    auto authenticated = std::optional<ApiKeyRecord>{};

    metrics.inc_requests_total();
    metrics.inc_requests_inflight();
    struct InflightGuard {
        MetricsRegistry &metrics;
        ~InflightGuard() { metrics.dec_requests_inflight(); }
    } inflight_guard{metrics};

    log_line("request_id=" + request_id + " method=" + method + " path=" + target);
    if (!req.body().empty()) {
        log_line("request_id=" + request_id + " body=" + truncate_body(req.body()));
    }

    auto json_response = [&](http::status status, const json &obj) {
        json payload = obj;
        payload["request_id"] = request_id;
        // llama.cpp web UI expects {"error":{"code":N,"message":"..."}} for both
        // /v1/chat/completions and /completion. Our generic errors use {"error":"string"}
        // — rewrap so the popup shows the text.
        if ((path == "/v1/chat/completions" || path == "/completion") &&
            payload.contains("error") && payload["error"].is_string()) {
            payload["error"] = {
                {"code",    static_cast<int>(status)},
                {"message", payload["error"].get<std::string>()},
                {"type",    "server_error"}
            };
        }
        http::response<http::string_body> res{status, req.version()};
        res.set(http::field::content_type, "application/json");
        res.set(http::field::server, "llm-manager");
        res.set("X-Request-Id", request_id);
        res.keep_alive(req.keep_alive());
        res.body() = payload.dump();
        res.prepare_payload();
        auto elapsed_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
            std::chrono::steady_clock::now() - start).count();
        metrics.observe_request_latency_ms(elapsed_ms);
        std::string log_message = "request_id=" + request_id +
                                  " status=" + std::to_string(res.result_int()) +
                                  " elapsed_ms=" + std::to_string(elapsed_ms);
        if (res.result_int() >= 400) {
            log_message += " error_body=" + truncate_body(res.body());
        }
        log_line(log_message);
        return res;
    };

    auto json_response_with_retry_after = [&](http::status status, const json &obj, int retry_after_sec) {
        auto res = json_response(status, obj);
        res.set(http::field::retry_after, std::to_string(std::max(1, retry_after_sec)));
        return res;
    };

    auto ensure_authenticated = [&](Role minimum_role) -> std::optional<http::response<http::string_body>> {
        std::string auth_error;
        authenticated = auth.authenticate(req, auth_error);
        if (!authenticated) {
            return json_response(http::status::unauthorized, {{"error", auth_error}});
        }
        if (minimum_role == Role::ADMIN && authenticated->role != Role::ADMIN) {
            return json_response(http::status::forbidden, {{"error", "Admin role required"}});
        }
        log_line("request_id=" + request_id +
                 " api_key_id=" + authenticated->key_id +
                 " role=" + role_to_string(authenticated->role));
        return std::nullopt;
    };

    try {
        if (path == "/health" && req.method() == http::verb::get) {
            return json_response(http::status::ok, manager.models_view());
        }

        if (path == "/models" && req.method() == http::verb::get) {
            return json_response(http::status::ok, manager.models_view());
        }

        if (path == "/queue/metrics" && req.method() == http::verb::get) {
            http::response<http::string_body> res{http::status::ok, req.version()};
            res.set(http::field::content_type, "text/plain; version=0.0.4; charset=utf-8");
            res.set(http::field::server, "llm-manager");
            res.set("X-Request-Id", request_id);
            res.keep_alive(req.keep_alive());
            res.body() = metrics.render_prometheus(scheduler.snapshot(), manager);
            res.prepare_payload();
            auto elapsed_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
                std::chrono::steady_clock::now() - start).count();
            metrics.observe_request_latency_ms(elapsed_ms);
            log_line("request_id=" + request_id + " status=" + std::to_string(res.result_int()) +
                     " elapsed_ms=" + std::to_string(elapsed_ms));
            return res;
        }

        if (path == "/switch-model" && req.method() == http::verb::post) {
            if (auto auth_res = ensure_authenticated(Role::ADMIN)) return *auth_res;
            json j = json::parse(req.body(), nullptr, false);
            if (j.is_discarded()) {
                return json_response(http::status::bad_request, {{"error", "Invalid JSON"}});
            }
            std::string model;
            if (j.contains("model_name")) model = j["model_name"].get<std::string>();
            if (j.contains("model")) model = j["model"].get<std::string>();
            if (model.empty()) {
                return json_response(http::status::bad_request, {{"error", "Expected 'model' or 'model_name'"}});
            }

            std::string err;
            bool ok = manager.switch_model(model, err);
            if (!ok) {
                auto status = (err == "Switch already in progress")
                    ? http::status::conflict
                    : http::status::internal_server_error;
                return json_response(status, {{"status", "error"}, {"error", err}});
            }
            auto state = manager.models_view();
            state["message"] = "Switched model successfully";
            metrics.inc_switch_total();
            return json_response(http::status::ok, state);
        }

        if (path == "/stop" && req.method() == http::verb::post) {
            if (auto auth_res = ensure_authenticated(Role::ADMIN)) return *auth_res;
            const auto cancelled = registry.cancel_all();
            metrics.add_cancellations_total(cancelled.size());
            std::string err;
            bool ok = manager.restart_active(err);
            if (!ok) {
                http::status status = http::status::internal_server_error;
                if (err == "Switch already in progress") status = http::status::conflict;
                else if (err == "No active model") status = http::status::service_unavailable;
                return json_response(status, {{"status", "error"}, {"error", err}});
            }
            auto state = manager.models_view();
            state["message"] = "Stopped in-flight prompts and restarted model";
            metrics.inc_worker_restarts_total();
            return json_response(http::status::ok, state);
        }

        if (req.method() == http::verb::post) {
            if (auto cancel_id = extract_cancel_request_id(path)) {
                if (auto auth_res = ensure_authenticated(Role::USER)) return *auth_res;
                auto ctx = registry.find(*cancel_id);
                if (!ctx) {
                    return json_response(http::status::not_found, {{"error", "Unknown request id"}});
                }
                if (authenticated->role != Role::ADMIN && authenticated->key_id != ctx->api_key_id) {
                    return json_response(http::status::forbidden, {{"error", "Cannot cancel another API key request"}});
                }

                const auto previous_state = ctx->state.load();
                registry.cancel_request(*cancel_id);
                metrics.add_cancellations_total();
                std::string restart_error;
                bool restarted = true;
                if (previous_state == RequestState::RUNNING) {
                    restarted = manager.restart_active(restart_error);
                    if (restarted) metrics.inc_worker_restarts_total();
                }

                json payload = {
                    {"cancelled_request_id", *cancel_id},
                    {"state", state_to_string(ctx->state.load())}
                };
                if (!restarted) payload["restart_error"] = restart_error;
                return json_response(http::status::ok, payload);
            }
        }

        if ((path == "/v1/chat/completions" || path == "/completion") && req.method() == http::verb::post) {
            if (auto auth_res = ensure_authenticated(Role::USER)) return *auth_res;
            json payload = json::parse(req.body(), nullptr, false);
            if (payload.is_discarded()) {
                return json_response(http::status::bad_request, {{"error", "Invalid JSON"}});
            }
            const bool completion_compat_mode = path == "/completion";
            if (completion_compat_mode) {
                payload = completion_payload_to_chat_payload(payload, config.limits);
            }
            const bool stream_requested = request_stream_enabled(payload);
            if (stream_requested) {
                payload["stream"] = false;
                log_line("request_id=" + request_id +
                         " stream_requested=true mode=buffered_sse_fallback");
            }
            if (completion_compat_mode) {
                log_line("request_id=" + request_id + " completion_compat_mode=true");
            }

            std::string token_error;
            auto estimate = estimate_chat_tokens(payload, config.limits, token_error);
            if (!estimate) {
                return json_response(http::status::bad_request, {{"error", token_error}});
            }
            log_line("request_id=" + request_id +
                     " prompt_tokens=" + std::to_string(estimate->prompt_tokens) +
                     " max_tokens=" + std::to_string(estimate->requested_max_tokens) +
                     " estimated_total_tokens=" + std::to_string(estimate->estimated_total_tokens));

            auto rate_limit_decision = rate_limiter.allow(authenticated->key_id, estimate->estimated_total_tokens);
            if (!rate_limit_decision.allowed) {
                metrics.inc_rate_limited_total();
                return json_response_with_retry_after(
                    http::status::too_many_requests,
                    {{"error", rate_limit_decision.error}},
                    rate_limit_decision.retry_after_sec);
            }

            const std::string upstream_request_body = payload.dump();
            auto ctx = registry.create(request_id, *authenticated, *estimate, upstream_request_body);
            if (!scheduler.try_enqueue(ctx)) {
                ctx->cancelled.store(true);
                registry.complete(ctx, RequestState::CANCELLED, {503, R"({"error":"Queue full"})"});
                metrics.inc_queue_rejected_total();
                return json_response_with_retry_after(
                    http::status::service_unavailable,
                    {{"error", "Queue full"}},
                    scheduler.retry_after_sec());
            }

            std::unique_lock<std::mutex> lock(ctx->mu);
            const bool finished = ctx->cv.wait_for(
                lock,
                std::chrono::seconds(std::max(1, config.limits.request_timeout_sec)),
                [&]() { return ctx->completed; });
            if (!finished) {
                lock.unlock();
                registry.cancel_request(request_id);
                metrics.add_cancellations_total();
                std::string restart_error;
                bool restarted = true;
                if (ctx->state.load() == RequestState::RUNNING) {
                    restarted = manager.restart_active(restart_error);
                    if (restarted) metrics.inc_worker_restarts_total();
                }
                json timeout_payload = {
                    {"error", "Request timed out"},
                    {"state", state_to_string(ctx->state.load())}
                };
                if (!restarted) timeout_payload["restart_error"] = restart_error;
                return json_response(http::status::gateway_timeout, timeout_payload);
            }

            const auto final_state = ctx->state.load();
            RequestResult result = ctx->result;
            lock.unlock();

            if (final_state == RequestState::CANCELLED) {
                return json_response(http::status::ok, {{"status", "cancelled"}});
            }

            http::response<http::string_body> res{
                static_cast<http::status>(result.status), req.version()};
            if (stream_requested && completion_compat_mode && result.status >= 200 && result.status < 300) {
                result.body = build_completion_buffered_stream_response(result.body);
                result.content_type = "text/event-stream; charset=utf-8";
            } else if (stream_requested && result.status >= 200 && result.status < 300) {
                result.body = build_buffered_stream_response(result.body);
                result.content_type = "text/event-stream; charset=utf-8";
            } else if (completion_compat_mode && result.status >= 200 && result.status < 300) {
                result.body = build_completion_compat_response(result.body);
                result.content_type = "application/json";
            }
            res.set(http::field::content_type, result.content_type);
            res.set(http::field::server, "llm-manager");
            res.set("X-Request-Id", request_id);
            res.keep_alive(req.keep_alive());
            res.body() = result.body;
            res.prepare_payload();
            auto elapsed_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
                std::chrono::steady_clock::now() - start).count();
            metrics.observe_request_latency_ms(elapsed_ms);
            log_line("request_id=" + request_id +
                     " final_state=" + state_to_string(final_state) +
                     " upstream_status=" + std::to_string(result.status) +
                     " elapsed_ms=" + std::to_string(elapsed_ms));
            if (result.status >= 400) {
                log_line("request_id=" + request_id +
                         " upstream_error_body=" + truncate_body(result.body));
            }
            return res;
        }

        if (req.method() == http::verb::get) {
            auto worker = manager.active_worker();
            if (!worker) {
                return json_response(http::status::service_unavailable, {{"error", "No active model"}});
            }
            auto upstream = forward_get_to_worker(*worker, target);
            http::response<http::string_body> res{
                static_cast<http::status>(upstream.status), req.version()};
            res.set(http::field::content_type, upstream.content_type);
            if (!upstream.content_encoding.empty()) {
                res.set(http::field::content_encoding, upstream.content_encoding);
            }
            res.set(http::field::server, "llm-manager");
            res.set("X-Request-Id", request_id);
            res.keep_alive(req.keep_alive());
            res.body() = upstream.body;
            res.prepare_payload();
            auto elapsed_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
                std::chrono::steady_clock::now() - start).count();
            log_line("request_id=" + request_id +
                     " proxied_get model=" + worker->model +
                     " upstream_status=" + std::to_string(upstream.status) +
                     " elapsed_ms=" + std::to_string(elapsed_ms));
            if (upstream.status >= 400) {
                log_line("request_id=" + request_id +
                         " proxied_get_error_body=" + truncate_body(upstream.body));
            }
            return res;
        }

        return json_response(http::status::not_found, {{"error", "Not found"}});
    } catch (const std::exception &e) {
        log_line("request_id=" + request_id + " exception=" + std::string(e.what()));
        return json_response(http::status::internal_server_error, {{"error", e.what()}});
    } catch (...) {
        log_line("request_id=" + request_id + " exception=unknown");
        return json_response(http::status::internal_server_error, {{"error", "Unknown exception"}});
    }
}

void do_session(
    boost::asio::ip::tcp::socket socket,
    ModelManager &manager,
    const ManagerConfig &config,
    const ApiKeyAuth &auth,
    RateLimiterStore &rate_limiter,
    RequestRegistry &registry,
    MetricsRegistry &metrics,
    Scheduler &scheduler) {
    try {
        beast::flat_buffer buffer;
        http::request<http::string_body> req;
        http::read(socket, buffer, req);
        auto res = handle_request(manager, config, auth, rate_limiter, registry, metrics, scheduler, std::move(req));
        http::write(socket, res);
        beast::error_code ec;
        socket.shutdown(boost::asio::ip::tcp::socket::shutdown_send, ec);
    } catch (const std::exception &e) {
        log_line("session_exception=" + std::string(e.what()));
    } catch (...) {
        log_line("session_exception=unknown");
    }
}