pi-model-sort 0.1.1 → 0.1.3
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/model-sort.ts +47 -0
- package/package.json +1 -1
package/model-sort.ts
CHANGED
|
@@ -136,6 +136,51 @@ 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
|
+
// Re-render — the original filterModels already called updateList() before
|
|
173
|
+
// we re-sorted, so the UI is stale until the next input event.
|
|
174
|
+
(this.updateList as () => void)();
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function unpatchFilterModels(): void {
|
|
179
|
+
if (origFilterModels === null) return;
|
|
180
|
+
(ModelSelectorComponent.prototype as unknown as Record<string, unknown>).filterModels = origFilterModels;
|
|
181
|
+
origFilterModels = null;
|
|
182
|
+
}
|
|
183
|
+
|
|
139
184
|
// ModelRegistry getAvailable / getAll patch
|
|
140
185
|
|
|
141
186
|
const REGISTRY_PATCH_KEY = "__model_sort_registry_patched";
|
|
@@ -251,6 +296,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
251
296
|
patchRegistry(ctx.modelRegistry as unknown as PatchedRegistry, () => lastUsed);
|
|
252
297
|
patchSortModels(() => lastUsed);
|
|
253
298
|
patchLoadModels(() => lastUsed);
|
|
299
|
+
patchFilterModels(() => lastUsed);
|
|
254
300
|
patchCycleScopedModel(() => lastUsed);
|
|
255
301
|
|
|
256
302
|
if (ctx.hasUI) {
|
|
@@ -280,6 +326,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
280
326
|
pi.on("session_shutdown", () => {
|
|
281
327
|
unpatchSortModels();
|
|
282
328
|
unpatchLoadModels();
|
|
329
|
+
unpatchFilterModels();
|
|
283
330
|
unpatchCycleScopedModel();
|
|
284
331
|
});
|
|
285
332
|
}
|