pi-model-sort 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.
Files changed (2) hide show
  1. package/model-sort.ts +43 -0
  2. package/package.json +1 -1
package/model-sort.ts CHANGED
@@ -136,6 +136,47 @@ function unpatchLoadModels(): void {
136
136
  origLoadModels = null;
137
137
  }
138
138
 
139
+ // ModelSelectorComponent filterModels patch — re-applies last-used sort after
140
+ // fuzzyFilter re-orders results by match quality. Without this, typing in the
141
+ // /model picker search box discards the last-used order.
142
+
143
+ let origFilterModels: ((query: string) => void) | null = null;
144
+
145
+ function patchFilterModels(getLastUsed: () => Record<string, number>): void {
146
+ if (origFilterModels !== null) return;
147
+
148
+ const proto = ModelSelectorComponent.prototype as unknown as Record<string, unknown>;
149
+ origFilterModels = proto.filterModels as (query: string) => void;
150
+
151
+ proto.filterModels = function (this: Record<string, unknown>, query: string) {
152
+ origFilterModels!.call(this, query);
153
+
154
+ const filtered = this.filteredModels as Array<{ provider: string; id: string; model: unknown }> | undefined;
155
+ if (!filtered || filtered.length <= 1 || !query) return;
156
+
157
+ const lastUsed = getLastUsed();
158
+ this.filteredModels = sortByLastUsed(filtered, lastUsed, buildCurrentModelKey(this));
159
+
160
+ // Re-sync selectedIndex — fuzzyFilter may have moved the current model.
161
+ const currentKey = buildCurrentModelKey(this);
162
+ if (currentKey) {
163
+ const newFiltered = this.filteredModels as Array<{ provider: string; id: string }>;
164
+ const newIndex = newFiltered.findIndex(
165
+ (item) => buildModelKey(item.provider, item.id) === currentKey,
166
+ );
167
+ if (newIndex >= 0) {
168
+ this.selectedIndex = newIndex;
169
+ }
170
+ }
171
+ };
172
+ }
173
+
174
+ function unpatchFilterModels(): void {
175
+ if (origFilterModels === null) return;
176
+ (ModelSelectorComponent.prototype as unknown as Record<string, unknown>).filterModels = origFilterModels;
177
+ origFilterModels = null;
178
+ }
179
+
139
180
  // ModelRegistry getAvailable / getAll patch
140
181
 
141
182
  const REGISTRY_PATCH_KEY = "__model_sort_registry_patched";
@@ -251,6 +292,7 @@ export default function (pi: ExtensionAPI) {
251
292
  patchRegistry(ctx.modelRegistry as unknown as PatchedRegistry, () => lastUsed);
252
293
  patchSortModels(() => lastUsed);
253
294
  patchLoadModels(() => lastUsed);
295
+ patchFilterModels(() => lastUsed);
254
296
  patchCycleScopedModel(() => lastUsed);
255
297
 
256
298
  if (ctx.hasUI) {
@@ -280,6 +322,7 @@ export default function (pi: ExtensionAPI) {
280
322
  pi.on("session_shutdown", () => {
281
323
  unpatchSortModels();
282
324
  unpatchLoadModels();
325
+ unpatchFilterModels();
283
326
  unpatchCycleScopedModel();
284
327
  });
285
328
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-model-sort",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "Sort models in pi's /model selector by last usage — most recently used models appear first",
5
5
  "type": "module",
6
6
  "author": "Tom X Nguyen",