pi-cliproxyapi 0.4.0 → 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/README.md +40 -67
- package/index.ts +2 -2
- package/package.json +1 -1
- package/src/config.ts +2 -2
- package/src/status-quota.ts +80 -16
- package/src/ui-setup.ts +1 -9
- package/src/usage-shared-cache.ts +9 -2
package/README.md
CHANGED
|
@@ -71,7 +71,7 @@ Extra Models keys: `d` removes a custom group (with confirmation).
|
|
|
71
71
|
|
|
72
72
|
You need a running [CliProxyAPI](https://github.com/router-for-me/CLIProxyAPI) instance — this is the corporate LLM proxy that aggregates multiple providers behind a single OpenAI-compatible endpoint.
|
|
73
73
|
|
|
74
|
-
For full functionality (Usage tab, enriched model metadata from [models.dev](https://models.dev)), also
|
|
74
|
+
For full functionality (Usage tab, enriched model metadata from [models.dev](https://models.dev)), also install the **[pi-bridge](https://github.com/abix5/pi-cliproxyapi-bridge)** plugin into that instance. See [Discovery](#discovery) below.
|
|
75
75
|
|
|
76
76
|
## Install
|
|
77
77
|
|
|
@@ -117,8 +117,7 @@ cache.
|
|
|
117
117
|
"proxy": {
|
|
118
118
|
"endpoint": "https://proxy.example.com/v1",
|
|
119
119
|
"apiKey": "!cat ~/.pi/agent/pi-cliproxyapi/key",
|
|
120
|
-
"providerPrefix": "corp"
|
|
121
|
-
"usageKey": "!cat ~/.pi/agent/pi-cliproxyapi/usage-key"
|
|
120
|
+
"providerPrefix": "corp"
|
|
122
121
|
},
|
|
123
122
|
"builtinProviders": {
|
|
124
123
|
"anthropic": { "enabled": true, "models": ["claude-opus-4-7"] },
|
|
@@ -137,82 +136,56 @@ Values support `!command` (shell exec), `$ENV_VAR`, or literal strings. The `/cl
|
|
|
137
136
|
|
|
138
137
|
## Discovery
|
|
139
138
|
|
|
140
|
-
The
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
The **[pi-cliproxyapi-wellknown](https://github.com/abix5/pi-cliproxyapi-wellknown)** sidecar runs alongside CliProxyAPI and provides:
|
|
145
|
-
|
|
146
|
-
- `/.well-known/pi` — model discovery with metadata from [models.dev](https://models.dev) (context windows, costs, reasoning flags)
|
|
147
|
-
- `/api/usage` — per-account quota windows used by the hub Usage tab
|
|
139
|
+
The extension reads the model catalogue and provider quota from the
|
|
140
|
+
**[pi-bridge](https://github.com/abix5/pi-cliproxyapi-bridge)** plugin running
|
|
141
|
+
inside CliProxyAPI, authenticating with the same `apiKey` it uses for model
|
|
142
|
+
calls. No second credential and no extra container are involved.
|
|
148
143
|
|
|
149
144
|
```
|
|
150
|
-
┌──────────────┐
|
|
151
|
-
│ Pi + plugin │────▶│ CliProxyAPI
|
|
152
|
-
│ │ │ /v1/models, /v1/chat/...
|
|
153
|
-
│ │
|
|
154
|
-
│ │
|
|
155
|
-
│
|
|
156
|
-
|
|
157
|
-
│ │ │ /api/usage │
|
|
158
|
-
│ │ └───────────────────────────┘
|
|
159
|
-
└──────────────┘
|
|
145
|
+
┌──────────────┐ ┌────────────────────────────────────────┐
|
|
146
|
+
│ Pi + plugin │────▶│ CliProxyAPI │
|
|
147
|
+
│ │ │ /v1/models, /v1/chat/... │
|
|
148
|
+
│ │ │ /v0/resource/plugins/pi-bridge/… │
|
|
149
|
+
│ │ │ well-known → model catalogue │
|
|
150
|
+
│ │ │ usage → per-account quota │
|
|
151
|
+
└──────────────┘ └────────────────────────────────────────┘
|
|
160
152
|
```
|
|
161
153
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
Clone the sidecar repo next to your CliProxyAPI deployment:
|
|
165
|
-
|
|
166
|
-
```bash
|
|
167
|
-
git clone https://github.com/abix5/pi-cliproxyapi-wellknown.git
|
|
168
|
-
```
|
|
154
|
+
Without the plugin the extension still works: it falls back to raw `/v1/models`
|
|
155
|
+
with local heuristics.
|
|
169
156
|
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
# ... your existing CliProxyAPI service ...
|
|
176
|
-
|
|
177
|
-
pi-cliproxyapi-wellknown:
|
|
178
|
-
build:
|
|
179
|
-
context: ./pi-cliproxyapi-wellknown
|
|
180
|
-
restart: unless-stopped
|
|
181
|
-
ports:
|
|
182
|
-
- "127.0.0.1:3458:3458"
|
|
183
|
-
environment:
|
|
184
|
-
UPSTREAM_MODELS_URL: http://cliproxyapi:8317/v1/models
|
|
185
|
-
UPSTREAM_TOKEN: ${UPSTREAM_TOKEN} # CliProxyAPI bearer key
|
|
186
|
-
PI_PUBLIC_BASE_URL: ${PI_PUBLIC_BASE_URL} # e.g. https://proxy.example.com/v1
|
|
187
|
-
MANAGEMENT_API_URL: http://cliproxyapi:8317/v0/management
|
|
188
|
-
MANAGEMENT_API_KEY: ${MANAGEMENT_API_KEY}
|
|
189
|
-
PI_PLUGIN_USAGE_KEY: ${PI_PLUGIN_USAGE_KEY} # shared with Pi plugin
|
|
190
|
-
depends_on:
|
|
191
|
-
cliproxyapi:
|
|
192
|
-
condition: service_healthy
|
|
193
|
-
networks:
|
|
194
|
-
- your-network
|
|
195
|
-
```
|
|
157
|
+
| | With pi-bridge | Without |
|
|
158
|
+
| --- | --- | --- |
|
|
159
|
+
| Model discovery | Enriched from [models.dev](https://models.dev) — real context windows, costs, reasoning flags | Defaults: `contextWindow=128k`, `maxTokens=16k`, `cost=0` |
|
|
160
|
+
| Usage tab | Per-account quota bars | Unavailable |
|
|
161
|
+
| Classification | Server-side | Local heuristics by `owned_by` |
|
|
196
162
|
|
|
197
|
-
|
|
163
|
+
See the plugin's README for installing it into CliProxyAPI.
|
|
198
164
|
|
|
199
|
-
|
|
165
|
+
## Setup
|
|
200
166
|
|
|
201
167
|
Run `/cliproxy-setup` in Pi and enter:
|
|
202
168
|
|
|
203
169
|
- **endpoint** — your public proxy URL ending with `/v1`
|
|
204
170
|
- **apiKey** — CliProxyAPI bearer key
|
|
205
|
-
- **providerPrefix** — short slug for custom provider names (e.g. `corp
|
|
206
|
-
- **usageKey** — same value as `PI_PLUGIN_USAGE_KEY` above (enables the Usage tab)
|
|
171
|
+
- **providerPrefix** — short slug for custom provider names (e.g. `corp`)
|
|
207
172
|
|
|
208
|
-
|
|
173
|
+
## Migrating from the wellknown sidecar
|
|
209
174
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
175
|
+
Earlier versions read `/.well-known/pi` and `/api/usage` from a separate
|
|
176
|
+
`pi-cliproxyapi-wellknown` container, which needed its own `usageKey`. Version
|
|
177
|
+
0.4.0 reads both from the pi-bridge plugin instead.
|
|
178
|
+
|
|
179
|
+
1. Install pi-bridge into CliProxyAPI (see its README).
|
|
180
|
+
2. Update this extension to 0.4.0 or later.
|
|
181
|
+
3. Confirm the Usage tab still populates — it now says `source=plugin`.
|
|
182
|
+
4. Drop the sidecar's routes from your reverse proxy, then stop the container.
|
|
183
|
+
5. Remove `proxy.usageKey` from the config above; it is no longer read by the
|
|
184
|
+
setup wizard and is only consulted as a fallback.
|
|
185
|
+
|
|
186
|
+
Steps 1–2 are safe in either order: until the plugin answers, the extension
|
|
187
|
+
keeps using the sidecar, and a version older than 0.4.0 keeps working against a
|
|
188
|
+
server that already runs the plugin.
|
|
216
189
|
|
|
217
190
|
## Layout
|
|
218
191
|
|
|
@@ -222,8 +195,8 @@ src/
|
|
|
222
195
|
config.ts ~/.pi/agent/pi-cliproxyapi/config.json
|
|
223
196
|
commands.ts 2 slash commands (hub + setup)
|
|
224
197
|
apply.ts pi.registerProvider calls
|
|
225
|
-
fetch-models.ts
|
|
226
|
-
fetch-usage.ts
|
|
198
|
+
fetch-models.ts catalogue from pi-bridge, /v1/models fallback
|
|
199
|
+
fetch-usage.ts quota from pi-bridge, sidecar fallback, TTL cache
|
|
227
200
|
compat.ts baseUrl derivation, model classification
|
|
228
201
|
conflicts.ts read-only ~/.pi/{models,auth}.json scan
|
|
229
202
|
ui-frame.ts single source of truth for overlay frames
|
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
package/src/config.ts
CHANGED
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
// "discoveryExcludes": ["*:*"],
|
|
23
23
|
// "overrides": {},
|
|
24
24
|
// "refreshIntervalMinutes": 0,
|
|
25
|
-
// "usageCacheTtlMs":
|
|
25
|
+
// "usageCacheTtlMs": 15000
|
|
26
26
|
// }
|
|
27
27
|
|
|
28
28
|
import { execSync } from "node:child_process";
|
|
@@ -143,7 +143,7 @@ const DEFAULT_CONFIG: ProxyConfig = {
|
|
|
143
143
|
discoveryExcludes: ["*:*"],
|
|
144
144
|
overrides: {},
|
|
145
145
|
refreshIntervalMinutes: 0,
|
|
146
|
-
usageCacheTtlMs:
|
|
146
|
+
usageCacheTtlMs: 15_000,
|
|
147
147
|
};
|
|
148
148
|
|
|
149
149
|
export function loadConfig(): ProxyConfig {
|
package/src/status-quota.ts
CHANGED
|
@@ -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
|
|
70
|
-
*
|
|
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
|
-
|
|
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
|
-
//
|
|
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
|
-
|
|
196
|
+
const push = (label: string, fraction: number) => {
|
|
197
|
+
const color = colorForFraction(fraction);
|
|
151
198
|
parts.push(
|
|
152
|
-
`${theme.fg("dim",
|
|
199
|
+
`${theme.fg("dim", label)} ${theme.fg(color, brailleFor(fraction))} ${theme.fg(color, String(pct(fraction)))}`,
|
|
153
200
|
);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
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
|
+
}
|
package/src/ui-setup.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// First-run setup wizard: collects endpoint + apiKey + providerPrefix
|
|
1
|
+
// First-run setup wizard: collects endpoint + apiKey + providerPrefix
|
|
2
2
|
// and writes them to ~/.pi/agent/pi-cliproxyapi/config.json.
|
|
3
3
|
//
|
|
4
4
|
// All three fields support the same "!cmd" / "$ENV" / literal semantics as
|
|
@@ -64,11 +64,6 @@ const STEPS: WizardStep[] = [
|
|
|
64
64
|
validate: (raw) =>
|
|
65
65
|
/^[a-z0-9][a-z0-9-]*$/i.test(raw) ? null : "letters/digits/dashes only",
|
|
66
66
|
},
|
|
67
|
-
{
|
|
68
|
-
label: "usageKey",
|
|
69
|
-
hint: "Optional X-Plugin-Key for /api/usage. Leave blank to skip the Usage tab",
|
|
70
|
-
required: false,
|
|
71
|
-
},
|
|
72
67
|
];
|
|
73
68
|
|
|
74
69
|
/**
|
|
@@ -96,7 +91,6 @@ export async function runSetup(
|
|
|
96
91
|
endpoint: existing.proxy.endpoint ?? "",
|
|
97
92
|
apiKey: existing.proxy.apiKey ?? "",
|
|
98
93
|
providerPrefix: existing.proxy.providerPrefix ?? "",
|
|
99
|
-
usageKey: existing.proxy.usageKey ?? "",
|
|
100
94
|
};
|
|
101
95
|
|
|
102
96
|
let cancelled = false;
|
|
@@ -141,8 +135,6 @@ export async function runSetup(
|
|
|
141
135
|
apiKey: values.apiKey ?? "",
|
|
142
136
|
providerPrefix: values.providerPrefix ?? "",
|
|
143
137
|
};
|
|
144
|
-
if (values.usageKey) next.proxy.usageKey = values.usageKey;
|
|
145
|
-
else delete next.proxy.usageKey;
|
|
146
138
|
|
|
147
139
|
saveConfig(next);
|
|
148
140
|
ctx.ui.notify(`saved to ${CONFIG_PATH}`, "info");
|
|
@@ -28,8 +28,15 @@ import { log } from "./log.ts";
|
|
|
28
28
|
export const USAGE_CACHE_PATH = join(CONFIG_DIR, "usage-cache.json");
|
|
29
29
|
export const USAGE_LOCK_PATH = join(CONFIG_DIR, "usage-cache.lock");
|
|
30
30
|
|
|
31
|
-
/**
|
|
32
|
-
|
|
31
|
+
/**
|
|
32
|
+
* Minimum interval between network fetches, shared across all Pi instances.
|
|
33
|
+
*
|
|
34
|
+
* This throttles calls to the bridge only, which are cheap: the bridge holds
|
|
35
|
+
* its own cache and is the only thing that talks to the providers, whose rate
|
|
36
|
+
* limits are the real constraint. So it can be short enough for the status line
|
|
37
|
+
* to react to a finished turn.
|
|
38
|
+
*/
|
|
39
|
+
export const USAGE_CACHE_TTL_MS = 15_000;
|
|
33
40
|
/** A lock file older than this is considered stale (process crashed). */
|
|
34
41
|
const LOCK_STALE_MS = 30_000;
|
|
35
42
|
|