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:
HanishKVC 2024-05-17 17:39:18 +05:30
parent 69ecad21e7
commit 0402a4b60e

View 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);
}
}
}