Files
openchamber/packages/ui/src/lib/scheduledTasksApi.ts
T
Bohdan Triapitsyn 4f228f768d feat: add scheduled tasks with locale-aware scheduling and safer desktop quit flow (#920)
* feat: keep desktop app running in background when closing last window

Closing last window hides it instead of quitting — sidecar keeps running
Cmd+Q now shows confirmation dialog warning about stopping background processes
Clicking dock icon reopens hidden window or creates a new one

* docs: add scheduled tasks impl plan

* feat: add scheduled tasks runtime, api, and ui

* feat: conditionally confirm desktop quit on risks

* chore: remove scheduled tasks plan doc

* feat: add scheduled tasks runtime and management UI

Add server-side scheduled task runtime with project-backed config persistence
Add task scheduling UI and API integration for creating and editing schedules
Add tests for runtime scheduling behavior and project config validation

* feat: add locale display preferences for scheduled tasks

Add Appearance settings for time format and week start with settings.json persistence
Apply preferences in scheduled task editor for time display and weekday ordering
Rename Thinking level control and disable it when model variants are unavailable

* feat: improve scheduled tasks editor and sidebar action order

Reorder session sidebar header actions to separate creation and management tools
Polish scheduled tasks dialog layout and controls for clearer editing flow

* feat: polish scheduled task editor usability

Improve scheduled task dialog layout for clearer scheduling controls
Refine time and weekday inputs for more intuitive task configuration
Update editor labels and control states for better model variant guidance

* feat: add prompt autocomplete and command-aware scheduled runs

Add @ and / autocomplete support to task, multi-run, and agent manager prompt fields
Fix agent mention selection so subagents can be inserted from @ suggestions
Run scheduled prompts as commands when they match slash commands, with message fallback
2026-04-16 15:55:08 +03:00

124 lines
3.9 KiB
TypeScript

export type ScheduledTaskStatus = 'idle' | 'running' | 'success' | 'error';
export type ScheduledTask = {
id: string;
name: string;
enabled: boolean;
schedule: {
kind: 'daily' | 'weekly' | 'once' | 'cron';
times?: string[];
time?: string;
date?: string;
weekdays?: number[];
cron?: string;
timezone?: string;
};
execution: {
prompt: string;
providerID: string;
modelID: string;
variant?: string;
agent?: string;
};
state: {
createdAt: number;
updatedAt: number;
lastRunAt?: number;
lastStatus?: ScheduledTaskStatus;
lastError?: string;
lastDurationMs?: number;
lastSessionId?: string;
nextRunAt?: number;
};
};
const parseErrorMessage = async (response: Response, fallback: string) => {
try {
const parsed = await response.json();
if (parsed && typeof parsed.error === 'string' && parsed.error.trim().length > 0) {
return parsed.error;
}
} catch {
return fallback;
}
return fallback;
};
const ensureProjectID = (projectID: string): string => {
const trimmed = typeof projectID === 'string' ? projectID.trim() : '';
if (!trimmed) {
throw new Error('projectId is required');
}
return trimmed;
};
export const fetchScheduledTasks = async (projectID: string): Promise<ScheduledTask[]> => {
const safeProjectID = ensureProjectID(projectID);
const response = await fetch(`/api/projects/${encodeURIComponent(safeProjectID)}/scheduled-tasks`);
if (!response.ok) {
throw new Error(await parseErrorMessage(response, 'Failed to load scheduled tasks'));
}
const parsed = await response.json().catch(() => null);
if (!parsed || !Array.isArray(parsed.tasks)) {
return [];
}
return parsed.tasks as ScheduledTask[];
};
export const upsertScheduledTask = async (projectID: string, task: Partial<ScheduledTask>): Promise<ScheduledTask[]> => {
const safeProjectID = ensureProjectID(projectID);
const response = await fetch(`/api/projects/${encodeURIComponent(safeProjectID)}/scheduled-tasks`, {
method: 'PUT',
headers: {
'content-type': 'application/json',
accept: 'application/json',
},
body: JSON.stringify({ task }),
});
if (!response.ok) {
throw new Error(await parseErrorMessage(response, 'Failed to save scheduled task'));
}
const parsed = await response.json().catch(() => null);
if (!parsed || !Array.isArray(parsed.tasks)) {
return [];
}
return parsed.tasks as ScheduledTask[];
};
export const deleteScheduledTask = async (projectID: string, taskID: string): Promise<ScheduledTask[]> => {
const safeProjectID = ensureProjectID(projectID);
const safeTaskID = ensureProjectID(taskID);
const response = await fetch(`/api/projects/${encodeURIComponent(safeProjectID)}/scheduled-tasks/${encodeURIComponent(safeTaskID)}`, {
method: 'DELETE',
headers: {
accept: 'application/json',
},
});
if (!response.ok) {
throw new Error(await parseErrorMessage(response, 'Failed to delete scheduled task'));
}
const parsed = await response.json().catch(() => null);
if (!parsed || !Array.isArray(parsed.tasks)) {
return [];
}
return parsed.tasks as ScheduledTask[];
};
export const runScheduledTaskNow = async (projectID: string, taskID: string): Promise<{ sessionId?: string }> => {
const safeProjectID = ensureProjectID(projectID);
const safeTaskID = ensureProjectID(taskID);
const response = await fetch(`/api/projects/${encodeURIComponent(safeProjectID)}/scheduled-tasks/${encodeURIComponent(safeTaskID)}/run`, {
method: 'POST',
headers: {
accept: 'application/json',
},
});
if (!response.ok) {
throw new Error(await parseErrorMessage(response, 'Failed to run scheduled task'));
}
const parsed = await response.json().catch(() => null);
return {
sessionId: typeof parsed?.sessionId === 'string' && parsed.sessionId.length > 0 ? parsed.sessionId : undefined,
};
};