feat: add speech telemetry and bundle guard

This commit is contained in:
colinislit
2025-11-25 13:37:00 +01:00
parent b6c5d1a6d3
commit 7033329e0f
17 changed files with 2258 additions and 481 deletions

42
lib/telemetry/speech.ts Normal file
View File

@@ -0,0 +1,42 @@
interface SpeechTelemetryContext {
context: string
patientId?: string
intakeId?: string
reportId?: string
}
export type SpeechTelemetryAction = 'start' | 'stop' | 'final'
export type SpeechTelemetryPayload = SpeechTelemetryContext & {
action: SpeechTelemetryAction
metadata?: Record<string, unknown>
}
export function logSpeechUsage(payload: SpeechTelemetryPayload) {
if (typeof window === 'undefined') return
const body = JSON.stringify(payload)
if (navigator.sendBeacon) {
try {
const blob = new Blob([body], { type: 'application/json' })
navigator.sendBeacon('/api/telemetry/speech', blob)
return
} catch (error) {
console.warn('sendBeacon speech telemetry failed, falling back to fetch', error)
}
}
fetch('/api/telemetry/speech', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body,
credentials: 'same-origin',
keepalive: true,
}).catch((error) => {
console.warn('Speech telemetry fetch failed', error)
})
}
export type SpeechTelemetryOptions = SpeechTelemetryContext