pi-cliproxyapi 0.4.1 → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.ts CHANGED
@@ -168,7 +168,7 @@ async function refreshQuotaStatus(
168
168
  };
169
169
  setStatus(key: string, text: string | undefined): void;
170
170
  },
171
- model: { provider: string } | undefined,
171
+ model: { provider: string; id?: string } | undefined,
172
172
  opts: { readOnly?: boolean } = {},
173
173
  ): Promise<void> {
174
174
  if (!model) {
@@ -180,7 +180,7 @@ async function refreshQuotaStatus(
180
180
  ui.setStatus(QUOTA_STATUS_KEY, undefined);
181
181
  return;
182
182
  }
183
- const rendered = renderQuotaSegment(doc, model.provider, ui.theme);
183
+ const rendered = renderQuotaSegment(doc, model.provider, ui.theme, model.id);
184
184
  ui.setStatus(QUOTA_STATUS_KEY, rendered ?? undefined);
185
185
  }
186
186
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-cliproxyapi",
3
- "version": "0.4.1",
3
+ "version": "0.4.2",
4
4
  "type": "module",
5
5
  "description": "Pi extension for corporate management of model providers via a single CliProxyAPI endpoint",
6
6
  "license": "MIT",
@@ -58,6 +58,36 @@ function is7dGroup(g: UsageGroup): boolean {
58
58
  return s.includes("7d") || s.includes("seven-day") || s.includes("seven day");
59
59
  }
60
60
 
61
+ /**
62
+ * A model-scoped weekly window, e.g. `seven-day-fable`, as opposed to the
63
+ * account-wide `seven-day`.
64
+ */
65
+ function scopedModelName(g: UsageGroup): string | null {
66
+ const id = g.id.toLowerCase();
67
+ const prefix = "seven-day-";
68
+ return id.startsWith(prefix) ? id.slice(prefix.length) || null : null;
69
+ }
70
+
71
+ /**
72
+ * Does a scoped window belong to the model currently selected?
73
+ *
74
+ * Windows are named after the model family (`Fable` -> `seven-day-fable`) while
75
+ * model ids carry a version (`claude-fable-5`), so this looks for the family
76
+ * name as a delimited word inside the id rather than comparing the two
77
+ * directly. Done without a regex because the scope comes from the server.
78
+ */
79
+ function scopeMatchesModel(scope: string, modelID: string): boolean {
80
+ const isWordChar = (c: string | undefined) =>
81
+ c !== undefined && /[a-z0-9]/.test(c);
82
+ const id = modelID.toLowerCase();
83
+ for (let i = id.indexOf(scope); i !== -1; i = id.indexOf(scope, i + 1)) {
84
+ const before = id[i - 1];
85
+ const after = id[i + scope.length];
86
+ if (!isWordChar(before) && !isWordChar(after)) return true;
87
+ }
88
+ return false;
89
+ }
90
+
61
91
  interface WindowAggregate {
62
92
  fraction: number;
63
93
  }
@@ -66,10 +96,8 @@ interface WindowAggregate {
66
96
  * Aggregate a window period across live accounts.
67
97
  *
68
98
  * Semantics: within each account, take the MIN remaining among matching
69
- * windows (the account's bottleneck for that period — e.g. claude has both
70
- * a general "7d Weekly" and a model-specific "7d Sonnet" window; the tighter
71
- * one governs). Then take the MAX across accounts (the router will dispatch
72
- * to the account with the most headroom).
99
+ * windows (the account's bottleneck for that period). Then take the MAX across
100
+ * accounts (the router will dispatch to the account with the most headroom).
73
101
  */
74
102
  function aggregateWindow(
75
103
  accounts: UsageAccount[],
@@ -124,6 +152,9 @@ function pct(fraction: number): number {
124
152
  * @param doc - usage document from /api/usage
125
153
  * @param piProvider - Pi provider name of the current model (e.g. "anthropic")
126
154
  * @param theme - Pi theme for coloring (from ctx.ui.theme)
155
+ * @param modelID - id of the selected model (e.g. "claude-fable-5"). When the
156
+ * provider reports a weekly window scoped to that model, it is shown as its
157
+ * own segment and excluded from the account-wide figure.
127
158
  */
128
159
  export function renderQuotaSegment(
129
160
  doc: UsageDocument,
@@ -132,6 +163,7 @@ export function renderQuotaSegment(
132
163
  theme: {
133
164
  fg(color: "success" | "warning" | "error" | "dim", text: string): string;
134
165
  },
166
+ modelID?: string,
135
167
  ): string | null {
136
168
  const usageKey = providerToUsageKey(piProvider);
137
169
  if (!usageKey) return null;
@@ -139,24 +171,56 @@ export function renderQuotaSegment(
139
171
  const accounts = doc.accounts.filter((a) => a.provider === usageKey);
140
172
  if (accounts.length === 0) return null;
141
173
 
174
+ // A weekly window scoped to the selected model, e.g. Fable. It is a sub-cap
175
+ // on the weekly pool rather than a separate allowance, so it is shown
176
+ // alongside the account-wide weekly figure, never instead of it.
177
+ const scopeOf = (g: UsageGroup) => (modelID ? scopedModelName(g) : null);
178
+ const isSelectedModelWindow = (g: UsageGroup) => {
179
+ const scope = scopeOf(g);
180
+ return scope !== null && scopeMatchesModel(scope, modelID as string);
181
+ };
182
+
142
183
  const w5 = aggregateWindow(accounts, is5hGroup);
143
- const w7 = aggregateWindow(accounts, is7dGroup);
184
+ // Other models' scoped windows say nothing about the model in use, and
185
+ // including them used to drag the weekly figure down for no reason.
186
+ const w7 = aggregateWindow(
187
+ accounts,
188
+ (g) => is7dGroup(g) && scopeOf(g) === null,
189
+ );
190
+ const wModel = aggregateWindow(accounts, isSelectedModelWindow);
144
191
 
145
- // No 5h/7d windows found → hide the segment entirely
146
- if (!w5 && !w7) return null;
192
+ // Nothing to show → hide the segment entirely
193
+ if (!w5 && !w7 && !wModel) return null;
147
194
 
148
195
  const parts: string[] = [theme.fg("dim", GAUGE_ICON)];
149
-
150
- if (w5) {
196
+ const push = (label: string, fraction: number) => {
197
+ const color = colorForFraction(fraction);
151
198
  parts.push(
152
- `${theme.fg("dim", "5h")} ${theme.fg(colorForFraction(w5.fraction), brailleFor(w5.fraction))} ${theme.fg(colorForFraction(w5.fraction), String(pct(w5.fraction)))}`,
199
+ `${theme.fg("dim", label)} ${theme.fg(color, brailleFor(fraction))} ${theme.fg(color, String(pct(fraction)))}`,
153
200
  );
154
- }
155
- if (w7) {
156
- parts.push(
157
- `${theme.fg("dim", "7d")} ${theme.fg(colorForFraction(w7.fraction), brailleFor(w7.fraction))} ${theme.fg(colorForFraction(w7.fraction), String(pct(w7.fraction)))}`,
158
- );
159
- }
201
+ };
202
+
203
+ if (w5) push("5h", w5.fraction);
204
+ if (w7) push("7d", w7.fraction);
205
+ if (wModel)
206
+ push(modelWindowLabel(accounts, modelID as string), wModel.fraction);
160
207
 
161
208
  return parts.join(" ");
162
209
  }
210
+
211
+ /**
212
+ * Label for the selected model's own weekly window, taken from the group so it
213
+ * reads the way the provider names it ("7d Fable" -> "Fable").
214
+ */
215
+ function modelWindowLabel(accounts: UsageAccount[], modelID: string): string {
216
+ for (const a of accounts) {
217
+ for (const g of a.groups ?? []) {
218
+ const scope = scopedModelName(g);
219
+ if (scope && scopeMatchesModel(scope, modelID)) {
220
+ const label = (g.label ?? "").replace(/^7d\s+/i, "").trim();
221
+ return label || scope;
222
+ }
223
+ }
224
+ }
225
+ return "model";
226
+ }