| 209 | ): FieldTree<TModel>; |
| 210 | |
| 211 | export function form<TModel>(...args: any[]): FieldTree<TModel> { |
| 212 | const [model, schema, options] = normalizeFormArgs<TModel>(args); |
| 213 | const injector = options?.injector ?? inject(Injector); |
| 214 | const pathNode = runInInjectionContext(injector, () => SchemaImpl.rootCompile(schema)); |
| 215 | const fieldManager = new FormFieldManager( |
| 216 | injector, |
| 217 | options?.name, |
| 218 | options?.submission as FormSubmitOptions<unknown, unknown> | undefined, |
| 219 | ); |
| 220 | const adapter = options?.adapter ?? new BasicFieldAdapter(); |
| 221 | const fieldRoot = FieldNode.newRoot(fieldManager, model, pathNode, adapter); |
| 222 | fieldManager.createFieldManagementEffect(fieldRoot.structure); |
| 223 | |
| 224 | // Register a WebMCP tool for the form if configured. |
| 225 | const {experimentalWebMcpTool} = options ?? {}; |
| 226 | if (experimentalWebMcpTool) { |
| 227 | const registerWebMcpForm = runInInjectionContext(injector, () => |
| 228 | inject(REGISTER_WEBMCP_FORM, {optional: true}), |
| 229 | ); |
| 230 | if (registerWebMcpForm) { |
| 231 | runInInjectionContext(injector, () => |
| 232 | registerWebMcpForm(fieldRoot.fieldTree, { |
| 233 | name: experimentalWebMcpTool.name, |
| 234 | description: experimentalWebMcpTool.description, |
| 235 | }), |
| 236 | ); |
| 237 | } else { |
| 238 | if (typeof ngDevMode !== 'undefined' && ngDevMode) { |
| 239 | throw new Error( |
| 240 | `Cannot register form "${experimentalWebMcpTool.name}" as a WebMCP tool. ` + |
| 241 | `Make sure to use \`provideExperimentalWebMcpForms()\` in your application bootstrap configuration.`, |
| 242 | ); |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | return fieldRoot.fieldTree as FieldTree<TModel>; |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Applies a schema to each item of an array. |