mirror of
https://github.com/labring/FastGPT.git
synced 2025-08-01 20:27:45 +00:00
perf: ui
This commit is contained in:
@@ -5,10 +5,9 @@ import { authUser } from '@/service/utils/auth';
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
try {
|
||||
const { historyId, contentId } = req.query as { historyId: string; contentId: string };
|
||||
console.log(historyId, contentId);
|
||||
const { chatId, contentId } = req.query as { chatId: string; contentId: string };
|
||||
|
||||
if (!historyId || !contentId) {
|
||||
if (!chatId || !contentId) {
|
||||
throw new Error('缺少参数');
|
||||
}
|
||||
|
||||
@@ -17,7 +16,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
// 凭证校验
|
||||
const { userId } = await authUser({ req, authToken: true });
|
||||
|
||||
const chatRecord = await Chat.findById(historyId);
|
||||
const chatRecord = await Chat.findById(chatId);
|
||||
|
||||
if (!chatRecord) {
|
||||
throw new Error('找不到对话');
|
||||
@@ -26,7 +25,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
// 删除一条数据库记录
|
||||
await Chat.updateOne(
|
||||
{
|
||||
_id: historyId,
|
||||
_id: chatId,
|
||||
userId
|
||||
},
|
||||
{ $pull: { content: { _id: contentId } } }
|
||||
|
@@ -27,7 +27,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
_id: item._id,
|
||||
updateTime: item.updateTime,
|
||||
appId: item.appId,
|
||||
title: item.customTitle || item.title,
|
||||
customTitle: item.customTitle,
|
||||
title: item.title,
|
||||
top: item.top
|
||||
}))
|
||||
});
|
||||
|
@@ -7,22 +7,22 @@ import { rawSearchKey } from '@/constants/chat';
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
try {
|
||||
const { historyId, contentId } = req.query as {
|
||||
historyId: string;
|
||||
const { chatId, contentId } = req.query as {
|
||||
chatId: string;
|
||||
contentId: string;
|
||||
};
|
||||
await connectToDatabase();
|
||||
|
||||
const { userId } = await authUser({ req, authToken: true });
|
||||
|
||||
if (!historyId || !contentId) {
|
||||
if (!chatId || !contentId) {
|
||||
throw new Error('params is error');
|
||||
}
|
||||
|
||||
const history = await Chat.aggregate([
|
||||
{
|
||||
$match: {
|
||||
_id: new Types.ObjectId(historyId),
|
||||
_id: new Types.ObjectId(chatId),
|
||||
userId: new Types.ObjectId(userId)
|
||||
}
|
||||
},
|
||||
|
@@ -4,7 +4,7 @@ import { connectToDatabase, Chat } from '@/service/mongo';
|
||||
import { authUser } from '@/service/utils/auth';
|
||||
|
||||
export type Props = {
|
||||
historyId: string;
|
||||
chatId: string;
|
||||
customTitle?: string;
|
||||
top?: boolean;
|
||||
};
|
||||
@@ -12,7 +12,7 @@ export type Props = {
|
||||
/* 更新聊天标题 */
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
try {
|
||||
const { historyId, customTitle, top } = req.body as Props;
|
||||
const { chatId, customTitle, top } = req.body as Props;
|
||||
|
||||
const { userId } = await authUser({ req, authToken: true });
|
||||
|
||||
@@ -20,7 +20,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
|
||||
await Chat.findOneAndUpdate(
|
||||
{
|
||||
_id: historyId,
|
||||
_id: chatId,
|
||||
userId
|
||||
},
|
||||
{
|
||||
|
@@ -7,12 +7,12 @@ import { Types } from 'mongoose';
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
try {
|
||||
let {
|
||||
historyId,
|
||||
chatId,
|
||||
contentId,
|
||||
quoteId,
|
||||
sourceText = ''
|
||||
} = req.body as {
|
||||
historyId: string;
|
||||
chatId: string;
|
||||
contentId: string;
|
||||
quoteId: string;
|
||||
sourceText: string;
|
||||
@@ -21,13 +21,13 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
|
||||
const { userId } = await authUser({ req, authToken: true });
|
||||
|
||||
if (!contentId || !historyId || !quoteId) {
|
||||
if (!contentId || !chatId || !quoteId) {
|
||||
throw new Error('params is error');
|
||||
}
|
||||
|
||||
await Chat.updateOne(
|
||||
{
|
||||
_id: new Types.ObjectId(historyId),
|
||||
_id: new Types.ObjectId(chatId),
|
||||
userId: new Types.ObjectId(userId),
|
||||
'content._id': new Types.ObjectId(contentId)
|
||||
},
|
||||
|
@@ -53,7 +53,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
const { chat, history = [] }: { chat?: ChatSchema; history?: ChatItemType[] } =
|
||||
await (async () => {
|
||||
if (chatId) {
|
||||
// auth historyId
|
||||
// auth chatId
|
||||
const chat = await Chat.findOne({
|
||||
_id: chatId,
|
||||
userId
|
||||
@@ -104,7 +104,6 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
intro: app.intro,
|
||||
canUse: app.share.isShare || isOwner
|
||||
},
|
||||
customTitle: chat?.customTitle,
|
||||
title: chat?.title || '新对话',
|
||||
variables: chat?.variables || {},
|
||||
history
|
||||
|
@@ -7,7 +7,7 @@ import { authUser } from '@/service/utils/auth';
|
||||
import { Types } from 'mongoose';
|
||||
|
||||
type Props = {
|
||||
historyId?: string;
|
||||
chatId?: string;
|
||||
appId: string;
|
||||
variables?: Record<string, any>;
|
||||
prompts: [ChatItemType, ChatItemType];
|
||||
@@ -16,7 +16,7 @@ type Props = {
|
||||
/* 聊天内容存存储 */
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
try {
|
||||
const { historyId, appId, prompts } = req.body as Props;
|
||||
const { chatId, appId, prompts } = req.body as Props;
|
||||
|
||||
if (!prompts) {
|
||||
throw new Error('缺少参数');
|
||||
@@ -25,7 +25,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
const { userId } = await authUser({ req, authToken: true });
|
||||
|
||||
const response = await saveChat({
|
||||
historyId,
|
||||
chatId,
|
||||
appId,
|
||||
prompts,
|
||||
userId
|
||||
@@ -43,13 +43,13 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
}
|
||||
|
||||
export async function saveChat({
|
||||
newHistoryId,
|
||||
historyId,
|
||||
newChatId,
|
||||
chatId,
|
||||
appId,
|
||||
prompts,
|
||||
variables,
|
||||
userId
|
||||
}: Props & { newHistoryId?: Types.ObjectId; userId: string }): Promise<{ newHistoryId: string }> {
|
||||
}: Props & { newChatId?: Types.ObjectId; userId: string }): Promise<{ newChatId: string }> {
|
||||
await connectToDatabase();
|
||||
const { app } = await authApp({ appId, userId, authOwner: false });
|
||||
|
||||
@@ -60,9 +60,9 @@ export async function saveChat({
|
||||
}
|
||||
|
||||
const [response] = await Promise.all([
|
||||
...(historyId
|
||||
...(chatId
|
||||
? [
|
||||
Chat.findByIdAndUpdate(historyId, {
|
||||
Chat.findByIdAndUpdate(chatId, {
|
||||
$push: {
|
||||
content: {
|
||||
$each: prompts
|
||||
@@ -72,19 +72,19 @@ export async function saveChat({
|
||||
title: prompts[0].value.slice(0, 20),
|
||||
updateTime: new Date()
|
||||
}).then(() => ({
|
||||
newHistoryId: ''
|
||||
newChatId: ''
|
||||
}))
|
||||
]
|
||||
: [
|
||||
Chat.create({
|
||||
_id: newHistoryId,
|
||||
_id: newChatId,
|
||||
userId,
|
||||
appId,
|
||||
variables,
|
||||
content: prompts,
|
||||
title: prompts[0].value.slice(0, 20)
|
||||
}).then((res) => ({
|
||||
newHistoryId: String(res._id)
|
||||
newChatId: String(res._id)
|
||||
}))
|
||||
]),
|
||||
// update app
|
||||
@@ -99,6 +99,6 @@ export async function saveChat({
|
||||
|
||||
return {
|
||||
// @ts-ignore
|
||||
newHistoryId: response?.newHistoryId || ''
|
||||
newChatId: response?.newChatId || ''
|
||||
};
|
||||
}
|
||||
|
@@ -20,7 +20,7 @@ import { BillSourceEnum } from '@/constants/user';
|
||||
|
||||
export type MessageItemType = ChatCompletionRequestMessage & { _id?: string };
|
||||
type FastGptWebChatProps = {
|
||||
historyId?: string; // undefined: nonuse history, '': new chat, 'xxxxx': use history
|
||||
chatId?: string; // undefined: nonuse history, '': new chat, 'xxxxx': use history
|
||||
appId?: string;
|
||||
};
|
||||
type FastGptShareChatProps = {
|
||||
@@ -47,14 +47,7 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
|
||||
res.end();
|
||||
});
|
||||
|
||||
let {
|
||||
historyId,
|
||||
appId,
|
||||
shareId,
|
||||
stream = false,
|
||||
messages = [],
|
||||
variables = {}
|
||||
} = req.body as Props;
|
||||
let { chatId, appId, shareId, stream = false, messages = [], variables = {} } = req.body as Props;
|
||||
|
||||
let billId = '';
|
||||
|
||||
@@ -91,7 +84,7 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
|
||||
appId,
|
||||
userId
|
||||
}),
|
||||
getChatHistory({ historyId, userId })
|
||||
getChatHistory({ chatId, userId })
|
||||
]);
|
||||
|
||||
const isOwner = !shareId && userId === String(app.userId);
|
||||
@@ -107,9 +100,9 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
|
||||
throw new Error('Question is empty');
|
||||
}
|
||||
|
||||
const newHistoryId = historyId === '' ? new Types.ObjectId() : undefined;
|
||||
if (stream && newHistoryId) {
|
||||
res.setHeader('newHistoryId', String(newHistoryId));
|
||||
const newChatId = chatId === '' ? new Types.ObjectId() : undefined;
|
||||
if (stream && newChatId) {
|
||||
res.setHeader('newChatId', String(newChatId));
|
||||
}
|
||||
|
||||
billId = await createTaskBill({
|
||||
@@ -133,10 +126,10 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
|
||||
});
|
||||
|
||||
// save chat
|
||||
if (typeof historyId === 'string') {
|
||||
if (typeof chatId === 'string') {
|
||||
await saveChat({
|
||||
historyId,
|
||||
newHistoryId,
|
||||
chatId,
|
||||
newChatId,
|
||||
appId,
|
||||
variables,
|
||||
prompts: [
|
||||
@@ -173,10 +166,10 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
|
||||
} else {
|
||||
res.json({
|
||||
data: {
|
||||
newHistoryId,
|
||||
newChatId,
|
||||
...responseData
|
||||
},
|
||||
id: historyId || '',
|
||||
id: chatId || '',
|
||||
model: '',
|
||||
usage: { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0 },
|
||||
choices: [
|
||||
|
@@ -7,7 +7,7 @@ import { Types } from 'mongoose';
|
||||
import type { ChatItemType } from '@/types/chat';
|
||||
|
||||
export type Props = {
|
||||
historyId?: string;
|
||||
chatId?: string;
|
||||
limit?: number;
|
||||
};
|
||||
export type Response = { history: ChatItemType[] };
|
||||
@@ -16,11 +16,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
try {
|
||||
await connectToDatabase();
|
||||
const { userId } = await authUser({ req });
|
||||
const { historyId, limit } = req.body as Props;
|
||||
const { chatId, limit } = req.body as Props;
|
||||
|
||||
jsonRes<Response>(res, {
|
||||
data: await getChatHistory({
|
||||
historyId,
|
||||
chatId,
|
||||
userId,
|
||||
limit
|
||||
})
|
||||
@@ -34,16 +34,16 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
}
|
||||
|
||||
export async function getChatHistory({
|
||||
historyId,
|
||||
chatId,
|
||||
userId,
|
||||
limit = 50
|
||||
}: Props & { userId: string }): Promise<Response> {
|
||||
if (!historyId) {
|
||||
if (!chatId) {
|
||||
return { history: [] };
|
||||
}
|
||||
|
||||
const history = await Chat.aggregate([
|
||||
{ $match: { _id: new Types.ObjectId(historyId), userId: new Types.ObjectId(userId) } },
|
||||
{ $match: { _id: new Types.ObjectId(chatId), userId: new Types.ObjectId(userId) } },
|
||||
{
|
||||
$project: {
|
||||
content: {
|
||||
|
Reference in New Issue
Block a user