@punica/form-mcp 0.8.0 → 0.10.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.
@@ -17,6 +17,10 @@ export type FormItem<E> = {
17
17
  * property and on the form item type.
18
18
  */
19
19
  value?: any;
20
+ /**
21
+ * The item needs a value. What counts as empty is up to its validation;
22
+ * `@punica/form-kit` takes `undefined`, `null`, `''`, `false` and `[]`.
23
+ */
20
24
  required?: boolean;
21
25
  disabled?: boolean;
22
26
  hidden?: boolean;
@@ -1 +1 @@
1
- {"version":"2.3.1"}
1
+ {"version":"2.3.3"}
@@ -73,6 +73,23 @@ export declare const FileField: (props: FieldProps) => FormPropertyDecorator;
73
73
  * @ArrayField({ label: 'Lines', row: () => new LineForm(), minItems: 1 })
74
74
  * lines: LineForm[] = [];
75
75
  *
76
+ * The rows start from the value of the property: a row for every element,
77
+ * whose values replace those of a new `row()`. `lines: LineForm[] =
78
+ * [new LineForm()]` starts with one row as the class has it; an element may
79
+ * name only what differs (`{ quantity: 1 } as LineForm`), the other fields
80
+ * then come from the class. `[]` starts with none: `minItems` only checks
81
+ * how many there are, it adds no rows.
82
+ *
83
+ * Each row has a remove button and the list an add button below the rows,
84
+ * with the texts `removeLabel` and `addLabel` (`Remove` and `Add` by
85
+ * default). The add button is disabled once the list has `maxItems` rows.
86
+ *
87
+ * @ArrayField({ label: 'Tickets', row: () => new TicketForm(), addLabel: 'Add ticket', removeLabel: 'Remove ticket' })
88
+ * tickets: TicketForm[] = [{ quantity: 1 } as TicketForm];
89
+ *
90
+ * A row's messages are its fields' own: an error for `tickets.0.quantity`
91
+ * that `SetErrors` returns shows under that row's Quantity.
92
+ *
76
93
  * The row class needs no `@FormKit`; with it, `columns` lays out its fields.
77
94
  */
78
95
  export declare const ArrayField: (props: FieldProps & {
@@ -1 +1 @@
1
- {"version":"0.7.1"}
1
+ {"version":"0.7.3"}
@@ -40,7 +40,16 @@ export type FormViewProps<E extends object> = ViewProps<E> & {
40
40
  * state while the submit runs, and a reset button when `resetLabel` is set.
41
41
  *
42
42
  * A submit validates every item first: an invalid form shows its messages
43
- * and `onSubmit` is not called.
43
+ * and `onSubmit` is not called. A valid one calls `onSubmit(values,
44
+ * controller)`: `values` is a copy of the form's class instance holding the
45
+ * current values (what `GetValues` returns), a list as an array of its rows'
46
+ * values. Outside a submit, read them with
47
+ * `controller.getService(GetValues).run()`.
48
+ *
49
+ * ```tsx
50
+ * <FormView controller={controller} submitLabel="Create"
51
+ * onSubmit={(values) => api.create(values)} />
52
+ * ```
44
53
  */
45
54
  export declare function FormView<E extends object>({ controller, onSubmit, onError, submitLabel, resetLabel, components }: FormViewProps<E>): ReactNode;
46
55
  export type KitFormProps<E extends object> = ViewProps<E> & {
@@ -12,7 +12,25 @@ export type Option = string | {
12
12
  export type Options = Option[] | (() => Promise<Option[]>);
13
13
  /** How many columns of the form a field takes. */
14
14
  export type Span = 1 | 2 | 3 | 4 | 'full';
15
- /** The rules the kit validates by itself. */
15
+ /**
16
+ * The rules the kit validates by itself, on submit, on `Trigger` and when a
17
+ * field that shows an error changes.
18
+ *
19
+ * - An empty value (`undefined`, `null`, `''`, `false`, `[]`) is checked only
20
+ * by `required` (and a list by `minItems`): an empty optional field passes,
21
+ * whatever its other rules say. A `NumberField` is `undefined` while empty.
22
+ * - `minLength`, `maxLength` and `pattern` check strings; `min` and `max`
23
+ * check numbers; `minDate` and `maxDate` check `yyyy-mm-dd` strings;
24
+ * `minItems` and `maxItems` check arrays (choices, files, rows).
25
+ * - The first rule a value breaks is its only message; the field's own
26
+ * `validation` runs after the rules, for a value that passed them.
27
+ * - Messages come from `setMessages` (every form) or `messages` (one field).
28
+ *
29
+ * ```ts
30
+ * @NumberField({ label: 'Quantity', required: true, min: 1 })
31
+ * quantity?: number;
32
+ * ```
33
+ */
16
34
  export type Rules = {
17
35
  /** Fewest characters allowed. */
18
36
  minLength?: number;
@@ -66,9 +84,15 @@ export type KitItem<E> = FormItem<E> & Rules & {
66
84
  accept?: string;
67
85
  /** The language a date field is typed and shown in, the one of the page by default. */
68
86
  locale?: string;
69
- /** The text of the button that adds a row to a list, `Add` by default. */
87
+ /**
88
+ * The text of the button that adds a row to a list (`ArrayField`), `Add`
89
+ * by default. It is the button's accessible name as well.
90
+ */
70
91
  addLabel?: string;
71
- /** The text of the button that removes a row from a list, `Remove` by default. */
92
+ /**
93
+ * The text of the button that removes a row from a list (`ArrayField`),
94
+ * `Remove` by default; every row has one.
95
+ */
72
96
  removeLabel?: string;
73
97
  /** Visible lines of a text area. */
74
98
  rows?: number;
@@ -77,7 +101,16 @@ export type KitItem<E> = FormItem<E> & Rules & {
77
101
  /** Messages of this field, in place of the kit's. */
78
102
  messages?: Partial<Messages>;
79
103
  };
80
- /** What a field decorator takes: everything of an item but its property and type. */
104
+ /**
105
+ * What a field decorator takes: everything of a `KitItem` but its property
106
+ * and type. That is `label`, `required`, `disabled`, `hidden` and
107
+ * `validation` of a form item, the `Rules` the kit checks, and what the
108
+ * components show (`help`, `placeholder`, `options`, `addLabel`, …).
109
+ *
110
+ * `required: true` means the value must not be empty: a text that is not
111
+ * `''`, a number that is not `undefined`, a ticked checkbox, a list with an
112
+ * element.
113
+ */
81
114
  export type FieldProps = Omit<KitItem<any>, 'property' | 'type'>;
82
115
  /** The messages of the built-in rules. Each receives the label of the field. */
83
116
  export type Messages = {
@@ -15,6 +15,10 @@ export type RowOf<E, K extends keyof E> = NonNullable<E[K]> extends ReadonlyArra
15
15
  * await lines.append('lines', { name: 'Paper' });
16
16
  * lines.rows('lines')[0].controller.getService(SetValue).run('price', 5);
17
17
  *
18
+ * The rows start from the value of the item: a row for every element, whose
19
+ * values replace those of a new `row()`, so an element names only what
20
+ * differs (`[{ quantity: 1 }]`); an empty or missing value starts with none.
21
+ *
18
22
  * The value of the item is the array of the values of its rows. The item is
19
23
  * dirty, touched and in error when a row is, and validating the form
20
24
  * validates the rows. A row class without `@Services` gets the services of
@@ -1 +1 @@
1
- {"version":"2.3.1"}
1
+ {"version":"2.3.3"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@punica/form-mcp",
3
- "version": "0.8.0",
3
+ "version": "0.10.0",
4
4
  "description": "MCP server for Punica Form: look up the exact API of its packages, read the rules and check code that uses them.",
5
5
  "keywords": [
6
6
  "mcp",
@@ -40,10 +40,10 @@
40
40
  "zod": "^4.6.7"
41
41
  },
42
42
  "devDependencies": {
43
- "@punica/form": "^2.3.1",
44
- "@punica/form-service": "^2.3.1",
43
+ "@punica/form": "^2.3.3",
44
+ "@punica/form-service": "^2.3.3",
45
45
  "@punica/form-react": "^0.2.0",
46
- "@punica/form-kit": "^0.7.1",
46
+ "@punica/form-kit": "^0.7.3",
47
47
  "@punica/form-schema": "^0.1.0"
48
48
  },
49
49
  "scripts": {
package/src/knowledge.mjs CHANGED
@@ -64,12 +64,13 @@ function declarationAt(source, index) {
64
64
  * such as the interface a configuration type is made of: without them the
65
65
  * options of a middleware could not be looked up.
66
66
  */
67
- function localsOf(source, text, seen = new Set()) {
67
+ function localsOf(source, text, seen = new Set(), exported = false) {
68
68
  const found = [];
69
+ const pattern = exported
70
+ ? /^(?:export )?(?:declare )?(?:interface|type) (\w+)/gm
71
+ : /^(?:declare )?(?:interface|type) (\w+)/gm;
69
72
 
70
- for (const match of source.matchAll(
71
- /^(?:declare )?(?:interface|type) (\w+)/gm
72
- )) {
73
+ for (const match of source.matchAll(pattern)) {
73
74
  const name = match[1];
74
75
 
75
76
  if (seen.has(name) || !new RegExp(`\\b${name}\\b`).test(text)) continue;
@@ -82,7 +83,7 @@ function localsOf(source, text, seen = new Set()) {
82
83
  .filter(Boolean)
83
84
  .join('\n');
84
85
 
85
- found.push(declaration, ...localsOf(source, declaration, seen));
86
+ found.push(declaration, ...localsOf(source, declaration, seen, exported));
86
87
  }
87
88
 
88
89
  return found;
@@ -113,17 +114,33 @@ function entry(source, index, kind, name) {
113
114
  '.'
114
115
  );
115
116
 
117
+ // A type made of other types (`Omit<KitItem, …>`, `A & B`) says nothing by
118
+ // itself: the types of its module it names come along, exported or not
119
+ const derived = kind === 'type' && !/=\s*\{/.test(declaration);
120
+
121
+ const own = [
122
+ comment,
123
+ declaration,
124
+ ...localsOf(source, declaration, new Set([name]))
125
+ ]
126
+ .filter(Boolean)
127
+ .join('\n');
128
+
116
129
  return {
117
130
  name,
118
131
  kind,
119
132
  summary: summary.trim(),
120
- declaration: [
121
- comment,
122
- declaration,
123
- ...localsOf(source, declaration, new Set([name]))
124
- ]
125
- .filter(Boolean)
126
- .join('\n')
133
+ // What a search reads: the types that only come along are not its words
134
+ text: own,
135
+ declaration: derived
136
+ ? [
137
+ comment,
138
+ declaration,
139
+ ...localsOf(source, declaration, new Set([name]), true)
140
+ ]
141
+ .filter(Boolean)
142
+ .join('\n')
143
+ : own
127
144
  };
128
145
  }
129
146
 
@@ -203,7 +220,7 @@ export function createKnowledge(packages) {
203
220
  return names
204
221
  .map((name) => api.get(name))
205
222
  .filter((item) => {
206
- const text = `${item.name} ${item.declaration}`.toLowerCase();
223
+ const text = `${item.name} ${item.text}`.toLowerCase();
207
224
 
208
225
  return words.every((word) => text.includes(word));
209
226
  });
@@ -237,13 +254,46 @@ export function createKnowledge(packages) {
237
254
  return `## ${name} (not exported, ${api.get(users[0]).package})\n\nIt cannot be imported; it is part of ${users.join(', ')}, which can.\n\n\`\`\`ts\n${declaration}\n\`\`\``;
238
255
  }
239
256
 
257
+ /** The words of a name, with the ones that mean the same made one. */
258
+ const wordsOf = (name) =>
259
+ name
260
+ .split(/(?=[A-Z][a-z])|_/)
261
+ .map((word) => word.toLowerCase())
262
+ .map((word) => (/^(options|config|params)$/.test(word) ? 'props' : word))
263
+ .filter(Boolean);
264
+
265
+ /** The exports that share most words with a name that is not one. */
266
+ function closest(name) {
267
+ const wanted = new Set(wordsOf(name));
268
+
269
+ return names
270
+ .map((other) => ({
271
+ other,
272
+ shared: wordsOf(other).filter((word) => wanted.has(word)).length
273
+ }))
274
+ .filter(({ shared }) => shared > 0)
275
+ .sort((a, b) => b.shared - a.shared || a.other.length - b.other.length)
276
+ .slice(0, 6)
277
+ .map(({ other }) => other);
278
+ }
279
+
240
280
  function formatMarkdown(name) {
241
281
  const item = get(name);
242
282
 
243
283
  if (!item && /^\w+$/.test(name) && local(name)) return local(name);
244
284
 
245
285
  if (!item) {
246
- return `${name} is not exported by ${packages.map((p) => p.name).join(' or ')}. Exports: ${names.join(', ')}`;
286
+ const missing = `${name} is not exported by ${packages.map((p) => p.name).join(' or ')}.`;
287
+ // TextFieldOptions: what TextField takes is in its own declaration
288
+ const owner = /^(\w+?)(?:Options|Props|Config|Params)$/.exec(name)?.[1];
289
+
290
+ if (owner && get(owner)) {
291
+ return `${missing} What ${owner} takes is in its declaration:\n\n${formatMarkdown(owner)}`;
292
+ }
293
+
294
+ const near = /^\w+$/.test(name) ? closest(name) : [];
295
+
296
+ return `${missing}${near.length ? ` Closest: ${near.join(', ')}.` : ''} Exports: ${names.join(', ')}`;
247
297
  }
248
298
 
249
299
  return `## ${item.name} (${item.kind}, ${item.package})\n\n\`\`\`ts\n${item.declaration}\n\`\`\``;