@oxvo/ai-live-assist 7.4.50 → 7.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (163) hide show
  1. package/cjs/AiLiveAssist.d.ts +76 -11
  2. package/cjs/AiLiveAssist.js +1771 -298
  3. package/cjs/client.d.ts +19 -0
  4. package/cjs/client.js +51 -0
  5. package/cjs/context.d.ts +23 -1
  6. package/cjs/context.js +713 -126
  7. package/cjs/control.d.ts +2 -0
  8. package/cjs/control.js +42 -3
  9. package/cjs/ctaOffer.d.ts +11 -0
  10. package/cjs/ctaOffer.js +185 -0
  11. package/cjs/dialogFocusScope.js +1 -1
  12. package/cjs/guideAdmission.d.ts +46 -0
  13. package/cjs/guideAdmission.js +397 -0
  14. package/cjs/guideHost.d.ts +47 -0
  15. package/cjs/guideHost.js +48 -0
  16. package/cjs/guideInstallation.d.ts +12 -0
  17. package/cjs/guideInstallation.js +60 -0
  18. package/cjs/guideInstaller.d.ts +4 -0
  19. package/cjs/guideInstaller.js +116 -0
  20. package/cjs/guideMount.d.ts +22 -0
  21. package/cjs/guideMount.js +84 -0
  22. package/cjs/guideRecording.d.ts +64 -0
  23. package/cjs/guideRecording.js +176 -0
  24. package/cjs/guideRecordingSession.d.ts +35 -0
  25. package/cjs/guideRecordingSession.js +167 -0
  26. package/cjs/guideSubjectStore.d.ts +32 -0
  27. package/cjs/guideSubjectStore.js +105 -0
  28. package/cjs/guideTypes.d.ts +117 -0
  29. package/cjs/guideTypes.js +15 -0
  30. package/cjs/index.d.ts +6 -0
  31. package/cjs/index.js +25 -11
  32. package/cjs/liveAudioProcessor.d.ts +92 -0
  33. package/cjs/liveAudioProcessor.js +269 -0
  34. package/cjs/liveAudioQueue.d.ts +40 -0
  35. package/cjs/liveAudioQueue.js +130 -0
  36. package/cjs/liveAudioWorklet.d.ts +1 -0
  37. package/cjs/liveAudioWorklet.js +63 -0
  38. package/cjs/liveBrowserDetector.d.ts +16 -0
  39. package/cjs/liveBrowserDetector.js +75 -0
  40. package/cjs/liveBrowserDetectorWorker.d.ts +1 -0
  41. package/cjs/liveBrowserDetectorWorker.js +72 -0
  42. package/cjs/liveControlChannel.d.ts +176 -0
  43. package/cjs/liveControlChannel.js +829 -0
  44. package/cjs/liveDuplexMedia.d.ts +204 -0
  45. package/cjs/liveDuplexMedia.js +740 -0
  46. package/cjs/liveGuideAdmission.d.ts +21 -0
  47. package/cjs/liveGuideAdmission.js +94 -0
  48. package/cjs/liveGuideWidget.d.ts +68 -0
  49. package/cjs/liveGuideWidget.js +451 -0
  50. package/cjs/liveSpeechAssets.d.ts +36 -0
  51. package/cjs/liveSpeechAssets.js +39 -0
  52. package/cjs/liveSpeechCaptureBridge.d.ts +32 -0
  53. package/cjs/liveSpeechCaptureBridge.js +140 -0
  54. package/cjs/liveSpeechDetector.d.ts +62 -0
  55. package/cjs/liveSpeechDetector.js +206 -0
  56. package/cjs/liveSpeechModel.d.ts +46 -0
  57. package/cjs/liveSpeechModel.js +105 -0
  58. package/cjs/liveSpeechResampler.d.ts +22 -0
  59. package/cjs/liveSpeechResampler.js +93 -0
  60. package/cjs/liveSpeechWorker.d.ts +56 -0
  61. package/cjs/liveSpeechWorker.js +143 -0
  62. package/cjs/locales/resources.js +568 -8
  63. package/cjs/media.d.ts +11 -0
  64. package/cjs/media.js +78 -9
  65. package/cjs/messages.js +22 -8
  66. package/cjs/pageAssistancePreference.d.ts +15 -3
  67. package/cjs/pageAssistancePreference.js +46 -7
  68. package/cjs/preview.d.ts +4 -1
  69. package/cjs/preview.js +42 -1
  70. package/cjs/recording.d.ts +12 -0
  71. package/cjs/recording.js +150 -0
  72. package/cjs/standalone.d.ts +6 -0
  73. package/cjs/standalone.js +12 -0
  74. package/cjs/types.d.ts +31 -5
  75. package/cjs/types.js +14 -0
  76. package/cjs/ui.d.ts +128 -4
  77. package/cjs/ui.js +1918 -276
  78. package/cjs/version.d.ts +1 -1
  79. package/cjs/version.js +1 -1
  80. package/cjs/voicePresenceUi.d.ts +52 -4
  81. package/cjs/voicePresenceUi.js +659 -57
  82. package/lib/AiLiveAssist.d.ts +76 -11
  83. package/lib/AiLiveAssist.js +1772 -299
  84. package/lib/client.d.ts +19 -0
  85. package/lib/client.js +51 -0
  86. package/lib/context.d.ts +23 -1
  87. package/lib/context.js +713 -126
  88. package/lib/control.d.ts +2 -0
  89. package/lib/control.js +42 -3
  90. package/lib/ctaOffer.d.ts +11 -0
  91. package/lib/ctaOffer.js +179 -0
  92. package/lib/dialogFocusScope.js +1 -1
  93. package/lib/guideAdmission.d.ts +46 -0
  94. package/lib/guideAdmission.js +392 -0
  95. package/lib/guideHost.d.ts +47 -0
  96. package/lib/guideHost.js +42 -0
  97. package/lib/guideInstallation.d.ts +12 -0
  98. package/lib/guideInstallation.js +56 -0
  99. package/lib/guideInstaller.d.ts +4 -0
  100. package/lib/guideInstaller.js +113 -0
  101. package/lib/guideMount.d.ts +22 -0
  102. package/lib/guideMount.js +77 -0
  103. package/lib/guideRecording.d.ts +64 -0
  104. package/lib/guideRecording.js +172 -0
  105. package/lib/guideRecordingSession.d.ts +35 -0
  106. package/lib/guideRecordingSession.js +163 -0
  107. package/lib/guideSubjectStore.d.ts +32 -0
  108. package/lib/guideSubjectStore.js +101 -0
  109. package/lib/guideTypes.d.ts +117 -0
  110. package/lib/guideTypes.js +11 -0
  111. package/lib/index.d.ts +6 -0
  112. package/lib/index.js +21 -10
  113. package/lib/liveAudioProcessor.d.ts +92 -0
  114. package/lib/liveAudioProcessor.js +265 -0
  115. package/lib/liveAudioQueue.d.ts +40 -0
  116. package/lib/liveAudioQueue.js +126 -0
  117. package/lib/liveAudioWorklet.d.ts +1 -0
  118. package/lib/liveAudioWorklet.js +61 -0
  119. package/lib/liveBrowserDetector.d.ts +16 -0
  120. package/lib/liveBrowserDetector.js +71 -0
  121. package/lib/liveBrowserDetectorWorker.d.ts +1 -0
  122. package/lib/liveBrowserDetectorWorker.js +70 -0
  123. package/lib/liveControlChannel.d.ts +176 -0
  124. package/lib/liveControlChannel.js +825 -0
  125. package/lib/liveDuplexMedia.d.ts +204 -0
  126. package/lib/liveDuplexMedia.js +736 -0
  127. package/lib/liveGuideAdmission.d.ts +21 -0
  128. package/lib/liveGuideAdmission.js +90 -0
  129. package/lib/liveGuideWidget.d.ts +68 -0
  130. package/lib/liveGuideWidget.js +447 -0
  131. package/lib/liveSpeechAssets.d.ts +36 -0
  132. package/lib/liveSpeechAssets.js +35 -0
  133. package/lib/liveSpeechCaptureBridge.d.ts +32 -0
  134. package/lib/liveSpeechCaptureBridge.js +136 -0
  135. package/lib/liveSpeechDetector.d.ts +62 -0
  136. package/lib/liveSpeechDetector.js +202 -0
  137. package/lib/liveSpeechModel.d.ts +46 -0
  138. package/lib/liveSpeechModel.js +101 -0
  139. package/lib/liveSpeechResampler.d.ts +22 -0
  140. package/lib/liveSpeechResampler.js +89 -0
  141. package/lib/liveSpeechWorker.d.ts +56 -0
  142. package/lib/liveSpeechWorker.js +137 -0
  143. package/lib/locales/resources.js +568 -8
  144. package/lib/media.d.ts +11 -0
  145. package/lib/media.js +77 -8
  146. package/lib/messages.js +22 -8
  147. package/lib/pageAssistancePreference.d.ts +15 -3
  148. package/lib/pageAssistancePreference.js +46 -7
  149. package/lib/preview.d.ts +4 -1
  150. package/lib/preview.js +42 -1
  151. package/lib/recording.d.ts +12 -0
  152. package/lib/recording.js +146 -0
  153. package/lib/standalone.d.ts +6 -0
  154. package/lib/standalone.js +6 -0
  155. package/lib/types.d.ts +31 -5
  156. package/lib/types.js +14 -0
  157. package/lib/ui.d.ts +128 -4
  158. package/lib/ui.js +1919 -277
  159. package/lib/version.d.ts +1 -1
  160. package/lib/version.js +1 -1
  161. package/lib/voicePresenceUi.d.ts +52 -4
  162. package/lib/voicePresenceUi.js +659 -57
  163. package/package.json +16 -1
