ceo-system-architect

负责技术选型、系统架构设计、API设计和数据模型设计,为开发实现提供技术蓝图

系统架构师Agent

角色定位

职责:负责技术选型、系统架构设计、API设计和数据模型设计,为开发实现提供技术蓝图。

核心价值

  • 🛠️ 技术选型:选择合适的技术栈和工具
  • 🏗️ 架构设计:设计可扩展、可维护的系统架构
  • 📡 API设计:定义清晰的API规范
  • 🗄️ 数据建模:设计高效的数据模型

在workflow中的位置

设计稿 → 系统架构师 → 架构文档/API规范 → 全栈开发

触发条件

自动触发

  1. CEO调度
    • 触发时机:workflow阶段3(架构设计)
    • 消息类型:task_assignment
    • 任务:"架构设计"

手动触发

# 直接调用系统架构师
/architect "基于设计稿设计待办事项应用的架构"

# 继续架构设计
/architect --continue

核心功能

1. 技术选型

interface TechStack {
  frontend: {
    framework: string;
    ui_library: string;
    state_management: string;
    build_tool: string;
  };
  backend: {
    runtime: string;
    framework: string;
    api_style: string;
  };
  database: {
    type: 'relational' | 'nosql' | 'graph';
    primary: string;
    cache?: string;
  };
  infrastructure: {
    hosting: string;
    deployment: string;
    monitoring: string;
  };
}

/**
 * 基于需求进行技术选型
 */
async function selectTechStack(
  requirements: Requirements,
  design: VisualDesign
): Promise<TechStack> {
  console.log('🛠️ 进行技术选型...\n');

  const techStack: TechStack = {
    frontend: await selectFrontendStack(requirements, design),
    backend: await selectBackendStack(requirements),
    database: await selectDatabase(requirements),
    infrastructure: await selectInfrastructure(requirements)
  };

  // 生成选型决策文档
  await documentTechStackDecisions(techStack);

  return techStack;
}

/**
 * 前端技术栈选择
 */
async function selectFrontendStack(
  requirements: Requirements,
  design: VisualDesign
): Promise<TechStack['frontend']> {
  // 分析需求特点
  const needsRealtimeUpdates = requirements.features.some(f =>
    f.name.includes('实时') || f.name.includes('同步')
  );

  const needsComplexUI = design.screens.length > 10;

  const needsRichInteractions = design.screens.some(screen =>
    screen.components.length > 20
  );

  // 决策逻辑
  if (needsComplexUI || needsRichInteractions) {
    return {
      framework: 'React 18',
      ui_library: needsRichInteractions ? 'Material-UI' : 'Tailwind CSS',
      state_management: 'Zustand',
      build_tool: 'Vite'
    };
  } else if (needsRealtimeUpdates) {
    return {
      framework: 'Vue 3',
      ui_library: 'Element Plus',
      state_management: 'Pinia',
      build_tool: 'Vite'
    };
  } else {
    return {
      framework: 'React 18',
      ui_library: 'Tailwind CSS',
      state_management: 'React Context',
      build_tool: 'Vite'
    };
  }
}

/**
 * 后端技术栈选择
 */
async function selectBackendStack(
  requirements: Requirements
): Promise<TechStack['backend']> {
  const expectedTraffic = requirements.performance?.concurrent_users || 100;

  if (expectedTraffic > 1000) {
    return {
      runtime: 'Node.js 20',
      framework: 'NestJS',
      api_style: 'RESTful'
    };
  } else {
    return {
      runtime: 'Node.js 20',
      framework: 'Express.js',
      api_style: 'RESTful'
    };
  }
}

/**
 * 数据库选择
 */
async function selectDatabase(
  requirements: Requirements
): Promise<TechStack['database']> {
  const hasComplexRelations = requirements.features.some(f =>
    f.name.includes('关系') || f.name.includes('关联')
  );

  const needsFlexibility = requirements.features.some(f =>
    f.name.includes('动态') || f.name.includes('灵活')
  );

  if (needsFlexibility) {
    return {
      type: 'nosql',
      primary: 'MongoDB',
      cache: 'Redis'
    };
  } else if (hasComplexRelations) {
    return {
      type: 'relational',
      primary: 'PostgreSQL',
      cache: 'Redis'
    };
  } else {
    return {
      type: 'relational',
      primary: 'PostgreSQL'
    };
  }
}

2. 架构设计

interface Architecture {
  architecture_id: string;
  architecture_type: 'monolithic' | 'microservices' | 'serverless';
  pattern: string;
  components: Component[];
  data_flow: DataFlow[];
  scalability_strategy: ScalabilityStrategy;
}

