@hop-top/kit 0.5.0-alpha.3 → 0.5.0-alpha.5

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 (188) hide show
  1. package/CHANGELOG.md +52 -0
  2. package/LICENSE +1 -1
  3. package/README.md +47 -575
  4. package/dist/{aim-CTp-kJW2.d.ts → aim-DZHwwiIc.d.ts} +1 -1
  5. package/dist/aim.d.ts +1 -1
  6. package/dist/aim.js +13 -414
  7. package/dist/{alias-dhhr08Ei.d.ts → alias-BOj0-VAr.d.ts} +1 -1
  8. package/dist/alias.d.ts +1 -1
  9. package/dist/alias.js +18 -206
  10. package/dist/{api-miiso81x.d.ts → api-DAexsu2H.d.ts} +1 -1
  11. package/dist/api.d.ts +1 -1
  12. package/dist/api.js +10 -164
  13. package/dist/auth.js +6 -40
  14. package/dist/chunk-3FIKGFKI.js +54 -0
  15. package/dist/chunk-4UE5TCMQ.js +104 -0
  16. package/dist/chunk-6HFAEZ52.js +29 -0
  17. package/dist/chunk-6SVOHNIC.js +17 -0
  18. package/dist/chunk-72XZWQX7.js +504 -0
  19. package/dist/chunk-ABQSE7AI.js +513 -0
  20. package/dist/chunk-BDNNPRX6.js +41 -0
  21. package/dist/chunk-BRNEKPWX.js +37 -0
  22. package/dist/chunk-CSJWB6D3.js +151 -0
  23. package/dist/chunk-GGWJRWWA.js +309 -0
  24. package/dist/chunk-IQXEISMC.js +142 -0
  25. package/dist/chunk-IXVQ62NZ.js +334 -0
  26. package/dist/chunk-JTMPNXVQ.js +1491 -0
  27. package/dist/chunk-KVG7YCTS.js +115 -0
  28. package/dist/chunk-L23O2FFE.js +51 -0
  29. package/dist/chunk-MQXYDYRD.js +968 -0
  30. package/dist/chunk-MUIQVU6E.js +251 -0
  31. package/dist/chunk-POR5L2DG.js +247 -0
  32. package/dist/chunk-PSFYJGDG.js +1445 -0
  33. package/dist/chunk-QJVSKKRQ.js +369 -0
  34. package/dist/chunk-QPUCXQXT.js +67 -0
  35. package/dist/chunk-R5U7XKVJ.js +16 -0
  36. package/dist/chunk-S45UHXQF.js +182 -0
  37. package/dist/chunk-T46YPHKN.js +669 -0
  38. package/dist/chunk-UKZIAQA7.js +26 -0
  39. package/dist/chunk-WBVO43HU.js +355 -0
  40. package/dist/chunk-WUOAQVOA.js +86 -0
  41. package/dist/chunk-WWUKGJB5.js +90 -0
  42. package/dist/chunk-YGYQUFGY.js +219 -0
  43. package/dist/chunk-YOBAUCVN.js +583 -0
  44. package/dist/chunk-ZC5TZYZJ.js +609 -0
  45. package/dist/{cli-DLygZLi9.d.ts → cli-8rfITnv4.d.ts} +13 -4
  46. package/dist/cli.d.ts +4 -4
  47. package/dist/cli.js +51 -1057
  48. package/dist/{config-BUY7_W2n.d.ts → config-Bm9fBXch.d.ts} +35 -4
  49. package/dist/config.d.ts +1 -1
  50. package/dist/config.js +6 -78
  51. package/dist/errcorrect.js +8 -68
  52. package/dist/{error-DISJKeDu.d.ts → error-Do8Qji43.d.ts} +88 -1
  53. package/dist/id/index.d.ts +1 -1
  54. package/dist/id/index.js +14 -65
  55. package/dist/{index-MLrRDvEG.d.ts → index-B-pqGHhU.d.ts} +113 -19
  56. package/dist/{index-Bc5c7Ty5.d.ts → index-DEyjGoo2.d.ts} +1 -1
  57. package/dist/{index-QSffbLnj.d.ts → index-VLDcI5NF.d.ts} +3 -2
  58. package/dist/index.d.ts +18 -15
  59. package/dist/index.js +39 -6722
  60. package/dist/llm-DYuR7_uX.d.ts +1069 -0
  61. package/dist/llm-anthropic-Cd_I2xXB.d.ts +67 -0
  62. package/dist/llm-anthropic.d.ts +2 -0
  63. package/dist/llm-anthropic.js +13 -0
  64. package/dist/llm-openai-DyFrucmV.d.ts +87 -0
  65. package/dist/llm-openai.d.ts +2 -0
  66. package/dist/llm-openai.js +19 -0
  67. package/dist/llm.d.ts +1 -1
  68. package/dist/llm.js +101 -356
  69. package/dist/mcp/index.d.ts +2 -1
  70. package/dist/mcp/index.js +155 -1453
  71. package/dist/{netpolicy-asvCfwi8.d.ts → netpolicy-Brx5XZUB.d.ts} +16 -3
  72. package/dist/netpolicy.d.ts +1 -1
  73. package/dist/netpolicy.js +25 -118
  74. package/dist/{output-4aXs91GY.d.ts → output-CjZWmL8s.d.ts} +28 -4
  75. package/dist/output.d.ts +2 -2
  76. package/dist/output.js +137 -971
  77. package/dist/progress.js +6 -55
  78. package/dist/provenance.js +6 -31
  79. package/dist/routellm-B0nv59mH.d.ts +111 -0
  80. package/dist/routellm.d.ts +2 -2
  81. package/dist/routellm.js +13 -255
  82. package/dist/{rpc-D5xLEf8V.d.ts → rpc-D31Lg0Kc.d.ts} +1 -1
  83. package/dist/rpc.d.ts +2 -2
  84. package/dist/rpc.js +4 -98
  85. package/dist/safety.js +6 -43
  86. package/dist/scope.js +20 -377
  87. package/dist/{serve-DE-jscpb.d.ts → serve-D8yWMrTc.d.ts} +2 -2
  88. package/dist/serve.d.ts +2 -2
  89. package/dist/serve.js +75 -1019
  90. package/dist/sqlstore.js +6 -39
  91. package/dist/stream.js +8 -51
  92. package/dist/telemetry/index.js +24 -608
  93. package/dist/tui/index.d.ts +5 -5
  94. package/dist/tui/index.js +17 -374
  95. package/dist/upgrade.js +6 -114
  96. package/dist/xdg.js +14 -128
  97. package/package.json +130 -25
  98. package/src/README.md +15 -0
  99. package/src/cli.help-command.test.ts +105 -0
  100. package/src/cli.test.ts +95 -1
  101. package/src/cli.ts +78 -13
  102. package/src/completion-values.test.ts +10 -10
  103. package/src/compliance.test.ts +196 -2
  104. package/src/compliance.ts +440 -20
  105. package/src/config.test.ts +187 -1
  106. package/src/config.ts +173 -7
  107. package/src/gen/crud_pb.ts +7 -7
  108. package/src/id/README.md +38 -0
  109. package/src/id/contract.test.ts +1 -1
  110. package/src/id/index.test.ts +1 -1
  111. package/src/id/index.ts +3 -2
  112. package/src/index.test.ts +12 -0
  113. package/src/index.ts +2 -0
  114. package/src/llm-anthropic.conformance.test.ts +1135 -0
  115. package/src/llm-anthropic.test.ts +152 -0
  116. package/src/llm-anthropic.ts +940 -0
  117. package/src/llm-choice-hermetic.test.ts +335 -0
  118. package/src/llm-config.test.ts +232 -0
  119. package/src/llm-config.ts +106 -0
  120. package/src/llm-endpoint.ts +254 -0
  121. package/src/llm-error.ts +17 -0
  122. package/src/llm-errors.ts +152 -0
  123. package/src/llm-file.ts +126 -0
  124. package/src/llm-http.ts +317 -0
  125. package/src/llm-keys.ts +167 -0
  126. package/src/llm-media.conformance.test.ts +87 -0
  127. package/src/llm-media.test.ts +279 -0
  128. package/src/llm-media.ts +322 -0
  129. package/src/llm-openai.conformance.test.ts +407 -0
  130. package/src/llm-openai.test.ts +510 -0
  131. package/src/llm-openai.ts +708 -0
  132. package/src/llm-payload.ts +155 -0
  133. package/src/llm-quote.ts +46 -0
  134. package/src/llm-routellm.conformance.test.ts +44 -0
  135. package/src/llm-toolnames.conformance.test.ts +112 -0
  136. package/src/llm-toolnames.test.ts +132 -0
  137. package/src/llm-toolnames.ts +298 -0
  138. package/src/llm-uri.ts +99 -0
  139. package/src/llm.test.ts +43 -15
  140. package/src/llm.toolcall.test.ts +175 -0
  141. package/src/llm.ts +602 -190
  142. package/src/mcp/README.md +57 -0
  143. package/src/mcp/conformance.test.ts +24 -3
  144. package/src/mcp/dispatch.ts +4 -4
  145. package/src/mcp/identity.test.ts +166 -0
  146. package/src/mcp/identity.ts +72 -0
  147. package/src/mcp/index.ts +6 -0
  148. package/src/mcp/legacy.ts +21 -8
  149. package/src/mcp/mcp.test.ts +14 -4
  150. package/src/mcp/modern-confirm.ts +1 -1
  151. package/src/mcp/modern.ts +12 -11
  152. package/src/mcp/tasks.ts +1 -1
  153. package/src/mcp/types.ts +80 -8
  154. package/src/netpolicy.test.ts +78 -0
  155. package/src/netpolicy.ts +28 -3
  156. package/src/output/README.md +46 -0
  157. package/src/output/builtins.ts +5 -5
  158. package/src/output/error.ts +144 -1
  159. package/src/output/formatters/README.md +50 -0
  160. package/src/output.ts +24 -1
  161. package/src/published-types.test.ts +115 -0
  162. package/src/routellm.test.ts +209 -28
  163. package/src/routellm.toolcall.test.ts +521 -0
  164. package/src/routellm.ts +213 -177
  165. package/src/router/README.md +44 -0
  166. package/src/router/server.test.ts +41 -0
  167. package/src/router/server.ts +11 -0
  168. package/src/safety.test.ts +19 -9
  169. package/src/scope-defaults.json +29 -29
  170. package/src/serve.ts +1 -1
  171. package/src/telemetry/README.md +48 -0
  172. package/src/telemetry/client.ts +5 -5
  173. package/src/toolspec.test.ts +2 -2
  174. package/src/triton/README.md +34 -0
  175. package/src/tui/README.md +42 -0
  176. package/src/wizard.test.ts +7 -7
  177. package/test/llm/conformance-helpers.ts +100 -0
  178. package/test/output/dispatch.test.ts +1 -2
  179. package/test/output/error.test.ts +33 -2
  180. package/test/output/smoke-capture.ts +2 -2
  181. package/test/output/smoke.test.ts +2 -2
  182. package/test/output/taxonomy-contract.test.ts +148 -0
  183. package/test/setup-env.ts +16 -0
  184. package/tsconfig.json +3 -1
  185. package/tsconfig.test.json +10 -0
  186. package/vitest.config.ts +9 -0
  187. package/dist/llm-B5XUWH8T.d.ts +0 -231
  188. package/dist/routellm-PGSt2D1y.d.ts +0 -75
