Skip to content

Commit

Permalink
Fix: frequent database queries in session callback
Browse files Browse the repository at this point in the history
  • Loading branch information
Peek-A-Booo committed May 25, 2023
1 parent 59a2686 commit ccfee50
Show file tree
Hide file tree
Showing 17 changed files with 264 additions and 118 deletions.
7 changes: 7 additions & 0 deletions CHANGE_LOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,23 @@

> 2023-05-25
### Fixed

- Fix the bug of frequent database queries in session callback

### Add

- Add a feature to count conversation fees and allow users to view the current amount of USD or tokens consumed during the conversation.
- Improve Token and session introduction
- Access the Help and Feedback Platform at support.qq.com
- Added the ability to switch between using Enter and Command+Enter (Mac)/Ctrl+Enter (Windows) for inputting information.
- Display the current application version in the menu
- Click on the session title to modify the current session configuration

### Changed

- Optimize the copy and delete UI of the conversation list to simplify the operation steps.
- Adjust the login credential validity period to 3 days

## v0.4.1

Expand Down
23 changes: 23 additions & 0 deletions CHANGE_LOG.zh_CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# L-GPT 更新日志

## v0.4.2

> 2023-05-25
### 修复

- 修复 session 回调频繁查询数据库的 bug

### 新增

- 新增当前会话消耗的 Token 和 USD 展示
- 新增有关 Token 和会话上下文限制相关介绍
- 新增问题意见反馈渠道:support.qq.com
- 新增支持切换 Enter 和 Command+Enter (Mac)/Ctrl+Enter (Windows) 输入信息的方式
- 新增在左侧菜单顶部展示当前应用版本号
- 新增支持点击会话标题修改当前会话配置

### 调整

- 简化会话聊天内容“复制”和“删除”的 UI 和逻辑
- 调整登录有效期时长为 3 天
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"@emotion/css": "11.11.0",
"@next-auth/prisma-adapter": "1.0.6",
"@prisma/client": "4.14.1",
"@radix-ui/react-accordion": "^1.1.1",
"@radix-ui/react-accordion": "1.1.1",
"@radix-ui/react-alert-dialog": "1.0.3",
"@radix-ui/react-context-menu": "2.1.3",
"@radix-ui/react-dialog": "1.0.3",
Expand Down
54 changes: 26 additions & 28 deletions src/app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,42 +35,38 @@ export const authOptions: NextAuthOptions = {
],
secret: process.env.EMAIL_SECRET,
callbacks: {
async jwt({ token, trigger }) {
const id = token.sub || token.id;

if (trigger === "update") {
const databaseUser = await prisma.user.findUnique({
where: { id: id as string },
});
return {
id,
name: databaseUser?.name,
email: databaseUser?.email,
image: databaseUser?.image,
};
}

return {
id,
name: token.name,
email: token.email,
image: token.picture || token.image,
};
},
async session({ session, token }: any) {
if (token) {
session.user.id = token.id;
session.user.name = token.name;
session.user.email = token.email;
session.user.image = token.image;
}
// through to web browser
return session;
},
async jwt({ token, user }) {
const databaseUser = await prisma.user.findFirst({
where: { email: token?.email },
});

// If there is no representation, it means that it is a new user
if (!databaseUser) {
return {
id: user?.id || token?.id || "",
name: token.name,
email: token.email,
image: token.image,
};
} else {
await prisma.user.update({
data: { recentlyUse: new Date() },
where: { id: databaseUser.id },
});
}

return {
id: databaseUser.id,
name: databaseUser.name,
email: databaseUser.email,
image: databaseUser.image,
};
},
async redirect({ url, baseUrl }) {
// Allows relative callback URLs
if (url.startsWith("/")) return `${baseUrl}${url}`;
Expand All @@ -81,7 +77,9 @@ export const authOptions: NextAuthOptions = {
},
session: {
strategy: "jwt",
maxAge: 7 * 24 * 60 * 60, // 30 days
// 1. If the user does not act within the validity period, the session will expire and they must log in again.
// 2. All actions that trigger the useSession behavior in the interface will refresh this validity period
maxAge: 3 * 24 * 60 * 60, // 3 days
},
pages: {
error: "/authError",
Expand Down
6 changes: 6 additions & 0 deletions src/app/api/azure/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getServerSession } from "next-auth/next";
import { authOptions } from "@/app/api/auth/[...nextauth]/route";
import { LLM } from "@/utils/constant";
import { isUndefined } from "@/lib";
import { prisma } from "@/lib/prisma";

// export const runtime = "edge";

Expand Down Expand Up @@ -122,6 +123,11 @@ export async function POST(request: Request) {

stream(response.body as ReadableStream, writable);

await prisma.user.update({
data: { recentlyUse: new Date() },
where: { id: session?.user.id },
});

return new Response(readable, response);
} catch (error: any) {
console.log(error, "azure error");
Expand Down
6 changes: 6 additions & 0 deletions src/app/api/openai/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getServerSession } from "next-auth/next";
import { authOptions } from "@/app/api/auth/[...nextauth]/route";
import { LLM } from "@/utils/constant";
import { isUndefined } from "@/lib";
import { prisma } from "@/lib/prisma";

// export const runtime = "edge";

Expand Down Expand Up @@ -80,6 +81,11 @@ export async function POST(request: Request) {
}),
});

