From 40d1abfe5018de1b5d3568c66a7d01e5f3eacaec Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Sat, 24 Jun 2023 00:45:30 -0700 Subject: [PATCH] Update the new /config APIs to configure Khoj for first time users - Setup state.config and sub-components from unset state - Setup search types with default settings --- src/khoj/routers/api.py | 18 +++++++++++++++--- src/khoj/routers/web_client.py | 6 +++--- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/khoj/routers/api.py b/src/khoj/routers/api.py index f7658caa..ddb3cc9d 100644 --- a/src/khoj/routers/api.py +++ b/src/khoj/routers/api.py @@ -16,7 +16,10 @@ 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 ( + ContentConfig, FullConfig, + ProcessorConfig, + SearchConfig, SearchResponse, TextContentConfig, ConversationProcessorConfig, @@ -74,7 +77,10 @@ async def set_config_data(updated_config: FullConfig): @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 + if not state.config: + state.config = FullConfig() + state.config.search_type = SearchConfig.parse_obj(constants.default_config["search-type"]) + state.config.content_type = ContentConfig(github=updated_config) try: save_config_to_file_updated_state() return {"status": "ok"} @@ -84,7 +90,10 @@ async def set_content_config_github_data(updated_config: GithubContentConfig): @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 + if not state.config: + state.config = FullConfig() + state.config.search_type = SearchConfig.parse_obj(constants.default_config["search-type"]) + state.config.content_type = ContentConfig(**{content_type: updated_config}) try: save_config_to_file_updated_state() return {"status": "ok"} @@ -94,7 +103,10 @@ async def set_content_config_data(content_type: str, updated_config: TextContent @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 + if not state.config: + state.config = FullConfig() + state.config.search_type = SearchConfig.parse_obj(constants.default_config["search-type"]) + state.config.processor = ProcessorConfig(conversation=updated_config) try: save_config_to_file_updated_state() return {"status": "ok"} diff --git a/src/khoj/routers/web_client.py b/src/khoj/routers/web_client.py index ee617615..c886dc55 100644 --- a/src/khoj/routers/web_client.py +++ b/src/khoj/routers/web_client.py @@ -41,7 +41,7 @@ def github_config_page(request: Request): ) current_config = ( - state.config.content_type.github if state.config.content_type.github is not None else default_config + state.config.content_type.github if state.config and state.config.content_type.github else default_config ) current_config = json.loads(current_config.json()) @@ -65,7 +65,7 @@ def content_config_page(request: Request, content_type: str): ) current_config = ( state.config.content_type[content_type] - if state.config.content_type[content_type] is not None + if state.config and content_type in state.config.content_type else default_config ) current_config = json.loads(current_config.json()) @@ -93,7 +93,7 @@ def conversation_processor_config_page(request: Request): current_processor_conversation_config = ( state.config.processor.conversation - if state.config.processor.conversation is not None + if state.config and state.config.processor.conversation else default_processor_config ) current_processor_conversation_config = json.loads(current_processor_conversation_config.json())