Files
platform/src/servers/api/tasks/tasks.ts
T
pastilhasandClaude Opus 4.6 f32f427972 script-mode task execution — database-backed tasks with direct script runner
Tasks now live in the database (mode: script or agentic). Script-mode tasks
bypass the agent entirely — the implementation is materialized to a temp file
and executed directly, with stdout/stderr streamed to the UI via WebSocket.

Includes convert-to-mp3 as the first native script task.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-08 16:07:59 +00:00

93 lines
2.6 KiB
TypeScript

import { createRouter } from '../../create-router';
import { getTasksForUser, getTaskByDirName, getTaskById, createTask, updateTask, deleteTask } from 'officerdb';
type TriggerConfig = { type: 'file'; extensions: string[] } | { type: 'directory' };
function isPrivileged(role: string) {
return role === 'Super Admin';
}
export const tasksRouter = createRouter();
tasksRouter.get('/', async (ctx) => {
const user = ctx.get('user');
const rows = await getTasksForUser(user.id);
const tasks = rows.map((row) => ({
id: row.id,
dirName: row.dirName,
name: row.name,
description: row.description,
scope: row.scope,
triggers: (row.trigger as TriggerConfig[]) ?? [],
mode: row.mode ?? 'agentic',
userId: row.userId,
}));
return ctx.json(tasks);
});
tasksRouter.get('/:name', async (ctx) => {
const user = ctx.get('user');
const name = ctx.req.param('name');
const task = await getTaskByDirName(name, user.id);
if (!task) return ctx.text('Not found', 404);
return ctx.json({
id: task.id,
dirName: task.dirName,
name: task.name,
description: task.description,
scope: task.scope,
mode: task.mode,
language: task.language,
body: task.body,
implementation: task.implementation,
inputs: task.inputs,
args: task.args,
trigger: task.trigger,
version: task.version,
userId: task.userId,
});
});
tasksRouter.post('/', async (ctx) => {
const user = ctx.get('user');
const body = await ctx.req.json<{ name: string; description?: string; mode?: string; language?: string }>();
if (!body.name?.trim()) return ctx.text('Name is required', 400);
const dirName = body.name.trim().toLowerCase().replace(/\s+/g, '-').replace(/[^a-z0-9-]/g, '');
if (!dirName) return ctx.text('Invalid name', 400);
const scope = isPrivileged(user.role) ? 'global' : 'user';
const task = await createTask({
scope,
userId: user.id,
dirName,
name: body.name.trim(),
description: body.description ?? null,
mode: body.mode ?? 'agentic',
language: body.language ?? null,
});
return ctx.json(task);
});
tasksRouter.delete('/:name', async (ctx) => {
const user = ctx.get('user');
const name = ctx.req.param('name');
const task = await getTaskByDirName(name, user.id);
if (!task) return ctx.text('Not found', 404);
// Only owner or Super Admin can delete
if (task.scope === 'native') return ctx.text('Cannot delete native tasks', 403);
if (task.userId !== user.id && !isPrivileged(user.role)) return ctx.text('Forbidden', 403);
await deleteTask(task.id);
return ctx.json({ ok: true });
});