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
43 changes: 43 additions & 0 deletions packages/core/test/webview-rate-limit.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { describe, expect, it } from "vitest";
import { initialState, reducer } from "../../../surfaces/webview/src/state/store";

describe("webview plan limits", () => {
const claude = { type: "rateLimit", cli: "claude", utilization: 73, window: "five_hour", status: "allowed" };

it("keeps a plan reading with its engine through local and host switches", () => {
let state = reducer(initialState, claude);
state = reducer(state, { type: "__switchEngine", cli: "codex" });
expect(state.rateLimits[state.cli]).toBeUndefined();
state = reducer(state, { type: "clear" });
state = reducer(state, { type: "usage", contextTokens: 24_000 });
expect(state.contextTokens).toBe(24_000);
expect(state.rateLimits[state.cli]).toBeUndefined();
state = reducer(state, { type: "platform", cli: "claude" });
expect(state.rateLimits[state.cli]?.utilization).toBe(73);
});

it("files a late Claude frame under Claude while Codex is selected", () => {
let state = reducer(initialState, { type: "platform", cli: "codex" });
state = reducer(state, claude);
expect(state.rateLimits.codex).toBeUndefined();
expect(state.rateLimits.claude?.utilization).toBe(73);
});

it("preserves status-only readings within the reporting engine", () => {
let state = reducer(initialState, claude);
state = reducer(state, { type: "rateLimit", cli: "codex", status: "allowed_warning" });
expect(state.rateLimits.codex?.utilization).toBeUndefined();
state = reducer(state, { type: "rateLimit", cli: "claude", status: "allowed_warning" });
expect(state.rateLimits.claude?.utilization).toBe(73);
expect(state.rateLimits.claude?.status).toBe("allowed_warning");
state = reducer(state, { ...claude, status: "rejected", utilization: 100 });
expect(state.rateLimits.claude?.utilization).toBe(100);
});
});

