@mindlink-ai/dsh-wechat-bot 0.0.0-stage → 1.0.0

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/client.js ADDED
@@ -0,0 +1,355 @@
1
+ /**
2
+ * Client half of @local/wechat-bot.
3
+ *
4
+ * 在左侧边栏注册「微信bot」入口(sidebar.panellist),并在主面板注册同名页面(main)。
5
+ * 面板通过同源 /wechat-bot/* 路由与 Host 通信,不依赖任何 @deepseek-ai Client 包。
6
+ */
7
+ window.__ModuleLoader__.load({
8
+ id: '@mindlink-ai/dsh-wechat-bot',
9
+ factory(require) {
10
+ const React = require('react')
11
+ const h = React.createElement
12
+ const { useCallback, useEffect, useRef, useState } = React
13
+
14
+ const MUTED = 'var(--dsw-alias-text-secondary, #6b7280)'
15
+ const TEXT = 'var(--dsw-alias-text-primary, #111827)'
16
+ const BORDER = 'var(--dsw-alias-border-1, #e5e7eb)'
17
+ const PANEL = 'var(--dsw-alias-bg-layer-1, #ffffff)'
18
+ const ACCENT = 'var(--dsw-alias-brand-primary, #2563eb)'
19
+ const DANGER = '#b91c1c'
20
+
21
+ const card = {
22
+ border: `1px solid ${BORDER}`,
23
+ borderRadius: 10,
24
+ padding: '14px 16px',
25
+ background: PANEL,
26
+ marginBottom: 14,
27
+ }
28
+ const button = {
29
+ cursor: 'pointer',
30
+ border: `1px solid ${BORDER}`,
31
+ background: 'transparent',
32
+ color: TEXT,
33
+ borderRadius: 6,
34
+ padding: '4px 10px',
35
+ fontSize: 13,
36
+ marginRight: 8,
37
+ }
38
+ const primaryButton = { ...button, background: ACCENT, borderColor: ACCENT, color: '#ffffff' }
39
+ const input = {
40
+ width: '100%',
41
+ boxSizing: 'border-box',
42
+ border: `1px solid ${BORDER}`,
43
+ borderRadius: 6,
44
+ padding: '6px 8px',
45
+ fontSize: 13,
46
+ color: TEXT,
47
+ background: 'transparent',
48
+ fontFamily: 'inherit',
49
+ }
50
+ const row = { display: 'flex', alignItems: 'center', gap: 10, flexWrap: 'wrap' }
51
+ const mono = { fontFamily: 'ui-monospace, SFMono-Regular, Menlo, monospace', fontSize: 12 }
52
+ const label = { display: 'block', fontSize: 12, color: MUTED, margin: '10px 0 4px' }
53
+
54
+ const STATE_TEXT = {
55
+ online: '在线',
56
+ connecting: '连接中',
57
+ unpaired: '未登录',
58
+ expired: '连接失效',
59
+ error: '连接异常',
60
+ }
61
+
62
+ function StatusPill({ ok, children }) {
63
+ return h(
64
+ 'span',
65
+ {
66
+ style: {
67
+ display: 'inline-flex',
68
+ alignItems: 'center',
69
+ gap: 6,
70
+ fontSize: 12,
71
+ color: ok ? '#15803d' : MUTED,
72
+ background: ok ? 'rgba(21,128,61,0.10)' : 'transparent',
73
+ border: `1px solid ${ok ? 'rgba(21,128,61,0.35)' : BORDER}`,
74
+ borderRadius: 999,
75
+ padding: '2px 10px',
76
+ },
77
+ },
78
+ h('span', { style: { width: 7, height: 7, borderRadius: '50%', background: ok ? '#16a34a' : '#9ca3af' } }),
79
+ children,
80
+ )
81
+ }
82
+
83
+ const fmtTime = (ts) => {
84
+ if (!ts) return '—'
85
+ const d = new Date(ts)
86
+ return `${d.toLocaleDateString()} ${d.toLocaleTimeString()}`
87
+ }
88
+
89
+ function WeChatBotIcon({ size = 16 }) {
90
+ return h(
91
+ 'svg',
92
+ {
93
+ width: size,
94
+ height: size,
95
+ viewBox: '0 0 24 24',
96
+ fill: 'none',
97
+ stroke: 'currentColor',
98
+ strokeWidth: 1.8,
99
+ strokeLinecap: 'round',
100
+ strokeLinejoin: 'round',
101
+ 'aria-hidden': true,
102
+ },
103
+ h('path', { d: 'M4 5.5A2.5 2.5 0 0 1 6.5 3h11A2.5 2.5 0 0 1 20 5.5v8a2.5 2.5 0 0 1-2.5 2.5H9l-4 4v-4H6.5A2.5 2.5 0 0 1 4 13.5z' }),
104
+ h('path', { d: 'M8.5 8.5h7M8.5 11.5h4.5' }),
105
+ )
106
+ }
107
+
108
+ const get = (url) =>
109
+ fetch(url, { credentials: 'same-origin', cache: 'no-store' }).then((response) => {
110
+ if (!response.ok) throw new Error(`${response.status} ${response.statusText}`)
111
+ return response.json()
112
+ })
113
+
114
+ const post = (url, body) =>
115
+ fetch(url, {
116
+ method: 'POST',
117
+ credentials: 'same-origin',
118
+ headers: { 'content-type': 'application/json' },
119
+ body: JSON.stringify(body ?? {}),
120
+ }).then(async (response) => {
121
+ const data = await response.json().catch(() => ({}))
122
+ if (!response.ok) throw new Error(data.error || `${response.status} ${response.statusText}`)
123
+ return data
124
+ })
125
+
126
+ function WeChatBotPanel() {
127
+ const [state, setState] = useState(null)
128
+ const [error, setError] = useState('')
129
+ const [busy, setBusy] = useState('')
130
+ const [notice, setNotice] = useState('')
131
+ const [testText, setTestText] = useState('这是一条来自 DSH 微信 bot 的测试消息')
132
+ const [copied, setCopied] = useState(false)
133
+
134
+ const refresh = useCallback(async () => {
135
+ try {
136
+ const next = await get('/wechat-bot/state')
137
+ setState(next)
138
+ setError('')
139
+ } catch (cause) {
140
+ setError(String(cause?.message || cause))
141
+ }
142
+ }, [])
143
+
144
+ useEffect(() => {
145
+ let alive = true
146
+ const tick = () => {
147
+ if (alive) refresh()
148
+ }
149
+ tick()
150
+ const timer = setInterval(tick, 2000)
151
+ return () => {
152
+ alive = false
153
+ clearInterval(timer)
154
+ }
155
+ }, [refresh])
156
+
157
+ const act = (name, fn) => async () => {
158
+ setBusy(name)
159
+ setNotice('')
160
+ try {
161
+ const result = await fn()
162
+ if (result?.state) setState(result.state)
163
+ setNotice(`${name}:完成`)
164
+ } catch (cause) {
165
+ setNotice(`${name}:${String(cause?.message || cause)}`)
166
+ } finally {
167
+ setBusy('')
168
+ }
169
+ }
170
+
171
+ const copyQr = async () => {
172
+ try {
173
+ await navigator.clipboard.writeText(state?.qr?.url || '')
174
+ setCopied(true)
175
+ setTimeout(() => setCopied(false), 1800)
176
+ } catch {
177
+ setNotice('复制失败,请手动选中链接复制')
178
+ }
179
+ }
180
+
181
+ const sendTest = async () => {
182
+ setBusy('send')
183
+ setNotice('')
184
+ try {
185
+ const result = await post('/wechat-bot/send', { text: testText })
186
+ if (result?.ok) setNotice(`已发送到微信(${result.chunks ?? 1} 条 / ${result.chars ?? 0} 字)`)
187
+ else setNotice(`发送失败:${result?.error || '未知错误'}${result?.message ? ` · ${result.message}` : ''}`)
188
+ } catch (cause) {
189
+ setNotice(`发送失败:${String(cause?.message || cause)}`)
190
+ } finally {
191
+ setBusy('')
192
+ }
193
+ }
194
+
195
+ if (!state) {
196
+ return h('div', { style: { padding: 24, color: MUTED } }, error ? `微信 bot 不可用:${error}` : '正在读取微信 bot 状态…')
197
+ }
198
+
199
+ const { connection, account, qr, counters, lastPush, recent, settings } = state
200
+ const online = connection.state === 'online'
201
+ const qrUrl = qr?.url || ''
202
+
203
+ return h(
204
+ 'div',
205
+ { style: { padding: '24px 28px 40px', color: TEXT, maxWidth: 820, boxSizing: 'border-box' } },
206
+ h('div', { style: { ...row, marginBottom: 14 } },
207
+ h('div', { style: { fontSize: 18, fontWeight: 600, marginRight: 6 } }, '微信 bot'),
208
+ h(StatusPill, { ok: online }, STATE_TEXT[connection.state] || connection.state),
209
+ connection.state === 'online' && account?.botId
210
+ ? h('span', { style: { fontSize: 12, color: MUTED } }, `bot ${account.botId}`)
211
+ : null,
212
+ h('span', { style: { fontSize: 12, color: MUTED } }, `收 ${counters.received} · 发 ${counters.sent} · 失败 ${counters.failed}`),
213
+ ),
214
+ error ? h('div', { style: { color: DANGER, fontSize: 12, marginBottom: 8 } }, error) : null,
215
+ notice ? h('div', { style: { color: MUTED, fontSize: 12, marginBottom: 8 } }, notice) : null,
216
+
217
+ // 连接状态
218
+ h('div', { style: card },
219
+ h('div', { style: { fontSize: 14, fontWeight: 600, marginBottom: 6 } }, '连接状态'),
220
+ h('div', { style: { fontSize: 12, color: MUTED, lineHeight: 1.8 } },
221
+ `状态:${STATE_TEXT[connection.state] || connection.state}(${connection.state})`,
222
+ connection.error ? `|${connection.error}` : '',
223
+ h('br'),
224
+ `最近推送:${lastPush?.at ? `${fmtTime(lastPush.at)}${lastPush.ok ? ` 成功(${lastPush.chunks} 条 / ${lastPush.chars} 字)` : ` 失败(${lastPush.error || '未知错误'})`}` : '尚未推送'}`,
225
+ h('br'),
226
+ `会话转发:${settings?.chatForward ? '开启' : '关闭'}${settings?.chatSessionId ? `(${settings.chatSessionId})` : ''}`,
227
+ h('br'),
228
+ '微信指令:/help /status /login /logout /recent /news /new',
229
+ ),
230
+ h('div', { style: { marginTop: 10 } },
231
+ h('button', { style: primaryButton, disabled: Boolean(busy), onClick: act('开始登录', () => post('/wechat-bot/login', { action: 'start' })) }, '开始 / 刷新登录'),
232
+ qrUrl ? h('button', { style: button, disabled: Boolean(busy), onClick: act('换一个二维码', () => post('/wechat-bot/login', { action: 'start' })) }, '换一个二维码') : null,
233
+ h('button', { style: button, disabled: Boolean(busy) || !account?.botId, onClick: act('退出登录', () => post('/wechat-bot/login', { action: 'logout' })) }, '退出登录'),
234
+ ),
235
+ ),
236
+
237
+ // 登录二维码
238
+ qrUrl
239
+ ? h('div', { style: card },
240
+ h('div', { style: { fontSize: 14, fontWeight: 600, marginBottom: 4 } }, '扫码登录'),
241
+ h('div', { style: { fontSize: 12, color: MUTED, marginBottom: 8 } },
242
+ qr.hasQr
243
+ ? '用手机微信扫描下方二维码确认登录(二维码约 2 分钟自动更换)。'
244
+ : '用手机微信打开下方链接并确认登录(二维码约 2 分钟过期)。'),
245
+ qr.hasQr
246
+ ? h('img', {
247
+ src: qr.dataUrl,
248
+ alt: '微信登录二维码',
249
+ style: {
250
+ width: 220,
251
+ height: 220,
252
+ display: 'block',
253
+ border: `1px solid ${BORDER}`,
254
+ borderRadius: 8,
255
+ background: '#fff',
256
+ marginBottom: 8,
257
+ },
258
+ })
259
+ : null,
260
+ h('div', { style: { ...mono, fontSize: 11, color: MUTED, wordBreak: 'break-all', background: '#f7f7f7', padding: '6px 8px', borderRadius: 6, userSelect: 'all' } }, qrUrl),
261
+ h('div', { style: { marginTop: 8 } },
262
+ h('button', { style: primaryButton, onClick: copyQr }, copied ? '已复制' : '复制链接'),
263
+ h('a', { href: qrUrl, target: '_blank', rel: 'noreferrer', style: { ...button, display: 'inline-block', textDecoration: 'none' } }, '在新标签页打开'),
264
+ h('button', { style: button, disabled: Boolean(busy), onClick: act('取消登录', () => post('/wechat-bot/login', { action: 'cancel' })) }, '取消登录'),
265
+ ),
266
+ )
267
+ : null,
268
+
269
+ // 作者信息
270
+ h('div', { style: card },
271
+ h('div', { style: { fontSize: 14, fontWeight: 600, marginBottom: 6 } }, '作者信息'),
272
+ h('div', { style: { fontSize: 12, color: MUTED, lineHeight: 2 } },
273
+ h('div', null,
274
+ '作者:',
275
+ h('span', { style: { color: TEXT, fontWeight: 600 } }, '思链科技(MindLink)'),
276
+ ),
277
+ h('div', null,
278
+ '插件:',
279
+ h('span', { style: mono }, '@mindlink-ai/dsh-wechat-bot'),
280
+ ' · v1.0.0',
281
+ ),
282
+ h('div', null, '通道:微信官方 iLink(ClawBot) · 本地优先,聊天数据只留在本机'),
283
+ h('div', null, '许可:MIT · 反馈与定制请联系插件维护者'),
284
+ ),
285
+ ),
286
+
287
+ // 账号信息
288
+ h('div', { style: card },
289
+ h('div', { style: { fontSize: 14, fontWeight: 600, marginBottom: 6 } }, '账号信息'),
290
+ account?.botId
291
+ ? h('div', { style: { ...mono, color: MUTED, lineHeight: 2 } },
292
+ `bot ID:${account.botId}`,
293
+ h('br'),
294
+ `用户 ID:${account.userId || '—'}`,
295
+ h('br'),
296
+ `网关:${account.baseUrl || '—'}`,
297
+ h('br'),
298
+ `登录时间:${account.savedAt ? fmtTime(Date.parse(account.savedAt)) : '—'}`,
299
+ )
300
+ : h('div', { style: { fontSize: 12, color: MUTED } }, '尚未登录。点上面的「开始 / 刷新登录」,然后用手机微信扫码确认。'),
301
+ ),
302
+
303
+ // 测试推送
304
+ h('div', { style: card },
305
+ h('div', { style: { fontSize: 14, fontWeight: 600, marginBottom: 4 } }, '测试推送'),
306
+ h('div', { style: { fontSize: 12, color: MUTED } }, '推送到你本人的微信(1:1 会话)。首次使用前,请在微信里给机器人发一条任意消息,让 bot 记录会话上下文。'),
307
+ h('label', { style: label }, '消息内容'),
308
+ h('textarea', {
309
+ style: { ...input, minHeight: 64, resize: 'vertical' },
310
+ value: testText,
311
+ onChange: (event) => setTestText(event.target.value),
312
+ }),
313
+ h('div', { style: { marginTop: 8 } },
314
+ h('button', {
315
+ style: primaryButton,
316
+ disabled: Boolean(busy) || !testText.trim(),
317
+ onClick: sendTest,
318
+ }, busy === 'send' ? '发送中…' : '发送到微信'),
319
+ ),
320
+ ),
321
+
322
+ // 最近消息
323
+ h('div', { style: card },
324
+ h('div', { style: { fontSize: 14, fontWeight: 600, marginBottom: 8 } }, `最近收到的消息(${recent.length})`),
325
+ recent.length === 0
326
+ ? h('div', { style: { fontSize: 12, color: MUTED } }, '暂无。给机器人发一条消息后,这里会显示记录并刷新推送上下文。')
327
+ : h('ul', { style: { listStyle: 'none', margin: 0, padding: 0, maxHeight: 240, overflow: 'auto' } },
328
+ recent.slice().reverse().map((item, index) =>
329
+ h('li', { key: `${item.at}-${index}`, style: { padding: '6px 0', borderBottom: `1px solid ${BORDER}`, fontSize: 12 } },
330
+ h('span', { style: { color: MUTED } }, `${fmtTime(item.at)} `),
331
+ h('span', null, item.text || '[非文本]'),
332
+ ),
333
+ ),
334
+ ),
335
+ ),
336
+
337
+ )
338
+ }
339
+
340
+ return {
341
+ inject: ['slots'],
342
+ apply(ctx) {
343
+ ctx.slots.inject('sidebar.panellist', () =>
344
+ ctx.slots.register(
345
+ { name: 'sidebar.panellist', id: 'wechat-bot', order: 5, label: () => '微信bot' },
346
+ WeChatBotIcon,
347
+ ),
348
+ )
349
+ ctx.slots.inject('main', () =>
350
+ ctx.slots.register({ name: 'main', key: 'wechat-bot' }, WeChatBotPanel),
351
+ )
352
+ },
353
+ }
354
+ },
355
+ })
@@ -0,0 +1,4 @@
1
+ - insert:
2
+ - id: wechat-bot
3
+ name: '@mindlink-ai/dsh-wechat-bot'
4
+ config: {}
package/icon.svg ADDED
@@ -0,0 +1,4 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round">
2
+ <path d="M4 5.5A2.5 2.5 0 0 1 6.5 3h11A2.5 2.5 0 0 1 20 5.5v8a2.5 2.5 0 0 1-2.5 2.5H9l-4 4v-4H6.5A2.5 2.5 0 0 1 4 13.5z"/>
3
+ <path d="M8.5 8.5h7M8.5 11.5h4.5"/>
4
+ </svg>