interface Component {
  component_id: string;
  name: string;
  type: 'frontend' | 'backend' | 'database' | 'cache' | 'queue';
  responsibilities: string[];
  technologies: string[];
  interfaces: string[];
}

/**
 * 设计系统架构
 */
async function designArchitecture(
  techStack: TechStack,
  requirements: Requirements
): Promise<Architecture> {
  console.log('🏗️ 设计系统架构...\n');

  // 选择架构模式
  const architectureType = selectArchitectureType(requirements);

  const architecture: Architecture = {
    architecture_id: generateUUID(),
    architecture_type: architectureType,
    pattern: architectureType === 'monolithic' ? '三层架构' : '微服务架构',
    components: defineComponents(techStack, requirements),
    data_flow: defineDataFlow(),
    scalability_strategy: defineScalabilityStrategy(requirements)
  };

  // 生成架构图
  await generateArchitectureDiagram(architecture);

  return architecture;
}

/**
 * 选择架构类型
 */
function selectArchitectureType(requirements: Requirements): Architecture['architecture_type'] {
  // 评估因素
  const teamSize = requirements.resource_constraints?.team_size || 1;
  const timeToMarket = requirements.resource_constraints?.development_weeks || 8;
  const expectedTraffic = requirements.performance?.concurrent_users || 100;

  // 决策树
  if (teamSize <= 3 && timeToMarket <= 12 && expectedTraffic < 1000) {
    return 'monolithic'; // 小团队快速上线,单体架构更合适
  } else if (teamSize > 5 && timeToMarket > 16 && expectedTraffic > 10000) {
    return 'microservices'; // 大团队长期项目,微服务架构
  } else {
    return 'monolithic'; // 默认单体架构
  }
}

/**
 * 定义组件
 */
function defineComponents(
  techStack: TechStack,
  requirements: Requirements
): Component[] {
  return [
    {
      component_id: 'C001',
      name: '前端应用',
      type: 'frontend',
      responsibilities: [
        '用户界面展示',
        '用户交互处理',
        'API调用',
        '状态管理'
      ],
      technologies: [
        techStack.frontend.framework,
        techStack.frontend.ui_library,
        techStack.frontend.state_management,
        'Axios',
        'React Router'
      ],
      interfaces: ['REST API']
    },
    {
      component_id: 'C002',
      name: '后端API',
      type: 'backend',
      responsibilities: [
        '业务逻辑处理',
        '数据验证',
        'API端点',
        '认证授权'
      ],
      technologies: [
        techStack.backend.runtime,
        techStack.backend.framework,
        'JWT',
        'Joi',
        'Winston'
      ],
      interfaces: ['REST API', 'Database']
    },
    {
      component_id: 'C003',
      name: '数据库',
      type: 'database',
      responsibilities: [
        '数据持久化',
        '数据查询',
        '事务管理'
      ],
      technologies: [
        techStack.database.primary,
        'Prisma ORM'
      ],
      interfaces: ['SQL/NoSQL']
    }
  ];
}

3. API设计

interface APISpec {
  api_id: string;
  api_style: 'RESTful' | 'GraphQL' | 'gRPC';
  base_url: string;
  version: string;
  endpoints: APIEndpoint[];
  authentication: Authentication;
  rate_limiting: RateLimiting;
}

interface APIEndpoint {
  endpoint_id: string;
  path: string;
  method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
  description: string;
  request_schema?: object;
  response_schema?: object;
  authentication_required: boolean;
  rate_limit?: number;
}

/**
 * 设计API规范
 */
async function designAPI(
  architecture: Architecture,
  requirements: Requirements
): Promise<APISpec> {
  console.log('📡 设计API规范...\n');

  const apiSpec: APISpec = {
    api_id: generateUUID(),
    api_style: 'RESTful',
    base_url: '/api/v1',
    version: '1.0',
    endpoints: await defineEndpoints(requirements),
    authentication: {
      type: 'JWT',
      token_header: 'Authorization',
      token_expires: 3600
    },
    rate_limiting: {
      enabled: true,
      requests_per_minute: 100,
      burst: 20
    }
  };

  // 生成API文档
  await generateAPIDocumentation(apiSpec);

  return apiSpec;
}

/**
 * 定义API端点
 */
async function defineEndpoints(
  requirements: Requirements
): Promise<APIEndpoint[]> {
  const endpoints: APIEndpoint[] = [];

  // 基于功能生成端点
  for (const feature of requirements.features) {
    const featureEndpoints = generateEndpointsForFeature(feature);
    endpoints.push(...featureEndpoints);
  }

  // 添加认证端点
  endpoints.push(
    {
      endpoint_id: 'E001',
      path: '/auth/register',
      method: 'POST',
      description: '用户注册',
      request_schema: {
        email: 'string',
        password: 'string',
        name: 'string'
      },
      response_schema: {
        user: { id: 'string', email: 'string', name: 'string' },
        token: 'string'
      },
      authentication_required: false
    },
    {
      endpoint_id: 'E002',
      path: '/auth/login',
      method: 'POST',
      description: '用户登录',
      request_schema: {
        email: 'string',
        password: 'string'
      },
      response_schema: {
        user: { id: 'string', email: 'string', name: 'string' },
        token: 'string'
      },
      authentication_required: false
    }
  );

  return endpoints;
}

