@2en/clawly-plugins 1.32.0-beta.0 → 1.32.0-beta.2
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/gateway/index.ts +2 -0
- package/gateway/weixin-login.ts +140 -0
- package/package.json +1 -1
package/gateway/index.ts
CHANGED
|
@@ -23,6 +23,7 @@ import {registerPresence} from './presence'
|
|
|
23
23
|
import {registerSessionSanitize} from './session-sanitize'
|
|
24
24
|
import {registerUploadHttpRoute} from '../http/file/upload'
|
|
25
25
|
import {registerVersion} from './version'
|
|
26
|
+
import {registerWeixinLogin} from './weixin-login'
|
|
26
27
|
|
|
27
28
|
export function registerGateway(api: PluginApi) {
|
|
28
29
|
// Initialize OTel logs provider (no-op if plugin telemetry config is missing)
|
|
@@ -72,4 +73,5 @@ export function registerGateway(api: PluginApi) {
|
|
|
72
73
|
registerUploadHttpRoute(api)
|
|
73
74
|
registerAudit(api)
|
|
74
75
|
registerCalendarNative(api)
|
|
76
|
+
registerWeixinLogin(api)
|
|
75
77
|
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WeChat (openclaw-weixin) QR login bridge.
|
|
3
|
+
*
|
|
4
|
+
* The official @tencent-weixin/openclaw-weixin plugin registers as a ChannelPlugin
|
|
5
|
+
* with loginWithQrStart/loginWithQrWait on its gateway adapter, but does NOT
|
|
6
|
+
* register as the `web.login.start` provider (WhatsApp owns that slot).
|
|
7
|
+
*
|
|
8
|
+
* These two gateway methods expose the WeChat plugin's login flow to the mobile app:
|
|
9
|
+
* clawly.weixin.login.start → { qrDataUrl, message }
|
|
10
|
+
* clawly.weixin.login.wait → { connected, message }
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import type {PluginApi} from '../types'
|
|
14
|
+
|
|
15
|
+
const WEIXIN_CHANNEL_ID = 'openclaw-weixin'
|
|
16
|
+
const GATEWAY_TIMEOUT_MS = 60_000
|
|
17
|
+
|
|
18
|
+
/** Race a promise against a timeout, clearing the timer on settle. */
|
|
19
|
+
function withTimeout<T>(promise: Promise<T>, ms: number, label: string): Promise<T> {
|
|
20
|
+
let timeoutId: ReturnType<typeof setTimeout> | undefined
|
|
21
|
+
const timeoutPromise = new Promise<never>((_, reject) => {
|
|
22
|
+
timeoutId = setTimeout(() => reject(new Error(`${label} timed out`)), ms)
|
|
23
|
+
})
|
|
24
|
+
return Promise.race([promise, timeoutPromise]).finally(() => clearTimeout(timeoutId))
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
type ChannelPluginLike = {
|
|
28
|
+
id: string
|
|
29
|
+
gateway?: {
|
|
30
|
+
loginWithQrStart?: (params: {
|
|
31
|
+
accountId?: string
|
|
32
|
+
force?: boolean
|
|
33
|
+
timeoutMs?: number
|
|
34
|
+
verbose?: boolean
|
|
35
|
+
}) => Promise<{qrDataUrl?: string; message: string}>
|
|
36
|
+
loginWithQrWait?: (params: {
|
|
37
|
+
accountId?: string
|
|
38
|
+
timeoutMs?: number
|
|
39
|
+
}) => Promise<{connected: boolean; message: string}>
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Resolve the openclaw-weixin channel plugin from the host OpenClaw process.
|
|
45
|
+
*
|
|
46
|
+
* Accesses the plugin registry singleton via the well-known global symbol
|
|
47
|
+
* `Symbol.for("openclaw.pluginRegistryState")` (see openclaw/src/plugins/runtime.ts).
|
|
48
|
+
* This avoids importing internal OpenClaw paths that don't exist in the published
|
|
49
|
+
* npm package (bundled dist only).
|
|
50
|
+
*
|
|
51
|
+
* Assumed registry shape: `{ channels: Array<{ plugin: ChannelPlugin }> }`.
|
|
52
|
+
* Verified against OpenClaw 2026.3.x. If the shape changes, this returns null
|
|
53
|
+
* with a logged warning — re-verify after major OpenClaw upgrades.
|
|
54
|
+
*/
|
|
55
|
+
function resolveWeixinPlugin(logger?: {warn: (msg: string) => void}): ChannelPluginLike | null {
|
|
56
|
+
try {
|
|
57
|
+
const REGISTRY_STATE = Symbol.for('openclaw.pluginRegistryState')
|
|
58
|
+
const registryState = (globalThis as Record<symbol, unknown>)[REGISTRY_STATE] as
|
|
59
|
+
| {registry: {channels: Array<{plugin: ChannelPluginLike}>} | null}
|
|
60
|
+
| undefined
|
|
61
|
+
const channels = registryState?.registry?.channels
|
|
62
|
+
if (!channels) {
|
|
63
|
+
logger?.warn('weixin-login: plugin registry not available on globalThis')
|
|
64
|
+
return null
|
|
65
|
+
}
|
|
66
|
+
const entry = channels.find((e) => e.plugin?.id === WEIXIN_CHANNEL_ID)
|
|
67
|
+
return entry?.plugin ?? null
|
|
68
|
+
} catch (err) {
|
|
69
|
+
logger?.warn(
|
|
70
|
+
`weixin-login: failed to resolve channel plugin: ${err instanceof Error ? err.message : err}`,
|
|
71
|
+
)
|
|
72
|
+
return null
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function registerWeixinLogin(api: PluginApi) {
|
|
77
|
+
api.registerGatewayMethod('clawly.weixin.login.start', async ({params, respond}) => {
|
|
78
|
+
const plugin = resolveWeixinPlugin(api.logger)
|
|
79
|
+
if (!plugin?.gateway?.loginWithQrStart) {
|
|
80
|
+
respond(false, undefined, {
|
|
81
|
+
code: 'WEIXIN_PLUGIN_NOT_AVAILABLE',
|
|
82
|
+
message:
|
|
83
|
+
'WeChat plugin is not installed or does not support QR login. ' +
|
|
84
|
+
'Install it with: openclaw plugins install "@tencent-weixin/openclaw-weixin"',
|
|
85
|
+
})
|
|
86
|
+
return
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
try {
|
|
90
|
+
const p = (params ?? {}) as Record<string, unknown>
|
|
91
|
+
const result = await withTimeout(
|
|
92
|
+
plugin.gateway.loginWithQrStart({
|
|
93
|
+
accountId: p.accountId as string | undefined,
|
|
94
|
+
force: p.force !== undefined ? Boolean(p.force) : undefined,
|
|
95
|
+
timeoutMs: typeof p.timeoutMs === 'number' ? p.timeoutMs : undefined,
|
|
96
|
+
verbose: p.verbose !== undefined ? Boolean(p.verbose) : undefined,
|
|
97
|
+
}),
|
|
98
|
+
GATEWAY_TIMEOUT_MS,
|
|
99
|
+
'loginWithQrStart',
|
|
100
|
+
)
|
|
101
|
+
respond(true, result)
|
|
102
|
+
} catch (err) {
|
|
103
|
+
respond(false, undefined, {
|
|
104
|
+
code: 'WEIXIN_LOGIN_START_ERROR',
|
|
105
|
+
message: err instanceof Error ? err.message : 'Failed to start WeChat QR login',
|
|
106
|
+
})
|
|
107
|
+
}
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
api.registerGatewayMethod('clawly.weixin.login.wait', async ({params, respond}) => {
|
|
111
|
+
const plugin = resolveWeixinPlugin(api.logger)
|
|
112
|
+
if (!plugin?.gateway?.loginWithQrWait) {
|
|
113
|
+
respond(false, undefined, {
|
|
114
|
+
code: 'WEIXIN_PLUGIN_NOT_AVAILABLE',
|
|
115
|
+
message: 'WeChat plugin is not installed or does not support QR login.',
|
|
116
|
+
})
|
|
117
|
+
return
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
try {
|
|
121
|
+
const p = (params ?? {}) as Record<string, unknown>
|
|
122
|
+
const result = await withTimeout(
|
|
123
|
+
plugin.gateway.loginWithQrWait({
|
|
124
|
+
accountId: p.accountId as string | undefined,
|
|
125
|
+
timeoutMs: typeof p.timeoutMs === 'number' ? p.timeoutMs : undefined,
|
|
126
|
+
}),
|
|
127
|
+
GATEWAY_TIMEOUT_MS,
|
|
128
|
+
'loginWithQrWait',
|
|
129
|
+
)
|
|
130
|
+
respond(true, result)
|
|
131
|
+
} catch (err) {
|
|
132
|
+
respond(false, undefined, {
|
|
133
|
+
code: 'WEIXIN_LOGIN_WAIT_ERROR',
|
|
134
|
+
message: err instanceof Error ? err.message : 'Failed to wait for WeChat QR login',
|
|
135
|
+
})
|
|
136
|
+
}
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
api.logger.info('weixin-login: registered clawly.weixin.login.start/wait methods')
|
|
140
|
+
}
|