Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions apps/web/src/components/bots/botOrgTree.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { describe, expect, it } from "vite-plus/test";

import { isFounderName, orderBotTree } from "./botOrgTree";
import { splitStandingThreads } from "./botPresence";

const bot = (name: string) => ({ id: name, standing: { name } });
const shape = (names: string[]) =>
orderBotTree(names.map(bot)).map(({ thread, depth }) => `${depth}:${thread.id}`);

describe("orderBotTree", () => {
it("nests the exec team under CEO and engineering under CTO", () => {
expect(
shape([
"PR Reviewer",
"Growth",
"Release Engineer",
"CFO",
"CEO",
"Infra / ARC Ops",
"Platform",
"CTO",
"Product",
"CoS",
]),
).toEqual([
"0:CoS",
"0:CEO",
"1:CTO",
"2:Platform",
"2:Infra / ARC Ops",
"2:Release Engineer",
"2:PR Reviewer",
"1:CFO",
"1:Product",
"1:Growth",
]);
});

it("shows a bot as a root when its manager is not in the list", () => {
expect(shape(["CTO", "Platform"])).toEqual(["0:CTO", "1:Platform"]);
expect(shape(["Platform", "CFO"])).toEqual(["0:CFO", "0:Platform"]);
});

it("maps existing persona names to their seat and sorts unknown bots last", () => {
expect(shape(["Zed", "Platform / CTO", "CTO", "Product / Site", "CEO", "Alpha"])).toEqual([
"0:CEO",
"1:CTO",
"2:Platform / CTO",
"1:Product / Site",
"0:Alpha",
"0:Zed",
]);
});

it("never drops a bot, even with duplicate seats", () => {
expect(shape(["CEO", "CTO", "CTO"]).length).toBe(3);
});
});

describe("founders are not bots", () => {
it("recognizes founder names", () => {
expect(isFounderName(" Kameron ")).toBe(true);
expect(isFounderName("Jeff")).toBe(true);
expect(isFounderName("CEO")).toBe(false);
});

it("keeps a founder thread out of the Bots shelf", () => {
const { bots, rest } = splitStandingThreads([
{ id: "k", standing: { name: "Kameron" } },
{ id: "c", standing: { name: "CEO" } },
{ id: "w", standing: null },
]);
expect(bots.map((t) => t.id)).toEqual(["c"]);
expect(rest.map((t) => t.id)).toEqual(["k", "w"]);
});
});
93 changes: 93 additions & 0 deletions apps/web/src/components/bots/botOrgTree.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* The Bots shelf as an org tree. The chain of command mirrors closura-agent-config
* `config/standing-threads.json` (`reportsTo`): CoS and CEO at the root, the executive team under
* CEO, and the engineering team under CTO. Founders are humans, never bots, so they have no
* place here and never list in the Bots shelf.
*/

/** Normalized bot name -> the normalized name it reports to; `null` is a root. */
const REPORTS_TO: Readonly<Record<string, string | null>> = {
cos: null,
ceo: null,
cto: "ceo",
cfo: "ceo",
product: "ceo",
growth: "ceo",
platform: "cto",
"infra / arc ops": "cto",
"release engineer": "cto",
"pr reviewer": "cto",
};

/** Existing persona names that are the same seat under a longer title. */
const ALIASES: Readonly<Record<string, string>> = {
"product / site": "product",
"platform / cto": "platform",
"infra/arc ops": "infra / arc ops",
"arc ops": "infra / arc ops",
};

/** Root order and sibling order follow the chart, not the alphabet. */
const SEAT_ORDER = Object.keys(REPORTS_TO);

/** Founders are humans: Kameron and Jeff are never listed as bots. */
const FOUNDER_NAMES = new Set(["kameron", "kameron smith", "jeff", "jeffrey", "jeffrey smith"]);

export function botSeat(name: string | null | undefined): string {
const key = (name ?? "").trim().toLowerCase().replace(/\s+/g, " ");
return ALIASES[key] ?? key;
}

export function isFounderName(name: string | null | undefined): boolean {
return FOUNDER_NAMES.has((name ?? "").trim().toLowerCase().replace(/\s+/g, " "));
}

export interface BotTreeRow<T> {
readonly thread: T;
readonly depth: number;
}

type Named = { readonly standing: { readonly name: string } | null; readonly id: string };

function rank(seat: string): number {
const index = SEAT_ORDER.indexOf(seat);
return index === -1 ? SEAT_ORDER.length : index;
}

