@averyyy/pi-coding-agent 1.4.0-piclient.12 → 1.4.0-piclient.13

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.
@@ -8,7 +8,7 @@ import { operationSignal, raceWithAbortSignal } from "../utils/abort.js";
8
8
  import { AuthStorage as DefaultAuthStorage } from "./auth-storage.js";
9
9
  import { ModelConfig } from "./model-config.js";
10
10
  import { FileModelsStore, InMemoryCodingAgentModelsStore } from "./models-store.js";
11
- import { cancelDeferredPiServer, classifyPiServer, fetchDeferredPiServer, fetchPiServerModels, generateImagesPiServer, streamPiServer, streamRawPiServer, } from "./pi-server-client.js";
11
+ import { cancelDeferredPiServer, classifyPiServer, fetchDeferredPiServer, generateImagesPiServer, streamPiServer, streamRawPiServer, } from "./pi-server-client.js";
12
12
  import { composeModelProvider, configuredRequestAuthStatus, resolveCompatibilityRequestConfig, resolveConfiguredModelHeaders, validateExtensionProvider, } from "./provider-composer.js";
13
13
  import { withPersonalizedCatalog, withRemoteCatalog } from "./remote-catalog-provider.js";
14
14
  import { RuntimeCredentials } from "./runtime-credentials.js";
@@ -45,7 +45,6 @@ export class ModelRuntime {
45
45
  models;
46
46
  credentials;
47
47
  defaultBuiltins;
48
- nativeCatalogProviders;
49
48
  builtins = new Map();
50
49
  nativeExtensionProviders = new Map();
51
50
  extensionProviders = new Map();
@@ -55,18 +54,8 @@ export class ModelRuntime {
55
54
  modelsPath;
56
55
  modelNetworkEnabled;
57
56
  piServerMode = process.env.PI_SERVER_MODE === "true";
58
- remoteCatalog = {
59
- models: [],
60
- available: [],
61
- };
62
- remoteProviderCapabilities = [];
63
- remoteCatalogError;
64
57
  registrationRefresh = Promise.resolve();
65
58
  registrationRefreshErrors = new Map();
66
- remoteCatalogRefreshSeq = 0;
67
- remoteCatalogRequested = false;
68
- localOperationAvailable = [];
69
- remoteProjection;
70
59
  config;
71
60
  snapshot = {
72
61
  all: [],
@@ -80,12 +69,11 @@ export class ModelRuntime {
80
69
  providerAvailabilitySeq = new Map();
81
70
  availabilityError;
82
71
  credentialOperations = new Map();
83
- constructor(credentials, config, modelsPath, modelsStore, providers, modelNetworkEnabled, nativeCatalogProviders) {
72
+ constructor(credentials, config, modelsPath, modelsStore, providers, modelNetworkEnabled) {
84
73
  this.credentials = credentials;
85
74
  this.config = config;
86
75
  this.modelsPath = modelsPath;
87
76
  this.modelNetworkEnabled = modelNetworkEnabled;
88
- this.nativeCatalogProviders = nativeCatalogProviders;
89
77
  this.defaultBuiltins = new Map(providers.map((provider) => [provider.id, provider]));
90
78
  for (const [providerId, provider] of this.defaultBuiltins)
91
79
  this.builtins.set(providerId, provider);
@@ -111,7 +99,7 @@ export class ModelRuntime {
111
99
  }
112
100
  return withRemoteCatalog(provider, options.catalogBaseUrl, builtinModelDataGeneratedAt);
113
101
  });
114
- const runtime = new ModelRuntime(credentials, config, modelsPath, modelsStore, providers, process.env.PI_OFFLINE === undefined, new Set(builtinProviders.filter((provider) => provider.refreshModels !== undefined).map((provider) => provider.id)));
102
+ const runtime = new ModelRuntime(credentials, config, modelsPath, modelsStore, providers, process.env.PI_OFFLINE === undefined);
115
103
  runtime.configureRadiusProviders();
116
104
  runtime.rebuildProviders();
117
105
  const refreshFromNetwork = runtime.modelNetworkEnabled && options.allowModelNetwork === true;
@@ -198,7 +186,6 @@ export class ModelRuntime {
198
186
  this.updateModelSnapshot();
199
187
  }
200
188
  updateModelSnapshot() {
201
- this.remoteProjection = undefined;
202
189
  const all = [...this.models.getModels()];
203
190
  this.snapshot = {
204
191
  ...this.snapshot,
@@ -218,8 +205,6 @@ export class ModelRuntime {
218
205
  ]);
219
206
  if (seq !== this.availabilityRefreshSeq)
220
207
  return;
221
- this.localOperationAvailable = allAvailable;
222
- this.remoteProjection = undefined;
223
208
  const auth = new Map(checks);
224
209
  const configuredProviders = new Set(checks
225
210
  .filter((entry) => entry[1] !== undefined)
@@ -263,11 +248,6 @@ export class ModelRuntime {
263
248
  signal.throwIfAborted();
264
249
  if (this.providerAvailabilitySeq.get(providerId) !== providerSeq)
265
250
  return;
266
- this.localOperationAvailable = [
267
- ...this.localOperationAvailable.filter((model) => model.provider !== providerId),
268
- ...allAvailable,
269
- ];
270
- this.remoteProjection = undefined;
271
251
  const available = allAvailable.filter((model) => isModelType(model, "chat"));
272
252
  const configuredProviders = new Set(this.snapshot.configuredProviders);
273
253
  const storedProviders = new Set(this.snapshot.storedProviders);
@@ -310,165 +290,60 @@ export class ModelRuntime {
310
290
  }
311
291
  getProviders() {
312
292
  if (this.piServerMode) {
313
- const ids = new Set([
314
- ...this.models.getProviders().map((provider) => provider.id),
315
- ...this.getAllModels().map((model) => model.provider),
316
- ]);
317
- return [...ids].flatMap((id) => this.getProvider(id) ?? []);
293
+ return this.models.getProviders().flatMap((provider) => this.getProvider(provider.id) ?? []);
318
294
  }
319
295
  return this.models.getProviders();
320
296
  }
321
297
  getProvider(providerId) {
322
298
  const local = this.models.getProvider(providerId);
323
- if (!this.piServerMode)
299
+ if (!this.piServerMode || !local)
324
300
  return local;
325
- const catalog = this.getAllModels(providerId);
326
- if (!local && catalog.length === 0)
327
- return undefined;
328
- const capabilities = this.remoteProviderCapabilities.find((provider) => provider.id === providerId);
329
301
  return {
330
302
  ...local,
331
- id: providerId,
332
- name: local?.name ?? providerId,
333
- auth: local?.auth ?? {},
334
303
  getModels: () => this.getModels(providerId),
335
304
  getAllModels: () => this.getAllModels(providerId),
336
305
  stream: (model, context, options) => this.stream(model, context, options),
337
306
  streamSimple: (model, context, options) => this.streamSimple(model, context, options),
338
- generateImages: local?.generateImages || catalog.some((model) => isModelType(model, "image"))
307
+ generateImages: local.generateImages
339
308
  ? (model, context, options) => this.generateImages(model, context, options)
340
309
  : undefined,
341
- classify: local?.classify || catalog.some((model) => isModelType(model, "classifier"))
342
- ? (model, context, options) => this.classify(model, context, options)
343
- : undefined,
344
- fetchDeferred: local?.fetchDeferred || capabilities?.fetchDeferred
310
+ classify: local.classify ? (model, context, options) => this.classify(model, context, options) : undefined,
311
+ fetchDeferred: local.fetchDeferred
345
312
  ? (model, handle, options) => this.streamDeferred(model, handle, options)
346
313
  : undefined,
347
- cancelDeferred: local?.cancelDeferred || capabilities?.cancelDeferred
314
+ cancelDeferred: local.cancelDeferred
348
315
  ? (model, handle, options) => this.cancelDeferred(model, handle, options)
349
316
  : undefined,
350
317
  };
351
318
  }
352
319
  getModels(providerId) {
353
- if (this.piServerMode)
354
- return this.getModelsOfType("chat", providerId);
355
320
  return this.models.getModels(providerId);
356
321
  }
357
322
  getModel(providerId, modelId) {
358
- if (this.piServerMode)
359
- return this.getModelOfType("chat", providerId, modelId);
360
323
  return this.models.getModel(providerId, modelId);
361
324
  }
362
325
  getModelsOfType(type, providerId) {
363
- if (this.piServerMode)
364
- return this.getRemoteModels(false, providerId).filter((model) => isModelType(model, type));
365
326
  return this.models.getModelsOfType(type, providerId);
366
327
  }
367
328
  getModelOfType(type, providerId, modelId) {
368
- if (this.piServerMode)
369
- return this.getModelsOfType(type, providerId).find((model) => model.id === modelId);
370
329
  return this.models.getModelOfType(type, providerId, modelId);
371
330
  }
372
331
  getAllModels(providerId) {
373
- if (this.piServerMode)
374
- return this.getRemoteModels(false, providerId);
375
332
  return this.models.getAllModels(providerId);
376
333
  }
377
334
  async getAvailableOfType(type, providerId, options) {
378
- if (this.piServerMode) {
379
- options?.signal?.throwIfAborted();
380
- await raceWithAbortSignal(this.registrationRefresh, options?.signal);
381
- return this.getRemoteModels(true, providerId).filter((model) => isModelType(model, type));
382
- }
335
+ await raceWithAbortSignal(this.registrationRefresh, options?.signal);
383
336
  return this.models.getAvailableOfType(type, providerId, options);
384
337
  }
385
338
  async getAllAvailable(providerId, options) {
386
- if (this.piServerMode) {
387
- options?.signal?.throwIfAborted();
388
- await raceWithAbortSignal(this.registrationRefresh, options?.signal);
389
- return this.getRemoteModels(true, providerId);
390
- }
339
+ await raceWithAbortSignal(this.registrationRefresh, options?.signal);
391
340
  return this.models.getAllAvailable(providerId, options);
392
341
  }
393
- getRemoteModels(available, providerId) {
394
- if (this.remoteProjection) {
395
- const models = available ? this.remoteProjection.available : this.remoteProjection.models;
396
- return providerId === undefined ? models : models.filter((model) => model.provider === providerId);
397
- }
398
- const catalog = new Map(this.remoteCatalog.models.map((model) => [`${model.type ?? "chat"}\0${model.provider}\0${model.id}`, model]));
399
- for (const model of this.localOperationAvailable) {
400
- const key = `${model.type ?? "chat"}\0${model.provider}\0${model.id}`;
401
- if (!catalog.has(key))
402
- catalog.set(key, model);
403
- }
404
- const localAvailableKeys = new Set(this.localOperationAvailable.map((model) => `${model.type ?? "chat"}\0${model.provider}\0${model.id}`));
405
- for (const id of this.nativeCatalogProviders) {
406
- for (const model of this.models.getAllModels(id)) {
407
- const key = `${model.type ?? "chat"}\0${model.provider}\0${model.id}`;
408
- if (!catalog.has(key) || localAvailableKeys.has(key))
409
- catalog.set(key, model);
410
- }
411
- }
412
- const availableKeys = new Set(this.remoteCatalog.available.map((model) => `${model.type ?? "chat"}\0${model.provider}\0${model.id}`));
413
- for (const model of this.localOperationAvailable) {
414
- availableKeys.add(`${model.type ?? "chat"}\0${model.provider}\0${model.id}`);
415
- }
416
- const overlayProviders = new Set([...this.config.getProviderIds(), ...this.getRegisteredProviderIds()]);
417
- for (const id of overlayProviders) {
418
- const native = this.nativeExtensionProviders.get(id);
419
- const base = native ?? this.builtins.get(id);
420
- const baseModels = native
421
- ? (native.getAllModels?.() ?? native.getModels())
422
- : [...catalog.values()].filter((model) => model.provider === id);
423
- const provider = composeModelProvider(id, base && {
424
- ...base,
425
- getModels: () => baseModels.filter((model) => isModelType(model, "chat")),
426
- getAllModels: () => baseModels,
427
- }, this.config, this.extensionProviders.get(id));
428
- for (const [key, model] of catalog)
429
- if (model.provider === id)
430
- catalog.delete(key);
431
- for (const model of provider.getAllModels?.() ?? provider.getModels()) {
432
- catalog.set(`${model.type ?? "chat"}\0${model.provider}\0${model.id}`, model);
433
- }
434
- }
435
- const models = [...catalog.values()];
436
- const availableProviders = new Set(models
437
- .filter((model) => availableKeys.has(`${model.type ?? "chat"}\0${model.provider}\0${model.id}`))
438
- .map((model) => model.provider));
439
- const virtualModels = [...this.virtualModels.values()].flatMap((models) => [...models.values()].map((entry) => entry.model));
440
- const virtualIds = new Set(virtualModels.map((model) => `${model.provider}\0${model.id}`));
441
- const physical = models.filter((model) => !isModelType(model, "chat") || !virtualIds.has(`${model.provider}\0${model.id}`));
442
- const eligibleVirtual = virtualModels.filter((model) => !models.some((entry) => entry.provider === model.provider) || availableProviders.has(model.provider));
443
- this.remoteProjection = {
444
- models: [...physical, ...virtualModels],
445
- available: [
446
- ...physical.filter((model) => availableKeys.has(`${model.type ?? "chat"}\0${model.provider}\0${model.id}`)),
447
- ...eligibleVirtual,
448
- ],
449
- };
450
- return this.getRemoteModels(available, providerId);
451
- }
452
- async refreshRemoteCatalog(signal) {
453
- const seq = ++this.remoteCatalogRefreshSeq;
454
- this.remoteCatalogRequested = true;
455
- const timeoutSignal = AbortSignal.timeout(15_000);
456
- const catalog = await fetchPiServerModels({
457
- signal: signal ? AbortSignal.any([signal, timeoutSignal]) : timeoutSignal,
458
- });
459
- if (seq !== this.remoteCatalogRefreshSeq)
460
- return;
461
- this.remoteCatalog = catalog;
462
- this.remoteProviderCapabilities = catalog.providers;
463
- this.remoteCatalogError = undefined;
464
- this.remoteProjection = undefined;
465
- }
466
342
  async checkAuth(providerId, options) {
467
343
  return this.models.checkAuth(providerId, options);
468
344
  }
469
345
  async getAvailable(providerId, options) {
470
- if (this.piServerMode)
471
- return this.getAvailableOfType("chat", providerId, options);
346
+ await raceWithAbortSignal(this.registrationRefresh, options?.signal);
472
347
  if (providerId) {
473
348
  const errorSeq = ++this.availabilityErrorSeq;
474
349
  try {
@@ -488,8 +363,6 @@ export class ModelRuntime {
488
363
  return this.snapshot.available;
489
364
  }
490
365
  getAvailableSnapshot() {
491
- if (this.piServerMode)
492
- return this.getRemoteModels(true).filter((model) => isModelType(model, "chat"));
493
366
  return this.snapshot.available;
494
367
  }
495
368
  getError() {
@@ -502,8 +375,6 @@ export class ModelRuntime {
502
375
  }
503
376
  if (this.availabilityError)
504
377
  errors.push(`Availability refresh: ${this.availabilityError}`);
505
- if (this.remoteCatalogError)
506
- errors.push(`pi-server model catalog: ${this.remoteCatalogError}`);
507
378
  for (const [providerId, error] of this.registrationRefreshErrors)
508
379
  errors.push(`Provider "${providerId}" refresh: ${error}`);
509
380
  return errors.length > 0 ? errors.join("\n\n") : undefined;
@@ -528,8 +399,6 @@ export class ModelRuntime {
528
399
  return this.isUsingOAuth(providerId) && this.models.getProvider(providerId)?.auth.oauth?.isSubscription === true;
529
400
  }
530
401
  hasConfiguredAuth(providerId) {
531
- if (this.piServerMode)
532
- return this.getRemoteModels(true, providerId).length > 0;
533
402
  return this.snapshot.configuredProviders.has(providerId);
534
403
  }
535
404
  async getAuth(providerOrModel, overrides = {}) {
@@ -826,51 +695,6 @@ export class ModelRuntime {
826
695
  return { aborted: true, errors: new Map() };
827
696
  throw error;
828
697
  }
829
- if (this.piServerMode) {
830
- this.config = await ModelConfig.load(this.modelsPath);
831
- this.configureRadiusProviders();
832
- this.rebuildProviders();
833
- let result = await this.models.refresh({ ...options, allowNetwork: false });
834
- const nativeProviders = [
835
- ...new Set([...this.nativeCatalogProviders, ...this.nativeExtensionProviders.keys()]),
836
- ].filter((id) => options.providers === undefined || options.providers.includes(id));
837
- if ((options.allowNetwork ?? this.modelNetworkEnabled) && nativeProviders.length > 0) {
838
- const nativeResult = await this.models.refresh({
839
- ...options,
840
- providers: nativeProviders,
841
- allowNetwork: true,
842
- });
843
- result = {
844
- aborted: result.aborted || nativeResult.aborted,
845
- errors: new Map([...result.errors, ...nativeResult.errors]),
846
- };
847
- }
848
- const errors = new Map(result.errors);
849
- try {
850
- await this.queueAvailabilityRefresh(options.signal);
851
- }
852
- catch (error) {
853
- if (!options.signal?.aborted)
854
- errors.set("availability", error instanceof Error ? error : new Error(String(error)));
855
- }
856
- try {
857
- if (!options.signal?.aborted && (!this.remoteCatalogRequested || options.allowNetwork !== false)) {
858
- await this.refreshRemoteCatalog(options.signal);
859
- }
860
- }
861
- catch (error) {
862
- if (!options.signal?.aborted) {
863
- const remoteError = error instanceof Error ? error : new Error(String(error));
864
- this.remoteCatalogError = remoteError.message;
865
- errors.set("pi-server", remoteError);
866
- }
867
- }
868
- for (const id of this.registrationRefreshErrors.keys()) {
869
- if (!errors.has(id) && (options.providers === undefined || options.providers.includes(id)))
870
- this.registrationRefreshErrors.delete(id);
871
- }
872
- return { aborted: result.aborted || options.signal?.aborted === true, errors };
873
- }
874
698
  this.config = await ModelConfig.load(this.modelsPath);
875
699
  this.configureRadiusProviders();
876
700
  if (options.providers) {
@@ -908,6 +732,10 @@ export class ModelRuntime {
908
732
  // Availability errors are recorded by the latest pass; refreshed models remain usable.
909
733
  }
910
734
  }
735
+ for (const id of this.registrationRefreshErrors.keys()) {
736
+ if (!errors.has(id) && (options.providers === undefined || options.providers.includes(id)))
737
+ this.registrationRefreshErrors.delete(id);
738
+ }
911
739
  return { aborted: result.aborted || (options.signal?.aborted ?? false), errors };
912
740
  }
913
741
  queueRegistrationRefresh(providerId) {