This commit is contained in:
archer
2023-07-01 13:09:02 +08:00
parent 4c54e1821b
commit 9bdd5f522d
85 changed files with 4738 additions and 1236 deletions

View File

@@ -4,9 +4,9 @@ import { connectToDatabase, Chat, Model } from '@/service/mongo';
import type { InitChatResponse } from '@/api/response/chat';
import { authUser } from '@/service/utils/auth';
import { ChatItemType } from '@/types/chat';
import { authModel } from '@/service/utils/auth';
import { authApp } from '@/service/utils/auth';
import mongoose from 'mongoose';
import type { ModelSchema } from '@/types/mongoSchema';
import type { AppSchema } from '@/types/mongoSchema';
/* 初始化我的聊天框,需要身份验证 */
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
@@ -21,7 +21,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
await connectToDatabase();
// 没有 modelId 时直接获取用户的第一个id
const model = await (async () => {
const app = await (async () => {
if (!modelId) {
const myModel = await Model.findOne({ userId });
if (!myModel) {
@@ -29,23 +29,23 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
name: '应用1',
userId
});
return (await Model.findById(_id)) as ModelSchema;
return (await Model.findById(_id)) as AppSchema;
} else {
return myModel;
}
} else {
// 校验使用权限
const authRes = await authModel({
modelId,
const authRes = await authApp({
appId: modelId,
userId,
authUser: false,
authOwner: false
});
return authRes.model;
return authRes.app;
}
})();
modelId = modelId || model._id;
modelId = modelId || app._id;
// 历史记录
let history: ChatItemType[] = [];
@@ -87,21 +87,21 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
]);
}
const isOwner = String(model.userId) === userId;
const isOwner = String(app.userId) === userId;
jsonRes<InitChatResponse>(res, {
data: {
chatId: chatId || '',
modelId: modelId,
model: {
name: model.name,
avatar: model.avatar,
intro: model.intro,
canUse: model.share.isShare || isOwner
name: app.name,
avatar: app.avatar,
intro: app.intro,
canUse: app.share.isShare || isOwner
},
chatModel: model.chat.chatModel,
systemPrompt: isOwner ? model.chat.systemPrompt : '',
limitPrompt: isOwner ? model.chat.limitPrompt : '',
chatModel: app.chat.chatModel,
systemPrompt: isOwner ? app.chat.systemPrompt : '',
limitPrompt: isOwner ? app.chat.limitPrompt : '',
history
}
});

View File

@@ -2,7 +2,7 @@ import type { NextApiRequest, NextApiResponse } from 'next';
import { jsonRes } from '@/service/response';
import { ChatItemType } from '@/types/chat';
import { connectToDatabase, Chat, Model } from '@/service/mongo';
import { authModel } from '@/service/utils/auth';
import { authApp } from '@/service/utils/auth';
import { authUser } from '@/service/utils/auth';
import { Types } from 'mongoose';
@@ -49,7 +49,7 @@ export async function saveChat({
userId
}: Props & { newChatId?: Types.ObjectId; userId: string }): Promise<{ newChatId: string }> {
await connectToDatabase();
const { model } = await authModel({ modelId, userId, authOwner: false });
const { app } = await authApp({ appId: modelId, userId, authOwner: false });
const content = prompts.map((item) => ({
_id: item._id,
@@ -59,7 +59,7 @@ export async function saveChat({
quote: item.quote || []
}));
if (String(model.userId) === userId) {
if (String(app.userId) === userId) {
await Model.findByIdAndUpdate(modelId, {
updateTime: new Date()
});
@@ -93,8 +93,8 @@ export async function saveChat({
newChatId: String(res._id)
}))
]),
// update model
...(String(model.userId) === userId
// update app
...(String(app.userId) === userId
? [
Model.findByIdAndUpdate(modelId, {
updateTime: new Date()

View File

@@ -1,7 +1,7 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import { jsonRes } from '@/service/response';
import { connectToDatabase, ShareChat } from '@/service/mongo';
import { authModel, authUser } from '@/service/utils/auth';
import { authApp, authUser } from '@/service/utils/auth';
import type { ShareChatEditType } from '@/types/model';
/* create a shareChat */
@@ -14,8 +14,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
await connectToDatabase();
const { userId } = await authUser({ req, authToken: true });
await authModel({
modelId,
await authApp({
appId: modelId,
userId,
authOwner: false
});

View File

@@ -2,7 +2,7 @@ import type { NextApiRequest, NextApiResponse } from 'next';
import { jsonRes } from '@/service/response';
import { connectToDatabase, ShareChat, User } from '@/service/mongo';
import type { InitShareChatResponse } from '@/api/response/chat';
import { authModel } from '@/service/utils/auth';
import { authApp } from '@/service/utils/auth';
import { hashPassword } from '@/service/utils/tools';
import { HUMAN_ICON } from '@/constants/chat';
@@ -35,8 +35,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}
// 校验使用权限
const { model } = await authModel({
modelId: shareChat.modelId,
const { app } = await authApp({
appId: shareChat.modelId,
userId: String(shareChat.userId),
authOwner: false
});
@@ -48,11 +48,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
maxContext: shareChat.maxContext,
userAvatar: user?.avatar || HUMAN_ICON,
model: {
name: model.name,
avatar: model.avatar,
intro: model.intro
name: app.name,
avatar: app.avatar,
intro: app.intro
},
chatModel: model.chat.chatModel
chatModel: app.chat.chatModel
}
});
} catch (err) {