@hyav/pi-provider 0.1.7 → 0.2.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/core/runtime.ts CHANGED
@@ -1,21 +1,14 @@
1
1
  import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
2
- import type { PiProviderDefinition } from "./definition.ts";
3
- import { validatePiProviderDefinition } from "./definition.ts";
2
+ import { type PiProviderDefinition, validatePiProviderDefinition } from "./definition.ts";
4
3
  import { LiveCheckManager, type LiveCheckResult } from "./live-check-manager.ts";
5
4
  import {
6
- fetchOfficialPricing,
7
- findOfficialMeta,
8
- getPricingCacheAge,
9
- type OfficialModelMeta,
10
- OPENROUTER_MODELS_URL,
11
- } from "./official-pricing.ts";
12
- import type { PreflightContextLike } from "./preflight-manager.ts";
13
- import { PreflightManager } from "./preflight-manager.ts";
14
- import {
15
- cancelDeferredProviderRegistrations,
16
- refreshProviderRegistrations,
17
- registerProviderAdapter,
18
- } from "./provider-registration.ts";
5
+ createEmptyCatalogSnapshot,
6
+ loadPiCatalog,
7
+ type PiCatalogSnapshot,
8
+ toPiCatalogSnapshot,
9
+ } from "./pi-model-metadata.ts";
10
+ import { type PreflightContextLike, PreflightManager } from "./preflight-manager.ts";
11
+ import { cancelDeferredProviderRegistrations, registerProviderAdapter } from "./provider-registration.ts";
19
12
  import type { PiProviderDependencies, PiProviderLoader } from "./runtime-config.ts";
20
13
  import { resolvePiProviderDependencies } from "./runtime-config.ts";
21
14
  import type { StatusContextLike } from "./status-manager.ts";
@@ -30,7 +23,6 @@ import {
30
23
  } from "./status-report.ts";
31
24
  import { applyTunerAdapters, sortTunerAdapters } from "./tuner-manager.ts";
