Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(sidebar): a member who may not move bots gets no bot drag (#3914)

Milind Soniba0af3ca5d74
diff --git a/server/request-auth.test.ts b/server/request-auth.test.ts
index a7136f08c5..645fad73ba 100644
--- a/server/request-auth.test.ts
+++ b/server/request-auth.test.ts
@@ -199,6 +199,12 @@ describe("scopes", () => {
expect(clientGroupPatchViolation({ memberIds: [] })).toBe("memberIds");
});
+ it("refuses a session without the admin scope a bot's move into or out of a department", () => {
+ expect(clientBotPatchViolation({ section: "Work" }, false, true)).toBe("section");
+ expect(clientBotPatchViolation({ section: "" }, false, true)).toBe("section");
+ for (const method of ["POST", "PUT"]) expect(requiredScope(method, "/api/sidebar-sections"), method).toBe("admin");
+ });
+
it("lets a bot's maker without the admin scope say who it is and how it looks, never what it may do or whom it directs", () => {
const profile = { name: "Scout", title: "Trips", soul: "Plans trips.", color: "teal", avatarCrop: "square", voice: "alloy", modelSelection: { instanceId: "claude", model: "m" } };
expect(adminOnlyBotSetting(profile)).toBeNull();
diff --git a/src/components/BotContextMenu.member.test.ts b/src/components/BotContextMenu.member.test.ts
index 3aa4076163..815b6dbef0 100644
--- a/src/components/BotContextMenu.member.test.ts
+++ b/src/components/BotContextMenu.member.test.ts
@@ -55,7 +55,7 @@ describe("a team bot's actions for a session without the admin scope", () => {
it("keeps every action for an admin", () => {
fixture.admin = true;
const html = render(scout);
- for (const offered of [">Archive</button>", ">Delete</button>", "Duplicate"]) expect(html).toContain(offered);
+ for (const offered of [">Archive</button>", ">Delete</button>", "Duplicate", "Move to department"]) expect(html).toContain(offered);
expect(html).toContain("Choose Claude or an ACP model provider first");
});
});
diff --git a/src/components/Sidebar.departments.test.ts b/src/components/Sidebar.departments.test.ts
index 2c6dbfadd3..5cfc7a67b7 100644
--- a/src/components/Sidebar.departments.test.ts
+++ b/src/components/Sidebar.departments.test.ts
@@ -3,7 +3,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { Action, AppState, Bot, Group } from "@/state/store";
const fixture = vi.hoisted(() => ({
- values: [] as unknown[], index: 0, advanced: false, phone: false,
+ values: [] as unknown[], index: 0, advanced: false, phone: false, admin: null as boolean | null,
state: {} as AppState, dispatch: vi.fn(), storage: new Map<string, string>(),
requests: [] as Array<{ path: string; method: string; body: unknown }>,
}));
@@ -37,6 +37,7 @@ vi.mock("./DesktopCapabilities", async () => {
});
vi.mock("@/lib/thread-preferences", () => ({ useShowThreads: () => fixture.advanced }));
vi.mock("@/lib/interface-mode", () => ({ useAdvancedMode: () => fixture.advanced, setAdvancedMode: () => {} }));
+vi.mock("@/lib/use-owner-or-admin", () => ({ useOwnerOrAdmin: () => fixture.admin }));
vi.mock("@/state/store", async (importOriginal) => {
const original = await importOriginal<typeof import("@/state/store")>();
return { ...original, useStore: () => ({ state: fixture.state, dispatch: fixture.dispatch }) };
@@ -109,6 +110,7 @@ beforeEach(() => {
fixture.values = [];
fixture.advanced = false;
fixture.phone = false;
+ fixture.admin = null;
fixture.storage = new Map();
fixture.requests = [];
rowElements = [];
@@ -291,6 +293,37 @@ describe("dragging a bot", () => {
});
});
+describe("who may drag a bot: whoever the bot menu offers Move to department", () => {
+ it("gives a Team Cloud member without the admin scope no row to drag and no Alt+Up/Down, and starts no drag", () => {
+ const drawnEarlier = row("atlas").botDrag!;
+ fixture.admin = false;
+ expect(listed()).toEqual(["atlas", "bea", "cleo", "dodo", "echo"]);
+ expect(rows().map((props) => props.botDrag)).toEqual([undefined, undefined, undefined, undefined, undefined]);
+ carried = transfer();
+ drawnEarlier.row("atlas").onDragStart(at() as never);
+ drawnEarlier.step("atlas", 1);
+ expect(carried.types).toEqual([]);
+ expect(actions()).toEqual([]);
+ });
+
+ it("drops nothing from a drag begun before the session said they are a member", () => {
+ drag("atlas");
+ fixture.admin = false;
+ section(userSectionId("Work")).onDragOver(at() as never);
+ section(userSectionId("Work")).onDrop(at() as never);
+ expect(actions()).toEqual([]);
+ expect(fixture.requests).toEqual([]);
+ });
+
+ it("lets an admin drag", () => {
+ fixture.admin = true;
+ for (const props of rows()) expect(props.botDrag!.row(props.bot.id).draggable).toBe(true);
+ drag("atlas");
+ section(userSectionId("Work")).onDrop(at() as never);
+ expect(actions()).toEqual([{ type: "updateBot", botId: "atlas", patch: { section: "Work" } }, { type: "botsOrdered", botIds: ["dodo", "echo", "atlas"] }]);
+ });
+});
+
describe("a department's menu in Simple", () => {
const openMenu = (name: string) => {
const button = { getBoundingClientRect: () => ({ right: 280, bottom: 300 }), focus: vi.fn(), isConnected: true } as unknown as HTMLButtonElement;
diff --git a/src/components/SidebarBotDrag.tsx b/src/components/SidebarBotDrag.tsx
index 3cd768aaf4..601838eaa5 100644
--- a/src/components/SidebarBotDrag.tsx
+++ b/src/components/SidebarBotDrag.tsx
@@ -5,7 +5,7 @@ import { nextRename } from "@/lib/rename";
import { BOTS_SECTION_ID, userSectionName } from "@/lib/sidebar-layout";
import { BOT_ROW_DRAG_TYPE, botDropPlace, botListOf, planBotDrop, sameBotDrop, stepBot, unusedName, type BotDrop, type BotDropPlace, type BotMove } from "@/lib/sidebar-bot-drop";
import type { Action, Bot } from "@/state/store";
-import { moveBotsToSection, renameDepartment, saveBotOrder } from "./bot-sections";
+import { moveBotsToSection, renameDepartment, saveBotOrder, useMayMoveBots } from "./bot-sections";
/** The department being named, before it exists. The server trims names and never keeps a control character. */
export const NEW_DEPARTMENT = "\u0000new";
@@ -48,6 +48,7 @@ export function useSidebarBotDrag({ dispatch, announce, onNamed, onRenamed, onEr
const [dragging, setDragging] = useState<string | null>(null);
const [drop, setDrop] = useState<BotDrop | null>(null);
const [naming, setNaming] = useState<Naming | null>(null);
+ const mayMove = useMayMoveBots();
const latest = useRef<Arranged>({ enabled: false, lists: new Map(), bots: [], taken: [] });
const moving = useRef<{ botId: string | null; drop: BotDrop | null }>({ botId: null, drop: null });
const controller = useRef<BotRowDrag | null>(null);
@@ -78,7 +79,7 @@ export function useSidebarBotDrag({ dispatch, announce, onNamed, onRenamed, onEr
const finish = (target: BotDrop | null) => {
const botId = moving.current.botId;
end();
- const move = botId && target ? planBotDrop(latest.current.lists, botId, target) : null;
+ const move = botId && target && latest.current.enabled ? planBotDrop(latest.current.lists, botId, target) : null;
if (botId && move) apply(botId, move, false);
};
const rowDrop = (event: DragEvent<HTMLElement>, botId: string): BotDrop => {
@@ -138,10 +139,11 @@ export function useSidebarBotDrag({ dispatch, announce, onNamed, onRenamed, onEr
arrange({ enabled, main, sectioned, departments, bots, taken }: {
enabled: boolean; main: Bot[]; sectioned: Bot[]; departments: string[]; bots: Bot[]; taken: string[];
}) {
+ const movable = enabled && mayMove;
const lists = new Map<string, string[]>([["", main.map((bot) => bot.id)]]);
for (const name of departments) if (name !== NEW_DEPARTMENT) lists.set(name, sectioned.filter((bot) => bot.section === name).map((bot) => bot.id));
- latest.current = { enabled, lists, bots, taken };
- const active = enabled && dragging !== null && botListOf(lists, dragging) !== undefined ? dragging : null;
+ latest.current = { enabled: movable, lists, bots, taken };
+ const active = movable && dragging !== null && botListOf(lists, dragging) !== undefined ? dragging : null;
const shown = active ? drop : null;
const mainIds = lists.get("")!.filter((id) => id !== active);
const listFor = (sectionId: string) => (sectionId === BOTS_SECTION_ID ? "" : userSectionName(sectionId));
@@ -156,7 +158,7 @@ export function useSidebarBotDrag({ dispatch, announce, onNamed, onRenamed, onEr
};
return {
row: (botId: string): { botDrag?: BotRowDrag; botDrop?: BotDropPlace | null; botDragging?: boolean } => {
- if (!enabled || botListOf(lists, botId) === undefined) return {};
+ if (!movable || botListOf(lists, botId) === undefined) return {};
const place = shown?.kind === "row" && shown.botId === botId ? shown.place
: shown?.kind === "end" && shown.list === "" && botId === mainIds.at(-1) ? "after" : null;
return { botDrag: controller.current!, botDrop: place, botDragging: active === botId };
diff --git a/src/components/SidebarBotMenu.tsx b/src/components/SidebarBotMenu.tsx
index 420d0df093..4362f73f08 100644
--- a/src/components/SidebarBotMenu.tsx
+++ b/src/components/SidebarBotMenu.tsx
@@ -20,7 +20,7 @@ import { submenuIntent, type SubmenuIntent } from "@/lib/submenu-intent";
import { useAdvancedMode } from "@/lib/interface-mode";
import { useMoveToTeam } from "./MoveToTeam";
import type { MenuState } from "./Sidebar";
-import { departmentNames, moveBotsToSection } from "./bot-sections";
+import { departmentNames, moveBotsToSection, useMayMoveBots } from "./bot-sections";
type Flyout = "more" | "move";
@@ -68,6 +68,7 @@ export function BotContextMenu({
const showThreads = useShowThreads();
const admin = useOwnerOrAdmin();
const member = admin === false;
+ const mayMove = useMayMoveBots();
const clearThreads = useDeleteAllThreads(simpleMenu), moveToTeam = useMoveToTeam({ blocked: (bot) => (bot.defaultBot ? t("teamMove.blockedDefault", { name: bot.name }) : bot.chiefOfStaff ? t("teamMove.blockedChief") : state.bots.filter((candidate) => !candidate.hidden).length <= 1 ? t("teamMove.blockedLast") : undefined) });
const remoteClient = window.ogb?.remoteClient?.active === true;
const bot = shown ? state.bots.find((b) => b.id === shown.botId) : undefined;
@@ -236,7 +237,7 @@ export function BotContextMenu({
<ChevronRight size={15} aria-hidden="true" className="text-ink-secondary rtl:-scale-x-100" />
</button>
);
- const moveToSection = member ? null : flyoutButton("move", moveRef, <FolderPlus size={16} className="text-ink-secondary" />, t("sidebar.bot.moveToSection"));
+ const moveToSection = mayMove ? flyoutButton("move", moveRef, <FolderPlus size={16} className="text-ink-secondary" />, t("sidebar.bot.moveToSection")) : null;
const rename = onRename && item(<SquarePen size={16} className="text-ink-secondary" />, t("sidebar.bot.rename"), () => onRename(bot.id));
const editProfile = item(<Pencil size={16} className="text-ink-secondary" />, t("sidebar.bot.simple.editProfile"), () => {
openForSettings();
@@ -361,7 +362,7 @@ export function BotContextMenu({
{divider("threads")}
{team && mayChangeOwner(bot, team) && item(<Users size={16} className="text-ink-secondary" />, ownerMenuLabel(bot, team), () => pickOwnerOf([bot], team, shown))}
{remoteClient ? [
- !member && item(<FolderPlus size={16} className="text-ink-secondary" />, t("sidebar.bot.moveToSection"), () => {
+ mayMove && item(<FolderPlus size={16} className="text-ink-secondary" />, t("sidebar.bot.moveToSection"), () => {
onClose();
onMoveToSection(bot.id);
}),
@@ -379,7 +380,7 @@ export function BotContextMenu({
() => dispatch({ type: "updateBot", botId: bot.id, patch: { pinned: !bot.pinned } }),
),
chiefItem(),
- !member && item(<FolderPlus size={16} className="text-ink-secondary" />, t("sidebar.bot.moveToSection"), () => {
+ mayMove && item(<FolderPlus size={16} className="text-ink-secondary" />, t("sidebar.bot.moveToSection"), () => {
onClose();
onMoveToSection(bot.id);
}),
diff --git a/src/components/bot-sections.ts b/src/components/bot-sections.ts
index 2fd30b4e31..4f29e7afea 100644
--- a/src/components/bot-sections.ts
+++ b/src/components/bot-sections.ts
@@ -1,8 +1,15 @@
import type { Dispatch } from "react";
+import { useOwnerOrAdmin } from "@/lib/use-owner-or-admin";
import { api, type Action, type AppState, type Bot } from "@/state/store";
const failure = (cause: unknown) => (cause instanceof Error ? cause.message : String(cause));
+/** Whether this person may move bots, from a menu or by dragging: into or out of a department, or to another place in
+ * a list. */
+export function useMayMoveBots(): boolean {
+ return useOwnerOrAdmin() !== false;
+}
+
/** Every department in use, in the order the workspace lists them. */
export function departmentNames(state: Pick<AppState, "sections" | "bots" | "groups">): string[] {
return [...new Set([