Skip to content

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

fix(usage): show bots under their current name in Usage

AdityaUmalef14496612232
diff --git a/src/components/UsageSection.botnames.test.ts b/src/components/UsageSection.botnames.test.ts
new file mode 100644
index 0000000000..083b45511f
--- /dev/null
+++ b/src/components/UsageSection.botnames.test.ts
@@ -0,0 +1,40 @@
+import { createElement } from "react";
+import { renderToStaticMarkup } from "react-dom/server";
+import { describe, expect, it, vi } from "vitest";
+
+vi.mock("@/state/store", async (original) => {
+ const actual = await original<typeof import("@/state/store")>();
+ const bots = [{
+ id: "b1", threadId: "t1", name: "Research Lead", title: "", description: "", notifications: true, color: "green", unread: false, busy: false,
+ activity: "idle", modelSelection: { instanceId: "claude", model: "m" }, approvalMode: "ask", alwaysAllow: [], messages: [], activeLeafId: null,
+ }];
+ return { ...actual, useStore: () => ({ state: { ...actual.initialState, bots }, dispatch: vi.fn() }), api: vi.fn(() => new Promise(() => {})) };
+});
+const { GroupBars, RunRow } = await import("./UsageSection");
+
+const group = (key: string, label: string) => ({ key, label, turns: 1, input: 1_000, output: 0, cachedInput: 0, costUsd: null });
+
+describe("usage by bot", () => {
+ it("shows a renamed bot under its current name, not the name its first usage row was booked under", () => {
+ const html = renderToStaticMarkup(createElement(GroupBars, { groupBy: "bot", groups: [group("bot:b1", "Bot 7")] }));
+ expect(html).toContain("Research Lead");
+ expect(html).not.toContain("Bot 7");
+ });
+
+ it("keeps the booked name for a bot that no longer exists", () => {
+ const html = renderToStaticMarkup(createElement(GroupBars, { groupBy: "bot", groups: [group("bot:gone", "Old helper")] }));
+ expect(html).toContain("Old helper");
+ });
+
+ it("names a renamed bot by its current name in the runs list", () => {
+ const root = {
+ id: "r1", title: "", source: "owner" as const, startedAt: 0, endedAt: 0, running: false, timed: false, usageStatus: "complete" as const,
+ bots: [{ id: "b1", name: "Bot 7" }, { id: "gone", name: "Old helper" }],
+ durationMs: 0, costUsd: null, tokens: 0, input: 0, cachedInput: 0, output: 0, steps: 0, tools: 0, errors: 0,
+ };
+ const html = renderToStaticMarkup(createElement(RunRow, { root, onOpen: () => {} }));
+ expect(html).toContain("A message to Research Lead");
+ expect(html).toContain("Research Lead, Old helper");
+ expect(html).not.toContain("Bot 7");
+ });
+});
diff --git a/src/components/UsageSection.tsx b/src/components/UsageSection.tsx
index 38e3d14943..c6e7235d69 100644
--- a/src/components/UsageSection.tsx
+++ b/src/components/UsageSection.tsx
@@ -4,7 +4,7 @@ import { cn } from "@/lib/cn";
import { activeLocale, documentLanguage, t } from "@/lib/i18n";
import { listenLiveFrames } from "@/lib/live-events";
import { formatTokens, hasFiniteCost, headlineTokens } from "@/lib/usage";
-import { fetchTraces, formatCost, formatDuration, USAGE_PERIODS, usageExportHref, usagePeriodRange, type UsagePeriod } from "@/lib/usage-trace";
+import { currentBotName, fetchTraces, formatCost, formatDuration, runTitle, USAGE_PERIODS, usageExportHref, usagePeriodRange, type UsagePeriod } from "@/lib/usage-trace";
import { api, useStore } from "@/state/store";
import type { LocaleKey } from "@/locales";
import type { TraceList, TraceRootSummary } from "../../shared/usage-trace";
@@ -91,7 +91,7 @@ export function GroupBars({ groupBy, groups }: { groupBy: UsageGroupBy; groups:
<li key={group.key} className="grid grid-cols-[minmax(0,9rem)_1fr_auto] items-center gap-3 text-[13px]" title={t("usage.page.turns", { count: String(group.turns) })}>
<span className="flex min-w-0 items-center gap-2 text-ink">
{bot && <BotAvatar bot={bot} state="idle" size={18} animated={false} />}
- <span className="truncate">{groupBy === "model" ? modelName(group.label, state.instances) : usageGroupLabel(groupBy, group)}</span>
+ <span className="truncate">{bot ? bot.name : groupBy === "model" ? modelName(group.label, state.instances) : usageGroupLabel(groupBy, group)}</span>
</span>
<span className="h-2 overflow-hidden rounded-full bg-raised">
<span className="block h-full rounded-full bg-accent/70" style={{ width: `${Math.max(2, (headlineTokens(group) / max) * 100)}%` }} />
@@ -147,7 +147,8 @@ function startedLabel(at: number): string {
export function RunRow({ root, onOpen }: { root: TraceRootSummary; onOpen: () => void }) {
const { state } = useStore();
- const title = root.title || t(`trace.source.${root.source}`, { bot: root.bots[0]?.name ?? "" });
+ const title = runTitle(root, state.bots);
+ const names = root.bots.map((entry) => currentBotName(state.bots, entry.id, entry.name)).join(", ");
return (
<li>
<button type="button" onClick={onOpen} className="grid w-full grid-cols-[auto_minmax(0,1fr)_auto] items-center gap-3 rounded-xl px-3 py-2.5 text-left hover:bg-raised/50" data-testid="usage-run">
@@ -161,7 +162,7 @@ export function RunRow({ root, onOpen }: { root: TraceRootSummary; onOpen: () =>
</span>
<span className="min-w-0">
<span className="block truncate text-[14px] text-ink">{title}</span>
- <span className="block truncate text-[12px] text-ink-tertiary">{startedLabel(root.startedAt)} · {root.bots.map((bot) => bot.name).join(", ")}</span>
+ <span className="block truncate text-[12px] text-ink-tertiary">{startedLabel(root.startedAt)} · {names}</span>
</span>
<span className="flex items-center gap-3 text-[13px] tabular-nums text-ink-secondary">
{root.running && <LiveBadge />}
diff --git a/src/components/usage/TraceWaterfall.tsx b/src/components/usage/TraceWaterfall.tsx
index 244360251b..5589af6fb9 100644
--- a/src/components/usage/TraceWaterfall.tsx
+++ b/src/components/usage/TraceWaterfall.tsx
@@ -4,7 +4,7 @@ import { cn } from "@/lib/cn";
import { t } from "@/lib/i18n";
import { formatTokens, hasFiniteCost } from "@/lib/usage";
import {
- formatCost, formatDuration, formatTick, groupLabel, timeAxis, toolLabel, traceBadges, traceRows, type TraceRow,
+ formatCost, formatDuration, formatTick, groupLabel, runTitle, timeAxis, toolLabel, traceBadges, traceRows, type TraceRow,
} from "@/lib/usage-trace";
import { useStore, type InstanceInfo } from "@/state/store";
import type { TraceDetail, TraceNode, TraceRootSummary, TraceTotals } from "../../../shared/usage-trace";
@@ -87,11 +87,13 @@ function RowName({ row, models }: { row: TraceRow; models: readonly InstanceInfo
let name: string;
let detail: string | undefined;
switch (node.kind) {
- case "turn":
- icon = <TurnAvatar bot={state.bots.find((bot) => bot.id === node.botId)} name={node.botName} />;
- name = node.botName || t("trace.unknownBot");
+ case "turn": {
+ const bot = state.bots.find((entry) => entry.id === node.botId);
+ icon = <TurnAvatar bot={bot} name={node.botName} />;
+ name = (bot?.name ?? node.botName) || t("trace.unknownBot");
detail = modelName(node.model, models);
break;
+ }
case "step":
icon = <StepIcon />;
name = t("trace.step");
@@ -206,7 +208,7 @@ export function TraceWaterfall({ detail, now, actions }: { detail: TraceDetail;
if (next.has(id)) next.delete(id); else next.add(id);
return next;
});
- const title = root.title || t(`trace.source.${root.source}`, { bot: root.bots[0]?.name ?? "" });
+ const title = runTitle(root, state.bots);
return (
<section className="overflow-hidden rounded-2xl border border-hairline/50 bg-card" data-testid="trace-waterfall" aria-label={t("trace.title")}>
diff --git a/src/lib/usage-trace.ts b/src/lib/usage-trace.ts
index 1dc30ce800..256a896060 100644
--- a/src/lib/usage-trace.ts
+++ b/src/lib/usage-trace.ts
@@ -5,7 +5,7 @@ import { en, type LocaleKey } from "@/locales";
import { toolStepLabel } from "@/lib/tool-step-label";
import { formatUsd, hasFiniteCost, usdText } from "@/lib/usage";
import type {
- TraceDetail, TraceGroup, TraceList, TraceNode, TraceStep, TraceTokens, TraceTool, TraceTurn,
+ TraceDetail, TraceGroup, TraceList, TraceNode, TraceRootSummary, TraceStep, TraceTokens, TraceTool, TraceTurn,
} from "../../shared/usage-trace.ts";
export type UsagePeriod = "today" | "week" | "month" | "lastMonth" | "days30";
@@ -254,3 +254,16 @@ export function threadsOf(turns: readonly TraceTurn[]): Set<string> {
export function isStep(node: TraceNode): node is TraceStep {
return node.kind === "step";
}
+
+type NamedBot = { id: string; name: string };
+
+/** Usage rows keep the name a bot had when they were booked; a bot that still exists shows under its current name. */
+export function currentBotName(bots: readonly NamedBot[], id: string, booked: string): string {
+ return bots.find((bot) => bot.id === id)?.name ?? booked;
+}
+
+/** A run's title: its own, or "A message to <first bot>" under that bot's current name. */
+export function runTitle(root: TraceRootSummary, bots: readonly NamedBot[]): string {
+ const first = root.bots[0];
+ return root.title || t(`trace.source.${root.source}`, { bot: first ? currentBotName(bots, first.id, first.name) : "" });
+}