feat: 实现Operation-Agent核心功能及电商平台适配器

refactor: 重构项目结构,分离server和dashboard代码
style: 统一代码风格,修复lint警告
test: 添加平台适配器工厂测试用例
ci: 更新CI/CD流程,增加语义验证和性能测试
docs: 添加语义中心文档,定义统一数据模型和状态机
This commit is contained in:
2026-03-19 15:23:56 +08:00
parent aa2cf560c6
commit 8de9ea0aaa
41 changed files with 5615 additions and 497 deletions

View File

@@ -0,0 +1,61 @@
import { Controller, Get, Put, Param, UseGuards } from '@nestjs/common';
import { AISelfImprovementService } from '../../core/ai/AISelfImprovementService';
import { RbacGuard } from '../../core/guards/rbac.guard';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
@ApiTags('AI Self-Improvement')
@ApiBearerAuth()
@Controller('api/ai/self-improvement')
export class AISelfImprovementController {
constructor(private aiSelfImprovementService: AISelfImprovementService) {}
/**
* 生成改进建议
*/
@Get('suggestions/generate')
@UseGuards(RbacGuard)
async generateSuggestions() {
const suggestions = await this.aiSelfImprovementService.generateImprovementSuggestions();
return { success: true, suggestions };
}
/**
* 获取所有改进建议
*/
@Get('suggestions')
@UseGuards(RbacGuard)
async getSuggestions() {
const suggestions = await this.aiSelfImprovementService.getImprovementSuggestions();
return { success: true, suggestions };
}
/**
* 更新建议状态
*/
@Put('suggestions/:id/status')
@UseGuards(RbacGuard)
async updateSuggestionStatus(@Param('id') id: string, @Param('status') status: 'implemented' | 'dismissed') {
const result = await this.aiSelfImprovementService.updateSuggestionStatus(id, status);
return { success: result };
}
/**
* 自动应用改进建议
*/
@Get('suggestions/apply')
@UseGuards(RbacGuard)
async applySuggestions() {
const result = await this.aiSelfImprovementService.applyImprovementSuggestions();
return { success: true, ...result };
}
/**
* 执行定期优化
*/
@Get('optimize')
@UseGuards(RbacGuard)
async performOptimization() {
await this.aiSelfImprovementService.performRegularOptimization();
return { success: true, message: '定期优化执行完成' };
}
}

View File

@@ -0,0 +1,30 @@
import { Controller, Get, UseGuards } from '@nestjs/common';
import { MonitoringService } from '../../core/monitoring/MonitoringService';
import { RbacGuard } from '../../core/guards/rbac.guard';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
@ApiTags('Monitoring')
@ApiBearerAuth()
@Controller('api/monitoring')
export class MonitoringController {
constructor(private monitoringService: MonitoringService) {}
/**
* 获取当前监控指标
*/
@Get('metrics')
@UseGuards(RbacGuard)
async getMetrics() {
return this.monitoringService.getMetrics();
}
/**
* 手动收集和记录指标
*/
@Get('collect')
@UseGuards(RbacGuard)
async collectMetrics() {
await this.monitoringService.collectAndRecordMetrics();
return { success: true, message: '指标收集成功' };
}
}

View File

@@ -0,0 +1,89 @@
import { Controller, Post, Get, Put, Delete, Param, Body, UseGuards } from '@nestjs/common';
import { OperationAgentService } from '../../core/operation/OperationAgentService';
import { StoreBindingDto } from '../dto/StoreBindingDto';
import { Store } from '../../entities/Store';
import { RbacGuard } from '../../core/guards/rbac.guard';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
@ApiTags('Operation-Agent')
@ApiBearerAuth()
@Controller('api/operation-agent')
export class OperationAgentController {
constructor(private operationAgentService: OperationAgentService) {}
/**
* 绑定店铺
*/
@Post('stores')
@UseGuards(RbacGuard)
async bindStore(@Body() dto: StoreBindingDto): Promise<Store> {
return this.operationAgentService.bindStore(dto);
}
/**
* 获取商户的店铺列表
*/
@Get('stores/:merchantId')
@UseGuards(RbacGuard)
async getStores(@Param('merchantId') merchantId: string): Promise<Store[]> {
return this.operationAgentService.getStores(merchantId);
}
/**
* 获取店铺详情
*/
@Get('stores/detail/:storeId')
@UseGuards(RbacGuard)
async getStore(@Param('storeId') storeId: string): Promise<Store> {
return this.operationAgentService.getStore(storeId);
}
/**
* 同步店铺商品
*/
@Post('stores/:storeId/products/sync')
@UseGuards(RbacGuard)
async syncProducts(@Param('storeId') storeId: string): Promise<{ success: boolean; count: number }> {
return this.operationAgentService.syncProducts(storeId);
}
/**
* 同步店铺订单
*/
@Post('stores/:storeId/orders/sync')
@UseGuards(RbacGuard)
async syncOrders(@Param('storeId') storeId: string): Promise<{ success: boolean; count: number }> {
return this.operationAgentService.syncOrders(storeId);
}
/**
* 更新商品价格
*/
@Put('stores/:storeId/products/:productId/price')
@UseGuards(RbacGuard)
async updateProductPrice(
@Param('storeId') storeId: string,
@Param('productId') productId: string,
@Body('price') price: number
): Promise<boolean> {
return this.operationAgentService.updateProductPrice(storeId, productId, price);
}
/**
* 停用店铺
*/
@Put('stores/:storeId/deactivate')
@UseGuards(RbacGuard)
async deactivateStore(@Param('storeId') storeId: string): Promise<Store> {
return this.operationAgentService.deactivateStore(storeId);
}
/**
* 重新激活店铺
*/
@Put('stores/:storeId/reactivate')
@UseGuards(RbacGuard)
async reactivateStore(@Param('storeId') storeId: string): Promise<Store> {
return this.operationAgentService.reactivateStore(storeId);
}
}

