Let the coordinator and workspace admins hand over tasks and routines

delegate_task takes a task_id to hand over a task already on a list: it
moves to the worker (a workspace's task stays on its list), goes to
doing, and the job is linked back so its end is noted on the task.
hand_over_schedule moves a bot's own routine to another bot, keeping its
workspace. Users get Hand over in the Tasks UI and both routine lists.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-10-25 11:23:26 +01:00
co-authored by Claude Opus 5.5
parent b4283b607e
commit 93896646c7
36 changed files with 1334 additions and 94 deletions
@@ -0,0 +1,50 @@
import userAuth from "@/src/functions/backend/auth/user-auth";
import handOverCronJob from "@/src/functions/backend/db/cron-jobs/hand-over-cron-job";
import { HandOverCronJobRequestSchema } from "@/src/schemas/cron-job-schema";
import type { CronJobWithNames } from "@/src/types";
import AppError from "@/src/utils/app-error";
import parseWithSchema from "@/src/utils/parse-with-schema";
import isReadOnlyUser from "@/src/utils/rbac/is-read-only-user";
import safeErrorMessage from "@/src/utils/safe-error-message";
import type { APIResponseObject, BunextAPIRouteHandler } from "@moduletrace/bunext/types";
/**
* Hands one of the session user's routines to another of their bots where
* it runs (`handOverCronJob`). Returns the routine.
*/
export const handler: BunextAPIRouteHandler<APIResponseObject<CronJobWithNames>> = async ({ req, body }) => {
try {
if (req.method != "POST") {
throw new AppError(`Method not allowed`);
}
const { user, user_types } = await userAuth({ req });
if (!user?.logged_in_status || !user.id) {
return { success: false, msg: `Unauthorized`, logoutUser: true };
}
if (isReadOnlyUser({ user_types: user_types || [] })) {
throw new AppError(`This user can only read records.`);
}
const { cron_job_id, bot_id } = parseWithSchema({ schema: HandOverCronJobRequestSchema, data: body });
const handed = await handOverCronJob({
user_id: user.id,
cron_job_id,
to_bot_id: bot_id,
handed_by: user.first_name || user.username || "The user",
});
return {
success: true,
singleRes: handed.cron_job,
msg: `${handed.bot_name} runs it now.${
handed.computer_dropped ? ` Its computer isn't one ${handed.bot_name} reaches, so it uses their default.` : ""
}`,
};
} catch (error: unknown) {
return { success: false, msg: safeErrorMessage({ error }) };
}
};
@@ -0,0 +1,41 @@
import userAuth from "@/src/functions/backend/auth/user-auth";
import grabHandOverTargets from "@/src/functions/backend/db/bots/grab-hand-over-targets";
import grabUserCronJob from "@/src/functions/backend/db/cron-jobs/grab-user-cron-job";
import { CronJobIdRequestSchema } from "@/src/schemas/cron-job-schema";
import type { HandOverTarget } from "@/src/types";
import AppError from "@/src/utils/app-error";
import parseWithSchema from "@/src/utils/parse-with-schema";
import safeErrorMessage from "@/src/utils/safe-error-message";
import type { APIResponseObject, BunextAPIRouteHandler } from "@moduletrace/bunext/types";
/**
* Who one of the user's routines can be handed to: the other bots where
* it runs. Loaded when the picker opens.
*/
export const handler: BunextAPIRouteHandler<APIResponseObject<HandOverTarget>> = async ({ req, body }) => {
try {
if (req.method != "POST") {
throw new AppError(`Method not allowed`);
}
const { user } = await userAuth({ req });
if (!user?.logged_in_status || !user.id) {
return { success: false, msg: `Unauthorized`, logoutUser: true };
}
const { cron_job_id } = parseWithSchema({ schema: CronJobIdRequestSchema, data: body });
const cron_job = await grabUserCronJob({ user_id: user.id, cron_job_id });
return {
success: true,
payload: await grabHandOverTargets({
user_id: user.id,
workspace_id: cron_job.workspace_id || null,
except_bot_id: cron_job.bot_id || null,
}),
};
} catch (error: unknown) {
return { success: false, msg: safeErrorMessage({ error }) };
}
};
@@ -0,0 +1,41 @@
import userAuth from "@/src/functions/backend/auth/user-auth";
import grabHandOverTargets from "@/src/functions/backend/db/bots/grab-hand-over-targets";
import assertUserReachesTask from "@/src/functions/backend/db/tasks/assert-user-reaches-task";
import { TaskIdRequestSchema } from "@/src/schemas/task-schema";
import type { HandOverTarget } from "@/src/types";
import AppError from "@/src/utils/app-error";
import parseWithSchema from "@/src/utils/parse-with-schema";
import safeErrorMessage from "@/src/utils/safe-error-message";
import type { APIResponseObject, BunextAPIRouteHandler } from "@moduletrace/bunext/types";
/**
* Who a task can be handed to: the bots in its workspace for a workspace's
* task, else the bots the user chats with. Loaded when the picker opens.
*/
export const handler: BunextAPIRouteHandler<APIResponseObject<HandOverTarget>> = async ({ req, body }) => {
try {
if (req.method != "POST") {
throw new AppError(`Method not allowed`);
}
const { user } = await userAuth({ req });
if (!user?.logged_in_status || !user.id) {
return { success: false, msg: `Unauthorized`, logoutUser: true };
}
const { task_id } = parseWithSchema({ schema: TaskIdRequestSchema, data: body });
const task = await assertUserReachesTask({ user_id: user.id, task_id });
return {
success: true,
payload: await grabHandOverTargets({
user_id: user.id,
workspace_id: task.workspace_id || null,
except_bot_id: task.bot_id || null,
}),
};
} catch (error: unknown) {
return { success: false, msg: safeErrorMessage({ error }) };
}
};
@@ -0,0 +1,63 @@
import userAuth from "@/src/functions/backend/auth/user-auth";
import wakeBotsQueue from "@/src/bots-queue/wake-bots-queue";
import assertUserReachesTask from "@/src/functions/backend/db/tasks/assert-user-reaches-task";
import grabTask from "@/src/functions/backend/db/tasks/grab-task";
import handOverTask from "@/src/functions/backend/db/tasks/hand-over-task";
import { HandOverTaskRequestSchema } from "@/src/schemas/task-schema";
import type { TaskDetail } from "@/src/types";
import AppError from "@/src/utils/app-error";
import parseWithSchema from "@/src/utils/parse-with-schema";
import canManageCrew from "@/src/utils/rbac/can-manage-crew";
import isReadOnlyUser from "@/src/utils/rbac/is-read-only-user";
import safeErrorMessage from "@/src/utils/safe-error-message";
import type { APIResponseObject, BunextAPIRouteHandler } from "@moduletrace/bunext/types";
/**
* Hands an open task to another bot and starts the work (`handOverTask`):
* the bot works that same task, and its result lands in its conversation.
* Crew managers only, like every other change to a task by hand. Returns
* the task as it is now.
*/
export const handler: BunextAPIRouteHandler<APIResponseObject<TaskDetail>> = async ({ req, body }) => {
try {
if (req.method != "POST") {
throw new AppError(`Method not allowed`);
}
const { user, user_types } = await userAuth({ req });
if (!user?.logged_in_status || !user.id) {
return { success: false, msg: `Unauthorized`, logoutUser: true };
}
if (isReadOnlyUser({ user_types: user_types || [] })) {
throw new AppError(`This user can only read records.`);
}
if (!canManageCrew({ user_types: user_types || [] })) {
throw new AppError(`Only an admin can hand a task over.`);
}
const { task_id, bot_id, instructions } = parseWithSchema({ schema: HandOverTaskRequestSchema, data: body });
await assertUserReachesTask({ user_id: user.id, task_id });
const handed = await handOverTask({
user_id: user.id,
task_id,
to_bot_id: bot_id,
instructions,
handed_by: user.first_name || user.username || "The user",
});
wakeBotsQueue();
return {
success: true,
singleRes: await grabTask({ user_id: user.id, task_id }),
msg: `${handed.bot_name} has it now and is starting on it.`,
};
} catch (error: unknown) {
return { success: false, msg: safeErrorMessage({ error }) };
}
};