pi-credits 0.3.0 → 0.3.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 +4 -4
- package/package.json +8 -17
- package/src/manager.ts +11 -3
- package/src/providers/deepseek.ts +1 -1
- package/src/providers/fireworks.ts +1 -1
- package/src/providers/index.ts +1 -1
- package/src/providers/moonshot.ts +2 -2
- package/src/providers/openai-codex.ts +1 -1
- package/src/providers/openrouter.ts +1 -1
- package/src/providers/vercel-ai-gateway.ts +1 -1
- package/src/types.ts +1 -1
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@ A [pi](https://pi.dev/) extension that shows the active model provider's credit
|
|
|
6
6
|
|
|
7
7
|

|
|
8
8
|
|
|
9
|
-
> Example with an OpenAI Codex subscription, paired with my [pi-spark](https://github.com/zlliang/pi-spark) package.
|
|
9
|
+
> Example with an OpenAI Codex subscription, paired with my [pi-spark](https://github.com/zlliang/pi-packages/tree/main/packages/pi-spark) package.
|
|
10
10
|
|
|
11
11
|
## Supported providers
|
|
12
12
|
|
|
@@ -27,12 +27,12 @@ Install from npm:
|
|
|
27
27
|
pi install npm:pi-credits
|
|
28
28
|
```
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
For local development from this monorepo:
|
|
31
31
|
|
|
32
32
|
```bash
|
|
33
|
-
pi install
|
|
33
|
+
pi install /path/to/pi-packages/packages/pi-credits
|
|
34
34
|
```
|
|
35
35
|
|
|
36
36
|
## Other pi packages
|
|
37
37
|
|
|
38
|
-
- [pi-spark](https://github.com/zlliang/pi-spark): a small, opinionated collection of pi extensions.
|
|
38
|
+
- [pi-spark](https://github.com/zlliang/pi-packages/tree/main/packages/pi-spark): a small, opinionated collection of pi extensions.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-credits",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.2",
|
|
4
4
|
"description": "A pi extension that shows the active provider's credit balance or rate-limit usage",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pi-coding-agent",
|
|
@@ -8,7 +8,8 @@
|
|
|
8
8
|
],
|
|
9
9
|
"repository": {
|
|
10
10
|
"type": "git",
|
|
11
|
-
"url": "git+https://github.com/zlliang/pi-
|
|
11
|
+
"url": "git+https://github.com/zlliang/pi-packages.git",
|
|
12
|
+
"directory": "packages/pi-credits"
|
|
12
13
|
},
|
|
13
14
|
"license": "MIT",
|
|
14
15
|
"type": "module",
|
|
@@ -17,17 +18,13 @@
|
|
|
17
18
|
"src",
|
|
18
19
|
"docs",
|
|
19
20
|
"assets",
|
|
20
|
-
"README.md"
|
|
21
|
-
"LICENSE"
|
|
21
|
+
"README.md"
|
|
22
22
|
],
|
|
23
23
|
"pi": {
|
|
24
24
|
"extensions": [
|
|
25
25
|
"./index.ts"
|
|
26
26
|
],
|
|
27
|
-
"image": "https://raw.githubusercontent.com/zlliang/pi-
|
|
28
|
-
},
|
|
29
|
-
"scripts": {
|
|
30
|
-
"typecheck": "tsc --noEmit"
|
|
27
|
+
"image": "https://raw.githubusercontent.com/zlliang/pi-packages/main/packages/pi-credits/assets/cover.png"
|
|
31
28
|
},
|
|
32
29
|
"dependencies": {
|
|
33
30
|
"@grpc/grpc-js": "^1.14.4",
|
|
@@ -39,13 +36,7 @@
|
|
|
39
36
|
"@earendil-works/pi-coding-agent": "*",
|
|
40
37
|
"@earendil-works/pi-tui": "*"
|
|
41
38
|
},
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"@earendil-works/pi-ai": "*",
|
|
45
|
-
"@earendil-works/pi-coding-agent": "*",
|
|
46
|
-
"@earendil-works/pi-tui": "*",
|
|
47
|
-
"@types/node": "*",
|
|
48
|
-
"typebox": "*",
|
|
49
|
-
"typescript": "^6.0.3"
|
|
39
|
+
"scripts": {
|
|
40
|
+
"typecheck": "pnpm --dir ../.. typecheck"
|
|
50
41
|
}
|
|
51
|
-
}
|
|
42
|
+
}
|
package/src/manager.ts
CHANGED
|
@@ -9,6 +9,7 @@ const REQUEST_TIMEOUT_MS = 30_000;
|
|
|
9
9
|
|
|
10
10
|
export class CreditsManager {
|
|
11
11
|
private inflight: AbortController | undefined = undefined;
|
|
12
|
+
private currentProvider: string | undefined = undefined;
|
|
12
13
|
|
|
13
14
|
async refresh(ctx: ExtensionContext): Promise<void> {
|
|
14
15
|
this.inflight?.abort();
|
|
@@ -16,10 +17,17 @@ export class CreditsManager {
|
|
|
16
17
|
const provider = findProvider(ctx.model?.provider);
|
|
17
18
|
if (!provider) {
|
|
18
19
|
this.inflight = undefined;
|
|
20
|
+
this.currentProvider = undefined;
|
|
19
21
|
ctx.ui.setStatus(STATUS_KEY, undefined);
|
|
20
22
|
return;
|
|
21
23
|
}
|
|
22
24
|
|
|
25
|
+
// Clear stale credits from another provider while the new fetch is in flight.
|
|
26
|
+
if (this.currentProvider !== provider.id) {
|
|
27
|
+
this.currentProvider = provider.id;
|
|
28
|
+
ctx.ui.setStatus(STATUS_KEY, undefined);
|
|
29
|
+
}
|
|
30
|
+
|
|
23
31
|
const controller = new AbortController();
|
|
24
32
|
this.inflight = controller;
|
|
25
33
|
|
|
@@ -30,7 +38,7 @@ export class CreditsManager {
|
|
|
30
38
|
|
|
31
39
|
private async fetch(ctx: ExtensionContext, provider: CreditsProvider, signal: AbortSignal): Promise<void> {
|
|
32
40
|
try {
|
|
33
|
-
const apiKey = await ctx.modelRegistry.getApiKeyForProvider(provider.
|
|
41
|
+
const apiKey = await ctx.modelRegistry.getApiKeyForProvider(provider.id);
|
|
34
42
|
if (!apiKey) {
|
|
35
43
|
ctx.ui.setStatus(STATUS_KEY, undefined);
|
|
36
44
|
return;
|
|
@@ -42,12 +50,12 @@ export class CreditsManager {
|
|
|
42
50
|
const credits = await provider.fetch(ctx, apiKey, AbortSignal.any(signals));
|
|
43
51
|
|
|
44
52
|
// The active model may have changed while the request was in flight.
|
|
45
|
-
if (ctx.model?.provider !== provider.
|
|
53
|
+
if (ctx.model?.provider !== provider.id) return;
|
|
46
54
|
|
|
47
55
|
ctx.ui.setStatus(STATUS_KEY, renderCredits(ctx.ui.theme, provider.label, credits));
|
|
48
56
|
} catch (error) {
|
|
49
57
|
if (signal.aborted || ctx.signal?.aborted) return;
|
|
50
|
-
if (ctx.model?.provider !== provider.
|
|
58
|
+
if (ctx.model?.provider !== provider.id) return;
|
|
51
59
|
|
|
52
60
|
const message = error instanceof Error ? error.message : String(error);
|
|
53
61
|
ctx.ui.setStatus(STATUS_KEY, renderError(ctx.ui.theme, provider.label, message));
|
|
@@ -99,7 +99,7 @@ function moneyToNumber(money: Money | null | undefined): number | undefined {
|
|
|
99
99
|
}
|
|
100
100
|
|
|
101
101
|
export const fireworksProvider: CreditsProvider = {
|
|
102
|
-
|
|
102
|
+
id: PROVIDER,
|
|
103
103
|
label: "Fireworks",
|
|
104
104
|
|
|
105
105
|
async fetch(_ctx, apiKey, signal): Promise<Credits> {
|
package/src/providers/index.ts
CHANGED
|
@@ -13,9 +13,9 @@ interface MoonshotBalanceResponse {
|
|
|
13
13
|
* different currencies (USD vs CNY), which the endpoint does not report, so each pi provider ID
|
|
14
14
|
* fixes both host and currency.
|
|
15
15
|
*/
|
|
16
|
-
function createMoonshotProvider(
|
|
16
|
+
function createMoonshotProvider(id: string, host: string, currency: string): CreditsProvider {
|
|
17
17
|
return {
|
|
18
|
-
|
|
18
|
+
id,
|
|
19
19
|
label: "Moonshot",
|
|
20
20
|
|
|
21
21
|
async fetch(_ctx, apiKey, signal): Promise<Credits> {
|
|
@@ -33,7 +33,7 @@ function parseUsedPercent(window?: CodexRateWindow | null): number | undefined {
|
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
export const openaiCodexProvider: CreditsProvider = {
|
|
36
|
-
|
|
36
|
+
id: PROVIDER,
|
|
37
37
|
label: "Codex",
|
|
38
38
|
|
|
39
39
|
async fetch(ctx, apiKey, signal): Promise<Credits> {
|
package/src/types.ts
CHANGED
|
@@ -18,7 +18,7 @@ export interface CreditsLane {
|
|
|
18
18
|
|
|
19
19
|
/** A credits source for a pi provider, shown in the status line while that provider is active. */
|
|
20
20
|
export interface CreditsProvider {
|
|
21
|
-
readonly
|
|
21
|
+
readonly id: Provider;
|
|
22
22
|
readonly label: string;
|
|
23
23
|
fetch(ctx: ExtensionContext, apiKey: string, signal: AbortSignal): Promise<Credits>;
|
|
24
24
|
}
|