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