-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathFeishuChannelService.cs
More file actions
977 lines (853 loc) · 35.5 KB
/
FeishuChannelService.cs
File metadata and controls
977 lines (853 loc) · 35.5 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
using System.Collections.Concurrent;
using System.Text;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using WebCodeCli.Domain.Common.Extensions;
using WebCodeCli.Domain.Common.Options;
using WebCodeCli.Domain.Domain.Model;
using WebCodeCli.Domain.Domain.Model.Channels;
using WebCodeCli.Domain.Domain.Service;
using WebCodeCli.Domain.Domain.Service.Adapters;
using WebCodeCli.Domain.Repositories.Base.ChatSession;
namespace WebCodeCli.Domain.Domain.Service.Channels;
/// <summary>
/// 飞书渠道服务实现
/// 负责处理飞书消息发送、接收和流式回复
/// 与 CliExecutorService 集成实现 AI 助手功能
/// </summary>
[ServiceDescription(typeof(IFeishuChannelService), ServiceLifetime.Singleton)]
public class FeishuChannelService : BackgroundService, IFeishuChannelService
{
private readonly FeishuOptions _options;
private readonly ILogger<FeishuChannelService> _logger;
private readonly IFeishuCardKitClient _cardKit;
private readonly IServiceProvider _serviceProvider;
private FeishuMessageHandler? _messageHandler;
private readonly ICliExecutorService _cliExecutor;
private readonly IChatSessionService _chatSessionService;
private bool _isRunning = false;
// 飞书渠道默认回退工具 ID(最终还会检查配置和实际可用工具)
private const string FallbackToolId = "claude-code";
// 事件去重缓存,避免重复处理相同event_id的消息
private readonly ConcurrentDictionary<string, DateTime> _processedEventIds = new();
// 事件缓存过期时间(10分钟,超过这个时间的event_id会被清理)
private const int EventCacheExpirationMinutes = 10;
/// <summary>
/// 服务是否运行中
/// </summary>
public bool IsRunning => _isRunning;
/// <summary>
/// 获取聊天的当前活跃会话ID
/// </summary>
/// <param name="chatKey">聊天键(格式:feishu:{AppId}:{ChatId})</param>
/// <returns>当前会话ID,如果不存在则返回null</returns>
public string? GetCurrentSession(string chatKey, string? username = null)
{
using var scope = _serviceProvider.CreateScope();
var repo = scope.ServiceProvider.GetRequiredService<IChatSessionRepository>();
var bindingService = scope.ServiceProvider.GetRequiredService<IFeishuUserBindingService>();
var sessions = GetValidFeishuSessions(repo, bindingService, chatKey, username);
var activeSession = sessions.FirstOrDefault(s => s.IsFeishuActive && s.FeishuChatKey == chatKey);
if (activeSession != null)
{
return activeSession.SessionId;
}
var latestSession = sessions.OrderByDescending(s => s.UpdatedAt).FirstOrDefault();
if (latestSession == null || string.IsNullOrWhiteSpace(username))
{
return null;
}
return SwitchCurrentSession(chatKey, latestSession.SessionId, username)
? latestSession.SessionId
: null;
}
/// <summary>
/// 获取会话的最后活跃时间
/// </summary>
/// <param name="sessionId">会话ID</param>
/// <returns>最后活跃时间,如果会话不存在则返回null</returns>
public DateTime? GetSessionLastActiveTime(string sessionId)
{
using var scope = _serviceProvider.CreateScope();
var repo = scope.ServiceProvider.GetRequiredService<IChatSessionRepository>();
var session = repo.GetByIdAsync(sessionId).GetAwaiter().GetResult();
return session?.UpdatedAt;
}
/// <summary>
/// 获取聊天的所有会话ID列表
/// </summary>
/// <param name="chatKey">聊天键</param>
/// <returns>会话ID列表</returns>
public List<string> GetChatSessions(string chatKey, string? username = null)
{
using var scope = _serviceProvider.CreateScope();
var repo = scope.ServiceProvider.GetRequiredService<IChatSessionRepository>();
var bindingService = scope.ServiceProvider.GetRequiredService<IFeishuUserBindingService>();
var sessions = GetValidFeishuSessions(repo, bindingService, chatKey, username);
return sessions.Select(s => s.SessionId).ToList();
}
/// <summary>
/// 切换聊天的当前活跃会话
/// </summary>
/// <param name="chatKey">聊天键</param>
/// <param name="sessionId">要切换到的会话ID</param>
/// <returns>是否切换成功</returns>
public bool SwitchCurrentSession(string chatKey, string sessionId, string? username = null)
{
using var scope = _serviceProvider.CreateScope();
var repo = scope.ServiceProvider.GetRequiredService<IChatSessionRepository>();
if (string.IsNullOrWhiteSpace(username))
{
return repo.SetActiveSessionAsync(chatKey, sessionId).GetAwaiter().GetResult();
}
var targetSession = repo.GetByIdAndUsernameAsync(sessionId, username).GetAwaiter().GetResult();
if (targetSession == null)
{
return false;
}
var userChatSessions = repo.GetListAsync(x => x.Username == username && x.FeishuChatKey == chatKey)
.GetAwaiter().GetResult();
foreach (var session in userChatSessions.Where(x => x.IsFeishuActive))
{
session.IsFeishuActive = false;
session.UpdatedAt = DateTime.Now;
repo.UpdateAsync(session).GetAwaiter().GetResult();
}
targetSession.FeishuChatKey = chatKey;
targetSession.IsFeishuActive = true;
targetSession.UpdatedAt = DateTime.Now;
repo.UpdateAsync(targetSession).GetAwaiter().GetResult();
return true;
}
/// <summary>
/// 关闭指定会话
/// </summary>
/// <param name="chatKey">聊天键</param>
/// <param name="sessionId">要关闭的会话ID</param>
/// <returns>是否关闭成功</returns>
public bool CloseSession(string chatKey, string sessionId, string? username = null)
{
using var scope = _serviceProvider.CreateScope();
var repo = scope.ServiceProvider.GetRequiredService<IChatSessionRepository>();
bool success;
if (string.IsNullOrWhiteSpace(username))
{
success = repo.CloseFeishuSessionAsync(chatKey, sessionId).GetAwaiter().GetResult();
}
else
{
var targetSession = repo.GetByIdAndUsernameAsync(sessionId, username).GetAwaiter().GetResult();
if (targetSession == null)
{
return false;
}
var wasActive = targetSession.IsFeishuActive && string.Equals(targetSession.FeishuChatKey, chatKey, StringComparison.OrdinalIgnoreCase);
success = repo.DeleteAsync(targetSession).GetAwaiter().GetResult();
if (success && wasActive)
{
var latestSession = repo.GetByUsernameOrderByUpdatedAtAsync(username).GetAwaiter().GetResult().FirstOrDefault();
if (latestSession != null)
{
SwitchCurrentSession(chatKey, latestSession.SessionId, username);
}
}
}
if (success)
{
_cliExecutor.CleanupSessionWorkspace(sessionId);
_logger.LogInformation("Closed session {SessionId} for chat {ChatKey}, user={User}", sessionId, chatKey, username ?? string.Empty);
}
return success;
}
public FeishuChannelService(
IOptions<FeishuOptions> options,
ILogger<FeishuChannelService> logger,
IFeishuCardKitClient cardKit,
IServiceProvider serviceProvider,
ICliExecutorService cliExecutor,
IChatSessionService chatSessionService)
{
_options = options.Value;
_logger = logger;
_cardKit = cardKit;
_serviceProvider = serviceProvider;
_cliExecutor = cliExecutor;
_chatSessionService = chatSessionService;
}
/// <summary>
/// 后台服务主执行方法
/// </summary>
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
if (!_options.Enabled)
{
_logger.LogInformation("Feishu channel is disabled");
return;
}
_logger.LogInformation("Starting Feishu channel service...");
// 不再订阅静态事件,通过 HandleIncomingMessageAsync 方法处理消息(避免重复处理)
_isRunning = true;
_logger.LogInformation("Feishu channel service started (AppId: {AppId})", _options.AppId);
// 保持运行,等待取消信号
// WebSocket 连接由外部的 FeishuNetSdk.WebSocket 服务管理
try
{
while (!stoppingToken.IsCancellationRequested)
{
await Task.Delay(TimeSpan.FromMinutes(1), stoppingToken);
}
}
catch (OperationCanceledException)
{
_logger.LogInformation("Feishu channel service cancellation requested");
}
}
/// <summary>
/// 停止服务
/// </summary>
public override async Task StopAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Stopping Feishu channel service...");
_isRunning = false;
await base.StopAsync(cancellationToken);
_logger.LogInformation("Feishu channel service stopped");
}
/// <summary>
/// 处理收到的消息
/// </summary>
private async Task OnMessageReceivedAsync(FeishuIncomingMessage message)
{
try
{
_logger.LogInformation(
"🔥 [FeishuChannel] 收到消息事件: MessageId={MessageId}, ChatId={ChatId}, Content={Content}",
message.MessageId,
message.ChatId,
message.Content);
string sessionId;
try
{
// 获取当前会话(无会话时抛出异常)
sessionId = GetCurrentSession(message);
}
catch (InvalidOperationException ex)
{
// 没有会话时提示用户
await ReplyMessageAsync(message.MessageId, $"⚠️ {ex.Message}", message.SenderName, message.AppId);
return;
}
var toolId = ResolveToolId(message.ChatId, message.SenderName);
// 添加用户消息到会话
_chatSessionService.AddMessage(sessionId, new ChatMessage
{
Role = "user",
Content = message.Content,
CliToolId = toolId,
CreatedAt = DateTime.UtcNow
});
// 创建流式回复,立即显示"思考中"状态
var effectiveOptions = await ResolveEffectiveOptionsAsync(message.SenderName, message.ChatId, message.AppId);
var handle = await SendStreamingMessageAsync(
message.ChatId,
effectiveOptions.ThinkingMessage,
message.MessageId,
message.SenderName,
message.AppId);
_logger.LogInformation(
"🔥 [FeishuChannel] 流式句柄已创建: CardId={CardId}",
handle.CardId);
// 执行 CLI 工具并流式更新卡片
await ExecuteCliAndStreamAsync(
handle,
sessionId,
toolId,
message.Content,
message.MessageId,
effectiveOptions.ThinkingMessage,
message.SenderName,
message.AppId);
}
catch (Exception ex)
{
_logger.LogError(
ex,
"🔥 [FeishuChannel] 处理消息失败: {MessageId}",
message.MessageId);
}
}
/// <summary>
/// 处理收到的消息(由 FeishuMessageHandler 调用)
/// </summary>
/// <param name="message">收到的消息</param>
public async Task HandleIncomingMessageAsync(FeishuIncomingMessage message)
{
// 事件去重检查
if (!string.IsNullOrEmpty(message.EventId))
{
// 清理过期的event_id
CleanupExpiredEventIds();
if (_processedEventIds.TryAdd(message.EventId, DateTime.UtcNow))
{
_logger.LogDebug("处理新事件: EventId={EventId}", message.EventId);
}
else
{
_logger.LogInformation("跳过重复事件: EventId={EventId}", message.EventId);
return;
}
}
_logger.LogInformation("🔥 [FeishuChannel] HandleIncomingMessageAsync 被调用");
await OnMessageReceivedAsync(message);
}
/// <summary>
/// 清理过期的事件ID缓存
/// </summary>
private void CleanupExpiredEventIds()
{
var expirationTime = DateTime.UtcNow.AddMinutes(-EventCacheExpirationMinutes);
var expiredIds = _processedEventIds
.Where(kv => kv.Value < expirationTime)
.Select(kv => kv.Key)
.ToList();
foreach (var id in expiredIds)
{
_processedEventIds.TryRemove(id, out _);
}
if (expiredIds.Count > 0)
{
_logger.LogDebug("清理了 {Count} 个过期的事件ID", expiredIds.Count);
}
}
/// <summary>
/// 获取当前会话
/// 如果聊天没有活动会话,则抛出异常提示用户手动创建
/// </summary>
/// <param name="message">飞书 incoming 消息</param>
/// <returns>会话ID</returns>
/// <exception cref="InvalidOperationException">如果没有当前会话则抛出</exception>
private string GetCurrentSession(FeishuIncomingMessage message)
{
var chatKey = message.ChatId.ToLowerInvariant();
var username = message.SenderName;
_logger.LogInformation("🔍 [会话匹配] 消息ChatId={ChatId}, ChatKey={ChatKey}, User={User}",
message.ChatId, chatKey, username);
var currentSessionId = GetCurrentSession(chatKey, username);
if (!string.IsNullOrWhiteSpace(currentSessionId))
{
using var scope = _serviceProvider.CreateScope();
var repo = scope.ServiceProvider.GetRequiredService<IChatSessionRepository>();
var currentSession = repo.GetByIdAsync(currentSessionId).GetAwaiter().GetResult();
if (currentSession != null)
{
currentSession.UpdatedAt = DateTime.UtcNow;
repo.UpdateAsync(currentSession).GetAwaiter().GetResult();
}
return currentSessionId;
}
throw new InvalidOperationException("当前没有可用会话,请先发送 /feishusessions 命令创建或选择会话。");
}
/// <summary>
/// 创建新会话
/// </summary>
/// <param name="message">飞书 incoming 消息</param>
/// <param name="customWorkspacePath">自定义工作区路径(可选)</param>
/// <returns>新会话ID</returns>
public string CreateNewSession(FeishuIncomingMessage message, string? customWorkspacePath = null, string? toolId = null)
{
var chatKey = message.ChatId.ToLowerInvariant();
var username = string.IsNullOrWhiteSpace(message.SenderName) ? "unknown" : message.SenderName;
var resolvedToolId = NormalizeToolId(toolId) ?? ResolveToolId(chatKey, username);
using var scope = _serviceProvider.CreateScope();
var repo = scope.ServiceProvider.GetRequiredService<IChatSessionRepository>();
var newSessionId = repo.CreateFeishuSessionAsync(
chatKey,
username,
null,
resolvedToolId).GetAwaiter().GetResult();
var session = repo.GetByIdAsync(newSessionId).GetAwaiter().GetResult();
if (session == null)
{
throw new InvalidOperationException($"创建飞书会话后未找到会话记录: {newSessionId}");
}
if (!string.IsNullOrWhiteSpace(customWorkspacePath))
{
var sessionDirectoryService = scope.ServiceProvider.GetRequiredService<ISessionDirectoryService>();
sessionDirectoryService.SetSessionWorkspaceAsync(newSessionId, username, customWorkspacePath, true)
.GetAwaiter().GetResult();
session = repo.GetByIdAsync(newSessionId).GetAwaiter().GetResult();
if (session == null)
{
throw new InvalidOperationException($"设置飞书会话工作区后未找到会话记录: {newSessionId}");
}
session.ToolId = resolvedToolId;
session.UpdatedAt = DateTime.Now;
repo.UpdateAsync(session).GetAwaiter().GetResult();
}
else
{
var workspacePath = _cliExecutor.InitializeSessionWorkspaceAsync(newSessionId).GetAwaiter().GetResult();
session.WorkspacePath = workspacePath;
session.IsCustomWorkspace = false;
session.ToolId = resolvedToolId;
session.UpdatedAt = DateTime.Now;
repo.UpdateAsync(session).GetAwaiter().GetResult();
}
_logger.LogInformation(
"Created new session: {SessionId} for chat: {ChatId} (user: {UserName}, tool: {ToolId}, workspace: {WorkspacePath})",
newSessionId,
message.ChatId,
username,
resolvedToolId,
customWorkspacePath ?? session.WorkspacePath ?? string.Empty);
return newSessionId;
}
public string? GetSessionUsername(string chatKey)
{
using var scope = _serviceProvider.CreateScope();
var repo = scope.ServiceProvider.GetRequiredService<IChatSessionRepository>();
var bindingService = scope.ServiceProvider.GetRequiredService<IFeishuUserBindingService>();
var session = GetValidFeishuSessions(repo, bindingService, chatKey).FirstOrDefault();
return session?.Username;
}
public string ResolveToolId(string chatKey, string? username = null)
{
var normalizedChatKey = chatKey.ToLowerInvariant();
using var scope = _serviceProvider.CreateScope();
var repo = scope.ServiceProvider.GetRequiredService<IChatSessionRepository>();
var bindingService = scope.ServiceProvider.GetRequiredService<IFeishuUserBindingService>();
var sessions = GetValidFeishuSessions(repo, bindingService, normalizedChatKey, username);
var activeToolId = sessions
.Where(s => s.IsFeishuActive)
.Select(s => NormalizeToolId(s.ToolId))
.FirstOrDefault(IsConfiguredToolAvailable);
if (!string.IsNullOrWhiteSpace(activeToolId))
{
return activeToolId;
}
var historicalToolId = sessions
.Select(s => NormalizeToolId(s.ToolId))
.FirstOrDefault(IsConfiguredToolAvailable);
if (!string.IsNullOrWhiteSpace(historicalToolId))
{
return historicalToolId;
}
return ResolveDefaultToolId();
}
private List<ChatSessionEntity> GetValidFeishuSessions(
IChatSessionRepository repo,
IFeishuUserBindingService bindingService,
string chatKey,
string? username = null)
{
CleanupUnboundFeishuSessions(repo, bindingService);
List<ChatSessionEntity> sessions;
if (string.IsNullOrWhiteSpace(username))
{
sessions = repo.GetByFeishuChatKeyAsync(chatKey).GetAwaiter().GetResult();
}
else
{
sessions = repo.GetByUsernameOrderByUpdatedAtAsync(username).GetAwaiter().GetResult();
}
return sessions.OrderByDescending(s => s.UpdatedAt).ToList();
}
private void CleanupUnboundFeishuSessions(IChatSessionRepository repo, IFeishuUserBindingService bindingService)
{
var boundWebUsernames = bindingService.GetAllBoundWebUsernamesAsync().GetAwaiter().GetResult();
var feishuSessions = repo.GetListAsync(x => x.FeishuChatKey != null).GetAwaiter().GetResult();
var invalidSessions = feishuSessions
.Where(session => !boundWebUsernames.Contains(session.Username))
.ToList();
foreach (var invalidSession in invalidSessions)
{
repo.DeleteAsync(invalidSession).GetAwaiter().GetResult();
_logger.LogInformation("清理未绑定 Web 用户的飞书旧会话: {SessionId}, User={User}, ChatKey={ChatKey}", invalidSession.SessionId, invalidSession.Username, invalidSession.FeishuChatKey ?? string.Empty);
}
}
/// <summary>
/// 执行 CLI 工具并流式更新卡片
/// </summary>
private async Task ExecuteCliAndStreamAsync(
FeishuStreamingHandle handle,
string sessionId,
string toolId,
string userPrompt,
string messageId,
string thinkingMessage,
string? username = null,
string? appId = null)
{
var outputBuilder = new StringBuilder();
var assistantMessageBuilder = new StringBuilder();
var jsonlBuffer = new StringBuilder(); // JSONL 缓冲区,处理不完整的行
var resolvedToolId = NormalizeToolId(toolId) ?? ResolveDefaultToolId();
var tool = _cliExecutor.GetTool(resolvedToolId);
if (tool == null)
{
await handle.FinishAsync($"错误:未找到 CLI 工具 '{resolvedToolId}',请在配置中添加该工具。");
_logger.LogWarning("CLI tool not found: {ToolId}", resolvedToolId);
return;
}
// 获取适配器(用于解析 JSONL 输出)
var adapter = _cliExecutor.GetAdapter(tool);
var useAdapter = adapter != null && _cliExecutor.SupportsStreamParsing(tool);
_logger.LogDebug(
"Executing CLI tool: {ToolId} for session: {SessionId}, UseAdapter: {UseAdapter}",
tool.Id,
sessionId,
useAdapter);
try
{
// 执行 CLI 工具并流式处理输出
await foreach (var chunk in _cliExecutor.ExecuteStreamAsync(sessionId, tool.Id, userPrompt))
{
if (chunk.IsError)
{
_logger.LogError(
"CLI execution error: {Error}",
chunk.ErrorMessage ?? "Unknown error");
await handle.FinishAsync($"错误:{chunk.ErrorMessage ?? "执行失败"}");
return;
}
// 累积原始输出内容
outputBuilder.Append(chunk.Content);
// 如果使用适配器,解析 JSONL 并提取助手消息
string displayContent;
if (useAdapter)
{
// 解析 JSONL 行并提取助手消息(使用缓冲区处理不完整的行)
ProcessJsonlChunk(chunk.Content, adapter!, assistantMessageBuilder, jsonlBuffer);
displayContent = assistantMessageBuilder.ToString();
// 如果没有助手消息,显示"思考中"
if (string.IsNullOrWhiteSpace(displayContent))
{
displayContent = thinkingMessage;
}
}
else
{
// 不使用适配器时,过滤系统消息
displayContent = FormatMarkdownOutput(outputBuilder.ToString());
}
// 流式更新卡片(节流在 handle 内部处理)
await handle.UpdateAsync(displayContent);
_logger.LogDebug(
"Streamed chunk: {ContentPreview}...",
chunk.Content.Length > 50 ? chunk.Content[..50] : chunk.Content);
// 如果完成,跳出循环
if (chunk.IsCompleted)
{
break;
}
}
// 完成流式回复
string finalOutput;
if (useAdapter)
{
// 使用适配器时,使用提取的助手消息
finalOutput = assistantMessageBuilder.ToString().Trim();
if (string.IsNullOrWhiteSpace(finalOutput))
{
finalOutput = ExtractFallbackOutput(outputBuilder.ToString(), adapter!) ?? "无输出";
}
}
else
{
// 不使用适配器时,过滤系统消息
finalOutput = FormatMarkdownOutput(outputBuilder.ToString());
}
try
{
await ReplyMessageAsync(messageId, "已完成", username, appId);
}
catch (Exception notificationEx)
{
_logger.LogWarning(notificationEx, "发送完成通知失败: MessageId={MessageId}", messageId);
}
await handle.FinishAsync(finalOutput);
// 添加助手回复到会话
_chatSessionService.AddMessage(sessionId, new ChatMessage
{
Role = "assistant",
Content = finalOutput,
CliToolId = tool.Id,
IsCompleted = true,
CreatedAt = DateTime.UtcNow
});
// 更新会话最后活动时间
using var scope = _serviceProvider.CreateScope();
var repo = scope.ServiceProvider.GetRequiredService<IChatSessionRepository>();
var session = await repo.GetByIdAsync(sessionId);
if (session != null)
{
session.ToolId = tool.Id;
session.UpdatedAt = DateTime.UtcNow;
await repo.UpdateAsync(session);
}
_logger.LogInformation(
"CLI execution completed for message: {MessageId}, session: {SessionId}",
messageId,
sessionId);
}
catch (Exception ex)
{
_logger.LogError(ex, "CLI execution failed for message: {MessageId}", messageId);
await handle.FinishAsync($"执行出错:{ex.Message}");
}
}
/// <summary>
/// 格式化 Markdown 输出
/// 适用于飞书卡片显示
/// </summary>
private static string FormatMarkdownOutput(string output)
{
if (string.IsNullOrWhiteSpace(output))
return "无输出";
// 过滤系统钩子消息(Claude Code CLI 的内部调试信息)
// 这些 JSON 格式的消息包含 hook_started、hook_response、SessionStart 等
// 参考网页端的 JSONL 适配器过滤逻辑
var lines = output.Split('\n');
var filteredLines = new List<string>();
foreach (var line in lines)
{
var trimmedLine = line.Trim();
// 跳过空行
if (string.IsNullOrWhiteSpace(trimmedLine))
{
filteredLines.Add(line);
continue;
}
// 跳过所有系统消息(与网页端行为一致)
// 系统消息格式: {"type":"system",...}
// 包括: init、hook_started、hook_response、hook_event 等
if (trimmedLine.StartsWith("{") && trimmedLine.Contains("\"type\":\"system\""))
{
// 过滤所有 type 为 system 的 JSON 消息
continue;
}
filteredLines.Add(line);
}
var formatted = string.Join('\n', filteredLines);
// 移除过多的空行(最多保留连续 2 个空行)
formatted = System.Text.RegularExpressions.Regex.Replace(
formatted,
@"\n{3,}",
"\n\n");
// 限制最大长度(飞书卡片有内容限制)
const int maxLength = 10000;
if (formatted.Length > maxLength)
{
formatted = formatted[..maxLength] + "\n\n... (内容过长,已截断)";
}
return formatted.Trim();
}
private string ResolveDefaultToolId()
{
var configured = NormalizeToolId(_options.DefaultToolId);
if (IsConfiguredToolAvailable(configured))
{
return configured!;
}
foreach (var candidate in new[] { FallbackToolId, "codex", "opencode" })
{
var normalized = NormalizeToolId(candidate);
if (IsConfiguredToolAvailable(normalized))
{
return normalized!;
}
}
return _cliExecutor.GetAvailableTools().FirstOrDefault()?.Id ?? FallbackToolId;
}
private bool IsConfiguredToolAvailable(string? toolId)
{
var normalized = NormalizeToolId(toolId);
return !string.IsNullOrWhiteSpace(normalized) && _cliExecutor.GetTool(normalized) != null;
}
private static string? NormalizeToolId(string? toolId)
{
if (string.IsNullOrWhiteSpace(toolId))
{
return null;
}
if (toolId.Equals("claude", StringComparison.OrdinalIgnoreCase))
{
return "claude-code";
}
if (toolId.Equals("opencode-cli", StringComparison.OrdinalIgnoreCase))
{
return "opencode";
}
return toolId;
}
private static string? ExtractFallbackOutput(string fullOutput, ICliToolAdapter adapter)
{
if (string.IsNullOrWhiteSpace(fullOutput))
{
return null;
}
string? lastUsefulContent = null;
var lines = fullOutput.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var rawLine in lines)
{
var line = rawLine.Trim();
if (string.IsNullOrWhiteSpace(line))
{
continue;
}
var outputEvent = adapter.ParseOutputLine(line);
if (outputEvent == null || string.IsNullOrWhiteSpace(outputEvent.Content))
{
continue;
}
if (outputEvent.EventType is "result" or "error" or "raw" or "assistant" or "assistant:message" or "stream_event")
{
lastUsefulContent = outputEvent.Content.Trim();
}
}
return lastUsefulContent;
}
/// <summary>
/// 发送文本消息
/// </summary>
public async Task<string> SendMessageAsync(string chatId, string content, string? username = null, string? appId = null)
{
_logger.LogDebug("Sending message to chat {ChatId}: {Content}", chatId, content);
var effectiveOptions = await ResolveEffectiveOptionsAsync(username, chatId, appId);
var messageId = await _cardKit.SendTextMessageAsync(chatId, content, optionsOverride: effectiveOptions);
_logger.LogDebug("Text message sent: MessageId={MessageId}", messageId);
return messageId;
}
/// <summary>
/// 回复消息
/// </summary>
public async Task<string> ReplyMessageAsync(string messageId, string content, string? username = null, string? appId = null)
{
_logger.LogDebug("Replying to message {MessageId}: {Content}", messageId, content);
var effectiveOptions = await ResolveEffectiveOptionsAsync(username, appId: appId);
var replyMessageId = await _cardKit.ReplyTextMessageAsync(messageId, content, optionsOverride: effectiveOptions);
_logger.LogDebug("Text reply sent: MessageId={ReplyMessageId}", replyMessageId);
return replyMessageId;
}
/// <summary>
/// 发送流式消息(核心方法)
/// 立即创建卡片并发送,返回流式句柄用于后续更新
/// </summary>
public async Task<FeishuStreamingHandle> SendStreamingMessageAsync(
string chatId,
string initialContent,
string? replyToMessageId = null,
string? username = null,
string? appId = null)
{
_logger.LogDebug(
"Creating streaming message for chat {ChatId} (reply to: {ReplyMessageId})",
chatId,
replyToMessageId ?? "none");
var effectiveOptions = await ResolveEffectiveOptionsAsync(username, chatId, appId);
// 通过 CardKit 客户端创建流式句柄
var handle = await _cardKit.CreateStreamingHandleAsync(
chatId,
replyToMessageId,
initialContent,
effectiveOptions.DefaultCardTitle,
optionsOverride: effectiveOptions);
_logger.LogDebug(
"Streaming handle created: CardId={CardId}, MessageId={MessageId}",
handle.CardId,
handle.MessageId);
return handle;
}
private async Task<FeishuOptions> ResolveEffectiveOptionsAsync(string? username = null, string? chatId = null, string? appId = null)
{
using var scope = _serviceProvider.CreateScope();
var userFeishuBotConfigService = scope.ServiceProvider.GetRequiredService<IUserFeishuBotConfigService>();
if (!string.IsNullOrWhiteSpace(appId))
{
var appOptions = await userFeishuBotConfigService.GetEffectiveOptionsByAppIdAsync(appId);
if (appOptions != null)
{
return appOptions;
}
}
var resolvedUsername = username;
if (string.IsNullOrWhiteSpace(resolvedUsername) && !string.IsNullOrWhiteSpace(chatId))
{
resolvedUsername = GetSessionUsername(chatId.ToLowerInvariant());
}
if (string.IsNullOrWhiteSpace(resolvedUsername))
{
var userContext = scope.ServiceProvider.GetRequiredService<IUserContextService>();
if (userContext.IsAuthenticated())
{
resolvedUsername = userContext.GetCurrentUsername();
}
}
return await userFeishuBotConfigService.GetEffectiveOptionsAsync(resolvedUsername);
}
/// <summary>
/// 处理 JSONL 输出块,解析并提取助手消息
/// 使用缓冲区处理跨 chunk 的不完整 JSON 行
/// </summary>
private void ProcessJsonlChunk(string content, ICliToolAdapter adapter, StringBuilder assistantMessageBuilder, StringBuilder jsonlBuffer)
{
if (string.IsNullOrEmpty(content))
{
return;
}
// 将新内容添加到缓冲区
jsonlBuffer.Append(content);
// 处理缓冲区中的完整行
while (true)
{
var bufferContent = jsonlBuffer.ToString();
var newlineIndex = bufferContent.IndexOf('\n');
// 如果没有完整的行,等待更多数据
if (newlineIndex < 0)
{
break;
}
// 提取完整的行
var line = bufferContent.Substring(0, newlineIndex).TrimEnd('\r');
jsonlBuffer.Remove(0, newlineIndex + 1);
// 处理这一行
ProcessJsonlLine(line, adapter, assistantMessageBuilder);
}
}
/// <summary>
/// 处理单行 JSONL
/// </summary>
private void ProcessJsonlLine(string line, ICliToolAdapter adapter, StringBuilder assistantMessageBuilder)
{
var trimmedLine = line.Trim();
if (string.IsNullOrWhiteSpace(trimmedLine))
{
return;
}
// 跳过非 JSON 行
if (!trimmedLine.StartsWith("{"))
{
return;
}
try
{
// 使用适配器解析输出行
var outputEvent = adapter.ParseOutputLine(trimmedLine);
if (outputEvent == null)
{
return;
}
// 提取助手消息
var assistantMessage = adapter.ExtractAssistantMessage(outputEvent);
if (!string.IsNullOrEmpty(assistantMessage))
{
assistantMessageBuilder.Append(assistantMessage);
}
}
catch (Exception ex)
{
_logger.LogDebug(ex, "Failed to parse JSONL line: {Line}", trimmedLine.Length > 100 ? trimmedLine[..100] : trimmedLine);
}
}
}