@hop-top/kit 0.4.0-alpha.1

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 (174) hide show
  1. package/CHANGELOG.md +86 -0
  2. package/LICENSE +25 -0
  3. package/README.md +189 -0
  4. package/dist/aim.d.ts +116 -0
  5. package/dist/aim.js +417 -0
  6. package/dist/alias.d.ts +73 -0
  7. package/dist/alias.js +207 -0
  8. package/dist/api.d.ts +62 -0
  9. package/dist/api.js +165 -0
  10. package/dist/auth.d.ts +26 -0
  11. package/dist/auth.js +41 -0
  12. package/dist/cli.d.ts +309 -0
  13. package/dist/cli.js +885 -0
  14. package/dist/config.d.ts +97 -0
  15. package/dist/config.js +79 -0
  16. package/dist/errcorrect.d.ts +37 -0
  17. package/dist/errcorrect.js +69 -0
  18. package/dist/llm.d.ts +188 -0
  19. package/dist/llm.js +356 -0
  20. package/dist/output.d.ts +213 -0
  21. package/dist/output.js +447 -0
  22. package/dist/progress.d.ts +30 -0
  23. package/dist/progress.js +56 -0
  24. package/dist/provenance.d.ts +24 -0
  25. package/dist/provenance.js +32 -0
  26. package/dist/routellm.d.ts +64 -0
  27. package/dist/routellm.js +258 -0
  28. package/dist/rpc.d.ts +25 -0
  29. package/dist/rpc.js +101 -0
  30. package/dist/safety.d.ts +24 -0
  31. package/dist/safety.js +44 -0
  32. package/dist/scope.d.ts +121 -0
  33. package/dist/scope.js +380 -0
  34. package/dist/sqlstore.d.ts +84 -0
  35. package/dist/sqlstore.js +85 -0
  36. package/dist/stream.d.ts +35 -0
  37. package/dist/stream.js +52 -0
  38. package/dist/tui/index.d.ts +300 -0
  39. package/dist/tui/index.js +332 -0
  40. package/dist/upgrade.d.ts +95 -0
  41. package/dist/upgrade.js +115 -0
  42. package/dist/xdg.d.ts +169 -0
  43. package/dist/xdg.js +129 -0
  44. package/eslint.config.mjs +17 -0
  45. package/package.json +133 -0
  46. package/src/README.md +3 -0
  47. package/src/aim.test.ts +331 -0
  48. package/src/aim.ts +388 -0
  49. package/src/alias.test.ts +423 -0
  50. package/src/alias.ts +256 -0
  51. package/src/api.test.ts +190 -0
  52. package/src/api.ts +214 -0
  53. package/src/auth.test.ts +24 -0
  54. package/src/auth.ts +37 -0
  55. package/src/bus.test.ts +178 -0
  56. package/src/bus.ts +114 -0
  57. package/src/cli.test.ts +690 -0
  58. package/src/cli.ts +741 -0
  59. package/src/completion-values.test.ts +202 -0
  60. package/src/completion-values.ts +173 -0
  61. package/src/completion.test.ts +212 -0
  62. package/src/completion.ts +158 -0
  63. package/src/compliance.test.ts +134 -0
  64. package/src/compliance.ts +669 -0
  65. package/src/config.test.ts +110 -0
  66. package/src/config.ts +164 -0
  67. package/src/errcorrect.test.ts +134 -0
  68. package/src/errcorrect.ts +84 -0
  69. package/src/flagregister.test.ts +103 -0
  70. package/src/flagregister.ts +78 -0
  71. package/src/gen/crud_connect.ts +76 -0
  72. package/src/gen/crud_pb.ts +269 -0
  73. package/src/hint.test.ts +315 -0
  74. package/src/hint.ts +178 -0
  75. package/src/llm.test.ts +612 -0
  76. package/src/llm.ts +577 -0
  77. package/src/log.test.ts +145 -0
  78. package/src/log.ts +155 -0
  79. package/src/output/builtins.ts +34 -0
  80. package/src/output/dispatch.ts +195 -0
  81. package/src/output/flags.ts +151 -0
  82. package/src/output/format_help.ts +80 -0
  83. package/src/output/formatter.ts +167 -0
  84. package/src/output/formatters/csv.ts +72 -0
  85. package/src/output/formatters/json.ts +44 -0
  86. package/src/output/formatters/table.ts +44 -0
  87. package/src/output/formatters/text.ts +109 -0
  88. package/src/output/formatters/yaml.ts +47 -0
  89. package/src/output/projection.ts +126 -0
  90. package/src/output/registry.ts +88 -0
  91. package/src/output/template.ts +63 -0
  92. package/src/output.test.ts +117 -0
  93. package/src/output.ts +90 -0
  94. package/src/progress.test.ts +93 -0
  95. package/src/progress.ts +58 -0
  96. package/src/provenance.test.ts +59 -0
  97. package/src/provenance.ts +26 -0
  98. package/src/routellm.test.ts +179 -0
  99. package/src/routellm.ts +365 -0
  100. package/src/router/bert.test.ts +180 -0
  101. package/src/router/bert.ts +145 -0
  102. package/src/router/controller.test.ts +183 -0
  103. package/src/router/controller.ts +165 -0
  104. package/src/router/integration.test.ts +309 -0
  105. package/src/router/intent.test.ts +187 -0
  106. package/src/router/intent.ts +208 -0
  107. package/src/router/mf.test.ts +66 -0
  108. package/src/router/mf.ts +52 -0
  109. package/src/router/random.test.ts +23 -0
  110. package/src/router/random.ts +13 -0
  111. package/src/router/router.test.ts +31 -0
  112. package/src/router/router.ts +62 -0
  113. package/src/router/server.test.ts +193 -0
  114. package/src/router/server.ts +134 -0
  115. package/src/router/sw.test.ts +199 -0
  116. package/src/router/sw.ts +232 -0
  117. package/src/rpc.test.ts +47 -0
  118. package/src/rpc.ts +91 -0
  119. package/src/safety.test.ts +53 -0
  120. package/src/safety.ts +42 -0
  121. package/src/scope-defaults.json +72 -0
  122. package/src/scope.test.ts +177 -0
  123. package/src/scope.ts +382 -0
  124. package/src/setflag.test.ts +110 -0
  125. package/src/setflag.ts +102 -0
  126. package/src/sqlstore.test.ts +110 -0
  127. package/src/sqlstore.ts +144 -0
  128. package/src/stream.test.ts +69 -0
  129. package/src/stream.ts +47 -0
  130. package/src/textflag.test.ts +105 -0
  131. package/src/textflag.ts +68 -0
  132. package/src/toolspec.test.ts +187 -0
  133. package/src/toolspec.ts +279 -0
  134. package/src/triton/client.test.ts +205 -0
  135. package/src/triton/client.ts +261 -0
  136. package/src/tui/anim.test.ts +146 -0
  137. package/src/tui/anim.ts +134 -0
  138. package/src/tui/badge.test.ts +41 -0
  139. package/src/tui/badge.ts +55 -0
  140. package/src/tui/confirm.test.ts +68 -0
  141. package/src/tui/confirm.ts +49 -0
  142. package/src/tui/index.ts +29 -0
  143. package/src/tui/list.test.ts +84 -0
  144. package/src/tui/list.ts +72 -0
  145. package/src/tui/parity.ts +41 -0
  146. package/src/tui/pills.test.ts +40 -0
  147. package/src/tui/pills.ts +43 -0
  148. package/src/tui/progress.test.ts +164 -0
  149. package/src/tui/progress.ts +120 -0
  150. package/src/tui/spinner.test.ts +111 -0
  151. package/src/tui/spinner.ts +96 -0
  152. package/src/tui/status.test.ts +68 -0
  153. package/src/tui/status.ts +66 -0
  154. package/src/upgrade.test.ts +298 -0
  155. package/src/upgrade.ts +205 -0
  156. package/src/uri.ts +199 -0
  157. package/src/wizard.test.ts +441 -0
  158. package/src/wizard.ts +662 -0
  159. package/src/xdg.test.ts +202 -0
  160. package/src/xdg.ts +280 -0
  161. package/test/output/cols.test.ts +148 -0
  162. package/test/output/csv.test.ts +129 -0
  163. package/test/output/dispatch.test.ts +194 -0
  164. package/test/output/format_help.test.ts +131 -0
  165. package/test/output/formatter.test.ts +137 -0
  166. package/test/output/output_flag.test.ts +124 -0
  167. package/test/output/registry.test.ts +127 -0
  168. package/test/output/smoke-capture.ts +83 -0
  169. package/test/output/smoke.test.ts +142 -0
  170. package/test/output/template.test.ts +95 -0
  171. package/test/output/text.test.ts +75 -0
  172. package/test/testdata/api-fixture.json +115 -0
  173. package/test/testdata/query-vectors.json +187 -0
  174. package/tsconfig.json +12 -0
