Spaces:
Paused
Paused
File size: 5,381 Bytes
f7bc25b d6e60a2 00ae9f2 d6e60a2 1ec5621 6ff9f06 9286b99 d6e60a2 1ec5621 d6e60a2 ec5941c d6e60a2 afd821e d6e60a2 6ff9f06 3c970e3 c433f20 3c970e3 d6e60a2 3c970e3 d6e60a2 afd821e d6e60a2 1ec5621 793fe4f 1ec5621 6ff9f06 1ec5621 d6e60a2 9286b99 1ec5621 9286b99 1ec5621 9286b99 00ae9f2 1ec5621 949d7e2 6ff9f06 9286b99 1ec5621 afd821e 1ec5621 6ff9f06 1ec5621 6ff9f06 1ec5621 9286b99 d6e60a2 1ec5621 d6e60a2 7f9e34a 3c970e3 d6e60a2 9286b99 d6e60a2 3c970e3 afd821e e287d47 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
from python.helpers.api import ApiHandler, Request, Response
from agent import AgentContext, AgentContextType
from python.helpers.task_scheduler import TaskScheduler
from python.helpers.localization import Localization
from python.helpers.dotenv import get_dotenv_value
class Poll(ApiHandler):
async def process(self, input: dict, request: Request) -> dict | Response:
ctxid = input.get("context", "")
from_no = input.get("log_from", 0)
notifications_from = input.get("notifications_from", 0)
# Get timezone from input (default to dotenv default or UTC if not provided)
timezone = input.get("timezone", get_dotenv_value("DEFAULT_USER_TIMEZONE", "UTC"))
Localization.get().set_timezone(timezone)
# context instance - get or create only if ctxid is provided
if ctxid:
try:
context = self.use_context(ctxid, create_if_not_exists=False)
except Exception as e:
context = None
else:
context = None
# Get logs only if we have a context
logs = context.log.output(start=from_no) if context else []
# Get notifications from global notification manager
notification_manager = AgentContext.get_notification_manager()
notifications = notification_manager.output(start=notifications_from)
# loop AgentContext._contexts
# Get a task scheduler instance
scheduler = TaskScheduler.get()
# Always reload the scheduler on each poll to ensure we have the latest task state
# await scheduler.reload() # does not seem to be needed
# loop AgentContext._contexts and divide into contexts and tasks
ctxs = []
tasks = []
processed_contexts = set() # Track processed context IDs
all_ctxs = list(AgentContext._contexts.values())
# First, identify all tasks
for ctx in all_ctxs:
# Skip if already processed
if ctx.id in processed_contexts:
continue
# Skip BACKGROUND contexts as they should be invisible to users
if ctx.type == AgentContextType.BACKGROUND:
processed_contexts.add(ctx.id)
continue
# Create the base context data that will be returned
context_data = ctx.output()
context_task = scheduler.get_task_by_uuid(ctx.id)
# Determine if this is a task-dedicated context by checking if a task with this UUID exists
is_task_context = (
context_task is not None and context_task.context_id == ctx.id
)
if not is_task_context:
ctxs.append(context_data)
else:
# If this is a task, get task details from the scheduler
task_details = scheduler.serialize_task(ctx.id)
if task_details:
# Add task details to context_data with the same field names
# as used in scheduler endpoints to maintain UI compatibility
context_data.update({
"task_name": task_details.get("name"), # name is for context, task_name for the task name
"uuid": task_details.get("uuid"),
"state": task_details.get("state"),
"type": task_details.get("type"),
"system_prompt": task_details.get("system_prompt"),
"prompt": task_details.get("prompt"),
"last_run": task_details.get("last_run"),
"last_result": task_details.get("last_result"),
"attachments": task_details.get("attachments", []),
"context_id": task_details.get("context_id"),
})
# Add type-specific fields
if task_details.get("type") == "scheduled":
context_data["schedule"] = task_details.get("schedule")
elif task_details.get("type") == "planned":
context_data["plan"] = task_details.get("plan")
else:
context_data["token"] = task_details.get("token")
tasks.append(context_data)
# Mark as processed
processed_contexts.add(ctx.id)
# Sort tasks and chats by their creation date, descending
ctxs.sort(key=lambda x: x["created_at"], reverse=True)
tasks.sort(key=lambda x: x["created_at"], reverse=True)
# data from this server
return {
"deselect_chat": ctxid and not context,
"context": context.id if context else "",
"contexts": ctxs,
"tasks": tasks,
"logs": logs,
"log_guid": context.log.guid if context else "",
"log_version": len(context.log.updates) if context else 0,
"log_progress": context.log.progress if context else 0,
"log_progress_active": context.log.progress_active if context else False,
"paused": context.paused if context else False,
"notifications": notifications,
"notifications_guid": notification_manager.guid,
"notifications_version": len(notification_manager.updates),
}
|