@assistant-ui/react-hook-form 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+
2
+ > @assistant-ui/react-hook-form@0.0.4 build /home/runner/work/assistant-ui/assistant-ui/packages/react-hook-form
3
+ > tsup src/index.ts --format cjs,esm --dts --sourcemap
4
+
5
+ CLI Building entry: src/index.ts
6
+ CLI Using tsconfig: tsconfig.json
7
+ CLI tsup v8.1.0
8
+ CLI Target: esnext
9
+ CJS Build start
10
+ ESM Build start
11
+ ESM dist/index.js 2.69 KB
12
+ ESM dist/index.js.map 5.69 KB
13
+ ESM ⚡️ Build success in 63ms
14
+ CJS dist/index.cjs 3.88 KB
15
+ CJS dist/index.cjs.map 5.82 KB
16
+ CJS ⚡️ Build success in 68ms
17
+ DTS Build start
18
+ DTS ⚡️ Build success in 3910ms
19
+ DTS dist/index.d.cts 1.68 KB
20
+ DTS dist/index.d.ts 1.68 KB
package/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # @assistant-ui/react-hook-form
2
2
 
3
+ ## 0.0.4
4
+
5
+ ### Patch Changes
6
+
7
+ - 671dc86: feat: Tool Render functions
8
+ - Updated dependencies [671dc86]
9
+ - @assistant-ui/react@0.1.5
10
+
3
11
  ## 0.0.3
4
12
 
5
13
  ### Patch Changes
