@hop-top/kit 0.5.0-alpha.0 → 0.5.0-alpha.10
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 +262 -0
- package/LICENSE +1 -1
- package/LICENSE-cobra.txt +174 -0
- package/NOTICE +15 -0
- package/README.md +55 -259
- package/dist/aim-DZHwwiIc.d.ts +139 -0
- package/dist/aim.d.ts +1 -116
- package/dist/aim.js +13 -414
- package/dist/alias-D9_qFlyP.d.ts +174 -0
- package/dist/alias.d.ts +2 -73
- package/dist/alias.js +27 -207
- package/dist/api-DAexsu2H.d.ts +77 -0
- package/dist/api.d.ts +1 -62
- package/dist/api.js +10 -164
- package/dist/auth-2-jfgiHn.d.ts +34 -0
- package/dist/auth.d.ts +1 -26
- package/dist/auth.js +6 -40
- package/dist/chunk-2JV3A7OP.js +1894 -0
- package/dist/chunk-3FIKGFKI.js +54 -0
- package/dist/chunk-4UE5TCMQ.js +104 -0
- package/dist/chunk-6HFAEZ52.js +29 -0
- package/dist/chunk-6SVOHNIC.js +17 -0
- package/dist/chunk-72XZWQX7.js +504 -0
- package/dist/chunk-ABQSE7AI.js +513 -0
- package/dist/chunk-BDNNPRX6.js +41 -0
- package/dist/chunk-BLS2OBIQ.js +263 -0
- package/dist/chunk-BRNEKPWX.js +37 -0
- package/dist/chunk-CSJWB6D3.js +151 -0
- package/dist/chunk-GGWJRWWA.js +309 -0
- package/dist/chunk-GHPJJV5L.js +613 -0
- package/dist/chunk-IXVQ62NZ.js +334 -0
- package/dist/chunk-JKMQSAJP.js +174 -0
- package/dist/chunk-JZ22ZUFA.js +1131 -0
- package/dist/chunk-K7VCKJ27.js +814 -0
- package/dist/chunk-KVG7YCTS.js +115 -0
- package/dist/chunk-L23O2FFE.js +51 -0
- package/dist/chunk-MQXYDYRD.js +968 -0
- package/dist/chunk-MUIQVU6E.js +251 -0
- package/dist/chunk-NLMS7HGP.js +178 -0
- package/dist/chunk-POR5L2DG.js +247 -0
- package/dist/chunk-PSFYJGDG.js +1445 -0
- package/dist/chunk-QJVSKKRQ.js +369 -0
- package/dist/chunk-QPUCXQXT.js +67 -0
- package/dist/chunk-QQQYCWSC.js +240 -0
- package/dist/chunk-R5U7XKVJ.js +16 -0
- package/dist/chunk-SNVGDYX7.js +187 -0
- package/dist/chunk-T46YPHKN.js +669 -0
- package/dist/chunk-UKZIAQA7.js +26 -0
- package/dist/chunk-WBVO43HU.js +355 -0
- package/dist/chunk-WUOAQVOA.js +86 -0
- package/dist/chunk-WWUKGJB5.js +90 -0
- package/dist/chunk-XJTRY453.js +868 -0
- package/dist/chunk-YGYQUFGY.js +219 -0
- package/dist/chunk-YOBAUCVN.js +583 -0
- package/dist/cli-C5B5tnpA.d.ts +400 -0
- package/dist/cli.d.ts +7 -309
- package/dist/cli.js +70 -885
- package/dist/completion-csVun6SP.d.ts +112 -0
- package/dist/completion-values-By-mnjNt.d.ts +142 -0
- package/dist/completion-values.d.ts +1 -0
- package/dist/completion-values.js +29 -0
- package/dist/completion.d.ts +3 -0
- package/dist/completion.js +34 -0
- package/dist/compliance-AxFf-uMy.d.ts +60 -0
- package/dist/compliance.d.ts +1 -0
- package/dist/compliance.js +17 -0
- package/dist/config-CaK60ByQ.d.ts +241 -0
- package/dist/config.d.ts +1 -97
- package/dist/config.js +8 -78
- package/dist/errcorrect-BcX1Ft0K.d.ts +44 -0
- package/dist/errcorrect.d.ts +1 -37
- package/dist/errcorrect.js +8 -68
- package/dist/error-Do8Qji43.d.ts +231 -0
- package/dist/id/index.d.ts +2 -83
- package/dist/id/index.js +14 -65
- package/dist/index-BKJZClJx.d.ts +1486 -0
- package/dist/index-C3Zw4wDJ.d.ts +359 -0
- package/dist/index-DCe1boBz.d.ts +328 -0
- package/dist/index-VLDcI5NF.d.ts +95 -0
- package/dist/index.d.ts +37 -0
- package/dist/index.js +132 -0
- package/dist/llm-DYuR7_uX.d.ts +1069 -0
- package/dist/llm-anthropic-Cd_I2xXB.d.ts +67 -0
- package/dist/llm-anthropic.d.ts +2 -0
- package/dist/llm-anthropic.js +13 -0
- package/dist/llm-openai-DyFrucmV.d.ts +87 -0
- package/dist/llm-openai.d.ts +2 -0
- package/dist/llm-openai.js +19 -0
- package/dist/llm.d.ts +1 -188
- package/dist/llm.js +101 -356
- package/dist/log-CkaxMX3U.d.ts +115 -0
- package/dist/log.d.ts +2 -0
- package/dist/log.js +14 -0
- package/dist/mcp/index.d.ts +4 -0
- package/dist/mcp/index.js +184 -0
- package/dist/netpolicy-Brx5XZUB.d.ts +151 -0
- package/dist/netpolicy.d.ts +1 -0
- package/dist/netpolicy.js +25 -0
- package/dist/output-CjZWmL8s.d.ts +420 -0
- package/dist/output.d.ts +3 -213
- package/dist/output.js +137 -447
- package/dist/parity-CvMuYcVI.d.ts +61 -0
- package/dist/progress-CAzyxeWl.d.ts +38 -0
- package/dist/progress.d.ts +1 -30
- package/dist/progress.js +6 -55
- package/dist/provenance-SMFHm5gx.d.ts +30 -0
- package/dist/provenance.d.ts +1 -24
- package/dist/provenance.js +6 -31
- package/dist/routellm-B0nv59mH.d.ts +111 -0
- package/dist/routellm.d.ts +2 -64
- package/dist/routellm.js +13 -255
- package/dist/rpc-D31Lg0Kc.d.ts +32 -0
- package/dist/rpc.d.ts +2 -25
- package/dist/rpc.js +4 -98
- package/dist/safety-CUaYEjZc.d.ts +30 -0
- package/dist/safety.d.ts +1 -24
- package/dist/safety.js +6 -43
- package/dist/scope-BrhYB4ij.d.ts +141 -0
- package/dist/scope.d.ts +1 -121
- package/dist/scope.js +20 -377
- package/dist/serve-D8yWMrTc.d.ts +646 -0
- package/dist/serve.d.ts +3 -0
- package/dist/serve.js +75 -0
- package/dist/sqlstore.js +6 -39
- package/dist/stream-CrjeCp0M.d.ts +44 -0
- package/dist/stream.d.ts +1 -35
- package/dist/stream.js +8 -51
- package/dist/telemetry/index.d.ts +1 -315
- package/dist/telemetry/index.js +26 -599
- package/dist/toolspec-DcPwwoAZ.d.ts +368 -0
- package/dist/toolspec.d.ts +2 -0
- package/dist/toolspec.js +37 -0
- package/dist/tui/index.d.ts +6 -298
- package/dist/tui/index.js +18 -322
- package/dist/upgrade-p_zh3pvV.d.ts +102 -0
- package/dist/upgrade.d.ts +1 -95
- package/dist/upgrade.js +6 -114
- package/dist/xdg-DB7Shoib.d.ts +179 -0
- package/dist/xdg.d.ts +1 -169
- package/dist/xdg.js +14 -128
- package/package.json +236 -46
- package/src/README.md +15 -0
- package/src/alias.conformance.test.ts +250 -0
- package/src/alias.test.ts +206 -2
- package/src/alias.ts +196 -21
- package/src/cli.help-command.test.ts +105 -0
- package/src/cli.offline.test.ts +93 -0
- package/src/cli.parity.test.ts +192 -0
- package/src/cli.test.ts +95 -1
- package/src/cli.ts +195 -25
- package/src/completion-scripts.ts +16 -0
- package/src/completion-values.test.ts +76 -16
- package/src/completion-values.ts +170 -66
- package/src/completion.conformance.test.ts +297 -0
- package/src/completion.test.ts +256 -145
- package/src/completion.ts +767 -121
- package/src/compliance.test.ts +196 -2
- package/src/compliance.ts +440 -20
- package/src/config-merge.test.ts +425 -0
- package/src/config.test.ts +228 -1
- package/src/config.ts +429 -16
- package/src/gen/crud_pb.ts +7 -7
- package/src/id/README.md +38 -0
- package/src/id/contract.test.ts +1 -1
- package/src/id/index.test.ts +1 -1
- package/src/id/index.ts +3 -2
- package/src/index.test.ts +71 -0
- package/src/index.ts +46 -0
- package/src/llm-anthropic.conformance.test.ts +1135 -0
- package/src/llm-anthropic.test.ts +152 -0
- package/src/llm-anthropic.ts +940 -0
- package/src/llm-choice-hermetic.test.ts +335 -0
- package/src/llm-config.test.ts +232 -0
- package/src/llm-config.ts +106 -0
- package/src/llm-endpoint.ts +254 -0
- package/src/llm-error.ts +17 -0
- package/src/llm-errors.ts +152 -0
- package/src/llm-file.ts +126 -0
- package/src/llm-http.ts +317 -0
- package/src/llm-keys.ts +167 -0
- package/src/llm-media.conformance.test.ts +87 -0
- package/src/llm-media.test.ts +279 -0
- package/src/llm-media.ts +322 -0
- package/src/llm-openai.conformance.test.ts +407 -0
- package/src/llm-openai.test.ts +510 -0
- package/src/llm-openai.ts +708 -0
- package/src/llm-payload.ts +155 -0
- package/src/llm-quote.ts +46 -0
- package/src/llm-routellm.conformance.test.ts +44 -0
- package/src/llm-toolnames.conformance.test.ts +112 -0
- package/src/llm-toolnames.test.ts +132 -0
- package/src/llm-toolnames.ts +298 -0
- package/src/llm-uri.ts +99 -0
- package/src/llm.test.ts +43 -15
- package/src/llm.toolcall.test.ts +175 -0
- package/src/llm.ts +602 -190
- package/src/log.parity.test.ts +105 -0
- package/src/log.test.ts +270 -1
- package/src/log.ts +228 -49
- package/src/mcp/README.md +63 -0
- package/src/mcp/args-separator.test.ts +164 -0
- package/src/mcp/args.test.ts +223 -0
- package/src/mcp/args.ts +160 -0
- package/src/mcp/audit.test.ts +520 -0
- package/src/mcp/audit.ts +70 -0
- package/src/mcp/bridge.test.ts +222 -0
- package/src/mcp/bridge.ts +291 -0
- package/src/mcp/conformance.test.ts +357 -0
- package/src/mcp/dispatch.ts +359 -0
- package/src/mcp/identity.test.ts +166 -0
- package/src/mcp/identity.ts +72 -0
- package/src/mcp/index.ts +176 -0
- package/src/mcp/legacy-notify.test.ts +59 -0
- package/src/mcp/legacy.ts +367 -0
- package/src/mcp/mcp.test.ts +707 -0
- package/src/mcp/modern-confirm.test.ts +520 -0
- package/src/mcp/modern-confirm.ts +484 -0
- package/src/mcp/modern.ts +788 -0
- package/src/mcp/stdio.test.ts +313 -0
- package/src/mcp/stdio.ts +254 -0
- package/src/mcp/tasks.ts +82 -0
- package/src/mcp/testtree.ts +250 -0
- package/src/mcp/toolname.test.ts +147 -0
- package/src/mcp/types.ts +689 -0
- package/src/netpolicy.test.ts +280 -0
- package/src/netpolicy.ts +256 -0
- package/src/output/README.md +46 -0
- package/src/output/builtins.ts +5 -5
- package/src/output/dispatch.ts +10 -2
- package/src/output/error.ts +459 -0
- package/src/output/formatter.ts +29 -5
- package/src/output/formatters/README.md +50 -0
- package/src/output/formatters/csv.ts +48 -12
- package/src/output/formatters/json.ts +4 -25
- package/src/output/formatters/project.ts +34 -0
- package/src/output/formatters/table.ts +8 -6
- package/src/output/formatters/text.ts +7 -3
- package/src/output/formatters/yaml.ts +5 -22
- package/src/output/projection.test.ts +298 -0
- package/src/output/projection.ts +33 -59
- package/src/output/template.ts +11 -21
- package/src/output.surface.test.ts +41 -0
- package/src/output.ts +64 -3
- package/src/published-types.test.ts +133 -0
- package/src/routellm.test.ts +209 -28
- package/src/routellm.toolcall.test.ts +521 -0
- package/src/routellm.ts +213 -177
- package/src/router/README.md +44 -0
- package/src/router/server.test.ts +41 -0
- package/src/router/server.ts +11 -0
- package/src/safety.test.ts +19 -9
- package/src/scope-defaults.json +29 -29
- package/src/serve.test.ts +1257 -0
- package/src/serve.ts +1665 -0
- package/src/telemetry/README.md +48 -0
- package/src/telemetry/client.test.ts +31 -0
- package/src/telemetry/client.ts +12 -6
- package/src/telemetry/offline.test.ts +64 -0
- package/src/toolspec-internal.ts +279 -0
- package/src/toolspec-manifest.test.ts +167 -0
- package/src/toolspec-manifest.ts +410 -0
- package/src/toolspec-spec-schemas.ts +12 -0
- package/src/toolspec-spec.test.ts +273 -0
- package/src/toolspec-spec.ts +458 -0
- package/src/toolspec.from-command.test.ts +350 -0
- package/src/toolspec.test.ts +2 -2
- package/src/toolspec.ts +222 -0
- package/src/triton/README.md +34 -0
- package/src/tui/README.md +42 -0
- package/src/tui/parity.test.ts +142 -0
- package/src/tui/parity.ts +59 -13
- package/src/wizard.test.ts +7 -7
- package/test/llm/conformance-helpers.ts +100 -0
- package/test/output/csv.test.ts +164 -0
- package/test/output/dispatch.test.ts +1 -2
- package/test/output/error.test.ts +277 -0
- package/test/output/smoke-capture.ts +2 -2
- package/test/output/smoke.test.ts +2 -2
- package/test/output/taxonomy-contract.test.ts +148 -0
- package/test/setup-env.ts +16 -0
- package/test/toolspec/fixture.ts +79 -0
- package/tsconfig.json +3 -1
- package/tsconfig.test.json +10 -0
- package/vitest.config.ts +9 -0
|
@@ -0,0 +1,521 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for how the routellm adapter serializes messages onto the
|
|
3
|
+
* OpenAI-compatible wire: tool-call linkage, content parts, and the
|
|
4
|
+
* errors it returns instead of degrading either to plain text.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
8
|
+
import {
|
|
9
|
+
LLMError,
|
|
10
|
+
inlineSource,
|
|
11
|
+
urlSource,
|
|
12
|
+
type Message,
|
|
13
|
+
type ResolvedConfig,
|
|
14
|
+
type ToolCall,
|
|
15
|
+
} from "./llm";
|
|
16
|
+
import { RouteLLMAdapter } from "./routellm";
|
|
17
|
+
|
|
18
|
+
type WireMessage = Record<string, unknown>;
|
|
19
|
+
|
|
20
|
+
const cfg: ResolvedConfig = {
|
|
21
|
+
uri: { scheme: "routellm", model: "mf:0.7" },
|
|
22
|
+
provider: { model: "mf:0.7", baseURL: "http://routellm.test" },
|
|
23
|
+
fallbacks: [],
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const call: ToolCall = {
|
|
27
|
+
id: "call_1",
|
|
28
|
+
name: "get_weather",
|
|
29
|
+
arguments: { city: "NYC" },
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
let bodies: { messages: WireMessage[] }[];
|
|
33
|
+
const origFetch = globalThis.fetch;
|
|
34
|
+
|
|
35
|
+
function jsonResponse(): globalThis.Response {
|
|
36
|
+
return new globalThis.Response(
|
|
37
|
+
JSON.stringify({
|
|
38
|
+
choices: [
|
|
39
|
+
{ message: { role: "assistant", content: "ok" }, finish_reason: "stop" },
|
|
40
|
+
],
|
|
41
|
+
}),
|
|
42
|
+
{ status: 200, headers: { "Content-Type": "application/json" } }
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
beforeEach(() => {
|
|
47
|
+
delete process.env["ROUTELLM_BASE_URL"];
|
|
48
|
+
bodies = [];
|
|
49
|
+
globalThis.fetch = vi.fn(async (_url: unknown, init?: RequestInit) => {
|
|
50
|
+
bodies.push(JSON.parse(String(init?.body)));
|
|
51
|
+
return jsonResponse();
|
|
52
|
+
}) as unknown as typeof globalThis.fetch;
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
afterEach(() => {
|
|
56
|
+
globalThis.fetch = origFetch;
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
async function wire(messages: Message[]): Promise<WireMessage[]> {
|
|
60
|
+
await new RouteLLMAdapter(cfg).complete({ messages });
|
|
61
|
+
expect(bodies).toHaveLength(1);
|
|
62
|
+
return bodies[0].messages;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async function rejects(messages: Message[], pattern: RegExp): Promise<void> {
|
|
66
|
+
const p = new RouteLLMAdapter(cfg).complete({ messages });
|
|
67
|
+
await expect(p).rejects.toThrow(LLMError);
|
|
68
|
+
await expect(
|
|
69
|
+
new RouteLLMAdapter(cfg).complete({ messages })
|
|
70
|
+
).rejects.toThrow(pattern);
|
|
71
|
+
expect(bodies).toHaveLength(0);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// ─── Tool-call linkage ──────────────────────────────────────────────────────
|
|
75
|
+
|
|
76
|
+
describe("routellm tool-call linkage on the wire", () => {
|
|
77
|
+
it("sends assistant tool_calls and a linked tool result", async () => {
|
|
78
|
+
const msgs = await wire([
|
|
79
|
+
{ role: "user", content: "Weather?" },
|
|
80
|
+
{ role: "assistant", content: "", toolCalls: [call] },
|
|
81
|
+
{ role: "tool", content: "sunny", toolCallId: "call_1" },
|
|
82
|
+
]);
|
|
83
|
+
|
|
84
|
+
expect(msgs).toHaveLength(3);
|
|
85
|
+
expect(msgs[0]).toEqual({ role: "user", content: "Weather?" });
|
|
86
|
+
expect(msgs[1]).toEqual({
|
|
87
|
+
role: "assistant",
|
|
88
|
+
tool_calls: [
|
|
89
|
+
{
|
|
90
|
+
id: "call_1",
|
|
91
|
+
type: "function",
|
|
92
|
+
function: { name: "get_weather", arguments: '{"city":"NYC"}' },
|
|
93
|
+
},
|
|
94
|
+
],
|
|
95
|
+
});
|
|
96
|
+
expect(msgs[2]).toEqual({
|
|
97
|
+
role: "tool",
|
|
98
|
+
content: "sunny",
|
|
99
|
+
tool_call_id: "call_1",
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("keeps assistant text sent alongside tool calls", async () => {
|
|
104
|
+
const msgs = await wire([
|
|
105
|
+
{ role: "assistant", content: "Checking.", toolCalls: [call] },
|
|
106
|
+
]);
|
|
107
|
+
expect(msgs[0]["content"]).toBe("Checking.");
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("encodes arguments: JSON string as is, value stringified, none as {}", async () => {
|
|
111
|
+
const msgs = await wire([
|
|
112
|
+
{
|
|
113
|
+
role: "assistant",
|
|
114
|
+
content: "",
|
|
115
|
+
toolCalls: [
|
|
116
|
+
{ id: "a", name: "f", arguments: '{"x":1}' },
|
|
117
|
+
{ id: "b", name: "f", arguments: { y: [2] } },
|
|
118
|
+
{ id: "c", name: "f", arguments: undefined },
|
|
119
|
+
],
|
|
120
|
+
},
|
|
121
|
+
]);
|
|
122
|
+
const calls = msgs[0]["tool_calls"] as {
|
|
123
|
+
function: { arguments: string };
|
|
124
|
+
}[];
|
|
125
|
+
expect(calls.map((c) => c.function.arguments)).toEqual([
|
|
126
|
+
'{"x":1}',
|
|
127
|
+
'{"y":[2]}',
|
|
128
|
+
"{}",
|
|
129
|
+
]);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("maps messages without the new fields exactly as before", async () => {
|
|
133
|
+
const msgs = await wire([
|
|
134
|
+
{ role: "system", content: "be brief" },
|
|
135
|
+
{ role: "user", content: "hi" },
|
|
136
|
+
{ role: "assistant", content: "hello" },
|
|
137
|
+
]);
|
|
138
|
+
expect(msgs).toEqual([
|
|
139
|
+
{ role: "system", content: "be brief" },
|
|
140
|
+
{ role: "user", content: "hi" },
|
|
141
|
+
{ role: "assistant", content: "hello" },
|
|
142
|
+
]);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it("stream sends the same linkage", async () => {
|
|
146
|
+
globalThis.fetch = vi.fn(async (_url: unknown, init?: RequestInit) => {
|
|
147
|
+
bodies.push(JSON.parse(String(init?.body)));
|
|
148
|
+
return new globalThis.Response("data: [DONE]\n\n", { status: 200 });
|
|
149
|
+
}) as unknown as typeof globalThis.fetch;
|
|
150
|
+
|
|
151
|
+
const adapter = new RouteLLMAdapter(cfg);
|
|
152
|
+
for await (const tok of adapter.stream({
|
|
153
|
+
messages: [
|
|
154
|
+
{ role: "assistant", content: "", toolCalls: [call] },
|
|
155
|
+
{ role: "tool", content: "sunny", toolCallId: "call_1" },
|
|
156
|
+
],
|
|
157
|
+
})) {
|
|
158
|
+
expect(tok.done).toBe(true);
|
|
159
|
+
}
|
|
160
|
+
expect(bodies[0].messages[1]).toEqual({
|
|
161
|
+
role: "tool",
|
|
162
|
+
content: "sunny",
|
|
163
|
+
tool_call_id: "call_1",
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it("rejects a tool result without toolCallId", async () => {
|
|
168
|
+
await rejects(
|
|
169
|
+
[{ role: "tool", content: "sunny" }],
|
|
170
|
+
/tool result needs toolCallId/
|
|
171
|
+
);
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it("checks a missing toolCallId before other tool-result faults", async () => {
|
|
175
|
+
await rejects(
|
|
176
|
+
[{ role: "tool", content: "", parts: [{ type: "text", text: "x" }] }],
|
|
177
|
+
/tool result needs toolCallId/
|
|
178
|
+
);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it("rejects a tool call without an id", async () => {
|
|
182
|
+
await rejects(
|
|
183
|
+
[{ role: "assistant", content: "", toolCalls: [{ ...call, id: "" }] }],
|
|
184
|
+
/tool call "get_weather" needs an id/
|
|
185
|
+
);
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it("rejects arguments that are not valid JSON", async () => {
|
|
189
|
+
await rejects(
|
|
190
|
+
[
|
|
191
|
+
{
|
|
192
|
+
role: "assistant",
|
|
193
|
+
content: "",
|
|
194
|
+
toolCalls: [{ ...call, arguments: "{city:" }],
|
|
195
|
+
},
|
|
196
|
+
],
|
|
197
|
+
/arguments are not valid JSON/
|
|
198
|
+
);
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it("rejects toolCallId on a non-tool role", async () => {
|
|
202
|
+
await rejects(
|
|
203
|
+
[{ role: "user", content: "x", toolCallId: "call_1" }],
|
|
204
|
+
/toolCallId.*only on role tool/
|
|
205
|
+
);
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
it("rejects toolCalls on a non-assistant role", async () => {
|
|
209
|
+
await rejects(
|
|
210
|
+
[{ role: "user", content: "x", toolCalls: [call] }],
|
|
211
|
+
/toolCalls.*only on role assistant/
|
|
212
|
+
);
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
it("rejects a tool result carrying parts", async () => {
|
|
216
|
+
await rejects(
|
|
217
|
+
[
|
|
218
|
+
{
|
|
219
|
+
role: "tool",
|
|
220
|
+
content: "",
|
|
221
|
+
toolCallId: "call_1",
|
|
222
|
+
parts: [{ type: "text", text: "sunny" }],
|
|
223
|
+
},
|
|
224
|
+
],
|
|
225
|
+
/tool result.*only content/
|
|
226
|
+
);
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it("rejects assistant tool calls with parts", async () => {
|
|
230
|
+
await rejects(
|
|
231
|
+
[
|
|
232
|
+
{
|
|
233
|
+
role: "assistant",
|
|
234
|
+
content: "",
|
|
235
|
+
toolCalls: [call],
|
|
236
|
+
parts: [{ type: "text", text: "x" }],
|
|
237
|
+
},
|
|
238
|
+
],
|
|
239
|
+
/assistant tool calls do not support content parts/
|
|
240
|
+
);
|
|
241
|
+
});
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
// ─── Content parts ──────────────────────────────────────────────────────────
|
|
245
|
+
|
|
246
|
+
describe("routellm content parts on the wire", () => {
|
|
247
|
+
it("sends text and URL image parts as a content array", async () => {
|
|
248
|
+
const msgs = await wire([
|
|
249
|
+
{
|
|
250
|
+
role: "user",
|
|
251
|
+
content: "ignored when parts are set",
|
|
252
|
+
parts: [
|
|
253
|
+
{ type: "text", text: "What is this?" },
|
|
254
|
+
{ type: "image", source: urlSource("https://example.test/cat.png") },
|
|
255
|
+
],
|
|
256
|
+
},
|
|
257
|
+
]);
|
|
258
|
+
expect(msgs[0]).toEqual({
|
|
259
|
+
role: "user",
|
|
260
|
+
content: [
|
|
261
|
+
{ type: "text", text: "What is this?" },
|
|
262
|
+
{
|
|
263
|
+
type: "image_url",
|
|
264
|
+
image_url: { url: "https://example.test/cat.png" },
|
|
265
|
+
},
|
|
266
|
+
],
|
|
267
|
+
});
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
it("inlines non-URL images as a base64 data URI", async () => {
|
|
271
|
+
const msgs = await wire([
|
|
272
|
+
{
|
|
273
|
+
role: "user",
|
|
274
|
+
content: "",
|
|
275
|
+
parts: [
|
|
276
|
+
{
|
|
277
|
+
type: "image",
|
|
278
|
+
source: inlineSource(new Uint8Array([1, 2, 3]), "image/png"),
|
|
279
|
+
},
|
|
280
|
+
],
|
|
281
|
+
},
|
|
282
|
+
]);
|
|
283
|
+
const content = msgs[0]["content"] as WireMessage[];
|
|
284
|
+
expect(content[0]).toEqual({
|
|
285
|
+
type: "image_url",
|
|
286
|
+
image_url: { url: "data:image/png;base64,AQID" },
|
|
287
|
+
});
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
it("sends a PDF as a file part", async () => {
|
|
291
|
+
const msgs = await wire([
|
|
292
|
+
{
|
|
293
|
+
role: "user",
|
|
294
|
+
content: "",
|
|
295
|
+
parts: [
|
|
296
|
+
{
|
|
297
|
+
type: "image",
|
|
298
|
+
mimeType: "application/pdf",
|
|
299
|
+
source: inlineSource(new Uint8Array([1, 2, 3]), ""),
|
|
300
|
+
},
|
|
301
|
+
],
|
|
302
|
+
},
|
|
303
|
+
]);
|
|
304
|
+
const content = msgs[0]["content"] as WireMessage[];
|
|
305
|
+
expect(content[0]).toEqual({ type: "file", file: { file_data: "AQID" } });
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
it("rejects an image with no URL and no MIME type", async () => {
|
|
309
|
+
await rejects(
|
|
310
|
+
[
|
|
311
|
+
{
|
|
312
|
+
role: "user",
|
|
313
|
+
content: "",
|
|
314
|
+
parts: [{ type: "image", source: inlineSource(new Uint8Array(), "") }],
|
|
315
|
+
},
|
|
316
|
+
],
|
|
317
|
+
/image part needs a MIME type/
|
|
318
|
+
);
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
it("rejects modalities the wire cannot carry", async () => {
|
|
322
|
+
await rejects(
|
|
323
|
+
[
|
|
324
|
+
{
|
|
325
|
+
role: "user",
|
|
326
|
+
content: "",
|
|
327
|
+
parts: [
|
|
328
|
+
{ type: "audio", source: inlineSource(new Uint8Array(), "audio/mpeg") },
|
|
329
|
+
],
|
|
330
|
+
},
|
|
331
|
+
],
|
|
332
|
+
/unsupported modality "audio"/
|
|
333
|
+
);
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
it("rejects parts on a non-user role", async () => {
|
|
337
|
+
await rejects(
|
|
338
|
+
[{ role: "system", content: "", parts: [{ type: "text", text: "x" }] }],
|
|
339
|
+
/role "system" does not support content parts/
|
|
340
|
+
);
|
|
341
|
+
});
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
// ─── Tool names ─────────────────────────────────────────────────────────────
|
|
345
|
+
|
|
346
|
+
describe("routellm tool names (openai rule)", () => {
|
|
347
|
+
const history = (name: string): Message[] => [
|
|
348
|
+
{ role: "user", content: "Hi" },
|
|
349
|
+
{
|
|
350
|
+
role: "assistant",
|
|
351
|
+
content: "",
|
|
352
|
+
toolCalls: [{ id: "call_0", name, arguments: "{}" }],
|
|
353
|
+
},
|
|
354
|
+
{ role: "tool", content: "opened", toolCallId: "call_0" },
|
|
355
|
+
];
|
|
356
|
+
|
|
357
|
+
function withParams(params?: Record<string, string>): ResolvedConfig {
|
|
358
|
+
return { ...cfg, provider: { ...cfg.provider, params } };
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
function sentNames(): string[] {
|
|
362
|
+
const msgs = bodies[0]?.messages ?? [];
|
|
363
|
+
return msgs.flatMap((m) =>
|
|
364
|
+
((m["tool_calls"] as { function: { name: string } }[] | undefined) ?? [])
|
|
365
|
+
.map((tc) => tc.function.name)
|
|
366
|
+
);
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
it("maps history names forward by default", async () => {
|
|
370
|
+
await new RouteLLMAdapter(cfg).complete({ messages: history("pane.open") });
|
|
371
|
+
expect(sentNames()).toEqual(["pane_open"]);
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
it("maps an overlong history name to 64 bytes with a hash", async () => {
|
|
375
|
+
await new RouteLLMAdapter(cfg).complete({
|
|
376
|
+
messages: history("a".repeat(65)),
|
|
377
|
+
});
|
|
378
|
+
expect(sentNames()).toEqual(["a".repeat(55) + "_635361c4"]);
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
it("maps history names forward when streaming", async () => {
|
|
382
|
+
globalThis.fetch = vi.fn(async (_url: unknown, init?: RequestInit) => {
|
|
383
|
+
bodies.push(JSON.parse(String(init?.body)));
|
|
384
|
+
return new globalThis.Response("data: [DONE]\n\n", { status: 200 });
|
|
385
|
+
}) as unknown as typeof globalThis.fetch;
|
|
386
|
+
const adapter = new RouteLLMAdapter(cfg);
|
|
387
|
+
for await (const _tok of adapter.stream({ messages: history("ns/tool") })) {
|
|
388
|
+
// drain
|
|
389
|
+
}
|
|
390
|
+
expect(sentNames()).toEqual(["ns_tool"]);
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
it("sends history names unchanged in strict mode", async () => {
|
|
394
|
+
const adapter = new RouteLLMAdapter(withParams({ tool_names: "strict" }));
|
|
395
|
+
await adapter.complete({ messages: history("pane.open") });
|
|
396
|
+
expect(sentNames()).toEqual(["pane.open"]);
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
it("accepts an explicit sanitize mode", async () => {
|
|
400
|
+
const adapter = new RouteLLMAdapter(withParams({ tool_names: "sanitize" }));
|
|
401
|
+
await adapter.complete({ messages: history("pane.open") });
|
|
402
|
+
expect(sentNames()).toEqual(["pane_open"]);
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
it("refuses an unknown mode at construction", () => {
|
|
406
|
+
expect(
|
|
407
|
+
() => new RouteLLMAdapter(withParams({ tool_names: "loose" }))
|
|
408
|
+
).toThrow(/tool_names/);
|
|
409
|
+
});
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
// ─── Tool calling through the openai adapter ────────────────────────────────
|
|
413
|
+
|
|
414
|
+
describe("routellm tool calling", () => {
|
|
415
|
+
const defs = [{ name: "pane.open", description: "Open", parameters: { type: "object" } }];
|
|
416
|
+
|
|
417
|
+
it("callWithTools sends wire names and returns the original", async () => {
|
|
418
|
+
const urls: string[] = [];
|
|
419
|
+
globalThis.fetch = vi.fn(async (url: unknown, init?: RequestInit) => {
|
|
420
|
+
urls.push(String(url));
|
|
421
|
+
bodies.push(JSON.parse(String(init?.body)));
|
|
422
|
+
return new globalThis.Response(
|
|
423
|
+
JSON.stringify({
|
|
424
|
+
choices: [
|
|
425
|
+
{
|
|
426
|
+
message: {
|
|
427
|
+
role: "assistant",
|
|
428
|
+
content: null,
|
|
429
|
+
tool_calls: [
|
|
430
|
+
{ id: "call_9", type: "function", function: { name: "pane_open", arguments: '{"x":1}' } },
|
|
431
|
+
],
|
|
432
|
+
},
|
|
433
|
+
finish_reason: "tool_calls",
|
|
434
|
+
},
|
|
435
|
+
],
|
|
436
|
+
}),
|
|
437
|
+
{ status: 200, headers: { "Content-Type": "application/json" } }
|
|
438
|
+
);
|
|
439
|
+
}) as unknown as typeof globalThis.fetch;
|
|
440
|
+
|
|
441
|
+
const r = await new RouteLLMAdapter(cfg).callWithTools(
|
|
442
|
+
{ messages: [{ role: "user", content: "open it" }] },
|
|
443
|
+
defs
|
|
444
|
+
);
|
|
445
|
+
expect(r.toolCalls).toEqual([{ id: "call_9", name: "pane.open", arguments: { x: 1 } }]);
|
|
446
|
+
expect(urls).toEqual(["http://routellm.test/v1/chat/completions"]);
|
|
447
|
+
const body = bodies[0] as unknown as {
|
|
448
|
+
model: string;
|
|
449
|
+
tools: { function: { name: string } }[];
|
|
450
|
+
};
|
|
451
|
+
expect(body.model).toBe("router-mf-0.7");
|
|
452
|
+
expect(body.tools[0]!.function.name).toBe("pane_open");
|
|
453
|
+
});
|
|
454
|
+
|
|
455
|
+
it("streamWithTools folds tool-call deltas into the outcome", async () => {
|
|
456
|
+
const events = [
|
|
457
|
+
{ choices: [{ index: 0, delta: { tool_calls: [{ index: 0, id: "c1", function: { name: "pane_open", arguments: "{}" } }] } }] },
|
|
458
|
+
{ choices: [{ index: 0, delta: {}, finish_reason: "tool_calls" }] },
|
|
459
|
+
];
|
|
460
|
+
globalThis.fetch = vi.fn(async (_url: unknown, init?: RequestInit) => {
|
|
461
|
+
bodies.push(JSON.parse(String(init?.body)));
|
|
462
|
+
return new globalThis.Response(
|
|
463
|
+
events.map((e) => `data: ${JSON.stringify(e)}\n\n`).join("") + "data: [DONE]\n\n",
|
|
464
|
+
{ status: 200, headers: { "Content-Type": "text/event-stream" } }
|
|
465
|
+
);
|
|
466
|
+
}) as unknown as typeof globalThis.fetch;
|
|
467
|
+
|
|
468
|
+
const st = new RouteLLMAdapter(cfg).streamWithTools(
|
|
469
|
+
{ messages: [{ role: "user", content: "open it" }] },
|
|
470
|
+
defs
|
|
471
|
+
);
|
|
472
|
+
for await (const _t of st) {
|
|
473
|
+
// drain
|
|
474
|
+
}
|
|
475
|
+
expect(st.outcome()?.toolCalls).toEqual([{ id: "c1", name: "pane.open", arguments: {} }]);
|
|
476
|
+
});
|
|
477
|
+
});
|
|
478
|
+
|
|
479
|
+
describe("routellm stream events (through the openai adapter)", () => {
|
|
480
|
+
const sse = [
|
|
481
|
+
{ choices: [{ delta: { tool_calls: [{ index: 0, id: "c1", function: { name: "pane_open", arguments: "{}" } }] } }] },
|
|
482
|
+
{ choices: [{ delta: {}, finish_reason: "tool_calls" }] },
|
|
483
|
+
{ choices: [], usage: { prompt_tokens: 3, completion_tokens: 2, total_tokens: 5 } },
|
|
484
|
+
].map((c) => `data: ${JSON.stringify(c)}\n\n`).join("") + "data: [DONE]\n\n";
|
|
485
|
+
|
|
486
|
+
beforeEach(() => {
|
|
487
|
+
globalThis.fetch = vi.fn(async (_url: unknown, init?: RequestInit) => {
|
|
488
|
+
bodies.push(JSON.parse(String(init?.body)));
|
|
489
|
+
return new globalThis.Response(sse, {
|
|
490
|
+
status: 200,
|
|
491
|
+
headers: { "Content-Type": "text/event-stream" },
|
|
492
|
+
});
|
|
493
|
+
}) as unknown as typeof globalThis.fetch;
|
|
494
|
+
});
|
|
495
|
+
|
|
496
|
+
async function run(streamEvents: boolean) {
|
|
497
|
+
const s = new RouteLLMAdapter(cfg).streamWithTools(
|
|
498
|
+
{ messages: [{ role: "user", content: "Hi" }], streamEvents },
|
|
499
|
+
[{ name: "pane.open", description: "", parameters: {} }]
|
|
500
|
+
);
|
|
501
|
+
const toks = [];
|
|
502
|
+
for await (const t of s) toks.push(t);
|
|
503
|
+
return toks;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
it("asks for usage and yields tool-call and usage events when opted in", async () => {
|
|
507
|
+
const toks = await run(true);
|
|
508
|
+
expect((bodies[0] as Record<string, unknown>)["stream_options"]).toEqual({ include_usage: true });
|
|
509
|
+
expect(toks.map((t) => t.toolCall?.kind ?? (t.usage ? "usage" : t.done ? "done" : "text"))).toEqual(
|
|
510
|
+
["start", "delta", "end", "usage", "done"]
|
|
511
|
+
);
|
|
512
|
+
expect(toks[0]!.toolCall).toMatchObject({ index: 0, id: "c1", name: "pane.open" });
|
|
513
|
+
expect(toks[3]!.usage).toMatchObject({ promptTokens: 3, completionTokens: 2, totalTokens: 5 });
|
|
514
|
+
});
|
|
515
|
+
|
|
516
|
+
it("yields today's tokens when not opted in", async () => {
|
|
517
|
+
const toks = await run(false);
|
|
518
|
+
expect((bodies[0] as Record<string, unknown>)["stream_options"]).toBeUndefined();
|
|
519
|
+
expect(toks).toEqual([{ content: "", done: true }]);
|
|
520
|
+
});
|
|
521
|
+
});
|