fix: unify stream cleanup and async request ownership
This commit is contained in:
1 parent
9b165a8971
commit
fb39af6c28
13 files changed
+667
-461
No files matched your search
@@ -6,12 +6,7 @@ import type { MessageStore } from '../message-store';
|
||||
import type { OneBotInstanceContext } from '../instance-context';
|
||||
import { resolveRemoteMediaUrl } from '../media-url-resolver';
|
||||
import { segmentsToRawMessage } from '../helper/cq';
|
||||
import {
|
||||
deliverPttTransText,
|
||||
failPttTransWaiter,
|
||||
pttTransKey,
|
||||
waitPttTransText,
|
||||
} from './ptt-trans-waiter';
|
||||
import { pttTransKey, requestPttTransText } from './ptt-trans-waiter';
|
||||
import type { JsonObject, JsonValue } from '../types';
|
||||
|
||||
const log = createLogger('OneBot');
|
||||
@@ -197,15 +192,8 @@ export async function fetchPttText(
|
||||
// c2c: receiver is self (inbound voice); group: the group uin.
|
||||
const peerUin = isGroup ? Number(event.group_id) || cached.sessionId : selfId;
|
||||
|
||||
// Register the waiter BEFORE triggering, so a fast async push can't race
|
||||
// ahead of us. The trigger response may carry the text inline (already
|
||||
// transcribed) — settle immediately then; otherwise the Event 0x210
|
||||
// subType-61 push resolves the waiter via the event-pipeline subscription.
|
||||
const key = pttTransKey(selfId, messageId);
|
||||
const waiter = waitPttTransText(key, 20000);
|
||||
|
||||
try {
|
||||
const syncText = await bridge.apis.extras.translatePttToText({
|
||||
const text = await requestPttTransText(pttTransKey(selfId, messageId), 20_000, () =>
|
||||
bridge.apis.extras.translatePttToText({
|
||||
isGroup,
|
||||
msgId: messageId,
|
||||
senderUin,
|
||||
@@ -215,16 +203,9 @@ export async function fetchPttText(
|
||||
duration: cached.duration ?? 0,
|
||||
size: cached.fileSize ?? 0,
|
||||
format: cached.voiceFormat ?? 0,
|
||||
});
|
||||
|
||||
if (syncText) {
|
||||
deliverPttTransText(key, syncText);
|
||||
}
|
||||
} catch (e) {
|
||||
failPttTransWaiter(key, e instanceof Error ? e : new Error(String(e)));
|
||||
}
|
||||
|
||||
return { text: await waiter };
|
||||
}),
|
||||
);
|
||||
return { text };
|
||||
}
|
||||
|
||||
export async function getImageInfo(
|
||||
|
||||
@@ -1,69 +1,58 @@
|
||||
// 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>;
|
||||
resolve(text: string): void;
|
||||
reject(error: Error): void;
|
||||
}
|
||||
|
||||
const pending = new Map<string, Pending>();
|
||||
|
||||
export function pttTransKey(selfUin: number, msgId: number): string {
|
||||
return `${selfUin}:${msgId}`;
|
||||
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('语音转文字请求被新的请求取代'));
|
||||
}
|
||||
/** Register before triggering; only this request may settle its own response or timeout. */
|
||||
export function requestPttTransText(
|
||||
key: string,
|
||||
timeoutMs: number,
|
||||
trigger: () => Promise<string>,
|
||||
): Promise<string> {
|
||||
pending.get(key)?.reject(new Error('语音转文字请求被新的请求取代'));
|
||||
|
||||
return new Promise<string>((resolve, reject) => {
|
||||
const timer = setTimeout(() => {
|
||||
if (pending.get(key)?.timer === timer) {
|
||||
pending.delete(key);
|
||||
const settle = (finish: () => void) => {
|
||||
if (pending.get(key) !== waiter) {
|
||||
return;
|
||||
}
|
||||
|
||||
reject(new Error('语音转文字超时(未收到结果推送)'));
|
||||
clearTimeout(timer);
|
||||
pending.delete(key);
|
||||
finish();
|
||||
};
|
||||
|
||||
const waiter: Pending = {
|
||||
resolve: (text) => settle(() => resolve(text)),
|
||||
reject: (error) => settle(() => reject(error)),
|
||||
};
|
||||
const timer = setTimeout(() => {
|
||||
waiter.reject(new Error('语音转文字超时(未收到结果推送)'));
|
||||
}, timeoutMs);
|
||||
timer.unref();
|
||||
pending.set(key, { resolve, reject, timer });
|
||||
pending.set(key, waiter);
|
||||
|
||||
void Promise.resolve()
|
||||
.then(trigger)
|
||||
.then(
|
||||
(text) => {
|
||||
if (text) {
|
||||
waiter.resolve(text);
|
||||
}
|
||||
},
|
||||
(error: unknown) =>
|
||||
waiter.reject(error instanceof Error ? error : new Error(String(error))),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/** Resolve a waiting fetch_ptt_text with the recognised text. No-op if none. */
|
||||
/** QQ push results identify the account and message, not an individual trigger request. */
|
||||
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);
|
||||
pending.get(key)?.resolve(text);
|
||||
}
|
||||
Reference in new issue
Block a user