@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,22 @@
1
+ export type GuideMountReservation = {
2
+ promote(document: Document, identity: string, instance: unknown): () => void;
3
+ };
4
+ export declare class GuideMountConflictError extends Error {
5
+ constructor();
6
+ }
7
+ export declare function mountedGuide<T>(document: Document, identity: string): T | undefined;
8
+ export declare function claimGuideMount(document: Document, identity: string, instance: unknown, pending?: GuideMountReservation): () => void;
9
+ /** Owns one asynchronous installation until canonical bootstrap identity is known. */
10
+ export declare class PendingGuideMount<T> {
11
+ private readonly document;
12
+ readonly promise: Promise<T>;
13
+ private readonly claim;
14
+ private resolvePromise;
15
+ private rejectPromise;
16
+ private promoted;
17
+ constructor(document: Document, identity: string);
18
+ promote(document: Document, identity: string, instance: unknown): () => void;
19
+ resolve(instance: T): void;
20
+ reject(error: unknown): void;
21
+ release(): void;
22
+ }
@@ -0,0 +1,77 @@
1
+ // A document-owned registry also coordinates different copies/versions of the SDK
2
+ // before asynchronous bootstrap has created the visible widget root.
3
+ const MOUNT_KEY = Symbol.for("oxvo.ai-guide.document-mount.v1");
4
+ export class GuideMountConflictError extends Error {
5
+ constructor() {
6
+ super("An AI Guide is already mounted on this page.");
7
+ this.name = "GuideMountConflictError";
8
+ }
9
+ }
10
+ export function mountedGuide(document, identity) {
11
+ const mounted = document[MOUNT_KEY];
12
+ if (!mounted)
13
+ return undefined;
14
+ if (mounted.identity !== identity &&
15
+ mounted.installationIdentity !== identity)
16
+ throw new GuideMountConflictError();
17
+ return mounted.instance;
18
+ }
19
+ export function claimGuideMount(document, identity, instance, pending) {
20
+ if (pending)
21
+ return pending.promote(document, identity, instance);
22
+ const owner = document;
23
+ if (owner[MOUNT_KEY] ||
24
+ document.querySelector("[data-oxvo-ai-live-assist-root]")) {
25
+ throw new GuideMountConflictError();
26
+ }
27
+ const claim = { identity, instance };
28
+ Object.defineProperty(owner, MOUNT_KEY, { value: claim, configurable: true });
29
+ return () => {
30
+ if (owner[MOUNT_KEY] === claim)
31
+ Reflect.deleteProperty(owner, MOUNT_KEY);
32
+ };
33
+ }
34
+ /** Owns one asynchronous installation until canonical bootstrap identity is known. */
35
+ export class PendingGuideMount {
36
+ constructor(document, identity) {
37
+ this.document = document;
38
+ this.promoted = false;
39
+ const owner = document;
40
+ if (owner[MOUNT_KEY] ||
41
+ document.querySelector("[data-oxvo-ai-live-assist-root]"))
42
+ throw new GuideMountConflictError();
43
+ this.promise = new Promise((resolve, reject) => {
44
+ this.resolvePromise = resolve;
45
+ this.rejectPromise = reject;
46
+ });
47
+ this.claim = {
48
+ identity,
49
+ installationIdentity: identity,
50
+ instance: this.promise,
51
+ };
52
+ Object.defineProperty(owner, MOUNT_KEY, {
53
+ value: this.claim,
54
+ configurable: true,
55
+ });
56
+ }
57
+ promote(document, identity, instance) {
58
+ if (document !== this.document ||
59
+ document[MOUNT_KEY] !== this.claim ||
60
+ this.promoted)
61
+ throw new GuideMountConflictError();
62
+ this.promoted = true;
63
+ this.claim.identity = identity;
64
+ this.claim.instance = instance;
65
+ return () => this.release();
66
+ }
67
+ resolve(instance) {
68
+ this.resolvePromise(instance);
69
+ }
70
+ reject(error) {
71
+ this.rejectPromise(error);
72
+ }
73
+ release() {
74
+ if (this.document[MOUNT_KEY] === this.claim)
75
+ Reflect.deleteProperty(this.document, MOUNT_KEY);
76
+ }
77
+ }
@@ -0,0 +1,64 @@
1
+ /** Browser-free structural subset of the real recorder App. No lifecycle ownership. */
2
+ export type GuideRecordingSource = {
3
+ active(): boolean;
4
+ getSessionMeta(): {
5
+ sessionID?: unknown;
6
+ projectID?: unknown;
7
+ } | null;
8
+ getAssistSessionGrant(): {
9
+ sessionId?: unknown;
10
+ token?: unknown;
11
+ } | null;
12
+ session: {
13
+ getTabId(): unknown;
14
+ };
15
+ attachStartCallback(callback: () => void, useSafe?: boolean): void;
16
+ attachStopCallback(callback: () => void, useSafe?: boolean): void;
17
+ };
18
+ /** A real replay grant candidate, not proof of a server-validated Guide recording link. */
19
+ export type GuideRecordingLink = Readonly<{
20
+ sessionId: string;
21
+ token: string;
22
+ tabId: string;
23
+ }>;
24
+ export type GuideRecordingObserver = Readonly<{
25
+ getCurrentLink(): GuideRecordingLink | null;
26
+ /** Immediately supplies the current candidate, then changed lifecycle snapshots. */
27
+ subscribeLinkChange(listener: (link: GuideRecordingLink | null) => void): () => void;
28
+ dispose(): void;
29
+ }>;
30
+ /** Observe only an explicitly supplied recorder; never discover, start or stop one. */
31
+ export declare function observeGuideRecording(source: GuideRecordingSource, input: {
32
+ projectId: number;
33
+ }): GuideRecordingObserver;
34
+ /** Structural match for Browser's actual networkCapture.sanitizer data API. */
35
+ export type GuideRecordingNetworkData = {
36
+ readonly status: number;
37
+ readonly method: string;
38
+ url: string;
39
+ request: {
40
+ body: string | null;
41
+ headers: Record<string, string>;
42
+ };
43
+ response: {
44
+ body: unknown;
45
+ headers: Record<string, string>;
46
+ };
47
+ };
48
+ export type GuideRecordingNetworkSanitizer = (data: GuideRecordingNetworkData) => GuideRecordingNetworkData | null;
49
+ type NetworkCaptureOptions = {
50
+ sanitizer?: GuideRecordingNetworkSanitizer;
51
+ };
52
+ /**
53
+ * Supply this result as networkCapture BEFORE constructing a Guide-owned Browser.
54
+ * Browser captures the sanitizer in its constructor. This cannot retrofit an
55
+ * external recorder, and it does not initialize or enable recording/analytics.
56
+ */
57
+ export declare function createGuideRecordingNetworkCapture<T extends object = NetworkCaptureOptions>(input: {
58
+ runtimeUrl: string;
59
+ pageUrl: string;
60
+ networkCapture?: T & NetworkCaptureOptions;
61
+ }): T & {
62
+ sanitizer: GuideRecordingNetworkSanitizer;
63
+ };
64
+ export {};
@@ -0,0 +1,172 @@
1
+ const recordingHubs = new WeakMap();
2
+ const MAX_REPLAY_SESSION_ID = 9223372036854775807n;
3
+ function replaySessionId(value) {
4
+ const text = typeof value === "string"
5
+ ? value
6
+ : typeof value === "number" && Number.isSafeInteger(value)
7
+ ? String(value)
8
+ : "";
9
+ return /^[1-9][0-9]{0,18}$/.test(text) &&
10
+ BigInt(text) <= MAX_REPLAY_SESSION_ID
11
+ ? text
12
+ : null;
13
+ }
14
+ function currentLink(source, projectId) {
15
+ try {
16
+ if (!source.active())
17
+ return null;
18
+ const metadata = source.getSessionMeta();
19
+ if (metadata?.projectID !== projectId &&
20
+ metadata?.projectID !== String(projectId)) {
21
+ return null;
22
+ }
23
+ const sessionId = replaySessionId(metadata.sessionID);
24
+ if (!sessionId)
25
+ return null;
26
+ const grant = source.getAssistSessionGrant();
27
+ if (replaySessionId(grant?.sessionId) !== sessionId ||
28
+ typeof grant?.token !== "string" ||
29
+ grant.token.length < 1 ||
30
+ grant.token.length > 4096 ||
31
+ /\s/.test(grant.token)) {
32
+ return null;
33
+ }
34
+ const tabId = source.session.getTabId();
35
+ if (typeof tabId !== "string" || !/^[A-Za-z0-9_.:-]{8,128}$/.test(tabId)) {
36
+ return null;
37
+ }
38
+ return Object.freeze({ sessionId, token: grant.token, tabId });
39
+ }
40
+ catch {
41
+ // Recorder failures must not produce a fabricated link or disclose a grant.
42
+ return null;
43
+ }
44
+ }
45
+ function recordingHub(source) {
46
+ const existing = recordingHubs.get(source);
47
+ if (existing)
48
+ return existing;
49
+ const hub = { listeners: new Set() };
50
+ let refreshPending = false;
51
+ const scheduleRefresh = () => {
52
+ if (refreshPending || hub.listeners.size === 0)
53
+ return;
54
+ refreshPending = true;
55
+ // App invokes callbacks before flipping its activity state. Its API has no
56
+ // detach operation, so share one inert-when-unused callback pair per source.
57
+ void Promise.resolve().then(() => {
58
+ refreshPending = false;
59
+ for (const refresh of [...hub.listeners]) {
60
+ if (hub.listeners.has(refresh))
61
+ refresh();
62
+ }
63
+ });
64
+ };
65
+ source.attachStartCallback(scheduleRefresh);
66
+ source.attachStopCallback(scheduleRefresh);
67
+ recordingHubs.set(source, hub);
68
+ return hub;
69
+ }
70
+ function sameLink(left, right) {
71
+ return (left?.sessionId === right?.sessionId &&
72
+ left?.token === right?.token &&
73
+ left?.tabId === right?.tabId);
74
+ }
75
+ /** Observe only an explicitly supplied recorder; never discover, start or stop one. */
76
+ export function observeGuideRecording(source, input) {
77
+ const { projectId } = input;
78
+ if (!Number.isSafeInteger(projectId) || projectId <= 0) {
79
+ throw new Error("Invalid AI Guide recording workspace.");
80
+ }
81
+ let disposed = false;
82
+ const subscriptions = new Set();
83
+ const getCurrentLink = () => disposed ? null : currentLink(source, projectId);
84
+ return Object.freeze({
85
+ getCurrentLink,
86
+ subscribeLinkChange(listener) {
87
+ if (disposed)
88
+ return () => undefined;
89
+ const hub = recordingHub(source);
90
+ let previous = getCurrentLink();
91
+ const notify = (link) => {
92
+ try {
93
+ listener(link);
94
+ }
95
+ catch {
96
+ // Do not leak credentials through recorder debug/error callbacks.
97
+ }
98
+ };
99
+ const refresh = () => {
100
+ const next = getCurrentLink();
101
+ if (sameLink(previous, next))
102
+ return;
103
+ previous = next;
104
+ notify(next);
105
+ };
106
+ const unsubscribe = () => {
107
+ hub.listeners.delete(refresh);
108
+ subscriptions.delete(unsubscribe);
109
+ previous = null;
110
+ };
111
+ subscriptions.add(unsubscribe);
112
+ hub.listeners.add(refresh);
113
+ notify(previous);
114
+ return unsubscribe;
115
+ },
116
+ dispose() {
117
+ disposed = true;
118
+ for (const unsubscribe of subscriptions)
119
+ unsubscribe();
120
+ },
121
+ });
122
+ }
123
+ /**
124
+ * Supply this result as networkCapture BEFORE constructing a Guide-owned Browser.
125
+ * Browser captures the sanitizer in its constructor. This cannot retrofit an
126
+ * external recorder, and it does not initialize or enable recording/analytics.
127
+ */
128
+ export function createGuideRecordingNetworkCapture(input) {
129
+ let page;
130
+ let runtime;
131
+ try {
132
+ page = new URL(input.pageUrl);
133
+ runtime = new URL(input.runtimeUrl, page);
134
+ if (!["http:", "https:"].includes(page.protocol) ||
135
+ !["http:", "https:"].includes(runtime.protocol) ||
136
+ runtime.username ||
137
+ runtime.password ||
138
+ runtime.search ||
139
+ runtime.hash) {
140
+ throw new Error();
141
+ }
142
+ }
143
+ catch {
144
+ throw new Error("Invalid AI Guide recording privacy configuration.");
145
+ }
146
+ const excluded = (value) => {
147
+ try {
148
+ const url = new URL(value, page);
149
+ const path = decodeURIComponent(url.pathname);
150
+ // The namespace guard also protects runtime-returned URLs on another
151
+ // allowed host. Do not rely on recorder ingest-origin filtering.
152
+ return (!["http:", "https:"].includes(url.protocol) ||
153
+ /(?:^|\/)ai-live-assist(?:\/|$)/i.test(path));
154
+ }
155
+ catch {
156
+ return true;
157
+ }
158
+ };
159
+ const existing = input.networkCapture?.sanitizer;
160
+ const sanitizer = (data) => {
161
+ if (excluded(data.url))
162
+ return null;
163
+ try {
164
+ const sanitized = existing ? existing(data) : data;
165
+ return sanitized && !excluded(sanitized.url) ? sanitized : null;
166
+ }
167
+ catch {
168
+ return null;
169
+ }
170
+ };
171
+ return { ...input.networkCapture, sanitizer };
172
+ }
@@ -0,0 +1,35 @@
1
+ import type { RuntimeClient } from "./client.js";
2
+ import type { GuideIdentity } from "./guideTypes.js";
3
+ import type { GuideSubjectStore } from "./guideSubjectStore.js";
4
+ import type { GuideRecordingObserver } from "./guideRecording.js";
5
+ export type GuideRecordingStatus = "off" | "starting" | "recording" | "linked" | "unavailable";
6
+ export type GuideRecordingAdapter = {
7
+ start(): Promise<GuideRecordingObserver>;
8
+ stop(): void;
9
+ dispose(): void;
10
+ report(status: GuideRecordingStatus): void;
11
+ };
12
+ export type GuideRecordingFactory = (identity: GuideIdentity, runtimeUrl: string) => GuideRecordingAdapter;
13
+ /** Optional recording has its own lifecycle and never supplies Guide admission. */
14
+ export declare class GuideRecordingSession {
15
+ private readonly identity;
16
+ private readonly store;
17
+ private readonly runtime;
18
+ private readonly adapter;
19
+ private observer;
20
+ private unsubscribe;
21
+ private starting;
22
+ private consented;
23
+ private destroyed;
24
+ private session;
25
+ private linkController;
26
+ private lastAttempt;
27
+ private generation;
28
+ constructor(identity: GuideIdentity, store: GuideSubjectStore, runtime: RuntimeClient, adapter: GuideRecordingAdapter);
29
+ applyConsent(consented: boolean): void;
30
+ bindSession(sessionId: string, tabId: string): void;
31
+ endSession(): void;
32
+ destroy(): void;
33
+ private start;
34
+ private link;
35
+ }
@@ -0,0 +1,163 @@
1
+ /** Optional recording has its own lifecycle and never supplies Guide admission. */
2
+ export class GuideRecordingSession {
3
+ constructor(identity, store, runtime, adapter) {
4
+ this.identity = identity;
5
+ this.store = store;
6
+ this.runtime = runtime;
7
+ this.adapter = adapter;
8
+ this.observer = null;
9
+ this.unsubscribe = null;
10
+ this.starting = null;
11
+ this.consented = false;
12
+ this.destroyed = false;
13
+ this.session = null;
14
+ this.linkController = null;
15
+ this.lastAttempt = null;
16
+ this.generation = 0;
17
+ }
18
+ applyConsent(consented) {
19
+ if (this.destroyed || consented === this.consented)
20
+ return;
21
+ this.consented = consented;
22
+ this.generation += 1;
23
+ if (!consented) {
24
+ this.linkController?.abort();
25
+ this.linkController = null;
26
+ this.lastAttempt = null;
27
+ this.unsubscribe?.();
28
+ this.unsubscribe = null;
29
+ this.observer?.dispose();
30
+ this.observer = null;
31
+ this.adapter.stop();
32
+ this.adapter.report("off");
33
+ return;
34
+ }
35
+ this.start();
36
+ }
37
+ bindSession(sessionId, tabId) {
38
+ if (this.destroyed)
39
+ return;
40
+ if (this.session?.sessionId !== sessionId ||
41
+ this.session?.tabId !== tabId) {
42
+ this.linkController?.abort();
43
+ this.linkController = null;
44
+ this.lastAttempt = null;
45
+ }
46
+ this.session = { sessionId, tabId };
47
+ this.link(this.observer?.getCurrentLink() ?? null);
48
+ }
49
+ endSession() {
50
+ this.session = null;
51
+ this.lastAttempt = null;
52
+ this.linkController?.abort();
53
+ this.linkController = null;
54
+ // AI End is not revocation of independent browsing-recording consent.
55
+ }
56
+ destroy() {
57
+ this.destroyed = true;
58
+ this.generation += 1;
59
+ this.endSession();
60
+ this.unsubscribe?.();
61
+ this.unsubscribe = null;
62
+ this.observer?.dispose();
63
+ this.observer = null;
64
+ this.adapter.dispose();
65
+ }
66
+ start() {
67
+ if (this.starting || !this.consented || this.destroyed)
68
+ return;
69
+ const generation = this.generation;
70
+ this.adapter.report("starting");
71
+ const starting = this.adapter
72
+ .start()
73
+ .then((observer) => {
74
+ if (this.destroyed ||
75
+ !this.consented ||
76
+ generation !== this.generation) {
77
+ observer.dispose();
78
+ this.adapter.stop();
79
+ return;
80
+ }
81
+ this.observer = observer;
82
+ this.adapter.report("recording");
83
+ this.unsubscribe = observer.subscribeLinkChange((link) => this.link(link));
84
+ })
85
+ .catch(() => {
86
+ if (!this.destroyed && this.consented && generation === this.generation)
87
+ this.adapter.report("unavailable");
88
+ })
89
+ .finally(() => {
90
+ if (this.starting === starting)
91
+ this.starting = null;
92
+ // A new explicit grant can arrive while a revoked startup settles.
93
+ // Retry only that newer choice, never an unchanged failed start.
94
+ if (generation !== this.generation && this.consented && !this.destroyed)
95
+ this.start();
96
+ });
97
+ this.starting = starting;
98
+ }
99
+ link(link) {
100
+ const session = this.session;
101
+ if (!link || !session || !this.consented || this.destroyed)
102
+ return;
103
+ const previous = this.lastAttempt;
104
+ if (previous?.sessionId === session.sessionId &&
105
+ previous.replaySessionId === link.sessionId &&
106
+ previous.grant === link.token)
107
+ return;
108
+ let credential;
109
+ try {
110
+ credential = this.store.readSubject()?.credential;
111
+ }
112
+ catch {
113
+ /* fail closed below */
114
+ }
115
+ if (!credential) {
116
+ this.adapter.report("unavailable");
117
+ return;
118
+ }
119
+ this.linkController?.abort();
120
+ const controller = new AbortController();
121
+ this.linkController = controller;
122
+ this.lastAttempt = {
123
+ sessionId: session.sessionId,
124
+ replaySessionId: link.sessionId,
125
+ grant: link.token,
126
+ };
127
+ const timeout = setTimeout(() => controller.abort(), 10000);
128
+ void this.runtime
129
+ .linkGuideRecording(session.sessionId, {
130
+ schemaVersion: 3,
131
+ installKey: this.identity.installKey,
132
+ subjectCredential: credential,
133
+ tabId: session.tabId,
134
+ replaySessionId: link.sessionId,
135
+ replayGrant: link.token,
136
+ }, controller.signal)
137
+ .then((result) => {
138
+ if (controller.signal.aborted ||
139
+ this.linkController !== controller ||
140
+ !this.consented ||
141
+ this.destroyed)
142
+ return;
143
+ if (!result ||
144
+ result.schemaVersion !== 3 ||
145
+ result.linked !== true ||
146
+ result.sessionId !== session.sessionId ||
147
+ result.replaySessionId !== link.sessionId)
148
+ throw new Error("Invalid recording link.");
149
+ this.adapter.report("linked");
150
+ })
151
+ .catch(() => {
152
+ if (!this.destroyed &&
153
+ this.consented &&
154
+ this.linkController === controller)
155
+ this.adapter.report("unavailable");
156
+ })
157
+ .finally(() => {
158
+ clearTimeout(timeout);
159
+ if (this.linkController === controller)
160
+ this.linkController = null;
161
+ });
162
+ }
163
+ }
@@ -0,0 +1,32 @@
1
+ import { type GuideIdentity, type GuideLaunchIntent } from "./guideTypes.js";
2
+ export type GuideSubjectRecord = {
3
+ v: 1;
4
+ origin: string;
5
+ projectId: number;
6
+ guideId: string;
7
+ subjectId: string;
8
+ subjectAuthorityRevision: number;
9
+ credential: string;
10
+ expiresAt: string;
11
+ };
12
+ export type GuideLaunchRecord = {
13
+ v: 1;
14
+ subjectId: string;
15
+ intent: GuideLaunchIntent;
16
+ recordingConsent?: boolean;
17
+ };
18
+ export declare class GuideSubjectStore {
19
+ readonly identity: GuideIdentity;
20
+ readonly origin: string;
21
+ private readonly storage;
22
+ private readonly subjectKey;
23
+ private readonly launchKey;
24
+ constructor(identity: GuideIdentity, origin: string, namespace: string, storage?: Storage);
25
+ readSubject(): GuideSubjectRecord | null;
26
+ saveSubject(subject: GuideSubjectRecord): void;
27
+ readLaunch(): GuideLaunchRecord | null;
28
+ saveLaunch(record: GuideLaunchRecord): void;
29
+ clearLaunch(launchRequestId: string): void;
30
+ private read;
31
+ private write;
32
+ }
@@ -0,0 +1,101 @@
1
+ import { GUIDE_SUBJECT_CREDENTIAL_PATTERN, GUIDE_UUID_PATTERN, } from "./guideTypes.js";
2
+ export class GuideSubjectStore {
3
+ constructor(identity, origin, namespace, storage = localStorage) {
4
+ this.identity = identity;
5
+ this.origin = origin;
6
+ this.storage = storage;
7
+ this.subjectKey = `__oxvo_ai_guide_subject_v1_${namespace}`;
8
+ this.launchKey = `__oxvo_ai_guide_launch_v1_${namespace}`;
9
+ }
10
+ readSubject() {
11
+ const raw = this.read(this.subjectKey);
12
+ if (!raw)
13
+ return null;
14
+ const subject = raw;
15
+ if (subject.v !== 1 ||
16
+ subject.origin !== this.origin ||
17
+ subject.projectId !== this.identity.projectId ||
18
+ subject.guideId !== this.identity.guideId ||
19
+ typeof subject.subjectId !== "string" ||
20
+ !GUIDE_UUID_PATTERN.test(subject.subjectId) ||
21
+ !Number.isSafeInteger(subject.subjectAuthorityRevision) ||
22
+ (subject.subjectAuthorityRevision ?? -1) < 0 ||
23
+ typeof subject.credential !== "string" ||
24
+ !GUIDE_SUBJECT_CREDENTIAL_PATTERN.test(subject.credential) ||
25
+ typeof subject.expiresAt !== "string" ||
26
+ !Number.isFinite(Date.parse(subject.expiresAt)))
27
+ throw new Error("The saved AI Guide identity is invalid.");
28
+ // Preserve expired identities; never downgrade rejected/expired possession to
29
+ // an anonymous replacement while recovering a possibly paid operation.
30
+ return subject;
31
+ }
32
+ saveSubject(subject) {
33
+ this.write(this.subjectKey, subject);
34
+ const saved = this.readSubject();
35
+ if (!saved || JSON.stringify(saved) !== JSON.stringify(subject)) {
36
+ throw new Error("AI Guide could not securely save this session identity.");
37
+ }
38
+ }
39
+ readLaunch() {
40
+ const raw = this.read(this.launchKey);
41
+ if (!raw)
42
+ return null;
43
+ const record = raw;
44
+ const intent = record.intent;
45
+ if (record.v !== 1 ||
46
+ typeof record.subjectId !== "string" ||
47
+ !GUIDE_UUID_PATTERN.test(record.subjectId) ||
48
+ !intent ||
49
+ intent.schemaVersion !== 3 ||
50
+ intent.protocolVersion !== 2 ||
51
+ intent.installKey !== this.identity.installKey ||
52
+ !GUIDE_UUID_PATTERN.test(intent.launchRequestId) ||
53
+ typeof intent.tabId !== "string" ||
54
+ !/^[A-Za-z0-9_.:-]{1,128}$/.test(intent.tabId) ||
55
+ typeof intent.path !== "string" ||
56
+ intent.path.length > 4096 ||
57
+ (intent.mode !== "text" && intent.mode !== "voice") ||
58
+ intent.experienceMode !== "standard" ||
59
+ !intent.consent ||
60
+ !intent.capabilitySummary ||
61
+ (record.recordingConsent !== undefined &&
62
+ typeof record.recordingConsent !== "boolean"))
63
+ throw new Error("The saved AI Guide launch is invalid.");
64
+ return record;
65
+ }
66
+ saveLaunch(record) {
67
+ const previous = this.readLaunch();
68
+ if (previous &&
69
+ previous.intent.launchRequestId !== record.intent.launchRequestId) {
70
+ throw new Error("The previous AI Guide session must finish first.");
71
+ }
72
+ this.write(this.launchKey, record);
73
+ if (JSON.stringify(this.readLaunch()) !== JSON.stringify(record)) {
74
+ throw new Error("AI Guide could not securely save this launch.");
75
+ }
76
+ }
77
+ clearLaunch(launchRequestId) {
78
+ const record = this.readLaunch();
79
+ if (record?.intent.launchRequestId === launchRequestId)
80
+ this.storage.removeItem(this.launchKey);
81
+ }
82
+ read(key) {
83
+ const value = this.storage.getItem(key);
84
+ if (value === null)
85
+ return null;
86
+ if (value.length > 16384)
87
+ throw new Error("The saved AI Guide state is invalid.");
88
+ try {
89
+ const parsed = JSON.parse(value);
90
+ if (!parsed || typeof parsed !== "object" || Array.isArray(parsed))
91
+ throw new Error();
92
+ return parsed;
93
+ }
94
+ catch {
95
+ throw new Error("The saved AI Guide state is invalid.");
96
+ }
97
+ }
98
+ write(key, value) {
99
+ this.storage.setItem(key, JSON.stringify(value));
100
+ }
101
+ }