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:
parent
1d7739b7f5
commit
e2efcb4fc2
2 changed files with 21 additions and 0 deletions
|
@ -68,6 +68,7 @@ Once inside
|
||||||
* oneshot or streamed mode.
|
* oneshot or streamed mode.
|
||||||
|
|
||||||
* In completion 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.
|
* 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
|
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.
|
explicitly add the needed prefix, when they enter their chat message.
|
||||||
|
|
|
@ -55,6 +55,8 @@ let gUsageMsg = `
|
||||||
|
|
||||||
/** @typedef {{role: string, content: string}[]} ChatMessages */
|
/** @typedef {{role: string, content: string}[]} ChatMessages */
|
||||||
|
|
||||||
|
/** @typedef {{iLastSys: number, xchat: ChatMessages}} SimpleChatODS */
|
||||||
|
|
||||||
class SimpleChat {
|
class SimpleChat {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -76,6 +78,23 @@ class SimpleChat {
|
||||||
this.iLastSys = -1;
|
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.
|
* Recent chat messages.
|
||||||
* If iRecentUserMsgCnt < 0
|
* If iRecentUserMsgCnt < 0
|
||||||
|
@ -142,6 +161,7 @@ class SimpleChat {
|
||||||
if (role == Roles.System) {
|
if (role == Roles.System) {
|
||||||
this.iLastSys = this.xchat.length - 1;
|
this.iLastSys = this.xchat.length - 1;
|
||||||
}
|
}
|
||||||
|
this.save();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue