@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
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.LiveControlChannel = exports.LIVE_CONTROL_CLIENT_CAPABILITIES = exports.LIVE_CONTROL_CLIENT_VERSION = void 0;
4
4
  const liveDuplexMedia_js_1 = require("./liveDuplexMedia.js");
5
5
  const version_js_1 = require("./version.js");
6
+ const contracts_js_1 = require("./commerce/contracts.js");
6
7
  exports.LIVE_CONTROL_CLIENT_VERSION = version_js_1.VERSION;
7
8
  exports.LIVE_CONTROL_CLIENT_CAPABILITIES = ['live_duplex_v1', 'live_local_wake_v1', 'live_fragment_captions_v1'];
8
9
  const protocol = 'oxvo-ai-live-assist-v2';
@@ -10,6 +11,11 @@ const path = '/ai-live-assist/v2/live/control';
10
11
  const phases = ['voice_off', 'local_listening', 'wake_buffering', 'starting', 'active', 'tentative_idle', 'closing',
11
12
  'uncertain', 'muted', 'suspended', 'budget_blocked', 'ended'];
12
13
  const unavailable = () => new Error('The AI Guide voice connection is unavailable.');
14
+ const sameCatalog = (left, right) => {
15
+ const canonical = (value) => JSON.stringify(value, (_key, item) => item && typeof item === 'object' && !Array.isArray(item)
16
+ ? Object.fromEntries(Object.entries(item).sort(([a], [b]) => a.localeCompare(b))) : item);
17
+ return left !== null && canonical(left) === canonical(right);
18
+ };
13
19
  const uuid = (value) => typeof value === 'string' && /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(value);
14
20
  const count = (value) => typeof value === 'number' && Number.isSafeInteger(value) && value >= 0;
15
21
  const object = (value, keys) => {
@@ -66,6 +72,12 @@ class LiveControlChannel {
66
72
  this.captureEpoch = 0;
67
73
  this.requiresGesture = true;
68
74
  this.voicePrepared = false;
75
+ this.commerceTaskStatus = null;
76
+ this.commerceCatalog = null;
77
+ this.commerceDisplayedCatalog = null;
78
+ this.commerceDisplayedProducts = [];
79
+ this.commerceAcknowledgements = new Map();
80
+ this.minimumInputEpoch = 0;
69
81
  const { authorization: authorizationInput, trustedRuntimeOrigin, ...localOptions } = options;
70
82
  this.options = { ...localOptions, media: { ...options.media, dependencies: { ...options.media.dependencies } } };
71
83
  this.clock = options.clock ?? defaultClock;
@@ -121,7 +133,10 @@ class LiveControlChannel {
121
133
  this.close('protocol');
122
134
  return;
123
135
  }
124
- this.write('hello', { clientVersion: exports.LIVE_CONTROL_CLIENT_VERSION, liveCapabilities: [...exports.LIVE_CONTROL_CLIENT_CAPABILITIES] }, true);
136
+ this.write('hello', { clientVersion: exports.LIVE_CONTROL_CLIENT_VERSION, liveCapabilities: [...exports.LIVE_CONTROL_CLIENT_CAPABILITIES],
137
+ ...(this.options.commerce ? { commerceVersion: 1 } : {}),
138
+ ...(this.options.commerce?.cartVersion === 1 && this.options.commerce.bootstrap.cartEnabled
139
+ ? { commerceCartVersion: 1 } : {}) }, true);
125
140
  });
126
141
  socket.addEventListener('message', event => this.receive(event.data));
127
142
  socket.addEventListener('error', () => this.close('transport'));
@@ -186,8 +201,77 @@ class LiveControlChannel {
186
201
  if (!uuid(input.inputId) || !count(input.inputEpoch) || typeof input.text !== 'string' ||
187
202
  input.text.length < 1 || input.text.length > 4000 || !input.text.trim())
188
203
  throw unavailable();
204
+ this.minimumInputEpoch = input.inputEpoch;
205
+ this.commerceTask = undefined;
206
+ this.commerceCatalog = null;
207
+ this.commerceDisplayedCatalog = null;
208
+ this.commerceDisplayedProducts = [];
189
209
  this.command('text.submit', { ...input });
190
210
  }