@@ -0,0 +1,117 @@
1
+ import type { AiGuideControllerState, AiGuideOpenInput, BootstrapConfig, ConsentScope, LaunchResult, PluginOptions } from "./types.js";
2
+ export type GuideIdentity = Readonly<{
3
+ projectId: number;
4
+ guideId: string;
5
+ installKey: string;
6
+ }>;
7
+ export type GuideBootstrapConfig = BootstrapConfig & GuideIdentity & {
8
+ schemaVersion: 3;
9
+ configRevision: number;
10
+ configChecksum: string;
11
+ };
12
+ export type GuideLaunchIntent = {
13
+ schemaVersion: 3;
14
+ protocolVersion: 2;
15
+ installKey: string;
16
+ launchRequestId: string;
17
+ clientVersion: string;
18
+ configRevision: number;
19
+ path: string;
20
+ tabId: string;
21
+ mode: "text" | "voice";
22
+ experienceMode: "standard";
23
+ consent: {
24
+ disclosureChecksum: string;
25
+ scopes: ConsentScope[];
26
+ locale: string;
27
+ };
28
+ capabilitySummary: {
29
+ ctaOffer: boolean;
30
+ microphone: boolean;
31
+ webrtc: boolean;
32
+ audioPlayback: boolean;
33
+ webAudio: boolean;
34
+ svgOverlay: boolean;
35
+ visualViewport: boolean;
36
+ reducedMotion: boolean;
37
+ lowPower: boolean;
38
+ };
39
+ };
40
+ export type GuideAdmissionResult = {
41
+ schemaVersion: 3;
42
+ protocolVersion: 2;
43
+ projectId: number;
44
+ guideId: string;
45
+ subjectId: string;
46
+ subjectAuthorityRevision: number;
47
+ tabId: string;
48
+ launchRequestId: string;
49
+ configRevision: number;
50
+ configChecksum: string;
51
+ admissionGrant: string;
52
+ expiresAt: string;
53
+ subjectCredentialExpiresAt: string;
54
+ subjectCredential?: string;
55
+ };
56
+ export type GuideLaunchResult = LaunchResult & {
57
+ schemaVersion: 3;
58
+ protocolVersion: 2;
59
+ projectId: number;
60
+ guideId: string;
61
+ subjectId: string;
62
+ launchRequestId: string;
63
+ };
64
+ export type GuideLaunchRecoveryInput = {
65
+ schemaVersion: 3;
66
+ installKey: string;
67
+ subjectCredential: string;
68
+ launchRequestId: string;
69
+ tabId: string;
70
+ action: "resume" | "end";
71
+ };
72
+ export type GuideLaunchRecoveryResult = {
73
+ schemaVersion: 3;
74
+ launchRequestId: string;
75
+ status: "pending" | "terminal";
76
+ } | {
77
+ schemaVersion: 3;
78
+ launchRequestId: string;
79
+ status: "ready";
80
+ launch: GuideLaunchResult;
81
+ };
82
+ export type GuideRecordingLinkInput = {
83
+ schemaVersion: 3;
84
+ installKey: string;
85
+ subjectCredential: string;
86
+ tabId: string;
87
+ replaySessionId: string;
88
+ replayGrant: string;
89
+ };
90
+ export type GuideRecordingLinkResult = {
91
+ schemaVersion: 3;
92
+ linked: true;
93
+ sessionId: string;
94
+ replaySessionId: string;
95
+ };
96
+ /** Public standalone declarations deliberately do not depend on Browser/App. */
97
+ export type AiGuideController = {
98
+ readonly version: string;
99
+ open(input?: AiGuideOpenInput): AiGuideControllerState;
100
+ close(): void;
101
+ getState(): AiGuideControllerState;
102
+ end(): Promise<void>;
103
+ destroy(): void;
104
+ };
105
+ export type StandaloneGuideOptions = Omit<PluginOptions, "siteKey" | "recordReplay"> & {
106
+ installKey: string;
107
+ runtimeUrl: string;
108
+ /** Opt in to this frame's own document and server-approved origin only.
109
+ * Session recording is unavailable in frames; the Guide remains usable.
110
+ * The embedding site must delegate microphone permission for voice.
111
+ */
112
+ allowInFrame?: boolean;
113
+ };
114
+ export declare const GUIDE_UUID_PATTERN: RegExp;
115
+ export declare const GUIDE_INSTALL_KEY_PATTERN: RegExp;
116
+ export declare const GUIDE_SUBJECT_CREDENTIAL_PATTERN: RegExp;
117
+ export declare function guideLaunchRequestId(value: string): string;
@@ -0,0 +1,11 @@
1
+ export const GUIDE_UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
2
+ export const GUIDE_INSTALL_KEY_PATTERN = /^gk_[0-9a-f]{32}$/;
3
+ export const GUIDE_SUBJECT_CREDENTIAL_PATTERN = /^[A-Za-z0-9_-]{32,256}$/;
4
+ export function guideLaunchRequestId(value) {
5
+ // Existing durable launch intents already use a UUID with a `launch_` prefix.
6
+ // Preserve that one-to-one identity instead of generating a second attempt.
7
+ const uuid = value.startsWith("launch_") ? value.slice(7) : value;
8
+ if (!GUIDE_UUID_PATTERN.test(uuid))
9
+ throw new Error("Invalid AI Guide launch request ID.");
10
+ return uuid;
11
+ }
package/lib/index.d.ts CHANGED
@@ -1,9 +1,15 @@
1
1
  import type { App, Options as BrowserOptions } from '@oxvo/browser';
