@xynogen/pix-models 0.1.16 → 0.1.17

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xynogen/pix-models",
3
- "version": "0.1.16",
3
+ "version": "0.1.17",
4
4
  "description": "Pi extension — enhanced /models picker with BenchLM ranks",
5
5
  "type": "module",
6
6
  "main": "src/index.ts",
@@ -1,5 +1,13 @@
1
1
  import { describe, expect, it } from "bun:test";
2
- import { benchStars, fmtCost, fmtCtx, sortModels } from "./models.ts";
2
+ import {
3
+ benchStars,
4
+ filterModelItems,
5
+ fmtCost,
6
+ fmtCtx,
7
+ type ModelSearchLookup,
8
+ normalizeModelText,
9
+ sortModels,
10
+ } from "./models.ts";
3
11
 
4
12
  describe("fmtCtx", () => {
5
13
  it("formats 0 as 0", () => expect(fmtCtx(0)).toBe("0"));
@@ -120,3 +128,124 @@ describe("sortModels", () => {
120
128
  expect(sorted.map((m) => m.name)).toEqual(["Alpha", "Delta", "Beta", "Gamma"]);
121
129
  });
122
130
  });
131
+
132
+ // ─── normalizeModelText ───────────────────────────────────────────────────────
133
+
134
+ describe("normalizeModelText", () => {
135
+ it("lowercases", () => {
136
+ expect(normalizeModelText("GLM-5.2")).toBe("glm52");
137
+ });
138
+
139
+ it("strips hyphens", () => {
140
+ expect(normalizeModelText("claude-opus-4-8")).toBe("claudeopus48");
141
+ });
142
+
143
+ it("strips dots", () => {
144
+ expect(normalizeModelText("qwen3.7-max")).toBe("qwen37max");
145
+ });
146
+
147
+ it("strips all non-alphanumeric", () => {
148
+ expect(normalizeModelText("a!b@c#d$e%f^g&h")).toBe("abcdefgh");
149
+ });
150
+
151
+ it("handles plain alphanumeric", () => {
152
+ expect(normalizeModelText("abc123")).toBe("abc123");
153
+ });
154
+
155
+ it("handles empty string", () => {
156
+ expect(normalizeModelText("")).toBe("");
157
+ });
158
+ });
159
+
160
+ // ─── filterModelItems ─────────────────────────────────────────────────────────
161
+
162
+ describe("filterModelItems", () => {
163
+ // Fixture: models with ids matching the acceptance criteria.
164
+ // Ranks are assigned so some overlap with digit-queries.
165
+ const mk = (id: string, rank?: number) => ({
166
+ value: `p/${id}`,
167
+ _rank: rank,
168
+ });
169
+
170
+ type Fixture = ReturnType<typeof mk>;
171
+
172
+ /** Build lookup maps exactly like production: haystack = `${id} ${name ?? ""}`. */
173
+ function buildLookup(items: Fixture[]): ModelSearchLookup {
174
+ const rankByValue = new Map<string, number>();
175
+ const searchTextByValue = new Map<string, string>();
176
+ const normalizedByValue = new Map<string, string>();
177
+ for (const it of items) {
178
+ // value is "p/glm-5.2" — extract the id part.
179
+ const id = it.value.split("/")[1] ?? "";
180
+ if (it._rank != null) rankByValue.set(it.value, it._rank);
181
+ const text = `${id} ${""}`; // no name in test fixture
182
+ searchTextByValue.set(it.value, text);
183
+ normalizedByValue.set(it.value, normalizeModelText(text));
184
+ }
185
+ return { rankByValue, searchTextByValue, normalizedByValue };
186
+ }
187
+
188
+ // Full fixture with ranks: glm-5.2→3, minimax-m3→8, claude-opus-4-8→2.
189
+ const allItems: Fixture[] = [
190
+ mk("glm-5.2", 3),
191
+ mk("glm-5.1"),
192
+ mk("minimax-m3", 8),
193
+ mk("claude-opus-4-8", 2),
194
+ mk("claude-sonnet-4-6"),
195
+ mk("qwen3.7-max"),
196
+ ];
197
+ const allLookup = buildLookup(allItems);
198
+
199
+ function values(result: Fixture[]): string[] {
200
+ return result.map((it) => it.value);
201
+ }
202
+
203
+ it("empty query returns all items in original order", () => {
204
+ const result = filterModelItems(allItems, "", allLookup);
205
+ expect(values(result)).toEqual(allItems.map((it) => it.value));
206
+ });
207
+
208
+ it("query '52' includes glm-5.2 via normalized substring", () => {
209
+ const result = filterModelItems(allItems, "52", allLookup);
210
+ expect(values(result)).toContain("p/glm-5.2");
211
+ });
212
+
213
+ it("query 'm3' includes minimax-m3 first (substring priority over fuzzy noise)", () => {
214
+ const result = filterModelItems(allItems, "m3", allLookup);
215
+ expect(values(result)[0]).toBe("p/minimax-m3");
216
+ });
217
+
218
+ it("query '48' includes claude-opus-4-8", () => {
219
+ const result = filterModelItems(allItems, "48", allLookup);
220
+ expect(values(result)).toContain("p/claude-opus-4-8");
221
+ });
222
+
223
+ it("exact id query 'glm-5.2' still matches glm-5.2", () => {
224
+ const result = filterModelItems(allItems, "glm-5.2", allLookup);
225
+ expect(values(result)).toContain("p/glm-5.2");
226
+ });
227
+
228
+ it("digit query matching a rank returns that ranked item first", () => {
229
+ // glm-5.2 has rank 3 — query "3" should put it first.
230
+ const result = filterModelItems(allItems, "3", allLookup);
231
+ expect(values(result)[0]).toBe("p/glm-5.2");
232
+ });
233
+
234
+ it("dedupe: item matching both rank and substring appears once", () => {
235
+ // claude-opus-4-8 has rank 2; query "2" matches by rank AND
236
+ // normalized substring ("2" ⊂ "claudeopus48"). Should appear once.
237
+ const result = filterModelItems(allItems, "2", allLookup);
238
+ const occurrences = values(result).filter((v) => v === "p/claude-opus-4-8");
239
+ expect(occurrences.length).toBe(1);
240
+ });
241
+
242
+ it("digit query '8' (rank 8) returns minimax-m3 as rank match first", () => {
243
+ const result = filterModelItems(allItems, "8", allLookup);
244
+ expect(values(result)[0]).toBe("p/minimax-m3");
245
+ });
246
+
247
+ it("whitespace query is treated as empty", () => {
248
+ const result = filterModelItems(allItems, " ", allLookup);
249
+ expect(values(result)).toEqual(allItems.map((it) => it.value));
250
+ });
251
+ });
package/src/models.ts CHANGED
@@ -87,6 +87,68 @@ export function sortModels<T extends SortableModel>(models: T[]): T[] {
87
87
  });
