@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,350 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import * as fs from "fs";
|
|
3
|
+
import * as os from "os";
|
|
4
|
+
import * as path from "path";
|
|
5
|
+
import { Command, Option } from "commander";
|
|
6
|
+
import { completionCommand } from "./completion";
|
|
7
|
+
import {
|
|
8
|
+
buildManifest,
|
|
9
|
+
findCommand,
|
|
10
|
+
loadToolSpec,
|
|
11
|
+
toolSpecFromCommand,
|
|
12
|
+
validateToolSpec,
|
|
13
|
+
} from "./toolspec";
|
|
14
|
+
|
|
15
|
+
function buildProgram(): Command {
|
|
16
|
+
const program = new Command("spaced").version("1.0.0");
|
|
17
|
+
program.option("--format <fmt>", "Output format");
|
|
18
|
+
|
|
19
|
+
program
|
|
20
|
+
.command("launch <mission> [vehicle]")
|
|
21
|
+
.alias("go")
|
|
22
|
+
.description("Launch a mission")
|
|
23
|
+
.option("--payload <items...>", "Payload manifest")
|
|
24
|
+
.option("-o, --output <file>", "Write JSON report to file")
|
|
25
|
+
.option("--dry-run", "Simulate launch without executing")
|
|
26
|
+
.addOption(new Option("--orbit <orbit>", "Target orbit").makeOptionMandatory())
|
|
27
|
+
.addOption(new Option("--secret", "internal").hideHelp())
|
|
28
|
+
.action(() => {});
|
|
29
|
+
|
|
30
|
+
const mission = program.command("mission").description("Mission catalog");
|
|
31
|
+
mission.command("list").description("List missions").action(() => {});
|
|
32
|
+
mission.command("purge").description("Purge catalog").action(() => {});
|
|
33
|
+
|
|
34
|
+
program.command("internal", { hidden: true }).action(() => {});
|
|
35
|
+
return program;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
describe("toolSpecFromCommand", () => {
|
|
39
|
+
it("projects names, descriptions, aliases and children", () => {
|
|
40
|
+
const spec = toolSpecFromCommand(buildProgram());
|
|
41
|
+
expect(spec.name).toBe("spaced");
|
|
42
|
+
expect(spec.commands.map((c) => c.name)).toEqual(["launch", "mission"]);
|
|
43
|
+
|
|
44
|
+
const launch = findCommand(spec, "launch")!;
|
|
45
|
+
expect(launch.short).toBe("Launch a mission");
|
|
46
|
+
expect(launch.aliases).toEqual(["go"]);
|
|
47
|
+
|
|
48
|
+
const mission = findCommand(spec, "mission")!;
|
|
49
|
+
expect(mission.children!.map((c) => c.name)).toEqual(["list", "purge"]);
|
|
50
|
+
expect(mission.children![0].short).toBe("List missions");
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("omits hidden commands and hidden options", () => {
|
|
54
|
+
const spec = toolSpecFromCommand(buildProgram());
|
|
55
|
+
expect(findCommand(spec, "internal")).toBeUndefined();
|
|
56
|
+
const launch = findCommand(spec, "launch")!;
|
|
57
|
+
expect(launch.flags!.some((f) => f.name === "secret")).toBe(false);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("projects flags with Go walker naming", () => {
|
|
61
|
+
const spec = toolSpecFromCommand(buildProgram());
|
|
62
|
+
const flags = findCommand(spec, "launch")!.flags!;
|
|
63
|
+
// default is pflag's DefValue spelling, the manifest's rule: a
|
|
64
|
+
// bool is "false", a list "[]", a value flag with none is omitted.
|
|
65
|
+
expect(flags).toEqual([
|
|
66
|
+
{ name: "payload", type: "stringSlice", description: "Payload manifest", default: "[]" },
|
|
67
|
+
{
|
|
68
|
+
name: "output",
|
|
69
|
+
short: "o",
|
|
70
|
+
type: "string",
|
|
71
|
+
description: "Write JSON report to file",
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
name: "dry-run",
|
|
75
|
+
type: "bool",
|
|
76
|
+
description: "Simulate launch without executing",
|
|
77
|
+
default: "false",
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
name: "orbit",
|
|
81
|
+
type: "string",
|
|
82
|
+
description: "Target orbit",
|
|
83
|
+
required: true,
|
|
84
|
+
},
|
|
85
|
+
]);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("projects flag default and choices, and arg variadic, as Go does", () => {
|
|
89
|
+
const program = new Command("t");
|
|
90
|
+
const attach = program.command("attach <name> [files...]").action(() => {});
|
|
91
|
+
attach.addOption(
|
|
92
|
+
new Option("--mode <mode>", "Attach mode").choices(["fast", "safe"]).default("fast"),
|
|
93
|
+
);
|
|
94
|
+
attach.option("--label <text>", "Free-form label");
|
|
95
|
+
const spec = toolSpecFromCommand(program);
|
|
96
|
+
const cmd = findCommand(spec, "attach")!;
|
|
97
|
+
expect(cmd.flags).toEqual([
|
|
98
|
+
{
|
|
99
|
+
name: "mode",
|
|
100
|
+
type: "string",
|
|
101
|
+
description: "Attach mode",
|
|
102
|
+
default: "fast",
|
|
103
|
+
choices: ["fast", "safe"],
|
|
104
|
+
},
|
|
105
|
+
{ name: "label", type: "string", description: "Free-form label" },
|
|
106
|
+
]);
|
|
107
|
+
expect(cmd.args).toEqual([
|
|
108
|
+
{ name: "name", required: true },
|
|
109
|
+
{ name: "files", variadic: true },
|
|
110
|
+
]);
|
|
111
|
+
// The ToolSpec default and the manifest default are the same bytes.
|
|
112
|
+
const m = buildManifest(program, "1.2").commands[0];
|
|
113
|
+
expect(m.flags!.map((f) => [f.name, f.default])).toEqual(
|
|
114
|
+
cmd.flags!.map((f) => [f.name, f.default]).sort(),
|
|
115
|
+
);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("reads the contract off the command's own kit/* annotations, as WalkCobra does", () => {
|
|
119
|
+
const program = new Command("t");
|
|
120
|
+
for (const [name, ann] of [
|
|
121
|
+
["cond", { "kit/side-effect": "write-local", "kit/idempotent": "conditional" }],
|
|
122
|
+
["yes", { "kit/idempotent": "yes", "kit/retryable": "true" }],
|
|
123
|
+
["no", { "kit/idempotent": "no" }],
|
|
124
|
+
["odd", { "kit/idempotent": "maybe" }],
|
|
125
|
+
["retry", { "kit/retryable": "true" }],
|
|
126
|
+
["plain", {}],
|
|
127
|
+
] as const) {
|
|
128
|
+
const c = program.command(name).action(() => {});
|
|
129
|
+
(c as Command & { annotations?: Record<string, string> }).annotations = { ...ann };
|
|
130
|
+
}
|
|
131
|
+
const spec = toolSpecFromCommand(program, {
|
|
132
|
+
annotations: { no: { contract: { idempotent: false, retryable: true } } },
|
|
133
|
+
});
|
|
134
|
+
expect(findCommand(spec, "cond")!.contract).toEqual({
|
|
135
|
+
idempotent: false,
|
|
136
|
+
retryable: false,
|
|
137
|
+
sideEffects: ["write-local"],
|
|
138
|
+
idempotentClass: "conditional",
|
|
139
|
+
});
|
|
140
|
+
expect(findCommand(spec, "yes")!.contract).toEqual({
|
|
141
|
+
idempotent: true,
|
|
142
|
+
retryable: true,
|
|
143
|
+
idempotentClass: "yes",
|
|
144
|
+
});
|
|
145
|
+
expect(findCommand(spec, "odd")!.contract).toBeUndefined();
|
|
146
|
+
expect(findCommand(spec, "retry")!.contract).toEqual({ idempotent: false, retryable: true });
|
|
147
|
+
expect(findCommand(spec, "plain")!.contract).toBeUndefined();
|
|
148
|
+
// The per-path annotation map still wins over the command's own.
|
|
149
|
+
expect(findCommand(spec, "no")!.contract).toEqual({ idempotent: false, retryable: true });
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("puts root options on spec.flags", () => {
|
|
153
|
+
const spec = toolSpecFromCommand(buildProgram());
|
|
154
|
+
expect(spec.flags!.map((f) => f.name)).toEqual(["version", "format"]);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it("projects positional arguments in order", () => {
|
|
158
|
+
const spec = toolSpecFromCommand(buildProgram());
|
|
159
|
+
expect(findCommand(spec, "launch")!.args).toEqual([
|
|
160
|
+
{ name: "mission", required: true },
|
|
161
|
+
{ name: "vehicle" },
|
|
162
|
+
]);
|
|
163
|
+
expect(findCommand(spec, "list")!.args).toBeUndefined();
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it("includes commands registered after construction", () => {
|
|
167
|
+
const program = buildProgram();
|
|
168
|
+
program.command("plugin-" + "dyn").description("dynamic").action(() => {});
|
|
169
|
+
const spec = toolSpecFromCommand(program);
|
|
170
|
+
expect(findCommand(spec, "plugin-dyn")!.short).toBe("dynamic");
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it("merges per-command annotations keyed by path", () => {
|
|
174
|
+
const spec = toolSpecFromCommand(buildProgram(), {
|
|
175
|
+
annotations: {
|
|
176
|
+
launch: {
|
|
177
|
+
contract: { idempotent: false, retryable: false },
|
|
178
|
+
safety: { level: "dangerous" },
|
|
179
|
+
previewModes: ["--dry-run"],
|
|
180
|
+
short: "Launch (annotated)",
|
|
181
|
+
},
|
|
182
|
+
"mission list": {
|
|
183
|
+
contract: { idempotent: true, retryable: true },
|
|
184
|
+
safety: { level: "safe" },
|
|
185
|
+
},
|
|
186
|
+
},
|
|
187
|
+
});
|
|
188
|
+
const launch = findCommand(spec, "launch")!;
|
|
189
|
+
expect(launch.safety).toEqual({ level: "dangerous" });
|
|
190
|
+
expect(launch.previewModes).toEqual(["--dry-run"]);
|
|
191
|
+
expect(launch.short).toBe("Launch (annotated)");
|
|
192
|
+
expect(launch.flags!.length).toBe(4);
|
|
193
|
+
const list = findCommand(spec, "list")!;
|
|
194
|
+
expect(list.contract).toEqual({ idempotent: true, retryable: true });
|
|
195
|
+
expect(findCommand(spec, "purge")!.safety).toBeUndefined();
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
it("produces a spec that validates and defaults schemaVersion", () => {
|
|
199
|
+
const spec = toolSpecFromCommand(buildProgram());
|
|
200
|
+
expect(spec.schemaVersion).toBe("1");
|
|
201
|
+
expect(validateToolSpec(spec)).toEqual([]);
|
|
202
|
+
const custom = toolSpecFromCommand(buildProgram(), { schemaVersion: "2" });
|
|
203
|
+
expect(custom.schemaVersion).toBe("2");
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it("serializes without undefined keys", () => {
|
|
207
|
+
const spec = toolSpecFromCommand(buildProgram());
|
|
208
|
+
const list = findCommand(spec, "list")!;
|
|
209
|
+
expect(Object.keys(list).sort()).toEqual(["name", "short"]);
|
|
210
|
+
});
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
describe("toolSpecFromCommand exit codes", () => {
|
|
214
|
+
it("reads kit/exit-codes off the command, annotation map wins", () => {
|
|
215
|
+
const program = new Command("t");
|
|
216
|
+
const get = program.command("get").action(() => {});
|
|
217
|
+
(get as Command & { annotations?: Record<string, string> }).annotations = {
|
|
218
|
+
"kit/exit-codes": "OK, NOT_FOUND,TRANSIENT",
|
|
219
|
+
};
|
|
220
|
+
const put = program.command("put").action(() => {});
|
|
221
|
+
(put as Command & { annotations?: Record<string, string> }).annotations = {
|
|
222
|
+
"kit/exit-codes": "OK",
|
|
223
|
+
};
|
|
224
|
+
program.command("plain").action(() => {});
|
|
225
|
+
|
|
226
|
+
const spec = toolSpecFromCommand(program, {
|
|
227
|
+
annotations: { put: { exitCodes: ["OK", "CONFLICT"] } },
|
|
228
|
+
});
|
|
229
|
+
expect(findCommand(spec, "get")!.exitCodes).toEqual([
|
|
230
|
+
"OK",
|
|
231
|
+
"NOT_FOUND",
|
|
232
|
+
"TRANSIENT",
|
|
233
|
+
]);
|
|
234
|
+
expect(findCommand(spec, "put")!.exitCodes).toEqual(["OK", "CONFLICT"]);
|
|
235
|
+
expect(findCommand(spec, "plain")!.exitCodes).toBeUndefined();
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
it("validateToolSpec rejects an empty exit-code symbol", () => {
|
|
239
|
+
const errors = validateToolSpec({
|
|
240
|
+
name: "t",
|
|
241
|
+
schemaVersion: "1",
|
|
242
|
+
commands: [{ name: "get", exitCodes: ["OK", ""] }],
|
|
243
|
+
});
|
|
244
|
+
expect(errors.some((e) => e.includes("exit code"))).toBe(true);
|
|
245
|
+
});
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
describe("loadToolSpec args/short/required", () => {
|
|
249
|
+
it("maps args, undeclared_args, short, flag required/default/choices, arg variadic and idempotent_class from YAML", () => {
|
|
250
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "toolspec-"));
|
|
251
|
+
const file = path.join(dir, "t.toolspec.yaml");
|
|
252
|
+
fs.writeFileSync(
|
|
253
|
+
file,
|
|
254
|
+
[
|
|
255
|
+
"name: t",
|
|
256
|
+
'schema_version: "1"',
|
|
257
|
+
"commands:",
|
|
258
|
+
" - name: get",
|
|
259
|
+
" short: Get a thing",
|
|
260
|
+
" undeclared_args: true",
|
|
261
|
+
" exit_codes: [OK, NOT_FOUND]",
|
|
262
|
+
" args:",
|
|
263
|
+
" - name: id",
|
|
264
|
+
" required: true",
|
|
265
|
+
" - name: field",
|
|
266
|
+
" - name: files",
|
|
267
|
+
" variadic: true",
|
|
268
|
+
" flags:",
|
|
269
|
+
" - name: out",
|
|
270
|
+
" required: true",
|
|
271
|
+
" default: report.json",
|
|
272
|
+
" choices: [report.json, out.json]",
|
|
273
|
+
" contract:",
|
|
274
|
+
" idempotent_class: conditional",
|
|
275
|
+
].join("\n"),
|
|
276
|
+
);
|
|
277
|
+
try {
|
|
278
|
+
const get = loadToolSpec(file).commands[0];
|
|
279
|
+
expect(get.short).toBe("Get a thing");
|
|
280
|
+
expect(get.undeclaredArgs).toBe(true);
|
|
281
|
+
expect(get.exitCodes).toEqual(["OK", "NOT_FOUND"]);
|
|
282
|
+
expect(get.args).toEqual([
|
|
283
|
+
{ name: "id", required: true, variadic: undefined },
|
|
284
|
+
{ name: "field", required: undefined, variadic: undefined },
|
|
285
|
+
{ name: "files", required: undefined, variadic: true },
|
|
286
|
+
]);
|
|
287
|
+
expect(get.flags![0].required).toBe(true);
|
|
288
|
+
expect(get.flags![0].default).toBe("report.json");
|
|
289
|
+
expect(get.flags![0].choices).toEqual(["report.json", "out.json"]);
|
|
290
|
+
expect(get.contract).toEqual({
|
|
291
|
+
idempotent: false,
|
|
292
|
+
retryable: false,
|
|
293
|
+
sideEffects: undefined,
|
|
294
|
+
preConditions: undefined,
|
|
295
|
+
idempotentClass: "conditional",
|
|
296
|
+
});
|
|
297
|
+
} finally {
|
|
298
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
299
|
+
}
|
|
300
|
+
});
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
describe("toolSpecFromCommand negatable flags", () => {
|
|
304
|
+
it("types --no-x as a bool flag named no-x, as pflag does", () => {
|
|
305
|
+
const program = new Command("t");
|
|
306
|
+
program.command("run").option("--no-color", "c").action(() => {});
|
|
307
|
+
expect(findCommand(toolSpecFromCommand(program), "run")!.flags).toEqual([
|
|
308
|
+
{ name: "no-color", type: "bool", description: "c", default: "false" },
|
|
309
|
+
]);
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
it("types both halves of a --x/--no-x pair bool", () => {
|
|
313
|
+
const program = new Command("t");
|
|
314
|
+
program.option("--hints", "on").option("--no-hints", "off");
|
|
315
|
+
expect(toolSpecFromCommand(program).flags).toEqual([
|
|
316
|
+
{ name: "hints", type: "bool", description: "on", default: "false" },
|
|
317
|
+
{ name: "no-hints", type: "bool", description: "off", default: "false" },
|
|
318
|
+
]);
|
|
319
|
+
});
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
describe("toolSpecFromCommand builtins and hidden (Go WalkCobra)", () => {
|
|
323
|
+
function tree(): Command {
|
|
324
|
+
const program = new Command("t");
|
|
325
|
+
program.command("real").action(() => {});
|
|
326
|
+
program.command("man").action(() => {});
|
|
327
|
+
program.command("__complete").action(() => {});
|
|
328
|
+
program.addCommand(completionCommand(program));
|
|
329
|
+
const grp = program.command("grp", { hidden: true });
|
|
330
|
+
grp.command("leaf").action(() => {});
|
|
331
|
+
return program;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
it("skips framework builtins: help, completion and its shells, man, __complete", () => {
|
|
335
|
+
const spec = toolSpecFromCommand(tree());
|
|
336
|
+
expect(spec.commands.map((c) => c.name)).toEqual(["real"]);
|
|
337
|
+
expect(findCommand(spec, "bash")).toBeUndefined();
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
it("includeHidden keeps hidden commands, never builtins", () => {
|
|
341
|
+
const spec = toolSpecFromCommand(tree(), { includeHidden: true });
|
|
342
|
+
expect(spec.commands.map((c) => c.name)).toEqual(["real", "grp"]);
|
|
343
|
+
expect(findCommand(spec, "grp")!.children!.map((c) => c.name)).toEqual(["leaf"]);
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
it("buildManifest skips the same builtins", () => {
|
|
347
|
+
const paths = buildManifest(tree(), "1.1").commands.map((c) => c.path.join(" "));
|
|
348
|
+
expect(paths).toEqual(["t grp leaf", "t real"]);
|
|
349
|
+
});
|
|
350
|
+
});
|
package/src/toolspec.test.ts
CHANGED
|
@@ -27,8 +27,8 @@ describe("loadToolSpec", () => {
|
|
|
27
27
|
]);
|
|
28
28
|
expect(spec.stateIntrospection!.envVars).toEqual([
|
|
29
29
|
"SPACED_FORMAT",
|
|
30
|
-
"SPACED_LOG_LEVEL",
|
|
31
30
|
"SPACED_LOG_FORMAT",
|
|
31
|
+
"SPACED_LOG_LEVEL",
|
|
32
32
|
]);
|
|
33
33
|
});
|
|
34
34
|
|
|
@@ -40,7 +40,7 @@ describe("loadToolSpec", () => {
|
|
|
40
40
|
expect(launch).toBeDefined();
|
|
41
41
|
expect(launch!.intent?.domain).toBe("space");
|
|
42
42
|
expect(launch!.intent?.category).toBe("operations");
|
|
43
|
-
expect(launch!.intent?.tags).toEqual(["
|
|
43
|
+
expect(launch!.intent?.tags).toEqual(["launch", "mission"]);
|
|
44
44
|
});
|
|
45
45
|
|
|
46
46
|
it("loads contract fields", () => {
|
package/src/toolspec.ts
CHANGED
|
@@ -1,15 +1,32 @@
|
|
|
1
1
|
import * as fs from "fs";
|
|
2
2
|
import * as yaml from "js-yaml";
|
|
3
|
+
import type {
|
|
4
|
+
Command as CommanderCommand,
|
|
5
|
+
Option as CommanderOption,
|
|
6
|
+
} from "commander";
|
|
7
|
+
import {
|
|
8
|
+
declaredArgs,
|
|
9
|
+
flagChoices,
|
|
10
|
+
flagTypeAndDefault,
|
|
11
|
+
isBuiltin,
|
|
12
|
+
} from "./toolspec-internal.js";
|
|
3
13
|
|
|
4
14
|
// --- Types ---
|
|
5
15
|
|
|
6
16
|
export type SafetyLevel = "safe" | "caution" | "dangerous";
|
|
7
17
|
|
|
8
18
|
export interface Contract {
|
|
19
|
+
/** True iff kit/idempotent is "yes"; see idempotentClass for "no" / "conditional". */
|
|
9
20
|
idempotent: boolean;
|
|
10
21
|
sideEffects?: string[];
|
|
11
22
|
retryable: boolean;
|
|
12
23
|
preConditions?: string[];
|
|
24
|
+
/**
|
|
25
|
+
* The raw kit/idempotent class when it is one of yes|no|conditional
|
|
26
|
+
* (YAML key `idempotent_class`). "conditional" means the command
|
|
27
|
+
* takes --idempotency-key and a retry must supply one.
|
|
28
|
+
*/
|
|
29
|
+
idempotentClass?: string;
|
|
13
30
|
}
|
|
14
31
|
|
|
15
32
|
export interface Safety {
|
|
@@ -47,14 +64,41 @@ export interface Flag {
|
|
|
47
64
|
short?: string;
|
|
48
65
|
type?: string;
|
|
49
66
|
description?: string;
|
|
67
|
+
/** Flag must be supplied (commander `makeOptionMandatory`). */
|
|
68
|
+
required?: boolean;
|
|
50
69
|
deprecated?: boolean;
|
|
51
70
|
replacedBy?: string;
|
|
71
|
+
/**
|
|
72
|
+
* Default value spelled as pflag prints it ("false" for a bool,
|
|
73
|
+
* "[a,b]" for a list), the same string the manifest's `default`
|
|
74
|
+
* carries; absent when empty.
|
|
75
|
+
*/
|
|
76
|
+
default?: string;
|
|
77
|
+
/** Closed set of legal values (commander `.choices()`, kit WithFlagEnum), in order. */
|
|
78
|
+
choices?: string[];
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** A declared positional argument (mirrors Go toolspec.Arg). */
|
|
82
|
+
export interface Arg {
|
|
83
|
+
name: string;
|
|
84
|
+
/** False/absent for an optional argument. */
|
|
85
|
+
required?: boolean;
|
|
86
|
+
/** True for a `<name...>` argument: takes every remaining operand; always last. */
|
|
87
|
+
variadic?: boolean;
|
|
52
88
|
}
|
|
53
89
|
|
|
54
90
|
export interface Command {
|
|
55
91
|
name: string;
|
|
92
|
+
/** One-line summary of the command. */
|
|
93
|
+
short?: string;
|
|
56
94
|
aliases?: string[];
|
|
57
95
|
flags?: Flag[];
|
|
96
|
+
/** Declared positional arguments, in order. */
|
|
97
|
+
args?: Arg[];
|
|
98
|
+
/** Command takes positional arguments it does not declare. */
|
|
99
|
+
undeclaredArgs?: boolean;
|
|
100
|
+
/** Exit-code symbols the command may produce (kit/exit-codes). */
|
|
101
|
+
exitCodes?: string[];
|
|
58
102
|
children?: Command[];
|
|
59
103
|
contract?: Contract;
|
|
60
104
|
safety?: Safety;
|
|
@@ -103,11 +147,18 @@ function mapFlag(raw: any): Flag {
|
|
|
103
147
|
short: raw.short,
|
|
104
148
|
type: raw.type,
|
|
105
149
|
description: raw.description,
|
|
150
|
+
required: raw.required,
|
|
106
151
|
deprecated: raw.deprecated,
|
|
107
152
|
replacedBy: raw.replaced_by,
|
|
153
|
+
default: raw.default,
|
|
154
|
+
choices: raw.choices,
|
|
108
155
|
};
|
|
109
156
|
}
|
|
110
157
|
|
|
158
|
+
function mapArg(raw: any): Arg {
|
|
159
|
+
return { name: raw.name ?? "", required: raw.required, variadic: raw.variadic };
|
|
160
|
+
}
|
|
161
|
+
|
|
111
162
|
function mapProvenance(raw: any): Provenance | undefined {
|
|
112
163
|
if (!raw) return undefined;
|
|
113
164
|
return {
|
|
@@ -124,6 +175,7 @@ function mapContract(raw: any): Contract | undefined {
|
|
|
124
175
|
sideEffects: raw.side_effects,
|
|
125
176
|
retryable: raw.retryable ?? false,
|
|
126
177
|
preConditions: raw.pre_conditions,
|
|
178
|
+
idempotentClass: raw.idempotent_class,
|
|
127
179
|
};
|
|
128
180
|
}
|
|
129
181
|
|
|
@@ -157,8 +209,12 @@ function mapIntent(raw: any): Intent | undefined {
|
|
|
157
209
|
function mapCommand(raw: any): Command {
|
|
158
210
|
return {
|
|
159
211
|
name: raw.name ?? "",
|
|
212
|
+
short: raw.short,
|
|
160
213
|
aliases: raw.aliases,
|
|
161
214
|
flags: raw.flags?.map(mapFlag),
|
|
215
|
+
args: raw.args?.map(mapArg),
|
|
216
|
+
undeclaredArgs: raw.undeclared_args,
|
|
217
|
+
exitCodes: raw.exit_codes,
|
|
162
218
|
children: raw.children?.map(mapCommand),
|
|
163
219
|
contract: mapContract(raw.contract),
|
|
164
220
|
safety: mapSafety(raw.safety),
|
|
@@ -250,6 +306,9 @@ export function validateToolSpec(spec: ToolSpec): string[] {
|
|
|
250
306
|
` (must be safe|caution|dangerous)`,
|
|
251
307
|
);
|
|
252
308
|
}
|
|
309
|
+
if (cmd.exitCodes?.some((c) => typeof c !== "string" || c.trim() === "")) {
|
|
310
|
+
errors.push(`${path}: exit code symbols must be non-empty strings`);
|
|
311
|
+
}
|
|
253
312
|
if (cmd.children) {
|
|
254
313
|
for (const child of cmd.children) {
|
|
255
314
|
validateCmd(child, `${path}/${child.name || "<unnamed>"}`);
|
|
@@ -277,3 +336,166 @@ export function findCommand(
|
|
|
277
336
|
}
|
|
278
337
|
return undefined;
|
|
279
338
|
}
|
|
339
|
+
|
|
340
|
+
export * from "./toolspec-manifest.js";
|
|
341
|
+
export * from "./toolspec-spec.js";
|
|
342
|
+
|
|
343
|
+
// --- Build from a commander tree ---
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* Per-command fields merged over what the commander tree yields.
|
|
347
|
+
* Structural fields (name, aliases, flags, args, children) always
|
|
348
|
+
* come from the tree.
|
|
349
|
+
*/
|
|
350
|
+
export type CommandAnnotation = Omit<
|
|
351
|
+
Partial<Command>,
|
|
352
|
+
"name" | "aliases" | "flags" | "args" | "children"
|
|
353
|
+
>;
|
|
354
|
+
|
|
355
|
+
export interface FromCommandOptions {
|
|
356
|
+
/** Annotations keyed by space-joined path below the root, e.g. "mission list". */
|
|
357
|
+
annotations?: Record<string, CommandAnnotation>;
|
|
358
|
+
/** Defaults to "1", the version the YAML toolspecs declare. */
|
|
359
|
+
schemaVersion?: string;
|
|
360
|
+
/** Keep hidden commands (Go WithIncludeHidden). Hidden options stay omitted. */
|
|
361
|
+
includeHidden?: boolean;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Build a ToolSpec from a constructed commander tree, so CLIs with
|
|
366
|
+
* dynamically registered subcommands need no hand-synced YAML.
|
|
367
|
+
* Mirrors Go's WalkCobra projection: hidden commands (unless
|
|
368
|
+
* includeHidden) and hidden options are omitted, framework builtins
|
|
369
|
+
* (help, completion and its shells, man, __complete,
|
|
370
|
+
* __completeNoDesc) and kit/spec-command commands are skipped, flag
|
|
371
|
+
* names are undashed, root options become spec.flags.
|
|
372
|
+
*/
|
|
373
|
+
export function toolSpecFromCommand(
|
|
374
|
+
program: CommanderCommand,
|
|
375
|
+
opts: FromCommandOptions = {},
|
|
376
|
+
): ToolSpec {
|
|
377
|
+
const ctx: WalkContext = {
|
|
378
|
+
annotations: opts.annotations ?? {},
|
|
379
|
+
includeHidden: opts.includeHidden ?? false,
|
|
380
|
+
};
|
|
381
|
+
const spec: ToolSpec = {
|
|
382
|
+
name: program.name(),
|
|
383
|
+
schemaVersion: opts.schemaVersion ?? "1",
|
|
384
|
+
commands: projectChildren(program, [], ctx),
|
|
385
|
+
};
|
|
386
|
+
const flags = projectFlags(program);
|
|
387
|
+
if (flags) spec.flags = flags;
|
|
388
|
+
return spec;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
interface WalkContext {
|
|
392
|
+
annotations: Record<string, CommandAnnotation>;
|
|
393
|
+
includeHidden: boolean;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
function projectChildren(
|
|
397
|
+
cmd: CommanderCommand,
|
|
398
|
+
path: string[],
|
|
399
|
+
ctx: WalkContext,
|
|
400
|
+
): Command[] {
|
|
401
|
+
// visibleCommands drops hidden subcommands but appends commander's
|
|
402
|
+
// implicit help placeholder, which is not in cmd.commands.
|
|
403
|
+
const visible = cmd.createHelp().visibleCommands(cmd);
|
|
404
|
+
return cmd.commands
|
|
405
|
+
.filter((c) => c.name() !== "")
|
|
406
|
+
.filter((c) => ctx.includeHidden || visible.includes(c))
|
|
407
|
+
.filter((c) => !isBuiltin(c, cmd))
|
|
408
|
+
.filter((c) => kitAnnotations(c)["kit/spec-command"] !== "true")
|
|
409
|
+
.map((c) => projectCommand(c, [...path, c.name()], ctx));
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
function projectCommand(
|
|
413
|
+
cmd: CommanderCommand,
|
|
414
|
+
path: string[],
|
|
415
|
+
ctx: WalkContext,
|
|
416
|
+
): Command {
|
|
417
|
+
const out: Command = { name: cmd.name() };
|
|
418
|
+
const short = cmd.summary() || cmd.description();
|
|
419
|
+
if (short) out.short = short;
|
|
420
|
+
const aliases = cmd.aliases();
|
|
421
|
+
if (aliases.length > 0) out.aliases = [...aliases];
|
|
422
|
+
const flags = projectFlags(cmd);
|
|
423
|
+
if (flags) out.flags = flags;
|
|
424
|
+
const args: Arg[] = declaredArgs(cmd);
|
|
425
|
+
if (args.length > 0) out.args = args;
|
|
426
|
+
const exitCodes = splitCSV(kitAnnotations(cmd)["kit/exit-codes"]);
|
|
427
|
+
if (exitCodes) out.exitCodes = exitCodes;
|
|
428
|
+
const children = projectChildren(cmd, path, ctx);
|
|
429
|
+
if (children.length > 0) out.children = children;
|
|
430
|
+
const contract = projectContract(kitAnnotations(cmd));
|
|
431
|
+
if (contract) out.contract = contract;
|
|
432
|
+
|
|
433
|
+
const ann = ctx.annotations[path.join(" ")];
|
|
434
|
+
if (ann) {
|
|
435
|
+
for (const [k, v] of Object.entries(ann)) {
|
|
436
|
+
if (v !== undefined) (out as any)[k] = v;
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
return out;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* kit/* annotations on a commander command. commander has no
|
|
444
|
+
* annotations concept; they ride an optional `annotations` property,
|
|
445
|
+
* the same convention the MCP bridge reads.
|
|
446
|
+
*/
|
|
447
|
+
function kitAnnotations(cmd: CommanderCommand): Record<string, string> {
|
|
448
|
+
return (cmd as { annotations?: Record<string, string> }).annotations ?? {};
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
/** Comma-separated annotation value, trimmed, empties dropped. */
|
|
452
|
+
function splitCSV(s: string | undefined): string[] | undefined {
|
|
453
|
+
if (!s) return undefined;
|
|
454
|
+
const out = s.split(",").map((p) => p.trim()).filter((p) => p !== "");
|
|
455
|
+
return out.length > 0 ? out : undefined;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
const IDEMPOTENT_CLASSES = new Set(["yes", "no", "conditional"]);
|
|
459
|
+
|
|
460
|
+
/**
|
|
461
|
+
* The contract Go's projectContract reads off the command's own kit/*
|
|
462
|
+
* annotations: side effect as written, idempotent class (bool true iff
|
|
463
|
+
* "yes"), retryable. Undefined when none is declared, so the key is
|
|
464
|
+
* omitted; the per-path annotation map still overrides it.
|
|
465
|
+
*/
|
|
466
|
+
function projectContract(ann: Record<string, string>): Contract | undefined {
|
|
467
|
+
const sideEffect = ann["kit/side-effect"];
|
|
468
|
+
const cls = ann["kit/idempotent"];
|
|
469
|
+
const retryable = ann["kit/retryable"] === "true";
|
|
470
|
+
const hasClass = cls !== undefined && IDEMPOTENT_CLASSES.has(cls);
|
|
471
|
+
if (!sideEffect && !hasClass && !retryable) return undefined;
|
|
472
|
+
const out: Contract = { idempotent: hasClass && cls === "yes", retryable };
|
|
473
|
+
if (sideEffect) out.sideEffects = [sideEffect];
|
|
474
|
+
if (hasClass) out.idempotentClass = cls;
|
|
475
|
+
return out;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
/**
|
|
479
|
+
* Visible options as Go projects flags: pflag type name and default
|
|
480
|
+
* spelling (the manifest's rule, so a ToolSpec and a manifest built
|
|
481
|
+
* from one tree agree byte-for-byte), required, choices.
|
|
482
|
+
*/
|
|
483
|
+
function projectFlags(cmd: CommanderCommand): Flag[] | undefined {
|
|
484
|
+
const out: Flag[] = [];
|
|
485
|
+
for (const opt of cmd.options as readonly CommanderOption[]) {
|
|
486
|
+
if (opt.hidden) continue;
|
|
487
|
+
const name = (opt.long ?? opt.short ?? "").replace(/^-+/, "");
|
|
488
|
+
if (!name) continue;
|
|
489
|
+
const flag: Flag = { name };
|
|
490
|
+
if (opt.long && opt.short) flag.short = opt.short.replace(/^-/, "");
|
|
491
|
+
const [type, def] = flagTypeAndDefault(opt);
|
|
492
|
+
flag.type = type;
|
|
493
|
+
if (opt.description) flag.description = opt.description;
|
|
494
|
+
if (opt.mandatory) flag.required = true;
|
|
495
|
+
if (def !== "") flag.default = def;
|
|
496
|
+
const choices = flagChoices(opt);
|
|
497
|
+
if (choices) flag.choices = choices;
|
|
498
|
+
out.push(flag);
|
|
499
|
+
}
|
|
500
|
+
return out.length > 0 ? out : undefined;
|
|
501
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# triton
|
|
2
|
+
|
|
3
|
+
## What it answers
|
|
4
|
+
|
|
5
|
+
How to call a model hosted on Triton Inference Server over HTTP with the KServe v2 protocol, including `triton://host:port/model[/version]` URI parsing. Wrong module for chat-style LLM calls (`@hop-top/kit/llm`) and for routing decisions ([`../router`](../router/README.md)).
|
|
6
|
+
|
|
7
|
+
## Use it when
|
|
8
|
+
|
|
9
|
+
- parse a configured endpoint: `parseTritonURI(uri)` returns `{ host, port, modelName, modelVersion }`
|
|
10
|
+
- run inference: `new TritonClient(...)` implementing `TritonScorer`
|
|
11
|
+
- liveness: `ServerHealth`
|
|
12
|
+
|
|
13
|
+
## Quick start
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { parseTritonURI } from './client';
|
|
17
|
+
|
|
18
|
+
console.log(parseTritonURI('triton://localhost:8000/bert_router'));
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Contract
|
|
22
|
+
|
|
23
|
+
- Request and response bodies are the KServe v2 `InferRequest` / `InferResponse` shapes; tensor `datatype` is one of `TritonDataType`.
|
|
24
|
+
- Failures raise `TritonError`.
|
|
25
|
+
- Not on the package exports map and not in the `tsup` build: reachable only by relative import inside `src/`.
|
|
26
|
+
|
|
27
|
+
## Neighbours
|
|
28
|
+
|
|
29
|
+
- [`../router/bert.ts`](../router/bert.ts): the consumer, through the `TritonInfer` interface
|
|
30
|
+
- `@hop-top/kit/llm`: provider clients for chat completions
|
|
31
|
+
|
|
32
|
+
## See also
|
|
33
|
+
|
|
34
|
+
- [KServe v2 inference protocol](https://kserve.github.io/website/modelserving/inference_api/)
|