@controleonline/ui-crm 1.0.69 → 1.0.74
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/.release/rc-manifest.json +7 -7
- package/package.json +4 -3
- package/src/react/components/conversation/Attachment.js +61 -0
- package/src/react/components/conversation/AudioPlayer.native.js +28 -0
- package/src/react/components/conversation/AudioPlayer.web.js +9 -0
- package/src/react/components/conversation/Composer.js +134 -0
- package/src/react/components/conversation/ConversationView.js +91 -0
- package/src/react/components/conversation/MessageBubble.js +68 -0
- package/src/react/components/conversation/OptionPicker.js +62 -0
- package/src/react/components/conversation/RichText.js +34 -0
- package/src/react/components/conversation/fileApi.js +78 -0
- package/src/react/components/conversation/formatText.js +61 -0
- package/src/react/components/conversation/requestApi.js +91 -0
- package/src/react/components/conversation/useAudioRecording.native.js +31 -0
- package/src/react/components/conversation/useAudioRecording.web.js +54 -0
- package/src/react/components/conversation/useChatPalette.js +41 -0
- package/src/react/pages/crm/conversation.js +25 -326
- package/src/react/pages/mcp/McpPage.js +134 -130
- package/src/react/pages/mcp/useRequests.js +127 -0
- package/src/react/pages/settings/GeneralSettings.js +8 -0
- package/src/react/pages/settings/sections/TicketSettingsSection.js +124 -0
- package/src/react/pages/tickets/TicketConversationPage.js +5 -164
- package/src/react/pages/tickets/TicketFunnelPage.js +5 -1
- package/src/react/pages/tickets/TicketWorkspace.js +245 -0
- package/src/react/router/routes.js +7 -7
- package/src/tests/react/components/conversation/formatText.test.js +40 -0
- package/src/react/pages/crm/conversation.styles.js +0 -206
- package/src/react/pages/mcp/McpPage.styles.js +0 -132
- package/src/react/pages/mcp/useMcpChat.js +0 -277
|
@@ -1,206 +0,0 @@
|
|
|
1
|
-
import { StyleSheet } from 'react-native';
|
|
2
|
-
|
|
3
|
-
const styles = StyleSheet.create({
|
|
4
|
-
container: {
|
|
5
|
-
flex: 1,
|
|
6
|
-
backgroundColor: '#EEEAE5',
|
|
7
|
-
},
|
|
8
|
-
header: {
|
|
9
|
-
backgroundColor: '#26C865',
|
|
10
|
-
flexDirection: 'row',
|
|
11
|
-
alignItems: 'center',
|
|
12
|
-
paddingHorizontal: 14,
|
|
13
|
-
paddingBottom: 10,
|
|
14
|
-
borderBottomWidth: 1,
|
|
15
|
-
borderBottomColor: 'rgba(0,0,0,0.04)',
|
|
16
|
-
},
|
|
17
|
-
backButton: {
|
|
18
|
-
marginRight: 12,
|
|
19
|
-
padding: 6,
|
|
20
|
-
},
|
|
21
|
-
headerInfo: {
|
|
22
|
-
flex: 1,
|
|
23
|
-
flexDirection: 'row',
|
|
24
|
-
alignItems: 'center',
|
|
25
|
-
},
|
|
26
|
-
avatar: {
|
|
27
|
-
width: 34,
|
|
28
|
-
height: 34,
|
|
29
|
-
borderRadius: 17,
|
|
30
|
-
backgroundColor: 'rgba(255, 255, 255, 0.18)',
|
|
31
|
-
alignItems: 'center',
|
|
32
|
-
justifyContent: 'center',
|
|
33
|
-
marginRight: 10,
|
|
34
|
-
},
|
|
35
|
-
headerText: {
|
|
36
|
-
flex: 1,
|
|
37
|
-
},
|
|
38
|
-
headerName: {
|
|
39
|
-
fontSize: 17,
|
|
40
|
-
fontWeight: '400',
|
|
41
|
-
color: '#FFFFFF',
|
|
42
|
-
},
|
|
43
|
-
messagesContainer: {
|
|
44
|
-
flex: 1,
|
|
45
|
-
},
|
|
46
|
-
messagesContent: {
|
|
47
|
-
paddingTop: 10,
|
|
48
|
-
paddingBottom: 14,
|
|
49
|
-
},
|
|
50
|
-
messagesContentEmpty: {
|
|
51
|
-
flexGrow: 1,
|
|
52
|
-
justifyContent: 'center',
|
|
53
|
-
},
|
|
54
|
-
emptyStateContainer: {
|
|
55
|
-
alignItems: 'center',
|
|
56
|
-
justifyContent: 'center',
|
|
57
|
-
paddingHorizontal: 28,
|
|
58
|
-
},
|
|
59
|
-
emptyStateIconCircle: {
|
|
60
|
-
width: 132,
|
|
61
|
-
height: 132,
|
|
62
|
-
borderRadius: 66,
|
|
63
|
-
backgroundColor: '#E7EDF3',
|
|
64
|
-
alignItems: 'center',
|
|
65
|
-
justifyContent: 'center',
|
|
66
|
-
marginBottom: 18,
|
|
67
|
-
},
|
|
68
|
-
emptyStateTitle: {
|
|
69
|
-
fontSize: 18,
|
|
70
|
-
color: '#334155',
|
|
71
|
-
fontWeight: '400',
|
|
72
|
-
marginBottom: 6,
|
|
73
|
-
textAlign: 'center',
|
|
74
|
-
},
|
|
75
|
-
emptyStateSubtitle: {
|
|
76
|
-
fontSize: 14,
|
|
77
|
-
color: '#64748B',
|
|
78
|
-
textAlign: 'center',
|
|
79
|
-
lineHeight: 20,
|
|
80
|
-
},
|
|
81
|
-
dateContainer: {
|
|
82
|
-
alignItems: 'center',
|
|
83
|
-
marginVertical: 14,
|
|
84
|
-
},
|
|
85
|
-
dateText: {
|
|
86
|
-
backgroundColor: '#D9D4CD',
|
|
87
|
-
color: '#73808F',
|
|
88
|
-
fontSize: 12,
|
|
89
|
-
paddingHorizontal: 11,
|
|
90
|
-
paddingVertical: 3,
|
|
91
|
-
borderRadius: 12,
|
|
92
|
-
},
|
|
93
|
-
messageContainer: {
|
|
94
|
-
marginVertical: 2,
|
|
95
|
-
marginHorizontal: 12,
|
|
96
|
-
},
|
|
97
|
-
userMessage: {
|
|
98
|
-
alignItems: 'flex-end',
|
|
99
|
-
},
|
|
100
|
-
otherMessage: {
|
|
101
|
-
alignItems: 'flex-start',
|
|
102
|
-
},
|
|
103
|
-
messageBubble: {
|
|
104
|
-
maxWidth: '80%',
|
|
105
|
-
paddingHorizontal: 11,
|
|
106
|
-
paddingTop: 8,
|
|
107
|
-
paddingBottom: 7,
|
|
108
|
-
borderRadius: 14,
|
|
109
|
-
shadowColor: '#000',
|
|
110
|
-
shadowOffset: {
|
|
111
|
-
width: 0,
|
|
112
|
-
height: 1,
|
|
113
|
-
},
|
|
114
|
-
shadowOpacity: 0.06,
|
|
115
|
-
shadowRadius: 1.5,
|
|
116
|
-
elevation: 1,
|
|
117
|
-
},
|
|
118
|
-
userBubble: {
|
|
119
|
-
backgroundColor: '#D8F3C1',
|
|
120
|
-
borderBottomRightRadius: 4,
|
|
121
|
-
},
|
|
122
|
-
otherBubble: {
|
|
123
|
-
backgroundColor: '#FFFFFF',
|
|
124
|
-
borderBottomLeftRadius: 4,
|
|
125
|
-
},
|
|
126
|
-
messageText: {
|
|
127
|
-
fontSize: 16,
|
|
128
|
-
lineHeight: 21,
|
|
129
|
-
fontWeight: '400',
|
|
130
|
-
},
|
|
131
|
-
userMessageText: {
|
|
132
|
-
color: '#111827',
|
|
133
|
-
},
|
|
134
|
-
otherMessageText: {
|
|
135
|
-
color: '#111827',
|
|
136
|
-
},
|
|
137
|
-
messageTime: {
|
|
138
|
-
fontSize: 10,
|
|
139
|
-
marginTop: 4,
|
|
140
|
-
alignSelf: 'flex-end',
|
|
141
|
-
},
|
|
142
|
-
userMessageTime: {
|
|
143
|
-
color: '#718175',
|
|
144
|
-
},
|
|
145
|
-
otherMessageTime: {
|
|
146
|
-
color: '#8893A0',
|
|
147
|
-
},
|
|
148
|
-
inputContainer: {
|
|
149
|
-
backgroundColor: '#F2F4F6',
|
|
150
|
-
flexDirection: 'row',
|
|
151
|
-
alignItems: 'center',
|
|
152
|
-
paddingHorizontal: 10,
|
|
153
|
-
paddingTop: 8,
|
|
154
|
-
borderTopWidth: 1,
|
|
155
|
-
borderTopColor: '#E6EBF1',
|
|
156
|
-
},
|
|
157
|
-
inputWrapper: {
|
|
158
|
-
flex: 1,
|
|
159
|
-
backgroundColor: '#FFFFFF',
|
|
160
|
-
borderRadius: 28,
|
|
161
|
-
paddingHorizontal: 15,
|
|
162
|
-
minHeight: 48,
|
|
163
|
-
justifyContent: 'center',
|
|
164
|
-
shadowColor: '#000',
|
|
165
|
-
shadowOffset: {
|
|
166
|
-
width: 0,
|
|
167
|
-
height: 1,
|
|
168
|
-
},
|
|
169
|
-
shadowOpacity: 0.04,
|
|
170
|
-
shadowRadius: 1.5,
|
|
171
|
-
elevation: 0,
|
|
172
|
-
},
|
|
173
|
-
textInput: {
|
|
174
|
-
fontSize: 17,
|
|
175
|
-
color: '#2A3440',
|
|
176
|
-
paddingVertical: 10,
|
|
177
|
-
borderWidth: 0,
|
|
178
|
-
outlineStyle: 'none',
|
|
179
|
-
outlineWidth: 0,
|
|
180
|
-
outlineColor: 'transparent',
|
|
181
|
-
},
|
|
182
|
-
sendButton: {
|
|
183
|
-
width: 44,
|
|
184
|
-
height: 44,
|
|
185
|
-
borderRadius: 22,
|
|
186
|
-
marginLeft: 8,
|
|
187
|
-
alignItems: 'center',
|
|
188
|
-
justifyContent: 'center',
|
|
189
|
-
shadowColor: '#000',
|
|
190
|
-
shadowOffset: {
|
|
191
|
-
width: 0,
|
|
192
|
-
height: 1,
|
|
193
|
-
},
|
|
194
|
-
shadowOpacity: 0.08,
|
|
195
|
-
shadowRadius: 2,
|
|
196
|
-
elevation: 1,
|
|
197
|
-
},
|
|
198
|
-
sendButtonActive: {
|
|
199
|
-
backgroundColor: '#2ECF6E',
|
|
200
|
-
},
|
|
201
|
-
sendButtonInactive: {
|
|
202
|
-
backgroundColor: '#E7EBEF',
|
|
203
|
-
},
|
|
204
|
-
});
|
|
205
|
-
|
|
206
|
-
export default styles;
|
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
import {StyleSheet} from 'react-native';
|
|
2
|
-
|
|
3
|
-
export default StyleSheet.create({
|
|
4
|
-
root: {
|
|
5
|
-
flex: 1,
|
|
6
|
-
flexDirection: 'row',
|
|
7
|
-
backgroundColor: '#0f1419',
|
|
8
|
-
minHeight: 0,
|
|
9
|
-
},
|
|
10
|
-
sidebar: {
|
|
11
|
-
width: 280,
|
|
12
|
-
maxWidth: '35%',
|
|
13
|
-
borderRightWidth: 1,
|
|
14
|
-
borderRightColor: '#2a3441',
|
|
15
|
-
backgroundColor: '#15202b',
|
|
16
|
-
},
|
|
17
|
-
sidebarHeader: {
|
|
18
|
-
flexDirection: 'row',
|
|
19
|
-
alignItems: 'center',
|
|
20
|
-
justifyContent: 'space-between',
|
|
21
|
-
paddingHorizontal: 12,
|
|
22
|
-
paddingVertical: 12,
|
|
23
|
-
},
|
|
24
|
-
sidebarTitle: {
|
|
25
|
-
color: '#e7e9ea',
|
|
26
|
-
fontSize: 16,
|
|
27
|
-
fontWeight: '700',
|
|
28
|
-
},
|
|
29
|
-
newBtn: {
|
|
30
|
-
backgroundColor: '#1d9bf0',
|
|
31
|
-
paddingHorizontal: 10,
|
|
32
|
-
paddingVertical: 6,
|
|
33
|
-
borderRadius: 8,
|
|
34
|
-
},
|
|
35
|
-
newBtnText: {color: '#fff', fontWeight: '600', fontSize: 13},
|
|
36
|
-
providerRow: {maxHeight: 44, marginBottom: 4},
|
|
37
|
-
providerRowContent: {paddingHorizontal: 8, gap: 6, alignItems: 'center'},
|
|
38
|
-
providerChip: {
|
|
39
|
-
borderWidth: 1,
|
|
40
|
-
borderColor: '#38444d',
|
|
41
|
-
borderRadius: 16,
|
|
42
|
-
paddingHorizontal: 10,
|
|
43
|
-
paddingVertical: 6,
|
|
44
|
-
marginRight: 6,
|
|
45
|
-
},
|
|
46
|
-
providerChipActive: {
|
|
47
|
-
backgroundColor: '#1d9bf0',
|
|
48
|
-
borderColor: '#1d9bf0',
|
|
49
|
-
},
|
|
50
|
-
providerChipDisabled: {opacity: 0.4},
|
|
51
|
-
providerChipText: {color: '#8b98a5', fontSize: 12},
|
|
52
|
-
providerChipTextActive: {color: '#fff'},
|
|
53
|
-
taskRow: {
|
|
54
|
-
paddingHorizontal: 12,
|
|
55
|
-
paddingVertical: 10,
|
|
56
|
-
borderBottomWidth: StyleSheet.hairlineWidth,
|
|
57
|
-
borderBottomColor: '#2a3441',
|
|
58
|
-
},
|
|
59
|
-
taskRowActive: {backgroundColor: '#1e2732'},
|
|
60
|
-
taskRowText: {color: '#e7e9ea', fontSize: 13},
|
|
61
|
-
taskRowMeta: {color: '#8b98a5', fontSize: 11, marginTop: 2},
|
|
62
|
-
sectionTitle: {color: '#8b98a5', fontSize: 11, fontWeight: '700', textTransform: 'uppercase', paddingHorizontal: 12, paddingTop: 12, paddingBottom: 4},
|
|
63
|
-
spinner: {marginTop: 16},
|
|
64
|
-
emptyText: {color: '#8b98a5', padding: 16, fontSize: 13},
|
|
65
|
-
chatPane: {flex: 1, minWidth: 0},
|
|
66
|
-
chatHeader: {
|
|
67
|
-
flexDirection: 'row',
|
|
68
|
-
alignItems: 'center',
|
|
69
|
-
gap: 8,
|
|
70
|
-
paddingHorizontal: 16,
|
|
71
|
-
paddingVertical: 12,
|
|
72
|
-
borderBottomWidth: 1,
|
|
73
|
-
borderBottomColor: '#2a3441',
|
|
74
|
-
},
|
|
75
|
-
chatHeaderText: {flex: 1, minWidth: 0},
|
|
76
|
-
actionsRow: {flexDirection: 'row', gap: 6},
|
|
77
|
-
actionBtn: {borderWidth: 1, borderColor: '#1d9bf0', borderRadius: 8, paddingHorizontal: 10, paddingVertical: 6},
|
|
78
|
-
actionBtnText: {color: '#1d9bf0', fontSize: 12, fontWeight: '600'},
|
|
79
|
-
chatTitle: {color: '#e7e9ea', fontSize: 16, fontWeight: '600'},
|
|
80
|
-
chatSubtitle: {color: '#8b98a5', fontSize: 12, marginTop: 2},
|
|
81
|
-
messagesContent: {padding: 12, paddingBottom: 24},
|
|
82
|
-
bubble: {
|
|
83
|
-
maxWidth: '85%',
|
|
84
|
-
borderRadius: 12,
|
|
85
|
-
paddingHorizontal: 12,
|
|
86
|
-
paddingVertical: 8,
|
|
87
|
-
marginVertical: 4,
|
|
88
|
-
},
|
|
89
|
-
bubbleUser: {
|
|
90
|
-
alignSelf: 'flex-end',
|
|
91
|
-
backgroundColor: '#1d9bf0',
|
|
92
|
-
},
|
|
93
|
-
bubbleAi: {
|
|
94
|
-
alignSelf: 'flex-start',
|
|
95
|
-
backgroundColor: '#212a33',
|
|
96
|
-
},
|
|
97
|
-
bubbleNote: {alignSelf: 'center', backgroundColor: '#3a2f12', borderWidth: 1, borderColor: '#8a6d1d'},
|
|
98
|
-
bubbleCustomer: {backgroundColor: '#1f3a2b'},
|
|
99
|
-
bubbleAuthor: {color: '#8b98a5', fontSize: 11, fontWeight: '700', marginBottom: 2},
|
|
100
|
-
bubbleMeta: {color: '#8b98a5', fontSize: 11, marginTop: 4},
|
|
101
|
-
ticketTypeRow: {flexDirection: 'row', gap: 6, paddingHorizontal: 10, paddingTop: 8},
|
|
102
|
-
composerHint: {color: '#8b98a5', fontSize: 12, padding: 12, borderTopWidth: 1, borderTopColor: '#2a3441'},
|
|
103
|
-
bubbleText: {color: '#e7e9ea', fontSize: 14, lineHeight: 20},
|
|
104
|
-
composer: {
|
|
105
|
-
flexDirection: 'row',
|
|
106
|
-
alignItems: 'flex-end',
|
|
107
|
-
padding: 10,
|
|
108
|
-
borderTopWidth: 1,
|
|
109
|
-
borderTopColor: '#2a3441',
|
|
110
|
-
gap: 8,
|
|
111
|
-
},
|
|
112
|
-
input: {
|
|
113
|
-
flex: 1,
|
|
114
|
-
minHeight: 40,
|
|
115
|
-
maxHeight: 120,
|
|
116
|
-
borderWidth: 1,
|
|
117
|
-
borderColor: '#38444d',
|
|
118
|
-
borderRadius: 12,
|
|
119
|
-
paddingHorizontal: 12,
|
|
120
|
-
paddingVertical: 8,
|
|
121
|
-
color: '#e7e9ea',
|
|
122
|
-
backgroundColor: '#15202b',
|
|
123
|
-
},
|
|
124
|
-
sendBtn: {
|
|
125
|
-
backgroundColor: '#1d9bf0',
|
|
126
|
-
borderRadius: 12,
|
|
127
|
-
paddingHorizontal: 14,
|
|
128
|
-
paddingVertical: 10,
|
|
129
|
-
},
|
|
130
|
-
sendBtnDisabled: {opacity: 0.5},
|
|
131
|
-
sendBtnText: {color: '#fff', fontWeight: '700'},
|
|
132
|
-
});
|
|
@@ -1,277 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* @agents Data + realtime logic for /ai. Replies arrive by websocket (store "websocket",
|
|
3
|
-
* events mcp_chat_reply / conversation_message); a single fallback reload covers a lost event.
|
|
4
|
-
*/
|
|
5
|
-
import {useCallback, useEffect, useMemo, useRef, useState} from 'react';
|
|
6
|
-
import {useStore} from '@store';
|
|
7
|
-
import {api} from '@controleonline/ui-common/src/api';
|
|
8
|
-
import useToastMessage from '../../hooks/useToastMessage';
|
|
9
|
-
|
|
10
|
-
const {
|
|
11
|
-
idOf,
|
|
12
|
-
isCustomerConversation,
|
|
13
|
-
extractChatEvents,
|
|
14
|
-
mergeConversations,
|
|
15
|
-
} = require('./mcpChatUtils');
|
|
16
|
-
|
|
17
|
-
const REPLY_FALLBACK_MS = 90000;
|
|
18
|
-
const LIST_DEBOUNCE_MS = 400;
|
|
19
|
-
const TICKET_REFRESH_MS = 20000;
|
|
20
|
-
const toItems = res => {
|
|
21
|
-
const items = res?.member || res?.['hydra:member'] || res || [];
|
|
22
|
-
return Array.isArray(items) ? items : [];
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
export default function useMcpChat() {
|
|
26
|
-
const {showError} = useToastMessage();
|
|
27
|
-
const peopleStore = useStore('people');
|
|
28
|
-
const websocketStore = useStore('websocket');
|
|
29
|
-
const authStore = useStore('auth');
|
|
30
|
-
const wsMessages = websocketStore?.getters?.messages;
|
|
31
|
-
const currentPeopleId = idOf(authStore?.getters?.user?.people);
|
|
32
|
-
const company = peopleStore?.getters?.currentCompany || peopleStore?.getters?.mainCompany || null;
|
|
33
|
-
const companyId = idOf(company);
|
|
34
|
-
|
|
35
|
-
const [providers, setProviders] = useState([]);
|
|
36
|
-
const [provider, setProvider] = useState(null);
|
|
37
|
-
const [tasks, setTasks] = useState([]);
|
|
38
|
-
const [activeTaskId, setActiveTaskId] = useState(null);
|
|
39
|
-
const [messages, setMessages] = useState([]);
|
|
40
|
-
const [pending, setPending] = useState({}); // taskId -> 'pending' | 'processing'
|
|
41
|
-
const [loadingList, setLoadingList] = useState(true);
|
|
42
|
-
const [loadingChat, setLoadingChat] = useState(false);
|
|
43
|
-
const [sending, setSending] = useState(false);
|
|
44
|
-
|
|
45
|
-
const activeRef = useRef(null);
|
|
46
|
-
activeRef.current = activeTaskId;
|
|
47
|
-
// Start after whatever is already in the store: old replies must not replay.
|
|
48
|
-
const wsCursor = useRef(Array.isArray(wsMessages) ? wsMessages.length : 0);
|
|
49
|
-
const listTimer = useRef(null);
|
|
50
|
-
const fallbackTimers = useRef({});
|
|
51
|
-
|
|
52
|
-
const activeTask = useMemo(
|
|
53
|
-
() => tasks.find(t => idOf(t) === Number(activeTaskId)) || null,
|
|
54
|
-
[tasks, activeTaskId],
|
|
55
|
-
);
|
|
56
|
-
// A ticket I opened with the platform (my company has no AI): I talk in it from here.
|
|
57
|
-
const myTicket = Boolean(activeTask) && isCustomerConversation(activeTask) && idOf(activeTask.client) === Number(currentPeopleId);
|
|
58
|
-
// Someone else's customer conversation handled by our AI: read-only here.
|
|
59
|
-
const customerChat = isCustomerConversation(activeTask) && !myTicket;
|
|
60
|
-
const [ticketType, setTicketType] = useState('support');
|
|
61
|
-
|
|
62
|
-
const loadProviders = useCallback(async () => {
|
|
63
|
-
try {
|
|
64
|
-
const res = await api.fetch('/mcp/chat/providers', {params: companyId ? {company_id: companyId} : {}});
|
|
65
|
-
const list = Array.isArray(res?.providers) ? res.providers : [];
|
|
66
|
-
setProviders(list);
|
|
67
|
-
// The company's chosen AI (General settings → IA); otherwise the first connected one.
|
|
68
|
-
setProvider(prev => prev || res?.default || list.find(p => p.default)?.provider || list[0]?.provider || null);
|
|
69
|
-
} catch (e) {
|
|
70
|
-
showError(e?.message || 'Falha ao carregar providers de IA');
|
|
71
|
-
}
|
|
72
|
-
}, [companyId, showError]);
|
|
73
|
-
|
|
74
|
-
const loadTasks = useCallback(async () => {
|
|
75
|
-
if (!companyId) return;
|
|
76
|
-
const base = {provider: `/people/${companyId}`, 'order[alterDate]': 'desc', itemsPerPage: 50};
|
|
77
|
-
try {
|
|
78
|
-
// The user's conversations: chats with the company AI + tickets with the platform team.
|
|
79
|
-
const [internal, tickets] = await Promise.all([
|
|
80
|
-
api.fetch('/tasks', {params: {...base, type: 'ia', ...(currentPeopleId ? {registeredBy: `/people/${currentPeopleId}`} : {})}}),
|
|
81
|
-
currentPeopleId
|
|
82
|
-
? api.fetch('/tasks', {params: {client: `/people/${currentPeopleId}`, announce: 'via:ai', 'order[alterDate]': 'desc', itemsPerPage: 50}})
|
|
83
|
-
: Promise.resolve([]),
|
|
84
|
-
]);
|
|
85
|
-
setTasks(mergeConversations([toItems(internal), toItems(tickets)], currentPeopleId));
|
|
86
|
-
} catch (e) {
|
|
87
|
-
showError(e?.message || 'Falha ao listar conversas');
|
|
88
|
-
} finally {
|
|
89
|
-
setLoadingList(false);
|
|
90
|
-
}
|
|
91
|
-
}, [companyId, currentPeopleId, showError]);
|
|
92
|
-
|
|
93
|
-
const scheduleTasksReload = useCallback(() => {
|
|
94
|
-
clearTimeout(listTimer.current);
|
|
95
|
-
listTimer.current = setTimeout(loadTasks, LIST_DEBOUNCE_MS);
|
|
96
|
-
}, [loadTasks]);
|
|
97
|
-
|
|
98
|
-
const loadMessages = useCallback(
|
|
99
|
-
async (taskId, {silent = false} = {}) => {
|
|
100
|
-
if (!taskId) {
|
|
101
|
-
setMessages([]);
|
|
102
|
-
return;
|
|
103
|
-
}
|
|
104
|
-
if (!silent) setLoadingChat(true);
|
|
105
|
-
try {
|
|
106
|
-
const res = await api.fetch('/task_interations', {
|
|
107
|
-
params: {'task.id': taskId, type: 'comment', 'order[id]': 'asc', itemsPerPage: 200},
|
|
108
|
-
});
|
|
109
|
-
// Ignore a late response for a chat the user already left.
|
|
110
|
-
if (Number(activeRef.current) === Number(taskId)) setMessages(toItems(res));
|
|
111
|
-
} catch (e) {
|
|
112
|
-
showError(e?.message || 'Falha ao carregar mensagens');
|
|
113
|
-
} finally {
|
|
114
|
-
if (!silent) setLoadingChat(false);
|
|
115
|
-
}
|
|
116
|
-
},
|
|
117
|
-
[showError],
|
|
118
|
-
);
|
|
119
|
-
|
|
120
|
-
useEffect(() => {
|
|
121
|
-
loadProviders();
|
|
122
|
-
loadTasks();
|
|
123
|
-
}, [loadProviders, loadTasks]);
|
|
124
|
-
|
|
125
|
-
useEffect(() => {
|
|
126
|
-
loadMessages(activeTaskId);
|
|
127
|
-
}, [activeTaskId, loadMessages]);
|
|
128
|
-
|
|
129
|
-
// Platform staff replies to my ticket are not pushed to my devices: refresh while it is open.
|
|
130
|
-
useEffect(() => {
|
|
131
|
-
if (!myTicket || !activeTaskId) return undefined;
|
|
132
|
-
const timer = setInterval(() => loadMessages(activeTaskId, {silent: true}), TICKET_REFRESH_MS);
|
|
133
|
-
return () => clearInterval(timer);
|
|
134
|
-
}, [myTicket, activeTaskId, loadMessages]);
|
|
135
|
-
|
|
136
|
-
useEffect(
|
|
137
|
-
() => () => {
|
|
138
|
-
clearTimeout(listTimer.current);
|
|
139
|
-
Object.values(fallbackTimers.current).forEach(clearTimeout);
|
|
140
|
-
},
|
|
141
|
-
[],
|
|
142
|
-
);
|
|
143
|
-
|
|
144
|
-
const settle = useCallback(taskId => {
|
|
145
|
-
clearTimeout(fallbackTimers.current[taskId]);
|
|
146
|
-
delete fallbackTimers.current[taskId];
|
|
147
|
-
setPending(prev => {
|
|
148
|
-
const next = {...prev};
|
|
149
|
-
delete next[taskId];
|
|
150
|
-
return next;
|
|
151
|
-
});
|
|
152
|
-
}, []);
|
|
153
|
-
|
|
154
|
-
// Realtime: consume every new event since the last cursor (several can arrive in one batch).
|
|
155
|
-
useEffect(() => {
|
|
156
|
-
const {events, nextIndex} = extractChatEvents(wsMessages, wsCursor.current);
|
|
157
|
-
wsCursor.current = nextIndex;
|
|
158
|
-
if (!events.length) return;
|
|
159
|
-
|
|
160
|
-
let reloadActive = false;
|
|
161
|
-
events.forEach(evt => {
|
|
162
|
-
if (companyId && evt.companyId && Number(evt.companyId) !== Number(companyId)) return;
|
|
163
|
-
const taskId = Number(evt.task_id || 0);
|
|
164
|
-
if (!taskId) return;
|
|
165
|
-
if (evt.status === 'processing') {
|
|
166
|
-
setPending(prev => ({...prev, [taskId]: 'processing'}));
|
|
167
|
-
} else if (evt.event === 'mcp_chat_reply') {
|
|
168
|
-
settle(taskId);
|
|
169
|
-
if (evt.status === 'error') showError(evt.body || 'A IA não conseguiu responder.');
|
|
170
|
-
}
|
|
171
|
-
if (taskId === Number(activeRef.current) && evt.status !== 'processing') reloadActive = true;
|
|
172
|
-
});
|
|
173
|
-
if (reloadActive) loadMessages(activeRef.current, {silent: true});
|
|
174
|
-
scheduleTasksReload();
|
|
175
|
-
}, [wsMessages, companyId, loadMessages, scheduleTasksReload, settle, showError]);
|
|
176
|
-
|
|
177
|
-
const armFallback = useCallback(
|
|
178
|
-
taskId => {
|
|
179
|
-
clearTimeout(fallbackTimers.current[taskId]);
|
|
180
|
-
fallbackTimers.current[taskId] = setTimeout(() => {
|
|
181
|
-
settle(taskId);
|
|
182
|
-
if (Number(activeRef.current) === Number(taskId)) loadMessages(taskId, {silent: true});
|
|
183
|
-
}, REPLY_FALLBACK_MS);
|
|
184
|
-
},
|
|
185
|
-
[loadMessages, settle],
|
|
186
|
-
);
|
|
187
|
-
|
|
188
|
-
const sendMessage = useCallback(
|
|
189
|
-
async text => {
|
|
190
|
-
const body = String(text || '').trim();
|
|
191
|
-
if (!body || sending) return false;
|
|
192
|
-
if (customerChat) {
|
|
193
|
-
// Employees answer customers in CRM / Support; /ai only follows the AI here.
|
|
194
|
-
showError('Responda o cliente pelo CRM ou Suporte.');
|
|
195
|
-
return false;
|
|
196
|
-
}
|
|
197
|
-
// No AI for my company (or I'm in my ticket): the message goes to a ticket with the platform.
|
|
198
|
-
const ticketMode = myTicket || (!activeTaskId && !provider);
|
|
199
|
-
if (!ticketMode && !provider) {
|
|
200
|
-
showError('Nenhuma IA configurada para a empresa. Configure em Configurador → IA.');
|
|
201
|
-
return false;
|
|
202
|
-
}
|
|
203
|
-
setSending(true);
|
|
204
|
-
setMessages(prev => [...prev, {id: `local-${Date.now()}`, body, status: 'pending'}]);
|
|
205
|
-
try {
|
|
206
|
-
const res = await api.post('/mcp/chat', {
|
|
207
|
-
message: body,
|
|
208
|
-
...(ticketMode ? {type: ticketType} : {provider}),
|
|
209
|
-
...(activeTaskId ? {task_id: activeTaskId} : {}),
|
|
210
|
-
...(companyId ? {company_id: companyId} : {}),
|
|
211
|
-
});
|
|
212
|
-
const taskId = Number(res?.task_id || 0);
|
|
213
|
-
if (taskId && res?.mode === 'ticket' && !res?.async) {
|
|
214
|
-
// Humans of the platform answer later; no AI reply to wait for.
|
|
215
|
-
if (taskId !== Number(activeTaskId)) setActiveTaskId(taskId);
|
|
216
|
-
else loadMessages(taskId, {silent: true});
|
|
217
|
-
scheduleTasksReload();
|
|
218
|
-
} else if (taskId) {
|
|
219
|
-
setPending(prev => ({...prev, [taskId]: prev[taskId] || 'pending'}));
|
|
220
|
-
armFallback(taskId);
|
|
221
|
-
if (taskId !== Number(activeTaskId)) setActiveTaskId(taskId);
|
|
222
|
-
else loadMessages(taskId, {silent: true});
|
|
223
|
-
scheduleTasksReload();
|
|
224
|
-
}
|
|
225
|
-
return true;
|
|
226
|
-
} catch (e) {
|
|
227
|
-
showError(e?.message || e?.error || 'Falha ao enviar mensagem');
|
|
228
|
-
setMessages(prev => prev.filter(m => !String(m.id).startsWith('local-')));
|
|
229
|
-
return false;
|
|
230
|
-
} finally {
|
|
231
|
-
setSending(false);
|
|
232
|
-
}
|
|
233
|
-
},
|
|
234
|
-
[activeTaskId, armFallback, companyId, customerChat, loadMessages, myTicket, provider, scheduleTasksReload, sending, showError, ticketType],
|
|
235
|
-
);
|
|
236
|
-
|
|
237
|
-
const takeOver = useCallback(
|
|
238
|
-
async type => {
|
|
239
|
-
if (!activeTaskId) return;
|
|
240
|
-
try {
|
|
241
|
-
await api.post('/mcp/chat/handoff', {task_id: activeTaskId, type, ...(companyId ? {company_id: companyId} : {})});
|
|
242
|
-
await Promise.all([loadTasks(), loadMessages(activeTaskId, {silent: true})]);
|
|
243
|
-
} catch (e) {
|
|
244
|
-
showError(e?.message || 'Falha ao transferir a conversa');
|
|
245
|
-
}
|
|
246
|
-
},
|
|
247
|
-
[activeTaskId, companyId, loadMessages, loadTasks, showError],
|
|
248
|
-
);
|
|
249
|
-
|
|
250
|
-
return {
|
|
251
|
-
companyId,
|
|
252
|
-
currentPeopleId,
|
|
253
|
-
providers,
|
|
254
|
-
provider,
|
|
255
|
-
setProvider,
|
|
256
|
-
tasks,
|
|
257
|
-
activeTask,
|
|
258
|
-
activeTaskId,
|
|
259
|
-
setActiveTaskId,
|
|
260
|
-
customerChat,
|
|
261
|
-
myTicket,
|
|
262
|
-
hasAi: providers.length > 0,
|
|
263
|
-
ticketType,
|
|
264
|
-
setTicketType,
|
|
265
|
-
messages,
|
|
266
|
-
loadingList,
|
|
267
|
-
loadingChat,
|
|
268
|
-
sending,
|
|
269
|
-
aiStatus: activeTaskId ? pending[activeTaskId] || null : null,
|
|
270
|
-
sendMessage,
|
|
271
|
-
takeOver,
|
|
272
|
-
startNewChat: () => {
|
|
273
|
-
setActiveTaskId(null);
|
|
274
|
-
setMessages([]);
|
|
275
|
-
},
|
|
276
|
-
};
|
|
277
|
-
}
|