@retion-miniapp/sdk 0.18.0 → 0.19.1

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.18.0" ;
200
+ var SDK_VERSION = "0.19.1" ;
201
201
 
202
202
  // src/apis/app.ts
203
203
  var closeApp = () => call("closeApp");
@@ -345,6 +345,7 @@ var announce = () => {
345
345
  return false;
346
346
  }
347
347
  last_announced_at = Date.now();
348
+ agentLog("setAgent \u2192", `agent_id=${current.agent_id}`);
348
349
  host.ReactNativeWebView.postMessage(JSON.stringify({
349
350
  type: AGENT_EVENT_TYPE,
350
351
  app_id: current.app_id,
@@ -443,6 +444,212 @@ var onAgentContext = (listener, options = {}) => {
443
444
  window.addEventListener("pagehide", off);
444
445
  return off;
445
446
  };
447
+ var AGENT_BRIDGE_VERSION = 1;
448
+ var AGENT_MESSAGE_EVENT_TYPE = "retion.ai.message";
449
+ var AGENT_STOPPED_EVENT_TYPE = "retion.ai.stopped";
450
+ var AGENT_PAYLOAD_META_EVENT_TYPE = "retion.ai.payload_meta";
451
+ var AGENT_PAYLOAD_META_RESULT_EVENT_TYPE = "retion.ai.payload_meta_result";
452
+ var agent_debug = true;
453
+ var setAgentBridgeDebug = (enabled) => {
454
+ agent_debug = enabled;
455
+ };
456
+ var agentLog = (...args) => {
457
+ if (!agent_debug) {
458
+ return;
459
+ }
460
+ let logger = console;
461
+ try {
462
+ if (typeof window !== "undefined" && window.parent && window.parent !== window) {
463
+ logger = window.parent.console;
464
+ }
465
+ } catch {
466
+ }
467
+ logger.log(`[retion-miniapp:agent${(current == null ? void 0 : current.app_id) ? `:${current.app_id}` : ""}]`, ...args);
468
+ };
469
+ var preview = (value) => {
470
+ try {
471
+ const text = JSON.stringify(value);
472
+ return text && text.length > 500 ? `${text.slice(0, 500)}\u2026` : text;
473
+ } catch {
474
+ return String(value);
475
+ }
476
+ };
477
+ var SENT_LIMIT = 50;
478
+ var sent_mids = [];
479
+ var sent_metas = [];
480
+ var remember = (list, value) => {
481
+ list.push(value);
482
+ if (list.length > SENT_LIMIT) {
483
+ list.shift();
484
+ }
485
+ };
486
+ var rawMessages = (raw) => {
487
+ const entry = raw && typeof raw === "object" ? raw : null;
488
+ if (!entry) {
489
+ return [];
490
+ }
491
+ const pick = (x, key) => x && typeof x === "object" ? x[key] : void 0;
492
+ const list = [
493
+ ...Array.isArray(entry.new_messages) ? entry.new_messages.map((x) => pick(x, "message")) : [entry.message],
494
+ ...Array.isArray(entry.update_messages) ? entry.update_messages.map((x) => {
495
+ var _a;
496
+ return (_a = pick(x, "update_message")) != null ? _a : x;
497
+ }) : [entry.update_message]
498
+ ];
499
+ return list.filter((m) => !!m && typeof m === "object");
500
+ };
501
+ var isEchoMessage = (m) => {
502
+ var _a, _b;
503
+ const mid = String((_b = (_a = m.message_mid) != null ? _a : m._id) != null ? _b : "");
504
+ if (mid && sent_mids.includes(mid)) {
505
+ return true;
506
+ }
507
+ return m.payload_meta != null && sent_metas.includes(JSON.stringify(m.payload_meta));
508
+ };
509
+ var isEchoOnly = (raw) => {
510
+ const messages = rawMessages(raw);
511
+ return messages.length > 0 && messages.every(isEchoMessage);
512
+ };
513
+ var listenHostEvent = (type, app_id, dedupeKey, handle) => {
514
+ if (typeof window === "undefined") {
515
+ return () => {
516
+ };
517
+ }
518
+ const seen = [];
519
+ const handler = (event) => {
520
+ if (event.source) {
521
+ return;
522
+ }
523
+ const data = parseEventData(event.data);
524
+ if (!data || data.type !== type) {
525
+ return;
526
+ }
527
+ const target_app = app_id != null ? app_id : current == null ? void 0 : current.app_id;
528
+ if (target_app && data.app_id && data.app_id !== target_app) {
529
+ agentLog(`${type}: b\u1ECF (c\u1EE7a app ${String(data.app_id)})`);
530
+ return;
531
+ }
532
+ if (!data.app_id && data.agent_id && (current == null ? void 0 : current.agent_id) && data.agent_id !== current.agent_id) {
533
+ agentLog(`${type}: b\u1ECF (c\u1EE7a agent ${String(data.agent_id)})`);
534
+ return;
535
+ }
536
+ const key = dedupeKey(data);
537
+ if (key !== null) {
538
+ if (seen.includes(key)) {
539
+ return;
540
+ }
541
+ seen.push(key);
542
+ if (seen.length > DEDUPE_LIMIT) {
543
+ seen.shift();
544
+ }
545
+ }
546
+ try {
547
+ handle(data);
548
+ } catch (error) {
549
+ console.error(`[retion-miniapp] ${type} listener l\u1ED7i`, error);
550
+ }
551
+ };
552
+ const targets = getEventWindows().flatMap((w) => [w, w.document]);
553
+ targets.forEach((target) => target.addEventListener("message", handler));
554
+ let active = true;
555
+ const off = () => {
556
+ if (!active) {
557
+ return;
558
+ }
559
+ active = false;
560
+ targets.forEach((target) => target.removeEventListener("message", handler));
561
+ window.removeEventListener("pagehide", off);
562
+ };
563
+ window.addEventListener("pagehide", off);
564
+ return off;
565
+ };
566
+ var onAgentMessage = (listener, options = {}) => listenHostEvent(
567
+ AGENT_MESSAGE_EVENT_TYPE,
568
+ options.app_id,
569
+ (data) => {
570
+ var _a, _b;
571
+ return `${String((_a = data.fb_client_id) != null ? _a : "")}:${JSON.stringify((_b = data.raw) != null ? _b : null)}`;
572
+ },
573
+ (data) => {
574
+ if (!options.includeEcho && isEchoOnly(data.raw)) {
575
+ agentLog("message: b\u1ECF echo tin m\xECnh v\u1EEBa g\u1EEDi", preview(data.raw));
576
+ return;
577
+ }
578
+ agentLog("message \u2190", `v${String(data.v)}`, `fb_client_id=${String(data.fb_client_id)}`, preview(data.raw));
579
+ const message = { ...data };
580
+ delete message.type;
581
+ listener(message);
582
+ }
583
+ );
584
+ var onAgentStop = (listener, options = {}) => {
585
+ let last_at = 0;
586
+ return listenHostEvent(AGENT_STOPPED_EVENT_TYPE, options.app_id, () => null, (data) => {
587
+ const now = Date.now();
588
+ if (now - last_at < 50) {
589
+ return;
590
+ }
591
+ last_at = now;
592
+ agentLog("stopped \u2190", `source=${String(data.source)}`);
593
+ const event = { ...data };
594
+ delete event.type;
595
+ listener(event);
596
+ });
597
+ };
598
+ var request_seq = 0;
599
+ var sendPayloadMeta = (payload_meta, options = {}) => {
600
+ if (!payload_meta || typeof payload_meta !== "object" || Array.isArray(payload_meta)) {
601
+ agentLog("sendPayloadMeta: payload_meta ph\u1EA3i l\xE0 object", preview(payload_meta));
602
+ return Promise.resolve({ ok: false, error: "invalid" });
603
+ }
604
+ const host = getHostWindow();
605
+ if (!host) {
606
+ agentLog("sendPayloadMeta: kh\xF4ng ch\u1EA1y trong app ch\u1EE7 \u2192 b\u1ECF");
607
+ return Promise.resolve({ ok: false, error: "no_host" });
608
+ }
609
+ request_seq += 1;
610
+ const request_id = `pm_${Date.now().toString(36)}_${request_seq}`;
611
+ const meta_key = JSON.stringify(payload_meta);
612
+ remember(sent_metas, meta_key);
613
+ return new Promise((resolve) => {
614
+ var _a, _b, _c;
615
+ let done = false;
616
+ const finish = (result) => {
617
+ if (done) {
618
+ return;
619
+ }
620
+ done = true;
621
+ clearTimeout(timer);
622
+ off();
623
+ if (result.message_mid) {
624
+ remember(sent_mids, result.message_mid);
625
+ }
626
+ agentLog("payload_meta_result \u2190", request_id, preview(result));
627
+ resolve(result);
628
+ };
629
+ const off = listenHostEvent(AGENT_PAYLOAD_META_RESULT_EVENT_TYPE, void 0, () => null, (data) => {
630
+ if (data.request_id !== request_id) {
631
+ return;
632
+ }
633
+ const result = { ...data };
634
+ delete result.type;
635
+ delete result.v;
636
+ delete result.request_id;
637
+ finish({ ...result, ok: !!data.ok });
638
+ });
639
+ const timer = setTimeout(() => finish({ ok: false, error: "timeout" }), (_a = options.timeout_ms) != null ? _a : 15e3);
640
+ agentLog("payload_meta \u2192", request_id, `text=${JSON.stringify((_b = options.text) != null ? _b : "")}`, preview(payload_meta));
641
+ host.ReactNativeWebView.postMessage(JSON.stringify({
642
+ type: AGENT_PAYLOAD_META_EVENT_TYPE,
643
+ v: AGENT_BRIDGE_VERSION,
644
+ request_id,
645
+ ...(current == null ? void 0 : current.app_id) ? { app_id: current.app_id } : null,
646
+ ...options.agent_id || (current == null ? void 0 : current.agent_id) ? { agent_id: options.agent_id || (current == null ? void 0 : current.agent_id) } : null,
647
+ text: (_c = options.text) != null ? _c : "",
648
+ payload_meta
649
+ }));
650
+ });
651
+ };
652
+ var getAgentBridgeVersion = () => AGENT_BRIDGE_VERSION;
446
653
 
447
654
  // src/apis/location.ts
448
655
  var getCurrentLocation = (options = {}) => {
@@ -483,7 +690,12 @@ if (typeof window !== "undefined") {
483
690
  }
484
691
  }
485
692
 
693
+ exports.AGENT_BRIDGE_VERSION = AGENT_BRIDGE_VERSION;
486
694
  exports.AGENT_CONTEXT_EVENT_TYPE = AGENT_CONTEXT_EVENT_TYPE;
695
+ exports.AGENT_MESSAGE_EVENT_TYPE = AGENT_MESSAGE_EVENT_TYPE;
696
+ exports.AGENT_PAYLOAD_META_EVENT_TYPE = AGENT_PAYLOAD_META_EVENT_TYPE;
697
+ exports.AGENT_PAYLOAD_META_RESULT_EVENT_TYPE = AGENT_PAYLOAD_META_RESULT_EVENT_TYPE;
698
+ exports.AGENT_STOPPED_EVENT_TYPE = AGENT_STOPPED_EVENT_TYPE;
487
699
  exports.CodeType = CodeType;
488
700
  exports.ErrorCode = ErrorCode;
489
701
  exports.HapticType = HapticType;
@@ -496,6 +708,7 @@ exports.ToastType = ToastType;
496
708
  exports.authenticateBiometric = authenticateBiometric;
497
709
  exports.closeApp = closeApp;
498
710
  exports.getAccessToken = getAccessToken;
711
+ exports.getAgentBridgeVersion = getAgentBridgeVersion;
499
712
  exports.getBiometricAvailability = getBiometricAvailability;
500
713
  exports.getCurrentLocation = getCurrentLocation;
501
714
  exports.getRouteParams = getRouteParams;
@@ -504,6 +717,8 @@ exports.getUploadCapabilities = getUploadCapabilities;
504
717
  exports.haptic = haptic;
505
718
  exports.isInHost = isInHost;
506
719
  exports.onAgentContext = onAgentContext;
720
+ exports.onAgentMessage = onAgentMessage;
721
+ exports.onAgentStop = onAgentStop;
507
722
  exports.onAppPaused = onAppPaused;
508
723
  exports.onAppResumed = onAppResumed;
509
724
  exports.onContextChange = onContextChange;
@@ -516,7 +731,9 @@ exports.openScreen = openScreen;
516
731
  exports.printImage = printImage;
517
732
  exports.requestPayment = requestPayment;
518
733
  exports.scanCode = scanCode;
734
+ exports.sendPayloadMeta = sendPayloadMeta;
519
735
  exports.setAgent = setAgent;
736
+ exports.setAgentBridgeDebug = setAgentBridgeDebug;
520
737
  exports.showToast = showToast;
521
738
  exports.startScanner = startScanner;
522
739
  exports.startSpeech = startSpeech;