File size: 7,380 Bytes
a202950 ed78fab a202950 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 | import { WebSocket } from "ws";
import type { BufferedAudioFrame } from "../shared/realtime-voice-protocol";
import type {
RealtimeSttRecognizer,
SendVoiceMessage,
SttEnvironment,
} from "./realtime-stt-provider";
const CONNECT_TIMEOUT_MS = 8000;
const MAX_PENDING_FRAMES = 600;
function transcriptWords(text: string): string[] {
return text
.replace(/[^\p{L}\p{N}]+/gu, " ")
.trim()
.split(/\s+/)
.filter(Boolean);
}
function endpointingMs(environment: SttEnvironment): number {
const value = Number(environment.SHIHAB_ENDPOINTING_MS ?? 800);
return Number.isFinite(value) && value >= 300 && value <= 3000
? Math.round(value)
: 800;
}
export function shihabWebSocketUrl(
environment: SttEnvironment = process.env,
): string {
const url = new URL("wss://api.shihab.co.uk/v1/listen/stream");
url.search = new URLSearchParams({
model: environment.SHIHAB_MODEL?.trim() || "shihab-asr-v1",
language: "ar-PS",
encoding: "linear16",
sample_rate: "16000",
channels: "1",
interim_results: "true",
endpointing: String(endpointingMs(environment)),
store: "false",
}).toString();
return url.toString();
}
export class ShihabRealtimeRecognizer implements RealtimeSttRecognizer {
private socket: WebSocket | null = null;
private connectPromise: Promise<void> | null = null;
private active = true;
private ready = false;
private lastAcknowledgedSequence = -1;
private pendingFrames = new Map<number, BufferedAudioFrame>();
private finalEventHistory = new Map<number, Record<string, unknown>>();
private latestInterimEvent: Record<string, unknown> | null = null;
private utteranceIndex = 0;
private revision = 0;
constructor(
readonly sessionId: string,
private readonly emit: SendVoiceMessage,
private readonly environment: SttEnvironment = process.env,
) {}
start(): Promise<void> {
this.active = true;
return this.ensureConnected();
}
accept(frame: BufferedAudioFrame): void {
if (frame.audioSequence <= this.lastAcknowledgedSequence) {
this.emitAck();
return;
}
if (!this.pendingFrames.has(frame.audioSequence)) {
if (this.pendingFrames.size >= MAX_PENDING_FRAMES) {
this.emitError("Shihab replay buffer is full");
return;
}
this.pendingFrames.set(frame.audioSequence, frame);
}
this.sendFrame(frame);
if (!this.socket || this.socket.readyState !== WebSocket.OPEN) {
void this.ensureConnected().catch((error) => this.emitError(error));
}
}
stop(): void {
this.active = false;
if (this.socket?.readyState === WebSocket.OPEN) {
this.socket.send(JSON.stringify({ type: "CloseStream" }));
}
}
close(): void {
this.active = false;
this.socket?.close();
this.socket = null;
this.connectPromise = null;
this.ready = false;
}
acknowledgedSequence(): number {
return this.lastAcknowledgedSequence;
}
replayTranscriptEvents(): void {
for (const event of [...this.finalEventHistory.entries()]
.sort(([left], [right]) => left - right)
.map(([, value]) => value)) {
this.emit(event);
}
if (this.latestInterimEvent) this.emit(this.latestInterimEvent);
}
private ensureConnected(): Promise<void> {
if (this.socket?.readyState === WebSocket.OPEN && this.ready) {
return Promise.resolve();
}
if (this.connectPromise) return this.connectPromise;
const apiKey = this.environment.SHIHAB_API_KEY?.trim();
if (!apiKey) return Promise.reject(new Error("SHIHAB_API_KEY is missing"));
const promise = new Promise<void>((resolve, reject) => {
const socket = new WebSocket(shihabWebSocketUrl(this.environment), {
headers: { Authorization: `Bearer ${apiKey}` },
perMessageDeflate: false,
});
this.socket = socket;
let settled = false;
const timeout = setTimeout(() => {
if (settled) return;
settled = true;
socket.close();
reject(new Error("Timed out connecting to ShihabASR"));
}, CONNECT_TIMEOUT_MS);
socket.on("open", () => {
if (settled) return;
settled = true;
clearTimeout(timeout);
this.ready = true;
for (const frame of [...this.pendingFrames.values()].sort(
(left, right) => left.audioSequence - right.audioSequence,
)) {
this.sendFrame(frame);
}
resolve();
});
socket.on("message", (raw, isBinary) => {
if (isBinary) return;
let message: Record<string, any>;
try {
message = JSON.parse(raw.toString());
} catch {
return;
}
if (message.type === "Results") this.forwardResult(message);
else if (message.type === "Error") {
this.emitError(
`${message.code ?? "shihab_error"}: ${message.message ?? "ShihabASR error"}`,
);
}
});
socket.on("error", (error) => {
if (!settled) {
settled = true;
clearTimeout(timeout);
reject(error);
} else this.emitError(error);
});
socket.on("close", (code) => {
clearTimeout(timeout);
if (this.socket === socket) this.socket = null;
this.ready = false;
this.connectPromise = null;
if (this.active && code !== 1000) {
this.emitError(`ShihabASR stream closed (${code})`);
}
});
}).finally(() => {
this.connectPromise = null;
});
this.connectPromise = promise;
return promise;
}
private sendFrame(frame: BufferedAudioFrame): void {
if (!this.ready || this.socket?.readyState !== WebSocket.OPEN) return;
this.socket.send(Buffer.from(frame.audio, "base64"));
this.lastAcknowledgedSequence = Math.max(
this.lastAcknowledgedSequence,
frame.audioSequence,
);
this.pendingFrames.delete(frame.audioSequence);
this.emitAck();
}
private emitAck(): void {
this.emit({
type: "audio_ack",
sessionId: this.sessionId,
audioSequence: this.lastAcknowledgedSequence,
});
}
private forwardResult(message: Record<string, any>): void {
const text = String(message.transcript ?? "").trim();
if (!text) return;
const isFinal = message.is_final === true;
if (this.environment.STT_LOG_TRANSCRIPTS?.trim().toLowerCase() === "true") {
console.log(
`[voice-ws][shihab][${this.sessionId}] ${isFinal ? "final" : "interim"}: ${JSON.stringify(text)}`,
);
}
const event: Record<string, unknown> = {
type: isFinal ? "final" : "interim",
sessionId: this.sessionId,
utteranceId: `${this.sessionId}:${this.utteranceIndex}`,
utteranceIndex: this.utteranceIndex,
revision: ++this.revision,
audioSequence: this.lastAcknowledgedSequence,
text,
words: isFinal ? transcriptWords(text) : undefined,
};
if (isFinal) {
this.finalEventHistory.set(this.utteranceIndex, event);
this.latestInterimEvent = null;
this.utteranceIndex++;
this.revision = 0;
} else this.latestInterimEvent = event;
this.emit(event);
}
private emitError(error: unknown): void {
this.emit({
type: "error",
sessionId: this.sessionId,
message: error instanceof Error ? error.message : String(error),
});
}
}
|