diff --git a/.clang-tidy b/.clang-tidy index 14ca6b9..9895ab9 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,4 +1,5 @@ -Checks: '-*, +Checks: "-*, + clang-*, google-build-explicit-make-pair, @@ -9,11 +10,12 @@ Checks: '-*, google-runtime-int, google-runtime-operator, - readability-non-const-parameter, + + -readability-non-const-parameter, readability-avoid-const-params-in-decls, readability-const-return-type, readability-container-size-empty, - readability-convert-member-functions-to-static, + -readability-convert-member-functions-to-static, readability-delete-null-pointer, readability-deleted-default, readability-make-member-function-const, @@ -33,7 +35,8 @@ Checks: '-*, cert-dcl21-cpp, cert-dcl50-cpp, - cert-env33-c, + # 不要使用 system() + -cert-env33-c, cert-err34-c, cert-err52-cpp, cert-flp30-c, @@ -46,7 +49,7 @@ Checks: '-*, modernize-loop-convert, modernize-make-shared, modernize-make-unique, - modernize-pass-by-value, + -modernize-pass-by-value, modernize-raw-string-literal, modernize-redundant-void-arg, modernize-replace-auto-ptr, @@ -128,9 +131,8 @@ Checks: '-*, performance-no-automatic-move, performance-trivially-destructible, performance-unnecessary-copy-initialization, - - boost-use-to-string, - boost-use-ranges, -' -WarningsAsErrors: '*' \ No newline at end of file + boost-use-to-string, + boost-use-ranges" + +WarningsAsErrors: "*" diff --git a/.vscode/settings.json b/.vscode/settings.json index 90f28b1..196b926 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,8 +1,6 @@ { "files.autoSave": "onFocusChange", "editor.fontSize": 14, - "editor.fontFamily": "'Monaspace Krypton Light', 'Monaspace Krypton Light', 'Monaspace Krypton Light'", - "terminal.integrated.fontFamily": "Monaspace Krypton Light", "cmake.configureOnOpen": true, "cmake.debugConfig": { "console": "integratedTerminal", @@ -39,6 +37,7 @@ "--all-scopes-completion", "--completion-style=detailed", "-j=4", + "--clang-tidy", "--pch-storage=memory", "--compile-commands-dir=build", "--background-index", diff --git a/CMakeLists.txt b/CMakeLists.txt index 558f821..773c8f7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) if (MSVC) add_compile_options(/utf-8) + add_definitions(-D_CRT_SECURE_NO_WARNINGS) endif() add_definitions(-D_WIN32_WINNT=0x0601) diff --git a/openaiclient.cpp b/openaiclient.cpp index e606358..dd4971d 100644 --- a/openaiclient.cpp +++ b/openaiclient.cpp @@ -64,5 +64,4 @@ FrameData* OpenAIClient::ask_openai(const std::string& text) void OpenAIClient::disconnect() { - } diff --git a/openaiclient.h b/openaiclient.h index 121390e..577821c 100644 --- a/openaiclient.h +++ b/openaiclient.h @@ -7,7 +7,7 @@ class OpenAIClient { public: - OpenAIClient(asio::io_context& io_context); + explicit OpenAIClient(asio::io_context& io_context); public: bool connect(const std::string& ip, unsigned int port); @@ -15,11 +15,11 @@ public: void disconnect(); private: - std::string ip_{}; - std::string port_{}; + std::string ip_; + std::string port_; asio::ip::tcp::socket socket_; asio::io_context& io_context_; - CMutBuffer buffer_{}; + CMutBuffer buffer_; std::array tmp_buf_{}; }; diff --git a/server/config.h b/server/config.h index 40a2785..b6c6d28 100644 --- a/server/config.h +++ b/server/config.h @@ -8,7 +8,7 @@ struct Configuration { std::string user_name; std::string model_name; std::string assistant_name; - long max_tokens{}; + int32_t max_tokens{}; }; class ConfigSet @@ -16,6 +16,7 @@ class ConfigSet public: ConfigSet() = default; ~ConfigSet() = default; + public: static bool parse_config(Configuration& config, const std::string& config_path = ""); }; diff --git a/server/jsondata.cxx b/server/jsondata.cxx index a180de4..f63ae8a 100644 --- a/server/jsondata.cxx +++ b/server/jsondata.cxx @@ -1,6 +1,6 @@ #include "jsondata.h" -#include #include +#include CJsonOper::CJsonOper(const std::string& user_name, const std::string& model, const std::string& assistant_name) : user_(user_name), model_(model), assistant_(assistant_name) @@ -92,15 +92,15 @@ size_t CJsonOper::get_u8_len(unsigned char ch) { if (ch <= 0x7F) { return 1; - } else if ((ch & 0xE0) == 0xC0) { + } else if ((ch & (unsigned char)0xE0) == 0xC0) { return 2; - } else if ((ch & 0xF0) == 0xE0) { + } else if ((ch & (unsigned char)0xF0) == 0xE0) { return 3; - } else if ((ch & 0xF8) == 0xF0) { + } else if ((ch & (unsigned char)0xF8) == 0xF0) { return 4; - } else if ((ch & 0xFC) == 0xF8) { + } else if ((ch & (unsigned char)0xFC) == 0xF8) { return 5; - } else if ((ch & 0xFE) == 0xFC) { + } else if ((ch & (unsigned char)0xFE) == 0xFC) { return 6; } else { std::cerr << "invalid u8 first ch." << std::endl; @@ -118,4 +118,3 @@ std::string CJsonOper::trim(const std::string& input) size_t end = input.find_last_not_of(" \t\n\r\f\v"); return input.substr(start, end - start + 1); } - diff --git a/server/main.cxx b/server/main.cxx index 79f82c3..c15e109 100644 --- a/server/main.cxx +++ b/server/main.cxx @@ -78,7 +78,8 @@ int main(int argc, char* argv[]) // std::cout << "post success!" << std::endl; // auto re = json->parse(out); - // std::string use = "本次tokens消耗:" + std::to_string(re.prompt_tokens) + "+" + std::to_string(re.completion_tokens) + "=" + + // std::string use = "本次tokens消耗:" + std::to_string(re.prompt_tokens) + "+" + std::to_string(re.completion_tokens) + "=" + // + // std::to_string(re.total_tokens); // std::cout << use << std::endl; diff --git a/server/server.cxx b/server/server.cxx index 0fbe76e..bef3503 100644 --- a/server/server.cxx +++ b/server/server.cxx @@ -1,9 +1,9 @@ #include "server.h" +#include #include - -Server::Server(asio::io_context& io_context, short port) : io_context_(io_context), acceptor_(io_context) +Server::Server(asio::io_context& io_context, uint16_t port) : io_context_(io_context), acceptor_(io_context) { port_ = port; } @@ -44,7 +44,7 @@ void Server::set_worker(std::shared_ptr worker, std::shared_ptr lock(cli_mutex_); client_map_[client_key] = std::make_shared(); - clients_.insert( - std::make_pair(socket->remote_endpoint().address().to_string(), - std::thread([this, socket, client_key]() { th_client(socket, client_key); }))); + clients_.insert(std::make_pair(socket->remote_endpoint().address().to_string(), + std::thread([this, socket, client_key]() { th_client(socket, client_key); }))); } do_accept(); @@ -100,7 +99,7 @@ void Server::th_client(const std::shared_ptr& socket, con cache->buffer_.push(cache->tmp_buf_.data(), len); while (true) { - auto frame = com_parse(cache->buffer_); + auto* frame = com_parse(cache->buffer_); if (frame == nullptr) { break; } diff --git a/server/server.h b/server/server.h index b5a89d3..5fff784 100644 --- a/server/server.h +++ b/server/server.h @@ -11,13 +11,13 @@ struct ClientCache { std::array tmp_buf_{}; - CMutBuffer buffer_{}; + CMutBuffer buffer_; }; class Server { public: - Server(asio::io_context& io_context, short port); + Server(asio::io_context& io_context, uint16_t port); ~Server(); public: @@ -26,7 +26,7 @@ public: public: void set_worker(std::shared_ptr worker, std::shared_ptr json); - void set_token(long tokens); + void set_token(int32_t tokens); private: void do_accept(); @@ -40,16 +40,16 @@ private: asio::io_context& io_context_; asio::ip::tcp::acceptor acceptor_; - std::shared_ptr worker_{}; - std::shared_ptr json_{}; + std::shared_ptr worker_; + std::shared_ptr json_; std::mutex ask_mutex_; std::mutex cli_mutex_; std::unordered_map clients_; std::map> client_map_; - CMutBuffer buffer_{}; - short port_; - long tokens_{}; - long use_tokens_{}; + CMutBuffer buffer_; + uint16_t port_; + int32_t tokens_{}; + int32_t use_tokens_{}; }; #endif diff --git a/server/zapi.cxx b/server/zapi.cxx index 86dce06..7a1507b 100644 --- a/server/zapi.cxx +++ b/server/zapi.cxx @@ -35,7 +35,7 @@ bool COpenAI::post(const std::string& json, std::string& out) if (res != CURLE_OK) { std::cerr << "curl post failed: " << curl_easy_strerror(res) << std::endl; } else { - //std::cout << recv_ << std::endl; + // std::cout << recv_ << std::endl; out = recv_; } curl_slist_free_all(headers); @@ -45,6 +45,6 @@ bool COpenAI::post(const std::string& json, std::string& out) size_t COpenAI::recv_call(void* contents, size_t size, size_t nmemb, std::string* output) { size_t total_size = size * nmemb; - output->append(reinterpret_cast(contents), total_size); + output->append(static_cast(contents), total_size); return total_size; } diff --git a/server/zapi.h b/server/zapi.h index 2e4c521..ed01d1a 100644 --- a/server/zapi.h +++ b/server/zapi.h @@ -18,10 +18,10 @@ private: static size_t recv_call(void* contents, size_t size, size_t nmemb, std::string* output); private: - std::string api_key_{}; - std::string api_url_{}; - std::string recv_{}; - CURL* curl_{}; + std::string api_key_; + std::string api_url_; + std::string recv_; + CURL* curl_; }; #endif \ No newline at end of file