pi-usage-hub 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/README.md +1 -1
- package/examples/custom-provider.ts +81 -0
- package/package.json +1 -1
- package/examples/auth-providers.ts +0 -42
package/README.md
CHANGED
|
@@ -158,7 +158,7 @@ pi.on("session_shutdown", async () => {
|
|
|
158
158
|
|
|
159
159
|
### L2 provider registration
|
|
160
160
|
|
|
161
|
-
See [`examples/
|
|
161
|
+
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
162
|
|
|
163
163
|
```ts
|
|
164
164
|
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
|
-
}
|