it("keeps a late Claude quota out of Custom when that engine is available", () => {
let state = reducer(initialState, { type: "platform", cli: "custom" });
state = reducer(state, { type: "rateLimit", cli: "claude", utilization: 91, status: "allowed" });
expect(state.rateLimits[state.cli]).toBeUndefined();
expect(state.rateLimits.claude.utilization).toBe(91);
});
35 changes: 18 additions & 17 deletions surfaces/webview/src/chat/Composer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function CtxDot({ tokens, window: win, compacting }: { tokens: number; window: n
return (
<span
ref={rootRef}
className="ml-auto relative flex items-center"
className="relative flex items-center"
title={compacting ? "Compacting context…" : `Context: ${tokens.toLocaleString()} / ${win.toLocaleString()} tokens (${pct}%)\n${fmtK(tokens)} / ${fmtK(win)} ctx`}
>
{open && (
Expand Down Expand Up @@ -223,6 +223,7 @@ function ChipGroup({ label, value, options, onPick, accent = "green" }: {
// Input + engine tabs + model/effort pickers. FROZEN while an approval is pending.
export function Composer() {
const { state, send, selectEngine, switchEngine, queueCount, markCompacting } = useStore();
const limit = state.rateLimits[state.cli];
const [text, setText] = useState("");
const [effort, setEffort] = useState("default");
const [model, setModel] = useState("default");
Expand Down Expand Up @@ -537,7 +538,7 @@ export function Composer() {
>
{/* engine segmented control + a single "controls" disclosure (model/effort/mode
live in the popover so the bar stays clean on a phone) */}
<div className="relative mb-2 flex items-center gap-1.5 text-xs">
<div className="relative mb-2 flex flex-wrap items-center gap-1.5 text-xs">
<div className="an-term-seg">
{(["claude", "codex"] as Cli[]).map((c) => {
const on = state.cli === c;
Expand Down Expand Up @@ -565,21 +566,21 @@ export function Composer() {
<span className="max-w-[7rem] truncate">{MODES[state.cli].find((m) => m.value === mode)?.label ?? mode}</span>
<svg width="10" height="10" viewBox="0 0 10 10" fill="none" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" style={{ transform: controlsOpen ? "rotate(180deg)" : "none", transition: "transform .15s" }}><path d="M2.5 4l2.5 2.5L7.5 4" /></svg>
</button>
{state.limitPct !== undefined && state.limitPct >= 50 && (
<span className="ml-auto mr-2 flex items-center">
<LimitDot pct={state.limitPct} window={state.limitWindow} resetsAt={state.limitResetsAt} status={state.limitStatus} />
</span>
)}
{(state.contextTokens !== undefined || state.isCompacting) && (() => {
const tokens = state.contextTokens ?? 0;
const win = state.contextWindow ?? (state.cli === "codex" ? 256_000 : 200_000);
return <CtxDot tokens={tokens} window={win} compacting={state.isCompacting} />;
})()}
{queueCount > 0 && (
<span className="ml-auto animate-pulse text-[10px]" style={{ color: "var(--an-amber)" }}>
⏳ {queueCount} queued
</span>
)}
<span className="ml-auto flex items-center gap-2">
{limit?.utilization !== undefined && limit.utilization >= 50 && (
<LimitDot pct={limit.utilization} window={limit.window} resetsAt={limit.resetsAt} status={limit.status} />
)}
{(state.contextTokens !== undefined || state.isCompacting) && (() => {
const tokens = state.contextTokens ?? 0;
const win = state.contextWindow ?? (state.cli === "codex" ? 256_000 : 200_000);
return <CtxDot tokens={tokens} window={win} compacting={state.isCompacting} />;
})()}
{queueCount > 0 && (
<span className="animate-pulse text-[10px]" style={{ color: "var(--an-amber)" }}>
⏳ {queueCount} queued
</span>
)}
</span>

{controlsOpen && (
<>
Expand Down
18 changes: 8 additions & 10 deletions surfaces/webview/src/state/store.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,7 @@ export interface State {
contextWindow?: number;
// account-wide plan rate-limit utilization (claude.ai), for the "running low" gauge.
// Unlike context, this is NOT reset on a new chat — it tracks the plan, not the session.
limitPct?: number; // 0-100 utilization of the active window
limitWindow?: string; // 'five_hour' | 'seven_day' | ...
limitResetsAt?: number; // epoch ms when the active window resets
limitStatus?: string; // 'allowed' | 'allowed_warning' | 'rejected'
rateLimits: Partial<Record<Cli, Extract<ServerMessage, { type: "rateLimit" }>>>;
isCompacting: boolean;
currentModel?: string;
// live model catalog per engine, seeded from the static baseline and upgraded when the
Expand All @@ -165,7 +162,8 @@ export function isApprovalForView(a: { sessionId?: string }, activeSessionId?: s
return !a.sessionId || !activeSessionId || a.sessionId === activeSessionId;
}

const initialState: State = {
export const initialState: State = {
rateLimits: {},
phase: "connecting",
walletAddress: null,
cli: "claude",
Expand Down Expand Up @@ -313,7 +311,7 @@ type LocalAction =
| { type: "__clearCelebrate" };
type Action = ServerMessage | LocalAction;

function reducer(state: State, ev: Action): State {
export function reducer(state: State, ev: Action): State {
switch (ev.type) {
case "__typing":
return { ...state, typing: true };
Expand Down Expand Up @@ -396,10 +394,10 @@ function reducer(state: State, ev: Action): State {
// percentage so the gauge never unmounts on a status change.
return {
...state,
limitPct: ev.utilization ?? state.limitPct,
limitWindow: ev.window,
limitResetsAt: ev.resetsAt,
limitStatus: ev.status,
rateLimits: {
...state.rateLimits,
[ev.cli]: { ...ev, utilization: ev.utilization ?? state.rateLimits[ev.cli]?.utilization },
},
};
case "__compactStart":
return { ...state, isCompacting: true };
Expand Down
Loading