forked from shuyu-labs/WebCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContextPreviewPanel.razor
More file actions
728 lines (648 loc) · 33.6 KB
/
ContextPreviewPanel.razor
File metadata and controls
728 lines (648 loc) · 33.6 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
@using System.Text.Json
@using WebCodeCli.Domain.Domain.Model
@using WebCodeCli.Domain.Domain.Service
@using Microsoft.JSInterop
@inject IContextManagerService ContextManagerService
@inject ILocalizationService L
@inject IJSRuntime JSRuntime
<!-- 上下文预览面板 -->
<div class="@ContainerClass">
<!-- 头部 -->
@if (ShowHeader)
{
<div class="p-4 border-b-2 border-gray-200 flex items-center justify-between bg-gradient-to-r from-gray-50 to-white flex-shrink-0">
<div class="flex items-center gap-2">
<svg class="w-5 h-5 text-gray-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"></path>
</svg>
<h3 class="text-lg font-bold text-gray-800">@T("contextPreview.title")</h3>
</div>
<button @onclick="Close" class="p-2 hover:bg-white rounded-lg transition-colors" title="@T("common.close")">
<svg class="w-5 h-5 text-gray-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path>
</svg>
</button>
</div>
}
<!-- 统计信息 -->
@if (_statistics != null)
{
<div class="p-4 bg-gray-50 border-b border-gray-200 flex-shrink-0">
<div class="grid grid-cols-2 gap-3">
<div class="bg-white rounded-lg p-3 shadow-sm border border-gray-200">
<div class="text-xs text-gray-500 font-medium">@T("contextPreview.tokenUsage")</div>
<div class="text-lg font-bold text-gray-700">@_statistics.UsedTokens.ToString("N0")</div>
<div class="text-xs text-gray-400">/ @_statistics.TotalTokens.ToString("N0")</div>
</div>
<div class="bg-white rounded-lg p-3 shadow-sm border border-gray-200">
<div class="text-xs text-gray-500 font-medium">@T("contextPreview.usageRate")</div>
<div class="text-lg font-bold @GetUsageColorClass(_statistics.UsagePercentage)">@_statistics.UsagePercentage.ToString("F1")%</div>
<div class="w-full bg-gray-200 rounded-full h-2 mt-1">
<div class="@GetUsageColorClass(_statistics.UsagePercentage) h-2 rounded-full transition-all" style="width: @(_statistics.UsagePercentage)%"></div>
</div>
</div>
<div class="bg-white rounded-lg p-3 shadow-sm border border-gray-200">
<div class="text-xs text-gray-500 font-medium">@T("contextPreview.items")</div>
<div class="text-lg font-bold text-gray-700">@_statistics.ItemCount</div>
<div class="text-xs text-gray-400">@T("contextPreview.included", ("count", _statistics.IncludedItemCount.ToString()))</div>
</div>
<div class="bg-white rounded-lg p-3 shadow-sm border border-gray-200">
<div class="text-xs text-gray-500 font-medium">@T("contextPreview.remaining")</div>
<div class="text-lg font-bold text-green-600">@_statistics.RemainingTokens.ToString("N0")</div>
<div class="text-xs text-gray-400">@T("contextPreview.tokens")</div>
</div>
</div>
</div>
}
<!-- 工具栏 -->
<div class="p-3 border-b border-gray-200 flex flex-wrap items-center gap-2 flex-shrink-0 bg-white overflow-hidden">
<input type="text"
@bind="_searchKeyword"
@bind:event="oninput"
@onkeyup="HandleSearch"
placeholder="@T("contextPreview.searchPlaceholder")"
class="flex-1 min-w-0 px-3 py-2 text-sm border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-gray-400" />
<select @bind="_filterType" @bind:after="HandleFilterChange" class="flex-shrink-0 w-24 px-2 py-2 text-sm border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-gray-400 truncate">
<option value="">@T("contextPreview.filterAll")</option>
<option value="UserMessage">@T("contextPreview.type.userMessage")</option>
<option value="AssistantMessage">@T("contextPreview.type.assistantMessage")</option>
<option value="CodeSnippet">@T("contextPreview.type.codeSnippet")</option>
<option value="FileReference">@T("contextPreview.type.fileReference")</option>
<option value="WorkspaceFile">@T("contextPreview.type.workspaceFile")</option>
<option value="ErrorMessage">@T("contextPreview.type.errorMessage")</option>
</select>
<button @onclick="ShowCompressDialog" class="flex-shrink-0 p-2 hover:bg-gray-100 rounded-lg transition-colors" title="@T("contextPreview.compressTitle")">
<svg class="w-5 h-5 text-gray-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 11H5m14 0a2 2 0 012 2v6a2 2 0 01-2 2H5a2 2 0 01-2-2v-6a2 2 0 012-2m14 0V9a2 2 0 00-2-2M5 11V9a2 2 0 012-2m0 0V5a2 2 0 012-2h6a2 2 0 012 2v2M7 7h10"></path>
</svg>
</button>
</div>
<!-- 上下文项列表 -->
<div class="flex-1 overflow-y-auto p-3 space-y-2">
@if (_filteredItems.Any())
{
@foreach (var item in _filteredItems.OrderByDescending(i => i.Priority).ThenByDescending(i => i.CreatedAt))
{
<div class="@GetItemCardClass(item) rounded-lg p-3 border-2 transition-all hover:shadow-md">
<div class="flex items-start justify-between gap-2 mb-2">
<div class="flex items-center gap-2 flex-1 min-w-0">
<input type="checkbox"
checked="@item.IsIncluded"
@onchange="() => ToggleItem(item)"
class="w-4 h-4 text-gray-600 rounded focus:ring-2 focus:ring-gray-400 cursor-pointer" />
<span class="@GetTypeBadgeClass(item.Type) text-xs font-semibold px-2 py-0.5 rounded-full whitespace-nowrap">
@GetTypeDisplayName(item.Type)
</span>
@if (item.Priority >= 7)
{
<svg class="w-4 h-4 text-yellow-500 flex-shrink-0" fill="currentColor" viewBox="0 0 20 20">
<path d="M9.049 2.927c.3-.921 1.603-.921 1.902 0l1.07 3.292a1 1 0 00.95.69h3.462c.969 0 1.371 1.24.588 1.81l-2.8 2.034a1 1 0 00-.364 1.118l1.07 3.292c.3.921-.755 1.688-1.54 1.118l-2.8-2.034a1 1 0 00-1.175 0l-2.8 2.034c-.784.57-1.838-.197-1.539-1.118l1.07-3.292a1 1 0 00-.364-1.118L2.98 8.72c-.783-.57-.38-1.81.588-1.81h3.461a1 1 0 00.951-.69l1.07-3.292z"></path>
</svg>
}
</div>
<div class="flex items-center gap-2 flex-shrink-0">
<span class="text-xs text-gray-500 font-mono">@T("contextPreview.tokensCount", ("count", item.EstimatedTokens.ToString()))</span>
<div class="flex gap-1">
<button @onclick="() => IncreasePriority(item)" class="p-1 hover:bg-gray-100 rounded transition-colors" title="@T("contextPreview.priorityUp")">
<svg class="w-3 h-3 text-gray-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 15l7-7 7 7"></path>
</svg>
</button>
<button @onclick="() => DecreasePriority(item)" class="p-1 hover:bg-gray-100 rounded transition-colors" title="@T("contextPreview.priorityDown")">
<svg class="w-3 h-3 text-gray-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"></path>
</svg>
</button>
</div>
</div>
</div>
@if (!string.IsNullOrEmpty(item.FilePath))
{
<div class="flex items-center gap-1 mb-1">
<svg class="w-3 h-3 text-gray-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 21h10a2 2 0 002-2V9.414a1 1 0 00-.293-.707l-5.414-5.414A1 1 0 0012.586 3H7a2 2 0 00-2 2v14a2 2 0 002 2z"></path>
</svg>
<span class="text-xs text-gray-600 font-mono truncate">@item.FilePath</span>
</div>
}
<div class="text-sm text-gray-700 break-words cursor-pointer hover:bg-gray-50 -mx-1 px-1 py-1 rounded transition-colors" @onclick="() => ShowFullContent(item)">
@if (!string.IsNullOrEmpty(item.Summary))
{
<div class="font-medium text-gray-600 mb-1">@item.Summary</div>
}
@if (item.Content.Length > 150)
{
<div class="line-clamp-3">@item.Content</div>
<button class="text-xs text-gray-600 hover:text-gray-800 mt-1">
@T("contextPreview.viewFull")
</button>
}
else
{
<div>@item.Content</div>
}
</div>
@if (item.Tags.Any())
{
<div class="flex flex-wrap gap-1 mt-2">
@foreach (var tag in item.Tags)
{
<span class="text-xs bg-gray-100 text-gray-600 px-2 py-0.5 rounded-full">@tag</span>
}
</div>
}
</div>
}
}
else
{
<div class="text-center py-12 text-gray-400">
<svg class="w-16 h-16 mx-auto mb-4 text-gray-300" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"></path>
</svg>
<p class="text-sm">@T("contextPreview.empty")</p>
</div>
}
</div>
<!-- 底部操作栏 -->
<div class="p-3 border-t-2 border-gray-200 flex gap-2 flex-shrink-0 bg-gray-50">
<button @onclick="RefreshContext" class="flex-1 btn-default text-sm py-2 shadow-sm hover:shadow-md transition-all">
<svg class="w-4 h-4 inline-block mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"></path>
</svg>
@T("common.refresh")
</button>
<button @onclick="ExportContext" class="flex-1 btn-primary text-sm py-2 shadow-md hover:shadow-lg transition-all">
<svg class="w-4 h-4 inline-block mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4"></path>
</svg>
@T("common.export")
</button>
</div>
</div>
<!-- 完整内容对话框 -->
@if (_showFullContentDialog && _selectedItem != null)
{
<div class="fixed inset-0 bg-black/50 backdrop-blur-sm flex items-center justify-center z-50 p-4" @onclick="CloseFullContentDialog">
<div class="bg-white rounded-2xl shadow-2xl max-w-4xl w-full max-h-[80vh] flex flex-col" @onclick:stopPropagation="true">
<!-- 对话框头部 -->
<div class="p-6 border-b-2 border-gray-200 flex items-center justify-between bg-gradient-to-r from-gray-50 to-white">
<div class="flex items-center gap-3">
<span class="@GetTypeBadgeClass(_selectedItem.Type) text-sm font-semibold px-3 py-1 rounded-full">
@GetTypeDisplayName(_selectedItem.Type)
</span>
@if (!string.IsNullOrEmpty(_selectedItem.Summary))
{
<h3 class="text-lg font-bold text-gray-800">@_selectedItem.Summary</h3>
}
else
{
<h3 class="text-lg font-bold text-gray-800">@T("contextPreview.fullContent")</h3>
}
</div>
<button @onclick="CloseFullContentDialog" class="p-2 hover:bg-white rounded-lg transition-colors" title="@T("common.close")">
<svg class="w-6 h-6 text-gray-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path>
</svg>
</button>
</div>
<!-- 内容区域 -->
<div class="flex-1 overflow-y-auto p-6">
@if (!string.IsNullOrEmpty(_selectedItem.FilePath))
{
<div class="flex items-center gap-2 mb-4 p-3 bg-gray-50 rounded-lg border border-gray-200">
<svg class="w-4 h-4 text-gray-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 21h10a2 2 0 002-2V9.414a1 1 0 00-.293-.707l-5.414-5.414A1 1 0 0012.586 3H7a2 2 0 00-2 2v14a2 2 0 002 2z"></path>
</svg>
<span class="text-sm text-gray-700 font-mono">@_selectedItem.FilePath</span>
</div>
}
<div class="bg-gray-50 rounded-lg p-4 border border-gray-200">
<pre class="whitespace-pre-wrap break-words text-sm text-gray-800 font-mono leading-relaxed">@_selectedItem.Content</pre>
</div>
@if (_selectedItem.Tags.Any())
{
<div class="mt-4">
<div class="text-sm text-gray-500 mb-2">@T("contextPreview.tags")</div>
<div class="flex flex-wrap gap-2">
@foreach (var tag in _selectedItem.Tags)
{
<span class="text-sm bg-gray-100 text-gray-700 px-3 py-1 rounded-full font-medium">@tag</span>
}
</div>
</div>
}
<div class="mt-4 grid grid-cols-3 gap-3">
<div class="bg-white rounded-lg p-3 border border-gray-200">
<div class="text-xs text-gray-500">@T("contextPreview.tokenCount")</div>
<div class="text-lg font-bold text-gray-700">@_selectedItem.EstimatedTokens</div>
</div>
<div class="bg-white rounded-lg p-3 border border-gray-200">
<div class="text-xs text-gray-500">@T("contextPreview.priority")</div>
<div class="text-lg font-bold text-gray-700">@_selectedItem.Priority / 10</div>
</div>
<div class="bg-white rounded-lg p-3 border border-gray-200">
<div class="text-xs text-gray-500">@T("contextPreview.createdAt")</div>
<div class="text-sm font-medium text-gray-700">@_selectedItem.CreatedAt.ToString("HH:mm:ss")</div>
</div>
</div>
</div>
<!-- 底部按钮 -->
<div class="p-6 border-t-2 border-gray-200 flex gap-3 bg-gray-50">
<button @onclick="() => CopyContent(_selectedItem.Content)" class="flex-1 px-4 py-3 border border-gray-300 rounded-xl text-gray-700 font-medium hover:bg-white transition-colors flex items-center justify-center gap-2">
@if (_copied)
{
<svg class="w-5 h-5 text-green-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"></path>
</svg>
<span class="text-green-600">@T("contextPreview.copied")</span>
}
else
{
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z"></path>
</svg>
@T("contextPreview.copy")
}
</button>
<button @onclick="CloseFullContentDialog" class="flex-1 px-4 py-3 bg-gray-800 hover:bg-black text-white rounded-xl font-medium transition-all shadow-md hover:shadow-lg">
@T("common.close")
</button>
</div>
</div>
</div>
}
<!-- 压缩对话框 -->
@if (_showCompressDialog)
{
<div class="fixed inset-0 bg-black/50 backdrop-blur-sm flex items-center justify-center z-50 p-4" @onclick="CloseCompressDialog">
<div class="bg-white rounded-2xl shadow-2xl max-w-md w-full p-6" @onclick:stopPropagation="true">
<h3 class="text-xl font-bold text-gray-800 mb-4">@T("contextPreview.compressTitle")</h3>
<div class="space-y-3 mb-6">
<label class="flex items-center gap-3 p-3 border-2 border-gray-200 rounded-lg cursor-pointer hover:bg-gray-50 transition-colors">
<input type="radio" name="compress-strategy" value="KeepRecent" @onchange="() => _selectedStrategy = CompressionStrategy.KeepRecent" checked="@(_selectedStrategy == CompressionStrategy.KeepRecent)" class="w-4 h-4" />
<div class="flex-1">
<div class="font-semibold text-gray-800">@T("contextPreview.compress.keepRecent")</div>
<div class="text-xs text-gray-500">@T("contextPreview.compress.keepRecentDesc")</div>
</div>
</label>
<label class="flex items-center gap-3 p-3 border-2 border-gray-200 rounded-lg cursor-pointer hover:bg-gray-50 transition-colors">
<input type="radio" name="compress-strategy" value="KeepHighPriority" @onchange="() => _selectedStrategy = CompressionStrategy.KeepHighPriority" checked="@(_selectedStrategy == CompressionStrategy.KeepHighPriority)" class="w-4 h-4" />
<div class="flex-1">
<div class="font-semibold text-gray-800">@T("contextPreview.compress.keepHighPriority")</div>
<div class="text-xs text-gray-500">@T("contextPreview.compress.keepHighPriorityDesc")</div>
</div>
</label>
<label class="flex items-center gap-3 p-3 border-2 border-gray-200 rounded-lg cursor-pointer hover:bg-gray-50 transition-colors">
<input type="radio" name="compress-strategy" value="SmartSummary" @onchange="() => _selectedStrategy = CompressionStrategy.SmartSummary" checked="@(_selectedStrategy == CompressionStrategy.SmartSummary)" class="w-4 h-4" />
<div class="flex-1">
<div class="font-semibold text-gray-800">@T("contextPreview.compress.smartSummary")</div>
<div class="text-xs text-gray-500">@T("contextPreview.compress.smartSummaryDesc")</div>
</div>
</label>
<label class="flex items-center gap-3 p-3 border-2 border-gray-200 rounded-lg cursor-pointer hover:bg-gray-50 transition-colors">
<input type="radio" name="compress-strategy" value="RemoveDuplicates" @onchange="() => _selectedStrategy = CompressionStrategy.RemoveDuplicates" checked="@(_selectedStrategy == CompressionStrategy.RemoveDuplicates)" class="w-4 h-4" />
<div class="flex-1">
<div class="font-semibold text-gray-800">@T("contextPreview.compress.removeDuplicates")</div>
<div class="text-xs text-gray-500">@T("contextPreview.compress.removeDuplicatesDesc")</div>
</div>
</label>
</div>
<div class="flex gap-3">
<button @onclick="CloseCompressDialog" class="flex-1 px-4 py-3 border border-gray-300 rounded-xl text-gray-700 font-medium hover:bg-gray-50 transition-colors">
@T("common.cancel")
</button>
<button @onclick="CompressContext" disabled="@_isCompressing" class="flex-1 px-4 py-3 bg-gray-800 hover:bg-black text-white rounded-xl font-medium disabled:opacity-50 transition-all shadow-md hover:shadow-lg">
@if (_isCompressing)
{
<span>@T("contextPreview.compressing")</span>
}
else
{
<span>@T("contextPreview.compressStart")</span>
}
</button>
</div>
</div>
</div>
}
@code {
[Parameter] public string SessionId { get; set; } = string.Empty;
[Parameter] public EventCallback OnContextChanged { get; set; }
[Parameter] public bool Embedded { get; set; }
[Parameter] public bool ShowHeader { get; set; } = true;
[Parameter] public bool IsActive { get; set; } = true;
private bool _isVisible = false;
private List<ContextItem> _contextItems = new();
private List<ContextItem> _filteredItems = new();
private ContextStatistics? _statistics;
private bool _wasActive = false;
private string _lastSessionId = string.Empty;
private bool _isAutoRefreshing = false;
private string _searchKeyword = string.Empty;
private string _filterType = string.Empty;
private bool _showCompressDialog = false;
private bool _isCompressing = false;
private CompressionStrategy _selectedStrategy = CompressionStrategy.SmartSummary;
private bool _showFullContentDialog = false;
private ContextItem? _selectedItem = null;
// 本地化相关
private Dictionary<string, string> _translations = new();
private string _currentLanguage = "zh-CN";
private string ContainerClass => Embedded
? "flex flex-col h-full w-full bg-white"
: (_isVisible ? "fixed inset-y-0 right-0 w-96 bg-white shadow-2xl z-50 flex flex-col border-l-2 border-gray-300 transition-all duration-300" : "hidden");
protected override async Task OnInitializedAsync()
{
// 组件初始化时加载翻译资源
_currentLanguage = await L.GetCurrentLanguageAsync();
await LoadTranslationsAsync();
await base.OnInitializedAsync();
}
public async Task ShowAsync(string sessionId)
{
SessionId = sessionId;
_isVisible = true;
_currentLanguage = await L.GetCurrentLanguageAsync();
await LoadTranslationsAsync();
await RefreshContext();
}
public void Close()
{
_isVisible = false;
StateHasChanged();
}
protected override async Task OnParametersSetAsync()
{
if (Embedded && IsActive && !_isAutoRefreshing && !string.IsNullOrWhiteSpace(SessionId))
{
if (!_wasActive || !string.Equals(_lastSessionId, SessionId, StringComparison.Ordinal))
{
_isAutoRefreshing = true;
_lastSessionId = SessionId;
// 确保翻译资源已加载
if (_translations.Count == 0)
{
_currentLanguage = await L.GetCurrentLanguageAsync();
await LoadTranslationsAsync();
}
await RefreshContext();
_isAutoRefreshing = false;
}
}
_wasActive = IsActive;
await base.OnParametersSetAsync();
}
private async Task RefreshContext()
{
_contextItems = ContextManagerService.GetContextItems(SessionId);
_statistics = ContextManagerService.GetContextStatistics(SessionId);
ApplyFilters();
StateHasChanged();
await Task.CompletedTask;
}
private void ApplyFilters()
{
_filteredItems = _contextItems;
// 应用搜索
if (!string.IsNullOrWhiteSpace(_searchKeyword))
{
_filteredItems = ContextManagerService.SearchContextItems(SessionId, _searchKeyword);
}
// 应用类型筛选
if (!string.IsNullOrEmpty(_filterType) && Enum.TryParse<ContextItemType>(_filterType, out var type))
{
_filteredItems = _filteredItems.Where(i => i.Type == type).ToList();
}
}
private async Task HandleSearch(KeyboardEventArgs e)
{
ApplyFilters();
await Task.CompletedTask;
}
private async Task HandleFilterChange()
{
ApplyFilters();
await Task.CompletedTask;
}
private async Task ToggleItem(ContextItem item)
{
ContextManagerService.ToggleContextItem(SessionId, item.Id);
await RefreshContext();
await OnContextChanged.InvokeAsync();
}
private async Task IncreasePriority(ContextItem item)
{
var newPriority = Math.Min(item.Priority + 1, 10);
ContextManagerService.UpdateContextItemPriority(SessionId, item.Id, newPriority);
await RefreshContext();
}
private async Task DecreasePriority(ContextItem item)
{
var newPriority = Math.Max(item.Priority - 1, 0);
ContextManagerService.UpdateContextItemPriority(SessionId, item.Id, newPriority);
await RefreshContext();
}
private void ShowCompressDialog()
{
_showCompressDialog = true;
StateHasChanged();
}
private void CloseCompressDialog()
{
_showCompressDialog = false;
StateHasChanged();
}
private async Task CompressContext()
{
_isCompressing = true;
StateHasChanged();
try
{
var result = await ContextManagerService.CompressContextAsync(SessionId, _selectedStrategy);
Console.WriteLine($"压缩完成: 节省 {result.TokensSaved} tokens ({result.CompressionRatio:F1}%)");
await RefreshContext();
await OnContextChanged.InvokeAsync();
CloseCompressDialog();
}
catch (Exception ex)
{
Console.WriteLine($"压缩失败: {ex.Message}");
}
finally
{
_isCompressing = false;
StateHasChanged();
}
}
private async Task ExportContext()
{
try
{
var json = await ContextManagerService.ExportContextAsync(SessionId);
// TODO: 触发下载
Console.WriteLine("上下文已导出");
}
catch (Exception ex)
{
Console.WriteLine($"导出失败: {ex.Message}");
}
}
private void ShowFullContent(ContextItem item)
{
_selectedItem = item;
_showFullContentDialog = true;
StateHasChanged();
}
private void CloseFullContentDialog()
{
_showFullContentDialog = false;
_selectedItem = null;
StateHasChanged();
}
private bool _copied = false;
private async Task CopyContent(string content)
{
try
{
// 使用统一的剪贴板函数(含回退方案)
var success = await JSRuntime.InvokeAsync<bool>("clipboardCopy.copyText", content);
if (success)
{
_copied = true;
StateHasChanged();
// 2秒后恢复状态
await Task.Delay(2000);
_copied = false;
StateHasChanged();
}
else
{
Console.WriteLine("复制失败:剪贴板操作返回 false");
}
}
catch (Exception ex)
{
Console.WriteLine($"复制失败: {ex.Message}");
}
}
private string GetItemCardClass(ContextItem item)
{
var baseClass = "bg-white";
if (!item.IsIncluded)
{
baseClass += " opacity-50 border-gray-200";
}
else if (item.Priority >= 7)
{
baseClass += " border-yellow-300 bg-yellow-50";
}
else
{
baseClass += " border-gray-200";
}
return baseClass;
}
private string GetTypeBadgeClass(ContextItemType type)
{
return type switch
{
ContextItemType.UserMessage => "bg-blue-100 text-blue-700",
ContextItemType.AssistantMessage => "bg-green-100 text-green-700",
ContextItemType.CodeSnippet => "bg-purple-100 text-purple-700",
ContextItemType.FileReference => "bg-orange-100 text-orange-700",
ContextItemType.WorkspaceFile => "bg-indigo-100 text-indigo-700",
ContextItemType.ErrorMessage => "bg-red-100 text-red-700",
_ => "bg-gray-100 text-gray-700"
};
}
private string GetTypeDisplayName(ContextItemType type)
{
return type switch
{
ContextItemType.UserMessage => T("contextPreview.type.user"),
ContextItemType.AssistantMessage => T("contextPreview.type.assistant"),
ContextItemType.CodeSnippet => T("contextPreview.type.code"),
ContextItemType.FileReference => T("contextPreview.type.file"),
ContextItemType.WorkspaceFile => T("contextPreview.type.workspace"),
ContextItemType.ErrorMessage => T("contextPreview.type.error"),
_ => type.ToString()
};
}
private string GetUsageColorClass(double percentage)
{
if (percentage >= 90) return "text-red-600 bg-red-600";
if (percentage >= 70) return "text-yellow-600 bg-yellow-600";
return "text-green-600 bg-green-600";
}
#region 本地化辅助方法
private async Task LoadTranslationsAsync()
{
try
{
var allTranslations = await L.GetAllTranslationsAsync(_currentLanguage);
_translations = FlattenTranslations(allTranslations);
}
catch (Exception ex)
{
Console.WriteLine($"[上下文预览] 加载翻译资源失败: {ex.Message}");
}
}
private Dictionary<string, string> FlattenTranslations(Dictionary<string, object> source, string prefix = "")
{
var result = new Dictionary<string, string>();
foreach (var kvp in source)
{
var key = string.IsNullOrEmpty(prefix) ? kvp.Key : $"{prefix}.{kvp.Key}";
if (kvp.Value is JsonElement jsonElement)
{
if (jsonElement.ValueKind == JsonValueKind.Object)
{
var nested = JsonSerializer.Deserialize<Dictionary<string, object>>(jsonElement.GetRawText());
if (nested != null)
{
foreach (var item in FlattenTranslations(nested, key))
{
result[item.Key] = item.Value;
}
}
}
else if (jsonElement.ValueKind == JsonValueKind.String)
{
result[key] = jsonElement.GetString() ?? key;
}
}
else if (kvp.Value is Dictionary<string, object> dict)
{
foreach (var item in FlattenTranslations(dict, key))
{
result[item.Key] = item.Value;
}
}
else if (kvp.Value is string str)
{
result[key] = str;
}
}
return result;
}
private string T(string key)
{
if (_translations.TryGetValue(key, out var translation))
{
return translation;
}
var parts = key.Split('.');
return parts.Length > 0 ? parts[^1] : key;
}
private string T(string key, params (string name, string value)[] parameters)
{
var text = T(key);
foreach (var (name, value) in parameters)
{
text = text.Replace($"{{{name}}}", value);
}
return text;
}
#endregion
}