View File

@@ -0,0 +1,22 @@
import { IsString, IsOptional, IsObject } from 'class-validator';
export class StoreBindingDto {
@IsString()
merchantId: string;
@IsString()
platform: string;
@IsString()
platformShopId: string;
@IsString()
name: string;
@IsOptional()
@IsString()
description?: string;
@IsObject()
authInfo: Record<string, any>;
}

View File

@@ -0,0 +1,43 @@
import { Router } from 'express';
import { AISelfImprovementController } from '../controllers/AISelfImprovementController';
import { Container } from 'typedi';
const router = Router();
const aiSelfImprovementController = Container.get(AISelfImprovementController);
// 生成改进建议
router.get('/suggestions/generate', (req, res, next) => {
aiSelfImprovementController.generateSuggestions()
.then(result => res.json(result))
.catch(next);
});
// 获取所有改进建议
router.get('/suggestions', (req, res, next) => {
aiSelfImprovementController.getSuggestions()
.then(result => res.json(result))
.catch(next);
});
// 更新建议状态
router.put('/suggestions/:id/status', (req, res, next) => {
aiSelfImprovementController.updateSuggestionStatus(req.params.id, req.params.status)
.then(result => res.json(result))
.catch(next);
});
// 自动应用改进建议
router.get('/suggestions/apply', (req, res, next) => {
aiSelfImprovementController.applySuggestions()
.then(result => res.json(result))
.catch(next);
});
// 执行定期优化
router.get('/optimize', (req, res, next) => {
aiSelfImprovementController.performOptimization()
.then(result => res.json(result))
.catch(next);
});
export default router;

View File

@@ -0,0 +1,22 @@
import { Router } from 'express';
import { MonitoringController } from '../controllers/MonitoringController';
import { Container } from 'typedi';
const router = Router();
const monitoringController = Container.get(MonitoringController);
// 获取当前监控指标
router.get('/metrics', (req, res, next) => {
monitoringController.getMetrics()
.then(result => res.json(result))
.catch(next);
});
// 手动收集和记录指标
router.get('/collect', (req, res, next) => {
monitoringController.collectMetrics()
.then(result => res.json(result))
.catch(next);
});
export default router;

View File

@@ -0,0 +1,64 @@
import { Router } from 'express';
import { OperationAgentController } from '../controllers/OperationAgentController';
import { Container } from 'typedi';
const router = Router();
const operationAgentController = Container.get(OperationAgentController);
// 绑定店铺
router.post('/stores', (req, res, next) => {
operationAgentController.bindStore(req.body)
.then(result => res.json(result))
.catch(next);
});
// 获取商户的店铺列表
router.get('/stores/:merchantId', (req, res, next) => {
operationAgentController.getStores(req.params.merchantId)
.then(result => res.json(result))
.catch(next);
});
// 获取店铺详情
router.get('/stores/detail/:storeId', (req, res, next) => {
operationAgentController.getStore(req.params.storeId)
.then(result => res.json(result))
.catch(next);
});
// 同步店铺商品
router.post('/stores/:storeId/products/sync', (req, res, next) => {
operationAgentController.syncProducts(req.params.storeId)
.then(result => res.json(result))
.catch(next);
});
// 同步店铺订单
router.post('/stores/:storeId/orders/sync', (req, res, next) => {
operationAgentController.syncOrders(req.params.storeId)
.then(result => res.json(result))
.catch(next);
});
// 更新商品价格
router.put('/stores/:storeId/products/:productId/price', (req, res, next) => {
operationAgentController.updateProductPrice(req.params.storeId, req.params.productId, req.body.price)
.then(result => res.json(result))
.catch(next);
});
// 停用店铺
router.put('/stores/:storeId/deactivate', (req, res, next) => {
operationAgentController.deactivateStore(req.params.storeId)
.then(result => res.json(result))
.catch(next);
});
// 重新激活店铺
router.put('/stores/:storeId/reactivate', (req, res, next) => {
operationAgentController.reactivateStore(req.params.storeId)
.then(result => res.json(result))
.catch(next);
});
export default router;