@assistant-ui/react-hook-form 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
package/README.md CHANGED
@@ -1,39 +1,5 @@
1
- # `assistant-ui`
1
+ # `@assistant-ui/react-hook-form`
2
2
 
3
- `assistant-ui` is a set of React components for AI chat.
3
+ React Hook Form integration for `@assistant-ui/react`.
4
4
 
5
- - [Discord](https://discord.gg/S9dwgCNEFs)
6
- - [Website](https://assistant-ui.com/)
7
- - [Demo](https://assistant-ui-rsc-example.vercel.app/)
8
-
9
- ## Documentation
10
-
11
- - [Documentation](https://www.assistant-ui.com/docs/getting-started)
12
-
13
- ## Minimal Example with Vercel AI SDK
14
-
15
- ```sh
16
- npx assistant-ui@latest add modal
17
- ```
18
-
19
- ```tsx
20
- "use client";
21
-
22
- import { useChat } from "@ai-sdk/react";
23
- import { AssistantRuntimeProvider } from "@assistant-ui/react";
24
- import { useVercelUseChatRuntime } from "@assistant-ui/react-ai-sdk";
25
- import { AssistantModal } from "@/components/ui/assistant-ui/assistant-modal";
26
-
27
- export default const MyApp = () => {
28
- const chat = useChat({
29
- api: "/api/chat" // your backend route
30
- });
31
- const runtime = useVercelUseChatRuntime(chat);
32
-
33
- return (
34
- <AssistantRuntimeProvider runtime={runtime}>
35
- <AssistantModal />
36
- </AssistantRuntimeProvider>
37
- );
38
- }
39
- ```
5
+ Simply replace `useForm` with `useAssistantForm` to give the chatbot the ability to interact with your form.
package/dist/index.cjs ADDED
@@ -0,0 +1,103 @@
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 __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ formTools: () => formTools,
24
+ useAssistantForm: () => useAssistantForm
25
+ });
26
+ module.exports = __toCommonJS(src_exports);
27
+
28
+ // src/useAssistantForm.tsx
29
+ var import_experimental = require("@assistant-ui/react/experimental");
30
+ var import_react = require("react");
31
+ var import_react_hook_form = require("react-hook-form");
32
+
33
+ // src/formTools.tsx
34
+ var import_zod = require("zod");
35
+ var formTools = {
36
+ set_form_field: {
37
+ description: "Sets a form field. Call this function as soon as the user provides the data for each field.",
38
+ parameters: import_zod.z.object({
39
+ name: import_zod.z.string(),
40
+ value: import_zod.z.string()
41
+ })
42
+ },
43
+ submit_form: {
44
+ description: "Submits the form. Confirm with user before submitting.",
45
+ parameters: import_zod.z.object({})
46
+ }
47
+ };
48
+
49
+ // src/useAssistantForm.tsx
50
+ var useAssistantForm = (props) => {
51
+ const form = (0, import_react_hook_form.useForm)(props);
52
+ const { useModelConfig } = (0, import_experimental.useAssistantContext)();
53
+ const registerModelConfigProvider = useModelConfig(
54
+ (c) => c.registerModelConfigProvider
55
+ );
56
+ (0, import_react.useEffect)(() => {
57
+ return registerModelConfigProvider(() => {
58
+ return {
59
+ system: `Form State:
60
+ ${JSON.stringify(form.getValues())}`,
61
+ tools: {
62
+ set_form_field: {
63
+ ...formTools.set_form_field,
64
+ execute: (args) => {
65
+ form.setValue(args.name, args.value);
66
+ return { success: true };
67
+ }
68
+ },
69
+ submit_form: {
70
+ ...formTools.submit_form,
71
+ execute: () => {
72
+ const { _names, _fields } = form.control;
73
+ for (const name of _names.mount) {
74
+ const field = _fields[name];
75
+ if (field?._f) {
76
+ const fieldReference = Array.isArray(field._f.refs) ? field._f.refs[0] : field._f.ref;
77
+ if (fieldReference instanceof HTMLElement) {
78
+ const form2 = fieldReference.closest("form");
79
+ if (form2) {
80
+ form2.requestSubmit();
81
+ return { success: true };
82
+ }
83
+ }
84
+ }
85
+ }
86
+ return {
87
+ success: false,
88
+ message: "Unable retrieve the form element. This is a coding error."
89
+ };
90
+ }
91
+ }
92
+ }
93
+ };
94
+ });
95
+ }, [form, registerModelConfigProvider]);
96
+ return form;
97
+ };
98
+ // Annotate the CommonJS export names for ESM import in node:
99
+ 0 && (module.exports = {
100
+ formTools,
101
+ useAssistantForm
102
+ });
103
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../src/useAssistantForm.tsx","../src/formTools.tsx"],"sourcesContent":["export { useAssistantForm } from \"./useAssistantForm\";\nexport { formTools } from \"./formTools\";\n","\"use client\";\n\nimport { useAssistantContext } 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 { formTools } from \"./formTools\";\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?: UseFormProps<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 return registerModelConfigProvider(() => {\n return {\n system: `Form State:\\n${JSON.stringify(form.getValues())}`,\n\n tools: {\n set_form_field: {\n ...formTools.set_form_field,\n execute: (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: () => {\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 });\n }, [form, registerModelConfigProvider]);\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,0BAAoC;AACpC,mBAA0B;AAC1B,6BAKO;;;ACTP,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;;;ADHO,IAAM,mBAAmB,CAM9B,UAC8D;AAC9D,QAAM,WAAO,gCAAoD,KAAK;AAEtE,QAAM,EAAE,eAAe,QAAI,yCAAoB;AAC/C,QAAM,8BAA8B;AAAA,IAClC,CAAC,MAAM,EAAE;AAAA,EACX;AAEA,8BAAU,MAAM;AACd,WAAO,4BAA4B,MAAM;AACvC,aAAO;AAAA,QACL,QAAQ;AAAA,EAAgB,KAAK,UAAU,KAAK,UAAU,CAAC,CAAC;AAAA,QAExD,OAAO;AAAA,UACL,gBAAgB;AAAA,YACd,GAAG,UAAU;AAAA,YACb,SAAS,CAAC,SAAS;AAEjB,mBAAK,SAAS,KAAK,MAAa,KAAK,KAAY;AAEjD,qBAAO,EAAE,SAAS,KAAK;AAAA,YACzB;AAAA,UACF;AAAA,UACA,aAAa;AAAA,YACX,GAAG,UAAU;AAAA,YACb,SAAS,MAAM;AACb,oBAAM,EAAE,QAAQ,QAAQ,IAAI,KAAK;AACjC,yBAAW,QAAQ,OAAO,OAAO;AAC/B,sBAAM,QAAQ,QAAQ,IAAI;AAC1B,oBAAI,OAAO,IAAI;AACb,wBAAM,iBAAiB,MAAM,QAAQ,MAAM,GAAG,IAAI,IAC9C,MAAM,GAAG,KAAK,CAAC,IACf,MAAM,GAAG;AAEb,sBAAI,0BAA0B,aAAa;AACzC,0BAAMA,QAAO,eAAe,QAAQ,MAAM;AAC1C,wBAAIA,OAAM;AACR,sBAAAA,MAAK,cAAc;AAEnB,6BAAO,EAAE,SAAS,KAAK;AAAA,oBACzB;AAAA,kBACF;AAAA,gBACF;AAAA,cACF;AAEA,qBAAO;AAAA,gBACL,SAAS;AAAA,gBACT,SACE;AAAA,cACJ;AAAA,YACF;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAAA,IACF,CAAC;AAAA,EACH,GAAG,CAAC,MAAM,2BAA2B,CAAC;AAEtC,SAAO;AACT;","names":["form"]}
@@ -0,0 +1,26 @@
1
+ import { FieldValues, UseFormProps, UseFormReturn } from 'react-hook-form';
2
+ import { z } from 'zod';
3
+
4
+ declare const useAssistantForm: <TFieldValues extends FieldValues = FieldValues, TContext = any, TTransformedValues extends FieldValues | undefined = undefined>(props?: UseFormProps<TFieldValues, TContext>) => UseFormReturn<TFieldValues, TContext, TTransformedValues>;
5
+
6
+ declare const formTools: {
7
+ set_form_field: {
8
+ description: string;
9
+ parameters: z.ZodObject<{
10
+ name: z.ZodString;
11
+ value: z.ZodString;
12
+ }, "strip", z.ZodTypeAny, {
13
+ value: string;
14
+ name: string;
15
+ }, {
16
+ value: string;
17
+ name: string;
18
+ }>;
19
+ };
20
+ submit_form: {
21
+ description: string;
22
+ parameters: z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>;
23
+ };
24
+ };
25
+
26
+ export { formTools, useAssistantForm };
@@ -0,0 +1,26 @@
1
+ import { FieldValues, UseFormProps, UseFormReturn } from 'react-hook-form';
2
+ import { z } from 'zod';
3
+
4
+ declare const useAssistantForm: <TFieldValues extends FieldValues = FieldValues, TContext = any, TTransformedValues extends FieldValues | undefined = undefined>(props?: UseFormProps<TFieldValues, TContext>) => UseFormReturn<TFieldValues, TContext, TTransformedValues>;
5
+
6
+ declare const formTools: {
7
+ set_form_field: {
8
+ description: string;
9
+ parameters: z.ZodObject<{
10
+ name: z.ZodString;
11
+ value: z.ZodString;
12
+ }, "strip", z.ZodTypeAny, {
13
+ value: string;
14
+ name: string;
15
+ }, {
16
+ value: string;
17
+ name: string;
18
+ }>;
19
+ };
20
+ submit_form: {
21
+ description: string;
22
+ parameters: z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>;
23
+ };
24
+ };
25
+
26
+ export { formTools, useAssistantForm };
package/dist/index.js ADDED
@@ -0,0 +1,77 @@
1
+ // src/useAssistantForm.tsx
2
+ import { useAssistantContext } from "@assistant-ui/react/experimental";
3
+ import { useEffect } from "react";
4
+ import {
5
+ useForm
6
+ } from "react-hook-form";
7
+
8
+ // src/formTools.tsx
9
+ import { z } from "zod";
10
+ var formTools = {
11
+ set_form_field: {
12
+ description: "Sets a form field. Call this function as soon as the user provides the data for each field.",
13
+ parameters: z.object({
14
+ name: z.string(),
15
+ value: z.string()
16
+ })
17
+ },
18
+ submit_form: {
19
+ description: "Submits the form. Confirm with user before submitting.",
20
+ parameters: z.object({})
21
+ }
22
+ };
23
+
24
+ // src/useAssistantForm.tsx
25
+ var useAssistantForm = (props) => {
26
+ const form = useForm(props);
27
+ const { useModelConfig } = useAssistantContext();
28
+ const registerModelConfigProvider = useModelConfig(
29
+ (c) => c.registerModelConfigProvider
30
+ );
31
+ useEffect(() => {
32
+ return registerModelConfigProvider(() => {
33
+ return {
34
+ system: `Form State:
35
+ ${JSON.stringify(form.getValues())}`,
36
+ tools: {
37
+ set_form_field: {
38
+ ...formTools.set_form_field,
39
+ execute: (args) => {
40
+ form.setValue(args.name, args.value);
41
+ return { success: true };
42
+ }
43
+ },
44
+ submit_form: {
45
+ ...formTools.submit_form,
46
+ execute: () => {
47
+ const { _names, _fields } = form.control;
48
+ for (const name of _names.mount) {
49
+ const field = _fields[name];
50
+ if (field?._f) {
51
+ const fieldReference = Array.isArray(field._f.refs) ? field._f.refs[0] : field._f.ref;
52
+ if (fieldReference instanceof HTMLElement) {
53
+ const form2 = fieldReference.closest("form");
54
+ if (form2) {
55
+ form2.requestSubmit();
56
+ return { success: true };
57
+ }
58
+ }
59
+ }
60
+ }
61
+ return {
62
+ success: false,
63
+ message: "Unable retrieve the form element. This is a coding error."
64
+ };
65
+ }
66
+ }
67
+ }
68
+ };
69
+ });
70
+ }, [form, registerModelConfigProvider]);
71
+ return form;
72
+ };
73
+ export {
74
+ formTools,
75
+ useAssistantForm
76
+ };
77
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/useAssistantForm.tsx","../src/formTools.tsx"],"sourcesContent":["\"use client\";\n\nimport { useAssistantContext } 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 { formTools } from \"./formTools\";\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?: UseFormProps<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 return registerModelConfigProvider(() => {\n return {\n system: `Form State:\\n${JSON.stringify(form.getValues())}`,\n\n tools: {\n set_form_field: {\n ...formTools.set_form_field,\n execute: (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: () => {\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 });\n }, [form, registerModelConfigProvider]);\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,SAAS,2BAA2B;AACpC,SAAS,iBAAiB;AAC1B;AAAA,EAIE;AAAA,OACK;;;ACTP,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;;;ADHO,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,WAAO,4BAA4B,MAAM;AACvC,aAAO;AAAA,QACL,QAAQ;AAAA,EAAgB,KAAK,UAAU,KAAK,UAAU,CAAC,CAAC;AAAA,QAExD,OAAO;AAAA,UACL,gBAAgB;AAAA,YACd,GAAG,UAAU;AAAA,YACb,SAAS,CAAC,SAAS;AAEjB,mBAAK,SAAS,KAAK,MAAa,KAAK,KAAY;AAEjD,qBAAO,EAAE,SAAS,KAAK;AAAA,YACzB;AAAA,UACF;AAAA,UACA,aAAa;AAAA,YACX,GAAG,UAAU;AAAA,YACb,SAAS,MAAM;AACb,oBAAM,EAAE,QAAQ,QAAQ,IAAI,KAAK;AACjC,yBAAW,QAAQ,OAAO,OAAO;AAC/B,sBAAM,QAAQ,QAAQ,IAAI;AAC1B,oBAAI,OAAO,IAAI;AACb,wBAAM,iBAAiB,MAAM,QAAQ,MAAM,GAAG,IAAI,IAC9C,MAAM,GAAG,KAAK,CAAC,IACf,MAAM,GAAG;AAEb,sBAAI,0BAA0B,aAAa;AACzC,0BAAMA,QAAO,eAAe,QAAQ,MAAM;AAC1C,wBAAIA,OAAM;AACR,sBAAAA,MAAK,cAAc;AAEnB,6BAAO,EAAE,SAAS,KAAK;AAAA,oBACzB;AAAA,kBACF;AAAA,gBACF;AAAA,cACF;AAEA,qBAAO;AAAA,gBACL,SAAS;AAAA,gBACT,SACE;AAAA,cACJ;AAAA,YACF;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAAA,IACF,CAAC;AAAA,EACH,GAAG,CAAC,MAAM,2BAA2B,CAAC;AAEtC,SAAO;AACT;","names":["form"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@assistant-ui/react-hook-form",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "peerDependencies": {
@@ -18,6 +18,7 @@
18
18
  "zod": "^3.23.8"
19
19
  },
20
20
  "devDependencies": {
21
+ "tsup": "^8.1.0",
21
22
  "@assistant-ui/react": "0.1.1",
22
23
  "@assistant-ui/tsconfig": "0.0.0"
23
24
  },
@@ -25,6 +26,6 @@
25
26
  "access": "public"
26
27
  },
27
28
  "scripts": {
28
- "prepublish": "cp ../../README.md ./README.md"
29
+ "build": "tsup src/index.ts --format cjs,esm --dts --sourcemap"
29
30
  }
30
31
  }