@charmland/pi-hyper-provider 0.3.2 → 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.
- package/README.md +3 -4
- package/package.json +4 -3
- package/src/credits.ts +29 -36
- package/src/index.ts +55 -0
package/README.md
CHANGED
|
@@ -18,10 +18,9 @@ pi install git:github.com/charmbracelet/pi-hyper-provider
|
|
|
18
18
|
```
|
|
19
19
|
|
|
20
20
|
> [!IMPORTANT]
|
|
21
|
-
>
|
|
22
|
-
>
|
|
23
|
-
>
|
|
24
|
-
> metadata.
|
|
21
|
+
> Pi refreshes provider extensions' model catalogs in TUI and RPC modes. Other
|
|
22
|
+
> non-interactive modes use the latest persisted catalog, so open the TUI when
|
|
23
|
+
> you want to discover new models or update model metadata.
|
|
25
24
|
|
|
26
25
|
## Usage
|
|
27
26
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@charmland/pi-hyper-provider",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"description": "Pi extension for Charm's Hyper inference provider",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"keywords": [
|
|
@@ -25,12 +25,13 @@
|
|
|
25
25
|
"peerDependencies": {
|
|
26
26
|
"@earendil-works/pi-ai": "*",
|
|
27
27
|
"@earendil-works/pi-coding-agent": "*",
|
|
28
|
+
"@earendil-works/pi-tui": "*",
|
|
28
29
|
"typebox": "*"
|
|
29
30
|
},
|
|
30
31
|
"devDependencies": {
|
|
31
32
|
"@biomejs/biome": "^2.4.16",
|
|
32
|
-
"@earendil-works/pi-ai": "^0.
|
|
33
|
-
"@earendil-works/pi-coding-agent": "^0.
|
|
33
|
+
"@earendil-works/pi-ai": "^0.85.1",
|
|
34
|
+
"@earendil-works/pi-coding-agent": "^0.85.1",
|
|
34
35
|
"typebox": "^1.1.38",
|
|
35
36
|
"typescript": "^6.0.3"
|
|
36
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
|
|
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
|
|
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
|
-
|
|
131
|
-
|
|
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(
|
|
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
|
-
|
|
156
|
-
|
|
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
|
-
|
|
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(
|
|
175
|
+
async function shareFetch(lease: CredentialLease): Promise<void> {
|
|
178
176
|
const active = inFlight;
|
|
179
|
-
if (active?.apiKey === apiKey && active.credentialEpoch ===
|
|
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(
|
|
186
|
-
const started = { apiKey, credentialEpoch:
|
|
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
|
|
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.
|
|
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 &&
|
|
231
|
+
forcedLease !== undefined && apiKey === forcedLease.apiKey && ownsCredential(forcedLease);
|
|
231
232
|
if (invocation < committedInvocation && !forcedLeaseStillValid) return;
|
|
232
|
-
if (!
|
|
233
|
+
if (!apiKey) {
|
|
233
234
|
committedInvocation = invocation;
|
|
234
235
|
invalidateCredential();
|
|
235
|
-
|
|
236
|
-
clearCreditState();
|
|
236
|
+
resetRetryState();
|
|
237
237
|
renderStatus(ctx);
|
|
238
238
|
return;
|
|
239
239
|
}
|
|
240
240
|
if (invocation >= committedInvocation) committedInvocation = invocation;
|
|
241
|
-
if (!isUserRequested && currentApiKey ===
|
|
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 !==
|
|
247
|
-
currentApiKey =
|
|
246
|
+
if (currentApiKey !== apiKey) {
|
|
247
|
+
currentApiKey = apiKey;
|
|
248
248
|
invalidateCredential();
|
|
249
|
-
|
|
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
|
|
253
|
+
const lease = { apiKey, credentialEpoch: expectedCredentialEpoch, teamName: storedTeamName(apiKey) };
|
|
255
254
|
failureLease = lease;
|
|
256
|
-
await shareFetch(
|
|
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
|
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { createProvider, envApiKeyAuth, lazyOAuth, type OAuthAuth } from "@earendil-works/pi-ai";
|
|
2
|
+
// Pi 0.84.2's extension loader aliases the root package but not API subpaths.
|
|
2
3
|
import { openAICompletionsApi } from "@earendil-works/pi-ai/compat";
|
|
3
4
|
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
5
|
+
import { Text } from "@earendil-works/pi-tui";
|
|
4
6
|
import type { CreditStatusRuntime } from "./credits.js";
|
|
5
7
|
import { HYPER_API_BASE_URL, PROVIDER_DISPLAY_NAME, PROVIDER_NAME } from "./hyper.js";
|
|
6
8
|
import { createNotifier } from "./notify.js";
|
|
@@ -16,11 +18,31 @@ type PendingCreditStatusRefresh = {
|
|
|
16
18
|
model: ExtensionContext["model"];
|
|
17
19
|
};
|
|
18
20
|
|
|
21
|
+
function prismLabel(value: string | undefined): string | undefined {
|
|
22
|
+
return value !== undefined && value.trim() !== "" && value.length <= 200 && !/[\p{Cc}\p{Cf}\p{Zl}\p{Zp}]/u.test(value)
|
|
23
|
+
? value.trim()
|
|
24
|
+
: undefined;
|
|
25
|
+
}
|
|
26
|
+
|
|
19
27
|
export default function (pi: ExtensionAPI) {
|
|
20
28
|
const notifier = createNotifier();
|
|
21
29
|
let creditStatusState: CreditStatusState = { kind: "idle" };
|
|
22
30
|
let pendingCreditStatusRefresh: PendingCreditStatusRefresh | undefined;
|
|
23
31
|
let creditStatusRefreshWork: ReturnType<typeof setImmediate> | undefined;
|
|
32
|
+
let collectingPrismRoute = false;
|
|
33
|
+
let prismRoute: { modelName: string | undefined; modelId: string | undefined } | undefined;
|
|
34
|
+
|
|
35
|
+
pi.registerEntryRenderer("hyper-prism-route", (entry, _options, theme) => {
|
|
36
|
+
const data = entry.data;
|
|
37
|
+
if (typeof data !== "object" || data === null) return;
|
|
38
|
+
const modelName =
|
|
39
|
+
"modelName" in data && typeof data.modelName === "string" ? prismLabel(data.modelName) : undefined;
|
|
40
|
+
const modelId = "modelId" in data && typeof data.modelId === "string" ? prismLabel(data.modelId) : undefined;
|
|
41
|
+
const label = modelName ?? modelId;
|
|
42
|
+
if (label) {
|
|
43
|
+
return new Text(`${theme.fg("muted", "Prism")} ${theme.fg("dim", "→")} ${theme.fg("muted", label)}`, 0, 0);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
24
46
|
|
|
25
47
|
function loadCreditStatus(): Promise<CreditStatusRuntime> {
|
|
26
48
|
if (creditStatusState.kind === "ready") return Promise.resolve(creditStatusState.runtime);
|
|
@@ -109,6 +131,39 @@ export default function (pi: ExtensionAPI) {
|
|
|
109
131
|
scheduleCreditStatusRefresh(ctx, ctx.model);
|
|
110
132
|
});
|
|
111
133
|
|
|
134
|
+
pi.on("turn_start", () => {
|
|
135
|
+
collectingPrismRoute = true;
|
|
136
|
+
prismRoute = undefined;
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
pi.on("after_provider_response", (event) => {
|
|
140
|
+
if (!collectingPrismRoute) return;
|
|
141
|
+
// Collect only during the assistant request, not between-turn compaction.
|
|
142
|
+
const modelName = prismLabel(event.headers["x-prism-model-name"]);
|
|
143
|
+
const modelId = prismLabel(event.headers["x-prism-model-id"]);
|
|
144
|
+
prismRoute = modelName || modelId ? { modelName, modelId } : undefined;
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
pi.on("message_end", (event) => {
|
|
148
|
+
if (event.message.role === "assistant") collectingPrismRoute = false;
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
pi.on("turn_end", (event) => {
|
|
152
|
+
const route = prismRoute;
|
|
153
|
+
prismRoute = undefined;
|
|
154
|
+
collectingPrismRoute = false;
|
|
155
|
+
if (
|
|
156
|
+
event.message.role !== "assistant" ||
|
|
157
|
+
event.message.provider !== PROVIDER_NAME ||
|
|
158
|
+
event.message.stopReason === "error" ||
|
|
159
|
+
event.message.stopReason === "aborted"
|
|
160
|
+
)
|
|
161
|
+
return;
|
|
162
|
+
// message_end extensions run before the final UI update. turn_end runs
|
|
163
|
+
// after the assistant and its tool results have been rendered.
|
|
164
|
+
if (route) pi.appendEntry("hyper-prism-route", route);
|
|
165
|
+
});
|
|
166
|
+
|
|
112
167
|
pi.registerProvider(
|
|
113
168
|
createProvider({
|
|
114
169
|
id: PROVIDER_NAME,
|