@menuia/core 1.1.1 → 1.2.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.d.mts CHANGED
@@ -8,6 +8,12 @@ interface MenuIAConfig {
8
8
  customFields?: Record<string, string>;
9
9
  metadata?: Record<string, unknown>;
10
10
  channel?: 'WIDGET' | 'API';
11
+ /** Visitor email - used for session resume and proactive messages */
12
+ visitorEmail?: string;
13
+ /** Visitor phone - used for session resume and proactive messages */
14
+ visitorPhone?: string;
15
+ /** Visitor name */
16
+ visitorName?: string;
11
17
  onMessage?: (message: ChatMessage) => void;
12
18
  onStatusChange?: (status: ConnectionStatus) => void;
13
19
  onTicketCreated?: (ticketId: string) => void;
@@ -84,6 +90,23 @@ declare class MenuIAClient {
84
90
  getSession(): WidgetSession;
85
91
  /** Update custom fields (sent with next message) */
86
92
  setCustomFields(fields: Record<string, string>): void;
93
+ /**
94
+ * Check for proactive messages waiting for this visitor.
95
+ * Call this when the widget opens before the user sends a message.
96
+ * If messages are found, the session is automatically resumed.
97
+ */
98
+ checkProactiveMessages(): Promise<{
99
+ hasMessages: boolean;
100
+ messages: ChatMessage[];
101
+ }>;
102
+ /**
103
+ * Start polling for proactive messages (before user sends first message).
104
+ * Checks every intervalMs. Stops automatically when messages are found.
105
+ */
106
+ startProactivePolling(intervalMs?: number): void;
107
+ /** Stop proactive polling */
108
+ stopProactivePolling(): void;
109
+ private _proactivePollInterval;
87
110
  /** Clear session and start fresh */
88
111
  clearSession(): void;
89
112
  /** Cleanup - call when component unmounts */
package/dist/index.d.ts CHANGED
@@ -8,6 +8,12 @@ interface MenuIAConfig {
8
8
  customFields?: Record<string, string>;
9
9
  metadata?: Record<string, unknown>;
10
10
  channel?: 'WIDGET' | 'API';
11
+ /** Visitor email - used for session resume and proactive messages */
12
+ visitorEmail?: string;
13
+ /** Visitor phone - used for session resume and proactive messages */
14
+ visitorPhone?: string;
15
+ /** Visitor name */
16
+ visitorName?: string;
11
17
  onMessage?: (message: ChatMessage) => void;
12
18
  onStatusChange?: (status: ConnectionStatus) => void;
13
19
  onTicketCreated?: (ticketId: string) => void;
@@ -84,6 +90,23 @@ declare class MenuIAClient {
84
90
  getSession(): WidgetSession;
85
91
  /** Update custom fields (sent with next message) */
86
92
  setCustomFields(fields: Record<string, string>): void;
93
+ /**
94
+ * Check for proactive messages waiting for this visitor.
95
+ * Call this when the widget opens before the user sends a message.
96
+ * If messages are found, the session is automatically resumed.
97
+ */
98
+ checkProactiveMessages(): Promise<{
99
+ hasMessages: boolean;
100
+ messages: ChatMessage[];
101
+ }>;
102
+ /**
103
+ * Start polling for proactive messages (before user sends first message).
104
+ * Checks every intervalMs. Stops automatically when messages are found.
105
+ */
106
+ startProactivePolling(intervalMs?: number): void;
107
+ /** Stop proactive polling */
108
+ stopProactivePolling(): void;
109
+ private _proactivePollInterval;
87
110
  /** Clear session and start fresh */
88
111
  clearSession(): void;
89
112
  /** Cleanup - call when component unmounts */
package/dist/index.js CHANGED
@@ -33,6 +33,7 @@ var MenuIAClient = class {
33
33
  this.pollInterval = null;
34
34
  this.lastMessageCount = 0;
35
35
  this.resolvedAgentId = null;
36
+ this._proactivePollInterval = null;
36
37
  if (!config.widgetId && !config.agentId) {
37
38
  throw new Error("[MenuIA] widgetId ou agentId e obrigatorio");
38
39
  }
@@ -273,6 +274,66 @@ var MenuIAClient = class {
273
274
  setCustomFields(fields) {
274
275
  this.config.customFields = { ...this.config.customFields, ...fields };
275
276
  }
277
+ /**
278
+ * Check for proactive messages waiting for this visitor.
279
+ * Call this when the widget opens before the user sends a message.
280
+ * If messages are found, the session is automatically resumed.
281
+ */
282
+ async checkProactiveMessages() {
283
+ const email = this.config.visitorEmail || this.config.metadata?.email;
284
+ const phone = this.config.visitorPhone || this.config.metadata?.phone;
285
+ if (!email && !phone) return { hasMessages: false, messages: [] };
286
+ try {
287
+ const params = new URLSearchParams();
288
+ if (email) params.set("email", String(email));
289
+ if (phone) params.set("phone", String(phone));
290
+ const res = await fetch(`${this.config.apiUrl}/widget/${this.getWidgetId()}/check-proactive?${params}`);
291
+ if (!res.ok) return { hasMessages: false, messages: [] };
292
+ const data = await res.json();
293
+ if (data.hasMessages && data.conversationId) {
294
+ this.session.conversationId = data.conversationId;
295
+ this.session.messages = (data.messages || []).map((m) => ({
296
+ id: m.id,
297
+ role: m.role,
298
+ content: m.content,
299
+ createdAt: m.createdAt,
300
+ authorName: m.authorName,
301
+ mediaType: m.mediaType,
302
+ mediaUrl: m.mediaUrl
303
+ }));
304
+ this.lastMessageCount = this.session.messages.length;
305
+ this.saveSession();
306
+ for (const msg of this.session.messages) {
307
+ if (msg.role === "assistant") this.config.onMessage?.(msg);
308
+ }
309
+ return { hasMessages: true, messages: this.session.messages };
310
+ }
311
+ return { hasMessages: false, messages: [] };
312
+ } catch {
313
+ return { hasMessages: false, messages: [] };
314
+ }
315
+ }
316
+ /**
317
+ * Start polling for proactive messages (before user sends first message).
318
+ * Checks every intervalMs. Stops automatically when messages are found.
319
+ */
320
+ startProactivePolling(intervalMs = 1e4) {
321
+ const check = async () => {
322
+ const result = await this.checkProactiveMessages();
323
+ if (result.hasMessages) {
324
+ this.stopProactivePolling();
325
+ }
326
+ };
327
+ check();
328
+ this._proactivePollInterval = setInterval(check, intervalMs);
329
+ }
330
+ /** Stop proactive polling */
331
+ stopProactivePolling() {
332
+ if (this._proactivePollInterval) {
333
+ clearInterval(this._proactivePollInterval);
334
+ this._proactivePollInterval = null;
335
+ }
336
+ }
276
337
  /** Clear session and start fresh */
277
338
  clearSession() {
278
339
  this.session = this.createNewSession();
@@ -282,6 +343,7 @@ var MenuIAClient = class {
282
343
  /** Cleanup - call when component unmounts */
283
344
  destroy() {
284
345
  this.stopPolling();
346
+ this.stopProactivePolling();
285
347
  }
286
348
  // =====================
287
349
  // PRIVATE
package/dist/index.mjs CHANGED
@@ -7,6 +7,7 @@ var MenuIAClient = class {
7
7
  this.pollInterval = null;
8
8
  this.lastMessageCount = 0;
9
9
  this.resolvedAgentId = null;
10
+ this._proactivePollInterval = null;
10
11
  if (!config.widgetId && !config.agentId) {
11
12
  throw new Error("[MenuIA] widgetId ou agentId e obrigatorio");
12
13
  }
@@ -247,6 +248,66 @@ var MenuIAClient = class {
247
248
  setCustomFields(fields) {
248
249
  this.config.customFields = { ...this.config.customFields, ...fields };
249
250
  }
251
+ /**
252
+ * Check for proactive messages waiting for this visitor.
253
+ * Call this when the widget opens before the user sends a message.
254
+ * If messages are found, the session is automatically resumed.
255
+ */
256
+ async checkProactiveMessages() {
257
+ const email = this.config.visitorEmail || this.config.metadata?.email;
258
+ const phone = this.config.visitorPhone || this.config.metadata?.phone;
259
+ if (!email && !phone) return { hasMessages: false, messages: [] };
260
+ try {
261
+ const params = new URLSearchParams();
262
+ if (email) params.set("email", String(email));
263
+ if (phone) params.set("phone", String(phone));
264
+ const res = await fetch(`${this.config.apiUrl}/widget/${this.getWidgetId()}/check-proactive?${params}`);
265
+ if (!res.ok) return { hasMessages: false, messages: [] };
266
+ const data = await res.json();
267
+ if (data.hasMessages && data.conversationId) {
268
+ this.session.conversationId = data.conversationId;
269
+ this.session.messages = (data.messages || []).map((m) => ({
270
+ id: m.id,
271
+ role: m.role,
272
+ content: m.content,
273
+ createdAt: m.createdAt,
274
+ authorName: m.authorName,
275
+ mediaType: m.mediaType,
276
+ mediaUrl: m.mediaUrl
277
+ }));
278
+ this.lastMessageCount = this.session.messages.length;
279
+ this.saveSession();
280
+ for (const msg of this.session.messages) {
281
+ if (msg.role === "assistant") this.config.onMessage?.(msg);
282
+ }
283
+ return { hasMessages: true, messages: this.session.messages };
284
+ }
285
+ return { hasMessages: false, messages: [] };
286
+ } catch {
287
+ return { hasMessages: false, messages: [] };
288
+ }
289
+ }
290
+ /**
291
+ * Start polling for proactive messages (before user sends first message).
292
+ * Checks every intervalMs. Stops automatically when messages are found.
293
+ */
294
+ startProactivePolling(intervalMs = 1e4) {
295
+ const check = async () => {
296
+ const result = await this.checkProactiveMessages();
297
+ if (result.hasMessages) {
298
+ this.stopProactivePolling();
299
+ }
300
+ };
301
+ check();
302
+ this._proactivePollInterval = setInterval(check, intervalMs);
303
+ }
304
+ /** Stop proactive polling */
305
+ stopProactivePolling() {
306
+ if (this._proactivePollInterval) {
307
+ clearInterval(this._proactivePollInterval);
308
+ this._proactivePollInterval = null;
309
+ }
310
+ }
250
311
  /** Clear session and start fresh */
251
312
  clearSession() {
252
313
  this.session = this.createNewSession();
@@ -256,6 +317,7 @@ var MenuIAClient = class {
256
317
  /** Cleanup - call when component unmounts */
257
318
  destroy() {
258
319
  this.stopPolling();
320
+ this.stopProactivePolling();
259
321
  }
260
322
  // =====================
261
323
  // PRIVATE
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@menuia/core",
3
- "version": "1.1.1",
3
+ "version": "1.2.0",
4
4
  "description": "MenuIA Chat SDK - Core",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",