Skip to content

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

Tag a department in a room with @<department name>

bsisduck158da34ee105
diff --git a/server/comms.test.ts b/server/comms.test.ts
index ea16248e68..b9ebcf330c 100644
--- a/server/comms.test.ts
+++ b/server/comms.test.ts
@@ -76,6 +76,20 @@ describe("roomResponders", () => {
expect(roomResponders("@Milind take this", members, { kind: "member", botId: "atlas" })).toEqual([members[1]]);
});
+ it("routes a department tag to each member of that department", () => {
+ const maya = { id: "maya", name: "Maya", section: "Research" };
+ const theo = { id: "theo", name: "Theo", section: "Research" };
+ const nora = { id: "nora", name: "Nora", section: "Design" };
+ const off = { id: "off", name: "Off", section: "Research", hidden: true };
+ const room = [maya, theo, nora, off, ...members];
+ const lead = { kind: "member", botId: "atlas" } as const;
+ expect(roomResponders("@Research status", room, lead)).toEqual([maya, theo]);
+ expect(roomResponders("@Nora and @research", room, lead)).toEqual([nora, maya, theo]);
+ expect(roomResponders("@Maya and @Research", room, lead)).toEqual([maya, theo]);
+ expect(roomResponders("@General status", room, lead)).toEqual(members);
+ expect(roomResponders("@Researchers status", room, lead)).toEqual([members[0]]);
+ });
+
it("supports everyone and mentions-only room policies", () => {
expect(roomResponders("hello", members, { kind: "everyone" })).toEqual(members);
expect(roomResponders("hello", members, { kind: "mentions" })).toEqual([]);
diff --git a/server/store.ts b/server/store.ts
index f8b33688d9..4c25e3797f 100644
--- a/server/store.ts
+++ b/server/store.ts
@@ -26,7 +26,7 @@ import { lastChangeRequestUrl } from "../shared/thread-link.ts";
import { REMOVED_COMPUTER_DRIVER, type ComputerEngineMove } from "./computer-engine-removal.ts";
import type { TeamSetupRequest, TeamSetupResult } from "../shared/team-setup.ts";
import type { GroupGoalRunCardData } from "../shared/group-goal-run.ts";
-import { isMentionBoundary, isMentionNameContinuation } from "../shared/mention-boundary.ts"; import { resolveMentions } from "../shared/room-mentions.ts";
+import { isMentionBoundary, isMentionNameContinuation } from "../shared/mention-boundary.ts"; import { resolveMentions } from "../shared/room-mentions.ts"; import { departmentMentions } from "../shared/department-mention.ts";
import type { HandedState } from "./delta-context.ts";
import type { AgentPart, PartPair, RoomPart } from "./package-parts.ts";
import type {
@@ -214,7 +214,7 @@ export function normalizeGroupDefaultResponder(
return { kind: "member", botId: memberIds[0] };
}
-export function roomResponders<T extends { id: string; name: string; hidden?: boolean }>(
+export function roomResponders<T extends { id: string; name: string; section?: string; hidden?: boolean }>(
text: string,
members: T[],
defaultResponder: GroupDefaultResponder,
@@ -233,7 +233,8 @@ export function roomResponders<T extends { id: string; name: string; hidden?: bo
}
}
const { bots, ambiguous } = resolveMentions(text, available, picked);
- const mentioned = bots.filter((bot) => !ambiguous.some((sharing) => sharing.includes(bot)));
+ const named = bots.filter((bot) => !ambiguous.some((sharing) => sharing.includes(bot)));
+ const mentioned = [...new Set([...named, ...departmentMentions(text, available)])];
if (mentioned.length) return mentioned;
if (defaultResponder.kind === "everyone") return available;
if (defaultResponder.kind === "member") {
diff --git a/shared/department-mention.test.ts b/shared/department-mention.test.ts
new file mode 100644
index 0000000000..db8f88ab0d
--- /dev/null
+++ b/shared/department-mention.test.ts
@@ -0,0 +1,44 @@
+import { describe, expect, it } from "vitest";
+import { departmentMentions, taggableDepartments } from "./department-mention";
+
+const maya = { id: "maya", name: "Maya", section: "Research" };
+const theo = { id: "theo", name: "Theo", section: " Research " };
+const nora = { id: "nora", name: "Nora", section: "Design" };
+const lead = { id: "lead", name: "Design Lead", section: "Design" };
+const solo = { id: "solo", name: "Solo" };
+const ghost = { id: "ghost", name: "Ghost", section: "Ops", hidden: true };
+const members = [maya, theo, nora, lead, solo, ghost];
+
+describe("department tags", () => {
+ it("lists each department that has a visible member", () => {
+ expect(taggableDepartments(members)).toEqual([
+ { name: "Research", members: [maya, theo] },
+ { name: "Design", members: [nora, lead] },
+ { name: "General", members: [solo] },
+ ]);
+ });
+
+ it("gives a name to the member that has it", () => {
+ const research = { id: "research", name: "research", section: "Design" };
+ expect(taggableDepartments([maya, research]).map((department) => department.name)).toEqual(["Design"]);
+ expect(departmentMentions("@Research status", [maya, research])).toEqual([]);
+ });
+
+ it("reaches each member of a tagged department once, in any case", () => {
+ expect(departmentMentions("@research status, then @Research again", members)).toEqual([maya, theo]);
+ expect(departmentMentions("@Design and @General", members)).toEqual([nora, lead, solo]);
+ expect(departmentMentions("@Ops status", members)).toEqual([]);
+ });
+
+ it("prefers a longer bot name and rejects a longer word", () => {
+ expect(departmentMentions("@Design Lead status", members)).toEqual([]);
+ expect(departmentMentions("@Designers status", members)).toEqual([]);
+ expect(departmentMentions("mail@Design /@Design", members)).toEqual([]);
+ expect(departmentMentions("【@Design】 status", members)).toEqual([nora, lead]);
+ });
+
+ it("reads a name with punctuation in it", () => {
+ const scout = { id: "scout", name: "Scout", section: "AI/ML" };
+ expect(departmentMentions("@AI/ML status", [scout, solo])).toEqual([scout]);
+ });
+});
diff --git a/shared/department-mention.ts b/shared/department-mention.ts
new file mode 100644
index 0000000000..0ecbfc4a80
--- /dev/null
+++ b/shared/department-mention.ts
@@ -0,0 +1,45 @@
+// @<department name>: a room line that tags a department reaches each of its bots in that room. One rule for the server,
+// which routes the line, and the renderer, which draws the tag and lists it.
+import { isMentionBoundary, isMentionNameContinuation } from "./mention-boundary.ts";
+
+type Member = { name: string; section?: string | null; hidden?: boolean };
+
+/** The unnamed department has the name the roster gives it. */
+export const departmentName = (section?: string | null): string => section?.trim() || "General";
+
+/** Each department with a visible member, in member order. A department with the name of a member has no tag: that
+ * name is the member's. */
+export function taggableDepartments<T extends Member>(members: readonly T[]): Array<{ name: string; members: T[] }> {
+ const visible = members.filter((member) => !member.hidden && member.name.trim());
+ const taken = new Set(visible.map((member) => member.name.trim().toLowerCase()));
+ const departments = new Map<string, { name: string; members: T[] }>();
+ for (const member of visible) {
+ const name = departmentName(member.section);
+ const key = name.toLowerCase();
+ if (taken.has(key)) continue;
+ const department = departments.get(key) ?? { name, members: [] };
+ department.members.push(member);
+ departments.set(key, department);
+ }
+ return [...departments.values()];
+}
+
+/** The members of each department the line @-tags, once each. The longest known name wins, so "@Design Lead" is the
+ * bot of that name and not the Design department. */
+export function departmentMentions<T extends Member>(text: string, members: readonly T[]): T[] {
+ const departments = taggableDepartments(members);
+ if (!departments.length) return [];
+ const names = [
+ ...departments.map((department) => ({ name: department.name, members: department.members })),
+ ...members.filter((member) => !member.hidden && member.name.trim()).map((member) => ({ name: member.name, members: [] as T[] })),
+ ].sort((a, b) => b.name.length - a.name.length);
+ const tagged: T[] = [];
+ for (let at = text.indexOf("@"); at !== -1; at = text.indexOf("@", at + 1)) {
+ if (!isMentionBoundary(text, at)) continue;
+ const rest = text.slice(at + 1);
+ const hit = names.find(({ name }) => rest.slice(0, name.length).toLowerCase() === name.toLowerCase()
+ && !isMentionNameContinuation(rest.slice(name.length)));
+ for (const member of hit?.members ?? []) if (!tagged.includes(member)) tagged.push(member);
+ }
+ return tagged;
+}
diff --git a/src/components/Composer.tsx b/src/components/Composer.tsx
index cacefe20c9..4abd992e57 100644
--- a/src/components/Composer.tsx
+++ b/src/components/Composer.tsx
@@ -90,7 +90,7 @@ import {
roomSteersQueued,
} from "./ComposerQueuedMessages";
import { skillAuthoringEnabled } from "@/lib/feature-flags";
-import { mentionChoicesForQuery, mentionQueryAt } from "@/lib/mentions"; import { mentionPicksFor, restoreMentionPicks } from "@/lib/mention-picks";
+import { departmentChoices, mentionChoicesForQuery, mentionQueryAt } from "@/lib/mentions"; import { mentionPicksFor, restoreMentionPicks } from "@/lib/mention-picks";
import { serializeThreadRefs, threadTokenFromPaste, threadTokenSpacing } from "@/lib/thread-refs";
import {
composerSlashTrigger,
@@ -355,7 +355,7 @@ export function Composer({
if (!mention || mention.start === dismissedAt || isPlaceId(bot?.id)) return []; // a My Cloud bot's chat names none of this computer's
const pool: MentionChoice[] = group
? [
- ...(!group.dm ? [{ id: "__everyone__", name: "everyone" }] : []),
+ ...(!group.dm ? [{ id: "__everyone__", name: "everyone" }, ...departmentChoices(members ?? [])] : []),
...(members ?? []).map((member) => ({ id: member.id, name: member.name, bot: member })),
]
: bots
@@ -825,7 +825,7 @@ export function Composer({
<MentionTextarea
wrapperClassName={stacked ? "basis-full" : undefined} ghost={suggestion.text ? <SuggestedReplyGhost text={suggestion.text} onUse={suggestion.accept} /> : undefined}
inputRef={inputRef}
- peers={[...(group ? members ?? [] : bots.filter((member) => member.id !== bot?.id)), ...teammates.peers]}
+ peers={[...(group ? [...(members ?? []), ...(group.dm ? [] : departmentChoices(members ?? []))] : bots.filter((member) => member.id !== bot?.id)), ...teammates.peers]}
everyone={Boolean(group && !group.dm)}
dir="auto"
rows={1}
diff --git a/src/components/RoomTextMessage.tsx b/src/components/RoomTextMessage.tsx
index 065de544da..2af79cee59 100644
--- a/src/components/RoomTextMessage.tsx
+++ b/src/components/RoomTextMessage.tsx
@@ -1,3 +1,4 @@
+import { departmentChoices } from "@/lib/mentions";
import { memo, useEffect, useMemo, useState } from "react";
import { formattedReplyWidth, isFormattedReply, splitReplyBlocks } from "@/lib/formatted-reply";
import { intlLocale, t } from "@/lib/i18n";
@@ -109,7 +110,7 @@ export const RoomTextMessage = memo(function RoomTextMessage({
useEffect(() => { if (focusedSearch && collapsible) setExpanded(true); }, [focusedSearch, collapsible, focusNonce]);
const simple = !useAdvancedMode();
const speakerBot = members.find((member) => member.id === m.from?.botId);
- const botText = rawText; const teammates = useTeammates(); const peers = useMemo(() => [...members, ...teammates.peers], [members, teammates]);
+ const botText = rawText; const teammates = useTeammates(); const peers = useMemo(() => [...members, ...(dm ? [] : departmentChoices(members)), ...teammates.peers], [members, dm, teammates]);
const pin = roomPin({ id: groupId, pinnedMessageId: pinned ? m.id : undefined }, m, dispatch);
const forkAction = !phone && !dm && m.kind === "text" && !m.id.startsWith("optimistic-")
? [forkMessageItem({ kind: "group", ownerId: groupId, threadId, messageId: m.id }, dispatch, working)] : [];
diff --git a/src/lib/group-routing.test.ts b/src/lib/group-routing.test.ts
index d772908255..ac08aa59e5 100644
--- a/src/lib/group-routing.test.ts
+++ b/src/lib/group-routing.test.ts
@@ -50,6 +50,17 @@ describe("roomRespondersForComposer", () => {
expect(roomRespondersForComposer("İ @Milind", members, mentionsOnly)).toEqual([members[1]]);
});
+ it("routes a department tag to each member of that department", () => {
+ const maya = { id: "maya", name: "Maya", section: "Research" };
+ const theo = { id: "theo", name: "Theo", section: "Research" };
+ const nora = { id: "nora", name: "Nora", section: "Design" };
+ const room = [maya, theo, nora, ...members];
+ const led = { defaultResponder: { kind: "member", botId: members[0]!.id } } as const;
+ expect(roomRespondersForComposer("@Research status", room, led)).toEqual([maya, theo]);
+ expect(roomRespondersForComposer("@Nora and @research", room, led)).toEqual([nora, maya, theo]);
+ expect(roomRespondersForComposer("@Researchers status", room, led)).toEqual([members[0]]);
+ });
+
it("supports everyone and mentions-only room policies", () => {
expect(roomRespondersForComposer("hello", members, { defaultResponder: { kind: "everyone" } })).toEqual(members);
expect(roomRespondersForComposer("hello", members, { defaultResponder: { kind: "mentions" } })).toEqual([]);
diff --git a/src/lib/group-routing.ts b/src/lib/group-routing.ts
index c8d0da34a5..f869b3c8b7 100644
--- a/src/lib/group-routing.ts
+++ b/src/lib/group-routing.ts
@@ -1,5 +1,6 @@
import type { Bot, ConfigStatus, Group, GroupDefaultResponder } from "@/state/store";
import { isMentionBoundary, isMentionNameContinuation } from "../../shared/mention-boundary";
+import { departmentMentions } from "../../shared/department-mention";
import { resolveMentions } from "../../shared/room-mentions";
import { t } from "./i18n";
@@ -55,7 +56,7 @@ export function openTaskWorking(group: Pick<Group, "threadId" | "working" | "tas
/** Who answers this line, as the room reads it. `ambiguous`: it @-tags a name several members share and none of them
* was picked from the @ list, so the room refuses it and nobody answers. */
-export function roomRouteForComposer<T extends { id: string; name: string; hidden?: boolean }>(
+export function roomRouteForComposer<T extends { id: string; name: string; section?: string; hidden?: boolean }>(
text: string,
members: T[],
group: Pick<Group, "defaultResponder">,
@@ -75,7 +76,8 @@ export function roomRouteForComposer<T extends { id: string; name: string; hidde
}
const mentioned = resolveMentions(text, available, picked);
if (mentioned.ambiguous.length) return { responders: [], ambiguous: true };
- if (mentioned.bots.length) return { responders: mentioned.bots, ambiguous: false };
+ const tagged = [...new Set([...mentioned.bots, ...departmentMentions(text, available)])];
+ if (tagged.length) return { responders: tagged, ambiguous: false };
const fallback = effectiveDefaultResponder(group, available);
if (fallback.kind === "everyone" || fallback.kind === "auto") return { responders: available, ambiguous: false };
const lead = fallback.kind === "member" ? available.find((member) => member.id === fallback.botId) : undefined;
diff --git a/src/lib/mentions.test.ts b/src/lib/mentions.test.ts
index dd4762b017..0e0f9d6c41 100644
--- a/src/lib/mentions.test.ts
+++ b/src/lib/mentions.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
-import { mentionChoicesForQuery, mentionClassName, mentionQueryAt, mentionRanges, mentionRowDescription } from "./mentions";
+import { departmentChoices, mentionChoicesForQuery, mentionClassName, mentionQueryAt, mentionRanges, mentionRowDescription } from "./mentions";
const peers = [{ name: "Atlas" }, { name: "New Bot" }, { name: "New Bot 2" }, { name: "調査担当" }, { name: "Hidden", hidden: true }];
const matches = (text: string, everyone = false) => mentionRanges(text, peers, everyone).map(({ start, end }) => text.slice(start, end));
@@ -45,6 +45,16 @@ describe("mention display ranges", () => {
expect(matches("**@Atlas** (@New Bot 2) 【@調査担当】")).toEqual(["@Atlas", "@New Bot 2", "@調査担当"]);
expect(matches("user@Atlas /@Atlas")).toEqual([]);
});
+ it("lists a room's departments only when it has more than one", () => {
+ const maya = { name: "Maya", section: "Research" };
+ const theo = { name: "Theo", section: "Research" };
+ const solo = { name: "Solo" };
+ expect(departmentChoices([maya, theo])).toEqual([]);
+ expect(departmentChoices([maya, theo, solo])).toEqual([
+ { id: "team:Research", name: "Research", team: { count: 2 } },
+ { id: "team:General", name: "General", team: { count: 1 } },
+ ]);
+ });
it("only decorates everyone in channels and rejects longer words", () => {
expect(matches("@everyone @Everyone! @everyone_else @everyone2", true)).toEqual(["@everyone", "@Everyone"]);
expect(matches("@everyone")).toEqual([]);
diff --git a/src/lib/mentions.ts b/src/lib/mentions.ts
index f1840cb0fc..59ac3288ab 100644
--- a/src/lib/mentions.ts
+++ b/src/lib/mentions.ts
@@ -3,6 +3,7 @@ import { MAUS_COLORS, type MausColor } from "./mascot";
import { isMentionBoundary, isMentionNameContinuation } from "../../shared/mention-boundary";
import { personTagAt, personTagNames } from "../../shared/person-mention";
import { devsTagAt } from "../../shared/devs-mention";
+import { taggableDepartments } from "../../shared/department-mention";
import { blankCode } from "../../shared/skill-command";
/** A name the @ list offers: a bot's, or (`person`) a teammate's on a Team Cloud. */
@@ -30,6 +31,13 @@ export function mentionChoicesForQuery<T extends { name: string }>(pool: readonl
return pool.filter((choice) => !normalized || choice.name.toLowerCase().includes(normalized));
}
+/** A room's departments as rows of the @ list and names to highlight. A room with one department has none: @everyone
+ * already reaches it. */
+export function departmentChoices(members: readonly { name: string; section?: string; hidden?: boolean }[]): Array<{ id: string; name: string; team: { count: number } }> {
+ const departments = taggableDepartments(members);
+ return departments.length > 1 ? departments.map(({ name, members: bots }) => ({ id: `team:${name}`, name, team: { count: bots.length } })) : [];
+}
+
export type MentionRowDetail =
| { kind: "bot"; title: string }
| { kind: "everyone"; count: number }