|
1 | 1 | import { randomUUID } from "node:crypto"; |
2 | 2 | import type { RealtimeTranscriptionProviderPlugin } from "../plugins/types.js"; |
3 | 3 | import type { RealtimeTranscriptionProviderConfig } from "../realtime-transcription/provider-types.js"; |
4 | | -import { parseFiniteNumber as readFiniteNumber } from "../shared/number-coercion.js"; |
| 4 | +import { |
| 5 | +asDateTimestampMs, |
| 6 | +parseFiniteNumber as readFiniteNumber, |
| 7 | +resolveExpiresAtMsFromDurationMs, |
| 8 | +} from "../shared/number-coercion.js"; |
5 | 9 | import { recordTalkObservabilityEvent } from "../talk/observability.js"; |
6 | 10 | import { |
7 | 11 | type TalkEvent, |
@@ -177,8 +181,13 @@ function closeTranscriptionSession(
|
177 | 181 | } |
178 | 182 | |
179 | 183 | function pruneExpiredTranscriptionSessions(nowMs = Date.now()): void { |
| 184 | +const validNowMs = asDateTimestampMs(nowMs); |
| 185 | +if (validNowMs === undefined) { |
| 186 | +return; |
| 187 | +} |
180 | 188 | for (const session of transcriptionSessions.values()) { |
181 | | -if (nowMs > session.expiresAtMs) { |
| 189 | +const expiresAtMs = asDateTimestampMs(session.expiresAtMs); |
| 190 | +if (expiresAtMs === undefined || validNowMs > expiresAtMs) { |
182 | 191 | closeTranscriptionSession(session, "completed"); |
183 | 192 | } |
184 | 193 | } |
@@ -210,7 +219,10 @@ export function createTalkTranscriptionRelaySession(
|
210 | 219 | enforceTranscriptionSessionLimits(params.connId); |
211 | 220 | assertRelayInputAudioConfig(params.providerConfig); |
212 | 221 | const transcriptionSessionId = randomUUID(); |
213 | | -const expiresAtMs = Date.now() + TRANSCRIPTION_SESSION_TTL_MS; |
| 222 | +const expiresAtMs = resolveExpiresAtMsFromDurationMs(TRANSCRIPTION_SESSION_TTL_MS); |
| 223 | +if (expiresAtMs === undefined) { |
| 224 | +throw new Error("Transcription relay session expiry is outside the supported Date range"); |
| 225 | +} |
214 | 226 | const talk = createTalkSessionController( |
215 | 227 | { |
216 | 228 | sessionId: transcriptionSessionId, |
@@ -346,7 +358,15 @@ function getTranscriptionSession(
|
346 | 358 | connId: string, |
347 | 359 | ): TranscriptionRelaySession { |
348 | 360 | const session = transcriptionSessions.get(transcriptionSessionId); |
349 | | -if (!session || session.connId !== connId || Date.now() > session.expiresAtMs) { |
| 361 | +const nowMs = asDateTimestampMs(Date.now()); |
| 362 | +const expiresAtMs = session ? asDateTimestampMs(session.expiresAtMs) : undefined; |
| 363 | +if ( |
| 364 | +!session || |
| 365 | +session.connId !== connId || |
| 366 | +nowMs === undefined || |
| 367 | +expiresAtMs === undefined || |
| 368 | +nowMs > expiresAtMs |
| 369 | +) { |
350 | 370 | if (session) { |
351 | 371 | closeTranscriptionSession(session, "completed"); |
352 | 372 | } |
|