242 lines
8.6 KiB
Markdown
242 lines
8.6 KiB
Markdown
# AI模板管理系统架构总结 - 混合式设计
|
||
|
||
## 🎯 核心设计理念
|
||
|
||
基于您的优雅抽象类设计,实现**代码定义逻辑 + 数据库存储配置**的混合式架构:
|
||
|
||
```typescript
|
||
// 抽象层次 (代码中定义)
|
||
Template (公共属性抽象类)
|
||
├── ImageGenerateTemplate (图片生成抽象)
|
||
├── VideoGenerateTemplate (视频生成抽象)
|
||
├── N8nImageGenerateTemplate (N8n图片实现)
|
||
└── N8nVideoGenerateTemplate (N8n视频实现)
|
||
|
||
// 具体配置 (数据库中存储)
|
||
CharacterFigurineTemplate → n8n_templates表中的配置记录
|
||
PhotoRestoreTemplate → n8n_templates表中的配置记录
|
||
|
||
// 动态实例 (Entity层)
|
||
new N8nVideoGenerateTemplateEntity(id).onInit().then(e=>e.execute(imgUrl))
|
||
```
|
||
|
||
## 🏗️ 混合式架构设计
|
||
|
||
### 1. 现有代码抽象层次 (保持不变)
|
||
```
|
||
Template (基础抽象类) [src/templates/types.ts:4-13]
|
||
├── ImageGenerateTemplate (图片生成抽象) [src/templates/types.ts:18-20]
|
||
│ └── N8nImageGenerateTemplate (N8n图片实现) [src/templates/n8nTemplate.ts:4-32]
|
||
│ ├── PhotoRestoreTemplate (具体配置存DB)
|
||
│ ├── PetFigurineTemplate (具体配置存DB)
|
||
│ └── CosplayRealPersonTemplate (具体配置存DB)
|
||
└── VideoGenerateTemplate (视频生成抽象) [src/templates/types.ts:23-25]
|
||
└── N8nVideoGenerateTemplate (N8n视频实现) [src/templates/n8nTemplate.ts:35-74]
|
||
├── CharacterFigurineTemplate (具体配置存DB)
|
||
├── GarageOpeningTemplate (具体配置存DB)
|
||
└── NuclearExplosionTemplate (具体配置存DB)
|
||
```
|
||
|
||
### 2. 架构组件分层
|
||
#### 代码层 (现有优势保持)
|
||
- **Template抽象类**: 公共属性定义 [types.ts:4-13]
|
||
- **N8nTemplate系列**: 执行逻辑实现 [n8nTemplate.ts]
|
||
- **TemplateManager**: 模板注册和管理 [types.ts:28-71]
|
||
- **TemplateService**: 业务服务集成 [template.service.ts]
|
||
|
||
#### 数据库层 (新增能力)
|
||
- **n8n_templates表**: 存储模板配置信息
|
||
- **N8nTemplateEntity**: 配置数据实体
|
||
- **TemplateUsageLog**: 使用记录统计
|
||
|
||
#### Entity层 (动态桥接)
|
||
- **N8nImageGenerateTemplateEntity**: 动态图片模板实例
|
||
- **N8nVideoGenerateTemplateEntity**: 动态视频模板实例
|
||
- **N8nTemplateFactoryService**: 模板工厂服务
|
||
|
||
## ✅ 混合式架构的优势
|
||
|
||
### 🔒 保持类型安全 (现有优势)
|
||
```typescript
|
||
// 现有代码已实现类型安全
|
||
const template = templateManager.getTemplate('photo_restore_v1');
|
||
if (template instanceof N8nImageGenerateTemplate) {
|
||
const result = await template.execute(imageUrl); // 类型检查
|
||
}
|
||
|
||
// Entity层同样保持类型安全
|
||
const template = await new N8nVideoGenerateTemplateEntity(id, repo).onInit();
|
||
const result = await template.execute(imageUrl); // 强类型约束
|
||
```
|
||
|
||
### 🧬 代码复用增强 (现有+新增)
|
||
```typescript
|
||
// 现有的N8n抽象类提供通用执行逻辑
|
||
export abstract class N8nImageGenerateTemplate extends ImageGenerateTemplate {
|
||
execute(imageUrl: string): Promise<string> {
|
||
return axios.request({
|
||
url: 'https://n8n.bowongai.com/webhook/...',
|
||
data: {
|
||
model: this.imageModel, // 子类提供
|
||
prompt: this.imagePrompt, // 子类提供
|
||
image_url: imageUrl
|
||
}
|
||
});
|
||
}
|
||
}
|
||
|
||
// Entity层动态提供配置,复用执行逻辑
|
||
export class N8nImageGenerateTemplateEntity extends N8nImageGenerateTemplate {
|
||
get imageModel(): string { return this.config?.imageModel || ''; }
|
||
get imagePrompt(): string { return this.config?.imagePrompt || ''; }
|
||
// execute方法自动继承,无需重写
|
||
}
|
||
```
|
||
|
||
### 🚀 扩展性大幅增强 (质的飞跃)
|
||
```typescript
|
||
// 代码扩展:添加新抽象类型
|
||
export class NewAIProviderTemplate extends ImageGenerateTemplate {
|
||
// 新AI服务商的实现逻辑
|
||
}
|
||
|
||
// 配置扩展:数据库中添加新模板记录
|
||
INSERT INTO n8n_templates (code, name, description, ...) VALUES
|
||
('new_style_v1', '新风格模板', '描述...', ...);
|
||
|
||
// 无需重启服务,立即生效
|
||
const newTemplate = await factory.createTemplateByCode('new_style_v1');
|
||
```
|
||
|
||
### 🎛️ 运营管理能力 (全新增值)
|
||
- **在线配置**: 管理后台可调整模板参数、提示词等
|
||
- **A/B测试**: 同功能不同配置版本对比测试
|
||
- **灰度发布**: 新模板先小范围开放测试
|
||
- **实时监控**: 每个模板的使用率、成功率、耗时等
|
||
- **个性化**: 根据用户行为推荐合适的模板
|
||
|
||
|
||
## 🔧 使用方式和扩展
|
||
|
||
### 您期望的使用方式
|
||
```typescript
|
||
// 通过ID创建并执行 (您的预期语法)
|
||
const result = await new N8nVideoGenerateTemplateEntity(templateId, repo)
|
||
.onInit()
|
||
.then(e => e.execute(imgUrl));
|
||
|
||
// 通过工厂服务简化使用
|
||
const template = await templateFactory.createTemplateByCode('character_figurine_v1');
|
||
const result = await template.execute(imageUrl);
|
||
|
||
// 支持链式调用
|
||
const result = await templateFactory
|
||
.createVideoTemplate(1)
|
||
.then(t => t.execute(imageUrl));
|
||
```
|
||
|
||
### 前端API调用 (保持简洁)
|
||
```typescript
|
||
// 基于数据库ID的调用
|
||
const result = await fetch('/api/templates/1/execute', {
|
||
method: 'POST',
|
||
headers: { 'Authorization': `Bearer ${token}` },
|
||
body: JSON.stringify({ imageUrl: 'https://...' })
|
||
});
|
||
|
||
// 基于模板代码的调用 (兼容现有)
|
||
const result = await fetch('/api/templates/character_figurine_v1/execute', {
|
||
method: 'POST',
|
||
headers: { 'Authorization': `Bearer ${token}` },
|
||
body: JSON.stringify({ imageUrl: 'https://...' })
|
||
});
|
||
```
|
||
|
||
### 添加新模板 (多种方式)
|
||
```typescript
|
||
// 方式1: 数据库中直接添加配置 (推荐)
|
||
INSERT INTO n8n_templates (
|
||
code, name, description, template_type,
|
||
image_model, image_prompt, ...
|
||
) VALUES (
|
||
'new_template_v1', '新模板', '描述', 'image',
|
||
'gemini-2.5-flash-image-preview', 'AI提示词', ...
|
||
);
|
||
|
||
// 方式2: 代码中添加新抽象类型 (框架扩展)
|
||
export class CustomAITemplate extends ImageGenerateTemplate {
|
||
// 新AI服务商的实现
|
||
}
|
||
|
||
// 方式3: 迁移现有代码模板到数据库
|
||
await migrationService.migrateExistingTemplates();
|
||
```
|
||
|
||
## 🎯 混合式架构的核心价值
|
||
|
||
### 1. **保持现有优势** (代码层不变)
|
||
- **统一抽象**: N8n工作流统一处理所有AI模型调用
|
||
- **类型安全**: TypeScript抽象类确保编译时类型检查
|
||
- **执行高效**: 内存中执行,无数据库查询开销
|
||
- **易于调试**: 完整的IDE支持和断点调试
|
||
|
||
### 2. **新增数据驱动能力** (数据库层增值)
|
||
- **配置动态化**: 模板参数可在线调整,无需重新部署
|
||
- **版本管理**: 支持模板配置的版本控制和回滚
|
||
- **权限控制**: 可设置模板的可见性和访问权限
|
||
- **成本优化**: 基于使用统计调整积分定价策略
|
||
|
||
### 3. **业务智能提升** (Entity层创新)
|
||
- **个性化推荐**: 根据用户历史偏好推荐模板
|
||
- **A/B测试框架**: 同功能不同配置版本效果对比
|
||
- **实时监控**: 模板性能、成功率、用户满意度监控
|
||
- **自动化运营**: 根据数据自动调整模板优先级
|
||
|
||
### 4. **开发效率飞跃** (工具链增强)
|
||
```typescript
|
||
// 一行代码创建任意模板实例
|
||
const template = await factory.createTemplateByCode('any_template_v1');
|
||
|
||
// 支持批量操作
|
||
const results = await Promise.all(
|
||
imageUrls.map(url => template.execute(url))
|
||
);
|
||
|
||
// 支持条件执行
|
||
const template = user.isPremium ?
|
||
await factory.createTemplateByCode('premium_template_v1') :
|
||
await factory.createTemplateByCode('basic_template_v1');
|
||
```
|
||
|
||
### 5. **运营价值最大化** (商业化支持)
|
||
- **精准定价**: 基于模板成本和用户价值的动态定价
|
||
- **用户分层**: VIP用户专享模板和优先队列
|
||
- **市场洞察**: 了解哪些模板最受欢迎,指导产品方向
|
||
- **收入优化**: 通过数据分析优化模板组合和推荐策略
|
||
|
||
## 🚀 总结
|
||
|
||
这种混合式架构完美融合了:
|
||
|
||
### 保持现有架构优势
|
||
✅ **代码优先**: 执行逻辑仍在TypeScript代码中,保持类型安全
|
||
✅ **性能卓越**: 内存执行,无数据库查询延迟
|
||
✅ **结构清晰**: 抽象层次分明,易于理解和维护
|
||
✅ **扩展便利**: 继承抽象类即可添加新功能
|
||
|
||
### 新增数据库强大能力
|
||
🆕 **配置灵活**: 模板参数可在线调整,支持实时生效
|
||
🆕 **运营友好**: 完整的管理后台和数据分析面板
|
||
🆕 **业务智能**: 个性化推荐、A/B测试、用户画像
|
||
🆕 **商业价值**: 数据驱动的定价策略和收入优化
|
||
|
||
**您的使用体验**:
|
||
```typescript
|
||
// 简单优雅的API
|
||
const result = await new N8nVideoGenerateTemplateEntity(id, repo)
|
||
.onInit()
|
||
.then(e => e.execute(imgUrl));
|
||
```
|
||
|
||
这是一个既保持技术优雅性,又具备强大商业价值的完美架构设计!
|