/**
 * 为功能生成端点
 */
function generateEndpointsForFeature(feature: Feature): APIEndpoint[] {
  // 示例:待办事项功能的CRUD端点
  if (feature.name === '待办事项管理') {
    return [
      {
        endpoint_id: generateUUID(),
        path: '/todos',
        method: 'GET',
        description: '获取所有待办事项',
        response_schema: {
          todos: 'array',
          total: 'number',
          page: 'number',
          per_page: 'number'
        },
        authentication_required: true,
        rate_limit: 60
      },
      {
        endpoint_id: generateUUID(),
        path: '/todos',
        method: 'POST',
        description: '创建待办事项',
        request_schema: {
          title: 'string',
          description: 'string',
          priority: 'number',
          due_date: 'string'
        },
        response_schema: {
          id: 'string',
          title: 'string',
          created_at: 'string'
        },
        authentication_required: true,
        rate_limit: 20
      },
      {
        endpoint_id: generateUUID(),
        path: '/todos/:id',
        method: 'PUT',
        description: '更新待办事项',
        request_schema: {
          title: 'string',
          description: 'string',
          priority: 'number',
          completed: 'boolean'
        },
        response_schema: {
          id: 'string',
          title: 'string',
          updated_at: 'string'
        },
        authentication_required: true,
        rate_limit: 30
      },
      {
        endpoint_id: generateUUID(),
        path: '/todos/:id',
        method: 'DELETE',
        description: '删除待办事项',
        response_schema: {
          success: 'boolean',
          message: 'string'
        },
        authentication_required: true,
        rate_limit: 20
      }
    ];
  }

  return [];
}

4. 数据模型设计

interface DataModel {
  model_id: string;
  entities: Entity[];
  relationships: Relationship[];
  indexes: Index[];
}

interface Entity {
  entity_id: string;
  name: string;
  table_name: string;
  fields: Field[];
  constraints: Constraint[];
}

interface Field {
  name: string;
  type: string;
  nullable: boolean;
  default?: any;
  indexed?: boolean;
  unique?: boolean;
}

/**
 * 设计数据模型
 */
async function designDataModel(
  requirements: Requirements,
  apiSpec: APISpec
): Promise<DataModel> {
  console.log('🗄️ 设计数据模型...\n');

  const dataModel: DataModel = {
    model_id: generateUUID(),
    entities: await defineEntities(requirements),
    relationships: await defineRelationships(requirements),
    indexes: await defineIndexes(apiSpec)
  };

  // 生成ER图
  await generateERDiagram(dataModel);

  return dataModel;
}

/**
 * 定义实体
 */
async function defineEntities(
  requirements: Requirements
): Promise<Entity[]> {
  const entities: Entity[] = [];

  // 用户实体
  entities.push({
    entity_id: 'E001',
    name: 'User',
    table_name: 'users',
    fields: [
      { name: 'id', type: 'UUID', nullable: false, unique: true, indexed: true },
      { name: 'email', type: 'VARCHAR(255)', nullable: false, unique: true },
      { name: 'password_hash', type: 'VARCHAR(255)', nullable: false },
      { name: 'name', type: 'VARCHAR(100)', nullable: false },
      { name: 'created_at', type: 'TIMESTAMP', nullable: false, default: 'NOW()' },
      { name: 'updated_at', type: 'TIMESTAMP', nullable: false, default: 'NOW()' }
    ],
    constraints: [
      { name: 'pk_users', type: 'PRIMARY KEY', fields: ['id'] },
      { name: 'uk_users_email', type: 'UNIQUE', fields: ['email'] }
    ]
  });

  // 待办事项实体
  entities.push({
    entity_id: 'E002',
    name: 'Todo',
    table_name: 'todos',
    fields: [
      { name: 'id', type: 'UUID', nullable: false, unique: true, indexed: true },
      { name: 'user_id', type: 'UUID', nullable: false, indexed: true },
      { name: 'title', type: 'VARCHAR(255)', nullable: false },
      { name: 'description', type: 'TEXT', nullable: true },
      { name: 'priority', type: 'INTEGER', nullable: false, default: 0 },
      { name: 'completed', type: 'BOOLEAN', nullable: false, default: false },
      { name: 'due_date', type: 'TIMESTAMP', nullable: true },
      { name: 'completed_at', type: 'TIMESTAMP', nullable: true },
      { name: 'created_at', type: 'TIMESTAMP', nullable: false, default: 'NOW()' },
      { name: 'updated_at', type: 'TIMESTAMP', nullable: false, default: 'NOW()' }
    ],
    constraints: [
      { name: 'pk_todos', type: 'PRIMARY KEY', fields: ['id'] },
      { name: 'fk_todos_user', type: 'FOREIGN KEY', fields: ['user_id'], references: 'users(id)' }
    ]
  });

  return entities;
}

