@hop-top/kit 0.4.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (174) hide show
  1. package/CHANGELOG.md +86 -0
  2. package/LICENSE +25 -0
  3. package/README.md +189 -0
  4. package/dist/aim.d.ts +116 -0
  5. package/dist/aim.js +417 -0
  6. package/dist/alias.d.ts +73 -0
  7. package/dist/alias.js +207 -0
  8. package/dist/api.d.ts +62 -0
  9. package/dist/api.js +165 -0
  10. package/dist/auth.d.ts +26 -0
  11. package/dist/auth.js +41 -0
  12. package/dist/cli.d.ts +309 -0
  13. package/dist/cli.js +885 -0
  14. package/dist/config.d.ts +97 -0
  15. package/dist/config.js +79 -0
  16. package/dist/errcorrect.d.ts +37 -0
  17. package/dist/errcorrect.js +69 -0
  18. package/dist/llm.d.ts +188 -0
  19. package/dist/llm.js +356 -0
  20. package/dist/output.d.ts +213 -0
  21. package/dist/output.js +447 -0
  22. package/dist/progress.d.ts +30 -0
  23. package/dist/progress.js +56 -0
  24. package/dist/provenance.d.ts +24 -0
  25. package/dist/provenance.js +32 -0
  26. package/dist/routellm.d.ts +64 -0
  27. package/dist/routellm.js +258 -0
  28. package/dist/rpc.d.ts +25 -0
  29. package/dist/rpc.js +101 -0
  30. package/dist/safety.d.ts +24 -0
  31. package/dist/safety.js +44 -0
  32. package/dist/scope.d.ts +121 -0
  33. package/dist/scope.js +380 -0
  34. package/dist/sqlstore.d.ts +84 -0
  35. package/dist/sqlstore.js +85 -0
  36. package/dist/stream.d.ts +35 -0
  37. package/dist/stream.js +52 -0
  38. package/dist/tui/index.d.ts +300 -0
  39. package/dist/tui/index.js +332 -0
  40. package/dist/upgrade.d.ts +95 -0
  41. package/dist/upgrade.js +115 -0
  42. package/dist/xdg.d.ts +169 -0
  43. package/dist/xdg.js +129 -0
  44. package/eslint.config.mjs +17 -0
  45. package/package.json +133 -0
  46. package/src/README.md +3 -0
  47. package/src/aim.test.ts +331 -0
  48. package/src/aim.ts +388 -0
  49. package/src/alias.test.ts +423 -0
  50. package/src/alias.ts +256 -0
  51. package/src/api.test.ts +190 -0
  52. package/src/api.ts +214 -0
  53. package/src/auth.test.ts +24 -0
  54. package/src/auth.ts +37 -0
  55. package/src/bus.test.ts +178 -0
  56. package/src/bus.ts +114 -0
  57. package/src/cli.test.ts +690 -0
  58. package/src/cli.ts +741 -0
  59. package/src/completion-values.test.ts +202 -0
  60. package/src/completion-values.ts +173 -0
  61. package/src/completion.test.ts +212 -0
  62. package/src/completion.ts +158 -0
  63. package/src/compliance.test.ts +134 -0
  64. package/src/compliance.ts +669 -0
  65. package/src/config.test.ts +110 -0
  66. package/src/config.ts +164 -0
  67. package/src/errcorrect.test.ts +134 -0
  68. package/src/errcorrect.ts +84 -0
  69. package/src/flagregister.test.ts +103 -0
  70. package/src/flagregister.ts +78 -0
  71. package/src/gen/crud_connect.ts +76 -0
  72. package/src/gen/crud_pb.ts +269 -0
  73. package/src/hint.test.ts +315 -0
  74. package/src/hint.ts +178 -0
  75. package/src/llm.test.ts +612 -0
  76. package/src/llm.ts +577 -0
  77. package/src/log.test.ts +145 -0
  78. package/src/log.ts +155 -0
  79. package/src/output/builtins.ts +34 -0
  80. package/src/output/dispatch.ts +195 -0
  81. package/src/output/flags.ts +151 -0
  82. package/src/output/format_help.ts +80 -0
  83. package/src/output/formatter.ts +167 -0
  84. package/src/output/formatters/csv.ts +72 -0
  85. package/src/output/formatters/json.ts +44 -0
  86. package/src/output/formatters/table.ts +44 -0
  87. package/src/output/formatters/text.ts +109 -0
  88. package/src/output/formatters/yaml.ts +47 -0
  89. package/src/output/projection.ts +126 -0
  90. package/src/output/registry.ts +88 -0
  91. package/src/output/template.ts +63 -0
  92. package/src/output.test.ts +117 -0
  93. package/src/output.ts +90 -0
  94. package/src/progress.test.ts +93 -0
  95. package/src/progress.ts +58 -0
  96. package/src/provenance.test.ts +59 -0
  97. package/src/provenance.ts +26 -0
  98. package/src/routellm.test.ts +179 -0
  99. package/src/routellm.ts +365 -0
  100. package/src/router/bert.test.ts +180 -0
  101. package/src/router/bert.ts +145 -0
  102. package/src/router/controller.test.ts +183 -0
  103. package/src/router/controller.ts +165 -0
  104. package/src/router/integration.test.ts +309 -0
  105. package/src/router/intent.test.ts +187 -0
  106. package/src/router/intent.ts +208 -0
  107. package/src/router/mf.test.ts +66 -0
  108. package/src/router/mf.ts +52 -0
  109. package/src/router/random.test.ts +23 -0
  110. package/src/router/random.ts +13 -0
  111. package/src/router/router.test.ts +31 -0
  112. package/src/router/router.ts +62 -0
  113. package/src/router/server.test.ts +193 -0
  114. package/src/router/server.ts +134 -0
  115. package/src/router/sw.test.ts +199 -0
  116. package/src/router/sw.ts +232 -0
  117. package/src/rpc.test.ts +47 -0
  118. package/src/rpc.ts +91 -0
  119. package/src/safety.test.ts +53 -0
  120. package/src/safety.ts +42 -0
  121. package/src/scope-defaults.json +72 -0
  122. package/src/scope.test.ts +177 -0
  123. package/src/scope.ts +382 -0
  124. package/src/setflag.test.ts +110 -0
  125. package/src/setflag.ts +102 -0
  126. package/src/sqlstore.test.ts +110 -0
  127. package/src/sqlstore.ts +144 -0
  128. package/src/stream.test.ts +69 -0
  129. package/src/stream.ts +47 -0
  130. package/src/textflag.test.ts +105 -0
  131. package/src/textflag.ts +68 -0
  132. package/src/toolspec.test.ts +187 -0
  133. package/src/toolspec.ts +279 -0
  134. package/src/triton/client.test.ts +205 -0
  135. package/src/triton/client.ts +261 -0
  136. package/src/tui/anim.test.ts +146 -0
  137. package/src/tui/anim.ts +134 -0
  138. package/src/tui/badge.test.ts +41 -0
  139. package/src/tui/badge.ts +55 -0
  140. package/src/tui/confirm.test.ts +68 -0
  141. package/src/tui/confirm.ts +49 -0
  142. package/src/tui/index.ts +29 -0
  143. package/src/tui/list.test.ts +84 -0
  144. package/src/tui/list.ts +72 -0
  145. package/src/tui/parity.ts +41 -0
  146. package/src/tui/pills.test.ts +40 -0
  147. package/src/tui/pills.ts +43 -0
  148. package/src/tui/progress.test.ts +164 -0
  149. package/src/tui/progress.ts +120 -0
  150. package/src/tui/spinner.test.ts +111 -0
  151. package/src/tui/spinner.ts +96 -0
  152. package/src/tui/status.test.ts +68 -0
  153. package/src/tui/status.ts +66 -0
  154. package/src/upgrade.test.ts +298 -0
  155. package/src/upgrade.ts +205 -0
  156. package/src/uri.ts +199 -0
  157. package/src/wizard.test.ts +441 -0
  158. package/src/wizard.ts +662 -0
  159. package/src/xdg.test.ts +202 -0
  160. package/src/xdg.ts +280 -0
  161. package/test/output/cols.test.ts +148 -0
  162. package/test/output/csv.test.ts +129 -0
  163. package/test/output/dispatch.test.ts +194 -0
  164. package/test/output/format_help.test.ts +131 -0
  165. package/test/output/formatter.test.ts +137 -0
  166. package/test/output/output_flag.test.ts +124 -0
  167. package/test/output/registry.test.ts +127 -0
  168. package/test/output/smoke-capture.ts +83 -0
  169. package/test/output/smoke.test.ts +142 -0
  170. package/test/output/template.test.ts +95 -0
  171. package/test/output/text.test.ts +75 -0
  172. package/test/testdata/api-fixture.json +115 -0
  173. package/test/testdata/query-vectors.json +187 -0
  174. package/tsconfig.json +12 -0
