From 2bd36d4b3401098aeb7a5039e30d1e3838202564 Mon Sep 17 00:00:00 2001 From: heimoshuiyu Date: Tue, 24 Oct 2023 20:54:53 +0800 Subject: [PATCH] v1.6.0 save api as template --- src/CHATGPT_API_WEB_VERSION.ts | 2 +- src/app.tsx | 6 +++ src/chatbox.tsx | 91 +++++++++++++++++++++++++++++++++- src/settings.tsx | 26 +++++++++- 4 files changed, 122 insertions(+), 3 deletions(-) diff --git a/src/CHATGPT_API_WEB_VERSION.ts b/src/CHATGPT_API_WEB_VERSION.ts index 451d8f5..79ca382 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.5.0"; +const CHATGPT_API_WEB_VERSION = "v1.6.0"; export default CHATGPT_API_WEB_VERSION; diff --git a/src/app.tsx b/src/app.tsx index 7252a73..39e5c80 100644 --- a/src/app.tsx +++ b/src/app.tsx @@ -14,6 +14,11 @@ export interface ChatStoreMessage extends Message { example: boolean; } +export interface TemplateAPI { + name: string; + key: string; + endpoint: string; +} export interface ChatStore { chatgpt_api_web_version: string; systemMessageContent: string; @@ -78,6 +83,7 @@ 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 const STORAGE_NAME_TEMPLATE_API = `${STORAGE_NAME_TEMPLATE}-api`; export function addTotalCost(cost: number) { let totalCost = getTotalCost(); diff --git a/src/chatbox.tsx b/src/chatbox.tsx index 90632be..905be02 100644 --- a/src/chatbox.tsx +++ b/src/chatbox.tsx @@ -1,7 +1,13 @@ import structuredClone from "@ungap/structured-clone"; import { createRef } from "preact"; import { StateUpdater, useEffect, useState } from "preact/hooks"; -import { ChatStore, STORAGE_NAME_TEMPLATE, addTotalCost } from "./app"; +import { + ChatStore, + STORAGE_NAME_TEMPLATE, + STORAGE_NAME_TEMPLATE_API, + TemplateAPI, + addTotalCost, +} from "./app"; import ChatGPT, { calculate_token_length, ChunkMessage, @@ -233,10 +239,22 @@ export default function ChatBOX(props: { localStorage.getItem(STORAGE_NAME_TEMPLATE) || "[]" ) as TemplateChatStore[] ); + const [templateAPIs, _setTemplateAPIs] = useState( + JSON.parse( + localStorage.getItem(STORAGE_NAME_TEMPLATE_API) || "[]" + ) as TemplateAPI[] + ); const setTemplates = (templates: TemplateChatStore[]) => { localStorage.setItem(STORAGE_NAME_TEMPLATE, JSON.stringify(templates)); _setTemplates(templates); }; + const setTemplateAPIs = (templateAPIs: TemplateAPI[]) => { + localStorage.setItem( + STORAGE_NAME_TEMPLATE_API, + JSON.stringify(templateAPIs) + ); + _setTemplateAPIs(templateAPIs); + }; return (
@@ -248,6 +266,8 @@ export default function ChatBOX(props: { selectedChatStoreIndex={props.selectedChatIndex} templates={templates} setTemplates={setTemplates} + templateAPIs={templateAPIs} + setTemplateAPIs={setTemplateAPIs} /> )}

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

+

已保存的 API 模板

+
+
+ {templateAPIs.map((t, index) => ( +
{ + chatStore.apiEndpoint = t.endpoint; + chatStore.apiKey = t.key; + setChatStore({ ...chatStore }); + }} + > + {t.name} +
+ + + + +
+ ))} +
+

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

@@ -465,6 +542,18 @@ export default function ChatBOX(props: { 请在左上角创建新会话:)

)} + {chatStore.chatgpt_api_web_version < "v1.6.0" && ( +

+
+ 提示:当前会话版本 {chatStore.chatgpt_api_web_version} {"< v1.6.0"} + 。 +
+ v1.6.0 开始保存会话模板时会将 apiKey 和 apiEndpoint + 设置为空,继续使用旧版可能在保存读取模板时出现问题 +
+ 请在左上角创建新会话:) +

+ )} {showRetry && (

+