pi-credits 0.1.0 → 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 CHANGED
@@ -1,15 +1,21 @@
1
+ ![Cover](./assets/cover.png)
2
+
1
3
  # pi-credits
2
4
 
3
- A [pi](https://pi.dev/) extension that shows the active model provider's credit balance or rate-limit usage as a footer status.
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
+
7
+ ![Screenshot](./assets/screenshot.png)
4
8
 
5
- - The status appears only while a supported provider is active and uses that provider's stored credential or API key.
6
- - The status refreshes on session start, model switch, and after each turn that incurs usage.
9
+ > Example with an OpenAI Codex subscription, paired with my [pi-spark](https://github.com/zlliang/pi-spark) package.
7
10
 
8
11
  ## Supported providers
9
12
 
10
- - **OpenAI Codex:** rate-limit usage of the 5-hour and weekly windows, as used percentages.
11
- - **OpenRouter:** remaining credit balance in dollars.
12
- - **Vercel AI Gateway:** remaining credit balance in dollars.
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).
13
19
 
14
20
  ## Install
15
21
 
Binary file
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-credits",
3
- "version": "0.1.0",
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
+ };
@@ -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,