Skip to content
Closed
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
11 changes: 11 additions & 0 deletions apps/web/src/components/settings/AppearanceSettingsPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,17 @@ export function AppearanceSettingsPanel() {
}
/>
<ThreadDetailsItemsRow />
<SettingsRow
{...searchableSetting("sidebar-provider-usage")}
description="Show provider allowances in the sidebar footer. Select a circle to open Usage."
control={
<Switch
aria-label="Provider usage circles"
checked={settings.sidebarProviderUsage}
onCheckedChange={(sidebarProviderUsage) => updateSettings({ sidebarProviderUsage })}
/>
}
/>
<SettingsRow
{...searchableSetting("effort-display")}
description="Show reasoning effort in the composer as its name, such as High, or as bars."
Expand Down
6 changes: 6 additions & 0 deletions apps/web/src/components/settings/settingsSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,12 @@ export const SETTINGS_SEARCH_ITEMS = [
to: "/settings/appearance",
searchTerms: ["reasoning effort thinking bars graph word label level composer"],
},
{
id: "sidebar-provider-usage",
title: "Provider usage circles",
to: "/settings/appearance",
searchTerms: ["sidebar footer quota allowance subscription limits rings providers"],
},
{
id: "math-rendering",
title: "Render math",
Expand Down
20 changes: 13 additions & 7 deletions apps/web/src/components/sidebar/SidebarChrome.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { memo } from "react";
import { Link, useLocation, useNavigate } from "@tanstack/react-router";

import { useAtomValue } from "@effect/atom-react";
import { useEnvironmentIdentificationMode } from "../../hooks/useSettings";
import { useClientSettings, useEnvironmentIdentificationMode } from "../../hooks/useSettings";
import { shortcutLabelForCommand } from "../../keybindings";
import {
navigateHistory,
Expand Down Expand Up @@ -36,6 +36,7 @@ import { readPullRequestListPreferences } from "../pullRequest/pullRequestListPr
import { isSidebarUtilityPage, useNavigateToMainApp } from "./mainAppLocation";
import { SidebarThreadUndoNotice } from "./SidebarThreadUndoNotice";
import { SidebarProviderUpdatePill } from "./SidebarProviderUpdatePill";
import { SidebarProviderUsage } from "./SidebarProviderUsage";
import { SidebarUpdateArchitectureWarning, SidebarUpdatePill } from "./SidebarUpdatePill";
import { PullRequestGlyph } from "~/components/pullRequest/pullRequestIcons";
import { observeResize } from "~/lib/observeResize";
Expand Down Expand Up @@ -226,6 +227,7 @@ const SidebarUtilityMenu = memo(function SidebarUtilityMenu() {
select: (location) => isSidebarUtilityPage(location.pathname),
});
const pullRequestsSupported = usePullRequestsSupported();
const showProviderUsage = useClientSettings((settings) => settings.sidebarProviderUsage);
const closeMobileSidebar = () => {
if (isMobile) {
setOpenMobile(false);
Expand Down Expand Up @@ -278,14 +280,18 @@ const SidebarUtilityMenu = memo(function SidebarUtilityMenu() {
onClick={handlePullRequestsClick}
/>
) : null}
<SidebarUtilityItem
icon={<ChartNoAxesColumnIcon />}
label="Usage"
onClick={handleUsageClick}
/>
{showProviderUsage ? (
<SidebarProviderUsage onClick={handleUsageClick} />
) : (
<SidebarUtilityItem
icon={<ChartNoAxesColumnIcon />}
label="Usage"
onClick={handleUsageClick}
/>
)}
</>
)}
<SidebarThreadUndoNotice />
<SidebarThreadUndoNotice reserveActionSpace={showProviderUsage && !isOnUtilityPage} />
<SidebarUpdatePill />
</SidebarMenu>
);
Expand Down
133 changes: 133 additions & 0 deletions apps/web/src/components/sidebar/SidebarProviderUsage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import { useAtomValue } from "@effect/atom-react";
import {
collectLimitAccounts,
collectLimitPools,
displayLimitWindows,
} from "@t3tools/shared/usageLimits";
import { ChartNoAxesColumnIcon } from "lucide-react";
import { useState } from "react";

import { useNowMinute } from "../../hooks/useNowMinute";
import { observeResize } from "../../lib/observeResize";
import { environmentPresentations } from "../../state/presentation";
import { ProviderInstanceIcon } from "../chat/ProviderInstanceIcon";
import { SidebarMenuButton, SidebarMenuItem } from "../ui/sidebar";
import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
import { PROVIDER_PRESENTATION } from "../usage/usageProviders";

export function SidebarProviderUsage({ onClick }: { readonly onClick: () => void }) {
const presentations = useAtomValue(environmentPresentations.presentationsWithProvidersAtom);
const nowMinute = useNowMinute();
const now = Date.parse(`${nowMinute}:00Z`);
const pools = collectLimitPools(collectLimitAccounts(presentations), now);
const entries = Object.values(PROVIDER_PRESENTATION).flatMap((provider) => {
const pool = pools.find((entry) => entry.driver === provider.driverKind);
const configured = [...presentations.values()].some((presentation) =>
presentation.serverConfig?.providers.some(
(entry) => entry.driver === provider.driverKind && entry.enabled && entry.installed,
),
);
if (!pool && !configured) return [];
const window = pool ? displayLimitWindows(pool)[0] : undefined;
const expired = window?.members.some(
(member) => member.window.resetsAt && Date.parse(member.window.resetsAt) <= now,
);
return [{ ...provider, window, left: expired ? null : (window?.remainingPercent ?? null) }];
});
const [capacity, setCapacity] = useState(0);
const observeWidth = (element: HTMLLIElement) =>
observeResize(element, ([entry]) => {
if (entry) setCapacity(Math.max(0, Math.floor((entry.contentRect.width + 4) / 36)));
});

return (
<SidebarMenuItem
className="min-w-0 flex-initial"
ref={observeWidth}
style={{ width: Math.max(32, entries.length * 36 - 4) }}
>
<div className="flex min-w-0 items-center gap-1" data-provider-usage>
{entries.length === 0 && capacity > 0 ? (
<Tooltip>
<TooltipTrigger
render={<SidebarMenuButton aria-label="Usage" onClick={onClick} size="icon" />}
>
<ChartNoAxesColumnIcon />
</TooltipTrigger>
<TooltipPopup side="top">Usage</TooltipPopup>
</Tooltip>
) : (
entries.slice(0, capacity).map((entry) => {
const label = `${entry.label}: ${entry.left === null ? "usage unavailable" : `${entry.left}% left`}`;
return (
<Tooltip key={entry.driverKind}>
<TooltipTrigger
render={
<SidebarMenuButton
aria-label={`${label}. Open Usage`}
onClick={onClick}
size="icon"
/>
}
>
<span
className="relative grid size-4 shrink-0 place-items-center"
data-usage-glyph
>
<svg
aria-hidden
className="pointer-events-none absolute inset-0 size-4"
viewBox="0 0 16 16"
>
<circle
cx="8"
cy="8"
r="7"
fill="none"
className={
entry.left === 0 ? "stroke-error/40" : "stroke-sidebar-foreground/20"
}
strokeWidth="1"
strokeDasharray={entry.left === null ? "1 2" : undefined}
/>
{entry.left !== null && entry.left > 0 ? (
<circle
cx="8"
cy="8"
r="7"
fill="none"
pathLength="100"
strokeWidth="1"
strokeDasharray={`${entry.left} 100`}
strokeLinecap="round"
transform="rotate(-90 8 8)"
className={
entry.left <= 10
? "stroke-error"
: entry.left <= 20
? "stroke-warning"
: "stroke-success"
}
/>
) : null}
</svg>
<ProviderInstanceIcon
driverKind={entry.driverKind}
displayName={entry.label}
className="absolute inset-0 grid place-items-center"
iconClassName="block size-[9px]"
/>
</span>
</TooltipTrigger>
<TooltipPopup side="top">
{label}
{entry.window ? ` · ${entry.window.label}` : ""}
</TooltipPopup>
</Tooltip>
);
})
)}
</div>
</SidebarMenuItem>
);
}
8 changes: 6 additions & 2 deletions apps/web/src/components/sidebar/SidebarThreadUndoNotice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import { InlineButton } from "../ui/button";
import { SidebarMenuItem } from "../ui/sidebar";

/** Fills the free space in the sidebar footer's utility row, so it never moves or covers the list. */
export function SidebarThreadUndoNotice() {
export function SidebarThreadUndoNotice({
reserveActionSpace,
}: {
readonly reserveActionSpace: boolean;
}) {
const notice = useThreadUndoNotice((state) => state.notice);
const keybindings = useAtomValue(primaryServerKeybindingsAtom);
// Short enough to fit beside the utility icons; screen readers hear the full summary.
Expand All @@ -31,7 +35,7 @@ export function SidebarThreadUndoNotice() {

return (
<SidebarMenuItem
className="@container flex min-w-0 flex-1"
className={cn("@container flex min-w-0 flex-1", reserveActionSpace && "min-w-6")}
aria-hidden={!summary}
inert={!summary}
>
Expand Down
2 changes: 2 additions & 0 deletions packages/contracts/src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ export const ClientSettingsSchema = Schema.Struct({
),
chatWidth: ChatWidth.pipe(Schema.withDecodingDefault(Effect.succeed("comfortable" as const))),
effortDisplay: EffortDisplay.pipe(Schema.withDecodingDefault(Effect.succeed("label" as const))),
sidebarProviderUsage: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(false))),
loadBalancingEnabled: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(false))),
loadBalancingWeights: LoadBalancingWeights.pipe(Schema.withDecodingDefault(Effect.succeed({}))),
appearanceContrast: AppearanceContrast.pipe(
Expand Down Expand Up @@ -1861,6 +1862,7 @@ export const ClientSettingsPatch = Schema.Struct({
diffColorScheme: Schema.optionalKey(DiffColorScheme),
chatWidth: Schema.optionalKey(ChatWidth),
effortDisplay: Schema.optionalKey(EffortDisplay),
sidebarProviderUsage: Schema.optionalKey(Schema.Boolean),
loadBalancingEnabled: Schema.optionalKey(Schema.Boolean),
loadBalancingWeights: Schema.optionalKey(LoadBalancingWeights),
appearanceContrast: Schema.optionalKey(AppearanceContrast),
Expand Down
Loading