package/src/aim.ts ADDED
@@ -0,0 +1,388 @@
1
+ /**
2
+ * aim.ts — AI model registry: types, source, cache, registry, query parser.
3
+ * Port of Go's aim package.
4
+ */
5
+ import * as fs from 'fs';
6
+ import * as path from 'path';
7
+ import { cacheDir } from './xdg';
8
+
9
+ // ── Types ──────────────────────────────────────────────────────
10
+ export interface Modalities { input?: string[]; output?: string[] }
11
+ export interface Limit { context?: number; output?: number }
12
+ export interface Cost { input?: number; output?: number }
13
+
14
+ export interface Model {
15
+ id: string; name: string; provider?: string; family?: string;
16
+ input: string[]; output: string[];
17
+ tool_call: boolean; reasoning: boolean; open_weights: boolean;
18
+ context: number; maxOutput: number;
19
+ costInput: number; costOutput: number;
20
+ modalities?: Modalities; limit?: Limit; cost?: Cost;
21
+ }
22
+
23
+ export interface Provider {
24
+ id: string; name: string; models: Record<string, Model>;
25
+ }
26
+
27
+ export interface Filter {
28
+ input?: string; output?: string; provider?: string; family?: string;
29
+ toolCall?: boolean; reasoning?: boolean; openWeights?: boolean;
30
+ query?: string;
31
+ }
32
+
33
+ /** Flatten nested wire fields into top-level Model fields. */
34
+ export function normalize(m: Model): Model {
35
+ const n = { ...m };
36
+ n.input = n.modalities?.input ?? n.input ?? [];
37
+ n.output = n.modalities?.output ?? n.output ?? [];
38
+ n.context = n.limit?.context ?? n.context ?? 0;
39
+ n.maxOutput = n.limit?.output ?? n.maxOutput ?? 0;
40
+ n.costInput = n.cost?.input ?? n.costInput ?? 0;
41
+ n.costOutput = n.cost?.output ?? n.costOutput ?? 0;
42
+ n.tool_call = n.tool_call ?? false;
43
+ n.reasoning = n.reasoning ?? false;
44
+ n.open_weights = n.open_weights ?? false;
45
+ return n;
46
+ }
47
+
48
+ // ── Source ──────────────────────────────────────────────────────
49
+ export interface Source {
50
+ fetch(signal?: AbortSignal): Promise<Record<string, Provider>>;
51
+ }
52
+ export interface ETagSource extends Source {
53
+ fetchWithETag(etag: string, signal?: AbortSignal): Promise<{
54
+ providers: Record<string, Provider> | null;
55
+ etag: string; notModified: boolean;
56
+ }>;
57
+ }
58
+
59
+ export interface ModelsDevSourceOptions {
60
+ url?: string; timeout?: number; maxSize?: number;
61
+ }
62
+
63
+ function validateProviders(raw: Record<string, Provider>) {
64
+ for (const [key, p] of Object.entries(raw)) {
65
+ if (key !== p.id)
66
+ throw new Error(`aim: map key "${key}" != provider.id "${p.id}"`);
67
+ for (const [id, m] of Object.entries(p.models)) {
68
+ const normalized = normalize(m);
69
+ if (!normalized.provider) normalized.provider = key;
70
+ p.models[id] = normalized;
71
+ }
72
+ }
73
+ return raw;
74
+ }
75
+
76
+ export class ModelsDevSource implements ETagSource {
77
+ private url: string;
78
+ private timeout: number;
79
+ private maxSize: number;
80
+
81
+ constructor(opts?: ModelsDevSourceOptions) {
82
+ this.url = opts?.url ?? 'https://models.dev/api.json';
83
+ this.timeout = opts?.timeout ?? 30_000;
84
+ this.maxSize = opts?.maxSize ?? 50 * 1024 * 1024;
85
+ }
86
+
87
+ private doFetch(headers: Record<string, string>, signal?: AbortSignal) {
88
+ const ctrl = new AbortController();
89
+ const timer = setTimeout(() => ctrl.abort(), this.timeout);
90
+ if (signal) signal.addEventListener('abort', () => ctrl.abort(), { once: true });
91
+ return { ctrl, timer, promise: globalThis.fetch(this.url, {
92
+ headers, signal: ctrl.signal,
93
+ }) };
94
+ }
95
+
96
+ private async readBody(resp: Response) {
97
+ const text = await resp.text();
98
+ if (text.length > this.maxSize)
99
+ throw new Error(`aim: response exceeds max size (${this.maxSize} bytes)`);
100
+ return validateProviders(JSON.parse(text));
101
+ }
102
+
103
+ async fetch(signal?: AbortSignal): Promise<Record<string, Provider>> {
104
+ const { timer, promise } = this.doFetch({ Accept: 'application/json' }, signal);
105
+ try {
106
+ const resp = await promise;
107
+ if (resp.status !== 200)
108
+ throw new Error(`aim: unexpected status ${resp.status} from ${this.url}`);
109
+ return this.readBody(resp);
110
+ } finally { clearTimeout(timer); }
111
+ }
112
+
113
+ async fetchWithETag(etag: string, signal?: AbortSignal) {
114
+ const hdrs: Record<string, string> = { Accept: 'application/json' };
115
+ if (etag) hdrs['If-None-Match'] = etag;
116
+ const { timer, promise } = this.doFetch(hdrs, signal);
117
+ try {
118
+ const resp = await promise;
119
+ if (resp.status === 304)
120
+ return { providers: null, etag, notModified: true as const };
121
+ if (resp.status !== 200)
122
+ throw new Error(`aim: unexpected status ${resp.status} from ${this.url}`);
123
+ const providers = await this.readBody(resp);
124
+ return {
125
+ providers, notModified: false as const,
126
+ etag: resp.headers.get('etag') ?? '',
127
+ };
128
+ } finally { clearTimeout(timer); }
129
+ }
130
+ }
131
+
132
+ // ── Cache ──────────────────────────────────────────────────────
133
+ const DATA_FILE = 'providers.json';
134
+ const ETAG_FILE = 'etag';
135
+ const LOCK_FILE = '.lock';
136
+ const LOCK_STALE_MS = 30_000;
137
+ const LOCK_POLL_MS = 50;
138
+ const LOCK_TIMEOUT_MS = 10_000;
139
+
140
+ function acquireLock(dir: string): void {
141
+ const lockPath = path.join(dir, LOCK_FILE);
142
+ const deadline = Date.now() + LOCK_TIMEOUT_MS;
143
+ while (true) {
144
+ try {
145
+ fs.writeFileSync(lockPath, String(process.pid), { flag: 'wx' });
146
+ return;
147
+ } catch {
148
+ // break stale locks older than LOCK_STALE_MS
149
+ try {
150
+ const stat = fs.statSync(lockPath);
151
+ if (Date.now() - stat.mtimeMs > LOCK_STALE_MS) {
152
+ fs.unlinkSync(lockPath);
153
+ continue;
154
+ }
155
+ } catch { /* lock disappeared */ continue; }
156
+ if (Date.now() >= deadline)
157
+ throw new Error('aim: lock acquisition timed out');
158
+ // busy-wait (sync cache is already blocking)
159
+ const until = Date.now() + LOCK_POLL_MS;
160
+ while (Date.now() < until) { /* spin */ }
161
+ }
162
+ }
163
+ }
164
+
165
+ function releaseLock(dir: string): void {
166
+ try { fs.unlinkSync(path.join(dir, LOCK_FILE)); } catch { /* ok */ }
167
+ }
168
+
169
+ interface CacheEnvelope {
170
+ fetched_at: string; providers: Record<string, Provider>;
171
+ }
172
+ export interface CacheOptions { ttl?: number; dir?: string }
173
+
174
+ export class Cache implements Source {
175
+ private src: Source;
176
+ private ttl: number;
177
+ private dir: string;
178
+
179
+ constructor(src: Source, opts?: CacheOptions) {
180
+ this.src = src;
181
+ this.ttl = opts?.ttl ?? 86_400_000;
182
+ this.dir = opts?.dir ?? path.join(cacheDir(''), 'hop/aim');
183
+ }
184
+
185
+ async fetch(signal?: AbortSignal): Promise<Record<string, Provider>> {
186
+ const env = this.load();
187
+ if (env && Date.now() - new Date(env.fetched_at).getTime() < this.ttl)
188
+ return env.providers;
189
+ try { return await this.refresh(signal); }
190
+ catch {
191
+ if (env) return env.providers;
192
+ throw new Error('aim: fetch failed and no cached data available');
193
+ }
194
+ }
195
+
196
+ async forceRefresh(signal?: AbortSignal) { return this.refresh(signal); }
197
+
198
+ private async refresh(signal?: AbortSignal) {
199
+ fs.mkdirSync(this.dir, { recursive: true, mode: 0o755 });
200
+ acquireLock(this.dir);
201
+ try {
202
+ let providers: Record<string, Provider>;
203
+ const src = this.src;
204
+ if (typeof (src as ETagSource).fetchWithETag === 'function') {
205
+ const etag = this.readFile(ETAG_FILE);
206
+ const r = await (src as ETagSource).fetchWithETag(etag, signal);
207
+ if (r.notModified) {
208
+ const env = this.load();
209
+ if (env) { env.fetched_at = new Date().toISOString(); this.store(env); return env.providers; }
210
+ // cache corrupt/missing despite 304 — fall through to unconditional fetch
211
+ }
212
+ providers = r.providers ?? await src.fetch(signal);
213
+ if (r.etag) this.writeFile(ETAG_FILE, r.etag);
214
+ } else {
215
+ providers = await src.fetch(signal);
216
+ }
217
+ this.store({ fetched_at: new Date().toISOString(), providers });
218
+ return providers;
219
+ } finally { releaseLock(this.dir); }
220
+ }
221
+
222
+ private load(): CacheEnvelope | null {
223
+ try {
224
+ const env: CacheEnvelope = JSON.parse(
225
+ fs.readFileSync(path.join(this.dir, DATA_FILE), 'utf-8'),
226
+ );
227
+ // re-normalize to populate fields lost in JSON round-trip
228
+ for (const [key, p] of Object.entries(env.providers)) {
229
+ for (const [id, m] of Object.entries(p.models)) {
230
+ const n = normalize(m);
231
+ if (!n.provider) n.provider = key;
232
+ p.models[id] = n;
233
+ }
234
+ }
235
+ return env;
236
+ } catch { return null; }
237
+ }
238
+
239
+ private store(env: CacheEnvelope) {
240
+ const tmp = path.join(this.dir, DATA_FILE + '.tmp');
241
+ fs.writeFileSync(tmp, JSON.stringify(env), { mode: 0o644 });
242
+ fs.renameSync(tmp, path.join(this.dir, DATA_FILE));
243
+ }
244
+
245
+ private readFile(name: string): string {
246
+ try { return fs.readFileSync(path.join(this.dir, name), 'utf-8'); }
247
+ catch { return ''; }
248
+ }
249
+
250
+ private writeFile(name: string, data: string) {
251
+ const tmp = path.join(this.dir, name + '.tmp');
252
+ try { fs.writeFileSync(tmp, data, { mode: 0o644 }); fs.renameSync(tmp, path.join(this.dir, name)); }
253
+ catch { /* best-effort */ }
254
+ }
255
+ }
256
+
257
+ // ── Query Parser ───────────────────────────────────────────────
258
+ export function parseQuery(q: string): Filter {
259
+ const f: Filter = {};
260
+ const free: string[] = [];
261
+ for (let i = 0, n = q.length; i < n;) {
262
+ if (q[i] === ' ' || q[i] === '\t') { i++; continue; }
263
+ let val: string; let quoted = false;
264
+ if (q[i] === '"') {
265
+ const j = q.indexOf('"', i + 1);
266
+ if (j < 0) throw new Error('unterminated quote');
267
+ val = q.slice(i + 1, j); quoted = true; i = j + 1;
268
+ } else {
269
+ let j = i;
270
+ while (j < n && q[j] !== ' ' && q[j] !== '\t') j++;
271
+ val = q.slice(i, j); i = j;
272
+ }
273
+ if (quoted) { free.push(val); continue; }
274
+ const ci = val.indexOf(':');
275
+ if (ci < 0) { free.push(val); continue; }
276
+ const key = val.slice(0, ci), v = val.slice(ci + 1);
277
+ if (key === '') throw new Error(`malformed tag "${val}": missing key`);
278
+ if (v === '') throw new Error(`empty value for tag "${key}"`);
279
+ switch (key) {
280
+ case 'provider': f.provider = csv(f.provider, v); break;
281
+ case 'family': f.family = csv(f.family, v); break;
282
+ case 'in': f.input = csv(f.input, v); break;
283
+ case 'out': f.output = csv(f.output, v); break;
284
+ case 'toolcall': case 'reasoning': case 'openweights': {
285
+ const b = toBool(v, key);
286
+ if (key === 'toolcall') f.toolCall = b;
287
+ else if (key === 'reasoning') f.reasoning = b;
288
+ else f.openWeights = b;
289
+ break;
290
+ }
291
+ default: throw new Error(`unknown tag "${key}"`);
292
+ }
293
+ }
294
+ if (free.length > 0) f.query = free.join(' ');
295
+ return f;
296
+ }
297
+
298
+ function toBool(v: string, tag: string): boolean {
299
+ if (v === 'true') return true;
300
+ if (v === 'false') return false;
301
+ throw new Error(`invalid bool value "${v}" for tag "${tag}": must be true or false`);
302
+ }
303
+
304
+ function csv(a: string | undefined, b: string) { return a ? a + ',' + b : b; }
305
+
306
+ // ── Registry ───────────────────────────────────────────────────
307
+ export interface RegistryOptions {
308
+ sources?: Source[]; cacheDir?: string; cacheTTL?: number;
309
+ }
310
+
311
+ export class Registry {
312
+ private cache: Cache;
313
+ private providers: Record<string, Provider> | null = null;
314
+
315
+ constructor(opts?: RegistryOptions) {
316
+ const sources = opts?.sources ?? [new ModelsDevSource()];
317
+ const merged: Source = {
318
+ async fetch(signal?: AbortSignal) {
319
+ const r: Record<string, Provider> = {};
320
+ for (const s of sources) Object.assign(r, await s.fetch(signal));
321
+ return r;
322
+ },
323
+ };
324
+ this.cache = new Cache(merged, { dir: opts?.cacheDir, ttl: opts?.cacheTTL });
325
+ }
326
+
327
+ async ensure(): Promise<void> {
328
+ if (this.providers) return;
329
+ try { this.providers = await this.cache.fetch(); }
330
+ catch { this.providers = {}; }
331
+ }
332
+
333
+ async refresh() { this.providers = await this.cache.forceRefresh(); }
334
+
335
+ async getProviders(): Promise<Provider[]> {
336
+ await this.ensure();
337
+ return Object.values(this.providers!).sort((a, b) => a.id.localeCompare(b.id));
338
+ }
339
+
340
+ async models(f: Filter): Promise<Model[]> {
341
+ await this.ensure();
342
+ const out: Model[] = [];
343
+ for (const p of Object.values(this.providers!))
344
+ for (const m of Object.values(p.models))
345
+ if (matchesFilter(m, f)) out.push(m);
346
+ out.sort((a, b) =>
347
+ a.provider !== b.provider
348
+ ? (a.provider ?? '').localeCompare(b.provider ?? '')
349
+ : a.id.localeCompare(b.id));
350
+ return out;
351
+ }
352
+
353
+ async get(provider: string, model: string): Promise<Model | undefined> {
354
+ await this.ensure();
355
+ return this.providers![provider]?.models[model];
356
+ }
357
+
358
+ async query(q: string): Promise<Model[]> { return this.models(parseQuery(q)); }
359
+ }
360
+
361
+ // ── Filter matching ────────────────────────────────────────────
362
+ function csvMatch(filter: string, value: string | undefined): boolean {
363
+ return filter.split(',').some(v => v.trim() === value);
364
+ }
365
+
366
+ function matchesFilter(m: Model, f: Filter): boolean {
367
+ if (f.provider && !csvMatch(f.provider, m.provider)) return false;
368
+ if (f.family && !csvMatch(f.family, m.family)) return false;
369
+ if (f.input && !modSubset(f.input, m.input)) return false;
370
+ if (f.output && !modSubset(f.output, m.output)) return false;
371
+ if (f.toolCall !== undefined && m.tool_call !== f.toolCall) return false;
372
+ if (f.reasoning !== undefined && m.reasoning !== f.reasoning) return false;
373
+ if (f.openWeights !== undefined && m.open_weights !== f.openWeights) return false;
374
+ if (f.query) {
375
+ const q = f.query.toLowerCase();
376
+ if (!m.id.toLowerCase().includes(q) && !m.name.toLowerCase().includes(q)) return false;
377
+ }
378
+ return true;
379
+ }
380
+
381
+ function modSubset(filter: string, mods: string[]): boolean {
382
+ const set = new Set(mods.map(v => v.toLowerCase().trim()));
383
+ for (const w of filter.split(',')) {
384
+ const t = w.toLowerCase().trim();
385
+ if (t && !set.has(t)) return false;
386
+ }
387
+ return true;
388
+ }