Let workers ask each other through the coordinator in chatrooms
A worker asks for another bot's help with ask_crew. The coordinator reads the request, asks the right bot (in a workspace, only its members), and sends the answer back into the asking bot's conversation, waking it. Each exchange is kept as a read-only chatroom: listed in the main rail after the workspaces, or in the workspace's rail for requests made there, with live status rows for each bot while it's open and a Stop button. Room sessions are hidden from every conversation list, other bots' words reach the model fenced as data, and turns, nesting and open rooms are capped. Closed rooms are pruned after a retention period and past a cap per place (Settings > General). Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
import userAuth from "@/src/functions/backend/auth/user-auth";
|
||||
import grabUserChatrooms from "@/src/functions/backend/db/chatrooms/grab-user-chatrooms";
|
||||
import { AppData } from "@/src/data/app-data";
|
||||
import type { ChatroomSummary } from "@/src/types";
|
||||
import AppError from "@/src/utils/app-error";
|
||||
import safeErrorMessage from "@/src/utils/safe-error-message";
|
||||
import type { APIResponseObject, BunextAPIRouteHandler } from "@moduletrace/bunext/types";
|
||||
|
||||
/**
|
||||
* The user's latest chatrooms for the rail (open ones first) in one place:
|
||||
* a workspace's (`?workspace_id=`), else those asked from bots' own chats.
|
||||
* Loaded by the rail itself so no page carries them.
|
||||
*/
|
||||
export const handler: BunextAPIRouteHandler<APIResponseObject<ChatroomSummary>> = async ({ req, query }) => {
|
||||
try {
|
||||
if (req.method != "GET") {
|
||||
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 };
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
payload: await grabUserChatrooms({
|
||||
user_id: user.id,
|
||||
limit: AppData.ChatroomRailLimit + 1,
|
||||
workspace_id: Number(query?.workspace_id) || null,
|
||||
}),
|
||||
};
|
||||
} catch (error: unknown) {
|
||||
return { success: false, msg: safeErrorMessage({ error }) };
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,39 @@
|
||||
import userAuth from "@/src/functions/backend/auth/user-auth";
|
||||
import stopChatroom from "@/src/functions/backend/db/chatrooms/stop-chatroom";
|
||||
import { ChatroomIdRequestSchema } from "@/src/schemas/bot-schema";
|
||||
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";
|
||||
|
||||
/**
|
||||
* Stops one of the user's open chatrooms: everything in it that could
|
||||
* still run is cancelled and the asking bot is told. The room's new state
|
||||
* goes out live.
|
||||
*/
|
||||
export const handler: BunextAPIRouteHandler<APIResponseObject> = 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 { chatroom_id } = parseWithSchema({ schema: ChatroomIdRequestSchema, data: body });
|
||||
|
||||
const stopped = await stopChatroom({ chatroom_id, user_id: user.id });
|
||||
|
||||
return { success: true, msg: stopped ? `Stopped.` : `Nothing is running.` };
|
||||
} catch (error: unknown) {
|
||||
return { success: false, msg: safeErrorMessage({ error }) };
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,46 @@
|
||||
import grabSuperAdminUser from "@/src/functions/backend/backups/grab-super-admin-user";
|
||||
import grabChatroomRetentionOverview from "@/src/functions/backend/db/app-settings/grab-chatroom-retention";
|
||||
import saveAppSetting from "@/src/functions/backend/db/app-settings/save-app-setting";
|
||||
import logEvent, { grabRequestIP } from "@/src/functions/backend/logger";
|
||||
import { ChatroomKeepDaysSettingKey, ChatroomMaxPerPlaceSettingKey } from "@/src/dict/chatroom-dict";
|
||||
import { SaveChatroomRetentionRequestSchema } from "@/src/schemas/bot-schema";
|
||||
import type { ChatroomRetentionOverview } 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";
|
||||
|
||||
/**
|
||||
* Super admin only: how many days closed chatrooms are kept and how many
|
||||
* rooms one place keeps. Zero puts the code's default back.
|
||||
*/
|
||||
export const handler: BunextAPIRouteHandler<APIResponseObject<ChatroomRetentionOverview>> = async ({ req, body }) => {
|
||||
try {
|
||||
if (req.method != "POST") throw new AppError(`Method not allowed`);
|
||||
|
||||
const user = await grabSuperAdminUser({ req });
|
||||
|
||||
if (!user?.id) throw new AppError(`Only a super admin can change how long chatrooms are kept.`);
|
||||
|
||||
const { keep_days, max_per_place } = parseWithSchema({ schema: SaveChatroomRetentionRequestSchema, data: body });
|
||||
|
||||
await saveAppSetting({ key: ChatroomKeepDaysSettingKey, value: keep_days > 0 ? String(keep_days) : null });
|
||||
await saveAppSetting({
|
||||
key: ChatroomMaxPerPlaceSettingKey,
|
||||
value: max_per_place > 0 ? String(max_per_place) : null,
|
||||
});
|
||||
|
||||
await logEvent({
|
||||
title: "Chatroom retention changed",
|
||||
category: "system",
|
||||
user_id: user.id,
|
||||
plain_text: `User #${user.id} set closed chatrooms to be kept for ${keep_days || "the default number of"} days, ${max_per_place || "the default number of"} rooms per place.`,
|
||||
metadata: { keep_days, max_per_place },
|
||||
ip_address: grabRequestIP(req),
|
||||
});
|
||||
|
||||
return { success: true, singleRes: await grabChatroomRetentionOverview() };
|
||||
} catch (error: unknown) {
|
||||
return { success: false, msg: safeErrorMessage({ error }) };
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user