This commit is contained in:
xuzhenjun
2023-04-16 00:45:47 +08:00
parent e8c3f06099
commit f2112e6fe1
7 changed files with 96 additions and 164 deletions

View File

@@ -1,7 +1,7 @@
import { createParser } from "eventsource-parser";
import { NextRequest } from "next/server";
import { requestOpenai } from "../common";
import clientPromise from "../../db/mongdb";
import Atlas from "atlas-fetch-data-api";
async function createStream(req: NextRequest) {
const encoder = new TextEncoder();
@@ -51,31 +51,39 @@ async function createStream(req: NextRequest) {
export async function POST(req: NextRequest) {
const accessCode = req.headers.get("access-code");
try {
// // 使用 clientPromise 连接到 MongoDB 数据库
// const client = await clientPromise;
// // 选择数据库和集合
// const db = client.db("chat_db");
// const usersCollection = db.collection("users");
// // 查询用户数据
// const user = await usersCollection.findOne({ key: accessCode });
// console.log(user, accessCode);
// const tips =
// "您的链接授权已过期,为了避免恶意盗刷,\n 请关注微信公众号【coder思维】\n回复关键词`ai` 获取授权链接 \n ![](/wx.png)";
// if (!user) {
// return new Response(tips);
// }
// console.log("compare: ");
// console.log(
// user["expire"] < new Date().getTime(),
// user["expire"],
// new Date().getTime(),
// );
// if (user["expire"] < new Date().getTime()) {
// // 判断用户是否过期
// return new Response(tips);
// }
// MongoDB init by URL Endpoint
const atlasAPI = new Atlas({
dataSource: "Cluster0",
database: "chat_db",
apiKey:
"8uOObGDRUqxdzfFzk91CHMq1UcUbqwQvnE6XjPQZe2Nv1xEXRBUi3vakKBWg7nbH",
apiUrl: "https://data.mongodb-api.com/app/data-ffyyc/endpoint/data/v1",
});
// // 创建查询条件
let userRes = await atlasAPI.findOne({
collection: "users",
filter: { key: accessCode },
});
const tips =
"您的链接授权已过期,为了避免恶意盗刷,\n 请关注微信公众号【code思维】\n回复关键词`ai` 获取授权链接 \n ![](/wx.png)";
if (!userRes || !userRes.document) {
return new Response(tips);
}
const user = userRes.document;
console.log(
new Date(user["expire"]).getTime() < new Date().getTime(),
user["expire"],
new Date().getTime(),
);
if (new Date(user["expire"]).getTime() < new Date().getTime()) {
// 判断用户是否过期
return new Response(tips);
}
// 创建查询条件
// // 计算24小时前的时间戳
// const currentTime = new Date();
// const startTime = new Date(currentTime.getTime() - 24 * 60 * 60 * 1000);

View File

@@ -336,6 +336,13 @@ export function Chat(props: {
showSideBar?: () => void;
sideBarShowing?: boolean;
}) {
const [createNewSession, currentIndex, removeSession] = useChatStore(
(state) => [
state.newSession,
state.currentSessionIndex,
state.removeSession,
],
);
type RenderMessage = Message & { preview?: boolean };
const chatStore = useChatStore();
@@ -571,16 +578,18 @@ export function Chat(props: {
onClick={props?.showSideBar}
/>
</div>
<div className={styles["window-action-button"]}>
<IconButton
icon={<BrainIcon />}
bordered
title={Locale.Chat.Actions.CompressedHistory}
onClick={() => {
setShowPromptModal(true);
}}
/>
</div>
{!isMobileScreen() && (
<div className={styles["window-action-button"]}>
<IconButton
icon={<BrainIcon />}
bordered
title={Locale.Chat.Actions.CompressedHistory}
onClick={() => {
setShowPromptModal(true);
}}
/>
</div>
)}
<div className={styles["window-action-button"]}>
<IconButton
icon={<ExportIcon />}
@@ -594,6 +603,18 @@ export function Chat(props: {
}}
/>
</div>
{isMobileScreen() && ( // mobile only 显示新增聊天
<div className={styles["window-action-button"]}>
<IconButton
icon={<AddIcon />}
text={Locale.Home.NewChat}
onClick={() => {
createNewSession();
}}
bordered
/>
</div>
)}
{!isMobileScreen() && (
<div className={styles["window-action-button"]}>
<IconButton

View File

@@ -1,35 +0,0 @@
import { MongoClient } from "mongodb";
const uri = process.env.MONGODB_URI as string; //your mongodb connection string
const options = {};
declare global {
var _mongoClientPromise: Promise<MongoClient>;
}
class Singleton {
private static _instance: Singleton;
private client: MongoClient;
private clientPromise: Promise<MongoClient>;
private constructor() {
this.client = new MongoClient(uri, options);
this.clientPromise = this.client.connect();
if (process.env.NODE_ENV === "development") {
// In development mode, use a global variable so that the value
// is preserved across module reloads caused by HMR (Hot Module Replacement).
global._mongoClientPromise = this.clientPromise;
}
}
public static get instance() {
if (!this._instance) {
this._instance = new Singleton();
}
return this._instance.clientPromise;
}
}
const clientPromise = Singleton.instance;
// Export a module-scoped MongoClient promise. By doing this in a
// separate module, the client can be shared across functions.
export default clientPromise;

View File

@@ -10,7 +10,7 @@ const cn = {
ChatItemCount: (count: number) => `${count} 条对话`,
},
Chat: {
SubTitle: (count: number) => `与 ChatGPT 的 ${count} 条对话`,
SubTitle: (count: number) => `${count} 条对话`,
Actions: {
ChatList: "查看消息列表",
CompressedHistory: "查看压缩后的历史 Prompt",