From 41610d4801de8e5bab969e85fe2f04e31825b461 Mon Sep 17 00:00:00 2001 From: sunls24 Date: Wed, 27 Nov 2024 23:02:56 +0800 Subject: [PATCH] feat: remove infrequently used features --- app/api/chat/route.ts | 5 +- app/api/openai.ts | 2 +- app/image/page.tsx | 29 ----- components/chat-body.tsx | 29 +---- components/chat-input.tsx | 18 --- components/chat.tsx | 45 +------ components/dividers.tsx | 38 ------ components/header.tsx | 4 +- components/image/body.tsx | 50 -------- components/image/result.tsx | 41 ------ components/image/settings.tsx | 90 ------------- components/settings/settings-plugins.tsx | 28 +--- components/settings/settings.tsx | 13 +- lib/constants.ts | 6 - lib/store/chat.ts | 19 --- lib/store/config-chat.ts | 2 - lib/store/config-image.ts | 13 -- package.json | 2 +- pnpm-lock.yaml | 155 ++++++++++++----------- 19 files changed, 97 insertions(+), 492 deletions(-) delete mode 100644 app/image/page.tsx delete mode 100644 components/dividers.tsx delete mode 100644 components/image/body.tsx delete mode 100644 components/image/result.tsx delete mode 100644 components/image/settings.tsx delete mode 100644 lib/store/config-image.ts diff --git a/app/api/chat/route.ts b/app/api/chat/route.ts index 7b94e65..a5271c0 100644 --- a/app/api/chat/route.ts +++ b/app/api/chat/route.ts @@ -7,10 +7,7 @@ import { NextResponse } from "next/server"; export const runtime = "edge"; export async function POST(req: Request) { - const { messages, config, contextIndex } = await req.json(); - if (contextIndex) { - messages.splice(0, contextIndex); - } + const { messages, config } = await req.json(); try { const result = await streamText({ diff --git a/app/api/openai.ts b/app/api/openai.ts index 370be40..ab2d667 100644 --- a/app/api/openai.ts +++ b/app/api/openai.ts @@ -1,8 +1,8 @@ import { createOpenAI, OpenAIProvider } from "@ai-sdk/openai"; -const baseURL = process.env.OPENAI_BASE_URL; const OPENAI_API_KEY = process.env.OPENAI_API_KEY ?? ""; const clientPool: Map = new Map(); +const baseURL = process.env.OPENAI_BASE_URL; export function getOpenAI(apiKey: string): OpenAIProvider { apiKey = apiKey || OPENAI_API_KEY; diff --git a/app/image/page.tsx b/app/image/page.tsx deleted file mode 100644 index 95e7831..0000000 --- a/app/image/page.tsx +++ /dev/null @@ -1,29 +0,0 @@ -import React from "react"; -import { Card } from "@/components/ui/card"; -import { Separator } from "@/components/ui/separator"; -import { Metadata } from "next"; -import Body from "@/components/image/body"; -import HeaderSimple from "@/components/header-simple"; -import { Cat } from "lucide-react"; - -export const metadata: Metadata = { - title: "文生图", - appleWebApp: { - title: "文生图", - }, -}; - -export default function Page() { - return ( -
- - } - /> - - - -
- ); -} diff --git a/components/chat-body.tsx b/components/chat-body.tsx index 48d4aa7..0ed8bb4 100644 --- a/components/chat-body.tsx +++ b/components/chat-body.tsx @@ -1,8 +1,6 @@ import React, { useEffect, useRef, useState } from "react"; import ChatMsg from "@/components/chat-msg"; import { Message } from "ai"; -import { useChatStore } from "@/lib/store/chat"; -import Dividers from "@/components/dividers"; import { emitter, mittKey } from "@/lib/mitt"; import { ScrollArea } from "@/components/ui/scroll-area"; import { Loader } from "lucide-react"; @@ -10,24 +8,18 @@ import { Loader } from "lucide-react"; function ChatBody({ isLoading, messages, - contextIndex, reload, deleteMsg, editMsg, }: { isLoading: boolean; messages: Message[]; - contextIndex: number; reload: () => void; deleteMsg: (index: number) => void; editMsg: (index: number, content: string) => void; }) { const scrollRef = useRef(null); const [autoScroll, setAutoScroll] = useState(false); - const [currentIndex, clearContextHistory] = useChatStore((state) => [ - state.currentIndex, - state.clearContextHistory, - ]); useEffect(() => { (scrollRef.current!.firstElementChild as HTMLDivElement).style.display = @@ -48,10 +40,6 @@ function ChatBody({ scrollTo(); }); - useEffect(() => { - scrollTo(); - }, [currentIndex]); - function scrollTo() { requestAnimationFrame(() => { if ( @@ -89,16 +77,11 @@ function ChatBody({ = contextIndex ? deleteMsg : undefined - } - editMsg={ - !isLoading && index >= contextIndex ? editMsg : undefined - } + deleteMsg={!isLoading ? deleteMsg : undefined} + editMsg={!isLoading ? editMsg : undefined} reload={ !isLoading && index === messages.length - 1 && - index >= contextIndex && (index != 0 || value.role === "user") ? reload : undefined @@ -109,14 +92,6 @@ function ChatBody({ value.role === "assistant" } /> - {index === contextIndex - 1 && ( - - )} ); })} diff --git a/components/chat-input.tsx b/components/chat-input.tsx index df1560e..b8ef821 100644 --- a/components/chat-input.tsx +++ b/components/chat-input.tsx @@ -2,7 +2,6 @@ import React, { ChangeEventHandler, useState } from "react"; import Textarea from "@/components/textarea"; import { Button } from "@/components/ui/button"; import { - Archive, MessageCircleOff, PauseCircle, RefreshCcw, @@ -19,7 +18,6 @@ function ChatInput({ handleInputChange, handleSubmit, stop, - updateContext, }: { isLoading: boolean; input: string; @@ -27,7 +25,6 @@ function ChatInput({ handleInputChange: ChangeEventHandler; handleSubmit: ChangeEventHandler; stop: () => void; - updateContext: () => void; }) { const resetSession = useChatStore((state) => state.resetSession); @@ -58,21 +55,6 @@ function ChatInput({ return (
- - - - } - /> {!isLoading && ( throttle(100), []); - useEffect(() => { - throttleMemo(() => setSlowMessages(messages), messages.length === 0); - }, [messages]); - const { append: topicAppend, setMessages: topicSetMessages } = useChat({ onFinish(msg) { checkAutoTopic(() => updateCurrentTopic(trimTopic(msg.content))); @@ -61,16 +57,10 @@ function Chat() { return () => emitter.off(mittKey.STOP_LOADING, stop); }, []); - const [contextIndex, updateContext] = useChatStore((state) => [ - state.currentSession().contextIndex, - state.updateContext, - ]); - const getOptions = useCallback( () => ({ options: { body: { - contextIndex, config: { ...apiConfig, plugins: Object.fromEntries( @@ -82,7 +72,7 @@ function Chat() { }, }, }), - [apiConfig, contextIndex], + [apiConfig], ); // load message @@ -112,11 +102,8 @@ function Chat() { messages.splice(index, 1); saveMessage(messages); toast.success("消息已删除"); - if (index < contextIndex) { - updateContext(contextIndex - 1); - } }, - [messages, contextIndex], + [messages], ); const reloadByConfig = useCallback(() => reload(getOptions()), [getOptions]); @@ -125,8 +112,7 @@ function Chat() {
handleSubmit(e, getOptions())} stop={stop} - updateContext={() => { - if (slowMessages.length === 0) { - return; - } - if (contextIndex && contextIndex === slowMessages.length) { - updateContext(0); - return; - } - if (isLoading) { - stop(); - if (slowMessages.length === 1 && slowMessages[0].role === "user") { - return; - } - } - updateContext(slowMessages.length); - emitter.emit(mittKey.SCROLL); - }} />
); diff --git a/components/dividers.tsx b/components/dividers.tsx deleted file mode 100644 index 36787aa..0000000 --- a/components/dividers.tsx +++ /dev/null @@ -1,38 +0,0 @@ -import React from "react"; -import clsx from "clsx"; - -const lineStyle = "h-[1.5px] grow bg-foreground"; - -function Dividers({ - text, - className, - hoverText, - hoverClick, -}: { - text: string; - className?: string; - hoverText?: string; - hoverClick?: () => void; -}) { - return ( -
- -
- - {text} - - {hoverText && ( - - )} -
- -
- ); -} - -export default React.memo(Dividers); diff --git a/components/header.tsx b/components/header.tsx index 50118f1..357efbb 100644 --- a/components/header.tsx +++ b/components/header.tsx @@ -4,7 +4,7 @@ import { Button } from "@/components/ui/button"; import HeaderTopic from "@/components/header-topic"; import ChatList from "@/components/dialog/chat-list"; import Settings from "@/components/settings/settings"; -import { ImageButton, ShortcutButton } from "@/components/button-icon"; +import { ShortcutButton } from "@/components/button-icon"; import { Separator } from "@/components/ui/separator"; function Header() { @@ -21,8 +21,6 @@ function Header() { - - diff --git a/components/image/body.tsx b/components/image/body.tsx deleted file mode 100644 index 558db1f..0000000 --- a/components/image/body.tsx +++ /dev/null @@ -1,50 +0,0 @@ -"use client"; -import React, { useState } from "react"; -import Settings from "@/components/image/settings"; -import Result from "@/components/image/result"; -import { useImageConfig } from "@/lib/store/config-image"; -import { fetchPost } from "@/lib/utils"; -import { useConfig } from "@/lib/store/config-chat"; -import { toast } from "sonner"; - -function Body() { - const config = useImageConfig(); - const [apiKey, model] = useConfig((state) => [ - state.apiConfig.apiKey, - state.apiConfig.model, - ]); - const [isLoading, setIsLoading] = useState(false); - const [img, setImg] = useState(); - - async function onGenerate(text: string) { - setImg(undefined); - setIsLoading(true); - try { - const res = await fetchPost("/api/image", { - config, - apiKey, - model, - prompt: text, - }); - const url = await res.text(); - if (!res.ok) { - throw new Error(url); - } - setImg(url); - toast.success("图像生成成功"); - } catch (err: any) { - toast.error(err.toString()); - } finally { - setIsLoading(false); - } - } - - return ( -
- - -
- ); -} - -export default Body; diff --git a/components/image/result.tsx b/components/image/result.tsx deleted file mode 100644 index 137cc01..0000000 --- a/components/image/result.tsx +++ /dev/null @@ -1,41 +0,0 @@ -import React from "react"; -import { Loader } from "lucide-react"; - -function Result({ - img, - isLoading, -}: { - isLoading: boolean; - img: string | undefined; -}) { - return ( -
- {img ? ( - - ) : ( -
- -
- )} - {isLoading && ( -
- - 精彩即将呈现 -
- )} -
- ); -} - -export default Result; - -function Img({ url }: { url: string }) { - return ( - An error occurred - ); -} diff --git a/components/image/settings.tsx b/components/image/settings.tsx deleted file mode 100644 index b5b9e7e..0000000 --- a/components/image/settings.tsx +++ /dev/null @@ -1,90 +0,0 @@ -import React, { useState } from "react"; -import { useImageConfig } from "@/lib/store/config-image"; -import SelectWarp from "@/components/select-warp"; -import { Button } from "@/components/ui/button"; -import Textarea from "@/components/textarea"; -import { Box, RefreshCcw } from "lucide-react"; -import { imageModels } from "@/lib/constants"; -import Mounted from "@/components/mounted"; -import SettingsSwitch from "@/components/settings/settings-switch"; -import { Skeleton } from "@/components/ui/skeleton"; - -function Settings({ - isLoading, - onGenerate, -}: { - isLoading: boolean; - onGenerate: (text: string) => Promise; -}) { - const config = useImageConfig(); - const updateConfig = useImageConfig((state) => state.update); - - const [text, setText] = useState(); - - async function onKeyDown(e: React.KeyboardEvent) { - if (e.key !== "Enter" || e.nativeEvent.isComposing || e.shiftKey) { - return; - } - e.preventDefault(); - await onClick(); - } - - async function onClick() { - if (isLoading || !text) { - return; - } - await onGenerate(text); - } - - return ( -
- updateConfig((cfg) => (cfg.model = v))} - widthClass="w-[150px]" - disabled={isLoading} - icon={} - /> -