@assistant-ui/react-hook-form 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
package/README.md CHANGED
@@ -2,4 +2,4 @@
2
2
 
3
3
  React Hook Form integration for `@assistant-ui/react`.
4
4
 
5
- Simply replace `useForm` with `useAssistantForm` to give the chatbot the ability to interact with your form.
5
+ Simply replace `useForm` with `useAssistantForm` to give the chatbot the ability to interact with your form.
package/dist/index.js CHANGED
@@ -49,6 +49,7 @@ var formTools = {
49
49
  // src/useAssistantForm.tsx
50
50
  var useAssistantForm = (props) => {
51
51
  const form = (0, import_react_hook_form.useForm)(props);
52
+ const { control, getValues, setValue } = form;
52
53
  const { useModelConfig } = (0, import_experimental.useAssistantContext)();
53
54
  const registerModelConfigProvider = useModelConfig(
54
55
  (c) => c.registerModelConfigProvider
@@ -56,19 +57,19 @@ var useAssistantForm = (props) => {
56
57
  (0, import_react.useEffect)(() => {
57
58
  const value = {
58
59
  system: `Form State:
59
- ${JSON.stringify(form.getValues())}`,
60
+ ${JSON.stringify(getValues())}`,
60
61
  tools: {
61
62
  set_form_field: {
62
63
  ...formTools.set_form_field,
63
64
  execute: async (args) => {
64
- form.setValue(args.name, args.value);
65
+ setValue(args.name, args.value);
65
66
  return { success: true };
66
67
  }
67
68
  },
68
69
  submit_form: {
69
70
  ...formTools.submit_form,
70
71
  execute: async () => {
71
- const { _names, _fields } = form.control;
72
+ const { _names, _fields } = control;
72
73
  for (const name of _names.mount) {
73
74
  const field = _fields[name];
74
75
  if (field?._f) {
@@ -91,12 +92,7 @@ ${JSON.stringify(form.getValues())}`,
91
92
  }
92
93
  };
93
94
  return registerModelConfigProvider(() => value);
94
- }, [
95
- form.control,
96
- form.setValue,
97
- form.getValues,
98
- registerModelConfigProvider
99
- ]);
95
+ }, [control, setValue, getValues, registerModelConfigProvider]);
100
96
  const renderFormFieldTool = props?.assistant?.tools?.set_form_field?.render;
101
97
  (0, import_experimental.useAssistantToolRenderer)(
102
98
  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 useAssistantContext,\n useAssistantToolRenderer,\n} from \"@assistant-ui/react/experimental\";\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 tools?: {\n set_form_field?: {\n render?: ToolCallContentPartComponent<\n z.ZodType<typeof formTools.set_form_field>,\n unknown\n >;\n };\n submit_form?: {\n render?: ToolCallContentPartComponent<\n z.ZodType<typeof formTools.submit_form>,\n unknown\n >;\n };\n };\n };\n};\n\nexport const useAssistantForm = <\n TFieldValues extends FieldValues = FieldValues,\n // biome-ignore lint/suspicious/noExplicitAny: <explanation>\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\n const { useModelConfig } = useAssistantContext();\n const registerModelConfigProvider = useModelConfig(\n (c) => c.registerModelConfigProvider,\n );\n\n useEffect(() => {\n const value: ModelConfig = {\n system: `Form State:\\n${JSON.stringify(form.getValues())}`,\n\n tools: {\n set_form_field: {\n ...formTools.set_form_field,\n execute: async (args) => {\n // biome-ignore lint/suspicious/noExplicitAny: TODO\n form.setValue(args.name as any, args.value as any);\n\n return { success: true };\n },\n },\n submit_form: {\n ...formTools.submit_form,\n execute: async () => {\n const { _names, _fields } = form.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 registerModelConfigProvider(() => value);\n }, [\n form.control,\n form.setValue,\n form.getValues,\n registerModelConfigProvider,\n ]);\n\n const renderFormFieldTool = props?.assistant?.tools?.set_form_field?.render;\n useAssistantToolRenderer(\n renderFormFieldTool\n ? {\n name: \"set_form_field\",\n render: renderFormFieldTool,\n }\n : null,\n );\n\n const renderSubmitFormTool = props?.assistant?.tools?.submit_form?.render;\n useAssistantToolRenderer(\n renderSubmitFormTool\n ? {\n name: \"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,0BAKO;AACP,mBAA0B;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;;;ADyBO,IAAM,mBAAmB,CAM9B,UAC8D;AAC9D,QAAM,WAAO,gCAAoD,KAAK;A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V,IACA;AAAA,EACN;AAEA,QAAM,uBAAuB,OAAO,WAAW,OAAO,aAAa;AACnE;AAAA,IACE,uBACI;AAAA,MACE,MAAM;AAAA,MACN,QAAQ;AAAA,IACV,IACA;AAAA,EACN;AAEA,SAAO;AACT;","names":["form"]}
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 useAssistantContext,\n useAssistantToolRenderer,\n} from \"@assistant-ui/react/experimental\";\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 tools?: {\n set_form_field?: {\n render?: ToolCallContentPartComponent<\n z.ZodType<typeof formTools.set_form_field>,\n unknown\n >;\n };\n submit_form?: {\n render?: ToolCallContentPartComponent<\n z.ZodType<typeof formTools.submit_form>,\n unknown\n >;\n };\n };\n };\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 { useModelConfig } = useAssistantContext();\n const registerModelConfigProvider = useModelConfig(\n (c) => c.registerModelConfigProvider,\n );\n\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 registerModelConfigProvider(() => value);\n }, [control, setValue, getValues, registerModelConfigProvider]);\n\n const renderFormFieldTool = props?.assistant?.tools?.set_form_field?.render;\n useAssistantToolRenderer(\n renderFormFieldTool\n ? {\n name: \"set_form_field\",\n render: renderFormFieldTool,\n }\n : null,\n );\n\n const renderSubmitFormTool = props?.assistant?.tools?.submit_form?.render;\n useAssistantToolRenderer(\n renderSubmitFormTool\n ? {\n name: \"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,0BAKO;AACP,mBAA0B;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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E;AAAA,IACE,sBACI;AAAA,MACE,MAAM;AAAA,MACN,QAAQ;AAAA,IACV,IACA;AAAA,EACN;AAEA,QAAM,uBAAuB,OAAO,WAAW,OAAO,aAAa;AACnE;AAAA,IACE,uBACI;AAAA,MACE,MAAM;AAAA,MACN,QAAQ;AAAA,IACV,IACA;AAAA,EACN;AAEA,SAAO;AACT;","names":["form"]}
package/dist/index.mjs CHANGED
@@ -27,6 +27,7 @@ var formTools = {
27
27
  // src/useAssistantForm.tsx
28
28
  var useAssistantForm = (props) => {
29
29
  const form = useForm(props);
30
+ const { control, getValues, setValue } = form;
30
31
  const { useModelConfig } = useAssistantContext();
31
32
  const registerModelConfigProvider = useModelConfig(
32
33
  (c) => c.registerModelConfigProvider
@@ -34,19 +35,19 @@ var useAssistantForm = (props) => {
34
35
  useEffect(() => {
35
36
  const value = {
36
37
  system: `Form State:
37
- ${JSON.stringify(form.getValues())}`,
38
+ ${JSON.stringify(getValues())}`,
38
39
  tools: {
39
40
  set_form_field: {
40
41
  ...formTools.set_form_field,
41
42
  execute: async (args) => {
42
- form.setValue(args.name, args.value);
43
+ setValue(args.name, args.value);
43
44
  return { success: true };
44
45
  }
45
46
  },
46
47
  submit_form: {
47
48
  ...formTools.submit_form,
48
49
  execute: async () => {
49
- const { _names, _fields } = form.control;
50
+ const { _names, _fields } = control;
50
51
  for (const name of _names.mount) {
51
52
  const field = _fields[name];
52
53
  if (field?._f) {
@@ -69,12 +70,7 @@ ${JSON.stringify(form.getValues())}`,
69
70
  }
70
71
  };
71
72
  return registerModelConfigProvider(() => value);
72
- }, [
73
- form.control,
74
- form.setValue,
75
- form.getValues,
76
- registerModelConfigProvider
77
- ]);
73
+ }, [control, setValue, getValues, registerModelConfigProvider]);
78
74
  const renderFormFieldTool = props?.assistant?.tools?.set_form_field?.render;
79
75
  useAssistantToolRenderer(
80
76
  renderFormFieldTool ? {
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/useAssistantForm.tsx","../src/formTools.tsx"],"sourcesContent":["\"use client\";\n\nimport {\n type ModelConfig,\n type ToolCallContentPartComponent,\n useAssistantContext,\n useAssistantToolRenderer,\n} from \"@assistant-ui/react/experimental\";\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 tools?: {\n set_form_field?: {\n render?: ToolCallContentPartComponent<\n z.ZodType<typeof formTools.set_form_field>,\n unknown\n >;\n };\n submit_form?: {\n render?: ToolCallContentPartComponent<\n z.ZodType<typeof formTools.submit_form>,\n unknown\n >;\n };\n };\n };\n};\n\nexport const useAssistantForm = <\n TFieldValues extends FieldValues = FieldValues,\n // biome-ignore lint/suspicious/noExplicitAny: <explanation>\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\n const { useModelConfig } = useAssistantContext();\n const registerModelConfigProvider = useModelConfig(\n (c) => c.registerModelConfigProvider,\n );\n\n useEffect(() => {\n const value: ModelConfig = {\n system: `Form State:\\n${JSON.stringify(form.getValues())}`,\n\n tools: {\n set_form_field: {\n ...formTools.set_form_field,\n execute: async (args) => {\n // biome-ignore lint/suspicious/noExplicitAny: TODO\n form.setValue(args.name as any, args.value as any);\n\n return { success: true };\n },\n },\n submit_form: {\n ...formTools.submit_form,\n execute: async () => {\n const { _names, _fields } = form.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 registerModelConfigProvider(() => value);\n }, [\n form.control,\n form.setValue,\n form.getValues,\n registerModelConfigProvider,\n ]);\n\n const renderFormFieldTool = props?.assistant?.tools?.set_form_field?.render;\n useAssistantToolRenderer(\n renderFormFieldTool\n ? {\n name: \"set_form_field\",\n render: renderFormFieldTool,\n }\n : null,\n );\n\n const renderSubmitFormTool = props?.assistant?.tools?.submit_form?.render;\n useAssistantToolRenderer(\n renderSubmitFormTool\n ? {\n name: \"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;;;ADyBO,IAAM,mBAAmB,CAM9B,UAC8D;AAC9D,QAAM,OAAO,QAAoD,KAAK;AAEtE,QAAM,EAAE,eAAe,IAAI,oBAAoB;AAC/C,QAAM,8BAA8B;AAAA,IAClC,CAAC,MAAM,EAAE;AAAA,EACX;AAEA,YAAU,MAAM;AACd,UAAM,QAAqB;AAAA,MACzB,QAAQ;AAAA,EAAg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form"]}
1
+ {"version":3,"sources":["../src/useAssistantForm.tsx","../src/formTools.tsx"],"sourcesContent":["\"use client\";\n\nimport {\n type ModelConfig,\n type ToolCallContentPartComponent,\n useAssistantContext,\n useAssistantToolRenderer,\n} from \"@assistant-ui/react/experimental\";\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 tools?: {\n set_form_field?: {\n render?: ToolCallContentPartComponent<\n z.ZodType<typeof formTools.set_form_field>,\n unknown\n >;\n };\n submit_form?: {\n render?: ToolCallContentPartComponent<\n z.ZodType<typeof formTools.submit_form>,\n unknown\n >;\n };\n };\n };\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 { useModelConfig } = useAssistantContext();\n const registerModelConfigProvider = useModelConfig(\n (c) => c.registerModelConfigProvider,\n );\n\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 registerModelConfigProvider(() => value);\n }, [control, setValue, getValues, registerModelConfigProvider]);\n\n const renderFormFieldTool = props?.assistant?.tools?.set_form_field?.render;\n useAssistantToolRenderer(\n renderFormFieldTool\n ? {\n name: \"set_form_field\",\n render: renderFormFieldTool,\n }\n : null,\n );\n\n const renderSubmitFormTool = props?.assistant?.tools?.submit_form?.render;\n useAssistantToolRenderer(\n renderSubmitFormTool\n ? {\n name: \"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;;;ADyBO,IAAM,mBAAmB,CAK9B,UAC8D;AAC9D,QAAM,OAAO,QAAoD,KAAK;AACtE,QAAM,EAAE,SAAS,WAAW,SAAS,IAAI;AAEzC,QAAM,EAAE,eAAe,IAAI,oBAAoB;AAC/C,QAAM,8BAA8B;AAAA,IAClC,CAAC,MAAM,EAAE;AAAA,EACX;AAEA,YAAU,MAAM;AACd,UAAM,QAAqB;AAAA,MACzB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@@
1
1
  {
2
2
  "name": "@assistant-ui/react-hook-form",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
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.1.6",
30
+ "@assistant-ui/react": "^0.1.7",
31
31
  "react-hook-form": "^7.x.x",
32
32
  "@types/react": "*",
33
33
  "react": "^18"
@@ -38,8 +38,9 @@
38
38
  }
39
39
  },
40
40
  "devDependencies": {
41
+ "@types/node": "^20.14.7",
41
42
  "tsup": "^8.1.0",
42
- "@assistant-ui/react": "0.1.6",
43
+ "@assistant-ui/react": "0.1.7",
43
44
  "@assistant-ui/tsconfig": "0.0.0"
44
45
  },
45
46
  "publishConfig": {