⭐️⭐️⭐️ Open-source, self-hosted alternative to Linear and Plane. Project management and issue tracking where teams and AI agents work side by side to plan and ship products.
TypeScript agenticagileai-agentsbug-reportingbug-trackerganttissue-trackerjirajira-alternativekanbanlinearlinear-alternativeopen-sourceproduct-managementproject-managementroadmapself-hostedsprint-planningteam-collaborationtrello-alternative
import { Elysia, t } from 'elysia';
import { authContext } from '#shared/auth-context';
import { guards } from '#shared/guards';
import { requireUser } from '#shared/access';
import { noContent } from '#shared/http';
import { HttpError, rethrowDuplicate } from '#shared/lib';
import { accessErrors, commonErrors, errors } from '#shared/responses';
import { mcpTool } from '#mcp/generate';
import { paginate } from '#shared/pagination';
import { nextCronRun } from './cron';
import { checkScheduleOptions, isScheduleType } from './extension';
import {
AgentScheduleResponse,
AgentSchedulePageResponse,
agentSchedulePageQuery,
CanceledRunsResponse,
QueuedRunResponse,
ScheduleRunListResponse,
createScheduleBody,
scheduleParams,
scheduleRunParams,
updateScheduleBody,
} from './model';
import {
cancelPendingScheduleRuns,
createAgentSchedule,
deleteAgentSchedule,
enqueueManualScheduleRun,
getAgentSchedule,
listAgentSchedules,
listScheduleRuns,
newScheduleTrigger,
requiredText,
scheduleTriggerFields,
updateAgentSchedule,
} from './service';
export const agentScheduleRoutes = new Elysia({
name: 'agent-schedules',
detail: { tags: ['Agent Schedules'] },
})
.use(authContext)
.use(guards)
.get(
'/projects/:projectKey/agent-schedules',
({ project, user, query }) =>
paginate(query, (window) => listAgentSchedules(project.id, requireUser(user).id, window)),
{
permission: ['agent_schedules', 'read'],
query: agentSchedulePageQuery,
response: { 200: AgentSchedulePageResponse, ...accessErrors },
detail: {
summary: 'List agent schedules',
description:
"One page of the project's agent schedules with their cron or column, next run, " +
'and last run, newest first.',
...mcpTool('list_agent_schedules'),
},
},
)
.post(
'/projects/:projectKey/agent-schedules',
async ({ project, body, set, user }) => {
const type = body.type ?? 'cron';
if (!isScheduleType(type)) throw new HttpError(400, 'Unknown schedule type');
const trigger = await newScheduleTrigger(project, type, body);
const options = await checkScheduleOptions({
type,
options: body.options ?? {},
projectId: project.id,
});
let row;
try {
row = await createAgentSchedule({
projectId: project.id,
agentId: body.agentId,
actorUserId: requireUser(user).id,
name: requiredText(body.name, 'Name'),
type,
status: body.status ?? 'active',
options,
...trigger,
});
} catch (err) {
rethrowDuplicate(err, 'schedule');
}
if (!row) throw new HttpError(400, 'Select an agent from this project');
set.status = 201;
return row;
},
{
body: createScheduleBody,
permission: ['agent_schedules', 'create'],
response: { 201: AgentScheduleResponse, ...commonErrors, ...errors(409) },
detail: {
summary: 'Create an agent schedule',
description:
'Create a schedule that sends a task to an agent on a cron, on an issue each ' +
'time one enters a column, or on a type this instance adds. It starts active ' +
'unless status is given.',
...mcpTool('create_agent_schedule'),
},
},
)
.patch(
'/projects/:projectKey/agent-schedules/:scheduleId',
async ({ project, params, body, user }) => {
const current = await getAgentSchedule(project.id, params.scheduleId, requireUser(user).id);
if (!current) throw new HttpError(404, 'Schedule not found');
if (!current.supported) {
throw new HttpError(
409,
'This schedule was set up with Cloud or Pro and does not run here; it can only be deleted',
);
}
const trigger = await scheduleTriggerFields(project, current.type, body);
// A resumed cron schedule runs next from now, not from when it was paused.
const resuming = body.status === 'active' && current.status === 'paused';
if (resuming && trigger.nextRunAt === undefined && current.cron !== null) {
trigger.nextRunAt = nextCronRun(current.cron);
}
const options =
body.options === undefined
? undefined
: await checkScheduleOptions({
type: current.type,
options: body.options,
projectId: project.id,
});
let row;
try {
row = await updateAgentSchedule(
project.id,
params.scheduleId,
{
...(body.agentId !== undefined ? { agentId: body.agentId } : {}),
...(body.name !== undefined ? { name: requiredText(body.name, 'Name') } : {}),
...trigger,
...(options !== undefined ? { options } : {}),
...(body.status !== undefined ? { status: body.status } : {}),
},
requireUser(user).id,
);
} catch (err) {
rethrowDuplicate(err, 'schedule');
}
if (!row) throw new HttpError(404, 'Schedule not found');
return row;
},
{
params: scheduleParams,
body: updateScheduleBody,
permission: ['agent_schedules', 'edit'],
response: { 200: AgentScheduleResponse, ...commonErrors, ...errors(409) },
detail: {
summary: 'Update an agent schedule',
description:
"Update a schedule's agent, task, cron or column, options, or status. Its type stays " +
'the one it was created with. A schedule that is not supported here answers 409.',
...mcpTool('update_agent_schedule'),
},
},
)
.delete(
'/projects/:projectKey/agent-schedules/:scheduleId',
async ({ project, params, user }) => {
if (!(await deleteAgentSchedule(project.id, params.scheduleId, requireUser(user).id))) {
throw new HttpError(404, 'Schedule not found');
}
return noContent();
},
{
params: scheduleParams,
permission: ['agent_schedules', 'delete'],
response: { 204: t.Void(), ...commonErrors },
detail: {
summary: 'Delete an agent schedule',
description: 'Delete a schedule with its run history. Irreversible.',
...mcpTool('delete_agent_schedule'),
},
},
)
.post(
'/projects/:projectKey/agent-schedules/:scheduleId/run',
async ({ project, params, set, user }) => {
const runId = await enqueueManualScheduleRun(
project.id,
params.scheduleId,
requireUser(user).id,
);
if (runId == null) throw new HttpError(404, 'Schedule not found');
set.status = 202;
return { runId };
},
{
params: scheduleParams,
permission: ['agent_schedules', 'edit'],
response: { 202: QueuedRunResponse, ...commonErrors },
detail: {
summary: 'Run an agent schedule now',
description:
'Queue a run of the schedule now and return its run id. It runs in the background; ' +
"read the result with list_agent_schedule_runs. Only a 'cron' schedule runs on " +
'demand: the runs of the others work on the issue that started them.',
...mcpTool('run_agent_schedule'),
},
},
)
.post(
'/projects/:projectKey/agent-schedules/:scheduleId/runs/cancel',
async ({ project, params, user }) => {
const canceled = await cancelPendingScheduleRuns(
project.id,
params.scheduleId,
requireUser(user).id,
);
if (canceled == null) throw new HttpError(404, 'Schedule not found');
return { canceled };
},
{
params: scheduleParams,
permission: ['agent_schedules', 'edit'],
response: { 200: CanceledRunsResponse, ...commonErrors },
detail: {
summary: 'End the pending runs of an agent schedule',
description:
'End every run of the schedule that has not started yet, so it is never run. ' +
'A run already being executed, and one that finished, are left as they are.',
...mcpTool('cancel_agent_schedule_runs'),
},
},
)
.post(
'/projects/:projectKey/agent-schedules/:scheduleId/runs/:runId/cancel',
async ({ project, params, user }) => {
const canceled = await cancelPendingScheduleRuns(
project.id,
params.scheduleId,
requireUser(user).id,
params.runId,
);
if (canceled == null) throw new HttpError(404, 'Schedule not found');
if (canceled === 0) throw new HttpError(404, 'Pending run not found');
return { canceled };
},
{
params: scheduleRunParams,
permission: ['agent_schedules', 'edit'],
response: { 200: CanceledRunsResponse, ...commonErrors },
detail: {
summary: 'End one pending run of an agent schedule',
description: 'End a run that has not started yet, so it is never run.',
...mcpTool('cancel_agent_schedule_run'),
},
},
)
.get(
'/projects/:projectKey/agent-schedules/:scheduleId/runs',
async ({ project, params, user }) => {
const rows = await listScheduleRuns(project.id, params.scheduleId, requireUser(user).id);
if (!rows) throw new HttpError(404, 'Schedule not found');
return rows;
},
{
params: scheduleParams,
permission: ['agent_schedules', 'read'],
response: { 200: ScheduleRunListResponse, ...commonErrors },
detail: {
summary: 'List agent schedule runs',
description:
"The schedule's last 50 runs, newest first, with their status, output, and error.",
...mcpTool('list_agent_schedule_runs'),
},
},
);