From 4722a2c16d4f3bae8c310233b883319e42a38e53 Mon Sep 17 00:00:00 2001 From: sabaimran Date: Sun, 18 Jun 2023 10:06:45 -0700 Subject: [PATCH] Add Github configuration page and success notifications --- src/khoj/interface/web/config.html | 3 + .../web/content_type_github_input.html | 99 +++++++++++++++++++ .../interface/web/content_type_input.html | 9 +- .../web/processor_conversation_input.html | 9 +- src/khoj/routers/api.py | 34 ++++++- src/khoj/routers/web_client.py | 21 ++++ 6 files changed, 168 insertions(+), 7 deletions(-) create mode 100644 src/khoj/interface/web/content_type_github_input.html diff --git a/src/khoj/interface/web/config.html b/src/khoj/interface/web/config.html index 5416b0d5..091303ca 100644 --- a/src/khoj/interface/web/config.html +++ b/src/khoj/interface/web/config.html @@ -14,6 +14,9 @@ +

Processors

+ + +{% endblock %} diff --git a/src/khoj/interface/web/content_type_input.html b/src/khoj/interface/web/content_type_input.html index 30966026..6110e5bd 100644 --- a/src/khoj/interface/web/content_type_input.html +++ b/src/khoj/interface/web/content_type_input.html @@ -2,6 +2,7 @@ {% block content %}

{{ content_type }}

+
@@ -136,7 +137,13 @@ }) .then(response => response.json()) .then(data => { - console.log('Success:', data); + if (data["status"] == "ok") { + document.getElementById("success").innerHTML = "✅ Successfully updated. Go to your settings to regenerate your index."; + document.getElementById("success").style.display = "block"; + } else { + document.getElementById("success").innerHTML = "⚠️ Failed to update settings."; + document.getElementById("success").style.display = "block"; + } }) }); diff --git a/src/khoj/interface/web/processor_conversation_input.html b/src/khoj/interface/web/processor_conversation_input.html index f8714659..fd499574 100644 --- a/src/khoj/interface/web/processor_conversation_input.html +++ b/src/khoj/interface/web/processor_conversation_input.html @@ -2,6 +2,7 @@ {% block content %}

Conversation

+
@@ -65,7 +66,13 @@ }) .then(response => response.json()) .then(data => { - console.log('Success:', data); + if (data["status"] == "ok") { + document.getElementById("success").innerHTML = "✅ Successfully updated. Go to your settings to regenerate your index."; + document.getElementById("success").style.display = "block"; + } else { + document.getElementById("success").innerHTML = "⚠️ Failed to update settings."; + document.getElementById("success").style.display = "block"; + } }) }); diff --git a/src/khoj/routers/api.py b/src/khoj/routers/api.py index d0962d62..f10543bd 100644 --- a/src/khoj/routers/api.py +++ b/src/khoj/routers/api.py @@ -15,7 +15,13 @@ from khoj.processor.conversation.gpt import converse, extract_questions from khoj.processor.conversation.utils import message_to_log, message_to_prompt from khoj.search_type import image_search, text_search from khoj.utils.helpers import log_telemetry, timer -from khoj.utils.rawconfig import FullConfig, SearchResponse, TextContentConfig, ConversationProcessorConfig +from khoj.utils.rawconfig import ( + FullConfig, + SearchResponse, + TextContentConfig, + ConversationProcessorConfig, + GithubContentConfig, +) from khoj.utils.state import SearchType from khoj.utils import state, constants from khoj.utils.yaml import save_config_to_file_updated_state @@ -66,16 +72,34 @@ async def set_config_data(updated_config: FullConfig): return state.config -@api.post("/config/data/content_type/{content_type}") +@api.post("/config/data/content_type/github", status_code=200) +async def set_content_config_github_data(updated_config: GithubContentConfig): + state.config.content_type.github = updated_config + try: + save_config_to_file_updated_state() + return {"status": "ok"} + except Exception as e: + return {"status": "error", "message": str(e)} + + +@api.post("/config/data/content_type/{content_type}", status_code=200) async def set_content_config_data(content_type: str, updated_config: TextContentConfig): state.config.content_type[content_type] = updated_config - save_config_to_file_updated_state() + try: + save_config_to_file_updated_state() + return {"status": "ok"} + except Exception as e: + return {"status": "error", "message": str(e)} -@api.post("/config/data/processor/conversation") +@api.post("/config/data/processor/conversation", status_code=200) async def set_processor_conversation_config_data(updated_config: ConversationProcessorConfig): state.config.processor.conversation = updated_config - save_config_to_file_updated_state() + try: + save_config_to_file_updated_state() + return {"status": "ok"} + except Exception as e: + return {"status": "error", "message": str(e)} @api.get("/search", response_model=List[SearchResponse]) diff --git a/src/khoj/routers/web_client.py b/src/khoj/routers/web_client.py index da40628c..ee617615 100644 --- a/src/khoj/routers/web_client.py +++ b/src/khoj/routers/web_client.py @@ -30,6 +30,27 @@ def config_page(request: Request): return templates.TemplateResponse("config.html", context={"request": request}) +@web_client.get("/config/content_type/github", response_class=HTMLResponse) +def github_config_page(request: Request): + default_copy = constants.default_config.copy() + default_github = default_copy["content-type"]["github"] # type: ignore + + default_config = TextContentConfig( + compressed_jsonl=default_github["compressed-jsonl"], + embeddings_file=default_github["embeddings-file"], + ) + + current_config = ( + state.config.content_type.github if state.config.content_type.github is not None else default_config + ) + + current_config = json.loads(current_config.json()) + + return templates.TemplateResponse( + "content_type_github_input.html", context={"request": request, "current_config": current_config} + ) + + @web_client.get("/config/content_type/{content_type}", response_class=HTMLResponse) def content_config_page(request: Request, content_type: str): if content_type not in VALID_CONTENT_TYPES: