@router-for-me/pi-cliproxyapi-provider 1.4.16 → 1.4.18

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/extensions/lib.ts CHANGED
@@ -71,6 +71,9 @@ export interface CodexClientModel {
71
71
  description?: string;
72
72
  context_window?: number;
73
73
  max_context_window?: number;
74
+ max_tokens?: number;
75
+ max_output_tokens?: number;
76
+ max_completion_tokens?: number;
74
77
  input_modalities?: string[];
75
78
  supported_reasoning_levels?: CodexReasoningLevel[] | string[];
76
79
  default_service_tier?: string | null;
@@ -110,6 +113,9 @@ export interface PiProviderModel {
110
113
  contextWindow: number;
111
114
  maxTokens: number;
112
115
  thinkingLevelMap?: ThinkingLevelMap;
116
+ stale?: boolean;
117
+ lastSeenAt?: number;
118
+ staleSince?: number;
113
119
  }
114
120
 
115
121
  export interface MappedModels {
@@ -253,6 +259,52 @@ export function loadConfigFile(agentDir: string): CliproxyConfigFile {
253
259
  }
254
260
  }
255
261
 
262
+ export const SETTINGS_FILE_NAME = "settings.json";
263
+
264
+ export interface ConfiguredDefaultModelSettings {
265
+ defaultProvider?: string;
266
+ defaultModel?: string;
267
+ }
268
+
269
+ export function loadConfiguredDefaultSettings(agentDir: string): ConfiguredDefaultModelSettings {
270
+ const settingsPath = join(agentDir, SETTINGS_FILE_NAME);
271
+ try {
272
+ const parsed = JSON.parse(readFileSync(settingsPath, "utf8")) as Record<string, unknown>;
273
+ return {
274
+ defaultProvider:
275
+ typeof parsed.defaultProvider === "string" && parsed.defaultProvider.trim()
276
+ ? parsed.defaultProvider.trim()
277
+ : undefined,
278
+ defaultModel:
279
+ typeof parsed.defaultModel === "string" && parsed.defaultModel.trim()
280
+ ? parsed.defaultModel.trim()
281
+ : undefined,
282
+ };
283
+ } catch {
284
+ return {};
285
+ }
286
+ }
287
+
288
+ export function isModelReferencedAsDefault(
289
+ configured: ConfiguredDefaultModelSettings | string | undefined,
290
+ modelId: string,
291
+ providerId: string,
292
+ ): boolean {
293
+ if (!configured) return false;
294
+ const settings: ConfiguredDefaultModelSettings =
295
+ typeof configured === "string" ? { defaultModel: configured } : configured;
296
+
297
+ if (!settings.defaultModel) return false;
298
+ const trimmed = settings.defaultModel.trim();
299
+ if (trimmed === `${providerId}/${modelId}` || trimmed === `${providerId}:${modelId}`) {
300
+ return true;
301
+ }
302
+ if (trimmed === modelId) {
303
+ return settings.defaultProvider === undefined || settings.defaultProvider === providerId;
304
+ }
305
+ return false;
306
+ }
307
+
256
308
  export function saveConfigFile(agentDir: string, config: CliproxyConfigFile): void {
257
309
  const configPath = join(agentDir, CONFIG_FILE_NAME);
258
310
  mkdirSync(dirname(configPath), { recursive: true });
@@ -487,6 +539,22 @@ export function toPiModel(
487
539
  ? matchModelCost(id, costCatalog, fastMode && supportsFastServiceTier(model))
488
540
  : { ...ZERO_COST };
489
541
 
542
+ const maxTokens =
543
+ (typeof model.max_tokens === "number" && Number.isFinite(model.max_tokens) && model.max_tokens > 0
544
+ ? model.max_tokens
545
+ : undefined) ??
546
+ (typeof model.max_output_tokens === "number" &&
547
+ Number.isFinite(model.max_output_tokens) &&
548
+ model.max_output_tokens > 0
549
+ ? model.max_output_tokens
550
+ : undefined) ??
551
+ (typeof model.max_completion_tokens === "number" &&
552
+ Number.isFinite(model.max_completion_tokens) &&
553
+ model.max_completion_tokens > 0
554
+ ? model.max_completion_tokens
555
+ : undefined) ??
556
+ DEFAULT_MAX_TOKENS;
557
+
490
558
  return {
491
559
  id,
492
560
  name: (model.display_name ?? model.name ?? id).trim() || id,
@@ -494,7 +562,7 @@ export function toPiModel(
494
562
  input: buildInputModalities(model),
495
563
  cost,
496
564
  contextWindow,
497
- maxTokens: DEFAULT_MAX_TOKENS,
565
+ maxTokens,
498
566
  thinkingLevelMap: buildThinkingLevelMap(efforts),
499
567
  };
500
568
  }
@@ -884,6 +952,95 @@ export async function loadMappedModels(
884
952
  };
885
953
  }
886
954
 
955
+ export const DEFAULT_MODEL_STALE_TTL_MS = 7 * 24 * 60 * 60 * 1000;
956
+
957
+ export interface MergeModelsResult {
958
+ merged: MappedModels;
959
+ retainedStaleModels: PiProviderModel[];
960
+ droppedModelIds: string[];
961
+ }
962
+
963
+ /**
964
+ * Merge newly fetched models with the existing disk cache to prevent transient
965
+ * upstream pruning (such as HTTP 503 / 429 capacity blips) from wiping out valid models.
966
+ */
967
+ export function mergeModelsWithExistingCache(
968
+ existingCache: ModelsCacheFile | null,
969
+ fresh: MappedModels,
970
+ now = Date.now(),
971
+ staleTtlMs = DEFAULT_MODEL_STALE_TTL_MS,
972
+ ): MergeModelsResult {
973
+ if (!existingCache || !Array.isArray(existingCache.models) || existingCache.models.length === 0) {
974
+ const modelsWithTimestamp = fresh.models.map((model) => ({
975
+ ...model,
976
+ lastSeenAt: now,
977
+ stale: false,
978
+ }));
979
+ return {
980
+ merged: {
981
+ ...fresh,
982
+ models: modelsWithTimestamp,
983
+ },
984
+ retainedStaleModels: [],
985
+ droppedModelIds: [],
986
+ };
987
+ }
988
+
989
+ const freshModelMap = new Map<string, PiProviderModel>();
990
+ const freshFastSet = new Set(fresh.fastModelIds);
991
+
992
+ for (const model of fresh.models) {
993
+ const updated: PiProviderModel = {
994
+ ...model,
995
+ lastSeenAt: now,
996
+ stale: false,
997
+ };
998
+ delete updated.staleSince;
999
+ freshModelMap.set(model.id, updated);
1000
+ }
1001
+
1002
+ const retainedStaleModels: PiProviderModel[] = [];
1003
+ const droppedModelIds: string[] = [];
1004
+
1005
+ for (const prevModel of existingCache.models) {
1006
+ if (freshModelMap.has(prevModel.id)) {
1007
+ continue;
1008
+ }
1009
+ const lastSeen = prevModel.lastSeenAt ?? existingCache.fetchedAt ?? now;
1010
+ const isFirstMissing = prevModel.stale !== true;
1011
+ const staleSince = isFirstMissing ? now : (prevModel.staleSince ?? lastSeen);
1012
+
1013
+ // Retain models from the moment they first go missing until they have been
1014
+ // missing across the full stale retention window.
1015
+ if (now - staleSince < staleTtlMs) {
1016
+ const retained: PiProviderModel = {
1017
+ ...prevModel,
1018
+ lastSeenAt: lastSeen,
1019
+ staleSince,
1020
+ stale: true,
1021
+ };
1022
+ retainedStaleModels.push(retained);
1023
+ if (existingCache.fastModelIds?.includes(prevModel.id) && !freshFastSet.has(prevModel.id)) {
1024
+ freshFastSet.add(prevModel.id);
1025
+ }
1026
+ } else {
1027
+ droppedModelIds.push(prevModel.id);
1028
+ }
1029
+ }
1030
+
1031
+ const mergedModels = [...freshModelMap.values(), ...retainedStaleModels];
1032
+
1033
+ return {
1034
+ merged: {
1035
+ ...fresh,
1036
+ models: mergedModels,
1037
+ fastModelIds: Array.from(freshFastSet),
1038
+ },
1039
+ retainedStaleModels,
1040
+ droppedModelIds,
1041
+ };
1042
+ }
1043
+
887
1044
  /**
888
1045
  * Load mapped models from the matching cache, or fetch remotely and update the cache.
889
1046
  * A forced refresh always bypasses the cache.
@@ -897,21 +1054,23 @@ export async function resolveMappedModels(
897
1054
  fastMode?: boolean;
898
1055
  signal?: AbortSignal;
899
1056
  shouldCommit?: () => boolean;
1057
+ staleTtlMs?: number;
900
1058
  } = {},
901
1059
  ): Promise<ResolvedModelsResult> {
902
1060
  const cacheMatchesFastMode = (cache: ModelsCacheFile): boolean =>
903
1061
  options.fastMode === undefined || (cache.fastMode ?? false) === options.fastMode;
904
1062
 
1063
+ const existingCache = loadModelsCache(agentDir, baseUrlInput);
905
1064
  if (!options.forceRefresh) {
906
- const cache = loadModelsCache(agentDir, baseUrlInput);
907
- if (cache && cacheMatchesFastMode(cache)) {
908
- return { loaded: cache, fromCache: true };
1065
+ if (existingCache && cacheMatchesFastMode(existingCache)) {
1066
+ return { loaded: existingCache, fromCache: true };
909
1067
  }
910
1068
  }
911
1069
 
912
- const loaded = await loadMappedModels(baseUrlInput, apiKey, options.fastMode, agentDir, options.signal);
1070
+ const fresh = await loadMappedModels(baseUrlInput, apiKey, options.fastMode, agentDir, options.signal);
1071
+ const { merged } = mergeModelsWithExistingCache(existingCache, fresh, Date.now(), options.staleTtlMs);
913
1072
  if (!options.signal?.aborted && (options.shouldCommit?.() ?? true)) {
914
- saveModelsCache(agentDir, loaded);
1073
+ saveModelsCache(agentDir, merged);
915
1074
  }
916
- return { loaded, fromCache: false };
1075
+ return { loaded: merged, fromCache: false };
917
1076
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@router-for-me/pi-cliproxyapi-provider",
3
- "version": "1.4.16",
3
+ "version": "1.4.18",
4
4
  "description": "Pi provider extension that auto-registers CLIProxyAPI models, plus TUI elapsed/TPS status",
5
5
  "type": "module",
6
6
  "keywords": [