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)}>
{props.children}
+