Spaces:
Runtime error
Runtime error
Miguel Diaz commited on
Commit ·
b44092b
1
Parent(s): 6eb753d
Dev: Update async
Browse files- main.py +0 -84
- static/js/chatHandler.js +5 -57
- static/js/windowHandler.js +26 -103
main.py
CHANGED
|
@@ -86,90 +86,6 @@ async def chat_stream(data = Depends(validate_token)):
|
|
| 86 |
"presence_penalty": float(data.get("config", []).get("presence_penalty", 1))
|
| 87 |
}
|
| 88 |
|
| 89 |
-
try:
|
| 90 |
-
response = openai.ChatCompletion.create(
|
| 91 |
-
model="gpt-3.5-turbo",
|
| 92 |
-
messages=messages,
|
| 93 |
-
temperature=config["temperature"],
|
| 94 |
-
frequency_penalty=config["frequency_penalty"],
|
| 95 |
-
presence_penalty=config["presence_penalty"],
|
| 96 |
-
request_timeout = 25,
|
| 97 |
-
stream=True
|
| 98 |
-
)
|
| 99 |
-
except requests.exceptions.RequestException as e:
|
| 100 |
-
print("Timeout (requests)")
|
| 101 |
-
print(e)
|
| 102 |
-
raise HTTPException(
|
| 103 |
-
status_code=status.HTTP_408_REQUEST_TIMEOUT,
|
| 104 |
-
detail="Los servidores tardaron mucho en responder, puede haber sobrecarga en OpenAI, reintenta luego (error 1)"
|
| 105 |
-
)
|
| 106 |
-
except openai_error.APIConnectionError as e:
|
| 107 |
-
print("APIConnectionError")
|
| 108 |
-
print(e)
|
| 109 |
-
raise HTTPException(
|
| 110 |
-
status_code=status.HTTP_408_REQUEST_TIMEOUT,
|
| 111 |
-
detail="El servidor no respondió, puede haber sobrecarga en OpenAI, reintenta luego (error 2)"
|
| 112 |
-
)
|
| 113 |
-
except openai_error.Timeout as e:
|
| 114 |
-
print("Timeout (openai)")
|
| 115 |
-
print(e)
|
| 116 |
-
raise HTTPException(
|
| 117 |
-
status_code=status.HTTP_408_REQUEST_TIMEOUT,
|
| 118 |
-
detail="El servidor no respondió, puede haber sobrecarga en OpenAI, reintenta luego (error 3)"
|
| 119 |
-
)
|
| 120 |
-
except openai_error.InvalidRequestError as e:
|
| 121 |
-
print("Timeout (openai)")
|
| 122 |
-
print(e)
|
| 123 |
-
error = "El servidor no respondió, puede haber sobrecarga en OpenAI, reintenta luego (error 3)"
|
| 124 |
-
if "This model's maximum context length is 4097 tokens" in e.message:
|
| 125 |
-
error = "ChatGPT se gomitó 🤮, limpia el chat y reintenta."
|
| 126 |
-
raise HTTPException(
|
| 127 |
-
status_code=status.HTTP_408_REQUEST_TIMEOUT,
|
| 128 |
-
detail= error
|
| 129 |
-
)
|
| 130 |
-
|
| 131 |
-
token = create_jwt_token(data.pop("token_data"))
|
| 132 |
-
|
| 133 |
-
async def __streamer():
|
| 134 |
-
chunk_size = 1024
|
| 135 |
-
chunk_body = ""
|
| 136 |
-
total_tokens = 0
|
| 137 |
-
yield '{"type": "role", "role": "assistant", "input_tokens": ' + str(token_length) + ', "content": "'
|
| 138 |
-
|
| 139 |
-
for chunk in response:
|
| 140 |
-
delta = chunk["choices"][0]["delta"]
|
| 141 |
-
|
| 142 |
-
chunk_body += delta.get("content", "")
|
| 143 |
-
total_tokens += 1
|
| 144 |
-
|
| 145 |
-
if len(chunk_body) > chunk_size:
|
| 146 |
-
yield json.dumps(chunk_body)[1:-1]
|
| 147 |
-
chunk_body = ""
|
| 148 |
-
|
| 149 |
-
if chunk_body:
|
| 150 |
-
yield json.dumps(chunk_body)[1:-1]
|
| 151 |
-
|
| 152 |
-
yield f'", "token": "{token}", ' + '"config":' + json.dumps(config) +', ' + f'"output_tokens":{total_tokens}' +'}'
|
| 153 |
-
|
| 154 |
-
return StreamingResponse(__streamer(), media_type="application/json")
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
@app.post("/chat_test")
|
| 158 |
-
async def chat_test(data = Depends(validate_token)):
|
| 159 |
-
messages = data["messages"]
|
| 160 |
-
try:
|
| 161 |
-
token_length = len(tokenizer.encode(messages[-1]["content"]))
|
| 162 |
-
except:
|
| 163 |
-
token_length = len(messages[-1]["content"])
|
| 164 |
-
print("Error in token length")
|
| 165 |
-
print("Message:", messages[-1]["content"])
|
| 166 |
-
|
| 167 |
-
config = {
|
| 168 |
-
"temperature": float(data.get("config", []).get("temperature", 1)),
|
| 169 |
-
"frequency_penalty": float(data.get("config", []).get("frequency_penalty", 1)),
|
| 170 |
-
"presence_penalty": float(data.get("config", []).get("presence_penalty", 1))
|
| 171 |
-
}
|
| 172 |
-
|
| 173 |
try:
|
| 174 |
response = openai.ChatCompletion.create(
|
| 175 |
model="gpt-3.5-turbo",
|
|
|
|
| 86 |
"presence_penalty": float(data.get("config", []).get("presence_penalty", 1))
|
| 87 |
}
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
try:
|
| 90 |
response = openai.ChatCompletion.create(
|
| 91 |
model="gpt-3.5-turbo",
|
static/js/chatHandler.js
CHANGED
|
@@ -38,8 +38,8 @@ class ChatGPT{
|
|
| 38 |
});
|
| 39 |
$(document).on("chat:enviar", (event, params) => {
|
| 40 |
this.reintentos = 0;
|
| 41 |
-
this.enviar(params)
|
| 42 |
-
|
| 43 |
});
|
| 44 |
$(document).on("enviar:reintentar", (event, params) => {
|
| 45 |
this.reintentos++;
|
|
@@ -78,58 +78,6 @@ class ChatGPT{
|
|
| 78 |
}
|
| 79 |
|
| 80 |
enviar(mensaje){
|
| 81 |
-
|
| 82 |
-
let tempMensajes = [];
|
| 83 |
-
let tokens = 0;
|
| 84 |
-
for(let i = 0; i < this.convesacion.length; i++){
|
| 85 |
-
let actMsg = this.convesacion[i];
|
| 86 |
-
tempMensajes.push({role: actMsg.role, content: actMsg.content})
|
| 87 |
-
tokens += actMsg.tokens
|
| 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()
|
| 95 |
-
$.ajax({
|
| 96 |
-
type: "POST",
|
| 97 |
-
url: this.endpointChat,
|
| 98 |
-
data: JSON.stringify({
|
| 99 |
-
messages: tempMensajes,
|
| 100 |
-
token: this.token,
|
| 101 |
-
config: this.config
|
| 102 |
-
}),
|
| 103 |
-
headers: {
|
| 104 |
-
"Content-type": "application/json"
|
| 105 |
-
}
|
| 106 |
-
})
|
| 107 |
-
.done(( data, status, jqXHR ) => {
|
| 108 |
-
console.log("Enviado: ", data);
|
| 109 |
-
this.convesacion.push({role: "user", content: mensaje, tokens: data.input_tokens} )
|
| 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 ) => {
|
| 117 |
-
console.log("enviado fallido")
|
| 118 |
-
console.log(jqXHR)
|
| 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 |
-
})
|
| 129 |
-
|
| 130 |
-
}
|
| 131 |
-
|
| 132 |
-
enviar2(mensaje){
|
| 133 |
let tempMensajes = [];
|
| 134 |
let tokens = 0;
|
| 135 |
for(let i = 0; i < this.convesacion.length; i++){
|
|
@@ -141,7 +89,7 @@ class ChatGPT{
|
|
| 141 |
tempMensajes.push({role: "user", content: mensaje});
|
| 142 |
$(document).trigger("precarga:inicio", mensaje);
|
| 143 |
|
| 144 |
-
fetch(
|
| 145 |
method: "POST",
|
| 146 |
body: JSON.stringify({
|
| 147 |
messages: tempMensajes,
|
|
@@ -156,7 +104,7 @@ class ChatGPT{
|
|
| 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();
|
|
@@ -201,7 +149,7 @@ class ChatGPT{
|
|
| 201 |
});
|
| 202 |
})
|
| 203 |
.then(data => console.log('Solicitud finalizada', data)) //imprimir los datos en la consola
|
| 204 |
-
|
| 205 |
|
| 206 |
|
| 207 |
}
|
|
|
|
| 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++;
|
|
|
|
| 78 |
}
|
| 79 |
|
| 80 |
enviar(mensaje){
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
let tempMensajes = [];
|
| 82 |
let tokens = 0;
|
| 83 |
for(let i = 0; i < this.convesacion.length; i++){
|
|
|
|
| 89 |
tempMensajes.push({role: "user", content: mensaje});
|
| 90 |
$(document).trigger("precarga:inicio", mensaje);
|
| 91 |
|
| 92 |
+
fetch(this.endpointChat, {
|
| 93 |
method: "POST",
|
| 94 |
body: JSON.stringify({
|
| 95 |
messages: tempMensajes,
|
|
|
|
| 104 |
return new ReadableStream({
|
| 105 |
start(controller) {
|
| 106 |
function push() {
|
| 107 |
+
reader.read().then(({done, value}) => {
|
| 108 |
if (done) {
|
| 109 |
console.log("done", done);
|
| 110 |
controller.close();
|
|
|
|
| 149 |
});
|
| 150 |
})
|
| 151 |
.then(data => console.log('Solicitud finalizada', data)) //imprimir los datos en la consola
|
| 152 |
+
.catch(err => console.log('Solicitud fallida', err)); // Capturar errores
|
| 153 |
|
| 154 |
|
| 155 |
}
|
static/js/windowHandler.js
CHANGED
|
@@ -3,23 +3,12 @@ class WindowHandler{
|
|
| 3 |
this.chatbox = $("#chat");
|
| 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{
|
| 20 |
-
this.precargaMensaje(params.mensaje, "fallido")
|
| 21 |
-
}
|
| 22 |
-
});
|
| 23 |
|
| 24 |
$("#input-text").keypress((event) => {
|
| 25 |
if (!event.shiftKey && event.keyCode === 13) {
|
|
@@ -32,7 +21,7 @@ class WindowHandler{
|
|
| 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)}
|
|
@@ -40,9 +29,7 @@ class WindowHandler{
|
|
| 40 |
$(document).on("precarga:fin", (event, params) => {
|
| 41 |
this.precargaFin()
|
| 42 |
});
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
}
|
| 47 |
|
| 48 |
limpiarChat(){
|
|
@@ -56,11 +43,6 @@ class WindowHandler{
|
|
| 56 |
if(mensaje==""){
|
| 57 |
return false;
|
| 58 |
}
|
| 59 |
-
$("#input-text").val("");
|
| 60 |
-
$("button").prop("disabled", true);
|
| 61 |
-
$("textarea").prop("disabled", true);
|
| 62 |
-
this.mostarMensaje(mensaje, "user");
|
| 63 |
-
this.precargaMensaje("", "")
|
| 64 |
$(document).trigger("chat:enviar", mensaje);
|
| 65 |
}
|
| 66 |
|
|
@@ -80,95 +62,33 @@ class WindowHandler{
|
|
| 80 |
});
|
| 81 |
}
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
if(i%2==0){
|
| 89 |
-
msgProcesado += textoActual.replace(/\n/g, "<br>").replace(/`(.*?)`/gm, "<b>$1</b>");
|
| 90 |
-
}else{
|
| 91 |
-
let temp = textoActual.split("\n",1);
|
| 92 |
-
msgProcesado += "<pre><code class='language-";
|
| 93 |
-
msgProcesado += temp[0].length>1?temp[0]:"none";
|
| 94 |
-
temp = $("<div></div>").text(textoActual.substr(temp[0].length)).html()
|
| 95 |
-
msgProcesado += "'>"+temp+"</code></pre>";
|
| 96 |
-
}
|
| 97 |
-
}
|
| 98 |
-
this.mostarMensaje(msgProcesado, params.role);
|
| 99 |
-
}
|
| 100 |
-
|
| 101 |
-
mostarMensaje(mensaje, role){
|
| 102 |
-
let clone = this.template.clone();
|
| 103 |
-
if(role=="user"){
|
| 104 |
-
clone.addClass("me");
|
| 105 |
-
clone.find("div p").text(mensaje);
|
| 106 |
-
this.chatbox.append(clone);
|
| 107 |
-
this.chatbox.scrollTop(this.chatbox[0].scrollHeight);
|
| 108 |
-
return;
|
| 109 |
-
}else{
|
| 110 |
-
$("button").prop("disabled", false);
|
| 111 |
-
$("textarea").prop("disabled", false);
|
| 112 |
-
|
| 113 |
-
if($(".precarga").length){
|
| 114 |
-
clone = $(".precarga")
|
| 115 |
-
clone.removeClass("precarga")
|
| 116 |
-
}else{
|
| 117 |
-
this.chatbox.append(clone);
|
| 118 |
-
}
|
| 119 |
-
clone.find("div p").html(mensaje);
|
| 120 |
-
Prism.highlightAll();
|
| 121 |
-
this.active = false;
|
| 122 |
-
}
|
| 123 |
-
|
| 124 |
-
this.chatbox.scrollTop(this.chatbox[0].scrollHeight);
|
| 125 |
-
}
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
precargaMensaje(mensaje, role){
|
| 130 |
-
if(role=="reenvio"){
|
| 131 |
-
$(".message.precarga").find("div p").text("Algo sucedió, estoy reintentando...");
|
| 132 |
-
if(!$(".message.precarga").hasClass("warning")){
|
| 133 |
-
$(".message.precarga").addClass("warning");
|
| 134 |
-
}
|
| 135 |
-
}else if(role=="fallido"){
|
| 136 |
-
$(".message.precarga").removeClass("warning");
|
| 137 |
-
$(".message.precarga").addClass("error");
|
| 138 |
-
$(".message.precarga").find("div p").text("Algo muy malo pasó 🤷♂️");
|
| 139 |
-
$(".message.precarga").removeClass("precarga");
|
| 140 |
-
}else if(role=="token vencido"){
|
| 141 |
-
$(".message.precarga").removeClass("warning");
|
| 142 |
-
$(".message.precarga").addClass("error");
|
| 143 |
-
$(".message.precarga").find("div p").text("El token se venció 🤦♂️, recarga la pagina");
|
| 144 |
-
$(".message.precarga").removeClass("precarga");
|
| 145 |
-
}else{
|
| 146 |
-
let clone = this.template.clone();
|
| 147 |
-
clone.addClass("precarga");
|
| 148 |
-
clone.find("div p").html("Cargando...");
|
| 149 |
-
this.chatbox.append(clone);
|
| 150 |
-
this.chatbox.scrollTop(this.chatbox[0].scrollHeight);
|
| 151 |
-
}
|
| 152 |
-
}
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
precargaInicio(){
|
| 157 |
let clone = this.template.clone();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
this.active = clone;
|
| 159 |
-
this.active.find("div p").html("");
|
| 160 |
-
this.chatbox.append(this.active);
|
| 161 |
this.chatbox.scrollTop(this.chatbox[0].scrollHeight);
|
| 162 |
-
|
| 163 |
}
|
|
|
|
| 164 |
precargaMensaje2(mensaje){
|
| 165 |
-
|
| 166 |
-
html
|
| 167 |
-
|
|
|
|
| 168 |
|
| 169 |
}
|
|
|
|
| 170 |
precargaFin(){
|
| 171 |
-
let mensaje = this.
|
| 172 |
let segmentos = mensaje.split("```");
|
| 173 |
let msgProcesado = "";
|
| 174 |
if(segmentos.length%2==0){
|
|
@@ -186,7 +106,10 @@ class WindowHandler{
|
|
| 186 |
msgProcesado += "'>"+temp+"</code></pre>";
|
| 187 |
}
|
| 188 |
}
|
| 189 |
-
|
|
|
|
|
|
|
|
|
|
| 190 |
this.active = false;
|
| 191 |
|
| 192 |
}
|
|
|
|
| 3 |
this.chatbox = $("#chat");
|
| 4 |
this.template = $('<div class="message"><div><p></p></div></div>');
|
| 5 |
this.active = false;
|
| 6 |
+
this.mensaje = "";
|
| 7 |
|
| 8 |
// this.evCtx = document;
|
| 9 |
|
|
|
|
|
|
|
| 10 |
$(document).on("chat:enviar", (event, params) => this.recalcularTextarea());
|
| 11 |
$(document).on("chat:limpiar", () => this.limpiarChat());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
$("#input-text").keypress((event) => {
|
| 14 |
if (!event.shiftKey && event.keyCode === 13) {
|
|
|
|
| 21 |
this.cargarChat(chatH.convesacion);
|
| 22 |
|
| 23 |
$(document).on("precarga:inicio", (event, params) => {
|
| 24 |
+
this.precargaInicio(params)
|
| 25 |
});
|
| 26 |
$(document).on("precarga:mensaje", (event, params) => {
|
| 27 |
this.precargaMensaje2(params)}
|
|
|
|
| 29 |
$(document).on("precarga:fin", (event, params) => {
|
| 30 |
this.precargaFin()
|
| 31 |
});
|
| 32 |
+
|
|
|
|
|
|
|
| 33 |
}
|
| 34 |
|
| 35 |
limpiarChat(){
|
|
|
|
| 43 |
if(mensaje==""){
|
| 44 |
return false;
|
| 45 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
$(document).trigger("chat:enviar", mensaje);
|
| 47 |
}
|
| 48 |
|
|
|
|
| 62 |
});
|
| 63 |
}
|
| 64 |
|
| 65 |
+
precargaInicio(mensaje){
|
| 66 |
+
$("#input-text").val("");
|
| 67 |
+
$("button").prop("disabled", true);
|
| 68 |
+
$("textarea").prop("disabled", true);
|
| 69 |
+
this.mensaje = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
let clone = this.template.clone();
|
| 71 |
+
clone.addClass("me");
|
| 72 |
+
clone.find("div p").text(mensaje);
|
| 73 |
+
this.chatbox.append(clone);
|
| 74 |
+
|
| 75 |
+
clone = this.template.clone();
|
| 76 |
+
clone.find("div p").html("");
|
| 77 |
+
this.chatbox.append(clone);
|
| 78 |
this.active = clone;
|
|
|
|
|
|
|
| 79 |
this.chatbox.scrollTop(this.chatbox[0].scrollHeight);
|
|
|
|
| 80 |
}
|
| 81 |
+
|
| 82 |
precargaMensaje2(mensaje){
|
| 83 |
+
this.mensaje += mensaje
|
| 84 |
+
let html = this.active.find("div p").html();
|
| 85 |
+
html += mensaje.replace(/\n/g, "<br>").replace(/`([^`\w\W]+?)`/gm, "<b>$1</b>")
|
| 86 |
+
this.active.find("div p").html(html);
|
| 87 |
|
| 88 |
}
|
| 89 |
+
|
| 90 |
precargaFin(){
|
| 91 |
+
let mensaje = this.mensaje;
|
| 92 |
let segmentos = mensaje.split("```");
|
| 93 |
let msgProcesado = "";
|
| 94 |
if(segmentos.length%2==0){
|
|
|
|
| 106 |
msgProcesado += "'>"+temp+"</code></pre>";
|
| 107 |
}
|
| 108 |
}
|
| 109 |
+
$("button").prop("disabled", false);
|
| 110 |
+
$("textarea").prop("disabled", false);
|
| 111 |
+
$("textarea").focus();
|
| 112 |
+
this.active.find("div p").html(msgProcesado)
|
| 113 |
this.active = false;
|
| 114 |
|
| 115 |
}
|