SimpleChat:MCUI: Store and use current chat session id

Also

allow to switch chat session optionally, wrt some of the related
helpers.

setup for two chat sessions by default.
This commit is contained in:
HanishKVC 2024-05-21 16:01:11 +05:30
parent 1b82f2281f
commit 928cc36427

View file

@ -157,6 +157,8 @@ class MultiChatUI {
constructor() { constructor() {
/** @type {Object<string, SimpleChat>} */ /** @type {Object<string, SimpleChat>} */
this.simpleChats = {}; this.simpleChats = {};
/** @type {string} */
this.curChatId = "";
// the ui elements // the ui elements
this.elInSystem = /** @type{HTMLInputElement} */(document.getElementById("system-in")); this.elInSystem = /** @type{HTMLInputElement} */(document.getElementById("system-in"));
@ -186,15 +188,22 @@ class MultiChatUI {
} }
/** /**
* Setup the needed callbacks wrt UI * Setup the needed callbacks wrt UI, curChatId to defaultChatId and
* @param {string} chatId * optionally switch to specified defaultChatId.
* @param {string} defaultChatId
* @param {boolean} [bSwitchSession=false]
*/ */
setup_ui(chatId) { setup_ui(defaultChatId, bSwitchSession=false) {
this.curChatId = defaultChatId;
if (bSwitchSession) {
this.handle_session_switch(this.curChatId);
}
this.elBtnUser.addEventListener("click", (ev)=>{ this.elBtnUser.addEventListener("click", (ev)=>{
if (this.elInUser.disabled) { if (this.elInUser.disabled) {
return; return;
} }
this.handle_user_submit(chatId, this.elSelectApiEP.value); this.handle_user_submit(this.curChatId, this.elSelectApiEP.value);
}); });
this.elInUser.addEventListener("keyup", (ev)=> { this.elInUser.addEventListener("keyup", (ev)=> {
@ -208,11 +217,15 @@ class MultiChatUI {
} }
/** /**
* Start a new chat session * Setup a new chat session and optionally switch to it.
* @param {string} chatId * @param {string} chatId
* @param {boolean} [bSwitchSession=false]
*/ */
new_chat(chatId) { new_chat_session(chatId, bSwitchSession=false) {
this.simpleChats[chatId] = new SimpleChat(); this.simpleChats[chatId] = new SimpleChat();
if (bSwitchSession) {
this.handle_session_switch(chatId);
}
} }
/** /**
@ -298,7 +311,7 @@ class MultiChatUI {
let target = /** @type{HTMLButtonElement} */(ev.target); let target = /** @type{HTMLButtonElement} */(ev.target);
console.debug(`DBUG:MCUI:SessionClick:${target.id}`); console.debug(`DBUG:MCUI:SessionClick:${target.id}`);
if (this.elInUser.disabled) { if (this.elInUser.disabled) {
console.error(`ERRR:MCUI:SessionClick:${target.id}:Current session awaiting response, skipping switch...`); console.error(`ERRR:MCUI:SessionClick:${target.id}:Current session [${this.curChatId}] awaiting response, ignoring switch...`);
return; return;
} }
this.handle_session_switch(target.id); this.handle_session_switch(target.id);
@ -308,6 +321,7 @@ class MultiChatUI {
} }
/** /**
* Switch ui to the specified chatId and set curChatId to same.
* @param {string} chatId * @param {string} chatId
*/ */
async handle_session_switch(chatId) { async handle_session_switch(chatId) {
@ -319,19 +333,22 @@ class MultiChatUI {
this.elInUser.value = ""; this.elInUser.value = "";
chat.show(this.elDivChat); chat.show(this.elDivChat);
this.elInUser.focus(); this.elInUser.focus();
this.curChatId = chatId;
} }
} }
let gMuitChat; let gMuitChat;
const gChatId = "Default"; const gChatIds = [ "Default", "Other" ];
function startme() { function startme() {
console.log("INFO:StartMe:Starting..."); console.log("INFO:StartMe:Starting...");
gMuitChat = new MultiChatUI(); gMuitChat = new MultiChatUI();
gMuitChat.new_chat(gChatId); for (let cid of gChatIds) {
gMuitChat.setup_ui(gChatId); gMuitChat.new_chat_session(cid);
}
gMuitChat.setup_ui(gChatIds[0]);
gMuitChat.show_sessions(); gMuitChat.show_sessions();
} }