:beers: [Feature] Messager and MessagerList: Manager messages and viewers
Browse files
apps/llm_mixer/js/messager.js
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import { get_request_messages } from "./chat_operator.js";
|
2 |
+
|
3 |
+
export class Messager {
|
4 |
+
constructor(message) {
|
5 |
+
this.message = message;
|
6 |
+
this.create_request_message();
|
7 |
+
this.create_viewer();
|
8 |
+
}
|
9 |
+
create_request_message() {
|
10 |
+
this.request_message = {
|
11 |
+
role: this.message.role,
|
12 |
+
content: this.message.content,
|
13 |
+
};
|
14 |
+
}
|
15 |
+
create_viewer() {
|
16 |
+
this.viewer = $("<div>").addClass(`chat-${this.message.role} mb-2 p-2`);
|
17 |
+
this.viewer.append(this.message.content);
|
18 |
+
}
|
19 |
+
}
|
20 |
+
|
21 |
+
export class MessagerList {
|
22 |
+
constructor(messagers_container) {
|
23 |
+
this.messagers_container = messagers_container;
|
24 |
+
this.messagers = [];
|
25 |
+
}
|
26 |
+
|
27 |
+
push(messager) {
|
28 |
+
this.messagers.push(messager);
|
29 |
+
this.messagers_container.append(messager.viewer);
|
30 |
+
}
|
31 |
+
|
32 |
+
pop(n = 1) {
|
33 |
+
let popped_messagers = this.messagers.splice(-n, n);
|
34 |
+
this.messagers_container.children().slice(-n).remove();
|
35 |
+
return popped_messagers;
|
36 |
+
}
|
37 |
+
|
38 |
+
extend(messagers) {
|
39 |
+
this.messagers = this.messagers.concat(
|
40 |
+
messagers.map(function (messager) {
|
41 |
+
return messager;
|
42 |
+
})
|
43 |
+
);
|
44 |
+
this.messagers_container.append(
|
45 |
+
messagers.map(function (messager) {
|
46 |
+
return messager.viewer;
|
47 |
+
})
|
48 |
+
);
|
49 |
+
}
|
50 |
+
|
51 |
+
clear() {
|
52 |
+
this.messagers = [];
|
53 |
+
this.messagers_container.empty();
|
54 |
+
}
|
55 |
+
|
56 |
+
get_messages() {
|
57 |
+
return this.messagers.map(function (messager) {
|
58 |
+
return messager.message;
|
59 |
+
});
|
60 |
+
}
|
61 |
+
get_request_messages() {
|
62 |
+
return this.messagers.map(function (messager) {
|
63 |
+
return messager.request_message;
|
64 |
+
});
|
65 |
+
}
|
66 |
+
|
67 |
+
get_message_viewers() {
|
68 |
+
return this.messagers.map(function (messager) {
|
69 |
+
return messager.viewer;
|
70 |
+
});
|
71 |
+
}
|
72 |
+
}
|