@oxvo/ai-live-assist 7.5.20 → 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 +259 -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 +259 -9
  97. package/lib/version.d.ts +1 -1
  98. package/lib/version.js +1 -1
  99. package/package.json +1 -1
@@ -1,5 +1,6 @@
1
1
  import { LiveDuplexMedia } from './liveDuplexMedia.js';
2
2
  import { VERSION } from './version.js';
3
+ import { parseLiveCommerceCatalog, displayedCommerceProducts, isDisplayedCommerceProducts, commerceCartSelection, liveCommerceDisplayedReceipt } from './commerce/contracts.js';
3
4
  export const LIVE_CONTROL_CLIENT_VERSION = VERSION;
4
5
  export const LIVE_CONTROL_CLIENT_CAPABILITIES = ['live_duplex_v1', 'live_local_wake_v1', 'live_fragment_captions_v1'];
5
6
  const protocol = 'oxvo-ai-live-assist-v2';
@@ -7,6 +8,11 @@ const path = '/ai-live-assist/v2/live/control';
7
8
  const phases = ['voice_off', 'local_listening', 'wake_buffering', 'starting', 'active', 'tentative_idle', 'closing',
8
9
  'uncertain', 'muted', 'suspended', 'budget_blocked', 'ended'];
9
10
  const unavailable = () => new Error('The AI Guide voice connection is unavailable.');
11
+ const sameCatalog = (left, right) => {
12
+ const canonical = (value) => JSON.stringify(value, (_key, item) => item && typeof item === 'object' && !Array.isArray(item)
13
+ ? Object.fromEntries(Object.entries(item).sort(([a], [b]) => a.localeCompare(b))) : item);
14
+ return left !== null && canonical(left) === canonical(right);
15
+ };
10
16
  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);
11
17
  const count = (value) => typeof value === 'number' && Number.isSafeInteger(value) && value >= 0;
12
18
  const object = (value, keys) => {
@@ -63,6 +69,12 @@ export class LiveControlChannel {
63
69
  this.captureEpoch = 0;
64
70
  this.requiresGesture = true;
65
71
  this.voicePrepared = false;
72
+ this.commerceTaskStatus = null;
73
+ this.commerceCatalog = null;
74
+ this.commerceDisplayedCatalog = null;
75
+ this.commerceDisplayedProducts = [];
76
+ this.commerceAcknowledgements = new Map();
77
+ this.minimumInputEpoch = 0;
66
78
  const { authorization: authorizationInput, trustedRuntimeOrigin, ...localOptions } = options;
67
79
  this.options = { ...localOptions, media: { ...options.media, dependencies: { ...options.media.dependencies } } };
68
80
  this.clock = options.clock ?? defaultClock;
@@ -118,7 +130,10 @@ export class LiveControlChannel {
118
130
  this.close('protocol');
119
131
  return;
120
132
  }
121
- this.write('hello', { clientVersion: LIVE_CONTROL_CLIENT_VERSION, liveCapabilities: [...LIVE_CONTROL_CLIENT_CAPABILITIES] }, true);
133
+ this.write('hello', { clientVersion: LIVE_CONTROL_CLIENT_VERSION, liveCapabilities: [...LIVE_CONTROL_CLIENT_CAPABILITIES],
134
+ ...(this.options.commerce ? { commerceVersion: 1 } : {}),
135
+ ...(this.options.commerce?.cartVersion === 1 && this.options.commerce.bootstrap.cartEnabled
136
+ ? { commerceCartVersion: 1 } : {}) }, true);
122
137
  });
123
138
  socket.addEventListener('message', event => this.receive(event.data));
124
139
  socket.addEventListener('error', () => this.close('transport'));
@@ -183,8 +198,77 @@ export class LiveControlChannel {
183
198
  if (!uuid(input.inputId) || !count(input.inputEpoch) || typeof input.text !== 'string' ||
184
199
  input.text.length < 1 || input.text.length > 4000 || !input.text.trim())
185
200
  throw unavailable();
201
+ this.minimumInputEpoch = input.inputEpoch;
202
+ this.commerceTask = undefined;
203
+ this.commerceCatalog = null;
204
+ this.commerceDisplayedCatalog = null;
205
+ this.commerceDisplayedProducts = [];
186
206
  this.command('text.submit', { ...input });
187
207
  }
