cc-haha/adapters/wechat/typing.ts
程序员阿江(Relakkes) 622b94011f fix: keep WeChat agent replies visible
WeChat development turns were reaching the local CLI, but the adapter treated iLink business failures as successful sends and only emitted a short-lived typing indicator. The adapter now validates sendmessage/sendtyping ret codes, retries text replies without stale context tokens, keeps typing alive during long-running tool work, and surfaces queue failures back to the chat.

Constraint: WeChat uses separate sendmessage and sendtyping APIs with business-level ret codes inside HTTP 200 responses.
Rejected: Only fixing the typing indicator | the transcript showed the agent completed the task, so the reply delivery path also needed hardening.
Confidence: high
Scope-risk: narrow
Directive: Do not treat WeChat HTTP 200 responses as successful until the iLink ret/errcode body has been checked.
Tested: bun test adapters/wechat/__tests__/protocol.test.ts adapters/wechat/__tests__/typing.test.ts
Tested: cd adapters && bunx tsc --noEmit
Tested: bun run check:adapters
Tested: real bound WeChat getconfig, typing, text send, cancel typing smoke
Not-tested: Full inbound WeChat user-message loop without a fresh user-triggered message
2026-05-05 19:58:53 +08:00

43 lines
1.1 KiB
TypeScript

export type WechatTypingStatus = 'typing' | 'cancel'
type SendTyping = (chatId: string, status: WechatTypingStatus) => Promise<void>
/**
* WeChat typing indicators expire quickly. Keep sending typing while the
* desktop session is active, then cancel when the turn completes.
*/
export class WechatTypingController {
private readonly active = new Map<string, ReturnType<typeof setInterval>>()
constructor(
private readonly sendTyping: SendTyping,
private readonly keepaliveIntervalMs = 5000,
) {}
start(chatId: string): void {
void this.sendTyping(chatId, 'typing')
if (this.active.has(chatId)) return
const timer = setInterval(() => {
void this.sendTyping(chatId, 'typing')
}, this.keepaliveIntervalMs)
this.active.set(chatId, timer)
}
stop(chatId: string): void {
const timer = this.active.get(chatId)
if (timer) {
clearInterval(timer)
this.active.delete(chatId)
}
void this.sendTyping(chatId, 'cancel')
}
destroy(): void {
for (const timer of this.active.values()) {
clearInterval(timer)
}
this.active.clear()
}
}