Skip to content

Commit 3e1a27d

Browse files
committed
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.
1 parent 12dab21 commit 3e1a27d

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

examples/server/public_simplechat/readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ Once inside
6868
* oneshot or streamed mode.
6969

7070
* In completion mode
71+
* one normally doesnt use a system prompt in completion mode.
7172
* logic by default doesnt insert any role specific "ROLE: " prefix wrt each role's message.
7273
If the model requires any prefix wrt user role messages, then the end user has to
7374
explicitly add the needed prefix, when they enter their chat message.

examples/server/public_simplechat/simplechat.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ let gUsageMsg = `
5555

5656
/** @typedef {{role: string, content: string}[]} ChatMessages */
5757

58+
/** @typedef {{iLastSys: number, xchat: ChatMessages}} SimpleChatODS */
59+
5860
class SimpleChat {
5961

6062
/**
@@ -76,6 +78,23 @@ class SimpleChat {
7678
this.iLastSys = -1;
7779
}
7880

81+
save() {
82+
/** @type {SimpleChatODS} */
83+
let ods = {iLastSys: this.iLastSys, xchat: this.xchat};
84+
localStorage.setItem(this.chatId, JSON.stringify(ods));
85+
}
86+
87+
load() {
88+
let sods = localStorage.getItem(this.chatId);
89+
if (sods == null) {
90+
return;
91+
}
92+
/** @type {SimpleChatODS} */
93+
let ods = JSON.parse(sods);
94+
this.iLastSys = ods.iLastSys;
95+
this.xchat = ods.xchat;
96+
}
97+
7998
/**
8099
* Recent chat messages.
81100
* If iRecentUserMsgCnt < 0
@@ -142,6 +161,7 @@ class SimpleChat {
142161
if (role == Roles.System) {
143162
this.iLastSys = this.xchat.length - 1;
144163
}
164+
this.save();
145165
return true;
146166
}
147167

0 commit comments

Comments
 (0)