khoj 1.16.1.dev47__py3-none-any.whl → 1.17.1.dev216__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- khoj/configure.py +6 -6
- khoj/database/adapters/__init__.py +47 -2
- khoj/database/migrations/0053_agent_style_color_agent_style_icon.py +61 -0
- khoj/database/models/__init__.py +35 -0
- khoj/interface/web/assets/icons/favicon-128x128.png +0 -0
- khoj/interface/web/assets/icons/favicon-256x256.png +0 -0
- khoj/interface/web/assets/icons/khoj-logo-sideways-200.png +0 -0
- khoj/interface/web/assets/icons/khoj-logo-sideways-500.png +0 -0
- khoj/interface/web/assets/icons/khoj-logo-sideways.svg +31 -5384
- khoj/interface/web/assets/icons/khoj.svg +26 -0
- khoj/interface/web/chat.html +5 -5
- khoj/interface/web/content_source_computer_input.html +3 -3
- khoj/interface/web/content_source_github_input.html +1 -1
- khoj/interface/web/content_source_notion_input.html +1 -1
- khoj/interface/web/public_conversation.html +1 -1
- khoj/interface/web/search.html +2 -2
- khoj/interface/web/{config.html → settings.html} +30 -30
- khoj/interface/web/utils.html +1 -1
- khoj/processor/content/docx/docx_to_entries.py +4 -9
- khoj/processor/content/github/github_to_entries.py +1 -3
- khoj/processor/content/images/image_to_entries.py +4 -9
- khoj/processor/content/markdown/markdown_to_entries.py +4 -9
- khoj/processor/content/notion/notion_to_entries.py +1 -3
- khoj/processor/content/org_mode/org_to_entries.py +4 -9
- khoj/processor/content/pdf/pdf_to_entries.py +4 -9
- khoj/processor/content/plaintext/plaintext_to_entries.py +4 -9
- khoj/processor/content/text_to_entries.py +1 -3
- khoj/processor/tools/online_search.py +4 -4
- khoj/routers/api.py +49 -4
- khoj/routers/api_agents.py +3 -1
- khoj/routers/api_chat.py +80 -88
- khoj/routers/api_content.py +538 -0
- khoj/routers/api_model.py +156 -0
- khoj/routers/helpers.py +308 -7
- khoj/routers/notion.py +2 -8
- khoj/routers/web_client.py +43 -256
- khoj/search_type/text_search.py +5 -4
- khoj/utils/fs_syncer.py +3 -1
- khoj/utils/rawconfig.py +6 -1
- {khoj-1.16.1.dev47.dist-info → khoj-1.17.1.dev216.dist-info}/METADATA +2 -2
- {khoj-1.16.1.dev47.dist-info → khoj-1.17.1.dev216.dist-info}/RECORD +44 -42
- khoj/routers/api_config.py +0 -434
- khoj/routers/indexer.py +0 -349
- {khoj-1.16.1.dev47.dist-info → khoj-1.17.1.dev216.dist-info}/WHEEL +0 -0
- {khoj-1.16.1.dev47.dist-info → khoj-1.17.1.dev216.dist-info}/entry_points.txt +0 -0
- {khoj-1.16.1.dev47.dist-info → khoj-1.17.1.dev216.dist-info}/licenses/LICENSE +0 -0
khoj/routers/helpers.py
CHANGED
|
@@ -5,6 +5,7 @@ import io
|
|
|
5
5
|
import json
|
|
6
6
|
import logging
|
|
7
7
|
import math
|
|
8
|
+
import os
|
|
8
9
|
import re
|
|
9
10
|
from concurrent.futures import ThreadPoolExecutor
|
|
10
11
|
from datetime import datetime, timedelta, timezone
|
|
@@ -36,6 +37,7 @@ from PIL import Image
|
|
|
36
37
|
from starlette.authentication import has_required_scope
|
|
37
38
|
from starlette.requests import URL
|
|
38
39
|
|
|
40
|
+
from khoj.database import adapters
|
|
39
41
|
from khoj.database.adapters import (
|
|
40
42
|
AgentAdapters,
|
|
41
43
|
AutomationAdapters,
|
|
@@ -43,18 +45,31 @@ from khoj.database.adapters import (
|
|
|
43
45
|
EntryAdapters,
|
|
44
46
|
create_khoj_token,
|
|
45
47
|
get_khoj_tokens,
|
|
48
|
+
get_user_name,
|
|
49
|
+
get_user_notion_config,
|
|
50
|
+
get_user_subscription_state,
|
|
46
51
|
run_with_process_lock,
|
|
47
52
|
)
|
|
48
53
|
from khoj.database.models import (
|
|
49
54
|
ChatModelOptions,
|
|
50
55
|
ClientApplication,
|
|
51
56
|
Conversation,
|
|
57
|
+
GithubConfig,
|
|
52
58
|
KhojUser,
|
|
59
|
+
NotionConfig,
|
|
53
60
|
ProcessLock,
|
|
54
61
|
Subscription,
|
|
55
62
|
TextToImageModelConfig,
|
|
56
63
|
UserRequests,
|
|
57
64
|
)
|
|
65
|
+
from khoj.processor.content.docx.docx_to_entries import DocxToEntries
|
|
66
|
+
from khoj.processor.content.github.github_to_entries import GithubToEntries
|
|
67
|
+
from khoj.processor.content.images.image_to_entries import ImageToEntries
|
|
68
|
+
from khoj.processor.content.markdown.markdown_to_entries import MarkdownToEntries
|
|
69
|
+
from khoj.processor.content.notion.notion_to_entries import NotionToEntries
|
|
70
|
+
from khoj.processor.content.org_mode.org_to_entries import OrgToEntries
|
|
71
|
+
from khoj.processor.content.pdf.pdf_to_entries import PdfToEntries
|
|
72
|
+
from khoj.processor.content.plaintext.plaintext_to_entries import PlaintextToEntries
|
|
58
73
|
from khoj.processor.conversation import prompts
|
|
59
74
|
from khoj.processor.conversation.anthropic.anthropic_chat import (
|
|
60
75
|
anthropic_send_message_to_model,
|
|
@@ -70,11 +85,15 @@ from khoj.processor.conversation.utils import (
|
|
|
70
85
|
generate_chatml_messages_with_context,
|
|
71
86
|
save_to_conversation_log,
|
|
72
87
|
)
|
|
88
|
+
from khoj.processor.speech.text_to_speech import is_eleven_labs_enabled
|
|
73
89
|
from khoj.routers.email import is_resend_enabled, send_task_email
|
|
74
90
|
from khoj.routers.storage import upload_image
|
|
91
|
+
from khoj.routers.twilio import is_twilio_enabled
|
|
92
|
+
from khoj.search_type import text_search
|
|
75
93
|
from khoj.utils import state
|
|
76
94
|
from khoj.utils.config import OfflineChatProcessorModel
|
|
77
95
|
from khoj.utils.helpers import (
|
|
96
|
+
LRU,
|
|
78
97
|
ConversationCommand,
|
|
79
98
|
ImageIntentType,
|
|
80
99
|
is_none_or_empty,
|
|
@@ -91,6 +110,11 @@ logger = logging.getLogger(__name__)
|
|
|
91
110
|
executor = ThreadPoolExecutor(max_workers=1)
|
|
92
111
|
|
|
93
112
|
|
|
113
|
+
NOTION_OAUTH_CLIENT_ID = os.getenv("NOTION_OAUTH_CLIENT_ID")
|
|
114
|
+
NOTION_OAUTH_CLIENT_SECRET = os.getenv("NOTION_OAUTH_CLIENT_SECRET")
|
|
115
|
+
NOTION_REDIRECT_URI = os.getenv("NOTION_REDIRECT_URI")
|
|
116
|
+
|
|
117
|
+
|
|
94
118
|
def is_query_empty(query: str) -> bool:
|
|
95
119
|
return is_none_or_empty(query.strip())
|
|
96
120
|
|
|
@@ -299,7 +323,7 @@ async def aget_relevant_output_modes(query: str, conversation_history: dict, is_
|
|
|
299
323
|
response = await send_message_to_model_wrapper(relevant_mode_prompt)
|
|
300
324
|
|
|
301
325
|
try:
|
|
302
|
-
response = response.strip()
|
|
326
|
+
response = response.strip().strip('"')
|
|
303
327
|
|
|
304
328
|
if is_none_or_empty(response):
|
|
305
329
|
return ConversationCommand.Text
|
|
@@ -780,7 +804,7 @@ async def text_to_image(
|
|
|
780
804
|
chat_history += f"A: Improved Prompt: {chat['intent']['inferred-queries'][0]}\n"
|
|
781
805
|
|
|
782
806
|
if send_status_func:
|
|
783
|
-
async for event in send_status_func("
|
|
807
|
+
async for event in send_status_func("**Enhancing the Painting Prompt**"):
|
|
784
808
|
yield {ChatEvent.STATUS: event}
|
|
785
809
|
improved_image_prompt = await generate_better_image_prompt(
|
|
786
810
|
message,
|
|
@@ -792,7 +816,7 @@ async def text_to_image(
|
|
|
792
816
|
)
|
|
793
817
|
|
|
794
818
|
if send_status_func:
|
|
795
|
-
async for event in send_status_func(f"
|
|
819
|
+
async for event in send_status_func(f"**Painting to Imagine**:\n{improved_image_prompt}"):
|
|
796
820
|
yield {ChatEvent.STATUS: event}
|
|
797
821
|
|
|
798
822
|
if text_to_image_config.model_type == TextToImageModelConfig.ModelType.OPENAI:
|
|
@@ -906,7 +930,7 @@ class ApiUserRateLimiter:
|
|
|
906
930
|
)
|
|
907
931
|
raise HTTPException(
|
|
908
932
|
status_code=429,
|
|
909
|
-
detail="We're glad you're enjoying Khoj! You've exceeded your usage limit for today. Come back tomorrow or subscribe to increase your usage limit via [your settings](https://app.khoj.dev/
|
|
933
|
+
detail="We're glad you're enjoying Khoj! You've exceeded your usage limit for today. Come back tomorrow or subscribe to increase your usage limit via [your settings](https://app.khoj.dev/settings).",
|
|
910
934
|
)
|
|
911
935
|
|
|
912
936
|
# Add the current request to the cache
|
|
@@ -945,7 +969,7 @@ class ConversationCommandRateLimiter:
|
|
|
945
969
|
if not subscribed and count_requests >= self.trial_rate_limit:
|
|
946
970
|
raise HTTPException(
|
|
947
971
|
status_code=429,
|
|
948
|
-
detail=f"We're glad you're enjoying Khoj! You've exceeded your `/{conversation_command.value}` command usage limit for today. Subscribe to increase your usage limit via [your settings](https://app.khoj.dev/
|
|
972
|
+
detail=f"We're glad you're enjoying Khoj! You've exceeded your `/{conversation_command.value}` command usage limit for today. Subscribe to increase your usage limit via [your settings](https://app.khoj.dev/settings).",
|
|
949
973
|
)
|
|
950
974
|
await UserRequests.objects.acreate(user=user, slug=command_slug)
|
|
951
975
|
return
|
|
@@ -964,14 +988,15 @@ class ApiIndexedDataLimiter:
|
|
|
964
988
|
self.total_entries_size_limit = total_entries_size_limit
|
|
965
989
|
self.subscribed_total_entries_size = subscribed_total_entries_size_limit
|
|
966
990
|
|
|
967
|
-
def __call__(self, request: Request, files: List[UploadFile]):
|
|
991
|
+
def __call__(self, request: Request, files: List[UploadFile] = None):
|
|
968
992
|
if state.billing_enabled is False:
|
|
969
993
|
return
|
|
994
|
+
|
|
970
995
|
subscribed = has_required_scope(request, ["premium"])
|
|
971
996
|
incoming_data_size_mb = 0.0
|
|
972
997
|
deletion_file_names = set()
|
|
973
998
|
|
|
974
|
-
if not request.user.is_authenticated:
|
|
999
|
+
if not request.user.is_authenticated or not files:
|
|
975
1000
|
return
|
|
976
1001
|
|
|
977
1002
|
user: KhojUser = request.user.object
|
|
@@ -1198,3 +1223,279 @@ class ChatEvent(Enum):
|
|
|
1198
1223
|
MESSAGE = "message"
|
|
1199
1224
|
REFERENCES = "references"
|
|
1200
1225
|
STATUS = "status"
|
|
1226
|
+
|
|
1227
|
+
|
|
1228
|
+
def get_user_config(user: KhojUser, request: Request, is_detailed: bool = False):
|
|
1229
|
+
user_picture = request.session.get("user", {}).get("picture")
|
|
1230
|
+
is_active = has_required_scope(request, ["premium"])
|
|
1231
|
+
has_documents = EntryAdapters.user_has_entries(user=user)
|
|
1232
|
+
|
|
1233
|
+
if not is_detailed:
|
|
1234
|
+
return {
|
|
1235
|
+
"request": request,
|
|
1236
|
+
"username": user.username if user else None,
|
|
1237
|
+
"user_photo": user_picture,
|
|
1238
|
+
"is_active": is_active,
|
|
1239
|
+
"has_documents": has_documents,
|
|
1240
|
+
"khoj_version": state.khoj_version,
|
|
1241
|
+
}
|
|
1242
|
+
|
|
1243
|
+
user_subscription_state = get_user_subscription_state(user.email)
|
|
1244
|
+
user_subscription = adapters.get_user_subscription(user.email)
|
|
1245
|
+
subscription_renewal_date = (
|
|
1246
|
+
user_subscription.renewal_date.strftime("%d %b %Y")
|
|
1247
|
+
if user_subscription and user_subscription.renewal_date
|
|
1248
|
+
else (user_subscription.created_at + timedelta(days=7)).strftime("%d %b %Y")
|
|
1249
|
+
)
|
|
1250
|
+
given_name = get_user_name(user)
|
|
1251
|
+
|
|
1252
|
+
enabled_content_sources_set = set(EntryAdapters.get_unique_file_sources(user))
|
|
1253
|
+
enabled_content_sources = {
|
|
1254
|
+
"computer": ("computer" in enabled_content_sources_set),
|
|
1255
|
+
"github": ("github" in enabled_content_sources_set),
|
|
1256
|
+
"notion": ("notion" in enabled_content_sources_set),
|
|
1257
|
+
}
|
|
1258
|
+
|
|
1259
|
+
notion_oauth_url = get_notion_auth_url(user)
|
|
1260
|
+
current_notion_config = get_user_notion_config(user)
|
|
1261
|
+
notion_token = current_notion_config.token if current_notion_config else ""
|
|
1262
|
+
|
|
1263
|
+
selected_chat_model_config = ConversationAdapters.get_conversation_config(user)
|
|
1264
|
+
chat_models = ConversationAdapters.get_conversation_processor_options().all()
|
|
1265
|
+
chat_model_options = list()
|
|
1266
|
+
for chat_model in chat_models:
|
|
1267
|
+
chat_model_options.append({"name": chat_model.chat_model, "id": chat_model.id})
|
|
1268
|
+
|
|
1269
|
+
search_model_options = adapters.get_or_create_search_models().all()
|
|
1270
|
+
all_search_model_options = list()
|
|
1271
|
+
for search_model_option in search_model_options:
|
|
1272
|
+
all_search_model_options.append({"name": search_model_option.name, "id": search_model_option.id})
|
|
1273
|
+
|
|
1274
|
+
current_search_model_option = adapters.get_user_search_model_or_default(user)
|
|
1275
|
+
|
|
1276
|
+
selected_paint_model_config = ConversationAdapters.get_user_text_to_image_model_config(user)
|
|
1277
|
+
paint_model_options = ConversationAdapters.get_text_to_image_model_options().all()
|
|
1278
|
+
all_paint_model_options = list()
|
|
1279
|
+
for paint_model in paint_model_options:
|
|
1280
|
+
all_paint_model_options.append({"name": paint_model.model_name, "id": paint_model.id})
|
|
1281
|
+
|
|
1282
|
+
voice_models = ConversationAdapters.get_voice_model_options()
|
|
1283
|
+
voice_model_options = list()
|
|
1284
|
+
for voice_model in voice_models:
|
|
1285
|
+
voice_model_options.append({"name": voice_model.name, "id": voice_model.model_id})
|
|
1286
|
+
|
|
1287
|
+
if len(voice_model_options) == 0:
|
|
1288
|
+
eleven_labs_enabled = False
|
|
1289
|
+
else:
|
|
1290
|
+
eleven_labs_enabled = is_eleven_labs_enabled()
|
|
1291
|
+
|
|
1292
|
+
selected_voice_model_config = ConversationAdapters.get_voice_model_config(user)
|
|
1293
|
+
|
|
1294
|
+
return {
|
|
1295
|
+
"request": request,
|
|
1296
|
+
# user info
|
|
1297
|
+
"username": user.username if user else None,
|
|
1298
|
+
"user_photo": user_picture,
|
|
1299
|
+
"is_active": is_active,
|
|
1300
|
+
"given_name": given_name,
|
|
1301
|
+
"phone_number": str(user.phone_number) if user.phone_number else "",
|
|
1302
|
+
"is_phone_number_verified": user.verified_phone_number,
|
|
1303
|
+
# user content settings
|
|
1304
|
+
"enabled_content_source": enabled_content_sources,
|
|
1305
|
+
"has_documents": has_documents,
|
|
1306
|
+
"notion_token": notion_token,
|
|
1307
|
+
# user model settings
|
|
1308
|
+
"search_model_options": all_search_model_options,
|
|
1309
|
+
"selected_search_model_config": current_search_model_option.id,
|
|
1310
|
+
"chat_model_options": chat_model_options,
|
|
1311
|
+
"selected_chat_model_config": selected_chat_model_config.id if selected_chat_model_config else None,
|
|
1312
|
+
"paint_model_options": all_paint_model_options,
|
|
1313
|
+
"selected_paint_model_config": selected_paint_model_config.id if selected_paint_model_config else None,
|
|
1314
|
+
"voice_model_options": voice_model_options,
|
|
1315
|
+
"selected_voice_model_config": selected_voice_model_config.model_id if selected_voice_model_config else None,
|
|
1316
|
+
# user billing info
|
|
1317
|
+
"subscription_state": user_subscription_state,
|
|
1318
|
+
"subscription_renewal_date": subscription_renewal_date,
|
|
1319
|
+
# server settings
|
|
1320
|
+
"khoj_cloud_subscription_url": os.getenv("KHOJ_CLOUD_SUBSCRIPTION_URL"),
|
|
1321
|
+
"billing_enabled": state.billing_enabled,
|
|
1322
|
+
"is_eleven_labs_enabled": eleven_labs_enabled,
|
|
1323
|
+
"is_twilio_enabled": is_twilio_enabled(),
|
|
1324
|
+
"khoj_version": state.khoj_version,
|
|
1325
|
+
"anonymous_mode": state.anonymous_mode,
|
|
1326
|
+
"notion_oauth_url": notion_oauth_url,
|
|
1327
|
+
}
|
|
1328
|
+
|
|
1329
|
+
|
|
1330
|
+
def configure_content(
|
|
1331
|
+
files: Optional[dict[str, dict[str, str]]],
|
|
1332
|
+
regenerate: bool = False,
|
|
1333
|
+
t: Optional[state.SearchType] = state.SearchType.All,
|
|
1334
|
+
user: KhojUser = None,
|
|
1335
|
+
) -> bool:
|
|
1336
|
+
success = True
|
|
1337
|
+
if t == None:
|
|
1338
|
+
t = state.SearchType.All
|
|
1339
|
+
|
|
1340
|
+
if t is not None and t in [type.value for type in state.SearchType]:
|
|
1341
|
+
t = state.SearchType(t)
|
|
1342
|
+
|
|
1343
|
+
if t is not None and not t.value in [type.value for type in state.SearchType]:
|
|
1344
|
+
logger.warning(f"🚨 Invalid search type: {t}")
|
|
1345
|
+
return False
|
|
1346
|
+
|
|
1347
|
+
search_type = t.value if t else None
|
|
1348
|
+
|
|
1349
|
+
no_documents = all([not files.get(file_type) for file_type in files])
|
|
1350
|
+
|
|
1351
|
+
if files is None:
|
|
1352
|
+
logger.warning(f"🚨 No files to process for {search_type} search.")
|
|
1353
|
+
return True
|
|
1354
|
+
|
|
1355
|
+
try:
|
|
1356
|
+
# Initialize Org Notes Search
|
|
1357
|
+
if (search_type == state.SearchType.All.value or search_type == state.SearchType.Org.value) and files["org"]:
|
|
1358
|
+
logger.info("🦄 Setting up search for orgmode notes")
|
|
1359
|
+
# Extract Entries, Generate Notes Embeddings
|
|
1360
|
+
text_search.setup(
|
|
1361
|
+
OrgToEntries,
|
|
1362
|
+
files.get("org"),
|
|
1363
|
+
regenerate=regenerate,
|
|
1364
|
+
user=user,
|
|
1365
|
+
)
|
|
1366
|
+
except Exception as e:
|
|
1367
|
+
logger.error(f"🚨 Failed to setup org: {e}", exc_info=True)
|
|
1368
|
+
success = False
|
|
1369
|
+
|
|
1370
|
+
try:
|
|
1371
|
+
# Initialize Markdown Search
|
|
1372
|
+
if (search_type == state.SearchType.All.value or search_type == state.SearchType.Markdown.value) and files[
|
|
1373
|
+
"markdown"
|
|
1374
|
+
]:
|
|
1375
|
+
logger.info("💎 Setting up search for markdown notes")
|
|
1376
|
+
# Extract Entries, Generate Markdown Embeddings
|
|
1377
|
+
text_search.setup(
|
|
1378
|
+
MarkdownToEntries,
|
|
1379
|
+
files.get("markdown"),
|
|
1380
|
+
regenerate=regenerate,
|
|
1381
|
+
user=user,
|
|
1382
|
+
)
|
|
1383
|
+
|
|
1384
|
+
except Exception as e:
|
|
1385
|
+
logger.error(f"🚨 Failed to setup markdown: {e}", exc_info=True)
|
|
1386
|
+
success = False
|
|
1387
|
+
|
|
1388
|
+
try:
|
|
1389
|
+
# Initialize PDF Search
|
|
1390
|
+
if (search_type == state.SearchType.All.value or search_type == state.SearchType.Pdf.value) and files["pdf"]:
|
|
1391
|
+
logger.info("🖨️ Setting up search for pdf")
|
|
1392
|
+
# Extract Entries, Generate PDF Embeddings
|
|
1393
|
+
text_search.setup(
|
|
1394
|
+
PdfToEntries,
|
|
1395
|
+
files.get("pdf"),
|
|
1396
|
+
regenerate=regenerate,
|
|
1397
|
+
user=user,
|
|
1398
|
+
)
|
|
1399
|
+
|
|
1400
|
+
except Exception as e:
|
|
1401
|
+
logger.error(f"🚨 Failed to setup PDF: {e}", exc_info=True)
|
|
1402
|
+
success = False
|
|
1403
|
+
|
|
1404
|
+
try:
|
|
1405
|
+
# Initialize Plaintext Search
|
|
1406
|
+
if (search_type == state.SearchType.All.value or search_type == state.SearchType.Plaintext.value) and files[
|
|
1407
|
+
"plaintext"
|
|
1408
|
+
]:
|
|
1409
|
+
logger.info("📄 Setting up search for plaintext")
|
|
1410
|
+
# Extract Entries, Generate Plaintext Embeddings
|
|
1411
|
+
text_search.setup(
|
|
1412
|
+
PlaintextToEntries,
|
|
1413
|
+
files.get("plaintext"),
|
|
1414
|
+
regenerate=regenerate,
|
|
1415
|
+
user=user,
|
|
1416
|
+
)
|
|
1417
|
+
|
|
1418
|
+
except Exception as e:
|
|
1419
|
+
logger.error(f"🚨 Failed to setup plaintext: {e}", exc_info=True)
|
|
1420
|
+
success = False
|
|
1421
|
+
|
|
1422
|
+
try:
|
|
1423
|
+
if no_documents:
|
|
1424
|
+
github_config = GithubConfig.objects.filter(user=user).prefetch_related("githubrepoconfig").first()
|
|
1425
|
+
if (
|
|
1426
|
+
search_type == state.SearchType.All.value or search_type == state.SearchType.Github.value
|
|
1427
|
+
) and github_config is not None:
|
|
1428
|
+
logger.info("🐙 Setting up search for github")
|
|
1429
|
+
# Extract Entries, Generate Github Embeddings
|
|
1430
|
+
text_search.setup(
|
|
1431
|
+
GithubToEntries,
|
|
1432
|
+
None,
|
|
1433
|
+
regenerate=regenerate,
|
|
1434
|
+
user=user,
|
|
1435
|
+
config=github_config,
|
|
1436
|
+
)
|
|
1437
|
+
|
|
1438
|
+
except Exception as e:
|
|
1439
|
+
logger.error(f"🚨 Failed to setup GitHub: {e}", exc_info=True)
|
|
1440
|
+
success = False
|
|
1441
|
+
|
|
1442
|
+
try:
|
|
1443
|
+
if no_documents:
|
|
1444
|
+
# Initialize Notion Search
|
|
1445
|
+
notion_config = NotionConfig.objects.filter(user=user).first()
|
|
1446
|
+
if (
|
|
1447
|
+
search_type == state.SearchType.All.value or search_type == state.SearchType.Notion.value
|
|
1448
|
+
) and notion_config:
|
|
1449
|
+
logger.info("🔌 Setting up search for notion")
|
|
1450
|
+
text_search.setup(
|
|
1451
|
+
NotionToEntries,
|
|
1452
|
+
None,
|
|
1453
|
+
regenerate=regenerate,
|
|
1454
|
+
user=user,
|
|
1455
|
+
config=notion_config,
|
|
1456
|
+
)
|
|
1457
|
+
|
|
1458
|
+
except Exception as e:
|
|
1459
|
+
logger.error(f"🚨 Failed to setup Notion: {e}", exc_info=True)
|
|
1460
|
+
success = False
|
|
1461
|
+
|
|
1462
|
+
try:
|
|
1463
|
+
# Initialize Image Search
|
|
1464
|
+
if (search_type == state.SearchType.All.value or search_type == state.SearchType.Image.value) and files[
|
|
1465
|
+
"image"
|
|
1466
|
+
]:
|
|
1467
|
+
logger.info("🖼️ Setting up search for images")
|
|
1468
|
+
# Extract Entries, Generate Image Embeddings
|
|
1469
|
+
text_search.setup(
|
|
1470
|
+
ImageToEntries,
|
|
1471
|
+
files.get("image"),
|
|
1472
|
+
regenerate=regenerate,
|
|
1473
|
+
user=user,
|
|
1474
|
+
)
|
|
1475
|
+
except Exception as e:
|
|
1476
|
+
logger.error(f"🚨 Failed to setup images: {e}", exc_info=True)
|
|
1477
|
+
success = False
|
|
1478
|
+
try:
|
|
1479
|
+
if (search_type == state.SearchType.All.value or search_type == state.SearchType.Docx.value) and files["docx"]:
|
|
1480
|
+
logger.info("📄 Setting up search for docx")
|
|
1481
|
+
text_search.setup(
|
|
1482
|
+
DocxToEntries,
|
|
1483
|
+
files.get("docx"),
|
|
1484
|
+
regenerate=regenerate,
|
|
1485
|
+
user=user,
|
|
1486
|
+
)
|
|
1487
|
+
except Exception as e:
|
|
1488
|
+
logger.error(f"🚨 Failed to setup docx: {e}", exc_info=True)
|
|
1489
|
+
success = False
|
|
1490
|
+
|
|
1491
|
+
# Invalidate Query Cache
|
|
1492
|
+
if user:
|
|
1493
|
+
state.query_cache[user.uuid] = LRU()
|
|
1494
|
+
|
|
1495
|
+
return success
|
|
1496
|
+
|
|
1497
|
+
|
|
1498
|
+
def get_notion_auth_url(user: KhojUser):
|
|
1499
|
+
if not NOTION_OAUTH_CLIENT_ID or not NOTION_OAUTH_CLIENT_SECRET or not NOTION_REDIRECT_URI:
|
|
1500
|
+
return None
|
|
1501
|
+
return f"https://api.notion.com/v1/oauth/authorize?client_id={NOTION_OAUTH_CLIENT_ID}&redirect_uri={NOTION_REDIRECT_URI}&response_type=code&state={user.uuid}"
|
khoj/routers/notion.py
CHANGED
|
@@ -11,7 +11,7 @@ from starlette.responses import RedirectResponse
|
|
|
11
11
|
|
|
12
12
|
from khoj.database.adapters import aget_user_by_uuid
|
|
13
13
|
from khoj.database.models import KhojUser, NotionConfig
|
|
14
|
-
from khoj.routers.
|
|
14
|
+
from khoj.routers.helpers import configure_content
|
|
15
15
|
from khoj.utils.state import SearchType
|
|
16
16
|
|
|
17
17
|
NOTION_OAUTH_CLIENT_ID = os.getenv("NOTION_OAUTH_CLIENT_ID")
|
|
@@ -25,12 +25,6 @@ executor = ThreadPoolExecutor()
|
|
|
25
25
|
logger = logging.getLogger(__name__)
|
|
26
26
|
|
|
27
27
|
|
|
28
|
-
def get_notion_auth_url(user: KhojUser):
|
|
29
|
-
if not NOTION_OAUTH_CLIENT_ID or not NOTION_OAUTH_CLIENT_SECRET or not NOTION_REDIRECT_URI:
|
|
30
|
-
return None
|
|
31
|
-
return f"https://api.notion.com/v1/oauth/authorize?client_id={NOTION_OAUTH_CLIENT_ID}&redirect_uri={NOTION_REDIRECT_URI}&response_type=code&state={user.uuid}"
|
|
32
|
-
|
|
33
|
-
|
|
34
28
|
async def run_in_executor(func, *args):
|
|
35
29
|
loop = asyncio.get_event_loop()
|
|
36
30
|
return await loop.run_in_executor(executor, func, *args)
|
|
@@ -86,6 +80,6 @@ async def notion_auth_callback(request: Request, background_tasks: BackgroundTas
|
|
|
86
80
|
notion_redirect = str(request.app.url_path_for("notion_config_page"))
|
|
87
81
|
|
|
88
82
|
# Trigger an async job to configure_content. Let it run without blocking the response.
|
|
89
|
-
background_tasks.add_task(run_in_executor, configure_content, {}, False, SearchType.Notion,
|
|
83
|
+
background_tasks.add_task(run_in_executor, configure_content, {}, False, SearchType.Notion, user)
|
|
90
84
|
|
|
91
85
|
return RedirectResponse(notion_redirect)
|