pi-usage-hub 1.0.0 → 1.0.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 +8 -7
- package/examples/custom-provider.ts +81 -0
- package/package.json +1 -1
- package/examples/auth-providers.ts +0 -42
package/README.md
CHANGED
|
@@ -101,7 +101,7 @@ Built-in providers are the ones already used and smoke-tested by the author. The
|
|
|
101
101
|
|
|
102
102
|
## Pull API
|
|
103
103
|
|
|
104
|
-
The hub caches results for 60 seconds and deduplicates concurrent requests. It never pushes footer text: consumers
|
|
104
|
+
The hub caches results for 60 seconds and deduplicates concurrent requests. It never pushes footer text: consumers start refreshes without blocking lifecycle events, read the cached summary, and re-render when notified.
|
|
105
105
|
|
|
106
106
|
| API | Role |
|
|
107
107
|
|-----|------|
|
|
@@ -135,14 +135,15 @@ const offUpdated = pi.events.on("pi-usage-hub:updated", () => {
|
|
|
135
135
|
requestRender?.();
|
|
136
136
|
});
|
|
137
137
|
|
|
138
|
-
|
|
138
|
+
// Pi awaits lifecycle handlers; refresh in the background and re-render on pi-usage-hub:updated.
|
|
139
|
+
pi.on("session_start", (_event, ctx) => {
|
|
139
140
|
void usageHub?.refresh({ model: ctx.model, force: true });
|
|
140
141
|
});
|
|
141
|
-
pi.on("model_select",
|
|
142
|
-
|
|
142
|
+
pi.on("model_select", (event) => {
|
|
143
|
+
void usageHub?.refresh({ model: event.model, force: true });
|
|
143
144
|
});
|
|
144
|
-
pi.on("agent_end",
|
|
145
|
-
|
|
145
|
+
pi.on("agent_end", (_event, ctx) => {
|
|
146
|
+
void usageHub?.refresh({ model: ctx.model, force: true });
|
|
146
147
|
});
|
|
147
148
|
|
|
148
149
|
// Inside footer render():
|
|
@@ -158,7 +159,7 @@ pi.on("session_shutdown", async () => {
|
|
|
158
159
|
|
|
159
160
|
### L2 provider registration
|
|
160
161
|
|
|
161
|
-
See [`examples/
|
|
162
|
+
See [`examples/custom-provider.ts`](./examples/custom-provider.ts) for a self-contained extension that implements and registers a provider. Copy it into `~/.pi/agent/extensions/`, then adapt the endpoint, credentials, response shape, and labels.
|
|
162
163
|
|
|
163
164
|
```ts
|
|
164
165
|
pi.events.on("pi-usage-hub:ready", (hub) => hub.register(myProvider));
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Example: register a custom usage provider through the L2 event API.
|
|
3
|
+
*
|
|
4
|
+
* Copy this file into ~/.pi/agent/extensions/ and adapt the endpoint,
|
|
5
|
+
* credentials, response shape, and labels for your provider.
|
|
6
|
+
*/
|
|
7
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
8
|
+
|
|
9
|
+
interface UsageReport {
|
|
10
|
+
session?: { pct: number; resetsIn?: string };
|
|
11
|
+
weekly?: { pct: number; resetsIn?: string };
|
|
12
|
+
monthly?: { pct: number; resetsIn?: string };
|
|
13
|
+
error?: string;
|
|
14
|
+
displayText?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface UsageProvider {
|
|
18
|
+
key: string;
|
|
19
|
+
matchProviders?: string[];
|
|
20
|
+
shortLabel: string;
|
|
21
|
+
label: string;
|
|
22
|
+
detect(): boolean;
|
|
23
|
+
fetchUsage(): Promise<UsageReport>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface UsageHub {
|
|
27
|
+
register(provider: UsageProvider): void;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
interface RelayUsageResponse {
|
|
31
|
+
used: number;
|
|
32
|
+
limit: number;
|
|
33
|
+
resetsIn?: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const endpoint = process.env.MY_RELAY_USAGE_URL;
|
|
37
|
+
const token = process.env.MY_RELAY_TOKEN;
|
|
38
|
+
|
|
39
|
+
const provider: UsageProvider = {
|
|
40
|
+
key: "my-relay",
|
|
41
|
+
matchProviders: ["my-relay"],
|
|
42
|
+
shortLabel: "RELAY",
|
|
43
|
+
label: "My Relay",
|
|
44
|
+
detect: () => Boolean(endpoint && token),
|
|
45
|
+
async fetchUsage() {
|
|
46
|
+
try {
|
|
47
|
+
const response = await fetch(endpoint!, {
|
|
48
|
+
headers: { authorization: `Bearer ${token}` },
|
|
49
|
+
});
|
|
50
|
+
if (!response.ok) return { error: `HTTP ${response.status}` };
|
|
51
|
+
|
|
52
|
+
const usage = await response.json() as RelayUsageResponse;
|
|
53
|
+
if (!Number.isFinite(usage.used) || !Number.isFinite(usage.limit) || usage.limit <= 0) {
|
|
54
|
+
return { error: "invalid usage response" };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return {
|
|
58
|
+
monthly: {
|
|
59
|
+
pct: (usage.used / usage.limit) * 100,
|
|
60
|
+
resetsIn: usage.resetsIn,
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
} catch (error) {
|
|
64
|
+
return { error: error instanceof Error ? error.message : String(error) };
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
export default function register(pi: ExtensionAPI) {
|
|
70
|
+
const offReady = pi.events.on("pi-usage-hub:ready", (hub: UsageHub) => {
|
|
71
|
+
hub.register(provider);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
// Covers the case where pi-usage-hub loaded before this extension.
|
|
75
|
+
pi.events.emit("pi-usage-hub:register", provider);
|
|
76
|
+
|
|
77
|
+
pi.on("session_shutdown", async () => {
|
|
78
|
+
offReady();
|
|
79
|
+
pi.events.emit("pi-usage-hub:unregister", { key: provider.key });
|
|
80
|
+
});
|
|
81
|
+
}
|
package/package.json
CHANGED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Example: register session/OAuth usage providers via L2 (not via config).
|
|
3
|
-
*
|
|
4
|
-
* Not loaded by the package. Copy into ~/.pi/agent/extensions/ or adapt.
|
|
5
|
-
*
|
|
6
|
-
* pi.events.on("pi-usage-hub:ready", hub => hub.register(...))
|
|
7
|
-
* pi.events.emit("pi-usage-hub:register", provider)
|
|
8
|
-
*
|
|
9
|
-
* Prefer L1 config when possible:
|
|
10
|
-
*
|
|
11
|
-
* { "name": "xai", "type": "xai", "matchProviders": ["xai-auth", "xai", "grok-cli"] }
|
|
12
|
-
*/
|
|
13
|
-
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
14
|
-
import { makeArkProvider } from "../providers/ark";
|
|
15
|
-
import { makeXaiProvider } from "../providers/xai";
|
|
16
|
-
import { makeKiroProvider } from "../providers/kiro";
|
|
17
|
-
import type { UsageHub, UsageProvider } from "../types";
|
|
18
|
-
|
|
19
|
-
const providers: UsageProvider[] = [
|
|
20
|
-
makeArkProvider("ark", { matchProviders: ["ark"] }),
|
|
21
|
-
makeXaiProvider("xai", {
|
|
22
|
-
matchProviders: ["xai-auth", "xai", "grok-cli"],
|
|
23
|
-
}),
|
|
24
|
-
makeKiroProvider("kiro", { matchProviders: ["kiro"] }),
|
|
25
|
-
];
|
|
26
|
-
|
|
27
|
-
export default function register(pi: ExtensionAPI) {
|
|
28
|
-
const attach = (hub: UsageHub) => {
|
|
29
|
-
for (const p of providers) hub.register(p);
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
const offReady = pi.events.on("pi-usage-hub:ready", (hub: UsageHub) => {
|
|
33
|
-
attach(hub);
|
|
34
|
-
});
|
|
35
|
-
for (const p of providers) {
|
|
36
|
-
pi.events.emit("pi-usage-hub:register", p);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
pi.on("session_shutdown", async () => {
|
|
40
|
-
offReady();
|
|
41
|
-
});
|
|
42
|
-
}
|