掌握 ai-home 前端的页面组件开发、MCP Tool 接入以及团队开发规范
ai-home 基于 React + TypeScript 构建,页面组件遵循统一的目录规范和开发模式。新增一个页面组件需要以下步骤:
根据页面功能将其归入对应的模块目录:
src/ ├── pages/ # 页面级组件 │ ├── Home/ # 首页模块 │ ├── Conversation/ # 对话模块 │ ├── Floorplan/ # 户型模块 │ └── Design/ # 设计模块 ├── components/ # 通用业务组件 └── common/ # 基础 UI 组件
每个页面组件包含以下标准文件:
pages/YourPage/ ├── index.tsx # 组件主文件 ├── index.module.scss # 样式文件(CSS Modules) ├── hooks.ts # 页面自定义 hooks └── types.ts # 页面类型定义
在路由配置文件中添加页面路由:
// src/router/routes.tsx import { lazy } from 'react'; const YourPage = lazy(() => import('@/pages/YourPage')); const routes = [ { path: '/your-page', element: <YourPage />, meta: { title: '页面标题', auth: true } }, // ...其他路由 ];
// src/pages/YourPage/index.tsx import { useEffect, useState } from 'react'; import styles from './index.module.scss'; import type { YourPageProps } from './types'; export default function YourPage({ }: YourPageProps) { const [data, setData] = useState(null); useEffect(() => { // 初始化逻辑 }, []); return ( <div className={styles.container}> {/* 页面内容 */} </div> ); }
组件使用 函数式组件 + Hooks,样式使用 CSS Modules,类型定义独立到 types.ts,避免组件文件过长。
ai-home 的前端 MCP Tool 是后端 Agent 通过 WebSocket 调用前端执行的工具。典型场景:3D 渲染、UI 交互操作等需要前端环境才能完成的任务。
在 Tool 注册文件中声明工具的名称、描述和参数:
// src/mcp/tools/myNewTool.ts import type { MCPToolDefinition } from '@/types/mcp'; export const myNewToolDef: MCPToolDefinition = { name: 'my_new_tool', description: '工具的描述,告诉 AI 什么时候该调用它', parameters: { type: 'object', properties: { targetId: { type: 'string', description: '目标对象 ID', }, options: { type: 'object', description: '操作选项', properties: { mode: { type: 'string', enum: ['fast', 'quality'] }, }, }, }, required: ['targetId'], }, };
// src/mcp/handlers/myNewToolHandler.ts import type { MCPToolHandler, MCPToolResult } from '@/types/mcp'; export const myNewToolHandler: MCPToolHandler = async (params) => { const { targetId, options } = params; try { // 业务逻辑:操作 3D 场景、修改 UI 等 const result = await doSomething(targetId, options); return { success: true, data: result, } as MCPToolResult; } catch (err) { return { success: false, error: String(err), } as MCPToolResult; } };
// src/mcp/registry.ts import { myNewToolDef } from './tools/myNewTool'; import { myNewToolHandler } from './handlers/myNewToolHandler'; toolRegistry.register({ definition: myNewToolDef, handler: myNewToolHandler, });
Handler 必须是 异步函数,返回值必须符合 MCPToolResult 类型。如果执行失败,success 字段设为 false 并附带 error 描述,后端 Agent 会据此决定下一步行为。
| 规范项 | 要求 | 说明 |
|---|---|---|
| 语言 | TypeScript Strict | 禁止 any,所有类型必须显式声明 |
| 组件风格 | 函数式 + Hooks | 不使用 Class Component |
| 样式方案 | CSS Modules | 文件名 *.module.scss,避免全局样式污染 |
| 状态管理 | Zustand / React Context | 全局状态用 Zustand,局部 Context 用 React 原生 |
| 请求封装 | 统一 HTTP Client | 使用 src/utils/request.ts,不直接用 fetch |
| 命名 | camelCase / PascalCase | 变量/函数 camelCase,组件/类型 PascalCase |
| 目录结构 | 按功能模块划分 | 每个模块一个目录,内部自包含组件/hooks/types |
# Commit Message 格式 feat(module): 新增 xxx 功能 fix(module): 修复 xxx 问题 refactor(module): 重构 xxx style(module): 调整 xxx 样式 docs: 更新文档
提交前必须通过 ESLint + Prettier 检查。运行 pnpm lint 确认无报错,pnpm typecheck 确认类型正确。CI 不通过的 PR 不会被合并。