Add share link hook (#351)

This commit is contained in:
Archer
2023-09-25 23:12:42 +08:00
committed by GitHub
parent 9136c9306a
commit 63cd379682
29 changed files with 430 additions and 98 deletions

View File

@@ -40,7 +40,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
await connectToDatabase();
/* user auth */
const { userId, user } = await authUser({ req, authBalance: true });
const { userId, user } = await authUser({ req, authToken: true, authBalance: true });
if (!user) {
throw new Error('user not found');

View File

@@ -9,7 +9,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
try {
const { name } = req.body as CreateTrainingBillType;
const { userId } = await authUser({ req, authToken: true });
const { userId } = await authUser({ req, authToken: true, authApiKey: true });
await connectToDatabase();

View File

@@ -16,7 +16,7 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
}
// 凭证校验
const { userId } = await authUser({ req });
const { userId } = await authUser({ req, authToken: true });
await PgClient.delete(PgDatasetTableName, {
where: [['user_id', userId], 'AND', ['id', dataId]]

View File

@@ -21,7 +21,7 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
await connectToDatabase();
// 凭证校验
const { userId } = await authUser({ req });
const { userId } = await authUser({ req, authToken: true });
jsonRes(res, {
data: await getVectorAndInsertDataset({

View File

@@ -36,7 +36,7 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
await connectToDatabase();
// 凭证校验
const { userId } = await authUser({ req });
const { userId } = await authUser({ req, authToken: true, authApiKey: true });
jsonRes<PushDataResponse>(res, {
data: await pushDataToKb({

View File

@@ -20,7 +20,7 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
// auth user and get kb
const [{ userId }, kb] = await Promise.all([
authUser({ req }),
authUser({ req, authToken: true }),
KB.findById(kbId, 'vectorModel')
]);

View File

@@ -18,7 +18,7 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
// 凭证校验
const [{ userId }, kb] = await Promise.all([
authUser({ req }),
authUser({ req, authToken: true, authApiKey: true }),
KB.findById(kbId, 'vectorModel')
]);

View File

@@ -17,7 +17,7 @@ type Response = {
export default withNextCors(async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
try {
const { userId } = await authUser({ req });
const { userId } = await authUser({ req, authToken: true });
let { input, model } = req.query as Props;
if (!Array.isArray(input)) {

View File

@@ -34,7 +34,7 @@ import requestIp from 'request-ip';
import { replaceVariable } from '@/utils/common/tools/text';
import { ModuleDispatchProps } from '@/types/core/modules';
import { selectShareResponse } from '@/utils/service/core/chat';
import { updateOutLinkUsage } from '@/service/support/outLink';
import { pushResult2Remote, updateOutLinkUsage } from '@/service/support/outLink';
import { updateApiKeyUsage } from '@/service/support/openapi';
export type MessageItemType = ChatCompletionRequestMessage & { dataId?: string };
@@ -44,6 +44,7 @@ type FastGptWebChatProps = {
};
type FastGptShareChatProps = {
shareId?: string;
authToken?: string;
};
export type Props = CreateChatCompletionRequest &
FastGptWebChatProps &
@@ -71,6 +72,7 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
chatId,
appId,
shareId,
authToken,
stream = false,
detail = false,
messages = [],
@@ -111,10 +113,15 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
if (shareId) {
return authOutLinkChat({
shareId,
ip: requestIp.getClientIp(req)
ip: requestIp.getClientIp(req),
authToken,
question:
(messages[messages.length - 2]?.role === 'user'
? messages[messages.length - 2].content
: messages[messages.length - 1]?.content) || ''
});
}
return authUser({ req, authBalance: true });
return authUser({ req, authToken: true, authApiKey: true, authBalance: true });
})();
if (!user) {
@@ -260,11 +267,13 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
response: responseData
});
!!shareId &&
if (shareId) {
pushResult2Remote({ authToken, shareId, responseData });
updateOutLinkUsage({
shareId,
total
});
}
!!apikey &&
updateApiKeyUsage({
apikey,

View File

@@ -16,7 +16,7 @@ export type Response = { history: ChatItemType[] };
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
await connectToDatabase();
const { userId } = await authUser({ req });
const { userId } = await authUser({ req, authToken: true });
const { chatId, limit } = req.body as Props;
jsonRes<Response>(res, {

View File

@@ -18,7 +18,7 @@ const fetchContent = async (req: NextApiRequest, res: NextApiResponse) => {
throw new Error('urlList is empty');
}
await authUser({ req });
await authUser({ req, authToken: true });
urlList = urlList.filter((url) => /^(http|https):\/\/[^ "]+$/.test(url));

View File

@@ -14,7 +14,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
throw new Error('fileId is empty');
}
const { userId } = await authUser({ req });
const { userId } = await authUser({ req, authToken: true });
const gridFs = new GridFSStorage('dataset', userId);

View File

@@ -16,7 +16,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
throw new Error('fileId is empty');
}
const { userId } = await authUser({ req });
const { userId } = await authUser({ req, authToken: true });
// auth file
const gridFs = new GridFSStorage('dataset', userId);

View File

@@ -5,12 +5,14 @@ import type { InitShareChatResponse } from '@/api/response/chat';
import { authApp } from '@/service/utils/auth';
import { HUMAN_ICON } from '@/constants/chat';
import { getChatModelNameList, getSpecialModule } from '@/components/ChatBox/utils';
import { authShareChatInit } from '@/service/support/outLink/auth';
/* init share chat window */
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
let { shareId } = req.query as {
let { shareId, authToken } = req.query as {
shareId: string;
authToken?: string;
};
if (!shareId) {
@@ -36,7 +38,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
userId: String(shareChat.userId),
authOwner: false
}),
User.findById(shareChat.userId, 'avatar')
User.findById(shareChat.userId, 'avatar'),
authShareChatInit(authToken, shareChat.limit?.hookUrl)
]);
jsonRes<InitShareChatResponse>(res, {