@oxvo/ai-live-assist 7.5.19 → 7.5.21

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 (99) hide show
  1. package/cjs/AiLiveAssist.d.ts +8 -1
  2. package/cjs/AiLiveAssist.js +113 -8
  3. package/cjs/client.d.ts +5 -3
  4. package/cjs/client.js +44 -5
  5. package/cjs/commerce/cartContracts.d.ts +158 -0
  6. package/cjs/commerce/cartContracts.js +228 -0
  7. package/cjs/commerce/cartSession.d.ts +86 -0
  8. package/cjs/commerce/cartSession.js +475 -0
  9. package/cjs/commerce/contracts.d.ts +152 -0
  10. package/cjs/commerce/contracts.js +322 -0
  11. package/cjs/commerce/pageContext.d.ts +50 -0
  12. package/cjs/commerce/pageContext.js +173 -0
  13. package/cjs/commerce/productPanel.d.ts +65 -0
  14. package/cjs/commerce/productPanel.js +662 -0
  15. package/cjs/commerce/realtimeCommerce.d.ts +73 -0
  16. package/cjs/commerce/realtimeCommerce.js +342 -0
  17. package/cjs/commerce/shopifyCart.d.ts +130 -0
  18. package/cjs/commerce/shopifyCart.js +1135 -0
  19. package/cjs/commerce/shopifyCartPresentation.d.ts +12 -0
  20. package/cjs/commerce/shopifyCartPresentation.js +379 -0
  21. package/cjs/commerce/shoppingNotes.d.ts +61 -0
  22. package/cjs/commerce/shoppingNotes.js +313 -0
  23. package/cjs/commerce/shoppingNotesView.d.ts +46 -0
  24. package/cjs/commerce/shoppingNotesView.js +269 -0
  25. package/cjs/control.d.ts +9 -1
  26. package/cjs/control.js +51 -1
  27. package/cjs/guideAdmission.d.ts +5 -2
  28. package/cjs/guideAdmission.js +21 -2
  29. package/cjs/guideInstaller.js +1 -1
  30. package/cjs/guideMeasurement.d.ts +5 -0
  31. package/cjs/guideMeasurement.js +29 -0
  32. package/cjs/guideSubjectStore.js +2 -0
  33. package/cjs/guideTypes.d.ts +18 -0
  34. package/cjs/liveControlChannel.d.ts +43 -1
  35. package/cjs/liveControlChannel.js +166 -14
  36. package/cjs/liveGuideAdmission.d.ts +3 -2
  37. package/cjs/liveGuideAdmission.js +10 -2
  38. package/cjs/liveGuideWidget.d.ts +18 -0
  39. package/cjs/liveGuideWidget.js +228 -23
  40. package/cjs/locales/resources.js +2240 -0
  41. package/cjs/messages.js +56 -0
  42. package/cjs/tabLock.d.ts +2 -0
  43. package/cjs/tabLock.js +11 -1
  44. package/cjs/types.d.ts +6 -2
  45. package/cjs/types.js +56 -0
  46. package/cjs/ui.d.ts +37 -0
  47. package/cjs/ui.js +261 -9
  48. package/cjs/version.d.ts +1 -1
  49. package/cjs/version.js +1 -1
  50. package/lib/AiLiveAssist.d.ts +8 -1
  51. package/lib/AiLiveAssist.js +113 -8
  52. package/lib/client.d.ts +5 -3
  53. package/lib/client.js +44 -5
  54. package/lib/commerce/cartContracts.d.ts +158 -0
  55. package/lib/commerce/cartContracts.js +221 -0
  56. package/lib/commerce/cartSession.d.ts +86 -0
  57. package/lib/commerce/cartSession.js +471 -0
  58. package/lib/commerce/contracts.d.ts +152 -0
  59. package/lib/commerce/contracts.js +300 -0
  60. package/lib/commerce/pageContext.d.ts +50 -0
  61. package/lib/commerce/pageContext.js +168 -0
  62. package/lib/commerce/productPanel.d.ts +65 -0
  63. package/lib/commerce/productPanel.js +657 -0
  64. package/lib/commerce/realtimeCommerce.d.ts +73 -0
  65. package/lib/commerce/realtimeCommerce.js +337 -0
  66. package/lib/commerce/shopifyCart.d.ts +130 -0
  67. package/lib/commerce/shopifyCart.js +1130 -0
  68. package/lib/commerce/shopifyCartPresentation.d.ts +12 -0
  69. package/lib/commerce/shopifyCartPresentation.js +376 -0
  70. package/lib/commerce/shoppingNotes.d.ts +61 -0
  71. package/lib/commerce/shoppingNotes.js +307 -0
  72. package/lib/commerce/shoppingNotesView.d.ts +46 -0
  73. package/lib/commerce/shoppingNotesView.js +265 -0
  74. package/lib/control.d.ts +9 -1
  75. package/lib/control.js +51 -1
  76. package/lib/guideAdmission.d.ts +5 -2
  77. package/lib/guideAdmission.js +21 -2
  78. package/lib/guideInstaller.js +1 -1
  79. package/lib/guideMeasurement.d.ts +5 -0
  80. package/lib/guideMeasurement.js +24 -0
  81. package/lib/guideSubjectStore.js +2 -0
  82. package/lib/guideTypes.d.ts +18 -0
  83. package/lib/liveControlChannel.d.ts +43 -1
  84. package/lib/liveControlChannel.js +166 -14
  85. package/lib/liveGuideAdmission.d.ts +3 -2
  86. package/lib/liveGuideAdmission.js +10 -2
  87. package/lib/liveGuideWidget.d.ts +18 -0
  88. package/lib/liveGuideWidget.js +228 -23
  89. package/lib/locales/resources.js +2240 -0
  90. package/lib/messages.js +56 -0
  91. package/lib/tabLock.d.ts +2 -0
  92. package/lib/tabLock.js +11 -1
  93. package/lib/types.d.ts +6 -2
  94. package/lib/types.js +56 -0
  95. package/lib/ui.d.ts +37 -0
  96. package/lib/ui.js +261 -9
  97. package/lib/version.d.ts +1 -1
  98. package/lib/version.js +1 -1
  99. package/package.json +1 -1
