:recycle: [Refactor] Moduralize update_message() with class ContentDisplayerUpdater
Browse files- components/chat_operator.js +31 -19
components/chat_operator.js
CHANGED
|
@@ -90,9 +90,9 @@ export function pop_messager(n = 2) {
|
|
| 90 |
}
|
| 91 |
|
| 92 |
export function update_message(json_chunks, content_displayer = null) {
|
| 93 |
-
|
| 94 |
-
content_displayer
|
| 95 |
-
|
| 96 |
json_chunks.forEach(function (item) {
|
| 97 |
let choice = item.choices[0];
|
| 98 |
let content = choice.delta.content;
|
|
@@ -102,22 +102,7 @@ export function update_message(json_chunks, content_displayer = null) {
|
|
| 102 |
console.log("role: " + role);
|
| 103 |
}
|
| 104 |
if (content) {
|
| 105 |
-
|
| 106 |
-
content_displayer.data(
|
| 107 |
-
"raw_content",
|
| 108 |
-
content_displayer.data("raw_content") + content
|
| 109 |
-
);
|
| 110 |
-
get_active_messager_list().messagers.slice(-1)[0].message.content +=
|
| 111 |
-
content;
|
| 112 |
-
content_displayer.html(
|
| 113 |
-
md_to_html_converter.makeHtml(
|
| 114 |
-
transform_footnote(content_displayer.data("raw_content"))
|
| 115 |
-
)
|
| 116 |
-
);
|
| 117 |
-
content_displayer
|
| 118 |
-
.find("table")
|
| 119 |
-
.addClass("table table-bordered table-hover");
|
| 120 |
-
screen_scroller.scroll_to_bottom();
|
| 121 |
}
|
| 122 |
if (finish_reason === "stop") {
|
| 123 |
console.log("[STOP]");
|
|
@@ -132,3 +117,30 @@ export function create_new_chat_session() {
|
|
| 132 |
chat_history.push(new_messager_list);
|
| 133 |
messagers_container.empty();
|
| 134 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
}
|
| 91 |
|
| 92 |
export function update_message(json_chunks, content_displayer = null) {
|
| 93 |
+
let content_displayer_updater = new ContentDisplayerUpdater(
|
| 94 |
+
content_displayer
|
| 95 |
+
);
|
| 96 |
json_chunks.forEach(function (item) {
|
| 97 |
let choice = item.choices[0];
|
| 98 |
let content = choice.delta.content;
|
|
|
|
| 102 |
console.log("role: " + role);
|
| 103 |
}
|
| 104 |
if (content) {
|
| 105 |
+
content_displayer_updater.update_with_chunk_content(content);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
}
|
| 107 |
if (finish_reason === "stop") {
|
| 108 |
console.log("[STOP]");
|
|
|
|
| 117 |
chat_history.push(new_messager_list);
|
| 118 |
messagers_container.empty();
|
| 119 |
}
|
| 120 |
+
|
| 121 |
+
export class ContentDisplayerUpdater {
|
| 122 |
+
constructor(content_displayer = null) {
|
| 123 |
+
if (content_displayer === null) {
|
| 124 |
+
self.content_displayer = get_latest_message_content_displayer();
|
| 125 |
+
} else {
|
| 126 |
+
self.content_displayer = content_displayer;
|
| 127 |
+
}
|
| 128 |
+
}
|
| 129 |
+
update_with_chunk_content(content) {
|
| 130 |
+
self.content_displayer.data(
|
| 131 |
+
"raw_content",
|
| 132 |
+
self.content_displayer.data("raw_content") + content
|
| 133 |
+
);
|
| 134 |
+
get_active_messager_list().messagers.slice(-1)[0].message.content +=
|
| 135 |
+
content;
|
| 136 |
+
self.content_displayer.html(
|
| 137 |
+
md_to_html_converter.makeHtml(
|
| 138 |
+
transform_footnote(self.content_displayer.data("raw_content"))
|
| 139 |
+
)
|
| 140 |
+
);
|
| 141 |
+
self.content_displayer
|
| 142 |
+
.find("table")
|
| 143 |
+
.addClass("table table-bordered table-hover");
|
| 144 |
+
screen_scroller.scroll_to_bottom();
|
| 145 |
+
}
|
| 146 |
+
}
|