@menuia/react 1.5.2 → 1.7.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/README.md +29 -0
- package/dist/index.d.mts +20 -3
- package/dist/index.d.ts +20 -3
- package/dist/index.js +108 -6
- package/dist/index.mjs +108 -6
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# @menuia/react
|
|
2
|
+
|
|
3
|
+
Componente e hook React do chat da MenuIA (usa o `@menuia/core`).
|
|
4
|
+
|
|
5
|
+
```tsx
|
|
6
|
+
import { useMenuIA } from '@menuia/react';
|
|
7
|
+
|
|
8
|
+
const chat = useMenuIA({ widgetId: 'SEU_WIDGET_ID' });
|
|
9
|
+
// chat.messages, chat.sendMessage(texto), chat.retryMessage(msg), ...
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Notificação quando a equipe ou a IA responde
|
|
13
|
+
|
|
14
|
+
O hook expõe os mesmos métodos do `MenuIAClient`:
|
|
15
|
+
|
|
16
|
+
```tsx
|
|
17
|
+
// Aviso do navegador com a aba escondida — chame dentro de um clique
|
|
18
|
+
<button onClick={() => chat.ativarNotificacaoNoNavegador({ titulo: 'Minha Loja' })}>
|
|
19
|
+
Avisar quando responderem
|
|
20
|
+
</button>
|
|
21
|
+
|
|
22
|
+
// App (React Native/Expo, ou web com Firebase): registre o token do aparelho
|
|
23
|
+
await chat.registrarPush({ tipo: 'expo', token });
|
|
24
|
+
// ao sair da conta
|
|
25
|
+
await chat.removerPush(token);
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Credenciais, quem recebe, contagem de mensagens e limites: veja o README do `@menuia/core`
|
|
29
|
+
(seção "Notificação quando a equipe ou a IA responde").
|
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
-
import { MenuIAConfig, ChatMessage, ConnectionStatus, MessageAttachment } from '@menuia/core';
|
|
3
|
-
export { ChatMessage, ConnectionStatus, MenuIAClient, MenuIAConfig, MessageAttachment, SendMessageOptions } from '@menuia/core';
|
|
2
|
+
import { MenuIAConfig, ChatMessage, ConnectionStatus, MessageAttachment, RegistrarPushOptions, RegistrarPushResult, NotificacaoNavegadorOptions } from '@menuia/core';
|
|
3
|
+
export { ChatMessage, ConnectionStatus, InteractiveButton, InteractivePayload, MenuIAClient, MenuIAConfig, MessageAttachment, NotificacaoNavegadorOptions, RegistrarPushOptions, RegistrarPushResult, SendMessageOptions } from '@menuia/core';
|
|
4
4
|
|
|
5
5
|
interface MenuIAChatProps extends Omit<MenuIAConfig, 'widgetId' | 'agentId'> {
|
|
6
6
|
/** Widget ID (from /dashboard/channels) - preferred */
|
|
@@ -34,7 +34,7 @@ interface MenuIAChatProps extends Omit<MenuIAConfig, 'widgetId' | 'agentId'> {
|
|
|
34
34
|
/** Show powered by MenuIA */
|
|
35
35
|
showPoweredBy?: boolean;
|
|
36
36
|
}
|
|
37
|
-
declare function MenuIAChat({ title, subtitle, avatar, primaryColor, theme, position, welcomeMessage, placeholder, defaultOpen, inline, width, height, showPoweredBy, widgetId, agentId, apiUrl, sessionId, customFields, metadata, channel, onMessage, onStatusChange, onTicketCreated, onError, }: MenuIAChatProps): react_jsx_runtime.JSX.Element;
|
|
37
|
+
declare function MenuIAChat({ title, subtitle, avatar, primaryColor, theme, position, welcomeMessage, placeholder, defaultOpen, inline, width, height, showPoweredBy, widgetId, agentId, apiUrl, sessionId, customFields, metadata, channel, sessionToken, visitorName, visitorEmail, visitorPhone, onMessage, onStatusChange, onTicketCreated, onError, onSessionTokenExpired, }: MenuIAChatProps): react_jsx_runtime.JSX.Element;
|
|
38
38
|
|
|
39
39
|
interface UseMenuIAReturn {
|
|
40
40
|
messages: ChatMessage[];
|
|
@@ -46,8 +46,25 @@ interface UseMenuIAReturn {
|
|
|
46
46
|
aiPaused: boolean;
|
|
47
47
|
error: string | null;
|
|
48
48
|
sendMessage: (content: string, attachments?: MessageAttachment[]) => Promise<void>;
|
|
49
|
+
/**
|
|
50
|
+
* Reenvia uma mensagem marcada com `failed` (ver `ChatMessage.failed`), sem criar outra bolha.
|
|
51
|
+
* Reenvia tambem o anexo original, quando havia um.
|
|
52
|
+
*/
|
|
53
|
+
retryMessage: (message: ChatMessage) => Promise<void>;
|
|
49
54
|
clearChat: () => void;
|
|
50
55
|
ticketId: string | null;
|
|
56
|
+
/**
|
|
57
|
+
* Registra o aparelho para receber notificacao quando a equipe/IA responder, com o app fechado
|
|
58
|
+
* (token do FCM ou do Expo). Ver `MenuIAClient.registrarPush`.
|
|
59
|
+
*/
|
|
60
|
+
registrarPush: (opcoes: RegistrarPushOptions) => Promise<RegistrarPushResult>;
|
|
61
|
+
/** Desliga a notificacao neste aparelho (ex.: ao sair da conta). */
|
|
62
|
+
removerPush: (token: string) => Promise<RegistrarPushResult>;
|
|
63
|
+
/**
|
|
64
|
+
* Aviso do navegador com a aba escondida. ⚠️ Chame dentro de um clique (o navegador so pede
|
|
65
|
+
* permissao em resposta a um gesto). Ver `MenuIAClient.ativarNotificacaoNoNavegador`.
|
|
66
|
+
*/
|
|
67
|
+
ativarNotificacaoNoNavegador: (opcoes?: NotificacaoNavegadorOptions) => Promise<NotificationPermission | 'indisponivel'>;
|
|
51
68
|
}
|
|
52
69
|
declare function useMenuIA(config: MenuIAConfig): UseMenuIAReturn;
|
|
53
70
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
-
import { MenuIAConfig, ChatMessage, ConnectionStatus, MessageAttachment } from '@menuia/core';
|
|
3
|
-
export { ChatMessage, ConnectionStatus, MenuIAClient, MenuIAConfig, MessageAttachment, SendMessageOptions } from '@menuia/core';
|
|
2
|
+
import { MenuIAConfig, ChatMessage, ConnectionStatus, MessageAttachment, RegistrarPushOptions, RegistrarPushResult, NotificacaoNavegadorOptions } from '@menuia/core';
|
|
3
|
+
export { ChatMessage, ConnectionStatus, InteractiveButton, InteractivePayload, MenuIAClient, MenuIAConfig, MessageAttachment, NotificacaoNavegadorOptions, RegistrarPushOptions, RegistrarPushResult, SendMessageOptions } from '@menuia/core';
|
|
4
4
|
|
|
5
5
|
interface MenuIAChatProps extends Omit<MenuIAConfig, 'widgetId' | 'agentId'> {
|
|
6
6
|
/** Widget ID (from /dashboard/channels) - preferred */
|
|
@@ -34,7 +34,7 @@ interface MenuIAChatProps extends Omit<MenuIAConfig, 'widgetId' | 'agentId'> {
|
|
|
34
34
|
/** Show powered by MenuIA */
|
|
35
35
|
showPoweredBy?: boolean;
|
|
36
36
|
}
|
|
37
|
-
declare function MenuIAChat({ title, subtitle, avatar, primaryColor, theme, position, welcomeMessage, placeholder, defaultOpen, inline, width, height, showPoweredBy, widgetId, agentId, apiUrl, sessionId, customFields, metadata, channel, onMessage, onStatusChange, onTicketCreated, onError, }: MenuIAChatProps): react_jsx_runtime.JSX.Element;
|
|
37
|
+
declare function MenuIAChat({ title, subtitle, avatar, primaryColor, theme, position, welcomeMessage, placeholder, defaultOpen, inline, width, height, showPoweredBy, widgetId, agentId, apiUrl, sessionId, customFields, metadata, channel, sessionToken, visitorName, visitorEmail, visitorPhone, onMessage, onStatusChange, onTicketCreated, onError, onSessionTokenExpired, }: MenuIAChatProps): react_jsx_runtime.JSX.Element;
|
|
38
38
|
|
|
39
39
|
interface UseMenuIAReturn {
|
|
40
40
|
messages: ChatMessage[];
|
|
@@ -46,8 +46,25 @@ interface UseMenuIAReturn {
|
|
|
46
46
|
aiPaused: boolean;
|
|
47
47
|
error: string | null;
|
|
48
48
|
sendMessage: (content: string, attachments?: MessageAttachment[]) => Promise<void>;
|
|
49
|
+
/**
|
|
50
|
+
* Reenvia uma mensagem marcada com `failed` (ver `ChatMessage.failed`), sem criar outra bolha.
|
|
51
|
+
* Reenvia tambem o anexo original, quando havia um.
|
|
52
|
+
*/
|
|
53
|
+
retryMessage: (message: ChatMessage) => Promise<void>;
|
|
49
54
|
clearChat: () => void;
|
|
50
55
|
ticketId: string | null;
|
|
56
|
+
/**
|
|
57
|
+
* Registra o aparelho para receber notificacao quando a equipe/IA responder, com o app fechado
|
|
58
|
+
* (token do FCM ou do Expo). Ver `MenuIAClient.registrarPush`.
|
|
59
|
+
*/
|
|
60
|
+
registrarPush: (opcoes: RegistrarPushOptions) => Promise<RegistrarPushResult>;
|
|
61
|
+
/** Desliga a notificacao neste aparelho (ex.: ao sair da conta). */
|
|
62
|
+
removerPush: (token: string) => Promise<RegistrarPushResult>;
|
|
63
|
+
/**
|
|
64
|
+
* Aviso do navegador com a aba escondida. ⚠️ Chame dentro de um clique (o navegador so pede
|
|
65
|
+
* permissao em resposta a um gesto). Ver `MenuIAClient.ativarNotificacaoNoNavegador`.
|
|
66
|
+
*/
|
|
67
|
+
ativarNotificacaoNoNavegador: (opcoes?: NotificacaoNavegadorOptions) => Promise<NotificationPermission | 'indisponivel'>;
|
|
51
68
|
}
|
|
52
69
|
declare function useMenuIA(config: MenuIAConfig): UseMenuIAReturn;
|
|
53
70
|
|
package/dist/index.js
CHANGED
|
@@ -64,16 +64,30 @@ function useMenuIA(config) {
|
|
|
64
64
|
const [error, setError] = (0, import_react.useState)(null);
|
|
65
65
|
const [ticketId, setTicketId] = (0, import_react.useState)(null);
|
|
66
66
|
const [aiPaused, setAiPaused] = (0, import_react.useState)(false);
|
|
67
|
+
const configRef = (0, import_react.useRef)(config);
|
|
68
|
+
configRef.current = config;
|
|
67
69
|
(0, import_react.useEffect)(() => {
|
|
68
70
|
const client = new import_core.MenuIAClient({
|
|
69
71
|
...config,
|
|
70
|
-
onStatusChange: (s) =>
|
|
72
|
+
onStatusChange: (s) => {
|
|
73
|
+
setStatus(s);
|
|
74
|
+
configRef.current.onStatusChange?.(s);
|
|
75
|
+
},
|
|
71
76
|
onMessage: (msg) => {
|
|
72
77
|
if (vazia(msg)) return;
|
|
73
78
|
setMessages((prev) => mesmaMensagem(prev, msg) ? prev : [...prev, msg]);
|
|
79
|
+
configRef.current.onMessage?.(msg);
|
|
80
|
+
},
|
|
81
|
+
onTicketCreated: (id) => {
|
|
82
|
+
setTicketId(id);
|
|
83
|
+
configRef.current.onTicketCreated?.(id);
|
|
84
|
+
},
|
|
85
|
+
// ⚠️ Antes o hook trocava os callbacks do app pelos dele: o onError do app nunca era chamado.
|
|
86
|
+
onError: (err) => {
|
|
87
|
+
setError(err.message);
|
|
88
|
+
configRef.current.onError?.(err);
|
|
74
89
|
},
|
|
75
|
-
|
|
76
|
-
onError: (err) => setError(err.message)
|
|
90
|
+
onSessionTokenExpired: () => configRef.current.onSessionTokenExpired?.()
|
|
77
91
|
});
|
|
78
92
|
clientRef.current = client;
|
|
79
93
|
client.loadHistory().then((msgs) => {
|
|
@@ -86,6 +100,10 @@ function useMenuIA(config) {
|
|
|
86
100
|
clientRef.current = null;
|
|
87
101
|
};
|
|
88
102
|
}, [config.widgetId, config.agentId, config.apiUrl]);
|
|
103
|
+
(0, import_react.useEffect)(() => {
|
|
104
|
+
if (config.sessionToken) clientRef.current?.setSessionToken(config.sessionToken);
|
|
105
|
+
}, [config.sessionToken]);
|
|
106
|
+
const anexosPorBolha = (0, import_react.useRef)(/* @__PURE__ */ new Map());
|
|
89
107
|
const sendMessage = (0, import_react.useCallback)(async (content, attachments) => {
|
|
90
108
|
const text = content.trim();
|
|
91
109
|
const hasAttachments = !!(attachments && attachments.length);
|
|
@@ -102,6 +120,7 @@ function useMenuIA(config) {
|
|
|
102
120
|
mediaType: firstAtt ? firstAtt.mimeType : void 0
|
|
103
121
|
};
|
|
104
122
|
setMessages((prev) => [...prev, userMsg]);
|
|
123
|
+
if (hasAttachments) anexosPorBolha.current.set(userMsg.id, attachments);
|
|
105
124
|
try {
|
|
106
125
|
setIsStreaming(true);
|
|
107
126
|
setStreamingText("");
|
|
@@ -144,6 +163,61 @@ function useMenuIA(config) {
|
|
|
144
163
|
} catch (fallbackErr) {
|
|
145
164
|
const errMsg = fallbackErr instanceof Error ? fallbackErr.message : "Erro ao enviar mensagem";
|
|
146
165
|
setError(errMsg);
|
|
166
|
+
setMessages((prev) => prev.map((m) => m.id === userMsg.id ? { ...m, failed: true } : m));
|
|
167
|
+
}
|
|
168
|
+
} finally {
|
|
169
|
+
setIsLoading(false);
|
|
170
|
+
setIsStreaming(false);
|
|
171
|
+
setStreamingText("");
|
|
172
|
+
}
|
|
173
|
+
}, []);
|
|
174
|
+
const retryMessage = (0, import_react.useCallback)(async (message) => {
|
|
175
|
+
if (!clientRef.current || !message) return;
|
|
176
|
+
const atts = anexosPorBolha.current.get(message.id);
|
|
177
|
+
const text = (message.content || "").trim();
|
|
178
|
+
if (!text && !(atts && atts.length)) return;
|
|
179
|
+
setError(null);
|
|
180
|
+
setIsLoading(true);
|
|
181
|
+
setMessages((prev) => prev.map((m) => m.id === message.id ? { ...m, failed: false } : m));
|
|
182
|
+
try {
|
|
183
|
+
setIsStreaming(true);
|
|
184
|
+
setStreamingText("");
|
|
185
|
+
const response = await clientRef.current.sendMessageStream(
|
|
186
|
+
{ content: text, attachments: atts },
|
|
187
|
+
(_token, fullText) => setStreamingText(fullText)
|
|
188
|
+
);
|
|
189
|
+
setAiPaused(!!response?.data?.aiPaused);
|
|
190
|
+
const finalContent = (response?.data?.message || "").trim();
|
|
191
|
+
if (finalContent && !response?.data?.aiPaused) {
|
|
192
|
+
const assistantMsg = {
|
|
193
|
+
id: `assistant_${Date.now()}`,
|
|
194
|
+
role: "assistant",
|
|
195
|
+
content: finalContent,
|
|
196
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
197
|
+
};
|
|
198
|
+
setMessages((prev) => mesmaMensagem(prev, assistantMsg) ? prev : [...prev, assistantMsg]);
|
|
199
|
+
}
|
|
200
|
+
if (response?.data?.ticketId) setTicketId(response.data.ticketId);
|
|
201
|
+
} catch (err) {
|
|
202
|
+
try {
|
|
203
|
+
setIsStreaming(false);
|
|
204
|
+
setStreamingText("");
|
|
205
|
+
const response = await clientRef.current.sendMessage({ content: text, attachments: atts });
|
|
206
|
+
setAiPaused(!!response?.data?.aiPaused);
|
|
207
|
+
const conteudoFb = (response?.data?.message || "").trim();
|
|
208
|
+
if (conteudoFb && !response?.data?.aiPaused) {
|
|
209
|
+
const fb = {
|
|
210
|
+
id: `assistant_${Date.now()}_fb`,
|
|
211
|
+
role: "assistant",
|
|
212
|
+
content: conteudoFb,
|
|
213
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
214
|
+
};
|
|
215
|
+
setMessages((prev) => mesmaMensagem(prev, fb) ? prev : [...prev, fb]);
|
|
216
|
+
}
|
|
217
|
+
} catch (fallbackErr) {
|
|
218
|
+
const errMsg = fallbackErr instanceof Error ? fallbackErr.message : "Erro ao reenviar mensagem";
|
|
219
|
+
setError(errMsg);
|
|
220
|
+
setMessages((prev) => prev.map((m) => m.id === message.id ? { ...m, failed: true } : m));
|
|
147
221
|
}
|
|
148
222
|
} finally {
|
|
149
223
|
setIsLoading(false);
|
|
@@ -151,7 +225,20 @@ function useMenuIA(config) {
|
|
|
151
225
|
setStreamingText("");
|
|
152
226
|
}
|
|
153
227
|
}, []);
|
|
228
|
+
const registrarPush = (0, import_react.useCallback)(async (opcoes) => {
|
|
229
|
+
if (!clientRef.current) return { ok: false, erro: "chat ainda nao iniciado" };
|
|
230
|
+
return clientRef.current.registrarPush(opcoes);
|
|
231
|
+
}, []);
|
|
232
|
+
const removerPush = (0, import_react.useCallback)(async (token) => {
|
|
233
|
+
if (!clientRef.current) return { ok: false, erro: "chat ainda nao iniciado" };
|
|
234
|
+
return clientRef.current.removerPush(token);
|
|
235
|
+
}, []);
|
|
236
|
+
const ativarNotificacaoNoNavegador = (0, import_react.useCallback)(async (opcoes) => {
|
|
237
|
+
if (!clientRef.current) return "indisponivel";
|
|
238
|
+
return clientRef.current.ativarNotificacaoNoNavegador(opcoes);
|
|
239
|
+
}, []);
|
|
154
240
|
const clearChat = (0, import_react.useCallback)(() => {
|
|
241
|
+
anexosPorBolha.current.clear();
|
|
155
242
|
clientRef.current?.clearSession();
|
|
156
243
|
setMessages([]);
|
|
157
244
|
setTicketId(null);
|
|
@@ -166,8 +253,12 @@ function useMenuIA(config) {
|
|
|
166
253
|
aiPaused,
|
|
167
254
|
error,
|
|
168
255
|
sendMessage,
|
|
256
|
+
retryMessage,
|
|
169
257
|
clearChat,
|
|
170
|
-
ticketId
|
|
258
|
+
ticketId,
|
|
259
|
+
registrarPush,
|
|
260
|
+
removerPush,
|
|
261
|
+
ativarNotificacaoNoNavegador
|
|
171
262
|
};
|
|
172
263
|
}
|
|
173
264
|
|
|
@@ -507,10 +598,15 @@ function MenuIAChat({
|
|
|
507
598
|
customFields,
|
|
508
599
|
metadata,
|
|
509
600
|
channel,
|
|
601
|
+
sessionToken,
|
|
602
|
+
visitorName,
|
|
603
|
+
visitorEmail,
|
|
604
|
+
visitorPhone,
|
|
510
605
|
onMessage,
|
|
511
606
|
onStatusChange,
|
|
512
607
|
onTicketCreated,
|
|
513
|
-
onError
|
|
608
|
+
onError,
|
|
609
|
+
onSessionTokenExpired
|
|
514
610
|
}) {
|
|
515
611
|
const [isOpen, setIsOpen] = (0, import_react2.useState)(defaultOpen || inline);
|
|
516
612
|
const [input, setInput] = (0, import_react2.useState)("");
|
|
@@ -535,10 +631,16 @@ function MenuIAChat({
|
|
|
535
631
|
customFields,
|
|
536
632
|
metadata,
|
|
537
633
|
channel,
|
|
634
|
+
// Widget privado: sem repassar o token o componente abria a sessao sem credencial (401).
|
|
635
|
+
sessionToken,
|
|
636
|
+
visitorName,
|
|
637
|
+
visitorEmail,
|
|
638
|
+
visitorPhone,
|
|
538
639
|
onMessage,
|
|
539
640
|
onStatusChange,
|
|
540
641
|
onTicketCreated,
|
|
541
|
-
onError
|
|
642
|
+
onError,
|
|
643
|
+
onSessionTokenExpired
|
|
542
644
|
});
|
|
543
645
|
const styles = getStyles(primaryColor, theme);
|
|
544
646
|
(0, import_react2.useEffect)(() => {
|
package/dist/index.mjs
CHANGED
|
@@ -26,16 +26,30 @@ function useMenuIA(config) {
|
|
|
26
26
|
const [error, setError] = useState(null);
|
|
27
27
|
const [ticketId, setTicketId] = useState(null);
|
|
28
28
|
const [aiPaused, setAiPaused] = useState(false);
|
|
29
|
+
const configRef = useRef(config);
|
|
30
|
+
configRef.current = config;
|
|
29
31
|
useEffect(() => {
|
|
30
32
|
const client = new MenuIAClient({
|
|
31
33
|
...config,
|
|
32
|
-
onStatusChange: (s) =>
|
|
34
|
+
onStatusChange: (s) => {
|
|
35
|
+
setStatus(s);
|
|
36
|
+
configRef.current.onStatusChange?.(s);
|
|
37
|
+
},
|
|
33
38
|
onMessage: (msg) => {
|
|
34
39
|
if (vazia(msg)) return;
|
|
35
40
|
setMessages((prev) => mesmaMensagem(prev, msg) ? prev : [...prev, msg]);
|
|
41
|
+
configRef.current.onMessage?.(msg);
|
|
42
|
+
},
|
|
43
|
+
onTicketCreated: (id) => {
|
|
44
|
+
setTicketId(id);
|
|
45
|
+
configRef.current.onTicketCreated?.(id);
|
|
46
|
+
},
|
|
47
|
+
// ⚠️ Antes o hook trocava os callbacks do app pelos dele: o onError do app nunca era chamado.
|
|
48
|
+
onError: (err) => {
|
|
49
|
+
setError(err.message);
|
|
50
|
+
configRef.current.onError?.(err);
|
|
36
51
|
},
|
|
37
|
-
|
|
38
|
-
onError: (err) => setError(err.message)
|
|
52
|
+
onSessionTokenExpired: () => configRef.current.onSessionTokenExpired?.()
|
|
39
53
|
});
|
|
40
54
|
clientRef.current = client;
|
|
41
55
|
client.loadHistory().then((msgs) => {
|
|
@@ -48,6 +62,10 @@ function useMenuIA(config) {
|
|
|
48
62
|
clientRef.current = null;
|
|
49
63
|
};
|
|
50
64
|
}, [config.widgetId, config.agentId, config.apiUrl]);
|
|
65
|
+
useEffect(() => {
|
|
66
|
+
if (config.sessionToken) clientRef.current?.setSessionToken(config.sessionToken);
|
|
67
|
+
}, [config.sessionToken]);
|
|
68
|
+
const anexosPorBolha = useRef(/* @__PURE__ */ new Map());
|
|
51
69
|
const sendMessage = useCallback(async (content, attachments) => {
|
|
52
70
|
const text = content.trim();
|
|
53
71
|
const hasAttachments = !!(attachments && attachments.length);
|
|
@@ -64,6 +82,7 @@ function useMenuIA(config) {
|
|
|
64
82
|
mediaType: firstAtt ? firstAtt.mimeType : void 0
|
|
65
83
|
};
|
|
66
84
|
setMessages((prev) => [...prev, userMsg]);
|
|
85
|
+
if (hasAttachments) anexosPorBolha.current.set(userMsg.id, attachments);
|
|
67
86
|
try {
|
|
68
87
|
setIsStreaming(true);
|
|
69
88
|
setStreamingText("");
|
|
@@ -106,6 +125,61 @@ function useMenuIA(config) {
|
|
|
106
125
|
} catch (fallbackErr) {
|
|
107
126
|
const errMsg = fallbackErr instanceof Error ? fallbackErr.message : "Erro ao enviar mensagem";
|
|
108
127
|
setError(errMsg);
|
|
128
|
+
setMessages((prev) => prev.map((m) => m.id === userMsg.id ? { ...m, failed: true } : m));
|
|
129
|
+
}
|
|
130
|
+
} finally {
|
|
131
|
+
setIsLoading(false);
|
|
132
|
+
setIsStreaming(false);
|
|
133
|
+
setStreamingText("");
|
|
134
|
+
}
|
|
135
|
+
}, []);
|
|
136
|
+
const retryMessage = useCallback(async (message) => {
|
|
137
|
+
if (!clientRef.current || !message) return;
|
|
138
|
+
const atts = anexosPorBolha.current.get(message.id);
|
|
139
|
+
const text = (message.content || "").trim();
|
|
140
|
+
if (!text && !(atts && atts.length)) return;
|
|
141
|
+
setError(null);
|
|
142
|
+
setIsLoading(true);
|
|
143
|
+
setMessages((prev) => prev.map((m) => m.id === message.id ? { ...m, failed: false } : m));
|
|
144
|
+
try {
|
|
145
|
+
setIsStreaming(true);
|
|
146
|
+
setStreamingText("");
|
|
147
|
+
const response = await clientRef.current.sendMessageStream(
|
|
148
|
+
{ content: text, attachments: atts },
|
|
149
|
+
(_token, fullText) => setStreamingText(fullText)
|
|
150
|
+
);
|
|
151
|
+
setAiPaused(!!response?.data?.aiPaused);
|
|
152
|
+
const finalContent = (response?.data?.message || "").trim();
|
|
153
|
+
if (finalContent && !response?.data?.aiPaused) {
|
|
154
|
+
const assistantMsg = {
|
|
155
|
+
id: `assistant_${Date.now()}`,
|
|
156
|
+
role: "assistant",
|
|
157
|
+
content: finalContent,
|
|
158
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
159
|
+
};
|
|
160
|
+
setMessages((prev) => mesmaMensagem(prev, assistantMsg) ? prev : [...prev, assistantMsg]);
|
|
161
|
+
}
|
|
162
|
+
if (response?.data?.ticketId) setTicketId(response.data.ticketId);
|
|
163
|
+
} catch (err) {
|
|
164
|
+
try {
|
|
165
|
+
setIsStreaming(false);
|
|
166
|
+
setStreamingText("");
|
|
167
|
+
const response = await clientRef.current.sendMessage({ content: text, attachments: atts });
|
|
168
|
+
setAiPaused(!!response?.data?.aiPaused);
|
|
169
|
+
const conteudoFb = (response?.data?.message || "").trim();
|
|
170
|
+
if (conteudoFb && !response?.data?.aiPaused) {
|
|
171
|
+
const fb = {
|
|
172
|
+
id: `assistant_${Date.now()}_fb`,
|
|
173
|
+
role: "assistant",
|
|
174
|
+
content: conteudoFb,
|
|
175
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
176
|
+
};
|
|
177
|
+
setMessages((prev) => mesmaMensagem(prev, fb) ? prev : [...prev, fb]);
|
|
178
|
+
}
|
|
179
|
+
} catch (fallbackErr) {
|
|
180
|
+
const errMsg = fallbackErr instanceof Error ? fallbackErr.message : "Erro ao reenviar mensagem";
|
|
181
|
+
setError(errMsg);
|
|
182
|
+
setMessages((prev) => prev.map((m) => m.id === message.id ? { ...m, failed: true } : m));
|
|
109
183
|
}
|
|
110
184
|
} finally {
|
|
111
185
|
setIsLoading(false);
|
|
@@ -113,7 +187,20 @@ function useMenuIA(config) {
|
|
|
113
187
|
setStreamingText("");
|
|
114
188
|
}
|
|
115
189
|
}, []);
|
|
190
|
+
const registrarPush = useCallback(async (opcoes) => {
|
|
191
|
+
if (!clientRef.current) return { ok: false, erro: "chat ainda nao iniciado" };
|
|
192
|
+
return clientRef.current.registrarPush(opcoes);
|
|
193
|
+
}, []);
|
|
194
|
+
const removerPush = useCallback(async (token) => {
|
|
195
|
+
if (!clientRef.current) return { ok: false, erro: "chat ainda nao iniciado" };
|
|
196
|
+
return clientRef.current.removerPush(token);
|
|
197
|
+
}, []);
|
|
198
|
+
const ativarNotificacaoNoNavegador = useCallback(async (opcoes) => {
|
|
199
|
+
if (!clientRef.current) return "indisponivel";
|
|
200
|
+
return clientRef.current.ativarNotificacaoNoNavegador(opcoes);
|
|
201
|
+
}, []);
|
|
116
202
|
const clearChat = useCallback(() => {
|
|
203
|
+
anexosPorBolha.current.clear();
|
|
117
204
|
clientRef.current?.clearSession();
|
|
118
205
|
setMessages([]);
|
|
119
206
|
setTicketId(null);
|
|
@@ -128,8 +215,12 @@ function useMenuIA(config) {
|
|
|
128
215
|
aiPaused,
|
|
129
216
|
error,
|
|
130
217
|
sendMessage,
|
|
218
|
+
retryMessage,
|
|
131
219
|
clearChat,
|
|
132
|
-
ticketId
|
|
220
|
+
ticketId,
|
|
221
|
+
registrarPush,
|
|
222
|
+
removerPush,
|
|
223
|
+
ativarNotificacaoNoNavegador
|
|
133
224
|
};
|
|
134
225
|
}
|
|
135
226
|
|
|
@@ -469,10 +560,15 @@ function MenuIAChat({
|
|
|
469
560
|
customFields,
|
|
470
561
|
metadata,
|
|
471
562
|
channel,
|
|
563
|
+
sessionToken,
|
|
564
|
+
visitorName,
|
|
565
|
+
visitorEmail,
|
|
566
|
+
visitorPhone,
|
|
472
567
|
onMessage,
|
|
473
568
|
onStatusChange,
|
|
474
569
|
onTicketCreated,
|
|
475
|
-
onError
|
|
570
|
+
onError,
|
|
571
|
+
onSessionTokenExpired
|
|
476
572
|
}) {
|
|
477
573
|
const [isOpen, setIsOpen] = useState2(defaultOpen || inline);
|
|
478
574
|
const [input, setInput] = useState2("");
|
|
@@ -497,10 +593,16 @@ function MenuIAChat({
|
|
|
497
593
|
customFields,
|
|
498
594
|
metadata,
|
|
499
595
|
channel,
|
|
596
|
+
// Widget privado: sem repassar o token o componente abria a sessao sem credencial (401).
|
|
597
|
+
sessionToken,
|
|
598
|
+
visitorName,
|
|
599
|
+
visitorEmail,
|
|
600
|
+
visitorPhone,
|
|
500
601
|
onMessage,
|
|
501
602
|
onStatusChange,
|
|
502
603
|
onTicketCreated,
|
|
503
|
-
onError
|
|
604
|
+
onError,
|
|
605
|
+
onSessionTokenExpired
|
|
504
606
|
});
|
|
505
607
|
const styles = getStyles(primaryColor, theme);
|
|
506
608
|
useEffect2(() => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@menuia/react",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.7.1",
|
|
4
4
|
"description": "MenuIA Chat SDK - React Component",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"react-dom": ">=17.0.0"
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@menuia/core": "^1.
|
|
20
|
+
"@menuia/core": "^1.7.1"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
23
|
"@types/react": "^18.2.0",
|