@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.
- package/CHANGELOG.md +86 -0
- package/LICENSE +25 -0
- package/README.md +189 -0
- package/dist/aim.d.ts +116 -0
- package/dist/aim.js +417 -0
- package/dist/alias.d.ts +73 -0
- package/dist/alias.js +207 -0
- package/dist/api.d.ts +62 -0
- package/dist/api.js +165 -0
- package/dist/auth.d.ts +26 -0
- package/dist/auth.js +41 -0
- package/dist/cli.d.ts +309 -0
- package/dist/cli.js +885 -0
- package/dist/config.d.ts +97 -0
- package/dist/config.js +79 -0
- package/dist/errcorrect.d.ts +37 -0
- package/dist/errcorrect.js +69 -0
- package/dist/llm.d.ts +188 -0
- package/dist/llm.js +356 -0
- package/dist/output.d.ts +213 -0
- package/dist/output.js +447 -0
- package/dist/progress.d.ts +30 -0
- package/dist/progress.js +56 -0
- package/dist/provenance.d.ts +24 -0
- package/dist/provenance.js +32 -0
- package/dist/routellm.d.ts +64 -0
- package/dist/routellm.js +258 -0
- package/dist/rpc.d.ts +25 -0
- package/dist/rpc.js +101 -0
- package/dist/safety.d.ts +24 -0
- package/dist/safety.js +44 -0
- package/dist/scope.d.ts +121 -0
- package/dist/scope.js +380 -0
- package/dist/sqlstore.d.ts +84 -0
- package/dist/sqlstore.js +85 -0
- package/dist/stream.d.ts +35 -0
- package/dist/stream.js +52 -0
- package/dist/tui/index.d.ts +300 -0
- package/dist/tui/index.js +332 -0
- package/dist/upgrade.d.ts +95 -0
- package/dist/upgrade.js +115 -0
- package/dist/xdg.d.ts +169 -0
- package/dist/xdg.js +129 -0
- package/eslint.config.mjs +17 -0
- package/package.json +133 -0
- package/src/README.md +3 -0
- package/src/aim.test.ts +331 -0
- package/src/aim.ts +388 -0
- package/src/alias.test.ts +423 -0
- package/src/alias.ts +256 -0
- package/src/api.test.ts +190 -0
- package/src/api.ts +214 -0
- package/src/auth.test.ts +24 -0
- package/src/auth.ts +37 -0
- package/src/bus.test.ts +178 -0
- package/src/bus.ts +114 -0
- package/src/cli.test.ts +690 -0
- package/src/cli.ts +741 -0
- package/src/completion-values.test.ts +202 -0
- package/src/completion-values.ts +173 -0
- package/src/completion.test.ts +212 -0
- package/src/completion.ts +158 -0
- package/src/compliance.test.ts +134 -0
- package/src/compliance.ts +669 -0
- package/src/config.test.ts +110 -0
- package/src/config.ts +164 -0
- package/src/errcorrect.test.ts +134 -0
- package/src/errcorrect.ts +84 -0
- package/src/flagregister.test.ts +103 -0
- package/src/flagregister.ts +78 -0
- package/src/gen/crud_connect.ts +76 -0
- package/src/gen/crud_pb.ts +269 -0
- package/src/hint.test.ts +315 -0
- package/src/hint.ts +178 -0
- package/src/llm.test.ts +612 -0
- package/src/llm.ts +577 -0
- package/src/log.test.ts +145 -0
- package/src/log.ts +155 -0
- package/src/output/builtins.ts +34 -0
- package/src/output/dispatch.ts +195 -0
- package/src/output/flags.ts +151 -0
- package/src/output/format_help.ts +80 -0
- package/src/output/formatter.ts +167 -0
- package/src/output/formatters/csv.ts +72 -0
- package/src/output/formatters/json.ts +44 -0
- package/src/output/formatters/table.ts +44 -0
- package/src/output/formatters/text.ts +109 -0
- package/src/output/formatters/yaml.ts +47 -0
- package/src/output/projection.ts +126 -0
- package/src/output/registry.ts +88 -0
- package/src/output/template.ts +63 -0
- package/src/output.test.ts +117 -0
- package/src/output.ts +90 -0
- package/src/progress.test.ts +93 -0
- package/src/progress.ts +58 -0
- package/src/provenance.test.ts +59 -0
- package/src/provenance.ts +26 -0
- package/src/routellm.test.ts +179 -0
- package/src/routellm.ts +365 -0
- package/src/router/bert.test.ts +180 -0
- package/src/router/bert.ts +145 -0
- package/src/router/controller.test.ts +183 -0
- package/src/router/controller.ts +165 -0
- package/src/router/integration.test.ts +309 -0
- package/src/router/intent.test.ts +187 -0
- package/src/router/intent.ts +208 -0
- package/src/router/mf.test.ts +66 -0
- package/src/router/mf.ts +52 -0
- package/src/router/random.test.ts +23 -0
- package/src/router/random.ts +13 -0
- package/src/router/router.test.ts +31 -0
- package/src/router/router.ts +62 -0
- package/src/router/server.test.ts +193 -0
- package/src/router/server.ts +134 -0
- package/src/router/sw.test.ts +199 -0
- package/src/router/sw.ts +232 -0
- package/src/rpc.test.ts +47 -0
- package/src/rpc.ts +91 -0
- package/src/safety.test.ts +53 -0
- package/src/safety.ts +42 -0
- package/src/scope-defaults.json +72 -0
- package/src/scope.test.ts +177 -0
- package/src/scope.ts +382 -0
- package/src/setflag.test.ts +110 -0
- package/src/setflag.ts +102 -0
- package/src/sqlstore.test.ts +110 -0
- package/src/sqlstore.ts +144 -0
- package/src/stream.test.ts +69 -0
- package/src/stream.ts +47 -0
- package/src/textflag.test.ts +105 -0
- package/src/textflag.ts +68 -0
- package/src/toolspec.test.ts +187 -0
- package/src/toolspec.ts +279 -0
- package/src/triton/client.test.ts +205 -0
- package/src/triton/client.ts +261 -0
- package/src/tui/anim.test.ts +146 -0
- package/src/tui/anim.ts +134 -0
- package/src/tui/badge.test.ts +41 -0
- package/src/tui/badge.ts +55 -0
- package/src/tui/confirm.test.ts +68 -0
- package/src/tui/confirm.ts +49 -0
- package/src/tui/index.ts +29 -0
- package/src/tui/list.test.ts +84 -0
- package/src/tui/list.ts +72 -0
- package/src/tui/parity.ts +41 -0
- package/src/tui/pills.test.ts +40 -0
- package/src/tui/pills.ts +43 -0
- package/src/tui/progress.test.ts +164 -0
- package/src/tui/progress.ts +120 -0
- package/src/tui/spinner.test.ts +111 -0
- package/src/tui/spinner.ts +96 -0
- package/src/tui/status.test.ts +68 -0
- package/src/tui/status.ts +66 -0
- package/src/upgrade.test.ts +298 -0
- package/src/upgrade.ts +205 -0
- package/src/uri.ts +199 -0
- package/src/wizard.test.ts +441 -0
- package/src/wizard.ts +662 -0
- package/src/xdg.test.ts +202 -0
- package/src/xdg.ts +280 -0
- package/test/output/cols.test.ts +148 -0
- package/test/output/csv.test.ts +129 -0
- package/test/output/dispatch.test.ts +194 -0
- package/test/output/format_help.test.ts +131 -0
- package/test/output/formatter.test.ts +137 -0
- package/test/output/output_flag.test.ts +124 -0
- package/test/output/registry.test.ts +127 -0
- package/test/output/smoke-capture.ts +83 -0
- package/test/output/smoke.test.ts +142 -0
- package/test/output/template.test.ts +95 -0
- package/test/output/text.test.ts +75 -0
- package/test/testdata/api-fixture.json +115 -0
- package/test/testdata/query-vectors.json +187 -0
- package/tsconfig.json +12 -0
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module output/template
|
|
3
|
+
*
|
|
4
|
+
* --template support via eta engine. EJS-style `<%= field %>` syntax.
|
|
5
|
+
*
|
|
6
|
+
* Template input: `{ items, cols, data }` where `items` is an array of
|
|
7
|
+
* objects projected through the ColumnSpec list (or the raw rows when no
|
|
8
|
+
* ColumnSpec is provided), `cols` is the list of header names, and `data`
|
|
9
|
+
* is the original payload for advanced use.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { Eta } from 'eta';
|
|
13
|
+
import type { ColumnSpec } from './formatter';
|
|
14
|
+
|
|
15
|
+
const eta = new Eta({ autoEscape: false });
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Renders an eta template against `data`.
|
|
19
|
+
*
|
|
20
|
+
* @param out Destination writable stream.
|
|
21
|
+
* @param src Template source string.
|
|
22
|
+
* @param data Single row or readonly array.
|
|
23
|
+
* @param columns Optional ColumnSpec list for header derivation + projection.
|
|
24
|
+
*/
|
|
25
|
+
export async function renderTemplate(
|
|
26
|
+
out: NodeJS.WritableStream,
|
|
27
|
+
src: string,
|
|
28
|
+
data: unknown,
|
|
29
|
+
columns?: readonly ColumnSpec[],
|
|
30
|
+
): Promise<void> {
|
|
31
|
+
const rows = Array.isArray(data) ? (data as readonly unknown[]) : [data];
|
|
32
|
+
const items = projectItems(rows, columns);
|
|
33
|
+
const cols = columns
|
|
34
|
+
? columns.map(c => c.header)
|
|
35
|
+
: items.length > 0 && typeof items[0] === 'object' && items[0] !== null
|
|
36
|
+
? Object.keys(items[0] as Record<string, unknown>)
|
|
37
|
+
: [];
|
|
38
|
+
const input = { items, cols, data };
|
|
39
|
+
|
|
40
|
+
let rendered: string;
|
|
41
|
+
try {
|
|
42
|
+
rendered = String(eta.renderString(src, input as never));
|
|
43
|
+
} catch (err) {
|
|
44
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
45
|
+
throw new Error(`template error: ${msg}`);
|
|
46
|
+
}
|
|
47
|
+
out.write(rendered);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function projectItems(
|
|
51
|
+
rows: readonly unknown[],
|
|
52
|
+
columns?: readonly ColumnSpec[],
|
|
53
|
+
): readonly Record<string, unknown>[] {
|
|
54
|
+
if (!columns) {
|
|
55
|
+
return rows.map(r => (r === null || typeof r !== 'object' ? {} : (r as Record<string, unknown>)));
|
|
56
|
+
}
|
|
57
|
+
return rows.map(row => {
|
|
58
|
+
const r = (row ?? {}) as Record<string, unknown>;
|
|
59
|
+
const out: Record<string, unknown> = {};
|
|
60
|
+
for (const c of columns) out[c.header] = r[c.key];
|
|
61
|
+
return out;
|
|
62
|
+
});
|
|
63
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from 'vitest';
|
|
2
|
+
import { render, JSON_FORMAT, YAML_FORMAT, TABLE_FORMAT } from './output';
|
|
3
|
+
|
|
4
|
+
function makeStream(): { write: ReturnType<typeof vi.fn>; captured: () => string } {
|
|
5
|
+
const chunks: string[] = [];
|
|
6
|
+
const write = vi.fn((chunk: string) => { chunks.push(chunk); return true; });
|
|
7
|
+
return { write, captured: () => chunks.join('') };
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
describe('render — JSON', () => {
|
|
11
|
+
it('writes 2-space indented JSON + trailing newline', () => {
|
|
12
|
+
const s = makeStream();
|
|
13
|
+
render({ write: s.write } as unknown as NodeJS.WritableStream, JSON_FORMAT, { a: 1, b: 'x' });
|
|
14
|
+
expect(s.captured()).toBe(JSON.stringify({ a: 1, b: 'x' }, null, 2) + '\n');
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('works with arrays', () => {
|
|
18
|
+
const s = makeStream();
|
|
19
|
+
render({ write: s.write } as unknown as NodeJS.WritableStream, JSON_FORMAT, [1, 2, 3]);
|
|
20
|
+
expect(s.captured()).toBe(JSON.stringify([1, 2, 3], null, 2) + '\n');
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
describe('render — YAML', () => {
|
|
25
|
+
it('writes valid YAML', () => {
|
|
26
|
+
const s = makeStream();
|
|
27
|
+
render({ write: s.write } as unknown as NodeJS.WritableStream, YAML_FORMAT, { name: 'hop', version: 1 });
|
|
28
|
+
const out = s.captured();
|
|
29
|
+
expect(out).toContain('name: hop');
|
|
30
|
+
expect(out).toContain('version: 1');
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('works with arrays', () => {
|
|
34
|
+
const s = makeStream();
|
|
35
|
+
render({ write: s.write } as unknown as NodeJS.WritableStream, YAML_FORMAT, ['a', 'b']);
|
|
36
|
+
const out = s.captured();
|
|
37
|
+
expect(out).toContain('- a');
|
|
38
|
+
expect(out).toContain('- b');
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
describe('render — table (array of objects)', () => {
|
|
43
|
+
const rows = [
|
|
44
|
+
{ id: '1', name: 'Alice', age: '30' },
|
|
45
|
+
{ id: '2', name: 'Bob', age: '25' },
|
|
46
|
+
];
|
|
47
|
+
|
|
48
|
+
it('includes headers from object keys', () => {
|
|
49
|
+
const s = makeStream();
|
|
50
|
+
render({ write: s.write } as unknown as NodeJS.WritableStream, TABLE_FORMAT, rows);
|
|
51
|
+
const out = s.captured();
|
|
52
|
+
expect(out).toContain('id');
|
|
53
|
+
expect(out).toContain('name');
|
|
54
|
+
expect(out).toContain('age');
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('includes all row values', () => {
|
|
58
|
+
const s = makeStream();
|
|
59
|
+
render({ write: s.write } as unknown as NodeJS.WritableStream, TABLE_FORMAT, rows);
|
|
60
|
+
const out = s.captured();
|
|
61
|
+
expect(out).toContain('Alice');
|
|
62
|
+
expect(out).toContain('Bob');
|
|
63
|
+
expect(out).toContain('30');
|
|
64
|
+
expect(out).toContain('25');
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('aligns columns (each column at least as wide as header)', () => {
|
|
68
|
+
const s = makeStream();
|
|
69
|
+
render({ write: s.write } as unknown as NodeJS.WritableStream, TABLE_FORMAT, rows);
|
|
70
|
+
const lines = s.captured().split('\n').filter(Boolean);
|
|
71
|
+
// header and two data rows
|
|
72
|
+
expect(lines.length).toBe(3);
|
|
73
|
+
// all lines same length (padded)
|
|
74
|
+
const lengths = lines.map(l => l.length);
|
|
75
|
+
expect(new Set(lengths).size).toBe(1);
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
describe('render — table (single object)', () => {
|
|
80
|
+
it('renders single object as 1-row table with headers', () => {
|
|
81
|
+
const s = makeStream();
|
|
82
|
+
render({ write: s.write } as unknown as NodeJS.WritableStream, TABLE_FORMAT, { id: '42', status: 'ok' });
|
|
83
|
+
const out = s.captured();
|
|
84
|
+
expect(out).toContain('id');
|
|
85
|
+
expect(out).toContain('status');
|
|
86
|
+
expect(out).toContain('42');
|
|
87
|
+
expect(out).toContain('ok');
|
|
88
|
+
const lines = out.split('\n').filter(Boolean);
|
|
89
|
+
expect(lines.length).toBe(2);
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
describe('render — table (empty array)', () => {
|
|
94
|
+
it('produces no output for empty array', () => {
|
|
95
|
+
const s = makeStream();
|
|
96
|
+
render({ write: s.write } as unknown as NodeJS.WritableStream, TABLE_FORMAT, []);
|
|
97
|
+
expect(s.captured()).toBe('');
|
|
98
|
+
expect(s.write).not.toHaveBeenCalled();
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
describe('render — unknown format', () => {
|
|
103
|
+
it('throws for unrecognised format', () => {
|
|
104
|
+
const s = makeStream();
|
|
105
|
+
// 'bogus' replaces the previous 'csv' fixture: csv is now a registered
|
|
106
|
+
// built-in, so the original would no longer be "unknown".
|
|
107
|
+
expect(() =>
|
|
108
|
+
render({ write: s.write } as unknown as NodeJS.WritableStream, 'bogus' as never, {}),
|
|
109
|
+
).toThrow(/unknown.*format/i);
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
describe('exported constants', () => {
|
|
114
|
+
it('JSON_FORMAT === "json"', () => expect(JSON_FORMAT).toBe('json'));
|
|
115
|
+
it('YAML_FORMAT === "yaml"', () => expect(YAML_FORMAT).toBe('yaml'));
|
|
116
|
+
it('TABLE_FORMAT === "table"', () => expect(TABLE_FORMAT).toBe('table'));
|
|
117
|
+
});
|
package/src/output.ts
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module output
|
|
3
|
+
* @package @hop-top/kit
|
|
4
|
+
*
|
|
5
|
+
* Output rendering — writes structured values to a writable stream in one of
|
|
6
|
+
* the registered formats: table, JSON, YAML, CSV, text, etc.
|
|
7
|
+
*
|
|
8
|
+
* ## Quick start
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { render, TABLE_FORMAT, JSON_FORMAT, YAML_FORMAT } from '@hop-top/kit/output';
|
|
12
|
+
*
|
|
13
|
+
* // Write a table to stdout
|
|
14
|
+
* render(process.stdout, TABLE_FORMAT, [{ id: '1', name: 'Alice' }]);
|
|
15
|
+
*
|
|
16
|
+
* // Write indented JSON
|
|
17
|
+
* render(process.stdout, JSON_FORMAT, { ok: true });
|
|
18
|
+
*
|
|
19
|
+
* // Write YAML
|
|
20
|
+
* render(process.stdout, YAML_FORMAT, { version: 1 });
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* ## Programmatic Formatter API
|
|
24
|
+
*
|
|
25
|
+
* For typed flag wiring + custom formatters use the Formatter / Registry
|
|
26
|
+
* surface from `output/formatter` and `output/registry`. The `render()`
|
|
27
|
+
* helper exported here is a thin shim over `defaultRegistry`.
|
|
28
|
+
*
|
|
29
|
+
* ## Backward compatibility
|
|
30
|
+
*
|
|
31
|
+
* `render(w, format, v)` keeps its original signature. New per-format
|
|
32
|
+
* options + column projection are accessed via `dispatch()`.
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
// Re-export the typed surface.
|
|
36
|
+
export type { Formatter, OptionSpec, OptionType, Options, ColumnSpec } from './output/formatter';
|
|
37
|
+
export { parseOptions, optionTypeName } from './output/formatter';
|
|
38
|
+
export { Registry, defaultRegistry, newRegistry } from './output/registry';
|
|
39
|
+
export { jsonFormatter } from './output/formatters/json';
|
|
40
|
+
export { yamlFormatter } from './output/formatters/yaml';
|
|
41
|
+
export { tableFormatter } from './output/formatters/table';
|
|
42
|
+
|
|
43
|
+
// Side-effect: register built-ins.
|
|
44
|
+
import './output/builtins';
|
|
45
|
+
|
|
46
|
+
import { defaultRegistry } from './output/registry';
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Supported output formats. Includes the original three for backward-compat
|
|
50
|
+
* + new built-ins (csv, text) registered on the default registry.
|
|
51
|
+
*/
|
|
52
|
+
export type Format = string;
|
|
53
|
+
|
|
54
|
+
/** Constant for the JSON output format. */
|
|
55
|
+
export const JSON_FORMAT = 'json' as const;
|
|
56
|
+
|
|
57
|
+
/** Constant for the YAML output format. */
|
|
58
|
+
export const YAML_FORMAT = 'yaml' as const;
|
|
59
|
+
|
|
60
|
+
/** Constant for the table output format. */
|
|
61
|
+
export const TABLE_FORMAT = 'table' as const;
|
|
62
|
+
|
|
63
|
+
/** Constant for the CSV output format. */
|
|
64
|
+
export const CSV_FORMAT = 'csv' as const;
|
|
65
|
+
|
|
66
|
+
/** Constant for the text output format. */
|
|
67
|
+
export const TEXT_FORMAT = 'text' as const;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Renders `v` to `w` in the requested `format`.
|
|
71
|
+
*
|
|
72
|
+
* Backward-compatible thin shim over `defaultRegistry.lookup(format).render`.
|
|
73
|
+
*
|
|
74
|
+
* @throws {Error} For unknown format strings.
|
|
75
|
+
*/
|
|
76
|
+
export function render(
|
|
77
|
+
w: NodeJS.WritableStream,
|
|
78
|
+
format: Format,
|
|
79
|
+
v: unknown,
|
|
80
|
+
): void {
|
|
81
|
+
const f = defaultRegistry.lookup(format);
|
|
82
|
+
if (!f) {
|
|
83
|
+
const valid = defaultRegistry.keys().join(', ');
|
|
84
|
+
throw new Error(`unknown output format "${format}" (valid: ${valid})`);
|
|
85
|
+
}
|
|
86
|
+
// Built-ins json/yaml/table are sync; other registered formatters may
|
|
87
|
+
// return a promise. Caller of the legacy render() expects sync, so we
|
|
88
|
+
// do not await — sync formatters complete before this returns.
|
|
89
|
+
void f.render(w, v as never, {}, []);
|
|
90
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { PassThrough } from 'node:stream';
|
|
3
|
+
import { ProgressReporter, type ProgressEvent } from './progress';
|
|
4
|
+
|
|
5
|
+
function collect(stream: PassThrough): string[] {
|
|
6
|
+
const lines: string[] = [];
|
|
7
|
+
stream.on('data', (chunk: Buffer) => {
|
|
8
|
+
const text = chunk.toString().trim();
|
|
9
|
+
if (text) lines.push(text);
|
|
10
|
+
});
|
|
11
|
+
return lines;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
describe('ProgressReporter', () => {
|
|
15
|
+
describe('non-TTY (JSON)', () => {
|
|
16
|
+
it('emits JSON event', () => {
|
|
17
|
+
const out = new PassThrough();
|
|
18
|
+
const lines = collect(out);
|
|
19
|
+
const r = new ProgressReporter(out, false);
|
|
20
|
+
const ev: ProgressEvent = {
|
|
21
|
+
phase: 'download',
|
|
22
|
+
step: 'fetch',
|
|
23
|
+
current: 5,
|
|
24
|
+
total: 10,
|
|
25
|
+
percent: 50,
|
|
26
|
+
};
|
|
27
|
+
r.emit(ev);
|
|
28
|
+
|
|
29
|
+
expect(lines).toHaveLength(1);
|
|
30
|
+
const parsed = JSON.parse(lines[0]);
|
|
31
|
+
expect(parsed.phase).toBe('download');
|
|
32
|
+
expect(parsed.percent).toBe(50);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('emits done as JSON', () => {
|
|
36
|
+
const out = new PassThrough();
|
|
37
|
+
const lines = collect(out);
|
|
38
|
+
const r = new ProgressReporter(out, false);
|
|
39
|
+
r.done('all done');
|
|
40
|
+
|
|
41
|
+
expect(lines).toHaveLength(1);
|
|
42
|
+
const parsed = JSON.parse(lines[0]);
|
|
43
|
+
expect(parsed.done).toBe(true);
|
|
44
|
+
expect(parsed.message).toBe('all done');
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
describe('TTY (human)', () => {
|
|
49
|
+
it('emits human-readable progress', () => {
|
|
50
|
+
const out = new PassThrough();
|
|
51
|
+
const lines = collect(out);
|
|
52
|
+
const r = new ProgressReporter(out, true);
|
|
53
|
+
r.emit({
|
|
54
|
+
phase: 'upload',
|
|
55
|
+
step: 'send',
|
|
56
|
+
current: 3,
|
|
57
|
+
total: 9,
|
|
58
|
+
percent: 33,
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
expect(lines).toHaveLength(1);
|
|
62
|
+
expect(lines[0]).toContain('upload');
|
|
63
|
+
expect(lines[0]).toContain('33%');
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('emits human-readable done', () => {
|
|
67
|
+
const out = new PassThrough();
|
|
68
|
+
const lines = collect(out);
|
|
69
|
+
const r = new ProgressReporter(out, true);
|
|
70
|
+
r.done('finished');
|
|
71
|
+
|
|
72
|
+
expect(lines).toHaveLength(1);
|
|
73
|
+
expect(lines[0]).toContain('finished');
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('includes optional message in event', () => {
|
|
78
|
+
const out = new PassThrough();
|
|
79
|
+
const lines = collect(out);
|
|
80
|
+
const r = new ProgressReporter(out, false);
|
|
81
|
+
r.emit({
|
|
82
|
+
phase: 'build',
|
|
83
|
+
step: 'compile',
|
|
84
|
+
current: 1,
|
|
85
|
+
total: 1,
|
|
86
|
+
percent: 100,
|
|
87
|
+
message: 'compiling main',
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
const parsed = JSON.parse(lines[0]);
|
|
91
|
+
expect(parsed.message).toBe('compiling main');
|
|
92
|
+
});
|
|
93
|
+
});
|
package/src/progress.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module progress
|
|
3
|
+
* @package @hop-top/kit
|
|
4
|
+
*
|
|
5
|
+
* Factor 9 — Observable Long-Running Operations.
|
|
6
|
+
*
|
|
7
|
+
* Emits structured progress events (JSON in non-TTY,
|
|
8
|
+
* human-readable in TTY).
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
export interface ProgressEvent {
|
|
12
|
+
phase: string;
|
|
13
|
+
step: string;
|
|
14
|
+
current: number;
|
|
15
|
+
total: number;
|
|
16
|
+
percent: number;
|
|
17
|
+
message?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface JobHandle {
|
|
21
|
+
id: string;
|
|
22
|
+
status: 'running' | 'completed' | 'failed' | 'cancelled';
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export class ProgressReporter {
|
|
26
|
+
private w: NodeJS.WritableStream;
|
|
27
|
+
private tty: boolean;
|
|
28
|
+
|
|
29
|
+
constructor(w: NodeJS.WritableStream, isTTY: boolean) {
|
|
30
|
+
this.w = w;
|
|
31
|
+
this.tty = isTTY;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
emit(event: ProgressEvent): void {
|
|
35
|
+
if (this.tty) {
|
|
36
|
+
const msg = event.message
|
|
37
|
+
? ` ${event.message}`
|
|
38
|
+
: '';
|
|
39
|
+
this.w.write(
|
|
40
|
+
`[${event.phase}] ${event.step}`
|
|
41
|
+
+ ` ${event.current}/${event.total}`
|
|
42
|
+
+ ` (${event.percent}%)${msg}\n`,
|
|
43
|
+
);
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
this.w.write(JSON.stringify(event) + '\n');
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
done(message: string): void {
|
|
50
|
+
if (this.tty) {
|
|
51
|
+
this.w.write(`done: ${message}\n`);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
this.w.write(
|
|
55
|
+
JSON.stringify({ done: true, message }) + '\n',
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { withProvenance, type Provenance } from './provenance';
|
|
3
|
+
|
|
4
|
+
describe('withProvenance', () => {
|
|
5
|
+
it('wraps data with _meta provenance', () => {
|
|
6
|
+
const data = [{ id: 1, name: 'alpha' }];
|
|
7
|
+
const prov: Provenance = {
|
|
8
|
+
source: 'spaced',
|
|
9
|
+
timestamp: '2026-04-17T12:00:00Z',
|
|
10
|
+
method: 'mission.list',
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const result = withProvenance(data, prov);
|
|
14
|
+
|
|
15
|
+
expect(result.data).toBe(data);
|
|
16
|
+
expect(result._meta).toEqual(prov);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('preserves original data by reference', () => {
|
|
20
|
+
const data = { key: 'value' };
|
|
21
|
+
const prov: Provenance = {
|
|
22
|
+
source: 'cli',
|
|
23
|
+
timestamp: '2026-04-17T00:00:00Z',
|
|
24
|
+
method: 'get',
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const result = withProvenance(data, prov);
|
|
28
|
+
expect(result.data).toBe(data);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('timestamp is valid ISO 8601', () => {
|
|
32
|
+
const prov: Provenance = {
|
|
33
|
+
source: 'test',
|
|
34
|
+
timestamp: '2026-04-17T12:30:45.123Z',
|
|
35
|
+
method: 'list',
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const result = withProvenance({}, prov);
|
|
39
|
+
const parsed = new Date(result._meta.timestamp);
|
|
40
|
+
expect(parsed.toISOString()).toBe('2026-04-17T12:30:45.123Z');
|
|
41
|
+
expect(isNaN(parsed.getTime())).toBe(false);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('serializes cleanly to JSON', () => {
|
|
45
|
+
const result = withProvenance(
|
|
46
|
+
[{ id: 1 }],
|
|
47
|
+
{
|
|
48
|
+
source: 'spaced',
|
|
49
|
+
timestamp: '2026-04-17T00:00:00Z',
|
|
50
|
+
method: 'mission.list',
|
|
51
|
+
},
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
const json = JSON.parse(JSON.stringify(result));
|
|
55
|
+
expect(json.data).toEqual([{ id: 1 }]);
|
|
56
|
+
expect(json._meta.source).toBe('spaced');
|
|
57
|
+
expect(json._meta.method).toBe('mission.list');
|
|
58
|
+
});
|
|
59
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module provenance
|
|
3
|
+
* @package @hop-top/kit
|
|
4
|
+
*
|
|
5
|
+
* Factor 11 — Provenance.
|
|
6
|
+
*
|
|
7
|
+
* Attaches origin metadata to any data payload for JSON output,
|
|
8
|
+
* enabling consumers to verify source, timing, and method.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
export interface Provenance {
|
|
12
|
+
source: string;
|
|
13
|
+
/** ISO 8601 timestamp */
|
|
14
|
+
timestamp: string;
|
|
15
|
+
method: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Wraps `data` with provenance metadata under `_meta`.
|
|
20
|
+
*/
|
|
21
|
+
export function withProvenance<T>(
|
|
22
|
+
data: T,
|
|
23
|
+
p: Provenance,
|
|
24
|
+
): { data: T; _meta: Provenance } {
|
|
25
|
+
return { data, _meta: p };
|
|
26
|
+
}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for RouteLLM adapter (routellm.ts).
|
|
3
|
+
* Covers: parseModelField, parseRouterConfig, scheme registration.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { describe, it, expect, beforeEach, afterEach } from "vitest";
|
|
7
|
+
import { resolve } from "./llm";
|
|
8
|
+
import { parseModelField, parseRouterConfig } from "./routellm";
|
|
9
|
+
|
|
10
|
+
// ─── parseModelField ──────────────────────────────────────────────────────────
|
|
11
|
+
|
|
12
|
+
describe("parseModelField", () => {
|
|
13
|
+
it("parses valid router:threshold", () => {
|
|
14
|
+
const r = parseModelField("mf:0.7");
|
|
15
|
+
expect(r.routerName).toBe("mf");
|
|
16
|
+
expect(r.threshold).toBe(0.7);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("parses zero threshold", () => {
|
|
20
|
+
const r = parseModelField("bert:0");
|
|
21
|
+
expect(r.routerName).toBe("bert");
|
|
22
|
+
expect(r.threshold).toBe(0);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("parses threshold of 1", () => {
|
|
26
|
+
const r = parseModelField("causal_llm:1");
|
|
27
|
+
expect(r.routerName).toBe("causal_llm");
|
|
28
|
+
expect(r.threshold).toBe(1);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("parses precise threshold", () => {
|
|
32
|
+
const r = parseModelField("mf:0.123456");
|
|
33
|
+
expect(r.routerName).toBe("mf");
|
|
34
|
+
expect(r.threshold).toBe(0.123456);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("throws on empty string", () => {
|
|
38
|
+
expect(() => parseModelField("")).toThrow("model field is required");
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("throws on missing colon", () => {
|
|
42
|
+
expect(() => parseModelField("mf")).toThrow("expected format");
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("throws on empty router name", () => {
|
|
46
|
+
expect(() => parseModelField(":0.5")).toThrow("expected format");
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("throws on empty threshold", () => {
|
|
50
|
+
expect(() => parseModelField("mf:")).toThrow("expected format");
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("throws on non-numeric threshold", () => {
|
|
54
|
+
expect(() => parseModelField("mf:abc")).toThrow("invalid threshold");
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
// ─── parseRouterConfig ────────────────────────────────────────────────────────
|
|
59
|
+
|
|
60
|
+
describe("parseRouterConfig", () => {
|
|
61
|
+
// Save and restore env vars around each test.
|
|
62
|
+
const envKeys = [
|
|
63
|
+
"ROUTELLM_BASE_URL",
|
|
64
|
+
"ROUTELLM_STRONG_MODEL",
|
|
65
|
+
"ROUTELLM_WEAK_MODEL",
|
|
66
|
+
"ROUTELLM_ROUTERS",
|
|
67
|
+
] as const;
|
|
68
|
+
|
|
69
|
+
const saved: Record<string, string | undefined> = {};
|
|
70
|
+
|
|
71
|
+
beforeEach(() => {
|
|
72
|
+
for (const k of envKeys) {
|
|
73
|
+
saved[k] = process.env[k];
|
|
74
|
+
delete process.env[k];
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
afterEach(() => {
|
|
79
|
+
for (const k of envKeys) {
|
|
80
|
+
if (saved[k] !== undefined) {
|
|
81
|
+
process.env[k] = saved[k];
|
|
82
|
+
} else {
|
|
83
|
+
delete process.env[k];
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("returns defaults when extras is undefined", () => {
|
|
89
|
+
const cfg = parseRouterConfig(undefined);
|
|
90
|
+
expect(cfg.baseUrl).toBe("http://localhost:6060");
|
|
91
|
+
expect(cfg.grpcPort).toBe(6061);
|
|
92
|
+
expect(cfg.strongModel).toBe("");
|
|
93
|
+
expect(cfg.weakModel).toBe("");
|
|
94
|
+
expect(cfg.routers).toEqual([]);
|
|
95
|
+
expect(cfg.autostart).toBe(false);
|
|
96
|
+
expect(cfg.eva).toEqual({ contracts: [], enforce: false });
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("returns defaults when extras has no routellm key", () => {
|
|
100
|
+
const cfg = parseRouterConfig({ other: "stuff" });
|
|
101
|
+
expect(cfg.baseUrl).toBe("http://localhost:6060");
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("extracts config from extras routellm sub-map", () => {
|
|
105
|
+
const cfg = parseRouterConfig({
|
|
106
|
+
routellm: {
|
|
107
|
+
base_url: "http://custom:9090",
|
|
108
|
+
grpc_port: 7070,
|
|
109
|
+
strong_model: "gpt-4",
|
|
110
|
+
weak_model: "gpt-3.5-turbo",
|
|
111
|
+
routers: ["mf", "bert"],
|
|
112
|
+
autostart: true,
|
|
113
|
+
eva: { contracts: ["c1"], enforce: true },
|
|
114
|
+
},
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
expect(cfg.baseUrl).toBe("http://custom:9090");
|
|
118
|
+
expect(cfg.grpcPort).toBe(7070);
|
|
119
|
+
expect(cfg.strongModel).toBe("gpt-4");
|
|
120
|
+
expect(cfg.weakModel).toBe("gpt-3.5-turbo");
|
|
121
|
+
expect(cfg.routers).toEqual(["mf", "bert"]);
|
|
122
|
+
expect(cfg.autostart).toBe(true);
|
|
123
|
+
expect(cfg.eva.contracts).toEqual(["c1"]);
|
|
124
|
+
expect(cfg.eva.enforce).toBe(true);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("env vars override extras", () => {
|
|
128
|
+
process.env["ROUTELLM_BASE_URL"] = "http://env:1111";
|
|
129
|
+
process.env["ROUTELLM_STRONG_MODEL"] = "env-strong";
|
|
130
|
+
process.env["ROUTELLM_WEAK_MODEL"] = "env-weak";
|
|
131
|
+
process.env["ROUTELLM_ROUTERS"] = "r1, r2";
|
|
132
|
+
|
|
133
|
+
const cfg = parseRouterConfig({
|
|
134
|
+
routellm: {
|
|
135
|
+
base_url: "http://config:9090",
|
|
136
|
+
strong_model: "config-strong",
|
|
137
|
+
},
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
expect(cfg.baseUrl).toBe("http://env:1111");
|
|
141
|
+
expect(cfg.strongModel).toBe("env-strong");
|
|
142
|
+
expect(cfg.weakModel).toBe("env-weak");
|
|
143
|
+
expect(cfg.routers).toEqual(["r1", "r2"]);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it("env vars override defaults when no extras", () => {
|
|
147
|
+
process.env["ROUTELLM_BASE_URL"] = "http://env-only:2222";
|
|
148
|
+
|
|
149
|
+
const cfg = parseRouterConfig(undefined);
|
|
150
|
+
expect(cfg.baseUrl).toBe("http://env-only:2222");
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it("throws when routellm key is not an object", () => {
|
|
154
|
+
expect(() => parseRouterConfig({ routellm: "bad" })).toThrow(
|
|
155
|
+
"expected map"
|
|
156
|
+
);
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
// ─── Scheme registration ──────────────────────────────────────────────────────
|
|
161
|
+
|
|
162
|
+
describe("routellm scheme registration", () => {
|
|
163
|
+
it("side-effect import registers the routellm scheme", () => {
|
|
164
|
+
// The top-level import of ./routellm triggers register("routellm", ...).
|
|
165
|
+
// Verify that resolve("routellm://mf:0.5") does NOT throw
|
|
166
|
+
// ProviderNotFoundError — it returns a Provider.
|
|
167
|
+
const provider = resolve("routellm://mf:0.5");
|
|
168
|
+
expect(provider).toBeDefined();
|
|
169
|
+
expect(typeof provider.close).toBe("function");
|
|
170
|
+
provider.close();
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it("exports RouteLLMAdapter and helpers", async () => {
|
|
174
|
+
const mod = await import("./routellm");
|
|
175
|
+
expect(mod.RouteLLMAdapter).toBeDefined();
|
|
176
|
+
expect(mod.parseModelField).toBeDefined();
|
|
177
|
+
expect(mod.parseRouterConfig).toBeDefined();
|
|
178
|
+
});
|
|
179
|
+
});
|