@assistant-ui/react-hook-form 0.0.2 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- package/.turbo/turbo-build.log +20 -0
- package/CHANGELOG.md +19 -0
- package/dist/index.cjs +50 -32
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +30 -4
- package/dist/index.d.ts +30 -4
- package/dist/index.js +54 -33
- package/dist/index.js.map +1 -1
- package/package.json +13 -4
- package/src/useAssistantForm.tsx +90 -38
@@ -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
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# @assistant-ui/react-hook-form
|
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
|
+
|
11
|
+
## 0.0.3
|
12
|
+
|
13
|
+
### Patch Changes
|
14
|
+
|
15
|
+
- 6e9528d: build: add changesets
|
16
|
+
- 6e9528d: feat: add useAssistantTool API
|
17
|
+
- Updated dependencies [6e9528d]
|
18
|
+
- Updated dependencies [6e9528d]
|
19
|
+
- @assistant-ui/react@0.1.3
|
package/dist/index.cjs
CHANGED
@@ -54,45 +54,63 @@ var useAssistantForm = (props) => {
|
|
54
54
|
(c) => c.registerModelConfigProvider
|
55
55
|
);
|
56
56
|
(0, import_react.useEffect)(() => {
|
57
|
-
|
58
|
-
|
59
|
-
system: `Form State:
|
57
|
+
const value = {
|
58
|
+
system: `Form State:
|
60
59
|
${JSON.stringify(form.getValues())}`,
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
}
|
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 };
|
83
81
|
}
|
84
82
|
}
|
85
83
|
}
|
86
|
-
return {
|
87
|
-
success: false,
|
88
|
-
message: "Unable retrieve the form element. This is a coding error."
|
89
|
-
};
|
90
84
|
}
|
85
|
+
return {
|
86
|
+
success: false,
|
87
|
+
message: "Unable retrieve the form element. This is a coding error."
|
88
|
+
};
|
91
89
|
}
|
92
90
|
}
|
93
|
-
}
|
94
|
-
}
|
95
|
-
|
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
|
+
);
|
96
114
|
return form;
|
97
115
|
};
|
98
116
|
// Annotate the CommonJS export names for ESM import in node:
|
package/dist/index.cjs.map
CHANGED
@@ -1 +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
|
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"]}
|
package/dist/index.d.cts
CHANGED
@@ -1,7 +1,19 @@
|
|
1
|
-
import { FieldValues,
|
1
|
+
import { FieldValues, UseFormReturn, UseFormProps } from 'react-hook-form';
|
2
2
|
import { z } from 'zod';
|
3
|
+
import { ComponentType } from 'react';
|
3
4
|
|
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
|
+
}>;
|
5
17
|
|
6
18
|
declare const formTools: {
|
7
19
|
set_form_field: {
|
@@ -10,11 +22,11 @@ declare const formTools: {
|
|
10
22
|
name: z.ZodString;
|
11
23
|
value: z.ZodString;
|
12
24
|
}, "strip", z.ZodTypeAny, {
|
13
|
-
value: string;
|
14
25
|
name: string;
|
15
|
-
}, {
|
16
26
|
value: string;
|
27
|
+
}, {
|
17
28
|
name: string;
|
29
|
+
value: string;
|
18
30
|
}>;
|
19
31
|
};
|
20
32
|
submit_form: {
|
@@ -23,4 +35,18 @@ declare const formTools: {
|
|
23
35
|
};
|
24
36
|
};
|
25
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
|
+
|
26
52
|
export { formTools, useAssistantForm };
|
package/dist/index.d.ts
CHANGED
@@ -1,7 +1,19 @@
|
|
1
|
-
import { FieldValues,
|
1
|
+
import { FieldValues, UseFormReturn, UseFormProps } from 'react-hook-form';
|
2
2
|
import { z } from 'zod';
|
3
|
+
import { ComponentType } from 'react';
|
3
4
|
|
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
|
+
}>;
|
5
17
|
|
6
18
|
declare const formTools: {
|
7
19
|
set_form_field: {
|
@@ -10,11 +22,11 @@ declare const formTools: {
|
|
10
22
|
name: z.ZodString;
|
11
23
|
value: z.ZodString;
|
12
24
|
}, "strip", z.ZodTypeAny, {
|
13
|
-
value: string;
|
14
25
|
name: string;
|
15
|
-
}, {
|
16
26
|
value: string;
|
27
|
+
}, {
|
17
28
|
name: string;
|
29
|
+
value: string;
|
18
30
|
}>;
|
19
31
|
};
|
20
32
|
submit_form: {
|
@@ -23,4 +35,18 @@ declare const formTools: {
|
|
23
35
|
};
|
24
36
|
};
|
25
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
|
+
|
26
52
|
export { formTools, useAssistantForm };
|
package/dist/index.js
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
// src/useAssistantForm.tsx
|
2
|
-
import {
|
2
|
+
import {
|
3
|
+
useAssistantContext,
|
4
|
+
useAssistantToolRenderer
|
5
|
+
} from "@assistant-ui/react/experimental";
|
3
6
|
import { useEffect } from "react";
|
4
7
|
import {
|
5
8
|
useForm
|
@@ -29,45 +32,63 @@ var useAssistantForm = (props) => {
|
|
29
32
|
(c) => c.registerModelConfigProvider
|
30
33
|
);
|
31
34
|
useEffect(() => {
|
32
|
-
|
33
|
-
|
34
|
-
system: `Form State:
|
35
|
+
const value = {
|
36
|
+
system: `Form State:
|
35
37
|
${JSON.stringify(form.getValues())}`,
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
}
|
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 };
|
58
59
|
}
|
59
60
|
}
|
60
61
|
}
|
61
|
-
return {
|
62
|
-
success: false,
|
63
|
-
message: "Unable retrieve the form element. This is a coding error."
|
64
|
-
};
|
65
62
|
}
|
63
|
+
return {
|
64
|
+
success: false,
|
65
|
+
message: "Unable retrieve the form element. This is a coding error."
|
66
|
+
};
|
66
67
|
}
|
67
68
|
}
|
68
|
-
}
|
69
|
-
}
|
70
|
-
|
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
|
+
);
|
71
92
|
return form;
|
72
93
|
};
|
73
94
|
export {
|
package/dist/index.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../src/useAssistantForm.tsx","../src/formTools.tsx"],"sourcesContent":["\"use client\";\n\nimport { useAssistantContext
|
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
|
+
"version": "0.0.4",
|
4
4
|
"license": "MIT",
|
5
5
|
"type": "module",
|
6
6
|
"peerDependencies": {
|
7
|
-
"@assistant-ui/react": "^0.1",
|
7
|
+
"@assistant-ui/react": "^0.1.5",
|
8
8
|
"react-hook-form": "^7.x.x",
|
9
9
|
"@types/react": "*",
|
10
10
|
"react": "^18"
|
@@ -19,11 +19,20 @@
|
|
19
19
|
},
|
20
20
|
"devDependencies": {
|
21
21
|
"tsup": "^8.1.0",
|
22
|
-
"@assistant-ui/react": "0.1.
|
22
|
+
"@assistant-ui/react": "0.1.5",
|
23
23
|
"@assistant-ui/tsconfig": "0.0.0"
|
24
24
|
},
|
25
25
|
"publishConfig": {
|
26
|
-
"access": "public"
|
26
|
+
"access": "public",
|
27
|
+
"provenance": true
|
28
|
+
},
|
29
|
+
"homepage": "https://assistant-ui.com/",
|
30
|
+
"repository": {
|
31
|
+
"type": "git",
|
32
|
+
"url": "git+https://github.com/Yonom/assistant-ui.git"
|
33
|
+
},
|
34
|
+
"bugs": {
|
35
|
+
"url": "https://github.com/Yonom/assistant-ui/issues"
|
27
36
|
},
|
28
37
|
"scripts": {
|
29
38
|
"build": "tsup src/index.ts --format cjs,esm --dts --sourcemap"
|
package/src/useAssistantForm.tsx
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
"use client";
|
2
2
|
|
3
|
-
import {
|
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?:
|
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
|
-
|
30
|
-
|
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
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
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
|
-
|
41
|
-
},
|
67
|
+
return { success: true };
|
42
68
|
},
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
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
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
81
|
+
if (fieldReference instanceof HTMLElement) {
|
82
|
+
const form = fieldReference.closest("form");
|
83
|
+
if (form) {
|
84
|
+
form.requestSubmit();
|
58
85
|
|
59
|
-
|
60
|
-
}
|
86
|
+
return { success: true };
|
61
87
|
}
|
62
88
|
}
|
63
89
|
}
|
90
|
+
}
|
64
91
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
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
|
-
|
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
|
};
|