diff --git a/src/CHATGPT_API_WEB_VERSION.ts b/src/CHATGPT_API_WEB_VERSION.ts index a7d5d37..451d8f5 100644 --- a/src/CHATGPT_API_WEB_VERSION.ts +++ b/src/CHATGPT_API_WEB_VERSION.ts @@ -1,3 +1,3 @@ -const CHATGPT_API_WEB_VERSION = "v1.4.0"; +const CHATGPT_API_WEB_VERSION = "v1.5.0"; export default CHATGPT_API_WEB_VERSION; diff --git a/src/app.tsx b/src/app.tsx index 120e6d6..471ba79 100644 --- a/src/app.tsx +++ b/src/app.tsx @@ -71,6 +71,7 @@ const STORAGE_NAME = "chatgpt-api-web"; const STORAGE_NAME_SELECTED = `${STORAGE_NAME}-selected`; const STORAGE_NAME_INDEXES = `${STORAGE_NAME}-indexes`; const STORAGE_NAME_TOTALCOST = `${STORAGE_NAME}-totalcost`; +export const STORAGE_NAME_TEMPLATE = `${STORAGE_NAME}-template`; export function addTotalCost(cost: number) { let totalCost = getTotalCost(); diff --git a/src/chatbox.tsx b/src/chatbox.tsx index 391a4d0..684ce6a 100644 --- a/src/chatbox.tsx +++ b/src/chatbox.tsx @@ -1,6 +1,6 @@ import { createRef } from "preact"; import { StateUpdater, useEffect, useState } from "preact/hooks"; -import { ChatStore, addTotalCost } from "./app"; +import { ChatStore, STORAGE_NAME_TEMPLATE, addTotalCost } from "./app"; import ChatGPT, { calculate_token_length, ChunkMessage, @@ -10,6 +10,10 @@ import Message from "./message"; import models from "./models"; import Settings from "./settings"; +export interface TemplateChatStore extends ChatStore { + name: string; +} + export default function ChatBOX(props: { chatStore: ChatStore; setChatStore: (cs: ChatStore) => void; @@ -271,6 +275,17 @@ export default function ChatBOX(props: { }; const [showSettings, setShowSettings] = useState(false); + + const [templates, _setTemplates] = useState( + JSON.parse( + localStorage.getItem(STORAGE_NAME_TEMPLATE) || "[]" + ) as TemplateChatStore[] + ); + const setTemplates = (templates: TemplateChatStore[]) => { + localStorage.setItem(STORAGE_NAME_TEMPLATE, JSON.stringify(templates)); + _setTemplates(templates); + }; + return (
{showSettings && ( @@ -279,6 +294,8 @@ export default function ChatBOX(props: { setChatStore={setChatStore} setShow={setShowSettings} selectedChatStoreIndex={props.selectedChatIndex} + templates={templates} + setTemplates={setTemplates} /> )}

)} + {templates.length > 0 && + chatStore.history.filter((msg) => !msg.example).length == 0 && ( +

+

Templates

+
+
+ {templates.map((t, index) => ( +
{ + const newChatStore: any = { ...t }; + delete newChatStore.name; + setChatStore({ ...newChatStore }); + }} + > + {t.name} +
+ + + + +
+ ))} +
+

+ )} {chatStore.history.length === 0 && (

暂无历史对话记录 diff --git a/src/settings.tsx b/src/settings.tsx index bf6badf..888f40e 100644 --- a/src/settings.tsx +++ b/src/settings.tsx @@ -2,6 +2,7 @@ import { createRef } from "preact"; import { StateUpdater, useState } from "preact/hooks"; import { ChatStore, clearTotalCost, getTotalCost } from "./app"; import models from "./models"; +import { TemplateChatStore } from "./chatbox"; const Help = (props: { children: any; help: string }) => { return ( @@ -155,6 +156,8 @@ export default (props: { setChatStore: (cs: ChatStore) => void; setShow: StateUpdater; selectedChatStoreIndex: number; + templates: TemplateChatStore[]; + setTemplates: (templates: TemplateChatStore[]) => void; }) => { let link = location.protocol + @@ -331,6 +334,21 @@ export default (props: { > Export +