mirror of
https://github.com/khoj-ai/khoj.git
synced 2024-11-27 17:35:07 +01:00
Allow creating automations from automation settings section in web ui
- Create new POST API endpoint to create automations - Use it in the settings page on the web interface to create new automations This simplified managing automations from the setting page by allowing both delete and create from the same page
This commit is contained in:
parent
2f9241b5a3
commit
23f2057868
2 changed files with 64 additions and 2 deletions
|
@ -295,8 +295,8 @@
|
|||
<tbody id="automations-list"></tbody>
|
||||
</table>
|
||||
<div class="card-action-row">
|
||||
<button class="card-button happy" id="create-automation" onclick="createAutomation()">
|
||||
Create Task
|
||||
<button class="card-button happy" id="create-automation">
|
||||
Create Automation
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -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')
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue