MGLDZM commited on
Commit
7109301
1 Parent(s): c3e2b34

Dev: front end finalizado

Browse files
static/css/tabs.css CHANGED
@@ -50,8 +50,13 @@
50
  .tab-label > .tab-name, .tab-label-add > .tab-name{
51
  display: flex;
52
  align-items: center;
53
- padding: 0 10px;
54
  text-wrap: nowrap;
 
 
 
 
 
55
  }
56
  .tab-label:hover {
57
  /* top: -0.25rem;
 
50
  .tab-label > .tab-name, .tab-label-add > .tab-name{
51
  display: flex;
52
  align-items: center;
53
+ margin: 0 10px;
54
  text-wrap: nowrap;
55
+ width: 64px;
56
+ overflow: hidden;
57
+ background: linear-gradient(90deg, rgba(0,0,0,1) 0%, rgba(0,0,0,0.8) 70%, rgba(0,0,0,0) 100%);
58
+ -webkit-background-clip: text;
59
+ -webkit-text-fill-color: transparent;
60
  }
61
  .tab-label:hover {
62
  /* top: -0.25rem;
static/js/chatHandler.js CHANGED
@@ -29,8 +29,9 @@ class ChatGPT{
29
  **********************/
30
 
31
  endpointChat = "/chat";
 
32
  token = null;
33
- windowHandlers = []
34
 
35
  constructor(token){
36
  // Token JWT de ejecuci贸n
@@ -38,20 +39,8 @@ class ChatGPT{
38
 
39
  // inicializa eventos de escucha
40
  this.cargarEventos();
41
-
42
-
43
- try{this.conversaciones = JSON.parse(localStorage.getItem("conversaciones"))}catch{}
44
- if (!this.conversaciones || this.conversaciones.length < 1){
45
- this.conversaciones = [[this.definicion]];
46
- }
47
-
48
- // Se crea un windowHandler por conversacion
49
- for(let conversacion of this.conversaciones){
50
- this.windowHandlers.push(new WindowHandler(conversacion, this.windowHandlers.length));
51
- }
52
 
53
- // Anucia chats creados
54
- $(document).trigger("chat:creado");
55
 
56
  }
57
 
@@ -62,26 +51,18 @@ class ChatGPT{
62
  this.reintentos = 0;
63
  this.enviar(params.ctx, params.ctx.conversacion);
64
  });
65
- $(document).on("chat:eliminar", (event, params) => this.eliminarChat(params.ctx, params.index))
66
  $("#nuevoChat").on("click", () => {$(document).trigger("chat:crear")})
67
  }
68
 
69
- crearChat(){
70
- // Creaci贸n de la estructura para un chat
71
-
72
- // Se agrega la nueva conversa basada en la definici贸n
73
- this.conversaciones.push([this.definicion])
74
-
75
  // Se crea el nuevo manejador de ventana
76
- this.windowHandlers.push(
77
- new WindowHandler(
78
- this.conversaciones[this.conversaciones.length-1],
79
- this.windowHandlers.length)
80
- );
81
-
82
- // Se anuncia la creaci贸n
83
- $(document).trigger("chat:creado", this.windowHandlers.length-1);
84
-
85
  }
86
 
87
  eliminarChat(ctx, index){
@@ -162,10 +143,6 @@ class ChatGPT{
162
  break;
163
  // Mensaje a mostrar
164
  case "mensaje":
165
- conversacion.push(response.mensaje);
166
- localStorage.setItem("conversaciones", JSON.stringify(self.conversaciones))
167
- ctx.trigger("chat:enviado", response.mensaje.content);
168
- ctx.trigger("chat:envio:exito", response.mensaje.content);
169
  ctx.respuestaMensaje(response.mensaje)
170
  break;
171
  // // Es una funci贸n
@@ -192,7 +169,8 @@ class ChatGPT{
192
  method: "POST",
193
  body: JSON.stringify({
194
  messages: tempMensajes,
195
- token: this.token
 
196
  }),
197
  timeout: 60000,
198
  dataType: "json"
@@ -212,5 +190,40 @@ class ChatGPT{
212
 
213
  }
214
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
215
 
216
  }
 
29
  **********************/
30
 
31
  endpointChat = "/chat";
32
+ endpointChat = "https://mgldzm-chgpt.hf.space/chat_async"
33
  token = null;
34
+ windowHandlers = {}
35
 
36
  constructor(token){
37
  // Token JWT de ejecuci贸n
 
39
 
40
  // inicializa eventos de escucha
41
  this.cargarEventos();
 
 
 
 
 
 
 
 
 
 
 
42
 
43
+ this.cargarChats()
 
44
 
45
  }
46
 
 
51
  this.reintentos = 0;
52
  this.enviar(params.ctx, params.ctx.conversacion);
53
  });
