diff --git a/tools/server/server-connect.cpp b/tools/server/server-connect.cpp index e18bd9c4ec..340395d181 100644 --- a/tools/server/server-connect.cpp +++ b/tools/server/server-connect.cpp @@ -56,7 +56,7 @@ static std::string format_share_code(const std::string & code) { } // whitespace is tolerated, the Web UI shows the code in blocks and users paste it back -// returns an empty string if the key is not a valid share code +// 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) { @@ -161,6 +161,10 @@ std::string server_connect::unavailable_reason(const common_params & params) { 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) { const std::string bin = find_binary(); if (bin.empty()) { @@ -168,10 +172,9 @@ bool server_connect::start(const common_params & params) { return false; } - // already validated by unavailable_reason() - const std::string code = params.server_connect_code.empty() - ? gen_share_code() - : normalize_share_code(params.server_connect_code); + // main() resolves this before anything can read it from /props + const std::string & code = params.server_connect_code; + GGML_ASSERT(!code.empty()); // always loopback, params.hostname may be 0.0.0.0 or a unix socket which the child cannot dial const std::vector args = { @@ -179,8 +182,8 @@ bool server_connect::start(const common_params & params) { "--host", "127.0.0.1", "--port", std::to_string(params.port), "--code", code, - // the kernel closes our end of this pipe even if we are killed without cleanup, - // so the child cannot outlive us + // 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", }; diff --git a/tools/server/server-connect.h b/tools/server/server-connect.h index 976872baab..49e96e2131 100644 --- a/tools/server/server-connect.h +++ b/tools/server/server-connect.h @@ -24,6 +24,8 @@ struct server_connect { // why --connect cannot work here, empty if it can static std::string unavailable_reason(const common_params & params); + static std::string resolve_code(const std::string & code); + bool start(const common_params & params); // idempotent, also called by the destructor diff --git a/tools/server/server-context.cpp b/tools/server/server-context.cpp index f78cfb36dd..0b6a41e385 100644 --- a/tools/server/server-context.cpp +++ b/tools/server/server-context.cpp @@ -4618,6 +4618,10 @@ static json get_res_props(const server_context_meta & meta, const common_params { "is_sleeping", is_sleeping }, { "cors_proxy_enabled", params.ui_mcp_proxy }, }; + + if (!params.server_connect_code.empty()) { + props["connect_code"] = params.server_connect_code; + } if (params.use_jinja) { if (!tmpl_tools.empty()) { props["chat_template_tool_use"] = tmpl_tools; diff --git a/tools/server/server-models.cpp b/tools/server/server-models.cpp index 5858f8f12d..9af7a160dd 100644 --- a/tools/server/server-models.cpp +++ b/tools/server/server-models.cpp @@ -1829,7 +1829,7 @@ void server_models_routes::init_routes() { if (name.empty()) { // main instance auto res = std::make_unique(); - res_ok(res, { + json props = { // TODO: add support for this on web UI {"role", "router"}, {"max_instances", params.models_max}, @@ -1845,7 +1845,11 @@ void server_models_routes::init_routes() { {"ui_settings", ui_settings}, {"build_info", std::string(llama_build_info())}, {"cors_proxy_enabled", params.ui_mcp_proxy}, - }); + }; + if (!params.server_connect_code.empty()) { + props["connect_code"] = params.server_connect_code; + } + res_ok(res, props); return res; } return proxy_get(req); diff --git a/tools/server/server.cpp b/tools/server/server.cpp index 612622c7b8..504e840add 100644 --- a/tools/server/server.cpp +++ b/tools/server/server.cpp @@ -183,6 +183,7 @@ int llama_server(common_params & params, int argc, char ** argv) { SRV_ERR("--connect is not available: %s\n", reason.c_str()); return 1; } + params.server_connect_code = server_connect::resolve_code(params.server_connect_code); } // note: this is guaranteed to out-live ctx_http and tools