From e81044c4b56a7e3425525d1ada0620ed78006852 Mon Sep 17 00:00:00 2001 From: heimoshuiyu Date: Thu, 16 Mar 2023 13:02:24 +0800 Subject: [PATCH] settings window --- src/app.tsx | 103 ++++++--------------------- src/getDefaultParam.ts | 18 +++++ src/settings.tsx | 156 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 197 insertions(+), 80 deletions(-) create mode 100644 src/getDefaultParam.ts create mode 100644 src/settings.tsx diff --git a/src/app.tsx b/src/app.tsx index c2e016c..20d3ac7 100644 --- a/src/app.tsx +++ b/src/app.tsx @@ -3,6 +3,8 @@ import "./global.css"; import ChatGPT, { Message, ChunkMessage } from "./chatgpt"; import { createRef } from "preact"; +import Settings from "./settings"; +import getDefaultParams from "./getDefaultParam"; export interface ChatStore { systemMessageContent: string; @@ -16,49 +18,23 @@ export interface ChatStore { streamMode: boolean; } -const defaultAPIKEY = () => { - const queryParameters = new URLSearchParams(window.location.search); - const key = queryParameters.get("key"); - return key; -}; - -const defaultSysMessage = () => { - const queryParameters = new URLSearchParams(window.location.search); - const sys = queryParameters.get("sys"); - return sys; -}; - -const defaultAPIEndpoint = () => { - const queryParameters = new URLSearchParams(window.location.search); - const sys = queryParameters.get("api"); - return sys; -}; - -const defauleMode = () => { - const queryParameters = new URLSearchParams(window.location.search); - const sys = queryParameters.get("mode"); - if (sys === "stream") return true; - if (sys === "fetch") return false; - return undefined; -}; - const _defaultAPIEndpoint = "https://api.openai.com/v1/chat/completions"; -export const newChatStore = ( +const newChatStore = ( apiKey = "", systemMessageContent = "你是一个猫娘,你要模仿猫娘的语气说话", apiEndpoint = _defaultAPIEndpoint, streamMode = true ): ChatStore => { return { - systemMessageContent: defaultSysMessage() || systemMessageContent, + systemMessageContent: getDefaultParams("sys", systemMessageContent), history: [], postBeginIndex: 0, tokenMargin: 1024, totalTokens: 0, maxTokens: 4096, - apiKey: defaultAPIKEY() || apiKey, - apiEndpoint: defaultAPIEndpoint() || apiEndpoint, - streamMode: defauleMode() ?? streamMode, + apiKey: getDefaultParams("key", apiKey), + apiEndpoint: getDefaultParams("api", apiEndpoint), + streamMode: getDefaultParams("mode", streamMode), }; }; @@ -213,16 +189,16 @@ export function App() { setChatStore({ ...chatStore }); }; - // change api key - const changAPIKEY = () => { - const newAPIKEY = prompt(`Current API KEY: ${chatStore.apiKey}`); - if (!newAPIKEY) return; - chatStore.apiKey = newAPIKEY; - setChatStore({ ...chatStore }); - }; + const [showSettings, setShowSettings] = useState(false); return (
+
-

+

setShowSettings(true)}>

- {" "} - {" "} - {" "} - {" "} + {" "} +
diff --git a/src/getDefaultParam.ts b/src/getDefaultParam.ts new file mode 100644 index 0000000..789bdb7 --- /dev/null +++ b/src/getDefaultParam.ts @@ -0,0 +1,18 @@ +function getDefaultParams(param: string, val: string): string; +function getDefaultParams(param: string, val: number): number; +function getDefaultParams(param: string, val: boolean): boolean; +function getDefaultParams(param: any, val: any) { + const queryParameters = new URLSearchParams(window.location.search); + const get = queryParameters.get(param); + if (typeof val === "string") { + return get ?? val; + } else if (typeof val === "number") { + return parseInt(get ?? `${val}`); + } else if (typeof val === "boolean") { + if (get === "stream") return true; + if (get === "fetch") return false; + return val; + } +} + +export default getDefaultParams; diff --git a/src/settings.tsx b/src/settings.tsx new file mode 100644 index 0000000..cea0000 --- /dev/null +++ b/src/settings.tsx @@ -0,0 +1,156 @@ +import { StateUpdater } from "preact/hooks"; +import { ChatStore } from "./app"; + +const Help = (props: { children: any; help: string }) => { + return ( +
+ +

{props.children}

+
+ ); +}; + +const Input = (props: { + chatStore: ChatStore; + setChatStore: (cs: ChatStore) => void; + field: "apiKey" | "systemMessageContent" | "apiEndpoint"; + help: string; +}) => { + return ( + + + { + props.chatStore[props.field] = event.target.value; + props.setChatStore({ ...props.chatStore }); + }} + > + + ); +}; +const Number = (props: { + chatStore: ChatStore; + setChatStore: (cs: ChatStore) => void; + field: "totalTokens" | "maxTokens" | "tokenMargin" | "postBeginIndex"; + readOnly: boolean; + help: string; +}) => { + return ( + + + { + console.log("type", typeof event.target.value); + let newNumber = parseInt(event.target.value); + if (newNumber < 0) newNumber = 0; + props.chatStore[props.field] = newNumber; + props.setChatStore({ ...props.chatStore }); + }} + > + + ); +}; +const Choice = (props: { + chatStore: ChatStore; + setChatStore: (cs: ChatStore) => void; + field: "streamMode"; + help: string; +}) => { + return ( + + + { + props.chatStore[props.field] = event.target.checked; + props.setChatStore({ ...props.chatStore }); + }} + > + + ); +}; + +export default (props: { + chatStore: ChatStore; + setChatStore: (cs: ChatStore) => void; + show: boolean; + setShow: StateUpdater; +}) => { + if (!props.show) return
; + return ( +
+
+

Settings

+
+
+ + + + + + + + +
+
+
+ +
+
+
+ ); +};