54
+ $(document).on("chat:salvar", (event, params) => this.salvarChats(params.index, params.conversacion))
55
  $("#nuevoChat").on("click", () => {$(document).trigger("chat:crear")})
56
  }
57
 
58
+ crearChat(index, conversacion){
 
 
 
 
 
59
  // Se crea el nuevo manejador de ventana
60
+ let uuid = this.generateRandID()
61
+ this.windowHandlers[index||uuid] = new WindowHandler(
62
+ conversacion||[this.definicion],
63
+ index||uuid,
64
+ this
65
+ );
 
 
 
66
  }
67
 
68
  eliminarChat(ctx, index){
 
143
  break;
144
  // Mensaje a mostrar
145
  case "mensaje":
 
 
 
 
146
  ctx.respuestaMensaje(response.mensaje)
147
  break;
148
  // // Es una funci贸n
 
169
  method: "POST",
170
  body: JSON.stringify({
171
  messages: tempMensajes,
172
+ token: this.token,
173
+ config: {"temperature": 1,"frequency_penalty": 0,"presence_penalty": 0}
174
  }),
175
  timeout: 60000,
176
  dataType: "json"
 
190
 
191
  }
192
 
193
+ salvarChats(index, conversacion){
194
+ let conversaciones = {}
195
+ try{conversaciones = JSON.parse(localStorage.getItem("conversaciones"))||{}}catch{}
196
+ conversaciones[index] = conversacion
197
+ localStorage.setItem("conversaciones", JSON.stringify(conversaciones))
198
+ }
199
+
200
+ cargarChats(){
201
+ let conversaciones = {}
202
+ try{conversaciones = JSON.parse(localStorage.getItem("conversaciones"))||{}}catch{}
203
+
204
+ if(!conversaciones || !Object.keys(conversaciones).length){
205
+ this.crearChat()
206
+ return
207
+ }
208
+ for(let key in conversaciones){
209
+ this.crearChat(key, conversaciones[key])
210
+ }
211
+
212
+
213
+ // try{this.conversaciones = JSON.parse(localStorage.getItem("conversaciones"))}catch{}
214
+ // if (!this.conversaciones || this.conversaciones.length < 1){
215
+ // this.conversaciones = [[this.definicion]];
216
+ // }
217
+
218
+ // // Se crea un windowHandler por conversacion
219
+ // for(let conversacion of this.conversaciones){
220
+ // this.windowHandlers.push(new WindowHandler(conversacion, this.windowHandlers.length));
221
+ // }
222
+ }
223
+
224
+ generateRandID(){
225
+ return btoa((Math.random()*100**8)).replaceAll("=","").split("").reverse().join("");
226
+ }
227
+
228
 
229
  }
