-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtravel-card.js
More file actions
2177 lines (1828 loc) · 83 KB
/
travel-card.js
File metadata and controls
2177 lines (1828 loc) · 83 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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ==================== 行程卡系统 - 核心逻辑 ====================
const TravelCard = {
// 当前状态
currentContactId: null,
currentDate: null,
refreshTimer: null, // 定时刷新计时器
cssSchemes: {}, // CSS美化方案
currentCSSSchemeId: 'default', // 当前使用的方案ID
// 初始化
init() {
console.log('行程卡系统初始化...');
this.loadCSSSchemes(); // 加载CSS方案
this.applyCSSScheme(); // 应用CSS方案
this.applyBackgrounds(); // 应用背景
this.loadCustomFont(); // 加载自定义字体
this.loadAllCards();
this.initEventListeners();
this.startAutoRefresh(); // 启动自动刷新
},
// 初始化事件监听
initEventListeners() {
// 点击模态框背景关闭
document.getElementById('travelCardModal')?.addEventListener('click', (e) => {
if (e.target.id === 'travelCardModal') {
this.closeCardModal();
}
});
document.getElementById('settingsModal')?.addEventListener('click', (e) => {
if (e.target.id === 'settingsModal') {
this.closeSettings();
}
});
},
// 加载所有行程卡
loadAllCards() {
const contacts = this.getAllContacts();
const cardBox = document.getElementById('travelCardBox');
const emptyState = document.getElementById('emptyState');
// 如果不在行程卡页面,不执行
if (!cardBox || !emptyState) {
console.log('⚠️ [行程卡] 不在行程卡页面,跳过加载');
return;
}
if (!contacts || contacts.length === 0) {
emptyState.style.display = 'block';
return;
}
emptyState.style.display = 'none';
cardBox.innerHTML = '';
contacts.forEach(contact => {
const cardElement = this.createCardElement(contact);
cardBox.appendChild(cardElement);
});
},
// 创建卡片元素
createCardElement(contact) {
const card = document.createElement('div');
card.className = 'card-item';
card.onclick = () => this.openCardModal(contact.id);
// 使用本地日期而不是UTC日期
const today = new Date();
const year = today.getFullYear();
const month = String(today.getMonth() + 1).padStart(2, '0');
const day = String(today.getDate()).padStart(2, '0');
const todayKey = `${year}-${month}-${day}`;
const travelCardData = this.getTravelCardData(contact.id, todayKey);
// 获取显示名称(备注优先,否则真实姓名)
const displayName = contact.nickname || contact.name;
// 检查是否为陈旧卡片(超过7天)
const isOld = this.isCardOld(today);
if (isOld) {
card.classList.add('card-old');
}
card.innerHTML = `
<div class="card-header">
<img src="${contact.avatarUrl || ''}" alt="${displayName}" class="card-avatar"
onerror="this.style.display='none'">
<div class="card-info">
<div class="card-name">${displayName}</div>
<div class="card-date">${today}</div>
</div>
</div>
<div class="card-preview">
${this.generatePreview(travelCardData)}
</div>
<div class="card-footer">
<span class="card-status">${travelCardData ? '已生成' : '未生成'}</span>
<button class="card-action-btn" onclick="event.stopPropagation(); TravelCard.generateNewCard('${contact.id}')">
${travelCardData ? '刷新' : '新增'}
</button>
</div>
`;
return card;
},
// 生成预览内容
generatePreview(travelCardData) {
if (!travelCardData || !travelCardData.schedules) {
return '<p class="preview-locked">暂无行程数据</p>';
}
const now = new Date();
const timezone = travelCardData.timezone;
const visibleSchedules = travelCardData.schedules
.filter(s => this.isScheduleVisible(s, now, timezone))
.slice(0, 3);
if (visibleSchedules.length === 0) {
return '<p class="preview-locked">今日行程尚未解锁</p>';
}
return visibleSchedules.map(schedule => `
<div class="preview-item">
<span class="preview-time">${schedule.time}</span>
<span class="preview-activity">${schedule.activity}</span>
</div>
`).join('');
},
// 判断行程是否可见(支持跨时区)
isScheduleVisible(schedule, currentTime, contactTimezone) {
// 1. 数据可见性检查
if (!schedule.isVisible) return false;
// 如果没有时区,使用本地时区简单比较
if (!contactTimezone) {
const scheduleTime = new Date();
const [hours, minutes] = schedule.time.split(':');
scheduleTime.setHours(parseInt(hours), parseInt(minutes), 0, 0);
return scheduleTime <= currentTime;
}
try {
// 2. 使用 Intl 获取联系人时区的当前日期和时间
const userNow = currentTime; // 当前用户本地时间(由外部传入)
// 获取联系人时区的当前日期(格式:YYYY-MM-DD)
const dateFormatter = new Intl.DateTimeFormat('en-US', {
timeZone: contactTimezone,
year: 'numeric',
month: '2-digit',
day: '2-digit'
});
const dateParts = dateFormatter.formatToParts(userNow);
let contactYear, contactMonth, contactDay;
dateParts.forEach(part => {
if (part.type === 'year') contactYear = part.value;
if (part.type === 'month') contactMonth = part.value;
if (part.type === 'day') contactDay = part.value;
});
const contactDateStr = `${contactYear}-${contactMonth}-${contactDay}`; // 联系人的当前日期
// 获取联系人时区的当前时间(小时:分钟)
const timeFormatter = new Intl.DateTimeFormat('en-US', {
timeZone: contactTimezone,
hour: '2-digit',
minute: '2-digit',
hour12: false
});
const timeParts = timeFormatter.formatToParts(userNow);
let contactHour, contactMinute;
timeParts.forEach(part => {
if (part.type === 'hour') contactHour = part.value;
if (part.type === 'minute') contactMinute = part.value;
});
const contactTimeStr = `${contactHour}:${contactMinute}`;
// 3. 比较日期
const cardDate = this.currentDate; // 行程卡日期,格式 YYYY-MM-DD
if (contactDateStr > cardDate) {
// 联系人时区已经过了今天 → 所有行程可见
console.log(`[时区] 联系人日期 ${contactDateStr} > 行程卡日期 ${cardDate} → 所有行程可见`);
return true;
}
if (contactDateStr < cardDate) {
// 联系人时区还没到这一天 → 所有行程不可见
console.log(`[时区] 联系人日期 ${contactDateStr} < 行程卡日期 ${cardDate} → 所有行程不可见`);
return false;
}
// 4. 同一天,比较时间
const [scheduleHour, scheduleMinute] = schedule.time.split(':').map(Number);
const contactTotal = parseInt(contactHour) * 60 + parseInt(contactMinute);
const scheduleTotal = scheduleHour * 60 + scheduleMinute;
const isVisible = contactTotal >= scheduleTotal;
console.log(`[时区] 联系人时间: ${contactTimeStr}, 行程时间: ${schedule.time}, 可见: ${isVisible}`);
return isVisible;
} catch (error) {
console.error('时区转换失败:', error);
// 降级:使用本地时区比较(仅当天时间)
const scheduleTime = new Date();
const [hours, minutes] = schedule.time.split(':');
scheduleTime.setHours(parseInt(hours), parseInt(minutes), 0, 0);
return scheduleTime <= currentTime;
}
},
// 打开行程卡模态框
openCardModal(contactId) {
this.currentContactId = contactId;
// 使用本地日期而不是UTC日期
const today = new Date();
const year = today.getFullYear();
const month = String(today.getMonth() + 1).padStart(2, '0');
const day = String(today.getDate()).padStart(2, '0');
this.currentDate = `${year}-${month}-${day}`;
const contact = this.getContact(contactId);
if (!contact) return;
const modal = document.getElementById('travelCardModal');
const displayName = contact.nickname || contact.name;
document.getElementById('modalAvatar').src = contact.avatarUrl || '';
document.getElementById('modalContactName').textContent = displayName;
document.getElementById('modalCurrentDate').textContent = this.currentDate;
// 应用陈旧化效果
const modalContent = modal.querySelector('.modal-content');
if (this.isCardOld(this.currentDate)) {
modalContent.classList.add('card-old');
} else {
modalContent.classList.remove('card-old');
}
modal.classList.add('active');
this.loadTimeline();
},
// 关闭行程卡模态框
closeCardModal() {
document.getElementById('travelCardModal').classList.remove('active');
this.currentContactId = null;
this.currentDate = null;
this.stopAutoRefresh(); // 停止自动刷新
},
// 启动自动刷新(每分钟)
startAutoRefresh() {
// 清除旧的计时器
if (this.refreshTimer) {
clearInterval(this.refreshTimer);
}
// 每分钟刷新一次时间轴(解锁新的行程项)
this.refreshTimer = setInterval(() => {
if (this.currentContactId && this.currentDate) {
console.log('🔄 [行程卡] 自动刷新时间轴');
this.loadTimeline();
}
}, 60000); // 60秒
console.log('✅ [行程卡] 自动刷新已启动');
},
// 停止自动刷新
stopAutoRefresh() {
if (this.refreshTimer) {
clearInterval(this.refreshTimer);
this.refreshTimer = null;
console.log('⏹️ [行程卡] 自动刷新已停止');
}
},
// 加载时间轴
loadTimeline() {
const container = document.getElementById('timelineContainer');
const loadingState = document.getElementById('loadingState');
const travelCardData = this.getTravelCardData(this.currentContactId, this.currentDate);
if (!travelCardData) {
// 显示加载状态,提示用户生成
container.innerHTML = `
<div class="loading-state">
<div class="empty-icon">📭</div>
<p>该日期暂无行程卡</p>
<button class="footer-btn" onclick="TravelCard.generateNewCard('${this.currentContactId}', '${this.currentDate}')" style="margin: 20px auto; max-width: 200px;">
<span>➕</span>
<span>生成行程卡</span>
</button>
</div>
`;
return;
}
const now = new Date();
const timezone = travelCardData.timezone;
container.innerHTML = travelCardData.schedules.map((schedule, index) => {
const isTimeVisible = this.isScheduleVisible(schedule, now, timezone);
const isDataVisible = schedule.isVisible !== false; // 行程数据中的可见性设置
const isVisible = isTimeVisible && isDataVisible; // 两个条件都要满足
const isLocked = !isVisible;
const hasLie = schedule.isLiar && isVisible; // 只有可见且撒谎的才显示发光
// 兼容旧数据:如果没有reported字段,使用activity
const reportedTitle = schedule.reported?.title || schedule.activity;
const reportedDetail = schedule.reported?.detail || schedule.description;
const truthActivity = schedule.truth?.activity || schedule.actualActivity || schedule.activity;
const truthLocation = schedule.truth?.location || schedule.location;
const truthDetail = schedule.truth?.detail || schedule.description;
const truthReason = schedule.truth?.reason || '未知原因';
return `
<div class="timeline-item ${hasLie ? 'has-secret' : ''}" data-schedule-index="${index}">
<div class="timeline-time">
<div class="time-label">${schedule.time}</div>
${schedule.endTime ? `<div class="time-range">- ${schedule.endTime}</div>` : ''}
${hasLie ? '<div class="secret-indicator">✨</div>' : ''}
</div>
<div class="timeline-dot ${isLocked ? 'locked' : ''} ${hasLie ? 'glowing' : ''}"></div>
<div class="timeline-content ${isLocked ? 'locked' : ''}" onclick="TravelCard.toggleCardFace(${index})">
${isLocked ? `
<div class="locked-placeholder">🔒</div>
` : `
<!-- 正面:报备的行程 -->
<div class="card-face card-front ${hasLie ? '' : 'active'}">
<div class="activity-title">${reportedTitle}</div>
${schedule.location ? `<div class="activity-location">📍 ${schedule.location}</div>` : ''}
${reportedDetail ? `<div class="activity-description">${reportedDetail}</div>` : ''}
</div>
<!-- 背面:真实活动(只有撒谎时才显示) -->
${hasLie ? `
<div class="card-face card-back">
<div class="truth-badge">🔍 真相</div>
<div class="activity-title truth-title">${truthActivity}</div>
${truthLocation ? `<div class="activity-location">📍 ${truthLocation}</div>` : ''}
${truthDetail ? `<div class="activity-description">${truthDetail}</div>` : ''}
<div class="lie-reason">
<span class="reason-label">💭 隐瞒原因:</span>
<span class="reason-text">${truthReason}</span>
</div>
</div>
` : ''}
`}
</div>
</div>
`;
}).join('');
},
// 翻转卡片
toggleCardFace(index) {
const item = document.querySelector(`.timeline-item[data-schedule-index="${index}"]`);
if (!item || !item.classList.contains('has-secret')) return;
const front = item.querySelector('.card-front');
const back = item.querySelector('.card-back');
if (front && back) {
front.classList.toggle('active');
back.classList.toggle('active');
item.classList.toggle('flipped');
}
},
// 生成新行程卡
async generateNewCard(contactId, date) {
// 使用本地日期而不是UTC日期
const targetDate = date || (() => {
const today = new Date();
const year = today.getFullYear();
const month = String(today.getMonth() + 1).padStart(2, '0');
const day = String(today.getDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
})();
const contact = this.getContact(contactId);
if (!contact) {
alert('联系人不存在');
return;
}
// 检查当天是否已经有行程卡
const existingCard = this.getTravelCardData(contactId, targetDate);
if (existingCard) {
const confirm = window.confirm('该日期已有行程卡,是否重新生成?');
if (!confirm) return;
}
// 显示加载状态
const container = document.getElementById('timelineContainer');
if (container) {
container.innerHTML = `
<div class="loading-state">
<div class="loading-spinner"></div>
<p>正在生成行程卡...</p>
<p class="loading-tip">请稍候,AI正在为您创建个性化行程</p>
</div>
`;
}
// 禁用生成按钮,防止重复点击
const generateButtons = document.querySelectorAll('button[onclick*="generateNewCard"]');
generateButtons.forEach(btn => {
btn.disabled = true;
btn.style.opacity = '0.6';
btn.style.cursor = 'not-allowed';
});
try {
// 调用AI生成行程卡
const schedule = await this.callAIForSchedule(contact, targetDate);
if (schedule && schedule.schedules) {
this.saveTravelCardData(contactId, targetDate, schedule);
if (window.AIAutoStatus && AIAutoStatus.statusManager) {
try {
AIAutoStatus.statusManager.checkAndUpdateStatus(contactId);
} catch (e) {
console.warn('生成行程卡后刷新AI状态失败:', e);
}
}
// 显示成功状态
if (container) {
container.innerHTML = `
<div class="loading-state success">
<div class="success-icon">✅</div>
<p>行程卡生成成功!</p>
<p class="loading-tip">正在加载内容...</p>
</div>
`;
}
// 延迟一下再加载,让用户看到成功提示
setTimeout(() => {
this.loadAllCards();
if (this.currentContactId === contactId) {
this.loadTimeline();
}
}, 800);
} else {
throw new Error('AI返回的数据格式不正确');
}
} catch (error) {
console.error('生成行程卡失败:', error);
// 失败后显示重试按钮
if (container) {
container.innerHTML = `
<div class="loading-state error">
<div class="error-icon">❌</div>
<p>生成失败: ${error.message}</p>
<button class="footer-btn retry-btn" onclick="TravelCard.generateNewCard('${contactId}', '${targetDate}')" style="margin: 20px auto; max-width: 200px;">
<span>🔄</span>
<span>重试</span>
</button>
</div>
`;
}
} finally {
// 恢复生成按钮
setTimeout(() => {
generateButtons.forEach(btn => {
btn.disabled = false;
btn.style.opacity = '1';
btn.style.cursor = 'pointer';
});
}, 1000);
}
},
// 调用AI生成行程卡
async callAIForSchedule(contact, date) {
let apiUrl, apiKey, model, temperature = null;
console.log('🔍 [行程卡] 当前联系人:', contact.name, 'API方案ID:', contact.apiScheme);
// 优先使用联系人的 API 方案
if (contact.apiScheme) {
const schemes = JSON.parse(localStorage.getItem('vibe_api_schemes') || '[]');
console.log('📋 [行程卡] 所有API方案:', schemes);
const scheme = schemes.find(s => s.id === contact.apiScheme);
if (scheme) {
apiUrl = scheme.apiUrl;
apiKey = scheme.apiKey;
model = scheme.model;
if (typeof scheme.temperature === 'number') {
temperature = scheme.temperature;
}
console.log('✅ [行程卡] 使用联系人API方案:', scheme.name, '模型:', model);
} else {
console.warn('⚠️ [行程卡] 联系人API方案不存在(ID:' + contact.apiScheme + ')');
}
}
// 如果没有方案,使用全局配置
if (!apiUrl) {
apiUrl = localStorage.getItem('apiUrl');
apiKey = localStorage.getItem('apiKey');
model = contact.model || localStorage.getItem('selectedModel');
console.log('🌐 [行程卡] 使用全局API配置');
}
if (temperature === null) {
const storedTemp = localStorage.getItem('apiTemperature');
const tempNum = storedTemp ? parseFloat(storedTemp) : NaN;
if (!isNaN(tempNum)) {
temperature = tempNum;
}
}
if (!apiUrl || !apiKey) {
throw new Error('未配置API,请先在设置中配置API或为联系人指定API方案');
}
// 构建提示词
const prompt = this.buildSchedulePrompt(contact, date);
console.log('📤 [行程卡] 发送API请求:', apiUrl, '模型:', model, '温度:', temperature ?? 0.8);
console.log('📝 [行程卡] 提示词:', prompt);
const response = await fetch(`${apiUrl}/chat/completions`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
},
body: JSON.stringify({
model: model,
messages: [
{
role: 'system',
content: '你是一个专业的日程规划助手,请根据角色设定生成合理的一天行程安排。'
},
{
role: 'user',
content: prompt
}
],
temperature: (typeof temperature === 'number') ? temperature : 0.8
})
});
if (!response.ok) {
const errorText = await response.text();
console.error('❌ [行程卡] API请求失败:', response.status, errorText);
throw new Error(`API 请求失败 (${response.status}): ${errorText.substring(0, 200)}`);
}
const data = await response.json();
console.log('📥 [行程卡] API返回数据:', data);
// 检查是否是错误响应
if (data.error) {
console.error('❌ [行程卡] API返回错误:', data.error);
throw new Error(`API错误: ${data.error.message || JSON.stringify(data.error)}`);
}
// 提取回复内容(支持多种API格式)
let reply = null;
if (data.choices && data.choices[0] && data.choices[0].message) {
reply = data.choices[0].message.content;
} else if (data.candidates && data.candidates[0] && data.candidates[0].content) {
const parts = data.candidates[0].content.parts;
if (parts && parts[0] && parts[0].text) {
reply = parts[0].text;
}
} else if (data.content && data.content[0] && data.content[0].text) {
reply = data.content[0].text;
} else if (data.text) {
reply = data.text;
} else if (data.response) {
reply = data.response;
}
if (!reply) {
console.error('❌ [行程卡] 无法识别的API返回格式:', JSON.stringify(data, null, 2));
throw new Error('API返回数据格式不支持');
}
console.log('✅ [行程卡] AI原始回复:', reply);
// 解析JSON
const schedule = this.parseScheduleFromAI(reply, contact, date);
return schedule;
},
// 构建行程卡生成提示词
buildSchedulePrompt(contact, date) {
const settings = this.getSettings();
const defaultPrompt = settings.defaultPrompt;
// 如果有自定义提示词,使用自定义提示词
if (defaultPrompt && defaultPrompt.trim()) {
return defaultPrompt
.replace(/{charName}/g, contact.name)
.replace(/{date}/g, date)
.replace(/{timezone}/g, contact.timezone || 'Asia/Shanghai');
}
// 默认提示词
const displayName = contact.nickname || contact.name;
// 获取联系人的时区(从timezoneSettings中)
const timezoneSettings = contact.timezoneSettings || { mode: 'same', sharedTimezone: 'Asia/Shanghai' };
const timezone = timezoneSettings.mode === 'same'
? timezoneSettings.sharedTimezone
: timezoneSettings.aiTimezone;
// 获取睡眠时间设置
const sleepSettings = contact.sleepSettings || {
bedtime: '23:30',
wakeupTime: '08:00'
};
const bilingualMode = contact.bilingualSettings?.enabled ? `(${contact.bilingualSettings.mode}双语模式)` : '';
return `请为角色"${contact.name}"${bilingualMode}生成${date}这一天的详细行程安排。
角色信息:
- 真实姓名:${contact.name}
- 备注名称:${displayName}
- 性别:${contact.gender || '未设置'}
- 初次见面语:${contact.greeting || '无'}
- 当前状态:${contact.status || '无'}
- 时区:${timezone}
- 睡觉时间:${sleepSettings.bedtime}
- 起床时间:${sleepSettings.wakeupTime}
${contact.persona ? `- 人设:${contact.persona}` : ''}
要求:
1. 生成一天的完整行程(从${sleepSettings.wakeupTime}起床到${sleepSettings.bedtime}睡觉)
2. 每个行程项必须包含:
- time: 开始时间
- endTime: 结束时间
- activity: 「大类标题」,用简短的中文/英文类别词,例如:起床、睡觉、工作、学习、用餐、会议、通勤、运动、休息、娱乐等
- location: 地点(可以简写)
- description: 「小类与细节」,用1-2句话具体描述这个时间段在做什么、氛围如何、有什么情绪或小动作
3. 行程要符合角色的性格和职业特点,可以在description里体现他/她的习惯和小细节
4. 所有时间统一使用 HH:mm(24小时制)
5. 必须返回有效的JSON格式,不能有注释或多余文本
6. 第一个行程应该是${sleepSettings.wakeupTime}起床,最后一个行程应该是${sleepSettings.bedtime}睡觉
7. 如果角色是夜猫子类型,可以在description里体现熬夜习惯,并适当增加夜间活动
返回格式示例:
\`\`\`json
{
"contactId": "${contact.id}",
"date": "${date}",
"timezone": "${timezone}",
"schedules": [
{
"id": "1",
"time": "${sleepSettings.wakeupTime}",
"endTime": "${this.addMinutes(sleepSettings.wakeupTime, 30)}",
"activity": "起床",
"location": "家",
"description": "迷迷糊糊醒来,在床上赖了几分钟才去洗漱。"
},
{
"id": "2",
"time": "09:00",
"endTime": "12:00",
"activity": "工作",
"location": "办公室",
"description": "处理邮件和需求评审,一边喝咖啡一边和同事小声吐槽今天的会议安排。"
},
{
"id": "last",
"time": "${sleepSettings.bedtime}",
"endTime": "${this.addMinutes(sleepSettings.bedtime, 30)}",
"activity": "睡觉",
"location": "家",
"description": "洗完澡上床刷了会儿手机,放下之后戴上眼罩慢慢入睡。"
}
]
}
\`\`\`
请直接返回JSON,不要添加任何其他说明文字。`;
},
// 从AI回复中解析行程JSON
parseScheduleFromAI(reply, contact, date) {
try {
// 尝试提取JSON(可能被包裹在```json```中)
let jsonStr = reply.trim();
// 移除markdown代码块标记
if (jsonStr.startsWith('```json')) {
jsonStr = jsonStr.substring(7);
} else if (jsonStr.startsWith('```')) {
jsonStr = jsonStr.substring(3);
}
if (jsonStr.endsWith('```')) {
jsonStr = jsonStr.substring(0, jsonStr.length - 3);
}
jsonStr = jsonStr.trim();
// 解析JSON
const schedule = JSON.parse(jsonStr);
// 验证数据结构
if (!schedule.schedules || !Array.isArray(schedule.schedules)) {
throw new Error('返回的JSON缺少schedules数组');
}
// 获取联系人的正确时区
const timezoneSettings = contact.timezoneSettings || { mode: 'same', sharedTimezone: 'Asia/Shanghai' };
const contactTimezone = timezoneSettings.mode === 'same'
? timezoneSettings.sharedTimezone
: timezoneSettings.aiTimezone;
// 确保必要字段存在
schedule.contactId = contact.id;
schedule.date = date;
schedule.timezone = contactTimezone; // 使用联系人的时区
console.log('🌍 [行程卡] 联系人时区:', contactTimezone, '时区设置:', timezoneSettings);
// 为每个schedule项添加默认值
schedule.schedules = schedule.schedules.map((item, index) => ({
id: item.id || String(index + 1),
time: item.time,
endTime: item.endTime || '',
activity: item.activity,
location: item.location || '',
description: item.description || '',
isVisible: item.isVisible !== false,
isTruthful: item.isTruthful !== false,
actualActivity: item.actualActivity || item.activity
}));
// 应用撒谎机制
schedule.schedules = this.applyLieMechanism(schedule.schedules, contact);
console.log('✅ [行程卡] 解析成功:', schedule);
return schedule;
} catch (error) {
console.error('❌ [行程卡] JSON解析失败:', error);
console.error('原始回复:', reply);
throw new Error('AI返回的内容不是有效的JSON格式,请重试');
}
},
// 应用撒谎机制
// 应用撒谎机制(优化版:控制频率和质量)
applyLieMechanism(schedules, contact) {
// 撒谎频率:0个(60%)、1个(30%)、2个(10%)
const rand = Math.random();
let lieCount = 0;
if (rand < 0.6) {
lieCount = 0; // 60%概率不撒谎
} else if (rand < 0.9) {
lieCount = 1; // 30%概率撒1个谎
} else {
lieCount = 2; // 10%概率撒2个谎
}
console.log(`🎲 [撒谎机制] 今日撒谎次数: ${lieCount}`);
if (lieCount === 0) {
// 不撒谎,所有行程都是真实的
return schedules.map(s => ({
...s,
isLiar: false,
reported: {
title: s.activity,
detail: s.description
},
truth: null
}));
}
// 随机选择要撒谎的行程(避开早晨和深夜)
const candidateIndices = schedules
.map((s, i) => ({ schedule: s, index: i }))
.filter(({ schedule }) => {
const hour = parseInt(schedule.time.split(':')[0]);
return hour >= 10 && hour <= 22; // 只在10:00-22:00之间撒谎
})
.map(({ index }) => index);
// 随机选择要撒谎的索引
const lieIndices = [];
while (lieIndices.length < Math.min(lieCount, candidateIndices.length)) {
const randomIndex = candidateIndices[Math.floor(Math.random() * candidateIndices.length)];
if (!lieIndices.includes(randomIndex)) {
lieIndices.push(randomIndex);
}
}
console.log(`🤥 [撒谎机制] 撒谎的行程索引:`, lieIndices);
// 生成掩饰故事
const coverStories = [
{ activity: '在图书馆学习', reason: '想给你个惊喜' },
{ activity: '在实验室做实验', reason: '不想让你担心' },
{ activity: '在咖啡厅工作', reason: '想独自处理一些事' },
{ activity: '在家休息', reason: '需要一些私人空间' },
{ activity: '外出办事', reason: '想给你准备礼物' },
{ activity: '整理房间', reason: '计划给你一个惊喜' },
{ activity: '散步', reason: '想一个人静静' },
{ activity: '看书', reason: '在准备给你的惊喜' }
];
return schedules.map((schedule, index) => {
if (lieIndices.includes(index)) {
const cover = coverStories[Math.floor(Math.random() * coverStories.length)];
console.log(`🤥 [撒谎机制] ${schedule.time} - 真实: ${schedule.activity}, 报备: ${cover.activity}`);
return {
...schedule,
isLiar: true,
reported: {
title: cover.activity,
detail: `${cover.activity},稍后联系你`
},
truth: {
activity: schedule.activity,
location: schedule.location,
detail: schedule.description,
reason: cover.reason
}
};
} else {
return {
...schedule,
isLiar: false,
reported: {
title: schedule.activity,
detail: schedule.description
},
truth: null
};
}
});
}
,
// 生成掩饰故事
generateCoverStory(actualActivity, contact) {
// 简单的掩饰故事生成逻辑
const coverStories = [
'在家休息',
'处理一些私人事务',
'外出办事',
'和朋友见面',
'在图书馆学习',
'去咖啡厅放松',
'整理房间',
'看书',
'散步',
'购物'
];
// 随机选择一个掩饰故事
const randomIndex = Math.floor(Math.random() * coverStories.length);
return coverStories[randomIndex];
},
// 上一天
previousDate() {
const date = new Date(this.currentDate);
date.setDate(date.getDate() - 1);
// 使用本地日期而不是UTC日期
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
this.currentDate = `${year}-${month}-${day}`;
document.getElementById('modalCurrentDate').textContent = this.currentDate;
// 更新陈旧化效果
const modal = document.getElementById('travelCardModal');
const modalContent = modal.querySelector('.modal-content');
if (this.isCardOld(this.currentDate)) {
modalContent.classList.add('card-old');
} else {
modalContent.classList.remove('card-old');
}
this.loadTimeline();
},
// 下一天
nextDate() {
const date = new Date(this.currentDate);
date.setDate(date.getDate() + 1);
// 使用本地日期而不是UTC日期
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
this.currentDate = `${year}-${month}-${day}`;
document.getElementById('modalCurrentDate').textContent = this.currentDate;
// 更新陈旧化效果
const modal = document.getElementById('travelCardModal');
const modalContent = modal.querySelector('.modal-content');
if (this.isCardOld(this.currentDate)) {
modalContent.classList.add('card-old');
} else {
modalContent.classList.remove('card-old');
}
this.loadTimeline();
},
// 重新生成行程卡
regenerateCard() {
if (!this.currentContactId || !this.currentDate) return;
this.generateNewCard(this.currentContactId, this.currentDate);
},
// 固化记忆
async consolidateMemory(event) {
if (!this.currentContactId || !this.currentDate) {
alert('请先打开一个行程卡');
return;
}
const contact = this.getContact(this.currentContactId);
if (!contact) {
alert('联系人不存在');
return;
}
// 检查是否已经固化过
const existingMemory = this.getMemory(this.currentContactId, this.currentDate);
if (existingMemory) {
const confirm = window.confirm('该日期已有固化记忆,是否重新生成?');
if (!confirm) return;
}
// 获取行程卡数据
const travelCardData = this.getTravelCardData(this.currentContactId, this.currentDate);
if (!travelCardData) {
alert('该日期没有行程卡数据');
return;
}
// 创建全屏加载遮罩
const loadingOverlay = document.createElement('div');
loadingOverlay.className = 'memory-loading-overlay';
loadingOverlay.innerHTML = `
<div class="memory-loading-content">
<div class="loading-spinner"></div>
<h3>正在固化记忆</h3>
<p>AI正在分析行程数据并生成记忆摘要...</p>
<div class="loading-progress">
<div class="progress-bar">
<div class="progress-fill"></div>
</div>
<p class="progress-text">请稍候,这可能需要几秒钟</p>
</div>
</div>
`;
document.body.appendChild(loadingOverlay);
// 启动进度条动画
setTimeout(() => {
const progressFill = loadingOverlay.querySelector('.progress-fill');
if (progressFill) {
progressFill.style.width = '100%';
}
}, 100);
try {
// 显示按钮加载状态(如果有按钮)
let originalText;
if (event && event.target) {
originalText = event.target.textContent;
event.target.textContent = '固化中...';
event.target.disabled = true;
}
// 调用AI进行脱水处理
const memory = await this.dehydrateMemory(contact, this.currentDate, travelCardData);