@hop-top/kit 0.5.0-alpha.7 → 0.5.0-alpha.9

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 (48) hide show
  1. package/CHANGELOG.md +38 -0
  2. package/dist/alias-D9_qFlyP.d.ts +174 -0
  3. package/dist/alias.d.ts +1 -1
  4. package/dist/alias.js +8 -2
  5. package/dist/{chunk-263IZ6Z6.js → chunk-B4ULGCCN.js} +43 -154
  6. package/dist/{chunk-DIDN4X5C.js → chunk-BLS2OBIQ.js} +99 -20
  7. package/dist/{chunk-JTMPNXVQ.js → chunk-P6GRX4RV.js} +486 -83
  8. package/dist/chunk-UZ65A27P.js +147 -0
  9. package/dist/{completion-sDKVXPUL.d.ts → completion-csVun6SP.d.ts} +4 -1
  10. package/dist/completion.d.ts +1 -1
  11. package/dist/{index-B-pqGHhU.d.ts → index-CaOXdl38.d.ts} +337 -24
  12. package/dist/index.d.ts +4 -4
  13. package/dist/index.js +5 -4
  14. package/dist/mcp/index.d.ts +2 -1
  15. package/dist/mcp/index.js +31 -2
  16. package/dist/{toolspec-UrWnoJ3Y.d.ts → toolspec-96rkLfsh.d.ts} +7 -2
  17. package/dist/toolspec.d.ts +1 -1
  18. package/dist/toolspec.js +3 -2
  19. package/package.json +1 -1
  20. package/src/alias.conformance.test.ts +250 -0
  21. package/src/alias.test.ts +96 -1
  22. package/src/alias.ts +179 -19
  23. package/src/completion.ts +4 -1
  24. package/src/mcp/README.md +6 -0
  25. package/src/mcp/args-separator.test.ts +164 -0
  26. package/src/mcp/args.test.ts +223 -0
  27. package/src/mcp/args.ts +160 -0
  28. package/src/mcp/audit.test.ts +520 -0
  29. package/src/mcp/audit.ts +70 -0
  30. package/src/mcp/bridge.test.ts +72 -0
  31. package/src/mcp/bridge.ts +50 -5
  32. package/src/mcp/conformance.test.ts +1 -1
  33. package/src/mcp/dispatch.ts +48 -2
  34. package/src/mcp/index.ts +33 -1
  35. package/src/mcp/legacy-notify.test.ts +59 -0
  36. package/src/mcp/legacy.ts +55 -15
  37. package/src/mcp/modern-confirm.ts +42 -10
  38. package/src/mcp/modern.ts +99 -35
  39. package/src/mcp/stdio.test.ts +313 -0
  40. package/src/mcp/stdio.ts +254 -0
  41. package/src/mcp/toolname.test.ts +147 -0
  42. package/src/mcp/types.ts +125 -18
  43. package/src/toolspec-internal.ts +24 -0
  44. package/src/toolspec-manifest.test.ts +25 -0
  45. package/src/toolspec-manifest.ts +5 -1
  46. package/src/toolspec.from-command.test.ts +51 -0
  47. package/src/toolspec.ts +29 -15
  48. package/dist/alias-BsJ2jGqQ.d.ts +0 -96
@@ -20,6 +20,7 @@ import {
20
20
  isRunnable,
21
21
  pflagTypes,
22
22
  splitCSV,
23
+ takesNoValue,
23
24
  walkCommands,
24
25
  type Annotations,
25
26
  } from "./toolspec-internal.js";
@@ -238,11 +239,14 @@ function flagTypeAndDefault(opt: CommanderOption): [string, string] {
238
239
  const def = opt.defaultValue;
239
240
  const declared = pflagTypes.get(opt);
240
241
  if (declared) return [declared, def === undefined ? "" : String(def)];
241
- if (opt.isBoolean()) return ["bool", def === undefined ? "false" : String(def)];
242
242
  if (opt.variadic) {
243
243
  const items = def === undefined ? [] : Array.isArray(def) ? def : [def];
244
244
  return ["stringSlice", `[${items.map(String).join(",")}]`];
245
245
  }
246
+ // A negated --no-x is its own pflag bool, unset (false) by default;
247
+ // commander's default for it belongs to the x attribute, not the flag.
248
+ if (opt.negate) return ["bool", "false"];
249
+ if (takesNoValue(opt)) return ["bool", def === undefined ? "false" : String(def)];
246
250
  return ["string", def === undefined ? "" : String(def)];
247
251
  }