2
2
  import AiLiveAssist from './AiLiveAssist.js';
3
+ import { type GuideHost } from './guideHost.js';
3
4
  import type { PluginOptions } from './types.js';
4
5
  import { VERSION } from './version.js';
5
6
  export type { AiGuideControllerState, AiGuideOpenInput, MessengerCompanionConfig, PluginOptions, } from './types.js';
6
7
  export { AiLiveAssist, VERSION };
8
+ export { LiveControlChannel } from './liveControlChannel.js';
9
+ export { createLegacyBrowserHost } from './guideHost.js';
10
+ export type { GuideHost, GuideIdentity, LegacyBrowserGuideHost, ReplayRecordingSource, StandaloneGuideHost, } from './guideHost.js';
11
+ /** Explicit Guide hosts remain gated until signed schema 3 admission is available. */
12
+ export declare function mountAiGuide(host: GuideHost, options?: PluginOptions): AiLiveAssist | undefined;
7
13
  export default function aiLiveAssist(options?: PluginOptions): (app: App | null) => AiLiveAssist | undefined;
8
14
  export declare function installAiLiveAssist(input: {
9
15
  tracker: {
package/lib/index.js CHANGED
@@ -1,24 +1,35 @@
1
1
  import AiLiveAssist from './AiLiveAssist.js';
2
+ import { createLegacyBrowserHost, guideMountIdentity, } from './guideHost.js';
3
+ import { GuideMountConflictError, mountedGuide } from './guideMount.js';
2
4
  import { VERSION } from './version.js';
3
- const installed = new WeakMap();
4
5
  export { AiLiveAssist, VERSION };
6
+ export { LiveControlChannel } from './liveControlChannel.js';
7
+ export { createLegacyBrowserHost } from './guideHost.js';
8
+ /** Explicit Guide hosts remain gated until signed schema 3 admission is available. */
9
+ export function mountAiGuide(host, options = {}) {
10
+ if (options.enabled === false || window.self !== window.top)
11
+ return undefined;
12
+ try {
13
+ const identity = guideMountIdentity(host, options.siteKey || (host.kind === 'legacy_browser' ? host.siteKey : ''), window.location.origin);
14
+ const existing = mountedGuide(document, identity);
15
+ return existing ?? new AiLiveAssist(host, options);
16
+ }
17
+ catch (error) {
18
+ if (!(error instanceof GuideMountConflictError))
19
+ throw error;
20
+ console.warn(error.message);
21
+ return undefined;
22
+ }
23
+ }
5
24
  export default function aiLiveAssist(options = {}) {
6
25
  return function install(app) {
7
26
  if (!app || options.enabled === false || app.insideIframe)
8
27
  return undefined;
9
- const existing = installed.get(app);
10
- if (existing)
11
- return existing;
12
- if (document.querySelector('[data-oxvo-ai-live-assist-root]')) {
13
- return undefined;
14
- }
15
28
  if (!app.checkRequiredVersion?.('7.4.50')) {
16
29
  console.warn('OXVO AI Guide requires @oxvo/browser version 7.4.50 or newer.');
17
30
  return undefined;
18
31
  }
19
- const instance = new AiLiveAssist(app, options);
20
- installed.set(app, instance);
21
- return instance;
32
+ return mountAiGuide(createLegacyBrowserHost(app, options), options);
22
33
  };
23
34
  }
24
35
  export function installAiLiveAssist(input) {
@@ -0,0 +1,92 @@
1
+ export type LiveAudioProcessorEvent = {
2
+ type: 'ready';
3
+ captureEpoch: number;
4
+ sampleRate: number;
5
+ } | {
6
+ type: 'capture_ready';
7
+ captureEpoch: number;
8
+ sourceStart: number;
9
+ } | {
10
+ type: 'wake_buffering';
11
+ captureEpoch: number;
12
+ operationId: string;
13
+ } | {
14
+ type: 'active';
15
+ captureEpoch: number;
16
+ operationId: string;
17
+ } | {
18
+ type: 'retirement_buffering' | 'retirement_complete';
19
+ captureEpoch: number;
20
+ operationId: string;
21
+ } | {
22
+ type: 'capture';
23
+ captureEpoch: number;
24
+ sequence: number;
25
+ sourceSampleOffset: number;
26
+ pcm: Float32Array;
27
+ } | {
28
+ type: 'heartbeat';
29
+ captureEpoch: number;
30
+ sourceEnd: number;
31
+ bufferedFrames: number;
32
+ active: boolean;
33
+ } | {
34
+ type: 'fault';
35
+ captureEpoch: number;
36
+ reason: 'invalid_audio' | 'capture_gap' | 'detector_backpressure' | 'wake_deadline' | 'wake_capacity' | 'processor_failure';
37
+ expectedFrame?: number;
38
+ actualFrame?: number;
39
+ };
40
+ export type LiveAudioProcessorCommand = {
41
+ type: 'begin_wake' | 'activate' | 'begin_retirement' | 'complete_retirement';
42
+ captureEpoch: number;
43
+ operationId: string;
44
+ } | {
45
+ type: 'detector_ack';
46
+ captureEpoch: number;
47
+ sequence: number;
48
+ } | {
49
+ type: 'close' | 'reset';
50
+ captureEpoch: number;
51
+ };
52
+ /** Worklet-safe engine: no network, timers, speech inference or microphone
53
+ * ownership. Detection gets a bounded copy; original-rate audio remains intact.
54
+ * The outside owner alone may begin/activate/reset from current authority proofs.
55
+ */
56
+ export declare class LiveAudioProcessor {
57
+ readonly sampleRate: number;
58
+ private epoch;
59
+ private readonly emit;
60
+ private queue;
61
+ private phase;
62
+ private deadlineFrame;
63
+ private retiringOperationId;
64
+ private completedRetirementId;
65
+ private history;
66
+ private historyEnd;
67
+ private historySize;
68
+ private expectedFrame;
69
+ private heartbeatFrame;
70
+ private detectorBuffer;
71
+ private detectorSize;
72
+ private detectorStart;
73
+ private detectorSequence;
74
+ private captureEstablished;
75
+ private captureStart;
76
+ private detectorProgressed;
77
+ private detectorInFlight;
78
+ private detectorPending;
79
+ constructor(sampleRate: number, epoch: number, emit: (event: LiveAudioProcessorEvent) => void);
80
+ command(value: unknown, currentFrame: number): boolean;
81
+ /** Exactly one native-rate input/output quantum. A gap cannot become silence. */
82
+ process(input: Float32Array | undefined, output: Float32Array, sourceFrame: number): boolean;
83
+ private captureForDetector;
84
+ /** Keep the most recent 600ms even after it was rendered to the old stream.
85
+ * This fixed ring never grows and is independent of detector scheduling.
86
+ */
87
+ private remember;
88
+ private historySnapshot;
89
+ private sendDetector;
90
+ private fault;
91
+ private erase;
92
+ }
@@ -0,0 +1,265 @@
1
+ import { LiveAudioQueue } from './liveAudioQueue.js';
2
+ /** Worklet-safe engine: no network, timers, speech inference or microphone
3
+ * ownership. Detection gets a bounded copy; original-rate audio remains intact.
4
+ * The outside owner alone may begin/activate/reset from current authority proofs.
5
+ */
6
+ export class LiveAudioProcessor {
7
+ constructor(sampleRate, epoch, emit) {
8
+ this.sampleRate = sampleRate;
9
+ this.epoch = epoch;
10
+ this.emit = emit;
11
+ this.phase = 'local';
12
+ this.deadlineFrame = null;
13
+ this.retiringOperationId = null;
14
+ this.completedRetirementId = null;
15
+ this.historyEnd = 0;
16
+ this.historySize = 0;
17
+ this.expectedFrame = null;
18
+ this.heartbeatFrame = null;
19
+ this.detectorSize = 0;
20
+ this.detectorStart = 0;
21
+ this.detectorSequence = 0;
22
+ this.captureEstablished = false;
23
+ this.captureStart = 0;
24
+ this.detectorProgressed = false;
25
+ this.detectorInFlight = null;
26
+ this.detectorPending = [];
27
+ this.queue = new LiveAudioQueue(sampleRate, epoch);
28
+ this.history = new Float32Array(this.queue.prerollFrames);
29
+ this.detectorBuffer = new Float32Array(Math.ceil(sampleRate * 0.032));
30
+ this.emit({ type: 'ready', captureEpoch: epoch, sampleRate });
31
+ }
32
+ command(value, currentFrame) {
33
+ if (!value || typeof value !== 'object' || Array.isArray(value) || !Number.isSafeInteger(currentFrame) || currentFrame < 0)
34
+ return false;
35
+ const command = value;
36
+ if (!Number.isSafeInteger(command.captureEpoch) || typeof command.captureEpoch !== 'number' || command.captureEpoch < 0)
37
+ return false;
38
+ if (command.type === 'reset') {
39
+ if (command.captureEpoch <= this.epoch)
40
+ return false;
41
+ this.erase();
42
+ this.epoch = command.captureEpoch;
43
+ this.queue = new LiveAudioQueue(this.sampleRate, this.epoch);
44
+ this.phase = 'local';
45
+ this.history = new Float32Array(this.queue.prerollFrames);
46
+ this.detectorBuffer = new Float32Array(Math.ceil(this.sampleRate * 0.032));
47
+ this.emit({ type: 'ready', captureEpoch: this.epoch, sampleRate: this.sampleRate });
48
+ return true;
49
+ }
50
+ if (command.captureEpoch !== this.epoch)
51
+ return false;
52
+ if (command.type === 'close') {
53
+ this.erase();
54
+ return true;
55
+ }
56
+ if (this.phase === 'closed')
57
+ return false;
58
+ if (command.type === 'detector_ack') {
59
+ if (this.detectorInFlight === null || command.sequence !== this.detectorInFlight)
60
+ return false;
61
+ this.detectorInFlight = null;
62
+ if (!this.detectorProgressed) {
63
+ this.detectorProgressed = true;
64
+ this.emit({ type: 'capture_ready', captureEpoch: this.epoch, sourceStart: this.captureStart });
65
+ }
66
+ const next = this.detectorPending.shift();
67
+ if (next)
68
+ this.sendDetector(next);
69
+ return true;
70
+ }
71
+ if (typeof command.operationId !== 'string')
72
+ return false;
73
+ if (command.type === 'begin_retirement') {
74
+ if (this.retiringOperationId !== command.operationId) {
75
+ if (this.retiringOperationId !== null)
76
+ return false;
77
+ const prefix = this.historySnapshot();
78
+ const retired = this.queue.retire(command.operationId, this.epoch, prefix);
79
+ prefix.fill(0);
80
+ if (!retired)
81
+ return false;
82
+ this.retiringOperationId = command.operationId;
83
+ this.phase = 'local';
84
+ this.deadlineFrame = null;
85
+ }
86
+ this.emit({ type: 'retirement_buffering', captureEpoch: this.epoch, operationId: command.operationId });
87
+ return true;
88
+ }
89
+ if (command.type === 'complete_retirement') {
90
+ if (this.retiringOperationId === command.operationId) {
91
+ this.completedRetirementId = command.operationId;
92
+ this.retiringOperationId = null;
93
+ }
94
+ else if (this.retiringOperationId !== null || this.completedRetirementId !== command.operationId)
95
+ return false;
96
+ // The owner sends this only after durable terminal proof, drained playback
97
+ // and closing the old peer. It does not reset capture or a pending wake.
98
+ this.emit({ type: 'retirement_complete', captureEpoch: this.epoch, operationId: command.operationId });
99
+ return true;
100
+ }
101
+ if (command.type === 'begin_wake' && this.detectorProgressed && command.operationId !== this.retiringOperationId &&
102
+ command.operationId !== this.completedRetirementId && this.queue.beginWake(command.operationId, this.epoch)) {
103
+ this.phase = 'waking';
104
+ this.deadlineFrame = currentFrame + this.sampleRate * 6;
105
+ this.emit({ type: 'wake_buffering', captureEpoch: this.epoch, operationId: command.operationId });
106
+ return true;
107
+ }
108
+ if (command.type === 'activate') {
109
+ if (this.deadlineFrame !== null && currentFrame >= this.deadlineFrame) {
110
+ this.fault('wake_deadline');
111
+ return false;
112
+ }
113
+ if (this.retiringOperationId !== null)
114
+ return false;
115
+ if (!this.queue.activate(command.operationId, this.epoch))
116
+ return false;
117
+ this.phase = 'active';
118
+ this.deadlineFrame = null;
119
+ this.emit({ type: 'active', captureEpoch: this.epoch, operationId: command.operationId });
120
+ return true;
121
+ }
122
+ return false;
123
+ }
124
+ /** Exactly one native-rate input/output quantum. A gap cannot become silence. */
125
+ process(input, output, sourceFrame) {
126
+ output.fill(0);
127
+ if (this.phase === 'closed')
128
+ return false;
129
+ try {
130
+ // Device graphs can render before the first capture track has PCM. This is
131
+ // preparation only: emit no heartbeat/detector readiness or paid start.
132
+ if (!input && this.expectedFrame === null && this.phase === 'local')
133
+ return true;
134
+ if (!input || input.length !== output.length || input.length === 0 || input.length > 4096 ||
135
+ !Number.isSafeInteger(sourceFrame) || sourceFrame < 0)
136
+ return this.fault('invalid_audio');
137
+ if (this.expectedFrame !== null && this.expectedFrame !== sourceFrame) {
138
+ if (this.captureEstablished || this.phase !== 'local')
139
+ return this.fault('capture_gap', { expectedFrame: this.expectedFrame, actualFrame: sourceFrame });
140
+ // Chromium can advance its context frame during initial device startup.
141
+ // Before the first complete detector packet, no wake is admissible: erase
142
+ // that partial preparation audio and require a contiguous fresh prefix.
143
+ this.queue.close();
144
+ this.queue = new LiveAudioQueue(this.sampleRate, this.epoch);
145
+ this.history.fill(0);
146
+ this.historySize = 0;
147
+ this.historyEnd = 0;
148
+ this.detectorBuffer.fill(0);
149
+ this.detectorSize = 0;
150
+ this.heartbeatFrame = null;
151
+ }
152
+ this.expectedFrame = sourceFrame + input.length;
153
+ if (this.deadlineFrame !== null && sourceFrame >= this.deadlineFrame)
154
+ return this.fault('wake_deadline');
155
+ if (!this.queue.capture(input, this.epoch))
156
+ return this.fault(this.queue.isClosed ? 'wake_capacity' : 'invalid_audio');
157
+ this.remember(input);
158
+ if (!this.captureForDetector(input, sourceFrame))
159
+ return false;
160
+ this.queue.render(output, this.epoch);
161
+ if (this.heartbeatFrame === null || sourceFrame - this.heartbeatFrame >= this.sampleRate) {
162
+ this.heartbeatFrame = sourceFrame;
163
+ this.emit({ type: 'heartbeat', captureEpoch: this.epoch, sourceEnd: this.expectedFrame,
164
+ bufferedFrames: this.queue.bufferedFrames, active: this.phase === 'active' });
165
+ }
166
+ return true;
167
+ }
168
+ catch {
169
+ output.fill(0);
170
+ return this.fault('processor_failure');
171
+ }
172
+ }
173
+ captureForDetector(input, sourceFrame) {
174
+ let offset = 0;
175
+ while (offset < input.length) {
176
+ if (this.detectorSize === 0)
177
+ this.detectorStart = sourceFrame + offset;
178
+ const count = Math.min(this.detectorBuffer.length - this.detectorSize, input.length - offset);
179
+ this.detectorBuffer.set(input.subarray(offset, offset + count), this.detectorSize);
180
+ offset += count;
181
+ this.detectorSize += count;
182
+ if (this.detectorSize !== this.detectorBuffer.length)
183
+ continue;
184
+ const event = { type: 'capture', captureEpoch: this.epoch,
185
+ sequence: this.detectorSequence++, sourceSampleOffset: this.detectorStart, pcm: this.detectorBuffer };
186
+ this.detectorBuffer = new Float32Array(event.pcm.length);
187
+ this.detectorSize = 0;
188
+ if (this.detectorInFlight === null)
189
+ this.sendDetector(event);
190
+ else if (this.detectorPending.length < 2)
191
+ this.detectorPending.push(event);
192
+ else {
193
+ event.pcm.fill(0);
194
+ return this.fault('detector_backpressure');
195
+ }
196
+ }
197
+ return true;
198
+ }
199
+ /** Keep the most recent 600ms even after it was rendered to the old stream.
200
+ * This fixed ring never grows and is independent of detector scheduling.
201
+ */
202
+ remember(input) {
203
+ const tail = input.subarray(Math.max(0, input.length - this.history.length));
204
+ const first = Math.min(tail.length, this.history.length - this.historyEnd);
205
+ this.history.set(tail.subarray(0, first), this.historyEnd);
206
+ if (first < tail.length)
207
+ this.history.set(tail.subarray(first), 0);
208
+ this.historyEnd = (this.historyEnd + tail.length) % this.history.length;
209
+ this.historySize = Math.min(this.history.length, this.historySize + tail.length);
210
+ }
211
+ historySnapshot() {
212
+ const prefix = new Float32Array(this.historySize);
213
+ const start = (this.historyEnd - this.historySize + this.history.length) % this.history.length;
214
+ const first = Math.min(this.historySize, this.history.length - start);
215
+ prefix.set(this.history.subarray(start, start + first));
216
+ if (first < this.historySize)
217
+ prefix.set(this.history.subarray(0, this.historySize - first), first);
218
+ return prefix;
219
+ }
220
+ sendDetector(event) {
221
+ if (!this.captureEstablished) {
222
+ this.captureEstablished = true;
223
+ this.captureStart = event.sourceSampleOffset;
224
+ }
225
+ // Sending fixes the detector's source continuity, but preparation needs a
226
+ // matching ACK proving its first real inference completed. Keep all native
227
+ // preroll intact while that bounded detector copy is in flight.
228
+ this.detectorInFlight = event.sequence;
229
+ this.emit(event);
230
+ }
231
+ fault(reason, diagnostic) {
232
+ if (this.phase === 'closed')
233
+ return false;
234
+ this.erase();
235
+ try {
236
+ this.emit({ type: 'fault', captureEpoch: this.epoch, reason, ...diagnostic });
237
+ }
238
+ catch { /* Audio remains erased and silent. */ }
239
+ return false;
240
+ }
241
+ erase() {
242
+ this.queue.close();
243
+ this.phase = 'closed';
244
+ this.deadlineFrame = null;
245
+ this.retiringOperationId = null;
246
+ this.completedRetirementId = null;
247
+ this.history.fill(0);
248
+ this.history = new Float32Array(0);
249
+ this.historySize = 0;
250
+ this.historyEnd = 0;
251
+ this.detectorBuffer.fill(0);
252
+ this.detectorBuffer = new Float32Array(0);
253
+ this.detectorSize = 0;
254
+ for (const pending of this.detectorPending)
255
+ pending.pcm.fill(0);
256
+ this.detectorPending = [];
257
+ this.detectorInFlight = null;
258
+ this.detectorSequence = 0;
259
+ this.captureEstablished = false;
260
+ this.captureStart = 0;
261
+ this.detectorProgressed = false;
262
+ this.expectedFrame = null;
263
+ this.heartbeatFrame = null;
264
+ }
265
+ }
@@ -0,0 +1,40 @@
1
+ /**
2
+ * In-memory mono PCM queue for the Live AudioWorklet. It neither opens a
3
+ * microphone nor authorizes a paid connection. The lifecycle owner must confirm
4
+ * provider closure and drained playback before activating a new paid generation.
5
+ * Physical capture may continue locally during retirement in the same epoch.
6
+ *
7
+ * Capture and playout use the SAME sample rate. A render call emits at most one
8
+ * output block: buffered speech is never burst, accelerated or spliced away.
9
+ */
10
+ export declare class LiveAudioQueue {
11
+ readonly sampleRate: number;
12
+ readonly captureEpoch: number;
13
+ readonly capacityFrames: number;
14
+ readonly prerollFrames: number;
15
+ private samples;
16
+ private head;
17
+ private size;
18
+ private phase;
19
+ private operationId;
20
+ constructor(sampleRate: number, captureEpoch: number);
21
+ get bufferedFrames(): number;
22
+ get bufferBytes(): number;
23
+ get isClosed(): boolean;
24
+ /** Input is post-browser echo cancellation, not a VAD-filtered sample stream. */
25
+ capture(input: Float32Array, epoch: number): boolean;
26
+ beginWake(operationId: string, epoch: number): boolean;
27
+ activate(operationId: string, epoch: number): boolean;
28
+ /** Called only after durable retirement commitment. Already-rendered audio is
29
+ * supplied by the processor's bounded history; the old transmission backlog
30
+ * must not become the new generation's prefix.
31
+ */
32
+ retire(operationId: string, epoch: number, prefix: Float32Array): boolean;
33
+ /** Call exactly once per AudioWorklet output quantum, including silence. */
34
+ render(output: Float32Array, epoch: number): number;
35
+ /** End, mute, cancellation, permission loss, timeout and overflow erase audio. */
36
+ close(): void;
37
+ private fail;
38
+ private append;
39
+ private eraseHead;
40
+ }