SimpleChat: A js skeleton with SimpleChat class
Allows maintaining an array of chat message. Allows adding chat message (from any of the roles be it system, user, assistant, ...) Allows showing chat messages till now, in a given div element.
This commit is contained in:
parent
69ecad21e7
commit
0402a4b60e
1 changed files with 35 additions and 0 deletions
35
examples/server/public/simplechat.js
Normal file
35
examples/server/public/simplechat.js
Normal file
|
@ -0,0 +1,35 @@
|
|||
// @ts-check
|
||||
|
||||
class SimpleChat {
|
||||
|
||||
constructor() {
|
||||
this.xchat = /** @type {{role: string, content: string}[]} */([]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an entry into xchat
|
||||
* @param {string} role
|
||||
* @param {string} content
|
||||
*/
|
||||
add(role, content) {
|
||||
this.xchat.push( {role: role, content: content} );
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the contents in the specified div
|
||||
* @param {HTMLDivElement} div
|
||||
* @param {boolean} bClear
|
||||
*/
|
||||
show(div, bClear=true) {
|
||||
if (bClear) {
|
||||
div.replaceChildren();
|
||||
}
|
||||
for(const x of this.xchat) {
|
||||
let entry = document.createElement("p");
|
||||
entry.innerText = `${x.role}: ${x.content}`;
|
||||
div.appendChild(entry);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue