Skip to content

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

Merge remote-tracking branch 'upstream/main' into feat/department-access

Azad Jagtap7b43d49c9674
diff --cc scripts/verify-team-bot-access.ts
index 84833fd9c4,d843a2bad4..a77d08253f
--- a/scripts/verify-team-bot-access.ts
+++ b/scripts/verify-team-bot-access.ts
@@@ -112,9 -115,13 +115,13 @@@ try
return answer.body;
};
const payroll = (await as("ada", "POST", "/api/bots", { name: "Payroll", title: "Answers pay questions", color: "teal" })).bot as { id: string; threadId: string };
- await as("ada", "POST", "/api/bots", { name: "Helpdesk", title: "IT questions", color: "blue" });
+ const helpdesk = (await as("ada", "POST", "/api/bots", { name: "Helpdesk", title: "IT questions", color: "blue" })).bot as { id: string };
if (before) await as("ada", "PATCH", `/api/bots/${payroll.id}`, { visibility: { people: [PEOPLE.bob.email, PEOPLE.cara.email, PEOPLE.dan.email] } });
else await as("ada", "PUT", `/api/bots/${payroll.id}/access`, { everyone: "none", people: { [PEOPLE.bob.email]: "view", [PEOPLE.cara.email]: "chat", [PEOPLE.dan.email]: "edit" } });
+ const sales = (await as("ada", "POST", "/api/bots", { name: "Sales notes", title: "Keeps the deal notes", color: "orange" })).bot as { id: string };
+ await as("ada", "PUT", `/api/bots/${sales.id}/access`, { everyone: "view", people: { [PEOPLE.bob.email]: "edit" } });
+ const recruiting = (await as("ada", "POST", "/api/bots", { name: "Recruiting", title: "Screens applicants", color: "purple" })).bot as { id: string };
+ await as("ada", "PUT", `/api/bots/${recruiting.id}/access`, { everyone: "none", people: { [PEOPLE.bob.email]: "view", [PEOPLE.cara.email]: "chat", [PEOPLE.dan.email]: "chat", [PEOPLE.eve.email]: "chat" } });
await as("ada", "POST", `/api/bots/${payroll.id}/messages`, { text: "When does payroll run this month?" });
for (let i = 0; i < 150; i++) {
const bots = (await as("ada", "GET", "/api/bots")).bots as Array<{ id: string; busy?: boolean }>;
@@@ -225,30 -276,40 +276,62 @@@
await openPayroll();
await page.until("!document.querySelector('[data-testid=view-only-composer]') && Boolean(document.querySelector('textarea'))", "Bob's composer");
} },
+ { name: "share-dialog-many-people", who: "ada", query: "mode=simple", act: async () => {
+ await crowd();
+ await page.open(`${ui!.previewUrl}?mode=simple`);
+ await openShare();
+ } },
+ { name: "share-dialog-many-people-390", who: "ada", query: "mode=simple", size: PHONE, act: async () => { await crowd(); await openShare(); } },
+ { name: "overview-many-bots", who: "ada", query: "mode=simple", act: async () => { await crowd(); await openSettings(); } },
+ { name: "overview-many-bots-390", who: "ada", query: "mode=simple", size: PHONE, act: async () => { await crowd(); await openSettings(); } },
+ { name: "share-dialog-made-owner", who: "ada", query: "mode=simple", act: async () => {
+ await openSettings();
+ await page.click(`[data-access-row="${recruiting.id}"]`, "the Recruiting row");
+ await page.until("Boolean(document.querySelector('[data-share-bot]'))", "the Share dialog");
+ await openMenu("What Bob can do");
+ await page.click('[role=menu] [data-menu-key="owner"]', "Make owner");
+ await page.until("Boolean(document.querySelector('[data-share-make-owner]'))", "the confirmation");
+ await page.evaluate("[...document.querySelectorAll('[data-share-make-owner] button')].find((entry) => entry.textContent === 'Make owner').click(); true");
+ await page.until("!document.querySelector('[data-share-make-owner]') && document.querySelector('[data-share-person]')?.getAttribute('data-share-person') === '[email protected]' && Boolean(document.querySelector('[data-share-person=\"[email protected]\"] [aria-haspopup=menu]'))", "Bob as owner, Ada still able to edit");
+ } },
+ { name: "overview-search-many-people", who: "ada", query: "mode=simple", act: async () => {
+ await crowd(); await openSettings();
+ await page.evaluate(`(() => { const field = document.querySelector("[data-access-search]"); Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value").set.call(field, "payroll"); field.dispatchEvent(new Event("input", { bubbles: true })); return true; })()`);
+ await page.until("document.querySelectorAll('[data-access-row]').length === 1", "the search");
+ { name: "department-access-preview", who: "ada", query: "mode=simple", act: async () => {
+ for (const id of [payroll.id, helpdesk.id]) await as("ada", "PATCH", `/api/bots/${id}`, { section: "Finance" });
+ await page.click('[aria-label="Actions for Finance department"]', "Finance's menu");
+ await page.until(`(() => { const item = [...document.querySelectorAll('[role=menuitem]')].find((entry) => entry.textContent.includes("Set for all bots")); item?.click(); return Boolean(item); })()`, "Set for all bots");
+ await page.until(`(() => { const tab = [...document.querySelectorAll('[data-testid=team-settings-dialog] [role=tab]')].find((entry) => entry.textContent.trim() === "Access"); tab?.click(); return Boolean(tab); })()`, "the Access tab");
+ const pick = (label: string, value: string) => page.evaluate(`(() => { const field = document.querySelector('[data-testid=team-settings-dialog] select[aria-label="${label}"]'); field.value = ${JSON.stringify(value)}; field.dispatchEvent(new Event("change", { bubbles: true })); return true; })()`);
+ await pick("Everyone in Acme", "view");
+ await pick("A person", PEOPLE.eve.email);
+ await page.until("Boolean(document.querySelector('[data-testid=team-settings-rows]'))", "the preview");
+ } },
+ { name: "department-access-applied", who: "ada", query: "mode=simple", act: async () => {
+ for (const id of [payroll.id, helpdesk.id]) await as("ada", "PATCH", `/api/bots/${id}`, { section: "Finance" });
+ await page.click('[aria-label="Actions for Finance department"]', "Finance's menu");
+ await page.until(`(() => { const item = [...document.querySelectorAll('[role=menuitem]')].find((entry) => entry.textContent.includes("Set for all bots")); item?.click(); return Boolean(item); })()`, "Set for all bots");
+ await page.until(`(() => { const tab = [...document.querySelectorAll('[data-testid=team-settings-dialog] [role=tab]')].find((entry) => entry.textContent.trim() === "Access"); tab?.click(); return Boolean(tab); })()`, "the Access tab");
+ const pick = (label: string, value: string) => page.evaluate(`(() => { const field = document.querySelector('[data-testid=team-settings-dialog] select[aria-label="${label}"]'); field.value = ${JSON.stringify(value)}; field.dispatchEvent(new Event("change", { bubbles: true })); return true; })()`);
+ await pick("Everyone in Acme", "view");
+ await pick("A person", PEOPLE.eve.email);
+ await page.until(`(() => { const apply = [...document.querySelectorAll('[data-testid=team-settings-dialog] button')].find((entry) => entry.textContent.startsWith("Apply to")); apply?.click(); return Boolean(apply); })()`, "Apply");
+ await page.until("Boolean(document.querySelector('[data-testid=team-settings-dialog] [role=status]')?.textContent.includes('applied'))", "the results");
+ const shared = ((await as("ada", "GET", "/api/bots")).bots as Array<{ id: string; access?: { everyone: string; people: Record<string, string> } }>).filter((bot) => bot.id === payroll.id || bot.id === helpdesk.id);
+ if (shared.length !== 2 || !shared.every((bot) => bot.access?.everyone === "view" && bot.access.people[PEOPLE.eve.email] === "chat")) throw new Error(`department access not applied: ${JSON.stringify(shared)}`);
} },
];
+ let crowded = false;
+ /** Ten people on Payroll and fifty bots, once. */
+ async function crowd() {
+ if (crowded) return;
+ crowded = true;
+ const added = Object.fromEntries(CROWD.map((person) => [person.email, "chat" as const]));
+ for (const person of CROWD) await team.pair(person);
+ await as("ada", "PUT", `/api/bots/${payroll.id}/access`, { everyone: "none", people: { [PEOPLE.bob.email]: "view", [PEOPLE.cara.email]: "chat", [PEOPLE.dan.email]: "edit", [PEOPLE.eve.email]: "view", ...added } });
+ for (let i = 1; i <= 46; i++) await as("ada", "POST", "/api/bots", { name: `Bot ${String(i).padStart(2, "0")} with quite a long name for the list`, title: "Filler", color: "teal" });
+ }
let serving: Who | null = null;
for (const shot of SHOTS.filter((entry) => !only || only.has(entry.name))) {
if (serving !== shot.who) {
diff --cc src/components/TeamSettingsDialog.tsx
index 626ead33a4,edd040b3d3..8ec918d322
--- a/src/components/TeamSettingsDialog.tsx
+++ b/src/components/TeamSettingsDialog.tsx
@@@ -108,15 -87,7 +108,16 @@@ export function TeamSettingsDialog({ te
const outcome = await applyTeamPlan(plan, async (bot, patch) => {
const saved = await persistBotUpdate(bot.id, patch, new AbortController().signal, api, desktopApprovalBridge(), bot);
dispatch({ type: "botPatched", bot: saved });
- }, { acknowledgeLocalAuto, onProgress: setProgress });
+ }, {
+ acknowledgeLocalAuto, onProgress: setProgress,
+ share: async (bot) => {
+ const latest = latestBots.current.find((one) => one.id === bot.id) ?? bot;
+ const row = change?.kind === "access" ? planAccess(latest, change) : null;
+ if (row?.status === "skip") throw new Error(row.reason);
++ if (row?.status === "unchanged") return "unchanged";
+ if (row?.status === "apply" && "access" in row) dispatch({ type: "botPatched", bot: await saveAccess(bot.id, row.access) });
+ },
+ });
setResults(outcome);
setRunning(false);
};
diff --cc src/lib/team-bulk-settings.test.ts
index ebfe3cd85a,ef2633a153..29ee2c3689
--- a/src/lib/team-bulk-settings.test.ts
+++ b/src/lib/team-bulk-settings.test.ts
@@@ -169,49 -176,3 +177,50 @@@ describe("team-wide settings", () =>
expect(renderToStaticMarkup(createElement(TeamMenuItems, { ...base, onSetForAll: vi.fn() }))).toContain("Set for all bots in this department…");
});
});
+
+describe("who can use a department's bots", () => {
+ const ADA = "[email protected]", BOB = "[email protected]", CARA = "[email protected]";
+ const shared = (id: string, access: Bot["access"], fields: Partial<Bot> = {}) => bot(id, { access, yourAccess: "edit", reportsTo: ADA, ...fields });
+
+ it("sets everyone's level and one person's on each bot, keeping who else was added", () => {
+ const payroll = shared("payroll", { everyone: "none", people: { [CARA]: "edit" } });
+ const ledger = shared("ledger", { everyone: "chat", people: { [BOB]: "chat" } });
+ const plan = planTeamChange([payroll, ledger], instances, { kind: "access", everyone: "chat", person: { email: BOB, level: "chat" } });
+ expect(plan).toEqual([
+ { bot: payroll, status: "apply", access: { everyone: "chat", people: { [CARA]: "edit", [BOB]: "chat" } } },
+ { bot: ledger, status: "unchanged" },
+ ]);
+ expect(planTeamChange([ledger], instances, { kind: "access", person: { email: BOB, level: null } })).toEqual([
+ { bot: ledger, status: "apply", access: { everyone: "chat", people: {} } },
+ ]);
+ });
+
+ it("skips someone's own MausBot and a bot this person can't share, and never lists a bot's owner", () => {
+ const own = shared("mine", { everyone: "none", people: {} }, { defaultBot: { person: BOB } } as Partial<Bot>);
+ const viewOnly = shared("atlas", { everyone: "none", people: {} }, { yourAccess: "chat" });
+ const owned = shared("owned", { everyone: "none", people: {} });
+ const plan = planTeamChange([own, viewOnly, owned], instances, { kind: "access", person: { email: ADA, level: "chat" } });
+ expect(plan.map((row) => row.status === "skip" ? row.reason : row.status)).toEqual(["it's someone's own MausBot", "you can't change who can use it", "unchanged"]);
+ });
+
+ it("plans again from the bot's latest sharing, so a grant another admin removed after the preview stays removed", () => {
+ const previewed = shared("payroll", { everyone: "none", people: { [CARA]: "chat" } });
+ const latest = shared("payroll", { everyone: "none", people: {} });
+ const change = { kind: "access" as const, everyone: "view" as const };
+ expect(planTeamChange([previewed], instances, change)[0]).toMatchObject({ access: { everyone: "view", people: { [CARA]: "chat" } } });
+ expect(planAccess(latest, change)).toEqual({ bot: latest, status: "apply", access: { everyone: "view", people: {} } });
+ });
+
+ it("saves each bot's sharing through its own request, one at a time, and never as a settings patch", async () => {
+ const payroll = shared("payroll", { everyone: "none", people: {} });
+ const plan = planTeamChange([payroll], instances, { kind: "access", everyone: "view" });
+ const persist = vi.fn(async () => undefined);
+ const share = vi.fn(async () => undefined);
+ expect(planNeedsLocalAutoConsent(plan)).toBe(false);
+ expect(await applyTeamPlan(plan, persist, { share })).toEqual([{ bot: payroll, status: "applied" }]);
+ expect(share).toHaveBeenCalledWith(payroll, { everyone: "view", people: {} });
+ expect(persist).not.toHaveBeenCalled();
++ expect(await applyTeamPlan(plan, persist, { share: async () => "unchanged" }), "already set by the time it was sent").toEqual([{ bot: payroll, status: "unchanged" }]);
+ });
+});
+
diff --cc src/lib/team-bulk-settings.ts
index 1a2b481a61,e039bbc892..3cba8b67cc
--- a/src/lib/team-bulk-settings.ts
+++ b/src/lib/team-bulk-settings.ts
@@@ -127,20 -109,18 +127,21 @@@ export function planNeedsLocalAutoConse
export async function applyTeamPlan(
plan: readonly TeamPlanRow[],
persist: (bot: Bot, patch: BotUpdatePatch) => Promise<unknown>,
- options: { acknowledgeLocalAuto?: boolean; onProgress?: (done: number) => void; share?: (bot: Bot, access: BotAccess) => Promise<unknown> } = {},
- options: { acknowledgeLocalAuto?: boolean; onProgress?: (done: number) => void } = {},
++ options: { acknowledgeLocalAuto?: boolean; onProgress?: (done: number) => void; share?: (bot: Bot, access: BotAccess) => Promise<"unchanged" | void> } = {},
): Promise<TeamResultRow[]> {
const results: TeamResultRow[] = [];
for (const row of plan) {
if (row.status === "unchanged") results.push({ bot: row.bot, status: "unchanged" });
else if (row.status === "skip") results.push({ bot: row.bot, status: "skipped", reason: row.reason });
else {
- const patch = options.acknowledgeLocalAuto && row.patch.approvalMode === "auto" && row.bot.computer === "local"
+ const patch = "access" in row ? null : options.acknowledgeLocalAuto && row.patch.approvalMode === "auto" && row.bot.computer === "local"
? { ...row.patch, acknowledgeLocalAuto: true } : row.patch;
try {
- await persist(row.bot, patch);
- results.push({ bot: row.bot, status: "applied" });
++ let outcome: "unchanged" | void = undefined;
+ if (patch) await persist(row.bot, patch);
- else if ("access" in row && options.share) await options.share(row.bot, row.access);
++ else if ("access" in row && options.share) outcome = await options.share(row.bot, row.access);
+ else throw new Error(t("teamSettings.skip.cantShare"));
- results.push({ bot: row.bot, status: "applied" });
++ results.push({ bot: row.bot, status: outcome === "unchanged" ? "unchanged" : "applied" });
} catch (cause) {
results.push({ bot: row.bot, status: "skipped", reason: cause instanceof Error ? cause.message : String(cause) });
}