Skip to content

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

Document the usage colour and split helpers

BunBnnuy9ca59af56ab8
diff --git a/server/routes/usage.ts b/server/routes/usage.ts
index de99efac3d..c2822b8a79 100644
--- a/server/routes/usage.ts
+++ b/server/routes/usage.ts
@@ -40,6 +40,7 @@ const whole = (raw: string | null, fallback: number, max: number): number | null
return /^\d{1,7}$/.test(raw) && Number(raw) <= max ? Number(raw) : null;
};
+/** The usage endpoints: trace browsing plus the grouped token and cost summary with its CSV export. */
export function createUsageRoutes(deps: UsageRouteDeps): RouteHandler {
const traces = createTraceService({ dataDir: deps.dataDir, recorder: deps.recorder, messages: deps.messages, liveBot: deps.liveBot });
return async ({ res, url, path, method, auth, json }) => {
diff --git a/server/usage-ledger.ts b/server/usage-ledger.ts
index bab7517af6..80a5601697 100644
--- a/server/usage-ledger.ts
+++ b/server/usage-ledger.ts
@@ -311,6 +311,7 @@ function barTokens(group: UsageGroup): number {
return Math.max(0, group.input - Math.min(group.cachedInput, group.input)) + group.output;
}
+/** Groups ledger rows by `groupBy`; when `splitBy` differs, each group also carries its pieces, largest first. */
export function summarizeUsage(
rows: UsageRow[],
groupBy: UsageGroupBy,
diff --git a/src/components/UsageSection.tsx b/src/components/UsageSection.tsx
index 63473d49db..6c3e8fcafd 100644
--- a/src/components/UsageSection.tsx
+++ b/src/components/UsageSection.tsx
@@ -69,6 +69,7 @@ export function usageGroupLabel(groupBy: UsageGroupBy, group: Pick<UsageGroup, "
return group.label;
}
+/** Loads the usage summary for a range and grouping, optionally split by model, and refreshes with `version`. */
function useUsageGroups(
range: { from: string; to: string },
groupBy: UsageGroupBy,
@@ -113,6 +114,7 @@ function Bar({ group, width, color }: { group: UsageGroup; width: number; color?
return <span data-model-color={color} className={cn("block h-full rounded-full", !color && "bg-accent/70")} style={{ width: `${width}%`, backgroundColor: color }} />;
}
+/** The six largest groups as proportional bars, with a model colour dot when the grouping is by model. */
export function GroupBars({ groupBy, groups }: { groupBy: UsageGroupBy; groups: readonly UsageGroup[] }) {
const { state } = useStore();
const top = [...groups].sort((a, b) => headlineTokens(b) - headlineTokens(a)).slice(0, 6);
@@ -169,6 +171,7 @@ function ModelLegend({ groups }: { groups: readonly UsageGroup[] }) {
);
}
+/** The "Where it went" panel: bots on one side, the chosen grouping on the other, both split by model. */
function WhereItWent({ range, version, onBudget }: { range: { from: string; to: string }; version: number; onBudget: (budget: BudgetState | null) => void }) {
const [other, setOther] = useState<UsageGroupBy>("model");
const byBot = useUsageGroups(range, "bot", version, "model");
diff --git a/src/lib/model-color.ts b/src/lib/model-color.ts
index 0178677468..3d6fa2d23f 100644
--- a/src/lib/model-color.ts
+++ b/src/lib/model-color.ts
@@ -73,6 +73,7 @@ export function modelIdOf(groupKey: string): string {
return slash === -1 ? rest : rest.slice(slash + 1);
}
+/** The driver segment of a `model:<driver>/<model>` key, or undefined when the key carries no driver. */
function driverOf(groupKey: string): string | undefined {
if (!groupKey.startsWith("model:")) return undefined;
const rest = groupKey.slice("model:".length);
@@ -87,6 +88,7 @@ function coreModelId(model: string): string {
return id.replace(/(?::|-)(?:free|beta|preview|latest)$/, "");
}
+/** The provider family a model belongs to, by its model id or its driver; unknown models use the neutral family. */
export function modelFamily(groupKey: string): ModelFamily {
const model = coreModelId(modelIdOf(groupKey));
const known = FAMILIES.find((family) => family.pattern.test(model));
@@ -124,6 +126,7 @@ const CHROMA_STEPS = [0.93, 1, 1.07] as const;
const SURFACES = ["#ffffff", "#3a322d"] as const;
const MIN_CONTRAST = 3.05;
+/** Whether a colour meets the minimum contrast on both the light and the dark surface. */
function readsOnBoth(hex: string): boolean {
return SURFACES.every((surface) => contrastRatio(hex, surface) >= MIN_CONTRAST);
}
@@ -143,6 +146,7 @@ function readableTone(hex: string, fallback: string): string {
return fallback;
}
+/** Moves a model deterministically around its family colour, then keeps the shade readable on both grounds. */
function shadeOf(model: string, family: ModelFamily): string {
const base = toOklch(family.color);
const index = hash32(`${SHADE_SALT}${model}`) % (HUE_STEPS.length * LIGHT_STEPS.length * CHROMA_STEPS.length);