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 3, 2024
1 parent 685db86 commit 1e3d24d
Show file tree
Hide file tree
Showing 32 changed files with 104 additions and 1,119 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
- `GOOGLE_API_KEY`:用于 Google 搜索插件(可选)
- `GOOGLE_ENGINE_ID`:用于 Google 搜索插件(可选)

_Google 的 Gemini 模型需要使用 [OneAPI](https://github.com/songquanpeng/one-api) 进行接口分发_

## 🚀 本地运行

1. 克隆仓库:
Expand Down
9 changes: 3 additions & 6 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 All @@ -19,9 +16,9 @@ export async function POST(req: Request) {
model: getOpenAI(config.apiKey).chat(config.model),
system: config.systemPrompt ? systemPrompt() : undefined,
messages: messages,
maxSteps: 3,
maxSteps: 6,
tools: Object.fromEntries(
Object.entries(tools).filter(([key]) => config.plugins[key]?.enabled),
Object.entries(tools).filter(([key]) => config.plugins[key] === true),
),
});

Expand Down
29 changes: 0 additions & 29 deletions app/image/page.tsx

This file was deleted.

29 changes: 0 additions & 29 deletions app/shortcut/page.tsx

This file was deleted.

34 changes: 1 addition & 33 deletions components/button-icon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import React, { ReactElement } from "react";
import { GITHUB_URL, VERSION } from "@/lib/constants";
import { Button } from "@/components/ui/button";
import { Cat, GithubIcon, Layers2, RotateCcwSquare } from "lucide-react";
import { GithubIcon } from "lucide-react";
import { clsx } from "clsx";

function ButtonIcon({
Expand Down Expand Up @@ -46,35 +46,3 @@ export function GithubButton() {
/>
);
}

export function HomeButton() {
return (
<ButtonIcon
url="/"
text="去聊天"
icon={<RotateCcwSquare size={20} strokeWidth={1.8} />}
/>
);
}

export function ImageButton() {
return (
<ButtonIcon
url="/image"
text="文生图"
icon={<Cat size={20} strokeWidth={1.8} />}
textClass="hidden sm:block"
/>
);
}

export function ShortcutButton() {
return (
<ButtonIcon
url="/shortcut"
text="快捷应用"
icon={<Layers2 size={20} strokeWidth={1.8} />}
textClass="hidden sm:block"
/>
);
}
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
50 changes: 7 additions & 43 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,13 @@ function Chat() {
stop,
} = useChat({
id: useChatID(),
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,28 +56,18 @@ 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(
Object.entries(apiConfig.plugins).filter(
([, value]) => value.enabled,
),
),
plugins: apiConfig.plugins,
},
},
},
}),
[apiConfig, contextIndex],
[apiConfig],
);

// load message
Expand Down Expand Up @@ -112,11 +97,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 +107,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 +120,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
Loading

0 comments on commit 1e3d24d

Please sign in to comment.