@retion-miniapp/sdk 0.17.1 → 0.19.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.
package/dist/index.cjs CHANGED
@@ -197,7 +197,7 @@ var on = (event, listener) => {
197
197
  };
198
198
 
199
199
  // src/version.ts
200
- var SDK_VERSION = "0.17.1" ;
200
+ var SDK_VERSION = "0.19.0" ;
201
201
 
202
202
  // src/apis/app.ts
203
203
  var closeApp = () => call("closeApp");
@@ -371,6 +371,250 @@ var setAgent = (options) => {
371
371
  listen();
372
372
  return announce();
373
373
  };
374
+ var AGENT_CONTEXT_EVENT_TYPE = "retion.ai.context";
375
+ var getEventWindows = () => {
376
+ if (typeof window === "undefined") {
377
+ return [];
378
+ }
379
+ const list = [window];
380
+ try {
381
+ if (window.parent && window.parent !== window) {
382
+ void window.parent.document;
383
+ list.push(window.parent);
384
+ }
385
+ } catch {
386
+ }
387
+ return list;
388
+ };
389
+ var parseEventData = (raw) => {
390
+ let data = raw;
391
+ if (typeof data === "string") {
392
+ try {
393
+ data = JSON.parse(data);
394
+ } catch {
395
+ return null;
396
+ }
397
+ }
398
+ return data && typeof data === "object" && !Array.isArray(data) ? data : null;
399
+ };
400
+ var DEDUPE_LIMIT = 50;
401
+ var onAgentContext = (listener, options = {}) => {
402
+ const seen = [];
403
+ const handler = (event) => {
404
+ var _a, _b, _c;
405
+ if (event.source) {
406
+ return;
407
+ }
408
+ const data = parseEventData(event.data);
409
+ if (!data || data.type !== AGENT_CONTEXT_EVENT_TYPE) {
410
+ return;
411
+ }
412
+ const target_app = (_a = options.app_id) != null ? _a : current == null ? void 0 : current.app_id;
413
+ if (target_app && data.app_id !== target_app) {
414
+ return;
415
+ }
416
+ const key = `${String((_b = data.message_mid) != null ? _b : "")}:${JSON.stringify((_c = data.payload_context) != null ? _c : null)}`;
417
+ if (seen.includes(key)) {
418
+ return;
419
+ }
420
+ seen.push(key);
421
+ if (seen.length > DEDUPE_LIMIT) {
422
+ seen.shift();
423
+ }
424
+ const context = { ...data };
425
+ delete context.type;
426
+ try {
427
+ listener(context);
428
+ } catch (error) {
429
+ console.error("[retion-miniapp] onAgentContext listener l\u1ED7i", error);
430
+ }
431
+ };
432
+ const targets = getEventWindows().flatMap((w) => [w, w.document]);
433
+ targets.forEach((target) => target.addEventListener("message", handler));
434
+ let active = true;
435
+ const off = () => {
436
+ if (!active) {
437
+ return;
438
+ }
439
+ active = false;
440
+ targets.forEach((target) => target.removeEventListener("message", handler));
441
+ window.removeEventListener("pagehide", off);
442
+ };
443
+ window.addEventListener("pagehide", off);
444
+ return off;
445
+ };
446
+ var AGENT_BRIDGE_VERSION = 1;
447
+ var AGENT_MESSAGE_EVENT_TYPE = "retion.ai.message";
448
+ var AGENT_STOPPED_EVENT_TYPE = "retion.ai.stopped";
449
+ var AGENT_PAYLOAD_META_EVENT_TYPE = "retion.ai.payload_meta";
450
+ var AGENT_PAYLOAD_META_RESULT_EVENT_TYPE = "retion.ai.payload_meta_result";
451
+ var SENT_LIMIT = 50;
452
+ var sent_mids = [];
453
+ var sent_metas = [];
454
+ var remember = (list, value) => {
455
+ list.push(value);
456
+ if (list.length > SENT_LIMIT) {
457
+ list.shift();
458
+ }
459
+ };
460
+ var rawMessages = (raw) => {
461
+ const entry = raw && typeof raw === "object" ? raw : null;
462
+ if (!entry) {
463
+ return [];
464
+ }
465
+ const pick = (x, key) => x && typeof x === "object" ? x[key] : void 0;
466
+ const list = [
467
+ ...Array.isArray(entry.new_messages) ? entry.new_messages.map((x) => pick(x, "message")) : [entry.message],
468
+ ...Array.isArray(entry.update_messages) ? entry.update_messages.map((x) => {
469
+ var _a;
470
+ return (_a = pick(x, "update_message")) != null ? _a : x;
471
+ }) : [entry.update_message]
472
+ ];
473
+ return list.filter((m) => !!m && typeof m === "object");
474
+ };
475
+ var isEchoMessage = (m) => {
476
+ var _a, _b;
477
+ const mid = String((_b = (_a = m.message_mid) != null ? _a : m._id) != null ? _b : "");
478
+ if (mid && sent_mids.includes(mid)) {
479
+ return true;
480
+ }
481
+ return m.payload_meta != null && sent_metas.includes(JSON.stringify(m.payload_meta));
482
+ };
483
+ var isEchoOnly = (raw) => {
484
+ const messages = rawMessages(raw);
485
+ return messages.length > 0 && messages.every(isEchoMessage);
486
+ };
487
+ var listenHostEvent = (type, app_id, dedupeKey, handle) => {
488
+ if (typeof window === "undefined") {
489
+ return () => {
490
+ };
491
+ }
492
+ const seen = [];
493
+ const handler = (event) => {
494
+ if (event.source) {
495
+ return;
496
+ }
497
+ const data = parseEventData(event.data);
498
+ if (!data || data.type !== type) {
499
+ return;
500
+ }
501
+ const target_app = app_id != null ? app_id : current == null ? void 0 : current.app_id;
502
+ if (target_app && data.app_id && data.app_id !== target_app) {
503
+ return;
504
+ }
505
+ if (!data.app_id && data.agent_id && (current == null ? void 0 : current.agent_id) && data.agent_id !== current.agent_id) {
506
+ return;
507
+ }
508
+ const key = dedupeKey(data);
509
+ if (key !== null) {
510
+ if (seen.includes(key)) {
511
+ return;
512
+ }
513
+ seen.push(key);
514
+ if (seen.length > DEDUPE_LIMIT) {
515
+ seen.shift();
516
+ }
517
+ }
518
+ try {
519
+ handle(data);
520
+ } catch (error) {
521
+ console.error(`[retion-miniapp] ${type} listener l\u1ED7i`, error);
522
+ }
523
+ };
524
+ const targets = getEventWindows().flatMap((w) => [w, w.document]);
525
+ targets.forEach((target) => target.addEventListener("message", handler));
526
+ let active = true;
527
+ const off = () => {
528
+ if (!active) {
529
+ return;
530
+ }
531
+ active = false;
532
+ targets.forEach((target) => target.removeEventListener("message", handler));
533
+ window.removeEventListener("pagehide", off);
534
+ };
535
+ window.addEventListener("pagehide", off);
536
+ return off;
537
+ };
538
+ var onAgentMessage = (listener, options = {}) => listenHostEvent(
539
+ AGENT_MESSAGE_EVENT_TYPE,
540
+ options.app_id,
541
+ (data) => {
542
+ var _a, _b;
543
+ return `${String((_a = data.fb_client_id) != null ? _a : "")}:${JSON.stringify((_b = data.raw) != null ? _b : null)}`;
544
+ },
545
+ (data) => {
546
+ if (!options.includeEcho && isEchoOnly(data.raw)) {
547
+ return;
548
+ }
549
+ const message = { ...data };
550
+ delete message.type;
551
+ listener(message);
552
+ }
553
+ );
554
+ var onAgentStop = (listener, options = {}) => {
555
+ let last_at = 0;
556
+ return listenHostEvent(AGENT_STOPPED_EVENT_TYPE, options.app_id, () => null, (data) => {
557
+ const now = Date.now();
558
+ if (now - last_at < 50) {
559
+ return;
560
+ }
561
+ last_at = now;
562
+ const event = { ...data };
563
+ delete event.type;
564
+ listener(event);
565
+ });
566
+ };
567
+ var request_seq = 0;
568
+ var sendPayloadMeta = (payload_meta, options = {}) => {
569
+ if (!payload_meta || typeof payload_meta !== "object" || Array.isArray(payload_meta)) {
570
+ return Promise.resolve({ ok: false, error: "invalid" });
571
+ }
572
+ const host = getHostWindow();
573
+ if (!host) {
574
+ return Promise.resolve({ ok: false, error: "no_host" });
575
+ }
576
+ request_seq += 1;
577
+ const request_id = `pm_${Date.now().toString(36)}_${request_seq}`;
578
+ const meta_key = JSON.stringify(payload_meta);
579
+ remember(sent_metas, meta_key);
580
+ return new Promise((resolve) => {
581
+ var _a, _b;
582
+ let done = false;
583
+ const finish = (result) => {
584
+ if (done) {
585
+ return;
586
+ }
587
+ done = true;
588
+ clearTimeout(timer);
589
+ off();
590
+ if (result.message_mid) {
591
+ remember(sent_mids, result.message_mid);
592
+ }
593
+ resolve(result);
594
+ };
595
+ const off = listenHostEvent(AGENT_PAYLOAD_META_RESULT_EVENT_TYPE, void 0, () => null, (data) => {
596
+ if (data.request_id !== request_id) {
597
+ return;
598
+ }
599
+ const result = { ...data };
600
+ delete result.type;
601
+ delete result.v;
602
+ delete result.request_id;
603
+ finish({ ...result, ok: !!data.ok });
604
+ });
605
+ const timer = setTimeout(() => finish({ ok: false, error: "timeout" }), (_a = options.timeout_ms) != null ? _a : 15e3);
606
+ host.ReactNativeWebView.postMessage(JSON.stringify({
607
+ type: AGENT_PAYLOAD_META_EVENT_TYPE,
608
+ v: AGENT_BRIDGE_VERSION,
609
+ request_id,
610
+ ...(current == null ? void 0 : current.app_id) ? { app_id: current.app_id } : null,
611
+ ...options.agent_id || (current == null ? void 0 : current.agent_id) ? { agent_id: options.agent_id || (current == null ? void 0 : current.agent_id) } : null,
612
+ text: (_b = options.text) != null ? _b : "",
613
+ payload_meta
614
+ }));
615
+ });
616
+ };
617
+ var getAgentBridgeVersion = () => AGENT_BRIDGE_VERSION;
374
618
 
