Skip to content

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

feat(rooms): say where a queued teammate is busy and that it starts when that finishes

Bugfixer 2a765ea9993cd
diff --git a/packages/phone-protocol/src/wire.ts b/packages/phone-protocol/src/wire.ts
index 44e6e78dd2..b2273b4517 100644
--- a/packages/phone-protocol/src/wire.ts
+++ b/packages/phone-protocol/src/wire.ts
@@ -901,8 +901,8 @@ export interface RoomQueueEntry {
/** 1-based place in that thread's line; `of` counts the whole line. */
position: number;
of: number;
- /** Who a queued member waits on: the member running now, else the one speaking (or the one ahead). */
- waitingForBotId?: string;
+ /** Who a queued member waits on: the member running now, else the one speaking (or the one ahead). `busyIn`: where it runs now outside this room, another room's thread (`groupId` set) or a thread of its own; it starts here when that finishes. */
+ waitingForBotId?: string; busyIn?: { groupId?: string; threadId: string };
assignmentId?: string;
}
diff --git a/server/host/room-jobs.test.ts b/server/host/room-jobs.test.ts
new file mode 100644
index 0000000000..6f2e20ba75
--- /dev/null
+++ b/server/host/room-jobs.test.ts
@@ -0,0 +1,43 @@
+import { describe, expect, it } from "vitest";
+
+import type { AppConfig } from "../config.ts";
+import type { HandoffView } from "../coordinator.ts";
+import type { Store } from "../store.ts";
+import { createRoomJobs } from "./room-jobs.ts";
+
+type Operation = { threadId: string; cancelled: boolean; botIds: Set<string> };
+type Room = { id: string; name: string; threadId: string; busyBotId?: string | null };
+
+function setup(input: { rooms: Room[]; operations?: Record<string, Operation[]>; busyThreads?: Record<string, string[]>; jobs: Partial<HandoffView>[] }) {
+ const store = {
+ groups: input.rooms,
+ tasks: (botId: string) => (input.busyThreads?.[botId] ?? []).map((threadId) => ({ threadId, title: threadId, busy: true })),
+ } as unknown as Store;
+ const active = input.jobs.map((job, index) => ({ id: `job-${index}`, parentId: "lead-turn", status: "queued", createdAt: index, ...job }) as HandoffView);
+ return createRoomJobs({
+ store, cfg: {} as AppConfig, coordinator: { active: () => active },
+ operations: (groupId) => input.operations?.[groupId] ?? [], speakerOf: () => undefined, laneRoomThread: () => undefined,
+ });
+}
+
+describe("a room's queue names where a waiting member is busy", () => {
+ const rooms: Room[] = [{ id: "room-1", name: "Group 1", threadId: "g1" }, { id: "room-2", name: "Group 2", threadId: "g2" }];
+ const handedIn2 = { botId: "coder", groupId: "room-2", threadId: "g2" };
+
+ it("marks a member queued here while its turn runs in another room", () => {
+ const jobs = setup({ rooms, operations: { "room-1": [{ threadId: "g1", cancelled: false, botIds: new Set(["coder"]) }] }, jobs: [handedIn2] });
+ expect(jobs.roomLineOf(rooms[1] as never)).toEqual([
+ { botId: "coder", threadId: "g2", state: "queued", position: 1, of: 1, assignmentId: "job-0", busyIn: { groupId: "room-1", threadId: "g1" } },
+ ]);
+ });
+
+ it("marks one busy in a thread of its own, and no place when it is free or only this room holds it", () => {
+ expect(setup({ rooms, busyThreads: { coder: ["own-chat"] }, jobs: [handedIn2] }).roomLineOf(rooms[1] as never)[0]?.busyIn)
+ .toEqual({ threadId: "own-chat" });
+ expect(setup({ rooms, jobs: [handedIn2] }).roomLineOf(rooms[1] as never)[0]?.busyIn).toBeUndefined();
+ const sameRoom = setup({ rooms, operations: { "room-2": [{ threadId: "g2", cancelled: false, botIds: new Set(["coder"]) }] }, jobs: [handedIn2] });
+ expect(sameRoom.roomLineOf(rooms[1] as never)[0]?.busyIn).toBeUndefined();
+ const cancelled = setup({ rooms, operations: { "room-1": [{ threadId: "g1", cancelled: true, botIds: new Set(["coder"]) }] }, jobs: [handedIn2] });
+ expect(cancelled.roomLineOf(rooms[1] as never)[0]?.busyIn).toBeUndefined();
+ });
+});
diff --git a/server/host/room-jobs.ts b/server/host/room-jobs.ts
index cf3104c358..0ff98d9a2f 100644
--- a/server/host/room-jobs.ts
+++ b/server/host/room-jobs.ts
@@ -13,7 +13,7 @@ export interface RoomJobsDeps {
// Read lazily: index.ts builds the coordinator after this module.
coordinator: Pick<Coordinator, "active">;
cfg: AppConfig;
- operations: (groupId: string) => Iterable<{ threadId: string; cancelled: boolean; line?: RoomRoundLine }>;
+ operations: (groupId: string) => Iterable<{ threadId: string; cancelled: boolean; botIds: ReadonlySet<string>; line?: RoomRoundLine }>;
speakerOf: (threadId: string) => string | undefined;
laneRoomThread: (n: Pick<HandoffView, "botId" | "threadId" | "groupId">) => string | undefined;
}
@@ -25,6 +25,18 @@ export function parallelRoomJobsEnabled(cfg: AppConfig): boolean {
const live = (n: HandoffView) => n.parentId !== undefined && (n.status === "queued" || n.status === "running");
export function createRoomJobs({ store, coordinator, cfg, operations, speakerOf, laneRoomThread }: RoomJobsDeps) {
+ /** Where a member runs now outside this room, if anywhere: a turn in another room, else a busy thread of its own. */
+ function busyElsewhere(botId: string, roomId: string): RoomQueueEntry["busyIn"] {
+ for (const other of store.groups) {
+ if (other.id === roomId) continue;
+ const turn = [...operations(other.id)].find(operation => !operation.cancelled && operation.botIds.has(botId));
+ if (turn) return { groupId: other.id, threadId: turn.threadId };
+ if (other.busyBotId === botId || speakerOf(other.threadId) === botId) return { groupId: other.id, threadId: other.threadId };
+ }
+ const task = store.tasks(botId).find(candidate => candidate.busy);
+ return task ? { threadId: task.threadId } : undefined;
+ }
+
function roomLineOf(record: GroupRecord): RoomQueueEntry[] {
const items: RoomLineItem[] = [];
for (const operation of operations(record.id)) {
@@ -40,7 +52,10 @@ export function createRoomJobs({ store, coordinator, cfg, operations, speakerOf,
const speaker = speakerOf(threadId);
if (speaker) speakers.set(threadId, speaker);
}
- return roomQueue(items, speakers);
+ return roomQueue(items, speakers).map(entry => {
+ const busyIn = entry.state === "queued" ? busyElsewhere(entry.botId, record.id) : undefined;
+ return busyIn ? { ...entry, busyIn } : entry;
+ });
}
function advanceRound(operation: { line?: RoomRoundLine }, current: string | undefined, spoken: ReadonlySet<string>): boolean {
diff --git a/server/room-queue.test.ts b/server/room-queue.test.ts
index 75947d2041..f7ad5ee2a3 100644
--- a/server/room-queue.test.ts
+++ b/server/room-queue.test.ts
@@ -48,6 +48,16 @@ describe("room queue", () => {
});
});
+describe("a member busy outside the room", () => {
+ it("tells the requester it is busy elsewhere and starts here when that finishes", () => {
+ const [entry] = roomQueue([job("e1", "queued")], new Map([["room", "lead"]]));
+ expect(queuedJobDetail({ ...entry!, busyIn: { groupId: "room-1", threadId: "t1" } }, nameOf))
+ .toBe("queued 1 of 1 in this room: Editor 1 is busy in another room now, so it starts here when that finishes");
+ expect(queuedJobDetail({ ...entry!, busyIn: { threadId: "own" } }, nameOf))
+ .toBe("queued 1 of 1 in this room: Editor 1 is busy in another thread of its own now, so it starts here when that finishes");
+ });
+});
+
describe("room queue merging", () => {
it("keeps a job's assignment id when the same member is also in the round, so its receipt still finds it", () => {
const line = roomQueue([
diff --git a/server/room-queue.ts b/server/room-queue.ts
index 84d5189e2d..2ba0b667e8 100644
--- a/server/room-queue.ts
+++ b/server/room-queue.ts
@@ -39,6 +39,10 @@ export function roomQueue(items: readonly RoomLineItem[], speakers: ReadonlyMap<
}
export function queuedJobDetail(entry: RoomQueueEntry, nameOf: (botId: string) => string): string {
+ if (entry.busyIn) {
+ const where = entry.busyIn.groupId ? "another room" : "another thread of its own";
+ return `queued ${entry.position} of ${entry.of} in this room: ${nameOf(entry.botId)} is busy in ${where} now, so it starts here when that finishes`;
+ }
const after = entry.waitingForBotId ? `, after ${nameOf(entry.waitingForBotId)}` : "";
return `queued ${entry.position} of ${entry.of} in this room: room jobs run one at a time, so it starts in turn${after}`;
}
diff --git a/src/components/GroupView.queue.test.ts b/src/components/GroupView.queue.test.ts
index 41e536472b..3bd463dddd 100644
--- a/src/components/GroupView.queue.test.ts
+++ b/src/components/GroupView.queue.test.ts
@@ -9,7 +9,8 @@ import type { Bot, Group } from "@/state/store";
vi.mock("@/state/store", async (importOriginal) => {
const original = await importOriginal<typeof import("@/state/store")>();
- return { ...original, useStore: () => ({ state: { ...original.initialState, bots: [] as Bot[] }, dispatch: vi.fn() }) };
+ const otherRoom = { id: "other-room", name: "Launch", threadId: "launch-thread", memberIds: [], defaultResponder: { kind: "mentions" }, bulletin: "", unread: false, createdAt: 1, messages: [] };
+ return { ...original, useStore: () => ({ state: { ...original.initialState, bots: [] as Bot[], groups: [otherRoom] }, dispatch: vi.fn() }) };
});
vi.mock("./DesktopCapabilities", async (importOriginal) => ({
...await importOriginal<typeof import("./DesktopCapabilities")>(),
@@ -54,6 +55,14 @@ describe("the room's queue", () => {
expect(rows).toEqual(["Second: Queued · waiting for First (2 of 3)", "Third: Queued · waiting for First (3 of 3)"]);
});
+ it("says a member busy in another room is queued and starts when that finishes", () => {
+ flushSync(() => root.render(createElement(RoomQueueList, { members, group: { ...room, queue: [
+ { botId: "second", threadId: "room-thread", state: "queued", position: 1, of: 1, busyIn: { groupId: "other-room", threadId: "launch-thread" } },
+ ] } })));
+ const rows = [...host.querySelectorAll('[data-testid="room-queue"] li')].map((li) => li.textContent);
+ expect(rows).toEqual(["Second: Busy in Launch · queued (1 of 1), starts when it finishes"]);
+ });
+
it("shows nothing when nobody waits in the open thread", () => {
flushSync(() => root.render(createElement(RoomQueueList, { members, group: { ...room, queue: [] } })));
expect(host.querySelector('[data-testid="room-queue"]')).toBeNull();
diff --git a/src/components/RoomHeader.tsx b/src/components/RoomHeader.tsx
index 3ebef2316e..d74c1b797a 100644
--- a/src/components/RoomHeader.tsx
+++ b/src/components/RoomHeader.tsx
@@ -9,8 +9,9 @@ import { normalizeState } from "@/lib/mascot";
import { effectivePlace, placeLabelKey } from "@/lib/place";
import { openRoomPanel } from "@/lib/room-panel";
import { chosenResponder, responderChoice, responderTitle } from "@/lib/room-responder";
-import { roomSpeakerIds } from "@/lib/room-threads";
+import { busyPlaceOf, roomSpeakerIds } from "@/lib/room-threads";
import { useStore, type Bot, type Group } from "@/state/store";
+import type { RoomQueueEntry } from "../../shared/wire";
import { BotAvatar } from "./Avatar";
import { RoomContextBadge } from "./ContextWarning";
import { ConversationTurnLimit } from "./ConversationTurnLimit";
@@ -60,12 +61,12 @@ export function DefaultResponderSelect({ group, members }: { group: Group; membe
);
}
-function memberMauses(group: Group, members: Bot[]) {
+function memberMauses(group: Group, members: Bot[], placeOf: (entry: RoomQueueEntry) => string | undefined) {
const contextWarnings = roomContextWarnings(group, members);
const speakersElsewhere = new Set(roomSpeakerIds(group).filter((id) => id !== group.busyBotId));
return members.map((b) => {
const busy = group.busyBotId === b.id;
- const queueTitle = busy ? undefined : roomMemberQueueTitle(group, members, b);
+ const queueTitle = busy ? undefined : roomMemberQueueTitle(group, members, b, placeOf);
const contextWarning = contextWarnings.find((warning) => warning.bot.id === b.id);
const busyElsewhere = !busy && speakersElsewhere.has(b.id);
const held = !group.dm && Boolean(group.holds?.[b.id]);
@@ -147,7 +148,8 @@ export function RoomHeader({
membersTriggerRef: RefObject<HTMLButtonElement | null>;
}) {
const { dragProps: headerDragProps, noDragStyle: headerNoDragStyle } = useCaptionChrome();
- const faces = memberMauses(group, members);
+ const { state } = useStore();
+ const faces = memberMauses(group, members, busyPlaceOf(state.groups, state.bots));
const membersLabel = members.length === 1
? t("room.members.ariaOne")
: t("room.members.ariaMany", { count: members.length });
diff --git a/src/components/RoomQueueList.tsx b/src/components/RoomQueueList.tsx
index 63c194aa39..ab5a2c1631 100644
--- a/src/components/RoomQueueList.tsx
+++ b/src/components/RoomQueueList.tsx
@@ -1,27 +1,29 @@
import { t } from "@/lib/i18n";
-import { roomQueueLabel, roomThreadQueue } from "@/lib/room-threads";
-import type { Bot, Group, Message } from "@/state/store";
+import { busyPlaceOf, roomQueueLabel, roomThreadQueue } from "@/lib/room-threads";
+import type { RoomQueueEntry } from "../../shared/wire";
+import { useStore, type Bot, type Group, type Message } from "@/state/store";
const nameIn = (members: Bot[]) => (botId: string) => members.find((member) => member.id === botId)?.name;
export function RoomQueueList({ group, members }: { group: Group; members: Bot[] }) {
+ const { state } = useStore();
const queued = roomThreadQueue(group, group.threadId);
if (!queued.length) return null;
- const nameOf = nameIn(members);
+ const nameOf = nameIn(members), placeOf = busyPlaceOf(state.groups, state.bots);
return (
<ul data-testid="room-queue" className="flex flex-col gap-0.5 pl-12 text-[13px] text-ink-secondary">
{queued.map((entry) => (
<li key={`${entry.botId}:${entry.position}`}>
- {t("room.queue.member", { name: nameOf(entry.botId) ?? t("sidebar.preview.aBot"), status: roomQueueLabel(entry, nameOf) })}
+ {t("room.queue.member", { name: nameOf(entry.botId) ?? t("sidebar.preview.aBot"), status: roomQueueLabel(entry, nameOf, placeOf) })}
</li>
))}
</ul>
);
}
-export function roomMemberQueueTitle(group: Group, members: Bot[], bot: Bot): string | undefined {
+export function roomMemberQueueTitle(group: Group, members: Bot[], bot: Bot, placeOf?: (entry: RoomQueueEntry) => string | undefined): string | undefined {
const entry = roomThreadQueue(group, group.threadId).find((candidate) => candidate.botId === bot.id);
- return entry ? t("room.queue.member", { name: bot.name, status: roomQueueLabel(entry, nameIn(members)) }) : undefined;
+ return entry ? t("room.queue.member", { name: bot.name, status: roomQueueLabel(entry, nameIn(members), placeOf) }) : undefined;
}
const ROUTED_BY = {
diff --git a/src/lib/room-threads.test.ts b/src/lib/room-threads.test.ts
index b99276846f..59bb2e3c91 100644
--- a/src/lib/room-threads.test.ts
+++ b/src/lib/room-threads.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
-import { roomQueueLabel, roomSpeakerIds, roomThreadQueue, roomThreadSpeakerId, roomThreadWorking, roomThreadsWorkingElsewhere } from "./room-threads";
+import { busyPlaceOf, roomQueueLabel, roomSpeakerIds, roomThreadQueue, roomThreadSpeakerId, roomThreadWorking, roomThreadsWorkingElsewhere } from "./room-threads";
import type { Group } from "@/state/store";
type Room = Pick<Group, "threadId" | "working" | "busyBotId" | "tasks">;
@@ -71,4 +71,15 @@ describe("room queue labels", () => {
expect(roomQueueLabel(roomThreadQueue({ queue }, "other")[0]!, () => undefined)).toBe("Queued (2 of 2)");
expect(roomThreadQueue({}, "open")).toEqual([]);
});
+
+ it("says where a waiting member is busy, by a name this viewer can see, and that it starts when that finishes", () => {
+ const placeOf = busyPlaceOf([{ id: "room-1", name: "Group 1" }], [{ id: "coder", tasks: [{ threadId: "own", title: "Fix login", createdAt: 1 }] }]);
+ const inRoom = { botId: "coder", threadId: "open", state: "queued" as const, position: 1, of: 2, waitingForBotId: "scout", busyIn: { groupId: "room-1", threadId: "g1" } };
+ expect(roomQueueLabel(inRoom, () => "Scout", placeOf)).toBe("Busy in Group 1 · queued (1 of 2), starts when it finishes");
+ expect(roomQueueLabel({ ...inRoom, busyIn: { threadId: "own" } }, () => "Scout", placeOf)).toBe("Busy in Fix login · queued (1 of 2), starts when it finishes");
+ // A room or thread this viewer can't see is not named.
+ expect(roomQueueLabel({ ...inRoom, busyIn: { groupId: "private-room", threadId: "p" } }, () => "Scout", placeOf))
+ .toBe("Busy in another conversation · queued (1 of 2), starts when it finishes");
+ expect(roomQueueLabel(inRoom, () => "Scout")).toBe("Busy in another conversation · queued (1 of 2), starts when it finishes");
+ });
});
diff --git a/src/lib/room-threads.ts b/src/lib/room-threads.ts
index 6c2458e919..0a9af905c0 100644
--- a/src/lib/room-threads.ts
+++ b/src/lib/room-threads.ts
@@ -1,5 +1,5 @@
import { t } from "@/lib/i18n";
-import type { Group, GroupTask } from "@/state/store";
+import type { Bot, Group, GroupTask } from "@/state/store";
import type { RoomQueueEntry } from "../../shared/wire";
type RoomThreads = Pick<Group, "threadId" | "working" | "busyBotId" | "tasks">;
@@ -39,7 +39,25 @@ export function roomThreadQueue(group: Pick<Group, "queue">, threadId: string):
return (group.queue ?? []).filter((entry) => entry.threadId === threadId && entry.state === "queued");
}
-export function roomQueueLabel(entry: RoomQueueEntry, nameOf: (botId: string) => string | undefined): string {
+/** The room or thread a queued member is busy in, by the name this viewer knows it by; undefined when it can't see it. */
+export function busyPlaceOf(groups: Pick<Group, "id" | "name">[], bots: Pick<Bot, "id" | "tasks">[]) {
+ return ({ botId, busyIn }: RoomQueueEntry): string | undefined => {
+ if (!busyIn) return undefined;
+ const place = busyIn.groupId
+ ? groups.find((group) => group.id === busyIn.groupId)?.name
+ : bots.find((bot) => bot.id === botId)?.tasks?.find((task) => task.threadId === busyIn.threadId)?.title;
+ return place?.trim() || undefined;
+ };
+}
+
+export function roomQueueLabel(entry: RoomQueueEntry, nameOf: (botId: string) => string | undefined,
+ placeOf?: (entry: RoomQueueEntry) => string | undefined): string {
+ if (entry.busyIn) {
+ const place = placeOf?.(entry);
+ return place
+ ? t("room.queue.busyIn", { place, position: entry.position, of: entry.of })
+ : t("room.queue.busyElsewhere", { position: entry.position, of: entry.of });
+ }
const name = entry.waitingForBotId ? nameOf(entry.waitingForBotId) : undefined;
return name
? t("room.queue.waitingFor", { name, position: entry.position, of: entry.of })
diff --git a/src/locales/en.json b/src/locales/en.json
index 86e942e9b4..ff698725a0 100644
--- a/src/locales/en.json
+++ b/src/locales/en.json
@@ -5233,6 +5233,8 @@
"room.memberWorkingElsewhere": "{name} is working in another thread",
"room.queue.waitingFor": "Queued · waiting for {name} ({position} of {of})",
"room.queue.waiting": "Queued ({position} of {of})",
+ "room.queue.busyIn": "Busy in {place} · queued ({position} of {of}), starts when it finishes",
+ "room.queue.busyElsewhere": "Busy in another conversation · queued ({position} of {of}), starts when it finishes",
"room.queue.member": "{name}: {status}",
"room.responder.everyone": "Plain messages go to every group member; @mentions override this",
"room.responder.mentions": "Only explicitly @mentioned bots respond",