@aiyiran/myclaw 1.1.205 → 1.1.207

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -52,10 +52,58 @@
52
52
  border-color: #667eea;
53
53
  outline: none;
54
54
  }
55
+ .download-button {
56
+ border: 0;
57
+ border-radius: 8px;
58
+ padding: 9px 14px;
59
+ color: white;
60
+ background: #0f766e;
61
+ font-size: 13px;
62
+ font-weight: 700;
63
+ cursor: pointer;
64
+ white-space: nowrap;
65
+ box-shadow: 0 2px 6px rgba(15, 118, 110, .24);
66
+ }
67
+ .download-button:hover { background: #115e59; }
68
+ .download-button:focus-visible { outline: 3px solid #99f6e4; outline-offset: 2px; }
69
+ .download-button:disabled { cursor: not-allowed; opacity: .55; box-shadow: none; }
55
70
  .session-meta {
56
71
  font-size: 12px;
57
72
  color: #888;
58
73
  }
74
+ .session-legend {
75
+ background: white;
76
+ border-radius: 12px;
77
+ padding: 14px 18px;
78
+ margin-bottom: 20px;
79
+ box-shadow: 0 2px 8px rgba(0,0,0,0.08);
80
+ }
81
+ .legend-title {
82
+ font-size: 13px;
83
+ font-weight: 700;
84
+ color: #394150;
85
+ margin-bottom: 10px;
86
+ }
87
+ .legend-buttons { display: flex; gap: 8px; flex-wrap: wrap; }
88
+ .legend-button {
89
+ appearance: none;
90
+ border: 1px solid #d8dee8;
91
+ background: #fff;
92
+ color: #4c5667;
93
+ border-radius: 999px;
94
+ padding: 7px 12px;
95
+ font-size: 12px;
96
+ font-weight: 650;
97
+ cursor: pointer;
98
+ display: inline-flex;
99
+ align-items: center;
100
+ gap: 7px;
101
+ transition: box-shadow .15s ease, transform .15s ease, border-color .15s ease;
102
+ }
103
+ .legend-button:hover { transform: translateY(-1px); box-shadow: 0 3px 8px rgba(0,0,0,.1); }
104
+ .legend-button.active { border-width: 2px; padding: 6px 11px; box-shadow: 0 0 0 2px rgba(0,0,0,.04); }
105
+ .legend-dot { width: 10px; height: 10px; border-radius: 50%; flex: 0 0 auto; }
106
+ .legend-count { opacity: .7; font-weight: 500; }
59
107
  .info-box {
60
108
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
61
109
  color: white;
@@ -97,7 +145,7 @@
97
145
  flex-wrap: wrap;
98
146
  gap: 10px;
99
147
  }
100
- .pair-left { display: flex; align-items: center; gap: 12px; }
148
+ .pair-left { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
101
149
  .pair-num {
102
150
  background: #4CAF50;
103
151
  color: white;
@@ -106,6 +154,15 @@
106
154
  font-weight: bold;
107
155
  font-size: 13px;
108
156
  }
157
+ .source-session {
158
+ background: #eef1f5;
159
+ color: #5f6b7a;
160
+ border: 1px solid #d8dee8;
161
+ padding: 4px 10px;
162
+ border-radius: 20px;
163
+ font-size: 12px;
164
+ font-weight: 600;
165
+ }
109
166
  .pair-duration {
110
167
  padding: 6px 14px;
111
168
  border-radius: 20px;
@@ -151,15 +208,21 @@
151
208
  <h1>📋 聊天记录</h1>
152
209
 
153
210
  <div class="session-selector" id="sessionSelector" style="display:none;">
154
- <label>🔍 切换会话:</label>
211
+ <label>🔍 切换工作空间:</label>
155
212
  <select id="sessionSelect"></select>
156
213
  <span class="session-meta" id="sessionMeta"></span>
214
+ <button type="button" class="download-button" id="downloadMarkdown">⬇ 下载当前视图 MD</button>
215
+ </div>
216
+
217
+ <div class="session-legend" id="sessionLegend" style="display:none;">
218
+ <div class="legend-title">会话来源(点击可筛选)</div>
219
+ <div class="legend-buttons" id="legendButtons"></div>
157
220
  </div>
158
221
 
159
222
  <div class="info-box">
160
- <p><strong>会话ID:</strong> <span id="sessionId">--</span></p>
161
- <p><strong>会话名称:</strong> <span id="sessionName">--</span></p>
162
- <p><strong>发起人:</strong> <span id="initiator">--</span></p>
223
+ <p><strong>工作空间:</strong> <span id="sessionId">--</span></p>
224
+ <p><strong>包含会话:</strong> <span id="sessionName">--</span></p>
225
+ <p><strong>用户时间范围:</strong> <span id="initiator">--</span></p>
163
226
  <div class="stats">
164
227
  <div class="stat-item">
165
228
  <div class="stat-num" id="totalPairs">--</div>
@@ -183,10 +246,24 @@
183
246
  <div id="conversationList"></div>
184
247
  </div>
185
248
 
186
- <!-- Load index.js (contains chatIndex + chatAll) -->
249
+ <!-- Load index.js (contains workspace timelines and original session data) -->
187
250
  <script src="index.js"></script>
188
251
  <script>
189
252
  var currentSession = null;
253
+ var currentChatData = null;
254
+ var activeSessionFilter = null;
255
+ var sessionVisualMap = {};
256
+
257
+ var SESSION_PALETTE = [
258
+ { border: '#2563eb', soft: '#eff6ff', text: '#1e40af' },
259
+ { border: '#16a34a', soft: '#f0fdf4', text: '#166534' },
260
+ { border: '#9333ea', soft: '#faf5ff', text: '#6b21a8' },
261
+ { border: '#ea580c', soft: '#fff7ed', text: '#9a3412' },
262
+ { border: '#0891b2', soft: '#ecfeff', text: '#155e75' },
263
+ { border: '#db2777', soft: '#fdf2f8', text: '#9d174d' },
264
+ { border: '#4f46e5', soft: '#eef2ff', text: '#3730a3' },
265
+ { border: '#ca8a04', soft: '#fefce8', text: '#854d0e' }
266
+ ];
190
267
 
191
268
  // parseTime, formatDuration, escapeHtml
192
269
  function parseTime(timeStr) {
@@ -198,6 +275,15 @@
198
275
  return null;
199
276
  }
200
277
 
278
+ function parseConversationTime(conversation, prefix) {
279
+ var raw = conversation[prefix + '_timestamp'];
280
+ if (raw) {
281
+ var parsed = new Date(raw);
282
+ if (!isNaN(parsed.getTime())) return parsed;
283
+ }
284
+ return parseTime(conversation[prefix + '_time']);
285
+ }
286
+
201
287
  function formatDuration(ms) {
202
288
  if (ms === null || ms === undefined || ms < 0) return '--';
203
289
  var seconds = Math.floor(ms / 1000);
@@ -214,11 +300,400 @@
214
300
 
215
301
  function escapeHtml(text) {
216
302
  var div = document.createElement('div');
217
- div.textContent = text;
303
+ div.textContent = text || '';
218
304
  return div.innerHTML;
219
305
  }
220
306
 
221
- // Build dropdown from chatIndex
307
+ function markdownCell(value) {
308
+ return String(value === null || value === undefined ? '' : value)
309
+ .replace(/\\/g, '\\\\')
310
+ .replace(/\|/g, '\\|')
311
+ .replace(/\r?\n/g, '<br>');
312
+ }
313
+
314
+ function markdownQuote(value) {
315
+ var text = String(value === null || value === undefined ? '' : value)
316
+ .replace(/\r\n?/g, '\n');
317
+ if (!text.trim()) return '> _无文本内容_';
318
+ return text.split('\n').map(function(line) {
319
+ return line ? '> ' + line : '>';
320
+ }).join('\n');
321
+ }
322
+
323
+ function formatLocalDateTime(date) {
324
+ function pad(value) { return String(value).padStart(2, '0'); }
325
+ return date.getFullYear() + '-' + pad(date.getMonth() + 1) + '-' + pad(date.getDate()) +
326
+ ' ' + pad(date.getHours()) + ':' + pad(date.getMinutes()) + ':' + pad(date.getSeconds());
327
+ }
328
+
329
+ function getSessionMeta(chatData, sessionId) {
330
+ var source = (chatData.session_sources || []).find(function(item) {
331
+ return item.session_id === sessionId;
332
+ });
333
+ var turn = (chatData.conversations || []).find(function(item) {
334
+ return item.source_session_id === sessionId;
335
+ });
336
+ var label = (turn && (turn.source_session_label || turn.source_session_name)) ||
337
+ (source && (source.session_label || source.session_name)) ||
338
+ (sessionId ? sessionId.split(':').pop() : '') || '未命名会话';
339
+ return { id: sessionId || '', label: label };
340
+ }
341
+
342
+ function getExportScope(chatData) {
343
+ if (!activeSessionFilter) return '全部会话';
344
+ return getSessionMeta(chatData, activeSessionFilter).label;
345
+ }
346
+
347
+ function getCurrentConversations() {
348
+ if (!currentChatData) return [];
349
+ var conversations = currentChatData.conversations || [];
350
+ if (!activeSessionFilter) return conversations.slice();
351
+ return conversations.filter(function(turn) {
352
+ return turn.source_session_id === activeSessionFilter;
353
+ });
354
+ }
355
+
356
+ function median(values) {
357
+ if (!values.length) return null;
358
+ var sorted = values.slice().sort(function(a, b) { return a - b; });
359
+ var middle = Math.floor(sorted.length / 2);
360
+ return sorted.length % 2 ? sorted[middle] : (sorted[middle - 1] + sorted[middle]) / 2;
361
+ }
362
+
363
+ function buildMarkdown(chatData, conversations) {
364
+ conversations = conversations || [];
365
+ var workspaceId = chatData.workspace_name || chatData.workspace_id || chatData.session_id || 'unknown';
366
+ var workspaceLabel = chatData.workspace_label || workspaceId;
367
+ var workspaceTitle = workspaceLabel === workspaceId ? workspaceLabel : workspaceLabel + '(' + workspaceId + ')';
368
+ var scope = getExportScope(chatData);
369
+ var replyCount = 0;
370
+ var userChars = 0;
371
+ var aiChars = 0;
372
+ var responseTimes = [];
373
+ var userDates = [];
374
+ var sessionStats = {};
375
+ var dailyStats = {};
376
+
377
+ (chatData.session_sources || []).forEach(function(source) {
378
+ var sessionId = source.session_id || '';
379
+ if (!sessionId || (activeSessionFilter && sessionId !== activeSessionFilter)) return;
380
+ var meta = getSessionMeta(chatData, sessionId);
381
+ sessionStats[sessionId] = {
382
+ id: sessionId, label: meta.label, turns: 0, replies: 0,
383
+ first: '', last: '', firstDate: null, lastDate: null
384
+ };
385
+ });
386
+
387
+ conversations.forEach(function(turn) {
388
+ var userText = String(turn.user || '');
389
+ var aiText = String(turn.ai || '');
390
+ var hasReply = Boolean(aiText.trim());
391
+ var userDate = parseConversationTime(turn, 'user');
392
+ var aiDate = parseConversationTime(turn, 'ai');
393
+ var displayTime = turn.user_time || '';
394
+ var day = displayTime.slice(0, 10) || (userDate ? formatLocalDateTime(userDate).slice(0, 10) : '未知日期');
395
+ var sessionId = turn.source_session_id || chatData.session_id || 'unknown';
396
+ var meta = getSessionMeta(chatData, sessionId);
397
+
398
+ userChars += userText.length;
399
+ aiChars += aiText.length;
400
+ if (hasReply) replyCount++;
401
+ if (userDate) userDates.push(userDate);
402
+ if (hasReply && userDate && aiDate) {
403
+ var responseMs = aiDate - userDate;
404
+ if (responseMs >= 0) responseTimes.push(responseMs);
405
+ }
406
+
407
+ if (!sessionStats[sessionId]) {
408
+ sessionStats[sessionId] = {
409
+ id: sessionId, label: meta.label, turns: 0, replies: 0,
410
+ first: '', last: '', firstDate: null, lastDate: null
411
+ };
412
+ }
413
+ var session = sessionStats[sessionId];
414
+ session.turns++;
415
+ if (hasReply) session.replies++;
416
+ if (userDate && (!session.firstDate || userDate < session.firstDate)) {
417
+ session.firstDate = userDate;
418
+ session.first = displayTime || formatLocalDateTime(userDate);
419
+ }
420
+ if (userDate && (!session.lastDate || userDate > session.lastDate)) {
421
+ session.lastDate = userDate;
422
+ session.last = displayTime || formatLocalDateTime(userDate);
423
+ }
424
+
425
+ if (!dailyStats[day]) {
426
+ dailyStats[day] = { turns: 0, replies: 0, sessions: {}, first: '', last: '' };
427
+ }
428
+ var daily = dailyStats[day];
429
+ daily.turns++;
430
+ if (hasReply) daily.replies++;
431
+ daily.sessions[sessionId] = true;
432
+ if (displayTime && (!daily.first || displayTime < daily.first)) daily.first = displayTime;
433
+ if (displayTime && (!daily.last || displayTime > daily.last)) daily.last = displayTime;
434
+ });
435
+
436
+ userDates.sort(function(a, b) { return a - b; });
437
+ var firstTime = conversations.length ? conversations[0].user_time || '' : '';
438
+ var lastTime = conversations.length ? conversations[conversations.length - 1].user_time || '' : '';
439
+ if (userDates.length) {
440
+ if (!firstTime) firstTime = formatLocalDateTime(userDates[0]);
441
+ if (!lastTime) lastTime = formatLocalDateTime(userDates[userDates.length - 1]);
442
+ }
443
+ var activeDays = Object.keys(dailyStats).filter(function(day) { return day !== '未知日期'; }).length;
444
+ var spanDays = userDates.length > 1 ? Math.floor((userDates[userDates.length - 1] - userDates[0]) / 86400000) + 1 : (userDates.length ? 1 : 0);
445
+ var sessions = Object.keys(sessionStats).map(function(id) { return sessionStats[id]; });
446
+ sessions.sort(function(a, b) {
447
+ if (!a.firstDate && !b.firstDate) return a.label.localeCompare(b.label);
448
+ if (!a.firstDate) return 1;
449
+ if (!b.firstDate) return -1;
450
+ return a.firstDate - b.firstDate;
451
+ });
452
+
453
+ var lines = [];
454
+ lines.push('# ' + workspaceTitle + '聊天记录');
455
+ lines.push('');
456
+ lines.push('> 本文件按用户消息时间合并排序,适合直接交给 AI 做内容分析。用户和 AI 原文均以引用块包裹,避免原文中的 Markdown 结构干扰文档层级。');
457
+ lines.push('');
458
+ lines.push('## 总体汇总');
459
+ lines.push('');
460
+ lines.push('| 指标 | 数值 |');
461
+ lines.push('| --- | --- |');
462
+ lines.push('| Workspace 名称 | ' + markdownCell(workspaceLabel) + ' |');
463
+ lines.push('| Workspace ID | `' + markdownCell(workspaceId) + '` |');
464
+ lines.push('| 导出范围 | ' + markdownCell(scope) + ' |');
465
+ lines.push('| Session 数 | ' + sessions.length + ' |');
466
+ lines.push('| 对话轮数 | ' + conversations.length + ' |');
467
+ lines.push('| AI 回复数 | ' + replyCount + ' |');
468
+ lines.push('| 未回复轮数 | ' + (conversations.length - replyCount) + ' |');
469
+ lines.push('| 首条用户消息 | ' + markdownCell(firstTime || '未知') + ' |');
470
+ lines.push('| 末条用户消息 | ' + markdownCell(lastTime || '未知') + ' |');
471
+ lines.push('| 活跃日期数 | ' + activeDays + ' |');
472
+ lines.push('| 日历跨度 | ' + spanDays + ' 天 |');
473
+ lines.push('| 导出时间 | ' + formatLocalDateTime(new Date()) + ' |');
474
+ lines.push('');
475
+ lines.push('## 会话概览');
476
+ lines.push('');
477
+ lines.push('| # | Session 名称 | Session ID | 对话数 | AI 回复 | 未回复 | 起始 | 最后 |');
478
+ lines.push('| ---: | --- | --- | ---: | ---: | ---: | --- | --- |');
479
+ sessions.forEach(function(session, index) {
480
+ lines.push('| ' + (index + 1) + ' | ' + markdownCell(session.label) + ' | `' + markdownCell(session.id) + '` | ' +
481
+ session.turns + ' | ' + session.replies + ' | ' + (session.turns - session.replies) + ' | ' +
482
+ markdownCell(session.first || '未知') + ' | ' + markdownCell(session.last || '未知') + ' |');
483
+ });
484
+ if (!sessions.length) lines.push('| - | 无数据 | - | 0 | 0 | 0 | - | - |');
485
+
486
+ lines.push('');
487
+ lines.push('## 每日对话分布');
488
+ lines.push('');
489
+ lines.push('| 日期 | 对话数 | AI 回复 | 未回复 | 活跃 Session | 当日首条 | 当日末条 |');
490
+ lines.push('| --- | ---: | ---: | ---: | ---: | --- | --- |');
491
+ Object.keys(dailyStats).sort().forEach(function(day) {
492
+ var daily = dailyStats[day];
493
+ lines.push('| ' + markdownCell(day) + ' | ' + daily.turns + ' | ' + daily.replies + ' | ' +
494
+ (daily.turns - daily.replies) + ' | ' + Object.keys(daily.sessions).length + ' | ' +
495
+ markdownCell(daily.first ? daily.first.slice(11) : '未知') + ' | ' +
496
+ markdownCell(daily.last ? daily.last.slice(11) : '未知') + ' |');
497
+ });
498
+ if (!Object.keys(dailyStats).length) lines.push('| - | 0 | 0 | 0 | 0 | - | - |');
499
+
500
+ lines.push('');
501
+ lines.push('## 响应时间汇总');
502
+ lines.push('');
503
+ lines.push('| 指标 | 数值 |');
504
+ lines.push('| --- | --- |');
505
+ lines.push('| 可计算样本 | ' + responseTimes.length + ' |');
506
+ lines.push('| 平均回复耗时 | ' + (responseTimes.length ? formatDuration(responseTimes.reduce(function(a, b) { return a + b; }, 0) / responseTimes.length) : '--') + ' |');
507
+ lines.push('| 中位回复耗时 | ' + (responseTimes.length ? formatDuration(median(responseTimes)) : '--') + ' |');
508
+ lines.push('| 最短回复耗时 | ' + (responseTimes.length ? formatDuration(Math.min.apply(null, responseTimes)) : '--') + ' |');
509
+ lines.push('| 最长回复耗时 | ' + (responseTimes.length ? formatDuration(Math.max.apply(null, responseTimes)) : '--') + ' |');
510
+
511
+ lines.push('');
512
+ lines.push('## 内容规模');
513
+ lines.push('');
514
+ lines.push('| 指标 | 数值 |');
515
+ lines.push('| --- | ---: |');
516
+ lines.push('| 用户总字符数 | ' + userChars + ' |');
517
+ lines.push('| AI 总字符数 | ' + aiChars + ' |');
518
+ lines.push('| 用户平均每轮字符数 | ' + (conversations.length ? Math.round(userChars / conversations.length) : 0) + ' |');
519
+ lines.push('| AI 平均每次回复字符数 | ' + (replyCount ? Math.round(aiChars / replyCount) : 0) + ' |');
520
+
521
+ lines.push('');
522
+ lines.push('# 对话正文');
523
+ lines.push('');
524
+ conversations.forEach(function(turn, index) {
525
+ var sessionId = turn.source_session_id || chatData.session_id || 'unknown';
526
+ var meta = getSessionMeta(chatData, sessionId);
527
+ var mergedIndex = turn.workspace_turn_index || (index + 1);
528
+ var originalIndex = turn.source_turn_index || mergedIndex;
529
+ lines.push('## 第 ' + mergedIndex + ' 轮 · ' + markdownCell(turn.user_time || '未知时间'));
530
+ lines.push('');
531
+ lines.push('- Session:' + markdownCell(meta.label));
532
+ lines.push('- Session ID:`' + markdownCell(sessionId) + '`');
533
+ lines.push('- 原 Session 轮次:第 ' + originalIndex + ' 轮');
534
+ if (turn.user_timestamp) lines.push('- 用户原始时间戳:`' + markdownCell(turn.user_timestamp) + '`');
535
+ lines.push('');
536
+ lines.push('### 用户');
537
+ lines.push('');
538
+ lines.push(markdownQuote(turn.user));
539
+ lines.push('');
540
+ lines.push('### AI' + (turn.ai_time ? ' · ' + markdownCell(turn.ai_time) : ''));
541
+ lines.push('');
542
+ lines.push(markdownQuote(turn.ai));
543
+ if (turn.ai_timestamp) {
544
+ lines.push('');
545
+ lines.push('_AI 原始时间戳:`' + markdownCell(turn.ai_timestamp) + '`_');
546
+ }
547
+ lines.push('');
548
+ lines.push('---');
549
+ lines.push('');
550
+ });
551
+ return lines.join('\n');
552
+ }
553
+
554
+ function sanitizeFilename(value) {
555
+ return String(value || '聊天记录')
556
+ .replace(/[\\/:*?"<>|]+/g, '_')
557
+ .replace(/\s+/g, '_')
558
+ .replace(/^_+|_+$/g, '')
559
+ .slice(0, 100) || '聊天记录';
560
+ }
561
+
562
+ function updateDownloadButton(chatData, conversations) {
563
+ var button = document.getElementById('downloadMarkdown');
564
+ if (!button) return;
565
+ button.disabled = !chatData;
566
+ button.textContent = '⬇ 下载' + getExportScope(chatData) + ' · ' + conversations.length + '轮 MD';
567
+ }
568
+
569
+ function downloadCurrentMarkdown() {
570
+ if (!currentChatData) return;
571
+ var conversations = getCurrentConversations();
572
+ var markdown = buildMarkdown(currentChatData, conversations);
573
+ var workspaceName = currentChatData.workspace_label || currentChatData.workspace_name || currentChatData.session_id || '聊天记录';
574
+ var filename = sanitizeFilename(workspaceName + '_' + getExportScope(currentChatData) + '_' + conversations.length + '轮') + '.md';
575
+ var blob = new Blob(['\uFEFF' + markdown], { type: 'text/markdown;charset=utf-8' });
576
+ var url = URL.createObjectURL(blob);
577
+ var anchor = document.createElement('a');
578
+ anchor.href = url;
579
+ anchor.download = filename;
580
+ document.body.appendChild(anchor);
581
+ anchor.click();
582
+ anchor.remove();
583
+ setTimeout(function() { URL.revokeObjectURL(url); }, 1000);
584
+ }
585
+
586
+ function stableHash(text) {
587
+ var hash = 2166136261;
588
+ for (var i = 0; i < text.length; i++) {
589
+ hash ^= text.charCodeAt(i);
590
+ hash = Math.imul(hash, 16777619);
591
+ }
592
+ return hash >>> 0;
593
+ }
594
+
595
+ function buildSessionVisualMap(chatData) {
596
+ var ids = (chatData.session_ids || []).slice().sort();
597
+ if (ids.length === 0) {
598
+ (chatData.conversations || []).forEach(function(turn) {
599
+ if (turn.source_session_id && ids.indexOf(turn.source_session_id) === -1) {
600
+ ids.push(turn.source_session_id);
601
+ }
602
+ });
603
+ ids.sort();
604
+ }
605
+
606
+ var visualMap = {};
607
+ var usedColors = {};
608
+ ids.forEach(function(sessionId, index) {
609
+ var colorIndex = stableHash(sessionId) % SESSION_PALETTE.length;
610
+ if (ids.length <= SESSION_PALETTE.length) {
611
+ while (usedColors[colorIndex]) {
612
+ colorIndex = (colorIndex + 1) % SESSION_PALETTE.length;
613
+ }
614
+ }
615
+ usedColors[colorIndex] = true;
616
+ var palette = SESSION_PALETTE[colorIndex];
617
+ var shortId = sessionId.split(':').pop().slice(0, 8);
618
+ visualMap[sessionId] = {
619
+ label: '会话 ' + String.fromCharCode(65 + (index % 26)),
620
+ shortId: shortId,
621
+ border: palette.border,
622
+ soft: palette.soft,
623
+ text: palette.text
624
+ };
625
+ });
626
+ return visualMap;
627
+ }
628
+
629
+ function buildSessionLegend(chatData) {
630
+ var legend = document.getElementById('sessionLegend');
631
+ var buttons = document.getElementById('legendButtons');
632
+ var isWorkspace = chatData.session_count !== undefined;
633
+ if (!isWorkspace) {
634
+ legend.style.display = 'none';
635
+ buttons.innerHTML = '';
636
+ return;
637
+ }
638
+
639
+ sessionVisualMap = buildSessionVisualMap(chatData);
640
+ var counts = {};
641
+ (chatData.conversations || []).forEach(function(turn) {
642
+ var sourceId = turn.source_session_id || '';
643
+ counts[sourceId] = (counts[sourceId] || 0) + 1;
644
+ });
645
+ buttons.innerHTML = '';
646
+ legend.style.display = 'block';
647
+
648
+ function addButton(sessionId, label, count, visual) {
649
+ var button = document.createElement('button');
650
+ button.type = 'button';
651
+ button.className = 'legend-button' + (activeSessionFilter === sessionId ? ' active' : '');
652
+ button.style.borderColor = visual.border;
653
+ button.style.background = activeSessionFilter === sessionId ? visual.soft : '#fff';
654
+ button.style.color = visual.text;
655
+ button.title = sessionId || '显示全部会话';
656
+
657
+ var dot = document.createElement('span');
658
+ dot.className = 'legend-dot';
659
+ dot.style.background = visual.border;
660
+ var text = document.createElement('span');
661
+ text.textContent = label;
662
+ var countEl = document.createElement('span');
663
+ countEl.className = 'legend-count';
664
+ countEl.textContent = count + '轮';
665
+ button.appendChild(dot);
666
+ button.appendChild(text);
667
+ button.appendChild(countEl);
668
+ button.addEventListener('click', function() {
669
+ activeSessionFilter = sessionId;
670
+ renderSession(currentChatData);
671
+ });
672
+ buttons.appendChild(button);
673
+ }
674
+
675
+ addButton(null, '全部会话', (chatData.conversations || []).length, {
676
+ border: '#64748b', soft: '#f8fafc', text: '#334155'
677
+ });
678
+ Object.keys(sessionVisualMap).sort().forEach(function(sessionId) {
679
+ var visual = sessionVisualMap[sessionId];
680
+ var sessionLabel = '';
681
+ var sourceTurn = (chatData.conversations || []).find(function(turn) {
682
+ return turn.source_session_id === sessionId;
683
+ });
684
+ if (sourceTurn) sessionLabel = sourceTurn.source_session_label || sourceTurn.source_session_name || '';
685
+ if (!sessionLabel) {
686
+ var sourceMeta = (chatData.session_sources || []).find(function(source) {
687
+ return source.session_id === sessionId;
688
+ });
689
+ if (sourceMeta) sessionLabel = sourceMeta.session_label || sourceMeta.session_name || '';
690
+ }
691
+ var label = visual.label + (sessionLabel ? ' · ' + sessionLabel : '') + ' · ' + visual.shortId;
692
+ addButton(sessionId, label, counts[sessionId] || 0, visual);
693
+ });
694
+ }
695
+
696
+ // Build dropdown from the workspace index (with old session-index fallback).
222
697
  function buildSelector() {
223
698
  if (typeof chatIndex === 'undefined' || !chatIndex || chatIndex.length === 0) {
224
699
  return; // No index data, hide selector
@@ -229,8 +704,16 @@
229
704
 
230
705
  chatIndex.forEach(function(item, i) {
231
706
  var opt = document.createElement('option');
232
- opt.value = item.session_id;
233
- var label = item.workspace_name + ' / ' + item.session_name + ' (' + item.total_pairs + '对)';
707
+ var workspaceMode = typeof chatWorkspaces !== 'undefined' && chatWorkspaces;
708
+ opt.value = workspaceMode ? item.workspace_id : item.session_id;
709
+ var label;
710
+ if (workspaceMode) {
711
+ var workspaceDisplay = item.workspace_label ? item.workspace_label + ' (' + item.workspace_name + ')' : item.workspace_name;
712
+ label = workspaceDisplay + ' · ' + item.session_count + '个会话 · ' + item.total_pairs + '对';
713
+ } else {
714
+ var sessionDisplay = item.session_label || item.session_name;
715
+ label = item.workspace_name + ' / ' + sessionDisplay + ' (' + item.total_pairs + '对)';
716
+ }
234
717
  if (item.first_time) label += ' [' + item.first_time + ']';
235
718
  opt.textContent = label;
236
719
  select.appendChild(opt);
@@ -241,17 +724,23 @@
241
724
  });
242
725
  }
243
726
 
244
- // Switch to a specific session
245
- function switchTo(sessionId) {
246
- if (typeof chatAll === 'undefined' || !chatAll) return;
247
-
248
- var data = chatAll[sessionId];
727
+ // Switch to a workspace timeline, or to a session for old-format data.
728
+ function switchTo(dataId) {
729
+ var data = null;
730
+ if (typeof chatWorkspaces !== 'undefined' && chatWorkspaces) {
731
+ data = chatWorkspaces[dataId];
732
+ }
733
+ if (!data && typeof chatAll !== 'undefined' && chatAll) {
734
+ data = chatAll[dataId];
735
+ }
249
736
  if (!data) {
250
- document.getElementById('conversationList').innerHTML = '<p style="text-align:center;color:#999;">该会话数据未找到</p>';
737
+ document.getElementById('conversationList').innerHTML = '<p style="text-align:center;color:#999;">该工作空间数据未找到</p>';
251
738
  return;
252
739
  }
253
740
 
254
- currentSession = sessionId;
741
+ currentSession = dataId;
742
+ currentChatData = data;
743
+ activeSessionFilter = null;
255
744
  renderSession(data);
256
745
 
257
746
  // Update meta
@@ -261,28 +750,41 @@
261
750
  }
262
751
  }
263
752
 
264
- // Render a session's conversations
753
+ // Render a workspace's merged timeline (also supports old session data).
265
754
  function renderSession(chatData) {
266
- document.getElementById('sessionId').textContent = chatData.session_id || '--';
267
- document.getElementById('sessionName').textContent = (chatData.workspace_name || '') + ' / ' + (chatData.session_name || '--');
268
- document.getElementById('initiator').textContent = chatData.initiator || '--';
269
- document.getElementById('totalPairs').textContent = chatData.total_pairs || 0;
755
+ var isWorkspace = chatData.session_count !== undefined;
756
+ currentChatData = chatData;
757
+ buildSessionLegend(chatData);
758
+ var workspaceDisplay = chatData.workspace_label ? chatData.workspace_label + ' (' + chatData.workspace_name + ')' : (chatData.workspace_name || chatData.session_id || '--');
759
+ document.getElementById('sessionId').textContent = workspaceDisplay;
760
+ if (isWorkspace) {
761
+ var sessionLabels = (chatData.session_labels || chatData.session_names || []).join('、');
762
+ document.getElementById('sessionName').textContent = chatData.session_count + ' 个' + (sessionLabels ? '(' + sessionLabels + ')' : '');
763
+ } else {
764
+ document.getElementById('sessionName').textContent = chatData.session_label || chatData.session_name || '--';
765
+ }
766
+ document.getElementById('initiator').textContent = (chatData.first_time && chatData.last_time) ? chatData.first_time + ' ~ ' + chatData.last_time : '--';
270
767
 
271
- var userCount = 0;
272
768
  var aiCount = 0;
273
769
  var totalDuration = 0;
274
770
  var list = document.getElementById('conversationList');
275
771
  list.innerHTML = '';
276
772
 
277
- var conversations = chatData.conversations || [];
773
+ var allConversations = chatData.conversations || [];
774
+ var conversations = activeSessionFilter ? allConversations.filter(function(turn) {
775
+ return turn.source_session_id === activeSessionFilter;
776
+ }) : allConversations;
777
+ updateDownloadButton(chatData, conversations);
778
+ var timelineStart = allConversations.length > 0 ? parseConversationTime(allConversations[0], 'user') : null;
779
+ document.getElementById('totalPairs').textContent = conversations.length;
278
780
  conversations.forEach(function(c, i) {
279
781
  var hasReply = c.ai && c.ai.trim();
280
782
  if (hasReply) aiCount++;
281
783
 
282
784
  var responseDuration = null;
283
785
  if (hasReply) {
284
- var userTime = parseTime(c.user_time);
285
- var aiTime = parseTime(c.ai_time);
786
+ var userTime = parseConversationTime(c, 'user');
787
+ var aiTime = parseConversationTime(c, 'ai');
286
788
  if (userTime && aiTime) {
287
789
  responseDuration = aiTime - userTime;
288
790
  if (responseDuration > 0) totalDuration += responseDuration;
@@ -291,28 +793,40 @@
291
793
 
292
794
  var intervalDuration = null;
293
795
  if (i < conversations.length - 1) {
294
- var nextUserTime = parseTime(conversations[i + 1].user_time);
295
- var currentUserTime = parseTime(c.user_time);
796
+ var nextUserTime = parseConversationTime(conversations[i + 1], 'user');
797
+ var currentUserTime = parseConversationTime(c, 'user');
296
798
  if (currentUserTime && nextUserTime) {
297
799
  intervalDuration = nextUserTime - currentUserTime;
298
800
  }
299
801
  }
300
802
 
301
803
  var elapsedDuration = null;
302
- if (i > 0) {
303
- var firstUserTime = parseTime(conversations[0].user_time);
304
- var currentUserTime2 = parseTime(c.user_time);
305
- if (firstUserTime && currentUserTime2) {
306
- elapsedDuration = currentUserTime2 - firstUserTime;
804
+ if (timelineStart) {
805
+ var currentUserTime2 = parseConversationTime(c, 'user');
806
+ if (currentUserTime2) {
807
+ elapsedDuration = currentUserTime2 - timelineStart;
307
808
  }
308
809
  }
309
810
 
310
811
  var pair = document.createElement('div');
311
812
  pair.className = 'pair';
813
+ var sourceBadge = '';
814
+ var visual = { border: '#94a3b8', soft: '#f8fafc', text: '#475569', label: '会话', shortId: '' };
815
+ if (c.source_session_id) {
816
+ visual = sessionVisualMap[c.source_session_id] || visual;
817
+ var originalTurn = c.source_turn_index || '?';
818
+ var readableSessionName = c.source_session_label || c.source_session_name || '';
819
+ var sourceLabel = visual.label + (readableSessionName ? ' · ' + readableSessionName : '') + ' · 原第 ' + originalTurn + ' 轮';
820
+ sourceBadge = '<span class="source-session" style="background:' + visual.soft + ';border-color:' + visual.border + ';color:' + visual.text + '" title="' + escapeHtml(c.source_session_id) + '">' + escapeHtml(sourceLabel) + '</span>';
821
+ }
822
+ pair.style.borderLeft = '6px solid ' + visual.border;
823
+ var displayedTurn = c.workspace_turn_index || (i + 1);
824
+ var turnLabel = isWorkspace ? '合并第 ' + displayedTurn + ' 轮' : '第 ' + displayedTurn + ' 轮';
312
825
  pair.innerHTML =
313
- '<div class="pair-header">' +
826
+ '<div class="pair-header" style="background:' + visual.soft + '">' +
314
827
  '<div class="pair-left">' +
315
- '<span class="pair-num">第 ' + (i + 1) + ' 轮</span>' +
828
+ '<span class="pair-num">' + turnLabel + '</span>' +
829
+ sourceBadge +
316
830
  (elapsedDuration !== null ? '<span class="pair-duration elapsed">⏰ 课程流逝: ' + formatDuration(elapsedDuration) + '</span>' : '<span class="pair-duration elapsed">⏰ 课程流逝: 0分</span>') +
317
831
  (intervalDuration !== null ? '<span class="pair-duration interval">⏸️ 间隔耗时: ' + formatDuration(intervalDuration) + '</span>' : '') +
318
832
  '</div>' +
@@ -348,13 +862,17 @@
348
862
 
349
863
  // Init
350
864
  (function() {
351
- // Strategy 1: Use index.js with chatIndex + chatAll
865
+ var downloadButton = document.getElementById('downloadMarkdown');
866
+ if (downloadButton) downloadButton.addEventListener('click', downloadCurrentMarkdown);
867
+
868
+ // Strategy 1: Use index.js workspace data (with old session fallback).
352
869
  if (typeof chatIndex !== 'undefined' && chatIndex && chatIndex.length > 0 &&
353
- typeof chatAll !== 'undefined' && chatAll) {
870
+ ((typeof chatWorkspaces !== 'undefined' && chatWorkspaces) ||
871
+ (typeof chatAll !== 'undefined' && chatAll))) {
354
872
  buildSelector();
355
- // Select first session
356
- document.getElementById('sessionSelect').value = chatIndex[0].session_id;
357
- switchTo(chatIndex[0].session_id);
873
+ var firstId = (typeof chatWorkspaces !== 'undefined' && chatWorkspaces) ? chatIndex[0].workspace_id : chatIndex[0].session_id;
874
+ document.getElementById('sessionSelect').value = firstId;
875
+ switchTo(firstId);
358
876
  return;
359
877
  }
360
878