forked from janhq/cortex.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_start_cmd.cc
More file actions
307 lines (290 loc) · 10.8 KB
/
server_start_cmd.cc
File metadata and controls
307 lines (290 loc) · 10.8 KB
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
#include "server_start_cmd.h"
#include "commands/cortex_upd_cmd.h"
#include "services/engine_service.h"
#include "utils/cortex_utils.h"
#include "utils/file_manager_utils.h"
#include "utils/process/utils.h"
#if defined(_WIN32) || defined(_WIN64)
#include "utils/widechar_conv.h"
#endif
namespace commands {
namespace {
bool TryConnectToServer(const std::string& host, int port) {
constexpr const auto kMaxRetry = 4u;
auto count = 0u;
// Check if server is started
while (true) {
if (IsServerAlive(host, port))
break;
// Wait for server up
std::this_thread::sleep_for(std::chrono::seconds(1));
if (count++ == kMaxRetry) {
std::cerr << "Could not start server" << std::endl;
return false;
}
}
return true;
}
} // namespace
bool ServerStartCmd::Exec(const std::string& host, int port,
const std::optional<std::string>& log_level) {
if (IsServerAlive(host, port)) {
CLI_LOG("The server has already started");
return true;
}
std::string log_level_;
if (!log_level.has_value()) {
log_level_ = "INFO";
} else {
log_level_ = log_level.value();
}
auto exe = commands::GetCortexServerBinary();
auto get_config_file_path = []() -> std::string {
if (file_manager_utils::cortex_config_file_path.empty()) {
return file_manager_utils::GetConfigurationPath().string();
}
return file_manager_utils::cortex_config_file_path;
};
auto get_data_folder_path = []() -> std::string {
if (file_manager_utils::cortex_data_folder_path.empty()) {
return file_manager_utils::GetCortexDataPath().string();
}
return file_manager_utils::cortex_data_folder_path;
};
#if defined(_WIN32) || defined(_WIN64)
// Windows-specific code to create a new process
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
std::wstring params = L"--start-server";
params += L" --config_file_path \"" +
file_manager_utils::GetConfigurationPath().wstring() + L"\"";
params += L" --data_folder_path \"" +
file_manager_utils::GetCortexDataPath().wstring() + L"\"";
params += L" --loglevel " + cortex::wc::Utf8ToWstring(log_level_);
std::wstring exe_w = cortex::wc::Utf8ToWstring(exe);
std::wstring current_path_w =
file_manager_utils::GetExecutableFolderContainerPath().wstring();
std::wstring wcmds = current_path_w + L"\\" + exe_w + L" " + params;
CTL_INF("wcmds: " << wcmds);
std::vector<wchar_t> mutable_cmds(wcmds.begin(), wcmds.end());
mutable_cmds.push_back(L'\0');
// Create child process
if (!CreateProcess(
NULL, // No module name (use command line)
mutable_cmds
.data(), // Command line (replace with your actual executable)
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance
CREATE_NO_WINDOW, // No new console
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi)) // Pointer to PROCESS_INFORMATION structure
{
std::cout << "Could not start server: " << GetLastError() << std::endl;
return false;
} else {
if (!TryConnectToServer(host, port)) {
return false;
}
std::cout << "Server started" << std::endl;
std::cout << "API Documentation available at: http://" << host << ":"
<< port << std::endl;
}
#else
std::vector<std::string> commands;
// Some engines requires to add lib search path before process being created
EngineService(std::make_shared<cortex::DylibPathManager>())
.RegisterEngineLibPath();
std::string p = cortex_utils::GetCurrentPath() + "/" + exe;
commands.push_back(p);
commands.push_back("--config_file_path");
commands.push_back(get_config_file_path());
commands.push_back("--data_folder_path");
commands.push_back(get_data_folder_path());
commands.push_back("--loglevel");
commands.push_back(log_level_);
auto result = cortex::process::SpawnProcess(commands);
if (result.has_error()) {
// Fork failed
std::cerr << "Could not start server: " << result.error() << std::endl;
return false;
} else {
// Parent process
if (!TryConnectToServer(host, port)) {
return false;
}
std::cout << "Server started" << std::endl;
std::cout << "API Documentation available at: http://" << host << ":"
<< port << std::endl;
}
#endif
return true;
}
bool ServerStartCmd::Exec(
const std::optional<std::string>& log_level,
const std::unordered_map<std::string, std::string>& options,
CortexConfig& data) {
for (const auto& [key, value] : options) {
if (!value.empty()) {
UpdateConfig(data, key, value);
}
}
auto config_path = file_manager_utils::GetConfigurationPath();
auto result =
config_yaml_utils::CortexConfigMgr::GetInstance().DumpYamlConfig(
data, config_path.string());
if (result.has_error()) {
CTL_WRN("Error update " << config_path.string() << result.error());
}
return Exec(data.apiServerHost, std::stoi(data.apiServerPort), log_level);
}
void ServerStartCmd::UpdateConfig(CortexConfig& data, const std::string& key,
const std::string& value) {
static const std::unordered_map<
std::string, std::function<void(CortexConfig&, const std::string&,
const std::string&)>>
updaters = {
{"logspath",
[](CortexConfig& data, const std::string&, const std::string& v) {
data.logFolderPath = v;
}},
{"logsllama",
[](CortexConfig& data, const std::string&, const std::string& v) {
data.logLlamaCppPath = v;
}},
{"logsonnx",
[](CortexConfig& data, const std::string&, const std::string& v) {
data.logOnnxPath = v;
}},
{"loglines",
[this](CortexConfig& data, const std::string& k,
const std::string& v) {
UpdateNumericField(k, v, [&data](float f) {
data.maxLogLines = static_cast<int>(f);
});
}},
{"host",
[](CortexConfig& data, const std::string&, const std::string& v) {
data.apiServerHost = v;
}},
{"port",
[](CortexConfig& data, const std::string& k, const std::string& v) {
data.apiServerPort = v;
(void)k;
}},
{"hf-token",
[](CortexConfig& data, const std::string&, const std::string& v) {
data.huggingFaceToken = v;
}},
{"gh-agent",
[](CortexConfig& data, const std::string&, const std::string& v) {
data.gitHubUserAgent = v;
}},
{"gh-token",
[](CortexConfig& data, const std::string&, const std::string& v) {
data.gitHubToken = v;
}},
{"cors",
[this](CortexConfig& data, const std::string& k,
const std::string& v) {
UpdateBooleanField(k, v, [&data](bool b) { data.enableCors = b; });
}},
{"origins",
[this](CortexConfig& data, const std::string& k,
const std::string& v) {
UpdateVectorField(k, v,
[&data](const std::vector<std::string>& orgs) {
data.allowedOrigins = orgs;
});
}},
{"proxy-url",
[](CortexConfig& data, const std::string&, const std::string& v) {
data.proxyUrl = v;
}},
{"verify-proxy",
[this](CortexConfig& data, const std::string& k,
const std::string& v) {
UpdateBooleanField(k, v,
[&data](bool b) { data.verifyProxySsl = b; });
}},
{"verify-proxy-host",
[this](CortexConfig& data, const std::string& k,
const std::string& v) {
UpdateBooleanField(
k, v, [&data](bool b) { data.verifyProxyHostSsl = b; });
}},
{"proxy-username",
[](CortexConfig& data, const std::string&, const std::string& v) {
data.proxyUsername = v;
}},
{"proxy-password",
[](CortexConfig& data, const std::string&, const std::string& v) {
data.proxyPassword = v;
}},
{"no-proxy",
[](CortexConfig& data, const std::string&, const std::string& v) {
data.noProxy = v;
}},
{"verify-ssl-peer",
[this](CortexConfig& data, const std::string& k,
const std::string& v) {
UpdateBooleanField(k, v,
[&data](bool b) { data.verifyPeerSsl = b; });
}},
{"verify-ssl-host",
[this](CortexConfig& data, const std::string& k,
const std::string& v) {
UpdateBooleanField(k, v,
[&data](bool b) { data.verifyHostSsl = b; });
}},
{"ssl-cert-path",
[](CortexConfig& data, const std::string&, const std::string& v) {
data.sslCertPath = v;
}},
{"ssl-key-path",
[](CortexConfig& data, const std::string&, const std::string& v) {
data.sslKeyPath = v;
}},
};
if (auto it = updaters.find(key); it != updaters.end()) {
it->second(data, key, value);
CTL_INF("Updated " << key << " to: " << value);
} else {
CTL_WRN("Warning: Unknown configuration key '" << key << "' ignored.");
}
}
void ServerStartCmd::UpdateVectorField(
const std::string& key, const std::string& value,
std::function<void(const std::vector<std::string>&)> setter) {
std::vector<std::string> tokens;
std::istringstream iss(value);
std::string token;
while (std::getline(iss, token, ',')) {
tokens.push_back(token);
}
setter(tokens);
(void)key;
}
void ServerStartCmd::UpdateNumericField(const std::string& key,
const std::string& value,
std::function<void(float)> setter) {
try {
float numeric_val = std::stof(value);
setter(numeric_val);
} catch (const std::exception& e) {
CLI_LOG("Failed to parse numeric value for " << key << ": " << e.what());
}
}
void ServerStartCmd::UpdateBooleanField(const std::string& key,
const std::string& value,
std::function<void(bool)> setter) {
bool bool_value = (value == "true" || value == "1");
setter(bool_value);
(void)key;
}
}; // namespace commands