@assistant-ui/react-hook-form 0.4.9 → 0.4.10
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/index.js +3 -4
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +4 -4
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
@@ -47,11 +47,10 @@ var formTools = {
|
|
47
47
|
};
|
48
48
|
|
49
49
|
// src/useAssistantForm.tsx
|
50
|
-
var import_react3 = require("@assistant-ui/react");
|
51
50
|
var useAssistantForm = (props) => {
|
52
51
|
const form = (0, import_react_hook_form.useForm)(props);
|
53
52
|
const { control, getValues, setValue } = form;
|
54
|
-
const
|
53
|
+
const assistantRuntime = (0, import_react.useAssistantRuntime)();
|
55
54
|
(0, import_react2.useEffect)(() => {
|
56
55
|
const value = {
|
57
56
|
system: `Form State:
|
@@ -89,10 +88,10 @@ ${JSON.stringify(getValues())}`,
|
|
89
88
|
}
|
90
89
|
}
|
91
90
|
};
|
92
|
-
return
|
91
|
+
return assistantRuntime.registerModelConfigProvider({
|
93
92
|
getModelConfig: () => value
|
94
93
|
});
|
95
|
-
}, [control, setValue, getValues,
|
94
|
+
}, [control, setValue, getValues, assistantRuntime]);
|
96
95
|
const renderFormFieldTool = props?.assistant?.tools?.set_form_field?.render;
|
97
96
|
(0, import_react.useAssistantToolUI)(
|
98
97
|
renderFormFieldTool ? {
|
package/dist/index.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/useAssistantForm.tsx","../src/formTools.tsx"],"sourcesContent":["export {\n useAssistantForm,\n type UseAssistantFormProps,\n} from \"./useAssistantForm\";\nexport { formTools } from \"./formTools\";\n","\"use client\";\n\nimport {\n type ModelConfig,\n type ToolCallContentPartComponent,\n useAssistantToolUI,\n} from \"@assistant-ui/react\";\nimport { useEffect } from \"react\";\nimport {\n type FieldValues,\n type UseFormProps,\n type UseFormReturn,\n useForm,\n} from \"react-hook-form\";\nimport type { z } from \"zod\";\nimport { formTools } from \"./formTools\";\
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/useAssistantForm.tsx","../src/formTools.tsx"],"sourcesContent":["export {\n useAssistantForm,\n type UseAssistantFormProps,\n} from \"./useAssistantForm\";\nexport { formTools } from \"./formTools\";\n","\"use client\";\n\nimport {\n type ModelConfig,\n type ToolCallContentPartComponent,\n useAssistantRuntime,\n useAssistantToolUI,\n} from \"@assistant-ui/react\";\nimport { useEffect } from \"react\";\nimport {\n type FieldValues,\n type UseFormProps,\n type UseFormReturn,\n useForm,\n} from \"react-hook-form\";\nimport type { z } from \"zod\";\nimport { formTools } from \"./formTools\";\n\nexport type UseAssistantFormProps<\n TFieldValues extends FieldValues,\n TContext,\n> = UseFormProps<TFieldValues, TContext> & {\n assistant?:\n | {\n tools?:\n | {\n set_form_field?:\n | {\n render?:\n | ToolCallContentPartComponent<\n z.infer<\n (typeof formTools.set_form_field)[\"parameters\"]\n >,\n unknown\n >\n | undefined;\n }\n | undefined;\n submit_form?:\n | {\n render?:\n | ToolCallContentPartComponent<\n z.infer<(typeof formTools.submit_form)[\"parameters\"]>,\n unknown\n >\n | undefined;\n }\n | undefined;\n }\n | undefined;\n }\n | undefined;\n};\n\nexport const useAssistantForm = <\n TFieldValues extends FieldValues = FieldValues,\n TContext = any,\n TTransformedValues extends FieldValues | undefined = undefined,\n>(\n props?: UseAssistantFormProps<TFieldValues, TContext>,\n): UseFormReturn<TFieldValues, TContext, TTransformedValues> => {\n const form = useForm<TFieldValues, TContext, TTransformedValues>(props);\n const { control, getValues, setValue } = form;\n\n const assistantRuntime = useAssistantRuntime();\n useEffect(() => {\n const value: ModelConfig = {\n system: `Form State:\\n${JSON.stringify(getValues())}`,\n\n tools: {\n set_form_field: {\n ...formTools.set_form_field,\n execute: async (args) => {\n setValue(args.name, args.value);\n\n return { success: true };\n },\n },\n submit_form: {\n ...formTools.submit_form,\n execute: async () => {\n const { _names, _fields } = control;\n for (const name of _names.mount) {\n const field = _fields[name];\n if (field?._f) {\n const fieldReference = Array.isArray(field._f.refs)\n ? field._f.refs[0]\n : field._f.ref;\n\n if (fieldReference instanceof HTMLElement) {\n const form = fieldReference.closest(\"form\");\n if (form) {\n form.requestSubmit();\n\n return { success: true };\n }\n }\n }\n }\n\n return {\n success: false,\n message:\n \"Unable retrieve the form element. This is a coding error.\",\n };\n },\n },\n },\n };\n return assistantRuntime.registerModelConfigProvider({\n getModelConfig: () => value,\n });\n }, [control, setValue, getValues, assistantRuntime]);\n\n const renderFormFieldTool = props?.assistant?.tools?.set_form_field?.render;\n useAssistantToolUI(\n renderFormFieldTool\n ? {\n toolName: \"set_form_field\",\n render: renderFormFieldTool,\n }\n : null,\n );\n\n const renderSubmitFormTool = props?.assistant?.tools?.submit_form?.render;\n useAssistantToolUI(\n renderSubmitFormTool\n ? {\n toolName: \"submit_form\",\n render: renderSubmitFormTool,\n }\n : null,\n );\n\n return form;\n};\n","import { z } from \"zod\";\n\nexport const formTools = {\n set_form_field: {\n description:\n \"Sets a form field. Call this function as soon as the user provides the data for each field.\",\n parameters: z.object({\n name: z.string(),\n value: z.string(),\n }),\n },\n submit_form: {\n description: \"Submits the form. Confirm with user before submitting.\",\n parameters: z.object({}),\n },\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACEA,mBAKO;AACP,IAAAA,gBAA0B;AAC1B,6BAKO;;;ACdP,iBAAkB;AAEX,IAAM,YAAY;AAAA,EACvB,gBAAgB;AAAA,IACd,aACE;AAAA,IACF,YAAY,aAAE,OAAO;AAAA,MACnB,MAAM,aAAE,OAAO;AAAA,MACf,OAAO,aAAE,OAAO;AAAA,IAClB,CAAC;AAAA,EACH;AAAA,EACA,aAAa;AAAA,IACX,aAAa;AAAA,IACb,YAAY,aAAE,OAAO,CAAC,CAAC;AAAA,EACzB;AACF;;;ADuCO,IAAM,mBAAmB,CAK9B,UAC8D;AAC9D,QAAM,WAAO,gCAAoD,KAAK;AACtE,QAAM,EAAE,SAA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ACnE;AAAA,IACE,uBACI;AAAA,MACE,UAAU;AAAA,MACV,QAAQ;AAAA,IACV,IACA;AAAA,EACN;AAEA,SAAO;AACT;","names":["import_react","form"]}
|
package/dist/index.mjs
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
// src/useAssistantForm.tsx
|
2
2
|
import {
|
3
|
+
useAssistantRuntime,
|
3
4
|
useAssistantToolUI
|
4
5
|
} from "@assistant-ui/react";
|
5
6
|
import { useEffect } from "react";
|
@@ -24,11 +25,10 @@ var formTools = {
|
|
24
25
|
};
|
25
26
|
|
26
27
|
// src/useAssistantForm.tsx
|
27
|
-
import { useAssistantActionsStore } from "@assistant-ui/react";
|
28
28
|
var useAssistantForm = (props) => {
|
29
29
|
const form = useForm(props);
|
30
30
|
const { control, getValues, setValue } = form;
|
31
|
-
const
|
31
|
+
const assistantRuntime = useAssistantRuntime();
|
32
32
|
useEffect(() => {
|
33
33
|
const value = {
|
34
34
|
system: `Form State:
|
@@ -66,10 +66,10 @@ ${JSON.stringify(getValues())}`,
|
|
66
66
|
}
|
67
67
|
}
|
68
68
|
};
|
69
|
-
return
|
69
|
+
return assistantRuntime.registerModelConfigProvider({
|
70
70
|
getModelConfig: () => value
|
71
71
|
});
|
72
|
-
}, [control, setValue, getValues,
|
72
|
+
}, [control, setValue, getValues, assistantRuntime]);
|
73
73
|
const renderFormFieldTool = props?.assistant?.tools?.set_form_field?.render;
|
74
74
|
useAssistantToolUI(
|
75
75
|
renderFormFieldTool ? {
|
package/dist/index.mjs.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../src/useAssistantForm.tsx","../src/formTools.tsx"],"sourcesContent":["\"use client\";\n\nimport {\n type ModelConfig,\n type ToolCallContentPartComponent,\n useAssistantToolUI,\n} from \"@assistant-ui/react\";\nimport { useEffect } from \"react\";\nimport {\n type FieldValues,\n type UseFormProps,\n type UseFormReturn,\n useForm,\n} from \"react-hook-form\";\nimport type { z } from \"zod\";\nimport { formTools } from \"./formTools\";\
|
1
|
+
{"version":3,"sources":["../src/useAssistantForm.tsx","../src/formTools.tsx"],"sourcesContent":["\"use client\";\n\nimport {\n type ModelConfig,\n type ToolCallContentPartComponent,\n useAssistantRuntime,\n useAssistantToolUI,\n} from \"@assistant-ui/react\";\nimport { useEffect } from \"react\";\nimport {\n type FieldValues,\n type UseFormProps,\n type UseFormReturn,\n useForm,\n} from \"react-hook-form\";\nimport type { z } from \"zod\";\nimport { formTools } from \"./formTools\";\n\nexport type UseAssistantFormProps<\n TFieldValues extends FieldValues,\n TContext,\n> = UseFormProps<TFieldValues, TContext> & {\n assistant?:\n | {\n tools?:\n | {\n set_form_field?:\n | {\n render?:\n | ToolCallContentPartComponent<\n z.infer<\n (typeof formTools.set_form_field)[\"parameters\"]\n >,\n unknown\n >\n | undefined;\n }\n | undefined;\n submit_form?:\n | {\n render?:\n | ToolCallContentPartComponent<\n z.infer<(typeof formTools.submit_form)[\"parameters\"]>,\n unknown\n >\n | undefined;\n }\n | undefined;\n }\n | undefined;\n }\n | undefined;\n};\n\nexport const useAssistantForm = <\n TFieldValues extends FieldValues = FieldValues,\n TContext = any,\n TTransformedValues extends FieldValues | undefined = undefined,\n>(\n props?: UseAssistantFormProps<TFieldValues, TContext>,\n): UseFormReturn<TFieldValues, TContext, TTransformedValues> => {\n const form = useForm<TFieldValues, TContext, TTransformedValues>(props);\n const { control, getValues, setValue } = form;\n\n const assistantRuntime = useAssistantRuntime();\n useEffect(() => {\n const value: ModelConfig = {\n system: `Form State:\\n${JSON.stringify(getValues())}`,\n\n tools: {\n set_form_field: {\n ...formTools.set_form_field,\n execute: async (args) => {\n setValue(args.name, args.value);\n\n return { success: true };\n },\n },\n submit_form: {\n ...formTools.submit_form,\n execute: async () => {\n const { _names, _fields } = control;\n for (const name of _names.mount) {\n const field = _fields[name];\n if (field?._f) {\n const fieldReference = Array.isArray(field._f.refs)\n ? field._f.refs[0]\n : field._f.ref;\n\n if (fieldReference instanceof HTMLElement) {\n const form = fieldReference.closest(\"form\");\n if (form) {\n form.requestSubmit();\n\n return { success: true };\n }\n }\n }\n }\n\n return {\n success: false,\n message:\n \"Unable retrieve the form element. This is a coding error.\",\n };\n },\n },\n },\n };\n return assistantRuntime.registerModelConfigProvider({\n getModelConfig: () => value,\n });\n }, [control, setValue, getValues, assistantRuntime]);\n\n const renderFormFieldTool = props?.assistant?.tools?.set_form_field?.render;\n useAssistantToolUI(\n renderFormFieldTool\n ? {\n toolName: \"set_form_field\",\n render: renderFormFieldTool,\n }\n : null,\n );\n\n const renderSubmitFormTool = props?.assistant?.tools?.submit_form?.render;\n useAssistantToolUI(\n renderSubmitFormTool\n ? {\n toolName: \"submit_form\",\n render: renderSubmitFormTool,\n }\n : null,\n );\n\n return form;\n};\n","import { z } from \"zod\";\n\nexport const formTools = {\n set_form_field: {\n description:\n \"Sets a form field. Call this function as soon as the user provides the data for each field.\",\n parameters: z.object({\n name: z.string(),\n value: z.string(),\n }),\n },\n submit_form: {\n description: \"Submits the form. Confirm with user before submitting.\",\n parameters: z.object({}),\n },\n};\n"],"mappings":";AAEA;AAAA,EAGE;AAAA,EACA;AAAA,OACK;AACP,SAAS,iBAAiB;AAC1B;AAAA,EAIE;AAAA,OACK;;;ACdP,SAAS,SAAS;AAEX,IAAM,YAAY;AAAA,EACvB,gBAAgB;AAAA,IACd,aACE;AAAA,IACF,YAAY,EAAE,OAAO;AAAA,MACnB,MAAM,EAAE,OAAO;AAAA,MACf,OAAO,EAAE,OAAO;AAAA,IAClB,CAAC;AAAA,EACH;AAAA,EACA,aAAa;AAAA,IACX,aAAa;AAAA,IACb,YAAY,EAAE,OAAO,CAAC,CAAC;AAAA,EACzB;AACF;;;ADuCO,IAAM,mBAAmB,CAK9B,UAC8D;AAC9D,QAAM,OAAO,QAAoD,KAAK;AACtE,QAAM,EAAE,SAAS,WAAW,SAAS,IAAI;AAEzC,QAAM,mBAAmB,oBAAoB;AAC7C,YAAU,MAAM;AACd,UAAM,QAAqB;AAAA,MACzB,QAA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form"]}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@assistant-ui/react-hook-form",
|
3
|
-
"version": "0.4.
|
3
|
+
"version": "0.4.10",
|
4
4
|
"license": "MIT",
|
5
5
|
"exports": {
|
6
6
|
".": {
|
@@ -27,7 +27,7 @@
|
|
27
27
|
"zod": "^3.23.8"
|
28
28
|
},
|
29
29
|
"peerDependencies": {
|
30
|
-
"@assistant-ui/react": "^0.5.
|
30
|
+
"@assistant-ui/react": "^0.5.61",
|
31
31
|
"@types/react": "*",
|
32
32
|
"react": "^18",
|
33
33
|
"react-hook-form": "^7.x.x"
|