88
88
  }
89
89
 
90
+ /** Lowercase and strip all non-alphanumerics: "glm-5.2" → "glm52". */
91
+ export function normalizeModelText(s: string): string {
92
+ return s.toLowerCase().replace(/[^a-z0-9]/g, "");
93
+ }
94
+
95
+ export type ModelSearchLookup = {
96
+ /** benchlm local rank per item value (ranked models only). */
97
+ rankByValue: Map<string, number>;
98
+ /** Clean haystack per value: `${id} ${name ?? ""}` (no ANSI, no rank cell). */
99
+ searchTextByValue: Map<string, string>;
100
+ /** normalizeModelText(haystack) per value — for family+version substring matches. */
101
+ normalizedByValue: Map<string, string>;
102
+ };
103
+
104
+ /**
105
+ * Filter+order picker items for a search query.
106
+ * - "" → all items unchanged.
107
+ * - digits-only → items whose benchlm rank equals the number, followed by
108
+ * normalized-substring matches ("52" ⊂ "glm52") rank-sorted.
109
+ * - otherwise → normalized-substring matches first (rank-sorted), then
110
+ * fuzzy matches over the clean haystack (rank-sorted, stable), deduped.
111
+ */
112
+ export function filterModelItems<T extends { value: string }>(
113
+ items: T[],
114
+ query: string,
115
+ lookup: ModelSearchLookup,
116
+ ): T[] {
117
+ const q = query.trim();
118
+ if (q.length === 0) return items;
119
+
120
+ const rankSort = (arr: T[]): T[] => {
121
+ return arr
122
+ .map((it, i) => ({ it, i }))
123
+ .sort((a, b) => {
124
+ const ra = lookup.rankByValue.get(a.it.value) ?? Infinity;
125
+ const rb = lookup.rankByValue.get(b.it.value) ?? Infinity;
126
+ if (ra !== rb) return ra - rb;
127
+ return a.i - b.i;
128
+ })
129
+ .map(({ it }) => it);
130
+ };
131
+
132
+ const nq = normalizeModelText(q);
133
+ const subMatches =
134
+ nq.length > 0
135
+ ? items.filter((it) => (lookup.normalizedByValue.get(it.value) ?? "").includes(nq))
136
+ : [];
137
+
138
+ if (/^\d+$/.test(q)) {
139
+ const wanted = Number(q);
140
+ const rankMatches = items.filter((it) => lookup.rankByValue.get(it.value) === wanted);
141
+ const rankValues = new Set(rankMatches.map((it) => it.value));
142
+ const extraSub = rankSort(subMatches).filter((it) => !rankValues.has(it.value));
143
+ return [...rankMatches, ...extraSub];
144
+ }
145
+
146
+ const fuzzy = fuzzyFilter(items, q, (it) => lookup.searchTextByValue.get(it.value) ?? "");
147
+ const subValues = new Set(subMatches.map((it) => it.value));
148
+ const extraFuzzy = rankSort(fuzzy).filter((it) => !subValues.has(it.value));
149
+ return [...rankSort(subMatches), ...extraFuzzy];
150
+ }
151
+
90
152
  async function showEnhancedPicker(pi: ExtensionAPI, ctx: ExtensionContext): Promise<void> {
91
153
  // Mirror the built-in /model selector, which calls refresh() then awaits
92
154
  // getAvailable() (see model-selector.js). Without refresh(), this extension
@@ -184,6 +246,17 @@ async function showEnhancedPicker(pi: ExtensionAPI, ctx: ExtensionContext): Prom
184
246
  if (localRank) rankByValue.set(`${m.provider}/${m.id}`, localRank);
185
247
  }
