File size: 8,125 Bytes
702292e e831ff4 c3e2b34 9e6c00e d4071bf 870555d f558ea3 9e6c00e abcd79a d4071bf c3e2b34 870555d c3e2b34 d4071bf 702292e 870555d 5c5b6bc 702292e 870555d c3e2b34 9e6c00e c3e2b34 870555d 7109301 702292e 7109301 d4071bf f558ea3 7109301 f558ea3 702292e c3e2b34 870555d f558ea3 c3e2b34 7109301 e831ff4 f558ea3 870555d c3e2b34 870555d c3e2b34 870555d c3e2b34 870555d c3e2b34 870555d c3e2b34 702292e f558ea3 c3e2b34 702292e 9e6c00e c3e2b34 e831ff4 702292e c3e2b34 702292e f329fd2 a1888d0 f329fd2 c3e2b34 f329fd2 5fee269 c3e2b34 af2012f c3e2b34 af2012f c3e2b34 f558ea3 7109301 f558ea3 702292e 9e6c00e 7109301 702292e f558ea3 7109301 f558ea3 f329fd2 702292e af2012f 702292e af2012f 702292e f329fd2 af2012f 702292e c3e2b34 7109301 c3e2b34 75d9414 7109301 e831ff4 c3e2b34 9e6c00e c3e2b34 702292e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 | class WindowHandler{
/* Eventos generados
- chat:enviar
- chat:eliminar
**********************/
/* Eventos escuchados
**********************/
conversacion = []
constructor(conversacion, index, chatHndl){
// El indice de este chat
this.index = index
this.conversacion = conversacion
this.chatHandler = chatHndl || 0
// Template de mensajes
this.templateMsg = $("#template-message").contents("div.message").clone()
this.tabs = $(".tabs")
this.templateLabel = $("#template-label").contents("label").clone()
this.templateTab = $("#template-tab").contents("div.tab").clone()
this.chatbox = this.templateTab.find(".chat")
// Parametros base
this.active = false;
// ha tenido su primera interacci贸n
this.interacted = false
this.crearVentanaChat(index, "Tab "+ index)
this.cargarEventos();
$(document).trigger("ventana:cambiada")
this.cargarChat(conversacion)
}
crearVentanaChat(index, nombre){
// coloca el valor al input y lo selecciona
let newLabel = this.templateLabel.clone()
newLabel.find("input").val(index).prop("checked", true);
newLabel.find("> div").text(nombre)
this.tabs.append(newLabel)
this.label = newLabel
// Crea la ventana de chat
let tempTab = this.templateTab.clone();
tempTab.attr("id", index)
$(".chats").append(tempTab)
// establece los contextos de ventana
this.ctx = tempTab
this.chatbox = this.ctx.find(".chat");
this.ventanaSeleccionada()
}
ventanaSeleccionada(){
// cuando la ventana es seleccionada, cambia
$(".tab").removeClass("active")
this.label.find("input")[0].checked=true;
this.ctx.addClass("active");
}
cargarEventos(){
this.label.click(()=> this.ventanaSeleccionada())
this.ctx.find(".input-text").keypress((event) => {
if (!event.shiftKey && event.keyCode === 13) {
this.manejadorEnviar();
}
this.recalcularTextarea()
});
this.ctx.find(".input-send").click(() => this.manejadorEnviar());
this.ctx.find(".input-text").on("keyup,keydown", () => this.recalcularTextarea());
this.ctx.find(".input-delete").click(() => this.eliminarChat() )
this.ctx.find(".input-menu").click(() => $(document).trigger("mostrar:opciones"))
}
crearMensaje(texto, user){
this.active = this.templateMsg.clone();
switch(user){
case "system":
return;
case "user":
this.active.addClass("me");
this.active.find("div p").text(texto);
break;
case "assistant":
texto = this.procesarTexto(texto);
this.active.find("div p").html(texto);
Prism.highlightAllUnder(this.active[0])
break;
case "loading":
this.active.find("div p").html('<div class="loader-wrap"><span class="loader"></span></div>');
break;
}
this.chatbox.append(this.active);
this.chatbox.scrollTop(this.chatbox[0].scrollHeight);
this.interacted=true
return this.active
}
manejadorEnviar(){
let mensaje = this.ctx.find(".input-text").val();
this.ctx.find("button").prop("disabled", true);
this.ctx.find("textarea").prop("disabled", true);
if(mensaje==""){
return false;
}
if(this.conversacion.length==0){
this.conversacion.push({role:"system", content: this.chatHandler.config.assistantPrompt})
}
this.conversacion.push({role: "user", content: mensaje})
self = this
this.crearMensaje(mensaje, "user");
$(document).trigger("chat:enviar", {ctx: self, conversacion:this.conversacion})
}
respuestaInicio(){
this.crearMensaje("", "loading");
}
respuestaStatus(mensaje, modo=false){
let temp = $("<div></div>")
temp.text(mensaje)
if(!this.active.find(".loader").hasClass("firststage")){
this.active.find(".loader").addClass("firststage")
}
switch(modo){
case "enlinea":
this.active.find("div p div:last-child").text(this.active.find("div p div:last-child").text() + mensaje);
break;
case "reemplazar":
this.active.find("div p div:not(.loader-wrap)").remove();
this.active.find("div p").append(temp)
break;
default:
this.active.find("div p").append(temp)
}
this.chatbox.scrollTop(this.chatbox[0].scrollHeight);
}
respuestaMensaje(data){
let mensaje = data.content
this.active.find("div p").html("");
mensaje = this.procesarTexto(mensaje);
this.active.find("div p").html(mensaje);
Prism.highlightAllUnder(this.active[0]);
this.chatbox.scrollTop(this.chatbox[0].scrollHeight);
this.conversacion.push(data)
this.active = false;
this.interacted = true;
this.ctx.find("button").prop("disabled", false);
this.ctx.find("textarea").prop("disabled", false);
this.ctx.find("textarea").val("")
this.ctx.find("textarea").focus();
$(document).trigger("chat:salvar", {index: this.index, conversacion: this.conversacion})
}
respuestaError(error){
this.ctx.find("button").prop("disabled", false);
this.ctx.find("textarea").prop("disabled", false);
this.ctx.find("textarea").val("")
this.ctx.find("textarea").focus();
if(error.hasOwnProperty("responseJSON")){
this.active.find("div p").html(error.responseJSON.detail)
}else{
this.active.find("div p").html("El API no responde, la conexi贸n pudo haberse caido")
}
switch(error.status | 0){
case 404:
this.active.addClass("error")
break;
case 408:
this.active.addClass("warning")
break;
default:
this.active.addClass("error")
}
this.active = false;
this.chatbox.scrollTop(this.chatbox[0].scrollHeight)
}
cargarChat(conversacion){
for(let mensaje of this.conversacion){
this.crearMensaje(mensaje.content, mensaje.role)
}
}
eliminarChat(){
if(confirm("驴Est谩s seguro que quieres eliminar esta conversaci贸n?")){
this.label.remove()
this.ctx.remove()
$(document).trigger("chat:eliminar", {ctx:this.ctx, index:this.index})
}
}
recalcularTextarea(){
this.ctx.find(".input-box").css("height", "30px");
let height = parseInt((this.ctx.find(".input-text").prop('scrollHeight')+15)/15)*15;
this.ctx.find(".input-box").css("height", height+"px");
height -= 30;
this.ctx.find(".chat").css("--textarea", height+"px");
}
procesarTexto(texto){
let resultado = "";
let codigo = false;
for(let actual of texto.split("```")){
if(codigo){
let temp = actual.split("\n",1);
resultado += "<pre><code class='language-";
resultado += temp[0].length>1?temp[0]:"none";
temp = $("<div></div>").text(actual.substr(temp[0].length+1)).html()
resultado += "'>"+temp+"</code></pre>";
}else{
resultado += $("<div></div>").text(actual).html().replace(/`([^`]+?)`/gm, "<b>$1</b>").replace(/\n/g, "<br>");
resultado = resultado.replace(/\[(.*?)\]\((.*?)\)/gm, "[<a href='$2' target='_blank'>$1</a>]")
}
codigo = !codigo;
}
return resultado
}
} |