@@ -0,0 +1,115 @@
1
+ /**
2
+ * Consumer-side type-check of the published declarations.
3
+ *
4
+ * The package's own `tsc` run only ever sees `src/`; what adopters get is
5
+ * the bundled `dist/*.d.ts` that tsup emits. The bundler can drop imports
6
+ * or turn a type into a value while rolling declarations into shared
7
+ * chunks, and neither `src/` type-checking nor `skipLibCheck: true`
8
+ * consumers would notice. This test builds the package into a staged
9
+ * `node_modules/@hop-top/kit`, then type-checks a consumer importing
10
+ * every `exports` subpath with `skipLibCheck: false` under each
11
+ * `moduleResolution` mode adopters use. node16 is the exception: kit's
12
+ * CommonJS declarations import ESM-only commander, which node16 reports
13
+ * as TS1479 in the declarations themselves, so node16 consumers need
14
+ * `skipLibCheck` (the documented migration is nodenext or bundler).
15
+ */
16
+ import { afterAll, beforeAll, describe, expect, it } from 'vitest';
17
+ import { execFileSync } from 'node:child_process';
18
+ import { copyFileSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
19
+ import * as path from 'node:path';
20
+ import * as ts from 'typescript';
21
+
22
+ const pkgDir = path.join(__dirname, '..');
23
+ const pkg = JSON.parse(readFileSync(path.join(pkgDir, 'package.json'), 'utf8')) as {
24
+ name: string;
25
+ exports: Record<string, unknown>;
26
+ scripts: Record<string, string>;
27
+ };
28
+
29
+ // Inside the package rather than the OS temp dir: the staged package's
30
+ // declarations import commander, zod, @types/node and friends, which
31
+ // only resolve from a path under sdk/ts/node_modules' reach.
32
+ const stageDir = path.join(pkgDir, `.consumer-types-${process.pid}`);
33
+ const stagedPkgDir = path.join(stageDir, 'node_modules', ...pkg.name.split('/'));
34
+
35
+ type Mode = Pick<ts.CompilerOptions, 'module' | 'moduleResolution' | 'skipLibCheck'>;
36
+
37
+ const modes: Record<string, Mode> = {
38
+ // node10 ignores `exports`; subpaths resolve only through `typesVersions`.
39
+ node10: { module: ts.ModuleKind.CommonJS, moduleResolution: ts.ModuleResolutionKind.Node10, skipLibCheck: false },
40
+ // Resolution only: TS1479 on commander inside the declarations is expected.
41
+ node16: { module: ts.ModuleKind.Node16, moduleResolution: ts.ModuleResolutionKind.Node16, skipLibCheck: true },
42
+ nodenext: { module: ts.ModuleKind.NodeNext, moduleResolution: ts.ModuleResolutionKind.NodeNext, skipLibCheck: false },
43
+ bundler: { module: ts.ModuleKind.ESNext, moduleResolution: ts.ModuleResolutionKind.Bundler, skipLibCheck: false },
44
+ };
45
+
46
+ /** Consumer source: one namespace import per exports subpath, plus typed uses. */
47
+ function consumerSource(): string {
48
+ const specs = Object.keys(pkg.exports).map((k) => (k === '.' ? pkg.name : `${pkg.name}/${k.slice(2)}`));
49
+ const imports = specs.map((s, i) => `import * as m${i} from '${s}';`).join('\n');
50
+ return `${imports}
51
+ import type { CliError } from '${pkg.name}/output';
52
+ import { output } from '${pkg.name}';
53
+
54
+ // Types re-exported through the root barrel's namespaces must stay types.
55
+ export const viaSubpath: CliError | null = output.wrapError(new Error('x'), output.CODE_GENERIC, output.EXIT_GENERIC);
56
+ export const viaNamespace: output.CliError | null = viaSubpath;
57
+ export const all = [${specs.map((_, i) => `m${i}`).join(', ')}];
58
+ `;
59
+ }
60
+
61
+ describe('published declarations', () => {
62
+ const files: string[] = [];
63
+
64
+ beforeAll(() => {
65
+ rmSync(stageDir, { recursive: true, force: true });
66
+ mkdirSync(stagedPkgDir, { recursive: true });
67
+ copyFileSync(path.join(pkgDir, 'package.json'), path.join(stagedPkgDir, 'package.json'));
68
+
69
+ // Run the real build script, redirected into the staged package.
70
+ const [bin, ...args] = pkg.scripts.build.split(/\s+/);
71
+ expect(bin).toBe('tsup');
72
+ execFileSync(
73
+ path.join(pkgDir, 'node_modules', '.bin', 'tsup'),
74
+ [...args, '--out-dir', path.join(stagedPkgDir, 'dist'), '--silent'],
75
+ { cwd: pkgDir, stdio: 'pipe' },
76
+ );
77
+
78
+ // Own package.json: without it the consumer's nearest manifest is
79
+ // sdk/ts's, and a self-reference import would resolve the package
80
+ // to ./dist instead of the staged build.
81
+ writeFileSync(path.join(stageDir, 'package.json'), '{ "name": "consumer", "private": true }\n');
82
+
83
+ // Both module formats: .mts is an ESM consumer, .cts a CJS one.
84
+ const src = consumerSource();
85
+ for (const ext of ['mts', 'cts']) {
86
+ const file = path.join(stageDir, `consumer.${ext}`);
87
+ writeFileSync(file, src);
88
+ files.push(file);
89
+ }
90
+ }, 120_000);
91
+
92
+ afterAll(() => { rmSync(stageDir, { recursive: true, force: true }); });
93
+
94
+ it.each(Object.entries(modes).map(([name, m]) => [name, m.skipLibCheck] as const))(
95
+ 'type-check clean (moduleResolution %s, skipLibCheck %s)', (mode) => {
96
+ const program = ts.createProgram(files, {
97
+ ...modes[mode],
98
+ target: ts.ScriptTarget.ES2022,
99
+ strict: true,
100
+ noEmit: true,
101
+ // `tsc --init` default; node16/nodenext imply it. Without it a
102
+ // node10 consumer trips on zod's own default imports (TS1259).
103
+ esModuleInterop: true,
104
+ types: ['node'],
105
+ typeRoots: [path.join(pkgDir, 'node_modules', '@types')],
106
+ });
107
+ const diagnostics = ts.getPreEmitDiagnostics(program);
108
+ const report = ts.formatDiagnostics(diagnostics, {
109
+ getCanonicalFileName: (f) => f,
110
+ getCurrentDirectory: () => stageDir,
111
+ getNewLine: () => '\n',
112
+ });
113
+ expect(report).toBe('');
114
+ }, 60_000);
115
+ });
@@ -3,55 +3,192 @@
3
3
  * Covers: parseModelField, parseRouterConfig, scheme registration.
4
4
  */
5
5
 
6
- import { describe, it, expect, beforeEach, afterEach } from "vitest";
6
+ import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
7
7
  import { resolve } from "./llm";
8
- import { parseModelField, parseRouterConfig } from "./routellm";
8
+ import { RouteLLMAdapter, parseModelField, parseRouterConfig } from "./routellm";
9
9
 
10
10
  // ─── parseModelField ──────────────────────────────────────────────────────────
11
11
 
12
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);
13
+ // Router pair: the text after the last colon parses as a finite float.
14
+ const pairs: [string, string, string, number][] = [
15
+ ["valid", "mf:0.7", "mf", 0.7],
16
+ ["zero", "bert:0", "bert", 0],
17
+ ["one", "causal_llm:1", "causal_llm", 1],
18
+ ["precise", "mf:0.123456", "mf", 0.123456],
19
+ ["exponent", "mf:5e-1", "mf", 0.5],
20
+ ["leading dot", "mf:.5", "mf", 0.5],
21
+ ["trailing dot", "mf:1.", "mf", 1],
22
+ ["hex float", "mf:0x1p-1", "mf", 0.5],
23
+ ["colon in router name", "a:b:0.5", "a:b", 0.5],
24
+ ["out of range still a pair", "mf:1.5", "mf", 1.5],
25
+ // Go ParseFloat syntax, probed against strconv.
26
+ ["upper-case hex float", "mf:0X.8P1", "mf", 1],
27
+ ["underscore after hex prefix", "mf:0x_1p-1", "mf", 0.5],
28
+ ["underscore between digits", "mf:1_0", "mf", 10],
29
+ ["underscore in exponent", "mf:1e+0_1", "mf", 10],
30
+ ["leading zeros", "mf:00.5", "mf", 0.5],
31
+ ["underflow to zero", "mf:1e-400", "mf", 0],
32
+ ];
33
+ for (const [name, model, router, threshold] of pairs) {
34
+ it(`router pair: ${name}`, () => {
35
+ const r = parseModelField(model);
36
+ expect(r.isTier).toBe(false);
37
+ expect(r.tier).toBe("");
38
+ expect(r.routerName).toBe(router);
39
+ expect(r.threshold).toBe(threshold);
40
+ });
41
+ }
42
+
43
+ // Tier: everything else non-empty, verbatim.
44
+ const tiers: [string, string][] = [
45
+ ["bare tier", "private"],
46
+ ["underscore tier", "coding_fast"],
47
+ ["server router id", "router-mf-0.5"],
48
+ ["non-numeric suffix", "mf:abc"],
49
+ ["colon in tier name", "a:b"],
50
+ ["empty name", ":0.5"],
51
+ ["empty threshold", "mf:"],
52
+ ["NaN suffix", "mf:NaN"],
53
+ ["Inf suffix", "mf:Inf"],
54
+ ["Infinity suffix", "mf:Infinity"],
55
+ ["overflowing suffix", "mf:1e400"],
56
+ ["whitespace suffix", "mf: 0.5"],
57
+ ["hex integer suffix", "mf:0x10"],
58
+ ["empty-after-sign suffix", "mf:+"],
59
+ ["double underscore", "mf:1__0"],
60
+ ["leading underscore", "mf:_1"],
61
+ ["trailing underscore", "mf:1_"],
62
+ ["underscore before dot", "mf:1_.5"],
63
+ ["underscore before hex exponent", "mf:0x1_p0"],
64
+ ["underscore inside hex prefix", "mf:0_x1p0"],
65
+ ];
66
+ for (const [name, model] of tiers) {
67
+ it(`tier: ${name}`, () => {
68
+ const r = parseModelField(model);
69
+ expect(r.isTier).toBe(true);
70
+ expect(r.tier).toBe(model);
71
+ expect(r.routerName).toBe("");
72
+ expect(r.threshold).toBe(0);
73
+ });
74
+ }
75
+
76
+ it("throws on empty string", () => {
77
+ expect(() => parseModelField("")).toThrow(
78
+ "model field is required (router_name:threshold or tier name)"
79
+ );
17
80
  });
