@tooluminati/forms 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/LICENSE +21 -0
- package/dist/create-form-tools.d.ts +6 -0
- package/dist/create-form-tools.d.ts.map +1 -0
- package/dist/create-form-tools.js +96 -0
- package/dist/create-form-tools.test.d.ts +2 -0
- package/dist/create-form-tools.test.d.ts.map +1 -0
- package/dist/create-form-tools.test.js +45 -0
- package/dist/errors.d.ts +7 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +9 -0
- package/dist/form-tool-definition.d.ts +11 -0
- package/dist/form-tool-definition.d.ts.map +1 -0
- package/dist/form-tool-definition.js +1 -0
- package/dist/formik.d.ts +16 -0
- package/dist/formik.d.ts.map +1 -0
- package/dist/formik.js +35 -0
- package/dist/index.cjs +413 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +375 -0
- package/dist/infer-schema.d.ts +3 -0
- package/dist/infer-schema.d.ts.map +1 -0
- package/dist/infer-schema.js +42 -0
- package/dist/infer-schema.test.d.ts +2 -0
- package/dist/infer-schema.test.d.ts.map +1 -0
- package/dist/infer-schema.test.js +27 -0
- package/dist/native-form.d.ts +8 -0
- package/dist/native-form.d.ts.map +1 -0
- package/dist/native-form.js +41 -0
- package/dist/react-hook-form.d.ts +21 -0
- package/dist/react-hook-form.d.ts.map +1 -0
- package/dist/react-hook-form.js +53 -0
- package/dist/react-hook-form.test.d.ts +2 -0
- package/dist/react-hook-form.test.d.ts.map +1 -0
- package/dist/react-hook-form.test.js +28 -0
- package/dist/tanstack-form.d.ts +17 -0
- package/dist/tanstack-form.d.ts.map +1 -0
- package/dist/tanstack-form.js +33 -0
- package/dist/types.d.ts +41 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -0
- package/dist/useWebMcpFormTool.d.ts +5 -0
- package/dist/useWebMcpFormTool.d.ts.map +1 -0
- package/dist/useWebMcpFormTool.js +14 -0
- package/package.json +54 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 jitterbox
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { WebMcpToolDescriptor } from '@tooluminati/core';
|
|
2
|
+
import type { FormDiagnosticSummary, WebMcpFormToolOptions } from './types';
|
|
3
|
+
export declare function createFormValidationSummaryTool<TValues>(options: WebMcpFormToolOptions<TValues>): WebMcpToolDescriptor<Record<string, never>, FormDiagnosticSummary>;
|
|
4
|
+
export declare function createFormSubmitTool<TValues>(options: WebMcpFormToolOptions<TValues>): WebMcpToolDescriptor;
|
|
5
|
+
export declare function createWebMcpFormTools<TValues>(options: WebMcpFormToolOptions<TValues>): WebMcpToolDescriptor[];
|
|
6
|
+
//# sourceMappingURL=create-form-tools.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-form-tools.d.ts","sourceRoot":"","sources":["../src/create-form-tools.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAE9D,OAAO,KAAK,EACV,qBAAqB,EAErB,qBAAqB,EACtB,MAAM,SAAS,CAAC;AAqBjB,wBAAgB,+BAA+B,CAAC,OAAO,EACrD,OAAO,EAAE,qBAAqB,CAAC,OAAO,CAAC,GACtC,oBAAoB,CAAC,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,EAAE,qBAAqB,CAAC,CA+BpE;AAED,wBAAgB,oBAAoB,CAAC,OAAO,EAC1C,OAAO,EAAE,qBAAqB,CAAC,OAAO,CAAC,GACtC,oBAAoB,CA0DtB;AAED,wBAAgB,qBAAqB,CAAC,OAAO,EAC3C,OAAO,EAAE,qBAAqB,CAAC,OAAO,CAAC,GACtC,oBAAoB,EAAE,CAYxB"}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { inferSchemaFromValue } from './infer-schema';
|
|
2
|
+
function formatFormErrors(errors, message) {
|
|
3
|
+
const lines = errors.map((error) => `${error.path ? `${error.path}: ` : ''}${error.message || error.kind}`);
|
|
4
|
+
return [message, ...lines].filter(Boolean).join('\n');
|
|
5
|
+
}
|
|
6
|
+
function applyRedactResult(result, redactResult) {
|
|
7
|
+
if (!redactResult) {
|
|
8
|
+
return result;
|
|
9
|
+
}
|
|
10
|
+
return redactResult(result);
|
|
11
|
+
}
|
|
12
|
+
export function createFormValidationSummaryTool(options) {
|
|
13
|
+
const schema = options.schema ?? inferSchemaFromValue(options.getValues());
|
|
14
|
+
return {
|
|
15
|
+
name: `${options.name}_validation_summary`,
|
|
16
|
+
description: 'Returns form schema, validation messages, and submitting/validating state without submitting.',
|
|
17
|
+
inputSchema: {
|
|
18
|
+
type: 'object',
|
|
19
|
+
properties: {},
|
|
20
|
+
additionalProperties: false,
|
|
21
|
+
},
|
|
22
|
+
annotations: { readOnlyHint: true },
|
|
23
|
+
execute: () => {
|
|
24
|
+
const summary = {
|
|
25
|
+
name: options.name,
|
|
26
|
+
errors: options.getErrors?.() ?? [],
|
|
27
|
+
schema,
|
|
28
|
+
...options.getSummary?.(),
|
|
29
|
+
};
|
|
30
|
+
if (options.redactValues) {
|
|
31
|
+
return {
|
|
32
|
+
...summary,
|
|
33
|
+
values: options.redactValues(options.getValues()),
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
return summary;
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
export function createFormSubmitTool(options) {
|
|
41
|
+
const schema = options.schema ?? inferSchemaFromValue(options.getValues());
|
|
42
|
+
if (!schema) {
|
|
43
|
+
throw new Error(`Could not infer WebMCP schema for form "${options.name}". ` +
|
|
44
|
+
'Provide an explicit schema or use concrete non-null defaults.');
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
name: options.name,
|
|
48
|
+
description: options.description,
|
|
49
|
+
inputSchema: schema,
|
|
50
|
+
annotations: {
|
|
51
|
+
readOnlyHint: false,
|
|
52
|
+
...options.annotations,
|
|
53
|
+
},
|
|
54
|
+
validateArgs: options.validateArgs,
|
|
55
|
+
execute: async (rawArgs) => {
|
|
56
|
+
const values = options.validateArgs
|
|
57
|
+
? options.validateArgs(rawArgs)
|
|
58
|
+
: rawArgs;
|
|
59
|
+
await options.setValues(values);
|
|
60
|
+
const result = await options.submit();
|
|
61
|
+
if (result.success) {
|
|
62
|
+
let output = result.message ?? 'Form submitted successfully.';
|
|
63
|
+
if (result.data !== undefined) {
|
|
64
|
+
output = {
|
|
65
|
+
message: output,
|
|
66
|
+
data: options.redactValues
|
|
67
|
+
? options.redactValues(result.data)
|
|
68
|
+
: result.data,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
return applyRedactResult(output, options.redactResult);
|
|
72
|
+
}
|
|
73
|
+
const errors = result.errors ?? options.getErrors?.() ?? [];
|
|
74
|
+
return applyRedactResult({
|
|
75
|
+
content: [
|
|
76
|
+
{
|
|
77
|
+
type: 'text',
|
|
78
|
+
text: `Form submission failed:\n${formatFormErrors(errors, result.message)}`,
|
|
79
|
+
},
|
|
80
|
+
],
|
|
81
|
+
isError: true,
|
|
82
|
+
}, options.redactResult);
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
export function createWebMcpFormTools(options) {
|
|
87
|
+
const includeValidationSummary = options.includeValidationSummary ?? true;
|
|
88
|
+
const submitTool = createFormSubmitTool(options);
|
|
89
|
+
if (includeValidationSummary) {
|
|
90
|
+
return [
|
|
91
|
+
submitTool,
|
|
92
|
+
createFormValidationSummaryTool(options),
|
|
93
|
+
];
|
|
94
|
+
}
|
|
95
|
+
return [submitTool];
|
|
96
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-form-tools.test.d.ts","sourceRoot":"","sources":["../src/create-form-tools.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import { createFormSubmitTool, createFormValidationSummaryTool, createWebMcpFormTools, } from './create-form-tools';
|
|
3
|
+
describe('create-form-tools', () => {
|
|
4
|
+
const baseOptions = {
|
|
5
|
+
name: 'submit_ticket',
|
|
6
|
+
description: 'Submit a support ticket.',
|
|
7
|
+
getValues: () => ({ subject: 'Help', priority: 'low' }),
|
|
8
|
+
setValues: vi.fn(async () => undefined),
|
|
9
|
+
submit: vi.fn(async () => ({ success: true })),
|
|
10
|
+
};
|
|
11
|
+
it('creates submit and validation summary tools by default', () => {
|
|
12
|
+
const tools = createWebMcpFormTools(baseOptions);
|
|
13
|
+
expect(tools.map((tool) => tool.name)).toEqual([
|
|
14
|
+
'submit_ticket',
|
|
15
|
+
'submit_ticket_validation_summary',
|
|
16
|
+
]);
|
|
17
|
+
});
|
|
18
|
+
it('omits validation summary when disabled', () => {
|
|
19
|
+
const tools = createWebMcpFormTools({
|
|
20
|
+
...baseOptions,
|
|
21
|
+
includeValidationSummary: false,
|
|
22
|
+
});
|
|
23
|
+
expect(tools).toHaveLength(1);
|
|
24
|
+
expect(tools[0]?.name).toBe('submit_ticket');
|
|
25
|
+
});
|
|
26
|
+
it('returns validation summary without submitting', async () => {
|
|
27
|
+
const tool = createFormValidationSummaryTool({
|
|
28
|
+
...baseOptions,
|
|
29
|
+
getErrors: () => [{ kind: 'required', path: 'subject', message: 'Required' }],
|
|
30
|
+
});
|
|
31
|
+
const summary = await tool.execute({}, {});
|
|
32
|
+
expect(summary.errors).toHaveLength(1);
|
|
33
|
+
expect(summary.schema).toBeDefined();
|
|
34
|
+
});
|
|
35
|
+
it('submits through the submit tool execute handler', async () => {
|
|
36
|
+
const submit = vi.fn(async () => ({ success: true, message: 'ok' }));
|
|
37
|
+
const tool = createFormSubmitTool({
|
|
38
|
+
...baseOptions,
|
|
39
|
+
submit,
|
|
40
|
+
});
|
|
41
|
+
const result = await tool.execute({ subject: 'Help', priority: 'low' }, {});
|
|
42
|
+
expect(submit).toHaveBeenCalledOnce();
|
|
43
|
+
expect(result).toBe('ok');
|
|
44
|
+
});
|
|
45
|
+
});
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAEzC,oBAAoB;AACpB,qBAAa,mBAAoB,SAAQ,KAAK;IAC5C,QAAQ,CAAC,MAAM,EAAE,SAAS,EAAE,CAAC;gBAEjB,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE;CAKjD"}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { JsonSchema } from '@tooluminati/core';
|
|
2
|
+
/** @internal Declarative-compat metadata; not shipped as a browser polyfill. */
|
|
3
|
+
export interface FormToolDefinition<TValues = Record<string, unknown>> {
|
|
4
|
+
name: string;
|
|
5
|
+
description: string;
|
|
6
|
+
schema?: JsonSchema;
|
|
7
|
+
getValues: () => TValues;
|
|
8
|
+
setValues: (values: TValues) => void | Promise<void>;
|
|
9
|
+
submit: () => unknown | Promise<unknown>;
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=form-tool-definition.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"form-tool-definition.d.ts","sourceRoot":"","sources":["../src/form-tool-definition.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAEpD,gFAAgF;AAChF,MAAM,WAAW,kBAAkB,CAAC,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IACnE,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,MAAM,CAAC,EAAE,UAAU,CAAC;IACpB,SAAS,EAAE,MAAM,OAAO,CAAC;IACzB,SAAS,EAAE,CAAC,MAAM,EAAE,OAAO,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACrD,MAAM,EAAE,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CAC1C"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/formik.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { WebMcpFormToolOptions } from './types';
|
|
2
|
+
export interface FormikLike<TValues> {
|
|
3
|
+
values: TValues;
|
|
4
|
+
errors?: unknown;
|
|
5
|
+
touched?: unknown;
|
|
6
|
+
isSubmitting?: boolean;
|
|
7
|
+
isValidating?: boolean;
|
|
8
|
+
setValues(values: TValues): void | Promise<void>;
|
|
9
|
+
validateForm(): Promise<unknown>;
|
|
10
|
+
submitForm(): Promise<void>;
|
|
11
|
+
}
|
|
12
|
+
export interface FormikWebMcpOptions<TValues> extends Pick<WebMcpFormToolOptions<TValues>, 'name' | 'description' | 'schema' | 'validateArgs' | 'annotations'> {
|
|
13
|
+
formik: FormikLike<TValues>;
|
|
14
|
+
}
|
|
15
|
+
export declare function useFormikWebMcpTool<TValues>({ formik, ...options }: FormikWebMcpOptions<TValues>): void;
|
|
16
|
+
//# sourceMappingURL=formik.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formik.d.ts","sourceRoot":"","sources":["../src/formik.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAa,qBAAqB,EAAE,MAAM,SAAS,CAAC;AAEhE,MAAM,WAAW,UAAU,CAAC,OAAO;IACjC,MAAM,EAAE,OAAO,CAAC;IAChB,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,SAAS,CAAC,MAAM,EAAE,OAAO,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACjD,YAAY,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC;IACjC,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CAC7B;AAED,MAAM,WAAW,mBAAmB,CAAC,OAAO,CAC1C,SAAQ,IAAI,CACV,qBAAqB,CAAC,OAAO,CAAC,EAC9B,MAAM,GAAG,aAAa,GAAG,QAAQ,GAAG,cAAc,GAAG,aAAa,CACnE;IACD,MAAM,EAAE,UAAU,CAAC,OAAO,CAAC,CAAC;CAC7B;AAkBD,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,EAC3C,MAAM,EACN,GAAG,OAAO,EACX,EAAE,mBAAmB,CAAC,OAAO,CAAC,GAAG,IAAI,CAqBrC"}
|
package/dist/formik.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { useWebMcpFormTool } from './useWebMcpFormTool';
|
|
2
|
+
function flattenErrors(errors, prefix = '') {
|
|
3
|
+
if (!errors || typeof errors !== 'object') {
|
|
4
|
+
return [];
|
|
5
|
+
}
|
|
6
|
+
return Object.entries(errors).flatMap(([key, value]) => {
|
|
7
|
+
const path = prefix ? `${prefix}.${key}` : key;
|
|
8
|
+
if (typeof value === 'string') {
|
|
9
|
+
return [{ path, message: value }];
|
|
10
|
+
}
|
|
11
|
+
return flattenErrors(value, path);
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
export function useFormikWebMcpTool({ formik, ...options }) {
|
|
15
|
+
useWebMcpFormTool({
|
|
16
|
+
...options,
|
|
17
|
+
getValues: () => formik.values,
|
|
18
|
+
setValues: (values) => formik.setValues(values),
|
|
19
|
+
getErrors: () => flattenErrors(formik.errors),
|
|
20
|
+
getSummary: () => ({
|
|
21
|
+
submitting: formik.isSubmitting,
|
|
22
|
+
validating: formik.isValidating,
|
|
23
|
+
touched: Boolean(formik.touched),
|
|
24
|
+
}),
|
|
25
|
+
submit: async () => {
|
|
26
|
+
const errors = await formik.validateForm();
|
|
27
|
+
const flattened = flattenErrors(errors);
|
|
28
|
+
if (flattened.length > 0) {
|
|
29
|
+
return { success: false, errors: flattened };
|
|
30
|
+
}
|
|
31
|
+
await formik.submitForm();
|
|
32
|
+
return { success: true };
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
}
|