@zhexin/pi-agy 0.0.2 → 0.0.4
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 +4 -0
- package/package.json +1 -1
- package/src/ccpa.ts +1 -1
- package/src/index.ts +27 -1
- package/src/usage.ts +62 -0
package/README.md
CHANGED
package/package.json
CHANGED
package/src/ccpa.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,11 +1,37 @@
|
|
|
1
1
|
import { createProvider } from "@earendil-works/pi-ai";
|
|
2
|
-
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import type { ExtensionAPI, ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
|
|
3
3
|
|
|
4
4
|
import { api } from "./api.ts";
|
|
5
5
|
import { oauth } from "./auth.ts";
|
|
6
6
|
import { agyCredential, fetchModels } from "./ccpa.ts";
|
|
7
|
+
import { usage } from "./usage.ts";
|
|
7
8
|
|
|
8
9
|
export default function (pi: ExtensionAPI): void {
|
|
10
|
+
pi.events.on("pi-usage:discover", (data) => {
|
|
11
|
+
const discovery = data as {
|
|
12
|
+
add: (provider: { id: string; label: string; authHint: string }) => void;
|
|
13
|
+
};
|
|
14
|
+
if (typeof discovery?.add !== "function") return;
|
|
15
|
+
discovery.add({
|
|
16
|
+
id: "agy",
|
|
17
|
+
label: "Antigravity",
|
|
18
|
+
authHint: "Run /login agy to authenticate.",
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
pi.events.on("pi-usage:request", async (data) => {
|
|
23
|
+
const request = data as { provider: string; ctx: ExtensionCommandContext } | undefined;
|
|
24
|
+
if (request?.provider !== "agy" || !request.ctx) return;
|
|
25
|
+
const { ctx } = request;
|
|
26
|
+
try {
|
|
27
|
+
await usage(ctx);
|
|
28
|
+
} catch (error) {
|
|
29
|
+
const message = `Antigravity quota query failed: ${error instanceof Error ? error.message : String(error)}`;
|
|
30
|
+
if (ctx.hasUI) ctx.ui.notify(message, "error");
|
|
31
|
+
else console.error(message);
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
|
|
9
35
|
pi.on("before_agent_start", async (event, ctx) => {
|
|
10
36
|
if (ctx.model?.provider !== "agy") return;
|
|
11
37
|
return {
|
package/src/usage.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
|
|
3
|
+
import { PROJECT_HEADER, rpc } from "./ccpa.ts";
|
|
4
|
+
|
|
5
|
+
type QuotaSummary = {
|
|
6
|
+
groups: {
|
|
7
|
+
displayName: string;
|
|
8
|
+
buckets: {
|
|
9
|
+
bucketId: string;
|
|
10
|
+
displayName: string;
|
|
11
|
+
window: string;
|
|
12
|
+
remainingFraction: number;
|
|
13
|
+
resetTime: string;
|
|
14
|
+
}[];
|
|
15
|
+
}[];
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
function resetLabel(resetTime: string | undefined, now: number): string {
|
|
19
|
+
const minutes = Math.ceil((Date.parse(resetTime ?? "") - now) / 60_000);
|
|
20
|
+
if (!Number.isFinite(minutes) || minutes <= 0) return "";
|
|
21
|
+
const hours = Math.floor(minutes / 60);
|
|
22
|
+
const days = Math.floor(hours / 24);
|
|
23
|
+
const remaining = days
|
|
24
|
+
? `${days}d ${hours % 24}h`
|
|
25
|
+
: hours
|
|
26
|
+
? `${hours}h ${minutes % 60}m`
|
|
27
|
+
: `${minutes}m`;
|
|
28
|
+
return ` · resets in ${remaining}`;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export async function usage(ctx: ExtensionCommandContext): Promise<void> {
|
|
32
|
+
const output = (text: string) => {
|
|
33
|
+
if (ctx.hasUI) ctx.ui.notify(text);
|
|
34
|
+
else console.log(text);
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const resolved = await ctx.modelRegistry.getProviderAuth("agy");
|
|
38
|
+
const auth = resolved?.auth;
|
|
39
|
+
const project = auth?.headers?.[PROJECT_HEADER];
|
|
40
|
+
if (!auth?.apiKey || !auth.baseUrl || !project) {
|
|
41
|
+
output("Run /login agy to authenticate.");
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const summary = await rpc<QuotaSummary>(
|
|
45
|
+
auth.apiKey,
|
|
46
|
+
auth.baseUrl,
|
|
47
|
+
"retrieveUserQuotaSummary",
|
|
48
|
+
{ project },
|
|
49
|
+
ctx.signal ?? AbortSignal.timeout(30_000),
|
|
50
|
+
);
|
|
51
|
+
const now = Date.now();
|
|
52
|
+
const groups = summary.groups.flatMap((group) => {
|
|
53
|
+
const buckets = group.buckets.map((bucket) => {
|
|
54
|
+
const fraction = bucket.remainingFraction;
|
|
55
|
+
const left = `${String(Math.round(Math.max(0, Math.min(1, fraction)) * 100)).padStart(3)}% left`;
|
|
56
|
+
const label = bucket.window || bucket.displayName || bucket.bucketId || "Quota";
|
|
57
|
+
return ` ${label.padEnd(6)} ${left}${resetLabel(bucket.resetTime, now)}`;
|
|
58
|
+
});
|
|
59
|
+
return buckets.length ? [[group.displayName || "AGY", ...buckets].join("\n")] : [];
|
|
60
|
+
});
|
|
61
|
+
output(groups.join("\n\n") || "No quota information available.");
|
|
62
|
+
}
|