211
+ get commerceNegotiated() { return !this.closed && this.ready?.commerceVersion === 1; }
212
+ get commerceCartNegotiated() { return this.commerceNegotiated && this.ready?.commerceCartVersion === 1; }
213
+ get sessionId() { return this.authorization.sessionId; }
214
+ setCommerceContext(context) {
215
+ if (!this.commerceNegotiated)
216
+ return false;
217
+ this.commerceCatalog = null;
218
+ this.commerceDisplayedCatalog = null;
219
+ this.commerceDisplayedProducts = [];
220
+ this.command('market' in context ? 'commerce.context' : 'commerce.context.clear', { ...context });
221
+ return true;
222
+ }
223
+ acknowledgeCommerceCatalog(value) {
224
+ const current = this.commerceCatalog;
225
+ const commerce = this.options.commerce;
226
+ if (!this.commerceNegotiated || !current || !commerce || !this.commerceTask || !(0, contracts_js_1.parseLiveCommerceCatalog)(current, commerce.bootstrap, commerce.currentContext(), this.commerceTask, this.clock.wallNow(), { cartNegotiated: this.commerceCartNegotiated }) ||
227
+ value.schemaVersion !== 1 || value.taskId !== current.taskId || value.inputEpoch !== current.inputEpoch ||
228
+ value.contextRevision !== current.contextRevision || value.pageId !== current.pageId ||
229
+ value.resultSetId !== current.catalog.resultSetId ||
230
+ !(this.commerceCartNegotiated ? (0, contracts_js_1.isDisplayedCommerceProducts)(current.catalog, value.products) :
231
+ JSON.stringify(value.products) === JSON.stringify((0, contracts_js_1.displayedCommerceProducts)(current.catalog))))
232
+ return false;
233
+ this.command('commerce.catalog.displayed', { ...value });
234
+ if (this.closed)
235
+ return false;
236
+ const seen = this.commerceDisplayedCatalog === current ? this.commerceDisplayedProducts : [];
237
+ this.commerceDisplayedProducts = (0, contracts_js_1.displayedCommerceProducts)(current.catalog).filter(pair => [...seen, ...value.products].some(value => value.productRef === pair.productRef && value.variantRef === pair.variantRef));
238
+ this.commerceDisplayedCatalog = current;
239
+ return true;
240
+ }
241
+ /** Display evidence only. A cart request still needs the runtime's fresh
242
+ * quote, consent checks and single-use execution admission. */
243
+ currentCartSelection(productRef, variantRef) {
244
+ const current = this.commerceCatalog;
245
+ const commerce = this.options.commerce;
246
+ if (!this.currentCartAuthority() || !current || current !== this.commerceDisplayedCatalog || !commerce ||
247
+ !this.commerceTask || !(0, contracts_js_1.parseLiveCommerceCatalog)(current, commerce.bootstrap, commerce.currentContext(), this.commerceTask, this.clock.wallNow(), { cartNegotiated: true }) ||
248
+ !this.commerceDisplayedProducts.some(value => value.productRef === productRef && value.variantRef === variantRef))
249
+ return null;
250
+ const selection = (0, contracts_js_1.commerceCartSelection)(current, productRef, variantRef);
251
+ const product = current.catalog.products.find(value => value.productRef === productRef && value.variant?.variantRef === variantRef);
252
+ return selection && product ? structuredClone({ selection, displayReceipt: {
253
+ ...(0, contracts_js_1.liveCommerceDisplayedReceipt)(current), products: this.commerceDisplayedProducts
254
+ },
255
+ product, expiresAt: current.catalog.expiresAt }) : null;
256
+ }
257
+ currentCartAuthority() {
258
+ if (!this.commerceCartNegotiated || this.barriers.size || !this.commerceTask || !this.ready ||
259
+ !['working', 'waiting'].includes(this.commerceTaskStatus ?? ''))
260
+ return null;
261
+ return { sessionId: this.authorization.sessionId, fencingToken: this.ready.fencingToken, ...this.commerceTask };
262
+ }
263
+ sendCommerceCart(type, payload) {
264
+ this.assertReady();
265
+ if (!this.commerceCartNegotiated || this.barriers.size ||
266
+ !['commerce.cart.request', 'commerce.cart.confirmation.result', 'commerce.cart.execution.begin', 'commerce.cart.execution.result'].includes(type))
267
+ throw unavailable();
268
+ const messageId = (this.options.randomUUID ?? (() => crypto.randomUUID()))();
269
+ if (!uuid(messageId) || this.outbound.has(messageId) || this.commerceAcknowledgements.has(messageId))
270
+ throw unavailable();
271
+ const accepted = new Promise(resolve => { this.commerceAcknowledgements.set(messageId, resolve); });
272
+ this.write(type, payload, false, null, messageId);
273
+ return { messageId, accepted };
274
+ }
191
275
  stop() {
192
276
  if (this.closed || !this.ready)
193
277
  return;
@@ -255,6 +339,10 @@ class LiveControlChannel {
255
339
  this.write(type, payload);
256
340
  }
257
341
  priority(type, payload) {
342
+ this.commerceCatalog = null;
343
+ this.commerceDisplayedCatalog = null;
344
+ this.commerceDisplayedProducts = [];
345
+ this.commerceTask = undefined;
258
346
  this.pendingCommands = [];
259
347
  if (this.barriers.size >= 4) {
260
348
  this.close('protocol');
@@ -268,12 +356,14 @@ class LiveControlChannel {
268
356
  if (item.barrier === null && !item.settled && item.epoch <= this.commandEpoch) {
269
357
  item.settled = true;
270
358
  this.clock.cancel(item.timer);
359
+ this.commerceAcknowledgements.get(item.id)?.(false);
360
+ this.commerceAcknowledgements.delete(item.id);
271
361
  }
272
362
  }
273
363
  this.commandEpoch++;
274
364
  }
275
365
  }
276
- write(type, payload, hello = false, barrier = null) {
366
+ write(type, payload, hello = false, barrier = null, requestedId) {
277
367
  try {
278
368
  this.now();
279
369
  const socket = this.socket;
@@ -281,7 +371,7 @@ class LiveControlChannel {
281
371
  throw unavailable();
282
372
  if ([...this.outbound.values()].filter(item => !item.settled).length >= 32)
283
373
  throw unavailable();
284
- const id = (this.options.randomUUID ?? (() => crypto.randomUUID()))();
374
+ const id = requestedId ?? (this.options.randomUUID ?? (() => crypto.randomUUID()))();
285
375
  if (!uuid(id) || this.outbound.has(id))
286
376
  throw unavailable();
287
377
  const frame = { v: 2, providerProtocol: 'live', id, sessionId: this.authorization.sessionId, tabId: this.options.tabId,
@@ -318,20 +408,24 @@ class LiveControlChannel {
318
408
  this.now();
319
409
  if (typeof data !== 'string' || new TextEncoder().encode(data).byteLength > 32768)
320
410
  throw unavailable();
321
- // Server frames have exactly two object levels and no arrays. Reject
322
- // duplicate keys (including escaped aliases) before JSON.parse folds them.
411
+ const parsed = JSON.parse(data);
412
+ const cartFrame = this.commerceCartNegotiated && ['commerce.cart.confirmation', 'commerce.cart.grant',
413
+ 'commerce.cart.execution.ready', 'commerce.cart.result'].includes(parsed?.type);
414
+ const commerceFrame = (this.commerceNegotiated && parsed?.type === 'commerce.catalog') || cartFrame;
415
+ // Only negotiated commerce frames may contain nested objects/arrays.
416
+ // Keep duplicate-key rejection, including escaped aliases, for both paths.
323
417
  const stack = [];
324
418
  const tokens = /"(?:\\.|[^"\\])*"|[{}]|\[|\]/g;
325
419
  for (const match of data.matchAll(tokens)) {
326
- if (match[0] === '{') {
327
- stack.push(new Set());
328
- if (stack.length > 2)
420
+ if (match[0] === '{' || match[0] === '[') {
421
+ if (match[0] === '[' && !commerceFrame)
422
+ throw unavailable();
423
+ stack.push(match[0] === '{' ? new Set() : null);
424
+ if (stack.length > (commerceFrame ? 10 : 2))
329
425
  throw unavailable();
330
426
  }
331
- else if (match[0] === '}')
427
+ else if (match[0] === '}' || match[0] === ']')
332
428
  stack.pop();
333
- else if (match[0] === '[' || match[0] === ']')
334
- throw unavailable();
335
429
  else if (data.slice((match.index ?? 0) + match[0].length).trimStart().startsWith(':')) {
336
430
  const key = JSON.parse(match[0]);
337
431
  const keys = stack.at(-1);
@@ -340,7 +434,7 @@ class LiveControlChannel {
340
434
  keys.add(key);
341
435
  }
342
436
  }
343
- const value = object(JSON.parse(data), ['v', 'providerProtocol', 'sessionId', 'sequence', 'commandEpoch', 'type', 'payload']);
437
+ const value = object(parsed, ['v', 'providerProtocol', 'sessionId', 'sequence', 'commandEpoch', 'type', 'payload']);
344
438
  if (value.v !== 2 || value.providerProtocol !== 'live' || value.sessionId !== this.authorization.sessionId ||
345
439
  !count(value.sequence) || value.sequence !== this.serverSequence + 1 || !count(value.commandEpoch) ||
346
440
  value.commandEpoch < this.serverEpoch || value.commandEpoch > this.commandEpoch || typeof value.type !== 'string')
@@ -349,7 +443,10 @@ class LiveControlChannel {
349
443
  this.serverEpoch = value.commandEpoch;
350
444
  const message = value;
351
445
  if (!['ready', 'ack', 'state', 'wake.buffer', 'rtc.answer', 'media.activate', 'media.retire', 'media.terminal',
352
- 'playback.suppress', 'capture.stop', 'wake.discard', 'wake.release', 'repeat_after_ready', 'text.answer', 'transcript.delta', 'task.state'].includes(message.type))
446
+ 'playback.suppress', 'capture.stop', 'wake.discard', 'wake.release', 'repeat_after_ready', 'text.answer', 'transcript.delta', 'task.state',
447
+ ...(this.commerceNegotiated ? ['commerce.catalog'] : []),
448
+ ...(this.commerceCartNegotiated ? ['commerce.cart.confirmation', 'commerce.cart.grant', 'commerce.cart.execution.ready',
449
+ 'commerce.cart.result'] : [])].includes(message.type))
353
450
  throw unavailable();
354
451
  if (message.type === 'ready') {
355
452
  this.acceptReady(message);
@@ -367,7 +464,15 @@ class LiveControlChannel {
367
464
  }
368
465
  }
369
466
  acceptReady(message) {
370
- const p = object(message.payload, ['fencingToken', 'authorityGeneration', 'commandEpoch', 'credentialExpiresAt', 'logicalDeadlineAt']);
467
+ const negotiated = Object.prototype.hasOwnProperty.call(message.payload, 'commerceVersion');
468
+ const cartNegotiated = Object.prototype.hasOwnProperty.call(message.payload, 'commerceCartVersion');
469
+ const p = object(message.payload, ['fencingToken', 'authorityGeneration', 'commandEpoch', 'credentialExpiresAt', 'logicalDeadlineAt',
470
+ ...(negotiated ? ['commerceVersion'] : []), ...(cartNegotiated ? ['commerceCartVersion'] : [])]);
471
+ if (negotiated && (!this.options.commerce || p.commerceVersion !== 1))
472
+ throw unavailable();
473
+ if (cartNegotiated && (!negotiated || this.options.commerce?.cartVersion !== 1 ||
474
+ !this.options.commerce.bootstrap.cartEnabled || p.commerceCartVersion !== 1))
475
+ throw unavailable();
371
476
  if (this.ready || message.sequence !== 1 || message.commandEpoch !== 0 || p.commandEpoch !== 0 ||
372
477
  !count(p.fencingToken) || p.fencingToken < 1 || !count(p.authorityGeneration) ||
373
478
  timestamp(p.credentialExpiresAt) !== timestamp(this.authorization.controlExpiresAt) ||
@@ -399,6 +504,8 @@ class LiveControlChannel {
399
504
  item.settled = true;
400
505
  this.clock.cancel(item.timer);
401
506
  this.barriers.delete(item.id);
507
+ this.commerceAcknowledgements.get(item.id)?.(p.status === 'accepted');
508
+ this.commerceAcknowledgements.delete(item.id);
402
509
  if (item.type === 'voice.ready' && p.status === 'accepted' && item.epoch === this.commandEpoch)
403
510
  this.voicePrepared = true;
404
511
  if (!this.barriers.size) {
@@ -541,8 +648,46 @@ class LiveControlChannel {
541
648
  if ((p.taskId !== null && !uuid(p.taskId)) || !count(p.inputEpoch) ||
542
649
  !['working', 'waiting', 'blocked', 'cancelled', 'failed', 'fresh_task_required'].includes(String(p.status)))
543
650
  throw unavailable();
651
+ if (p.inputEpoch >= this.minimumInputEpoch) {
652
+ if (p.taskId !== this.commerceTask?.taskId || p.inputEpoch !== this.commerceTask?.inputEpoch) {
653
+ this.commerceCatalog = null;
654
+ this.commerceDisplayedCatalog = null;
655
+ this.commerceDisplayedProducts = [];
656
+ }
657
+ this.commerceTask = p.taskId === null ? undefined : { taskId: p.taskId, inputEpoch: p.inputEpoch };
658
+ this.commerceTaskStatus = String(p.status);
659
+ if (['cancelled', 'failed', 'fresh_task_required'].includes(String(p.status))) {
660
+ this.commerceCatalog = null;
661
+ this.commerceDisplayedCatalog = null;
662
+ this.commerceDisplayedProducts = [];
663
+ }
664
+ }
544
665
  this.options.onTaskState?.(p);
545
666
  break;
667
+ case 'commerce.catalog': {
668
+ const commerce = this.options.commerce;
669
+ if (!commerce || !this.commerceNegotiated)
670
+ throw unavailable();
671
+ if (!this.commerceTask)
672
+ return;
673
+ const catalog = (0, contracts_js_1.parseLiveCommerceCatalog)(p, commerce.bootstrap, commerce.currentContext(), this.commerceTask, this.clock.wallNow(), { cartNegotiated: this.commerceCartNegotiated });
674
+ // A context change may have overtaken a valid in-flight catalog.
675
+ if (!catalog || catalog.inputEpoch < this.minimumInputEpoch || sameCatalog(this.commerceCatalog, catalog))
676
+ return;
677
+ this.commerceCatalog = catalog;
678
+ this.commerceDisplayedCatalog = null;
679
+ this.commerceDisplayedProducts = [];
680
+ this.options.onCommerceCatalog?.(catalog);
681
+ break;
682
+ }
683
+ case 'commerce.cart.confirmation':
684
+ case 'commerce.cart.grant':
685
+ case 'commerce.cart.execution.ready':
686
+ case 'commerce.cart.result':
687
+ // Deliver ready without awaiting a pending cart command. The cart
688
+ // session separately requires its exact ACK and execution admission.
689
+ this.options.onCommerceCartEvent?.(message.type, p);
690
+ break;
546
691
  default: throw unavailable();
547
692
  }
548
693
  }
@@ -801,6 +946,13 @@ class LiveControlChannel {
801
946
  this.lifetime.abort();
802
947
  this.detachExternal?.();
803
948
  this.detachExternal = null;
949
+ this.commerceCatalog = null;
950
+ this.commerceDisplayedCatalog = null;
951
+ this.commerceDisplayedProducts = [];
952
+ this.commerceTask = undefined;
953
+ for (const resolve of this.commerceAcknowledgements.values())
954
+ resolve(false);
955
+ this.commerceAcknowledgements.clear();
804
956
  this.clock.cancel(this.connectionTimer);
805
957
  this.clock.cancel(this.expiryTimer);
806
958
  for (const item of this.outbound.values())
@@ -14,8 +14,9 @@ export declare class LiveGuideAdmission {
14
14
  private readonly runtime;
15
15
  private readonly legacy?;
16
16
  private pendingAdmission;
17
- constructor(identity: GuideIdentity, store: GuideSubjectStore, runtime: RuntimeClient, legacy?: Pick<GuideAdmission, "endLaunch" | "complete"> | undefined);
17
+ constructor(identity: GuideIdentity, store: GuideSubjectStore, runtime: RuntimeClient, legacy?: (Pick<GuideAdmission, "endLaunch" | "complete"> & Partial<Pick<GuideAdmission, "measurementIntent" | "measurementBridge">>) | undefined);
18
18
  hasPendingLaunch(): boolean;
19
+ pendingLaunchId(): string | null;
19
20
  launch(intent: LiveGuideLaunchIntent, signal: AbortSignal): Promise<LiveControlAuthorization>;
20
- end(signal?: AbortSignal): Promise<void>;
21
+ end(signal?: AbortSignal, expectedLaunchRequestId?: string): Promise<void>;
21
22
  }
@@ -15,9 +15,13 @@ class LiveGuideAdmission {
15
15
  this.pendingAdmission = null;
16
16
  }
17
17
  hasPendingLaunch() { return this.store.readLaunch() !== null; }
18
+ pendingLaunchId() { return this.store.readLaunch()?.intent.launchRequestId ?? null; }
18
19
  async launch(intent, signal) {
19
20
  if (intent.installKey !== this.identity.installKey || this.store.readLaunch())
20
21
  throw unavailable();
22
+ const measurement = this.legacy?.measurementIntent?.(intent.clientVersion);
23
+ if (measurement)
24
+ intent = { ...intent, guideMeasurement: measurement };
21
25
  const subject = this.store.readSubject();
22
26
  const admission = this.runtime.admitLiveGuide(intent, subject?.credential, signal);
23
27
  this.pendingAdmission = admission;
@@ -52,13 +56,13 @@ class LiveGuideAdmission {
52
56
  }
53
57
  if (signal.aborted)
54
58
  throw unavailable();
55
- const launch = await this.runtime.launchLiveGuide(intent, grant, signal);
59
+ const launch = await this.runtime.launchLiveGuide(intent, grant, signal, ...(intent.guideMeasurement ? [this.legacy?.measurementBridge?.(intent)] : []));
56
60
  if (launch.projectId !== this.identity.projectId || launch.guideId !== this.identity.guideId ||
57
61
  launch.subjectId !== subjectId || launch.launchRequestId !== intent.launchRequestId)
58
62
  throw unavailable();
59
63
  return launch;
60
64
  }
61
- async end(signal) {
65
+ async end(signal, expectedLaunchRequestId) {
62
66
  if (this.pendingAdmission) {
63
67
  await this.pendingAdmission.catch(() => undefined);
64
68
  await Promise.resolve();
@@ -66,6 +70,10 @@ class LiveGuideAdmission {
66
70
  const record = this.store.readLaunch();
67
71
  if (!record)
68
72
  return;
73
+ // Ownership is captured by the widget before navigation releases its lock.
74
+ // A delayed cleanup must never adopt a replacement launch from another tab.
75
+ if (expectedLaunchRequestId !== undefined && record.intent.launchRequestId !== expectedLaunchRequestId)
76
+ return;
69
77
  const intent = record.intent;
70
78
  // A published model change shares the same visitor store. Retire the exact
71
79
  // prior protocol through its authoritative End readback, never discard it
@@ -11,6 +11,7 @@ type Options = {
11
11
  messages: PluginOptions['messages'];
12
12
  viewOptions: WidgetViewOptions;
13
13
  acquire(): boolean;
14
+ ownsActivity?(): boolean;
14
15
  release(): void;
15
16
  onState(): void;
16
17
  };
@@ -27,6 +28,9 @@ export declare class LiveGuideWidget {
27
28
  private destroyed;
28
29
  private active;
29
30
  private starting;
31
+ private ownsLock;
32
+ private ownedLaunchId;
33
+ private activeElsewhere;
30
34
  private phase;
31
35
  private mode;
32
36
  private captureEpoch;
@@ -46,6 +50,10 @@ export declare class LiveGuideWidget {
46
50
  private pendingQuestions;
47
51
  private readonly requestIds;
48
52
  private livePhase;
53
+ private readonly commerce;
54
+ private readonly commerceContext;
55
+ private commerceCatalog;
56
+ private readonly commerceCart;
49
57
  constructor(options: Options);
50
58
  open(input?: AiGuideOpenInput): AiGuideControllerState;
51
59
  close(): void;
@@ -62,7 +70,17 @@ export declare class LiveGuideWidget {
62
70
  private privacy;
63
71
  private toText;
64
72
  private pagehide;
73
+ private refreshOwnership;
74
+ private onCommerceContext;
75
+ private currentCartSnapshot;
76
+ private showCommerceCatalog;
65
77
  end(): Promise<void>;
78
+ onTabActivityChange(active: boolean): void;
79
+ private hasLock;
80
+ private acquireLock;
81
+ private releaseLock;
82
+ private showActiveElsewhere;
83
+ private detachSession;
66
84
  destroy(): void;
67
85
  }
68
86
  export {};