feat(dashboard): add scheduled task actions (#31)
This commit is contained in:
@@ -1,10 +1,12 @@
|
|||||||
import { useEffect, useMemo, useState, type ReactNode } from 'react';
|
import { useEffect, useMemo, useState, type ReactNode } from 'react';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
type DashboardTaskAction,
|
||||||
type DashboardOverview,
|
type DashboardOverview,
|
||||||
type DashboardTask,
|
type DashboardTask,
|
||||||
type StatusSnapshot,
|
type StatusSnapshot,
|
||||||
fetchDashboardData,
|
fetchDashboardData,
|
||||||
|
runScheduledTaskAction,
|
||||||
} from './api';
|
} from './api';
|
||||||
import {
|
import {
|
||||||
LOCALES,
|
LOCALES,
|
||||||
@@ -32,6 +34,7 @@ type UsageLimitWindow = 'h5' | 'd7';
|
|||||||
type DashboardView = 'usage' | 'inbox' | 'health' | 'rooms' | 'scheduled';
|
type DashboardView = 'usage' | 'inbox' | 'health' | 'rooms' | 'scheduled';
|
||||||
type TaskGroupKey = 'watchers' | 'scheduled' | 'paused' | 'completed';
|
type TaskGroupKey = 'watchers' | 'scheduled' | 'paused' | 'completed';
|
||||||
type TaskResultTone = 'ok' | 'fail' | 'none';
|
type TaskResultTone = 'ok' | 'fail' | 'none';
|
||||||
|
type TaskActionKey = `${string}:${DashboardTaskAction}`;
|
||||||
type InboxFilter = 'all' | InboxItem['kind'];
|
type InboxFilter = 'all' | InboxItem['kind'];
|
||||||
type HealthLevel = 'ok' | 'stale' | 'down';
|
type HealthLevel = 'ok' | 'stale' | 'down';
|
||||||
|
|
||||||
@@ -278,6 +281,12 @@ function taskDisplayName(task: DashboardTask, t: Messages): string {
|
|||||||
return task.id;
|
return task.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function taskActionsFor(task: DashboardTask): DashboardTaskAction[] {
|
||||||
|
if (task.status === 'active') return ['pause', 'cancel'];
|
||||||
|
if (task.status === 'paused') return ['resume', 'cancel'];
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
const INBOX_FILTERS: InboxFilter[] = [
|
const INBOX_FILTERS: InboxFilter[] = [
|
||||||
'all',
|
'all',
|
||||||
'ci-failure',
|
'ci-failure',
|
||||||
@@ -1089,10 +1098,14 @@ function UsagePanel({
|
|||||||
function TaskPanel({
|
function TaskPanel({
|
||||||
tasks,
|
tasks,
|
||||||
locale,
|
locale,
|
||||||
|
onTaskAction,
|
||||||
|
taskActionKey,
|
||||||
t,
|
t,
|
||||||
}: {
|
}: {
|
||||||
tasks: DashboardTask[];
|
tasks: DashboardTask[];
|
||||||
locale: Locale;
|
locale: Locale;
|
||||||
|
onTaskAction: (task: DashboardTask, action: DashboardTaskAction) => void;
|
||||||
|
taskActionKey: TaskActionKey | null;
|
||||||
t: Messages;
|
t: Messages;
|
||||||
}) {
|
}) {
|
||||||
const taskGroups = useMemo(() => {
|
const taskGroups = useMemo(() => {
|
||||||
@@ -1155,6 +1168,7 @@ function TaskPanel({
|
|||||||
task.lastResult,
|
task.lastResult,
|
||||||
t.tasks.noResult,
|
t.tasks.noResult,
|
||||||
);
|
);
|
||||||
|
const taskActions = taskActionsFor(task);
|
||||||
return (
|
return (
|
||||||
<article
|
<article
|
||||||
className={`task-card task-card-${group.key}`}
|
className={`task-card task-card-${group.key}`}
|
||||||
@@ -1177,6 +1191,28 @@ function TaskPanel({
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{taskActions.length > 0 ? (
|
||||||
|
<div className="task-actions">
|
||||||
|
{taskActions.map((action) => {
|
||||||
|
const actionKey: TaskActionKey = `${task.id}:${action}`;
|
||||||
|
const busy = taskActionKey === actionKey;
|
||||||
|
return (
|
||||||
|
<button
|
||||||
|
className={`task-action task-action-${action}`}
|
||||||
|
disabled={busy}
|
||||||
|
key={action}
|
||||||
|
onClick={() => onTaskAction(task, action)}
|
||||||
|
type="button"
|
||||||
|
>
|
||||||
|
{busy
|
||||||
|
? t.tasks.actions.busy
|
||||||
|
: t.tasks.actions[action]}
|
||||||
|
</button>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
) : null}
|
||||||
|
|
||||||
<div className="task-time-grid">
|
<div className="task-time-grid">
|
||||||
<span>
|
<span>
|
||||||
<small>{t.tasks.next}</small>
|
<small>{t.tasks.next}</small>
|
||||||
@@ -1278,6 +1314,9 @@ function App() {
|
|||||||
const [drawerOpen, setDrawerOpen] = useState(false);
|
const [drawerOpen, setDrawerOpen] = useState(false);
|
||||||
const [activeView, setActiveView] = useState<DashboardView>(readViewFromHash);
|
const [activeView, setActiveView] = useState<DashboardView>(readViewFromHash);
|
||||||
const [locale, setLocale] = useState<Locale>(readInitialLocale);
|
const [locale, setLocale] = useState<Locale>(readInitialLocale);
|
||||||
|
const [taskActionKey, setTaskActionKey] = useState<TaskActionKey | null>(
|
||||||
|
null,
|
||||||
|
);
|
||||||
const t = messages[locale];
|
const t = messages[locale];
|
||||||
|
|
||||||
function setDashboardLocale(nextLocale: Locale) {
|
function setDashboardLocale(nextLocale: Locale) {
|
||||||
@@ -1306,6 +1345,26 @@ function App() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function handleTaskAction(
|
||||||
|
task: DashboardTask,
|
||||||
|
action: DashboardTaskAction,
|
||||||
|
) {
|
||||||
|
if (action === 'cancel' && !window.confirm(t.tasks.actions.confirmCancel)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const actionKey: TaskActionKey = `${task.id}:${action}`;
|
||||||
|
setTaskActionKey(actionKey);
|
||||||
|
try {
|
||||||
|
await runScheduledTaskAction(task.id, action);
|
||||||
|
await refresh(false);
|
||||||
|
} catch (err) {
|
||||||
|
setError(err instanceof Error ? err.message : String(err));
|
||||||
|
} finally {
|
||||||
|
setTaskActionKey(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
document.documentElement.lang = localeTags[locale];
|
document.documentElement.lang = localeTags[locale];
|
||||||
}, [locale]);
|
}, [locale]);
|
||||||
@@ -1430,7 +1489,15 @@ function App() {
|
|||||||
<h2>{t.panels.scheduled}</h2>
|
<h2>{t.panels.scheduled}</h2>
|
||||||
<span>{t.panels.promptPreviews}</span>
|
<span>{t.panels.promptPreviews}</span>
|
||||||
</div>
|
</div>
|
||||||
<TaskPanel locale={locale} tasks={data.tasks} t={t} />
|
<TaskPanel
|
||||||
|
locale={locale}
|
||||||
|
onTaskAction={(task, action) =>
|
||||||
|
void handleTaskAction(task, action)
|
||||||
|
}
|
||||||
|
taskActionKey={taskActionKey}
|
||||||
|
tasks={data.tasks}
|
||||||
|
t={t}
|
||||||
|
/>
|
||||||
</section>
|
</section>
|
||||||
) : null}
|
) : null}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -100,6 +100,8 @@ export interface DashboardTask {
|
|||||||
isWatcher: boolean;
|
isWatcher: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type DashboardTaskAction = 'pause' | 'resume' | 'cancel';
|
||||||
|
|
||||||
async function fetchJson<T>(path: string): Promise<T> {
|
async function fetchJson<T>(path: string): Promise<T> {
|
||||||
const response = await fetch(path, {
|
const response = await fetch(path, {
|
||||||
headers: { accept: 'application/json' },
|
headers: { accept: 'application/json' },
|
||||||
@@ -110,6 +112,28 @@ async function fetchJson<T>(path: string): Promise<T> {
|
|||||||
return (await response.json()) as T;
|
return (await response.json()) as T;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function postJson<T>(path: string, body: unknown): Promise<T> {
|
||||||
|
const response = await fetch(path, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
accept: 'application/json',
|
||||||
|
'content-type': 'application/json',
|
||||||
|
},
|
||||||
|
body: JSON.stringify(body),
|
||||||
|
});
|
||||||
|
if (!response.ok) {
|
||||||
|
let message = `${path} failed: ${response.status}`;
|
||||||
|
try {
|
||||||
|
const payload = (await response.json()) as { error?: string };
|
||||||
|
if (payload.error) message = payload.error;
|
||||||
|
} catch {
|
||||||
|
// Keep the status-based message when the server body is not JSON.
|
||||||
|
}
|
||||||
|
throw new Error(message);
|
||||||
|
}
|
||||||
|
return (await response.json()) as T;
|
||||||
|
}
|
||||||
|
|
||||||
export async function fetchDashboardData(): Promise<{
|
export async function fetchDashboardData(): Promise<{
|
||||||
overview: DashboardOverview;
|
overview: DashboardOverview;
|
||||||
snapshots: StatusSnapshot[];
|
snapshots: StatusSnapshot[];
|
||||||
@@ -123,3 +147,17 @@ export async function fetchDashboardData(): Promise<{
|
|||||||
|
|
||||||
return { overview, snapshots, tasks };
|
return { overview, snapshots, tasks };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function runScheduledTaskAction(
|
||||||
|
taskId: string,
|
||||||
|
action: DashboardTaskAction,
|
||||||
|
): Promise<{
|
||||||
|
ok: true;
|
||||||
|
id?: string;
|
||||||
|
deleted?: boolean;
|
||||||
|
task?: DashboardTask | null;
|
||||||
|
}> {
|
||||||
|
return postJson(`/api/tasks/${encodeURIComponent(taskId)}/actions`, {
|
||||||
|
action,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|||||||
@@ -177,6 +177,13 @@ export interface Messages {
|
|||||||
noResult: string;
|
noResult: string;
|
||||||
noTime: string;
|
noTime: string;
|
||||||
now: string;
|
now: string;
|
||||||
|
actions: {
|
||||||
|
pause: string;
|
||||||
|
resume: string;
|
||||||
|
cancel: string;
|
||||||
|
busy: string;
|
||||||
|
confirmCancel: string;
|
||||||
|
};
|
||||||
};
|
};
|
||||||
status: {
|
status: {
|
||||||
processing: string;
|
processing: string;
|
||||||
@@ -386,6 +393,13 @@ export const messages = {
|
|||||||
noResult: '결과 없음',
|
noResult: '결과 없음',
|
||||||
noTime: '-',
|
noTime: '-',
|
||||||
now: '지금',
|
now: '지금',
|
||||||
|
actions: {
|
||||||
|
pause: 'Pause',
|
||||||
|
resume: 'Resume',
|
||||||
|
cancel: 'Cancel',
|
||||||
|
busy: 'Working',
|
||||||
|
confirmCancel: 'Cancel this scheduled work?',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
status: {
|
status: {
|
||||||
processing: '처리중',
|
processing: '처리중',
|
||||||
@@ -579,6 +593,13 @@ export const messages = {
|
|||||||
noResult: 'no result',
|
noResult: 'no result',
|
||||||
noTime: '-',
|
noTime: '-',
|
||||||
now: 'now',
|
now: 'now',
|
||||||
|
actions: {
|
||||||
|
pause: 'Pause',
|
||||||
|
resume: 'Resume',
|
||||||
|
cancel: 'Cancel',
|
||||||
|
busy: 'Working',
|
||||||
|
confirmCancel: 'Cancel this scheduled work?',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
status: {
|
status: {
|
||||||
processing: 'processing',
|
processing: 'processing',
|
||||||
@@ -772,6 +793,13 @@ export const messages = {
|
|||||||
noResult: '无结果',
|
noResult: '无结果',
|
||||||
noTime: '-',
|
noTime: '-',
|
||||||
now: '现在',
|
now: '现在',
|
||||||
|
actions: {
|
||||||
|
pause: 'Pause',
|
||||||
|
resume: 'Resume',
|
||||||
|
cancel: 'Cancel',
|
||||||
|
busy: 'Working',
|
||||||
|
confirmCancel: 'Cancel this scheduled work?',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
status: {
|
status: {
|
||||||
processing: '处理中',
|
processing: '处理中',
|
||||||
@@ -965,6 +993,13 @@ export const messages = {
|
|||||||
noResult: '結果なし',
|
noResult: '結果なし',
|
||||||
noTime: '-',
|
noTime: '-',
|
||||||
now: '今',
|
now: '今',
|
||||||
|
actions: {
|
||||||
|
pause: 'Pause',
|
||||||
|
resume: 'Resume',
|
||||||
|
cancel: 'Cancel',
|
||||||
|
busy: 'Working',
|
||||||
|
confirmCancel: 'Cancel this scheduled work?',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
status: {
|
status: {
|
||||||
processing: '処理中',
|
processing: '処理中',
|
||||||
|
|||||||
@@ -1198,6 +1198,43 @@ progress::-moz-progress-bar {
|
|||||||
background: rgba(43, 55, 38, 0.08);
|
background: rgba(43, 55, 38, 0.08);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.task-actions {
|
||||||
|
display: flex;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
gap: 6px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.task-action {
|
||||||
|
min-height: 34px;
|
||||||
|
padding: 0 12px;
|
||||||
|
border: 1px solid rgba(43, 55, 38, 0.12);
|
||||||
|
border-radius: 999px;
|
||||||
|
color: var(--ink);
|
||||||
|
font: inherit;
|
||||||
|
font-size: 12px;
|
||||||
|
font-weight: 900;
|
||||||
|
background: rgba(255, 250, 240, 0.84);
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.task-action:hover,
|
||||||
|
.task-action:focus-visible {
|
||||||
|
border-color: rgba(191, 95, 44, 0.42);
|
||||||
|
outline: none;
|
||||||
|
box-shadow: 0 0 0 3px rgba(191, 95, 44, 0.12);
|
||||||
|
}
|
||||||
|
|
||||||
|
.task-action:disabled {
|
||||||
|
cursor: progress;
|
||||||
|
opacity: 0.55;
|
||||||
|
}
|
||||||
|
|
||||||
|
.task-action-cancel {
|
||||||
|
color: #8f2f22;
|
||||||
|
border-color: rgba(183, 71, 52, 0.22);
|
||||||
|
background: rgba(183, 71, 52, 0.08);
|
||||||
|
}
|
||||||
|
|
||||||
.task-time-grid {
|
.task-time-grid {
|
||||||
display: grid;
|
display: grid;
|
||||||
grid-template-columns: repeat(3, minmax(0, 1fr));
|
grid-template-columns: repeat(3, minmax(0, 1fr));
|
||||||
@@ -1668,6 +1705,11 @@ progress::-moz-progress-bar {
|
|||||||
padding: 9px;
|
padding: 9px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.task-action {
|
||||||
|
min-height: 32px;
|
||||||
|
padding-inline: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
.task-card-main {
|
.task-card-main {
|
||||||
gap: 7px;
|
gap: 7px;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -233,6 +233,117 @@ describe('web dashboard server handler', () => {
|
|||||||
expect(ciFailure?.summary).not.toContain('plain-secret-value');
|
expect(ciFailure?.summary).not.toContain('plain-secret-value');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('mutates scheduled tasks through explicit action endpoints', async () => {
|
||||||
|
let task: ScheduledTask | undefined = makeTask({
|
||||||
|
id: 'ci-watch-1',
|
||||||
|
prompt: '[BACKGROUND CI WATCH]\nwatch',
|
||||||
|
});
|
||||||
|
const updates: Array<Partial<ScheduledTask>> = [];
|
||||||
|
const deleted: string[] = [];
|
||||||
|
const handler = createWebDashboardHandler({
|
||||||
|
readStatusSnapshots: () => [],
|
||||||
|
getTasks: () => (task ? [task] : []),
|
||||||
|
getTaskById: (id) => (task?.id === id ? task : undefined),
|
||||||
|
updateTask: (id, update) => {
|
||||||
|
expect(id).toBe('ci-watch-1');
|
||||||
|
updates.push(update);
|
||||||
|
task = task ? { ...task, ...update } : task;
|
||||||
|
},
|
||||||
|
deleteTask: (id) => {
|
||||||
|
deleted.push(id);
|
||||||
|
task = undefined;
|
||||||
|
},
|
||||||
|
getPairedTasks: () => [],
|
||||||
|
});
|
||||||
|
|
||||||
|
const pause = await handler(
|
||||||
|
new Request('http://localhost/api/tasks/ci-watch-1/actions', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'content-type': 'application/json' },
|
||||||
|
body: JSON.stringify({ action: 'pause' }),
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
expect(pause.status).toBe(200);
|
||||||
|
await expect(pause.json()).resolves.toMatchObject({
|
||||||
|
ok: true,
|
||||||
|
task: { id: 'ci-watch-1', status: 'paused' },
|
||||||
|
});
|
||||||
|
expect(updates.at(-1)).toMatchObject({
|
||||||
|
status: 'paused',
|
||||||
|
suspended_until: null,
|
||||||
|
});
|
||||||
|
|
||||||
|
const resume = await handler(
|
||||||
|
new Request('http://localhost/api/tasks/ci-watch-1/actions', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'content-type': 'application/json' },
|
||||||
|
body: JSON.stringify({ action: 'resume' }),
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
expect(resume.status).toBe(200);
|
||||||
|
await expect(resume.json()).resolves.toMatchObject({
|
||||||
|
ok: true,
|
||||||
|
task: { id: 'ci-watch-1', status: 'active' },
|
||||||
|
});
|
||||||
|
expect(updates.at(-1)).toMatchObject({
|
||||||
|
status: 'active',
|
||||||
|
suspended_until: null,
|
||||||
|
});
|
||||||
|
|
||||||
|
const cancel = await handler(
|
||||||
|
new Request('http://localhost/api/tasks/ci-watch-1/actions', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'content-type': 'application/json' },
|
||||||
|
body: JSON.stringify({ action: 'cancel' }),
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
expect(cancel.status).toBe(200);
|
||||||
|
await expect(cancel.json()).resolves.toEqual({
|
||||||
|
ok: true,
|
||||||
|
id: 'ci-watch-1',
|
||||||
|
deleted: true,
|
||||||
|
});
|
||||||
|
expect(deleted).toEqual(['ci-watch-1']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('rejects invalid scheduled task actions', async () => {
|
||||||
|
const handler = createWebDashboardHandler({
|
||||||
|
readStatusSnapshots: () => [],
|
||||||
|
getTasks: () => [],
|
||||||
|
getTaskById: (id) =>
|
||||||
|
id === 'task-1' ? makeTask({ id: 'task-1' }) : undefined,
|
||||||
|
updateTask: () => {
|
||||||
|
throw new Error('updateTask should not be called');
|
||||||
|
},
|
||||||
|
getPairedTasks: () => [],
|
||||||
|
});
|
||||||
|
|
||||||
|
const invalid = await handler(
|
||||||
|
new Request('http://localhost/api/tasks/task-1/actions', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'content-type': 'application/json' },
|
||||||
|
body: JSON.stringify({ action: 'restart-everything' }),
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
expect(invalid.status).toBe(400);
|
||||||
|
|
||||||
|
const missing = await handler(
|
||||||
|
new Request('http://localhost/api/tasks/missing/actions', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'content-type': 'application/json' },
|
||||||
|
body: JSON.stringify({ action: 'pause' }),
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
expect(missing.status).toBe(404);
|
||||||
|
|
||||||
|
const wrongMethod = await handler(
|
||||||
|
new Request('http://localhost/api/tasks/task-1/actions', {
|
||||||
|
method: 'GET',
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
expect(wrongMethod.status).toBe(405);
|
||||||
|
});
|
||||||
|
|
||||||
it('serves Vite static assets and falls back to index for SPA routes', async () => {
|
it('serves Vite static assets and falls back to index for SPA routes', async () => {
|
||||||
const staticDir = fs.mkdtempSync(
|
const staticDir = fs.mkdtempSync(
|
||||||
path.join(os.tmpdir(), 'ejclaw-dashboard-'),
|
path.join(os.tmpdir(), 'ejclaw-dashboard-'),
|
||||||
|
|||||||
@@ -2,7 +2,13 @@ import fs from 'fs';
|
|||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
|
||||||
import { WEB_DASHBOARD } from './config.js';
|
import { WEB_DASHBOARD } from './config.js';
|
||||||
import { getAllOpenPairedTasks, getAllTasks } from './db.js';
|
import {
|
||||||
|
deleteTask,
|
||||||
|
getAllOpenPairedTasks,
|
||||||
|
getAllTasks,
|
||||||
|
getTaskById,
|
||||||
|
updateTask,
|
||||||
|
} from './db.js';
|
||||||
import { logger } from './logger.js';
|
import { logger } from './logger.js';
|
||||||
import {
|
import {
|
||||||
readStatusSnapshots,
|
readStatusSnapshots,
|
||||||
@@ -21,6 +27,12 @@ export interface WebDashboardHandlerOptions {
|
|||||||
statusMaxAgeMs?: number;
|
statusMaxAgeMs?: number;
|
||||||
readStatusSnapshots?: (maxAgeMs: number) => StatusSnapshot[];
|
readStatusSnapshots?: (maxAgeMs: number) => StatusSnapshot[];
|
||||||
getTasks?: () => ScheduledTask[];
|
getTasks?: () => ScheduledTask[];
|
||||||
|
getTaskById?: (id: string) => ScheduledTask | undefined;
|
||||||
|
updateTask?: (
|
||||||
|
id: string,
|
||||||
|
updates: Partial<Pick<ScheduledTask, 'status' | 'suspended_until'>>,
|
||||||
|
) => void;
|
||||||
|
deleteTask?: (id: string) => void;
|
||||||
getPairedTasks?: () => PairedTask[];
|
getPairedTasks?: () => PairedTask[];
|
||||||
now?: () => string;
|
now?: () => string;
|
||||||
}
|
}
|
||||||
@@ -101,16 +113,84 @@ function serveStaticFile(staticDir: string, pathname: string): Response {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type TaskAction = 'pause' | 'resume' | 'cancel';
|
||||||
|
|
||||||
|
function parseTaskActionPath(pathname: string): string | null {
|
||||||
|
const match = pathname.match(/^\/api\/tasks\/([^/]+)\/actions$/);
|
||||||
|
if (!match) return null;
|
||||||
|
try {
|
||||||
|
return decodeURIComponent(match[1]);
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function isTaskAction(value: unknown): value is TaskAction {
|
||||||
|
return value === 'pause' || value === 'resume' || value === 'cancel';
|
||||||
|
}
|
||||||
|
|
||||||
|
async function readTaskAction(request: Request): Promise<TaskAction | null> {
|
||||||
|
try {
|
||||||
|
const body = (await request.json()) as { action?: unknown };
|
||||||
|
return isTaskAction(body.action) ? body.action : null;
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function createWebDashboardHandler(
|
export function createWebDashboardHandler(
|
||||||
opts: WebDashboardHandlerOptions = {},
|
opts: WebDashboardHandlerOptions = {},
|
||||||
): (request: Request) => Response | Promise<Response> {
|
): (request: Request) => Response | Promise<Response> {
|
||||||
const readSnapshots = opts.readStatusSnapshots ?? readStatusSnapshots;
|
const readSnapshots = opts.readStatusSnapshots ?? readStatusSnapshots;
|
||||||
const loadTasks = opts.getTasks ?? getAllTasks;
|
const loadTasks = opts.getTasks ?? getAllTasks;
|
||||||
|
const loadTaskById = opts.getTaskById ?? getTaskById;
|
||||||
|
const mutateTask = opts.updateTask ?? updateTask;
|
||||||
|
const removeTask = opts.deleteTask ?? deleteTask;
|
||||||
const loadPairedTasks = opts.getPairedTasks ?? getAllOpenPairedTasks;
|
const loadPairedTasks = opts.getPairedTasks ?? getAllOpenPairedTasks;
|
||||||
const statusMaxAgeMs = opts.statusMaxAgeMs ?? DEFAULT_STATUS_MAX_AGE_MS;
|
const statusMaxAgeMs = opts.statusMaxAgeMs ?? DEFAULT_STATUS_MAX_AGE_MS;
|
||||||
|
|
||||||
return (request: Request): Response => {
|
return async (request: Request): Promise<Response> => {
|
||||||
const url = new URL(request.url);
|
const url = new URL(request.url);
|
||||||
|
const actionTaskId = parseTaskActionPath(url.pathname);
|
||||||
|
|
||||||
|
if (actionTaskId) {
|
||||||
|
if (request.method !== 'POST') {
|
||||||
|
return jsonResponse({ error: 'Method not allowed' }, { status: 405 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const action = await readTaskAction(request);
|
||||||
|
if (!action) {
|
||||||
|
return jsonResponse({ error: 'Invalid task action' }, { status: 400 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const task = loadTaskById(actionTaskId);
|
||||||
|
if (!task) {
|
||||||
|
return jsonResponse({ error: 'Task not found' }, { status: 404 });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (task.status === 'completed' && action !== 'cancel') {
|
||||||
|
return jsonResponse(
|
||||||
|
{ error: 'Completed tasks cannot be changed' },
|
||||||
|
{ status: 409 },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (action === 'cancel') {
|
||||||
|
removeTask(actionTaskId);
|
||||||
|
return jsonResponse({ ok: true, id: actionTaskId, deleted: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
mutateTask(actionTaskId, {
|
||||||
|
status: action === 'pause' ? 'paused' : 'active',
|
||||||
|
suspended_until: null,
|
||||||
|
});
|
||||||
|
|
||||||
|
const updatedTask = loadTaskById(actionTaskId);
|
||||||
|
return jsonResponse({
|
||||||
|
ok: true,
|
||||||
|
task: updatedTask ? sanitizeScheduledTask(updatedTask) : null,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (request.method !== 'GET' && request.method !== 'HEAD') {
|
if (request.method !== 'GET' && request.method !== 'HEAD') {
|
||||||
return jsonResponse({ error: 'Method not allowed' }, { status: 405 });
|
return jsonResponse({ error: 'Method not allowed' }, { status: 405 });
|
||||||
|
|||||||
Reference in New Issue
Block a user