package/dist/index.cjs ADDED
@@ -0,0 +1,121 @@
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
+ const value = {
58
+ system: `Form State:
59
+ ${JSON.stringify(form.getValues())}`,
60
+ tools: {
61
+ set_form_field: {
62
+ ...formTools.set_form_field,
63
+ execute: async (args) => {
64
+ form.setValue(args.name, args.value);
65
+ return { success: true };
66
+ }
67
+ },
68
+ submit_form: {
69
+ ...formTools.submit_form,
70
+ execute: async () => {
71
+ const { _names, _fields } = form.control;
72
+ for (const name of _names.mount) {
73
+ const field = _fields[name];
74
+ if (field?._f) {
75
+ const fieldReference = Array.isArray(field._f.refs) ? field._f.refs[0] : field._f.ref;
76
+ if (fieldReference instanceof HTMLElement) {
77
+ const form2 = fieldReference.closest("form");
78
+ if (form2) {
79
+ form2.requestSubmit();
80
+ return { success: true };
81
+ }
82
+ }
83
+ }
84
+ }
85
+ return {
86
+ success: false,
87
+ message: "Unable retrieve the form element. This is a coding error."
88
+ };
89
+ }
90
+ }
91
+ }
92
+ };
93
+ return registerModelConfigProvider(() => value);
94
+ }, [
95
+ form.control,
96
+ form.setValue,
97
+ form.getValues,
98
+ registerModelConfigProvider
99
+ ]);
100
+ const renderFormFieldTool = props?.assistant?.tools?.set_form_field?.render;
101
+ (0, import_experimental.useAssistantToolRenderer)(
102
+ renderFormFieldTool ? {
103
+ name: "set_form_field",
104
+ render: renderFormFieldTool
105
+ } : null
106
+ );
107
+ const renderSubmitFormTool = props?.assistant?.tools?.submit_form?.render;
108
+ (0, import_experimental.useAssistantToolRenderer)(
109
+ renderSubmitFormTool ? {
110
+ name: "submit_form",
111
+ render: renderSubmitFormTool
112
+ } : null
113
+ );
114
+ return form;
115
+ };
116
+ // Annotate the CommonJS export names for ESM import in node:
117
+ 0 && (module.exports = {
118
+ formTools,
119
+ useAssistantForm
120
+ });
121
+ //# 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 {\n type ModelConfig,\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 type { ToolRenderComponent } from \"../../react/src/model-config/ToolRenderComponent\";\nimport { formTools } from \"./formTools\";\n\ntype UseAssistantFormProps<\n TFieldValues extends FieldValues,\n TContext,\n> = UseFormProps<TFieldValues, TContext> & {\n assistant?: {\n tools?: {\n set_form_field?: {\n render?: ToolRenderComponent<\n z.ZodType<typeof formTools.set_form_field>,\n unknown\n >;\n };\n submit_form?: {\n render?: ToolRenderComponent<\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,0BAIO;AACP,mBAA0B;AAC1B,6BAKO;;;ACbP,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;AAEtE,QAAM,EAAE,eAAe,QAAI,yCAAoB;AAC/C,QAAM,8BAA8B;AAAA,IAClC,CAAC,MAAM,EAAE;AAAA,EACX;AAEA,8BAAU,MAAM;AACd,UAAM,QAAqB;AAAA,MACzB,QAAQ;AAAA,EAAgB,KAAK,UAAU,KAAK,UAAU,CAAC,CAAC;AAAA,MAExD,OAAO;AAAA,QACL,gBAAgB;AAAA,UACd,GAAG,UAAU;AAAA,UACb,SAAS,OAAO,SAAS;AAEvB,iBAAK,SAAS,KAAK,MAAa,KAAK,KAAY;AAEjD,mBAAO,EAAE,SAAS,KAAK;AAAA,UACzB;AAAA,QACF;AAAA,QACA,aAAa;AAAA,UACX,GAAG,UAAU;AAAA,UACb,SAAS,YAAY;AACnB,kBAAM,EAAE,QAAQ,QAAQ,IAAI,KAAK;AACjC,uBAAW,QAAQ,OAAO,OAAO;AAC/B,oBAAM,QAAQ,QAAQ,IAAI;AAC1B,kBAAI,OAAO,IAAI;AACb,sBAAM,iBAAiB,MAAM,QAAQ,MAAM,GAAG,IAAI,IAC9C,MAAM,GAAG,KAAK,CAAC,IACf,MAAM,GAAG;AAEb,oBAAI,0BAA0B,aAAa;AACzC,wBAAMA,QAAO,eAAe,QAAQ,MAAM;AAC1C,sBAAIA,OAAM;AACR,oBAAAA,MAAK,cAAc;AAEnB,2BAAO,EAAE,SAAS,KAAK;AAAA,kBACzB;AAAA,gBACF;AAAA,cACF;AAAA,YACF;AAEA,mBAAO;AAAA,cACL,SAAS;AAAA,cACT,SACE;AAAA,YACJ;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAAA,IACF;AACA,WAAO,4BAA4B,MAAM,KAAK;AAAA,EAChD,GAAG;AAAA,IACD,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AAAA,IACL;AAAA,EACF,CAAC;AAED,QAAM,sBAAsB,OAAO,WAAW,OAAO,gBAAgB;AACrE;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"]}
@@ -0,0 +1,52 @@
1
+ import { FieldValues, UseFormReturn, UseFormProps } from 'react-hook-form';
2
+ import { z } from 'zod';
3
+ import { ComponentType } from 'react';
4
+
5
+ type ToolCallContentPart<TArgs = unknown, TResult = unknown> = {
6
+ type: "tool-call";
7
+ toolCallId: string;
8
+ toolName: string;
9
+ args: TArgs;
10
+ result?: TResult;
11
+ };
12
+
13
+ type ToolRenderComponent<TArgs, TResult> = ComponentType<{
14
+ part: ToolCallContentPart<TArgs, TResult>;
15
+ status: "done" | "in_progress" | "error";
16
+ }>;
17
+
18
+ declare const formTools: {
19
+ set_form_field: {
20
+ description: string;
21
+ parameters: z.ZodObject<{
22
+ name: z.ZodString;
23
+ value: z.ZodString;
24
+ }, "strip", z.ZodTypeAny, {
25
+ name: string;
26
+ value: string;
27
+ }, {
28
+ name: string;
29
+ value: string;
30
+ }>;
31
+ };
32
+ submit_form: {
33
+ description: string;
34
+ parameters: z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>;
35
+ };
36
+ };
37
+
38
+ type UseAssistantFormProps<TFieldValues extends FieldValues, TContext> = UseFormProps<TFieldValues, TContext> & {
39
+ assistant?: {
40
+ tools?: {
41
+ set_form_field?: {
42
+ render?: ToolRenderComponent<z.ZodType<typeof formTools.set_form_field>, unknown>;
43
+ };
44
+ submit_form?: {
45
+ render?: ToolRenderComponent<z.ZodType<typeof formTools.submit_form>, unknown>;
46
+ };
47
+ };
48
+ };
49
+ };
50
+ declare const useAssistantForm: <TFieldValues extends FieldValues = FieldValues, TContext = any, TTransformedValues extends FieldValues | undefined = undefined>(props?: UseAssistantFormProps<TFieldValues, TContext>) => UseFormReturn<TFieldValues, TContext, TTransformedValues>;
51
+
52
+ export { formTools, useAssistantForm };
@@ -0,0 +1,52 @@
1
+ import { FieldValues, UseFormReturn, UseFormProps } from 'react-hook-form';
2
+ import { z } from 'zod';
3
+ import { ComponentType } from 'react';
4
+
5
+ type ToolCallContentPart<TArgs = unknown, TResult = unknown> = {
6
+ type: "tool-call";
7
+ toolCallId: string;
8
+ toolName: string;
9
+ args: TArgs;
10
+ result?: TResult;
11
+ };
12
+
13
+ type ToolRenderComponent<TArgs, TResult> = ComponentType<{
14
+ part: ToolCallContentPart<TArgs, TResult>;
15
+ status: "done" | "in_progress" | "error";
16
+ }>;
17
+
18
+ declare const formTools: {
19
+ set_form_field: {
20
+ description: string;
21
+ parameters: z.ZodObject<{
22
+ name: z.ZodString;
23
+ value: z.ZodString;
24
+ }, "strip", z.ZodTypeAny, {
25
+ name: string;
26
+ value: string;
27
+ }, {
28
+ name: string;
29
+ value: string;
30
+ }>;
31
+ };
32
+ submit_form: {
33
+ description: string;
34
+ parameters: z.ZodObject<{}, "strip", z.ZodTypeAny, {}, {}>;
35
+ };
36
+ };
37
+
38
+ type UseAssistantFormProps<TFieldValues extends FieldValues, TContext> = UseFormProps<TFieldValues, TContext> & {
39
+ assistant?: {
40
+ tools?: {
41
+ set_form_field?: {
42
+ render?: ToolRenderComponent<z.ZodType<typeof formTools.set_form_field>, unknown>;
43
+ };
44
+ submit_form?: {
45
+ render?: ToolRenderComponent<z.ZodType<typeof formTools.submit_form>, unknown>;
46
+ };
47
+ };
48
+ };
49
+ };
50
+ declare const useAssistantForm: <TFieldValues extends FieldValues = FieldValues, TContext = any, TTransformedValues extends FieldValues | undefined = undefined>(props?: UseAssistantFormProps<TFieldValues, TContext>) => UseFormReturn<TFieldValues, TContext, TTransformedValues>;
51
+
52
+ export { formTools, useAssistantForm };
package/dist/index.js ADDED
@@ -0,0 +1,98 @@
1
+ // src/useAssistantForm.tsx
2
+ import {
3
+ useAssistantContext,
4
+ useAssistantToolRenderer
5
+ } from "@assistant-ui/react/experimental";
6
+ import { useEffect } from "react";
7
+ import {
8
+ useForm
9
+ } from "react-hook-form";
10
+
11
+ // src/formTools.tsx
12
+ import { z } from "zod";
13
+ var formTools = {
14
+ set_form_field: {
15
+ description: "Sets a form field. Call this function as soon as the user provides the data for each field.",
16
+ parameters: z.object({
17
+ name: z.string(),
18
+ value: z.string()
19
+ })
20
+ },
21
+ submit_form: {
22
+ description: "Submits the form. Confirm with user before submitting.",
23
+ parameters: z.object({})
24
+ }
25
+ };
26
+
27
+ // src/useAssistantForm.tsx
28
+ var useAssistantForm = (props) => {
29
+ const form = useForm(props);
30
+ const { useModelConfig } = useAssistantContext();
31
+ const registerModelConfigProvider = useModelConfig(
32
+ (c) => c.registerModelConfigProvider
33
+ );
34
+ useEffect(() => {
35
+ const value = {
36
+ system: `Form State:
37
+ ${JSON.stringify(form.getValues())}`,
38
+ tools: {
39
+ set_form_field: {
40
+ ...formTools.set_form_field,
41
+ execute: async (args) => {
42
+ form.setValue(args.name, args.value);
43
+ return { success: true };
44
+ }
45
+ },
46
+ submit_form: {
47
+ ...formTools.submit_form,
48
+ execute: async () => {
49
+ const { _names, _fields } = form.control;
50
+ for (const name of _names.mount) {
51
+ const field = _fields[name];
52
+ if (field?._f) {
53
+ const fieldReference = Array.isArray(field._f.refs) ? field._f.refs[0] : field._f.ref;
54
+ if (fieldReference instanceof HTMLElement) {
55
+ const form2 = fieldReference.closest("form");
56
+ if (form2) {
57
+ form2.requestSubmit();
58
+ return { success: true };
59
+ }
60
+ }
61
+ }
62
+ }
63
+ return {
64
+ success: false,
65
+ message: "Unable retrieve the form element. This is a coding error."
66
+ };
67
+ }
68
+ }
69
+ }
70
+ };
71
+ return registerModelConfigProvider(() => value);
72
+ }, [
73
+ form.control,
74
+ form.setValue,
75
+ form.getValues,
76
+ registerModelConfigProvider
77
+ ]);
78
+ const renderFormFieldTool = props?.assistant?.tools?.set_form_field?.render;
79
+ useAssistantToolRenderer(
80
+ renderFormFieldTool ? {
81
+ name: "set_form_field",
82
+ render: renderFormFieldTool
83
+ } : null
84
+ );
85
+ const renderSubmitFormTool = props?.assistant?.tools?.submit_form?.render;
86
+ useAssistantToolRenderer(
87
+ renderSubmitFormTool ? {
88
+ name: "submit_form",
89
+ render: renderSubmitFormTool
90
+ } : null
91
+ );
92
+ return form;
93
+ };
94
+ export {
95
+ formTools,
96
+ useAssistantForm
97
+ };
98
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/useAssistantForm.tsx","../src/formTools.tsx"],"sourcesContent":["\"use client\";\n\nimport {\n type ModelConfig,\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 type { ToolRenderComponent } from \"../../react/src/model-config/ToolRenderComponent\";\nimport { formTools } from \"./formTools\";\n\ntype UseAssistantFormProps<\n TFieldValues extends FieldValues,\n TContext,\n> = UseFormProps<TFieldValues, TContext> & {\n assistant?: {\n tools?: {\n set_form_field?: {\n render?: ToolRenderComponent<\n z.ZodType<typeof formTools.set_form_field>,\n unknown\n >;\n };\n submit_form?: {\n render?: ToolRenderComponent<\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,EAEE;AAAA,EACA;AAAA,OACK;AACP,SAAS,iBAAiB;AAC1B;AAAA,EAIE;AAAA,OACK;;;ACbP,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,EAAgB,KAAK,UAAU,KAAK,UAAU,CAAC,CAAC;AAAA,MAExD,OAAO;AAAA,QACL,gBAAgB;AAAA,UACd,GAAG,UAAU;AAAA,UACb,SAAS,OAAO,SAAS;AAEvB,iBAAK,SAAS,KAAK,MAAa,KAAK,KAAY;AAEjD,mBAAO,EAAE,SAAS,KAAK;AAAA,UACzB;AAAA,QACF;AAAA,QACA,aAAa;AAAA,UACX,GAAG,UAAU;AAAA,UACb,SAAS,YAAY;AACnB,kBAAM,EAAE,QAAQ,QAAQ,IAAI,KAAK;AACjC,uBAAW,QAAQ,OAAO,OAAO;AAC/B,oBAAM,QAAQ,QAAQ,IAAI;AAC1B,kBAAI,OAAO,IAAI;AACb,sBAAM,iBAAiB,MAAM,QAAQ,MAAM,GAAG,IAAI,IAC9C,MAAM,GAAG,KAAK,CAAC,IACf,MAAM,GAAG;AAEb,oBAAI,0BAA0B,aAAa;AACzC,wBAAMA,QAAO,eAAe,QAAQ,MAAM;AAC1C,sBAAIA,OAAM;AACR,oBAAAA,MAAK,cAAc;AAEnB,2BAAO,EAAE,SAAS,KAAK;AAAA,kBACzB;AAAA,gBACF;AAAA,cACF;AAAA,YACF;AAEA,mBAAO;AAAA,cACL,SAAS;AAAA,cACT,SACE;AAAA,YACJ;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAAA,IACF;AACA,WAAO,4BAA4B,MAAM,KAAK;AAAA,EAChD,GAAG;AAAA,IACD,KAAK;AAAA,IACL,KAAK;AAAA,IACL,KAAK;AAAA,IACL;AAAA,EACF,CAAC;AAED,QAAM,sBAAsB,OAAO,WAAW,OAAO,gBAAgB;AACrE;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/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@assistant-ui/react-hook-form",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "peerDependencies": {
7
- "@assistant-ui/react": "^0.1.3",
7
+ "@assistant-ui/react": "^0.1.5",
8
8
  "react-hook-form": "^7.x.x",
9
9
  "@types/react": "*",
10
10
  "react": "^18"
@@ -19,7 +19,7 @@
19
19
  },
