Link existing bots from the workspace rail's "+"

The "+" beside "Workspace bots" now opens a modal listing the crew's
workers not in the workspace yet (api/admin/workspaces/linkable-bots),
each added in one click, with "New bot" stacking the bot form over it.
Adding a bot to a workspace now pushes server:bot-created so other
rails pick it up live.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-10-19 20:37:25 +01:00
co-authored by Claude Opus 5.5
parent 139b3e443d
commit bf7109952b
8 changed files with 282 additions and 15 deletions
+14 -1
View File
@@ -1,3 +1,6 @@
import BunSQLite from "@moduletrace/bun-sqlite";
import type { BUN_SQLITE_COBBLE_BOTS } from "@/db/types/db";
import pushBotCreated from "@/src/bots-queue/push-bot-created";
import userAuth from "@/src/functions/backend/auth/user-auth";
import addBotToWorkspace from "@/src/functions/backend/db/workspaces/add-bot-to-workspace";
import grabWorkspaceBots from "@/src/functions/backend/db/workspaces/grab-workspace-bots";
@@ -14,7 +17,8 @@ import type {
/**
* Puts one of the session user's bots in one of their workspaces. Returns
* the workspace's crew.
* the workspace's crew, and pushes the bot to everyone who now sees it
* there.
*/
export const handler: BunextAPIRouteHandler<APIResponseObject<WorkspaceBot>> = async ({ req, body }) => {
try {
@@ -36,6 +40,15 @@ export const handler: BunextAPIRouteHandler<APIResponseObject<WorkspaceBot>> = a
await addBotToWorkspace({ user_id: user.id, workspace_id, bot_id });
const bot = (
await BunSQLite.sql<BUN_SQLITE_COBBLE_BOTS>({
sql: `SELECT * FROM bots WHERE id = ? LIMIT 1`,
values: [bot_id],
})
).payload?.[0];
if (bot) await pushBotCreated({ bot });
return {
success: true,
payload: await grabWorkspaceBots({ user_id: user.id, workspace_id }),
@@ -0,0 +1,58 @@
import userAuth from "@/src/functions/backend/auth/user-auth";
import grabBots from "@/src/functions/backend/db/bots/grab-bots";
import grabUserWorkspace from "@/src/functions/backend/db/workspaces/grab-user-workspace";
import grabWorkspaceBots from "@/src/functions/backend/db/workspaces/grab-workspace-bots";
import type { BotWithLastMessage } from "@/src/types";
import { WorkspaceIdRequestSchema } from "@/src/schemas/workspace-schema";
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 safeErrorMessage from "@/src/utils/safe-error-message";
import type {
APIResponseObject,
BunextAPIRouteHandler,
} from "@moduletrace/bunext/types";
/**
* The crew's workers that aren't in a workspace yet, for the workspace
* rail's "+". Super admins and admins only.
*/
export const handler: BunextAPIRouteHandler<APIResponseObject<BotWithLastMessage>> = 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 (!canManageCrew({ user_types })) {
throw new AppError(`Only admins add bots to workspaces.`);
}
const { workspace_id } = parseWithSchema({ schema: WorkspaceIdRequestSchema, data: body });
const workspace = await grabUserWorkspace({ workspace_id, user_id: user.id });
if (!workspace?.id) {
throw new AppError(`Workspace not found.`);
}
const [bots, members] = await Promise.all([
grabBots({ user_id: user.id }),
grabWorkspaceBots({ user_id: user.id, workspace_id }),
]);
return {
success: true,
payload: bots.filter(
(bot) => bot.kind != "coordinator" && !members.some((member) => member.id == bot.id),
),
};
} catch (error: unknown) {
return { success: false, msg: safeErrorMessage({ error }) };
}
};