@charmland/pi-hyper-provider 0.3.1 → 0.4.0
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 +1 -1
- package/src/index.ts +84 -45
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.0",
|
|
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.84.
|
|
33
|
-
"@earendil-works/pi-coding-agent": "^0.84.
|
|
33
|
+
"@earendil-works/pi-ai": "^0.84.4",
|
|
34
|
+
"@earendil-works/pi-coding-agent": "^0.84.4",
|
|
34
35
|
"typebox": "^1.1.38",
|
|
35
36
|
"typescript": "^6.0.3"
|
|
36
37
|
},
|
package/src/credits.ts
CHANGED
|
@@ -65,7 +65,7 @@ function isHyperModel(model: ExtensionContext["model"]): model is NonNullable<Ex
|
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
function statusText(balance: number, statusItems: HyperStatusItems, teamName: string | undefined): string {
|
|
68
|
-
const credits = `${HYPER_GEM} ${formatCredits(balance)}
|
|
68
|
+
const credits = `${HYPER_GEM} ${formatCredits(balance)} HC`;
|
|
69
69
|
if (statusItems.teamName && teamName) return `${teamName}: ${credits}`;
|
|
70
70
|
return credits;
|
|
71
71
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import { envApiKeyAuth, lazyOAuth, type
|
|
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,13 +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
|
-
const hyperApi = openAICompletionsApi();
|
|
22
|
-
let hyperModels: readonly Model<"openai-completions">[] = [];
|
|
23
29
|
let creditStatusState: CreditStatusState = { kind: "idle" };
|
|
24
30
|
let pendingCreditStatusRefresh: PendingCreditStatusRefresh | undefined;
|
|
25
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
|
+
});
|
|
26
46
|
|
|
27
47
|
function loadCreditStatus(): Promise<CreditStatusRuntime> {
|
|
28
48
|
if (creditStatusState.kind === "ready") return Promise.resolve(creditStatusState.runtime);
|
|
@@ -111,50 +131,69 @@ export default function (pi: ExtensionAPI) {
|
|
|
111
131
|
scheduleCreditStatusRefresh(ctx, ctx.model);
|
|
112
132
|
});
|
|
113
133
|
|
|
114
|
-
pi.
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
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,
|
|
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;
|
|
156
149
|
});
|
|
157
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
|
+
|
|
167
|
+
pi.registerProvider(
|
|
168
|
+
createProvider({
|
|
169
|
+
id: PROVIDER_NAME,
|
|
170
|
+
name: PROVIDER_DISPLAY_NAME,
|
|
171
|
+
baseUrl: HYPER_API_BASE_URL,
|
|
172
|
+
auth: {
|
|
173
|
+
apiKey: envApiKeyAuth("Hyper API key", ["HYPER_API_KEY"]),
|
|
174
|
+
oauth: lazyOAuth({
|
|
175
|
+
name: PROVIDER_DISPLAY_NAME,
|
|
176
|
+
load: async () => {
|
|
177
|
+
const { loginHyper, refreshHyperToken } = await import("./oauth.js");
|
|
178
|
+
return {
|
|
179
|
+
name: PROVIDER_DISPLAY_NAME,
|
|
180
|
+
login: loginHyper,
|
|
181
|
+
refresh: refreshHyperToken,
|
|
182
|
+
toAuth: async (credential) => ({ apiKey: credential.access }),
|
|
183
|
+
} satisfies OAuthAuth;
|
|
184
|
+
},
|
|
185
|
+
}),
|
|
186
|
+
},
|
|
187
|
+
models: [],
|
|
188
|
+
fetchModels: async ({ credential, signal }) => {
|
|
189
|
+
const token = credential?.type === "oauth" ? credential.access : credential?.key;
|
|
190
|
+
const { fetchHyperModels } = await import("./models.js");
|
|
191
|
+
return fetchHyperModels({ signal, token });
|
|
192
|
+
},
|
|
193
|
+
api: openAICompletionsApi(),
|
|
194
|
+
}),
|
|
195
|
+
);
|
|
196
|
+
|
|
158
197
|
pi.registerCommand("hyper-status", {
|
|
159
198
|
description: "Configure the Charm Hyper footer status",
|
|
160
199
|
handler: async (args, ctx) => {
|