mirror of
https://github.com/khoaliber/khoj.git
synced 2026-03-07 13:23:15 +00:00
Make, use func for constructing the automation created response
- Dedupe logic across http, ws chat API endpoints - Reduces size of already too long http, ws chat API endpoint funcs
This commit is contained in:
@@ -1,12 +1,10 @@
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import math
|
import math
|
||||||
import re
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import Dict, Optional
|
from typing import Dict, Optional
|
||||||
from urllib.parse import unquote
|
from urllib.parse import unquote
|
||||||
|
|
||||||
import cron_descriptor
|
|
||||||
from asgiref.sync import sync_to_async
|
from asgiref.sync import sync_to_async
|
||||||
from fastapi import APIRouter, Depends, HTTPException, Request, WebSocket
|
from fastapi import APIRouter, Depends, HTTPException, Request, WebSocket
|
||||||
from fastapi.requests import Request
|
from fastapi.requests import Request
|
||||||
@@ -37,6 +35,7 @@ from khoj.routers.helpers import (
|
|||||||
agenerate_chat_response,
|
agenerate_chat_response,
|
||||||
aget_relevant_information_sources,
|
aget_relevant_information_sources,
|
||||||
aget_relevant_output_modes,
|
aget_relevant_output_modes,
|
||||||
|
construct_automation_created_message,
|
||||||
create_automation,
|
create_automation,
|
||||||
get_conversation_command,
|
get_conversation_command,
|
||||||
is_ready_to_chat,
|
is_ready_to_chat,
|
||||||
@@ -401,24 +400,10 @@ async def websocket_endpoint(
|
|||||||
f"Unable to create automation. Ensure the automation doesn't already exist."
|
f"Unable to create automation. Ensure the automation doesn't already exist."
|
||||||
)
|
)
|
||||||
continue
|
continue
|
||||||
# Display next run time in user timezone instead of UTC
|
|
||||||
schedule = f'{cron_descriptor.get_description(crontime)} {automation.next_run_time.strftime("%Z")}'
|
|
||||||
next_run_time = automation.next_run_time.strftime("%Y-%m-%d %I:%M %p %Z")
|
|
||||||
# Remove /automated_task prefix from inferred_query
|
|
||||||
unprefixed_query_to_run = re.sub(r"^\/automated_task\s*", "", query_to_run)
|
|
||||||
# Create the automation response
|
|
||||||
scheme = "http" if not websocket.url.is_secure else "https"
|
|
||||||
automation_icon_url = f"{scheme}://{websocket.url.netloc}/static/assets/icons/automation.svg"
|
|
||||||
llm_response = f"""
|
|
||||||
###  Created Automation
|
|
||||||
- Subject: **{subject}**
|
|
||||||
- Query to Run: "{unprefixed_query_to_run}"
|
|
||||||
- Schedule: `{schedule}`
|
|
||||||
- Next Run At: {next_run_time}
|
|
||||||
|
|
||||||
Manage your tasks [here](/config#automations).
|
|
||||||
""".strip()
|
|
||||||
|
|
||||||
|
llm_response = construct_automation_created_message(
|
||||||
|
automation, crontime, query_to_run, subject, websocket.url
|
||||||
|
)
|
||||||
await sync_to_async(save_to_conversation_log)(
|
await sync_to_async(save_to_conversation_log)(
|
||||||
q,
|
q,
|
||||||
llm_response,
|
llm_response,
|
||||||
@@ -657,24 +642,8 @@ async def chat(
|
|||||||
media_type="text/plain",
|
media_type="text/plain",
|
||||||
status_code=500,
|
status_code=500,
|
||||||
)
|
)
|
||||||
# Display next run time in user timezone instead of UTC
|
|
||||||
schedule = f'{cron_descriptor.get_description(crontime)} {automation.next_run_time.strftime("%Z")}'
|
|
||||||
next_run_time = automation.next_run_time.strftime("%Y-%m-%d %I:%M %p %Z")
|
|
||||||
# Remove /automated_task prefix from inferred_query
|
|
||||||
unprefixed_query_to_run = re.sub(r"^\/automated_task\s*", "", query_to_run)
|
|
||||||
# Create the Automation response
|
|
||||||
scheme = "http" if not request.url.is_secure else "https"
|
|
||||||
automation_icon_url = f"{scheme}://{request.url.netloc}/static/assets/icons/automation.svg"
|
|
||||||
llm_response = f"""
|
|
||||||
###  Created Automation
|
|
||||||
- Subject: **{subject}**
|
|
||||||
- Query to Run: "{unprefixed_query_to_run}"
|
|
||||||
- Schedule: `{schedule}`
|
|
||||||
- Next Run At: {next_run_time}
|
|
||||||
|
|
||||||
Manage your automations [here](/config#automations).
|
|
||||||
""".strip()
|
|
||||||
|
|
||||||
|
llm_response = construct_automation_created_message(automation, crontime, query_to_run, subject, request.url)
|
||||||
await sync_to_async(save_to_conversation_log)(
|
await sync_to_async(save_to_conversation_log)(
|
||||||
q,
|
q,
|
||||||
llm_response,
|
llm_response,
|
||||||
|
|||||||
@@ -21,9 +21,11 @@ from typing import (
|
|||||||
)
|
)
|
||||||
from urllib.parse import parse_qs, urlencode
|
from urllib.parse import parse_qs, urlencode
|
||||||
|
|
||||||
|
import cron_descriptor
|
||||||
import openai
|
import openai
|
||||||
import pytz
|
import pytz
|
||||||
import requests
|
import requests
|
||||||
|
from apscheduler.job import Job
|
||||||
from apscheduler.triggers.cron import CronTrigger
|
from apscheduler.triggers.cron import CronTrigger
|
||||||
from asgiref.sync import sync_to_async
|
from asgiref.sync import sync_to_async
|
||||||
from fastapi import Depends, Header, HTTPException, Request, UploadFile
|
from fastapi import Depends, Header, HTTPException, Request, UploadFile
|
||||||
@@ -948,3 +950,23 @@ async def create_automation(
|
|||||||
jitter=30,
|
jitter=30,
|
||||||
)
|
)
|
||||||
return job, crontime_string, query_to_run, subject
|
return job, crontime_string, query_to_run, subject
|
||||||
|
|
||||||
|
|
||||||
|
def construct_automation_created_message(automation: Job, crontime: str, query_to_run: str, subject: str, url: URL):
|
||||||
|
# Display next run time in user timezone instead of UTC
|
||||||
|
schedule = f'{cron_descriptor.get_description(crontime)} {automation.next_run_time.strftime("%Z")}'
|
||||||
|
next_run_time = automation.next_run_time.strftime("%Y-%m-%d %I:%M %p %Z")
|
||||||
|
# Remove /automated_task prefix from inferred_query
|
||||||
|
unprefixed_query_to_run = re.sub(r"^\/automated_task\s*", "", query_to_run)
|
||||||
|
# Create the automation response
|
||||||
|
scheme = "http" if not url.is_secure else "https"
|
||||||
|
automation_icon_url = f"{scheme}://{url.netloc}/static/assets/icons/automation.svg"
|
||||||
|
return f"""
|
||||||
|
###  Created Automation
|
||||||
|
- Subject: **{subject}**
|
||||||
|
- Query to Run: "{unprefixed_query_to_run}"
|
||||||
|
- Schedule: `{schedule}`
|
||||||
|
- Next Run At: {next_run_time}
|
||||||
|
|
||||||
|
Manage your tasks [here](/config#automations).
|
||||||
|
""".strip()
|
||||||
|
|||||||
Reference in New Issue
Block a user