|
1 | | -/** |
2 | | - * Build analysis result payload for the AI analysis result page from a history list item. |
3 | | - * Mirrors PC viewHistoryResult: prefer raw_result (full_result), merge list metadata, fallback SL/TP. |
4 | | - */ |
5 | | - |
6 | | -function normalizeConfidence(value) { |
7 | | - const num = Number(value) |
8 | | - if (!Number.isFinite(num)) return 0 |
9 | | - if (num > 0 && num <= 1) return Math.round(num * 100) |
10 | | - return num |
11 | | -} |
| 1 | +import { professionalReportEnvelope } from './professionalReport.js' |
12 | 2 |
|
13 | 3 | export function parseFullResult(raw) { |
14 | 4 | if (raw == null) return null |
15 | | - let obj = raw |
16 | | - if (typeof obj === 'string') { |
| 5 | + let value = raw |
| 6 | + if (typeof value === 'string') { |
17 | 7 | try { |
18 | | - obj = JSON.parse(obj) |
| 8 | + value = JSON.parse(value) |
19 | 9 | } catch { |
20 | 10 | return null |
21 | 11 | } |
22 | 12 | } |
23 | | - if (!obj || typeof obj !== 'object') return null |
24 | | - if ( |
25 | | - obj.data && |
26 | | - typeof obj.data === 'object' && |
27 | | - !obj.decision && |
28 | | - (obj.data.decision || obj.data.summary || obj.data.consensus) |
29 | | - ) { |
30 | | - obj = obj.data |
31 | | - } |
32 | | - return obj |
33 | | -} |
34 | | - |
35 | | -function isMeaningfulFullResult(full) { |
36 | | - if (!full || typeof full !== 'object') return false |
37 | | - if (String(full.task_status || '').toLowerCase() === 'processing') { |
38 | | - return !!(full.consensus || full.detailed_analysis || (full.summary && full.summary.length > 40)) |
39 | | - } |
40 | | - return !!( |
41 | | - full.decision || |
42 | | - full.summary || |
43 | | - full.consensus || |
44 | | - full.detailed_analysis || |
45 | | - full.trading_plan || |
46 | | - full.trend_outlook |
47 | | - ) |
48 | | -} |
49 | | - |
50 | | -function buildTradingPlanFallback(item, price, decision) { |
51 | | - const dec = String(decision || 'HOLD').toUpperCase() |
52 | | - let stopLoss = null |
53 | | - let takeProfit = null |
54 | | - if (price > 0) { |
55 | | - if (dec === 'SELL') { |
56 | | - stopLoss = price * 1.05 |
57 | | - takeProfit = price * 0.95 |
58 | | - } else if (dec === 'BUY') { |
59 | | - stopLoss = price * 0.95 |
60 | | - takeProfit = price * 1.05 |
61 | | - } |
62 | | - } |
63 | | - const entry = price || item?.price || item?.current_price |
64 | | - return { |
65 | | - entry_price: entry, |
66 | | - stop_loss: stopLoss ?? item?.stop_loss, |
67 | | - take_profit: takeProfit ?? item?.take_profit |
68 | | - } |
| 13 | + if (!value || typeof value !== 'object') return null |
| 14 | + return value.data && typeof value.data === 'object' ? value.data : value |
69 | 15 | } |
70 | 16 |
|
71 | 17 | /** |
72 | | - * @param {object} item History row from GET /api/fast-analysis/history/all |
73 | | - * @returns {object|null} Payload for useAiAnalysisStore().setLastResult() |
| 18 | + * History is a read boundary, not a reconstruction engine. Only a persisted |
| 19 | + * professional_report_v1 artifact is renderable; legacy rows must be regenerated. |
74 | 20 | */ |
75 | 21 | export function buildHistoryResultPayload(item) { |
76 | 22 | if (!item || typeof item !== 'object') return null |
77 | | - |
78 | 23 | const full = parseFullResult(item.full_result) |
79 | | - const useFull = isMeaningfulFullResult(full) |
80 | | - const price = |
81 | | - Number(item.price) || |
82 | | - Number(item.current_price) || |
83 | | - Number(full?.market_data?.current_price) || |
84 | | - 0 |
85 | | - const decision = (useFull ? full.decision : item.decision) || 'HOLD' |
86 | | - const tradingPlanFallback = buildTradingPlanFallback(item, price, decision) |
87 | | - |
88 | | - const fallback = { |
89 | | - decision: item.decision, |
90 | | - confidence: normalizeConfidence(item.confidence), |
91 | | - summary: item.summary, |
92 | | - market: item.market, |
93 | | - symbol: item.symbol, |
94 | | - timeframe: item.timeframe, |
95 | | - market_data: { |
96 | | - current_price: item.price || item.current_price, |
97 | | - change_24h: 0 |
98 | | - }, |
99 | | - trading_plan: tradingPlanFallback, |
100 | | - scores: item.scores || {}, |
101 | | - reasons: item.reasons || [], |
102 | | - risks: item.risks || [], |
103 | | - indicators: item.indicators || {}, |
104 | | - memory_id: item.id |
105 | | - } |
106 | | - |
107 | | - if (!useFull) { |
108 | | - return fallback |
109 | | - } |
110 | | - |
111 | | - const risks = |
112 | | - (Array.isArray(full.risks) && full.risks.length ? full.risks : null) || |
113 | | - (Array.isArray(full.risk_factors) && full.risk_factors.length ? full.risk_factors : null) || |
114 | | - fallback.risks |
115 | | - |
116 | | - const reasons = |
117 | | - (Array.isArray(full.reasons) && full.reasons.length ? full.reasons : null) || fallback.reasons |
118 | | - |
119 | | - const scores = |
120 | | - full.scores && Object.keys(full.scores).length ? full.scores : fallback.scores |
121 | | - |
122 | | - const indicators = |
123 | | - full.indicators && Object.keys(full.indicators).length |
124 | | - ? full.indicators |
125 | | - : fallback.indicators |
126 | | - |
| 24 | + const envelope = professionalReportEnvelope(full) |
| 25 | + if (!envelope) return null |
127 | 26 | return { |
128 | | - ...full, |
129 | | - memory_id: item.id ?? full.memory_id, |
130 | | - market: full.market || item.market, |
131 | | - symbol: full.symbol || item.symbol, |
132 | | - timeframe: full.timeframe || item.timeframe, |
133 | | - decision: full.decision ?? item.decision, |
134 | | - confidence: normalizeConfidence( |
135 | | - full.confidence != null ? full.confidence : item.confidence |
136 | | - ), |
137 | | - summary: full.summary ?? item.summary, |
138 | | - reasons, |
139 | | - scores, |
140 | | - risks, |
141 | | - indicators, |
142 | | - market_data: full.market_data || fallback.market_data, |
143 | | - trading_plan: |
144 | | - full.trading_plan && Object.keys(full.trading_plan).length |
145 | | - ? full.trading_plan |
146 | | - : fallback.trading_plan |
| 27 | + ...envelope, |
| 28 | + runtime: { |
| 29 | + ...(envelope.runtime || {}), |
| 30 | + memory_id: item.id ?? envelope.runtime?.memory_id |
| 31 | + } |
147 | 32 | } |
148 | 33 | } |
0 commit comments