pi-cliproxyapi-provider 0.3.9 → 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.
@@ -25,14 +25,6 @@ export default async function (pi: ExtensionAPI) {
25
25
  const runtime = new ProviderRuntime({ pi, config, catalog });
26
26
  registerCliproxyapiCommand(pi, runtime, catalog);
27
27
  await runtime.start();
28
-
29
- let backgroundRefreshStarted = false;
30
- pi.on("session_start", (event) => {
31
- if (event.reason !== "startup" || backgroundRefreshStarted) return;
32
- backgroundRefreshStarted = true;
33
- // Cached models are the normal startup path; refresh quietly in the background.
34
- void runtime.refresh("models", "background");
35
- });
36
28
  } catch (error) {
37
29
  registerCliproxyapiCommand(pi);
38
30
  pi.registerProvider(config.providerName, buildProviderRegistration(config, buildUnavailableProviderModels()).config);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-cliproxyapi-provider",
3
- "version": "0.3.9",
3
+ "version": "0.4.0",
4
4
  "description": "Pi provider package for CLIProxyAPI with automatic model discovery and models.dev enrichment.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -41,8 +41,8 @@
41
41
  "update:models-dev": "node scripts/update-models-dev-fallback.mjs"
42
42
  },
43
43
  "peerDependencies": {
44
- "@earendil-works/pi-ai": "*",
45
- "@earendil-works/pi-coding-agent": "*"
44
+ "@earendil-works/pi-ai": ">=0.82.0",
45
+ "@earendil-works/pi-coding-agent": ">=0.82.0"
46
46
  },
47
47
  "peerDependenciesMeta": {
48
48
  "@earendil-works/pi-coding-agent": {
@@ -53,8 +53,8 @@
53
53
  }
54
54
  },
55
55
  "devDependencies": {
56
- "@earendil-works/pi-ai": "0.80.6",
57
- "@earendil-works/pi-coding-agent": "0.80.6",
56
+ "@earendil-works/pi-ai": "0.82.0",
57
+ "@earendil-works/pi-coding-agent": "0.82.0",
58
58
  "typescript": "^5.9.3"
59
59
  }
60
60
  }
