feat(onebot): fetch_ptt_text via async 0x210/61 result push (#79)
Live testing showed the voice-to-text text is delivered by an async push
(Event 0x210 subType 61), not the request response — so the previous
poll-by-resend never got it. Captured a real push and decoded its protobuf:
outer { f1, f2:item }; the item echoes the request msgId at field 1 (the
correlation key) and the recognised text at field 8.
- proto-defs: PttTransPush / PttTransPushItem (live-verified).
- protocol: Event0x210SubType.PttTransResult = 61 + decoder → internal
`ptt_trans_result` event { msgId, text } (replaces the temp hex dump).
- core: translatePttToText now sends ONE trigger and returns the inline text
only if present (already-transcribed); empty = pending.
- onebot: ptt-trans-waiter (keyed by selfUin:msgId), event-pipeline subscribes
`ptt_trans_result` to resolve it; fetch_ptt_text registers the waiter,
fires the trigger, and awaits the push (20s timeout).
Decoded one real push as text "12311123。" end to end.
This commit is contained in:
8 files changed
+164
-54
No files matched your search
@@ -0,0 +1,56 @@
|
||||
// Correlates an async voice-to-text result back to the fetch_ptt_text call
|
||||
// that triggered it. QQ delivers the text via an Event 0x210 subType-61 push
|
||||
// (decoded into a `ptt_trans_result` event), not in the request response — so
|
||||
// the action registers a waiter keyed by `${selfUin}:${msgId}`, fires the
|
||||
// trigger request, and the push subscription resolves the waiter by msgId.
|
||||
|
||||
interface Pending {
|
||||
resolve: (text: string) => void;
|
||||
reject: (err: Error) => void;
|
||||
timer: ReturnType<typeof setTimeout>;
|
||||
}
|
||||
|
||||
const pending = new Map<string, Pending>();
|
||||
|
||||
export function pttTransKey(selfUin: number, msgId: number): string {
|
||||
return `${selfUin}:${msgId}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a waiter for `key`'s transcription. Rejects after `timeoutMs`.
|
||||
* A new waiter for the same key supersedes (rejects) the previous one.
|
||||
*/
|
||||
export function waitPttTransText(key: string, timeoutMs: number): Promise<string> {
|
||||
const existing = pending.get(key);
|
||||
if (existing) {
|
||||
clearTimeout(existing.timer);
|
||||
pending.delete(key);
|
||||
existing.reject(new Error('语音转文字请求被新的请求取代'));
|
||||
}
|
||||
return new Promise<string>((resolve, reject) => {
|
||||
const timer = setTimeout(() => {
|
||||
if (pending.get(key)?.timer === timer) pending.delete(key);
|
||||
reject(new Error('语音转文字超时(未收到结果推送)'));
|
||||
}, timeoutMs);
|
||||
timer.unref?.();
|
||||
pending.set(key, { resolve, reject, timer });
|
||||
});
|
||||
}
|
||||
|
||||
/** Resolve a waiting fetch_ptt_text with the recognised text. No-op if none. */
|
||||
export function deliverPttTransText(key: string, text: string): void {
|
||||
const p = pending.get(key);
|
||||
if (!p) return;
|
||||
clearTimeout(p.timer);
|
||||
pending.delete(key);
|
||||
p.resolve(text);
|
||||
}
|
||||
|
||||
/** Reject a waiter (e.g. the trigger request itself failed). No-op if none. */
|
||||
export function failPttTransWaiter(key: string, err: Error): void {
|
||||
const p = pending.get(key);
|
||||
if (!p) return;
|
||||
clearTimeout(p.timer);
|
||||
pending.delete(key);
|
||||
p.reject(err);
|
||||
}
|
||||
Reference in new issue
Block a user