20
20
  "devDependencies": {
21
21
  "tsup": "^8.1.0",
22
- "@assistant-ui/react": "0.1.3",
22
+ "@assistant-ui/react": "0.1.5",
23
23
  "@assistant-ui/tsconfig": "0.0.0"
24
24
  },
25
25
  "publishConfig": {
@@ -1,6 +1,10 @@
1
1
  "use client";
2
2
 
3
- import { useAssistantContext } from "@assistant-ui/react/experimental";
3
+ import {
4
+ type ModelConfig,
5
+ useAssistantContext,
6
+ useAssistantToolRenderer,
7
+ } from "@assistant-ui/react/experimental";
4
8
  import { useEffect } from "react";
5
9
  import {
6
10
  type FieldValues,
@@ -8,15 +12,39 @@ import {
8
12
  type UseFormReturn,
9
13
  useForm,
10
14
  } from "react-hook-form";
15
+ import type { z } from "zod";
16
+ import type { ToolRenderComponent } from "../../react/src/model-config/ToolRenderComponent";
11
17
  import { formTools } from "./formTools";
12
18
 
19
+ type UseAssistantFormProps<
20
+ TFieldValues extends FieldValues,
21
+ TContext,
22
+ > = UseFormProps<TFieldValues, TContext> & {
23
+ assistant?: {
24
+ tools?: {
25
+ set_form_field?: {
26
+ render?: ToolRenderComponent<
27
+ z.ZodType<typeof formTools.set_form_field>,
28
+ unknown
29
+ >;
30
+ };
31
+ submit_form?: {
32
+ render?: ToolRenderComponent<
33
+ z.ZodType<typeof formTools.submit_form>,
34
+ unknown
35
+ >;
36
+ };
37
+ };
38
+ };
39
+ };
40
+
13
41
  export const useAssistantForm = <
14
42
  TFieldValues extends FieldValues = FieldValues,
15
43
  // biome-ignore lint/suspicious/noExplicitAny: <explanation>
16
44
  TContext = any,
17
45
  TTransformedValues extends FieldValues | undefined = undefined,
18
46
  >(
19
- props?: UseFormProps<TFieldValues, TContext>,
47
+ props?: UseAssistantFormProps<TFieldValues, TContext>,
20
48
  ): UseFormReturn<TFieldValues, TContext, TTransformedValues> => {
21
49
  const form = useForm<TFieldValues, TContext, TTransformedValues>(props);
22
50
 
@@ -26,53 +54,77 @@ export const useAssistantForm = <
26
54
  );
27
55
 
28
56
  useEffect(() => {
29
- return registerModelConfigProvider(() => {
30
- return {
31
- system: `Form State:\n${JSON.stringify(form.getValues())}`,
57
+ const value: ModelConfig = {
58
+ system: `Form State:\n${JSON.stringify(form.getValues())}`,
32
59
 
33
- tools: {
34
- set_form_field: {
35
- ...formTools.set_form_field,
36
- execute: async (args) => {
37
- // biome-ignore lint/suspicious/noExplicitAny: TODO
38
- form.setValue(args.name as any, args.value as any);
60
+ tools: {
61
+ set_form_field: {
62
+ ...formTools.set_form_field,
63
+ execute: async (args) => {
64
+ // biome-ignore lint/suspicious/noExplicitAny: TODO
65
+ form.setValue(args.name as any, args.value as any);
39
66
 
40
- return { success: true };
41
- },
67
+ return { success: true };
42
68
  },
43
- submit_form: {
44
- ...formTools.submit_form,
45
- execute: async () => {
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;
69
+ },
70
+ submit_form: {
71
+ ...formTools.submit_form,
72
+ execute: async () => {
73
+ const { _names, _fields } = form.control;
74
+ for (const name of _names.mount) {
75
+ const field = _fields[name];
76
+ if (field?._f) {
77
+ const fieldReference = Array.isArray(field._f.refs)
78
+ ? field._f.refs[0]
79
+ : field._f.ref;
53
80
 
54
- if (fieldReference instanceof HTMLElement) {
55
- const form = fieldReference.closest("form");
56
- if (form) {
57
- form.requestSubmit();
81
+ if (fieldReference instanceof HTMLElement) {
82
+ const form = fieldReference.closest("form");
83
+ if (form) {
84
+ form.requestSubmit();
58
85
 
59
- return { success: true };
60
- }
86
+ return { success: true };
61
87
  }
62
88
  }
63
89
  }
90
+ }
64
91
 
65
- return {
66
- success: false,
67
- message:
68
- "Unable retrieve the form element. This is a coding error.",
69
- };
70
- },
92
+ return {
93
+ success: false,
94
+ message:
95
+ "Unable retrieve the form element. This is a coding error.",
96
+ };
71
97
  },
72
98
  },
73
- };
74
- });
75
- }, [form, registerModelConfigProvider]);
99
+ },
100
+ };
101
+ return registerModelConfigProvider(() => value);
102
+ }, [
103
+ form.control,
104
+ form.setValue,
105
+ form.getValues,
106
+ registerModelConfigProvider,
107
+ ]);
108
+
109
+ const renderFormFieldTool = props?.assistant?.tools?.set_form_field?.render;
110
+ useAssistantToolRenderer(
111
+ renderFormFieldTool
112
+ ? {
113
+ name: "set_form_field",
114
+ render: renderFormFieldTool,
115
+ }
116
+ : null,
117
+ );
118
+
119
+ const renderSubmitFormTool = props?.assistant?.tools?.submit_form?.render;
120
+ useAssistantToolRenderer(
121
+ renderSubmitFormTool
122
+ ? {
123
+ name: "submit_form",
124
+ render: renderSubmitFormTool,
125
+ }
126
+ : null,
127
+ );
76
128
 
77
129
  return form;
78
130
  };