@menuia/core 1.1.1 → 1.3.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 +40 -1
- package/dist/index.d.ts +40 -1
- package/dist/index.js +132 -11
- package/dist/index.mjs +132 -11
- package/package.json +1 -1
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;
|
|
@@ -46,7 +52,7 @@ interface ChatResponse {
|
|
|
46
52
|
};
|
|
47
53
|
}
|
|
48
54
|
interface StreamEvent {
|
|
49
|
-
type: 'token' | 'tool_start' | 'tool_end' | 'done' | 'error' | 'response';
|
|
55
|
+
type: 'token' | 'thinking' | 'tool_start' | 'tool_done' | 'tool_end' | 'done' | 'error' | 'response';
|
|
50
56
|
data: unknown;
|
|
51
57
|
}
|
|
52
58
|
interface WidgetSession {
|
|
@@ -54,6 +60,14 @@ interface WidgetSession {
|
|
|
54
60
|
conversationId?: string;
|
|
55
61
|
ticketId?: string;
|
|
56
62
|
messages: ChatMessage[];
|
|
63
|
+
/** Signed token returned by startSession, authorizes the message poll */
|
|
64
|
+
widgetToken?: string;
|
|
65
|
+
/** Widget mode resolved from the server (ai/human/hybrid) */
|
|
66
|
+
mode?: 'ai' | 'human' | 'hybrid';
|
|
67
|
+
/** Agent ID resolved from the widget session (widgetId -> agentId) */
|
|
68
|
+
resolvedAgentId?: string;
|
|
69
|
+
/** Whether startSession has already run for this session */
|
|
70
|
+
started?: boolean;
|
|
57
71
|
}
|
|
58
72
|
|
|
59
73
|
declare class MenuIAClient {
|
|
@@ -66,6 +80,14 @@ declare class MenuIAClient {
|
|
|
66
80
|
constructor(config: MenuIAConfig);
|
|
67
81
|
/** Get the effective ID (widgetId preferred, falls back to agentId) */
|
|
68
82
|
private getWidgetId;
|
|
83
|
+
/** Agent id to use with /chat (resolved from the widget session, or configured directly) */
|
|
84
|
+
private getAgentId;
|
|
85
|
+
/**
|
|
86
|
+
* Resolve the widget session once (widgetId -> agentId + conversationId + token).
|
|
87
|
+
* When only an agentId is configured there is no widget to resolve; /chat is used
|
|
88
|
+
* directly and the conversationId comes back from the first response.
|
|
89
|
+
*/
|
|
90
|
+
private ensureSession;
|
|
69
91
|
/** Send a message to the AI agent */
|
|
70
92
|
sendMessage(options: SendMessageOptions): Promise<ChatResponse>;
|
|
71
93
|
/** Send a message and receive streaming response */
|
|
@@ -84,6 +106,23 @@ declare class MenuIAClient {
|
|
|
84
106
|
getSession(): WidgetSession;
|
|
85
107
|
/** Update custom fields (sent with next message) */
|
|
86
108
|
setCustomFields(fields: Record<string, string>): void;
|
|
109
|
+
/**
|
|
110
|
+
* Check for proactive messages waiting for this visitor.
|
|
111
|
+
* Call this when the widget opens before the user sends a message.
|
|
112
|
+
* If messages are found, the session is automatically resumed.
|
|
113
|
+
*/
|
|
114
|
+
checkProactiveMessages(): Promise<{
|
|
115
|
+
hasMessages: boolean;
|
|
116
|
+
messages: ChatMessage[];
|
|
117
|
+
}>;
|
|
118
|
+
/**
|
|
119
|
+
* Start polling for proactive messages (before user sends first message).
|
|
120
|
+
* Checks every intervalMs. Stops automatically when messages are found.
|
|
121
|
+
*/
|
|
122
|
+
startProactivePolling(intervalMs?: number): void;
|
|
123
|
+
/** Stop proactive polling */
|
|
124
|
+
stopProactivePolling(): void;
|
|
125
|
+
private _proactivePollInterval;
|
|
87
126
|
/** Clear session and start fresh */
|
|
88
127
|
clearSession(): void;
|
|
89
128
|
/** 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;
|
|
@@ -46,7 +52,7 @@ interface ChatResponse {
|
|
|
46
52
|
};
|
|
47
53
|
}
|
|
48
54
|
interface StreamEvent {
|
|
49
|
-
type: 'token' | 'tool_start' | 'tool_end' | 'done' | 'error' | 'response';
|
|
55
|
+
type: 'token' | 'thinking' | 'tool_start' | 'tool_done' | 'tool_end' | 'done' | 'error' | 'response';
|
|
50
56
|
data: unknown;
|
|
51
57
|
}
|
|
52
58
|
interface WidgetSession {
|
|
@@ -54,6 +60,14 @@ interface WidgetSession {
|
|
|
54
60
|
conversationId?: string;
|
|
55
61
|
ticketId?: string;
|
|
56
62
|
messages: ChatMessage[];
|
|
63
|
+
/** Signed token returned by startSession, authorizes the message poll */
|
|
64
|
+
widgetToken?: string;
|
|
65
|
+
/** Widget mode resolved from the server (ai/human/hybrid) */
|
|
66
|
+
mode?: 'ai' | 'human' | 'hybrid';
|
|
67
|
+
/** Agent ID resolved from the widget session (widgetId -> agentId) */
|
|
68
|
+
resolvedAgentId?: string;
|
|
69
|
+
/** Whether startSession has already run for this session */
|
|
70
|
+
started?: boolean;
|
|
57
71
|
}
|
|
58
72
|
|
|
59
73
|
declare class MenuIAClient {
|
|
@@ -66,6 +80,14 @@ declare class MenuIAClient {
|
|
|
66
80
|
constructor(config: MenuIAConfig);
|
|
67
81
|
/** Get the effective ID (widgetId preferred, falls back to agentId) */
|
|
68
82
|
private getWidgetId;
|
|
83
|
+
/** Agent id to use with /chat (resolved from the widget session, or configured directly) */
|
|
84
|
+
private getAgentId;
|
|
85
|
+
/**
|
|
86
|
+
* Resolve the widget session once (widgetId -> agentId + conversationId + token).
|
|
87
|
+
* When only an agentId is configured there is no widget to resolve; /chat is used
|
|
88
|
+
* directly and the conversationId comes back from the first response.
|
|
89
|
+
*/
|
|
90
|
+
private ensureSession;
|
|
69
91
|
/** Send a message to the AI agent */
|
|
70
92
|
sendMessage(options: SendMessageOptions): Promise<ChatResponse>;
|
|
71
93
|
/** Send a message and receive streaming response */
|
|
@@ -84,6 +106,23 @@ declare class MenuIAClient {
|
|
|
84
106
|
getSession(): WidgetSession;
|
|
85
107
|
/** Update custom fields (sent with next message) */
|
|
86
108
|
setCustomFields(fields: Record<string, string>): void;
|
|
109
|
+
/**
|
|
110
|
+
* Check for proactive messages waiting for this visitor.
|
|
111
|
+
* Call this when the widget opens before the user sends a message.
|
|
112
|
+
* If messages are found, the session is automatically resumed.
|
|
113
|
+
*/
|
|
114
|
+
checkProactiveMessages(): Promise<{
|
|
115
|
+
hasMessages: boolean;
|
|
116
|
+
messages: ChatMessage[];
|
|
117
|
+
}>;
|
|
118
|
+
/**
|
|
119
|
+
* Start polling for proactive messages (before user sends first message).
|
|
120
|
+
* Checks every intervalMs. Stops automatically when messages are found.
|
|
121
|
+
*/
|
|
122
|
+
startProactivePolling(intervalMs?: number): void;
|
|
123
|
+
/** Stop proactive polling */
|
|
124
|
+
stopProactivePolling(): void;
|
|
125
|
+
private _proactivePollInterval;
|
|
87
126
|
/** Clear session and start fresh */
|
|
88
127
|
clearSession(): void;
|
|
89
128
|
/** 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
|
}
|
|
@@ -47,6 +48,58 @@ var MenuIAClient = class {
|
|
|
47
48
|
getWidgetId() {
|
|
48
49
|
return this.config.widgetId || this.config.agentId || "";
|
|
49
50
|
}
|
|
51
|
+
/** Agent id to use with /chat (resolved from the widget session, or configured directly) */
|
|
52
|
+
getAgentId() {
|
|
53
|
+
return this.session.resolvedAgentId || this.config.agentId || this.config.widgetId || "";
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Resolve the widget session once (widgetId -> agentId + conversationId + token).
|
|
57
|
+
* When only an agentId is configured there is no widget to resolve; /chat is used
|
|
58
|
+
* directly and the conversationId comes back from the first response.
|
|
59
|
+
*/
|
|
60
|
+
async ensureSession() {
|
|
61
|
+
if (this.session.started) return;
|
|
62
|
+
if (!this.config.widgetId) {
|
|
63
|
+
this.session.started = true;
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
try {
|
|
67
|
+
const response = await fetch(`${this.config.apiUrl}/widget/${this.config.widgetId}/session`, {
|
|
68
|
+
method: "POST",
|
|
69
|
+
headers: { "Content-Type": "application/json" },
|
|
70
|
+
body: JSON.stringify({
|
|
71
|
+
name: this.config.visitorName || this.config.metadata?.name,
|
|
72
|
+
email: this.config.visitorEmail || this.config.metadata?.email,
|
|
73
|
+
phone: this.config.visitorPhone || this.config.metadata?.phone,
|
|
74
|
+
customData: this.config.customFields,
|
|
75
|
+
sessionId: this.session.sessionId
|
|
76
|
+
})
|
|
77
|
+
});
|
|
78
|
+
if (response.ok) {
|
|
79
|
+
const data = await response.json();
|
|
80
|
+
if (data.conversationId) this.session.conversationId = data.conversationId;
|
|
81
|
+
if (data.ticketId) this.session.ticketId = data.ticketId;
|
|
82
|
+
if (data.agentId) this.session.resolvedAgentId = data.agentId;
|
|
83
|
+
if (data.widgetToken) this.session.widgetToken = data.widgetToken;
|
|
84
|
+
if (data.mode) this.session.mode = data.mode;
|
|
85
|
+
if (Array.isArray(data.messages) && data.messages.length && this.session.messages.length === 0) {
|
|
86
|
+
this.session.messages = data.messages.map((m) => ({
|
|
87
|
+
id: m.id,
|
|
88
|
+
role: m.role,
|
|
89
|
+
content: m.content,
|
|
90
|
+
createdAt: m.createdAt,
|
|
91
|
+
mediaUrl: m.mediaUrl,
|
|
92
|
+
mediaType: m.mediaType,
|
|
93
|
+
authorName: m.authorName
|
|
94
|
+
}));
|
|
95
|
+
this.lastMessageCount = this.session.messages.length;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
} catch {
|
|
99
|
+
}
|
|
100
|
+
this.session.started = true;
|
|
101
|
+
this.saveSession();
|
|
102
|
+
}
|
|
50
103
|
// =====================
|
|
51
104
|
// PUBLIC API
|
|
52
105
|
// =====================
|
|
@@ -54,8 +107,9 @@ var MenuIAClient = class {
|
|
|
54
107
|
async sendMessage(options) {
|
|
55
108
|
this.setStatus("connecting");
|
|
56
109
|
try {
|
|
110
|
+
await this.ensureSession();
|
|
57
111
|
const body = {
|
|
58
|
-
agentId: this.
|
|
112
|
+
agentId: this.getAgentId(),
|
|
59
113
|
message: options.content,
|
|
60
114
|
channel: this.config.channel,
|
|
61
115
|
sessionId: this.session.sessionId,
|
|
@@ -71,7 +125,7 @@ var MenuIAClient = class {
|
|
|
71
125
|
if (options.attachments?.length) {
|
|
72
126
|
body.attachments = options.attachments;
|
|
73
127
|
}
|
|
74
|
-
const response = await fetch(`${this.config.apiUrl}/
|
|
128
|
+
const response = await fetch(`${this.config.apiUrl}/chat`, {
|
|
75
129
|
method: "POST",
|
|
76
130
|
headers: { "Content-Type": "application/json" },
|
|
77
131
|
body: JSON.stringify(body)
|
|
@@ -116,8 +170,9 @@ var MenuIAClient = class {
|
|
|
116
170
|
async sendMessageStream(options, onToken) {
|
|
117
171
|
this.setStatus("connecting");
|
|
118
172
|
try {
|
|
173
|
+
await this.ensureSession();
|
|
119
174
|
const body = {
|
|
120
|
-
agentId: this.
|
|
175
|
+
agentId: this.getAgentId(),
|
|
121
176
|
message: options.content,
|
|
122
177
|
channel: this.config.channel,
|
|
123
178
|
sessionId: this.session.sessionId,
|
|
@@ -133,7 +188,7 @@ var MenuIAClient = class {
|
|
|
133
188
|
if (options.attachments?.length) {
|
|
134
189
|
body.attachments = options.attachments;
|
|
135
190
|
}
|
|
136
|
-
const response = await fetch(`${this.config.apiUrl}/
|
|
191
|
+
const response = await fetch(`${this.config.apiUrl}/chat/stream`, {
|
|
137
192
|
method: "POST",
|
|
138
193
|
headers: { "Content-Type": "application/json" },
|
|
139
194
|
body: JSON.stringify(body)
|
|
@@ -209,10 +264,14 @@ var MenuIAClient = class {
|
|
|
209
264
|
}
|
|
210
265
|
/** Load conversation history from server */
|
|
211
266
|
async loadHistory() {
|
|
212
|
-
|
|
267
|
+
await this.ensureSession();
|
|
268
|
+
if (!this.session.conversationId) return this.session.messages;
|
|
213
269
|
try {
|
|
270
|
+
const params = new URLSearchParams();
|
|
271
|
+
if (this.session.widgetToken) params.set("token", this.session.widgetToken);
|
|
272
|
+
const qs = params.toString();
|
|
214
273
|
const response = await fetch(
|
|
215
|
-
`${this.config.apiUrl}/widget/
|
|
274
|
+
`${this.config.apiUrl}/widget/messages/${this.session.conversationId}${qs ? `?${qs}` : ""}`
|
|
216
275
|
);
|
|
217
276
|
if (!response.ok) return this.session.messages;
|
|
218
277
|
const data = await response.json();
|
|
@@ -225,11 +284,12 @@ var MenuIAClient = class {
|
|
|
225
284
|
mediaType: m.mediaType,
|
|
226
285
|
authorName: m.authorName
|
|
227
286
|
}));
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
287
|
+
if (messages.length) {
|
|
288
|
+
this.session.messages = messages;
|
|
289
|
+
this.lastMessageCount = messages.length;
|
|
290
|
+
this.saveSession();
|
|
291
|
+
}
|
|
292
|
+
return this.session.messages;
|
|
233
293
|
} catch {
|
|
234
294
|
return this.session.messages;
|
|
235
295
|
}
|
|
@@ -273,6 +333,66 @@ var MenuIAClient = class {
|
|
|
273
333
|
setCustomFields(fields) {
|
|
274
334
|
this.config.customFields = { ...this.config.customFields, ...fields };
|
|
275
335
|
}
|
|
336
|
+
/**
|
|
337
|
+
* Check for proactive messages waiting for this visitor.
|
|
338
|
+
* Call this when the widget opens before the user sends a message.
|
|
339
|
+
* If messages are found, the session is automatically resumed.
|
|
340
|
+
*/
|
|
341
|
+
async checkProactiveMessages() {
|
|
342
|
+
const email = this.config.visitorEmail || this.config.metadata?.email;
|
|
343
|
+
const phone = this.config.visitorPhone || this.config.metadata?.phone;
|
|
344
|
+
if (!email && !phone) return { hasMessages: false, messages: [] };
|
|
345
|
+
try {
|
|
346
|
+
const params = new URLSearchParams();
|
|
347
|
+
if (email) params.set("email", String(email));
|
|
348
|
+
if (phone) params.set("phone", String(phone));
|
|
349
|
+
const res = await fetch(`${this.config.apiUrl}/widget/${this.getWidgetId()}/check-proactive?${params}`);
|
|
350
|
+
if (!res.ok) return { hasMessages: false, messages: [] };
|
|
351
|
+
const data = await res.json();
|
|
352
|
+
if (data.hasMessages && data.conversationId) {
|
|
353
|
+
this.session.conversationId = data.conversationId;
|
|
354
|
+
this.session.messages = (data.messages || []).map((m) => ({
|
|
355
|
+
id: m.id,
|
|
356
|
+
role: m.role,
|
|
357
|
+
content: m.content,
|
|
358
|
+
createdAt: m.createdAt,
|
|
359
|
+
authorName: m.authorName,
|
|
360
|
+
mediaType: m.mediaType,
|
|
361
|
+
mediaUrl: m.mediaUrl
|
|
362
|
+
}));
|
|
363
|
+
this.lastMessageCount = this.session.messages.length;
|
|
364
|
+
this.saveSession();
|
|
365
|
+
for (const msg of this.session.messages) {
|
|
366
|
+
if (msg.role === "assistant") this.config.onMessage?.(msg);
|
|
367
|
+
}
|
|
368
|
+
return { hasMessages: true, messages: this.session.messages };
|
|
369
|
+
}
|
|
370
|
+
return { hasMessages: false, messages: [] };
|
|
371
|
+
} catch {
|
|
372
|
+
return { hasMessages: false, messages: [] };
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
/**
|
|
376
|
+
* Start polling for proactive messages (before user sends first message).
|
|
377
|
+
* Checks every intervalMs. Stops automatically when messages are found.
|
|
378
|
+
*/
|
|
379
|
+
startProactivePolling(intervalMs = 1e4) {
|
|
380
|
+
const check = async () => {
|
|
381
|
+
const result = await this.checkProactiveMessages();
|
|
382
|
+
if (result.hasMessages) {
|
|
383
|
+
this.stopProactivePolling();
|
|
384
|
+
}
|
|
385
|
+
};
|
|
386
|
+
check();
|
|
387
|
+
this._proactivePollInterval = setInterval(check, intervalMs);
|
|
388
|
+
}
|
|
389
|
+
/** Stop proactive polling */
|
|
390
|
+
stopProactivePolling() {
|
|
391
|
+
if (this._proactivePollInterval) {
|
|
392
|
+
clearInterval(this._proactivePollInterval);
|
|
393
|
+
this._proactivePollInterval = null;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
276
396
|
/** Clear session and start fresh */
|
|
277
397
|
clearSession() {
|
|
278
398
|
this.session = this.createNewSession();
|
|
@@ -282,6 +402,7 @@ var MenuIAClient = class {
|
|
|
282
402
|
/** Cleanup - call when component unmounts */
|
|
283
403
|
destroy() {
|
|
284
404
|
this.stopPolling();
|
|
405
|
+
this.stopProactivePolling();
|
|
285
406
|
}
|
|
286
407
|
// =====================
|
|
287
408
|
// 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
|
}
|
|
@@ -21,6 +22,58 @@ var MenuIAClient = class {
|
|
|
21
22
|
getWidgetId() {
|
|
22
23
|
return this.config.widgetId || this.config.agentId || "";
|
|
23
24
|
}
|
|
25
|
+
/** Agent id to use with /chat (resolved from the widget session, or configured directly) */
|
|
26
|
+
getAgentId() {
|
|
27
|
+
return this.session.resolvedAgentId || this.config.agentId || this.config.widgetId || "";
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Resolve the widget session once (widgetId -> agentId + conversationId + token).
|
|
31
|
+
* When only an agentId is configured there is no widget to resolve; /chat is used
|
|
32
|
+
* directly and the conversationId comes back from the first response.
|
|
33
|
+
*/
|
|
34
|
+
async ensureSession() {
|
|
35
|
+
if (this.session.started) return;
|
|
36
|
+
if (!this.config.widgetId) {
|
|
37
|
+
this.session.started = true;
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
try {
|
|
41
|
+
const response = await fetch(`${this.config.apiUrl}/widget/${this.config.widgetId}/session`, {
|
|
42
|
+
method: "POST",
|
|
43
|
+
headers: { "Content-Type": "application/json" },
|
|
44
|
+
body: JSON.stringify({
|
|
45
|
+
name: this.config.visitorName || this.config.metadata?.name,
|
|
46
|
+
email: this.config.visitorEmail || this.config.metadata?.email,
|
|
47
|
+
phone: this.config.visitorPhone || this.config.metadata?.phone,
|
|
48
|
+
customData: this.config.customFields,
|
|
49
|
+
sessionId: this.session.sessionId
|
|
50
|
+
})
|
|
51
|
+
});
|
|
52
|
+
if (response.ok) {
|
|
53
|
+
const data = await response.json();
|
|
54
|
+
if (data.conversationId) this.session.conversationId = data.conversationId;
|
|
55
|
+
if (data.ticketId) this.session.ticketId = data.ticketId;
|
|
56
|
+
if (data.agentId) this.session.resolvedAgentId = data.agentId;
|
|
57
|
+
if (data.widgetToken) this.session.widgetToken = data.widgetToken;
|
|
58
|
+
if (data.mode) this.session.mode = data.mode;
|
|
59
|
+
if (Array.isArray(data.messages) && data.messages.length && this.session.messages.length === 0) {
|
|
60
|
+
this.session.messages = data.messages.map((m) => ({
|
|
61
|
+
id: m.id,
|
|
62
|
+
role: m.role,
|
|
63
|
+
content: m.content,
|
|
64
|
+
createdAt: m.createdAt,
|
|
65
|
+
mediaUrl: m.mediaUrl,
|
|
66
|
+
mediaType: m.mediaType,
|
|
67
|
+
authorName: m.authorName
|
|
68
|
+
}));
|
|
69
|
+
this.lastMessageCount = this.session.messages.length;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
} catch {
|
|
73
|
+
}
|
|
74
|
+
this.session.started = true;
|
|
75
|
+
this.saveSession();
|
|
76
|
+
}
|
|
24
77
|
// =====================
|
|
25
78
|
// PUBLIC API
|
|
26
79
|
// =====================
|
|
@@ -28,8 +81,9 @@ var MenuIAClient = class {
|
|
|
28
81
|
async sendMessage(options) {
|
|
29
82
|
this.setStatus("connecting");
|
|
30
83
|
try {
|
|
84
|
+
await this.ensureSession();
|
|
31
85
|
const body = {
|
|
32
|
-
agentId: this.
|
|
86
|
+
agentId: this.getAgentId(),
|
|
33
87
|
message: options.content,
|
|
34
88
|
channel: this.config.channel,
|
|
35
89
|
sessionId: this.session.sessionId,
|
|
@@ -45,7 +99,7 @@ var MenuIAClient = class {
|
|
|
45
99
|
if (options.attachments?.length) {
|
|
46
100
|
body.attachments = options.attachments;
|
|
47
101
|
}
|
|
48
|
-
const response = await fetch(`${this.config.apiUrl}/
|
|
102
|
+
const response = await fetch(`${this.config.apiUrl}/chat`, {
|
|
49
103
|
method: "POST",
|
|
50
104
|
headers: { "Content-Type": "application/json" },
|
|
51
105
|
body: JSON.stringify(body)
|
|
@@ -90,8 +144,9 @@ var MenuIAClient = class {
|
|
|
90
144
|
async sendMessageStream(options, onToken) {
|
|
91
145
|
this.setStatus("connecting");
|
|
92
146
|
try {
|
|
147
|
+
await this.ensureSession();
|
|
93
148
|
const body = {
|
|
94
|
-
agentId: this.
|
|
149
|
+
agentId: this.getAgentId(),
|
|
95
150
|
message: options.content,
|
|
96
151
|
channel: this.config.channel,
|
|
97
152
|
sessionId: this.session.sessionId,
|
|
@@ -107,7 +162,7 @@ var MenuIAClient = class {
|
|
|
107
162
|
if (options.attachments?.length) {
|
|
108
163
|
body.attachments = options.attachments;
|
|
109
164
|
}
|
|
110
|
-
const response = await fetch(`${this.config.apiUrl}/
|
|
165
|
+
const response = await fetch(`${this.config.apiUrl}/chat/stream`, {
|
|
111
166
|
method: "POST",
|
|
112
167
|
headers: { "Content-Type": "application/json" },
|
|
113
168
|
body: JSON.stringify(body)
|
|
@@ -183,10 +238,14 @@ var MenuIAClient = class {
|
|
|
183
238
|
}
|
|
184
239
|
/** Load conversation history from server */
|
|
185
240
|
async loadHistory() {
|
|
186
|
-
|
|
241
|
+
await this.ensureSession();
|
|
242
|
+
if (!this.session.conversationId) return this.session.messages;
|
|
187
243
|
try {
|
|
244
|
+
const params = new URLSearchParams();
|
|
245
|
+
if (this.session.widgetToken) params.set("token", this.session.widgetToken);
|
|
246
|
+
const qs = params.toString();
|
|
188
247
|
const response = await fetch(
|
|
189
|
-
`${this.config.apiUrl}/widget/
|
|
248
|
+
`${this.config.apiUrl}/widget/messages/${this.session.conversationId}${qs ? `?${qs}` : ""}`
|
|
190
249
|
);
|
|
191
250
|
if (!response.ok) return this.session.messages;
|
|
192
251
|
const data = await response.json();
|
|
@@ -199,11 +258,12 @@ var MenuIAClient = class {
|
|
|
199
258
|
mediaType: m.mediaType,
|
|
200
259
|
authorName: m.authorName
|
|
201
260
|
}));
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
261
|
+
if (messages.length) {
|
|
262
|
+
this.session.messages = messages;
|
|
263
|
+
this.lastMessageCount = messages.length;
|
|
264
|
+
this.saveSession();
|
|
265
|
+
}
|
|
266
|
+
return this.session.messages;
|
|
207
267
|
} catch {
|
|
208
268
|
return this.session.messages;
|
|
209
269
|
}
|
|
@@ -247,6 +307,66 @@ var MenuIAClient = class {
|
|
|
247
307
|
setCustomFields(fields) {
|
|
248
308
|
this.config.customFields = { ...this.config.customFields, ...fields };
|
|
249
309
|
}
|
|
310
|
+
/**
|
|
311
|
+
* Check for proactive messages waiting for this visitor.
|
|
312
|
+
* Call this when the widget opens before the user sends a message.
|
|
313
|
+
* If messages are found, the session is automatically resumed.
|
|
314
|
+
*/
|
|
315
|
+
async checkProactiveMessages() {
|
|
316
|
+
const email = this.config.visitorEmail || this.config.metadata?.email;
|
|
317
|
+
const phone = this.config.visitorPhone || this.config.metadata?.phone;
|
|
318
|
+
if (!email && !phone) return { hasMessages: false, messages: [] };
|
|
319
|
+
try {
|
|
320
|
+
const params = new URLSearchParams();
|
|
321
|
+
if (email) params.set("email", String(email));
|
|
322
|
+
if (phone) params.set("phone", String(phone));
|
|
323
|
+
const res = await fetch(`${this.config.apiUrl}/widget/${this.getWidgetId()}/check-proactive?${params}`);
|
|
324
|
+
if (!res.ok) return { hasMessages: false, messages: [] };
|
|
325
|
+
const data = await res.json();
|
|
326
|
+
if (data.hasMessages && data.conversationId) {
|
|
327
|
+
this.session.conversationId = data.conversationId;
|
|
328
|
+
this.session.messages = (data.messages || []).map((m) => ({
|
|
329
|
+
id: m.id,
|
|
330
|
+
role: m.role,
|
|
331
|
+
content: m.content,
|
|
332
|
+
createdAt: m.createdAt,
|
|
333
|
+
authorName: m.authorName,
|
|
334
|
+
mediaType: m.mediaType,
|
|
335
|
+
mediaUrl: m.mediaUrl
|
|
336
|
+
}));
|
|
337
|
+
this.lastMessageCount = this.session.messages.length;
|
|
338
|
+
this.saveSession();
|
|
339
|
+
for (const msg of this.session.messages) {
|
|
340
|
+
if (msg.role === "assistant") this.config.onMessage?.(msg);
|
|
341
|
+
}
|
|
342
|
+
return { hasMessages: true, messages: this.session.messages };
|
|
343
|
+
}
|
|
344
|
+
return { hasMessages: false, messages: [] };
|
|
345
|
+
} catch {
|
|
346
|
+
return { hasMessages: false, messages: [] };
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* Start polling for proactive messages (before user sends first message).
|
|
351
|
+
* Checks every intervalMs. Stops automatically when messages are found.
|
|
352
|
+
*/
|
|
353
|
+
startProactivePolling(intervalMs = 1e4) {
|
|
354
|
+
const check = async () => {
|
|
355
|
+
const result = await this.checkProactiveMessages();
|
|
356
|
+
if (result.hasMessages) {
|
|
357
|
+
this.stopProactivePolling();
|
|
358
|
+
}
|
|
359
|
+
};
|
|
360
|
+
check();
|
|
361
|
+
this._proactivePollInterval = setInterval(check, intervalMs);
|
|
362
|
+
}
|
|
363
|
+
/** Stop proactive polling */
|
|
364
|
+
stopProactivePolling() {
|
|
365
|
+
if (this._proactivePollInterval) {
|
|
366
|
+
clearInterval(this._proactivePollInterval);
|
|
367
|
+
this._proactivePollInterval = null;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
250
370
|
/** Clear session and start fresh */
|
|
251
371
|
clearSession() {
|
|
252
372
|
this.session = this.createNewSession();
|
|
@@ -256,6 +376,7 @@ var MenuIAClient = class {
|
|
|
256
376
|
/** Cleanup - call when component unmounts */
|
|
257
377
|
destroy() {
|
|
258
378
|
this.stopPolling();
|
|
379
|
+
this.stopProactivePolling();
|
|
259
380
|
}
|
|
260
381
|
// =====================
|
|
261
382
|
// PRIVATE
|