208
+ get commerceNegotiated() { return !this.closed && this.ready?.commerceVersion === 1; }
209
+ get commerceCartNegotiated() { return this.commerceNegotiated && this.ready?.commerceCartVersion === 1; }
210
+ get sessionId() { return this.authorization.sessionId; }
211
+ setCommerceContext(context) {
212
+ if (!this.commerceNegotiated)
213
+ return false;
214
+ this.commerceCatalog = null;
215
+ this.commerceDisplayedCatalog = null;
216
+ this.commerceDisplayedProducts = [];
217
+ this.command('market' in context ? 'commerce.context' : 'commerce.context.clear', { ...context });
218
+ return true;
219
+ }
220
+ acknowledgeCommerceCatalog(value) {
221
+ const current = this.commerceCatalog;
222
+ const commerce = this.options.commerce;
223
+ if (!this.commerceNegotiated || !current || !commerce || !this.commerceTask || !parseLiveCommerceCatalog(current, commerce.bootstrap, commerce.currentContext(), this.commerceTask, this.clock.wallNow(), { cartNegotiated: this.commerceCartNegotiated }) ||
224
+ value.schemaVersion !== 1 || value.taskId !== current.taskId || value.inputEpoch !== current.inputEpoch ||
225
+ value.contextRevision !== current.contextRevision || value.pageId !== current.pageId ||
226
+ value.resultSetId !== current.catalog.resultSetId ||
227
+ !(this.commerceCartNegotiated ? isDisplayedCommerceProducts(current.catalog, value.products) :
228
+ JSON.stringify(value.products) === JSON.stringify(displayedCommerceProducts(current.catalog))))
229
+ return false;
230
+ this.command('commerce.catalog.displayed', { ...value });
231
+ if (this.closed)
232
+ return false;
233
+ const seen = this.commerceDisplayedCatalog === current ? this.commerceDisplayedProducts : [];
234
+ this.commerceDisplayedProducts = displayedCommerceProducts(current.catalog).filter(pair => [...seen, ...value.products].some(value => value.productRef === pair.productRef && value.variantRef === pair.variantRef));
235
+ this.commerceDisplayedCatalog = current;
236
+ return true;
237
+ }
238
+ /** Display evidence only. A cart request still needs the runtime's fresh
239
+ * quote, consent checks and single-use execution admission. */
240
+ currentCartSelection(productRef, variantRef) {
241
+ const current = this.commerceCatalog;
242
+ const commerce = this.options.commerce;
243
+ if (!this.currentCartAuthority() || !current || current !== this.commerceDisplayedCatalog || !commerce ||
244
+ !this.commerceTask || !parseLiveCommerceCatalog(current, commerce.bootstrap, commerce.currentContext(), this.commerceTask, this.clock.wallNow(), { cartNegotiated: true }) ||
245
+ !this.commerceDisplayedProducts.some(value => value.productRef === productRef && value.variantRef === variantRef))
246
+ return null;
247
+ const selection = commerceCartSelection(current, productRef, variantRef);
248
+ const product = current.catalog.products.find(value => value.productRef === productRef && value.variant?.variantRef === variantRef);
249
+ return selection && product ? structuredClone({ selection, displayReceipt: {
250
+ ...liveCommerceDisplayedReceipt(current), products: this.commerceDisplayedProducts
251
+ },
252
+ product, expiresAt: current.catalog.expiresAt }) : null;
253
+ }
254
+ currentCartAuthority() {
255
+ if (!this.commerceCartNegotiated || this.barriers.size || !this.commerceTask || !this.ready ||
256
+ !['working', 'waiting'].includes(this.commerceTaskStatus ?? ''))
257
+ return null;
258
+ return { sessionId: this.authorization.sessionId, fencingToken: this.ready.fencingToken, ...this.commerceTask };
259
+ }
260
+ sendCommerceCart(type, payload) {
261
+ this.assertReady();
262
+ if (!this.commerceCartNegotiated || this.barriers.size ||
263
+ !['commerce.cart.request', 'commerce.cart.confirmation.result', 'commerce.cart.execution.begin', 'commerce.cart.execution.result'].includes(type))
264
+ throw unavailable();
265
+ const messageId = (this.options.randomUUID ?? (() => crypto.randomUUID()))();
266
+ if (!uuid(messageId) || this.outbound.has(messageId) || this.commerceAcknowledgements.has(messageId))
267
+ throw unavailable();
268
+ const accepted = new Promise(resolve => { this.commerceAcknowledgements.set(messageId, resolve); });
269
+ this.write(type, payload, false, null, messageId);
270
+ return { messageId, accepted };
271
+ }
188
272
  stop() {
189
273
  if (this.closed || !this.ready)
190
274
  return;
@@ -252,6 +336,10 @@ export class LiveControlChannel {
252
336
  this.write(type, payload);
253
337
  }
254
338
  priority(type, payload) {
339
+ this.commerceCatalog = null;
340
+ this.commerceDisplayedCatalog = null;
341
+ this.commerceDisplayedProducts = [];
342
+ this.commerceTask = undefined;
255
343
  this.pendingCommands = [];
256
344
  if (this.barriers.size >= 4) {
257
345
  this.close('protocol');
@@ -265,12 +353,14 @@ export class LiveControlChannel {
265
353
  if (item.barrier === null && !item.settled && item.epoch <= this.commandEpoch) {
266
354
  item.settled = true;
267
355
  this.clock.cancel(item.timer);
356
+ this.commerceAcknowledgements.get(item.id)?.(false);
357
+ this.commerceAcknowledgements.delete(item.id);
268
358
  }
269
359
  }
270
360
  this.commandEpoch++;
271
361
  }
272
362
  }
273
- write(type, payload, hello = false, barrier = null) {
363
+ write(type, payload, hello = false, barrier = null, requestedId) {
274
364
  try {
275
365
  this.now();
276
366
  const socket = this.socket;
@@ -278,7 +368,7 @@ export class LiveControlChannel {
278
368
  throw unavailable();
279
369
  if ([...this.outbound.values()].filter(item => !item.settled).length >= 32)
280
370
  throw unavailable();
281
- const id = (this.options.randomUUID ?? (() => crypto.randomUUID()))();
371
+ const id = requestedId ?? (this.options.randomUUID ?? (() => crypto.randomUUID()))();
282
372
  if (!uuid(id) || this.outbound.has(id))
283
373
  throw unavailable();
284
374
  const frame = { v: 2, providerProtocol: 'live', id, sessionId: this.authorization.sessionId, tabId: this.options.tabId,
@@ -315,20 +405,24 @@ export class LiveControlChannel {
315
405
  this.now();
316
406
  if (typeof data !== 'string' || new TextEncoder().encode(data).byteLength > 32768)
317
407
  throw unavailable();
318
- // Server frames have exactly two object levels and no arrays. Reject
319
- // duplicate keys (including escaped aliases) before JSON.parse folds them.
408
+ const parsed = JSON.parse(data);
409
+ const cartFrame = this.commerceCartNegotiated && ['commerce.cart.confirmation', 'commerce.cart.grant',
410
+ 'commerce.cart.execution.ready', 'commerce.cart.result'].includes(parsed?.type);
411
+ const commerceFrame = (this.commerceNegotiated && parsed?.type === 'commerce.catalog') || cartFrame;
412
+ // Only negotiated commerce frames may contain nested objects/arrays.
413
+ // Keep duplicate-key rejection, including escaped aliases, for both paths.
320
414
  const stack = [];
321
415
  const tokens = /"(?:\\.|[^"\\])*"|[{}]|\[|\]/g;
322
416
  for (const match of data.matchAll(tokens)) {
323
- if (match[0] === '{') {
324
- stack.push(new Set());
325
- if (stack.length > 2)
417
+ if (match[0] === '{' || match[0] === '[') {
418
+ if (match[0] === '[' && !commerceFrame)
419
+ throw unavailable();
420
+ stack.push(match[0] === '{' ? new Set() : null);
421
+ if (stack.length > (commerceFrame ? 10 : 2))
326
422
  throw unavailable();
327
423
  }
328
- else if (match[0] === '}')
424
+ else if (match[0] === '}' || match[0] === ']')
329
425
  stack.pop();
330
- else if (match[0] === '[' || match[0] === ']')
331
- throw unavailable();
332
426
  else if (data.slice((match.index ?? 0) + match[0].length).trimStart().startsWith(':')) {
333
427
  const key = JSON.parse(match[0]);
334
428
  const keys = stack.at(-1);
@@ -337,7 +431,7 @@ export class LiveControlChannel {
337
431
  keys.add(key);
338
432
  }
339
433
  }
340
- const value = object(JSON.parse(data), ['v', 'providerProtocol', 'sessionId', 'sequence', 'commandEpoch', 'type', 'payload']);
434
+ const value = object(parsed, ['v', 'providerProtocol', 'sessionId', 'sequence', 'commandEpoch', 'type', 'payload']);
341
435
  if (value.v !== 2 || value.providerProtocol !== 'live' || value.sessionId !== this.authorization.sessionId ||
342
436
  !count(value.sequence) || value.sequence !== this.serverSequence + 1 || !count(value.commandEpoch) ||
343
437
  value.commandEpoch < this.serverEpoch || value.commandEpoch > this.commandEpoch || typeof value.type !== 'string')
@@ -346,7 +440,10 @@ export class LiveControlChannel {
346
440
  this.serverEpoch = value.commandEpoch;
347
441
  const message = value;
348
442
  if (!['ready', 'ack', 'state', 'wake.buffer', 'rtc.answer', 'media.activate', 'media.retire', 'media.terminal',
349
- 'playback.suppress', 'capture.stop', 'wake.discard', 'wake.release', 'repeat_after_ready', 'text.answer', 'transcript.delta', 'task.state'].includes(message.type))
443
+ 'playback.suppress', 'capture.stop', 'wake.discard', 'wake.release', 'repeat_after_ready', 'text.answer', 'transcript.delta', 'task.state',
444
+ ...(this.commerceNegotiated ? ['commerce.catalog'] : []),
445
+ ...(this.commerceCartNegotiated ? ['commerce.cart.confirmation', 'commerce.cart.grant', 'commerce.cart.execution.ready',
446
+ 'commerce.cart.result'] : [])].includes(message.type))
350
447
  throw unavailable();
351
448
  if (message.type === 'ready') {
352
449
  this.acceptReady(message);
@@ -364,7 +461,15 @@ export class LiveControlChannel {
364
461
  }
365
462
  }
366
463
  acceptReady(message) {
367
- const p = object(message.payload, ['fencingToken', 'authorityGeneration', 'commandEpoch', 'credentialExpiresAt', 'logicalDeadlineAt']);
464
+ const negotiated = Object.prototype.hasOwnProperty.call(message.payload, 'commerceVersion');
465
+ const cartNegotiated = Object.prototype.hasOwnProperty.call(message.payload, 'commerceCartVersion');
466
+ const p = object(message.payload, ['fencingToken', 'authorityGeneration', 'commandEpoch', 'credentialExpiresAt', 'logicalDeadlineAt',
467
+ ...(negotiated ? ['commerceVersion'] : []), ...(cartNegotiated ? ['commerceCartVersion'] : [])]);
468
+ if (negotiated && (!this.options.commerce || p.commerceVersion !== 1))
469
+ throw unavailable();
470
+ if (cartNegotiated && (!negotiated || this.options.commerce?.cartVersion !== 1 ||
471
+ !this.options.commerce.bootstrap.cartEnabled || p.commerceCartVersion !== 1))
472
+ throw unavailable();
368
473
  if (this.ready || message.sequence !== 1 || message.commandEpoch !== 0 || p.commandEpoch !== 0 ||
369
474
  !count(p.fencingToken) || p.fencingToken < 1 || !count(p.authorityGeneration) ||
370
475
  timestamp(p.credentialExpiresAt) !== timestamp(this.authorization.controlExpiresAt) ||
@@ -396,6 +501,8 @@ export class LiveControlChannel {
396
501
  item.settled = true;
397
502
  this.clock.cancel(item.timer);
398
503
  this.barriers.delete(item.id);
504
+ this.commerceAcknowledgements.get(item.id)?.(p.status === 'accepted');
505
+ this.commerceAcknowledgements.delete(item.id);
399
506
  if (item.type === 'voice.ready' && p.status === 'accepted' && item.epoch === this.commandEpoch)
400
507
  this.voicePrepared = true;
401
508
  if (!this.barriers.size) {
@@ -538,8 +645,46 @@ export class LiveControlChannel {
538
645
  if ((p.taskId !== null && !uuid(p.taskId)) || !count(p.inputEpoch) ||
539
646
  !['working', 'waiting', 'blocked', 'cancelled', 'failed', 'fresh_task_required'].includes(String(p.status)))
540
647
  throw unavailable();
648
+ if (p.inputEpoch >= this.minimumInputEpoch) {
649
+ if (p.taskId !== this.commerceTask?.taskId || p.inputEpoch !== this.commerceTask?.inputEpoch) {
650
+ this.commerceCatalog = null;
651
+ this.commerceDisplayedCatalog = null;
652
+ this.commerceDisplayedProducts = [];
653
+ }
654
+ this.commerceTask = p.taskId === null ? undefined : { taskId: p.taskId, inputEpoch: p.inputEpoch };
655
+ this.commerceTaskStatus = String(p.status);
656
+ if (['cancelled', 'failed', 'fresh_task_required'].includes(String(p.status))) {
657
+ this.commerceCatalog = null;
658
+ this.commerceDisplayedCatalog = null;
659
+ this.commerceDisplayedProducts = [];
660
+ }
661
+ }
541
662
  this.options.onTaskState?.(p);
542
663
  break;
664
+ case 'commerce.catalog': {
665
+ const commerce = this.options.commerce;
666
+ if (!commerce || !this.commerceNegotiated)
667
+ throw unavailable();
668
+ if (!this.commerceTask)
669
+ return;
670
+ const catalog = parseLiveCommerceCatalog(p, commerce.bootstrap, commerce.currentContext(), this.commerceTask, this.clock.wallNow(), { cartNegotiated: this.commerceCartNegotiated });
671
+ // A context change may have overtaken a valid in-flight catalog.
672
+ if (!catalog || catalog.inputEpoch < this.minimumInputEpoch || sameCatalog(this.commerceCatalog, catalog))
673
+ return;
674
+ this.commerceCatalog = catalog;
675
+ this.commerceDisplayedCatalog = null;
676
+ this.commerceDisplayedProducts = [];
677
+ this.options.onCommerceCatalog?.(catalog);
678
+ break;
679
+ }
680
+ case 'commerce.cart.confirmation':
681
+ case 'commerce.cart.grant':
682
+ case 'commerce.cart.execution.ready':
683
+ case 'commerce.cart.result':
684
+ // Deliver ready without awaiting a pending cart command. The cart
685
+ // session separately requires its exact ACK and execution admission.
686
+ this.options.onCommerceCartEvent?.(message.type, p);
687
+ break;
543
688
  default: throw unavailable();
544
689
  }
545
690
  }
@@ -798,6 +943,13 @@ export class LiveControlChannel {
798
943
  this.lifetime.abort();
799
944
  this.detachExternal?.();
800
945
  this.detachExternal = null;
946
+ this.commerceCatalog = null;
947
+ this.commerceDisplayedCatalog = null;
948
+ this.commerceDisplayedProducts = [];
949
+ this.commerceTask = undefined;
950
+ for (const resolve of this.commerceAcknowledgements.values())
951
+ resolve(false);
952
+ this.commerceAcknowledgements.clear();
801
953
  this.clock.cancel(this.connectionTimer);
802
954
  this.clock.cancel(this.expiryTimer);
803
955
  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
  }
@@ -12,9 +12,13 @@ export class LiveGuideAdmission {
12
12
  this.pendingAdmission = null;
13
13
  }
14
14
  hasPendingLaunch() { return this.store.readLaunch() !== null; }
15
+ pendingLaunchId() { return this.store.readLaunch()?.intent.launchRequestId ?? null; }
15
16
  async launch(intent, signal) {
16
17
  if (intent.installKey !== this.identity.installKey || this.store.readLaunch())
17
18
  throw unavailable();
19
+ const measurement = this.legacy?.measurementIntent?.(intent.clientVersion);
20
+ if (measurement)
21
+ intent = { ...intent, guideMeasurement: measurement };
18
22
  const subject = this.store.readSubject();
19
23
  const admission = this.runtime.admitLiveGuide(intent, subject?.credential, signal);
20
24
  this.pendingAdmission = admission;
@@ -49,13 +53,13 @@ export class LiveGuideAdmission {
49
53
  }
50
54
  if (signal.aborted)
51
55
  throw unavailable();
52
- const launch = await this.runtime.launchLiveGuide(intent, grant, signal);
56
+ const launch = await this.runtime.launchLiveGuide(intent, grant, signal, ...(intent.guideMeasurement ? [this.legacy?.measurementBridge?.(intent)] : []));
53
57
  if (launch.projectId !== this.identity.projectId || launch.guideId !== this.identity.guideId ||
54
58
  launch.subjectId !== subjectId || launch.launchRequestId !== intent.launchRequestId)
55
59
  throw unavailable();
56
60
  return launch;
57
61
  }
58
- async end(signal) {
62
+ async end(signal, expectedLaunchRequestId) {
59
63
  if (this.pendingAdmission) {
60
64
  await this.pendingAdmission.catch(() => undefined);
61
65
  await Promise.resolve();
@@ -63,6 +67,10 @@ export class LiveGuideAdmission {
63
67
  const record = this.store.readLaunch();
64
68
  if (!record)
65
69
  return;
70
+ // Ownership is captured by the widget before navigation releases its lock.
71
+ // A delayed cleanup must never adopt a replacement launch from another tab.
72
+ if (expectedLaunchRequestId !== undefined && record.intent.launchRequestId !== expectedLaunchRequestId)
73
+ return;
66
74
  const intent = record.intent;
67
75
  // A published model change shares the same visitor store. Retire the exact
68
76
  // 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 {};