await prisma.user.update({
data: { recentlyUse: new Date() },
where: { id: session?.user.id },
});

return new Response(response.body);
} catch (error) {
console.log(error, "openai error");
Expand Down
6 changes: 6 additions & 0 deletions src/app/api/user/update/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,11 @@ export async function POST(request: Request) {
where: { id: userId },
});

// update recently use
await prisma.user.update({
data: { recentlyUse: new Date() },
where: { id: userId },
});

return NextResponse.json({ error: 0 }, { status: 200 });
}
5 changes: 2 additions & 3 deletions src/components/chatSection/chatList/configure.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ const Configure: React.FC = () => {
const [isShow, setIsShow] = React.useState(true);

const { findChannel, options } = React.useMemo(() => {
const findChannel = channel.list.find(
(item) => item.channel_id === channel.activeId
);
const { list, activeId } = channel;
const findChannel = list.find((item) => item.channel_id === activeId);
const options =
LLM.find((item) => item.value === findChannel?.channel_model.type)
?.models || [];
Expand Down
10 changes: 8 additions & 2 deletions src/components/menu/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import Dropdown from "@/components/ui/Dropdown";
import type { IDropdownItems } from "@/components/ui/Dropdown";
import { LLM } from "@/utils/constant";
import MenuIcon from "./icon";
import pkg from "../../../package.json";

export const lans: IDropdownItems[] = [
{
Expand Down Expand Up @@ -155,10 +156,15 @@ const Menu: React.FC = () => {
"dark:bg-slate-800"
)}
>
<div className="flex font-extrabold h-12 text-transparent pb-2 text-2xl items-center">
<span className="bg-clip-text bg-gradient-to-r from-indigo-500 from-10% via-sky-500 via-30% to-emerald-500 to-90%">
<div className="flex gap-2 font-extrabold h-12 text-transparent pb-2 text-2xl items-center">
<span className="bg-clip-text leading-none bg-gradient-to-r from-indigo-500 from-10% via-sky-500 via-30% to-emerald-500 to-90%">
L - GPT
</span>
<div className="h-6 flex items-end">
<span className="text-neutral-300 text-sm font-medium">
v{pkg.version}
</span>
</div>
</div>
<Button
className="mb-2"
Expand Down
10 changes: 8 additions & 2 deletions src/components/menu/mobile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
import { LLM } from "@/utils/constant";
import MenuIcon from "./icon";
import { lans } from "./index";
import pkg from "../../../package.json";

const MobileMenu: React.FC = () => {
const { theme, setTheme } = useTheme();
Expand Down Expand Up @@ -121,10 +122,15 @@ const MobileMenu: React.FC = () => {
className="md:hidden"
overlayClassName="md:hidden"
title={
<div className="font-extrabold text-transparent text-xl">
<span className="bg-clip-text bg-gradient-to-r from-indigo-500 from-10% via-sky-500 via-30% to-emerald-500 to-90%">
<div className="font-extrabold text-transparent text-xl flex gap-2">
<span className="bg-clip-text leading-none bg-gradient-to-r from-indigo-500 from-10% via-sky-500 via-30% to-emerald-500 to-90%">
L - GPT
</span>
<div className="h-6 flex items-end">
<span className="text-neutral-300 text-sm font-medium">
v{pkg.version}
</span>
</div>
</div>
}
width="78%"
Expand Down
62 changes: 0 additions & 62 deletions src/components/navbar/changeTitle.tsx

This file was deleted.

Loading

1 comment on commit ccfee50

@vercel
Copy link

@vercel vercel bot commented on ccfee50 May 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.