@assistant-ui/react-hook-form 0.0.3 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/index.d.mts +52 -0
- package/dist/index.d.ts +52 -0
- package/dist/index.js +121 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +98 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +27 -7
- package/CHANGELOG.md +0 -11
- package/src/formTools.tsx +0 -16
- package/src/index.ts +0 -2
- package/src/useAssistantForm.tsx +0 -78
- package/tsconfig.json +0 -11
package/dist/index.d.mts
ADDED
@@ -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.d.ts
ADDED
@@ -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,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.js.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"]}
|
package/dist/index.mjs
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.mjs.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,33 @@
|
|
1
1
|
{
|
2
2
|
"name": "@assistant-ui/react-hook-form",
|
3
|
-
"version": "0.0.
|
3
|
+
"version": "0.0.5",
|
4
4
|
"license": "MIT",
|
5
|
-
"
|
5
|
+
"exports": {
|
6
|
+
".": {
|
7
|
+
"import": {
|
8
|
+
"types": "./dist/index.d.mts",
|
9
|
+
"default": "./dist/index.mjs"
|
10
|
+
},
|
11
|
+
"require": {
|
12
|
+
"types": "./dist/index.d.ts",
|
13
|
+
"default": "./dist/index.js"
|
14
|
+
}
|
15
|
+
}
|
16
|
+
},
|
17
|
+
"source": "./src/index.ts",
|
18
|
+
"main": "./dist/index.js",
|
19
|
+
"module": "./dist/index.mjs",
|
20
|
+
"types": "./dist/index.d.ts",
|
21
|
+
"files": [
|
22
|
+
"dist",
|
23
|
+
"README.md"
|
24
|
+
],
|
25
|
+
"sideEffects": false,
|
26
|
+
"dependencies": {
|
27
|
+
"zod": "^3.23.8"
|
28
|
+
},
|
6
29
|
"peerDependencies": {
|
7
|
-
"@assistant-ui/react": "^0.1.
|
30
|
+
"@assistant-ui/react": "^0.1.5",
|
8
31
|
"react-hook-form": "^7.x.x",
|
9
32
|
"@types/react": "*",
|
10
33
|
"react": "^18"
|
@@ -14,12 +37,9 @@
|
|
14
37
|
"optional": true
|
15
38
|
}
|
16
39
|
},
|
17
|
-
"dependencies": {
|
18
|
-
"zod": "^3.23.8"
|
19
|
-
},
|
20
40
|
"devDependencies": {
|
21
41
|
"tsup": "^8.1.0",
|
22
|
-
"@assistant-ui/react": "0.1.
|
42
|
+
"@assistant-ui/react": "0.1.5",
|
23
43
|
"@assistant-ui/tsconfig": "0.0.0"
|
24
44
|
},
|
25
45
|
"publishConfig": {
|
package/CHANGELOG.md
DELETED
package/src/formTools.tsx
DELETED
@@ -1,16 +0,0 @@
|
|
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
DELETED
package/src/useAssistantForm.tsx
DELETED
@@ -1,78 +0,0 @@
|
|
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: async (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: 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;
|
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
DELETED
@@ -1,11 +0,0 @@
|
|
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
|
-
}
|