248
252
 
@@ -3,7 +3,9 @@ import * as fs from "fs";
3
3
  import * as os from "os";
4
4
  import * as path from "path";
5
5
  import { Command, Option } from "commander";
6
+ import { completionCommand } from "./completion";
6
7
  import {
8
+ buildManifest,
7
9
  findCommand,
8
10
  loadToolSpec,
9
11
  toolSpecFromCommand,
@@ -214,3 +216,52 @@ describe("loadToolSpec args/short/required", () => {
214
216
  }
215
217
  });
216
218
  });
219
+
220
+ describe("toolSpecFromCommand negatable flags", () => {
221
+ it("types --no-x as a bool flag named no-x, as pflag does", () => {
222
+ const program = new Command("t");
223
+ program.command("run").option("--no-color", "c").action(() => {});
224
+ expect(findCommand(toolSpecFromCommand(program), "run")!.flags).toEqual([
225
+ { name: "no-color", type: "bool", description: "c" },
226
+ ]);
227
+ });
228
+
229
+ it("types both halves of a --x/--no-x pair bool", () => {
230
+ const program = new Command("t");
231
+ program.option("--hints", "on").option("--no-hints", "off");
232
+ expect(toolSpecFromCommand(program).flags).toEqual([
233
+ { name: "hints", type: "bool", description: "on" },
234
+ { name: "no-hints", type: "bool", description: "off" },
235
+ ]);
236
+ });
237
+ });
238
+
239
+ describe("toolSpecFromCommand builtins and hidden (Go WalkCobra)", () => {
240
+ function tree(): Command {
241
+ const program = new Command("t");
242
+ program.command("real").action(() => {});
243
+ program.command("man").action(() => {});
244
+ program.command("__complete").action(() => {});
245
+ program.addCommand(completionCommand(program));
246
+ const grp = program.command("grp", { hidden: true });
247
+ grp.command("leaf").action(() => {});
248
+ return program;
249
+ }
250
+
251
+ it("skips framework builtins: help, completion and its shells, man, __complete", () => {
252
+ const spec = toolSpecFromCommand(tree());
253
+ expect(spec.commands.map((c) => c.name)).toEqual(["real"]);
254
+ expect(findCommand(spec, "bash")).toBeUndefined();
255
+ });
256
+
257
+ it("includeHidden keeps hidden commands, never builtins", () => {
258
+ const spec = toolSpecFromCommand(tree(), { includeHidden: true });
259
+ expect(spec.commands.map((c) => c.name)).toEqual(["real", "grp"]);
260
+ expect(findCommand(spec, "grp")!.children!.map((c) => c.name)).toEqual(["leaf"]);
261
+ });
262
+
263
+ it("buildManifest skips the same builtins", () => {
264
+ const paths = buildManifest(tree(), "1.1").commands.map((c) => c.path.join(" "));
265
+ expect(paths).toEqual(["t grp leaf", "t real"]);
266
+ });
267
+ });
package/src/toolspec.ts CHANGED
@@ -4,6 +4,7 @@ import type {
4
4
  Command as CommanderCommand,
5
5
  Option as CommanderOption,
6
6
  } from "commander";
7
+ import { declaredArgs, isBuiltin, takesNoValue } from "./toolspec-internal.js";
7
8
 
8
9
  // --- Types ---
9
10
 
@@ -331,47 +332,62 @@ export interface FromCommandOptions {
331
332
  annotations?: Record<string, CommandAnnotation>;
332
333
  /** Defaults to "1", the version the YAML toolspecs declare. */
333
334
  schemaVersion?: string;
335
+ /** Keep hidden commands (Go WithIncludeHidden). Hidden options stay omitted. */
336
+ includeHidden?: boolean;
334
337
  }
335
338
 
336
339
  /**
337
340
  * Build a ToolSpec from a constructed commander tree, so CLIs with
338
341
  * dynamically registered subcommands need no hand-synced YAML.
339
- * Mirrors Go's WalkCobra projection: hidden commands and options are
340
- * omitted, flag names are undashed, root options become spec.flags.
342
+ * Mirrors Go's WalkCobra projection: hidden commands (unless
343
+ * includeHidden) and hidden options are omitted, framework builtins
344
+ * (help, completion and its shells, man, __complete,
345
+ * __completeNoDesc) and kit/spec-command commands are skipped, flag
346
+ * names are undashed, root options become spec.flags.
341
347
  */
