@charmland/pi-hyper-provider 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@charmland/pi-hyper-provider",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "description": "Pi extension for Charm's Hyper inference provider",
5
5
  "license": "MIT",
6
6
  "keywords": [
package/src/credits.ts CHANGED
@@ -20,21 +20,30 @@ const CREDITS_RETRY_DELAY_MS_INITIAL = 5_000;
20
20
  const CREDITS_RETRY_DELAY_MS_MAX = 5 * 60_000;
21
21
  const CREDITS_RETRY_EXPONENT_MAX = 6;
22
22
 
23
- const CreditsPayloadSchema = Type.Object(
24
- {
25
- balance: Type.Number(),
26
- },
27
- { additionalProperties: false },
28
- );
23
+ const CreditsPayloadSchema = Type.Union([
24
+ Type.Object(
25
+ {
26
+ balance: Type.Number(),
27
+ },
28
+ { additionalProperties: false },
29
+ ),
30
+ Type.Object(
31
+ {
32
+ balance_usd: Type.Number(),
33
+ },
34
+ { additionalProperties: false },
35
+ ),
36
+ ]);
29
37
  const CreditsPayloadValidator = Compile(CreditsPayloadSchema);
30
38
 
31
- async function fetchCredits(apiKey: string, signal: AbortSignal): Promise<number> {
39
+ async function fetchCredits(apiKey: string, signal: AbortSignal): Promise<number | undefined> {
32
40
  const payload = await httpClient.fetchJson(`${HYPER_API_BASE_URL}/credits`, {
33
41
  headers: hyperJsonHeaders({ Authorization: `Bearer ${apiKey}` }),
34
42
  signal,
35
43
  timeoutMs: CREDITS_FETCH_TIMEOUT_MS,
36
44
  });
37
- return parseSchema(CreditsPayloadValidator, payload, "Hyper /credits response").balance;
45
+ const credits = parseSchema(CreditsPayloadValidator, payload, "Hyper /credits response");
46
+ return "balance" in credits ? credits.balance : undefined;
38
47
  }
39
48
 
40
49
  function isTransientCreditError(error: unknown): boolean {
@@ -56,7 +65,7 @@ function isHyperModel(model: ExtensionContext["model"]): model is NonNullable<Ex
56
65
  }
57
66
 
58
67
  function statusText(balance: number, statusItems: HyperStatusItems, teamName: string | undefined): string {
59
- const credits = `${HYPER_GEM} ${formatCredits(balance)} hc`;
68
+ const credits = `${HYPER_GEM} ${formatCredits(balance)} HC`;
60
69
  if (statusItems.teamName && teamName) return `${teamName}: ${credits}`;
61
70
  return credits;
62
71
  }
package/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { envApiKeyAuth, lazyOAuth, type Model, type OAuthAuth } from "@earendil-works/pi-ai";
1
+ import { createProvider, envApiKeyAuth, lazyOAuth, type OAuthAuth } from "@earendil-works/pi-ai";
2
2
  import { openAICompletionsApi } from "@earendil-works/pi-ai/compat";
3
3
  import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
4
4
  import type { CreditStatusRuntime } from "./credits.js";
@@ -18,8 +18,6 @@ type PendingCreditStatusRefresh = {
18
18
 
19
19
  export default function (pi: ExtensionAPI) {
20
20
  const notifier = createNotifier();
21
- const hyperApi = openAICompletionsApi();
22
- let hyperModels: readonly Model<"openai-completions">[] = [];
23
21
  let creditStatusState: CreditStatusState = { kind: "idle" };
24
22
  let pendingCreditStatusRefresh: PendingCreditStatusRefresh | undefined;
25
23
  let creditStatusRefreshWork: ReturnType<typeof setImmediate> | undefined;
@@ -111,49 +109,35 @@ export default function (pi: ExtensionAPI) {
111
109
  scheduleCreditStatusRefresh(ctx, ctx.model);
112
110
  });
113
111
 
114
- pi.registerProvider({
115
- id: PROVIDER_NAME,
116
- name: PROVIDER_DISPLAY_NAME,
117
- baseUrl: HYPER_API_BASE_URL,
118
- auth: {
119
- apiKey: envApiKeyAuth("Hyper API key", ["HYPER_API_KEY"]),
120
- oauth: lazyOAuth({
121
- name: PROVIDER_DISPLAY_NAME,
122
- load: async () => {
123
- const { loginHyper, refreshHyperToken } = await import("./oauth.js");
124
- return {
125
- name: PROVIDER_DISPLAY_NAME,
126
- login: loginHyper,
127
- refresh: refreshHyperToken,
128
- toAuth: async (credential) => ({ apiKey: credential.access }),
129
- } satisfies OAuthAuth;
130
- },
131
- }),
132
- },
133
- getModels: () => hyperModels,
134
- refreshModels: async ({ allowNetwork, credential, publish, signal, stored }) => {
135
- const cleared = await publish({
136
- update: () => {
137
- hyperModels = [];
138
- },
139
- });
140
- if (!cleared) return;
141
- if (stored && !(await publish({ persist: null }))) return;
142
- if (!allowNetwork || signal.aborted) return;
143
-
144
- const token = credential?.type === "oauth" ? credential.access : credential?.key;
145
- const { fetchHyperModels } = await import("./models.js");
146
- const refreshed = await fetchHyperModels({ signal, token });
147
- if (signal.aborted) return;
148
- await publish({
149
- update: () => {
150
- hyperModels = refreshed;
151
- },
152
- });
153
- },
154
- stream: hyperApi.stream,
155
- streamSimple: hyperApi.streamSimple,
156
- });
112
+ pi.registerProvider(
113
+ createProvider({
114
+ id: PROVIDER_NAME,
115
+ name: PROVIDER_DISPLAY_NAME,
116
+ baseUrl: HYPER_API_BASE_URL,
117
+ auth: {
118
+ apiKey: envApiKeyAuth("Hyper API key", ["HYPER_API_KEY"]),
119
+ oauth: lazyOAuth({
120
+ name: PROVIDER_DISPLAY_NAME,
121
+ load: async () => {
122
+ const { loginHyper, refreshHyperToken } = await import("./oauth.js");
123
+ return {
124
+ name: PROVIDER_DISPLAY_NAME,
125
+ login: loginHyper,
126
+ refresh: refreshHyperToken,
127
+ toAuth: async (credential) => ({ apiKey: credential.access }),
128
+ } satisfies OAuthAuth;
129
+ },
130
+ }),
131
+ },
132
+ models: [],
133
+ fetchModels: async ({ credential, signal }) => {
134
+ const token = credential?.type === "oauth" ? credential.access : credential?.key;
135
+ const { fetchHyperModels } = await import("./models.js");
136
+ return fetchHyperModels({ signal, token });
137
+ },
138
+ api: openAICompletionsApi(),
139
+ }),
140
+ );
157
141
 
158
142
  pi.registerCommand("hyper-status", {
159
143
  description: "Configure the Charm Hyper footer status",