From e2efcb4fc2cded7f6a8e004d693666dd8df8e229 Mon Sep 17 00:00:00 2001 From: HanishKVC Date: Fri, 31 May 2024 00:05:43 +0530 Subject: [PATCH] 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. --- examples/server/public_simplechat/readme.md | 1 + .../server/public_simplechat/simplechat.js | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/examples/server/public_simplechat/readme.md b/examples/server/public_simplechat/readme.md index 1643485f8..5ca5f4ac6 100644 --- a/examples/server/public_simplechat/readme.md +++ b/examples/server/public_simplechat/readme.md @@ -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. diff --git a/examples/server/public_simplechat/simplechat.js b/examples/server/public_simplechat/simplechat.js index d115fb9b7..6b25b73bc 100644 --- a/examples/server/public_simplechat/simplechat.js +++ b/examples/server/public_simplechat/simplechat.js @@ -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; }