static/js/inputboxHandler.js DELETED
@@ -1,93 +0,0 @@
1
- class InputBox{
2
- isOpen = false;
3
- textarea = null;
4
- sendButton = null;
5
- deleteButton = null;
6
- optsButton = null;
7
- dialog = null;
8
- constructor(){
9
-
10
- this.textarea = $("textarea.input-text")
11
- this.sendButton = $("button.input-send")
12
- this.delButton = $("button.input-delete")
13
- this.optsButton = $("button.input-menu")
14
- this.dialog = $("dialog#menu")
15
-
16
- this.optsButton.click(() => this.toggleMenu())
17
- this.sendButton.click(() => this.enviarMensaje())
18
- this.delButton.click(() => this.eliminarChat())
19
-
20
- this.textarea.on("change keydown keyup paste", () => this.recalcularTextarea())
21
-
22
-
23
-
24
- $(document).on("chat:creado", (event, params) => this.chatCreado(params))
25
-
26
- }
27
-
28
-
29
-
30
- toggleMenu(){
31
- if(this.isOpen){
32
- this.dialog.close()
33
- }else{
34
- this.dialog.open()
35
- }
36
- this.isOpen = !this.isOpen
37
- }
38
-
39
- enviarMensaje(){
40
-
41
- }
42
-
43
- eliminarChat(){
44
-
45
- }
46
-
47
- recalcularTextarea(){
48
- this.textarea.css("height", "30px");
49
- let height = parseInt((this.textarea.prop('scrollHeight')+15)/15)*15;
50
- this.textarea.css("height", height+"px");
51
- height -= 30;
52
- this.textarea.css("--textarea", height+"px");
53
- }
54
-
55
- chatCreado(index){
56
-
57
- // Deseleccionar tabs
58
- let labels = $(".tab-label input");
59
- labels.prop("checked", false);
60
-
61
- let tabs = $(".tabs")
62
- let newTab = $(".template .tab-label-template").clone()
63
- newTab.removeClass("tab-label-template").addClass("tab-label")
64
- newTab.find("input").val(index)
65
- newTab.find("input").prop("checked", true);
66
- tabs.append(newTab)
67
- this.tabCambiada()
68
- }
69
-
70
- tabCambiada(){
71
- let tab = $(".tab");
72
- let tabActive = $(tab[$(".tab-label input:checked").val()]);
73
- let chat = tabActive.find(".chat")
74
-
75
-
76
- tab.removeClass("active")
77
- tabActive.addClass("active")
78
- tabActive.find("textarea").focus()
79
- if(chat.length>0){
80
- chat.scrollTop(chat[0].scrollHeight);
81
- }
82
- }
83
-
84
- eliminarChat(){
85
- if(confirm("驴Est谩s seguro que quieres eliminar esta conversaci贸n?") == true){
86
- let tabActive = $(".tab-label input:checked").val();
87
- $(document).trigger("chat:eliminar", tabActive)
88
-
89
-
90
- }
91
- }
92
-
93
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
static/js/tabsHandler.js DELETED
@@ -1,26 +0,0 @@
1
- $(document).ready(function() {
2
- $(document).on("chat:creado",function(event){
3
- $(".tab-switch").off("change")
4
- $(".tab-switch").on("change", radioChanged)
5
- radioChanged()
6
- });
7
-
8
- $("#nuevoChat").on("click", (event) => {
9
- $(document).trigger("chat:crear");
10
- })
11
-
12
- })
13
-
14
- function radioChanged(){
15
- let tab = $(".tab");
16
- let tabActive = $(tab[$(".tab-label input:checked").val()]);
17
- let chat = tabActive.find(".chat")
18
-
19
-
20
- tab.removeClass("active")
21
- tabActive.addClass("active")
22
- tabActive.find("textarea").focus()
23
- if(chat.length>0){
24
- chat.scrollTop(chat[0].scrollHeight);
25
- }
26
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
static/js/windowHandler.js CHANGED
@@ -1,11 +1,12 @@
1
  class WindowHandler{
2
 
3
  conversacion = []
4
- constructor(conversacion, index){
5
 
6
  // El indice de este chat
7
  this.index = index
8
  this.conversacion = conversacion
 
9
 
10
  // Template de mensajes
11
  this.templateMsg = $("#template-message").contents("div.message").clone()
@@ -18,7 +19,6 @@ class WindowHandler{
18
 
19
 
20
  // Parametros base
21
- // est谩 activo (es el visible en pantalla)
22
  this.active = false;
23
 
24
  // ha tenido su primera interacci贸n
@@ -27,26 +27,39 @@ class WindowHandler{
27
  this.crearVentanaChat(index, "Tab "+ index)
28
  this.cargarEventos();
29
  $(document).trigger("ventana:cambiada")
30
- //self.cargarChat(conversacion)
31
 
32
  }
33
 
34
- cargarEventos(){
35
- $(document).on("ventana:cambiada", function(){
36
- $(".tab").removeClass("active")
37
- let val = $(".tab-label input:checked").val();
38
- let sw = $($(".tab")[val]).addClass("active")
39
- })
40
- $(".tab-switch").on("change", ()=> $(document).trigger("ventana:cambiada"))
41
 
42
-
43
- // // En envio de chat se recalcula el tama帽o del area de texto
44
- // this.ctx.on("chat:enviar", (event, params) => this.recalcularTextarea());
 
45
 
46
- // // Cuando se elimina un chat
47
- // this.ctx.on("chat:eliminar:pre", () => this.eliminarChat());
48
 
49
- // //
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  this.ctx.find(".input-text").keypress((event) => {
51
  if (!event.shiftKey && event.keyCode === 13) {
52
  this.manejadorEnviar();
@@ -63,35 +76,8 @@ class WindowHandler{
63
  this.ctx.on("chat:envio:inicio", (event, params) => {
64
  this.respuestaInicio(params)
65
  });
66
- // this.ctx.on("precarga:iniciada", (event, params) => {
67
- // this.respuestaIniciada()
68
- // });
69
- // this.ctx.on("precarga:status", (event, params) => {
70
- // this.respuestaStatus(...Object.values(params))
71
- // });
72
- // this.ctx.on("precarga:mensaje", (event, params) => {
73
- // this.respuestaMensaje(params)}
74
- // );
75
- // this.ctx.on("precarga:error", (event, params) => {
76
- // this.respuestaError(params)
77
- // });
78
-
79
- }
80
 
81
- crearVentanaChat(index, nombre){
82
- // coloca el valor al input y lo selecciona
83
- let newLabel = this.templateLabel.clone()
84
- newLabel.find("input").val(index).prop("checked", true);
85
- newLabel.find("> div").text(nombre)
86
- this.tabs.append(newLabel)
87
-
88
- // Crea la ventana de chat
89
- let tempTab = this.templateTab.clone();
90
- $(".chats").append(tempTab)
91
-
92
- // establece los contextos de ventana
93
- this.ctx = tempTab
94
- this.chatbox = this.ctx.find(".chat");
95
 
96
  }
97
 
@@ -132,13 +118,10 @@ class WindowHandler{
132
  $(document).trigger("chat:enviar", {ctx: self})
133
  }
134
 
135
-
136
  respuestaInicio(){
137
  this.crearMensaje("", "loading");
138
  }
139
 
140
-
141
-
142
  respuestaStatus(mensaje, modo=false){
143
  let temp = $("<div></div>")
144
  temp.text(mensaje)
@@ -170,13 +153,17 @@ class WindowHandler{
170
 
171
  this.ctx.find("button").prop("disabled", false);
172
  this.ctx.find("textarea").prop("disabled", false);
 
173
  this.ctx.find("textarea").focus();
174
 
 
 
175
  }
176
 
177
  respuestaError(error){
178
  this.ctx.find("button").prop("disabled", false);
179
  this.ctx.find("textarea").prop("disabled", false);
 
180
  this.ctx.find("textarea").focus();
181
  if(error.hasOwnProperty("responseJSON")){
182
  this.active.find("div p").html(error.responseJSON.detail)
@@ -197,20 +184,19 @@ class WindowHandler{
197
  }
198
  this.active = false;
199
  this.chatbox.scrollTop(this.chatbox[0].scrollHeight)
200
-
201
-
202
-
203
  }
204
 
205
  cargarChat(conversacion){
206
- for(let mensaje of conversacion){
207
- this.crearMensaje(mensaje.contentm, mensaje.role)
208
  }
209
  }
210
 
211
  eliminarChat(){
212
  if(confirm("驴Est谩s seguro que quieres eliminar esta conversaci贸n?") == true){
213
  $(document).trigger("chat:eliminar", {ctx:this.ctx, index:this.index})
 
 
214
  }
215
  }
216
 
 
1
  class WindowHandler{
2
 
3
  conversacion = []
4
+ constructor(conversacion, index, chatHndl){
5
 
6
  // El indice de este chat
7
  this.index = index
8
  this.conversacion = conversacion
9
+ this.chatHandler = chatHndl
10
 
11
  // Template de mensajes
12
  this.templateMsg = $("#template-message").contents("div.message").clone()
 
19
 
20
 
21
  // Parametros base
 
22
  this.active = false;
23
 
24
  // ha tenido su primera interacci贸n
 
27
  this.crearVentanaChat(index, "Tab "+ index)
28
  this.cargarEventos();
29
  $(document).trigger("ventana:cambiada")
30
+ this.cargarChat(conversacion)
31
 
32
  }
33
 
34
+ crearVentanaChat(index, nombre){
35
+ // coloca el valor al input y lo selecciona
36
+ let newLabel = this.templateLabel.clone()
37
+ newLabel.find("input").val(index).prop("checked", true);
38
+ newLabel.find("> div").text(nombre)
39
+ this.tabs.append(newLabel)
40
+ this.label = newLabel
41
 
42
+ // Crea la ventana de chat
43
+ let tempTab = this.templateTab.clone();
44
+ tempTab.attr("id", index)
45
+ $(".chats").append(tempTab)
46
 
 
 
47
 
48
+ // establece los contextos de ventana
49
+ this.ctx = tempTab
50
+ this.chatbox = this.ctx.find(".chat");
51
+ this.ventanaSeleccionada()
52
+ }
53
+
54
+ ventanaSeleccionada(){
55
+ // cuando la ventana es seleccionada, cambia
56
+ $(".tab").removeClass("active")
57
+ this.label.find("input")[0].checked=true;
58
+ this.ctx.addClass("active");
59
+ }
60
+
61
+ cargarEventos(){
62
+ this.label.click(()=> this.ventanaSeleccionada())
63
  this.ctx.find(".input-text").keypress((event) => {
64
  if (!event.shiftKey && event.keyCode === 13) {
65
  this.manejadorEnviar();
 
76
  this.ctx.on("chat:envio:inicio", (event, params) => {
77
  this.respuestaInicio(params)
78
  });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
 
80
+ this.ctx.find(".input-delete").click(() => this.eliminarChat() )
 
 
 
 
 
 
 
 
 
 
 
 
 
81
 
82
  }
83
 
 
118
  $(document).trigger("chat:enviar", {ctx: self})
119
  }
120
 
 
121
  respuestaInicio(){
122
  this.crearMensaje("", "loading");
123
  }
124
 
 
 
125
  respuestaStatus(mensaje, modo=false){
126
  let temp = $("<div></div>")
127
  temp.text(mensaje)
 
153
 
154
  this.ctx.find("button").prop("disabled", false);
155
  this.ctx.find("textarea").prop("disabled", false);
156
+ this.ctx.find("textarea").val("")
157
  this.ctx.find("textarea").focus();
158
 
159
+ $(document).trigger("chat:salvar", {index: this.index, conversacion: this.conversacion})
160
+
161
  }
162
 
163
  respuestaError(error){
164
  this.ctx.find("button").prop("disabled", false);
165
  this.ctx.find("textarea").prop("disabled", false);
166
+ this.ctx.find("textarea").val("")
167
  this.ctx.find("textarea").focus();
168
  if(error.hasOwnProperty("responseJSON")){
169
  this.active.find("div p").html(error.responseJSON.detail)
 
184
  }
185
  this.active = false;
186
  this.chatbox.scrollTop(this.chatbox[0].scrollHeight)
 
 
 
187
  }
188
 
189
  cargarChat(conversacion){
190
+ for(let mensaje of this.conversacion){
191
+ this.crearMensaje(mensaje.content, mensaje.role)
192
  }
193
  }
194
 
195
  eliminarChat(){
196
  if(confirm("驴Est谩s seguro que quieres eliminar esta conversaci贸n?") == true){
197
  $(document).trigger("chat:eliminar", {ctx:this.ctx, index:this.index})
198
+ this.label.remove()
199
+ this.ctx.remove()
200
  }
201
  }
202