186
248
 
249
+ // Clean search haystacks — labels are ANSI-laden and carry the rank cell,
250
+ // so matching runs against raw id+name instead (see filterModelItems).
251
+ const searchTextByValue = new Map<string, string>();
252
+ const normalizedByValue = new Map<string, string>();
253
+ for (const { m } of dedupedRows) {
254
+ const value = `${m.provider}/${m.id}`;
255
+ const text = `${m.id} ${m.name ?? ""}`;
256
+ searchTextByValue.set(value, text);
257
+ normalizedByValue.set(value, normalizeModelText(text));
258
+ }
259
+
187
260
  const items: SelectItem[] = dedupedRows.map(({ m, dev, bench, localRank }) => {
188
261
  const isCurrent = current && m.provider === current.provider && m.id === current.id;
189
262
 
@@ -282,27 +355,11 @@ async function showEnhancedPicker(pi: ExtensionAPI, ctx: ExtensionContext): Prom
282
355
  selectedIndex: number;
283
356
  invalidate(): void;
284
357
  };
285
- const q = query.trim();
286
- let next: SelectItem[];
287
- if (q.length === 0) {
288
- next = internal.items;
289
- } else if (/^\d+$/.test(q)) {
290
- // Pure number → match by benchlm rank, not name.
291
- const wanted = Number(q);
292
- next = internal.items.filter((it) => rankByValue.get(it.value) === wanted);
293
- } else {
294
- next = fuzzyFilter(internal.items, q, (it) => `${it.label} ${it.description ?? ""}`);
295
- // Stable sort: ranked models (by rank asc) before unranked.
296
- next = next
297
- .map((it, i) => ({ it, i }))
298
- .sort((a, b) => {
299
- const ra = rankByValue.get(a.it.value) ?? Infinity;
300
- const rb = rankByValue.get(b.it.value) ?? Infinity;
301
- if (ra !== rb) return ra - rb;
302
- return a.i - b.i;
303
- })
304
- .map(({ it }) => it);
305
- }
358
+ const next = filterModelItems(internal.items, query, {
359
+ rankByValue,
360
+ searchTextByValue,
361
+ normalizedByValue,
362
+ });
306
363
  internal.filteredItems = next;
307
364
  internal.selectedIndex = 0;
308
365
  internal.invalidate();