@michengai/dsh-im-connect 1.0.11 → 1.0.13
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/CHANGELOG.md +15 -0
- package/CHANGELOG.zh-CN.md +15 -0
- package/README.md +28 -3
- package/README.zh-CN.md +28 -3
- package/client.js +438 -17
- package/lib/api-base.d.ts +21 -0
- package/lib/api-base.d.ts.map +1 -0
- package/lib/api-base.js +51 -0
- package/lib/api-base.js.map +1 -0
- package/lib/channels/channel-image-download.d.ts +8 -5
- package/lib/channels/channel-image-download.d.ts.map +1 -1
- package/lib/channels/channel-image-download.js +70 -2
- package/lib/channels/channel-image-download.js.map +1 -1
- package/lib/channels/diagnostics.d.ts +1 -1
- package/lib/channels/diagnostics.d.ts.map +1 -1
- package/lib/channels/diagnostics.js +2 -2
- package/lib/channels/diagnostics.js.map +1 -1
- package/lib/channels/discord.d.ts +3 -0
- package/lib/channels/discord.d.ts.map +1 -1
- package/lib/channels/discord.js +56 -7
- package/lib/channels/discord.js.map +1 -1
- package/lib/channels/factory.d.ts.map +1 -1
- package/lib/channels/factory.js +8 -2
- package/lib/channels/factory.js.map +1 -1
- package/lib/channels/file-send.d.ts +1 -1
- package/lib/channels/file-send.d.ts.map +1 -1
- package/lib/channels/file-send.js +2 -2
- package/lib/channels/file-send.js.map +1 -1
- package/lib/channels/meta.d.ts +2 -0
- package/lib/channels/meta.d.ts.map +1 -1
- package/lib/channels/meta.js +2 -0
- package/lib/channels/meta.js.map +1 -1
- package/lib/channels/telegram.d.ts +2 -0
- package/lib/channels/telegram.d.ts.map +1 -1
- package/lib/channels/telegram.js +29 -6
- package/lib/channels/telegram.js.map +1 -1
- package/lib/client.js +523 -21
- package/lib/index.d.ts +2 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +10 -6
- package/lib/index.js.map +1 -1
- package/lib/manager.d.ts +15 -0
- package/lib/manager.d.ts.map +1 -1
- package/lib/manager.js +221 -32
- package/lib/manager.js.map +1 -1
- package/lib/plugin-updater.d.ts +1 -0
- package/lib/plugin-updater.d.ts.map +1 -1
- package/lib/plugin-updater.js +7 -0
- package/lib/plugin-updater.js.map +1 -1
- package/package.json +5 -4
- package/src/api-base.ts +45 -0
- package/src/channels/channel-image-download.ts +83 -5
- package/src/channels/diagnostics.ts +2 -2
- package/src/channels/discord.ts +49 -7
- package/src/channels/factory.ts +8 -2
- package/src/channels/file-send.ts +2 -2
- package/src/channels/meta.ts +4 -0
- package/src/channels/telegram.ts +22 -6
- package/src/index.ts +12 -6
- package/src/manager.ts +214 -31
- package/src/plugin-update-ui.js +36 -5
- package/src/plugin-updater.ts +8 -1
package/src/manager.ts
CHANGED
|
@@ -27,6 +27,24 @@ export const API_CLIENT_HEADER = 'x-dsh-im-connect-client'
|
|
|
27
27
|
const MAX_API_BODY_BYTES = 1024 * 1024
|
|
28
28
|
const ACCOUNT_REMARK_LIMIT = 40
|
|
29
29
|
|
|
30
|
+
type ChannelProxyMode = 'inherit' | 'direct' | 'custom'
|
|
31
|
+
|
|
32
|
+
const PROXY_SUPPORTED_PLATFORMS = new Set<ChannelId>(['telegram', 'discord'])
|
|
33
|
+
function supportsProxy(platform: ChannelId): boolean {
|
|
34
|
+
return PROXY_SUPPORTED_PLATFORMS.has(platform)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function normalizeProxyUrl(input: unknown): string | undefined {
|
|
38
|
+
if (typeof input !== 'string' || !input.trim()) return undefined
|
|
39
|
+
try {
|
|
40
|
+
const url = new URL(input.trim())
|
|
41
|
+
if (!['http:', 'https:'].includes(url.protocol) || !url.hostname || url.port && Number(url.port) > 65535
|
|
42
|
+
|| url.username || url.password
|
|
43
|
+
|| url.pathname !== '/' || url.search || url.hash) return undefined
|
|
44
|
+
return url.toString()
|
|
45
|
+
} catch { return undefined }
|
|
46
|
+
}
|
|
47
|
+
|
|
30
48
|
/** 消息发送方式只有排队与插话两档;缺失或未知值一律回到排队,保持旧配置语义。 */
|
|
31
49
|
function normalizeSendMode(input: unknown): SendMode {
|
|
32
50
|
return input === 'steer' ? 'steer' : 'queue'
|
|
@@ -174,6 +192,9 @@ export interface AccountView {
|
|
|
174
192
|
commandPermissions: CommandPermissions
|
|
175
193
|
privateAccess: 'approved' | 'all'
|
|
176
194
|
sendMode: SendMode
|
|
195
|
+
proxyMode: ChannelProxyMode
|
|
196
|
+
proxyConfigured: boolean
|
|
197
|
+
proxyUrl?: string
|
|
177
198
|
lastCheckedAt?: string
|
|
178
199
|
}
|
|
179
200
|
|
|
@@ -189,7 +210,7 @@ export interface ChannelView {
|
|
|
189
210
|
label: string
|
|
190
211
|
description: string
|
|
191
212
|
kind: string
|
|
192
|
-
fields: Array<{ key: string; label: string; secret?: boolean }>
|
|
213
|
+
fields: Array<{ key: string; label: string; secret?: boolean; settingsOnly?: boolean }>
|
|
193
214
|
connected: boolean
|
|
194
215
|
receiveEnabled: boolean
|
|
195
216
|
configuredKeys: string[]
|
|
@@ -216,6 +237,8 @@ interface Persisted {
|
|
|
216
237
|
agentPreset?: string
|
|
217
238
|
cwd?: string
|
|
218
239
|
permission?: PermissionPreset
|
|
240
|
+
sidebarEntry?: boolean
|
|
241
|
+
onlineUpdate?: boolean
|
|
219
242
|
}
|
|
220
243
|
|
|
221
244
|
export interface PermissionOptionView {
|
|
@@ -240,8 +263,10 @@ export class ChannelManager {
|
|
|
240
263
|
private readonly log: (line: string) => void
|
|
241
264
|
private readonly ctx: Context
|
|
242
265
|
private readonly engineConfig: EngineConfig
|
|
266
|
+
private readonly hostUpdateDisabled: boolean
|
|
243
267
|
private store: Persisted = { channels: {}, allowlist: {}, pending: {} }
|
|
244
268
|
private readonly running = new Map<string, ChannelAdapter>()
|
|
269
|
+
private readonly proxyUrlCache = new Map<string, string>()
|
|
245
270
|
private readonly diagnosticJobs = new Map<string, Promise<AccountDiagnosticResult>>()
|
|
246
271
|
private readonly diagnosticAbort = new AbortController()
|
|
247
272
|
private readonly channelOperations = new KeyedSerialQueue()
|
|
@@ -249,10 +274,11 @@ export class ChannelManager {
|
|
|
249
274
|
// dispose 后阻止 initEnabled/startOne 再拉起渠道,避免插件重载时新旧双实例并存
|
|
250
275
|
private disposed = false
|
|
251
276
|
|
|
252
|
-
constructor(options: { ctx: Context; stateDir: string; log: (line: string) => void; engineConfig: EngineConfig }) {
|
|
277
|
+
constructor(options: { ctx: Context; stateDir: string; log: (line: string) => void; engineConfig: EngineConfig; hostUpdateDisabled?: boolean }) {
|
|
253
278
|
this.ctx = options.ctx
|
|
254
279
|
this.engineConfig = options.engineConfig
|
|
255
280
|
this.stateDir = options.stateDir
|
|
281
|
+
this.hostUpdateDisabled = options.hostUpdateDisabled === true
|
|
256
282
|
this.file = join(options.stateDir, 'channels.json')
|
|
257
283
|
this.sessions = new SessionMapStore(join(options.stateDir, 'sessions.json'))
|
|
258
284
|
this.log = options.log
|
|
@@ -313,6 +339,33 @@ export class ChannelManager {
|
|
|
313
339
|
})
|
|
314
340
|
}
|
|
315
341
|
|
|
342
|
+
private async hydrateProxyUrl(accountId: string, state: ChannelState): Promise<void> {
|
|
343
|
+
const platform = this.platformOf(accountId, state)
|
|
344
|
+
if (!supportsProxy(platform)) {
|
|
345
|
+
this.proxyUrlCache.delete(accountId)
|
|
346
|
+
return
|
|
347
|
+
}
|
|
348
|
+
const config = state.config ?? {}
|
|
349
|
+
if (config.proxyMode !== 'custom') {
|
|
350
|
+
this.proxyUrlCache.delete(accountId)
|
|
351
|
+
return
|
|
352
|
+
}
|
|
353
|
+
if (this.proxyUrlCache.has(accountId)) {
|
|
354
|
+
return
|
|
355
|
+
}
|
|
356
|
+
let proxyUrl = config.proxyUrl
|
|
357
|
+
if (!proxyUrl && config.proxyUrlRef) {
|
|
358
|
+
try { proxyUrl = await this.vault.resolve(config.proxyUrlRef) }
|
|
359
|
+
catch { this.log(`[manager] ${CHANNEL_META[platform]?.label ?? platform} 代理地址读取失败`) }
|
|
360
|
+
}
|
|
361
|
+
if (proxyUrl) this.proxyUrlCache.set(accountId, proxyUrl)
|
|
362
|
+
else this.proxyUrlCache.delete(accountId)
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
private async hydrateProxyUrls(): Promise<void> {
|
|
366
|
+
await Promise.all(Object.entries(this.store.channels).map(([accountId, state]) => this.hydrateProxyUrl(accountId, state)))
|
|
367
|
+
}
|
|
368
|
+
|
|
316
369
|
private accountView(accountId: string, state: ChannelState): AccountView {
|
|
317
370
|
const platform = this.platformOf(accountId, state)
|
|
318
371
|
const config = state.config ?? {}
|
|
@@ -349,6 +402,11 @@ export class ChannelManager {
|
|
|
349
402
|
commandPermissions: normalizeCommandPermissions(state.commandPermissions),
|
|
350
403
|
privateAccess: state.privateAccess === 'all' ? 'all' : 'approved',
|
|
351
404
|
sendMode: normalizeSendMode(state.sendMode),
|
|
405
|
+
proxyMode: supportsProxy(platform) && (config.proxyMode === 'direct' || config.proxyMode === 'custom') ? config.proxyMode : 'inherit',
|
|
406
|
+
proxyConfigured: supportsProxy(platform) && config.proxyMode === 'custom'
|
|
407
|
+
? this.proxyUrlCache.has(accountId)
|
|
408
|
+
: supportsProxy(platform) && Boolean(config.proxyUrlRef || config.proxyUrl),
|
|
409
|
+
...(supportsProxy(platform) && this.proxyUrlCache.has(accountId) ? { proxyUrl: this.proxyUrlCache.get(accountId)! } : {}),
|
|
352
410
|
lastCheckedAt: state.lastCheckedAt,
|
|
353
411
|
}
|
|
354
412
|
}
|
|
@@ -477,6 +535,7 @@ export class ChannelManager {
|
|
|
477
535
|
await this.vault.unset(ref)
|
|
478
536
|
}
|
|
479
537
|
delete this.store.channels[id]
|
|
538
|
+
this.proxyUrlCache.delete(id)
|
|
480
539
|
delete this.store.allowlist[id]
|
|
481
540
|
delete this.store.pending[id]
|
|
482
541
|
this.engine.clearAllowed(id)
|
|
@@ -490,6 +549,7 @@ export class ChannelManager {
|
|
|
490
549
|
|
|
491
550
|
async initEnabled(): Promise<void> {
|
|
492
551
|
if (this.disposed) return
|
|
552
|
+
await this.hydrateProxyUrls()
|
|
493
553
|
const started = Date.now()
|
|
494
554
|
await this.migrateLegacyWeixinToken().catch((error) => {
|
|
495
555
|
this.log(`[manager] 迁移旧版微信 token 失败,已保留原文件: ${error instanceof Error ? error.message : String(error)}`)
|
|
@@ -536,16 +596,63 @@ export class ChannelManager {
|
|
|
536
596
|
readBody: () => Promise<Record<string, unknown>>,
|
|
537
597
|
send: (status: number, body: unknown) => void,
|
|
538
598
|
): Promise<void> => {
|
|
539
|
-
const payload = () =>
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
599
|
+
const payload = async () => {
|
|
600
|
+
await this.hydrateProxyUrls()
|
|
601
|
+
return {
|
|
602
|
+
ok: true,
|
|
603
|
+
channels: this.list(),
|
|
604
|
+
groups: this.channelSessions(),
|
|
605
|
+
pending: this.pendingRequests(),
|
|
606
|
+
assistant: this.currentAssistant(),
|
|
607
|
+
}
|
|
608
|
+
}
|
|
546
609
|
try {
|
|
547
610
|
// 路由段统一从插件前缀之后开始,身份认证只由下方入口负责。
|
|
548
611
|
const parts = ('/dsh-im-connect/api' + path).split('/').filter(Boolean)
|
|
612
|
+
if (parts[2] === 'sidebar-entry' && parts.length === 3) {
|
|
613
|
+
if (method === 'GET' || method === 'HEAD') {
|
|
614
|
+
send(200, method === 'HEAD' ? undefined : { ok: true, enabled: this.getSidebarEntry() })
|
|
615
|
+
return
|
|
616
|
+
}
|
|
617
|
+
if (method === 'POST') {
|
|
618
|
+
const body = await readBody()
|
|
619
|
+
if (!body || typeof body.enabled !== 'boolean') {
|
|
620
|
+
send(400, { ok: false, error: 'enabled 必须是布尔值' })
|
|
621
|
+
return
|
|
622
|
+
}
|
|
623
|
+
const enabled = await this.setSidebarEntry(body.enabled)
|
|
624
|
+
send(200, { ok: true, enabled })
|
|
625
|
+
return
|
|
626
|
+
}
|
|
627
|
+
send(405, { ok: false, error: 'method not allowed' })
|
|
628
|
+
return
|
|
629
|
+
}
|
|
630
|
+
if (parts[2] === 'online-update' && parts.length === 3) {
|
|
631
|
+
if (method === 'GET' || method === 'HEAD') {
|
|
632
|
+
send(200, method === 'HEAD' ? undefined : {
|
|
633
|
+
ok: true,
|
|
634
|
+
enabled: this.isUpdateEnabled(),
|
|
635
|
+
locked: this.isHostUpdateDisabled(),
|
|
636
|
+
})
|
|
637
|
+
return
|
|
638
|
+
}
|
|
639
|
+
if (method === 'POST') {
|
|
640
|
+
if (this.isHostUpdateDisabled()) {
|
|
641
|
+
send(403, { ok: false, error: '在线更新已被宿主配置锁定禁用' })
|
|
642
|
+
return
|
|
643
|
+
}
|
|
644
|
+
const body = await readBody()
|
|
645
|
+
if (!body || typeof body.enabled !== 'boolean') {
|
|
646
|
+
send(400, { ok: false, error: 'enabled 必须是布尔值' })
|
|
647
|
+
return
|
|
648
|
+
}
|
|
649
|
+
const enabled = await this.setOnlineUpdate(body.enabled)
|
|
650
|
+
send(200, { ok: true, enabled })
|
|
651
|
+
return
|
|
652
|
+
}
|
|
653
|
+
send(405, { ok: false, error: 'method not allowed' })
|
|
654
|
+
return
|
|
655
|
+
}
|
|
549
656
|
if (parts[2] === 'assistant' && parts.length === 3) {
|
|
550
657
|
if (method === 'GET') {
|
|
551
658
|
send(200, {
|
|
@@ -600,7 +707,7 @@ export class ChannelManager {
|
|
|
600
707
|
return
|
|
601
708
|
}
|
|
602
709
|
if (parts[2] === 'channels' && parts.length === 3 && method === 'GET') {
|
|
603
|
-
send(200, payload())
|
|
710
|
+
send(200, await payload())
|
|
604
711
|
return
|
|
605
712
|
}
|
|
606
713
|
if (parts[2] === 'channels' && parts.length === 6 && parts[4] === 'qr') {
|
|
@@ -772,6 +879,33 @@ export class ChannelManager {
|
|
|
772
879
|
this.engine.dispose()
|
|
773
880
|
}
|
|
774
881
|
|
|
882
|
+
getSidebarEntry(): boolean {
|
|
883
|
+
return this.store.sidebarEntry === true
|
|
884
|
+
}
|
|
885
|
+
|
|
886
|
+
async setSidebarEntry(enabled: boolean): Promise<boolean> {
|
|
887
|
+
if (typeof enabled !== 'boolean') throw new Error('enabled 必须是布尔值')
|
|
888
|
+
this.store.sidebarEntry = enabled
|
|
889
|
+
this.flush()
|
|
890
|
+
return enabled
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
isHostUpdateDisabled(): boolean {
|
|
894
|
+
return this.hostUpdateDisabled
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
isUpdateEnabled(): boolean {
|
|
898
|
+
if (this.hostUpdateDisabled) return false
|
|
899
|
+
return this.store.onlineUpdate !== false
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
async setOnlineUpdate(enabled: boolean): Promise<boolean> {
|
|
903
|
+
if (typeof enabled !== 'boolean') throw new Error('enabled 必须是布尔值')
|
|
904
|
+
if (this.hostUpdateDisabled) throw new Error('在线更新已被宿主配置锁定禁用')
|
|
905
|
+
this.store.onlineUpdate = enabled
|
|
906
|
+
this.flush()
|
|
907
|
+
return enabled
|
|
908
|
+
}
|
|
775
909
|
|
|
776
910
|
currentAssistant(): AssistantModel | undefined {
|
|
777
911
|
return normalizeAssistantModel(this.store.assistant ?? this.engineConfig)
|
|
@@ -783,6 +917,20 @@ export class ChannelManager {
|
|
|
783
917
|
const state = this.store.channels[accountId]
|
|
784
918
|
if (!state) return { ok: false, error: '账号不存在' }
|
|
785
919
|
const platform = this.platformOf(accountId, state)
|
|
920
|
+
const hasProxySettings = Object.hasOwn(input, 'proxyMode') || Object.hasOwn(input, 'proxyUrl')
|
|
921
|
+
if (hasProxySettings && !supportsProxy(platform)) return { ok: false, error: '当前渠道不支持账号级代理' }
|
|
922
|
+
const previousProxyMode = state.config?.proxyMode === 'direct' || state.config?.proxyMode === 'custom' ? state.config.proxyMode : 'inherit'
|
|
923
|
+
const requestedProxyMode = input.proxyMode === undefined ? previousProxyMode : input.proxyMode
|
|
924
|
+
if (hasProxySettings && !['inherit', 'direct', 'custom'].includes(String(requestedProxyMode))) return { ok: false, error: '代理模式无效' }
|
|
925
|
+
let nextProxyUrl: string | undefined
|
|
926
|
+
if (supportsProxy(platform) && requestedProxyMode === 'custom') {
|
|
927
|
+
if (typeof input.proxyUrl === 'string' && input.proxyUrl.trim()) {
|
|
928
|
+
nextProxyUrl = normalizeProxyUrl(input.proxyUrl)
|
|
929
|
+
if (!nextProxyUrl) return { ok: false, error: '代理地址无效:仅支持不含路径、查询参数、片段或认证信息的 HTTP(S) 代理地址。' }
|
|
930
|
+
} else if (!state.config?.proxyUrlRef && !state.config?.proxyUrl) {
|
|
931
|
+
return { ok: false, error: '手动配置代理时必须填写代理地址' }
|
|
932
|
+
}
|
|
933
|
+
}
|
|
786
934
|
const normalized = await this.normalizeAccountSettings(platform, input, state)
|
|
787
935
|
if (!normalized.ok) return normalized
|
|
788
936
|
const previousCwd = normalizeWorkspacePath(state.cwd) ?? this.currentWorkspace()
|
|
@@ -807,8 +955,36 @@ export class ChannelManager {
|
|
|
807
955
|
state.privateAccess = normalized.settings.privateAccess
|
|
808
956
|
// 发送方式按注入时机读取,不需要重载会话。
|
|
809
957
|
state.sendMode = normalized.settings.sendMode
|
|
958
|
+
if (supportsProxy(platform) && !this.proxyUrlCache.has(accountId)) {
|
|
959
|
+
await this.hydrateProxyUrl(accountId, state)
|
|
960
|
+
}
|
|
961
|
+
const previousProxyUrl = this.proxyUrlCache.get(accountId)
|
|
962
|
+
const proxyModeChanged = requestedProxyMode !== previousProxyMode
|
|
963
|
+
const proxyUrlChanged = requestedProxyMode === 'custom'
|
|
964
|
+
&& nextProxyUrl !== undefined
|
|
965
|
+
&& nextProxyUrl !== previousProxyUrl
|
|
966
|
+
const proxyChanged = supportsProxy(platform) && hasProxySettings && (proxyModeChanged || proxyUrlChanged)
|
|
967
|
+
if (supportsProxy(platform) && hasProxySettings) {
|
|
968
|
+
const config = { ...(state.config ?? {}) }
|
|
969
|
+
if (requestedProxyMode === 'custom') {
|
|
970
|
+
if (proxyUrlChanged && nextProxyUrl) {
|
|
971
|
+
config.proxyUrl = nextProxyUrl
|
|
972
|
+
state.config = await this.persistSecrets(platform, accountId, config)
|
|
973
|
+
this.proxyUrlCache.set(accountId, nextProxyUrl)
|
|
974
|
+
}
|
|
975
|
+
state.config = { ...(state.config ?? config), proxyMode: 'custom' }
|
|
976
|
+
} else if (proxyModeChanged) {
|
|
977
|
+
const ref = config.proxyUrlRef || credentialRef(accountId, 'proxyUrl')
|
|
978
|
+
await this.vault.unset(ref)
|
|
979
|
+
delete config.proxyUrl
|
|
980
|
+
delete config.proxyUrlRef
|
|
981
|
+
state.config = { ...config, proxyMode: requestedProxyMode as ChannelProxyMode }
|
|
982
|
+
this.proxyUrlCache.delete(accountId)
|
|
983
|
+
}
|
|
984
|
+
}
|
|
810
985
|
this.flush()
|
|
811
986
|
if (reloadSessions) await this.engine.reloadChannel(accountId, { resetSessions })
|
|
987
|
+
if (proxyChanged && this.running.has(accountId)) await this.startOne(accountId)
|
|
812
988
|
return { ok: true, account: this.accountView(accountId, state) }
|
|
813
989
|
})
|
|
814
990
|
}
|
|
@@ -863,7 +1039,8 @@ export class ChannelManager {
|
|
|
863
1039
|
state.receiveEnabled = true
|
|
864
1040
|
await this.startOne(accountId)
|
|
865
1041
|
return { ok: true }
|
|
866
|
-
} catch {
|
|
1042
|
+
} catch (error) {
|
|
1043
|
+
this.log(`[manager] 重新连接 ${accountId} 失败: ${error instanceof Error ? error.message : String(error)}`)
|
|
867
1044
|
return { ok: false, error: '重新连接失败,请查看本机日志' }
|
|
868
1045
|
}
|
|
869
1046
|
})
|
|
@@ -1037,27 +1214,28 @@ export class ChannelManager {
|
|
|
1037
1214
|
const resolved = await this.resolveSecrets(platform, id, state.config ?? {})
|
|
1038
1215
|
if (this.disposed) return
|
|
1039
1216
|
const accountDir = this.accountStateDir(id, platform)
|
|
1040
|
-
|
|
1041
|
-
accountId: id,
|
|
1042
|
-
accountLabel: state.name || `${CHANNEL_META[platform].label}账号`,
|
|
1043
|
-
host: this.ctx,
|
|
1044
|
-
onWeixinBotToken: async (token) => {
|
|
1045
|
-
const ref = credentialRef(id, 'botToken')
|
|
1046
|
-
if (token) await this.vault.set(ref, token)
|
|
1047
|
-
else await this.vault.unset(ref)
|
|
1048
|
-
},
|
|
1049
|
-
})
|
|
1050
|
-
if (!adapter) throw new Error('凭据不足,无法启动渠道')
|
|
1051
|
-
if (platform === 'weixin') {
|
|
1052
|
-
this.seedAllowedUser(id, readWeixinAllowedUserId(accountDir) || resolved.allowedUserId)
|
|
1053
|
-
}
|
|
1054
|
-
this.seedAllowedUser(id, resolved.ownerOpenId || resolved.allowedUserId)
|
|
1055
|
-
this.engine.register(adapter)
|
|
1056
|
-
this.running.set(id, adapter)
|
|
1217
|
+
let adapter: ChannelAdapter | undefined
|
|
1057
1218
|
// 渠道网络异常时 start 可能永久挂起,超时按启动失败处理(catch 会顺带 stop)
|
|
1058
1219
|
const START_TIMEOUT_MS = 30_000
|
|
1059
1220
|
let startTimer: ReturnType<typeof setTimeout> | undefined
|
|
1060
1221
|
try {
|
|
1222
|
+
adapter = createChannelAdapter(platform, resolved, this.log, accountDir, {
|
|
1223
|
+
accountId: id,
|
|
1224
|
+
accountLabel: state.name || `${CHANNEL_META[platform].label}账号`,
|
|
1225
|
+
host: this.ctx,
|
|
1226
|
+
onWeixinBotToken: async (token) => {
|
|
1227
|
+
const ref = credentialRef(id, 'botToken')
|
|
1228
|
+
if (token) await this.vault.set(ref, token)
|
|
1229
|
+
else await this.vault.unset(ref)
|
|
1230
|
+
},
|
|
1231
|
+
})
|
|
1232
|
+
if (!adapter) throw new Error('凭据不足,无法启动渠道')
|
|
1233
|
+
if (platform === 'weixin') {
|
|
1234
|
+
this.seedAllowedUser(id, readWeixinAllowedUserId(accountDir) || resolved.allowedUserId)
|
|
1235
|
+
}
|
|
1236
|
+
this.seedAllowedUser(id, resolved.ownerOpenId || resolved.allowedUserId)
|
|
1237
|
+
this.engine.register(adapter)
|
|
1238
|
+
this.running.set(id, adapter)
|
|
1061
1239
|
await Promise.race([
|
|
1062
1240
|
adapter.start(),
|
|
1063
1241
|
new Promise<never>((_, reject) => {
|
|
@@ -1071,10 +1249,13 @@ export class ChannelManager {
|
|
|
1071
1249
|
return
|
|
1072
1250
|
}
|
|
1073
1251
|
} catch (error) {
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1252
|
+
if (adapter) {
|
|
1253
|
+
this.engine.unregister(id)
|
|
1254
|
+
this.running.delete(id)
|
|
1255
|
+
await Promise.resolve(adapter.stop()).catch(() => undefined)
|
|
1256
|
+
}
|
|
1077
1257
|
const message = error instanceof Error ? error.message : String(error)
|
|
1258
|
+
this.log(`[manager] 启动 ${id} 失败: ${message}`)
|
|
1078
1259
|
if (state) {
|
|
1079
1260
|
state.lastError = '连接失败,请查看本机日志'
|
|
1080
1261
|
this.flush()
|
|
@@ -1150,6 +1331,8 @@ export class ChannelManager {
|
|
|
1150
1331
|
assistant: normalizeAssistantModel(parsed.assistant ?? {}),
|
|
1151
1332
|
cwd: normalizeWorkspacePath(parsed.cwd),
|
|
1152
1333
|
permission: normalizePermission(parsed.permission),
|
|
1334
|
+
sidebarEntry: typeof parsed.sidebarEntry === 'boolean' ? parsed.sidebarEntry : undefined,
|
|
1335
|
+
onlineUpdate: typeof parsed.onlineUpdate === 'boolean' ? parsed.onlineUpdate : undefined,
|
|
1153
1336
|
}
|
|
1154
1337
|
} catch (error) {
|
|
1155
1338
|
try {
|
package/src/plugin-update-ui.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { resolveApiUrl } from "../lib/api-base.js";
|
|
2
|
+
|
|
1
3
|
const UPDATE_HEADER = "x-michengai-plugin-update";
|
|
2
4
|
const STYLE_ID = "michengai-plugin-update-ui";
|
|
3
5
|
const updatePrimitives = (typeof require === "function" ? (function () { try { return require("@deepseek-ai/dsh-client-ui-primitives"); } catch { return {}; } })() : {});
|
|
@@ -148,6 +150,7 @@ function ensureStyle() {
|
|
|
148
150
|
style.setAttribute("data-plugin", "@michengai/dsh-im-connect");
|
|
149
151
|
(document.head ?? document.documentElement).append(style);
|
|
150
152
|
}
|
|
153
|
+
|
|
151
154
|
function validPayload(value) {
|
|
152
155
|
if (value === null || typeof value !== "object") return false;
|
|
153
156
|
const item = value;
|
|
@@ -233,6 +236,7 @@ function UpdateDialog(props) {
|
|
|
233
236
|
}
|
|
234
237
|
function observePluginUpdate(options) {
|
|
235
238
|
if (typeof document === "undefined" || document.body === null) return () => {};
|
|
239
|
+
const endpoint = resolveApiUrl(options.endpoint);
|
|
236
240
|
ensureStyle();
|
|
237
241
|
const createRoot = ReactDOM.createRoot || function createRootFallback(host) {
|
|
238
242
|
return {
|
|
@@ -267,7 +271,7 @@ function observePluginUpdate(options) {
|
|
|
267
271
|
const root = createRoot(host);
|
|
268
272
|
dialogRoot = root;
|
|
269
273
|
root.render(React.createElement(UpdateDialog, {
|
|
270
|
-
flow: createUpdateFlow((method) => requestStatus(
|
|
274
|
+
flow: createUpdateFlow((method) => requestStatus(endpoint, method, controller.signal), payload, (next) => { payload = next; applyControls(); }),
|
|
271
275
|
packageName: options.packageName, zhName: options.zhName, enName: options.enName, onClose: closeDialog,
|
|
272
276
|
}));
|
|
273
277
|
}
|
|
@@ -275,6 +279,26 @@ function observePluginUpdate(options) {
|
|
|
275
279
|
const row = document.querySelector(options.titleRowSelector);
|
|
276
280
|
if (row === null) return;
|
|
277
281
|
const heading = row.querySelector("h1,h2");
|
|
282
|
+
const links = row.querySelector(options.linksSelector);
|
|
283
|
+
|
|
284
|
+
if (options.isEnabled && !options.isEnabled()) {
|
|
285
|
+
if (dialogHost !== undefined) closeDialog();
|
|
286
|
+
if (links !== null) {
|
|
287
|
+
const existing = links.querySelector(`[data-mpi-check="${options.packageName}"]`);
|
|
288
|
+
if (existing !== null) {
|
|
289
|
+
unmountCheck();
|
|
290
|
+
existing.remove();
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
if (heading !== null) {
|
|
294
|
+
const version = heading.querySelector(`.mpi-version[data-package="${options.packageName}"]`);
|
|
295
|
+
if (version !== null) {
|
|
296
|
+
version.remove();
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
|
|
278
302
|
if (heading !== null && payload !== undefined) {
|
|
279
303
|
let version = heading.querySelector(`.mpi-version[data-package="${options.packageName}"]`);
|
|
280
304
|
if (version === null) {
|
|
@@ -287,7 +311,6 @@ function observePluginUpdate(options) {
|
|
|
287
311
|
if (version.textContent !== versionLabel) version.textContent = versionLabel;
|
|
288
312
|
}
|
|
289
313
|
if (buttonHost !== undefined && !buttonHost.isConnected) unmountCheck();
|
|
290
|
-
const links = row.querySelector(options.linksSelector);
|
|
291
314
|
if (links === null) return;
|
|
292
315
|
const existing = links.querySelector(`[data-mpi-check="${options.packageName}"]`);
|
|
293
316
|
if (existing !== null) {
|
|
@@ -304,7 +327,7 @@ function observePluginUpdate(options) {
|
|
|
304
327
|
renderCheck();
|
|
305
328
|
};
|
|
306
329
|
const load = async () => {
|
|
307
|
-
payload = await requestStatus(
|
|
330
|
+
payload = await requestStatus(endpoint, "GET", controller.signal);
|
|
308
331
|
applyControls();
|
|
309
332
|
return payload;
|
|
310
333
|
};
|
|
@@ -313,9 +336,15 @@ function observePluginUpdate(options) {
|
|
|
313
336
|
frame = window.requestAnimationFrame(() => { frame = undefined; applyControls(); });
|
|
314
337
|
});
|
|
315
338
|
observer.observe(document.documentElement, { childList: true, subtree: true, attributes: true, attributeFilter: ["lang"] });
|
|
339
|
+
const onToggle = () => {
|
|
340
|
+
applyControls();
|
|
341
|
+
if (!options.isEnabled || options.isEnabled()) void load().catch(() => {});
|
|
342
|
+
};
|
|
343
|
+
if (typeof window !== "undefined") window.addEventListener("ima-update-toggle", onToggle);
|
|
316
344
|
applyControls();
|
|
317
345
|
void load().catch(() => {});
|
|
318
|
-
|
|
346
|
+
const cleanup = () => {
|
|
347
|
+
if (typeof window !== "undefined") window.removeEventListener("ima-update-toggle", onToggle);
|
|
319
348
|
controller.abort();
|
|
320
349
|
observer.disconnect();
|
|
321
350
|
closeDialog();
|
|
@@ -323,9 +352,11 @@ function observePluginUpdate(options) {
|
|
|
323
352
|
if (frame !== undefined) window.cancelAnimationFrame(frame);
|
|
324
353
|
document.querySelectorAll(`[data-mpi-check="${options.packageName}"],.mpi-version[data-package="${options.packageName}"]`).forEach((node) => node.remove());
|
|
325
354
|
};
|
|
355
|
+
return cleanup;
|
|
326
356
|
}
|
|
327
357
|
export {
|
|
328
358
|
handlePluginUpdateEscape,
|
|
329
359
|
manualPluginUpdateCommand,
|
|
330
|
-
observePluginUpdate
|
|
360
|
+
observePluginUpdate,
|
|
361
|
+
resolveApiUrl
|
|
331
362
|
};
|
package/src/plugin-updater.ts
CHANGED
|
@@ -16,7 +16,7 @@ type DesktopPnpmHandle = { done: Promise<{ exitCode: number | null; signal: Node
|
|
|
16
16
|
type DesktopPnpm = { runPlugin(args: readonly string[], invokingDir: string, signal?: AbortSignal): DesktopPnpmHandle }
|
|
17
17
|
type UpdateContext = Context & { webServer: WebServer; get?(name: string): unknown }
|
|
18
18
|
|
|
19
|
-
export type PluginUpdaterOptions = { readonly endpoint: string; readonly packageName: string; readonly manifestUrl: URL }
|
|
19
|
+
export type PluginUpdaterOptions = { readonly endpoint: string; readonly packageName: string; readonly manifestUrl: URL; readonly isEnabled?: () => Promise<boolean> | boolean }
|
|
20
20
|
|
|
21
21
|
type PackageManagerLaunch = { command: string; args: readonly string[]; env?: NodeJS.ProcessEnv }
|
|
22
22
|
type PluginManagerInstall = { installBundle(spec: string): Promise<{ application?: string; error?: { message?: string }; packageResult?: { output?: string } }> }
|
|
@@ -492,6 +492,13 @@ export function registerPluginUpdater(ctx: Context, options: PluginUpdaterOption
|
|
|
492
492
|
kind: 'exact', path: options.endpoint,
|
|
493
493
|
handler: async (request, response) => {
|
|
494
494
|
try {
|
|
495
|
+
if (options.isEnabled) {
|
|
496
|
+
const allowed = await options.isEnabled()
|
|
497
|
+
if (!allowed) {
|
|
498
|
+
json(response, 403, { ok: false, error: '在线更新已被禁用。' })
|
|
499
|
+
return
|
|
500
|
+
}
|
|
501
|
+
}
|
|
495
502
|
const target = runtime(host)
|
|
496
503
|
if (request.method === 'GET' || request.method === 'HEAD') {
|
|
497
504
|
const payload = await status(options, target)
|