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

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,19 +1,6 @@
1
- import { FieldValues, UseFormReturn, UseFormProps } from 'react-hook-form';
1
+ import { ToolCallContentPartComponent } from '@assistant-ui/react/experimental';
2
+ import { FieldValues, UseFormProps, UseFormReturn } from 'react-hook-form';
2
3
  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
4
 
18
5
  declare const formTools: {
19
6
  set_form_field: {
@@ -39,14 +26,14 @@ type UseAssistantFormProps<TFieldValues extends FieldValues, TContext> = UseForm
39
26
  assistant?: {
40
27
  tools?: {
41
28
  set_form_field?: {
42
- render?: ToolRenderComponent<z.ZodType<typeof formTools.set_form_field>, unknown>;
29
+ render?: ToolCallContentPartComponent<z.ZodType<typeof formTools.set_form_field>, unknown>;
43
30
  };
44
31
  submit_form?: {
45
- render?: ToolRenderComponent<z.ZodType<typeof formTools.submit_form>, unknown>;
32
+ render?: ToolCallContentPartComponent<z.ZodType<typeof formTools.submit_form>, unknown>;
46
33
  };
47
34
  };
48
35
  };
49
36
  };
50
37
  declare const useAssistantForm: <TFieldValues extends FieldValues = FieldValues, TContext = any, TTransformedValues extends FieldValues | undefined = undefined>(props?: UseAssistantFormProps<TFieldValues, TContext>) => UseFormReturn<TFieldValues, TContext, TTransformedValues>;
51
38
 
52
- export { formTools, useAssistantForm };
39
+ export { type UseAssistantFormProps, formTools, useAssistantForm };
package/dist/index.d.ts CHANGED
@@ -1,19 +1,6 @@
1
- import { FieldValues, UseFormReturn, UseFormProps } from 'react-hook-form';
1
+ import { ToolCallContentPartComponent } from '@assistant-ui/react/experimental';
2
+ import { FieldValues, UseFormProps, UseFormReturn } from 'react-hook-form';
2
3
  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
4
 
18
5
  declare const formTools: {
19
6
  set_form_field: {
@@ -39,14 +26,14 @@ type UseAssistantFormProps<TFieldValues extends FieldValues, TContext> = UseForm
39
26
  assistant?: {
40
27
  tools?: {
41
28
  set_form_field?: {
42
- render?: ToolRenderComponent<z.ZodType<typeof formTools.set_form_field>, unknown>;
29
+ render?: ToolCallContentPartComponent<z.ZodType<typeof formTools.set_form_field>, unknown>;
43
30
  };
44
31
  submit_form?: {
45
- render?: ToolRenderComponent<z.ZodType<typeof formTools.submit_form>, unknown>;
32
+ render?: ToolCallContentPartComponent<z.ZodType<typeof formTools.submit_form>, unknown>;
46
33
  };
47
34
  };
48
35
  };
49
36
  };
50
37
  declare const useAssistantForm: <TFieldValues extends FieldValues = FieldValues, TContext = any, TTransformedValues extends FieldValues | undefined = undefined>(props?: UseAssistantFormProps<TFieldValues, TContext>) => UseFormReturn<TFieldValues, TContext, TTransformedValues>;
51
38
 
52
- export { formTools, useAssistantForm };
39
+ export { type UseAssistantFormProps, formTools, useAssistantForm };
package/dist/index.js CHANGED
@@ -1,37 +1,59 @@
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
+
1
28
  // 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";
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");
10
32
 
11
33
  // src/formTools.tsx
12
- import { z } from "zod";
34
+ var import_zod = require("zod");
13
35
  var formTools = {
14
36
  set_form_field: {
15
37
  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()
38
+ parameters: import_zod.z.object({
39
+ name: import_zod.z.string(),
40
+ value: import_zod.z.string()
19
41
  })
20
42
  },
21
43
  submit_form: {
22
44
  description: "Submits the form. Confirm with user before submitting.",
23
- parameters: z.object({})
45
+ parameters: import_zod.z.object({})
24
46
  }
25
47
  };
26
48
 
27
49
  // src/useAssistantForm.tsx
28
50
  var useAssistantForm = (props) => {
29
- const form = useForm(props);
30
- const { useModelConfig } = useAssistantContext();
51
+ const form = (0, import_react_hook_form.useForm)(props);
52
+ const { useModelConfig } = (0, import_experimental.useAssistantContext)();
31
53
  const registerModelConfigProvider = useModelConfig(
32
54
  (c) => c.registerModelConfigProvider
33
55
  );
34
- useEffect(() => {
56
+ (0, import_react.useEffect)(() => {
35
57
  const value = {
36
58
  system: `Form State:
37
59
  ${JSON.stringify(form.getValues())}`,
@@ -76,14 +98,14 @@ ${JSON.stringify(form.getValues())}`,
76
98
  registerModelConfigProvider
77
99
  ]);
78
100
  const renderFormFieldTool = props?.assistant?.tools?.set_form_field?.render;
79
- useAssistantToolRenderer(
101
+ (0, import_experimental.useAssistantToolRenderer)(
80
102
  renderFormFieldTool ? {
81
103
  name: "set_form_field",
82
104
  render: renderFormFieldTool
83
105
  } : null
84
106
  );
85
107
  const renderSubmitFormTool = props?.assistant?.tools?.submit_form?.render;
86
- useAssistantToolRenderer(
108
+ (0, import_experimental.useAssistantToolRenderer)(
87
109
  renderSubmitFormTool ? {
88
110
  name: "submit_form",
89
111
  render: renderSubmitFormTool
@@ -91,8 +113,9 @@ ${JSON.stringify(form.getValues())}`,
91
113
  );
92
114
  return form;
93
115
  };
94
- export {
116
+ // Annotate the CommonJS export names for ESM import in node:
117
+ 0 && (module.exports = {
95
118
  formTools,
96
119
  useAssistantForm
97
- };
120
+ });
98
121
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +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"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/useAssistantForm.tsx","../src/formTools.tsx"],"sourcesContent":["export {\n useAssistantForm,\n type UseAssistantFormProps,\n} from \"./useAssistantForm\";\nexport { formTools } from \"./formTools\";\n","\"use client\";\n\nimport {\n type ModelConfig,\n type ToolCallContentPartComponent,\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 { formTools } from \"./formTools\";\n\nexport type UseAssistantFormProps<\n TFieldValues extends FieldValues,\n TContext,\n> = UseFormProps<TFieldValues, TContext> & {\n assistant?: {\n tools?: {\n set_form_field?: {\n render?: ToolCallContentPartComponent<\n z.ZodType<typeof formTools.set_form_field>,\n unknown\n >;\n };\n submit_form?: {\n render?: ToolCallContentPartComponent<\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,0BAKO;AACP,mBAA0B;AAC1B,6BAKO;;;ACdP,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"]}
@@ -1,59 +1,37 @@
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
1
  // 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");
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";
32
10
 
33
11
  // src/formTools.tsx
34
- var import_zod = require("zod");
12
+ import { z } from "zod";
35
13
  var formTools = {
36
14
  set_form_field: {
37
15
  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()
16
+ parameters: z.object({
17
+ name: z.string(),
18
+ value: z.string()
41
19
  })
42
20
  },
43
21
  submit_form: {
44
22
  description: "Submits the form. Confirm with user before submitting.",
45
- parameters: import_zod.z.object({})
23
+ parameters: z.object({})
46
24
  }
47
25
  };
48
26
 
49
27
  // src/useAssistantForm.tsx
50
28
  var useAssistantForm = (props) => {
51
- const form = (0, import_react_hook_form.useForm)(props);
52
- const { useModelConfig } = (0, import_experimental.useAssistantContext)();
29
+ const form = useForm(props);
30
+ const { useModelConfig } = useAssistantContext();
53
31
  const registerModelConfigProvider = useModelConfig(
54
32
  (c) => c.registerModelConfigProvider
55
33
  );
56
- (0, import_react.useEffect)(() => {
34
+ useEffect(() => {
57
35
  const value = {
58
36
  system: `Form State:
59
37
  ${JSON.stringify(form.getValues())}`,
@@ -98,14 +76,14 @@ ${JSON.stringify(form.getValues())}`,
98
76
  registerModelConfigProvider
99
77
  ]);
100
78
  const renderFormFieldTool = props?.assistant?.tools?.set_form_field?.render;
101
- (0, import_experimental.useAssistantToolRenderer)(
79
+ useAssistantToolRenderer(
102
80
  renderFormFieldTool ? {
103
81
  name: "set_form_field",
104
82
  render: renderFormFieldTool
105
83
  } : null
106
84
  );
107
85
  const renderSubmitFormTool = props?.assistant?.tools?.submit_form?.render;
108
- (0, import_experimental.useAssistantToolRenderer)(
86
+ useAssistantToolRenderer(
109
87
  renderSubmitFormTool ? {
110
88
  name: "submit_form",
111
89
  render: renderSubmitFormTool
@@ -113,9 +91,8 @@ ${JSON.stringify(form.getValues())}`,
113
91
  );
114
92
  return form;
115
93
  };
116
- // Annotate the CommonJS export names for ESM import in node:
117
- 0 && (module.exports = {
94
+ export {
118
95
  formTools,
119
96
  useAssistantForm
120
- });
121
- //# sourceMappingURL=index.cjs.map
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 type ToolCallContentPartComponent,\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 { formTools } from \"./formTools\";\n\nexport type UseAssistantFormProps<\n TFieldValues extends FieldValues,\n TContext,\n> = UseFormProps<TFieldValues, TContext> & {\n assistant?: {\n tools?: {\n set_form_field?: {\n render?: ToolCallContentPartComponent<\n z.ZodType<typeof formTools.set_form_field>,\n unknown\n >;\n };\n submit_form?: {\n render?: ToolCallContentPartComponent<\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,EAGE;AAAA,EACA;AAAA,OACK;AACP,SAAS,iBAAiB;AAC1B;AAAA,EAIE;AAAA,OACK;;;ACdP,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.4",
3
+ "version": "0.0.6",
4
4
  "license": "MIT",
5
- "type": "module",
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.5",
30
+ "@assistant-ui/react": "^0.1.6",
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.5",
42
+ "@assistant-ui/react": "0.1.6",
23
43
  "@assistant-ui/tsconfig": "0.0.0"
24
44
  },
25
45
  "publishConfig": {
@@ -1,20 +0,0 @@
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 DELETED
@@ -1,19 +0,0 @@
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
@@ -1 +0,0 @@
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/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
@@ -1,2 +0,0 @@
1
- export { useAssistantForm } from "./useAssistantForm";
2
- export { formTools } from "./formTools";
@@ -1,130 +0,0 @@
1
- "use client";
2
-
3
- import {
4
- type ModelConfig,
5
- useAssistantContext,
6
- useAssistantToolRenderer,
7
- } from "@assistant-ui/react/experimental";
8
- import { useEffect } from "react";
9
- import {
10
- type FieldValues,
11
- type UseFormProps,
12
- type UseFormReturn,
13
- useForm,
14
- } from "react-hook-form";
15
- import type { z } from "zod";
16
- import type { ToolRenderComponent } from "../../react/src/model-config/ToolRenderComponent";
17
- import { formTools } from "./formTools";
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
-
41
- export const useAssistantForm = <
42
- TFieldValues extends FieldValues = FieldValues,
43
- // biome-ignore lint/suspicious/noExplicitAny: <explanation>
44
- TContext = any,
45
- TTransformedValues extends FieldValues | undefined = undefined,
46
- >(
47
- props?: UseAssistantFormProps<TFieldValues, TContext>,
48
- ): UseFormReturn<TFieldValues, TContext, TTransformedValues> => {
49
- const form = useForm<TFieldValues, TContext, TTransformedValues>(props);
50
-
51
- const { useModelConfig } = useAssistantContext();
52
- const registerModelConfigProvider = useModelConfig(
53
- (c) => c.registerModelConfigProvider,
54
- );
55
-
56
- useEffect(() => {
57
- const value: ModelConfig = {
58
- system: `Form State:\n${JSON.stringify(form.getValues())}`,
59
-
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);
66
-
67
- return { success: true };
68
- },
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;
80
-
81
- if (fieldReference instanceof HTMLElement) {
82
- const form = fieldReference.closest("form");
83
- if (form) {
84
- form.requestSubmit();
85
-
86
- return { success: true };
87
- }
88
- }
89
- }
90
- }
91
-
92
- return {
93
- success: false,
94
- message:
95
- "Unable retrieve the form element. This is a coding error.",
96
- };
97
- },
98
- },
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
- );
128
-
129
- return form;
130
- };
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
- }