342
348
  export function toolSpecFromCommand(
343
349
  program: CommanderCommand,
344
350
  opts: FromCommandOptions = {},
345
351
  ): ToolSpec {
346
- const annotations = opts.annotations ?? {};
352
+ const ctx: WalkContext = {
353
+ annotations: opts.annotations ?? {},
354
+ includeHidden: opts.includeHidden ?? false,
355
+ };
347
356
  const spec: ToolSpec = {
348
357
  name: program.name(),
349
358
  schemaVersion: opts.schemaVersion ?? "1",
350
- commands: projectChildren(program, [], annotations),
359
+ commands: projectChildren(program, [], ctx),
351
360
  };
352
361
  const flags = projectFlags(program);
353
362
  if (flags) spec.flags = flags;
354
363
  return spec;
355
364
  }
356
365
 
366
+ interface WalkContext {
367
+ annotations: Record<string, CommandAnnotation>;
368
+ includeHidden: boolean;
369
+ }
370
+
357
371
  function projectChildren(
358
372
  cmd: CommanderCommand,
359
373
  path: string[],
360
- annotations: Record<string, CommandAnnotation>,
374
+ ctx: WalkContext,
361
375
  ): Command[] {
362
376
  // visibleCommands drops hidden subcommands but appends commander's
363
377
  // implicit help placeholder, which is not in cmd.commands.
364
378
  const visible = cmd.createHelp().visibleCommands(cmd);
365
- return visible
366
- .filter((c) => cmd.commands.includes(c))
379
+ return cmd.commands
380
+ .filter((c) => c.name() !== "")
381
+ .filter((c) => ctx.includeHidden || visible.includes(c))
382
+ .filter((c) => !isBuiltin(c, cmd))
367
383
  .filter((c) => kitAnnotations(c)["kit/spec-command"] !== "true")
368
- .map((c) => projectCommand(c, [...path, c.name()], annotations));
384
+ .map((c) => projectCommand(c, [...path, c.name()], ctx));
369
385
  }
370
386
 