375
619
  // src/apis/location.ts
376
620
  var getCurrentLocation = (options = {}) => {
@@ -411,6 +655,12 @@ if (typeof window !== "undefined") {
411
655
  }
412
656
  }
413
657
 
658
+ exports.AGENT_BRIDGE_VERSION = AGENT_BRIDGE_VERSION;
659
+ exports.AGENT_CONTEXT_EVENT_TYPE = AGENT_CONTEXT_EVENT_TYPE;
660
+ exports.AGENT_MESSAGE_EVENT_TYPE = AGENT_MESSAGE_EVENT_TYPE;
661
+ exports.AGENT_PAYLOAD_META_EVENT_TYPE = AGENT_PAYLOAD_META_EVENT_TYPE;
662
+ exports.AGENT_PAYLOAD_META_RESULT_EVENT_TYPE = AGENT_PAYLOAD_META_RESULT_EVENT_TYPE;
663
+ exports.AGENT_STOPPED_EVENT_TYPE = AGENT_STOPPED_EVENT_TYPE;
414
664
  exports.CodeType = CodeType;
415
665
  exports.ErrorCode = ErrorCode;
416
666
  exports.HapticType = HapticType;
@@ -423,6 +673,7 @@ exports.ToastType = ToastType;
423
673
  exports.authenticateBiometric = authenticateBiometric;
