@assistant-ui/react-hook-form 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Simon Farshid
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.
package/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # `assistant-ui`
2
+
3
+ `assistant-ui` is a set of React components for AI chat.
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
+ ```
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "@assistant-ui/react-hook-form",
3
+ "version": "0.0.1",
4
+ "license": "MIT",
5
+ "type": "module",
6
+ "peerDependencies": {
7
+ "@assistant-ui/react": "^0.1",
8
+ "react-hook-form": "^7.x.x",
9
+ "@types/react": "*",
10
+ "react": "^18"
11
+ },
12
+ "peerDependenciesMeta": {
13
+ "@types/react": {
14
+ "optional": true
15
+ }
16
+ },
17
+ "dependencies": {
18
+ "zod": "^3.23.8"
19
+ },
20
+ "devDependencies": {
21
+ "@assistant-ui/react": "0.1.1",
22
+ "@assistant-ui/tsconfig": "0.0.0"
23
+ },
24
+ "publishConfig": {
25
+ "access": "public"
26
+ },
27
+ "scripts": {
28
+ "prepublish": "cp ../../README.md ./README.md"
29
+ }
30
+ }
@@ -0,0 +1,16 @@
1
+ import { z } from "zod";
2
+
3
+ export const formTools = {
4
+ set_form_field: {
5
+ description:
6
+ "Sets a form field. Call this function as soon as the user provides the data for each field.",
7
+ parameters: z.object({
8
+ name: z.string(),
9
+ value: z.string(),
10
+ }),
11
+ },
12
+ submit_form: {
13
+ description: "Submits the form. Confirm with user before submitting.",
14
+ parameters: z.object({}),
15
+ },
16
+ };
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { useAssistantForm } from "./useAssistantForm";
2
+ export { formTools } from "./formTools";
@@ -0,0 +1,78 @@
1
+ "use client";
2
+
3
+ import { useAssistantContext } from "@assistant-ui/react/experimental";
4
+ import { useEffect } from "react";
5
+ import {
6
+ type FieldValues,
7
+ type UseFormProps,
8
+ type UseFormReturn,
9
+ useForm,
10
+ } from "react-hook-form";
11
+ import { formTools } from "./formTools";
12
+
13
+ export const useAssistantForm = <
14
+ TFieldValues extends FieldValues = FieldValues,
15
+ // biome-ignore lint/suspicious/noExplicitAny: <explanation>
16
+ TContext = any,
17
+ TTransformedValues extends FieldValues | undefined = undefined,
18
+ >(
19
+ props?: UseFormProps<TFieldValues, TContext>,
20
+ ): UseFormReturn<TFieldValues, TContext, TTransformedValues> => {
21
+ const form = useForm<TFieldValues, TContext, TTransformedValues>(props);
22
+
23
+ const { useModelConfig } = useAssistantContext();
24
+ const registerModelConfigProvider = useModelConfig(
25
+ (c) => c.registerModelConfigProvider,
26
+ );
27
+
28
+ useEffect(() => {
29
+ return registerModelConfigProvider(() => {
30
+ return {
31
+ system: `Form State:\n${JSON.stringify(form.getValues())}`,
32
+
33
+ tools: {
34
+ set_form_field: {
35
+ ...formTools.set_form_field,
36
+ execute: (args) => {
37
+ // biome-ignore lint/suspicious/noExplicitAny: TODO
38
+ form.setValue(args.name as any, args.value as any);
39
+
40
+ return { success: true };
41
+ },
42
+ },
43
+ submit_form: {
44
+ ...formTools.submit_form,
45
+ execute: () => {
46
+ const { _names, _fields } = form.control;
47
+ for (const name of _names.mount) {
48
+ const field = _fields[name];
49
+ if (field?._f) {
50
+ const fieldReference = Array.isArray(field._f.refs)
51
+ ? field._f.refs[0]
52
+ : field._f.ref;
53
+
54
+ if (fieldReference instanceof HTMLElement) {
55
+ const form = fieldReference.closest("form");
56
+ if (form) {
57
+ form.requestSubmit();
58
+
59
+ return { success: true };
60
+ }
61
+ }
62
+ }
63
+ }
64
+
65
+ return {
66
+ success: false,
67
+ message:
68
+ "Unable retrieve the form element. This is a coding error.",
69
+ };
70
+ },
71
+ },
72
+ },
73
+ };
74
+ });
75
+ }, [form, registerModelConfigProvider]);
76
+
77
+ return form;
78
+ };
package/tsconfig.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "extends": "@assistant-ui/tsconfig/base.json",
3
+ "compilerOptions": {
4
+ "paths": {
5
+ "@assistant-ui/*": ["../../packages/*/src"],
6
+ "@assistant-ui/react/*": ["../../packages/react/src/*"]
7
+ }
8
+ },
9
+ "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
10
+ "exclude": ["node_modules"]
11
+ }