SimpleChat: Add basic skeleton for saving and loading chat

Inturn when ever a chat message (system/user/model) is added,
the chat will be saved into browser's localStorage.
This commit is contained in:
HanishKVC 2024-05-31 00:05:43 +05:30
parent 1d7739b7f5
commit e2efcb4fc2
2 changed files with 21 additions and 0 deletions

View file

@ -68,6 +68,7 @@ Once inside
* oneshot or streamed mode.
* In completion mode
* one normally doesnt use a system prompt in completion mode.
* logic by default doesnt insert any role specific "ROLE: " prefix wrt each role's message.
If the model requires any prefix wrt user role messages, then the end user has to
explicitly add the needed prefix, when they enter their chat message.

View file

@ -55,6 +55,8 @@ let gUsageMsg = `
/** @typedef {{role: string, content: string}[]} ChatMessages */
/** @typedef {{iLastSys: number, xchat: ChatMessages}} SimpleChatODS */
class SimpleChat {
/**
@ -76,6 +78,23 @@ class SimpleChat {
this.iLastSys = -1;
}
save() {
/** @type {SimpleChatODS} */
let ods = {iLastSys: this.iLastSys, xchat: this.xchat};
localStorage.setItem(this.chatId, JSON.stringify(ods));
}
load() {
let sods = localStorage.getItem(this.chatId);
if (sods == null) {
return;
}
/** @type {SimpleChatODS} */
let ods = JSON.parse(sods);
this.iLastSys = ods.iLastSys;
this.xchat = ods.xchat;
}
/**
* Recent chat messages.
* If iRecentUserMsgCnt < 0
@ -142,6 +161,7 @@ class SimpleChat {
if (role == Roles.System) {
this.iLastSys = this.xchat.length - 1;
}
this.save();
return true;
}