@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
@@ -0,0 +1,331 @@
1
+ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
2
+ import * as fs from 'fs';
3
+ import * as path from 'path';
4
+ import * as os from 'os';
5
+ import {
6
+ parseQuery, normalize, Cache, Registry,
7
+ ModelsDevSource,
8
+ type Filter, type Model, type Provider, type Source,
9
+ } from './aim';
10
+
11
+ // ── Fixtures ───────────────────────────────────────────────────
12
+
13
+ const VECTORS_PATH = path.resolve(
14
+ __dirname, '../test/testdata/query-vectors.json',
15
+ );
16
+ const FIXTURE_PATH = path.resolve(
17
+ __dirname, '../test/testdata/api-fixture.json',
18
+ );
19
+
20
+ type Vector = {
21
+ input: string;
22
+ description: string;
23
+ expected?: Record<string, unknown>;
24
+ error?: string;
25
+ };
26
+
27
+ const vectors: Vector[] = JSON.parse(
28
+ fs.readFileSync(VECTORS_PATH, 'utf-8'),
29
+ );
30
+ const fixtureData: Record<string, Provider> = JSON.parse(
31
+ fs.readFileSync(FIXTURE_PATH, 'utf-8'),
32
+ );
33
+
34
+ // Map Go field names to TS field names for vector comparison.
35
+ function goFilterToTS(gf: Record<string, unknown>): Filter {
36
+ const f: Filter = {};
37
+ if (gf['Provider'] !== undefined) f.provider = gf['Provider'] as string;
38
+ if (gf['Family'] !== undefined) f.family = gf['Family'] as string;
39
+ if (gf['Input'] !== undefined) f.input = gf['Input'] as string;
40
+ if (gf['Output'] !== undefined) f.output = gf['Output'] as string;
41
+ if (gf['ToolCall'] !== undefined) f.toolCall = gf['ToolCall'] as boolean;
42
+ if (gf['Reasoning'] !== undefined) f.reasoning = gf['Reasoning'] as boolean;
43
+ if (gf['OpenWeights'] !== undefined)
44
+ f.openWeights = gf['OpenWeights'] as boolean;
45
+ if (gf['Query'] !== undefined) f.query = gf['Query'] as string;
46
+ return f;
47
+ }
48
+
49
+ let tmpDir: string;
50
+
51
+ beforeEach(() => {
52
+ tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'aim-test-'));
53
+ });
54
+
55
+ afterEach(() => {
56
+ fs.rmSync(tmpDir, { recursive: true, force: true });
57
+ });
58
+
59
+ // ── parseQuery — test vectors ──────────────────────────────────
60
+
61
+ describe('parseQuery', () => {
62
+ for (const v of vectors) {
63
+ it(v.description, () => {
64
+ if (v.error) {
65
+ expect(() => parseQuery(v.input)).toThrow(v.error);
66
+ } else {
67
+ const got = parseQuery(v.input);
68
+ const want = goFilterToTS(v.expected ?? {});
69
+ expect(got).toEqual(want);
70
+ }
71
+ });
72
+ }
73
+ });
74
+
75
+ // ── normalize ──────────────────────────────────────────────────
76
+
77
+ describe('normalize', () => {
78
+ it('flattens modalities and limit', () => {
79
+ const m = normalize({
80
+ id: 'test', name: 'Test',
81
+ modalities: { input: ['text'], output: ['image'] },
82
+ limit: { context: 100000, output: 4096 },
83
+ } as unknown as Model);
84
+ expect(m.input).toEqual(['text']);
85
+ expect(m.output).toEqual(['image']);
86
+ expect(m.context).toBe(100000);
87
+ expect(m.maxOutput).toBe(4096);
88
+ });
89
+
90
+ it('does not mutate the input object', () => {
91
+ const orig = {
92
+ id: 'test', name: 'Test',
93
+ modalities: { input: ['text'], output: ['image'] },
94
+ limit: { context: 100000, output: 4096 },
95
+ } as unknown as Model;
96
+ const before = JSON.stringify(orig);
97
+ normalize(orig);
98
+ expect(JSON.stringify(orig)).toBe(before);
99
+ });
100
+
101
+ it('defaults missing fields', () => {
102
+ const m = normalize({ id: 'x', name: 'X' } as unknown as Model);
103
+ expect(m.input).toEqual([]);
104
+ expect(m.output).toEqual([]);
105
+ expect(m.context).toBe(0);
106
+ expect(m.maxOutput).toBe(0);
107
+ expect(m.tool_call).toBe(false);
108
+ });
109
+ });
110
+
111
+ // ── ModelsDevSource (mocked fetch) ─────────────────────────────
112
+
113
+ describe('ModelsDevSource', () => {
114
+ it('fetches and normalizes providers', async () => {
115
+ const mockFetch = vi.fn().mockResolvedValue({
116
+ status: 200,
117
+ text: () => Promise.resolve(JSON.stringify(fixtureData)),
118
+ headers: new Map([['etag', '"abc"']]),
119
+ });
120
+ vi.stubGlobal('fetch', mockFetch);
121
+
122
+ const src = new ModelsDevSource({ url: 'https://test.local/api.json' });
123
+ const providers = await src.fetch();
124
+
125
+ expect(Object.keys(providers)).toHaveLength(3);
126
+ expect(providers['anthropic'].models['claude-3-5-sonnet'].input)
127
+ .toEqual(['text', 'image']);
128
+ expect(providers['anthropic'].models['claude-3-5-sonnet'].context)
129
+ .toBe(200000);
130
+
131
+ vi.unstubAllGlobals();
132
+ });
133
+
134
+ it('rejects status != 200', async () => {
135
+ vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ status: 500 }));
136
+ const src = new ModelsDevSource();
137
+ await expect(src.fetch()).rejects.toThrow('unexpected status 500');
138
+ vi.unstubAllGlobals();
139
+ });
140
+
141
+ it('rejects oversized response', async () => {
142
+ vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
143
+ status: 200,
144
+ text: () => Promise.resolve('x'.repeat(200)),
145
+ }));
146
+ const src = new ModelsDevSource({ maxSize: 100 });
147
+ await expect(src.fetch()).rejects.toThrow('exceeds max size');
148
+ vi.unstubAllGlobals();
149
+ });
150
+ });
151
+
152
+ // ── Cache ──────────────────────────────────────────────────────
153
+
154
+ describe('Cache', () => {
155
+ function mockSource(): Source {
156
+ return {
157
+ fetch: vi.fn().mockResolvedValue(fixtureData),
158
+ };
159
+ }
160
+
161
+ it('caches to disk and serves from cache', async () => {
162
+ const src = mockSource();
163
+ const cache = new Cache(src, { dir: tmpDir, ttl: 60_000 });
164
+
165
+ const first = await cache.fetch();
166
+ expect(Object.keys(first)).toHaveLength(3);
167
+ expect(src.fetch).toHaveBeenCalledTimes(1);
168
+
169
+ const second = await cache.fetch();
170
+ expect(Object.keys(second)).toHaveLength(3);
171
+ expect(src.fetch).toHaveBeenCalledTimes(1); // still 1
172
+
173
+ expect(fs.existsSync(path.join(tmpDir, 'providers.json'))).toBe(true);
174
+ });
175
+
176
+ it('refetches when TTL expired', async () => {
177
+ const src = mockSource();
178
+ const cache = new Cache(src, { dir: tmpDir, ttl: 1 }); // 1ms TTL
179
+
180
+ await cache.fetch();
181
+ await new Promise(r => setTimeout(r, 10));
182
+ await cache.fetch();
183
+ expect(src.fetch).toHaveBeenCalledTimes(2);
184
+ });
185
+
186
+ it('stale-on-error returns cached data', async () => {
187
+ const src = mockSource();
188
+ const cache = new Cache(src, { dir: tmpDir, ttl: 1 });
189
+
190
+ await cache.fetch(); // populate cache
191
+ (src.fetch as ReturnType<typeof vi.fn>)
192
+ .mockRejectedValue(new Error('network'));
193
+
194
+ await new Promise(r => setTimeout(r, 10));
195
+ const result = await cache.fetch(); // should serve stale
196
+ expect(Object.keys(result)).toHaveLength(3);
197
+ });
198
+ });
199
+
200
+ // ── Registry ───────────────────────────────────────────────────
201
+
202
+ describe('Registry', () => {
203
+ function fixtureSource(): Source {
204
+ // normalize models in fixture
205
+ const normalized = JSON.parse(JSON.stringify(fixtureData));
206
+ for (const p of Object.values(normalized) as Provider[]) {
207
+ for (const [id, m] of Object.entries(p.models)) {
208
+ p.models[id] = normalize(m);
209
+ }
210
+ }
211
+ return { fetch: vi.fn().mockResolvedValue(normalized) };
212
+ }
213
+
214
+ it('lists all providers sorted', async () => {
215
+ const reg = new Registry({
216
+ sources: [fixtureSource()], cacheDir: tmpDir,
217
+ });
218
+ const providers = await reg.getProviders();
219
+ expect(providers.map(p => p.id))
220
+ .toEqual(['anthropic', 'meta', 'openai']);
221
+ });
222
+
223
+ it('filters by provider', async () => {
224
+ const reg = new Registry({
225
+ sources: [fixtureSource()], cacheDir: tmpDir,
226
+ });
227
+ const models = await reg.models({ provider: 'anthropic' });
228
+ expect(models).toHaveLength(3);
229
+ expect(models.every(m => m.provider === 'anthropic')).toBe(true);
230
+ });
231
+
232
+ it('filters by comma-separated provider', async () => {
233
+ const reg = new Registry({
234
+ sources: [fixtureSource()], cacheDir: tmpDir,
235
+ });
236
+ const models = await reg.models({ provider: 'anthropic,openai' });
237
+ expect(models.length).toBeGreaterThan(0);
238
+ expect(models.every(
239
+ m => m.provider === 'anthropic' || m.provider === 'openai',
240
+ )).toBe(true);
241
+ });
242
+
243
+ it('filters by reasoning:true', async () => {
244
+ const reg = new Registry({
245
+ sources: [fixtureSource()], cacheDir: tmpDir,
246
+ });
247
+ const models = await reg.models({ reasoning: true });
248
+ const ids = models.map(m => m.id);
249
+ expect(ids).toContain('claude-3-7-sonnet');
250
+ expect(ids).toContain('o1');
251
+ expect(ids).not.toContain('gpt-4o');
252
+ });
253
+
254
+ it('filters by input modality', async () => {
255
+ const reg = new Registry({
256
+ sources: [fixtureSource()], cacheDir: tmpDir,
257
+ });
258
+ const models = await reg.models({ input: 'image' });
259
+ expect(models.every(m => m.input.includes('image'))).toBe(true);
260
+ });
261
+
262
+ it('filters by output modality', async () => {
263
+ const reg = new Registry({
264
+ sources: [fixtureSource()], cacheDir: tmpDir,
265
+ });
266
+ const models = await reg.models({ output: 'image' });
267
+ expect(models).toHaveLength(1);
268
+ expect(models[0].id).toBe('dall-e-3');
269
+ });
270
+
271
+ it('filters by openWeights', async () => {
272
+ const reg = new Registry({
273
+ sources: [fixtureSource()], cacheDir: tmpDir,
274
+ });
275
+ const models = await reg.models({ openWeights: true });
276
+ expect(models.every(m => m.open_weights === true)).toBe(true);
277
+ expect(models).toHaveLength(2);
278
+ });
279
+
280
+ it('filters by free-text query', async () => {
281
+ const reg = new Registry({
282
+ sources: [fixtureSource()], cacheDir: tmpDir,
283
+ });
284
+ const models = await reg.models({ query: 'claude' });
285
+ expect(models).toHaveLength(3);
286
+ });
287
+
288
+ it('get() returns specific model', async () => {
289
+ const reg = new Registry({
290
+ sources: [fixtureSource()], cacheDir: tmpDir,
291
+ });
292
+ const m = await reg.get('openai', 'gpt-4o');
293
+ expect(m).toBeDefined();
294
+ expect(m!.name).toBe('GPT-4o');
295
+ });
296
+
297
+ it('get() returns undefined for missing', async () => {
298
+ const reg = new Registry({
299
+ sources: [fixtureSource()], cacheDir: tmpDir,
300
+ });
301
+ expect(await reg.get('openai', 'nonexistent')).toBeUndefined();
302
+ expect(await reg.get('missing', 'gpt-4o')).toBeUndefined();
303
+ });
304
+
305
+ it('query() parses string and filters', async () => {
306
+ const reg = new Registry({
307
+ sources: [fixtureSource()], cacheDir: tmpDir,
308
+ });
309
+ const models = await reg.query('provider:openai reasoning:true');
310
+ expect(models).toHaveLength(1);
311
+ expect(models[0].id).toBe('o1');
312
+ });
313
+
314
+ it('merges multiple sources (last wins)', async () => {
315
+ const src1: Source = {
316
+ fetch: vi.fn().mockResolvedValue({
317
+ anthropic: fixtureData['anthropic'],
318
+ }),
319
+ };
320
+ const src2: Source = {
321
+ fetch: vi.fn().mockResolvedValue({
322
+ openai: fixtureData['openai'],
323
+ }),
324
+ };
325
+ const reg = new Registry({
326
+ sources: [src1, src2], cacheDir: tmpDir,
327
+ });
328
+ const providers = await reg.getProviders();
329
+ expect(providers.map(p => p.id)).toEqual(['anthropic', 'openai']);
330
+ });
331
+ });