foliko 1.0.40 → 1.0.41
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.
- package/.claude/settings.local.json +35 -1
- package/cli/src/ui/chat-ui.js +7 -2
- package/docs/features.md +120 -0
- package/examples/basic.js +110 -110
- package/examples/bootstrap.js +19 -0
- package/examples/mcp-example.js +53 -53
- package/examples/skill-example.js +49 -49
- package/examples/test-chat.js +6 -0
- package/examples/test-mcp.js +79 -79
- package/examples/test-reload.js +61 -61
- package/examples/test-web-plugin.js +98 -0
- package/package.json +4 -3
- package/plugins/default-plugins.js +11 -10
- package/plugins/feishu-plugin.js +291 -537
- package/plugins/scheduler-plugin.js +1 -1
- package/plugins/subagent-plugin.js +4 -4
- package/plugins/telegram-plugin.js +307 -522
- package/plugins/think-plugin.js +2 -2
- package/plugins/web-plugin.js +542 -0
- package/plugins/weixin-plugin.js +320 -274
- package/src/core/agent-chat.js +24 -27
- package/src/core/agent.js +77 -0
- package/src/core/framework.js +35 -0
- package/src/executors/executor-base.js +58 -58
- package/test-server.js +25 -0
package/plugins/weixin-plugin.js
CHANGED
|
@@ -9,343 +9,389 @@
|
|
|
9
9
|
|
|
10
10
|
const { Plugin } = require('../src/core/plugin-base')
|
|
11
11
|
const { z } = require('zod')
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
this._framework = null
|
|
33
|
-
this._bot = null
|
|
34
|
-
this._sessionPlugin = null
|
|
35
|
-
this._sessionDeleteHandler = null
|
|
36
|
-
this._sessionAgents = new Map() // userId -> Agent
|
|
37
|
-
this._qrcodeTerminal = null
|
|
38
|
-
this._origStderrWrite = null
|
|
39
|
-
this._initialized = false
|
|
12
|
+
const { WeixinBot } = require('@chnak/weixin-bot')
|
|
13
|
+
|
|
14
|
+
class WeixinPlugin extends Plugin {
|
|
15
|
+
constructor(config = {}) {
|
|
16
|
+
super()
|
|
17
|
+
this.name = 'weixin'
|
|
18
|
+
this.version = '1.0.0'
|
|
19
|
+
this.description = '微信对话插件,使用微信网页账号进行对话'
|
|
20
|
+
this.priority = 80
|
|
21
|
+
// 默认不启用,需要在 plugins.json 中设置 enabled: true
|
|
22
|
+
this.enabled = false
|
|
23
|
+
this.path=`.agent/data`
|
|
24
|
+
this.systemPrompt='你是一个微信助手。回复内容不要使用markdown格式文本'
|
|
25
|
+
|
|
26
|
+
this.config = {
|
|
27
|
+
forceLogin: config.forceLogin || process.env.WEIXIN_FORCE_LOGIN === 'true',
|
|
28
|
+
qrcodeTerminal: config.qrcodeTerminal !== false && process.env.WEIXIN_QRCODE_TERMINAL !== 'false',
|
|
29
|
+
allowedUsers: config.allowedUsers || []
|
|
40
30
|
}
|
|
41
31
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
32
|
+
this._framework = null
|
|
33
|
+
this._bot = null
|
|
34
|
+
this._sessionPlugin = null
|
|
35
|
+
this._sessionDeleteHandler = null
|
|
36
|
+
this._sessionAgents = new Map() // userId -> Agent
|
|
37
|
+
this._qrcodeTerminal = null
|
|
38
|
+
this._origStderrWrite = null
|
|
39
|
+
this._initialized = false
|
|
40
|
+
}
|
|
46
41
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
42
|
+
install(framework) {
|
|
43
|
+
this._framework = framework
|
|
44
|
+
return this
|
|
45
|
+
}
|
|
51
46
|
|
|
52
|
-
|
|
53
|
-
|
|
47
|
+
async start(framework) {
|
|
48
|
+
// 防止重复初始化
|
|
49
|
+
if (this._initialized) return this
|
|
50
|
+
this._initialized = true
|
|
54
51
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
52
|
+
// 获取 SessionPlugin 引用
|
|
53
|
+
this._sessionPlugin = framework.pluginManager.get('session')
|
|
54
|
+
|
|
55
|
+
// 监听 SessionPlugin 的会话删除事件
|
|
56
|
+
if (this._sessionPlugin) {
|
|
57
|
+
this._sessionDeleteHandler = (sessionId) => {
|
|
58
|
+
// 只处理 weixin 会话的删除
|
|
59
|
+
if (sessionId.startsWith('weixin_')) {
|
|
60
|
+
console.log(`[WeChat] Session deleted: ${sessionId}`)
|
|
62
61
|
}
|
|
63
|
-
this._sessionPlugin.on('session:deleted', this._sessionDeleteHandler)
|
|
64
62
|
}
|
|
63
|
+
this._sessionPlugin.on('session:deleted', this._sessionDeleteHandler)
|
|
64
|
+
}
|
|
65
65
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
66
|
+
// 监听定时提醒事件
|
|
67
|
+
if (this._framework) {
|
|
68
|
+
this._framework.on('scheduler:reminder', async (data) => {
|
|
69
|
+
console.log('[WeChat] Received scheduler reminder:', data)
|
|
70
|
+
await this._handleScheduledReminder(data)
|
|
69
71
|
})
|
|
70
|
-
return this
|
|
71
72
|
}
|
|
72
73
|
|
|
73
|
-
|
|
74
|
+
// 异步初始化 Bot
|
|
75
|
+
this._initBotAsync().catch(err => {
|
|
76
|
+
console.error('[WeChat] Failed to initialize bot:', err.message)
|
|
77
|
+
})
|
|
78
|
+
return this
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async _initBotAsync() {
|
|
74
82
|
|
|
75
|
-
|
|
83
|
+
// 如果启用终端二维码渲染
|
|
84
|
+
if (this.config.qrcodeTerminal) {
|
|
76
85
|
try {
|
|
77
|
-
|
|
78
|
-
|
|
86
|
+
this._qrcodeTerminal = require('qrcode-terminal')
|
|
87
|
+
this._interceptQRCode()
|
|
79
88
|
} catch (err) {
|
|
80
|
-
console.warn('[WeChat]
|
|
81
|
-
console.warn('[WeChat] Make sure @chnak/weixin-bot is installed: npm install @chnak/weixin-bot qrcode-terminal')
|
|
82
|
-
return
|
|
89
|
+
console.warn('[WeChat] qrcode-terminal not installed:', err.message)
|
|
83
90
|
}
|
|
91
|
+
}
|
|
84
92
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
93
|
+
this._bot = new WeixinBot({
|
|
94
|
+
tokenPath:`${this.path}/${this.name}.json`,
|
|
95
|
+
onError: (err) => {
|
|
96
|
+
console.error('[WeChat] Error:', err instanceof Error ? err.stack ?? err.message : String(err))
|
|
97
|
+
},
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
const loginOptions = { force: this.config.forceLogin }
|
|
101
|
+
console.log('[WeChat]', this.config.forceLogin ? '强制重新扫码登录...' : '正在登录(已有凭证则自动跳过扫码)...')
|
|
102
|
+
|
|
103
|
+
const creds = await this._bot.login(loginOptions)
|
|
104
|
+
console.log('[WeChat] 登录成功 — Bot ID:', creds.accountId)
|
|
105
|
+
console.log('[WeChat] 关联用户:', creds.userId)
|
|
106
|
+
console.log('[WeChat] API 地址:', creds.baseUrl)
|
|
107
|
+
|
|
108
|
+
// 注册消息处理
|
|
109
|
+
this._bot.onMessage(async (msg) => {
|
|
110
|
+
await this._handleMessage(msg)
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
// 启动Bot
|
|
114
|
+
await this._bot.run()
|
|
115
|
+
console.log('[WeChat] 开始接收微信消息')
|
|
116
|
+
}
|
|
104
117
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
118
|
+
/**
|
|
119
|
+
* 拦截 SDK 的 stderr 输出,渲染二维码到终端
|
|
120
|
+
*/
|
|
121
|
+
_interceptQRCode() {
|
|
122
|
+
if (!this._qrcodeTerminal) return
|
|
123
|
+
|
|
124
|
+
this._origStderrWrite = process.stderr.write.bind(process.stderr)
|
|
125
|
+
process.stderr.write = ((chunk, ...args) => {
|
|
126
|
+
const str = typeof chunk === 'string' ? chunk : chunk.toString()
|
|
127
|
+
// 检测到登录 URL,渲染二维码
|
|
128
|
+
if (str.startsWith('https://') && str.includes('qrcode=')) {
|
|
129
|
+
const url = str.trim()
|
|
130
|
+
this._qrcodeTerminal.generate(url, { small: true }, (qr) => {
|
|
131
|
+
this._origStderrWrite(qr + '\n')
|
|
132
|
+
})
|
|
133
|
+
}
|
|
134
|
+
return this._origStderrWrite(chunk, ...args)
|
|
135
|
+
})
|
|
136
|
+
}
|
|
109
137
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
138
|
+
/**
|
|
139
|
+
* 获取主Agent
|
|
140
|
+
*/
|
|
141
|
+
_getMainAgent() {
|
|
142
|
+
if (this._framework._mainAgent) {
|
|
143
|
+
return this._framework._mainAgent
|
|
144
|
+
}
|
|
145
|
+
const agents = this._framework._agents || []
|
|
146
|
+
return agents.length > 0 ? agents[agents.length - 1] : null
|
|
147
|
+
}
|
|
114
148
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
149
|
+
/**
|
|
150
|
+
* 获取或创建会话Agent
|
|
151
|
+
* 使用 SessionPlugin 统一管理会话历史
|
|
152
|
+
*/
|
|
153
|
+
_getSessionAgent(userId) {
|
|
154
|
+
// 检查缓存
|
|
155
|
+
if (this._sessionAgents.has(userId)) {
|
|
156
|
+
const agent = this._sessionAgents.get(userId)
|
|
157
|
+
console.log('[WeChat] Reusing cached session agent for userId:', userId)
|
|
158
|
+
return { agent, sessionId: `weixin_${userId}` }
|
|
118
159
|
}
|
|
119
160
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
this._qrcodeTerminal.generate(url, { small: true }, (qr) => {
|
|
133
|
-
this._origStderrWrite(qr + '\n')
|
|
134
|
-
})
|
|
135
|
-
}
|
|
136
|
-
return this._origStderrWrite(chunk, ...args)
|
|
161
|
+
// 创建新 agent
|
|
162
|
+
const agent = this._framework.createSessionAgent(`weixin_${userId}`, {
|
|
163
|
+
systemPrompt: this.systemPrompt
|
|
164
|
+
})
|
|
165
|
+
this._sessionAgents.set(userId, agent)
|
|
166
|
+
console.log('[WeChat] Created new session agent for userId:', userId)
|
|
167
|
+
|
|
168
|
+
// 使用 SessionPlugin 管理会话历史
|
|
169
|
+
if (this._sessionPlugin) {
|
|
170
|
+
const sessionId = `weixin_${userId}`
|
|
171
|
+
this._sessionPlugin.getOrCreateSession(sessionId, {
|
|
172
|
+
metadata: { platform: 'weixin', userId }
|
|
137
173
|
})
|
|
138
174
|
}
|
|
139
175
|
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
176
|
+
console.log(`[WeChat] Session ready for user: ${userId}`)
|
|
177
|
+
return { agent, sessionId: `weixin_${userId}` }
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* 处理消息
|
|
182
|
+
*/
|
|
183
|
+
async _handleMessage(msg) {
|
|
184
|
+
if (!msg || !msg.userId) return
|
|
185
|
+
|
|
186
|
+
const userId = msg.userId
|
|
187
|
+
// 从 SessionPlugin 获取历史消息数量
|
|
188
|
+
let messageCount = 0
|
|
189
|
+
if (this._sessionPlugin) {
|
|
190
|
+
const session = this._sessionPlugin.getSession(`weixin_${userId}`)
|
|
191
|
+
messageCount = session?.messages?.length || 0
|
|
149
192
|
}
|
|
150
193
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
* 使用 SessionPlugin 统一管理会话历史
|
|
154
|
-
*/
|
|
155
|
-
_getSessionAgent(userId) {
|
|
156
|
-
// 检查缓存
|
|
157
|
-
if (this._sessionAgents.has(userId)) {
|
|
158
|
-
const agent = this._sessionAgents.get(userId)
|
|
159
|
-
console.log('[WeChat] Reusing cached session agent for userId:', userId)
|
|
160
|
-
return { agent, sessionId: `weixin_${userId}` }
|
|
161
|
-
}
|
|
194
|
+
console.log(`[WeChat] #${messageCount + 1} | 类型: ${msg.type} | 用户: ${userId}`)
|
|
195
|
+
console.log(`[WeChat] 内容: ${msg.text}`)
|
|
162
196
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
console.log('[WeChat] Created new session agent for userId:', userId)
|
|
197
|
+
// 非文本消息暂不处理
|
|
198
|
+
if (msg.type !== 'text' || !msg.text) {
|
|
199
|
+
console.log('[WeChat] Unsupported message type or no text')
|
|
200
|
+
return
|
|
201
|
+
}
|
|
169
202
|
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
this._sessionPlugin.getOrCreateSession(sessionId, {
|
|
174
|
-
metadata: { platform: 'weixin', userId }
|
|
175
|
-
})
|
|
176
|
-
}
|
|
203
|
+
const text = msg.text.trim()
|
|
204
|
+
await this._processChat(userId, text, msg)
|
|
205
|
+
}
|
|
177
206
|
|
|
178
|
-
|
|
179
|
-
|
|
207
|
+
/**
|
|
208
|
+
* 处理对话
|
|
209
|
+
*/
|
|
210
|
+
async _processChat(userId, text, originalMsg) {
|
|
211
|
+
const sessionInfo = this._getSessionAgent(userId)
|
|
212
|
+
if (!sessionInfo) {
|
|
213
|
+
console.error('[WeChat] No session agent available')
|
|
214
|
+
return
|
|
180
215
|
}
|
|
181
216
|
|
|
182
|
-
|
|
183
|
-
* 处理消息
|
|
184
|
-
*/
|
|
185
|
-
async _handleMessage(msg) {
|
|
186
|
-
if (!msg || !msg.userId) return
|
|
217
|
+
const { agent, sessionId } = sessionInfo
|
|
187
218
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
const session = this._sessionPlugin.getSession(`weixin_${userId}`)
|
|
193
|
-
messageCount = session?.messages?.length || 0
|
|
194
|
-
}
|
|
219
|
+
// 使用 SessionPlugin 添加用户消息到历史
|
|
220
|
+
if (this._sessionPlugin) {
|
|
221
|
+
this._sessionPlugin.addMessage(sessionId, { role: 'user', content: text })
|
|
222
|
+
}
|
|
195
223
|
|
|
196
|
-
|
|
197
|
-
|
|
224
|
+
// 发送正在输入状态
|
|
225
|
+
try {
|
|
226
|
+
await this._bot.sendTyping(userId)
|
|
227
|
+
} catch { /* typing 失败不影响回复 */ }
|
|
198
228
|
|
|
199
|
-
|
|
200
|
-
if (msg.type !== 'text' || !msg.text) {
|
|
201
|
-
console.log('[WeChat] Unsupported message type or no text')
|
|
202
|
-
return
|
|
203
|
-
}
|
|
229
|
+
await new Promise((resolve) => setTimeout(resolve, 1000))
|
|
204
230
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
}
|
|
231
|
+
try {
|
|
232
|
+
let fullResponse = ''
|
|
208
233
|
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
return
|
|
234
|
+
// 使用流式响应
|
|
235
|
+
for await (const chunk of agent.chatStream(text, {
|
|
236
|
+
sessionId: sessionId
|
|
237
|
+
})) {
|
|
238
|
+
if (chunk.type === 'text' && chunk.text) {
|
|
239
|
+
fullResponse += chunk.text
|
|
240
|
+
}
|
|
217
241
|
}
|
|
218
242
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
// 使用 SessionPlugin 添加用户消息到历史
|
|
243
|
+
// 保存助手回复到历史(使用 SessionPlugin)
|
|
222
244
|
if (this._sessionPlugin) {
|
|
223
|
-
this._sessionPlugin.addMessage(sessionId, { role: '
|
|
245
|
+
this._sessionPlugin.addMessage(sessionId, { role: 'assistant', content: fullResponse })
|
|
224
246
|
}
|
|
225
247
|
|
|
226
|
-
//
|
|
227
|
-
|
|
228
|
-
await this._bot.
|
|
229
|
-
|
|
248
|
+
// 发送回复
|
|
249
|
+
if (fullResponse) {
|
|
250
|
+
await this._bot.reply(originalMsg, fullResponse)
|
|
251
|
+
console.log(`[WeChat] 回复成功 (${fullResponse.length} 字符)`)
|
|
252
|
+
} else {
|
|
253
|
+
await this._bot.reply(originalMsg, '抱歉,我没有收到有效的回复。')
|
|
254
|
+
}
|
|
230
255
|
|
|
231
|
-
|
|
256
|
+
} catch (err) {
|
|
257
|
+
console.error('[WeChat] Chat error:', err)
|
|
258
|
+
await this._bot.reply(originalMsg, `发生错误:${err.message}`)
|
|
259
|
+
}
|
|
260
|
+
}
|
|
232
261
|
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
}
|
|
243
|
-
}
|
|
262
|
+
/**
|
|
263
|
+
* 权限检查
|
|
264
|
+
*/
|
|
265
|
+
_checkPermission(userId) {
|
|
266
|
+
if (!this.config.allowedUsers || this.config.allowedUsers.length === 0) {
|
|
267
|
+
return true
|
|
268
|
+
}
|
|
269
|
+
return this.config.allowedUsers.includes(userId)
|
|
270
|
+
}
|
|
244
271
|
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
272
|
+
/**
|
|
273
|
+
* 清除会话
|
|
274
|
+
*/
|
|
275
|
+
_clearSession(userId) {
|
|
276
|
+
// 清除 SessionPlugin 中的会话(会触发 session:deleted 事件)
|
|
277
|
+
if (this._sessionPlugin) {
|
|
278
|
+
const sessionId = `weixin_${userId}`
|
|
279
|
+
this._sessionPlugin.deleteSession(sessionId)
|
|
280
|
+
}
|
|
281
|
+
}
|
|
249
282
|
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
await this._bot.reply(originalMsg, '抱歉,我没有收到有效的回复。')
|
|
256
|
-
}
|
|
283
|
+
/**
|
|
284
|
+
* 处理定时提醒
|
|
285
|
+
*/
|
|
286
|
+
async _handleScheduledReminder(data) {
|
|
287
|
+
const { taskName, message, sessionId } = data
|
|
257
288
|
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
}
|
|
289
|
+
if (!this._bot) {
|
|
290
|
+
console.warn('[WeChat] Bot not ready, cannot send reminder')
|
|
291
|
+
return
|
|
262
292
|
}
|
|
263
293
|
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
*/
|
|
267
|
-
_checkPermission(userId) {
|
|
268
|
-
if (!this.config.allowedUsers || this.config.allowedUsers.length === 0) {
|
|
269
|
-
return true
|
|
270
|
-
}
|
|
271
|
-
return this.config.allowedUsers.includes(userId)
|
|
272
|
-
}
|
|
294
|
+
// 构建提醒消息
|
|
295
|
+
const reminderText = `🔔 [${taskName}]\n\n${message}`
|
|
273
296
|
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
297
|
+
// 如果有 sessionId 是 weixin 类型的,发送到对应用户
|
|
298
|
+
if (sessionId && sessionId.startsWith('weixin_')) {
|
|
299
|
+
const userId = sessionId.replace('weixin_', '')
|
|
300
|
+
try {
|
|
301
|
+
await this._bot.sendText(userId, reminderText)
|
|
302
|
+
console.log(`[WeChat] Reminder sent to user ${userId}`)
|
|
303
|
+
} catch (err) {
|
|
304
|
+
console.error(`[WeChat] Failed to send reminder:`, err.message)
|
|
282
305
|
}
|
|
306
|
+
return
|
|
283
307
|
}
|
|
284
308
|
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
if (this._sessionPlugin) {
|
|
292
|
-
const allSessions = this._sessionPlugin.listSessions()
|
|
293
|
-
sessions = allSessions
|
|
294
|
-
.filter(s => s.id.startsWith('weixin_'))
|
|
295
|
-
.map(s => ({
|
|
296
|
-
userId: s.id.replace('weixin_', ''),
|
|
297
|
-
historyLength: s.messageCount,
|
|
298
|
-
lastActive: s.lastActive
|
|
299
|
-
}))
|
|
300
|
-
}
|
|
309
|
+
// 其他情况(包括 null 或其他 sessionId),发送到最近的 WeChat 会话
|
|
310
|
+
if (this._sessionPlugin) {
|
|
311
|
+
const allSessions = this._sessionPlugin.listSessions()
|
|
312
|
+
const weixinSessions = allSessions
|
|
313
|
+
.filter(s => s.id.startsWith('weixin_'))
|
|
314
|
+
.sort((a, b) => new Date(b.lastActive).getTime() - new Date(a.lastActive).getTime())
|
|
301
315
|
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
316
|
+
if (weixinSessions.length > 0) {
|
|
317
|
+
const userId = weixinSessions[0].id.replace('weixin_', '')
|
|
318
|
+
try {
|
|
319
|
+
await this._bot.sendText(userId, reminderText)
|
|
320
|
+
console.log(`[WeChat] Reminder sent to recent user ${userId}`)
|
|
321
|
+
} catch (err) {
|
|
322
|
+
console.error(`[WeChat] Failed to send reminder:`, err.message)
|
|
309
323
|
}
|
|
324
|
+
} else {
|
|
325
|
+
console.warn('[WeChat] No WeChat session found to send reminder')
|
|
310
326
|
}
|
|
311
327
|
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* 获取插件状态
|
|
332
|
+
*/
|
|
333
|
+
getStatus() {
|
|
334
|
+
// 从 SessionPlugin 获取 WeChat 相关会话
|
|
335
|
+
let sessions = []
|
|
336
|
+
if (this._sessionPlugin) {
|
|
337
|
+
const allSessions = this._sessionPlugin.listSessions()
|
|
338
|
+
sessions = allSessions
|
|
339
|
+
.filter(s => s.id.startsWith('weixin_'))
|
|
340
|
+
.map(s => ({
|
|
341
|
+
userId: s.id.replace('weixin_', ''),
|
|
342
|
+
historyLength: s.messageCount,
|
|
343
|
+
lastActive: s.lastActive
|
|
344
|
+
}))
|
|
345
|
+
}
|
|
312
346
|
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
this.
|
|
319
|
-
this.
|
|
320
|
-
console.log('[WeChat] Bot stopped')
|
|
347
|
+
return {
|
|
348
|
+
connected: !!this._bot,
|
|
349
|
+
sessionCount: sessions.length,
|
|
350
|
+
sessions,
|
|
351
|
+
config: {
|
|
352
|
+
forceLogin: this.config.forceLogin,
|
|
353
|
+
qrcodeTerminal: this.config.qrcodeTerminal
|
|
321
354
|
}
|
|
322
355
|
}
|
|
356
|
+
}
|
|
323
357
|
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
358
|
+
/**
|
|
359
|
+
* 停止 Bot
|
|
360
|
+
*/
|
|
361
|
+
stopBot() {
|
|
362
|
+
if (this._bot) {
|
|
363
|
+
this._bot.stop()
|
|
364
|
+
this._bot = null
|
|
365
|
+
console.log('[WeChat] Bot stopped')
|
|
329
366
|
}
|
|
367
|
+
}
|
|
330
368
|
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
369
|
+
reload(framework) {
|
|
370
|
+
this._framework = framework
|
|
371
|
+
this._initialized = false
|
|
372
|
+
this.stopBot()
|
|
373
|
+
this.start(framework)
|
|
374
|
+
}
|
|
337
375
|
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
376
|
+
uninstall(framework) {
|
|
377
|
+
// 销毁所有 session agents
|
|
378
|
+
for (const agent of this._sessionAgents.values()) {
|
|
379
|
+
agent.destroy()
|
|
380
|
+
}
|
|
381
|
+
this._sessionAgents.clear()
|
|
382
|
+
|
|
383
|
+
this.stopBot()
|
|
384
|
+
if (this._sessionPlugin && this._sessionDeleteHandler) {
|
|
385
|
+
this._sessionPlugin.off('session:deleted', this._sessionDeleteHandler)
|
|
386
|
+
this._sessionDeleteHandler = null
|
|
387
|
+
}
|
|
388
|
+
this._sessionPlugin = null
|
|
389
|
+
this._framework = null
|
|
390
|
+
// 恢复 stderr
|
|
391
|
+
if (this._origStderrWrite) {
|
|
392
|
+
process.stderr.write = this._origStderrWrite
|
|
349
393
|
}
|
|
350
394
|
}
|
|
351
395
|
}
|
|
396
|
+
|
|
397
|
+
module.exports = { WeixinPlugin }
|