-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathFeishuCommandService.cs
More file actions
311 lines (280 loc) · 10.9 KB
/
FeishuCommandService.cs
File metadata and controls
311 lines (280 loc) · 10.9 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
using WebCodeCli.Domain.Domain.Model.Channels;
using WebCodeCli.Domain.Domain.Service;
using Microsoft.Extensions.Logging;
namespace WebCodeCli.Domain.Domain.Service.Channels;
/// <summary>
/// 飞书命令服务
/// 按 CLI 工具聚合内置命令、技能和插件命令
/// </summary>
public class FeishuCommandService
{
private readonly ILogger<FeishuCommandService> _logger;
private readonly CommandScannerService _commandScannerService;
private readonly Dictionary<string, List<FeishuCommand>> _cachedCommands = new(StringComparer.OrdinalIgnoreCase);
private readonly object _lock = new();
public FeishuCommandService(ILogger<FeishuCommandService> logger, CommandScannerService commandScannerService)
{
_logger = logger;
_commandScannerService = commandScannerService;
}
public async Task<List<FeishuCommand>> GetCommandsAsync(string? toolId = null)
{
var normalizedToolId = NormalizeToolId(toolId);
lock (_lock)
{
if (_cachedCommands.TryGetValue(normalizedToolId, out var cached))
{
return cached.ToList();
}
}
await LoadCommandsAsync(normalizedToolId);
lock (_lock)
{
return _cachedCommands.TryGetValue(normalizedToolId, out var cached)
? cached.ToList()
: new List<FeishuCommand>();
}
}
public async Task RefreshCommandsAsync(string? toolId = null)
{
var normalizedToolId = NormalizeToolId(toolId);
_logger.LogInformation("🔄 [FeishuHelp] 刷新命令列表, ToolId={ToolId}", normalizedToolId);
lock (_lock)
{
if (string.IsNullOrWhiteSpace(normalizedToolId))
{
_cachedCommands.Clear();
}
else
{
_cachedCommands.Remove(normalizedToolId);
}
}
await LoadCommandsAsync(normalizedToolId);
lock (_lock)
{
var count = _cachedCommands.TryGetValue(normalizedToolId, out var cached) ? cached.Count : 0;
_logger.LogInformation("✅ [FeishuHelp] 命令列表已刷新, ToolId={ToolId}, Count={Count}", normalizedToolId, count);
}
}
public async Task<FeishuCommand?> GetCommandAsync(string commandId, string? toolId = null)
{
var commands = await GetCommandsAsync(toolId);
return commands.FirstOrDefault(c => c.Id.Equals(commandId, StringComparison.OrdinalIgnoreCase));
}
public async Task<List<FeishuCommandCategory>> GetCategorizedCommandsAsync(string? toolId = null)
{
var commands = await GetCommandsAsync(toolId);
return commands
.GroupBy(c => c.Category)
.Select(g => new FeishuCommandCategory
{
Id = g.Key,
Name = GetCategoryName(g.Key),
Commands = g.OrderBy(c => c.Name, StringComparer.OrdinalIgnoreCase).ToList()
})
.OrderBy(c => GetCategoryOrder(c.Id))
.ThenBy(c => c.Name, StringComparer.OrdinalIgnoreCase)
.ToList();
}
public async Task<List<FeishuCommandCategory>> FilterCommandsAsync(string keyword, string? toolId = null)
{
if (string.IsNullOrWhiteSpace(keyword))
{
return await GetCategorizedCommandsAsync(toolId);
}
var commands = await GetCommandsAsync(toolId);
var lowerKeyword = keyword.ToLowerInvariant();
var filtered = commands.Where(c =>
c.Name.ToLowerInvariant().Contains(lowerKeyword)
|| c.Description.ToLowerInvariant().Contains(lowerKeyword)
|| c.Id.ToLowerInvariant().Contains(lowerKeyword)
|| c.ExecuteText.ToLowerInvariant().Contains(lowerKeyword))
.ToList();
return filtered
.GroupBy(c => c.Category)
.Select(g => new FeishuCommandCategory
{
Id = g.Key,
Name = GetCategoryName(g.Key),
Commands = g.OrderBy(c => c.Name, StringComparer.OrdinalIgnoreCase).ToList()
})
.OrderBy(c => GetCategoryOrder(c.Id))
.ThenBy(c => c.Name, StringComparer.OrdinalIgnoreCase)
.ToList();
}
private Task LoadCommandsAsync(string normalizedToolId)
{
lock (_lock)
{
if (_cachedCommands.ContainsKey(normalizedToolId))
{
return Task.CompletedTask;
}
}
var commands = new List<FeishuCommand>();
commands.AddRange(GetFeishuBuiltInCommands(normalizedToolId));
commands.AddRange(GetToolBuiltInCommands(normalizedToolId));
foreach (var cmd in _commandScannerService.GetAllCommands(normalizedToolId))
{
commands.Add(new FeishuCommand
{
ToolId = normalizedToolId,
Id = cmd.Name.TrimStart('/', '$'),
Name = cmd.Name.StartsWith("/") || cmd.Name.StartsWith("$") ? cmd.Name : cmd.Invocation,
Description = cmd.Description,
Usage = string.IsNullOrWhiteSpace(cmd.Usage) ? cmd.Invocation : cmd.Usage,
ExecuteText = string.IsNullOrWhiteSpace(cmd.Invocation) ? cmd.Name : cmd.Invocation,
Category = cmd.Category
});
}
var deduplicated = commands
.GroupBy(c => c.Id, StringComparer.OrdinalIgnoreCase)
.Select(g => g.OrderBy(GetCommandPriority).First())
.OrderBy(c => GetCategoryOrder(c.Category))
.ThenBy(c => c.Name, StringComparer.OrdinalIgnoreCase)
.ToList();
lock (_lock)
{
_cachedCommands[normalizedToolId] = deduplicated;
}
return Task.CompletedTask;
}
private static int GetCommandPriority(FeishuCommand command)
{
return command.Category switch
{
"feishu_builtin" => 0,
"core_builtin" => 1,
"skills_project" => 2,
"commands_project" => 3,
"skills_global" => 4,
"commands_global" => 5,
"plugins" => 6,
_ => 10
};
}
private static string NormalizeToolId(string? toolId)
{
if (string.IsNullOrWhiteSpace(toolId))
{
return "claude-code";
}
if (toolId.Equals("claude", StringComparison.OrdinalIgnoreCase))
{
return "claude-code";
}
if (toolId.Equals("opencode-cli", StringComparison.OrdinalIgnoreCase))
{
return "opencode";
}
return toolId;
}
private static List<FeishuCommand> GetFeishuBuiltInCommands(string toolId)
{
return new List<FeishuCommand>
{
new()
{
ToolId = toolId,
Id = "feishuhelp",
Name = "/feishuhelp",
Description = "显示当前工具可用的内置命令、技能和插件命令",
Usage = "/feishuhelp [关键词]",
ExecuteText = "/feishuhelp",
Category = "feishu_builtin"
},
new()
{
ToolId = toolId,
Id = "feishusessions",
Name = "/feishusessions",
Description = "查看、切换和创建当前飞书聊天绑定的会话",
Usage = "/feishusessions",
ExecuteText = "/feishusessions",
Category = "feishu_builtin"
},
new()
{
ToolId = toolId,
Id = "feishuprojects",
Name = "/feishuprojects",
Description = "查看、创建、克隆、拉取和删除当前飞书聊天绑定的项目",
Usage = "/feishuprojects",
ExecuteText = "/feishuprojects",
Category = "feishu_builtin"
}
};
}
private static IEnumerable<FeishuCommand> GetToolBuiltInCommands(string toolId)
{
return toolId switch
{
"claude-code" => new[]
{
BuildToolBuiltIn(toolId, "init", "/init", "初始化当前项目的上下文与约定"),
BuildToolBuiltIn(toolId, "clear", "/clear", "清空当前会话上下文"),
BuildToolBuiltIn(toolId, "compact", "/compact", "压缩当前会话上下文,减少 token 占用")
},
"codex" => new[]
{
BuildToolBuiltIn(toolId, "init", "/init", "初始化当前项目的上下文与约定"),
BuildToolBuiltIn(toolId, "help", "/help", "显示 Codex 交互命令帮助"),
BuildToolBuiltIn(toolId, "status", "/status", "查看当前会话状态和工作区信息"),
BuildToolBuiltIn(toolId, "approvals", "/approvals", "查看或调整当前审批策略"),
BuildToolBuiltIn(toolId, "model", "/model", "查看或切换当前模型"),
BuildToolBuiltIn(toolId, "reasoning", "/reasoning", "查看或调整推理强度")
},
"opencode" => new[]
{
BuildToolBuiltIn(toolId, "init", "/init", "为当前项目初始化 OpenCode 工作上下文"),
BuildToolBuiltIn(toolId, "help", "/help", "显示 OpenCode 交互命令帮助"),
BuildToolBuiltIn(toolId, "undo", "/undo", "回滚上一条交互产生的变更"),
BuildToolBuiltIn(toolId, "redo", "/redo", "恢复最近一次撤销的变更"),
BuildToolBuiltIn(toolId, "share", "/share", "分享当前会话")
},
_ => Array.Empty<FeishuCommand>()
};
}
private static FeishuCommand BuildToolBuiltIn(string toolId, string id, string name, string description)
{
return new FeishuCommand
{
ToolId = toolId,
Id = id,
Name = name,
Description = description,
Usage = name,
ExecuteText = name,
Category = "core_builtin"
};
}
private static int GetCategoryOrder(string categoryId)
{
return categoryId switch
{
"feishu_builtin" => 0,
"core_builtin" => 1,
"skills_project" => 2,
"skills_global" => 3,
"commands_project" => 4,
"commands_global" => 5,
"plugins" => 6,
_ => 99
};
}
private string GetCategoryName(string categoryId)
{
return categoryId switch
{
"feishu_builtin" => "🤖 飞书命令",
"core_builtin" => "📦 内置命令",
"skills_project" => "📁 项目技能",
"skills_global" => "🌐 全局技能",
"commands_project" => "🧩 项目命令",
"commands_global" => "🛠️ 全局命令",
"plugins" => "🔌 插件",
_ => categoryId
};
}
}