pi-credits 0.1.1 → 0.1.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 +10 -6
- package/assets/cover.png +0 -0
- package/assets/screenshot.png +0 -0
- package/package.json +2 -1
- package/src/providers/deepseek.ts +56 -0
- package/src/providers/index.ts +2 -0
package/README.md
CHANGED
|
@@ -2,16 +2,20 @@
|
|
|
2
2
|
|
|
3
3
|
# pi-credits
|
|
4
4
|
|
|
5
|
-
A [pi](https://pi.dev/) extension that shows the active model provider's credit balance or rate-limit usage
|
|
5
|
+
A [pi](https://pi.dev/) extension that shows the active model provider's credit balance or rate-limit usage in the footer. It appears only for supported providers and uses the provider's stored credential or API key.
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+

|
|
8
|
+
|
|
9
|
+
> Example with an OpenAI Codex subscription, paired with my [pi-spark](https://github.com/zlliang/pi-spark) package.
|
|
9
10
|
|
|
10
11
|
## Supported providers
|
|
11
12
|
|
|
12
|
-
-
|
|
13
|
-
-
|
|
14
|
-
-
|
|
13
|
+
- DeepSeek
|
|
14
|
+
- OpenAI Codex
|
|
15
|
+
- OpenRouter
|
|
16
|
+
- Vercel AI Gateway
|
|
17
|
+
|
|
18
|
+
The provider-specific fetching approaches are strongly inspired by [CodexBar](https://github.com/steipete/codexbar).
|
|
15
19
|
|
|
16
20
|
## Install
|
|
17
21
|
|
package/assets/cover.png
ADDED
|
Binary file
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-credits",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.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",
|
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
"files": [
|
|
16
16
|
"index.ts",
|
|
17
17
|
"src",
|
|
18
|
+
"assets",
|
|
18
19
|
"README.md",
|
|
19
20
|
"LICENSE"
|
|
20
21
|
],
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { toNumber } from "../utils";
|
|
2
|
+
|
|
3
|
+
import type { Credits, CreditsProvider } from "../types";
|
|
4
|
+
|
|
5
|
+
const PROVIDER = "deepseek";
|
|
6
|
+
const URL = "https://api.deepseek.com/user/balance";
|
|
7
|
+
const FRANKFURTER_API = "https://api.frankfurter.dev/v2/rate";
|
|
8
|
+
|
|
9
|
+
interface DeepSeekBalanceResponse {
|
|
10
|
+
balance_infos?: DeepSeekBalanceInfo[] | null;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
interface DeepSeekBalanceInfo {
|
|
14
|
+
currency?: string;
|
|
15
|
+
total_balance?: string | number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface FrankfurterRateResponse {
|
|
19
|
+
rate?: string | number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async function convertToUSD(amount: number | undefined, currency: string | undefined, signal: AbortSignal): Promise<number | undefined> {
|
|
23
|
+
if (amount === undefined) return undefined;
|
|
24
|
+
if (!currency || currency === "USD") return amount;
|
|
25
|
+
|
|
26
|
+
const url = `${FRANKFURTER_API}/${encodeURIComponent(currency)}/USD`;
|
|
27
|
+
const response = await fetch(url, { headers: { Accept: "application/json" }, signal });
|
|
28
|
+
if (!response.ok) throw new Error("currency conversion failed");
|
|
29
|
+
|
|
30
|
+
const payload = (await response.json()) as FrankfurterRateResponse;
|
|
31
|
+
const rate = toNumber(payload.rate);
|
|
32
|
+
if (rate === undefined) throw new Error("currency conversion failed");
|
|
33
|
+
|
|
34
|
+
return amount * rate;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export const deepseekProvider: CreditsProvider = {
|
|
38
|
+
provider: PROVIDER,
|
|
39
|
+
label: "DeepSeek",
|
|
40
|
+
|
|
41
|
+
async fetch(_ctx, apiKey, signal): Promise<Credits> {
|
|
42
|
+
const headers: Record<string, string> = {
|
|
43
|
+
Accept: "application/json",
|
|
44
|
+
Authorization: `Bearer ${apiKey}`,
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const response = await fetch(URL, { headers, signal });
|
|
48
|
+
if (!response.ok) throw new Error("request failed");
|
|
49
|
+
|
|
50
|
+
const payload = (await response.json()) as DeepSeekBalanceResponse;
|
|
51
|
+
const balance = payload.balance_infos?.find((entry) => entry.currency === "USD") ?? payload.balance_infos?.[0];
|
|
52
|
+
const remaining = await convertToUSD(toNumber(balance?.total_balance), balance?.currency, signal);
|
|
53
|
+
|
|
54
|
+
return { type: "balance", remaining };
|
|
55
|
+
},
|
|
56
|
+
};
|
package/src/providers/index.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { deepseekProvider } from "./deepseek";
|
|
1
2
|
import { openaiCodexProvider } from "./openai-codex";
|
|
2
3
|
import { openrouterProvider } from "./openrouter";
|
|
3
4
|
import { vercelAiGatewayProvider } from "./vercel-ai-gateway";
|
|
@@ -5,6 +6,7 @@ import { vercelAiGatewayProvider } from "./vercel-ai-gateway";
|
|
|
5
6
|
import type { CreditsProvider } from "../types";
|
|
6
7
|
|
|
7
8
|
const PROVIDERS: CreditsProvider[] = [
|
|
9
|
+
deepseekProvider,
|
|
8
10
|
openaiCodexProvider,
|
|
9
11
|
openrouterProvider,
|
|
10
12
|
vercelAiGatewayProvider,
|