@retion-miniapp/sdk 0.19.1 → 0.19.3

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.d.cts CHANGED
@@ -528,7 +528,41 @@ declare const AGENT_STOPPED_EVENT_TYPE = "retion.ai.stopped";
528
528
  declare const AGENT_PAYLOAD_META_EVENT_TYPE = "retion.ai.payload_meta";
529
529
  declare const AGENT_PAYLOAD_META_RESULT_EVENT_TYPE = "retion.ai.payload_meta_result";
530
530
  /** Tin socket hội thoại Agent, app chủ đẩy nguyên vẹn */
531
- interface AgentMessage<TRaw = unknown> {
531
+ /**
532
+ * Tin nhắn trong gói socket hội thoại Agent — chỉ khai báo field hay dùng, còn lại giữ nguyên như server trả.
533
+ * message_type: 'client' = tin agent, 'page' = tin mình (echo), 'system' = tin hệ thống.
534
+ */
535
+ interface AgentSocketMessageItem {
536
+ _id?: string;
537
+ message_mid?: string;
538
+ message_text?: string;
539
+ message_type?: 'client' | 'page' | 'system' | (string & {});
540
+ fb_client_id?: string;
541
+ createdAt?: string;
542
+ updatedAt?: string;
543
+ /** Ngữ cảnh agent gắn vào câu hỏi (vd bộ lọc) */
544
+ payload_context?: unknown;
545
+ /** Dữ liệu mini app gửi bằng sendPayloadMeta */
546
+ payload_meta?: unknown;
547
+ message_attachments?: unknown[];
548
+ [key: string]: unknown;
549
+ }
550
+ /**
551
+ * 1 gói socket hội thoại Agent (đúng gói server bắn). Thường có `message` HOẶC `update_message`, kèm `conversation`
552
+ * và `raw` (webhook gốc). Field khác giữ nguyên.
553
+ */
554
+ interface AgentSocketPacket {
555
+ /** Tin mới */
556
+ message?: AgentSocketMessageItem;
557
+ /** Tin cũ được cập nhật (vd gắn payload_context) */
558
+ update_message?: AgentSocketMessageItem;
559
+ /** Trạng thái hội thoại sau gói này */
560
+ conversation?: Record<string, unknown>;
561
+ /** Webhook gốc server nhận */
562
+ raw?: unknown;
563
+ [key: string]: unknown;
564
+ }
565
+ interface AgentMessage<TRaw = AgentSocketPacket> {
532
566
  /** Phiên bản giao thức của app chủ */
533
567
  v: number;
534
568
  app_id: string;
@@ -564,7 +598,7 @@ interface SendPayloadMetaResult {
564
598
  /** 'no_host' | 'unsupported' | 'invalid' | 'no_agent' | 'no_conversation' | 'send_failed' | 'timeout' … */
565
599
  error?: string;
566
600
  }
567
- /** Bật / tắt log cầu nối Agent (mặc định bật). Log ra console cửa sổ cha nếu là app con trong iframe cùng origin. */
601
+ /** Bật / tắt log cầu nối Agent (mặc định TẮT — log có nội dung hội thoại, chỉ bật khi debug). Log ra console cửa sổ cha nếu là app con trong iframe cùng origin. */
568
602
  declare const setAgentBridgeDebug: (enabled: boolean) => void;
569
603
  /**
570
604
  * Nghe MỌI gói socket hội thoại Agent của mini app này — app chủ đẩy nguyên vẹn (step, trả lời…), mini app tự chọn.
@@ -574,7 +608,7 @@ declare const setAgentBridgeDebug: (enabled: boolean) => void;
574
608
  * @example
575
609
  * const off = onAgentMessage(({ raw }) => handleAgentStep(raw));
576
610
  */
577
- declare const onAgentMessage: <TRaw = unknown>(listener: (message: AgentMessage<TRaw>) => void, options?: OnAgentMessageOptions) => (() => void);
611
+ declare const onAgentMessage: <TRaw = AgentSocketPacket>(listener: (message: AgentMessage<TRaw>) => void, options?: OnAgentMessageOptions) => (() => void);
578
612
  /**
579
613
  * Nghe khi người dùng dừng lượt Agent (■ ở mini app hoặc nút dừng trong chat app chủ) → mini app ngừng thao tác.
580
614
  *
@@ -729,4 +763,4 @@ declare const getUploadCapabilities: () => Promise<UploadCapabilities>;
729
763
  */
730
764
  declare const uploadFiles: (options?: UploadFilesOptions) => Promise<UploadFilesResult>;
731
765
 
732
- export { AGENT_BRIDGE_VERSION, AGENT_CONTEXT_EVENT_TYPE, AGENT_MESSAGE_EVENT_TYPE, AGENT_PAYLOAD_META_EVENT_TYPE, AGENT_PAYLOAD_META_RESULT_EVENT_TYPE, AGENT_STOPPED_EVENT_TYPE, type AgentContext, type AgentInfo, type AgentMessage, type AgentStopEvent, type BiometricAvailability, type BiometricRequest, type BiometricResult, type BiometryType, type ChangelogPopupOptions, CodeType, ErrorCode, HapticType, type LocationOptions, type LocationResult, MiniAppError, type OnAgentContextOptions, type OnAgentMessageOptions, type OpenScreenRequest, type PaymentRequest, type PaymentResult, type PrintImageRequest, type RouteParams, SDK_VERSION, type ScanCodeOptions, type ScanResult, type ScannerEnd, ScannerEndReason, ScannerEvent, type ScannerEvents, ScannerLayout, type SendPayloadMetaOptions, type SendPayloadMetaResult, type SetAgentOptions, type ShowToastOptions, type SpeechError, type SpeechEvents, type SpeechOptions, type SpeechResult, type StartScannerOptions, ToastType, type TokenType, type UploadCapabilities, type UploadFileType, type UploadFilesOptions, type UploadFilesResult, type UploadProcessOptions, type UploadProgress, type UploadTarget, type UploadUrlTarget, type UploadedFile, authenticateBiometric, closeApp, getAccessToken, getAgentBridgeVersion, getBiometricAvailability, getCurrentLocation, getRouteParams, getToken, getUploadCapabilities, haptic, isInHost, onAgentContext, onAgentMessage, onAgentStop, onAppPaused, onAppResumed, onContextChange, onScanner, onSpeech, openChangelogHistory, openChangelogPopup, openLink, openScreen, printImage, requestPayment, scanCode, sendPayloadMeta, setAgent, setAgentBridgeDebug, showToast, startScanner, startSpeech, stopScanner, stopSpeech, uploadFiles };
766
+ export { AGENT_BRIDGE_VERSION, AGENT_CONTEXT_EVENT_TYPE, AGENT_MESSAGE_EVENT_TYPE, AGENT_PAYLOAD_META_EVENT_TYPE, AGENT_PAYLOAD_META_RESULT_EVENT_TYPE, AGENT_STOPPED_EVENT_TYPE, type AgentContext, type AgentInfo, type AgentMessage, type AgentSocketMessageItem, type AgentSocketPacket, type AgentStopEvent, type BiometricAvailability, type BiometricRequest, type BiometricResult, type BiometryType, type ChangelogPopupOptions, CodeType, ErrorCode, HapticType, type LocationOptions, type LocationResult, MiniAppError, type OnAgentContextOptions, type OnAgentMessageOptions, type OpenScreenRequest, type PaymentRequest, type PaymentResult, type PrintImageRequest, type RouteParams, SDK_VERSION, type ScanCodeOptions, type ScanResult, type ScannerEnd, ScannerEndReason, ScannerEvent, type ScannerEvents, ScannerLayout, type SendPayloadMetaOptions, type SendPayloadMetaResult, type SetAgentOptions, type ShowToastOptions, type SpeechError, type SpeechEvents, type SpeechOptions, type SpeechResult, type StartScannerOptions, ToastType, type TokenType, type UploadCapabilities, type UploadFileType, type UploadFilesOptions, type UploadFilesResult, type UploadProcessOptions, type UploadProgress, type UploadTarget, type UploadUrlTarget, type UploadedFile, authenticateBiometric, closeApp, getAccessToken, getAgentBridgeVersion, getBiometricAvailability, getCurrentLocation, getRouteParams, getToken, getUploadCapabilities, haptic, isInHost, onAgentContext, onAgentMessage, onAgentStop, onAppPaused, onAppResumed, onContextChange, onScanner, onSpeech, openChangelogHistory, openChangelogPopup, openLink, openScreen, printImage, requestPayment, scanCode, sendPayloadMeta, setAgent, setAgentBridgeDebug, showToast, startScanner, startSpeech, stopScanner, stopSpeech, uploadFiles };
package/dist/index.d.ts CHANGED
@@ -528,7 +528,41 @@ declare const AGENT_STOPPED_EVENT_TYPE = "retion.ai.stopped";
528
528
  declare const AGENT_PAYLOAD_META_EVENT_TYPE = "retion.ai.payload_meta";
529
529
  declare const AGENT_PAYLOAD_META_RESULT_EVENT_TYPE = "retion.ai.payload_meta_result";
530
530
  /** Tin socket hội thoại Agent, app chủ đẩy nguyên vẹn */
531
- interface AgentMessage<TRaw = unknown> {
531
+ /**
532
+ * Tin nhắn trong gói socket hội thoại Agent — chỉ khai báo field hay dùng, còn lại giữ nguyên như server trả.
533
+ * message_type: 'client' = tin agent, 'page' = tin mình (echo), 'system' = tin hệ thống.
534
+ */
535
+ interface AgentSocketMessageItem {
536
+ _id?: string;
537
+ message_mid?: string;
538
+ message_text?: string;
539
+ message_type?: 'client' | 'page' | 'system' | (string & {});
540
+ fb_client_id?: string;
541
+ createdAt?: string;
542
+ updatedAt?: string;
543
+ /** Ngữ cảnh agent gắn vào câu hỏi (vd bộ lọc) */
544
+ payload_context?: unknown;
545
+ /** Dữ liệu mini app gửi bằng sendPayloadMeta */
546
+ payload_meta?: unknown;
547
+ message_attachments?: unknown[];
548
+ [key: string]: unknown;
549
+ }
550
+ /**
551
+ * 1 gói socket hội thoại Agent (đúng gói server bắn). Thường có `message` HOẶC `update_message`, kèm `conversation`
552
+ * và `raw` (webhook gốc). Field khác giữ nguyên.
553
+ */
554
+ interface AgentSocketPacket {
555
+ /** Tin mới */
556
+ message?: AgentSocketMessageItem;
557
+ /** Tin cũ được cập nhật (vd gắn payload_context) */
558
+ update_message?: AgentSocketMessageItem;
559
+ /** Trạng thái hội thoại sau gói này */
560
+ conversation?: Record<string, unknown>;
561
+ /** Webhook gốc server nhận */
562
+ raw?: unknown;
563
+ [key: string]: unknown;
564
+ }
565
+ interface AgentMessage<TRaw = AgentSocketPacket> {
532
566
  /** Phiên bản giao thức của app chủ */
533
567
  v: number;
534
568
  app_id: string;
@@ -564,7 +598,7 @@ interface SendPayloadMetaResult {
564
598
  /** 'no_host' | 'unsupported' | 'invalid' | 'no_agent' | 'no_conversation' | 'send_failed' | 'timeout' … */
565
599
  error?: string;
566
600
  }
567
- /** Bật / tắt log cầu nối Agent (mặc định bật). Log ra console cửa sổ cha nếu là app con trong iframe cùng origin. */
601
+ /** Bật / tắt log cầu nối Agent (mặc định TẮT — log có nội dung hội thoại, chỉ bật khi debug). Log ra console cửa sổ cha nếu là app con trong iframe cùng origin. */
568
602
  declare const setAgentBridgeDebug: (enabled: boolean) => void;
569
603
  /**
570
604
  * Nghe MỌI gói socket hội thoại Agent của mini app này — app chủ đẩy nguyên vẹn (step, trả lời…), mini app tự chọn.
@@ -574,7 +608,7 @@ declare const setAgentBridgeDebug: (enabled: boolean) => void;
574
608
  * @example
575
609
  * const off = onAgentMessage(({ raw }) => handleAgentStep(raw));
576
610
  */
577
- declare const onAgentMessage: <TRaw = unknown>(listener: (message: AgentMessage<TRaw>) => void, options?: OnAgentMessageOptions) => (() => void);
611
+ declare const onAgentMessage: <TRaw = AgentSocketPacket>(listener: (message: AgentMessage<TRaw>) => void, options?: OnAgentMessageOptions) => (() => void);
578
612
  /**
579
613
  * Nghe khi người dùng dừng lượt Agent (■ ở mini app hoặc nút dừng trong chat app chủ) → mini app ngừng thao tác.
580
614
  *
@@ -729,4 +763,4 @@ declare const getUploadCapabilities: () => Promise<UploadCapabilities>;
729
763
  */
730
764
  declare const uploadFiles: (options?: UploadFilesOptions) => Promise<UploadFilesResult>;
731
765
 
732
- export { AGENT_BRIDGE_VERSION, AGENT_CONTEXT_EVENT_TYPE, AGENT_MESSAGE_EVENT_TYPE, AGENT_PAYLOAD_META_EVENT_TYPE, AGENT_PAYLOAD_META_RESULT_EVENT_TYPE, AGENT_STOPPED_EVENT_TYPE, type AgentContext, type AgentInfo, type AgentMessage, type AgentStopEvent, type BiometricAvailability, type BiometricRequest, type BiometricResult, type BiometryType, type ChangelogPopupOptions, CodeType, ErrorCode, HapticType, type LocationOptions, type LocationResult, MiniAppError, type OnAgentContextOptions, type OnAgentMessageOptions, type OpenScreenRequest, type PaymentRequest, type PaymentResult, type PrintImageRequest, type RouteParams, SDK_VERSION, type ScanCodeOptions, type ScanResult, type ScannerEnd, ScannerEndReason, ScannerEvent, type ScannerEvents, ScannerLayout, type SendPayloadMetaOptions, type SendPayloadMetaResult, type SetAgentOptions, type ShowToastOptions, type SpeechError, type SpeechEvents, type SpeechOptions, type SpeechResult, type StartScannerOptions, ToastType, type TokenType, type UploadCapabilities, type UploadFileType, type UploadFilesOptions, type UploadFilesResult, type UploadProcessOptions, type UploadProgress, type UploadTarget, type UploadUrlTarget, type UploadedFile, authenticateBiometric, closeApp, getAccessToken, getAgentBridgeVersion, getBiometricAvailability, getCurrentLocation, getRouteParams, getToken, getUploadCapabilities, haptic, isInHost, onAgentContext, onAgentMessage, onAgentStop, onAppPaused, onAppResumed, onContextChange, onScanner, onSpeech, openChangelogHistory, openChangelogPopup, openLink, openScreen, printImage, requestPayment, scanCode, sendPayloadMeta, setAgent, setAgentBridgeDebug, showToast, startScanner, startSpeech, stopScanner, stopSpeech, uploadFiles };
766
+ export { AGENT_BRIDGE_VERSION, AGENT_CONTEXT_EVENT_TYPE, AGENT_MESSAGE_EVENT_TYPE, AGENT_PAYLOAD_META_EVENT_TYPE, AGENT_PAYLOAD_META_RESULT_EVENT_TYPE, AGENT_STOPPED_EVENT_TYPE, type AgentContext, type AgentInfo, type AgentMessage, type AgentSocketMessageItem, type AgentSocketPacket, type AgentStopEvent, type BiometricAvailability, type BiometricRequest, type BiometricResult, type BiometryType, type ChangelogPopupOptions, CodeType, ErrorCode, HapticType, type LocationOptions, type LocationResult, MiniAppError, type OnAgentContextOptions, type OnAgentMessageOptions, type OpenScreenRequest, type PaymentRequest, type PaymentResult, type PrintImageRequest, type RouteParams, SDK_VERSION, type ScanCodeOptions, type ScanResult, type ScannerEnd, ScannerEndReason, ScannerEvent, type ScannerEvents, ScannerLayout, type SendPayloadMetaOptions, type SendPayloadMetaResult, type SetAgentOptions, type ShowToastOptions, type SpeechError, type SpeechEvents, type SpeechOptions, type SpeechResult, type StartScannerOptions, ToastType, type TokenType, type UploadCapabilities, type UploadFileType, type UploadFilesOptions, type UploadFilesResult, type UploadProcessOptions, type UploadProgress, type UploadTarget, type UploadUrlTarget, type UploadedFile, authenticateBiometric, closeApp, getAccessToken, getAgentBridgeVersion, getBiometricAvailability, getCurrentLocation, getRouteParams, getToken, getUploadCapabilities, haptic, isInHost, onAgentContext, onAgentMessage, onAgentStop, onAppPaused, onAppResumed, onContextChange, onScanner, onSpeech, openChangelogHistory, openChangelogPopup, openLink, openScreen, printImage, requestPayment, scanCode, sendPayloadMeta, setAgent, setAgentBridgeDebug, showToast, startScanner, startSpeech, stopScanner, stopSpeech, uploadFiles };
package/dist/index.js CHANGED
@@ -195,7 +195,7 @@ var on = (event, listener) => {
195
195
  };
196
196
 
197
197
  // src/version.ts
198
- var SDK_VERSION = "0.19.1" ;
198
+ var SDK_VERSION = "0.19.3" ;
199
199
 
200
200
  // src/apis/app.ts
201
201
  var closeApp = () => call("closeApp");
@@ -447,7 +447,7 @@ var AGENT_MESSAGE_EVENT_TYPE = "retion.ai.message";
447
447
  var AGENT_STOPPED_EVENT_TYPE = "retion.ai.stopped";
448
448
  var AGENT_PAYLOAD_META_EVENT_TYPE = "retion.ai.payload_meta";
449
449
  var AGENT_PAYLOAD_META_RESULT_EVENT_TYPE = "retion.ai.payload_meta_result";
450
- var agent_debug = true;
450
+ var agent_debug = false;
451
451
  var setAgentBridgeDebug = (enabled) => {
452
452
  agent_debug = enabled;
453
453
  };
@@ -504,6 +504,15 @@ var isEchoMessage = (m) => {
504
504
  }
505
505
  return m.payload_meta != null && sent_metas.includes(JSON.stringify(m.payload_meta));
506
506
  };
507
+ var pending_sends = 0;
508
+ var held_flushers = /* @__PURE__ */ new Set();
509
+ var isPossibleEcho = (raw) => rawMessages(raw).some((m) => m.message_type === "page");
510
+ var flushHeld = () => {
511
+ if (pending_sends > 0) {
512
+ return;
513
+ }
514
+ held_flushers.forEach((flush) => flush());
515
+ };
507
516
  var isEchoOnly = (raw) => {
508
517
  const messages = rawMessages(raw);
509
518
  return messages.length > 0 && messages.every(isEchoMessage);
@@ -561,14 +570,8 @@ var listenHostEvent = (type, app_id, dedupeKey, handle) => {
561
570
  window.addEventListener("pagehide", off);
562
571
  return off;
563
572
  };
564
- var onAgentMessage = (listener, options = {}) => listenHostEvent(
565
- AGENT_MESSAGE_EVENT_TYPE,
566
- options.app_id,
567
- (data) => {
568
- var _a, _b;
569
- return `${String((_a = data.fb_client_id) != null ? _a : "")}:${JSON.stringify((_b = data.raw) != null ? _b : null)}`;
570
- },
571
- (data) => {
573
+ var onAgentMessage = (listener, options = {}) => {
574
+ const emit2 = (data) => {
572
575
  if (!options.includeEcho && isEchoOnly(data.raw)) {
573
576
  agentLog("message: b\u1ECF echo tin m\xECnh v\u1EEBa g\u1EEDi", preview(data.raw));
574
577
  return;
@@ -576,9 +579,38 @@ var onAgentMessage = (listener, options = {}) => listenHostEvent(
576
579
  agentLog("message \u2190", `v${String(data.v)}`, `fb_client_id=${String(data.fb_client_id)}`, preview(data.raw));
577
580
  const message = { ...data };
578
581
  delete message.type;
579
- listener(message);
580
- }
581
- );
582
+ try {
583
+ listener(message);
584
+ } catch (error) {
585
+ console.error("[retion-miniapp] onAgentMessage listener l\u1ED7i", error);
586
+ }
587
+ };
588
+ const held = [];
589
+ const flush = () => {
590
+ held.splice(0).forEach(emit2);
591
+ };
592
+ held_flushers.add(flush);
593
+ const off = listenHostEvent(
594
+ AGENT_MESSAGE_EVENT_TYPE,
595
+ options.app_id,
596
+ (data) => {
597
+ var _a, _b;
598
+ return `${String((_a = data.fb_client_id) != null ? _a : "")}:${JSON.stringify((_b = data.raw) != null ? _b : null)}`;
599
+ },
600
+ (data) => {
601
+ if (!options.includeEcho && pending_sends > 0 && isPossibleEcho(data.raw)) {
602
+ agentLog("message: gi\u1EEF t\u1EA1m (ch\u1EDD k\u1EBFt qu\u1EA3 sendPayloadMeta \u0111\u1EC3 nh\u1EADn ra echo)");
603
+ held.push(data);
604
+ return;
605
+ }
606
+ emit2(data);
607
+ }
608
+ );
609
+ return () => {
610
+ held_flushers.delete(flush);
611
+ off();
612
+ };
613
+ };
582
614
  var onAgentStop = (listener, options = {}) => {
583
615
  let last_at = 0;
584
616
  return listenHostEvent(AGENT_STOPPED_EVENT_TYPE, options.app_id, () => null, (data) => {
@@ -608,6 +640,7 @@ var sendPayloadMeta = (payload_meta, options = {}) => {
608
640
  const request_id = `pm_${Date.now().toString(36)}_${request_seq}`;
609
641
  const meta_key = JSON.stringify(payload_meta);
610
642
  remember(sent_metas, meta_key);
643
+ pending_sends += 1;
611
644
  return new Promise((resolve) => {
612
645
  var _a, _b, _c;
613
646
  let done = false;
@@ -622,7 +655,9 @@ var sendPayloadMeta = (payload_meta, options = {}) => {
622
655
  remember(sent_mids, result.message_mid);
623
656
  }
624
657
  agentLog("payload_meta_result \u2190", request_id, preview(result));
658
+ pending_sends = Math.max(0, pending_sends - 1);
625
659
  resolve(result);
660
+ flushHeld();
626
661
  };
627
662
  const off = listenHostEvent(AGENT_PAYLOAD_META_RESULT_EVENT_TYPE, void 0, () => null, (data) => {
628
663
  if (data.request_id !== request_id) {