File size: 9,963 Bytes
89565fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#include "server-connect.h"

#include "server-common.h"
#include "subproc.h"

#include <cctype>
#include <chrono>
#include <exception>
#include <filesystem>
#include <random>
#include <string_view>
#include <system_error>
#include <thread>
#include <vector>

// share code = room code + pass code, must match llama-connect and the Web UI
// ref: https://github.com/ggml-org/llama-connect/blob/master/src/protocol.rs
static constexpr size_t CONNECT_ROOM_CODE_LEN = 8;
static constexpr size_t CONNECT_PASS_CODE_LEN = 32;

static const std::string CONNECT_CODE_CHARS = "ABCDEFGHJKMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz23456789";

#if defined(_WIN32)
static const std::string CONNECT_EXE_NAME = "llama-connect.exe";
static constexpr char    PATH_SEPARATOR   = ';';
#else
static const std::string CONNECT_EXE_NAME = "llama-connect";
static constexpr char    PATH_SEPARATOR   = ':';
#endif

// how long to wait for the child to notice the closed stdin before killing it
static constexpr int CONNECT_STOP_TIMEOUT_MS = 3000;

// the pass code guards the tunnel, so do not use random_string(): its mt19937 is predictable
static std::string gen_share_code() {
    std::random_device rd;
    std::uniform_int_distribution<size_t> dist(0, CONNECT_CODE_CHARS.size() - 1);

    std::string code(CONNECT_ROOM_CODE_LEN + CONNECT_PASS_CODE_LEN, ' ');
    for (char & c : code) {
        c = CONNECT_CODE_CHARS[dist(rd)];
    }

    return code;
}

static std::string format_share_code(const std::string & code) {
    std::string out;
    for (size_t i = 0; i < code.size(); i += 8) {
        if (i > 0) {
            out += ' ';
        }
        out += code.substr(i, 8);
    }
    return out;
}

// whitespace is tolerated, the Web UI shows the code in blocks and users paste it back
// returns an empty string if it is not a valid share code
static std::string normalize_share_code(const std::string & key) {
    std::string code;
    for (char c : key) {
        if (!std::isspace((unsigned char) c)) {
            code += c;
        }
    }

    if (code.size() != CONNECT_ROOM_CODE_LEN + CONNECT_PASS_CODE_LEN) {
        return "";
    }

    if (code.find_first_not_of(CONNECT_CODE_CHARS) != std::string::npos) {
        return "";
    }

    return code;
}

// llama-connect logs as "[LEVEL] text", forward at the same level so our verbosity filter applies
static void forward_child_log(const std::string & line) {
    static const std::pair<std::string_view, ggml_log_level> tags[] = {
        { "[ERROR] ", GGML_LOG_LEVEL_ERROR },
        { "[WARN] ",  GGML_LOG_LEVEL_WARN  },
        { "[INFO] ",  GGML_LOG_LEVEL_INFO  },
        { "[DEBUG] ", GGML_LOG_LEVEL_DEBUG },
        { "[TRACE] ", GGML_LOG_LEVEL_DEBUG },
    };

    // untagged lines are the startup banner, show them as info
    ggml_log_level level = GGML_LOG_LEVEL_INFO;
    const char * text = line.c_str();

    for (const auto & [tag, tag_level] : tags) {
        if (string_starts_with(line, tag)) {
            level = tag_level;
            text += tag.size();
            break;
        }
    }

    switch (level) {
        case GGML_LOG_LEVEL_ERROR: LOG_ERR("connect | %s", text); break;
        case GGML_LOG_LEVEL_WARN:  LOG_WRN("connect | %s", text); break;
        case GGML_LOG_LEVEL_DEBUG: LOG_DBG("connect | %s", text); break;
        default:                   LOG_INF("connect | %s", text); break;
    }
}

// the child dials over TCP: a wildcard bind is reached on loopback, a specific address on itself
// empty when every listener is a unix socket
static std::string upstream_host(const std::vector<std::string> & hosts) {
    for (const auto & host : hosts) {
        if (string_ends_with(host, ".sock")) {
            continue;
        }
        if (host == "0.0.0.0") {
            return "127.0.0.1";
        }
        if (host == "::") {
            return "[::1]";
        }
        // llama-connect builds http://host:port, so an IPv6 literal needs brackets
        if (host.find(':') != std::string::npos && host[0] != '[') {
            return "[" + host + "]";
        }
        return host;
    }
    return "";
}

static bool path_is_file(const std::filesystem::path & p) {
    std::error_code ec;
    return std::filesystem::is_regular_file(p, ec);
}

