@controleonline/ui-crm 1.0.60 → 1.0.61

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.
@@ -1,13 +1,13 @@
1
1
  {
2
- "version": "1.0.59",
2
+ "version": "1.0.61",
3
3
  "rc": 1,
4
- "branch": "rc/1.0.59-rc.1",
5
- "baseMaster": "54dd75b4f09b4be5cb3383131f1ec8f0bdb06567",
4
+ "branch": "rc/1.0.61-rc.1",
5
+ "baseMaster": "f74060050bf143d4436051cd70d688618bd01dc5",
6
6
  "tasks": [
7
- 1327
7
+ 1335
8
8
  ],
9
9
  "frozen": true,
10
10
  "repositories": {
11
- "ControleOnline/ui-crm": "727f84955dbb774dd35d3f0357caf6cc76e17f88"
11
+ "ControleOnline/ui-crm": "54cacf3c5bc086cbd32a13b8b14b889df261e729"
12
12
  }
13
13
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@controleonline/ui-crm",
3
- "version": "1.0.60",
3
+ "version": "1.0.61",
4
4
  "author": "ControleOnline",
5
5
  "description": "ControleOnline CRM Quasar UI Component",
6
6
  "license": "MIT",
@@ -28,6 +28,8 @@ const PROVIDER_LABELS = {
28
28
  export default function McpPage() {
29
29
  const {showError, showSuccess} = useToastMessage();
30
30
  const peopleStore = useStore('people');
31
+ const websocketStore = useStore('websocket');
32
+ const wsMessages = websocketStore?.getters?.messages || [];
31
33
  const company =
32
34
  peopleStore?.getters?.currentCompany ||
33
35
  peopleStore?.getters?.mainCompany ||
@@ -44,6 +46,8 @@ export default function McpPage() {
44
46
  const [loadingChat, setLoadingChat] = useState(false);
45
47
  const [sending, setSending] = useState(false);
46
48
  const listRef = useRef(null);
49
+ const pendingByTaskRef = useRef({}); // taskId -> userInteractionId
50
+ const handledWsRef = useRef(new Set());
47
51
 
48
52
  const companyQuery = useMemo(
49
53
  () => (companyId ? {company_id: Number(companyId)} : {}),
@@ -131,27 +135,45 @@ export default function McpPage() {
131
135
  setDraft('');
132
136
  };
133
137
 
134
- const pollUntilDone = async (taskId, interactionId) => {
135
- const maxAttempts = 40;
136
- for (let i = 0; i < maxAttempts; i += 1) {
137
- await new Promise(r => setTimeout(r, 1500));
138
- try {
139
- await loadMessages(taskId);
140
- if (interactionId) {
141
- const detail = await api.fetch(`/task_interations/${interactionId}`);
142
- const st = detail?.status || detail?.['status'];
143
- if (st === 'done' || st === 'error') {
144
- return st;
145
- }
146
- } else {
147
- return 'done';
148
- }
149
- } catch {
150
- // keep polling
151
- }
138
+ // Realtime: apply mcp_chat_reply from websocket store (no HTTP poll)
139
+ useEffect(() => {
140
+ if (!Array.isArray(wsMessages) || wsMessages.length === 0) {
141
+ return;
152
142
  }
153
- return 'timeout';
154
- };
143
+ const latest = wsMessages[wsMessages.length - 1];
144
+ const candidates = Array.isArray(latest) ? latest : [latest];
145
+ candidates.forEach(evt => {
146
+ if (!evt || typeof evt !== 'object') {
147
+ return;
148
+ }
149
+ const eventName = String(evt.event || evt.action || '');
150
+ if (eventName !== 'mcp_chat_reply') {
151
+ return;
152
+ }
153
+ const taskId = Number(evt.task_id || evt.taskId || 0);
154
+ const interactionId = Number(evt.interaction_id || evt.interactionId || 0);
155
+ const key = `${taskId}:${interactionId}:${evt.status || ''}:${String(evt.body || '').slice(0, 40)}`;
156
+ if (handledWsRef.current.has(key)) {
157
+ return;
158
+ }
159
+ handledWsRef.current.add(key);
160
+
161
+ if (taskId && Number(activeTaskId) === taskId) {
162
+ // Refresh only the open chat once when reply arrives
163
+ loadMessages(taskId);
164
+ } else if (taskId && !activeTaskId) {
165
+ setActiveTaskId(taskId);
166
+ }
167
+ if (taskId) {
168
+ delete pendingByTaskRef.current[taskId];
169
+ loadTasks();
170
+ }
171
+ if (String(evt.status || '') === 'error') {
172
+ showError(evt.body || 'A IA retornou erro ao processar a mensagem');
173
+ }
174
+ setSending(false);
175
+ });
176
+ }, [wsMessages, activeTaskId, loadMessages, loadTasks, showError]);
155
177
 
156
178
  const sendMessage = async () => {
157
179
  const text = draft.trim();
@@ -168,30 +190,38 @@ export default function McpPage() {
168
190
  ...(activeTaskId ? {task_id: activeTaskId} : {}),
169
191
  ...(companyId ? {company_id: Number(companyId)} : {}),
170
192
  };
193
+ // Optimistic local bubble while queue processes
194
+ setMessages(prev => [
195
+ ...prev,
196
+ {
197
+ id: `local-${Date.now()}`,
198
+ body: text,
199
+ status: 'pending',
200
+ registeredBy: {id: companyId ? Number(companyId) + 1 : 0},
201
+ },
202
+ ]);
203
+ setDraft('');
171
204
  const res = await api.post('/mcp/chat', body);
172
205
  const newTaskId = res?.task_id || res?.task?.id;
173
206
  const interactionId = res?.interaction_id;
174
- setDraft('');
175
- if (newTaskId && Number(newTaskId) !== Number(activeTaskId)) {
176
- setActiveTaskId(Number(newTaskId));
177
- await loadTasks();
207
+ if (newTaskId) {
208
+ pendingByTaskRef.current[Number(newTaskId)] = interactionId;
209
+ if (Number(newTaskId) !== Number(activeTaskId)) {
210
+ setActiveTaskId(Number(newTaskId));
211
+ loadTasks();
212
+ }
213
+ // Single reload after persist (user message on server) — no poll loop
214
+ await loadMessages(Number(newTaskId));
178
215
  }
179
- await loadMessages(newTaskId || activeTaskId);
180
- // Phase 2 runs in queue; poll for status/reply (WS will also push when available)
181
- if (res?.async || res?.status === 'pending') {
182
- const finalStatus = await pollUntilDone(newTaskId || activeTaskId, interactionId);
183
- await loadMessages(newTaskId || activeTaskId);
184
- if (finalStatus === 'error') {
185
- showError('A IA retornou erro ao processar a mensagem');
186
- } else if (finalStatus === 'timeout') {
187
- showError('Ainda processando… atualize o chat em instantes');
216
+ // sending stays true until websocket reply (or error toast)
217
+ if (!(res?.async || res?.status === 'pending')) {
218
+ setSending(false);
219
+ if (res?.message) {
220
+ await loadMessages(newTaskId || activeTaskId);
188
221
  }
189
- } else if (res?.message) {
190
- await loadMessages(newTaskId || activeTaskId);
191
222
  }
192
223
  } catch (e) {
193
224
  showError(e?.message || e?.error || 'Falha ao enviar mensagem');
194
- } finally {
195
225
  setSending(false);
196
226
  }
197
227
  };