Improve scheduled task text rendered in UI

This commit is contained in:
Debanjum Singh Solanky 2024-04-26 23:31:40 +05:30
parent 0e01362469
commit 6736551ba3
4 changed files with 24 additions and 8 deletions

View file

@ -280,7 +280,7 @@
<h3 class="card-title">Tasks</h3> <h3 class="card-title">Tasks</h3>
</div> </div>
<div class="card-description-row"> <div class="card-description-row">
<p id="tasks-settings-card-description" class="card-description">Manage your scheduled tasks handled by Khoj</p> <p id="tasks-settings-card-description" class="card-description">Manage your scheduled tasks</p>
</div> </div>
<table id="scheduled-tasks-table"> <table id="scheduled-tasks-table">
<thead> <thead>

View file

@ -3,6 +3,7 @@ import json
import logging import logging
import math import math
import os import os
import re
import time import time
import uuid import uuid
from typing import Any, Callable, List, Optional, Union from typing import Any, Callable, List, Optional, Union
@ -397,7 +398,11 @@ def get_jobs(request: Request) -> Response:
# Collate all tasks assigned by user that are still active # Collate all tasks assigned by user that are still active
tasks_info = [ tasks_info = [
{"id": task.id, "name": task.name, "next": task.next_run_time.strftime("%Y-%m-%d %H:%M")} {
"id": task.id,
"name": re.sub(r"^/task\s*", "", task.name),
"next": task.next_run_time.strftime("%Y-%m-%d %H:%M"),
}
for task in tasks for task in tasks
if task.id.startswith(f"job_{user.uuid}_") if task.id.startswith(f"job_{user.uuid}_")
] ]

View file

@ -3,6 +3,7 @@ import hashlib
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
@ -425,10 +426,11 @@ async def websocket_endpoint(
f"Unable to schedule reminder. Ensure the reminder doesn't already exist." f"Unable to schedule reminder. Ensure the reminder doesn't already exist."
) )
continue continue
unprefixed_inferred_query = re.sub(r"^\/task\s*", "", inferred_query)
next_run_time = job.next_run_time.strftime("%Y-%m-%d %H:%M:%S") next_run_time = job.next_run_time.strftime("%Y-%m-%d %H:%M:%S")
llm_response = f""" llm_response = f"""
### 🕒 Scheduled Job ### 🕒 Scheduled Task
- Query: **"{inferred_query}"** - Query: **"{unprefixed_inferred_query}"**
- Schedule: `{crontime}` - Schedule: `{crontime}`
- Next Run At: **{next_run_time}** UTC. - Next Run At: **{next_run_time}** UTC.
""".strip() """.strip()
@ -689,10 +691,11 @@ async def chat(
status_code=500, status_code=500,
) )
unprefixed_inferred_query = re.sub(r"^\/task\s*", "", inferred_query)
next_run_time = job.next_run_time.strftime("%Y-%m-%d %H:%M:%S") next_run_time = job.next_run_time.strftime("%Y-%m-%d %H:%M:%S")
llm_response = f""" llm_response = f"""
### 🕒 Scheduled Job ### 🕒 Scheduled Task
- Query: **"{inferred_query}"** - Query: **"{unprefixed_inferred_query}"**
- Schedule: `{crontime}` - Schedule: `{crontime}`
- Next Run At: **{next_run_time}** UTC.' - Next Run At: **{next_run_time}** UTC.'
""".strip() """.strip()

View file

@ -3,6 +3,7 @@ import base64
import io import io
import json import json
import logging import logging
import re
from concurrent.futures import ThreadPoolExecutor from concurrent.futures import ThreadPoolExecutor
from datetime import datetime, timedelta, timezone from datetime import datetime, timedelta, timezone
from functools import partial from functools import partial
@ -858,8 +859,15 @@ def should_notify(original_query: str, executed_query: str, ai_response: str) ->
with timer("Chat actor: Decide to notify user of AI response", logger): with timer("Chat actor: Decide to notify user of AI response", logger):
try: try:
response = send_message_to_model_wrapper_sync(to_notify_or_not) response = send_message_to_model_wrapper_sync(to_notify_or_not)
return "no" not in response.lower() should_notify_result = "no" not in response.lower()
logger.info(
f'Decided to {"not " if not should_notify_result else ""}notify user of scheduled task response.'
)
return should_notify_result
except: except:
logger.warning(
f"Fallback to notify user of scheduled task response as failed to infer should notify or not."
)
return True return True
@ -895,7 +903,7 @@ def scheduled_chat(executing_query: str, scheduling_query: str, user: KhojUser,
return None return None
# Extract the AI response from the chat API response # Extract the AI response from the chat API response
cleaned_query = scheduling_query.replace("/task", "", 1).strip() cleaned_query = re.sub(r"^/task\s*", "", scheduling_query).strip()
if raw_response.headers.get("Content-Type") == "application/json": if raw_response.headers.get("Content-Type") == "application/json":
response_map = raw_response.json() response_map = raw_response.json()
ai_response = response_map.get("response") or response_map.get("image") ai_response = response_map.get("response") or response_map.get("image")