std::string server_connect::find_binary() {
    // prefer the copy shipped next to llama-server over an unrelated one in PATH
    try {
        auto sibling = get_server_exec_path().parent_path() / CONNECT_EXE_NAME;
        if (path_is_file(sibling)) {
            return sibling.string();
        }
    } catch (const std::exception & e) {
        SRV_WRN("could not resolve the llama-server path (%s), looking for llama-connect in PATH only\n", e.what());
    }

    const std::string path_env = common_get_env("PATH");
    size_t start = 0;
    while (start <= path_env.size()) {
        size_t end = path_env.find(PATH_SEPARATOR, start);
        if (end == std::string::npos) {
            end = path_env.size();
        }
        const std::string dir = path_env.substr(start, end - start);
        if (!dir.empty()) {
            auto candidate = std::filesystem::path(dir) / CONNECT_EXE_NAME;
            if (path_is_file(candidate)) {
                return candidate.string();
            }
        }
        start = end + 1;
    }

    return "";
}

std::string server_connect::unavailable_reason(const common_params & params) {
    if (!common_subproc::is_supported()) {
        return "this build has subprocess support disabled, rebuild with -DLLAMA_SUBPROCESS=ON";
    }

    if (!params.server_connect_code.empty() && normalize_share_code(params.server_connect_code).empty()) {
        return "--connect-code must be " + std::to_string(CONNECT_ROOM_CODE_LEN + CONNECT_PASS_CODE_LEN)
             + " characters from '" + CONNECT_CODE_CHARS + "'";
    }

    if (upstream_host(params.hostnames).empty()) {
        return "llama-connect reaches the server over TCP, add a TCP address to --host";
    }

    if (find_binary().empty()) {
        return "could not find '" + CONNECT_EXE_NAME + "' next to llama-server or in PATH.\n"
               "    it is a separate binary: download it from https://github.com/ggml-org/llama-connect/releases\n"
               "    or build llama.cpp with -DLLAMA_CONNECT=ON to have it fetched automatically";
    }

    return "";
}

std::string server_connect::resolve_code(const std::string & code) {
    return code.empty() ? gen_share_code() : normalize_share_code(code);
}

bool server_connect::start(const common_params & params, int port) {
    const std::string bin = find_binary();
    if (bin.empty()) {
        SRV_ERR("%s", "llama-connect binary not found\n");
        return false;
    }

    // main() resolves this before anything can read it from /props
    const std::string & code = params.server_connect_code;
    GGML_ASSERT(!code.empty());

    const std::vector<std::string> args = {
        bin,
        "--host", upstream_host(params.hostnames),
        "--port", std::to_string(port),
        "--code", code,
        // the kernel closes our end of this pipe even if we are killed without cleanup
        // this is what stops the child from outliving us
        "--exit-on-stdin-eof",
    };

    proc = std::make_unique<common_subproc>();

    const int options = subprocess_option_no_window
                      | subprocess_option_combined_stdout_stderr
                      | subprocess_option_inherit_environment;

    if (!proc->create(args, options)) {
        SRV_ERR("failed to spawn '%s'\n", bin.c_str());
        proc.reset();
        return false;
    }

    log_thread = std::thread([this]() {
        FILE * out = proc->stdout_file();
        if (out == nullptr) {
            SRV_ERR("%s", "failed to get stdout of the llama-connect process\n");
            return;
        }
        std::vector<char> buf(4096);
        while (fgets(buf.data(), (int) buf.size(), out) != nullptr) {
            forward_child_log(buf.data());
        }
        // EOF means the child is gone
        if (!stopping.load(std::memory_order_acquire)) {
            SRV_ERR("%s", "llama-connect exited on its own, remote access is no longer available\n");
        }
    });

    SRV_INF("%s", "-----------------\n");
    SRV_INF("%s", "remote access is enabled via llama-connect\n");
    SRV_INF("share code (enter it in the Web UI under Settings -> Remote Access): %s\n",
            format_share_code(code).c_str());
    SRV_WRN("%s", "anyone with this code can use this server, do not share it publicly\n");
    SRV_INF("%s", "-----------------\n");

    return true;
}

void server_connect::stop() {
    if (!proc) {
        return;
    }

    SRV_INF("%s", "stopping llama-connect...\n");

    stopping.store(true, std::memory_order_release);

    proc->close_stdin();

    for (int elapsed = 0; elapsed < CONNECT_STOP_TIMEOUT_MS && proc->alive(); elapsed += 100) {
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }

    // no-op if the child already exited. the log thread leaves fgets() on EOF, which the kernel
    // raises once every writer of the stdout pipe is gone, so llama-connect must not spawn
    // children of its own: one of them holding that pipe blocks the join below forever
    proc->terminate();

    if (log_thread.joinable()) {
        try {
            log_thread.join();
        } catch (const std::system_error & e) {
            // ~thread() on a still-joinable thread calls std::terminate, detach instead
            SRV_ERR("failed to join the llama-connect log thread: %s\n", e.what());
            log_thread.detach();
        }
    }

    proc->join(); // reap the zombie
    proc.reset();
}

server_connect::server_connect() = default;

server_connect::~server_connect() {
    try {
        stop();
    } catch (const std::exception & e) {
        SRV_ERR("failed to stop llama-connect: %s\n", e.what());
    } catch (...) {
        SRV_ERR("%s", "failed to stop llama-connect\n");
    }
}