@menuia/react 1.4.2 → 1.5.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.d.mts CHANGED
@@ -42,6 +42,8 @@ interface UseMenuIAReturn {
42
42
  isLoading: boolean;
43
43
  isStreaming: boolean;
44
44
  streamingText: string;
45
+ /** A IA nao respondeu de proposito (atendente assumiu, modo humano, autoReply off, plano vencido). */
46
+ aiPaused: boolean;
45
47
  error: string | null;
46
48
  sendMessage: (content: string, attachments?: MessageAttachment[]) => Promise<void>;
47
49
  clearChat: () => void;
package/dist/index.d.ts CHANGED
@@ -42,6 +42,8 @@ interface UseMenuIAReturn {
42
42
  isLoading: boolean;
43
43
  isStreaming: boolean;
44
44
  streamingText: string;
45
+ /** A IA nao respondeu de proposito (atendente assumiu, modo humano, autoReply off, plano vencido). */
46
+ aiPaused: boolean;
45
47
  error: string | null;
46
48
  sendMessage: (content: string, attachments?: MessageAttachment[]) => Promise<void>;
47
49
  clearChat: () => void;
package/dist/index.js CHANGED
@@ -42,6 +42,12 @@ var import_react2 = __toESM(require("react"));
42
42
  // src/useMenuIA.ts
43
43
  var import_react = require("react");
44
44
  var import_core = require("@menuia/core");
45
+ var normalizar = (s) => (s || "").trim().replace(/\s+/g, " ").toLowerCase();
46
+ function mesmaMensagem(lista, msg) {
47
+ const alvo = normalizar(msg.content);
48
+ return lista.some((m) => m.id === msg.id || !!alvo && m.role === msg.role && normalizar(m.content) === alvo);
49
+ }
50
+ var vazia = (m) => !normalizar(m.content || "") && !m.mediaUrl;
45
51
  function useMenuIA(config) {
46
52
  const clientRef = (0, import_react.useRef)(null);
47
53
  const [messages, setMessages] = (0, import_react.useState)([]);
@@ -51,15 +57,14 @@ function useMenuIA(config) {
51
57
  const [streamingText, setStreamingText] = (0, import_react.useState)("");
52
58
  const [error, setError] = (0, import_react.useState)(null);
53
59
  const [ticketId, setTicketId] = (0, import_react.useState)(null);
60
+ const [aiPaused, setAiPaused] = (0, import_react.useState)(false);
54
61
  (0, import_react.useEffect)(() => {
55
62
  const client = new import_core.MenuIAClient({
56
63
  ...config,
57
64
  onStatusChange: (s) => setStatus(s),
58
65
  onMessage: (msg) => {
59
- setMessages((prev) => {
60
- if (prev.some((m) => m.id === msg.id)) return prev;
61
- return [...prev, msg];
62
- });
66
+ if (vazia(msg)) return;
67
+ setMessages((prev) => mesmaMensagem(prev, msg) ? prev : [...prev, msg]);
63
68
  },
64
69
  onTicketCreated: (id) => setTicketId(id),
65
70
  onError: (err) => setError(err.message)
@@ -100,17 +105,17 @@ function useMenuIA(config) {
100
105
  setStreamingText(fullText);
101
106
  }
102
107
  );
103
- const finalContent = response?.data?.message || streamingText;
104
- const assistantMsg = {
105
- id: `assistant_${Date.now()}`,
106
- role: "assistant",
107
- content: finalContent,
108
- createdAt: (/* @__PURE__ */ new Date()).toISOString()
109
- };
110
- setMessages((prev) => {
111
- const filtered = prev.filter((m) => m.id !== assistantMsg.id);
112
- return [...filtered, assistantMsg];
113
- });
108
+ setAiPaused(!!response?.data?.aiPaused);
109
+ const finalContent = (response?.data?.message || streamingText || "").trim();
110
+ if (finalContent && !response?.data?.aiPaused) {
111
+ const assistantMsg = {
112
+ id: `assistant_${Date.now()}`,
113
+ role: "assistant",
114
+ content: finalContent,
115
+ createdAt: (/* @__PURE__ */ new Date()).toISOString()
116
+ };
117
+ setMessages((prev) => mesmaMensagem(prev, assistantMsg) ? prev : [...prev, assistantMsg]);
118
+ }
114
119
  if (response?.data?.ticketId) {
115
120
  setTicketId(response.data.ticketId);
116
121
  }
@@ -119,13 +124,17 @@ function useMenuIA(config) {
119
124
  setIsStreaming(false);
120
125
  setStreamingText("");
121
126
  const response = await clientRef.current.sendMessage({ content: text, attachments });
122
- const assistantMsg = {
123
- id: `assistant_${Date.now()}_fb`,
124
- role: "assistant",
125
- content: response.data.message,
126
- createdAt: (/* @__PURE__ */ new Date()).toISOString()
127
- };
128
- setMessages((prev) => [...prev, assistantMsg]);
127
+ setAiPaused(!!response?.data?.aiPaused);
128
+ const conteudoFb = (response?.data?.message || "").trim();
129
+ if (conteudoFb && !response?.data?.aiPaused) {
130
+ const fb = {
131
+ id: `assistant_${Date.now()}_fb`,
132
+ role: "assistant",
133
+ content: conteudoFb,
134
+ createdAt: (/* @__PURE__ */ new Date()).toISOString()
135
+ };
136
+ setMessages((prev) => mesmaMensagem(prev, fb) ? prev : [...prev, fb]);
137
+ }
129
138
  } catch (fallbackErr) {
130
139
  const errMsg = fallbackErr instanceof Error ? fallbackErr.message : "Erro ao enviar mensagem";
131
140
  setError(errMsg);
@@ -148,6 +157,7 @@ function useMenuIA(config) {
148
157
  isLoading,
149
158
  isStreaming,
150
159
  streamingText,
160
+ aiPaused,
151
161
  error,
152
162
  sendMessage,
153
163
  clearChat,
package/dist/index.mjs CHANGED
@@ -4,6 +4,12 @@ import React, { useState as useState2, useRef as useRef2, useEffect as useEffect
4
4
  // src/useMenuIA.ts
5
5
  import { useState, useEffect, useRef, useCallback } from "react";
6
6
  import { MenuIAClient } from "@menuia/core";
7
+ var normalizar = (s) => (s || "").trim().replace(/\s+/g, " ").toLowerCase();
8
+ function mesmaMensagem(lista, msg) {
9
+ const alvo = normalizar(msg.content);
10
+ return lista.some((m) => m.id === msg.id || !!alvo && m.role === msg.role && normalizar(m.content) === alvo);
11
+ }
12
+ var vazia = (m) => !normalizar(m.content || "") && !m.mediaUrl;
7
13
  function useMenuIA(config) {
8
14
  const clientRef = useRef(null);
9
15
  const [messages, setMessages] = useState([]);
@@ -13,15 +19,14 @@ function useMenuIA(config) {
13
19
  const [streamingText, setStreamingText] = useState("");
14
20
  const [error, setError] = useState(null);
15
21
  const [ticketId, setTicketId] = useState(null);
22
+ const [aiPaused, setAiPaused] = useState(false);
16
23
  useEffect(() => {
17
24
  const client = new MenuIAClient({
18
25
  ...config,
19
26
  onStatusChange: (s) => setStatus(s),
20
27
  onMessage: (msg) => {
21
- setMessages((prev) => {
22
- if (prev.some((m) => m.id === msg.id)) return prev;
23
- return [...prev, msg];
24
- });
28
+ if (vazia(msg)) return;
29
+ setMessages((prev) => mesmaMensagem(prev, msg) ? prev : [...prev, msg]);
25
30
  },
26
31
  onTicketCreated: (id) => setTicketId(id),
27
32
  onError: (err) => setError(err.message)
@@ -62,17 +67,17 @@ function useMenuIA(config) {
62
67
  setStreamingText(fullText);
63
68
  }
64
69
  );
65
- const finalContent = response?.data?.message || streamingText;
66
- const assistantMsg = {
67
- id: `assistant_${Date.now()}`,
68
- role: "assistant",
69
- content: finalContent,
70
- createdAt: (/* @__PURE__ */ new Date()).toISOString()
71
- };
72
- setMessages((prev) => {
73
- const filtered = prev.filter((m) => m.id !== assistantMsg.id);
74
- return [...filtered, assistantMsg];
75
- });
70
+ setAiPaused(!!response?.data?.aiPaused);
71
+ const finalContent = (response?.data?.message || streamingText || "").trim();
72
+ if (finalContent && !response?.data?.aiPaused) {
73
+ const assistantMsg = {
74
+ id: `assistant_${Date.now()}`,
75
+ role: "assistant",
76
+ content: finalContent,
77
+ createdAt: (/* @__PURE__ */ new Date()).toISOString()
78
+ };
79
+ setMessages((prev) => mesmaMensagem(prev, assistantMsg) ? prev : [...prev, assistantMsg]);
80
+ }
76
81
  if (response?.data?.ticketId) {
77
82
  setTicketId(response.data.ticketId);
78
83
  }
@@ -81,13 +86,17 @@ function useMenuIA(config) {
81
86
  setIsStreaming(false);
82
87
  setStreamingText("");
83
88
  const response = await clientRef.current.sendMessage({ content: text, attachments });
84
- const assistantMsg = {
85
- id: `assistant_${Date.now()}_fb`,
86
- role: "assistant",
87
- content: response.data.message,
88
- createdAt: (/* @__PURE__ */ new Date()).toISOString()
89
- };
90
- setMessages((prev) => [...prev, assistantMsg]);
89
+ setAiPaused(!!response?.data?.aiPaused);
90
+ const conteudoFb = (response?.data?.message || "").trim();
91
+ if (conteudoFb && !response?.data?.aiPaused) {
92
+ const fb = {
93
+ id: `assistant_${Date.now()}_fb`,
94
+ role: "assistant",
95
+ content: conteudoFb,
96
+ createdAt: (/* @__PURE__ */ new Date()).toISOString()
97
+ };
98
+ setMessages((prev) => mesmaMensagem(prev, fb) ? prev : [...prev, fb]);
99
+ }
91
100
  } catch (fallbackErr) {
92
101
  const errMsg = fallbackErr instanceof Error ? fallbackErr.message : "Erro ao enviar mensagem";
93
102
  setError(errMsg);
@@ -110,6 +119,7 @@ function useMenuIA(config) {
110
119
  isLoading,
111
120
  isStreaming,
112
121
  streamingText,
122
+ aiPaused,
113
123
  error,
114
124
  sendMessage,
115
125
  clearChat,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@menuia/react",
3
- "version": "1.4.2",
3
+ "version": "1.5.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.5.0"
20
+ "@menuia/core": "^1.5.2"
21
21
  },
22
22
  "devDependencies": {
23
23
  "@types/react": "^18.2.0",