@hop-top/kit 0.5.0-alpha.1 → 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.
Files changed (285) hide show
  1. package/CHANGELOG.md +251 -0
  2. package/LICENSE +1 -1
  3. package/LICENSE-cobra.txt +174 -0
  4. package/NOTICE +15 -0
  5. package/README.md +55 -259
  6. package/dist/aim-DZHwwiIc.d.ts +139 -0
  7. package/dist/aim.d.ts +1 -116
  8. package/dist/aim.js +13 -414
  9. package/dist/alias-D9_qFlyP.d.ts +174 -0
  10. package/dist/alias.d.ts +2 -73
  11. package/dist/alias.js +27 -207
  12. package/dist/api-DAexsu2H.d.ts +77 -0
  13. package/dist/api.d.ts +1 -62
  14. package/dist/api.js +10 -164
  15. package/dist/auth-2-jfgiHn.d.ts +34 -0
  16. package/dist/auth.d.ts +1 -26
  17. package/dist/auth.js +6 -40
  18. package/dist/chunk-2JV3A7OP.js +1894 -0
  19. package/dist/chunk-3FIKGFKI.js +54 -0
  20. package/dist/chunk-4UE5TCMQ.js +104 -0
  21. package/dist/chunk-6HFAEZ52.js +29 -0
  22. package/dist/chunk-6SVOHNIC.js +17 -0
  23. package/dist/chunk-72XZWQX7.js +504 -0
  24. package/dist/chunk-ABQSE7AI.js +513 -0
  25. package/dist/chunk-BDNNPRX6.js +41 -0
  26. package/dist/chunk-BLS2OBIQ.js +263 -0
  27. package/dist/chunk-BRNEKPWX.js +37 -0
  28. package/dist/chunk-CSJWB6D3.js +151 -0
  29. package/dist/chunk-GGWJRWWA.js +309 -0
  30. package/dist/chunk-GHPJJV5L.js +613 -0
  31. package/dist/chunk-IXVQ62NZ.js +334 -0
  32. package/dist/chunk-JKMQSAJP.js +174 -0
  33. package/dist/chunk-JZ22ZUFA.js +1131 -0
  34. package/dist/chunk-K7VCKJ27.js +814 -0
  35. package/dist/chunk-KVG7YCTS.js +115 -0
  36. package/dist/chunk-L23O2FFE.js +51 -0
  37. package/dist/chunk-MQXYDYRD.js +968 -0
  38. package/dist/chunk-MUIQVU6E.js +251 -0
  39. package/dist/chunk-NLMS7HGP.js +178 -0
  40. package/dist/chunk-POR5L2DG.js +247 -0
  41. package/dist/chunk-PSFYJGDG.js +1445 -0
  42. package/dist/chunk-QJVSKKRQ.js +369 -0
  43. package/dist/chunk-QPUCXQXT.js +67 -0
  44. package/dist/chunk-QQQYCWSC.js +240 -0
  45. package/dist/chunk-R5U7XKVJ.js +16 -0
  46. package/dist/chunk-SNVGDYX7.js +187 -0
  47. package/dist/chunk-T46YPHKN.js +669 -0
  48. package/dist/chunk-UKZIAQA7.js +26 -0
  49. package/dist/chunk-WBVO43HU.js +355 -0
  50. package/dist/chunk-WUOAQVOA.js +86 -0
  51. package/dist/chunk-WWUKGJB5.js +90 -0
  52. package/dist/chunk-XJTRY453.js +868 -0
  53. package/dist/chunk-YGYQUFGY.js +219 -0
  54. package/dist/chunk-YOBAUCVN.js +583 -0
  55. package/dist/cli-C5B5tnpA.d.ts +400 -0
  56. package/dist/cli.d.ts +7 -309
  57. package/dist/cli.js +70 -945
  58. package/dist/completion-csVun6SP.d.ts +112 -0
  59. package/dist/completion-values-By-mnjNt.d.ts +142 -0
  60. package/dist/completion-values.d.ts +1 -0
  61. package/dist/completion-values.js +29 -0
  62. package/dist/completion.d.ts +3 -0
  63. package/dist/completion.js +34 -0
  64. package/dist/compliance-AxFf-uMy.d.ts +60 -0
  65. package/dist/compliance.d.ts +1 -0
  66. package/dist/compliance.js +17 -0
  67. package/dist/config-CaK60ByQ.d.ts +241 -0
  68. package/dist/config.d.ts +1 -97
  69. package/dist/config.js +8 -78
  70. package/dist/errcorrect-BcX1Ft0K.d.ts +44 -0
  71. package/dist/errcorrect.d.ts +1 -37
  72. package/dist/errcorrect.js +8 -68
  73. package/dist/error-Do8Qji43.d.ts +231 -0
  74. package/dist/id/index.d.ts +2 -83
  75. package/dist/id/index.js +14 -65
  76. package/dist/index-BKJZClJx.d.ts +1486 -0
  77. package/dist/index-C3Zw4wDJ.d.ts +359 -0
  78. package/dist/index-DCe1boBz.d.ts +328 -0
  79. package/dist/index-VLDcI5NF.d.ts +95 -0
  80. package/dist/index.d.ts +37 -0
  81. package/dist/index.js +132 -0
  82. package/dist/llm-DYuR7_uX.d.ts +1069 -0
  83. package/dist/llm-anthropic-Cd_I2xXB.d.ts +67 -0
  84. package/dist/llm-anthropic.d.ts +2 -0
  85. package/dist/llm-anthropic.js +13 -0
  86. package/dist/llm-openai-DyFrucmV.d.ts +87 -0
  87. package/dist/llm-openai.d.ts +2 -0
  88. package/dist/llm-openai.js +19 -0
  89. package/dist/llm.d.ts +1 -188
  90. package/dist/llm.js +101 -356
  91. package/dist/log-CkaxMX3U.d.ts +115 -0
  92. package/dist/log.d.ts +2 -0
  93. package/dist/log.js +14 -0
  94. package/dist/mcp/index.d.ts +4 -0
  95. package/dist/mcp/index.js +184 -0
  96. package/dist/netpolicy-Brx5XZUB.d.ts +151 -0
  97. package/dist/netpolicy.d.ts +1 -0
  98. package/dist/netpolicy.js +25 -0
  99. package/dist/output-CjZWmL8s.d.ts +420 -0
  100. package/dist/output.d.ts +3 -213
  101. package/dist/output.js +137 -447
  102. package/dist/parity-CvMuYcVI.d.ts +61 -0
  103. package/dist/progress-CAzyxeWl.d.ts +38 -0
  104. package/dist/progress.d.ts +1 -30
  105. package/dist/progress.js +6 -55
  106. package/dist/provenance-SMFHm5gx.d.ts +30 -0
  107. package/dist/provenance.d.ts +1 -24
  108. package/dist/provenance.js +6 -31
  109. package/dist/routellm-B0nv59mH.d.ts +111 -0
  110. package/dist/routellm.d.ts +2 -64
  111. package/dist/routellm.js +13 -255
  112. package/dist/rpc-D31Lg0Kc.d.ts +32 -0
  113. package/dist/rpc.d.ts +2 -25
  114. package/dist/rpc.js +4 -98
  115. package/dist/safety-CUaYEjZc.d.ts +30 -0
  116. package/dist/safety.d.ts +1 -24
  117. package/dist/safety.js +6 -43
  118. package/dist/scope-BrhYB4ij.d.ts +141 -0
  119. package/dist/scope.d.ts +1 -121
  120. package/dist/scope.js +20 -377
  121. package/dist/serve-D8yWMrTc.d.ts +646 -0
  122. package/dist/serve.d.ts +3 -0
  123. package/dist/serve.js +75 -0
  124. package/dist/sqlstore.js +6 -39
  125. package/dist/stream-CrjeCp0M.d.ts +44 -0
  126. package/dist/stream.d.ts +1 -35
  127. package/dist/stream.js +8 -51
  128. package/dist/telemetry/index.d.ts +1 -315
  129. package/dist/telemetry/index.js +26 -599
  130. package/dist/toolspec-DcPwwoAZ.d.ts +368 -0
  131. package/dist/toolspec.d.ts +2 -0
  132. package/dist/toolspec.js +37 -0
  133. package/dist/tui/index.d.ts +6 -298
  134. package/dist/tui/index.js +17 -381
  135. package/dist/upgrade-p_zh3pvV.d.ts +102 -0
  136. package/dist/upgrade.d.ts +1 -95
  137. package/dist/upgrade.js +6 -114
  138. package/dist/xdg-DB7Shoib.d.ts +179 -0
  139. package/dist/xdg.d.ts +1 -169
  140. package/dist/xdg.js +14 -128
  141. package/package.json +192 -24
  142. package/src/README.md +15 -0
  143. package/src/alias.conformance.test.ts +250 -0
  144. package/src/alias.test.ts +206 -2
  145. package/src/alias.ts +196 -21
  146. package/src/cli.help-command.test.ts +105 -0
  147. package/src/cli.offline.test.ts +93 -0
  148. package/src/cli.parity.test.ts +192 -0
  149. package/src/cli.test.ts +95 -1
  150. package/src/cli.ts +195 -25
  151. package/src/completion-scripts.ts +16 -0
  152. package/src/completion-values.test.ts +76 -16
  153. package/src/completion-values.ts +170 -66
  154. package/src/completion.conformance.test.ts +297 -0
  155. package/src/completion.test.ts +256 -145
  156. package/src/completion.ts +767 -121
  157. package/src/compliance.test.ts +196 -2
  158. package/src/compliance.ts +440 -20
  159. package/src/config-merge.test.ts +425 -0
  160. package/src/config.test.ts +228 -1
  161. package/src/config.ts +429 -16
  162. package/src/gen/crud_pb.ts +7 -7
  163. package/src/id/README.md +38 -0
  164. package/src/id/contract.test.ts +1 -1
  165. package/src/id/index.test.ts +1 -1
  166. package/src/id/index.ts +3 -2
  167. package/src/index.test.ts +71 -0
  168. package/src/index.ts +46 -0
  169. package/src/llm-anthropic.conformance.test.ts +1135 -0
  170. package/src/llm-anthropic.test.ts +152 -0
  171. package/src/llm-anthropic.ts +940 -0
  172. package/src/llm-choice-hermetic.test.ts +335 -0
  173. package/src/llm-config.test.ts +232 -0
  174. package/src/llm-config.ts +106 -0
  175. package/src/llm-endpoint.ts +254 -0
  176. package/src/llm-error.ts +17 -0
  177. package/src/llm-errors.ts +152 -0
  178. package/src/llm-file.ts +126 -0
  179. package/src/llm-http.ts +317 -0
  180. package/src/llm-keys.ts +167 -0
  181. package/src/llm-media.conformance.test.ts +87 -0
  182. package/src/llm-media.test.ts +279 -0
  183. package/src/llm-media.ts +322 -0
  184. package/src/llm-openai.conformance.test.ts +407 -0
  185. package/src/llm-openai.test.ts +510 -0
  186. package/src/llm-openai.ts +708 -0
  187. package/src/llm-payload.ts +155 -0
  188. package/src/llm-quote.ts +46 -0
  189. package/src/llm-routellm.conformance.test.ts +44 -0
  190. package/src/llm-toolnames.conformance.test.ts +112 -0
  191. package/src/llm-toolnames.test.ts +132 -0
  192. package/src/llm-toolnames.ts +298 -0
  193. package/src/llm-uri.ts +99 -0
  194. package/src/llm.test.ts +43 -15
  195. package/src/llm.toolcall.test.ts +175 -0
  196. package/src/llm.ts +602 -190
  197. package/src/log.parity.test.ts +105 -0
  198. package/src/log.test.ts +270 -1
  199. package/src/log.ts +228 -49
  200. package/src/mcp/README.md +63 -0
  201. package/src/mcp/args-separator.test.ts +164 -0
  202. package/src/mcp/args.test.ts +223 -0
  203. package/src/mcp/args.ts +160 -0
  204. package/src/mcp/audit.test.ts +520 -0
  205. package/src/mcp/audit.ts +70 -0
  206. package/src/mcp/bridge.test.ts +222 -0
  207. package/src/mcp/bridge.ts +291 -0
  208. package/src/mcp/conformance.test.ts +357 -0
  209. package/src/mcp/dispatch.ts +359 -0
  210. package/src/mcp/identity.test.ts +166 -0
  211. package/src/mcp/identity.ts +72 -0
  212. package/src/mcp/index.ts +176 -0
  213. package/src/mcp/legacy-notify.test.ts +59 -0
  214. package/src/mcp/legacy.ts +367 -0
  215. package/src/mcp/mcp.test.ts +707 -0
  216. package/src/mcp/modern-confirm.test.ts +520 -0
  217. package/src/mcp/modern-confirm.ts +484 -0
  218. package/src/mcp/modern.ts +788 -0
  219. package/src/mcp/stdio.test.ts +313 -0
  220. package/src/mcp/stdio.ts +254 -0
  221. package/src/mcp/tasks.ts +82 -0
  222. package/src/mcp/testtree.ts +250 -0
  223. package/src/mcp/toolname.test.ts +147 -0
  224. package/src/mcp/types.ts +689 -0
  225. package/src/netpolicy.test.ts +280 -0
  226. package/src/netpolicy.ts +256 -0
  227. package/src/output/README.md +46 -0
  228. package/src/output/builtins.ts +5 -5
  229. package/src/output/dispatch.ts +10 -2
  230. package/src/output/error.ts +459 -0
  231. package/src/output/formatter.ts +29 -5
  232. package/src/output/formatters/README.md +50 -0
  233. package/src/output/formatters/csv.ts +48 -12
  234. package/src/output/formatters/json.ts +4 -25
  235. package/src/output/formatters/project.ts +34 -0
  236. package/src/output/formatters/table.ts +8 -6
  237. package/src/output/formatters/text.ts +7 -3
  238. package/src/output/formatters/yaml.ts +5 -22
  239. package/src/output/projection.test.ts +298 -0
  240. package/src/output/projection.ts +33 -59
  241. package/src/output/template.ts +11 -21
  242. package/src/output.surface.test.ts +41 -0
  243. package/src/output.ts +64 -3
  244. package/src/published-types.test.ts +133 -0
  245. package/src/routellm.test.ts +209 -28
  246. package/src/routellm.toolcall.test.ts +521 -0
  247. package/src/routellm.ts +213 -177
  248. package/src/router/README.md +44 -0
  249. package/src/router/server.test.ts +41 -0
  250. package/src/router/server.ts +11 -0
  251. package/src/safety.test.ts +19 -9
  252. package/src/scope-defaults.json +29 -29
  253. package/src/serve.test.ts +1257 -0
  254. package/src/serve.ts +1665 -0
  255. package/src/telemetry/README.md +48 -0
  256. package/src/telemetry/client.test.ts +31 -0
  257. package/src/telemetry/client.ts +12 -6
  258. package/src/telemetry/offline.test.ts +64 -0
  259. package/src/toolspec-internal.ts +279 -0
  260. package/src/toolspec-manifest.test.ts +167 -0
  261. package/src/toolspec-manifest.ts +410 -0
  262. package/src/toolspec-spec-schemas.ts +12 -0
  263. package/src/toolspec-spec.test.ts +273 -0
  264. package/src/toolspec-spec.ts +458 -0
  265. package/src/toolspec.from-command.test.ts +350 -0
  266. package/src/toolspec.test.ts +2 -2
  267. package/src/toolspec.ts +222 -0
  268. package/src/triton/README.md +34 -0
  269. package/src/tui/README.md +42 -0
  270. package/src/tui/parity.test.ts +120 -31
  271. package/src/tui/parity.ts +58 -7
  272. package/src/wizard.test.ts +7 -7
  273. package/test/llm/conformance-helpers.ts +100 -0
  274. package/test/output/csv.test.ts +164 -0
  275. package/test/output/dispatch.test.ts +1 -2
  276. package/test/output/error.test.ts +277 -0
  277. package/test/output/smoke-capture.ts +2 -2
  278. package/test/output/smoke.test.ts +2 -2
  279. package/test/output/taxonomy-contract.test.ts +148 -0
  280. package/test/setup-env.ts +16 -0
  281. package/test/toolspec/fixture.ts +79 -0
  282. package/tsconfig.json +3 -1
  283. package/tsconfig.test.json +10 -0
  284. package/vitest.config.ts +9 -0
  285. package/src/tui/parity.json +0 -74
