Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit 5eda212

Browse files
chore: refactor utils (#1760)
* chore: config_yaml_utils * chore: file_manager_utils * chore: curl_utils * chore: system_info_utils * chore: clean e2e tests * fix: build macos * fix: docker e2e tests * fix: e2e docker * fix: e2e tests --------- Co-authored-by: vansangpfiev <sang@jan.ai>
1 parent d5231eb commit 5eda212

30 files changed

+1198
-1232
lines changed

docker/entrypoint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ echo "enableCors: true" >> /root/.cortexrc
77

88
# Install the engine
99
cortex engines install llama-cpp -s /opt/cortex.llamacpp
10-
cortex engines list
1110

1211
# Start the cortex server
1312
cortex start
13+
cortex engines list
1414

1515
# Keep the container running by tailing the log files
1616
tail -f /root/cortexcpp/logs/cortex.log &

engine/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,11 @@ aux_source_directory(cortex-common CORTEX_COMMON)
176176
aux_source_directory(config CONFIG_SRC)
177177
aux_source_directory(database DB_SRC)
178178
aux_source_directory(migrations MIGR_SRC)
179+
aux_source_directory(utils UTILS_SRC)
179180

180181
target_include_directories(${TARGET_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} )
181182

182-
target_sources(${TARGET_NAME} PRIVATE ${CONFIG_SRC} ${CTL_SRC} ${COMMON_SRC} ${SERVICES_SRC} ${DB_SRC} ${MIGR_SRC})
183+
target_sources(${TARGET_NAME} PRIVATE ${UTILS_SRC} ${CONFIG_SRC} ${CTL_SRC} ${COMMON_SRC} ${SERVICES_SRC} ${DB_SRC} ${MIGR_SRC})
183184

184185
set_target_properties(${TARGET_NAME} PROPERTIES
185186
RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}

engine/cli/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ add_executable(${TARGET_NAME} main.cc
8484
${CMAKE_CURRENT_SOURCE_DIR}/../services/hardware_service.cc
8585
${CMAKE_CURRENT_SOURCE_DIR}/utils/easywsclient.cc
8686
${CMAKE_CURRENT_SOURCE_DIR}/utils/download_progress.cc
87+
${CMAKE_CURRENT_SOURCE_DIR}/../utils/config_yaml_utils.cc
88+
${CMAKE_CURRENT_SOURCE_DIR}/../utils/file_manager_utils.cc
89+
${CMAKE_CURRENT_SOURCE_DIR}/../utils/curl_utils.cc
90+
${CMAKE_CURRENT_SOURCE_DIR}/../utils/system_info_utils.cc
8791
)
8892

8993
target_link_libraries(${TARGET_NAME} PRIVATE CLI11::CLI11)

engine/cli/commands/engine_list_cmd.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "common/engine_servicei.h"
55
#include "server_start_cmd.h"
66
#include "utils/curl_utils.h"
7+
#include "utils/engine_constants.h"
78
#include "utils/logging_utils.h"
89
#include "utils/url_parser.h"
910
// clang-format off

engine/cli/commands/ps_cmd.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <string>
33
#include <tabulate/table.hpp>
44
#include "utils/curl_utils.h"
5+
#include "utils/engine_constants.h"
56
#include "utils/format_utils.h"
67
#include "utils/logging_utils.h"
78
#include "utils/string_utils.h"

engine/cli/commands/server_start_cmd.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "server_start_cmd.h"
22
#include "commands/cortex_upd_cmd.h"
33
#include "utils/cortex_utils.h"
4+
#include "utils/engine_constants.h"
45
#include "utils/file_manager_utils.h"
56
#include "utils/widechar_conv.h"
67

@@ -27,6 +28,10 @@ bool TryConnectToServer(const std::string& host, int port) {
2728

2829
bool ServerStartCmd::Exec(const std::string& host, int port,
2930
const std::optional<std::string>& log_level) {
31+
if (IsServerAlive(host, port)) {
32+
CLI_LOG("The server has already started");
33+
return true;
34+
}
3035
std::string log_level_;
3136
if (!log_level.has_value()) {
3237
log_level_ = "INFO";

engine/cli/main.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ int main(int argc, char* argv[]) {
148148

149149
if (should_check_for_latest_llamacpp_version) {
150150
std::thread t1([]() {
151-
auto config = file_manager_utils::GetCortexConfig();
152151
// TODO: namh current we only check for llamacpp. Need to add support for other engine
153152
auto get_latest_version = []() -> cpp::result<std::string, std::string> {
154153
try {
@@ -176,6 +175,7 @@ int main(int argc, char* argv[]) {
176175

177176
auto now = std::chrono::system_clock::now();
178177
CTL_DBG("latest llama.cpp version: " << res.value());
178+
auto config = file_manager_utils::GetCortexConfig();
179179
config.checkedForLlamacppUpdateAt =
180180
std::chrono::duration_cast<std::chrono::milliseconds>(
181181
now.time_since_epoch())

engine/e2e-test/main.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,16 @@
33

44
### e2e tests are expensive, have to keep engines tests in order
55
from test_api_engine_list import TestApiEngineList
6-
from test_api_engine_install import TestApiEngineInstall
7-
from test_api_engine_get import TestApiEngineGet
8-
9-
### models, keeps in order, note that we only uninstall engine after finishing all models test
10-
from test_api_model_pull_direct_url import TestApiModelPullDirectUrl
11-
from test_api_model_start_stop import TestApiModelStartStop
12-
from test_api_model_get import TestApiModelGet
13-
from test_api_model_list import TestApiModelList
14-
from test_api_model_update import TestApiModelUpdate
15-
from test_api_model_delete import TestApiModelDelete
6+
from test_api_engine import TestApiEngine
7+
from test_api_model import TestApiModel
168
from test_api_model_import import TestApiModelImport
17-
from test_api_engine_uninstall import TestApiEngineUninstall
189

1910
###
2011
from test_cli_engine_get import TestCliEngineGet
2112
from test_cli_engine_install import TestCliEngineInstall
2213
from test_cli_engine_list import TestCliEngineList
2314
from test_cli_engine_uninstall import TestCliEngineUninstall
24-
from test_cli_model_delete import TestCliModelDelete
25-
from test_cli_model_pull_direct_url import TestCliModelPullDirectUrl
15+
from test_cli_model import TestCliModel
2616
from test_cli_server_start import TestCliServerStart
2717
from test_cortex_update import TestCortexUpdate
2818
from test_create_log_folder import TestCreateLogFolder

engine/e2e-test/test_api_engine_uninstall.py renamed to engine/e2e-test/test_api_engine.py

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,49 @@
1-
import time
2-
31
import pytest
42
import requests
3+
import time
54
from test_runner import (
6-
run,
7-
start_server_if_needed,
5+
start_server,
86
stop_server,
97
wait_for_websocket_download_success_event,
108
)
119

12-
13-
class TestApiEngineUninstall:
10+
class TestApiEngine:
1411

1512
@pytest.fixture(autouse=True)
1613
def setup_and_teardown(self):
1714
# Setup
18-
start_server_if_needed()
15+
success = start_server()
16+
if not success:
17+
raise Exception("Failed to start server")
1918

2019
yield
2120

2221
# Teardown
2322
stop_server()
23+
24+
# engines get
25+
def test_engines_get_llamacpp_should_be_successful(self):
26+
response = requests.get("http://localhost:3928/engines/llama-cpp")
27+
assert response.status_code == 200
28+
29+
# engines install
30+
def test_engines_install_llamacpp_specific_version_and_variant(self):
31+
data = {"version": "v0.1.35-27.10.24", "variant": "linux-amd64-avx-cuda-11-7"}
32+
response = requests.post(
33+
"http://localhost:3928/v1/engines/llama-cpp/install", json=data
34+
)
35+
assert response.status_code == 200
2436

37+
def test_engines_install_llamacpp_specific_version_and_null_variant(self):
38+
data = {"version": "v0.1.35-27.10.24"}
39+
response = requests.post(
40+
"http://localhost:3928/v1/engines/llama-cpp/install", json=data
41+
)
42+
assert response.status_code == 200
43+
44+
# engines uninstall
2545
@pytest.mark.asyncio
26-
async def test_engines_uninstall_llamacpp_should_be_successful(self):
46+
async def test_engines_install_uninstall_llamacpp_should_be_successful(self):
2747
response = requests.post("http://localhost:3928/v1/engines/llama-cpp/install")
2848
assert response.status_code == 200
2949
await wait_for_websocket_download_success_event(timeout=None)
@@ -33,7 +53,7 @@ async def test_engines_uninstall_llamacpp_should_be_successful(self):
3353
assert response.status_code == 200
3454

3555
@pytest.mark.asyncio
36-
async def test_engines_uninstall_llamacpp_with_only_version_should_be_failed(self):
56+
async def test_engines_install_uninstall_llamacpp_with_only_version_should_be_failed(self):
3757
# install first
3858
data = {"variant": "mac-arm64"}
3959
install_response = requests.post(
@@ -50,7 +70,7 @@ async def test_engines_uninstall_llamacpp_with_only_version_should_be_failed(sel
5070
assert response.json()["message"] == "No variant provided"
5171

5272
@pytest.mark.asyncio
53-
async def test_engines_uninstall_llamacpp_with_variant_should_be_successful(self):
73+
async def test_engines_install_uninstall_llamacpp_with_variant_should_be_successful(self):
5474
# install first
5575
data = {"variant": "mac-arm64"}
5676
install_response = requests.post(
@@ -62,7 +82,7 @@ async def test_engines_uninstall_llamacpp_with_variant_should_be_successful(self
6282
response = requests.delete("http://127.0.0.1:3928/v1/engines/llama-cpp/install")
6383
assert response.status_code == 200
6484

65-
def test_engines_uninstall_llamacpp_with_specific_variant_and_version_should_be_successful(
85+
def test_engines_install_uninstall_llamacpp_with_specific_variant_and_version_should_be_successful(
6686
self,
6787
):
6888
data = {"variant": "mac-arm64", "version": "v0.1.35"}
@@ -76,3 +96,5 @@ def test_engines_uninstall_llamacpp_with_specific_variant_and_version_should_be_
7696
"http://localhost:3928/v1/engines/llama-cpp/install", json=data
7797
)
7898
assert response.status_code == 200
99+
100+

engine/e2e-test/test_api_engine_get.py

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)