@leanmcp/elicitation 0.1.0

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/dist/index.js ADDED
@@ -0,0 +1,700 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+
21
+ // src/index.ts
22
+ var index_exports = {};
23
+ __export(index_exports, {
24
+ Elicitation: () => Elicitation,
25
+ ElicitationError: () => ElicitationError,
26
+ ElicitationFormBuilder: () => ElicitationFormBuilder,
27
+ ElicitationStrategyBase: () => ElicitationStrategyBase,
28
+ FormElicitationStrategy: () => FormElicitationStrategy,
29
+ MultiStepElicitationStrategy: () => MultiStepElicitationStrategy,
30
+ ValidationBuilder: () => ValidationBuilder,
31
+ buildElicitationRequest: () => buildElicitationRequest,
32
+ checkMissingFields: () => checkMissingFields,
33
+ getElicitationConfig: () => getElicitationConfig,
34
+ getElicitationStrategy: () => getElicitationStrategy,
35
+ isElicitationEnabled: () => isElicitationEnabled,
36
+ validation: () => validation
37
+ });
38
+ module.exports = __toCommonJS(index_exports);
39
+ var import_reflect_metadata3 = require("reflect-metadata");
40
+
41
+ // src/types.ts
42
+ var import_reflect_metadata = require("reflect-metadata");
43
+ var ElicitationError = class extends Error {
44
+ static {
45
+ __name(this, "ElicitationError");
46
+ }
47
+ code;
48
+ details;
49
+ constructor(message, code, details) {
50
+ super(message), this.code = code, this.details = details;
51
+ this.name = "ElicitationError";
52
+ }
53
+ };
54
+
55
+ // src/decorators.ts
56
+ var import_reflect_metadata2 = require("reflect-metadata");
57
+
58
+ // src/strategies/base.ts
59
+ var ElicitationStrategyBase = class {
60
+ static {
61
+ __name(this, "ElicitationStrategyBase");
62
+ }
63
+ /**
64
+ * Validate user response against field definitions
65
+ */
66
+ validateResponse(response, fields) {
67
+ const errors = [];
68
+ for (const field of fields) {
69
+ const value = response.values[field.name];
70
+ if (field.required && (value === void 0 || value === null || value === "")) {
71
+ errors.push({
72
+ field: field.name,
73
+ message: field.validation?.errorMessage || `${field.label} is required`
74
+ });
75
+ continue;
76
+ }
77
+ if (value === void 0 || value === null || value === "") {
78
+ continue;
79
+ }
80
+ const typeError = this.validateFieldType(value, field);
81
+ if (typeError) {
82
+ errors.push(typeError);
83
+ continue;
84
+ }
85
+ if (field.validation) {
86
+ const validationError = this.validateField(value, field);
87
+ if (validationError) {
88
+ errors.push(validationError);
89
+ }
90
+ }
91
+ }
92
+ return errors;
93
+ }
94
+ /**
95
+ * Validate field type
96
+ */
97
+ validateFieldType(value, field) {
98
+ switch (field.type) {
99
+ case "number":
100
+ if (typeof value !== "number" && isNaN(Number(value))) {
101
+ return {
102
+ field: field.name,
103
+ message: `${field.label} must be a number`
104
+ };
105
+ }
106
+ break;
107
+ case "boolean":
108
+ if (typeof value !== "boolean") {
109
+ return {
110
+ field: field.name,
111
+ message: `${field.label} must be true or false`
112
+ };
113
+ }
114
+ break;
115
+ case "email":
116
+ const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
117
+ if (!emailRegex.test(String(value))) {
118
+ return {
119
+ field: field.name,
120
+ message: `${field.label} must be a valid email address`
121
+ };
122
+ }
123
+ break;
124
+ case "url":
125
+ try {
126
+ new URL(String(value));
127
+ } catch {
128
+ return {
129
+ field: field.name,
130
+ message: `${field.label} must be a valid URL`
131
+ };
132
+ }
133
+ break;
134
+ }
135
+ return null;
136
+ }
137
+ /**
138
+ * Validate field against validation rules
139
+ */
140
+ validateField(value, field) {
141
+ const validation2 = field.validation;
142
+ if (!validation2) return null;
143
+ if (field.type === "number") {
144
+ const numValue = Number(value);
145
+ if (validation2.min !== void 0 && numValue < validation2.min) {
146
+ return {
147
+ field: field.name,
148
+ message: validation2.errorMessage || `${field.label} must be at least ${validation2.min}`
149
+ };
150
+ }
151
+ if (validation2.max !== void 0 && numValue > validation2.max) {
152
+ return {
153
+ field: field.name,
154
+ message: validation2.errorMessage || `${field.label} must be at most ${validation2.max}`
155
+ };
156
+ }
157
+ }
158
+ if (field.type === "text" || field.type === "textarea" || field.type === "email" || field.type === "url") {
159
+ const strValue = String(value);
160
+ if (validation2.minLength !== void 0 && strValue.length < validation2.minLength) {
161
+ return {
162
+ field: field.name,
163
+ message: validation2.errorMessage || `${field.label} must be at least ${validation2.minLength} characters`
164
+ };
165
+ }
166
+ if (validation2.maxLength !== void 0 && strValue.length > validation2.maxLength) {
167
+ return {
168
+ field: field.name,
169
+ message: validation2.errorMessage || `${field.label} must be at most ${validation2.maxLength} characters`
170
+ };
171
+ }
172
+ }
173
+ if (validation2.pattern) {
174
+ const regex = new RegExp(validation2.pattern);
175
+ if (!regex.test(String(value))) {
176
+ return {
177
+ field: field.name,
178
+ message: validation2.errorMessage || `${field.label} format is invalid`
179
+ };
180
+ }
181
+ }
182
+ if (validation2.customValidator) {
183
+ const result = validation2.customValidator(value);
184
+ if (result !== true) {
185
+ return {
186
+ field: field.name,
187
+ message: typeof result === "string" ? result : validation2.errorMessage || `${field.label} is invalid`
188
+ };
189
+ }
190
+ }
191
+ return null;
192
+ }
193
+ /**
194
+ * Merge elicited values with original arguments
195
+ */
196
+ mergeWithArgs(originalArgs, elicitedValues) {
197
+ return {
198
+ ...originalArgs,
199
+ ...elicitedValues
200
+ };
201
+ }
202
+ };
203
+
204
+ // src/strategies/form.ts
205
+ var FormElicitationStrategy = class extends ElicitationStrategyBase {
206
+ static {
207
+ __name(this, "FormElicitationStrategy");
208
+ }
209
+ buildRequest(config, context) {
210
+ return {
211
+ type: "elicitation",
212
+ title: config.title || "Additional Information Required",
213
+ description: config.description,
214
+ fields: config.fields || [],
215
+ metadata: {
216
+ strategy: "form",
217
+ previousValues: context.args
218
+ }
219
+ };
220
+ }
221
+ };
222
+
223
+ // src/strategies/multi-step.ts
224
+ var MultiStepElicitationStrategy = class extends ElicitationStrategyBase {
225
+ static {
226
+ __name(this, "MultiStepElicitationStrategy");
227
+ }
228
+ steps;
229
+ currentStep = 0;
230
+ accumulatedValues = {};
231
+ constructor(steps) {
232
+ super();
233
+ this.steps = steps;
234
+ }
235
+ buildRequest(config, context) {
236
+ while (this.currentStep < this.steps.length) {
237
+ const step = this.steps[this.currentStep];
238
+ if (!step.condition || step.condition(this.accumulatedValues)) {
239
+ return {
240
+ type: "elicitation",
241
+ title: step.title,
242
+ description: step.description,
243
+ fields: step.fields,
244
+ metadata: {
245
+ strategy: "multi-step",
246
+ stepNumber: this.currentStep + 1,
247
+ totalSteps: this.steps.length,
248
+ previousValues: {
249
+ ...context.args,
250
+ ...this.accumulatedValues
251
+ }
252
+ }
253
+ };
254
+ }
255
+ this.currentStep++;
256
+ }
257
+ return {
258
+ type: "elicitation",
259
+ title: "Complete",
260
+ description: "All steps completed",
261
+ fields: [],
262
+ metadata: {
263
+ strategy: "multi-step",
264
+ stepNumber: this.steps.length,
265
+ totalSteps: this.steps.length,
266
+ previousValues: this.accumulatedValues
267
+ }
268
+ };
269
+ }
270
+ /**
271
+ * Check if there are more steps
272
+ */
273
+ hasNextStep() {
274
+ return this.currentStep < this.steps.length - 1;
275
+ }
276
+ /**
277
+ * Move to next step and accumulate values
278
+ */
279
+ nextStep(values) {
280
+ this.accumulatedValues = {
281
+ ...this.accumulatedValues,
282
+ ...values
283
+ };
284
+ this.currentStep++;
285
+ }
286
+ /**
287
+ * Get all accumulated values
288
+ */
289
+ getAccumulatedValues() {
290
+ return {
291
+ ...this.accumulatedValues
292
+ };
293
+ }
294
+ /**
295
+ * Reset to first step
296
+ */
297
+ reset() {
298
+ this.currentStep = 0;
299
+ this.accumulatedValues = {};
300
+ }
301
+ /**
302
+ * Override merge to include all accumulated values
303
+ */
304
+ mergeWithArgs(originalArgs, elicitedValues) {
305
+ return {
306
+ ...originalArgs,
307
+ ...this.accumulatedValues,
308
+ ...elicitedValues
309
+ };
310
+ }
311
+ };
312
+
313
+ // src/decorators.ts
314
+ function checkMissingFields(args, config) {
315
+ if (!config.fields) return false;
316
+ for (const field of config.fields) {
317
+ if (field.required) {
318
+ const value = args[field.name];
319
+ if (value === void 0 || value === null || value === "") {
320
+ return true;
321
+ }
322
+ }
323
+ }
324
+ return false;
325
+ }
326
+ __name(checkMissingFields, "checkMissingFields");
327
+ function Elicitation(config) {
328
+ return (target, propertyKey, descriptor) => {
329
+ if (!descriptor || typeof descriptor.value !== "function") {
330
+ throw new Error("@Elicitation can only be applied to methods");
331
+ }
332
+ const originalMethod = descriptor.value;
333
+ Reflect.defineMetadata("elicitation:config", config, originalMethod);
334
+ Reflect.defineMetadata("elicitation:enabled", true, originalMethod);
335
+ const strategy = config.strategy || "form";
336
+ Reflect.defineMetadata("elicitation:strategy", strategy, originalMethod);
337
+ descriptor.value = async function(args, meta) {
338
+ const context = {
339
+ args: args || {},
340
+ meta,
341
+ previousAttempts: 0
342
+ };
343
+ if (config.condition && !config.condition(args || {})) {
344
+ return originalMethod.call(this, args, meta);
345
+ }
346
+ let needsElicitation = false;
347
+ let fieldsToCheck = [];
348
+ if (config.builder) {
349
+ const builtConfig = config.builder(context);
350
+ if (builtConfig && typeof builtConfig === "object" && "fields" in builtConfig) {
351
+ fieldsToCheck = builtConfig.fields || [];
352
+ } else if (Array.isArray(builtConfig)) {
353
+ const firstStep = builtConfig[0];
354
+ if (firstStep && firstStep.fields) {
355
+ fieldsToCheck = firstStep.fields;
356
+ }
357
+ }
358
+ } else if (config.fields) {
359
+ fieldsToCheck = config.fields;
360
+ }
361
+ for (const field of fieldsToCheck) {
362
+ if (field.required) {
363
+ const value = args[field.name];
364
+ if (value === void 0 || value === null || value === "") {
365
+ needsElicitation = true;
366
+ break;
367
+ }
368
+ }
369
+ }
370
+ if (needsElicitation) {
371
+ const elicitationRequest = buildElicitationRequestInternal(config, context);
372
+ return elicitationRequest;
373
+ }
374
+ return originalMethod.call(this, args, meta);
375
+ };
376
+ if (descriptor.value && typeof descriptor.value === "function") {
377
+ copyMethodMetadata(originalMethod, descriptor.value);
378
+ }
379
+ };
380
+ }
381
+ __name(Elicitation, "Elicitation");
382
+ function copyMethodMetadata(source, target) {
383
+ const metadataKeys = Reflect.getMetadataKeys(source) || [];
384
+ for (const key of metadataKeys) {
385
+ const value = Reflect.getMetadata(key, source);
386
+ if (value !== void 0) {
387
+ Reflect.defineMetadata(key, value, target);
388
+ }
389
+ }
390
+ }
391
+ __name(copyMethodMetadata, "copyMethodMetadata");
392
+ function buildElicitationRequestInternal(config, context) {
393
+ if (config.builder) {
394
+ const result = config.builder(context);
395
+ if (Array.isArray(result)) {
396
+ const strategy = new MultiStepElicitationStrategy(result);
397
+ return strategy.buildRequest(config, context);
398
+ }
399
+ if (result && typeof result === "object" && "fields" in result) {
400
+ const builtConfig = result;
401
+ const strategy = new FormElicitationStrategy();
402
+ return strategy.buildRequest(builtConfig, context);
403
+ }
404
+ return result;
405
+ }
406
+ const strategyType = config.strategy || "form";
407
+ switch (strategyType) {
408
+ case "form": {
409
+ const strategy = new FormElicitationStrategy();
410
+ return strategy.buildRequest(config, context);
411
+ }
412
+ case "multi-step": {
413
+ if (!config.fields) {
414
+ throw new Error("Multi-step elicitation requires either a builder or fields");
415
+ }
416
+ const steps = [
417
+ {
418
+ title: config.title || "Step 1",
419
+ description: config.description,
420
+ fields: config.fields
421
+ }
422
+ ];
423
+ const strategy = new MultiStepElicitationStrategy(steps);
424
+ return strategy.buildRequest(config, context);
425
+ }
426
+ default:
427
+ throw new Error(`Unsupported elicitation strategy: ${strategyType}`);
428
+ }
429
+ }
430
+ __name(buildElicitationRequestInternal, "buildElicitationRequestInternal");
431
+ function isElicitationEnabled(method) {
432
+ return Reflect.getMetadata("elicitation:enabled", method) === true;
433
+ }
434
+ __name(isElicitationEnabled, "isElicitationEnabled");
435
+ function getElicitationConfig(method) {
436
+ return Reflect.getMetadata("elicitation:config", method);
437
+ }
438
+ __name(getElicitationConfig, "getElicitationConfig");
439
+ function getElicitationStrategy(method) {
440
+ return Reflect.getMetadata("elicitation:strategy", method);
441
+ }
442
+ __name(getElicitationStrategy, "getElicitationStrategy");
443
+ function buildElicitationRequest(method, args, meta) {
444
+ const config = getElicitationConfig(method);
445
+ if (!config) return null;
446
+ const context = {
447
+ args,
448
+ meta,
449
+ previousAttempts: 0
450
+ };
451
+ if (config.condition && !config.condition(args)) {
452
+ return null;
453
+ }
454
+ if (config.builder) {
455
+ const result = config.builder(context);
456
+ if (Array.isArray(result)) {
457
+ const strategy = new MultiStepElicitationStrategy(result);
458
+ return strategy.buildRequest(config, context);
459
+ }
460
+ return result;
461
+ }
462
+ const strategyType = config.strategy || "form";
463
+ switch (strategyType) {
464
+ case "form": {
465
+ const strategy = new FormElicitationStrategy();
466
+ return strategy.buildRequest(config, context);
467
+ }
468
+ case "multi-step": {
469
+ if (!config.fields) {
470
+ throw new Error("Multi-step elicitation requires either a builder or fields");
471
+ }
472
+ const steps = [
473
+ {
474
+ title: config.title || "Step 1",
475
+ description: config.description,
476
+ fields: config.fields
477
+ }
478
+ ];
479
+ const strategy = new MultiStepElicitationStrategy(steps);
480
+ return strategy.buildRequest(config, context);
481
+ }
482
+ default:
483
+ throw new Error(`Unsupported elicitation strategy: ${strategyType}`);
484
+ }
485
+ }
486
+ __name(buildElicitationRequest, "buildElicitationRequest");
487
+
488
+ // src/builders/form-builder.ts
489
+ var ElicitationFormBuilder = class {
490
+ static {
491
+ __name(this, "ElicitationFormBuilder");
492
+ }
493
+ fields = [];
494
+ config = {};
495
+ /**
496
+ * Set the form title
497
+ */
498
+ title(title) {
499
+ this.config.title = title;
500
+ return this;
501
+ }
502
+ /**
503
+ * Set the form description
504
+ */
505
+ description(description) {
506
+ this.config.description = description;
507
+ return this;
508
+ }
509
+ /**
510
+ * Set a condition for when elicitation should occur
511
+ */
512
+ condition(condition) {
513
+ this.config.condition = condition;
514
+ return this;
515
+ }
516
+ /**
517
+ * Add a text field
518
+ */
519
+ addTextField(name, label, options) {
520
+ this.fields.push({
521
+ name,
522
+ label,
523
+ type: "text",
524
+ ...options
525
+ });
526
+ return this;
527
+ }
528
+ /**
529
+ * Add a textarea field
530
+ */
531
+ addTextAreaField(name, label, options) {
532
+ this.fields.push({
533
+ name,
534
+ label,
535
+ type: "textarea",
536
+ ...options
537
+ });
538
+ return this;
539
+ }
540
+ /**
541
+ * Add a number field
542
+ */
543
+ addNumberField(name, label, options) {
544
+ this.fields.push({
545
+ name,
546
+ label,
547
+ type: "number",
548
+ ...options
549
+ });
550
+ return this;
551
+ }
552
+ /**
553
+ * Add a boolean field (checkbox)
554
+ */
555
+ addBooleanField(name, label, options) {
556
+ this.fields.push({
557
+ name,
558
+ label,
559
+ type: "boolean",
560
+ ...options
561
+ });
562
+ return this;
563
+ }
564
+ /**
565
+ * Add a select field (dropdown)
566
+ */
567
+ addSelectField(name, label, options, fieldOptions) {
568
+ this.fields.push({
569
+ name,
570
+ label,
571
+ type: "select",
572
+ options,
573
+ ...fieldOptions
574
+ });
575
+ return this;
576
+ }
577
+ /**
578
+ * Add a multi-select field
579
+ */
580
+ addMultiSelectField(name, label, options, fieldOptions) {
581
+ this.fields.push({
582
+ name,
583
+ label,
584
+ type: "multiselect",
585
+ options,
586
+ ...fieldOptions
587
+ });
588
+ return this;
589
+ }
590
+ /**
591
+ * Add an email field
592
+ */
593
+ addEmailField(name, label, options) {
594
+ this.fields.push({
595
+ name,
596
+ label,
597
+ type: "email",
598
+ ...options
599
+ });
600
+ return this;
601
+ }
602
+ /**
603
+ * Add a URL field
604
+ */
605
+ addUrlField(name, label, options) {
606
+ this.fields.push({
607
+ name,
608
+ label,
609
+ type: "url",
610
+ ...options
611
+ });
612
+ return this;
613
+ }
614
+ /**
615
+ * Add a date field
616
+ */
617
+ addDateField(name, label, options) {
618
+ this.fields.push({
619
+ name,
620
+ label,
621
+ type: "date",
622
+ ...options
623
+ });
624
+ return this;
625
+ }
626
+ /**
627
+ * Add a custom field with full control
628
+ */
629
+ addCustomField(field) {
630
+ this.fields.push(field);
631
+ return this;
632
+ }
633
+ /**
634
+ * Build the final configuration
635
+ */
636
+ build() {
637
+ return {
638
+ ...this.config,
639
+ fields: this.fields,
640
+ strategy: "form"
641
+ };
642
+ }
643
+ };
644
+ var ValidationBuilder = class {
645
+ static {
646
+ __name(this, "ValidationBuilder");
647
+ }
648
+ validation = {};
649
+ min(min) {
650
+ this.validation.min = min;
651
+ return this;
652
+ }
653
+ max(max) {
654
+ this.validation.max = max;
655
+ return this;
656
+ }
657
+ minLength(minLength) {
658
+ this.validation.minLength = minLength;
659
+ return this;
660
+ }
661
+ maxLength(maxLength) {
662
+ this.validation.maxLength = maxLength;
663
+ return this;
664
+ }
665
+ pattern(pattern) {
666
+ this.validation.pattern = pattern;
667
+ return this;
668
+ }
669
+ customValidator(validator) {
670
+ this.validation.customValidator = validator;
671
+ return this;
672
+ }
673
+ errorMessage(message) {
674
+ this.validation.errorMessage = message;
675
+ return this;
676
+ }
677
+ build() {
678
+ return this.validation;
679
+ }
680
+ };
681
+ function validation() {
682
+ return new ValidationBuilder();
683
+ }
684
+ __name(validation, "validation");
685
+ // Annotate the CommonJS export names for ESM import in node:
686
+ 0 && (module.exports = {
687
+ Elicitation,
688
+ ElicitationError,
689
+ ElicitationFormBuilder,
690
+ ElicitationStrategyBase,
691
+ FormElicitationStrategy,
692
+ MultiStepElicitationStrategy,
693
+ ValidationBuilder,
694
+ buildElicitationRequest,
695
+ checkMissingFields,
696
+ getElicitationConfig,
697
+ getElicitationStrategy,
698
+ isElicitationEnabled,
699
+ validation
700
+ });