424
674
  exports.closeApp = closeApp;
425
675
  exports.getAccessToken = getAccessToken;
676
+ exports.getAgentBridgeVersion = getAgentBridgeVersion;
426
677
  exports.getBiometricAvailability = getBiometricAvailability;
427
678
  exports.getCurrentLocation = getCurrentLocation;
428
679
  exports.getRouteParams = getRouteParams;
@@ -430,6 +681,9 @@ exports.getToken = getToken;
430
681
  exports.getUploadCapabilities = getUploadCapabilities;
431
682
  exports.haptic = haptic;
432
683
  exports.isInHost = isInHost;
684
+ exports.onAgentContext = onAgentContext;
685
+ exports.onAgentMessage = onAgentMessage;
686
+ exports.onAgentStop = onAgentStop;
433
687
  exports.onAppPaused = onAppPaused;
434
688
  exports.onAppResumed = onAppResumed;
435
689
  exports.onContextChange = onContextChange;
@@ -442,6 +696,7 @@ exports.openScreen = openScreen;
442
696
  exports.printImage = printImage;
443
697
  exports.requestPayment = requestPayment;
444
698
  exports.scanCode = scanCode;
699
+ exports.sendPayloadMeta = sendPayloadMeta;
445
700
  exports.setAgent = setAgent;
446
701
  exports.showToast = showToast;
447
702
  exports.startScanner = startScanner;