From e214302817af2dd6521132ea299c254ca831d5d8 Mon Sep 17 00:00:00 2001 From: sijinhui Date: Wed, 8 May 2024 22:28:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0token=E7=9A=84=E8=AE=A1?= =?UTF-8?q?=E7=AE=97=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/admin/charts/route.ts | 2 +- app/api/logs/[[...path]]/route.ts | 40 ++++++++++++- app/components/chat.tsx | 93 +++++++++++++++++++------------ 3 files changed, 97 insertions(+), 38 deletions(-) diff --git a/app/api/admin/charts/route.ts b/app/api/admin/charts/route.ts index 959afa18d..f14fc1237 100644 --- a/app/api/admin/charts/route.ts +++ b/app/api/admin/charts/route.ts @@ -1,6 +1,6 @@ import { NextRequest, NextResponse } from "next/server"; import prisma from "@/lib/prisma"; -import { addHours, subMinutes } from "date-fns"; +// import { addHours, subMinutes } from "date-fns"; import { EChartsOption } from "echarts"; import { getCurStartEnd } from "@/app/utils/custom"; diff --git a/app/api/logs/[[...path]]/route.ts b/app/api/logs/[[...path]]/route.ts index a3473a67f..2ec175bb4 100644 --- a/app/api/logs/[[...path]]/route.ts +++ b/app/api/logs/[[...path]]/route.ts @@ -1,11 +1,49 @@ import { NextRequest, NextResponse } from "next/server"; import prisma from "@/lib/prisma"; import { getTokenLength } from "@/lib/utils"; +import { getSession } from "@/lib/auth"; +import { getCurStartEnd } from "@/app/utils/custom"; async function handle( req: NextRequest, { params }: { params: { path: string[] } }, ) { + // 判断网址和请求方法 + const method = req.method; + // const url = req.url; + const { pathname, searchParams } = new URL(req.url); + const searchText = searchParams.get("search"); + if (method === "GET") { + try { + const session = await getSession(); + const user_id = session?.user.id; + const { startOfTheDayInTimeZone, endOfTheDayInTimeZone } = + getCurStartEnd(); + const current_token = await prisma.logEntry + .findMany({ + where: { + userID: user_id, + createdAt: { + gte: startOfTheDayInTimeZone.toISOString(), // gte 表示 '大于等于' + lte: endOfTheDayInTimeZone.toISOString(), // lte 表示 '小于等于' + }, + }, + select: { + logToken: true, + }, + }) + .then((result) => { + return result.reduce( + (acc, cur) => (cur.logToken ? acc + cur.logToken : acc), + 0, + ); + }); + // console.log('-----------', user_id, current_token) + return NextResponse.json({ result: { current_token: current_token } }); + } catch {} + return NextResponse.json({ error: "未知错误" }, { status: 500 }); + } + try { const request_data = await req.json(); try { @@ -43,7 +81,7 @@ async function handle( return NextResponse.json({ status: 1 }); } -// export const GET = handle; +export const GET = handle; export const POST = handle; // export const runtime = "edge"; diff --git a/app/components/chat.tsx b/app/components/chat.tsx index 7e8418dd8..36164a0ab 100644 --- a/app/components/chat.tsx +++ b/app/components/chat.tsx @@ -101,7 +101,7 @@ import { getClientConfig } from "../config/client"; import Image from "next/image"; import { useAllModels } from "../utils/hooks"; import { MultimodalContent } from "../client/api"; -import { getTokenLength } from "@/lib/utils"; +// import { getTokenLength } from "@/lib/utils"; import VoiceInput from "./voice-input"; // import GptPrompts from "./gpt-prompts"; // const VoiceInput = dynamic( @@ -1268,6 +1268,27 @@ function _Chat() { setAttachImages(images); } + useEffect(() => { + if (!isLoading) { + try { + fetch("/api/logs/get_current_token", { + method: "GET", + credentials: "include", + }) + .then((response) => response.json()) + .then((result) => { + console.log("请求成功,", result); + localStorage.setItem( + "current_day_token", + result["result"]["current_token"], + ); + }); + } catch {} + + console.log("加载消息完成。获取token"); + } + }, [isLoading]); + // const [ voiceInputText, setVoiceInputText ] = useState(""); // const [ voiceInputLoading, setVoiceInputLoading ] = useState(false); @@ -1756,45 +1777,45 @@ function _Chat() { ); } -function getCurrentDayToken(sessions: ChatSession[]): number { - try { - const currentTime = new Date(); - const startOfTheDayInTimeZone = new Date( - currentTime.getFullYear(), - currentTime.getMonth(), - currentTime.getDate(), - 0, - 0, - 0, - ); - const current_day_message = sessions - .reduce((acc, item) => { - // @ts-ignore - return acc.concat(item.messages); - }, []) - .filter((item1) => { - // @ts-ignore - const dateToCheck = new Date(item1.date); - return startOfTheDayInTimeZone < dateToCheck; - }); - const all_current_day_content = current_day_message - // @ts-ignore - .map((item) => item.content) - .join(" "); - // 获取会话之后,再整合content, - return getTokenLength(all_current_day_content); - } catch (e) { - return 0; - } -} +// function getCurrentDayToken(sessions: ChatSession[]): number { +// try { +// const currentTime = new Date(); +// const startOfTheDayInTimeZone = new Date( +// currentTime.getFullYear(), +// currentTime.getMonth(), +// currentTime.getDate(), +// 0, +// 0, +// 0, +// ); +// const current_day_message = sessions +// .reduce((acc, item) => { +// // @ts-ignore +// return acc.concat(item.messages); +// }, []) +// .filter((item1) => { +// // @ts-ignore +// const dateToCheck = new Date(item1.date); +// return startOfTheDayInTimeZone < dateToCheck; +// }); +// const all_current_day_content = current_day_message +// // @ts-ignore +// .map((item) => item.content) +// .join(" "); +// // 获取会话之后,再整合content, +// return getTokenLength(all_current_day_content); +// } catch (e) { +// return 0; +// } +// } export function Chat() { const chatStore = useChatStore(); const sessionIndex = chatStore.currentSessionIndex; // 这里计先计算一下当天总token数。 - localStorage.setItem( - "current_day_token", - String(getCurrentDayToken(chatStore.sessions)), - ); + // localStorage.setItem( + // "current_day_token", + // String(getCurrentDayToken(chatStore.sessions)), + // ); return <_Chat key={sessionIndex}>; }