81
+ });
82
+
83
+ // ─── Adapter: model field on the wire ─────────────────────────────────────────
18
84
 
19
- it("parses zero threshold", () => {
20
- const r = parseModelField("bert:0");
21
- expect(r.routerName).toBe("bert");
22
- expect(r.threshold).toBe(0);
85
+ describe("RouteLLMAdapter model field", () => {
86
+ const origFetch = globalThis.fetch;
87
+ let sent: string[];
88
+
89
+ beforeEach(() => {
90
+ sent = [];
91
+ globalThis.fetch = vi.fn(async (_url: unknown, init?: RequestInit) => {
92
+ sent.push((JSON.parse(String(init?.body)) as { model: string }).model);
93
+ return new globalThis.Response(
94
+ JSON.stringify({
95
+ choices: [
96
+ { message: { role: "assistant", content: "ok" }, finish_reason: "stop" },
97
+ ],
98
+ }),
99
+ { status: 200, headers: { "Content-Type": "application/json" } }
100
+ );
101
+ }) as unknown as typeof globalThis.fetch;
23
102
  });
24
103
 
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);
104
+ afterEach(() => {
105
+ globalThis.fetch = origFetch;
29
106
  });
30
107
 
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);
108
+ function adapter(model: string, uriOnly = false): RouteLLMAdapter {
109
+ return new RouteLLMAdapter({
110
+ uri: { scheme: "routellm", model },
111
+ provider: {
112
+ model: uriOnly ? "" : model,
113
+ apiKey: "test-key",
114
+ baseURL: "http://127.0.0.1:6060",
115
+ },
116
+ fallbacks: [],
117
+ hermetic: true,
118
+ });
119
+ }
120
+
121
+ const wire: [string, string, string][] = [
122
+ // Router pair: translated to the server's router id.
123
+ ["router pair", "mf:0.7", "router-mf-0.7"],
124
+ ["threshold zero", "bert:0", "router-bert-0"],
125
+ ["threshold one", "causal_llm:1", "router-causal_llm-1"],
126
+ ["trailing zeros trimmed", "mf:0.50", "router-mf-0.5"],
127
+ ["exponent expanded", "mf:5e-1", "router-mf-0.5"],
128
+ ["tiny threshold without exponent", "mf:1e-7", "router-mf-0.0000001"],
129
+ ["negative zero", "mf:-0", "router-mf--0"],
130
+ // Tier: verbatim, server resolves router and threshold.
131
+ ["tier", "private", "private"],
132
+ ["tier underscore", "coding_fast", "coding_fast"],
133
+ ["tier server router id", "router-mf-0.5", "router-mf-0.5"],
134
+ ["tier with colon", "a:b", "a:b"],
135
+ ];
136
+ for (const [name, model, want] of wire) {
137
+ it(`sends ${name} as ${want}`, async () => {
138
+ const a = adapter(model);
139
+ await a.complete({ messages: [{ role: "user", content: "hi" }] });
140
+ expect(sent).toEqual([want]);
141
+ a.close();
142
+ });
143
+ }
144
+
145
+ it("exposes a tier", () => {
146
+ const a = adapter("private");
147
+ expect(a.isTier()).toBe(true);
148
+ expect(a.tier).toBe("private");
149
+ expect(a.routerName).toBe("");
150
+ expect(a.threshold).toBe(0);
151
+ a.close();
35
152
  });
36
153
 
37
- it("throws on empty string", () => {
38
- expect(() => parseModelField("")).toThrow("model field is required");
154
+ it("exposes a router pair", () => {
155
+ const a = adapter("mf:0");
156
+ expect(a.isTier()).toBe(false);
157
+ expect(a.tier).toBe("");
158
+ expect(a.routerName).toBe("mf");
159
+ expect(a.threshold).toBe(0);
160
+ a.close();
161
+ });
162
+
163
+ it("falls back to the URI model", async () => {
164
+ const a = adapter("bert:0.5", true);
165
+ await a.complete({ messages: [{ role: "user", content: "hi" }] });
166
+ expect(sent).toEqual(["router-bert-0.5"]);
167
+ a.close();
39
168
  });
40
169
 
41
- it("throws on missing colon", () => {
42
- expect(() => parseModelField("mf")).toThrow("expected format");
170
+ it("rejects an empty model", () => {
171
+ expect(() => adapter("")).toThrow(
172
+ "routellm: model field is required (router_name:threshold or tier name)"
173
+ );
43
174
  });
44
175
 
45
- it("throws on empty router name", () => {
46
- expect(() => parseModelField(":0.5")).toThrow("expected format");
176
+ it("rejects a threshold below range", () => {
177
+ expect(() => adapter("mf:-0.1")).toThrow(
178
+ "routellm: threshold -0.1000 out of range [0, 1]"
179
+ );
47
180
  });
48
181
 
49
- it("throws on empty threshold", () => {
50
- expect(() => parseModelField("mf:")).toThrow("expected format");
182
+ it("rejects a threshold above range", () => {
183
+ expect(() => adapter("mf:1.5")).toThrow(
184
+ "routellm: threshold 1.5000 out of range [0, 1]"
185
+ );
51
186
  });
52
187
 
53
- it("throws on non-numeric threshold", () => {
54
- expect(() => parseModelField("mf:abc")).toThrow("invalid threshold");
188
+ it("resolves a tier URI", () => {
189
+ const p = resolve("routellm://private");
190
+ expect(p).toBeDefined();
191
+ p.close();
55
192
  });
56
193
  });
57
194
 
@@ -157,6 +294,50 @@ describe("parseRouterConfig", () => {
157
294
  });
158
295
  });
159
296
 
297
+ // ─── Base URL precedence ──────────────────────────────────────────────────────
298
+
299
+ describe("RouteLLMAdapter base URL", () => {
300
+ const saved = process.env["ROUTELLM_BASE_URL"];
301
+ afterEach(() => {
302
+ if (saved === undefined) delete process.env["ROUTELLM_BASE_URL"];
303
+ else process.env["ROUTELLM_BASE_URL"] = saved;
304
+ });
305
+
306
+ const adapter = (baseURL?: string, hermetic = false) =>
307
+ new RouteLLMAdapter({
308
+ uri: { scheme: "routellm", model: "mf:0.5" },
309
+ provider: {
310
+ model: "mf:0.5",
311
+ baseURL,
312
+ extras: { routellm: { base_url: "http://extras.test:2" } },
313
+ },
314
+ fallbacks: [],
315
+ hermetic,
316
+ });
317
+
318
+ it("provider base URL beats extras and ROUTELLM_BASE_URL", () => {
319
+ process.env["ROUTELLM_BASE_URL"] = "http://env.test:3";
320
+ expect(adapter("http://uri.test:1").baseURL).toBe("http://uri.test:1/v1");
321
+ });
322
+
323
+ it("provider base URL equal to the default still wins", () => {
324
+ process.env["ROUTELLM_BASE_URL"] = "http://env.test:3";
325
+ expect(adapter("http://localhost:6060").baseURL).toBe("http://localhost:6060/v1");
326
+ });
327
+
328
+ it("router config applies without a provider base URL", () => {
329
+ process.env["ROUTELLM_BASE_URL"] = "http://env.test:3";
330
+ expect(adapter().baseURL).toBe("http://env.test:3/v1");
331
+ delete process.env["ROUTELLM_BASE_URL"];
332
+ expect(adapter().baseURL).toBe("http://extras.test:2/v1");
333
+ });
334
+
335
+ it("hermetic ignores ROUTELLM_BASE_URL", () => {
336
+ process.env["ROUTELLM_BASE_URL"] = "http://env.test:3";
337
+ expect(adapter(undefined, true).baseURL).toBe("http://extras.test:2/v1");
338
+ });
339
+ });
340
+
160
341
  // ─── Scheme registration ──────────────────────────────────────────────────────
161
342
 
162
343
  describe("routellm scheme registration", () => {