Skip to content

Agent Zero AI framework

scheduler contexts fix

frdel660eee209ff2
diff --git a/python/api/scheduler_task_create.py b/python/api/scheduler_task_create.py
index 385f858e6..c091b3b19 100644
--- a/python/api/scheduler_task_create.py
+++ b/python/api/scheduler_task_create.py
@@ -24,7 +24,7 @@ class SchedulerTaskCreate(ApiHandler):
# Get common fields from input
name = input.get("name")
- system_prompt = input.get("system_prompt")
+ system_prompt = input.get("system_prompt", "")
prompt = input.get("prompt")
attachments = input.get("attachments", [])
context_id = input.get("context_id", None)
@@ -44,8 +44,9 @@ class SchedulerTaskCreate(ApiHandler):
plan = input.get("plan", {})
# Validate required fields
- if not name or not system_prompt or not prompt:
- return {"error": "Missing required fields: name, system_prompt, prompt"}
+ if not name or not prompt:
+ # return {"error": "Missing required fields: name, system_prompt, prompt"}
+ raise ValueError("Missing required fields: name, system_prompt, prompt")
task = None
if schedule:
@@ -66,9 +67,9 @@ class SchedulerTaskCreate(ApiHandler):
try:
task_schedule = parse_task_schedule(schedule)
except ValueError as e:
- return {"error": str(e)}
+ raise ValueError(str(e))
else:
- return {"error": "Invalid schedule format. Must be string or object."}
+ raise ValueError("Invalid schedule format. Must be string or object.")
task = ScheduledTask.create(
name=name,
diff --git a/python/helpers/task_scheduler.py b/python/helpers/task_scheduler.py
index 2139cae1f..0a87dd88d 100644
--- a/python/helpers/task_scheduler.py
+++ b/python/helpers/task_scheduler.py
@@ -706,10 +706,10 @@ class TaskScheduler:
raise ValueError(f"Task {task.name} has no context ID")
config = initialize()
- context: AgentContext = AgentContext(config)
- context.id = task.context_id
+ context: AgentContext = AgentContext(config, id=task.context_id, name=task.name)
+ # context.id = task.context_id
# initial name before renaming is same as task name
- context.name = task.name
+ # context.name = task.name
# Save the context
save_tmp_chat(context)
@@ -772,7 +772,7 @@ class TaskScheduler:
# Ensure the context is properly registered in the AgentContext._contexts
# This is critical for the polling mechanism to find and stream logs
# Dict operations are atomic
- AgentContext._contexts[context.id] = context
+ # AgentContext._contexts[context.id] = context
agent = context.streaming_agent or context.agent0
# Prepare attachment filenames for logging
diff --git a/webui/index.js b/webui/index.js
index 9f2ddb6a6..605b38ea6 100644
--- a/webui/index.js
+++ b/webui/index.js
@@ -562,24 +562,8 @@ window.killChat = async function (id) {
const chatsAD = Alpine.$data(chatsSection);
console.log("Current contexts before deletion:", JSON.stringify(chatsAD.contexts.map(c => ({ id: c.id, name: c.name }))));
- // Find an alternate chat to switch to if we're deleting the current one
- let alternateChat = null;
- for (let i = 0; i < chatsAD.contexts.length; i++) {
- if (chatsAD.contexts[i].id !== id) {
- alternateChat = chatsAD.contexts[i];
- break;
- }
- }
-
- // If we're deleting the currently selected chat, switch to another one first
- if (context === id) {
- if (alternateChat) {
- setContext(alternateChat.id);
- } else {
- // If no other chats, create a new empty context
- setContext(generateGUID());
- }
- }
+ // switch to another context if deleting current
+ switchFromContext(id);
// Delete the chat on the server
await sendJsonData("/chat_remove", { context: id });
@@ -601,6 +585,29 @@ window.killChat = async function (id) {
}
}
+export function switchFromContext(id){
+ // If we're deleting the currently selected chat, switch to another one first
+ if (context === id) {
+ const chatsAD = Alpine.$data(chatsSection);
+
+ // Find an alternate chat to switch to if we're deleting the current one
+ let alternateChat = null;
+ for (let i = 0; i < chatsAD.contexts.length; i++) {
+ if (chatsAD.contexts[i].id !== id) {
+ alternateChat = chatsAD.contexts[i];
+ break;
+ }
+ }
+
+ if (alternateChat) {
+ setContext(alternateChat.id);
+ } else {
+ // If no other chats, create a new empty context
+ setContext(generateGUID());
+ }
+ }
+}
+
// Function to ensure proper UI state when switching contexts
function ensureProperTabSelection(contextId) {
// Get current active tab
diff --git a/webui/js/scheduler.js b/webui/js/scheduler.js
index 765758f7d..d3af8eb2a 100644
--- a/webui/js/scheduler.js
+++ b/webui/js/scheduler.js
@@ -4,6 +4,7 @@
*/
import { formatDateTime, getUserTimezone } from './time-utils.js';
+import { switchFromContext } from '../index.js';
// Ensure the showToast function is available
// if (typeof window.showToast !== 'function') {
@@ -652,8 +653,9 @@ const fullComponentImplementation = function() {
// Save task (create new or update existing)
async saveTask() {
// Validate task data
- if (!this.editingTask.name.trim()) {
- showToast('Task name is required', 'error');
+ if (!this.editingTask.name.trim() || !this.editingTask.prompt.trim()) {
+ // showToast('Task name and prompt are required', 'error');
+ alert('Task name and prompt are required');
return;
}
@@ -971,6 +973,9 @@ const fullComponentImplementation = function() {
showToast('Task deleted successfully', 'success');
+ // if we deleted selected context, switch to another
+ switchFromContext(taskId);
+
// If we were viewing the detail of the deleted task, close the detail view
if (this.selectedTaskForDetail && this.selectedTaskForDetail.uuid === taskId) {
this.closeTaskDetail();