Spaces:
Runtime error
Runtime error
Miguel Diaz commited on
Commit ·
6eb753d
1
Parent(s): 845c944
fix: statics, ctx for document
Browse files- static/js/chatHandler.js +19 -19
- static/js/windowHandler.js +10 -10
static/js/chatHandler.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
| 1 |
class ChatGPT{
|
| 2 |
constructor(token){
|
| 3 |
|
| 4 |
-
$("#input-delete").click(()=> $(
|
| 5 |
|
| 6 |
if (localStorage.getItem("convesacion") !== null) {
|
| 7 |
this.convesacion = JSON.parse(localStorage.getItem("convesacion"));
|
| 8 |
-
$(
|
| 9 |
}else{
|
| 10 |
let fecha = new Date().toJSON().slice(0, 10);
|
| 11 |
this.definicion = "Te llamas Chatsito, eres un asistente de apoyo a los amigos de MIA, ";
|
|
@@ -31,22 +31,22 @@ class ChatGPT{
|
|
| 31 |
|
| 32 |
this.endpointChat = "/chat_stream";
|
| 33 |
this.token = token
|
| 34 |
-
this.evCtx = document
|
| 35 |
this.reintentos = 0
|
| 36 |
-
$(
|
| 37 |
this.limpiarConfig()
|
| 38 |
});
|
| 39 |
-
$(
|
| 40 |
this.reintentos = 0;
|
| 41 |
this.enviar(params)
|
| 42 |
//this.enviar2(params)
|
| 43 |
});
|
| 44 |
-
$(
|
| 45 |
this.reintentos++;
|
| 46 |
this.enviar(params.mensaje)
|
| 47 |
});
|
| 48 |
|
| 49 |
-
$(
|
| 50 |
|
| 51 |
|
| 52 |
}
|
|
@@ -65,15 +65,15 @@ class ChatGPT{
|
|
| 65 |
|
| 66 |
reenviar(params){
|
| 67 |
if(params.status==404){
|
| 68 |
-
$(
|
| 69 |
return
|
| 70 |
}
|
| 71 |
if(this.reintentos < 3 ){
|
| 72 |
-
$(
|
| 73 |
-
$(
|
| 74 |
|
| 75 |
}else{
|
| 76 |
-
$(
|
| 77 |
}
|
| 78 |
}
|
| 79 |
|
|
@@ -88,7 +88,7 @@ class ChatGPT{
|
|
| 88 |
}
|
| 89 |
console.log("Enviado: ", mensaje);
|
| 90 |
tempMensajes.push({role: "user", content: mensaje});
|
| 91 |
-
$(
|
| 92 |
|
| 93 |
console.log("Cagados ", tokens, "tokens");
|
| 94 |
this.execStart = performance.now()
|
|
@@ -110,7 +110,7 @@ class ChatGPT{
|
|
| 110 |
this.convesacion.push({role: data.role, content: data.content, tokens: data.output_tokens} )
|
| 111 |
this.token = data.token;
|
| 112 |
data.execTime = performance.now() - this.execStart;
|
| 113 |
-
$(
|
| 114 |
localStorage.setItem("convesacion", JSON.stringify(this.convesacion))
|
| 115 |
})
|
| 116 |
.fail(( jqXHR, status, error ) => {
|
|
@@ -119,10 +119,10 @@ class ChatGPT{
|
|
| 119 |
console.log(status)
|
| 120 |
console.log(error)
|
| 121 |
if(jqXHR.status==404 || status==500){
|
| 122 |
-
$(
|
| 123 |
}else{
|
| 124 |
let execTime = performance.now() - this.execStart;
|
| 125 |
-
$(
|
| 126 |
}
|
| 127 |
|
| 128 |
})
|
|
@@ -139,7 +139,7 @@ class ChatGPT{
|
|
| 139 |
}
|
| 140 |
console.log("Enviado: ", mensaje);
|
| 141 |
tempMensajes.push({role: "user", content: mensaje});
|
| 142 |
-
$(
|
| 143 |
|
| 144 |
fetch('/chat_test', {
|
| 145 |
method: "POST",
|
|
@@ -156,11 +156,11 @@ class ChatGPT{
|
|
| 156 |
return new ReadableStream({
|
| 157 |
start(controller) {
|
| 158 |
function push() {
|
| 159 |
-
reader.read().then((
|
| 160 |
if (done) {
|
| 161 |
console.log("done", done);
|
| 162 |
controller.close();
|
| 163 |
-
$(
|
| 164 |
return;
|
| 165 |
}
|
| 166 |
|
|
@@ -185,7 +185,7 @@ class ChatGPT{
|
|
| 185 |
chatH.token = data.token
|
| 186 |
}else if(data.choices[0].delta.hasOwnProperty("content")){
|
| 187 |
let temp = data.choices[0].delta.content;
|
| 188 |
-
$(
|
| 189 |
|
| 190 |
}
|
| 191 |
}
|
|
|
|
| 1 |
class ChatGPT{
|
| 2 |
constructor(token){
|
| 3 |
|
| 4 |
+
$("#input-delete").click(()=> $(document).trigger("chat:limpiar"))
|
| 5 |
|
| 6 |
if (localStorage.getItem("convesacion") !== null) {
|
| 7 |
this.convesacion = JSON.parse(localStorage.getItem("convesacion"));
|
| 8 |
+
$(document).trigger("chat:cargar", this.convesacion);
|
| 9 |
}else{
|
| 10 |
let fecha = new Date().toJSON().slice(0, 10);
|
| 11 |
this.definicion = "Te llamas Chatsito, eres un asistente de apoyo a los amigos de MIA, ";
|
|
|
|
| 31 |
|
| 32 |
this.endpointChat = "/chat_stream";
|
| 33 |
this.token = token
|
| 34 |
+
// this.evCtx = document
|
| 35 |
this.reintentos = 0
|
| 36 |
+
$(document).on("chat:limpiar", () => {
|
| 37 |
this.limpiarConfig()
|
| 38 |
});
|
| 39 |
+
$(document).on("chat:enviar", (event, params) => {
|
| 40 |
this.reintentos = 0;
|
| 41 |
this.enviar(params)
|
| 42 |
//this.enviar2(params)
|
| 43 |
});
|
| 44 |
+
$(document).on("enviar:reintentar", (event, params) => {
|
| 45 |
this.reintentos++;
|
| 46 |
this.enviar(params.mensaje)
|
| 47 |
});
|
| 48 |
|
| 49 |
+
$(document).on("enviar:error", (event, params) => this.reenviar(params));
|
| 50 |
|
| 51 |
|
| 52 |
}
|
|
|
|
| 65 |
|
| 66 |
reenviar(params){
|
| 67 |
if(params.status==404){
|
| 68 |
+
$(document).trigger("enviar:fallido",{jqXHR:params.jqXHR, status:params.status, error:params.error, execTime:params.execTime, mensaje:params.mensaje});
|
| 69 |
return
|
| 70 |
}
|
| 71 |
if(this.reintentos < 3 ){
|
| 72 |
+
$(document).trigger("enviar:reintentar",{jqXHR:params.jqXHR, status:params.status, error:params.error, execTime:params.execTime, mensaje:params.mensaje});
|
| 73 |
+
$(document).trigger("enviar:enviar",{jqXHR:params.jqXHR, status:params.status, error:params.error, execTime:params.execTime, mensaje:params.mensaje});
|
| 74 |
|
| 75 |
}else{
|
| 76 |
+
$(document).trigger("enviar:fallido",{jqXHR:params.jqXHR, status:params.status, error:params.error, execTime:params.execTime, mensaje:params.mensaje});
|
| 77 |
}
|
| 78 |
}
|
| 79 |
|
|
|
|
| 88 |
}
|
| 89 |
console.log("Enviado: ", mensaje);
|
| 90 |
tempMensajes.push({role: "user", content: mensaje});
|
| 91 |
+
$(document).trigger("enviar:cargando",mensaje);
|
| 92 |
|
| 93 |
console.log("Cagados ", tokens, "tokens");
|
| 94 |
this.execStart = performance.now()
|
|
|
|
| 110 |
this.convesacion.push({role: data.role, content: data.content, tokens: data.output_tokens} )
|
| 111 |
this.token = data.token;
|
| 112 |
data.execTime = performance.now() - this.execStart;
|
| 113 |
+
$(document).trigger("enviar:exito", data);
|
| 114 |
localStorage.setItem("convesacion", JSON.stringify(this.convesacion))
|
| 115 |
})
|
| 116 |
.fail(( jqXHR, status, error ) => {
|
|
|
|
| 119 |
console.log(status)
|
| 120 |
console.log(error)
|
| 121 |
if(jqXHR.status==404 || status==500){
|
| 122 |
+
$(document).trigger("enviar:fallido",{jqXHR:jqXHR, status:status, error:error, mensaje:mensaje});
|
| 123 |
}else{
|
| 124 |
let execTime = performance.now() - this.execStart;
|
| 125 |
+
$(document).trigger("enviar:error",{jqXHR:jqXHR, status:status, error:error, execTime:execTime, mensaje:mensaje});
|
| 126 |
}
|
| 127 |
|
| 128 |
})
|
|
|
|
| 139 |
}
|
| 140 |
console.log("Enviado: ", mensaje);
|
| 141 |
tempMensajes.push({role: "user", content: mensaje});
|
| 142 |
+
$(document).trigger("precarga:inicio", mensaje);
|
| 143 |
|
| 144 |
fetch('/chat_test', {
|
| 145 |
method: "POST",
|
|
|
|
| 156 |
return new ReadableStream({
|
| 157 |
start(controller) {
|
| 158 |
function push() {
|
| 159 |
+
reader.read().then((done, value) => {
|
| 160 |
if (done) {
|
| 161 |
console.log("done", done);
|
| 162 |
controller.close();
|
| 163 |
+
$(document).trigger("precarga:fin");
|
| 164 |
return;
|
| 165 |
}
|
| 166 |
|
|
|
|
| 185 |
chatH.token = data.token
|
| 186 |
}else if(data.choices[0].delta.hasOwnProperty("content")){
|
| 187 |
let temp = data.choices[0].delta.content;
|
| 188 |
+
$(document).trigger("precarga:mensaje",temp);
|
| 189 |
|
| 190 |
}
|
| 191 |
}
|
static/js/windowHandler.js
CHANGED
|
@@ -4,16 +4,16 @@ class WindowHandler{
|
|
| 4 |
this.template = $('<div class="message"><div><p></p></div></div>');
|
| 5 |
this.active = false;
|
| 6 |
|
| 7 |
-
this.evCtx = document;
|
| 8 |
|
| 9 |
-
$(
|
| 10 |
|
| 11 |
-
$(
|
| 12 |
-
$(
|
| 13 |
-
$(
|
| 14 |
this.precargaMensaje(params.mensaje, "reenvio")
|
| 15 |
});
|
| 16 |
-
$(
|
| 17 |
if(params.jqXHR.status == 404){
|
| 18 |
this.precargaMensaje(params.mensaje, "token vencido")
|
| 19 |
}else{
|
|
@@ -31,13 +31,13 @@ class WindowHandler{
|
|
| 31 |
$("#input-text").keydown(() => this.recalcularTextarea());
|
| 32 |
this.cargarChat(chatH.convesacion);
|
| 33 |
|
| 34 |
-
$(
|
| 35 |
this.precargaInicio()
|
| 36 |
});
|
| 37 |
-
$(
|
| 38 |
this.precargaMensaje2(params)}
|
| 39 |
);
|
| 40 |
-
$(
|
| 41 |
this.precargaFin()
|
| 42 |
});
|
| 43 |
|
|
@@ -61,7 +61,7 @@ class WindowHandler{
|
|
| 61 |
$("textarea").prop("disabled", true);
|
| 62 |
this.mostarMensaje(mensaje, "user");
|
| 63 |
this.precargaMensaje("", "")
|
| 64 |
-
$(
|
| 65 |
}
|
| 66 |
|
| 67 |
recalcularTextarea(){
|
|
|
|
| 4 |
this.template = $('<div class="message"><div><p></p></div></div>');
|
| 5 |
this.active = false;
|
| 6 |
|
| 7 |
+
// this.evCtx = document;
|
| 8 |
|
| 9 |
+
$(document).on("enviar:exito", (event, params) => this.procesarTexto(params));
|
| 10 |
|
| 11 |
+
$(document).on("chat:enviar", (event, params) => this.recalcularTextarea());
|
| 12 |
+
$(document).on("chat:limpiar", () => this.limpiarChat());
|
| 13 |
+
$(document).on("enviar:reintentar", (event, params) => {
|
| 14 |
this.precargaMensaje(params.mensaje, "reenvio")
|
| 15 |
});
|
| 16 |
+
$(document).on("enviar:fallido", (event, params) => {
|
| 17 |
if(params.jqXHR.status == 404){
|
| 18 |
this.precargaMensaje(params.mensaje, "token vencido")
|
| 19 |
}else{
|
|
|
|
| 31 |
$("#input-text").keydown(() => this.recalcularTextarea());
|
| 32 |
this.cargarChat(chatH.convesacion);
|
| 33 |
|
| 34 |
+
$(document).on("precarga:inicio", (event, params) => {
|
| 35 |
this.precargaInicio()
|
| 36 |
});
|
| 37 |
+
$(document).on("precarga:mensaje", (event, params) => {
|
| 38 |
this.precargaMensaje2(params)}
|
| 39 |
);
|
| 40 |
+
$(document).on("precarga:fin", (event, params) => {
|
| 41 |
this.precargaFin()
|
| 42 |
});
|
| 43 |
|
|
|
|
| 61 |
$("textarea").prop("disabled", true);
|
| 62 |
this.mostarMensaje(mensaje, "user");
|
| 63 |
this.precargaMensaje("", "")
|
| 64 |
+
$(document).trigger("chat:enviar", mensaje);
|
| 65 |
}
|
| 66 |
|
| 67 |
recalcularTextarea(){
|