@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,183 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from "vitest";
|
|
2
|
+
import { Controller, parseModelName } from "./controller";
|
|
3
|
+
import { RoutingError } from "./router";
|
|
4
|
+
import type { Router, Middleware, ModelPair } from "./router";
|
|
5
|
+
|
|
6
|
+
// ─── Helpers ─────────────────────────────────────────────────────────────────
|
|
7
|
+
|
|
8
|
+
function fixedRouter(value: number): Router {
|
|
9
|
+
return { score: vi.fn(async () => value) };
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function overrideMiddleware(pair: ModelPair): Middleware {
|
|
13
|
+
return { getModelPair: vi.fn(async () => pair) };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function nullMiddleware(): Middleware {
|
|
17
|
+
return { getModelPair: vi.fn(async () => null) };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// ─── parseModelName ──────────────────────────────────────────────────────────
|
|
21
|
+
|
|
22
|
+
describe("parseModelName", () => {
|
|
23
|
+
it("parses router-mf-0.7", () => {
|
|
24
|
+
const r = parseModelName("router-mf-0.7");
|
|
25
|
+
expect(r.routerName).toBe("mf");
|
|
26
|
+
expect(r.threshold).toBe(0.7);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("parses router-sw_ranking-0.5", () => {
|
|
30
|
+
const r = parseModelName("router-sw_ranking-0.5");
|
|
31
|
+
expect(r.routerName).toBe("sw_ranking");
|
|
32
|
+
expect(r.threshold).toBe(0.5);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("parses threshold 0 and 1", () => {
|
|
36
|
+
expect(parseModelName("router-bert-0").threshold).toBe(0);
|
|
37
|
+
expect(parseModelName("router-bert-1").threshold).toBe(1);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("throws when prefix is missing", () => {
|
|
41
|
+
expect(() => parseModelName("mf-0.5")).toThrow(RoutingError);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("throws when threshold is missing", () => {
|
|
45
|
+
expect(() => parseModelName("router-mf")).toThrow(RoutingError);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("throws on non-numeric threshold", () => {
|
|
49
|
+
expect(() => parseModelName("router-mf-abc")).toThrow(RoutingError);
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// ─── Controller ──────────────────────────────────────────────────────────────
|
|
54
|
+
|
|
55
|
+
describe("Controller", () => {
|
|
56
|
+
it("routes to strong model when score >= threshold", async () => {
|
|
57
|
+
const ctrl = new Controller({
|
|
58
|
+
strongModel: "gpt-4",
|
|
59
|
+
weakModel: "gpt-3.5",
|
|
60
|
+
routers: { mf: fixedRouter(0.8) },
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
const model = await ctrl.route("hello", "mf", 0.5);
|
|
64
|
+
expect(model).toBe("gpt-4");
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("routes to weak model when score < threshold", async () => {
|
|
68
|
+
const ctrl = new Controller({
|
|
69
|
+
strongModel: "gpt-4",
|
|
70
|
+
weakModel: "gpt-3.5",
|
|
71
|
+
routers: { mf: fixedRouter(0.3) },
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
const model = await ctrl.route("hello", "mf", 0.5);
|
|
75
|
+
expect(model).toBe("gpt-3.5");
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("throws on unknown router", async () => {
|
|
79
|
+
const ctrl = new Controller({
|
|
80
|
+
strongModel: "gpt-4",
|
|
81
|
+
weakModel: "gpt-3.5",
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
await expect(ctrl.route("hi", "missing", 0.5)).rejects.toThrow(
|
|
85
|
+
RoutingError,
|
|
86
|
+
);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("throws on invalid threshold", async () => {
|
|
90
|
+
const ctrl = new Controller({
|
|
91
|
+
strongModel: "gpt-4",
|
|
92
|
+
weakModel: "gpt-3.5",
|
|
93
|
+
routers: { mf: fixedRouter(0.5) },
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
await expect(ctrl.route("hi", "mf", 1.5)).rejects.toThrow(
|
|
97
|
+
RoutingError,
|
|
98
|
+
);
|
|
99
|
+
await expect(ctrl.route("hi", "mf", -0.1)).rejects.toThrow(
|
|
100
|
+
RoutingError,
|
|
101
|
+
);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("tracks model counts", async () => {
|
|
105
|
+
const ctrl = new Controller({
|
|
106
|
+
strongModel: "gpt-4",
|
|
107
|
+
weakModel: "gpt-3.5",
|
|
108
|
+
routers: { mf: fixedRouter(0.8) },
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
await ctrl.route("a", "mf", 0.5);
|
|
112
|
+
await ctrl.route("b", "mf", 0.5);
|
|
113
|
+
const counts = ctrl.modelCounts.get("mf")!;
|
|
114
|
+
expect(counts.get("gpt-4")).toBe(2);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("addRouter registers a new router", async () => {
|
|
118
|
+
const ctrl = new Controller({
|
|
119
|
+
strongModel: "gpt-4",
|
|
120
|
+
weakModel: "gpt-3.5",
|
|
121
|
+
});
|
|
122
|
+
ctrl.addRouter("test", fixedRouter(1));
|
|
123
|
+
expect(ctrl.routerNames()).toContain("test");
|
|
124
|
+
const m = await ctrl.route("hi", "test", 0.5);
|
|
125
|
+
expect(m).toBe("gpt-4");
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it("routeByModelName delegates correctly", async () => {
|
|
129
|
+
const ctrl = new Controller({
|
|
130
|
+
strongModel: "gpt-4",
|
|
131
|
+
weakModel: "gpt-3.5",
|
|
132
|
+
routers: { bert: fixedRouter(0.9) },
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
const m = await ctrl.routeByModelName("hi", "router-bert-0.5");
|
|
136
|
+
expect(m).toBe("gpt-4");
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it("middleware can override model pair", async () => {
|
|
140
|
+
const custom: ModelPair = {
|
|
141
|
+
strong: "claude-3",
|
|
142
|
+
weak: "mixtral",
|
|
143
|
+
};
|
|
144
|
+
const ctrl = new Controller({
|
|
145
|
+
strongModel: "gpt-4",
|
|
146
|
+
weakModel: "gpt-3.5",
|
|
147
|
+
routers: { mf: fixedRouter(0.9) },
|
|
148
|
+
middleware: [overrideMiddleware(custom)],
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
const m = await ctrl.route("hi", "mf", 0.5);
|
|
152
|
+
expect(m).toBe("claude-3");
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it("null-returning middleware keeps default pair", async () => {
|
|
156
|
+
const ctrl = new Controller({
|
|
157
|
+
strongModel: "gpt-4",
|
|
158
|
+
weakModel: "gpt-3.5",
|
|
159
|
+
routers: { mf: fixedRouter(0.9) },
|
|
160
|
+
middleware: [nullMiddleware()],
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
const m = await ctrl.route("hi", "mf", 0.5);
|
|
164
|
+
expect(m).toBe("gpt-4");
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it("last middleware wins", async () => {
|
|
168
|
+
const first: ModelPair = { strong: "a", weak: "b" };
|
|
169
|
+
const second: ModelPair = { strong: "x", weak: "y" };
|
|
170
|
+
const ctrl = new Controller({
|
|
171
|
+
strongModel: "gpt-4",
|
|
172
|
+
weakModel: "gpt-3.5",
|
|
173
|
+
routers: { mf: fixedRouter(0.1) },
|
|
174
|
+
middleware: [
|
|
175
|
+
overrideMiddleware(first),
|
|
176
|
+
overrideMiddleware(second),
|
|
177
|
+
],
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
const m = await ctrl.route("hi", "mf", 0.5);
|
|
181
|
+
expect(m).toBe("y"); // weak, score=0.1 < 0.5
|
|
182
|
+
});
|
|
183
|
+
});
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Controller — router registry, threshold parsing, model routing.
|
|
3
|
+
*
|
|
4
|
+
* Manages a set of named routers and middleware. Routes prompts by
|
|
5
|
+
* scoring them against a router and comparing to a threshold.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import {
|
|
9
|
+
type Router,
|
|
10
|
+
type Middleware,
|
|
11
|
+
type ModelPair,
|
|
12
|
+
RoutingError,
|
|
13
|
+
route,
|
|
14
|
+
} from "./router";
|
|
15
|
+
|
|
16
|
+
// ─── Types ───────────────────────────────────────────────────────────────────
|
|
17
|
+
|
|
18
|
+
export interface ControllerOptions {
|
|
19
|
+
/** Default strong model name. */
|
|
20
|
+
strongModel: string;
|
|
21
|
+
/** Default weak model name. */
|
|
22
|
+
weakModel: string;
|
|
23
|
+
/** Named routers to register. */
|
|
24
|
+
routers?: Record<string, Router>;
|
|
25
|
+
/** Middleware applied in order before routing. */
|
|
26
|
+
middleware?: Middleware[];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Parsed model string: "router-<name>-<threshold>". */
|
|
30
|
+
export interface ParsedModelName {
|
|
31
|
+
routerName: string;
|
|
32
|
+
threshold: number;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// ─── Controller ──────────────────────────────────────────────────────────────
|
|
36
|
+
|
|
37
|
+
export class Controller {
|
|
38
|
+
readonly defaultPair: ModelPair;
|
|
39
|
+
private readonly _routers: Map<string, Router>;
|
|
40
|
+
private readonly _middleware: Middleware[];
|
|
41
|
+
/** Per-router, per-model call counts (for observability). */
|
|
42
|
+
readonly modelCounts: Map<string, Map<string, number>>;
|
|
43
|
+
|
|
44
|
+
constructor(opts: ControllerOptions) {
|
|
45
|
+
this.defaultPair = {
|
|
46
|
+
strong: opts.strongModel,
|
|
47
|
+
weak: opts.weakModel,
|
|
48
|
+
};
|
|
49
|
+
this._routers = new Map(Object.entries(opts.routers ?? {}));
|
|
50
|
+
this._middleware = opts.middleware ?? [];
|
|
51
|
+
this.modelCounts = new Map();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Register a router under a name. */
|
|
55
|
+
addRouter(name: string, router: Router): void {
|
|
56
|
+
this._routers.set(name, router);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** List registered router names. */
|
|
60
|
+
routerNames(): string[] {
|
|
61
|
+
return [...this._routers.keys()];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Route a prompt: score it with the named router and pick a model.
|
|
66
|
+
*
|
|
67
|
+
* @returns The chosen model name (strong or weak).
|
|
68
|
+
*/
|
|
69
|
+
async route(
|
|
70
|
+
prompt: string,
|
|
71
|
+
routerName: string,
|
|
72
|
+
threshold: number,
|
|
73
|
+
): Promise<string> {
|
|
74
|
+
this._validate(routerName, threshold);
|
|
75
|
+
|
|
76
|
+
const pair = await this._resolveModelPair(prompt);
|
|
77
|
+
const router = this._routers.get(routerName)!;
|
|
78
|
+
const winRate = await router.score(prompt);
|
|
79
|
+
const model = route(winRate, threshold, pair);
|
|
80
|
+
|
|
81
|
+
// Track counts.
|
|
82
|
+
if (!this.modelCounts.has(routerName)) {
|
|
83
|
+
this.modelCounts.set(routerName, new Map());
|
|
84
|
+
}
|
|
85
|
+
const counts = this.modelCounts.get(routerName)!;
|
|
86
|
+
counts.set(model, (counts.get(model) ?? 0) + 1);
|
|
87
|
+
|
|
88
|
+
return model;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Route using a model string of the form "router-<name>-<threshold>".
|
|
93
|
+
*
|
|
94
|
+
* This mirrors the Python server's model-name convention.
|
|
95
|
+
*/
|
|
96
|
+
async routeByModelName(
|
|
97
|
+
prompt: string,
|
|
98
|
+
modelName: string,
|
|
99
|
+
): Promise<string> {
|
|
100
|
+
const { routerName, threshold } = parseModelName(modelName);
|
|
101
|
+
return this.route(prompt, routerName, threshold);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// ── Helpers ──────────────────────────────────────────────────────
|
|
105
|
+
|
|
106
|
+
private _validate(routerName: string, threshold: number): void {
|
|
107
|
+
if (!this._routers.has(routerName)) {
|
|
108
|
+
throw new RoutingError(
|
|
109
|
+
`Invalid router "${routerName}". ` +
|
|
110
|
+
`Available: ${this.routerNames().join(", ")}`,
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
if (threshold < 0 || threshold > 1) {
|
|
114
|
+
throw new RoutingError(
|
|
115
|
+
`Invalid threshold ${threshold}. Must be in [0, 1].`,
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
private async _resolveModelPair(prompt: string): Promise<ModelPair> {
|
|
121
|
+
let pair = this.defaultPair;
|
|
122
|
+
for (const mw of this._middleware) {
|
|
123
|
+
const override = await mw.getModelPair(prompt);
|
|
124
|
+
if (override) pair = override;
|
|
125
|
+
}
|
|
126
|
+
return pair;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// ─── Model name parser ──────────────────────────────────────────────────────
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Parse a model name of the form "router-<name>-<threshold>".
|
|
134
|
+
*
|
|
135
|
+
* Example: "router-mf-0.7" => { routerName: "mf", threshold: 0.7 }
|
|
136
|
+
*/
|
|
137
|
+
export function parseModelName(model: string): ParsedModelName {
|
|
138
|
+
if (!model.startsWith("router-")) {
|
|
139
|
+
throw new RoutingError(
|
|
140
|
+
`Invalid model "${model}". ` +
|
|
141
|
+
`Expected format: router-<name>-<threshold>.`,
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const rest = model.slice("router-".length);
|
|
146
|
+
const lastDash = rest.lastIndexOf("-");
|
|
147
|
+
if (lastDash < 1) {
|
|
148
|
+
throw new RoutingError(
|
|
149
|
+
`Invalid model "${model}". ` +
|
|
150
|
+
`Expected format: router-<name>-<threshold>.`,
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const routerName = rest.slice(0, lastDash);
|
|
155
|
+
const rawThreshold = rest.slice(lastDash + 1);
|
|
156
|
+
const threshold = Number(rawThreshold);
|
|
157
|
+
|
|
158
|
+
if (Number.isNaN(threshold)) {
|
|
159
|
+
throw new RoutingError(
|
|
160
|
+
`Invalid threshold "${rawThreshold}" in model "${model}".`,
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return { routerName, threshold };
|
|
165
|
+
}
|
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Integration tests for the router package.
|
|
3
|
+
*
|
|
4
|
+
* Tests the full request flow: Controller -> Router -> model selection.
|
|
5
|
+
* Uses in-memory routers and mocks — no external dependencies.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { describe, it, expect } from "vitest";
|
|
9
|
+
import { Controller, parseModelName } from "./controller";
|
|
10
|
+
import { RandomRouter } from "./random";
|
|
11
|
+
import { RoutingError } from "./router";
|
|
12
|
+
import type { Router, Middleware, ModelPair } from "./router";
|
|
13
|
+
|
|
14
|
+
// ─── Test helpers ────────────────────────────────────────────────────────────
|
|
15
|
+
|
|
16
|
+
/** Returns a fixed score (simulates Triton MF/BERT scorer). */
|
|
17
|
+
class FixedRouter implements Router {
|
|
18
|
+
constructor(private readonly _score: number) {}
|
|
19
|
+
async score(_prompt: string): Promise<number> {
|
|
20
|
+
return this._score;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Always throws an error (simulates scorer failure). */
|
|
25
|
+
class FailingRouter implements Router {
|
|
26
|
+
constructor(private readonly _err: Error) {}
|
|
27
|
+
async score(_prompt: string): Promise<number> {
|
|
28
|
+
throw this._err;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Overrides model pair when prompt contains trigger keyword. */
|
|
33
|
+
class IntentMiddleware implements Middleware {
|
|
34
|
+
constructor(
|
|
35
|
+
private readonly trigger: string,
|
|
36
|
+
private readonly pair: ModelPair,
|
|
37
|
+
) {}
|
|
38
|
+
|
|
39
|
+
async getModelPair(prompt: string): Promise<ModelPair | null> {
|
|
40
|
+
if (prompt.includes(this.trigger)) {
|
|
41
|
+
return this.pair;
|
|
42
|
+
}
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// ─── Full request flow ──────────────────────────────────────────────────────
|
|
48
|
+
|
|
49
|
+
describe("integration: full request flow", () => {
|
|
50
|
+
it("routes to strong model when score >= threshold", async () => {
|
|
51
|
+
const ctrl = new Controller({
|
|
52
|
+
strongModel: "gpt-4o",
|
|
53
|
+
weakModel: "gpt-4o-mini",
|
|
54
|
+
routers: { fixed: new FixedRouter(0.8) },
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
const model = await ctrl.route("test prompt", "fixed", 0.5);
|
|
58
|
+
expect(model).toBe("gpt-4o");
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("routes to weak model when score < threshold", async () => {
|
|
62
|
+
const ctrl = new Controller({
|
|
63
|
+
strongModel: "gpt-4o",
|
|
64
|
+
weakModel: "gpt-4o-mini",
|
|
65
|
+
routers: { fixed: new FixedRouter(0.3) },
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
const model = await ctrl.route("test prompt", "fixed", 0.5);
|
|
69
|
+
expect(model).toBe("gpt-4o-mini");
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
// ─── RandomRouter (no external deps) ────────────────────────────────────────
|
|
74
|
+
|
|
75
|
+
describe("integration: RandomRouter", () => {
|
|
76
|
+
it("distributes roughly evenly at threshold 0.5", async () => {
|
|
77
|
+
const ctrl = new Controller({
|
|
78
|
+
strongModel: "strong",
|
|
79
|
+
weakModel: "weak",
|
|
80
|
+
routers: { random: new RandomRouter() },
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
let strongCount = 0;
|
|
84
|
+
let weakCount = 0;
|
|
85
|
+
const n = 200;
|
|
86
|
+
|
|
87
|
+
for (let i = 0; i < n; i++) {
|
|
88
|
+
const model = await ctrl.route(`prompt ${i}`, "random", 0.5);
|
|
89
|
+
if (model === "strong") strongCount++;
|
|
90
|
+
else weakCount++;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// With random scores, expect roughly even split.
|
|
94
|
+
expect(strongCount).toBeGreaterThan(20);
|
|
95
|
+
expect(weakCount).toBeGreaterThan(20);
|
|
96
|
+
expect(strongCount + weakCount).toBe(n);
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
// ─── Mock Triton scorer (MF/BERT) ──────────────────────────────────────────
|
|
101
|
+
|
|
102
|
+
describe("integration: mock Triton scorer", () => {
|
|
103
|
+
it("high score routes to strong model", async () => {
|
|
104
|
+
const ctrl = new Controller({
|
|
105
|
+
strongModel: "gpt-4o",
|
|
106
|
+
weakModel: "gpt-4o-mini",
|
|
107
|
+
routers: { mf: new FixedRouter(0.9) },
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
const model = await ctrl.route("complex math problem", "mf", 0.7);
|
|
111
|
+
expect(model).toBe("gpt-4o");
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("low score routes to weak model", async () => {
|
|
115
|
+
const ctrl = new Controller({
|
|
116
|
+
strongModel: "gpt-4o",
|
|
117
|
+
weakModel: "gpt-4o-mini",
|
|
118
|
+
routers: { mf: new FixedRouter(0.3) },
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
const model = await ctrl.route("hi", "mf", 0.7);
|
|
122
|
+
expect(model).toBe("gpt-4o-mini");
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("BERT scorer behaves same as MF", async () => {
|
|
126
|
+
const ctrl = new Controller({
|
|
127
|
+
strongModel: "claude-opus",
|
|
128
|
+
weakModel: "claude-haiku",
|
|
129
|
+
routers: { bert: new FixedRouter(0.6) },
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
// Score 0.6 >= threshold 0.5 -> strong.
|
|
133
|
+
expect(await ctrl.route("test", "bert", 0.5)).toBe("claude-opus");
|
|
134
|
+
// Score 0.6 < threshold 0.7 -> weak.
|
|
135
|
+
expect(await ctrl.route("test", "bert", 0.7)).toBe("claude-haiku");
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
// ─── Intent middleware routing ──────────────────────────────────────────────
|
|
140
|
+
|
|
141
|
+
describe("integration: intent middleware", () => {
|
|
142
|
+
it("overrides model pair when trigger keyword present", async () => {
|
|
143
|
+
const mw = new IntentMiddleware("code review", {
|
|
144
|
+
strong: "claude-opus",
|
|
145
|
+
weak: "claude-haiku",
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
const ctrl = new Controller({
|
|
149
|
+
strongModel: "gpt-4o",
|
|
150
|
+
weakModel: "gpt-4o-mini",
|
|
151
|
+
routers: { fixed: new FixedRouter(0.8) },
|
|
152
|
+
middleware: [mw],
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
// Without trigger: default pair, score 0.8 >= 0.5 -> strong.
|
|
156
|
+
const m1 = await ctrl.route("hello world", "fixed", 0.5);
|
|
157
|
+
expect(m1).toBe("gpt-4o");
|
|
158
|
+
|
|
159
|
+
// With trigger: overridden pair, score 0.8 >= 0.5 -> strong.
|
|
160
|
+
const m2 = await ctrl.route("do a code review", "fixed", 0.5);
|
|
161
|
+
expect(m2).toBe("claude-opus");
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it("uses default pair when middleware returns null", async () => {
|
|
165
|
+
const mw = new IntentMiddleware("never-match-this", {
|
|
166
|
+
strong: "x",
|
|
167
|
+
weak: "y",
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
const ctrl = new Controller({
|
|
171
|
+
strongModel: "default-strong",
|
|
172
|
+
weakModel: "default-weak",
|
|
173
|
+
routers: { fixed: new FixedRouter(0.8) },
|
|
174
|
+
middleware: [mw],
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
const model = await ctrl.route("test", "fixed", 0.5);
|
|
178
|
+
expect(model).toBe("default-strong");
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
// ─── Threshold calibration ──────────────────────────────────────────────────
|
|
183
|
+
|
|
184
|
+
describe("integration: threshold calibration", () => {
|
|
185
|
+
const score = 0.6;
|
|
186
|
+
|
|
187
|
+
it("threshold below score -> strong", async () => {
|
|
188
|
+
const ctrl = new Controller({
|
|
189
|
+
strongModel: "strong",
|
|
190
|
+
weakModel: "weak",
|
|
191
|
+
routers: { cal: new FixedRouter(score) },
|
|
192
|
+
});
|
|
193
|
+
expect(await ctrl.route("test", "cal", 0.5)).toBe("strong");
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
it("threshold at score -> strong (>=)", async () => {
|
|
197
|
+
const ctrl = new Controller({
|
|
198
|
+
strongModel: "strong",
|
|
199
|
+
weakModel: "weak",
|
|
200
|
+
routers: { cal: new FixedRouter(score) },
|
|
201
|
+
});
|
|
202
|
+
expect(await ctrl.route("test", "cal", 0.6)).toBe("strong");
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
it("threshold above score -> weak", async () => {
|
|
206
|
+
const ctrl = new Controller({
|
|
207
|
+
strongModel: "strong",
|
|
208
|
+
weakModel: "weak",
|
|
209
|
+
routers: { cal: new FixedRouter(score) },
|
|
210
|
+
});
|
|
211
|
+
expect(await ctrl.route("test", "cal", 0.7)).toBe("weak");
|
|
212
|
+
});
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
// ─── Error cases ────────────────────────────────────────────────────────────
|
|
216
|
+
|
|
217
|
+
describe("integration: error cases", () => {
|
|
218
|
+
it("throws on invalid model string (no prefix)", () => {
|
|
219
|
+
expect(() => parseModelName("mf-0.5")).toThrow(RoutingError);
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
it("throws on invalid model string (missing threshold)", () => {
|
|
223
|
+
expect(() => parseModelName("router-mf")).toThrow(RoutingError);
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
it("throws on invalid model string (NaN threshold)", () => {
|
|
227
|
+
expect(() => parseModelName("router-mf-abc")).toThrow(RoutingError);
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it("throws on unknown router", async () => {
|
|
231
|
+
const ctrl = new Controller({
|
|
232
|
+
strongModel: "s",
|
|
233
|
+
weakModel: "w",
|
|
234
|
+
routers: { known: new FixedRouter(0.5) },
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
await expect(
|
|
238
|
+
ctrl.route("test", "unknown", 0.5),
|
|
239
|
+
).rejects.toThrow(RoutingError);
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
it("throws on threshold out of [0,1]", async () => {
|
|
243
|
+
const ctrl = new Controller({
|
|
244
|
+
strongModel: "s",
|
|
245
|
+
weakModel: "w",
|
|
246
|
+
routers: { test: new FixedRouter(0.5) },
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
await expect(ctrl.route("test", "test", 1.5)).rejects.toThrow(
|
|
250
|
+
RoutingError,
|
|
251
|
+
);
|
|
252
|
+
await expect(ctrl.route("test", "test", -0.1)).rejects.toThrow(
|
|
253
|
+
RoutingError,
|
|
254
|
+
);
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
it("propagates scorer errors", async () => {
|
|
258
|
+
const ctrl = new Controller({
|
|
259
|
+
strongModel: "s",
|
|
260
|
+
weakModel: "w",
|
|
261
|
+
routers: {
|
|
262
|
+
broken: new FailingRouter(new Error("triton down")),
|
|
263
|
+
},
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
await expect(
|
|
267
|
+
ctrl.route("test", "broken", 0.5),
|
|
268
|
+
).rejects.toThrow("triton down");
|
|
269
|
+
});
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
// ─── Model counts tracking ─────────────────────────────────────────────────
|
|
273
|
+
|
|
274
|
+
describe("integration: model counts tracking", () => {
|
|
275
|
+
it("tracks routing decisions per router per model", async () => {
|
|
276
|
+
const ctrl = new Controller({
|
|
277
|
+
strongModel: "strong",
|
|
278
|
+
weakModel: "weak",
|
|
279
|
+
routers: {
|
|
280
|
+
high: new FixedRouter(0.9),
|
|
281
|
+
low: new FixedRouter(0.1),
|
|
282
|
+
},
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
await ctrl.route("test", "high", 0.5);
|
|
286
|
+
await ctrl.route("test", "low", 0.5);
|
|
287
|
+
await ctrl.route("test", "high", 0.5);
|
|
288
|
+
|
|
289
|
+
expect(ctrl.modelCounts.get("high")?.get("strong")).toBe(2);
|
|
290
|
+
expect(ctrl.modelCounts.get("low")?.get("weak")).toBe(1);
|
|
291
|
+
});
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
// ─── routeByModelName ───────────────────────────────────────────────────────
|
|
295
|
+
|
|
296
|
+
describe("integration: routeByModelName", () => {
|
|
297
|
+
it("parses model name and routes correctly", async () => {
|
|
298
|
+
const ctrl = new Controller({
|
|
299
|
+
strongModel: "gpt-4o",
|
|
300
|
+
weakModel: "gpt-4o-mini",
|
|
301
|
+
routers: { mf: new FixedRouter(0.8) },
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
const model = await ctrl.routeByModelName(
|
|
305
|
+
"test", "router-mf-0.5",
|
|
306
|
+
);
|
|
307
|
+
expect(model).toBe("gpt-4o");
|
|
308
|
+
});
|
|
309
|
+
});
|