diff --git a/src/khoj/interface/web/config.html b/src/khoj/interface/web/config.html index e9c24d06..ceb09ff1 100644 --- a/src/khoj/interface/web/config.html +++ b/src/khoj/interface/web/config.html @@ -295,8 +295,8 @@
-
@@ -700,6 +700,28 @@ }); } + async function createAutomation() { + const scheduling_request = window.prompt("Describe the automation you want to create"); + if (!scheduling_request) return; + + const ip_response = await fetch("https://ipapi.co/json"); + const ip_data = await ip_response.json(); + + const query_string = `q=${scheduling_request}&city=${ip_data.city}®ion=${ip_data.region}&country=${ip_data.country_name}&timezone=${ip_data.timezone}`; + const automation_response = await fetch(`/api/automation?${query_string}`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + }); + if (!automation_response.ok) { + throw new Error(`Failed to create automation: ${automation_response.status}`); + } + + listAutomations(); + } + document.getElementById("create-automation").addEventListener("click", async () => { await createAutomation(); }); + function getIndexedDataSize() { document.getElementById("indexed-data-size").innerHTML = "Calculating..."; fetch('/api/config/index/size') diff --git a/src/khoj/routers/api.py b/src/khoj/routers/api.py index 88148d78..663dc99a 100644 --- a/src/khoj/routers/api.py +++ b/src/khoj/routers/api.py @@ -32,6 +32,7 @@ from khoj.routers.helpers import ( ApiUserRateLimiter, CommonQueryParams, ConversationCommandRateLimiter, + create_automation, update_telemetry_state, ) from khoj.search_filter.date_filter import DateFilter @@ -447,3 +448,42 @@ def delete_automation(request: Request, automation_id: str) -> Response: # Return deleted automation information as a JSON response return Response(content=json.dumps(automation_info), media_type="application/json", status_code=200) + + +@api.post("/automation", response_class=Response) +@requires(["authenticated"]) +async def make_automation( + request: Request, + q: str, + city: Optional[str] = None, + region: Optional[str] = None, + country: Optional[str] = None, + timezone: Optional[str] = None, +) -> Response: + user: KhojUser = request.user.object + if city or region or country: + location = LocationData(city=city, region=region, country=country) + + # Create automation with scheduling query and location data + try: + automation, crontime, query_to_run, subject = await create_automation(q, location, timezone, user, request.url) + except Exception as e: + logger.error(f"Error creating automation {q} for {user.email}: {e}") + return Response( + content=f"Unable to create automation. Ensure the automation doesn't already exist.", + media_type="text/plain", + status_code=500, + ) + + # Collate info about the created user automation + schedule = f'{cron_descriptor.get_description(crontime)} {automation.next_run_time.strftime("%Z")}' + automation_info = { + "id": automation.id, + "subject": subject, + "query_to_run": query_to_run, + "scheduling_request": crontime, + "schedule": schedule, + "next": automation.next_run_time.strftime("%Y-%m-%d %I:%M %p %Z"), + } + # Return information about the created automation as a JSON response + return Response(content=json.dumps(automation_info), media_type="application/json", status_code=200)