fix: 完善微信发送运行时状态识别

This commit is contained in:
Wxw-Gu committed 2026-10-29 17:16:44 +08:00
1 parent d6d837e6f6
commit 84737a7407
7 files changed
+43 -278

No files matched your search

@@ -66,6 +66,16 @@ export function deriveMacWechatSenderStatus({
}: MacWechatSenderStatusInput): PersonalWechatSenderStatus {
const identityMatches = binding !== null && bindingMatchesRuntimeManifest(binding, manifest)
const stale = binding?.bindingState === 'stale'
const versionCapabilities = binding?.wechatVersion
? manifest.capabilitiesByVersion?.[binding.wechatVersion]
: undefined
const nativeMechanism = versionCapabilities?.textMechanism === 'native-msg-object'
const nativeBindingReady =
identityMatches &&
nativeMechanism &&
binding?.runtimeState === 'ready' &&
binding.sessionAttached &&
binding.scriptLoaded
let state: PersonalWechatSenderStatus['state'] = 'stopped'
let message = '正在初始化微信发送能力'
@@ -78,12 +88,17 @@ export function deriveMacWechatSenderStatus({
} else if (binding && !binding.wechatRunning) {
state = 'wechat_not_running'
message = '微信未运行'
} else if (binding?.bindingState === 'ready') {
} else if (binding?.runtimeState === 'unsupported_version') {
state = 'error'
message = binding.wechatVersion
? `当前微信版本是 ${binding.wechatVersion},不在支持列表`
: '当前微信版本不在支持列表'
} else if (binding?.bindingState === 'ready' || nativeBindingReady) {
state = 'online'
message = '个人微信已准备好发送日报'
} else if (binding?.bindingState === 'bound' || binding?.bindingState === 'binding') {
state = 'hook_not_ready'
message = '已绑定,等待捕获微信发送上下文'
message = nativeMechanism ? '已绑定,等待原生发送 hook 就绪' : '已绑定,等待捕获微信发送上下文'
} else if (stale) {
state = 'stopped'
message = '检测到微信已重启,请重新绑定'
@@ -96,13 +111,20 @@ export function deriveMacWechatSenderStatus({
}
const runtimeCapabilities = new Set(manifest.capabilities)
const bindingReady = identityMatches && state === 'online' && binding?.sendContextReady === true
const supportsCapability = (name: 'text' | 'image' | 'voice'): boolean => {
if (manifest.capabilitiesByVersion) return versionCapabilities?.[name] === 'verified'
return runtimeCapabilities.has(name)
}
const bindingReady =
identityMatches &&
state === 'online' &&
(binding?.sendContextReady === true || nativeBindingReady)
const canSendText =
bindingReady && runtimeCapabilities.has('text') && binding?.canSendText === true
bindingReady && supportsCapability('text') && binding?.canSendText === true
const canSendImage =
bindingReady && runtimeCapabilities.has('image') && binding?.canSendImage === true
bindingReady && supportsCapability('image') && binding?.canSendImage === true
const canSendVoice =
bindingReady && runtimeCapabilities.has('voice') && binding?.canSendVoice === true
bindingReady && supportsCapability('voice') && binding?.canSendVoice === true
const sessionAttached = identityMatches && (binding?.sessionAttached ?? false)
const scriptLoaded = identityMatches && (binding?.scriptLoaded ?? false)
@@ -159,7 +181,6 @@ class MacWechatRuntimeManager {
endpoint: `http://${HOST}:${PORT}`,
port: PORT,
runtimeDir: this.resolveRuntimeDir(),
manifest: this.manifest ?? undefined,
error: this.lastError ?? undefined,
binding: this.lastBinding ?? undefined
}
@@ -450,7 +471,11 @@ class MacWechatRuntimeManager {
const { status, body } = await this.request('GET', '/bindingStatus')
if (status !== 200) return null
try {
this.lastBinding = JSON.parse(body) as MacWechatBindingStatus
const binding = JSON.parse(body) as MacWechatBindingStatus & {
supportedVersions?: string
}
delete binding.supportedVersions
this.lastBinding = binding
/*
* A 20s bind timeout is NOT proof of the orphan-agent wall: in the
* stale-rebind path the host worker waits up to 45s for the old session
+10 -2
View File
@@ -45,6 +45,16 @@ export interface MacWechatRuntimeManifest {
addressProfileSha256: string
supportedWechatVersions: string[]
capabilities: Array<'text' | 'image' | 'voice'>
capabilitiesByVersion?: Record<
string,
{
text?: 'verified' | 'addresses-only' | 'unsupported'
image?: 'verified' | 'addresses-only' | 'unsupported'
voice?: 'verified' | 'addresses-only' | 'unsupported'
downloadImage?: 'verified' | 'addresses-only' | 'unsupported'
textMechanism?: string
}
>
}
export interface MacWechatBindingStatus {
@@ -60,7 +70,6 @@ export interface MacWechatBindingStatus {
wechatPid: number
wechatVersion?: string
supported: boolean
supportedVersions?: string
bindingState: MacWechatBindingState
bound: boolean
boundWechatPid: number
@@ -82,7 +91,6 @@ export interface MacWechatRuntimeSnapshot {
endpoint: string
port: number
runtimeDir?: string
manifest?: MacWechatRuntimeManifest
error?: string
binding?: MacWechatBindingStatus
}