diff --git a/src/addImage.tsx b/src/addImage.tsx index 0caec96..a05c9fe 100644 --- a/src/addImage.tsx +++ b/src/addImage.tsx @@ -1,5 +1,5 @@ import { useState } from "preact/hooks"; -import { ChatStore } from "@/app"; +import { ChatStore } from "@/types/chatstore"; import { MessageDetail } from "@/chatgpt"; import { Tr } from "@/translate"; diff --git a/src/app.tsx b/src/app.tsx index bbc8f53..9d8c6dc 100644 --- a/src/app.tsx +++ b/src/app.tsx @@ -3,89 +3,11 @@ import { useEffect, useState } from "preact/hooks"; import "@/global.css"; import { calculate_token_length } from "@/chatgpt"; -import getDefaultParams from "@/getDefaultParam"; +import getDefaultParams from "@/utils/getDefaultParam"; import ChatBOX from "@/chatbox"; import models, { defaultModel } from "@/models"; import { Tr, langCodeContext, LANG_OPTIONS } from "@/translate"; -import { ChatStore } from "@/types/chatstore"; - -import CHATGPT_API_WEB_VERSION from "@/CHATGPT_API_WEB_VERSION"; - -export interface TemplateAPI { - name: string; - key: string; - endpoint: string; -} - -export interface TemplateTools { - name: string; - toolsString: string; -} - -const _defaultAPIEndpoint = "https://api.openai.com/v1/chat/completions"; -export const newChatStore = ( - apiKey = "", - systemMessageContent = "", - apiEndpoint = _defaultAPIEndpoint, - streamMode = true, - model = defaultModel, - temperature = 0.7, - dev = false, - whisper_api = "https://api.openai.com/v1/audio/transcriptions", - whisper_key = "", - tts_api = "https://api.openai.com/v1/audio/speech", - tts_key = "", - tts_speed = 1.0, - tts_speed_enabled = false, - tts_format = "mp3", - toolsString = "", - image_gen_api = "https://api.openai.com/v1/images/generations", - image_gen_key = "", - json_mode = false, - logprobs = false -): ChatStore => { - return { - chatgpt_api_web_version: CHATGPT_API_WEB_VERSION, - systemMessageContent: getDefaultParams("sys", systemMessageContent), - toolsString, - history: [], - postBeginIndex: 0, - tokenMargin: 1024, - totalTokens: 0, - maxTokens: getDefaultParams( - "max", - models[getDefaultParams("model", model)]?.maxToken ?? 2048 - ), - maxGenTokens: 2048, - maxGenTokens_enabled: false, - apiKey: getDefaultParams("key", apiKey), - apiEndpoint: getDefaultParams("api", apiEndpoint), - streamMode: getDefaultParams("mode", streamMode), - model: getDefaultParams("model", model), - responseModelName: "", - cost: 0, - temperature: getDefaultParams("temp", temperature), - temperature_enabled: true, - top_p: 1, - top_p_enabled: false, - presence_penalty: 0, - frequency_penalty: 0, - develop_mode: getDefaultParams("dev", dev), - whisper_api: getDefaultParams("whisper-api", whisper_api), - whisper_key: getDefaultParams("whisper-key", whisper_key), - tts_api: getDefaultParams("tts-api", tts_api), - tts_key: getDefaultParams("tts-key", tts_key), - tts_voice: "alloy", - tts_speed: tts_speed, - tts_speed_enabled: tts_speed_enabled, - image_gen_api: image_gen_api, - image_gen_key: image_gen_key, - json_mode: json_mode, - tts_format: tts_format, - logprobs, - contents_for_index: [], - }; -}; +import { ChatStore, newChatStore } from "@/types/chatstore"; export const STORAGE_NAME = "chatgpt-api-web"; const STORAGE_NAME_SELECTED = `${STORAGE_NAME}-selected`; @@ -106,7 +28,7 @@ export function addTotalCost(cost: number) { export function getTotalCost(): number { let totalCost = parseFloat( - localStorage.getItem(STORAGE_NAME_TOTALCOST) ?? "0" + localStorage.getItem(STORAGE_NAME_TOTALCOST) ?? "0", ); return totalCost; } @@ -144,7 +66,7 @@ export function BuildFiledForSearch(chatStore: ChatStore): string[] { export function App() { // init selected index const [selectedChatIndex, setSelectedChatIndex] = useState( - parseInt(localStorage.getItem(STORAGE_NAME_SELECTED) ?? "1") + parseInt(localStorage.getItem(STORAGE_NAME_SELECTED) ?? "1"), ); console.log("selectedChatIndex", selectedChatIndex); useEffect(() => { @@ -161,7 +83,7 @@ export function App() { // copy from localStorage to indexedDB const allChatStoreIndexes: number[] = JSON.parse( - localStorage.getItem(STORAGE_NAME_INDEXES) ?? "[]" + localStorage.getItem(STORAGE_NAME_INDEXES) ?? "[]", ); let keyCount = 0; for (const i of allChatStoreIndexes) { @@ -175,7 +97,7 @@ export function App() { setSelectedChatIndex(keyCount); if (keyCount > 0) { alert( - "v2.0.0 Update: Imported chat history from localStorage to indexedDB. 🎉" + "v2.0.0 Update: Imported chat history from localStorage to indexedDB. 🎉", ); } } @@ -183,7 +105,7 @@ export function App() { if (oldVersion < 11) { if (oldVersion < 11 && oldVersion >= 1) { alert( - "Start upgrading storage, just a sec... (Click OK to continue)" + "Start upgrading storage, just a sec... (Click OK to continue)", ); } if ( @@ -201,7 +123,7 @@ export function App() { { multiEntry: true, unique: false, - } + }, ); // iter through all chatStore and update contents_for_index @@ -248,7 +170,7 @@ export function App() { const max = chatStore.maxTokens - chatStore.tokenMargin; let sum = 0; chatStore.postBeginIndex = chatStore.history.filter( - ({ hide }) => !hide + ({ hide }) => !hide, ).length; for (const msg of chatStore.history .filter(({ hide }) => !hide) @@ -263,7 +185,7 @@ export function App() { // manually estimate token chatStore.totalTokens = calculate_token_length( - chatStore.systemMessageContent + chatStore.systemMessageContent, ); for (const msg of chatStore.history .filter(({ hide }) => !hide) @@ -285,7 +207,7 @@ export function App() { // all chat store indexes const [allChatStoreIndexes, setAllChatStoreIndexes] = useState( - [] + [], ); const handleNewChatStoreWithOldOne = async (chatStore: ChatStore) => { @@ -312,8 +234,8 @@ export function App() { chatStore.image_gen_api, chatStore.image_gen_key, chatStore.json_mode, - false // logprobs default to false - ) + false, // logprobs default to false + ), ); setSelectedChatIndex(newKey as number); setAllChatStoreIndexes(await (await db).getAllKeys(STORAGE_NAME)); @@ -399,7 +321,7 @@ export function App() { return; console.log( "remove item", - `${STORAGE_NAME}-${selectedChatIndex}` + `${STORAGE_NAME}-${selectedChatIndex}`, ); (await db).delete(STORAGE_NAME, selectedChatIndex); const newAllChatStoreIndexes = await ( @@ -427,7 +349,7 @@ export function App() { onClick={async () => { if ( !confirm( - "Are you sure you want to delete **ALL** chat history?" + "Are you sure you want to delete **ALL** chat history?", ) ) return; diff --git a/src/chatbox.tsx b/src/chatbox.tsx index 74e86fd..b0e9aca 100644 --- a/src/chatbox.tsx +++ b/src/chatbox.tsx @@ -15,16 +15,12 @@ import { createRef } from "preact"; import { StateUpdater, useEffect, useState, Dispatch } from "preact/hooks"; import { Tr, langCodeContext, LANG_OPTIONS } from "@/translate"; import { - ChatStore, - ChatStoreMessage, STORAGE_NAME_TEMPLATE, STORAGE_NAME_TEMPLATE_API, STORAGE_NAME_TEMPLATE_API_IMAGE_GEN, STORAGE_NAME_TEMPLATE_API_TTS, STORAGE_NAME_TEMPLATE_API_WHISPER, STORAGE_NAME_TEMPLATE_TOOLS, - TemplateAPI, - TemplateTools, addTotalCost, getTotalCost, } from "@/app"; @@ -37,18 +33,22 @@ import ChatGPT, { ToolCall, Logprobs, } from "@/chatgpt"; +import { + ChatStore, + ChatStoreMessage, + TemplateChatStore, + TemplateAPI, + TemplateTools, +} from "./types/chatstore"; import Message from "@/message"; import models from "@/models"; import Settings from "@/settings"; -import getDefaultParams from "@/getDefaultParam"; +import getDefaultParams from "@/utils/getDefaultParam"; import { AddImage } from "@/addImage"; import { ListAPIs } from "@/listAPIs"; import { ListToolsTempaltes } from "@/listToolsTemplates"; import { autoHeight } from "@/textarea"; import Search from "@/search"; -export interface TemplateChatStore extends ChatStore { - name: string; -} export default function ChatBOX(props: { db: Promise>; diff --git a/src/editMessage.tsx b/src/editMessage.tsx index e267ad3..2c86fda 100644 --- a/src/editMessage.tsx +++ b/src/editMessage.tsx @@ -1,6 +1,6 @@ import { useState, useEffect, StateUpdater, Dispatch } from "preact/hooks"; import { Tr, langCodeContext, LANG_OPTIONS, tr } from "@/translate"; -import { ChatStore, ChatStoreMessage } from "@/app"; +import { ChatStore, ChatStoreMessage } from "@/types/chatstore"; import { EditMessageString } from "@/editMessageString"; import { EditMessageDetail } from "@/editMessageDetail"; diff --git a/src/editMessageDetail.tsx b/src/editMessageDetail.tsx index 9514501..b58f84b 100644 --- a/src/editMessageDetail.tsx +++ b/src/editMessageDetail.tsx @@ -1,4 +1,4 @@ -import { ChatStore, ChatStoreMessage } from "@/app"; +import { ChatStore, ChatStoreMessage } from "@/types/chatstore"; import { calculate_token_length } from "@/chatgpt"; import { Tr } from "@/translate"; diff --git a/src/editMessageString.tsx b/src/editMessageString.tsx index 80026b7..812ab45 100644 --- a/src/editMessageString.tsx +++ b/src/editMessageString.tsx @@ -1,4 +1,4 @@ -import { ChatStore, ChatStoreMessage } from "@/app"; +import { ChatStore, ChatStoreMessage } from "@/types/chatstore"; import { isVailedJSON } from "@/message"; import { calculate_token_length } from "@/chatgpt"; import { Tr } from "@/translate"; diff --git a/src/listAPIs.tsx b/src/listAPIs.tsx index 545e49d..f6d9cf1 100644 --- a/src/listAPIs.tsx +++ b/src/listAPIs.tsx @@ -1,4 +1,4 @@ -import { ChatStore, TemplateAPI } from "@/app"; +import { ChatStore, TemplateAPI } from "@/types/chatstore"; import { Tr } from "@/translate"; interface Props { diff --git a/src/listToolsTemplates.tsx b/src/listToolsTemplates.tsx index ec8d8be..87dd5b3 100644 --- a/src/listToolsTemplates.tsx +++ b/src/listToolsTemplates.tsx @@ -1,4 +1,4 @@ -import { ChatStore, TemplateTools } from "@/app"; +import { ChatStore, TemplateTools } from "@/types/chatstore"; import { Tr } from "@/translate"; interface Props { diff --git a/src/message.tsx b/src/message.tsx index bdfc9f0..3b624d3 100644 --- a/src/message.tsx +++ b/src/message.tsx @@ -3,7 +3,7 @@ import Markdown from "preact-markdown"; import { useState, useEffect, StateUpdater } from "preact/hooks"; import { Tr, langCodeContext, LANG_OPTIONS } from "@/translate"; -import { ChatStore, ChatStoreMessage } from "@/app"; +import { ChatStore, ChatStoreMessage } from "@/types/chatstore"; import { calculate_token_length, getMessageText } from "@/chatgpt"; import TTSButton, { TTSPlay } from "@/tts"; import { MessageHide } from "@/messageHide"; diff --git a/src/messageDetail.tsx b/src/messageDetail.tsx index d8c3eea..1978e0b 100644 --- a/src/messageDetail.tsx +++ b/src/messageDetail.tsx @@ -1,4 +1,4 @@ -import { ChatStoreMessage } from "@/app"; +import { ChatStoreMessage } from "@/types/chatstore"; interface Props { chat: ChatStoreMessage; diff --git a/src/messageHide.tsx b/src/messageHide.tsx index 97ec538..569f4f6 100644 --- a/src/messageHide.tsx +++ b/src/messageHide.tsx @@ -1,4 +1,4 @@ -import { ChatStoreMessage } from "@/app"; +import { ChatStoreMessage } from "@/types/chatstore"; import { getMessageText } from "@/chatgpt"; interface Props { diff --git a/src/messageToolCall.tsx b/src/messageToolCall.tsx index bcc3368..264a235 100644 --- a/src/messageToolCall.tsx +++ b/src/messageToolCall.tsx @@ -1,4 +1,4 @@ -import { ChatStoreMessage } from "@/app"; +import { ChatStoreMessage } from "@/types/chatstore"; interface Props { chat: ChatStoreMessage; diff --git a/src/messageToolResp.tsx b/src/messageToolResp.tsx index 33fb986..f884225 100644 --- a/src/messageToolResp.tsx +++ b/src/messageToolResp.tsx @@ -1,4 +1,4 @@ -import { ChatStoreMessage } from "@/app"; +import { ChatStoreMessage } from "@/types/chatstore"; interface Props { chat: ChatStoreMessage; diff --git a/src/search.tsx b/src/search.tsx index bb6a56e..e590e71 100644 --- a/src/search.tsx +++ b/src/search.tsx @@ -1,7 +1,7 @@ import { IDBPDatabase } from "idb"; import { StateUpdater, useRef, useState, Dispatch } from "preact/hooks"; -import { ChatStore } from "@/app"; +import { ChatStore } from "@/types/chatstore"; interface ChatStoreSearchResult { key: IDBValidKey; diff --git a/src/setAPIsTemplate.tsx b/src/setAPIsTemplate.tsx index 38d140f..c3a3319 100644 --- a/src/setAPIsTemplate.tsx +++ b/src/setAPIsTemplate.tsx @@ -1,4 +1,4 @@ -import { TemplateAPI } from "@/app"; +import { TemplateAPI } from "@/types/chatstore"; import { Tr } from "@/translate"; interface Props { diff --git a/src/settings.tsx b/src/settings.tsx index 3386ec6..ce120be 100644 --- a/src/settings.tsx +++ b/src/settings.tsx @@ -21,20 +21,19 @@ import { useState, Dispatch, } from "preact/hooks"; +import { clearTotalCost, getTotalCost } from "@/app"; import { ChatStore, + TemplateChatStore, TemplateAPI, TemplateTools, - clearTotalCost, - getTotalCost, -} from "@/app"; +} from "@/types/chatstore"; import models from "@/models"; -import { TemplateChatStore } from "@/chatbox"; import { tr, Tr, langCodeContext, LANG_OPTIONS } from "@/translate"; import { isVailedJSON } from "@/message"; import { SetAPIsTemplate } from "@/setAPIsTemplate"; import { autoHeight } from "@/textarea"; -import getDefaultParams from "@/getDefaultParam"; +import getDefaultParams from "@/utils/getDefaultParam"; const TTS_VOICES: string[] = [ "alloy", diff --git a/src/tts.tsx b/src/tts.tsx index ecea60d..822a5a7 100644 --- a/src/tts.tsx +++ b/src/tts.tsx @@ -1,7 +1,8 @@ import { SpeakerWaveIcon } from "@heroicons/react/24/outline"; import { useMemo, useState } from "preact/hooks"; -import { ChatStore, ChatStoreMessage, addTotalCost } from "@/app"; +import { addTotalCost } from "@/app"; +import { ChatStore, ChatStoreMessage } from "@/types/chatstore"; import { Message, getMessageText } from "@/chatgpt"; interface TTSProps { diff --git a/src/types/chatstore.ts b/src/types/chatstore.ts index bc77969..9486b8b 100644 --- a/src/types/chatstore.ts +++ b/src/types/chatstore.ts @@ -1,4 +1,7 @@ import { Logprobs, Message } from "@/chatgpt"; +import models, { defaultModel } from "@/models"; +import CHATGPT_API_WEB_VERSION from "@/CHATGPT_API_WEB_VERSION"; +import getDefaultParams from "@/utils/getDefaultParam"; /** * ChatStore is the main object of the chatgpt-api-web, @@ -44,6 +47,21 @@ export interface ChatStore { contents_for_index: string[]; } +export interface TemplateChatStore extends ChatStore { + name: string; +} + +export interface TemplateAPI { + name: string; + key: string; + endpoint: string; +} + +export interface TemplateTools { + name: string; + toolsString: string; +} + /** * ChatStoreMessage extends the Message type defined by OpenAI. * It adds more fields to be stored within the ChatStore structure. @@ -55,3 +73,68 @@ export interface ChatStoreMessage extends Message { audio: Blob | null; logprobs: Logprobs | null; } + +const _defaultAPIEndpoint = "https://api.openai.com/v1/chat/completions"; +export const newChatStore = ( + apiKey = "", + systemMessageContent = "", + apiEndpoint = _defaultAPIEndpoint, + streamMode = true, + model = defaultModel, + temperature = 0.7, + dev = false, + whisper_api = "https://api.openai.com/v1/audio/transcriptions", + whisper_key = "", + tts_api = "https://api.openai.com/v1/audio/speech", + tts_key = "", + tts_speed = 1.0, + tts_speed_enabled = false, + tts_format = "mp3", + toolsString = "", + image_gen_api = "https://api.openai.com/v1/images/generations", + image_gen_key = "", + json_mode = false, + logprobs = false, +): ChatStore => { + return { + chatgpt_api_web_version: CHATGPT_API_WEB_VERSION, + systemMessageContent: getDefaultParams("sys", systemMessageContent), + toolsString, + history: [], + postBeginIndex: 0, + tokenMargin: 1024, + totalTokens: 0, + maxTokens: getDefaultParams( + "max", + models[getDefaultParams("model", model)]?.maxToken ?? 2048, + ), + maxGenTokens: 2048, + maxGenTokens_enabled: false, + apiKey: getDefaultParams("key", apiKey), + apiEndpoint: getDefaultParams("api", apiEndpoint), + streamMode: getDefaultParams("mode", streamMode), + model: getDefaultParams("model", model), + responseModelName: "", + cost: 0, + temperature: getDefaultParams("temp", temperature), + temperature_enabled: true, + top_p: 1, + top_p_enabled: false, + presence_penalty: 0, + frequency_penalty: 0, + develop_mode: getDefaultParams("dev", dev), + whisper_api: getDefaultParams("whisper-api", whisper_api), + whisper_key: getDefaultParams("whisper-key", whisper_key), + tts_api: getDefaultParams("tts-api", tts_api), + tts_key: getDefaultParams("tts-key", tts_key), + tts_voice: "alloy", + tts_speed: tts_speed, + tts_speed_enabled: tts_speed_enabled, + image_gen_api: image_gen_api, + image_gen_key: image_gen_key, + json_mode: json_mode, + tts_format: tts_format, + logprobs, + contents_for_index: [], + }; +}; diff --git a/src/getDefaultParam.ts b/src/utils/getDefaultParam.ts similarity index 100% rename from src/getDefaultParam.ts rename to src/utils/getDefaultParam.ts