Files
makemd/docs/08_Analysis/06_Runtime_Architecture.md
wurenzhi eafa1bbe94 feat: 添加货币和汇率管理功能
refactor: 重构前端路由和登录逻辑

docs: 更新业务闭环、任务和架构文档

style: 调整代码格式和文件结构

chore: 更新依赖项和配置文件
2026-03-19 19:08:15 +08:00

520 lines
6.5 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 🌐 Runtime_Architecture.md运行态架构 · 可落地版)
---
# 🎯 一、设计目标
> 将系统从“接口驱动”升级为“事件驱动 + 自动运行系统”,实现业务闭环自动推进、可计费、可监控。
---
# 🧠 二、核心设计原则
### 1⃣ 事件驱动Event Driven
所有业务流转必须通过事件触发,而不是手动串流程
---
### 2⃣ 状态驱动State Driven
所有实体必须由 STATE_MACHINE 控制流转
---
### 3⃣ 服务编排Service Orchestration
所有业务逻辑必须在 Service 层执行
---
### 4⃣ 自动化优先Automation First
能自动执行的流程,禁止依赖用户点击
---
### 5⃣ 可计费Billable by Design
所有关键行为必须具备计费能力
---
# 🧩 三、Runtime 四大核心模块
---
## 1⃣ Event System事件系统
### 🎯 作用
解耦模块,实现系统自动联动
---
### 📌 事件结构
```typescript
interface DomainEvent {
id: string
type: string
payload: any
timestamp: number
source: string
merchantId?: string
}
```
---
### 📌 核心事件定义
```typescript
// 商品
PRODUCT_CREATED
PRODUCT_UPDATED
// 订单
ORDER_CREATED
ORDER_PAID
ORDER_COMPLETED
// 库存
INVENTORY_LOW
INVENTORY_UPDATED
// 功能
FEATURE_ENABLED
FEATURE_DISABLED
// AI
AI_TASK_CREATED
AI_TASK_COMPLETED
// 广告
AD_STARTED
AD_PERFORMANCE_UPDATED
// 任务
JOB_CREATED
JOB_UPDATED
JOB_COMPLETED
// 计费
BILLING_GENERATED
```
---
### 📌 事件流
```typescript
Controller Service Emit Event EventBus Listener Service
```
---
### 📌 示例
```typescript
await productService.createProduct(data)
eventBus.emit({
type: 'PRODUCT_CREATED',
payload: { productId },
source: 'ProductService',
merchantId: data.merchantId
})
```
---
## 2⃣ Job / Worker System任务系统
### 🎯 作用
执行异步任务 & 自动推进系统
---
### 📌 任务模型
```typescript
interface Job {
id: string
type: string
status: 'pending' | 'running' | 'success' | 'failed'
payload: any
result?: any
retryCount: number
merchantId?: string
priority?: number
}
```
---
### 📌 核心任务类型
```typescript
AI_OPTIMIZE_PRODUCT
SYNC_INVENTORY
RUN_ADS
CALCULATE_PROFIT
GENERATE_BILL
UPDATE_AD_BUDGET
STOP_AD
```
---
### 📌 执行流程
```typescript
Event Job Worker Service Event
```
---
### 📌 示例
```typescript
eventBus.on('PRODUCT_CREATED', async (event) => {
await jobService.create({
type: 'AI_OPTIMIZE_PRODUCT',
payload: { productId: event.payload.productId },
merchantId: event.merchantId
})
})
```
---
## 3⃣ Scheduler调度系统
### 🎯 作用
周期性驱动系统(无人值守)
---
### 📌 示例调度
```typescript
// 每5分钟
syncInventory()
// 每1小时
calculateROI()
// 每天
runSettlement()
```
---
### 📌 实现方式
```typescript
node-cron / bullmq / agenda
```
---
### 📌 调度 → Job
```typescript
cron.schedule('*/5 * * * *', async () => {
await jobService.create({ type: 'SYNC_INVENTORY' })
})
```
---
## 4⃣ State Engine状态推进器
### 🎯 作用
统一控制所有状态流转
---
### 📌 原则
❌ Service 随意改状态
✅ 必须通过 State Machine
---
### 📌 示例
```typescript
orderStateMachine.transition(order, 'PAY')
```
---
### 📌 自动推进
```typescript
Worker Service
```
---
# 💰 四、计费系统Billing Runtime
---
## 📊 1⃣ 计费模型
```typescript
FeatureBilling
UsageBilling
ResultBilling
SubscriptionBilling
```
---
## 📌 2⃣ 使用量计费
```typescript
UsageService.track({
feature: 'AI_OPTIMIZE',
usage: 1,
merchantId,
source: 'AI'
})
```
---
## 📌 3⃣ 账单生成
```typescript
BillingService.generateBill(merchantId)
```
---
## 📌 4⃣ 收费触发点
| 行为 | 是否计费 |
| --- | --- |
| AI优化 | ✅ |
| 广告投放 | ✅ |
| 自动补货 | ✅ |
| API调用 | ✅ |
---
# 🔄 五、完整运行链路(核心)
---
## 🎯 示例:商品自动赚钱闭环
```typescript
PRODUCT_CREATED
JobAI优化
AI_TASK_COMPLETED
AD_STARTED
ORDER_PAID
CALCULATE_PROFIT
GENERATE_BILL
```
---
# 🎯 六、前端 Runtime 对应设计(关键)
---
## 1⃣ Task Center必须做
### 字段
```typescript
//
```
---
## 2⃣ Event Log
```typescript
```
---
## 3⃣ Billing Timeline
```typescript
```
---
## 4⃣ ROI Dashboard核心
```typescript
ID
ROI
```
---
# 🧱 七、Service 分层强化(必须执行)
---
## ❗ 强制规则
```typescript
Controller
Service
Worker Service
```
---
# 🔐 八、运行安全设计
---
## 必须补
### 1⃣ 幂等性
```typescript
jobId: productId + type
```
---
### 2⃣ 重试机制
```typescript
retry 3
```
---
### 3⃣ 日志系统
```typescript
Event Log + Job Log + Error Log
```
---
# 🚀 九、实施步骤
---
## 1⃣ 新建模块
```bash
/src/runtime
├── eventBus.ts
├── jobQueue.ts
├── worker.ts
├── scheduler.ts
├── jobProcessor.ts
```
---
## 2⃣ 实现 BullMQ 分布式队列
```bash
/src/runtime/queue
├── index.ts
├── worker.ts
├── processor.ts
├── priority.ts
```
---
## 3⃣ 实现 WebSocket 实时推送
```bash
/src/runtime/ws
├── server.ts
├── client.ts
├── broadcast.ts
```
---
## 4⃣ 实现计费系统
```bash
/src/services
├── UsageService.ts
├── BillingService.ts
```
---
## 5⃣ 实现 AI 策略系统
```bash
/src/runtime/strategy
├── engine.ts
├── roi.ts
├── ad.ts
```
---
## 6⃣ 前端实现
```bash
/pages/TaskCenter/index.tsx
/pages/EventLog/index.tsx
/pages/Billing/index.tsx
/pages/ROIDashboard/index.tsx
```
---
# 🧠 最终结论
你现在系统的阶段是:
> ✅ 架构完成
> ❗ Runtime 未建立
---
# 🔥 一句话
> **没有 Runtime你的系统只是“能用”
> 有 Runtime才是“能赚钱 + 能自动跑”的系统。**