@@ -0,0 +1,410 @@
1
+ /**
2
+ * Port of Go's `toolspec.Manifest` and `BuildManifest`
3
+ * (go/ai/toolspec/spec.go, go/ai/toolspec/cli/spec_command.go): the
4
+ * flat, snake_case capability manifest a `<tool> spec` command emits,
5
+ * projected from a commander tree carrying kit/* annotations.
6
+ *
7
+ * Byte parity with Go is pinned by
8
+ * sdk/tests/cross-lang/fixtures/toolspec-manifest*.json.
9
+ */
10
+
11
+ import type {
12
+ Command as CommanderCommand,
13
+ Option as CommanderOption,
14
+ } from "commander";
15
+ import {
16
+ annotationsOf,
17
+ declaredArgs,
18
+ encodeGoJSON,
19
+ flagChoices,
20
+ flagTypeAndDefault,
21
+ isBuiltin,
22
+ isHidden,
23
+ isRunnable,
24
+ parseKitArgs,
25
+ splitCSV,
26
+ walkCommands,
27
+ type Annotations,
28
+ } from "./toolspec-internal.js";
29
+
30
+ /** Env var a harness sets to request a manifest schema version. */
31
+ export const SCHEMA_VERSION_ENV = "KIT_TOOLSPEC_SCHEMA";
32
+ /**
33
+ * Newest manifest schema version kit emits. 1.1 added per-command
34
+ * fields; 1.2 adds per-flag `required` and `choices` and per-argument
35
+ * `variadic`. Each is additive over the one before.
36
+ */
37
+ export const LATEST_SCHEMA_VERSION = "1.2";
38
+ const KNOWN_SCHEMA_VERSIONS = ["1.0", "1.1", LATEST_SCHEMA_VERSION];
39
+
40
+ /** Value side_effect / idempotent carry when nothing was declared. */
41
+ export const SIDE_EFFECT_UNKNOWN = "unknown";
42
+ export const IDEMPOTENT_UNKNOWN = "unknown";
43
+
44
+ export interface ManifestArg {
45
+ name: string;
46
+ required?: boolean;
47
+ /** Schema 1.2: a kit/args `name...` entry, always the last argument. */
48
+ variadic?: boolean;
49
+ }
50
+
51
+ export interface ManifestFlag {
52
+ name: string;
53
+ short?: string;
54
+ type?: string;
55
+ description?: string;
56
+ default?: string;
57
+ since_version?: string;
58
+ /** Schema 1.2: commander makeOptionMandatory (cobra MarkFlagRequired). */
59
+ required?: boolean;
60
+ /** Schema 1.2: commander `.choices()` (kit WithFlagEnum), in order. */
61
+ choices?: string[];
62
+ }
63
+
64
+ export interface ManifestExample {
65
+ title: string;
66
+ command: string;
67
+ output?: string;
68
+ }
69
+
70
+ export interface ManifestNextStep {
71
+ when?: string;
72
+ suggest: string;
73
+ reason?: string;
74
+ }
75
+
76
+ /** One invocable command. Field order is the Go struct order. */
77
+ export interface ManifestCommand {
78
+ path: string[];
79
+ short: string;
80
+ side_effect: string;
81
+ idempotent: string;
82
+ args?: ManifestArg[];
83
+ flags?: ManifestFlag[];
84
+ exit_codes?: string[];
85
+ deprecated?: boolean;
86
+ deprecated_since?: string;
87
+ removal_target?: string;
88
+ since_version?: string;
89
+ long?: string;
90
+ retryable: boolean;
91
+ /** Raw JSON Schema text, embedded verbatim (Go RawJSON). */
92
+ output_schema?: string;
93
+ output_schema_version?: string;
94
+ examples?: ManifestExample[];
95
+ next_steps?: ManifestNextStep[];
96
+ top_level_verb?: boolean;
97
+ hierarchical?: boolean;
98
+ passthrough?: boolean;
99
+ destructive_token_required?: boolean;
100
+ dry_run_supported: boolean;
101
+ dry_run_rationale?: string;
102
+ reserved?: boolean;
103
+ hidden?: boolean;
104
+ }
105
+
106
+ export interface Manifest {
107
+ tool: string;
108
+ version: string;
109
+ schema_version: string;
110
+ commands: ManifestCommand[];
111
+ }
112
+
113
+ export interface BuildManifestOptions {
114
+ /** Include deprecated commands (Go --include-deprecated). */
115
+ includeDeprecated?: boolean;
116
+ /** Depth-1 verbs the tool reserves (Go Root.IsReserved). */
117
+ reserved?: Iterable<string>;
118
+ /**
119
+ * The command being executed, if any. It gets the -h/--help flag
120
+ * cobra adds to the executing command, as a live Go run lists.
121
+ */
122
+ invokedCommand?: CommanderCommand;
123
+ }
124
+
125
+ // --- Schema negotiation ---
126
+
127
+ function parseSchemaVersion(s: string): [number, number] | undefined {
128
+ const t = s.trim();
129
+ const dot = t.indexOf(".");
130
+ if (dot < 0) return undefined;
131
+ const parts = [t.slice(0, dot), t.slice(dot + 1)];
132
+ const out: number[] = [];
133
+ for (const p of parts) {
134
+ if (!/^[0-9]+$/.test(p)) return undefined;
135
+ const n = Number(p);
136
+ if (!Number.isSafeInteger(n)) return undefined;
137
+ out.push(n);
138
+ }
139
+ return [out[0], out[1]];
140
+ }
141
+
142
+ function after(a: [number, number], b: [number, number]): boolean {
143
+ return a[0] !== b[0] ? a[0] > b[0] : a[1] > b[1];
144
+ }
145
+
146
+ /**
147
+ * Resolve the emitted schema_version (Go NegotiateSchemaVersion):
148
+ * declared is a floor; a well-formed request raises it to the highest
149
+ * known version not above the request; nothing is ever refused.
150
+ */
151
+ export function negotiateSchemaVersion(
152
+ declared: string,
153
+ requested: string,
154
+ ): string {
155
+ const floor = parseSchemaVersion(declared);
156
+ if (!floor) return declared;
157
+ const ceiling = parseSchemaVersion(requested);
158
+ if (!ceiling) return declared;
159
+ let resolved = declared;
160
+ let best = floor;
161
+ for (const v of KNOWN_SCHEMA_VERSIONS) {
162
+ const known = parseSchemaVersion(v)!;
163
+ if (after(known, best) && !after(known, ceiling)) {
164
+ resolved = v;
165
+ best = known;
166
+ }
167
+ }
168
+ return resolved;
169
+ }
170
+
171
+ // --- Annotation reading (Go cmdreflect / cmdmeta) ---
172
+
173
+ function flagSince(ann: Annotations): Map<string, string> {
174
+ const out = new Map<string, string>();
175
+ for (const entry of (ann["kit/flag-since"] ?? "").split(",")) {
176
+ const e = entry.trim();
177
+ const eq = e.indexOf("=");
178
+ if (eq < 0) continue;
179
+ out.set(e.slice(0, eq).trim(), e.slice(eq + 1).trim());
180
+ }
181
+ return out;
182
+ }
183
+
184
+ /** Go's json decoding matches keys exactly first, then case-insensitively. */
185
+ function fieldOf(rec: Record<string, unknown>, name: string): unknown {
186
+ if (name in rec) return rec[name];
187
+ const k = Object.keys(rec).find((x) => x.toLowerCase() === name);
188
+ return k === undefined ? undefined : rec[k];
189
+ }
190
+
191
+ /** Decode a JSON list annotation; absent or malformed yields undefined. */
192
+ function decodeList<T>(
193
+ raw: string | undefined,
194
+ fields: { name: string; required: boolean }[],
195
+ ): T[] | undefined {
196
+ if (!raw) return undefined;
197
+ let parsed: unknown;
198
+ try {
199
+ parsed = JSON.parse(raw);
200
+ } catch {
201
+ return undefined;
202
+ }
203
+ if (parsed === null) return [];
204
+ if (!Array.isArray(parsed)) return undefined;
205
+ const out: T[] = [];
206
+ for (const item of parsed) {
207
+ if (item === null) {
208
+ out.push(Object.fromEntries(fields.filter((f) => f.required).map((f) => [f.name, ""])) as T);
209
+ continue;
210
+ }
211
+ if (typeof item !== "object" || Array.isArray(item)) return undefined;
212
+ const rec: Record<string, string> = {};
213
+ for (const f of fields) {
214
+ const v = fieldOf(item as Record<string, unknown>, f.name);
215
+ if (v === undefined || v === null) {
216
+ if (f.required) rec[f.name] = "";
217
+ continue;
218
+ }
219
+ if (typeof v !== "string") return undefined;
220
+ if (v !== "" || f.required) rec[f.name] = v;
221
+ }
222
+ out.push(rec as T);
223
+ }
224
+ return out;
225
+ }
226
+
227
+ const DRY_RUN_SIDE_EFFECTS = new Set([
228
+ "write",
229
+ "write-local",
230
+ "write-shared",
231
+ "destructive",
232
+ "destructive-local",
233
+ "destructive-shared",
234
+ ]);
235
+
236
+ function dryRunSupported(ann: Annotations): boolean {
237
+ switch (ann["kit/dry-run"]) {
238
+ case "opted-out":
239
+ return false;
240
+ case "supported":
241
+ return true;
242
+ }
243
+ return DRY_RUN_SIDE_EFFECTS.has(ann["kit/side-effect"] ?? "");
244
+ }
245
+
246
+ function manifestFlags(
247
+ cmd: CommanderCommand,
248
+ ann: Annotations,
249
+ withHelp: boolean,
250
+ ): ManifestFlag[] | undefined {
251
+ const since = flagSince(ann);
252
+ const visible = (cmd.options as readonly CommanderOption[]).filter((o) => !o.hidden);
253
+ const flags: ManifestFlag[] = [];
254
+ for (const o of visible) {
255
+ const name = (o.long ?? o.short ?? "").replace(/^-+/, "");
256
+ if (name === "") continue;
257
+ const [type, def] = flagTypeAndDefault(o);
258
+ const f: ManifestFlag = { name };
259
+ if (o.long && o.short) f.short = o.short.replace(/^-/, "");
260
+ f.type = type;
261
+ if (o.description) f.description = o.description;
262
+ if (def !== "") f.default = def;
263
+ const sv = since.get(name);
264
+ if (sv) f.since_version = sv;
265
+ if (o.mandatory) f.required = true;
266
+ const choices = flagChoices(o);
267
+ if (choices) f.choices = choices;
268
+ flags.push(f);
269
+ }
270
+ // cobra adds -h/--help to the command it executes (and only that
271
+ // one) before its RunE runs, so a live Go spec lists it there.
272
+ if (withHelp && !flags.some((f) => f.name === "help")) {
273
+ const help: ManifestFlag = { name: "help" };
274
+ if (!flags.some((f) => f.short === "h")) help.short = "h";
275
+ help.type = "bool";
276
+ help.description = `help for ${cmd.name()}`;
277
+ help.default = "false";
278
+ flags.push(help);
279
+ }
280
+ flags.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
281
+ return flags.length > 0 ? flags : undefined;
282
+ }
283
+
284
+ /**
285
+ * Declared positionals: kit/args when set (Go cmdmeta.ParseArgs:
286
+ * `name?` optional, `name...` variadic, `name...?` both), else
287
+ * commander's registered arguments. Keys in Go struct order.
288
+ */
289
+ function manifestArgs(cmd: CommanderCommand, ann: Annotations): ManifestArg[] | undefined {
290
+ const declared = parseKitArgs(ann["kit/args"]);
291
+ const args: ManifestArg[] = declared
292
+ ? declared.map((a) => {
293
+ const out: ManifestArg = { name: a.name };
294
+ if (!a.optional) out.required = true;
295
+ if (a.variadic) out.variadic = true;
296
+ return out;
297
+ })
298
+ : declaredArgs(cmd);
299
+ return args.length > 0 ? args : undefined;
300
+ }
301
+
302
+ function manifestCommand(
303
+ cmd: CommanderCommand,
304
+ path: string[],
305
+ hidden: boolean,
306
+ reserved: boolean,
307
+ invoked: boolean,
308
+ ): ManifestCommand {
309
+ const ann = annotationsOf(cmd);
310
+ // commander's summary() is the one-liner when set; description()
311
+ // then holds the long text (cobra Short / Long).
312
+ const summary = cmd.summary();
313
+ const short = summary || cmd.description();
314
+ const long = summary ? cmd.description() : "";
315
+
316
+ // Keys are inserted in Go struct order; the encoder keeps it.
317
+ const out = {
318
+ path,
319
+ short,
320
+ side_effect: ann["kit/side-effect"] || SIDE_EFFECT_UNKNOWN,
321
+ idempotent: ann["kit/idempotent"] || IDEMPOTENT_UNKNOWN,
322
+ } as ManifestCommand;
323
+
324
+ const args = manifestArgs(cmd, ann);
325
+ if (args) out.args = args;
326
+ const flags = manifestFlags(cmd, ann, invoked);
327
+ if (flags) out.flags = flags;
328
+ const exitCodes = splitCSV(ann["kit/exit-codes"]);
329
+ if (exitCodes) out.exit_codes = exitCodes;
330
+
331
+ const deprecatedSince = ann["kit/deprecated-since"] ?? "";
332
+ const removalTarget = ann["kit/removal-target"] ?? "";
333
+ if (deprecatedSince || removalTarget) out.deprecated = true;
334
+ if (deprecatedSince) out.deprecated_since = deprecatedSince;
335
+ if (removalTarget) out.removal_target = removalTarget;
336
+ if (ann["kit/since"]) out.since_version = ann["kit/since"];
337
+
338
+ if (long) out.long = long;
339
+ out.retryable = ann["kit/retryable"] === "true";
340
+ const schema = ann["kit/output-schema"];
341
+ if (schema) {
342
+ out.output_schema = schema;
343
+ if (ann["kit/output-schema-version"]) {
344
+ out.output_schema_version = ann["kit/output-schema-version"];
345
+ }
346
+ }
347
+ const examples = decodeList<ManifestExample>(ann["kit/examples"], [
348
+ { name: "title", required: true },
349
+ { name: "command", required: true },
350
+ { name: "output", required: false },
351
+ ]);
352
+ if (examples && examples.length > 0) out.examples = examples;
353
+ const nextSteps = decodeList<ManifestNextStep>(ann["kit/next-steps"], [
354
+ { name: "when", required: false },
355
+ { name: "suggest", required: true },
356
+ { name: "reason", required: false },
357
+ ]);
358
+ if (nextSteps && nextSteps.length > 0) out.next_steps = nextSteps;
359
+
360
+ if (ann["kit/top-level-verb"] === "true") out.top_level_verb = true;
361
+ if (ann["kit/hierarchical"] === "true") out.hierarchical = true;
362
+ if (ann["kit/passthrough"] === "true") out.passthrough = true;
363
+ const token = ann["kit/destructive-token"];
364
+ if (token === "required" || token === "true") out.destructive_token_required = true;
365
+ out.dry_run_supported = dryRunSupported(ann);
366
+ if (ann["kit/dry-run-rationale"]) out.dry_run_rationale = ann["kit/dry-run-rationale"];
367
+ if (reserved) out.reserved = true;
368
+ if (hidden) out.hidden = true;
369
+ return out;
370
+ }
371
+
372
+ /**
373
+ * Project a commander tree into the Go-shaped Manifest (Go
374
+ * BuildManifest). Lists every runnable command except framework
375
+ * builtins (help, completion, man) and, unless includeDeprecated,
376
+ * deprecated ones; hidden and reserved commands stay listed and say
377
+ * so. Children are visited in name order and flags sorted by name,
378
+ * as cobra and pflag do by default.
379
+ */
380
+ export function buildManifest(
381
+ program: CommanderCommand,
382
+ schemaVersion: string,
383
+ opts: BuildManifestOptions = {},
384
+ ): Manifest {
385
+ const m: Manifest = {
386
+ tool: program.name(),
387
+ version: program.version() ?? "",
388
+ schema_version: schemaVersion,
389
+ commands: [],
390
+ };
391
+ walkCommands(program, opts.reserved, (cmd, parent, path, reserved) => {
392
+ const ann = annotationsOf(cmd);
393
+ const deprecated = !!(ann["kit/deprecated-since"] || ann["kit/removal-target"]);
394
+ if (!isRunnable(cmd) || isBuiltin(cmd, parent)) return;
395
+ if (deprecated && !opts.includeDeprecated) return;
396
+ m.commands.push(
397
+ manifestCommand(cmd, path, isHidden(cmd, parent), reserved, cmd === opts.invokedCommand),
398
+ );
399
+ });
400
+ return m;
401
+ }
402
+
403
+ /**
404
+ * Render a Manifest exactly as Go's `output.Render(w, "json", m)`:
405
+ * two-space indent, HTML-escaped strings, trailing newline, fields
406
+ * in Go struct order with omitempty applied.
407
+ */
408
+ export function renderManifestJSON(m: Manifest): string {
409
+ return encodeGoJSON(m, 0, new Set(["output_schema"])) + "\n";
410
+ }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * kit/output-schema values the `spec` and `spec coverage` commands
3
+ * carry: the JSON Schemas Go reflects from toolspec.Manifest and
4
+ * CoverageReport. Copied verbatim from contracts/toolspec-spec-v1/
5
+ * (generated by Go's TestSpecOutputSchemaContract); a test pins them.
6
+ */
7
+
8
+ export const MANIFEST_OUTPUT_SCHEMA =
9
+ "{\"$schema\":\"https://json-schema.org/draft/2020-12/schema\",\"$id\":\"https://hop.top/kit/go/ai/toolspec/manifest\",\"$ref\":\"#/$defs/Manifest\",\"$defs\":{\"Manifest\":{\"properties\":{\"tool\":{\"type\":\"string\"},\"version\":{\"type\":\"string\"},\"schema_version\":{\"type\":\"string\"},\"commands\":{\"items\":{\"$ref\":\"#/$defs/ManifestCommand\"},\"type\":\"array\"}},\"additionalProperties\":false,\"type\":\"object\",\"required\":[\"tool\",\"version\",\"schema_version\",\"commands\"]},\"ManifestArg\":{\"properties\":{\"name\":{\"type\":\"string\"},\"required\":{\"type\":\"boolean\"},\"variadic\":{\"type\":\"boolean\"}},\"additionalProperties\":false,\"type\":\"object\",\"required\":[\"name\"]},\"ManifestCommand\":{\"properties\":{\"path\":{\"items\":{\"type\":\"string\"},\"type\":\"array\"},\"short\":{\"type\":\"string\"},\"side_effect\":{\"type\":\"string\"},\"idempotent\":{\"type\":\"string\"},\"args\":{\"items\":{\"$ref\":\"#/$defs/ManifestArg\"},\"type\":\"array\"},\"flags\":{\"items\":{\"$ref\":\"#/$defs/ManifestFlag\"},\"type\":\"array\"},\"exit_codes\":{\"items\":{\"type\":\"string\"},\"type\":\"array\"},\"deprecated\":{\"type\":\"boolean\"},\"deprecated_since\":{\"type\":\"string\"},\"removal_target\":{\"type\":\"string\"},\"since_version\":{\"type\":\"string\"},\"long\":{\"type\":\"string\"},\"retryable\":{\"type\":\"boolean\"},\"output_schema\":{\"$ref\":\"#/$defs/RawJSON\"},\"output_schema_version\":{\"type\":\"string\"},\"examples\":{\"items\":{\"$ref\":\"#/$defs/ManifestExample\"},\"type\":\"array\"},\"next_steps\":{\"items\":{\"$ref\":\"#/$defs/ManifestNextStep\"},\"type\":\"array\"},\"top_level_verb\":{\"type\":\"boolean\"},\"hierarchical\":{\"type\":\"boolean\"},\"passthrough\":{\"type\":\"boolean\"},\"destructive_token_required\":{\"type\":\"boolean\"},\"dry_run_supported\":{\"type\":\"boolean\"},\"dry_run_rationale\":{\"type\":\"string\"},\"reserved\":{\"type\":\"boolean\"},\"hidden\":{\"type\":\"boolean\"}},\"additionalProperties\":false,\"type\":\"object\",\"required\":[\"path\",\"short\",\"side_effect\",\"idempotent\",\"retryable\",\"dry_run_supported\"]},\"ManifestExample\":{\"properties\":{\"title\":{\"type\":\"string\"},\"command\":{\"type\":\"string\"},\"output\":{\"type\":\"string\"}},\"additionalProperties\":false,\"type\":\"object\",\"required\":[\"title\",\"command\"]},\"ManifestFlag\":{\"properties\":{\"name\":{\"type\":\"string\"},\"short\":{\"type\":\"string\"},\"type\":{\"type\":\"string\"},\"description\":{\"type\":\"string\"},\"default\":{\"type\":\"string\"},\"since_version\":{\"type\":\"string\"},\"required\":{\"type\":\"boolean\"},\"choices\":{\"items\":{\"type\":\"string\"},\"type\":\"array\"}},\"additionalProperties\":false,\"type\":\"object\",\"required\":[\"name\"]},\"ManifestNextStep\":{\"properties\":{\"when\":{\"type\":\"string\"},\"suggest\":{\"type\":\"string\"},\"reason\":{\"type\":\"string\"}},\"additionalProperties\":false,\"type\":\"object\",\"required\":[\"suggest\"]},\"RawJSON\":{\"type\":\"string\",\"contentEncoding\":\"base64\"}}}";
10
+
11
+ export const COVERAGE_REPORT_OUTPUT_SCHEMA =
12
+ "{\"$schema\":\"https://json-schema.org/draft/2020-12/schema\",\"$id\":\"https://hop.top/kit/go/ai/toolspec/cli/coverage-report\",\"$ref\":\"#/$defs/CoverageReport\",\"$defs\":{\"CoverageReport\":{\"properties\":{\"tool\":{\"type\":\"string\"},\"total\":{\"type\":\"integer\"},\"annotated\":{\"type\":\"integer\"},\"unannotated\":{\"type\":\"integer\"},\"inferred\":{\"type\":\"integer\"},\"malformed\":{\"type\":\"integer\"},\"unannotated_paths\":{\"items\":{\"type\":\"string\"},\"type\":\"array\"},\"malformed_paths\":{\"items\":{\"type\":\"string\"},\"type\":\"array\"},\"measurable\":{\"type\":\"boolean\"}},\"additionalProperties\":false,\"type\":\"object\",\"required\":[\"tool\",\"total\",\"annotated\",\"unannotated\",\"inferred\",\"malformed\",\"measurable\"]}}}";