@charmland/pi-hyper-provider 0.1.2 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@charmland/pi-hyper-provider",
3
- "version": "0.1.2",
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.80.10",
34
- "@earendil-works/pi-coding-agent": "^0.80.10",
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
@@ -95,6 +95,7 @@ export function registerCreditStatus(pi: ExtensionAPI): void {
95
95
  }
96
96
 
97
97
  function refreshInBackground(ctx: ExtensionContext, selectedModel: ExtensionContext["model"] = ctx.model): void {
98
+ if (!ctx.hasUI) return;
98
99
  void refresh(ctx, selectedModel).catch((err) => {
99
100
  console.error(`Failed to update Hyper status: ${String(err)}`);
100
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 { HYPER_API_KEY, HYPER_USER_AGENT, hyperApiBaseUrl, PROVIDER_DISPLAY_NAME, PROVIDER_NAME } from "./hyper.js";
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(PROVIDER_NAME, {
17
- name: PROVIDER_DISPLAY_NAME,
18
- baseUrl: hyperApiBaseUrl(),
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
- login: loginHyper,
28
- refreshToken: refreshHyperToken,
29
- getApiKey: (credentials) => credentials.access,
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,11 +1,16 @@
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 { hyperApiBaseUrl, hyperProviderDir, legacyHyperExtensionDir } from "./hyper.js";
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;
@@ -122,8 +127,8 @@ function legacyModelCachePath(): string {
122
127
  return path.join(legacyHyperExtensionDir(), "models.json");
123
128
  }
124
129
 
125
- function toProviderModel(model: ProviderModel): ProviderModelConfig {
126
- const input: ProviderModelConfig["input"] = model.supports_attachments ? ["text", "image"] : ["text"];
130
+ function toProviderModel(model: ProviderModel): Model<"openai-completions"> {
131
+ const input: ("text" | "image")[] = model.supports_attachments ? ["text", "image"] : ["text"];
127
132
  const reasoningLevels = model.reasoning_levels ?? [];
128
133
  const supportsReasoningEffort = reasoningLevels.length > 0;
129
134
  const thinkingLevelMap = supportsReasoningEffort
@@ -135,6 +140,10 @@ function toProviderModel(model: ProviderModel): ProviderModelConfig {
135
140
  return {
136
141
  id: model.id,
137
142
  name: model.name,
143
+ api: "openai-completions",
144
+ provider: PROVIDER_NAME,
145
+ baseUrl: hyperApiBaseUrl(),
146
+ headers: { "User-Agent": HYPER_USER_AGENT },
138
147
  reasoning: model.can_reason,
139
148
  thinkingLevelMap,
140
149
  input,
@@ -157,8 +166,8 @@ function toProviderModel(model: ProviderModel): ProviderModelConfig {
157
166
  };
158
167
  }
159
168
 
160
- function toLegacyProviderModel(model: HyperModel): ProviderModelConfig {
161
- const input: ProviderModelConfig["input"] = model.supports_attachments ? ["text", "image"] : ["text"];
169
+ function toLegacyProviderModel(model: HyperModel): Model<"openai-completions"> {
170
+ const input: ("text" | "image")[] = model.supports_attachments ? ["text", "image"] : ["text"];
162
171
  const thinkingLevelMap = model.supports_reasoning_effort
163
172
  ? buildThinkingLevelMap(model.reasoning_effort_levels)
164
173
  : model.supports_reasoning
@@ -168,6 +177,10 @@ function toLegacyProviderModel(model: HyperModel): ProviderModelConfig {
168
177
  return {
169
178
  id: model.id,
170
179
  name: model.display_name,
180
+ api: "openai-completions",
181
+ provider: PROVIDER_NAME,
182
+ baseUrl: hyperApiBaseUrl(),
183
+ headers: { "User-Agent": HYPER_USER_AGENT },
171
184
  reasoning: model.supports_reasoning,
172
185
  thinkingLevelMap,
173
186
  input,
@@ -183,7 +196,7 @@ function toLegacyProviderModel(model: HyperModel): ProviderModelConfig {
183
196
  };
184
197
  }
185
198
 
186
- function modelsFromCatalog(catalog: ModelCatalog): ProviderModelConfig[] {
199
+ function modelsFromCatalog(catalog: ModelCatalog): Model<"openai-completions">[] {
187
200
  switch (catalog.kind) {
188
201
  case "provider":
189
202
  return catalog.payload.models.map(toProviderModel);
@@ -330,7 +343,7 @@ function isRecord(value: unknown): value is Record<string, unknown> {
330
343
  return typeof value === "object" && value !== null && !Array.isArray(value);
331
344
  }
332
345
 
333
- export async function loadModels(): Promise<ProviderModelConfig[]> {
346
+ export async function loadModels(): Promise<Model<"openai-completions">[]> {
334
347
  try {
335
348
  const payload = await fetchModelPayload();
336
349
  const providerCatalog = providerPayload(payload, "Hyper /provider response");
package/src/oauth.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { hostname } from "node:os";
2
- import type { OAuthCredentials, OAuthLoginCallbacks } from "@earendil-works/pi-ai";
2
+ import type { AuthInteraction, OAuthCredential } from "@earendil-works/pi-ai";
3
3
  import { type Static, Type } from "typebox";
4
4
  import { Value } from "typebox/value";
5
5
  import { fetchJson, fetchJsonResponse } from "./http.js";
@@ -254,9 +254,10 @@ function tokenToCredentials(
254
254
  token: TokenExchangeResponse,
255
255
  fallbackRefreshToken: string,
256
256
  metadata?: { teamName?: string },
257
- ): OAuthCredentials {
257
+ ): OAuthCredential {
258
258
  const expires = tokenExpiresAtMs(token);
259
259
  return {
260
+ type: "oauth",
260
261
  refresh: token.refresh_token || fallbackRefreshToken,
261
262
  access: token.access_token,
262
263
  expires,
@@ -275,30 +276,31 @@ function tokenExpiresAtMs(token: TokenExchangeResponse): number {
275
276
  return expiresAt - bufferMs;
276
277
  }
277
278
 
278
- export async function loginHyper(callbacks: OAuthLoginCallbacks): Promise<OAuthCredentials> {
279
- const deviceAuth = await initiateDeviceAuth(callbacks.signal);
280
- callbacks.onDeviceCode({
279
+ export async function loginHyper(interaction: AuthInteraction): Promise<OAuthCredential> {
280
+ const deviceAuth = await initiateDeviceAuth(interaction.signal);
281
+ interaction.notify({
282
+ type: "device_code",
281
283
  userCode: deviceAuth.user_code,
282
284
  verificationUri: deviceAuth.verification_url,
283
285
  intervalSeconds: deviceAuth.interval ?? DEFAULT_DEVICE_POLL_INTERVAL_SECONDS,
284
286
  expiresInSeconds: deviceAuth.expires_in,
285
287
  });
286
288
 
287
- const deviceToken = await pollDeviceAuth(deviceAuth, callbacks.signal);
288
- const token = await exchangeRefreshToken(deviceToken.refresh_token, callbacks.signal);
289
+ const deviceToken = await pollDeviceAuth(deviceAuth, interaction.signal);
290
+ const token = await exchangeRefreshToken(deviceToken.refresh_token, interaction.signal);
289
291
  return tokenToCredentials(token, deviceToken.refresh_token, {
290
292
  teamName: deviceToken.team_name,
291
293
  });
292
294
  }
293
295
 
294
- export async function refreshHyperToken(credentials: OAuthCredentials): Promise<OAuthCredentials> {
295
- const token = await exchangeRefreshToken(credentials.refresh);
296
- return tokenToCredentials(token, credentials.refresh, {
297
- teamName: teamNameFromCredentials(credentials),
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),
298
300
  });
299
301
  }
300
302
 
301
- function teamNameFromCredentials(credentials: OAuthCredentials): string | undefined {
302
- const teamName = credentials.teamName;
303
+ function teamNameFromCredentials(credential: OAuthCredential): string | undefined {
304
+ const teamName = credential.teamName;
303
305
  return typeof teamName === "string" && teamName.trim() ? teamName : undefined;
304
306
  }