depurada la conexion
This commit is contained in:
@@ -43,7 +43,7 @@ function handleKey (e) {
|
||||
|
||||
onMounted(() => {
|
||||
if (!chat.items.length) {
|
||||
chat.add({ type: 'text', owner: 'bot', text: '¡Hola! Probá /empleados, /tareas, etc.' })
|
||||
chat.add({ type: 'chat', owner: 'bot', text: '¡Hola! Probá /empleados, /tareas, etc.' })
|
||||
}
|
||||
scrollBottom()
|
||||
})
|
||||
@@ -58,7 +58,7 @@ watch(() => chat.items.length, scrollBottom)
|
||||
<div ref="list" class="flex-1 min-h-0 overflow-auto p-6 space-y-4 custom-scroll">
|
||||
<template v-for="(m,i) in chat.items" :key="i">
|
||||
<!-- mensaje de texto -->
|
||||
<div :class="m.owner==='user' ? 'flex justify-end' : 'flex justify-start'" v-if="m.type==='text'">
|
||||
<div :class="m.owner==='user' ? 'flex justify-end' : 'flex justify-start'" v-if="m.type==='chat'">
|
||||
<div
|
||||
class="max-w-lg rounded-lg px-4 py-2 shadow break-words"
|
||||
:class="m.owner === 'user' ? '' : ''"
|
||||
|
||||
@@ -9,6 +9,7 @@ import './style.css' // Tailwind o tus estilos globales
|
||||
|
||||
console.log(`VITE_API_EVENTS_URL: ${import.meta.env.VITE_API_EVENTS_URL || window?.RUNTIME_CONFIG?.VITE_API_EVENTS_URL || 'no definida'}`);
|
||||
console.log(`VITE_API_DB_URL: ${import.meta.env.VITE_API_DB_URL || window?.RUNTIME_CONFIG?.VITE_API_DB_URL || 'no definida'}`);
|
||||
console.log(`VITE_CONVERSATION_LAYER_ROUTER_URL: ${import.meta.env.VITE_CONVERSATION_LAYER_ROUTER_URL || window?.RUNTIME_CONFIG?.VITE_CONVERSATION_LAYER_ROUTER_URL || 'no definida'}`);
|
||||
|
||||
const apiBaseURL =
|
||||
import.meta.env.VITE_API_DB_URL || window?.RUNTIME_CONFIG?.VITE_API_DB_URL || 'no definida';
|
||||
|
||||
@@ -15,6 +15,8 @@ export const useChat = defineStore('chat', {
|
||||
|
||||
actions: {
|
||||
add (item) {
|
||||
console.log('agregado');
|
||||
|
||||
this.items.push(item)
|
||||
},
|
||||
|
||||
@@ -38,86 +40,91 @@ export const useChat = defineStore('chat', {
|
||||
}
|
||||
},
|
||||
|
||||
async sendMessage (text) {
|
||||
const routerUrl = window.RUNTIME_CONFIG?.VITE_CONVERSATION_LAYER_ROUTER_URL || 'http://localhost:8080/fallback-router'; // Fallback or error
|
||||
if (!window.RUNTIME_CONFIG?.VITE_CONVERSATION_LAYER_ROUTER_URL) {
|
||||
console.error('VITE_CONVERSATION_LAYER_ROUTER_URL is not defined in window.RUNTIME_CONFIG. Using fallback or please check your configuration.');
|
||||
}
|
||||
async sendMessage(text) {
|
||||
const routerUrl =
|
||||
import.meta.env.VITE_CONVERSATION_LAYER_ROUTER_URL ||
|
||||
window?.RUNTIME_CONFIG?.VITE_CONVERSATION_LAYER_ROUTER_URL ||
|
||||
'http://localhost:8080/fallback-router';
|
||||
|
||||
const msgId = `planilla-UI-${Date.now()}`;
|
||||
const ts = Math.floor(Date.now() / 1000);
|
||||
|
||||
const messagePayload = {
|
||||
id: `planilla-UI-${Date.now()}`, // Simple unique ID for now
|
||||
chatId: 'planilla-UI',
|
||||
id: msgId,
|
||||
from: 'planilla-UI',
|
||||
to: 'Nucleo',
|
||||
ts: Math.floor(Date.now() / 1000), // Timestamp in seconds
|
||||
ts,
|
||||
type: 'chat',
|
||||
text: text, // User's message text
|
||||
mediaUrl: null, // Assuming no media for now
|
||||
mentions: null, // Assuming no mentions for now
|
||||
text,
|
||||
mediaUrl: null,
|
||||
mentions: null,
|
||||
meta: {
|
||||
ack: 1, // Or 0 if acknowledgement is handled by the router
|
||||
ack: 1,
|
||||
hasReaction: false,
|
||||
isQuoted: false,
|
||||
},
|
||||
};
|
||||
|
||||
// Optimistically add message to chat
|
||||
this.add({
|
||||
id: messagePayload.id, // Use the same ID
|
||||
// Agregar solo para mostrar en UI mientras llega la respuesta
|
||||
this.items.push({
|
||||
id: msgId,
|
||||
type: 'chat',
|
||||
owner: 'user', // Or a more specific identifier if available
|
||||
text: text,
|
||||
ts: messagePayload.ts
|
||||
owner: 'user',
|
||||
text,
|
||||
ts,
|
||||
});
|
||||
|
||||
try {
|
||||
const response = await axios.post(routerUrl, messagePayload);
|
||||
console.log('Message sent, raw response:', response.data); // Log raw response
|
||||
|
||||
const response = await axios.post(routerUrl, { data: messagePayload });
|
||||
const conversation = response.data;
|
||||
|
||||
if (conversation && Array.isArray(conversation.messages)) {
|
||||
conversation.messages.forEach(msg => {
|
||||
let owner = 'bot'; // Default to bot
|
||||
if (msg.from === 'planilla-UI') {
|
||||
owner = 'user';
|
||||
// NOTE: This might add a duplicate if the optimistic message's ID
|
||||
// is different from the ID assigned by the router for the echoed message.
|
||||
// The current instructions are to render all messages from the router.
|
||||
// Refinement for deduplication can be done later if needed.
|
||||
} else if (msg.from === 'Nucleo') {
|
||||
owner = 'bot';
|
||||
}
|
||||
// else, it could be from another participant
|
||||
const newItems = [];
|
||||
|
||||
this.add({
|
||||
conversation.messages.forEach(msg => {
|
||||
// Evitar re-agregar el mensaje optimista
|
||||
newItems.push({
|
||||
id: msg.id,
|
||||
type: msg.type || 'chat', // Default to 'chat' if type is not present
|
||||
owner: owner,
|
||||
type: msg.type || 'chat',
|
||||
owner: msg.from === 'planilla-UI' ? 'user' : 'bot',
|
||||
text: msg.text,
|
||||
ts: msg.ts,
|
||||
// mediaUrl: msg.mediaUrl, // Uncomment if mediaUrl is expected
|
||||
});
|
||||
});
|
||||
|
||||
// Agregamos de nuevo el mensaje del usuario al principio, ya que fue omitido
|
||||
// newItems.unshift({
|
||||
// id: msgId,
|
||||
// type: 'chat',
|
||||
// owner: 'user',
|
||||
// text,
|
||||
// ts,
|
||||
// });
|
||||
|
||||
this.items = newItems;
|
||||
} else {
|
||||
console.error('Invalid conversation response format:', conversation);
|
||||
this.add({
|
||||
this.items.push({
|
||||
id: `error-${Date.now()}`,
|
||||
type: 'error',
|
||||
owner: 'system',
|
||||
text: 'Error processing server response. Invalid format.',
|
||||
ts: Math.floor(Date.now() / 1000)
|
||||
text: 'Error: respuesta inválida del servidor.',
|
||||
ts: Math.floor(Date.now() / 1000),
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error sending message:', error);
|
||||
this.add({
|
||||
this.items.push({
|
||||
id: `error-send-${Date.now()}`,
|
||||
type: 'error',
|
||||
owner: 'system',
|
||||
text: `Error sending message: ${error.message}`,
|
||||
ts: Math.floor(Date.now() / 1000)
|
||||
text: `Error al enviar: ${error.message}`,
|
||||
ts: Math.floor(Date.now() / 1000),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
},
|
||||
})
|
||||
Reference in New Issue
Block a user