package/src/auth.ts CHANGED
@@ -1,8 +1,10 @@
1
+ import { readStoredCredential } from "@earendil-works/pi-coding-agent";
2
+
1
3
  export async function getDiscoveryApiKey(providerName: string, env: NodeJS.ProcessEnv = process.env): Promise<string | undefined> {
2
4
  try {
3
- const { AuthStorage } = await import("@earendil-works/pi-coding-agent");
4
- const stored = await AuthStorage.create().getApiKey(providerName, { includeFallback: false });
5
- return stored ?? env.CLIPROXYAPI_API_KEY;
5
+ const credential = await readStoredCredential(providerName);
6
+ if (credential?.type === "api_key") return credential.key;
7
+ return env.CLIPROXYAPI_API_KEY;
6
8
  } catch {
7
9
  return env.CLIPROXYAPI_API_KEY;
8
10
  }
package/src/catalog.ts CHANGED
@@ -63,6 +63,8 @@ export class ProviderCatalog {
63
63
  private activeRefresh?: Promise<CatalogRefreshResult>;
64
64
  private activeRefreshTarget?: RefreshTarget;
65
65
  private activeRefreshMode?: "background" | "manual";
66
+ private activeRefreshController?: AbortController;
67
+ private readonly activeRefreshWaiters = new Set<symbol>();
66
68
  private readonly options: ProviderCatalogOptions;
67
69
 
68
70
  constructor(options: ProviderCatalogOptions) {
@@ -79,30 +81,67 @@ export class ProviderCatalog {
79
81
  target: RefreshTarget = "all",
80
82
  mode: "background" | "manual" = "manual",
81
83
  getDiscoveryApiKey?: () => Promise<string | undefined>,
84
+ signal?: AbortSignal,
82
85
  ): Promise<CatalogRefreshResult> {
83
86
  if (this.activeRefresh) {
84
- if (this.activeRefreshTarget === target && this.activeRefreshMode === mode && getDiscoveryApiKey === undefined) return this.activeRefresh;
87
+ if (this.activeRefreshTarget === target && this.activeRefreshMode === mode) {
88
+ return this.waitForActiveRefresh(signal);
89
+ }
85
90
  await this.activeRefresh;
86
91
  }
87
92
 
93
+ const controller = new AbortController();
88
94
  this.activeRefreshTarget = target;
89
95
  this.activeRefreshMode = mode;
90
- this.activeRefresh = this.performRefresh(target, mode, getDiscoveryApiKey).finally(() => {
96
+ this.activeRefreshController = controller;
97
+ this.activeRefresh = this.performRefresh(target, mode, getDiscoveryApiKey, controller.signal).finally(() => {
91
98
  this.activeRefresh = undefined;
92
99
  this.activeRefreshTarget = undefined;
93
100
  this.activeRefreshMode = undefined;
101
+ this.activeRefreshController = undefined;
102
+ this.activeRefreshWaiters.clear();
94
103
  });
95
- return this.activeRefresh;
104
+ return this.waitForActiveRefresh(signal);
96
105
  }
97
106
 
98
107
  current(): CatalogSnapshot | undefined {
99
108
  return this.snapshot;
100
109
  }
101
110
 
111
+ private async waitForActiveRefresh(signal?: AbortSignal): Promise<CatalogRefreshResult> {
112
+ const refresh = this.activeRefresh;
113
+ if (!refresh) throw new Error("No active refresh");
114
+ if (signal?.aborted) throw signal.reason ?? new Error("Refresh aborted");
115
+
116
+ const waiter = Symbol("refresh-waiter");
117
+ this.activeRefreshWaiters.add(waiter);
118
+ let onAbort: (() => void) | undefined;
119
+ const aborted = signal
120
+ ? new Promise<never>((_resolve, reject) => {
121
+ onAbort = () => {
122
+ this.activeRefreshWaiters.delete(waiter);
123
+ if (this.activeRefreshWaiters.size === 0) {
124
+ this.activeRefreshController?.abort(signal.reason ?? new Error("Refresh aborted"));
125
+ }
126
+ reject(signal.reason ?? new Error("Refresh aborted"));
127
+ };
128
+ signal.addEventListener("abort", onAbort, { once: true });
129
+ })
130
+ : undefined;
131
+
132
+ try {
133
+ return await (aborted ? Promise.race([refresh, aborted]) : refresh);
134
+ } finally {
135
+ this.activeRefreshWaiters.delete(waiter);
136
+ if (signal && onAbort) signal.removeEventListener("abort", onAbort);
137
+ }
138
+ }
139
+
102
140
  private async performRefresh(
103
141
  target: RefreshTarget,
104
142
  mode: "background" | "manual",
105
143
  getDiscoveryApiKey?: () => Promise<string | undefined>,
144
+ signal?: AbortSignal,
106
145
  ): Promise<CatalogRefreshResult> {
107
146
  const current = this.snapshot ?? await this.load();
108
147
  let cpaModels = current.cpaModels;
@@ -121,6 +160,7 @@ export class ProviderCatalog {
121
160
  this.options.config.baseUrl,
122
161
  discoveryHeaders(this.options.config, apiKey),
123
162
  mode === "background" ? this.options.backgroundTimeoutMs ?? 2_000 : this.options.manualTimeoutMs ?? 10_000,
163
+ signal,
124
164
  );
125
165
  if (mode === "background" && current.cpaModels.length > 0 && fresh.length === 0) {
126
166
  throw new Error("CPA automatic discovery returned no models; retained the last successful snapshot");
@@ -133,13 +173,14 @@ export class ProviderCatalog {
133
173
  models.changed = changed;
134
174
  models.updated = true;
135
175
  } catch (error) {
176
+ if (signal?.aborted) throw signal.reason ?? error;
136
177
  models.error = error;
137
178
  }
138
179
  }
139
180
 
140
181
  if (metadataResult.attempted) {
141
182
  try {
142
- const fresh = await fetchModelsDevCatalog(this.options.manualTimeoutMs ?? 10_000);
183
+ const fresh = await fetchModelsDevCatalog(this.options.manualTimeoutMs ?? 10_000, signal);
143
184
  const freshUpdatedAt = Date.now();
144
185
  const changed = !sameMetadata(current.metadata, fresh);
145
186
  await (this.options.writeSnapshot ?? writeCache)(modelsDevCachePath(), fresh, freshUpdatedAt);
@@ -149,6 +190,7 @@ export class ProviderCatalog {
149
190
  metadataResult.changed = changed;
150
191
  metadataResult.updated = true;
151
192
  } catch (error) {
193
+ if (signal?.aborted) throw signal.reason ?? error;
152
194
  metadataResult.error = error;
153
195
  }
154
196
  }
package/src/cpa.ts CHANGED
@@ -49,15 +49,20 @@ export function parseCpaModelsResponse(payload: unknown): CpaModel[] {
49
49
  return parseCpaModelEntries(response.data);
50
50
  }
51
51
 
52
- export async function fetchCpaModels(baseUrl: string, headers: Record<string, string> = {}, timeoutMs?: number): Promise<CpaModel[]> {
53
- return withNetworkTimeout(async (signal) => {
52
+ export async function fetchCpaModels(
53
+ baseUrl: string,
54
+ headers: Record<string, string> = {},
55
+ timeoutMs?: number,
56
+ signal?: AbortSignal,
57
+ ): Promise<CpaModel[]> {
58
+ return withNetworkTimeout(async (reqSignal) => {
54
59
  const response = await fetch(modelsEndpoint(baseUrl), {
55
60
  headers: { Accept: "application/json", ...headers },
56
- signal,
61
+ signal: reqSignal,
57
62
  });
58
63
  if (!response.ok) {
59
64
  throw new Error(`CPA model discovery failed: HTTP ${response.status} ${response.statusText}`);
60
65
  }
61
66
  return parseCpaModelsResponse(await response.json());
62
- }, timeoutMs, "CPA model discovery");
67
+ }, timeoutMs, "CPA model discovery", signal);
63
68
  }
package/src/models-dev.ts CHANGED
@@ -38,14 +38,14 @@ export function parseModelsDevCatalog(payload: unknown): ModelsDevCatalog {
38
38
  return catalog;
39
39
  }
40
40
 
41
- export async function fetchModelsDevCatalog(timeoutMs?: number): Promise<ModelsDevCatalog> {
42
- return withNetworkTimeout(async (signal) => {
43
- const response = await fetch(MODELS_DEV_URL, { headers: { Accept: "application/json" }, signal });
41
+ export async function fetchModelsDevCatalog(timeoutMs?: number, signal?: AbortSignal): Promise<ModelsDevCatalog> {
42
+ return withNetworkTimeout(async (reqSignal) => {
43
+ const response = await fetch(MODELS_DEV_URL, { headers: { Accept: "application/json" }, signal: reqSignal });
44
44
  if (!response.ok) {
45
45
  throw new Error(`models.dev fetch failed: HTTP ${response.status} ${response.statusText}`);
46
46
  }
47
47
  return parseModelsDevCatalog(await response.json());
48
- }, timeoutMs, "models.dev fetch");
48
+ }, timeoutMs, "models.dev fetch", signal);
49
49
  }
50
50
 
51
51
  export async function readBundledModelsDevFallback(path: string): Promise<ModelsDevCatalog> {
package/src/network.ts CHANGED
@@ -2,10 +2,23 @@ export async function withNetworkTimeout<T>(
2
2
  operation: (signal: AbortSignal) => Promise<T>,
3
3
  timeoutMs = 10_000,
4
4
  label = "network request",
5
+ parentSignal?: AbortSignal,
5
6
  ): Promise<T> {
6
7
  const controller = new AbortController();
7
8
  const timeoutError = new Error(`${label} timed out after ${timeoutMs}ms`);
8
9
 
10
+ const onParentAbort = () => {
11
+ controller.abort(parentSignal?.reason ?? new Error(`${label} aborted`));
12
+ };
13
+
14
+ if (parentSignal) {
15
+ if (parentSignal.aborted) {
16
+ controller.abort(parentSignal.reason ?? new Error(`${label} aborted`));
17
+ } else {
18
+ parentSignal.addEventListener("abort", onParentAbort, { once: true });
19
+ }
20
+ }
21
+
9
22
  let timeout: ReturnType<typeof setTimeout> | undefined;
10
23
  const operationPromise = operation(controller.signal);
11
24
  const timeoutPromise = new Promise<never>((_resolve, reject) => {
@@ -18,11 +31,17 @@ export async function withNetworkTimeout<T>(
18
31
  try {
19
32
  return await Promise.race([operationPromise, timeoutPromise]);
20
33
  } catch (error) {
34
+ if (parentSignal?.aborted) {
35
+ throw parentSignal.reason ?? error;
36
+ }
21
37
  if (controller.signal.aborted && controller.signal.reason instanceof Error) {
22
38
  throw controller.signal.reason;
23
39
  }
24
40
  throw error;
25
41
  } finally {
26
42
  if (timeout) clearTimeout(timeout);
43
+ if (parentSignal) {
44
+ parentSignal.removeEventListener("abort", onParentAbort);
45
+ }
27
46
  }
28
47
  }
@@ -1,4 +1,5 @@
1
- import type { ProviderConfig } from "@earendil-works/pi-coding-agent";
1
+ import type { RefreshModelsContext } from "@earendil-works/pi-ai";
2
+ import type { ProviderConfig, ProviderModelConfig } from "@earendil-works/pi-coding-agent";
2
3
  import type { CpaProviderConfig } from "./types.ts";
3
4
  import type { ProviderModelConfigLike } from "./types.ts";
4
5
 
@@ -10,6 +11,7 @@ export interface ProviderRegistration {
10
11
  export function buildProviderRegistration(
11
12
  config: CpaProviderConfig,
12
13
  models: ProviderModelConfigLike[],
14
+ refreshModels?: (context: RefreshModelsContext) => Promise<ProviderModelConfig[]>,
13
15
  ): ProviderRegistration {
14
16
  return {
15
17
  providerName: config.providerName,
@@ -20,7 +22,8 @@ export function buildProviderRegistration(
20
22
  apiKey: config.authRequired ? "$CLIPROXYAPI_API_KEY" : "cliproxyapi-no-auth",
21
23
  authHeader: config.authRequired && config.authHeader,
22
24
  headers: Object.keys(config.headers).length > 0 ? config.headers : undefined,
23
- models,
25
+ models: models as ProviderModelConfig[],
26
+ refreshModels,
24
27
  },
25
28
  };
26
29
  }
package/src/runtime.ts CHANGED
@@ -1,4 +1,6 @@
1
- import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
1
+ import type { RefreshModelsContext } from "@earendil-works/pi-ai";
2
+ import type { ExtensionAPI, ProviderModelConfig } from "@earendil-works/pi-coding-agent";
3
+ import { getDiscoveryApiKey } from "./auth.ts";
2
4
  import { ProviderCatalog, type CatalogRefreshResult, type CatalogSnapshot, type RefreshTarget } from "./catalog.ts";
3
5
  import { buildUnavailableProviderModels } from "./provider.ts";
4
6
  import { buildProviderRegistration } from "./registration.ts";
@@ -34,11 +36,27 @@ export class ProviderRuntime {
34
36
  return result;
35
37
  }
36
38
 
39
+ async refreshModels(context: RefreshModelsContext): Promise<ProviderModelConfig[]> {
40
+ if (!context.allowNetwork) {
41
+ const snapshot = await this.options.catalog.load();
42
+ return (snapshot.built.models.length > 0 ? snapshot.built.models : buildUnavailableProviderModels()) as ProviderModelConfig[];
43
+ }
44
+ const mode = context.force ? "manual" : "background";
45
+ const credential = context.credential;
46
+ const keyFn: () => Promise<string | undefined> = credential?.type === "api_key"
47
+ ? async () => credential.key
48
+ : () => getDiscoveryApiKey(this.options.config.providerName);
49
+ const result = await this.options.catalog.refresh("models", mode, keyFn, context.signal);
50
+ const models = (result.snapshot.built.models.length > 0 ? result.snapshot.built.models : buildUnavailableProviderModels()) as ProviderModelConfig[];
51
+ this.register(result.snapshot, false);
52
+ return models;
53
+ }
54
+
37
55
  private register(snapshot: CatalogSnapshot, force: boolean): void {
38
56
  const models = snapshot.built.models.length > 0 ? snapshot.built.models : buildUnavailableProviderModels();
39
57
  const fingerprint = JSON.stringify(models);
40
58
  if (!force && fingerprint === this.registeredFingerprint) return;
41
- const registration = buildProviderRegistration(this.options.config, models);
59
+ const registration = buildProviderRegistration(this.options.config, models, (ctx) => this.refreshModels(ctx));
42
60
  this.options.pi.registerProvider(registration.providerName, registration.config);
43
61
  this.registeredFingerprint = fingerprint;
44
62
  }