From 2670183343cbd8fe51722a9ae76e6eb7fcd9a0bc Mon Sep 17 00:00:00 2001 From: heimoshuiyu Date: Tue, 15 Oct 2024 15:07:05 +0800 Subject: [PATCH] refact: @/utils/totalCost.tx --- src/app.tsx | 18 ----------- src/chatbox.tsx | 69 ++++++++++++++++++++---------------------- src/settings.tsx | 2 +- src/tts.tsx | 2 +- src/utils/totalCost.ts | 18 +++++++++++ 5 files changed, 53 insertions(+), 56 deletions(-) create mode 100644 src/utils/totalCost.ts diff --git a/src/app.tsx b/src/app.tsx index 51fc850..ccb6aad 100644 --- a/src/app.tsx +++ b/src/app.tsx @@ -12,28 +12,10 @@ import { newChatStore } from "@/types/newChatstore"; import { STORAGE_NAME, STORAGE_NAME_INDEXES, - STORAGE_NAME_TOTALCOST, STORAGE_NAME_SELECTED } from '@/const'; -export function addTotalCost(cost: number) { - let totalCost = getTotalCost(); - totalCost += cost; - localStorage.setItem(STORAGE_NAME_TOTALCOST, `${totalCost}`); -} - -export function getTotalCost(): number { - let totalCost = parseFloat( - localStorage.getItem(STORAGE_NAME_TOTALCOST) ?? "0", - ); - return totalCost; -} - -export function clearTotalCost() { - localStorage.setItem(STORAGE_NAME_TOTALCOST, `0`); -} - export function BuildFiledForSearch(chatStore: ChatStore): string[] { const contents_for_index: string[] = []; diff --git a/src/chatbox.tsx b/src/chatbox.tsx index 6dd7afe..ba13ac1 100644 --- a/src/chatbox.tsx +++ b/src/chatbox.tsx @@ -22,10 +22,7 @@ import { STORAGE_NAME_TEMPLATE_API_WHISPER, STORAGE_NAME_TEMPLATE_TOOLS, } from "@/const"; -import { - addTotalCost, - getTotalCost, -} from "@/app"; +import { addTotalCost, getTotalCost } from "@/utils/totalCost"; import ChatGPT, { calculate_token_length, ChunkMessage, @@ -98,7 +95,7 @@ export default function ChatBOX(props: { const update_total_tokens = () => { // manually estimate token client.total_tokens = calculate_token_length( - chatStore.systemMessageContent, + chatStore.systemMessageContent ); for (const msg of chatStore.history .filter(({ hide }) => !hide) @@ -154,7 +151,7 @@ export default function ChatBOX(props: { // update tool call arguments const tool = allChunkTool.find( - (tool) => tool.index === tool_call.index, + (tool) => tool.index === tool_call.index ); if (!tool) { @@ -169,7 +166,7 @@ export default function ChatBOX(props: { allChunkMessage.join("") + allChunkTool.map((tool) => { return `Tool Call ID: ${tool.id}\nType: ${tool.type}\nFunction: ${tool.function.name}\nArguments: ${tool.function.arguments}`; - }), + }) ); } setShowGenerating(false); @@ -307,7 +304,7 @@ export default function ChatBOX(props: { setShowGenerating(true); const response = await client._fetch( chatStore.streamMode, - chatStore.logprobs, + chatStore.logprobs ); const contentType = response.headers.get("content-type"); if (contentType?.startsWith("text/event-stream")) { @@ -377,33 +374,33 @@ export default function ChatBOX(props: { const [templates, _setTemplates] = useState( JSON.parse( - localStorage.getItem(STORAGE_NAME_TEMPLATE) || "[]", - ) as TemplateChatStore[], + localStorage.getItem(STORAGE_NAME_TEMPLATE) || "[]" + ) as TemplateChatStore[] ); const [templateAPIs, _setTemplateAPIs] = useState( JSON.parse( - localStorage.getItem(STORAGE_NAME_TEMPLATE_API) || "[]", - ) as TemplateAPI[], + localStorage.getItem(STORAGE_NAME_TEMPLATE_API) || "[]" + ) as TemplateAPI[] ); const [templateAPIsWhisper, _setTemplateAPIsWhisper] = useState( JSON.parse( - localStorage.getItem(STORAGE_NAME_TEMPLATE_API_WHISPER) || "[]", - ) as TemplateAPI[], + localStorage.getItem(STORAGE_NAME_TEMPLATE_API_WHISPER) || "[]" + ) as TemplateAPI[] ); const [templateAPIsTTS, _setTemplateAPIsTTS] = useState( JSON.parse( - localStorage.getItem(STORAGE_NAME_TEMPLATE_API_TTS) || "[]", - ) as TemplateAPI[], + localStorage.getItem(STORAGE_NAME_TEMPLATE_API_TTS) || "[]" + ) as TemplateAPI[] ); const [templateAPIsImageGen, _setTemplateAPIsImageGen] = useState( JSON.parse( - localStorage.getItem(STORAGE_NAME_TEMPLATE_API_IMAGE_GEN) || "[]", - ) as TemplateAPI[], + localStorage.getItem(STORAGE_NAME_TEMPLATE_API_IMAGE_GEN) || "[]" + ) as TemplateAPI[] ); const [toolsTemplates, _setToolsTemplates] = useState( JSON.parse( - localStorage.getItem(STORAGE_NAME_TEMPLATE_TOOLS) || "[]", - ) as TemplateTools[], + localStorage.getItem(STORAGE_NAME_TEMPLATE_TOOLS) || "[]" + ) as TemplateTools[] ); const setTemplates = (templates: TemplateChatStore[]) => { localStorage.setItem(STORAGE_NAME_TEMPLATE, JSON.stringify(templates)); @@ -412,35 +409,35 @@ export default function ChatBOX(props: { const setTemplateAPIs = (templateAPIs: TemplateAPI[]) => { localStorage.setItem( STORAGE_NAME_TEMPLATE_API, - JSON.stringify(templateAPIs), + JSON.stringify(templateAPIs) ); _setTemplateAPIs(templateAPIs); }; const setTemplateAPIsWhisper = (templateAPIWhisper: TemplateAPI[]) => { localStorage.setItem( STORAGE_NAME_TEMPLATE_API_WHISPER, - JSON.stringify(templateAPIWhisper), + JSON.stringify(templateAPIWhisper) ); _setTemplateAPIsWhisper(templateAPIWhisper); }; const setTemplateAPIsTTS = (templateAPITTS: TemplateAPI[]) => { localStorage.setItem( STORAGE_NAME_TEMPLATE_API_TTS, - JSON.stringify(templateAPITTS), + JSON.stringify(templateAPITTS) ); _setTemplateAPIsTTS(templateAPITTS); }; const setTemplateAPIsImageGen = (templateAPIImageGen: TemplateAPI[]) => { localStorage.setItem( STORAGE_NAME_TEMPLATE_API_IMAGE_GEN, - JSON.stringify(templateAPIImageGen), + JSON.stringify(templateAPIImageGen) ); _setTemplateAPIsImageGen(templateAPIImageGen); }; const setTemplateTools = (templateTools: TemplateTools[]) => { localStorage.setItem( STORAGE_NAME_TEMPLATE_TOOLS, - JSON.stringify(templateTools), + JSON.stringify(templateTools) ); _setToolsTemplates(templateTools); }; @@ -811,49 +808,49 @@ export default function ChatBOX(props: { if (!newChatStore.apiEndpoint) { newChatStore.apiEndpoint = getDefaultParams( "api", - chatStore.apiEndpoint, + chatStore.apiEndpoint ); } if (!newChatStore.apiKey) { newChatStore.apiKey = getDefaultParams( "key", - chatStore.apiKey, + chatStore.apiKey ); } if (!newChatStore.whisper_api) { newChatStore.whisper_api = getDefaultParams( "whisper-api", - chatStore.whisper_api, + chatStore.whisper_api ); } if (!newChatStore.whisper_key) { newChatStore.whisper_key = getDefaultParams( "whisper-key", - chatStore.whisper_key, + chatStore.whisper_key ); } if (!newChatStore.tts_api) { newChatStore.tts_api = getDefaultParams( "tts-api", - chatStore.tts_api, + chatStore.tts_api ); } if (!newChatStore.tts_key) { newChatStore.tts_key = getDefaultParams( "tts-key", - chatStore.tts_key, + chatStore.tts_key ); } if (!newChatStore.image_gen_api) { newChatStore.image_gen_api = getDefaultParams( "image-gen-api", - chatStore.image_gen_api, + chatStore.image_gen_api ); } if (!newChatStore.image_gen_key) { newChatStore.image_gen_key = getDefaultParams( "image-gen-key", - chatStore.image_gen_key, + chatStore.image_gen_key ); } newChatStore.cost = 0; @@ -910,7 +907,7 @@ export default function ChatBOX(props: {
↖{Tr("Click the conor to create a new chat")}
⚠ {Tr( - "All chat history and settings are stored in the local browser", + "All chat history and settings are stored in the local browser" )}

@@ -1153,7 +1150,7 @@ export default function ChatBOX(props: { } else { return content.map((c) => c?.text).join(" "); } - }), + }) ) .concat([inputMsg]) .join(" "); @@ -1167,7 +1164,7 @@ export default function ChatBOX(props: { await navigator.mediaDevices.getUserMedia({ audio: true, }), - { audioBitsPerSecond: 64 * 1000 }, + { audioBitsPerSecond: 64 * 1000 } ); // mount mediaRecorder to ref diff --git a/src/settings.tsx b/src/settings.tsx index c63b893..9b92712 100644 --- a/src/settings.tsx +++ b/src/settings.tsx @@ -21,7 +21,7 @@ import { useState, Dispatch, } from "preact/hooks"; -import { clearTotalCost, getTotalCost } from "@/app"; +import { clearTotalCost, getTotalCost } from "@/utils/totalCost"; import { ChatStore, TemplateChatStore, diff --git a/src/tts.tsx b/src/tts.tsx index 822a5a7..6848507 100644 --- a/src/tts.tsx +++ b/src/tts.tsx @@ -1,7 +1,7 @@ import { SpeakerWaveIcon } from "@heroicons/react/24/outline"; import { useMemo, useState } from "preact/hooks"; -import { addTotalCost } from "@/app"; +import { addTotalCost } from "@/utils/totalCost"; import { ChatStore, ChatStoreMessage } from "@/types/chatstore"; import { Message, getMessageText } from "@/chatgpt"; diff --git a/src/utils/totalCost.ts b/src/utils/totalCost.ts new file mode 100644 index 0000000..5b2d702 --- /dev/null +++ b/src/utils/totalCost.ts @@ -0,0 +1,18 @@ +import { STORAGE_NAME_TOTALCOST } from "@/const"; + +export function addTotalCost(cost: number) { + let totalCost = getTotalCost(); + totalCost += cost; + localStorage.setItem(STORAGE_NAME_TOTALCOST, `${totalCost}`); +} + +export function getTotalCost(): number { + let totalCost = parseFloat( + localStorage.getItem(STORAGE_NAME_TOTALCOST) ?? "0" + ); + return totalCost; +} + +export function clearTotalCost() { + localStorage.setItem(STORAGE_NAME_TOTALCOST, `0`); +}