diff --git a/src/CHATGPT_API_WEB_VERSION.ts b/src/CHATGPT_API_WEB_VERSION.ts new file mode 100644 index 0000000..8ba647c --- /dev/null +++ b/src/CHATGPT_API_WEB_VERSION.ts @@ -0,0 +1,3 @@ +const CHATGPT_API_WEB_VERSION = "v1.2.2"; + +export default CHATGPT_API_WEB_VERSION; diff --git a/src/app.tsx b/src/app.tsx index de88811..dee192c 100644 --- a/src/app.tsx +++ b/src/app.tsx @@ -6,7 +6,10 @@ import getDefaultParams from "./getDefaultParam"; import ChatBOX from "./chatbox"; import { options } from "./settings"; +import CHATGPT_API_WEB_VERSION from './CHATGPT_API_WEB_VERSION' + export interface ChatStore { + chatgpt_api_web_version: string; systemMessageContent: string; history: Message[]; postBeginIndex: number; @@ -29,6 +32,7 @@ const newChatStore = ( model = "gpt-3.5-turbo" ): ChatStore => { return { + chatgpt_api_web_version: CHATGPT_API_WEB_VERSION, systemMessageContent: getDefaultParams("sys", systemMessageContent), history: [], postBeginIndex: 0, @@ -81,6 +85,8 @@ export function App() { // handle read from old version chatstore if (ret.model === undefined) ret.model = "gpt-3.5-turbo"; if (ret.responseModelName === undefined) ret.responseModelName = ""; + if (ret.chatgpt_api_web_version === undefined) + ret.chatgpt_api_web_version = CHATGPT_API_WEB_VERSION; return ret; }; diff --git a/src/settings.tsx b/src/settings.tsx index 3becf1f..4ef9b80 100644 --- a/src/settings.tsx +++ b/src/settings.tsx @@ -1,3 +1,4 @@ +import { createRef } from "preact"; import { StateUpdater } from "preact/hooks"; import { ChatStore } from "./app"; @@ -141,6 +142,8 @@ export default (props: { }&model=${props.chatStore.model}&sys=${encodeURIComponent( props.chatStore.systemMessageContent )}`; + + const importFileRef = createRef(); return (
@@ -225,12 +228,46 @@ export default (props: { "This will OVERWRITE the current chat history! Continue?" ) ) - alert("Error: This function is currently unimplemented :)"); - return; + return; + console.log("importFileRef", importFileRef); + importFileRef.current.click(); }} > Import + { + const file = importFileRef.current.files[0]; + console.log("file to import", file); + if (!file || file.type !== "application/json") { + alert("Please select a json file"); + return; + } + const reader = new FileReader(); + reader.onload = () => { + console.log("import content", reader.result); + if (!reader) { + alert("Empty file"); + return; + } + try { + const newChatStore: ChatStore = JSON.parse( + reader.result as string + ); + if (!newChatStore.chatgpt_api_web_version) { + throw "This is not an exported chatgpt-api-web chatstore file. The key 'chatgpt_api_web_version' is missing!"; + } + props.setChatStore({ ...newChatStore }); + } catch (e) { + alert(`Import error on parsing json: ${e}`); + } + }; + reader.readAsText(file); + }} + />