-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathcommandRouter.js
More file actions
81 lines (67 loc) · 2.23 KB
/
commandRouter.js
File metadata and controls
81 lines (67 loc) · 2.23 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
import { analyzeWechatMessages } from '../../analysis/wechatAnalyzer.js'
import { getWechatRuntimeConfig } from '../../config/env.js'
import { runOpenCli } from '../../adapters/opencli.js'
function stripMention(content, botName) {
return content.replace(botName, '').trim()
}
function parseTarget(tokens) {
const type = tokens[1]
const value = tokens.slice(2).join(' ').trim()
if (['群', '群聊', 'room', 'group'].includes(type)) {
return { room: value }
}
if (['好友', 'friend', 'contact'].includes(type)) {
return { friend: value }
}
return {}
}
export async function handleWechatCommand(content, context = {}) {
const config = getWechatRuntimeConfig()
const normalized = stripMention(content, config.botName)
if (!normalized.startsWith(config.commandPrefix)) {
return { handled: false }
}
const commandLine = normalized.slice(config.commandPrefix.length).trim()
const tokens = commandLine.split(/\s+/).filter(Boolean)
const command = tokens[0]
if (['分析', 'analyze', '统计', 'stats'].includes(command)) {
const statsOnly = ['统计', 'stats'].includes(command)
const target = parseTarget(tokens)
const result = await analyzeWechatMessages({
...target,
serviceType: context.serviceType,
dataDir: config.dataDir,
statsOnly,
})
if (statsOnly || !result.analysis) {
return {
handled: true,
reply: [
`${result.target}`,
`消息数:${result.stats.totalMessages}`,
`文本消息:${result.stats.textMessages}`,
`平均长度:${result.stats.averageTextLength}`,
`高频发言:${result.stats.topSpeakers.map((item) => `${item.name}(${item.count})`).join(',') || '无'}`,
].join('\n'),
}
}
return {
handled: true,
reply: result.analysis,
}
}
if (command === 'opencli') {
if (!config.enableRemoteOpenCli) {
return {
handled: true,
reply: '远程 OpenCLI 执行未开启。需要在 .env 中显式设置 ENABLE_REMOTE_OPENCLI=true。',
}
}
await runOpenCli(tokens.slice(1))
return {
handled: true,
reply: 'OpenCLI 命令已执行,结果请看本机控制台。',
}
}
return { handled: false }
}