char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Pascalserver + ui: fix stream routes for model names containing a slash (#26137)d73c1d6b2

char/aliceai-t5-moe
3.0 KiB79 linesraw
1#pragma once
2
3#include "server-http.h"
4
5#include <atomic>
6#include <cstddef>
7#include <functional>
8#include <memory>
9#include <string>
10
11// streaming buffer for one generation, survives HTTP disconnect. the producer appends SSE bytes,
12// readers drain from any offset via read_from. keyed by conversation_id, one conv = one live session
13
14struct stream_session;
15
16using stream_session_ptr = std::shared_ptr<stream_session>;
17
18// base of the producer/consumer pipe ends. virtual dtor so each runs its own teardown:
19// the producer finalizes the session, the consumer leaves it untouched
20struct stream_pipe {
21    virtual ~stream_pipe() = default;
22
23    bool is_cancelled() const;
24
25protected:
26    explicit stream_pipe(stream_session_ptr session);
27
28    stream_session_ptr session_;
29};
30
31// producer end: writes chunks into the ring buffer and owns the session lifetime, finalizing it
32// on destruction.
33struct stream_pipe_producer : stream_pipe {
34    ~stream_pipe_producer() override;
35
36    bool write(const char * data, size_t len);
37
38    static stream_pipe_producer * create(stream_session_ptr session);
39
40private:
41    explicit stream_pipe_producer(stream_session_ptr session);
42};
43
44void server_stream_session_manager_start();
45void server_stream_session_manager_stop();
46
47// route handler factories wired under /v1/stream/* by server.cpp
48// child-side handlers for the resumable stream routes. the conv id travels in the conv_id
49// query string because it can embed a model name containing slashes (org/repo), which the
50// decoded path would split before the param is captured
51server_http_context::handler_t server_stream_make_get_handler();
52// POST /v1/streams/lookup with body {"conversation_ids": [...]}: only answers for ids the
53// caller already owns (the WebUI passes the convs visible in its sidebar), the server never
54// lists ids it has not been asked about, so a random caller cannot enumerate live sessions
55server_http_context::handler_t server_stream_make_lookup_handler();
56server_http_context::handler_t server_stream_make_delete_handler();
57
58// extract the X-Conversation-Id header value (case-insensitive), empty when absent
59std::string server_stream_conv_id_from_headers(const std::map<std::string, std::string> & headers);
60
61// implement tee-style pipe (spipe) for "stream replay" functionality
62struct server_res_spipe : server_http_res {
63private:
64    // if set, the stream survives a client disconnect:
65    // connection kept alive, output is forwarded to spipe and reuse later
66    std::unique_ptr<stream_pipe_producer> spipe;
67    // if spipe is set, use this next_orig to implement tee-style pipe
68    std::function<bool(std::string &)> next_orig;
69    const server_http_req * req = nullptr;
70    // set once next_orig reports no more data, so on_complete() doesn't re-drain a finished stream
71    bool next_finished = false;
72
73public:
74    void set_req(const server_http_req * req);
75    bool conn_alive();
76    bool should_stop();
77    void on_complete() override;
78    void set_next(std::function<bool(std::string &)> next_fn);
79};