From 07dc04f40e05b75aff89a2001575758e1d7862a9 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Tue, 6 Feb 2024 01:50:48 +0530 Subject: [PATCH] Allow calls to Khoj server from Obsidian mobile app to fix CORS issue - Obsidian mobile uses capacitor js. Requests from it have origin as http://localhost on Android and capacitor://localhost on iOS - Allow those Obsidian mobile origins in CORS middleware of server --- src/interface/obsidian/src/chat_modal.ts | 10 ++++++---- src/khoj/main.py | 2 ++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/interface/obsidian/src/chat_modal.ts b/src/interface/obsidian/src/chat_modal.ts index 46c52e63..723ff218 100644 --- a/src/interface/obsidian/src/chat_modal.ts +++ b/src/interface/obsidian/src/chat_modal.ts @@ -253,10 +253,13 @@ export class KhojChatModal extends Modal { async getChatHistory(chatBodyEl: Element): Promise { // Get chat history from Khoj backend let chatUrl = `${this.setting.khojUrl}/api/chat/history?client=obsidian`; - let headers = { "Authorization": `Bearer ${this.setting.khojApiKey}` }; try { - let response = await fetch(chatUrl, { method: "GET", headers: headers }); + let response = await fetch(chatUrl, { + method: "GET", + headers: { "Authorization": `Bearer ${this.setting.khojApiKey}` }, + }); + let responseJson: any = await response.json(); if (responseJson.detail) { @@ -361,7 +364,6 @@ export class KhojChatModal extends Modal { let response = await fetch(chatUrl, { method: "GET", headers: { - "Access-Control-Allow-Origin": "*", "Content-Type": "text/event-stream", "Authorization": `Bearer ${this.setting.khojApiKey}`, }, @@ -422,7 +424,7 @@ export class KhojChatModal extends Modal { let chatBody = this.contentEl.getElementsByClassName("khoj-chat-body")[0]; let response = await request({ - url: `${this.setting.khojUrl}/api/chat/history?client=web`, + url: `${this.setting.khojUrl}/api/chat/history?client=obsidian`, method: "DELETE", headers: { "Authorization": `Bearer ${this.setting.khojApiKey}` }, }) diff --git a/src/khoj/main.py b/src/khoj/main.py index 72036695..0ca6442c 100644 --- a/src/khoj/main.py +++ b/src/khoj/main.py @@ -61,6 +61,8 @@ app.add_middleware( CORSMiddleware, allow_origins=[ "app://obsidian.md", + "capacitor://localhost", # To allow access from Obsidian iOS app using Capacitor.JS + "http://localhost", # To allow access from Obsidian Android app "http://localhost:*", "http://127.0.0.1:*", f"https://{KHOJ_DOMAIN}",