Skip to content

Commit

Permalink
feat: remove infrequently used features
Browse files Browse the repository at this point in the history
  • Loading branch information
sunls24 committed Dec 2, 2024
1 parent 685db86 commit 41610d4
Show file tree
Hide file tree
Showing 19 changed files with 97 additions and 492 deletions.
5 changes: 1 addition & 4 deletions app/api/chat/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion app/api/openai.ts
Original file line number Diff line number Diff line change
@@ -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<string, OpenAIProvider> = new Map();
const baseURL = process.env.OPENAI_BASE_URL;

export function getOpenAI(apiKey: string): OpenAIProvider {
apiKey = apiKey || OPENAI_API_KEY;
Expand Down
29 changes: 0 additions & 29 deletions app/image/page.tsx

This file was deleted.

29 changes: 2 additions & 27 deletions components/chat-body.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,25 @@
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";

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<HTMLDivElement>(null);
const [autoScroll, setAutoScroll] = useState(false);
const [currentIndex, clearContextHistory] = useChatStore((state) => [
state.currentIndex,
state.clearContextHistory,
]);

useEffect(() => {
(scrollRef.current!.firstElementChild as HTMLDivElement).style.display =
Expand All @@ -48,10 +40,6 @@ function ChatBody({
scrollTo();
});

useEffect(() => {
scrollTo();
}, [currentIndex]);

function scrollTo() {
requestAnimationFrame(() => {
if (
Expand Down Expand Up @@ -89,16 +77,11 @@ function ChatBody({
<ChatMsg
index={index}
msg={value}
deleteMsg={
!isLoading && index >= 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
Expand All @@ -109,14 +92,6 @@ function ChatBody({
value.role === "assistant"
}
/>
{index === contextIndex - 1 && (
<Dividers
text="上下文已清除"
className="pb-2"
hoverText={!isLoading ? "清除历史记录" : undefined}
hoverClick={clearContextHistory}
/>
)}
</div>
);
})}
Expand Down
18 changes: 0 additions & 18 deletions components/chat-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -19,15 +18,13 @@ function ChatInput({
handleInputChange,
handleSubmit,
stop,
updateContext,
}: {
isLoading: boolean;
input: string;
setInput: React.Dispatch<React.SetStateAction<string>>;
handleInputChange: ChangeEventHandler<HTMLTextAreaElement>;
handleSubmit: ChangeEventHandler<HTMLFormElement>;
stop: () => void;
updateContext: () => void;
}) {
const resetSession = useChatStore((state) => state.resetSession);

Expand Down Expand Up @@ -58,21 +55,6 @@ function ChatInput({
return (
<form onSubmit={handleSubmit} className="relative p-3 pt-1">
<div className="mb-1.5">
<TooltipWrap
content="清除上下文"
triggerAsChild={true}
trigger={
<Button
type="button"
className="h-8"
size="icon"
variant="ghost"
onClick={updateContext}
>
<Archive strokeWidth={1.5} size={22} />
</Button>
}
/>
{!isLoading && (
<TooltipWrap
content="重置聊天"
Expand Down
45 changes: 7 additions & 38 deletions components/chat.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"use client";
import React, { useCallback, useEffect, useMemo, useState } from "react";
import React, { useCallback, useEffect } from "react";
import ChatBody from "@/components/chat-body";
import ChatInput from "@/components/chat-input";
import { useChat } from "ai/react";
import { toast } from "sonner";
import { useChatID, useChatStore } from "@/lib/store/chat";
import { PROMPT_TOPIC } from "@/lib/constants";
import { throttle, trimTopic } from "@/lib/utils";
import { trimTopic } from "@/lib/utils";
import { useConfig } from "@/lib/store/config-chat";
import { emitter, mittKey } from "@/lib/mitt";
import { Separator } from "@/components/ui/separator";
Expand Down Expand Up @@ -35,18 +35,14 @@ function Chat() {
stop,
} = useChat({
id: useChatID(),
maxSteps: 3,
experimental_throttle: 100,
onError(err) {
toast.error(err.message);
input && setInput(input);
},
});

const [slowMessages, setSlowMessages] = useState(messages);
const throttleMemo = useMemo(() => throttle(100), []);
useEffect(() => {
throttleMemo(() => setSlowMessages(messages), messages.length === 0);
}, [messages]);

const { append: topicAppend, setMessages: topicSetMessages } = useChat({
onFinish(msg) {
checkAutoTopic(() => updateCurrentTopic(trimTopic(msg.content)));
Expand All @@ -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(
Expand All @@ -82,7 +72,7 @@ function Chat() {
},
},
}),
[apiConfig, contextIndex],
[apiConfig],
);

// load message
Expand Down Expand Up @@ -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]);
Expand All @@ -125,8 +112,7 @@ function Chat() {
<div className="flex h-0 flex-1 flex-col">
<ChatBody
isLoading={isLoading}
messages={slowMessages}
contextIndex={contextIndex}
messages={messages}
reload={reloadByConfig}
deleteMsg={deleteMsg}
editMsg={editMessage}
Expand All @@ -139,23 +125,6 @@ function Chat() {
handleInputChange={handleInputChange}
handleSubmit={(e) => 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);
}}
/>
</div>
);
Expand Down
38 changes: 0 additions & 38 deletions components/dividers.tsx

This file was deleted.

4 changes: 1 addition & 3 deletions components/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -21,8 +21,6 @@ function Header() {
<span className="flex-1" />
<ShortcutButton />
<Separator orientation="vertical" className="mx-1" />
<ImageButton />
<Separator orientation="vertical" className="mx-1" />
<Settings
trigger={
<Button variant="ghost" size="icon">
Expand Down
50 changes: 0 additions & 50 deletions components/image/body.tsx

This file was deleted.

Loading

0 comments on commit 41610d4

Please sign in to comment.