Skip to content
This repository was archived by the owner on Mar 14, 2024. It is now read-only.

Commit 607ff5e

Browse files
authored
Merge pull request #29 from ttempaa/main
Added sorting and saving of some settings in conversations
2 parents 5a84e88 + eb944f7 commit 607ff5e

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

client/.prettierrc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"printWidth": 120,
3+
"tabWidth": 4,
4+
"useTabs": true,
5+
"semi": true,
6+
"singleQuote": false,
7+
"trailingComma": "es5"
8+
}

client/js/chat.js

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,7 @@ const ask_gpt = async (message) => {
134134

135135
chunk = decodeUnicode(new TextDecoder().decode(value));
136136

137-
if (
138-
chunk.includes(`<form id="challenge-form" action="${url_prefix}/backend-api/v2/conversation?`)
139-
) {
137+
if (chunk.includes(`<form id="challenge-form" action="${url_prefix}/backend-api/v2/conversation?`)) {
140138
chunk = `cloudflare token expired, please refresh the page.`;
141139
}
142140

@@ -196,7 +194,10 @@ const ask_gpt = async (message) => {
196194

197195
const add_user_message_box = (message) => {
198196
const messageDiv = createElement("div", { classNames: ["message"] });
199-
const avatarContainer = createElement("div", { classNames: ["avatar-container"], innerHTML: user_image });
197+
const avatarContainer = createElement("div", {
198+
classNames: ["avatar-container"],
199+
innerHTML: user_image,
200+
});
200201
const contentDiv = createElement("div", {
201202
classNames: ["content"],
202203
id: `user_${token}`,
@@ -266,6 +267,16 @@ const load_conversation = async (conversation_id) => {
266267
let conversation = await JSON.parse(localStorage.getItem(`conversation:${conversation_id}`));
267268
console.log(conversation, conversation_id);
268269

270+
model = document.getElementById("model");
271+
provider = document.getElementById("provider");
272+
jailbreak = document.getElementById("jailbreak");
273+
let hasModel = Array.from(model.options).some((option) => option.value === conversation.model);
274+
let hasProvider = Array.from(provider.options).some((option) => option.value === conversation.provider);
275+
let hasJailbreak = Array.from(jailbreak.options).some((option) => option.value === conversation.jailbreak);
276+
if (hasModel) model.value = conversation.model;
277+
if (hasProvider) provider.value = conversation.provider;
278+
if (hasJailbreak) jailbreak.value = conversation.jailbreak;
279+
269280
for (item of conversation.items) {
270281
if (is_assistant(item.role)) {
271282
message_box.innerHTML += load_gpt_message_box(item.content);
@@ -287,7 +298,10 @@ const load_conversation = async (conversation_id) => {
287298

288299
const load_user_message_box = (content) => {
289300
const messageDiv = createElement("div", { classNames: ["message"] });
290-
const avatarContainer = createElement("div", { classNames: ["avatar-container"], innerHTML: user_image });
301+
const avatarContainer = createElement("div", {
302+
classNames: ["avatar-container"],
303+
innerHTML: user_image,
304+
});
291305
const contentDiv = createElement("div", { classNames: ["content"] });
292306
const preElement = document.createElement("pre");
293307
preElement.textContent = content;
@@ -322,19 +336,26 @@ const get_conversation = async (conversation_id) => {
322336

323337
const add_conversation = async (conversation_id, title) => {
324338
if (localStorage.getItem(`conversation:${conversation_id}`) == null) {
339+
jailbreak = document.getElementById("jailbreak");
340+
model = document.getElementById("model");
341+
provider = document.getElementById("provider");
325342
localStorage.setItem(
326343
`conversation:${conversation_id}`,
327344
JSON.stringify({
328345
id: conversation_id,
329346
title: title,
330347
items: [],
348+
created_at: Date.now(),
349+
model: model.options[model.selectedIndex].value,
350+
provider: provider.options[provider.selectedIndex].value,
351+
jailbreak: jailbreak.options[jailbreak.selectedIndex].value,
331352
})
332353
);
333354
}
334355
};
335356

336357
const add_message = async (conversation_id, role, content) => {
337-
before_adding = JSON.parse(localStorage.getItem(`conversation:${conversation_id}`));
358+
let before_adding = JSON.parse(localStorage.getItem(`conversation:${conversation_id}`));
338359

339360
before_adding.items.push({
340361
role: role,
@@ -356,6 +377,8 @@ const load_conversations = async (limit, offset, loader) => {
356377
}
357378
}
358379

380+
conversations.sort((a, b) => b.created_at - a.created_at);
381+
359382
//if (loader === undefined) spinner.parentNode.removeChild(spinner)
360383
await clear_conversations();
361384

@@ -511,20 +534,20 @@ function createElement(tag, { classNames, id, innerHTML, textContent } = {}) {
511534
el.appendChild(preElement);
512535
}
513536
return el;
514-
};
537+
}
515538

516539
//(async () => {
517-
//response = await fetch('/backend-api/v2/providers')
540+
//response = await fetch('/backend-api/v2/providers')
518541
// providers = await response.json()
519-
542+
520543
// let select = document.getElementById('provider');
521544
// select.textContent = '';
522-
//
545+
//
523546
// let auto = document.createElement('option');
524547
// auto.value = '';
525548
// auto.text = 'Provider: Auto';
526549
// select.appendChild(auto);
527-
//
550+
//
528551
// for (provider of providers) {
529552
// let option = document.createElement('option');
530553
// option.value = option.text = provider;

0 commit comments

Comments
 (0)