-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNarrativeMemory.tsx
More file actions
249 lines (234 loc) · 9.19 KB
/
Copy pathNarrativeMemory.tsx
File metadata and controls
249 lines (234 loc) · 9.19 KB
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
import React, { useEffect, useState } from 'react';
import { BookOpen, TrendingUp, Target, AlertTriangle, X } from 'lucide-react';
import { ConversationMemory } from '../types';
import { ConversationMemoryService } from '../services/conversationMemoryService';
interface Props {
isOpen: boolean;
onClose: () => void;
language: 'vi' | 'en';
}
export const NarrativeMemory: React.FC<Props> = ({ isOpen, onClose, language }) => {
const [memory, setMemory] = useState<ConversationMemory | null>(null);
const [loading, setLoading] = useState(true);
useEffect(() => {
if (isOpen) {
loadMemory();
}
}, [isOpen]);
const loadMemory = async () => {
setLoading(true);
try {
const data = await ConversationMemoryService.getMemory();
setMemory(data);
} catch (error) {
console.error('[NarrativeMemory] Load failed:', error);
}
setLoading(false);
};
if (!isOpen) return null;
const text = language === 'vi' ? {
title: "Hành Trình Của Bạn",
themes: "Chủ Đề Chính",
progress: "Tiến Bộ",
events: "Sự Kiện Gần Đây",
triggers: "Yếu Tố Khởi Phát",
baseline: "Tâm Trạng Cơ Bản",
close: "Đóng",
noData: "Chưa có dữ liệu",
loading: "Đang tải..."
} : {
title: "Your Journey",
themes: "Key Themes",
progress: "Progress Markers",
events: "Recent Events",
triggers: "Identified Triggers",
baseline: "Emotional Baseline",
close: "Close",
noData: "No data yet",
loading: "Loading..."
};
return (
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 backdrop-blur-sm p-4">
<div className="bg-white rounded-2xl shadow-2xl max-w-2xl w-full max-h-[85vh] overflow-y-auto">
{/* Header */}
<div className="bg-gradient-to-r from-indigo-600 to-purple-600 text-white p-6 rounded-t-2xl sticky top-0 z-10">
<div className="flex items-center justify-between">
<div className="flex items-center gap-3">
<BookOpen size={24} />
<h2 className="text-2xl font-bold">{text.title}</h2>
</div>
<button
onClick={onClose}
className="p-2 rounded-full hover:bg-white/20 transition-colors"
>
<X size={20} />
</button>
</div>
</div>
{loading ? (
<div className="p-12 text-center text-stone-500">
{text.loading}
</div>
) : !memory ? (
<div className="p-12 text-center text-stone-500">
{text.noData}
</div>
) : (
<div className="p-6 space-y-6">
{/* Recurring Themes */}
{memory.narrative.recurring_themes.length > 0 && (
<Section
icon={<TrendingUp size={18} className="text-orange-500" />}
title={text.themes}
>
<div className="flex flex-wrap gap-2">
{memory.narrative.recurring_themes.map((theme, idx) => (
<span
key={idx}
className="px-3 py-1.5 bg-orange-50 text-orange-700 rounded-full text-sm font-medium border border-orange-200"
>
{theme}
</span>
))}
</div>
</Section>
)}
{/* Progress Markers */}
{memory.narrative.progress_markers.length > 0 && (
<Section
icon={<Target size={18} className="text-green-500" />}
title={text.progress}
>
<ul className="space-y-2">
{memory.narrative.progress_markers.map((marker, idx) => (
<li
key={idx}
className="flex items-center gap-2 text-sm text-stone-700"
>
<div className="w-2 h-2 rounded-full bg-green-500"></div>
{marker}
</li>
))}
</ul>
</Section>
)}
{/* Recent Events */}
{memory.narrative.key_events.length > 0 && (
<Section
icon={<BookOpen size={18} className="text-blue-500" />}
title={text.events}
>
<div className="space-y-3">
{memory.narrative.key_events.slice(-5).reverse().map((event, idx) => (
<div
key={idx}
className="p-3 bg-blue-50 rounded-lg border border-blue-200"
>
<div className="flex items-center justify-between mb-1">
<span className="text-xs font-bold text-blue-700 uppercase">
{event.emotion}
</span>
<span className="text-xs text-blue-600">
{new Date(event.timestamp).toLocaleDateString(language === 'vi' ? 'vi-VN' : 'en-US')}
</span>
</div>
<p className="text-sm text-stone-700 line-clamp-2">
{event.event}
</p>
</div>
))}
</div>
</Section>
)}
{/* Triggers */}
{memory.emotional_baseline.triggers.size > 0 && (
<Section
icon={<AlertTriangle size={18} className="text-amber-500" />}
title={text.triggers}
>
<div className="space-y-2">
{Array.from(memory.emotional_baseline.triggers.entries())
.sort((a, b) => b[1] - a[1])
.slice(0, 5)
.map(([trigger, intensity]) => (
<div key={trigger} className="flex items-center gap-3">
<div className="flex-1">
<div className="flex items-center justify-between mb-1">
<span className="text-sm font-medium text-stone-700">
{trigger}
</span>
<span className="text-xs text-stone-500">
{Math.round(intensity * 100)}%
</span>
</div>
<div className="w-full bg-stone-200 rounded-full h-1.5">
<div
className="h-full bg-amber-500 rounded-full transition-all"
style={{ width: `${intensity * 100}%` }}
/>
</div>
</div>
</div>
))}
</div>
</Section>
)}
{/* Emotional Baseline */}
<Section
icon={<TrendingUp size={18} className="text-purple-500" />}
title={text.baseline}
>
<div className="bg-purple-50 rounded-lg p-4 border border-purple-200">
<div className="flex items-center justify-between mb-2">
<span className="text-sm text-purple-700 font-medium">
{language === 'vi' ? 'Điểm trung bình' : 'Average Score'}
</span>
<span className="text-2xl font-bold text-purple-900">
{memory.emotional_baseline.baseline_mood.toFixed(1)}/10
</span>
</div>
{memory.emotional_baseline.current_deviation !== 0 && (
<div className="text-xs text-purple-600">
{memory.emotional_baseline.current_deviation > 0 ? '↑' : '↓'}{' '}
{language === 'vi' ? 'Hiện tại' : 'Currently'}{' '}
{Math.abs(memory.emotional_baseline.current_deviation).toFixed(1)}{' '}
{memory.emotional_baseline.current_deviation > 0
? language === 'vi' ? 'cao hơn' : 'above'
: language === 'vi' ? 'thấp hơn' : 'below'
}{' '}
{language === 'vi' ? 'mức cơ bản' : 'baseline'}
</div>
)}
</div>
</Section>
</div>
)}
{/* Footer */}
<div className="border-t border-stone-200 p-4 bg-stone-50 rounded-b-2xl">
<button
onClick={onClose}
className="w-full py-3 px-4 rounded-lg bg-gradient-to-r from-indigo-600 to-purple-600 hover:from-indigo-700 hover:to-purple-700 text-white font-medium transition-all shadow-lg"
>
{text.close}
</button>
</div>
</div>
</div>
);
};
interface SectionProps {
icon: React.ReactNode;
title: string;
children: React.ReactNode;
}
const Section: React.FC<SectionProps> = ({ icon, title, children }) => {
return (
<div className="space-y-3">
<div className="flex items-center gap-2">
{icon}
<h3 className="font-semibold text-stone-800">{title}</h3>
</div>
<div className="pl-7">{children}</div>
</div>
);
};