package/src/wizard.ts ADDED
@@ -0,0 +1,662 @@
1
+ /**
2
+ * Headless sequential wizard engine — TypeScript port of kit/wizard.
3
+ */
4
+
5
+ // --- enums ---
6
+
7
+ export enum StepKind {
8
+ TextInput = "text_input",
9
+ Select = "select",
10
+ Confirm = "confirm",
11
+ MultiSelect = "multi_select",
12
+ Action = "action",
13
+ Summary = "summary",
14
+ }
15
+
16
+ export enum ErrorAction {
17
+ Abort = 0,
18
+ Retry = 1,
19
+ Skip = 2,
20
+ }
21
+
22
+ // --- types ---
23
+
24
+ export interface Option {
25
+ value: string;
26
+ label: string;
27
+ description?: string;
28
+ }
29
+
30
+ export interface Condition {
31
+ key: string;
32
+ pred: (value: unknown) => boolean;
33
+ }
34
+
35
+ export type ActionFn = (
36
+ results: Record<string, unknown>,
37
+ ) => void | Promise<void>;
38
+
39
+ export type ValidateTextFn = (value: string) => void;
40
+ export type ValidateChoiceFn = (value: string) => void;
41
+ export type ValidateChoicesFn = (values: string[]) => void;
42
+ export type FormatFn = (results: Record<string, unknown>) => string;
43
+
44
+ // --- Step ---
45
+
46
+ export class Step {
47
+ key: string;
48
+ kind: StepKind;
49
+ label: string;
50
+ description?: string;
51
+ group?: string;
52
+ required = false;
53
+ defaultValue?: unknown;
54
+ options?: Option[];
55
+ validateText?: ValidateTextFn;
56
+ validateChoice?: ValidateChoiceFn;
57
+ validateChoices?: ValidateChoicesFn;
58
+ when?: Condition;
59
+ actionFn?: ActionFn;
60
+ onError: ErrorAction = ErrorAction.Abort;
61
+ formatFn?: FormatFn;
62
+
63
+ constructor(init: {
64
+ key: string;
65
+ kind: StepKind;
66
+ label: string;
67
+ description?: string;
68
+ group?: string;
69
+ required?: boolean;
70
+ defaultValue?: unknown;
71
+ options?: Option[];
72
+ validateText?: ValidateTextFn;
73
+ validateChoice?: ValidateChoiceFn;
74
+ validateChoices?: ValidateChoicesFn;
75
+ when?: Condition;
76
+ actionFn?: ActionFn;
77
+ onError?: ErrorAction;
78
+ formatFn?: FormatFn;
79
+ }) {
80
+ this.key = init.key;
81
+ this.kind = init.kind;
82
+ this.label = init.label;
83
+ this.description = init.description;
84
+ this.group = init.group;
85
+ this.required = init.required ?? false;
86
+ this.defaultValue = init.defaultValue;
87
+ this.options = init.options;
88
+ this.validateText = init.validateText;
89
+ this.validateChoice = init.validateChoice;
90
+ this.validateChoices = init.validateChoices;
91
+ this.when = init.when;
92
+ this.actionFn = init.actionFn;
93
+ this.onError = init.onError ?? ErrorAction.Abort;
94
+ this.formatFn = init.formatFn;
95
+ }
96
+
97
+ // --- chainable modifiers (return new Step) ---
98
+
99
+ private clone(): Step {
100
+ return new Step({ ...this });
101
+ }
102
+
103
+ withRequired(): Step {
104
+ const s = this.clone();
105
+ s.required = true;
106
+ return s;
107
+ }
108
+
109
+ withDefault(v: unknown): Step {
110
+ const s = this.clone();
111
+ s.defaultValue = v;
112
+ return s;
113
+ }
114
+
115
+ withGroup(g: string): Step {
116
+ const s = this.clone();
117
+ s.group = g;
118
+ return s;
119
+ }
120
+
121
+ withDescription(d: string): Step {
122
+ const s = this.clone();
123
+ s.description = d;
124
+ return s;
125
+ }
126
+
127
+ withValidateText(fn: ValidateTextFn): Step {
128
+ const s = this.clone();
129
+ s.validateText = fn;
130
+ return s;
131
+ }
132
+
133
+ withValidateChoice(fn: ValidateChoiceFn): Step {
134
+ const s = this.clone();
135
+ s.validateChoice = fn;
136
+ return s;
137
+ }
138
+
139
+ withValidateChoices(fn: ValidateChoicesFn): Step {
140
+ const s = this.clone();
141
+ s.validateChoices = fn;
142
+ return s;
143
+ }
144
+
145
+ withWhen(key: string, pred: (value: unknown) => boolean): Step {
146
+ const s = this.clone();
147
+ s.when = { key, pred };
148
+ return s;
149
+ }
150
+
151
+ withOnError(a: ErrorAction): Step {
152
+ const s = this.clone();
153
+ s.onError = a;
154
+ return s;
155
+ }
156
+
157
+ withFormat(fn: FormatFn): Step {
158
+ const s = this.clone();
159
+ s.formatFn = fn;
160
+ return s;
161
+ }
162
+ }
163
+
164
+ // --- builders ---
165
+
166
+ export function textInput(key: string, label: string): Step {
167
+ return new Step({ key, kind: StepKind.TextInput, label });
168
+ }
169
+
170
+ export function select(
171
+ key: string,
172
+ label: string,
173
+ options: Option[],
174
+ ): Step {
175
+ return new Step({
176
+ key,
177
+ kind: StepKind.Select,
178
+ label,
179
+ options,
180
+ });
181
+ }
182
+
183
+ export function confirm(key: string, label: string): Step {
184
+ return new Step({ key, kind: StepKind.Confirm, label });
185
+ }
186
+
187
+ export function multiSelect(
188
+ key: string,
189
+ label: string,
190
+ options: Option[],
191
+ ): Step {
192
+ return new Step({
193
+ key,
194
+ kind: StepKind.MultiSelect,
195
+ label,
196
+ options,
197
+ });
198
+ }
199
+
200
+ export function action(
201
+ key: string,
202
+ label: string,
203
+ fn: ActionFn,
204
+ ): Step {
205
+ return new Step({
206
+ key,
207
+ kind: StepKind.Action,
208
+ label,
209
+ actionFn: fn,
210
+ });
211
+ }
212
+
213
+ export function summary(label: string): Step {
214
+ return new Step({ key: "", kind: StepKind.Summary, label });
215
+ }
216
+
217
+ // --- errors ---
218
+
219
+ export class ValidationError extends Error {
220
+ stepKey: string;
221
+
222
+ constructor(stepKey: string, cause: Error | string) {
223
+ const msg =
224
+ typeof cause === "string" ? cause : cause.message;
225
+ super(
226
+ `validation failed for "${stepKey}": ${msg}`,
227
+ );
228
+ this.name = "ValidationError";
229
+ this.stepKey = stepKey;
230
+ }
231
+ }
232
+
233
+ export class ActionError extends Error {
234
+ stepKey: string;
235
+ cause: Error;
236
+ action: ErrorAction;
237
+
238
+ constructor(stepKey: string, cause: Error, act: ErrorAction) {
239
+ super(`action "${stepKey}" failed: ${cause.message}`);
240
+ this.name = "ActionError";
241
+ this.stepKey = stepKey;
242
+ this.cause = cause;
243
+ this.action = act;
244
+ }
245
+ }
246
+
247
+ export class ActionRequest {
248
+ stepKey: string;
249
+ run: ActionFn;
250
+
251
+ constructor(stepKey: string, run: ActionFn) {
252
+ this.stepKey = stepKey;
253
+ this.run = run;
254
+ }
255
+ }
256
+
257
+ // --- Wizard ---
258
+
259
+ export class Wizard {
260
+ private steps: Step[];
261
+ private _current = 0;
262
+ private _results: Record<string, unknown> = {};
263
+ private _done = false;
264
+ private _dryRun = false;
265
+ private _onComplete?: (
266
+ results: Record<string, unknown>,
267
+ ) => void;
268
+
269
+ constructor(...steps: Step[]) {
270
+ const seen = new Set<string>();
271
+ for (let i = 0; i < steps.length; i++) {
272
+ const s = steps[i];
273
+
274
+ // auto-generate key for Summary
275
+ if (s.kind === StepKind.Summary && s.key === "") {
276
+ s.key = `__summary_${i}`;
277
+ }
278
+
279
+ if (s.key === "") {
280
+ throw new Error(
281
+ `step at index ${i}: key must not be empty`,
282
+ );
283
+ }
284
+
285
+ if (seen.has(s.key)) {
286
+ throw new Error(`duplicate step key "${s.key}"`);
287
+ }
288
+ seen.add(s.key);
289
+
290
+ validateDefault(s);
291
+
292
+ if (
293
+ s.kind === StepKind.Action &&
294
+ s.actionFn == null
295
+ ) {
296
+ throw new Error(
297
+ `step "${s.key}": action kind must have actionFn`,
298
+ );
299
+ }
300
+
301
+ if (
302
+ (s.kind === StepKind.Select ||
303
+ s.kind === StepKind.MultiSelect) &&
304
+ (!s.options || s.options.length === 0)
305
+ ) {
306
+ throw new Error(
307
+ `step "${s.key}": select/multi_select must have options`,
308
+ );
309
+ }
310
+ }
311
+ this.steps = steps;
312
+ }
313
+
314
+ current(): Step | null {
315
+ while (this._current < this.steps.length) {
316
+ const s = this.steps[this._current];
317
+ if (
318
+ s.when != null &&
319
+ !s.when.pred(this._results[s.when.key])
320
+ ) {
321
+ delete this._results[s.key]; // clear stale
322
+ this._current++;
323
+ continue;
324
+ }
325
+ return s;
326
+ }
327
+ this._done = true;
328
+ return null;
329
+ }
330
+
331
+ advance(
332
+ value: unknown,
333
+ ): [ActionRequest | null, ValidationError | null] {
334
+ const s = this.current();
335
+ if (s == null) return [null, null];
336
+
337
+ switch (s.kind) {
338
+ case StepKind.Action:
339
+ return [
340
+ new ActionRequest(s.key, s.actionFn!),
341
+ null,
342
+ ];
343
+
344
+ case StepKind.Summary:
345
+ this.doAdvance();
346
+ return [null, null];
347
+
348
+ case StepKind.TextInput:
349
+ case StepKind.Select: {
350
+ if (typeof value !== "string") {
351
+ return [
352
+ null,
353
+ new ValidationError(s.key, "expected string"),
354
+ ];
355
+ }
356
+ if (s.required && value === "") {
357
+ return [
358
+ null,
359
+ new ValidationError(s.key, "required"),
360
+ ];
361
+ }
362
+ if (
363
+ s.kind === StepKind.TextInput &&
364
+ s.validateText
365
+ ) {
366
+ try {
367
+ s.validateText(value);
368
+ } catch (e: unknown) {
369
+ return [
370
+ null,
371
+ new ValidationError(
372
+ s.key,
373
+ e instanceof Error ? e : new Error(String(e)),
374
+ ),
375
+ ];
376
+ }
377
+ }
378
+ if (s.kind === StepKind.Select) {
379
+ if (!isValidOption(s.options!, value)) {
380
+ return [
381
+ null,
382
+ new ValidationError(
383
+ s.key,
384
+ `invalid option "${value}"`,
385
+ ),
386
+ ];
387
+ }
388
+ if (s.validateChoice) {
389
+ try {
390
+ s.validateChoice(value);
391
+ } catch (e: unknown) {
392
+ return [
393
+ null,
394
+ new ValidationError(
395
+ s.key,
396
+ e instanceof Error
397
+ ? e
398
+ : new Error(String(e)),
399
+ ),
400
+ ];
401
+ }
402
+ }
403
+ }
404
+ this._results[s.key] = value;
405
+ break;
406
+ }
407
+
408
+ case StepKind.Confirm: {
409
+ if (typeof value !== "boolean") {
410
+ return [
411
+ null,
412
+ new ValidationError(s.key, "expected bool"),
413
+ ];
414
+ }
415
+ this._results[s.key] = value;
416
+ break;
417
+ }
418
+
419
+ case StepKind.MultiSelect: {
420
+ if (
421
+ !Array.isArray(value) ||
422
+ !value.every((v) => typeof v === "string")
423
+ ) {
424
+ return [
425
+ null,
426
+ new ValidationError(
427
+ s.key,
428
+ "expected string[]",
429
+ ),
430
+ ];
431
+ }
432
+ const ss = value as string[];
433
+ if (s.required && ss.length === 0) {
434
+ return [
435
+ null,
436
+ new ValidationError(s.key, "required"),
437
+ ];
438
+ }
439
+ for (const v of ss) {
440
+ if (!isValidOption(s.options!, v)) {
441
+ return [
442
+ null,
443
+ new ValidationError(
444
+ s.key,
445
+ `invalid option "${v}"`,
446
+ ),
447
+ ];
448
+ }
449
+ }
450
+ if (s.validateChoices) {
451
+ try {
452
+ s.validateChoices(ss);
453
+ } catch (e: unknown) {
454
+ return [
455
+ null,
456
+ new ValidationError(
457
+ s.key,
458
+ e instanceof Error
459
+ ? e
460
+ : new Error(String(e)),
461
+ ),
462
+ ];
463
+ }
464
+ }
465
+ this._results[s.key] = ss;
466
+ break;
467
+ }
468
+ }
469
+
470
+ this.doAdvance();
471
+ return [null, null];
472
+ }
473
+
474
+ private doAdvance(): void {
475
+ this._current++;
476
+ if (this.current() == null) {
477
+ this._done = true;
478
+ }
479
+ }
480
+
481
+ resolveAction(
482
+ err: Error | null,
483
+ ): ActionError | null {
484
+ const s = this.current();
485
+ if (s == null) return null;
486
+
487
+ if (err == null) {
488
+ this.doAdvance();
489
+ return null;
490
+ }
491
+
492
+ switch (s.onError) {
493
+ case ErrorAction.Abort:
494
+ return new ActionError(
495
+ s.key,
496
+ err,
497
+ ErrorAction.Abort,
498
+ );
499
+ case ErrorAction.Retry:
500
+ return null;
501
+ case ErrorAction.Skip:
502
+ this.doAdvance();
503
+ return null;
504
+ }
505
+ return new ActionError(s.key, err, s.onError);
506
+ }
507
+
508
+ back(): void {
509
+ this._done = false;
510
+ while (this._current > 0) {
511
+ this._current--;
512
+ const s = this.steps[this._current];
513
+ if (
514
+ s.when != null &&
515
+ !s.when.pred(this._results[s.when.key])
516
+ ) {
517
+ continue;
518
+ }
519
+ delete this._results[s.key];
520
+ return;
521
+ }
522
+ }
523
+
524
+ results(): Record<string, unknown> {
525
+ return { ...this._results };
526
+ }
527
+
528
+ done(): boolean {
529
+ return this._done;
530
+ }
531
+
532
+ stepCount(): number {
533
+ let n = 0;
534
+ for (const s of this.steps) {
535
+ if (
536
+ s.when != null &&
537
+ !s.when.pred(this._results[s.when.key])
538
+ ) {
539
+ continue;
540
+ }
541
+ n++;
542
+ }
543
+ return n;
544
+ }
545
+
546
+ stepIndex(): number {
547
+ let idx = 0;
548
+ for (
549
+ let i = 0;
550
+ i < this._current && i < this.steps.length;
551
+ i++
552
+ ) {
553
+ const s = this.steps[i];
554
+ if (
555
+ s.when != null &&
556
+ !s.when.pred(this._results[s.when.key])
557
+ ) {
558
+ continue;
559
+ }
560
+ idx++;
561
+ }
562
+ return idx;
563
+ }
564
+
565
+ setDryRun(v: boolean): void {
566
+ this._dryRun = v;
567
+ }
568
+
569
+ dryRun(): boolean {
570
+ return this._dryRun;
571
+ }
572
+
573
+ setOnComplete(
574
+ fn: (results: Record<string, unknown>) => void,
575
+ ): void {
576
+ this._onComplete = fn;
577
+ }
578
+
579
+ complete(): void {
580
+ if (this._dryRun || this._onComplete == null) return;
581
+ this._onComplete(this._results);
582
+ }
583
+ }
584
+
585
+ // --- validation helpers ---
586
+
587
+ function validateDefault(s: Step): void {
588
+ if (s.defaultValue == null) return;
589
+
590
+ switch (s.kind) {
591
+ case StepKind.TextInput:
592
+ case StepKind.Select:
593
+ if (typeof s.defaultValue !== "string") {
594
+ throw new Error(
595
+ `step "${s.key}": default must be string for ${s.kind}`,
596
+ );
597
+ }
598
+ break;
599
+ case StepKind.Confirm:
600
+ if (typeof s.defaultValue !== "boolean") {
601
+ throw new Error(
602
+ `step "${s.key}": default must be bool for confirm`,
603
+ );
604
+ }
605
+ break;
606
+ case StepKind.MultiSelect:
607
+ if (
608
+ !Array.isArray(s.defaultValue) ||
609
+ !s.defaultValue.every(
610
+ (v: unknown) => typeof v === "string",
611
+ )
612
+ ) {
613
+ throw new Error(
614
+ `step "${s.key}": default must be string[] for multi_select`,
615
+ );
616
+ }
617
+ break;
618
+ }
619
+ }
620
+
621
+ function isValidOption(opts: Option[], val: string): boolean {
622
+ return opts.some((o) => o.value === val);
623
+ }
624
+
625
+ // --- result accessors ---
626
+
627
+ export function resultString(
628
+ results: Record<string, unknown>,
629
+ key: string,
630
+ ): string {
631
+ const v = results[key];
632
+ return typeof v === "string" ? v : "";
633
+ }
634
+
635
+ export function resultBool(
636
+ results: Record<string, unknown>,
637
+ key: string,
638
+ ): boolean {
639
+ const v = results[key];
640
+ return typeof v === "boolean" ? v : false;
641
+ }
642
+
643
+ export function resultStrings(
644
+ results: Record<string, unknown>,
645
+ key: string,
646
+ ): string[] | null {
647
+ const v = results[key];
648
+ if (
649
+ Array.isArray(v) &&
650
+ v.every((x) => typeof x === "string")
651
+ ) {
652
+ return v as string[];
653
+ }
654
+ return null;
655
+ }
656
+
657
+ export function resultChoice(
658
+ results: Record<string, unknown>,
659
+ key: string,
660
+ ): string {
661
+ return resultString(results, key);
662
+ }