Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@ import { useNavigate } from "@tanstack/react-router";
import { useHostUsageQuota } from "renderer/hooks/host-service/useHostUsageQuota";
import { useLocalHostService } from "renderer/routes/_authenticated/providers/LocalHostServiceProvider/LocalHostServiceProvider";
import { useSettings } from "renderer/stores/settings";
import { QUOTA_AGENT_ORDER } from "renderer/utils/usage/quotaAgentLabels";
import { RailUsageRing } from "./components/RailUsageRing";
import { selectRailUsage } from "./utils/selectRailUsage";

export function RailUsage() {
const navigate = useNavigate();
const { activeHostUrl } = useLocalHostService();
const isShown = useSettings((s) => s.showRailUsage);
const quota = useHostUsageQuota(isShown ? activeHostUrl : null);
if (!isShown) return null;
const entries = selectRailUsage(quota.data ?? []);
const hiddenProviders = useSettings((s) => s.hiddenRailUsageProviders);
const hasVisibleProvider =
isShown &&
QUOTA_AGENT_ORDER.some((agent) => !hiddenProviders.includes(agent));
const quota = useHostUsageQuota(hasVisibleProvider ? activeHostUrl : null);
if (!hasVisibleProvider) return null;
const entries = selectRailUsage(quota.data ?? [], hiddenProviders);

return entries.map((entry) => (
<RailUsageRing
key={entry.account.accountKey}
key={entry.account.agent}
entry={entry}
onOpen={() =>
navigate({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export type { RailUsageEntry } from "./selectRailUsage";
export { MAX_RAIL_USAGE_ACCOUNTS, selectRailUsage } from "./selectRailUsage";
export { selectRailUsage } from "./selectRailUsage";
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,27 @@ describe("selectRailUsage", () => {
]);
});

it("caps the rail at two providers in a fixed order", () => {
it("shows all readable providers in a fixed order", () => {
const entries = selectRailUsage([
account({ agent: "grok", windows: [quotaWindow("weekly", 5)] }),
account({ agent: "agy", windows: [quotaWindow("3p-5h", 0)] }),
account({ agent: "codex", windows: [quotaWindow("primary", 5)] }),
account({}),
]);
expect(entries.map((entry) => entry.account.agent)).toEqual([
"claude",
"codex",
"agy",
]);
});

it("omits only the providers hidden in settings", () => {
const entries = selectRailUsage(
[account({}), account({ agent: "codex" }), account({ agent: "agy" })],
["codex"],
);
expect(entries.map((entry) => entry.account.agent)).toEqual([
"claude",
"agy",
]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,9 @@ import type {
UsageAccount,
UsageQuotaWindow,
} from "renderer/hooks/host-service/useHostUsageQuota";
import { QUOTA_AGENT_ORDER } from "renderer/utils/usage/quotaAgentLabels";
import { isModelScopedWindow } from "../isModelScopedWindow";

const RAIL_AGENT_ORDER: UsageAccount["agent"][] = [
"claude",
"codex",
"grok",
"agy",
"opencode",
];

export const MAX_RAIL_USAGE_ACCOUNTS = 2;
const MAX_RINGS_PER_ACCOUNT = 2;

export interface RailUsageEntry {
Expand All @@ -36,8 +28,10 @@ function ringWindows(windows: UsageQuotaWindow[]): UsageQuotaWindow[] {

export function selectRailUsage(
accounts: ReadonlyArray<UsageAccount>,
hiddenProviders: readonly UsageAccount["agent"][] = [],
): RailUsageEntry[] {
return RAIL_AGENT_ORDER.flatMap((agent) => {
return QUOTA_AGENT_ORDER.flatMap((agent) => {
if (hiddenProviders.includes(agent)) return [];
const readable = accounts.filter(
(account) =>
account.agent === agent &&
Expand All @@ -49,5 +43,5 @@ export function selectRailUsage(
if (!account) return [];
const rings = ringWindows(account.windows);
return rings.length > 0 ? [{ account, rings }] : [];
}).slice(0, MAX_RAIL_USAGE_ACCOUNTS);
});
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,72 @@
import { Trans } from "@lingui/react/macro";
import { Button } from "@superset/ui/button";
import {
DropdownMenu,
DropdownMenuCheckboxItem,
DropdownMenuContent,
DropdownMenuTrigger,
} from "@superset/ui/dropdown-menu";
import { Label } from "@superset/ui/label";
import { Switch } from "@superset/ui/switch";
import { useSettings } from "renderer/stores/settings";
import {
QUOTA_AGENT_LABELS,
QUOTA_AGENT_ORDER,
} from "renderer/utils/usage/quotaAgentLabels";

export function UsageRingsToggle() {
const isShown = useSettings((s) => s.showRailUsage);
const updateSetting = useSettings((s) => s.update);
const hiddenProviders = useSettings((s) => s.hiddenRailUsageProviders);
const toggleProvider = useSettings((s) => s.toggleRailUsageProvider);

return (
<div className="flex shrink-0 items-center gap-3">
<Label
htmlFor="usage-rail-rings"
className="text-xs text-muted-foreground"
>
<Trans>Show quota rings on sidebar</Trans>
</Label>
<span id="usage-rail-rings-description" className="sr-only">
<Trans>
Show how much quota each model provider has used, at the bottom of the
sidebar
</Trans>
</span>
<Switch
id="usage-rail-rings"
aria-describedby="usage-rail-rings-description"
checked={isShown}
onCheckedChange={(shown) => updateSetting("showRailUsage", shown)}
/>
<div className="flex min-w-0 flex-wrap items-center gap-x-3 gap-y-2">
<div className="flex min-w-0 items-center gap-3">
<Label
id="usage-rail-rings-label"
htmlFor="usage-rail-rings"
className="text-xs leading-snug text-muted-foreground"
>
<Trans>Show quota rings on sidebar</Trans>
</Label>
<span id="usage-rail-rings-description" className="sr-only">
<Trans>
Show how much quota each model provider has used, at the bottom of
the sidebar
</Trans>
</span>
<Switch
id="usage-rail-rings"
aria-describedby="usage-rail-rings-description"
checked={isShown}
onCheckedChange={(shown) => updateSetting("showRailUsage", shown)}
/>
</div>
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button
variant="outline"
size="sm"
disabled={!isShown}
aria-describedby="usage-rail-rings-label"
>
<Trans>Customize</Trans>
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
{QUOTA_AGENT_ORDER.map((agent) => (
<DropdownMenuCheckboxItem
key={agent}
checked={!hiddenProviders.includes(agent)}
onCheckedChange={() => toggleProvider(agent)}
onSelect={(event) => event.preventDefault()}
>
{QUOTA_AGENT_LABELS[agent]}
</DropdownMenuCheckboxItem>
))}
</DropdownMenuContent>
</DropdownMenu>
</div>
);
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
import type { UsageAccount } from "renderer/hooks/host-service/useHostUsageQuota";
import { QUOTA_AGENT_ORDER } from "renderer/utils/usage/quotaAgentLabels";

export type QuotaAgent = UsageAccount["agent"];

/** Agents whose logins Superset can add, switch, and remove. */
export type ManagedAgent = "claude" | "codex";

const AGENT_ORDER: QuotaAgent[] = [
"claude",
"codex",
"grok",
"agy",
"opencode",
];

export function isManagedAgent(agent: QuotaAgent): agent is ManagedAgent {
return agent === "claude" || agent === "codex";
}
Expand All @@ -27,7 +20,7 @@ export function isManagedAgent(agent: QuotaAgent): agent is ManagedAgent {
export function visibleQuotaAgents(
accounts: ReadonlyArray<Pick<UsageAccount, "agent">>,
): QuotaAgent[] {
return AGENT_ORDER.filter(
return QUOTA_AGENT_ORDER.filter(
(agent) =>
isManagedAgent(agent) ||
accounts.some((account) => account.agent === agent),
Expand Down
10 changes: 10 additions & 0 deletions apps/desktop/src/renderer/stores/settings.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { UsageAccount } from "renderer/hooks/host-service/useHostUsageQuota";
import { create } from "zustand";
import { persist } from "zustand/middleware";

Expand All @@ -13,10 +14,12 @@ interface Settings {
dictationEnabled: boolean;
showParadevAnywhereButton: boolean;
showRailUsage: boolean;
hiddenRailUsageProviders: UsageAccount["agent"][];
}

interface SettingsStore extends Settings {
update: <K extends keyof Settings>(key: K, value: Settings[K]) => void;
toggleRailUsageProvider: (provider: UsageAccount["agent"]) => void;
}

export const useSettings = create<SettingsStore>()(
Expand All @@ -29,7 +32,14 @@ export const useSettings = create<SettingsStore>()(
dictationEnabled: true,
showParadevAnywhereButton: true,
showRailUsage: true,
hiddenRailUsageProviders: [],
update: (key, value) => set({ [key]: value }),
toggleRailUsageProvider: (provider) =>
set(({ hiddenRailUsageProviders }) => ({
hiddenRailUsageProviders: hiddenRailUsageProviders.includes(provider)
? hiddenRailUsageProviders.filter((agent) => agent !== provider)
: [...hiddenRailUsageProviders, provider],
})),
}),
{ name: "settings" },
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { QUOTA_AGENT_LABELS } from "./quotaAgentLabels";
export { QUOTA_AGENT_LABELS, QUOTA_AGENT_ORDER } from "./quotaAgentLabels";
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import { BUILTIN_AGENT_LABELS } from "@superset/shared/agent-catalog";
import pick from "lodash/pick";
import type { UsageAccount } from "renderer/hooks/host-service/useHostUsageQuota";

export const QUOTA_AGENT_LABELS: Record<UsageAccount["agent"], string> = {
export const QUOTA_AGENT_ORDER = [
"claude",
"codex",
"grok",
"agy",
"opencode",
] as const satisfies readonly UsageAccount["agent"][];

export const QUOTA_AGENT_LABELS = {
...pick(BUILTIN_AGENT_LABELS, QUOTA_AGENT_ORDER),
claude: "Claude Code",
codex: "Codex",
grok: "Grok",
agy: "Antigravity",
opencode: "OpenCode",
};
} satisfies Record<UsageAccount["agent"], string>;
Loading