File size: 10,329 Bytes
e375f30
 
25fe4a1
 
e375f30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25fe4a1
 
e375f30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25fe4a1
e375f30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/**
 * 并行分析流处理器
 * 支持6个Agent并行执行,渐进式SSE推送
 * K线分为过去(出生到今年)和未来(今年到100岁)两个并行请求
 * 支持缓存命中时直接返回
 */
import { nanoid } from 'nanoid';
import {
  updateUserPoints,
  saveUserInput,
  saveAnalysis,
  logEvent,
} from './database.js';
import { calculateLifeTimeline } from './baziCalculator.js';
import { runParallelAgents, mergeAgentResults, sendSSE } from './parallelAnalyzer.js';
import {
  computeBaziHash,
  getCachedAnalysis,
  cacheAnalysis,
  extractCoreData,
  mergeCachedWithFresh,
  hasCachedAnalysis,
} from './cacheManager.js';

const COST_PER_ANALYSIS = process.env.COST_PER_ANALYSIS ? parseInt(process.env.COST_PER_ANALYSIS, 10) : 50;

/**
 * 并行分析流处理器
 */
export const handleParallelAnalyzeStream = async (req, res) => {
  // 设置SSE响应头
  res.setHeader('Content-Type', 'text/event-stream');
  res.setHeader('Cache-Control', 'no-cache');
  res.setHeader('Connection', 'keep-alive');
  res.setHeader('X-Accel-Buffering', 'no');

  const body = req.body || {};
  const useCustomApi = Boolean(body.useCustomApi);
  const skipCache = Boolean(body.skipCache); // 是否跳过缓存

  let authedInfo = req.__authedInfo || null;

  const input = {
    name: body.name || '',
    birthPlace: body.birthPlace || '',
    gender: body.gender,
    birthYear: body.birthYear,
    yearPillar: body.yearPillar,
    monthPillar: body.monthPillar,
    dayPillar: body.dayPillar,
    hourPillar: body.hourPillar,
    startAge: body.startAge,
    firstDaYun: body.firstDaYun,
  };

  const inputId = nanoid();
  const startTime = Date.now();

  // 进度回调
  const onProgress = (message) => {
    sendSSE(res, 'progress', { message, timestamp: Date.now() });
  };

  // 启动心跳保活
  const keepAliveInterval = setInterval(() => {
    if (!res.writableEnded) {
      res.write(': keep-alive\n\n');
    }
  }, 10000);

  const cleanup = () => clearInterval(keepAliveInterval);
  res.on('close', cleanup);
  res.on('finish', cleanup);

  sendSSE(res, 'progress', { message: '正在初始化并行分析系统...', phase: 'init' });

  // 计算八字哈希
  const baziHash = computeBaziHash(
    input.yearPillar,
    input.monthPillar,
    input.dayPillar,
    input.hourPillar
  );
  const genderKey = input.gender === 'Male' ? 'male' : 'female';

  sendSSE(res, 'progress', { message: `八字哈希: ${baziHash}`, phase: 'hash' });

  // 检查缓存
  if (!skipCache && !useCustomApi) {
    const cachedData = getCachedAnalysis(baziHash, genderKey);

    if (cachedData) {
      sendSSE(res, 'cache_hit', {
        message: '✓ 命中永久缓存,直接返回一致性结果',
        baziHash,
        cachedAt: cachedData.createdAt,
      });

      // 从缓存构建结果
      const cachedResult = mergeCachedWithFresh(cachedData);

      const finalResult = {
        chartData: cachedData.klineData || [],
        analysis: cachedResult,
      };

      // 处理用户积分(缓存命中也扣分)
      let user = null;
      let cost = 0;
      let isGuest = false;

      if (authedInfo) {
        const newPoints = Math.max(0, authedInfo.user.points - COST_PER_ANALYSIS);
        updateUserPoints(authedInfo.user.id, newPoints);
        cost = COST_PER_ANALYSIS;
        user = { id: authedInfo.user.id, email: authedInfo.user.email, points: newPoints };

        logEvent('info', '缓存命中分析', { baziHash, cost }, authedInfo.user.id, req.ip);
      } else {
        isGuest = true;
        logEvent('info', '游客缓存命中', { baziHash }, null, req.ip);
      }

      // 发送完成事件
      sendSSE(res, 'complete', {
        result: finalResult,
        user,
        cost,
        isGuest,
        fromCache: true,
        processingTimeMs: Date.now() - startTime,
      });

      return res.end();
    } else {
      sendSSE(res, 'cache_miss', { message: '缓存未命中,启动并行分析...', baziHash });
    }
  }

  // 预计算生命周期骨架
  let skeletonData = null;
  try {
    skeletonData = calculateLifeTimeline(input);
    sendSSE(res, 'progress', { message: '✓ 已生成100年流年骨架', phase: 'skeleton' });
  } catch (err) {
    console.error('骨架计算失败:', err);
    sendSSE(res, 'error', {
      error: 'SKELETON_CALC_FAILED',
      message: '流年骨架计算失败,请检查输入数据'
    });
    return res.end();
  }

  // 执行并行Agent分析
  sendSSE(res, 'parallel_start', {
    message: '🚀 启动6个专业Agent并行分析...',
    agents: ['core', 'kline_past', 'kline_future', 'career', 'marriage', 'crypto'],
  });

  const parallelResult = await runParallelAgents(input, skeletonData, res, onProgress);

  if (!parallelResult.success) {
    sendSSE(res, 'error', {
      error: 'ALL_AGENTS_FAILED',
      message: '所有Agent分析均失败,请稍后重试'
    });
    return res.end();
  }

  // 合并Agent结果
  sendSSE(res, 'progress', { message: '正在合并分析结果...', phase: 'merge' });
  const mergedAnalysis = mergeAgentResults(parallelResult.results, skeletonData);

  const finalResult = {
    chartData: mergedAnalysis.chartPoints || [],
    analysis: {
      bazi: mergedAnalysis.bazi || [],
      summary: mergedAnalysis.summary || '命理分析完成',
      summaryScore: mergedAnalysis.summaryScore || 5,
      personality: mergedAnalysis.personality || '',
      personalityScore: mergedAnalysis.personalityScore || 5,
      industry: mergedAnalysis.industry || '',
      industryScore: mergedAnalysis.industryScore || 5,
      fengShui: mergedAnalysis.fengShui || '',
      fengShuiScore: mergedAnalysis.fengShuiScore || 5,
      wealth: mergedAnalysis.wealth || '',
      wealthScore: mergedAnalysis.wealthScore || 5,
      marriage: mergedAnalysis.marriage || '',
      marriageScore: mergedAnalysis.marriageScore || 5,
      health: mergedAnalysis.health || '',
      healthScore: mergedAnalysis.healthScore || 5,
      family: mergedAnalysis.family || '',
      familyScore: mergedAnalysis.familyScore || 5,
      crypto: mergedAnalysis.crypto || '',
      cryptoScore: mergedAnalysis.cryptoScore || 5,
      cryptoYear: mergedAnalysis.cryptoYear || '待定',
      cryptoStyle: mergedAnalysis.cryptoStyle || '现货定投',

      // 扩展字段
      appearance: mergedAnalysis.appearance,
      bodyType: mergedAnalysis.bodyType,
      skin: mergedAnalysis.skin,
      characterSummary: mergedAnalysis.characterSummary,
      monthlyFortune: mergedAnalysis.monthlyFortune,
      monthlyHighlights: mergedAnalysis.monthlyHighlights,
      yearlyFortune: mergedAnalysis.yearlyFortune,
      yearlyKeyEvents: mergedAnalysis.yearlyKeyEvents,
      luckyColors: mergedAnalysis.luckyColors,
      luckyDirections: mergedAnalysis.luckyDirections,
      luckyZodiac: mergedAnalysis.luckyZodiac,
      luckyNumbers: mergedAnalysis.luckyNumbers,
      keyDatesThisMonth: mergedAnalysis.keyDatesThisMonth,
      keyDatesThisYear: mergedAnalysis.keyDatesThisYear,
      pastEvents: mergedAnalysis.pastEvents,
      futureEvents: mergedAnalysis.futureEvents,
      keyYears: mergedAnalysis.keyYears,
      peakYears: mergedAnalysis.peakYears,
      troughYears: mergedAnalysis.troughYears,
      healthBodyParts: mergedAnalysis.healthBodyParts,
    },
  };

  // 保存到缓存(永久缓存)
  if (!useCustomApi && !skipCache) {
    try {
      const coreData = extractCoreData(finalResult.analysis, finalResult.chartData);
      cacheAnalysis({
        baziHash,
        gender: genderKey,
        ...coreData,
        modelUsed: 'parallel-agents',
        version: 1,
      });
      sendSSE(res, 'progress', { message: '✓ 结果已存入永久缓存', phase: 'cache' });
    } catch (cacheErr) {
      console.error('缓存保存失败:', cacheErr);
      // 缓存失败不影响主流程
    }
  }

  // 处理用户数据
  let user = null;
  let cost = 0;
  let isGuest = false;

  if (!useCustomApi) {
    // 保存用户输入
    saveUserInput({
      id: inputId,
      userId: authedInfo ? authedInfo.user.id : null,
      name: input.name,
      gender: input.gender,
      birthYear: input.birthYear,
      yearPillar: input.yearPillar,
      monthPillar: input.monthPillar,
      dayPillar: input.dayPillar,
      hourPillar: input.hourPillar,
      startAge: input.startAge,
      firstDaYun: input.firstDaYun,
      modelName: 'parallel-agents',
      apiBaseUrl: '',
      useCustomApi: false,
      ipAddress: req.ip,
      userAgent: req.get('User-Agent'),
    });

    const analysisId = nanoid();

    if (authedInfo) {
      const newPoints = Math.max(0, authedInfo.user.points - COST_PER_ANALYSIS);
      updateUserPoints(authedInfo.user.id, newPoints);
      cost = COST_PER_ANALYSIS;

      saveAnalysis({
        id: analysisId,
        userId: authedInfo.user.id,
        inputId: inputId,
        cost,
        modelUsed: 'parallel-agents',
        chartData: finalResult.chartData,
        analysisData: finalResult.analysis,
        processingTimeMs: Date.now() - startTime,
        status: 'completed',
      });

      logEvent('info', '并行分析完成', {
        analysisId,
        cost,
        agents: parallelResult.completedAgents,
        successCount: parallelResult.successCount,
      }, authedInfo.user.id, req.ip);

      user = { id: authedInfo.user.id, email: authedInfo.user.email, points: newPoints };
    } else {
      isGuest = true;

      saveAnalysis({
        id: analysisId,
        userId: null,
        inputId: inputId,
        cost: 0,
        modelUsed: 'parallel-agents',
        chartData: finalResult.chartData,
        analysisData: finalResult.analysis,
        processingTimeMs: Date.now() - startTime,
        status: 'completed',
      });

      logEvent('info', '游客并行分析', {
        analysisId,
        agents: parallelResult.completedAgents,
      }, null, req.ip);
    }
  }

  // 发送完成事件
  sendSSE(res, 'complete', {
    result: finalResult,
    user,
    cost,
    isGuest,
    fromCache: false,
    processingTimeMs: Date.now() - startTime,
    agentsUsed: parallelResult.completedAgents,
    successCount: parallelResult.successCount,
    totalAgents: parallelResult.totalAgents,
  });

  res.end();
};

export default handleParallelAnalyzeStream;