371
387
  function projectCommand(
372
388
  cmd: CommanderCommand,
373
389
  path: string[],
374
- annotations: Record<string, CommandAnnotation>,
390
+ ctx: WalkContext,
375
391
  ): Command {
376
392
  const out: Command = { name: cmd.name() };
377
393
  const short = cmd.summary() || cmd.description();
@@ -380,16 +396,14 @@ function projectCommand(
380
396
  if (aliases.length > 0) out.aliases = [...aliases];
381
397
  const flags = projectFlags(cmd);
382
398
  if (flags) out.flags = flags;
383
- const args = cmd.registeredArguments.map((a): Arg =>
384
- a.required ? { name: a.name(), required: true } : { name: a.name() },
385
- );
399
+ const args: Arg[] = declaredArgs(cmd);
386
400
  if (args.length > 0) out.args = args;
387
401
  const exitCodes = splitCSV(kitAnnotations(cmd)["kit/exit-codes"]);
388
402
  if (exitCodes) out.exitCodes = exitCodes;
389
- const children = projectChildren(cmd, path, annotations);
403
+ const children = projectChildren(cmd, path, ctx);
390
404
  if (children.length > 0) out.children = children;
391
405
 
392
- const ann = annotations[path.join(" ")];
406
+ const ann = ctx.annotations[path.join(" ")];
393
407
  if (ann) {
394
408
  for (const [k, v] of Object.entries(ann)) {
395
409
  if (v !== undefined) (out as any)[k] = v;
@@ -432,7 +446,7 @@ function projectFlags(cmd: CommanderCommand): Flag[] | undefined {
432
446
 
433
447
  /** pflag-style type names, matching the Go walker's output. */
434
448
  function flagType(opt: CommanderOption): string {
435
- if (opt.isBoolean()) return "bool";
436
449
  if (opt.variadic) return "stringSlice";
450
+ if (takesNoValue(opt)) return "bool";
437
451
  return "string";
438
452
  }
@@ -1,96 +0,0 @@
1
- import { Command } from 'commander';
2
-
3
- /**
4
- * alias.ts — YAML-file-based command alias expansion.
5
- *
6
- * Port of Go's cli/alias package. Aliases live in per-tool YAML
7
- * config files with three-tier priority: seeded < global < local.
8
- */
9
-
10
- type AliasMap = Record<string, string>;
11
- interface ExpanderConfig {
12
- globalPath: string;
13
- localPath: string;
14
- seededAliases?: AliasMap;
15
- builtins?: Set<string>;
16
- }
17
- /** Read aliases from a single YAML config file. */
18
- declare function loadFrom(filePath: string): AliasMap;
19
- /** Write aliases into a YAML config file, preserving other keys. */
20
- declare function saveTo(filePath: string, aliases: AliasMap): void;
21
- /**
22
- * Find the first non-flag element in a slice.
23
- * Returns [index, value]. Flags with `=` are self-contained;
24
- * short flags without `=` consume the next element as their value.
25
- */
26
- declare function findFirstNonFlag(slice: string[]): [number, string];
27
- /**
28
- * Simple single-file alias store with load/save, set/get/remove,
29
- * and first-arg expansion.
30
- */
31
- declare class AliasStore {
32
- private filePath;
33
- private aliases;
34
- constructor(filePath: string);
35
- load(): void;
36
- save(): void;
37
- set(name: string, target: string): void;
38
- remove(name: string): void;
39
- get(name: string): string | undefined;
40
- /** Returns a shallow copy. */
41
- all(): AliasMap;
42
- /** Expand first arg if it matches an alias. */
43
- expand(args: string[]): string[];
44
- }
45
- /**
46
- * Three-tier alias expander matching Go's Expander.
47
- * Priority: seeded < global < local.
48
- */
49
- declare class Expander {
50
- private cfg;
51
- constructor(cfg: ExpanderConfig);
52
- /** Merged alias map (seeded < global < local). */
53
- load(): AliasMap;
54
- /**
55
- * Rewrite args if args[1] (or first non-flag arg) matches
56
- * an alias. Returns [rewrittenArgs, wasExpanded].
57
- */
58
- expand(args: string[]): [string[], boolean];
59
- /** Reject names that shadow builtins or contain whitespace. */
60
- validateName(name: string): void;
61
- }
62
- /** Walk the Commander program tree to find a command by space-delimited path. */
63
- declare function findCommand(program: Command, cmdPath: string): Command | undefined;
64
- /**
65
- * Register loaded aliases with Commander's native .alias() API.
66
- *
67
- * Simple single-command aliases use Commander's built-in .alias(),
68
- * giving free completion and help display. Multi-word or unresolved
69
- * targets get a hidden proxy command that dispatches the expanded
70
- * args from the program, like a native subcommand.
71
- *
72
- * Dispatch reuses Commander's own subcommand dispatch rather than
73
- * calling parse()/parseAsync() again: re-parsing resets program
74
- * option values mid-parse and fixes the sync/async mode, whereas
75
- * dispatch returns the target's result as-is. parseAsync() callers
76
- * thus await async targets (rejections propagate), and parse()
77
- * callers keep synchronous throws.
78
- */
79
- declare function bridgeAliases(program: Command, store: AliasStore): void;
80
-
81
- type alias_AliasMap = AliasMap;
82
- type alias_AliasStore = AliasStore;
83
- declare const alias_AliasStore: typeof AliasStore;
84
- type alias_Expander = Expander;
85
- declare const alias_Expander: typeof Expander;
86
- type alias_ExpanderConfig = ExpanderConfig;
87
- declare const alias_bridgeAliases: typeof bridgeAliases;
88
- declare const alias_findCommand: typeof findCommand;
89
- declare const alias_findFirstNonFlag: typeof findFirstNonFlag;
90
- declare const alias_loadFrom: typeof loadFrom;
91
- declare const alias_saveTo: typeof saveTo;
92
- declare namespace alias {
93
- export { type alias_AliasMap as AliasMap, alias_AliasStore as AliasStore, alias_Expander as Expander, type alias_ExpanderConfig as ExpanderConfig, alias_bridgeAliases as bridgeAliases, alias_findCommand as findCommand, alias_findFirstNonFlag as findFirstNonFlag, alias_loadFrom as loadFrom, alias_saveTo as saveTo };
94
- }
95
-
96
- export { type AliasMap as A, Expander as E, alias as a, AliasStore as b, type ExpanderConfig as c, bridgeAliases as d, findFirstNonFlag as e, findCommand as f, loadFrom as l, saveTo as s };