From 0402a4b60e02f65e82ff6322e5a5114b834479ba Mon Sep 17 00:00:00 2001 From: HanishKVC Date: Fri, 17 May 2024 17:39:18 +0530 Subject: [PATCH] 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. --- examples/server/public/simplechat.js | 35 ++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 examples/server/public/simplechat.js diff --git a/examples/server/public/simplechat.js b/examples/server/public/simplechat.js new file mode 100644 index 000000000..d0cfb6f8e --- /dev/null +++ b/examples/server/public/simplechat.js @@ -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); + } + } + +} +