/**
* Orders bots depth-first by reporting line. A bot whose manager is not in this list (another
* group, or not yet launched) is shown as a root, so no bot ever disappears. Bots outside the
* chart come after it, alphabetically.
*/
export function orderBotTree<T extends Named>(threads: readonly T[]): BotTreeRow<T>[] {
const bySeat = new Map<string, T>();
for (const thread of threads) {
const seat = botSeat(thread.standing?.name);
if (!bySeat.has(seat)) bySeat.set(seat, thread);
}
const compare = (left: T, right: T) =>
rank(botSeat(left.standing?.name)) - rank(botSeat(right.standing?.name)) ||
(left.standing?.name ?? "").localeCompare(right.standing?.name ?? "", undefined, {
sensitivity: "base",
}) ||
left.id.localeCompare(right.id);
const parentOf = (thread: T): T | null => {
const parentSeat = REPORTS_TO[botSeat(thread.standing?.name)];
const parent = parentSeat ? bySeat.get(parentSeat) : undefined;
return parent && parent !== thread ? parent : null;
};
const children = new Map<T, T[]>();
const roots: T[] = [];
for (const thread of threads) {
const parent = parentOf(thread);
if (parent === null) roots.push(thread);
else children.set(parent, [...(children.get(parent) ?? []), thread]);
}
const rows: BotTreeRow<T>[] = [];
const visit = (thread: T, depth: number) => {
rows.push({ thread, depth });
for (const child of (children.get(thread) ?? []).toSorted(compare)) visit(child, depth + 1);
};
for (const root of roots.toSorted(compare)) visit(root, 0);
return rows;
}
12 changes: 11 additions & 1 deletion apps/web/src/components/bots/botPresence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import type { OrchestrationV2ProviderFailureClass } from "@t3tools/contracts";

import type { ThreadStatusPill } from "../Sidebar.logic";
import type { BotAvatarState } from "./BotAvatar";
import { isFounderName } from "./botOrgTree";

function isFounderStanding(standing: unknown): boolean {
const name = (standing as { name?: unknown } | null)?.name;
return typeof name === "string" && isFounderName(name);
}

export interface BotPresence {
readonly avatarState: BotAvatarState;
Expand Down Expand Up @@ -59,7 +65,11 @@ export function splitStandingThreads<T extends { readonly standing: unknown }>(
): { readonly bots: T[]; readonly rest: T[] } {
const bots: T[] = [];
const rest: T[] = [];
for (const thread of threads) (thread.standing != null ? bots : rest).push(thread);
for (const thread of threads) {
// A founder is a human, not a bot: founder-to-founder threads stay out of the Bots shelf.
const isBot = thread.standing != null && !isFounderStanding(thread.standing);
(isBot ? bots : rest).push(thread);
}
return { bots, rest };
}

Expand Down
13 changes: 11 additions & 2 deletions apps/web/src/components/sidebar/SidebarBotsGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { contextMenuSourceOf } from "../../contextMenuSource";
import { useThreadSelectionStore } from "../../threadSelectionStore";
import { useUiStateStore } from "../../uiStateStore";
import { BotAvatar, BOT_ACCENT_CLASSES } from "../bots/BotAvatar";
import { orderBotTree } from "../bots/botOrgTree";
import { resolveBotPresence } from "../bots/botPresence";
import { resolveThreadLastVisitedAt, resolveThreadStatusPill } from "../Sidebar.logic";
import { useClientSettings } from "../../hooks/useSettings";
Expand Down Expand Up @@ -61,6 +62,7 @@ export function SidebarBotsGroup(
current[environmentId] === next ? current : { ...current, [environmentId]: next },
);
}, []);
const rows = orderBotTree(props.threads);
if (props.threads.length === 0) return null;
const label = props.label ?? "Bots";
const environmentIds = [...new Set(props.threads.map((thread) => thread.environmentId))];
Expand All @@ -78,12 +80,13 @@ export function SidebarBotsGroup(
<span aria-hidden className="h-px flex-1 bg-sidebar-foreground/15" />
</div>
<ul role="list" className="flex flex-col gap-px">
{props.threads.map((thread) => {
{rows.map(({ thread, depth }) => {
const key = scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id));
return (
<SidebarBotRow
key={key}
thread={thread}
depth={depth}
threadKey={key}
isActive={props.activeThreadKey === key}
typing={typingByEnvironment[thread.environmentId]}
Expand Down Expand Up @@ -119,6 +122,8 @@ function TypingFeed(props: {
const SidebarBotRow = memo(function SidebarBotRow(
props: BotRowHandlers & {
readonly thread: EnvironmentThreadShell;
/** Reporting-line depth: 0 is a root; each level indents one step. */
readonly depth: number;
readonly threadKey: string;
readonly isActive: boolean;
readonly typing: TypingByThread | undefined;
Expand Down Expand Up @@ -154,7 +159,11 @@ const SidebarBotRow = memo(function SidebarBotRow(
props.onThreadActivate(threadRef);
};
return (
<li className="list-none">
<li
className="list-none"
data-bot-depth={props.depth}
style={props.depth > 0 ? { paddingLeft: `${props.depth * 14}px` } : undefined}
>
<div
role="button"
tabIndex={0}
Expand Down
Loading