pi-bifrost-budget 1.0.0 → 1.0.1
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/LICENSE +21 -0
- package/README.md +24 -6
- package/extensions/bifrost-daily-budget.ts +72 -121
- package/package.json +22 -9
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Lutraman
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,13 +1,31 @@
|
|
|
1
|
-
#
|
|
1
|
+
# pi-bifrost-budget
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A Pi extension that displays the current Bifrost one-day virtual-key budget in the status bar.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## Requirements
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
- Pi coding agent 1.6 or later
|
|
8
|
+
- A Bifrost provider with an HTTPS API endpoint
|
|
9
|
+
- A virtual key configured either as an `X-Bf-Vk` request header or as a bearer API key beginning with `sk-bf-`. Pi resolves the credentials for the active model; no duplicate key or header is needed.
|
|
8
10
|
|
|
9
|
-
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
Install the package with:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
10
16
|
pi install npm:pi-bifrost-budget
|
|
11
17
|
```
|
|
12
18
|
|
|
13
|
-
|
|
19
|
+
The extension displays usage and the effective daily limit in the status bar, for example:
|
|
20
|
+
|
|
21
|
+
```text
|
|
22
|
+
Bifrost 12.34$/100.00$
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
The status refreshes when a session starts, after model selection, after an agent run, and every 60 seconds. Run `/bifrost-budget` to refresh manually.
|
|
26
|
+
|
|
27
|
+
The extension sends the virtual key only to the active model's HTTPS quota endpoint and does not follow redirects. It shows a warning when the lookup fails and hides the budget on unrelated providers.
|
|
28
|
+
|
|
29
|
+
## Tests
|
|
30
|
+
|
|
31
|
+
Run `npm test` with Node.js 24.
|
|
@@ -1,160 +1,111 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { homedir } from "node:os";
|
|
3
|
-
import { join } from "node:path";
|
|
4
|
-
import type {
|
|
5
|
-
ExtensionAPI,
|
|
6
|
-
ExtensionContext,
|
|
7
|
-
} from "@earendil-works/pi-coding-agent";
|
|
1
|
+
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
8
2
|
|
|
9
3
|
const STATUS_KEY = "bifrost-daily-budget";
|
|
10
4
|
const REFRESH_INTERVAL_MS = 60_000;
|
|
11
|
-
const
|
|
5
|
+
const QUOTA_PATH = "/api/governance/virtual-keys/quota";
|
|
12
6
|
|
|
13
|
-
|
|
7
|
+
class BudgetError extends Error {}
|
|
14
8
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
current_usage?: number;
|
|
18
|
-
reset_duration?: string;
|
|
19
|
-
override_amount?: number;
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
async function readJson(path: string): Promise<JsonObject> {
|
|
23
|
-
return JSON.parse(await readFile(path, "utf8")) as JsonObject;
|
|
9
|
+
function isBifrostProvider(provider: string | undefined): boolean {
|
|
10
|
+
return /^bifrost(?:[-_.]|$)/i.test(provider ?? "");
|
|
24
11
|
}
|
|
25
12
|
|
|
26
|
-
function
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
: undefined;
|
|
30
|
-
}
|
|
13
|
+
async function readDailyBudget(ctx: ExtensionContext): Promise<string | undefined> {
|
|
14
|
+
const model = ctx.model;
|
|
15
|
+
if (!model) return;
|
|
31
16
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
} | undefined> {
|
|
37
|
-
const [modelsFile, authFile] = await Promise.all([
|
|
38
|
-
readJson(join(PI_AGENT_DIR, "models.json")),
|
|
39
|
-
readJson(join(PI_AGENT_DIR, "auth.json")),
|
|
40
|
-
]);
|
|
41
|
-
|
|
42
|
-
const providers = asObject(modelsFile.providers);
|
|
43
|
-
const provider = asObject(providers?.[providerName]);
|
|
44
|
-
const baseUrl = provider?.baseUrl;
|
|
45
|
-
if (typeof baseUrl !== "string") return undefined;
|
|
46
|
-
|
|
47
|
-
const headers = asObject(provider.headers);
|
|
48
|
-
const virtualKeyHeader = Object.entries(headers ?? {}).find(
|
|
49
|
-
([name]) => name.toLowerCase() === "x-bf-vk",
|
|
50
|
-
);
|
|
51
|
-
if (!virtualKeyHeader || typeof virtualKeyHeader[1] !== "string") return undefined;
|
|
52
|
-
|
|
53
|
-
const auth = asObject(authFile[providerName]);
|
|
54
|
-
const authEnv = asObject(auth?.env);
|
|
55
|
-
const configuredValue = virtualKeyHeader[1];
|
|
56
|
-
let virtualKey = configuredValue;
|
|
57
|
-
if (configuredValue.startsWith("$") && configuredValue.length > 1) {
|
|
58
|
-
const variable = configuredValue.slice(1);
|
|
59
|
-
const storedValue = authEnv?.[variable];
|
|
60
|
-
virtualKey = process.env[variable]
|
|
61
|
-
?? (typeof storedValue === "string" ? storedValue : undefined)
|
|
62
|
-
?? (typeof auth?.key === "string" ? auth.key : undefined)
|
|
63
|
-
?? "";
|
|
17
|
+
const auth = await ctx.modelRegistry.getApiKeyAndHeaders(model);
|
|
18
|
+
if (!auth.ok) {
|
|
19
|
+
if (isBifrostProvider(model.provider)) throw new BudgetError("could not resolve the virtual key");
|
|
20
|
+
return;
|
|
64
21
|
}
|
|
65
|
-
if (!virtualKey) return undefined;
|
|
66
|
-
|
|
67
|
-
const quotaUrl = new URL(baseUrl);
|
|
68
|
-
quotaUrl.pathname = "/api/governance/virtual-keys/quota";
|
|
69
|
-
quotaUrl.search = "";
|
|
70
|
-
quotaUrl.hash = "";
|
|
71
|
-
return { quotaUrl, virtualKey };
|
|
72
|
-
}
|
|
73
22
|
|
|
74
|
-
|
|
75
|
-
const
|
|
76
|
-
|
|
23
|
+
// Use Pi's resolved request credentials, not a second interpretation of models.json/auth.json.
|
|
24
|
+
const headers = new Headers();
|
|
25
|
+
for (const [name, value] of Object.entries(auth.headers ?? {})) {
|
|
26
|
+
if (value === null) headers.delete(name);
|
|
27
|
+
else headers.set(name, value);
|
|
28
|
+
}
|
|
29
|
+
const vkHeader = headers.get("x-bf-vk");
|
|
30
|
+
const authorization = headers.get("authorization");
|
|
31
|
+
const bearer = authorization === null ? auth.apiKey : /^Bearer\s+(\S+)$/i.exec(authorization)?.[1];
|
|
32
|
+
const virtualKey = vkHeader || (bearer?.startsWith("sk-bf-") ? bearer : undefined);
|
|
33
|
+
if (!virtualKey) {
|
|
34
|
+
if (isBifrostProvider(model.provider)) throw new BudgetError("no virtual key configured");
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
77
37
|
|
|
78
|
-
const
|
|
79
|
-
|
|
38
|
+
const url = new URL(QUOTA_PATH, auth.baseUrl ?? model.baseUrl);
|
|
39
|
+
if (url.protocol !== "https:") throw new BudgetError("quota endpoint must use HTTPS");
|
|
40
|
+
const response = await fetch(url, {
|
|
41
|
+
headers: { "X-Bf-Vk": virtualKey },
|
|
42
|
+
redirect: "error",
|
|
80
43
|
signal: AbortSignal.timeout(10_000),
|
|
81
44
|
});
|
|
82
|
-
if (!response.ok) {
|
|
83
|
-
|
|
45
|
+
if (!response.ok) throw new BudgetError(`quota request returned HTTP ${response.status}`);
|
|
46
|
+
|
|
47
|
+
const body = await response.json() as { budgets?: unknown } | null;
|
|
48
|
+
if (Array.isArray(body?.budgets)) {
|
|
49
|
+
for (const budget of body.budgets) {
|
|
50
|
+
if (typeof budget?.reset_duration !== "string" || budget.reset_duration.toLowerCase() !== "1d") continue;
|
|
51
|
+
const { current_usage: used, max_limit: limit, override_amount: override } = budget;
|
|
52
|
+
if (!Number.isFinite(used) || !Number.isFinite(limit) || (override != null && !Number.isFinite(override))) continue;
|
|
53
|
+
const total = limit + (override ?? 0);
|
|
54
|
+
if (Number.isFinite(total)) return `${used.toFixed(2)}$/${total.toFixed(2)}$`;
|
|
55
|
+
}
|
|
84
56
|
}
|
|
85
|
-
|
|
86
|
-
const body = asObject(await response.json());
|
|
87
|
-
const budgets = Array.isArray(body?.budgets) ? (body.budgets as Budget[]) : [];
|
|
88
|
-
return budgets.find(
|
|
89
|
-
(budget) => budget.reset_duration?.toLowerCase() === "1d"
|
|
90
|
-
&& Number.isFinite(budget.current_usage)
|
|
91
|
-
&& Number.isFinite(budget.max_limit),
|
|
92
|
-
);
|
|
57
|
+
throw new BudgetError("quota response contains no valid daily budget");
|
|
93
58
|
}
|
|
94
59
|
|
|
95
60
|
export default function (pi: ExtensionAPI) {
|
|
96
61
|
let timer: ReturnType<typeof setInterval> | undefined;
|
|
97
|
-
let
|
|
98
|
-
let activeContext: ExtensionContext | undefined;
|
|
99
|
-
|
|
100
|
-
const refresh = async (ctx: ExtensionContext, reportError = false) => {
|
|
101
|
-
const generation = ++refreshGeneration;
|
|
102
|
-
const provider = ctx.model?.provider;
|
|
103
|
-
if (!provider) {
|
|
104
|
-
ctx.ui.setStatus(STATUS_KEY, undefined);
|
|
105
|
-
return;
|
|
106
|
-
}
|
|
62
|
+
let generation = 0;
|
|
107
63
|
|
|
64
|
+
const refresh = async (ctx: ExtensionContext, manual = false) => {
|
|
65
|
+
const request = ++generation;
|
|
108
66
|
try {
|
|
109
|
-
const budget = await
|
|
110
|
-
if (
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
const used = budget.current_usage!;
|
|
117
|
-
const total = budget.max_limit! + (budget.override_amount ?? 0);
|
|
118
|
-
ctx.ui.setStatus(STATUS_KEY, `${used.toFixed(2)}$/${total.toFixed(2)}$`);
|
|
67
|
+
const budget = await readDailyBudget(ctx);
|
|
68
|
+
if (request !== generation) return;
|
|
69
|
+
ctx.ui.setStatus(STATUS_KEY, budget ? ctx.ui.theme.fg("dim", `Bifrost ${budget}`) : undefined);
|
|
70
|
+
if (!budget && manual) ctx.ui.notify("The active model is not using a Bifrost virtual key.", "info");
|
|
119
71
|
} catch (error) {
|
|
120
|
-
if (
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
);
|
|
127
|
-
}
|
|
72
|
+
if (request !== generation) return;
|
|
73
|
+
// Do not surface arbitrary auth errors or response bodies, which may include secrets.
|
|
74
|
+
const detail = error instanceof BudgetError ? error.message
|
|
75
|
+
: error instanceof Error && error.name === "TimeoutError" ? "request timed out" : "unavailable";
|
|
76
|
+
ctx.ui.setStatus(STATUS_KEY, ctx.ui.theme.fg("warning", `Bifrost budget: ${detail} (/bifrost-budget)`));
|
|
77
|
+
if (manual) ctx.ui.notify(`Bifrost budget: ${detail}.`, "error");
|
|
128
78
|
}
|
|
129
79
|
};
|
|
130
80
|
|
|
131
|
-
|
|
81
|
+
const refreshSelection = (ctx: ExtensionContext) => {
|
|
82
|
+
ctx.ui.setStatus(STATUS_KEY, isBifrostProvider(ctx.model?.provider)
|
|
83
|
+
? ctx.ui.theme.fg("dim", "Bifrost budget: loading…") : undefined);
|
|
84
|
+
void refresh(ctx);
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
pi.on("session_start", (_event, ctx) => {
|
|
132
88
|
if (ctx.mode !== "tui") return;
|
|
133
|
-
|
|
134
|
-
await refresh(ctx);
|
|
89
|
+
refreshSelection(ctx);
|
|
135
90
|
timer = setInterval(() => void refresh(ctx), REFRESH_INTERVAL_MS);
|
|
136
91
|
});
|
|
137
92
|
|
|
138
|
-
pi.on("model_select",
|
|
139
|
-
if (ctx.mode === "tui")
|
|
93
|
+
pi.on("model_select", (_event, ctx) => {
|
|
94
|
+
if (ctx.mode === "tui") refreshSelection(ctx);
|
|
140
95
|
});
|
|
141
96
|
|
|
142
|
-
pi.on("agent_end",
|
|
143
|
-
if (ctx.mode === "tui")
|
|
97
|
+
pi.on("agent_end", (_event, ctx) => {
|
|
98
|
+
if (ctx.mode === "tui") void refresh(ctx);
|
|
144
99
|
});
|
|
145
100
|
|
|
146
101
|
pi.registerCommand("bifrost-budget", {
|
|
147
102
|
description: "Refresh the Bifrost daily budget status",
|
|
148
|
-
handler: async (_args, ctx) =>
|
|
149
|
-
await refresh(ctx, true);
|
|
150
|
-
},
|
|
103
|
+
handler: async (_args, ctx) => refresh(ctx, true),
|
|
151
104
|
});
|
|
152
105
|
|
|
153
|
-
pi.on("session_shutdown",
|
|
154
|
-
|
|
155
|
-
|
|
106
|
+
pi.on("session_shutdown", () => {
|
|
107
|
+
generation++;
|
|
108
|
+
clearInterval(timer);
|
|
156
109
|
timer = undefined;
|
|
157
|
-
activeContext?.ui.setStatus(STATUS_KEY, undefined);
|
|
158
|
-
activeContext = undefined;
|
|
159
110
|
});
|
|
160
111
|
}
|
package/package.json
CHANGED
|
@@ -1,17 +1,30 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-bifrost-budget",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "Display the current Bifrost daily budget in Pi",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"pi-package"
|
|
7
|
+
],
|
|
8
|
+
"license": "MIT",
|
|
5
9
|
"type": "module",
|
|
6
|
-
"
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/tomklino/pi-bifrost-budget.git"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"extensions",
|
|
16
|
+
"README.md",
|
|
17
|
+
"LICENSE"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"test": "node --test tests/*.test.mjs"
|
|
10
21
|
},
|
|
11
22
|
"peerDependencies": {
|
|
12
|
-
"@earendil-works/pi-coding-agent": "
|
|
23
|
+
"@earendil-works/pi-coding-agent": ">=1.6.0"
|
|
13
24
|
},
|
|
14
|
-
"
|
|
15
|
-
"
|
|
25
|
+
"pi": {
|
|
26
|
+
"extensions": [
|
|
27
|
+
"./extensions/bifrost-daily-budget.ts"
|
|
28
|
+
]
|
|
16
29
|
}
|
|
17
30
|
}
|