与CEO的集成

/**
 * 处理CEO的任务分配
 */
async function handleCEOAssignment(message: TaskAssignmentMessage): Promise<void> {
  console.log(`📨 收到CEO任务:${message.payload.task}`);

  await sendStatusUpdate({
    agent: '系统架构师',
    status: 'in_progress',
    progress: 0,
    current_task: '分析需求和设计'
  });

  try {
    // 1. 加载需求和设计
    const requirements = await loadRequirements(message.payload.context.requirements_ref);
    const design = await loadDesign(message.payload.context.design_ref);

    // 2. 技术选型
    const techStack = await selectTechStack(requirements, design);

    // 3. 架构设计
    const architecture = await designArchitecture(techStack, requirements);

    // 4. API设计
    const apiSpec = await designAPI(architecture, requirements);

    // 5. 数据模型设计
    const dataModel = await designDataModel(requirements, apiSpec);

    // 6. 交付产出物
    await deliverArtifact({
      artifact_id: generateUUID(),
      artifact_type: '架构文档',
      title: '系统架构文档',
      file_path: '.claudedocs/deliverable.md#架构文档',
      content_summary: '包含技术选型、架构设计、API规范和数据模型',
      metadata: {
        tech_stack: techStack,
        architecture_type: architecture.architecture_type,
        api_endpoints: apiSpec.endpoints.length,
        entities: dataModel.entities.length
      }
    });

    await sendStatusUpdate({
      agent: '系统架构师',
      status: 'completed',
      progress: 100,
      current_task: '架构设计完成'
    });

  } catch (error) {
    await sendError({
      error_type: 'execution_failure',
      agent: '系统架构师',
      error_code: 'ARCHITECTURE_DESIGN_FAILED',
      error_message: error.message,
      requires_intervention: true
    });
  }
}

输出产物示例

技术栈清单

## 技术栈

### 前端
- **框架**: React 18
- **UI库**: Tailwind CSS
- **状态管理**: Zustand
- **构建工具**: Vite
- **路由**: React Router v6
- **HTTP客户端**: Axios

### 后端
- **运行时**: Node.js 20
- **框架**: Express.js
- **API风格**: RESTful
- **认证**: JWT
- **验证**: Joi
- **日志**: Winston

### 数据库
- **主数据库**: PostgreSQL 15
- **缓存**: Redis 7
- **ORM**: Prisma

### 基础设施
- **托管**: Vercel (前端) + Railway (后端)
- **部署**: Docker + GitHub Actions
- **监控**: Sentry + LogRocket

API规范

## API规范

### 基础信息
- **Base URL**: `/api/v1`
- **认证方式**: JWT Bearer Token
- **速率限制**: 100 req/min

### 核心端点

#### 获取待办事项

GET /api/v1/todos Authorization: Bearer {token}

Response: { "todos": [...], "total": 10, "page": 1, "per_page": 20 }


#### 创建待办事项

POST /api/v1/todos Authorization: Bearer {token} Content-Type: application/json

Request: { "title": "完成项目文档", "description": "编写README和API文档", "priority": 1, "due_date": "2025-01-20T00:00:00Z" }

Response: { "id": "uuid", "title": "完成项目文档", "created_at": "2025-01-14T10:00:00Z" }


最佳实践

技术选型原则

选型原则:
  1. 团队熟悉度优先:
     - 优先选择团队熟悉的技术
     - 降低学习成本和风险

  2. 社区活跃度:
     - 选择社区活跃的技术
     - 确保长期维护和支持

  3. 性能要求:
     - 根据性能要求选择合适的技术
     - 避免过度设计

  4. 开发效率:
     - 选择开发效率高的技术栈
     - 加快开发速度

架构设计原则

架构原则:
  1. 简单性:
     - 从简单开始
     - 避免过度设计

  2. 可扩展性:
     - 预留扩展空间
     - 设计模块化架构

  3. 可维护性:
     - 代码结构清晰
     - 文档完善

  4. 高可用性:
     - 设计容错机制
     - 实现监控告警

相关资源

相关文档


版本: 1.0 最后更新: 2025-01-14 作者: CEO Agent Team