@@ -2,6 +2,9 @@ import { WidgetView } from './ui.js';
2
2
  import { LiveGuideAdmission } from './liveGuideAdmission.js';
3
3
  import { LiveControlChannel, LIVE_CONTROL_CLIENT_VERSION, LIVE_CONTROL_CLIENT_CAPABILITIES } from './liveControlChannel.js';
4
4
  import { createLiveBrowserDetector, validateLiveBrowserAssets } from './liveBrowserDetector.js';
5
+ import { CommercePageContext } from './commerce/pageContext.js';
6
+ import { CartSession } from './commerce/cartSession.js';
7
+ import { parseCommerceBootstrap, parseLiveCommerceCatalog, isCommerceOptions } from './commerce/contracts.js';
5
8
  const transcriptGroupLimit = 16000;
6
9
  /** Actual widget protocol branch. Reuses WidgetView, visitor possession storage,
7
10
  * local detector/media and authenticated runtime; never constructs a Realtime
@@ -15,6 +18,9 @@ export class LiveGuideWidget {
15
18
  this.destroyed = false;
16
19
  this.active = false;
17
20
  this.starting = false;
21
+ this.ownsLock = false;
22
+ this.ownedLaunchId = null;
23
+ this.activeElsewhere = false;
18
24
  this.phase = 'ready';
19
25
  this.mode = 'text';
20
26
  this.captureEpoch = 0;
@@ -34,7 +40,20 @@ export class LiveGuideWidget {
34
40
  this.pendingQuestions = [];
35
41
  this.requestIds = new Set();
36
42
  this.livePhase = 'voice_off';
43
+ this.commerceCatalog = null;
37
44
  this.pagehide = () => { void this.end().catch(() => undefined); };
45
+ this.refreshOwnership = () => {
46
+ if (this.destroyed || !this.activeElsewhere || this.active || this.starting || this.ending || !this.acquireLock())
47
+ return;
48
+ // A crashed owner's lock can expire without a storage event. An explicit
49
+ // open/focus may recover its saved intent, but never launches a new session.
50
+ if (this.admission.hasPendingLaunch()) {
51
+ this.ownedLaunchId = this.admission.pendingLaunchId();
52
+ void this.end().catch(() => undefined);
53
+ }
54
+ else
55
+ this.releaseLock();
56
+ };
38
57
  const config = options.bootstrap;
39
58
  if (config.providerProtocol !== 'live' || config.providerModel !== 'gpt-live-1' || !config.liveAssets ||
40
59
  JSON.stringify(config.liveCapabilities) !== JSON.stringify(LIVE_CONTROL_CLIENT_CAPABILITIES))
@@ -79,14 +98,40 @@ export class LiveGuideWidget {
79
98
  this.view = new WidgetView(config, options.messages ?? {}, callbacks, {
80
99
  ...options.viewOptions, preserveRawText: true, manualTurnsSupported: false,
81
100
  });
101
+ this.commerce = parseCommerceBootstrap(config.commerce, config.guideId, location.origin);
102
+ this.commerceContext = this.commerce ? new CommercePageContext(() => this.scopes.includes('page_context') && config.capabilities?.pageContext === true, context => this.onCommerceContext(context)) : null;
103
+ this.commerceCart = this.commerce?.cartEnabled && config.capabilities?.commerceCart === true ? new CartSession({
104
+ bootstrap: this.commerce, view: () => this.destroyed ? null : this.view,
105
+ current: () => this.currentCartSnapshot(),
106
+ transportSessionId: () => this.channel?.sessionId ?? null,
107
+ selection: (productRef, variantRef) => {
108
+ this.commerceContext?.refresh();
109
+ const selected = this.channel?.currentCartSelection(productRef, variantRef);
110
+ const visible = this.commerceCatalog;
111
+ return selected && visible && selected.displayReceipt.resultSetId === visible.catalog.resultSetId &&
112
+ selected.displayReceipt.taskId === visible.taskId && selected.displayReceipt.inputEpoch === visible.inputEpoch
113
+ ? selected : null;
114
+ },
115
+ send: (type, payload) => {
116
+ if (!this.channel)
117
+ throw new Error('The cart connection is unavailable.');
118
+ return this.channel.sendCommerceCart(type, payload);
119
+ },
120
+ }) : null;
121
+ this.commerceContext?.start();
82
122
  if (this.admission.hasPendingLaunch()) {
83
- this.phase = 'ending';
84
- this.view.showEndingDetached();
85
- void this.end().catch(() => undefined);
123
+ if (this.acquireLock()) {
124
+ this.ownedLaunchId = this.admission.pendingLaunchId();
125
+ void this.end().catch(() => undefined);
126
+ }
86
127
  }
87
- window.addEventListener('pagehide', this.pagehide);
128
+ // Capture runs before TabActivityLock releases cross-tab ownership.
129
+ window.addEventListener('pagehide', this.pagehide, true);
130
+ window.addEventListener('focus', this.refreshOwnership);
131
+ window.addEventListener('pageshow', this.refreshOwnership);
88
132
  }
89
133
  open(input = {}) {
134
+ this.refreshOwnership();
90
135
  if (input.requestId && this.requestIds.has(input.requestId))
91
136
  return this.getState();
92
137
  if (input.question && !this.active && this.pendingQuestions.length >= 8)
@@ -112,10 +157,14 @@ export class LiveGuideWidget {
112
157
  companion: false, pendingQuestion: this.pendingQuestions.length > 0 };
113
158
  }
114
159
  start(mode, scopes, question) {
115
- if (this.destroyed || this.starting || this.active || this.ending || this.admission.hasPendingLaunch() || !this.options.acquire())
160
+ if (this.destroyed || this.starting || this.active || this.ending ||
161
+ (question && (!question.trim() || question.length > 4000)) || !this.acquireLock())
116
162
  return false;
117
- if (question && (!question.trim() || question.length > 4000)) {
118
- this.options.release();
163
+ // Recover abandoned intent only after acquiring the actual activity lock;
164
+ // tabId equality cannot distinguish a reload from a still-live document.
165
+ if (this.admission.hasPendingLaunch()) {
166
+ this.ownedLaunchId = this.admission.pendingLaunchId();
167
+ void this.end().catch(() => undefined);
119
168
  return false;
120
169
  }
121
170
  this.mode = mode;
@@ -133,13 +182,16 @@ export class LiveGuideWidget {
133
182
  capabilitySummary: { ctaOffer: false, microphone: mode === 'voice' && scopes.includes('microphone'),
134
183
  webrtc: typeof RTCPeerConnection !== 'undefined', audioPlayback: typeof HTMLAudioElement !== 'undefined', webAudio: typeof AudioContext !== 'undefined',
135
184
  svgOverlay: false, visualViewport: typeof visualViewport !== 'undefined', reducedMotion: matchMedia('(prefers-reduced-motion: reduce)').matches, lowPower: false } };
185
+ this.ownedLaunchId = intent.launchRequestId;
136
186
  // Start after the view has committed its connecting presentation. This does
137
187
  // not manufacture activation: prepareVoice tests the current real gesture.
138
188
  queueMicrotask(() => {
139
189
  void (async () => {
140
190
  try {
191
+ if (operation.signal.aborted || this.destroyed || this.operation !== operation || !this.hasLock())
192
+ throw new Error('Cancelled.');
141
193
  const authorization = await this.admission.launch(intent, operation.signal);
142
- if (operation.signal.aborted || this.destroyed || this.operation !== operation)
194
+ if (operation.signal.aborted || this.destroyed || this.operation !== operation || !this.hasLock())
143
195
  throw new Error('Cancelled.');
144
196
  const audio = this.view.livePlaybackElement();
145
197
  const channel = new LiveControlChannel({ authorization, trustedRuntimeOrigin: new URL(this.options.runtime.baseUrl).origin,
@@ -156,22 +208,33 @@ export class LiveGuideWidget {
156
208
  },
157
209
  onTextAnswer: value => {
158
210
  this.backendWorking = false;
211
+ this.view.setCommerceBusy(false);
159
212
  this.view.addCaption('assistant', value.text, true);
160
213
  this.view.setAssistantState('ready');
161
214
  },
162
215
  onTaskState: value => {
216
+ if (['cancelled', 'failed', 'blocked', 'fresh_task_required'].includes(value.status))
217
+ this.commerceCart?.cut();
163
218
  this.backendWorking = value.status === 'working';
219
+ if (value.status !== 'working')
220
+ this.view.setCommerceBusy(false);
164
221
  if (value.status === 'working')
165
222
  this.view.setAssistantState('thinking');
166
223
  else if (value.status === 'fresh_task_required') {
167
224
  this.view.setAssistantState('ready');
168
225
  this.view.setStatus(this.view.message('taskContextLimit'));
169
226
  }
170
- else if (value.status === 'failed' || value.status === 'blocked')
227
+ else if (value.status === 'failed' || value.status === 'blocked') {
228
+ this.view.setCommerceError();
171
229
  this.view.setStatus(this.view.message('error'));
230
+ }
172
231
  else
173
232
  this.view.setAssistantState('ready');
174
233
  },
234
+ ...(this.commerce ? { commerce: { bootstrap: this.commerce, currentContext: () => this.commerceContext?.current ?? null,
235
+ ...(this.commerceCart ? { cartVersion: 1 } : {}) },
236
+ onCommerceCatalog: (value) => this.showCommerceCatalog(value),
237
+ onCommerceCartEvent: (type, payload) => this.commerceCart?.onEvent(type, payload) } : {}),
175
238
  onTranscript: value => this.onTranscript(value),
176
239
  onRepeatAfterReady: () => this.view.setStatus(this.view.message('repeatWhenMicrophoneReady')),
177
240
  media: { audioElement: audio, workletUrl: config.liveAssets.audioWorkletUrl, dataChannelLabel: 'oai-events',
@@ -191,6 +254,7 @@ export class LiveGuideWidget {
191
254
  this.active = true;
192
255
  this.phase = 'active';
193
256
  this.view.showActive(null);
257
+ this.commerceContext?.refresh({ force: true });
194
258
  this.view.configureVoiceControls(mode === 'voice', false, true);
195
259
  if (question)
196
260
  this.submitText(question, true);
@@ -201,7 +265,7 @@ export class LiveGuideWidget {
201
265
  this.options.onState();
202
266
  }
203
267
  catch {
204
- if (!this.destroyed && !this.ending) {
268
+ if (!this.destroyed && !this.ending && this.operation === operation) {
205
269
  this.starting = false;
206
270
  this.phase = 'error';
207
271
  this.view.showError(this.view.message('error'), true);
@@ -338,6 +402,7 @@ export class LiveGuideWidget {
338
402
  if (playing === null) {
339
403
  this.playbackUnknownSince ?? (this.playbackUnknownSince = now);
340
404
  if (now - this.playbackUnknownSince >= 2000) {
405
+ this.commerceCart?.cut();
341
406
  this.capturePrepared = false;
342
407
  this.channel.fence('audio_suspended');
343
408
  this.view.setMuted(true);
@@ -362,6 +427,8 @@ export class LiveGuideWidget {
362
427
  }
363
428
  }
364
429
  onControlState(state) {
430
+ if (['suspended', 'ended', 'budget_blocked'].includes(state.phase))
431
+ this.commerceCart?.cut();
365
432
  if (this.livePhase !== state.phase && state.phase === 'local_listening')
366
433
  this.wakeRequested = false;
367
434
  this.livePhase = state.phase;
@@ -383,6 +450,8 @@ export class LiveGuideWidget {
383
450
  if (!this.channel || !this.active || !text.trim() || text.length > 4000)
384
451
  return false;
385
452
  try {
453
+ this.commerceCart?.cut();
454
+ this.commerceContext?.refresh();
386
455
  this.channel.submitText({ inputId: crypto.randomUUID(), inputEpoch: ++this.inputEpoch, text });
387
456
  if (echo)
388
457
  this.view.addCaption('user', text, true);
@@ -394,12 +463,21 @@ export class LiveGuideWidget {
394
463
  return false;
395
464
  }
396
465
  }
397
- stop() { this.inputEpoch++; this.channel?.stop(); this.wakeRequested = false; this.view.interruptPlayback(); }
466
+ stop() {
467
+ this.commerceCart?.cut();
468
+ this.inputEpoch++;
469
+ this.channel?.stop();
470
+ this.wakeRequested = false;
471
+ this.view.interruptPlayback();
472
+ this.commerceContext?.refresh({ force: true });
473
+ }
398
474
  privacy(reason) {
475
+ this.commerceCart?.cut();
399
476
  this.inputEpoch++;
400
477
  this.capturePrepared = false;
401
478
  this.wakeRequested = false;
402
479
  this.channel?.fence(reason);
480
+ this.commerceContext?.refresh({ force: true });
403
481
  }
404
482
  toText() {
405
483
  this.privacy('mode_change');
@@ -408,24 +486,97 @@ export class LiveGuideWidget {
408
486
  this.view.setModeTransitionPending(null);
409
487
  this.options.onState();
410
488
  }
489
+ onCommerceContext(context) {
490
+ this.commerceCart?.cut();
491
+ this.commerceCatalog = null;
492
+ this.view.clearCommerceCatalog();
493
+ if (!this.active || !this.channel?.commerceNegotiated || !this.commerceContext)
494
+ return;
495
+ try {
496
+ this.channel.setCommerceContext(context ?? this.commerceContext.identity);
497
+ }
498
+ catch {
499
+ this.view.setCommerceError();
500
+ }
501
+ }
502
+ currentCartSnapshot() {
503
+ if (!this.active || this.destroyed || document.visibilityState === 'hidden' ||
504
+ !this.scopes.includes('ai_disclosure') || !this.scopes.includes('commerce_cart'))
505
+ return null;
506
+ this.commerceContext?.refresh();
507
+ const authority = this.channel?.currentCartAuthority();
508
+ const context = this.commerceContext?.current;
509
+ if (!authority || !context)
510
+ return null;
511
+ return { sessionId: authority.sessionId, context, provider: { kind: 'live', taskId: authority.taskId, inputEpoch: authority.inputEpoch },
512
+ fencingToken: authority.fencingToken, consentEpoch: 0 };
513
+ }
514
+ showCommerceCatalog(event) {
515
+ if (!this.commerce || !this.active || !this.channel?.commerceNegotiated ||
516
+ !parseLiveCommerceCatalog(event, this.commerce, this.commerceContext?.current ?? null, undefined, Date.now(), { cartNegotiated: this.channel.commerceCartNegotiated }))
517
+ return;
518
+ this.commerceCart?.cut();
519
+ this.commerceCatalog = event;
520
+ const current = () => this.commerceCatalog === event && this.active && !!this.commerce &&
521
+ !!parseLiveCommerceCatalog(event, this.commerce, this.commerceContext?.current ?? null, undefined, Date.now(), { cartNegotiated: this.channel?.commerceCartNegotiated ?? false });
522
+ const request = (key, detail) => {
523
+ const context = this.commerceContext?.current;
524
+ const sameContext = this.commerceCatalog === event && this.active && context?.pageId === event.pageId &&
525
+ context.contextRevision === event.contextRevision;
526
+ if (!sameContext || (key !== 'commerceRefresh' && !current()))
527
+ return;
528
+ this.view.setCommerceBusy(true);
529
+ if (!this.submitText(`${this.view.message(key)}: ${JSON.stringify(detail)}`, true))
530
+ this.view.setCommerceError();
531
+ };
532
+ this.view.setCommerceCatalog(event.catalog, {
533
+ displayMode: this.channel.commerceCartNegotiated ? 'visible' : 'all',
534
+ onSelection: (productRef, selectedOptions) => {
535
+ const product = event.catalog.products.find(item => item.productRef === productRef);
536
+ if (!product || !isCommerceOptions(selectedOptions) || selectedOptions.length !== product.options.length ||
537
+ !selectedOptions.every(choice => product.options.some(option => option.name === choice.name && option.values.includes(choice.value))))
538
+ return;
539
+ request('commerceOptions', { productRef, selectedOptions });
540
+ },
541
+ onCompare: productRefs => {
542
+ if (productRefs.length < 2 || productRefs.length > 3 || new Set(productRefs).size !== productRefs.length ||
543
+ !productRefs.every(ref => event.catalog.products.some(product => product.productRef === ref)))
544
+ return;
545
+ request('commerceCompare', { productRefs });
546
+ },
547
+ onRefresh: () => request('commerceRefresh', { productRefs: event.catalog.products.map(product => product.productRef) }),
548
+ onClose: () => { this.commerceCart?.cut(); this.commerceCatalog = null; this.view.clearCommerceCatalog(); },
549
+ onDisplayed: (resultSetId, products) => {
550
+ if (!current())
551
+ return;
552
+ try {
553
+ if (this.channel?.acknowledgeCommerceCatalog({ schemaVersion: 1, taskId: event.taskId, inputEpoch: event.inputEpoch,
554
+ contextRevision: event.contextRevision, pageId: event.pageId, resultSetId, products }))
555
+ this.commerceCart?.refreshControls();
556
+ }
557
+ catch { /* A newer context or command cut invalidates this presentation receipt. */ }
558
+ },
559
+ });
560
+ this.commerceCart?.refreshControls();
561
+ }
411
562
  end() {
412
563
  if (this.ending)
413
564
  return this.ending;
414
- this.active = false;
415
- this.starting = false;
416
- this.capturePrepared = false;
417
- this.pendingQuestions = [];
565
+ if (!this.hasLock()) {
566
+ this.ownsLock = false;
567
+ this.ownedLaunchId = null;
568
+ this.detachSession();
569
+ return Promise.resolve();
570
+ }
571
+ const launchRequestId = this.ownedLaunchId;
572
+ this.detachSession();
418
573
  this.phase = 'ending';
419
- const channel = this.channel;
420
- this.channel = null;
421
- channel?.end();
422
- this.operation?.abort();
423
- this.operation = null;
424
574
  if (!this.destroyed)
425
575
  this.view.showEndingDetached();
426
- const pending = this.admission.end().then(() => {
576
+ const pending = (launchRequestId ? this.admission.end(undefined, launchRequestId) : Promise.resolve()).then(() => {
577
+ this.ownedLaunchId = null;
427
578
  this.phase = 'ended';
428
- this.options.release();
579
+ this.releaseLock();
429
580
  if (!this.destroyed)
430
581
  this.view.showResumeStopped();
431
582
  this.options.onState();
@@ -436,11 +587,65 @@ export class LiveGuideWidget {
436
587
  this.ending = pending;
437
588
  return pending;
438
589
  }
590
+ onTabActivityChange(active) {
591
+ if (this.destroyed)
592
+ return;
593
+ if (this.ownsLock && !this.hasLock() && !this.ending) {
594
+ this.ownsLock = false;
595
+ this.ownedLaunchId = null;
596
+ this.detachSession();
597
+ this.phase = 'ready';
598
+ }
599
+ this.showActiveElsewhere(active && !this.hasLock());
600
+ }
601
+ hasLock() { return this.ownsLock && (this.options.ownsActivity?.() ?? true); }
602
+ acquireLock() {
603
+ if (!this.options.acquire()) {
604
+ this.showActiveElsewhere(true);
605
+ return false;
606
+ }
607
+ this.ownsLock = true;
608
+ this.showActiveElsewhere(false);
609
+ return true;
610
+ }
611
+ releaseLock() {
612
+ const owned = this.hasLock();
613
+ this.ownsLock = false;
614
+ if (owned)
615
+ this.options.release();
616
+ }
617
+ showActiveElsewhere(active) {
618
+ if (this.activeElsewhere === active)
619
+ return;
620
+ this.activeElsewhere = active;
621
+ this.view.showActiveElsewhere(active);
622
+ if (!this.ending)
623
+ this.phase = active ? 'active_elsewhere' : 'ready';
624
+ this.options.onState();
625
+ }
626
+ detachSession() {
627
+ this.commerceCart?.cut();
628
+ this.active = false;
629
+ this.starting = false;
630
+ this.capturePrepared = false;
631
+ this.pendingQuestions = [];
632
+ this.commerceCatalog = null;
633
+ this.view.clearCommerceCatalog();
634
+ const channel = this.channel;
635
+ this.channel = null;
636
+ channel?.end();
637
+ this.operation?.abort();
638
+ this.operation = null;
639
+ }
439
640
  destroy() {
440
641
  if (this.destroyed)
441
642
  return;
442
643
  this.destroyed = true;
443
- window.removeEventListener('pagehide', this.pagehide);
644
+ window.removeEventListener('pagehide', this.pagehide, true);
645
+ window.removeEventListener('focus', this.refreshOwnership);
646
+ window.removeEventListener('pageshow', this.refreshOwnership);
647
+ this.commerceCart?.destroy();
648
+ this.commerceContext?.destroy();
444
649
  void this.end().catch(() => undefined);
445
650
  this.view.destroy();
446
651
  }