fix problem with api key

This commit is contained in:
Xuan Son Nguyen 2024-11-09 18:40:07 -04:00
parent 22160f000e
commit 084afaa1e2
2 changed files with 36 additions and 17 deletions

View file

@ -196,6 +196,10 @@
<h3 class="text-lg font-bold mb-6">Settings</h3> <h3 class="text-lg font-bold mb-6">Settings</h3>
<div class="h-[calc(90vh-12rem)] overflow-y-auto"> <div class="h-[calc(90vh-12rem)] overflow-y-auto">
<p class="opacity-40 mb-6">Settings below are saved in browser's localStorage</p> <p class="opacity-40 mb-6">Settings below are saved in browser's localStorage</p>
<label class="input input-bordered flex items-center gap-2 mb-2">
<b>API Key</b>
<input type="text" class="grow" placeholder="Default: (empty)" v-model="config.apiKey" />
</label>
<label class="form-control mb-2"> <label class="form-control mb-2">
<div class="label">System Message</div> <div class="label">System Message</div>
<textarea class="textarea textarea-bordered h-24" :placeholder="'Default: ' + configDefault.systemMessage" v-model="config.systemMessage"></textarea> <textarea class="textarea textarea-bordered h-24" :placeholder="'Default: ' + configDefault.systemMessage" v-model="config.systemMessage"></textarea>
@ -331,7 +335,11 @@
if (!conv) return; if (!conv) return;
const msg = conv.messages.pop(); const msg = conv.messages.pop();
conv.lastModified = Date.now(); conv.lastModified = Date.now();
localStorage.setItem(convId, JSON.stringify(conv)); if (conv.messages.length === 0) {
StorageUtils.remove(convId);
} else {
localStorage.setItem(convId, JSON.stringify(conv));
}
return msg; return msg;
}, },
@ -512,6 +520,7 @@
this.isGenerating = false; this.isGenerating = false;
this.stopGeneration = () => {}; this.stopGeneration = () => {};
this.fetchMessages(); this.fetchMessages();
chatScrollToBottom();
}, },
// message actions // message actions

View file

@ -102,6 +102,12 @@ struct server_task_result {
bool error; bool error;
}; };
struct server_static_file {
const unsigned char * data;
unsigned int size;
const char * mime_type;
};
struct slot_params { struct slot_params {
bool stream = true; bool stream = true;
bool cache_prompt = false; // remember the prompt to avoid reprocessing all prompt bool cache_prompt = false; // remember the prompt to avoid reprocessing all prompt
@ -2254,6 +2260,16 @@ int main(int argc, char ** argv) {
LOG_INF("%s\n", common_params_get_system_info(params).c_str()); LOG_INF("%s\n", common_params_get_system_info(params).c_str());
LOG_INF("\n"); LOG_INF("\n");
// static files
std::map<std::string, server_static_file> static_files = {
{ "/", { index_html, index_html_len, "text/html; charset=utf-8" }},
{ "/completion.js", { completion_js, completion_js_len, "text/javascript; charset=utf-8" }},
{ "/deps_daisyui.min.css", { deps_daisyui_min_css, deps_daisyui_min_css_len, "text/css; charset=utf-8" }},
{ "/deps_markdown-it.js", { deps_markdown_it_js, deps_markdown_it_js_len, "text/javascript; charset=utf-8" }},
{ "/deps_tailwindcss.js", { deps_tailwindcss_js, deps_tailwindcss_js_len, "text/javascript; charset=utf-8" }},
{ "/deps_vue.esm-browser.js", { deps_vue_esm_browser_js, deps_vue_esm_browser_js_len, "text/javascript; charset=utf-8" }},
};
std::unique_ptr<httplib::Server> svr; std::unique_ptr<httplib::Server> svr;
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
if (params.ssl_file_key != "" && params.ssl_file_cert != "") { if (params.ssl_file_key != "" && params.ssl_file_cert != "") {
@ -2334,7 +2350,7 @@ int main(int argc, char ** argv) {
// Middlewares // Middlewares
// //
auto middleware_validate_api_key = [&params, &res_error](const httplib::Request & req, httplib::Response & res) { auto middleware_validate_api_key = [&params, &res_error, &static_files](const httplib::Request & req, httplib::Response & res) {
static const std::unordered_set<std::string> public_endpoints = { static const std::unordered_set<std::string> public_endpoints = {
"/health", "/health",
"/models", "/models",
@ -2346,8 +2362,8 @@ int main(int argc, char ** argv) {
return true; return true;
} }
// If path is public, skip validation // If path is public or is static file, skip validation
if (public_endpoints.find(req.path) != public_endpoints.end()) { if (public_endpoints.find(req.path) != public_endpoints.end() || static_files.find(req.path) != static_files.end()) {
return true; return true;
} }
@ -3091,13 +3107,6 @@ int main(int argc, char ** argv) {
res.status = 200; // HTTP OK res.status = 200; // HTTP OK
}; };
auto handle_static_file = [](unsigned char * content, size_t len, const char * mime_type) {
return [content, len, mime_type](const httplib::Request &, httplib::Response & res) {
res.set_content(reinterpret_cast<const char*>(content), len, mime_type);
return false;
};
};
// //
// Router // Router
// //
@ -3112,12 +3121,13 @@ int main(int argc, char ** argv) {
} }
} else { } else {
// using embedded static files // using embedded static files
svr->Get("/", handle_static_file(index_html, index_html_len, "text/html; charset=utf-8")); for (const auto & it : static_files) {
svr->Get("/completion.js", handle_static_file(completion_js, completion_js_len, "text/javascript; charset=utf-8")); const server_static_file & static_file = it.second;
svr->Get("/deps_daisyui.min.css", handle_static_file(deps_daisyui_min_css, deps_daisyui_min_css_len, "text/css; charset=utf-8")); svr->Get(it.first.c_str(), [&static_file](const httplib::Request &, httplib::Response & res) {
svr->Get("/deps_markdown-it.js", handle_static_file(deps_markdown_it_js, deps_markdown_it_js_len, "text/javascript; charset=utf-8")); res.set_content(reinterpret_cast<const char*>(static_file.data), static_file.size, static_file.mime_type);
svr->Get("/deps_tailwindcss.js", handle_static_file(deps_tailwindcss_js, deps_tailwindcss_js_len, "text/javascript; charset=utf-8")); return false;
svr->Get("/deps_vue.esm-browser.js", handle_static_file(deps_vue_esm_browser_js, deps_vue_esm_browser_js_len, "text/javascript; charset=utf-8")); });
}
} }
// register API routes // register API routes