@charmland/pi-hyper-provider 0.1.1 → 0.1.2

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 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
- <patch|minor|major|prepatch|preminor|premajor|prerelease>` to increment the
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.1",
3
+ "version": "0.1.2",
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.1",
34
- "@earendil-works/pi-coding-agent": "^0.80.1",
33
+ "@earendil-works/pi-ai": "^0.80.10",
34
+ "@earendil-works/pi-coding-agent": "^0.80.10",
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(ctx: ExtensionContext): string | undefined {
52
- const credential = ctx.modelRegistry.authStorage.get(PROVIDER_NAME);
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(ctx);
72
+ const teamName = storedTeamName();
72
73
  if (!statusItems.hypercredits) {
73
74
  ctx.ui.setStatus(PROVIDER_NAME, teamNameStatusText(statusItems, teamName));
74
75
  return;
package/src/models.ts CHANGED
@@ -11,7 +11,14 @@ import { parseSchema } from "./schema.js";
11
11
  const MODEL_FETCH_TIMEOUT_MS = 3_000;
12
12
  const MODEL_CACHE_VERSION = 1;
13
13
 
14
- const PI_THINKING_LEVELS = ["minimal", "low", "medium", "high", "xhigh"] as const satisfies readonly ThinkingLevel[];
14
+ const PI_THINKING_LEVELS = [
15
+ "minimal",
16
+ "low",
17
+ "medium",
18
+ "high",
19
+ "xhigh",
20
+ "max",
21
+ ] as const satisfies readonly ThinkingLevel[];
15
22
  // Hyper models without reasoning levels are on/off-only. Use Pi's medium level
16
23
  // as the single representative "on" state; it is not sent as reasoning_effort.
17
24
  const ON_OFF_THINKING_LEVEL_MAP: ThinkingLevelMap = {
package/src/oauth.ts CHANGED
@@ -1,12 +1,97 @@
1
1
  import { hostname } from "node:os";
2
2
  import type { OAuthCredentials, OAuthLoginCallbacks } from "@earendil-works/pi-ai";
3
- import { pollOAuthDeviceCodeFlow } from "@earendil-works/pi-ai/oauth";
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;