@charmland/pi-hyper-provider 0.1.1 → 0.1.3
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 +2 -3
- package/package.json +3 -3
- package/src/credits.ts +5 -3
- package/src/index.ts +20 -17
- package/src/models.ts +30 -10
- package/src/oauth.ts +101 -14
package/README.md
CHANGED
|
@@ -48,9 +48,8 @@ See the [contributing guide](https://github.com/charmbracelet/pi-hyper-provider?
|
|
|
48
48
|
|
|
49
49
|
## Releasing
|
|
50
50
|
|
|
51
|
-
1. `mise run release:bump
|
|
52
|
-
|
|
53
|
-
version
|
|
51
|
+
1. `mise run release:bump <patch|minor|major|prepatch|preminor|premajor|prerelease>`
|
|
52
|
+
to increment the version
|
|
54
53
|
2. `mise run release:pack` to dry-run build the release tarball
|
|
55
54
|
3. `mise run release:publish` to publish
|
|
56
55
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@charmland/pi-hyper-provider",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "Pi extension for Charm's Hyper inference provider",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"keywords": [
|
|
@@ -30,8 +30,8 @@
|
|
|
30
30
|
"dependencies": {},
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"@biomejs/biome": "^2.4.16",
|
|
33
|
-
"@earendil-works/pi-ai": "^0.
|
|
34
|
-
"@earendil-works/pi-coding-agent": "^0.
|
|
33
|
+
"@earendil-works/pi-ai": "^0.81.1",
|
|
34
|
+
"@earendil-works/pi-coding-agent": "^0.81.1",
|
|
35
35
|
"typebox": "^1.1.38",
|
|
36
36
|
"typescript": "^6.0.3"
|
|
37
37
|
},
|
package/src/credits.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { readStoredCredential } from "@earendil-works/pi-coding-agent";
|
|
2
3
|
import { Type } from "typebox";
|
|
3
4
|
import { fetchJson } from "./http.js";
|
|
4
5
|
import { hyperApiBaseUrl, hyperJsonHeaders, PROVIDER_NAME } from "./hyper.js";
|
|
@@ -48,8 +49,8 @@ function teamNameStatusText(statusItems: HyperStatusItems, teamName: string | un
|
|
|
48
49
|
return `${HYPER_GEM} ${teamName}`;
|
|
49
50
|
}
|
|
50
51
|
|
|
51
|
-
function storedTeamName(
|
|
52
|
-
const credential =
|
|
52
|
+
function storedTeamName(): string | undefined {
|
|
53
|
+
const credential = readStoredCredential(PROVIDER_NAME);
|
|
53
54
|
if (credential?.type !== "oauth") return undefined;
|
|
54
55
|
const teamName = credential.teamName;
|
|
55
56
|
return typeof teamName === "string" && teamName.trim() ? teamName : undefined;
|
|
@@ -68,7 +69,7 @@ export function registerCreditStatus(pi: ExtensionAPI): void {
|
|
|
68
69
|
}
|
|
69
70
|
|
|
70
71
|
const statusItems = readHyperStatusItems();
|
|
71
|
-
const teamName = storedTeamName(
|
|
72
|
+
const teamName = storedTeamName();
|
|
72
73
|
if (!statusItems.hypercredits) {
|
|
73
74
|
ctx.ui.setStatus(PROVIDER_NAME, teamNameStatusText(statusItems, teamName));
|
|
74
75
|
return;
|
|
@@ -94,6 +95,7 @@ export function registerCreditStatus(pi: ExtensionAPI): void {
|
|
|
94
95
|
}
|
|
95
96
|
|
|
96
97
|
function refreshInBackground(ctx: ExtensionContext, selectedModel: ExtensionContext["model"] = ctx.model): void {
|
|
98
|
+
if (!ctx.hasUI) return;
|
|
97
99
|
void refresh(ctx, selectedModel).catch((err) => {
|
|
98
100
|
console.error(`Failed to update Hyper status: ${String(err)}`);
|
|
99
101
|
});
|
package/src/index.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
+
import { createProvider, envApiKeyAuth } from "@earendil-works/pi-ai";
|
|
2
|
+
import { openAICompletionsApi } from "@earendil-works/pi-ai/compat";
|
|
1
3
|
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
4
|
import { registerCreditStatus } from "./credits.js";
|
|
3
|
-
import {
|
|
5
|
+
import { hyperApiBaseUrl, PROVIDER_DISPLAY_NAME, PROVIDER_NAME } from "./hyper.js";
|
|
4
6
|
import { loadModels } from "./models.js";
|
|
5
7
|
import { loginHyper, refreshHyperToken } from "./oauth.js";
|
|
6
8
|
import { migrateHyperSettings } from "./settings.js";
|
|
7
|
-
|
|
8
9
|
export default async function (pi: ExtensionAPI) {
|
|
9
10
|
try {
|
|
10
11
|
migrateHyperSettings();
|
|
@@ -13,22 +14,24 @@ export default async function (pi: ExtensionAPI) {
|
|
|
13
14
|
}
|
|
14
15
|
const models = await loadModels();
|
|
15
16
|
|
|
16
|
-
pi.registerProvider(
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
apiKey: HYPER_API_KEY,
|
|
20
|
-
headers: {
|
|
21
|
-
"User-Agent": HYPER_USER_AGENT,
|
|
22
|
-
},
|
|
23
|
-
api: "openai-completions",
|
|
24
|
-
models,
|
|
25
|
-
oauth: {
|
|
17
|
+
pi.registerProvider(
|
|
18
|
+
createProvider({
|
|
19
|
+
id: PROVIDER_NAME,
|
|
26
20
|
name: PROVIDER_DISPLAY_NAME,
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
21
|
+
baseUrl: hyperApiBaseUrl(),
|
|
22
|
+
auth: {
|
|
23
|
+
apiKey: envApiKeyAuth("Hyper API key", ["HYPER_API_KEY"]),
|
|
24
|
+
oauth: {
|
|
25
|
+
name: PROVIDER_DISPLAY_NAME,
|
|
26
|
+
login: loginHyper,
|
|
27
|
+
refresh: refreshHyperToken,
|
|
28
|
+
toAuth: async (credential) => ({ apiKey: credential.access }),
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
models,
|
|
32
|
+
api: openAICompletionsApi(),
|
|
33
|
+
}),
|
|
34
|
+
);
|
|
32
35
|
|
|
33
36
|
registerCreditStatus(pi);
|
|
34
37
|
}
|
package/src/models.ts
CHANGED
|
@@ -1,17 +1,29 @@
|
|
|
1
1
|
import { existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync } from "node:fs";
|
|
2
2
|
import * as path from "node:path";
|
|
3
|
-
import type { ThinkingLevel, ThinkingLevelMap } from "@earendil-works/pi-ai";
|
|
4
|
-
import type { ProviderModelConfig } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import type { Model, ThinkingLevel, ThinkingLevelMap } from "@earendil-works/pi-ai";
|
|
5
4
|
import { type Static, Type } from "typebox";
|
|
6
5
|
import { Value } from "typebox/value";
|
|
7
6
|
import { fetchJson } from "./http.js";
|
|
8
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
HYPER_USER_AGENT,
|
|
9
|
+
hyperApiBaseUrl,
|
|
10
|
+
hyperProviderDir,
|
|
11
|
+
legacyHyperExtensionDir,
|
|
12
|
+
PROVIDER_NAME,
|
|
13
|
+
} from "./hyper.js";
|
|
9
14
|
import { parseSchema } from "./schema.js";
|
|
10
15
|
|
|
11
16
|
const MODEL_FETCH_TIMEOUT_MS = 3_000;
|
|
12
17
|
const MODEL_CACHE_VERSION = 1;
|
|
13
18
|
|
|
14
|
-
const PI_THINKING_LEVELS = [
|
|
19
|
+
const PI_THINKING_LEVELS = [
|
|
20
|
+
"minimal",
|
|
21
|
+
"low",
|
|
22
|
+
"medium",
|
|
23
|
+
"high",
|
|
24
|
+
"xhigh",
|
|
25
|
+
"max",
|
|
26
|
+
] as const satisfies readonly ThinkingLevel[];
|
|
15
27
|
// Hyper models without reasoning levels are on/off-only. Use Pi's medium level
|
|
16
28
|
// as the single representative "on" state; it is not sent as reasoning_effort.
|
|
17
29
|
const ON_OFF_THINKING_LEVEL_MAP: ThinkingLevelMap = {
|
|
@@ -115,8 +127,8 @@ function legacyModelCachePath(): string {
|
|
|
115
127
|
return path.join(legacyHyperExtensionDir(), "models.json");
|
|
116
128
|
}
|
|
117
129
|
|
|
118
|
-
function toProviderModel(model: ProviderModel):
|
|
119
|
-
const input:
|
|
130
|
+
function toProviderModel(model: ProviderModel): Model<"openai-completions"> {
|
|
131
|
+
const input: ("text" | "image")[] = model.supports_attachments ? ["text", "image"] : ["text"];
|
|
120
132
|
const reasoningLevels = model.reasoning_levels ?? [];
|
|
121
133
|
const supportsReasoningEffort = reasoningLevels.length > 0;
|
|
122
134
|
const thinkingLevelMap = supportsReasoningEffort
|
|
@@ -128,6 +140,10 @@ function toProviderModel(model: ProviderModel): ProviderModelConfig {
|
|
|
128
140
|
return {
|
|
129
141
|
id: model.id,
|
|
130
142
|
name: model.name,
|
|
143
|
+
api: "openai-completions",
|
|
144
|
+
provider: PROVIDER_NAME,
|
|
145
|
+
baseUrl: hyperApiBaseUrl(),
|
|
146
|
+
headers: { "User-Agent": HYPER_USER_AGENT },
|
|
131
147
|
reasoning: model.can_reason,
|
|
132
148
|
thinkingLevelMap,
|
|
133
149
|
input,
|
|
@@ -150,8 +166,8 @@ function toProviderModel(model: ProviderModel): ProviderModelConfig {
|
|
|
150
166
|
};
|
|
151
167
|
}
|
|
152
168
|
|
|
153
|
-
function toLegacyProviderModel(model: HyperModel):
|
|
154
|
-
const input:
|
|
169
|
+
function toLegacyProviderModel(model: HyperModel): Model<"openai-completions"> {
|
|
170
|
+
const input: ("text" | "image")[] = model.supports_attachments ? ["text", "image"] : ["text"];
|
|
155
171
|
const thinkingLevelMap = model.supports_reasoning_effort
|
|
156
172
|
? buildThinkingLevelMap(model.reasoning_effort_levels)
|
|
157
173
|
: model.supports_reasoning
|
|
@@ -161,6 +177,10 @@ function toLegacyProviderModel(model: HyperModel): ProviderModelConfig {
|
|
|
161
177
|
return {
|
|
162
178
|
id: model.id,
|
|
163
179
|
name: model.display_name,
|
|
180
|
+
api: "openai-completions",
|
|
181
|
+
provider: PROVIDER_NAME,
|
|
182
|
+
baseUrl: hyperApiBaseUrl(),
|
|
183
|
+
headers: { "User-Agent": HYPER_USER_AGENT },
|
|
164
184
|
reasoning: model.supports_reasoning,
|
|
165
185
|
thinkingLevelMap,
|
|
166
186
|
input,
|
|
@@ -176,7 +196,7 @@ function toLegacyProviderModel(model: HyperModel): ProviderModelConfig {
|
|
|
176
196
|
};
|
|
177
197
|
}
|
|
178
198
|
|
|
179
|
-
function modelsFromCatalog(catalog: ModelCatalog):
|
|
199
|
+
function modelsFromCatalog(catalog: ModelCatalog): Model<"openai-completions">[] {
|
|
180
200
|
switch (catalog.kind) {
|
|
181
201
|
case "provider":
|
|
182
202
|
return catalog.payload.models.map(toProviderModel);
|
|
@@ -323,7 +343,7 @@ function isRecord(value: unknown): value is Record<string, unknown> {
|
|
|
323
343
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
324
344
|
}
|
|
325
345
|
|
|
326
|
-
export async function loadModels(): Promise<
|
|
346
|
+
export async function loadModels(): Promise<Model<"openai-completions">[]> {
|
|
327
347
|
try {
|
|
328
348
|
const payload = await fetchModelPayload();
|
|
329
349
|
const providerCatalog = providerPayload(payload, "Hyper /provider response");
|
package/src/oauth.ts
CHANGED
|
@@ -1,12 +1,97 @@
|
|
|
1
1
|
import { hostname } from "node:os";
|
|
2
|
-
import type {
|
|
3
|
-
import { pollOAuthDeviceCodeFlow } from "@earendil-works/pi-ai/oauth";
|
|
2
|
+
import type { AuthInteraction, OAuthCredential } from "@earendil-works/pi-ai";
|
|
4
3
|
import { type Static, Type } from "typebox";
|
|
5
4
|
import { Value } from "typebox/value";
|
|
6
5
|
import { fetchJson, fetchJsonResponse } from "./http.js";
|
|
7
6
|
import { hyperBaseUrl, hyperJsonHeaders } from "./hyper.js";
|
|
8
7
|
import { parseSchema } from "./schema.js";
|
|
9
8
|
|
|
9
|
+
// Inlined from pi-ai (no longer publicly exported)
|
|
10
|
+
const CANCEL_MESSAGE = "Login cancelled";
|
|
11
|
+
const TIMEOUT_MESSAGE = "Device flow timed out";
|
|
12
|
+
const SLOW_DOWN_TIMEOUT_MESSAGE =
|
|
13
|
+
"Device flow timed out after one or more slow_down responses. This is often caused by clock drift in WSL or VM environments. Please sync or restart the VM clock and try again.";
|
|
14
|
+
const MINIMUM_INTERVAL_MS = 1000;
|
|
15
|
+
const POLL_DEFAULT_INTERVAL_SECONDS = 5;
|
|
16
|
+
const SLOW_DOWN_INTERVAL_INCREMENT_MS = 5000;
|
|
17
|
+
|
|
18
|
+
type OAuthDeviceCodeIncompletePollResult =
|
|
19
|
+
| { status: "pending" }
|
|
20
|
+
| { status: "slow_down"; intervalSeconds?: number }
|
|
21
|
+
| { status: "failed"; message: string };
|
|
22
|
+
|
|
23
|
+
type OAuthDeviceCodePollResult<T> = OAuthDeviceCodeIncompletePollResult | { status: "complete"; value: T };
|
|
24
|
+
|
|
25
|
+
function abortableSleep(ms: number, signal: AbortSignal | undefined, cancelMessage: string): Promise<void> {
|
|
26
|
+
return new Promise((resolve, reject) => {
|
|
27
|
+
if (signal?.aborted) {
|
|
28
|
+
reject(new Error(cancelMessage));
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const onAbort = () => {
|
|
33
|
+
clearTimeout(timeout);
|
|
34
|
+
reject(new Error(cancelMessage));
|
|
35
|
+
};
|
|
36
|
+
const timeout = setTimeout(() => {
|
|
37
|
+
signal?.removeEventListener("abort", onAbort);
|
|
38
|
+
resolve();
|
|
39
|
+
}, ms);
|
|
40
|
+
|
|
41
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async function pollOAuthDeviceCodeFlow<T>(options: {
|
|
46
|
+
intervalSeconds?: number;
|
|
47
|
+
expiresInSeconds?: number;
|
|
48
|
+
poll: () => Promise<OAuthDeviceCodePollResult<T>>;
|
|
49
|
+
signal?: AbortSignal;
|
|
50
|
+
}): Promise<T> {
|
|
51
|
+
const deadline =
|
|
52
|
+
typeof options.expiresInSeconds === "number"
|
|
53
|
+
? Date.now() + options.expiresInSeconds * 1000
|
|
54
|
+
: Number.POSITIVE_INFINITY;
|
|
55
|
+
let intervalMs = Math.max(
|
|
56
|
+
MINIMUM_INTERVAL_MS,
|
|
57
|
+
Math.floor((options.intervalSeconds ?? POLL_DEFAULT_INTERVAL_SECONDS) * 1000),
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
let slowDownResponses = 0;
|
|
61
|
+
|
|
62
|
+
while (Date.now() < deadline) {
|
|
63
|
+
if (options.signal?.aborted) {
|
|
64
|
+
throw new Error(CANCEL_MESSAGE);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const result = await options.poll();
|
|
68
|
+
if (result.status === "complete") {
|
|
69
|
+
return result.value;
|
|
70
|
+
}
|
|
71
|
+
if (result.status === "failed") {
|
|
72
|
+
throw new Error(result.message);
|
|
73
|
+
}
|
|
74
|
+
if (result.status === "slow_down") {
|
|
75
|
+
slowDownResponses += 1;
|
|
76
|
+
intervalMs =
|
|
77
|
+
typeof result.intervalSeconds === "number" &&
|
|
78
|
+
Number.isFinite(result.intervalSeconds) &&
|
|
79
|
+
result.intervalSeconds > 0
|
|
80
|
+
? Math.max(MINIMUM_INTERVAL_MS, Math.floor(result.intervalSeconds * 1000))
|
|
81
|
+
: Math.max(MINIMUM_INTERVAL_MS, intervalMs + SLOW_DOWN_INTERVAL_INCREMENT_MS);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const remainingMs = deadline - Date.now();
|
|
85
|
+
if (remainingMs <= 0) {
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
await abortableSleep(Math.min(intervalMs, remainingMs), options.signal, CANCEL_MESSAGE);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
throw new Error(slowDownResponses > 0 ? SLOW_DOWN_TIMEOUT_MESSAGE : TIMEOUT_MESSAGE);
|
|
93
|
+
}
|
|
94
|
+
|
|
10
95
|
const DEFAULT_DEVICE_POLL_INTERVAL_SECONDS = 5;
|
|
11
96
|
const TOKEN_EXPIRY_BUFFER_MS = 30_000;
|
|
12
97
|
const OAUTH_FETCH_TIMEOUT_MS = 30_000;
|
|
@@ -169,9 +254,10 @@ function tokenToCredentials(
|
|
|
169
254
|
token: TokenExchangeResponse,
|
|
170
255
|
fallbackRefreshToken: string,
|
|
171
256
|
metadata?: { teamName?: string },
|
|
172
|
-
):
|
|
257
|
+
): OAuthCredential {
|
|
173
258
|
const expires = tokenExpiresAtMs(token);
|
|
174
259
|
return {
|
|
260
|
+
type: "oauth",
|
|
175
261
|
refresh: token.refresh_token || fallbackRefreshToken,
|
|
176
262
|
access: token.access_token,
|
|
177
263
|
expires,
|
|
@@ -190,30 +276,31 @@ function tokenExpiresAtMs(token: TokenExchangeResponse): number {
|
|
|
190
276
|
return expiresAt - bufferMs;
|
|
191
277
|
}
|
|
192
278
|
|
|
193
|
-
export async function loginHyper(
|
|
194
|
-
const deviceAuth = await initiateDeviceAuth(
|
|
195
|
-
|
|
279
|
+
export async function loginHyper(interaction: AuthInteraction): Promise<OAuthCredential> {
|
|
280
|
+
const deviceAuth = await initiateDeviceAuth(interaction.signal);
|
|
281
|
+
interaction.notify({
|
|
282
|
+
type: "device_code",
|
|
196
283
|
userCode: deviceAuth.user_code,
|
|
197
284
|
verificationUri: deviceAuth.verification_url,
|
|
198
285
|
intervalSeconds: deviceAuth.interval ?? DEFAULT_DEVICE_POLL_INTERVAL_SECONDS,
|
|
199
286
|
expiresInSeconds: deviceAuth.expires_in,
|
|
200
287
|
});
|
|
201
288
|
|
|
202
|
-
const deviceToken = await pollDeviceAuth(deviceAuth,
|
|
203
|
-
const token = await exchangeRefreshToken(deviceToken.refresh_token,
|
|
289
|
+
const deviceToken = await pollDeviceAuth(deviceAuth, interaction.signal);
|
|
290
|
+
const token = await exchangeRefreshToken(deviceToken.refresh_token, interaction.signal);
|
|
204
291
|
return tokenToCredentials(token, deviceToken.refresh_token, {
|
|
205
292
|
teamName: deviceToken.team_name,
|
|
206
293
|
});
|
|
207
294
|
}
|
|
208
295
|
|
|
209
|
-
export async function refreshHyperToken(
|
|
210
|
-
const token = await exchangeRefreshToken(
|
|
211
|
-
return tokenToCredentials(token,
|
|
212
|
-
teamName: teamNameFromCredentials(
|
|
296
|
+
export async function refreshHyperToken(credential: OAuthCredential, signal?: AbortSignal): Promise<OAuthCredential> {
|
|
297
|
+
const token = await exchangeRefreshToken(credential.refresh, signal);
|
|
298
|
+
return tokenToCredentials(token, credential.refresh, {
|
|
299
|
+
teamName: teamNameFromCredentials(credential),
|
|
213
300
|
});
|
|
214
301
|
}
|
|
215
302
|
|
|
216
|
-
function teamNameFromCredentials(
|
|
217
|
-
const teamName =
|
|
303
|
+
function teamNameFromCredentials(credential: OAuthCredential): string | undefined {
|
|
304
|
+
const teamName = credential.teamName;
|
|
218
305
|
return typeof teamName === "string" && teamName.trim() ? teamName : undefined;
|
|
219
306
|
}
|