32
25
  import type {
33
- ModelMetadataStatus,
34
26
  ProviderAdapter,
35
27
  ProviderCost,
36
28
  ProviderModelDraft,
@@ -116,7 +108,9 @@ export function scheduleModelCatalogRefresh(ctx: Pick<ExtensionContext, "modelRe
116
108
 
117
109
  export interface PiProviderRuntimeController {
118
110
  resetForSession(): void;
119
- updateOfficialPricing?(snapshot: Record<string, OfficialModelMeta>): void;
111
+ /** @deprecated Kept for backwards compatibility. */
112
+ updateOfficialPricing?(snapshot: Record<string, unknown>): void;
113
+ updatePiCatalog?(snapshot: PiCatalogSnapshot): void;
120
114
  shutdown(): void;
121
115
  clearStatusPresentation(ctx: Pick<ExtensionContext, "ui">): void;
122
116
  applyTunerPayload(payload: unknown, model: ActiveModel): unknown | undefined | Promise<unknown | undefined>;
@@ -135,23 +129,6 @@ function cloneProviderCost(cost: ProviderCost): ProviderCost {
135
129
  };
136
130
  }
137
131
 
138
- function getOfficialMetadataStatus(
139
- snapshot: Record<string, OfficialModelMeta>,
140
- runtime: PiProviderDependencies,
141
- ): ModelMetadataStatus | undefined {
142
- const source = runtime.officialPricingUrl === OPENROUTER_MODELS_URL ? "AA/OpenRouter" : "Official metadata";
143
- if (!runtime.enableOfficialPricingFallback && Object.keys(snapshot).length === 0) return undefined;
144
- if (Object.keys(snapshot).length === 0) return { state: "unavailable", source };
145
- const now = runtime.now();
146
- const age = getPricingCacheAge(runtime.officialPricingUrl, now);
147
- const updatedAt = age === undefined ? now : now - age;
148
- return {
149
- state: age !== undefined && age >= runtime.officialPricingCacheTtlMs ? "stale" : "fresh",
150
- updatedAt,
151
- source,
152
- };
153
- }
154
-
155
132
  function hasKnownNativeCost(cost: ProviderCost | undefined): cost is ProviderCost {
156
133
  if (!cost) return false;
157
134
  return (
@@ -164,12 +141,7 @@ function hasKnownNativeCost(cost: ProviderCost | undefined): cost is ProviderCos
164
141
  }
165
142
 
166
143
  /** Build report-only metadata; never merge external fields into Pi's native model. */
167
- function getNativeModelMetadata(
168
- model: ActiveModel,
169
- officialPricing: Record<string, OfficialModelMeta>,
170
- ): ProviderModelMetadata {
171
- const officialMeta =
172
- findOfficialMeta(`${model.provider}/${model.id}`, officialPricing) ?? findOfficialMeta(model.id, officialPricing);
144
+ function getNativeModelMetadata(model: ActiveModel): ProviderModelMetadata {
173
145
  const knownPrice = hasKnownNativeCost(model.cost);
174
146
  return {
175
147
  pricing: {
@@ -180,19 +152,13 @@ function getNativeModelMetadata(
180
152
  : {}),
181
153
  },
182
154
  fieldSources: {
155
+ cost: "native",
183
156
  contextWindow: "native",
184
157
  maxTokens: "native",
185
158
  input: "native",
186
159
  reasoning: "native",
160
+ thinkingLevelMap: model.thinkingLevelMap === undefined ? "default" : "native",
187
161
  },
188
- ...(officialMeta?.quality
189
- ? {
190
- quality: officialMeta.quality.map((score) => ({
191
- ...score,
192
- ...(score.confidenceInterval ? { confidenceInterval: { ...score.confidenceInterval } } : {}),
193
- })),
194
- }
195
- : {}),
196
162
  };
197
163
  }
198
164
 
@@ -200,7 +166,7 @@ export function installPiProviderRuntime(
200
166
  pi: ExtensionAPI,
201
167
  runtime: PiProviderDependencies,
202
168
  definition: PiProviderDefinition,
203
- officialPricing: Record<string, OfficialModelMeta> = {},
169
+ piCatalogSnapshot: PiCatalogSnapshot | Record<string, unknown> = {},
204
170
  options: {
205
171
  registerHandlers?: boolean;
206
172
  providerDrafts?: ReadonlyMap<ProviderAdapter, ProviderModelDraft[]>;
@@ -208,20 +174,20 @@ export function installPiProviderRuntime(
208
174
  ): PiProviderRuntimeController {
209
175
  validatePiProviderDefinition(definition);
210
176
  const registerHandlers = options.registerHandlers ?? true;
211
- let currentOfficialPricing = officialPricing;
212
- let currentOfficialMetadataStatus = getOfficialMetadataStatus(officialPricing, runtime);
177
+ let currentPiCatalog: PiCatalogSnapshot = toPiCatalogSnapshot(piCatalogSnapshot) ?? createEmptyCatalogSnapshot();
213
178
  const providers = [...definition.providers].sort(compareAdapterIds);
214
179
  const statuses = [...(definition.statuses ?? [])].sort(compareAdapterIds);
215
180
  const preflights = [...(definition.preflights ?? [])].sort(compareAdapterIds);
216
181
  const tuners = sortTunerAdapters(definition.tuners ?? []);
217
182
 
218
183
  for (const adapter of providers) {
219
- registerProviderAdapter(pi, adapter, runtime, officialPricing, options.providerDrafts?.get(adapter));
184
+ registerProviderAdapter(pi, adapter, runtime, currentPiCatalog, options.providerDrafts?.get(adapter));
220
185
  }
221
186
 
222
187
  const statusManager = new StatusManager(statuses, runtime.fetch, runtime.now);
223
188
  const preflightManager = new PreflightManager(preflights, runtime.fetch, runtime.now);
224
189
  const liveCheckManager = new LiveCheckManager(runtime.liveCheckRequestTimeoutMs, runtime.fetch, runtime.now);
190
+
225
191
  let lifecycleGeneration = 0;
226
192
  let statusPresentationGeneration = 0;
227
193
  let statusPresentationVisible = false;
@@ -237,10 +203,9 @@ export function installPiProviderRuntime(
237
203
  const native = resolveNativeProvider(createNativeProviderRegistry(ctx.modelRegistry), model.provider);
238
204
  return {
239
205
  provider,
240
- metadataStatus: currentOfficialMetadataStatus,
241
206
  modelMetadata:
242
207
  provider?.registration?.modelMetadata?.[model.id] ??
243
- (provider === undefined ? getNativeModelMetadata(model, currentOfficialPricing) : undefined),
208
+ (provider === undefined ? getNativeModelMetadata(model) : undefined),
244
209
  status: statuses.find(({ providerId }) => providerId === model.provider),
245
210
  preflight: preflights.find(({ providerId }) => providerId === model.provider),
246
211
  nativeProvider: native.provider,
@@ -281,7 +246,6 @@ export function installPiProviderRuntime(
281
246
  nativeLookupAvailable,
282
247
  nativePreflight,
283
248
  auth,
284
- metadataStatus,
285
249
  } = getStatusDetails(model, ctx);
286
250
  const diagnostics = status ? statusManager.getDiagnostics(model.provider) : undefined;
287
251
  const preflightDiagnostics = preflightManager.getDiagnostics(model.provider, model.id);
@@ -307,7 +271,6 @@ export function installPiProviderRuntime(
307
271
  liveCheckDiagnostics?.pending === true ||
308
272
  liveCheckDiagnostics?.lastError !== undefined),
309
273
  modelMetadata,
310
- metadataStatus,
311
274
  },
312
275
  );
313
276
  const message = report.report;
@@ -421,8 +384,10 @@ export function installPiProviderRuntime(
421
384
  const controller: PiProviderRuntimeController = {
422
385
  resetForSession,
423
386
  updateOfficialPricing(snapshot) {
424
- currentOfficialPricing = snapshot;
425
- currentOfficialMetadataStatus = getOfficialMetadataStatus(snapshot, runtime);
387
+ currentPiCatalog = toPiCatalogSnapshot(snapshot) ?? createEmptyCatalogSnapshot();
388
+ },
389
+ updatePiCatalog(snapshot) {
390
+ currentPiCatalog = toPiCatalogSnapshot(snapshot) ?? createEmptyCatalogSnapshot();
426
391
  },
427
392
  shutdown,
428
393
  clearStatusPresentation,
@@ -462,52 +427,9 @@ export function createPiProviderRuntime(
462
427
  ): (pi: ExtensionAPI) => Promise<void> {
463
428
  const runtime = resolvePiProviderDependencies(dependencies);
464
429
  return async (pi) => {
465
- let installedController: PiProviderRuntimeController | undefined;
466
- let disposed = false;
467
- let pricingRefreshController: AbortController | undefined;
468
- const cachePath =
469
- runtime.officialPricingUrl === OPENROUTER_MODELS_URL ? runtime.openRouterMetadataCachePath : undefined;
470
- const fetchPricing = (options: { allowNetwork?: boolean; signal?: AbortSignal } = {}) =>
471
- fetchOfficialPricing(
472
- runtime.fetch,
473
- runtime.officialPricingUrl,
474
- runtime.officialPricingTimeoutMs,
475
- runtime.officialPricingCacheTtlMs,
476
- runtime.officialPricingMaxStaleMs,
477
- runtime.now,
478
- { cachePath, ...options },
479
- );
480
- const officialPricingPromise = runtime.enableOfficialPricingFallback
481
- ? fetchPricing({ allowNetwork: false })
482
- : Promise.resolve({});
483
- const definitionPromise = loadDefinition(runtime);
484
- const [officialPricing, definition] = await Promise.all([officialPricingPromise, definitionPromise]);
430
+ const piCatalog = await loadPiCatalog({ fetch: runtime.fetch }).catch(() => ({ models: {}, providers: {} }));
431
+ const definition = await loadDefinition(runtime);
485
432
  validatePiProviderDefinition(definition);
486
-
487
- pi.on("session_start", () => {
488
- pricingRefreshController?.abort();
489
- pricingRefreshController = undefined;
490
- if (!runtime.enableOfficialPricingFallback || disposed) {
491
- return;
492
- }
493
- const controller = new AbortController();
494
- pricingRefreshController = controller;
495
- void fetchPricing({ signal: controller.signal })
496
- .then((snapshot) => {
497
- if (disposed || controller.signal.aborted) return;
498
- installedController?.updateOfficialPricing?.(snapshot);
499
- refreshProviderRegistrations(pi, definition.providers, runtime, snapshot);
500
- })
501
- .catch(() => undefined)
502
- .finally(() => {
503
- if (pricingRefreshController === controller) pricingRefreshController = undefined;
504
- });
505
- });
506
- pi.on("session_shutdown", () => {
507
- disposed = true;
508
- pricingRefreshController?.abort();
509
- pricingRefreshController = undefined;
510
- });
511
- installedController = installPiProviderRuntime(pi, runtime, definition, officialPricing);
433
+ installPiProviderRuntime(pi, runtime, definition, piCatalog);
512
434
  };
513
435
  }