SimpleChat:JS: Move to dictionary of SimpleChat, instead of array
This commit is contained in:
parent
8ef1aa97a6
commit
7be6aeb6d6
1 changed files with 17 additions and 16 deletions
|
@ -155,36 +155,35 @@ const gbCompletionFreshChatAlways = true;
|
||||||
class MultiChatUI {
|
class MultiChatUI {
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
/** @type {number} */
|
/** @type {Object<string, SimpleChat>} */
|
||||||
this.iChat = -1;
|
this.simpleChats = {};
|
||||||
/** @type {SimpleChat[]} */
|
|
||||||
this.simpleChats = [];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Start a new chat session
|
* Start a new chat session
|
||||||
|
* @param {string} chatId
|
||||||
*/
|
*/
|
||||||
new_chat() {
|
new_chat(chatId) {
|
||||||
this.simpleChats.push(new SimpleChat());
|
this.simpleChats[chatId] = new SimpleChat();
|
||||||
this.iChat = this.simpleChats.length - 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle user query submit request, wrt current chat session.
|
* Handle user query submit request, wrt specified chat session.
|
||||||
|
* @param {string} chatId
|
||||||
* @param {HTMLInputElement} inputSystem
|
* @param {HTMLInputElement} inputSystem
|
||||||
* @param {HTMLInputElement} inputUser
|
* @param {HTMLInputElement} inputUser
|
||||||
* @param {HTMLDivElement} divChat
|
* @param {HTMLDivElement} divChat
|
||||||
* @param {string} apiEP
|
* @param {string} apiEP
|
||||||
*/
|
*/
|
||||||
async handle_user_submit(inputSystem, inputUser, divChat, apiEP) {
|
async handle_user_submit(chatId, inputSystem, inputUser, divChat, apiEP) {
|
||||||
|
|
||||||
let chat = this.simpleChats[this.iChat];
|
let chat = this.simpleChats[chatId];
|
||||||
|
|
||||||
chat.add_system_anytime(inputSystem.value, "0");
|
chat.add_system_anytime(inputSystem.value, chatId);
|
||||||
|
|
||||||
let content = inputUser.value;
|
let content = inputUser.value;
|
||||||
if (!chat.add(Roles.User, content)) {
|
if (!chat.add(Roles.User, content)) {
|
||||||
console.debug("WARN:MCUI:HandleUserSubmit:Ignoring empty user input...");
|
console.debug(`WARN:MCUI:${chatId}:HandleUserSubmit:Ignoring empty user input...`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
chat.show(divChat);
|
chat.show(divChat);
|
||||||
|
@ -199,7 +198,7 @@ class MultiChatUI {
|
||||||
|
|
||||||
inputUser.value = "working...";
|
inputUser.value = "working...";
|
||||||
inputUser.disabled = true;
|
inputUser.disabled = true;
|
||||||
console.debug(`DBUG:MCUI:HandleUserSubmit:${theUrl}:ReqBody:${theBody}`);
|
console.debug(`DBUG:MCUI:${chatId}:HandleUserSubmit:${theUrl}:ReqBody:${theBody}`);
|
||||||
let resp = await fetch(theUrl, {
|
let resp = await fetch(theUrl, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
|
@ -211,7 +210,7 @@ class MultiChatUI {
|
||||||
inputUser.value = "";
|
inputUser.value = "";
|
||||||
inputUser.disabled = false;
|
inputUser.disabled = false;
|
||||||
let respBody = await resp.json();
|
let respBody = await resp.json();
|
||||||
console.debug("DBUG:MCUI:HandleUserSubmit:RespBody:", respBody);
|
console.debug(`DBUG:MCUI:${chatId}:HandleUserSubmit:RespBody:${respBody}`);
|
||||||
let assistantMsg;
|
let assistantMsg;
|
||||||
if (apiEP == ApiEP.Chat) {
|
if (apiEP == ApiEP.Chat) {
|
||||||
assistantMsg = respBody["choices"][0]["message"]["content"];
|
assistantMsg = respBody["choices"][0]["message"]["content"];
|
||||||
|
@ -238,6 +237,7 @@ class MultiChatUI {
|
||||||
|
|
||||||
|
|
||||||
let gMuitChat = new MultiChatUI();
|
let gMuitChat = new MultiChatUI();
|
||||||
|
const gChatId = "Default";
|
||||||
|
|
||||||
|
|
||||||
function startme() {
|
function startme() {
|
||||||
|
@ -251,14 +251,15 @@ function startme() {
|
||||||
if (divChat == null) {
|
if (divChat == null) {
|
||||||
throw Error("ERRR:StartMe:Chat element missing");
|
throw Error("ERRR:StartMe:Chat element missing");
|
||||||
}
|
}
|
||||||
|
console.log("INFO:StartMe:Starting...");
|
||||||
|
|
||||||
gMuitChat.new_chat();
|
gMuitChat.new_chat(gChatId);
|
||||||
|
|
||||||
btnSubmit?.addEventListener("click", (ev)=>{
|
btnSubmit?.addEventListener("click", (ev)=>{
|
||||||
if (inputUser.disabled) {
|
if (inputUser.disabled) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
gMuitChat.handle_user_submit(inputSystem, inputUser, divChat, selectApiEP.value);
|
gMuitChat.handle_user_submit(gChatId, inputSystem, inputUser, divChat, selectApiEP.value);
|
||||||
});
|
});
|
||||||
|
|
||||||
inputUser?.addEventListener("keyup", (ev)=> {
|
inputUser?.addEventListener("keyup", (ev)=> {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue