Hansimov commited on
Commit
b30bb95
1 Parent(s): 4e6658c

:gem: [Feature] New ChatHistoryStorer: save chat messages in browser with Dexie

Browse files
components/buttons_binder.js CHANGED
@@ -10,6 +10,7 @@ import {
10
  import { setup_available_models_on_select } from "./llm_models_loader.js";
11
 
12
  import { screen_scroller } from "./screen_scroller.js";
 
13
 
14
  export class ButtonsBinder {
15
  constructor() { }
@@ -118,6 +119,7 @@ class NewChatButtonBinder {
118
  const button = $("#new-chat-session");
119
  button.attr("status", "new").attr("title", "New Chat");
120
  button.click(() => {
 
121
  create_new_chat_session();
122
  });
123
  }
 
10
  import { setup_available_models_on_select } from "./llm_models_loader.js";
11
 
12
  import { screen_scroller } from "./screen_scroller.js";
13
+ import { chat_history_storer } from "../networks/chat_history_storer.js";
14
 
15
  export class ButtonsBinder {
16
  constructor() { }
 
119
  const button = $("#new-chat-session");
120
  button.attr("status", "new").attr("title", "New Chat");
121
  button.click(() => {
122
+ chat_history_storer.save_current_chat_session();
123
  create_new_chat_session();
124
  });
125
  }
index.html CHANGED
@@ -16,6 +16,9 @@
16
  <script src="https://cdnjs.cloudflare.com/ajax/libs/showdown/2.1.0/showdown.min.js"></script>
17
  <script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js"></script>
18
  <script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.11/clipboard.min.js"></script>
 
 
 
19
  <link rel="stylesheet" href="./css/default.css" />
20
  </head>
21
 
 
16
  <script src="https://cdnjs.cloudflare.com/ajax/libs/showdown/2.1.0/showdown.min.js"></script>
17
  <script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js"></script>
18
  <script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.11/clipboard.min.js"></script>
19
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.30.1/moment.min.js"></script>
20
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.30.1/moment-with-locales.min.js"></script>
21
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/dexie/3.2.4/dexie.min.js"></script>
22
  <link rel="stylesheet" href="./css/default.css" />
23
  </head>
24
 
networks/chat_history_storer.js ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ class ChatHistoryStorer {
2
+ constructor() {
3
+ this.init_database();
4
+ }
5
+ init_database() {
6
+ this.db = new Dexie("chat_history");
7
+ this.db.version(1).stores({
8
+ chat_history: "index, html, saved_datetime"
9
+ });
10
+ this.db.chat_history.count((count) => {
11
+ console.log(`${count} records loaded from chat_history.`);
12
+ });
13
+ }
14
+ get_current_datetime_string() {
15
+ return moment().format("YYYY-MM-DD_HH:mm:ss.SSS");
16
+ }
17
+
18
+ get_chat_container_html() {
19
+ let messagers_container = $("#messagers-container");
20
+ if (messagers_container.children().length > 0) {
21
+ return messagers_container[0].outHTML;
22
+ } else {
23
+ return null;
24
+ }
25
+ }
26
+ create_chat_index() {
27
+ let date_string = this.get_current_datetime_string();
28
+ let chat_index = `chat_${date_string}`;
29
+ return chat_index;
30
+ }
31
+ save_current_chat_session() {
32
+ let chat_container_html = this.get_chat_container_html();
33
+ let messagers_container = $("#messagers-container");
34
+ if (chat_container_html === null) {
35
+ console.log("Empty messagers_container, no chat session to save.");
36
+ return;
37
+ } else {
38
+ let chat_index = this.create_chat_index();
39
+ let chat_saved_datetime = this.get_current_datetime_string();
40
+ this.db.chat_history.put({
41
+ index: chat_index,
42
+ html: chat_container_html,
43
+ saved_datetime: chat_saved_datetime,
44
+ });
45
+ console.log(
46
+ `${messagers_container.children().length} messages saved at ${chat_saved_datetime}.`
47
+ );
48
+ }
49
+ }
50
+ }
51
+
52
+ export let chat_history_storer = new ChatHistoryStorer();