@charmland/pi-hyper-provider 0.4.0 → 0.4.1

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.
Files changed (2) hide show
  1. package/package.json +3 -3
  2. package/src/credits.ts +29 -36
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@charmland/pi-hyper-provider",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
4
4
  "description": "Pi extension for Charm's Hyper inference provider",
5
5
  "license": "MIT",
6
6
  "keywords": [
@@ -30,8 +30,8 @@
30
30
  },
31
31
  "devDependencies": {
32
32
  "@biomejs/biome": "^2.4.16",
33
- "@earendil-works/pi-ai": "^0.84.4",
34
- "@earendil-works/pi-coding-agent": "^0.84.4",
33
+ "@earendil-works/pi-ai": "^0.85.1",
34
+ "@earendil-works/pi-coding-agent": "^0.85.1",
35
35
  "typebox": "^1.1.38",
36
36
  "typescript": "^6.0.3"
37
37
  },
package/src/credits.ts CHANGED
@@ -75,9 +75,10 @@ function teamNameStatusText(statusItems: HyperStatusItems, teamName: string | un
75
75
  return `${HYPER_GEM} ${teamName}`;
76
76
  }
77
77
 
78
- function storedTeamName(): string | undefined {
78
+ function storedTeamName(apiKey?: string): string | undefined {
79
79
  const credential = readStoredCredential(PROVIDER_NAME);
80
80
  if (credential?.type !== "oauth") return undefined;
81
+ if (apiKey !== undefined && credential.access !== apiKey) return undefined;
81
82
  const teamName = credential.teamName;
82
83
  return typeof teamName === "string" && teamName.trim() ? teamName : undefined;
83
84
  }
@@ -101,16 +102,15 @@ export function createCreditStatusRuntime(warn: WarningSink): CreditStatusRuntim
101
102
  let consecutiveFailures = 0;
102
103
  let retryAtMs = 0;
103
104
  let currentApiKey: string | undefined;
104
- let cachedBalance: number | undefined;
105
+ let cachedCredit: { balance: number | undefined; teamName: string | undefined } | undefined;
105
106
  let statusItemsCache: HyperStatusItems | undefined;
106
107
  let inFlight:
107
108
  | { apiKey: string; credentialEpoch: number; controller: AbortController; operation: Promise<void> }
108
109
  | undefined;
109
110
  let disposed = false;
110
- type CredentialLease = { apiKey: string; credentialEpoch: number };
111
+ type CredentialLease = { apiKey: string; credentialEpoch: number; teamName: string | undefined };
111
112
 
112
- function clearCreditState(): void {
113
- cachedBalance = undefined;
113
+ function resetRetryState(): void {
114
114
  consecutiveFailures = 0;
115
115
  retryAtMs = 0;
116
116
  }
@@ -127,12 +127,11 @@ export function createCreditStatusRuntime(warn: WarningSink): CreditStatusRuntim
127
127
 
128
128
  function renderStatus(ctx: ExtensionContext): void {
129
129
  const statusItems = getStatusItems();
130
- const teamName = storedTeamName();
131
- if (!statusItems.hypercredits || cachedBalance === undefined) {
132
- ctx.ui.setStatus(PROVIDER_NAME, teamNameStatusText(statusItems, teamName));
130
+ if (!statusItems.hypercredits || cachedCredit?.balance === undefined) {
131
+ ctx.ui.setStatus(PROVIDER_NAME, teamNameStatusText(statusItems, storedTeamName()));
133
132
  return;
134
133
  }
135
- ctx.ui.setStatus(PROVIDER_NAME, statusText(cachedBalance, statusItems, teamName));
134
+ ctx.ui.setStatus(PROVIDER_NAME, statusText(cachedCredit.balance, statusItems, cachedCredit.teamName));
136
135
  }
137
136
 
138
137
  function ownsCredential(lease: CredentialLease): boolean {
@@ -152,13 +151,12 @@ export function createCreditStatusRuntime(warn: WarningSink): CreditStatusRuntim
152
151
  try {
153
152
  const balance = await fetchCredits(lease.apiKey, signal);
154
153
  if (disposed || !ownsCredential(lease)) return;
155
- cachedBalance = balance;
156
- consecutiveFailures = 0;
157
- retryAtMs = 0;
154
+ cachedCredit = { balance, teamName: lease.teamName };
155
+ resetRetryState();
158
156
  } catch (error) {
159
157
  if (disposed || !ownsCredential(lease)) throw error;
160
158
  if (!isTransientCreditError(error)) {
161
- clearCreditState();
159
+ resetRetryState();
162
160
  throw error;
163
161
  }
164
162
  consecutiveFailures += 1;
@@ -174,16 +172,16 @@ export function createCreditStatusRuntime(warn: WarningSink): CreditStatusRuntim
174
172
  }
175
173
  }
176
174
 
177
- async function shareFetch(apiKey: string, expectedCredentialEpoch: number): Promise<void> {
175
+ async function shareFetch(lease: CredentialLease): Promise<void> {
178
176
  const active = inFlight;
179
- if (active?.apiKey === apiKey && active.credentialEpoch === expectedCredentialEpoch) {
177
+ if (active?.apiKey === lease.apiKey && active.credentialEpoch === lease.credentialEpoch) {
180
178
  await active.operation;
181
179
  return;
182
180
  }
183
181
 
184
182
  const controller = new AbortController();
185
- const operation = fetchAndCache({ apiKey, credentialEpoch: expectedCredentialEpoch }, controller.signal);
186
- const started = { apiKey, credentialEpoch: expectedCredentialEpoch, controller, operation };
183
+ const operation = fetchAndCache(lease, controller.signal);
184
+ const started = { apiKey: lease.apiKey, credentialEpoch: lease.credentialEpoch, controller, operation };
187
185
  inFlight = started;
188
186
  try {
189
187
  await operation;
@@ -203,7 +201,9 @@ export function createCreditStatusRuntime(warn: WarningSink): CreditStatusRuntim
203
201
  const invocation = invocationSequence + 1;
204
202
  invocationSequence = invocation;
205
203
  const forcedLease: CredentialLease | undefined =
206
- isUserRequested && currentApiKey !== undefined ? { apiKey: currentApiKey, credentialEpoch } : undefined;
204
+ isUserRequested && currentApiKey !== undefined
205
+ ? { apiKey: currentApiKey, credentialEpoch, teamName: cachedCredit?.teamName }
206
+ : undefined;
207
207
  let failureLease: CredentialLease | undefined;
208
208
  if (!isHyperModel(selectedModel)) {
209
209
  committedInvocation = invocation;
@@ -224,36 +224,35 @@ export function createCreditStatusRuntime(warn: WarningSink): CreditStatusRuntim
224
224
  // retaining a balance only while it belongs to the committed credential.
225
225
  renderStatus(ctx);
226
226
  try {
227
- const auth = await ctx.modelRegistry.getApiKeyAndHeaders(selectedModel);
227
+ const auth = await ctx.modelRegistry.getProviderAuth(PROVIDER_NAME).catch(() => undefined);
228
228
  if (disposed) return;
229
+ const apiKey = auth?.auth.apiKey;
229
230
  const forcedLeaseStillValid =
230
- forcedLease !== undefined && auth.ok && auth.apiKey === forcedLease.apiKey && ownsCredential(forcedLease);
231
+ forcedLease !== undefined && apiKey === forcedLease.apiKey && ownsCredential(forcedLease);
231
232
  if (invocation < committedInvocation && !forcedLeaseStillValid) return;
232
- if (!auth.ok || !auth.apiKey) {
233
+ if (!apiKey) {
233
234
  committedInvocation = invocation;
234
235
  invalidateCredential();
235
- currentApiKey = undefined;
236
- clearCreditState();
236
+ resetRetryState();
237
237
  renderStatus(ctx);
238
238
  return;
239
239
  }
240
240
  if (invocation >= committedInvocation) committedInvocation = invocation;
241
- if (!isUserRequested && currentApiKey === auth.apiKey && Date.now() < retryAtMs) {
241
+ if (!isUserRequested && currentApiKey === apiKey && Date.now() < retryAtMs) {
242
242
  // This auth result supersedes older general state, but a same-credential
243
243
  // forced refresh retains its independent fetch lease.
244
244
  return;
245
245
  }
246
- if (currentApiKey !== auth.apiKey) {
247
- currentApiKey = auth.apiKey;
246
+ if (currentApiKey !== apiKey) {
247
+ currentApiKey = apiKey;
248
248
  invalidateCredential();
249
- clearCreditState();
250
- // Never show the previous account while the replacement request runs.
249
+ resetRetryState();
251
250
  renderStatus(ctx);
252
251
  }
253
252
  const expectedCredentialEpoch = credentialEpoch;
254
- const lease = { apiKey: auth.apiKey, credentialEpoch: expectedCredentialEpoch };
253
+ const lease = { apiKey, credentialEpoch: expectedCredentialEpoch, teamName: storedTeamName(apiKey) };
255
254
  failureLease = lease;
256
- await shareFetch(auth.apiKey, expectedCredentialEpoch);
255
+ await shareFetch(lease);
257
256
  if (disposed) return;
258
257
  if (!canRender(invocation, forcedLease)) return;
259
258
  render(ctx, lease);
@@ -262,12 +261,6 @@ export function createCreditStatusRuntime(warn: WarningSink): CreditStatusRuntim
262
261
  if (!canRender(invocation, forcedLease)) return;
263
262
  const failedOperationStillOwnsCredential = failureLease !== undefined && ownsCredential(failureLease);
264
263
  if (failureLease !== undefined && failedOperationStillOwnsCredential) render(ctx, failureLease);
265
- if (
266
- isUserRequested &&
267
- (failedOperationStillOwnsCredential || (failureLease === undefined && invocation === invocationSequence))
268
- ) {
269
- ctx.ui.notify("Unable to refresh Hypercredit balance", "warning");
270
- }
271
264
  }
272
265
  }
273
266