fix: dataset selector load error (#4243)

* fix: dataset selector load error

* fix: path auth error

* fix: plugin scroll

* export chat log with contact (#4211)

* export chat log with contact

* fix

---------

Co-authored-by: heheer <heheer@sealos.io>
This commit is contained in:
Archer
2025-03-20 00:17:10 +08:00
committed by GitHub
parent 70563d2bcb
commit f9cecfd49a
22 changed files with 160 additions and 114 deletions

View File

@@ -18,6 +18,10 @@ import { MongoTeamMember } from '@fastgpt/service/support/user/team/teamMemberSc
import { ChatItemValueTypeEnum, ChatSourceEnum } from '@fastgpt/global/core/chat/constants';
import { AIChatItemValueItemType } from '@fastgpt/global/core/chat/type';
const formatJsonString = (data: any) => {
return JSON.stringify(data).replace(/"/g, '""').replace(/\n/g, '\\n');
};
export type ExportChatLogsBody = GetAppChatLogsProps & {
title: string;
sourcesMap: Record<string, { label: string }>;
@@ -40,7 +44,30 @@ async function handler(req: ApiRequestProps<ExportChatLogsBody, {}>, res: NextAp
}
const { teamId } = await authApp({ req, authToken: true, appId, per: WritePermissionVal });
const teamMembers = await MongoTeamMember.find({ teamId });
const teamMemberWithContact = await MongoTeamMember.aggregate([
{ $match: { teamId: new Types.ObjectId(teamId) } },
{
$lookup: {
from: 'users',
localField: 'userId',
foreignField: '_id',
as: 'user'
}
},
{
$project: {
memberId: '$_id',
teamId: 1,
userId: 1,
name: 1,
role: 1,
status: 1,
contact: { $ifNull: [{ $arrayElemAt: ['$user.contact', 0] }, '-'] }
}
}
]);
console.log(teamMemberWithContact);
const where = {
teamId: new Types.ObjectId(teamId),
@@ -104,40 +131,32 @@ async function handler(req: ApiRequestProps<ExportChatLogsBody, {}>, res: NextAp
},
{
$addFields: {
userGoodFeedbackCount: {
$size: {
$filter: {
input: '$chatitems',
as: 'item',
cond: { $ifNull: ['$$item.userGoodFeedback', false] }
}
userGoodFeedbackItems: {
$filter: {
input: '$chatitems',
as: 'item',
cond: { $ifNull: ['$$item.userGoodFeedback', false] }
}
},
userBadFeedbackCount: {
$size: {
$filter: {
input: '$chatitems',
as: 'item',
cond: { $ifNull: ['$$item.userBadFeedback', false] }
}
userBadFeedbackItems: {
$filter: {
input: '$chatitems',
as: 'item',
cond: { $ifNull: ['$$item.userBadFeedback', false] }
}
},
customFeedbacksCount: {
$size: {
$filter: {
input: '$chatitems',
as: 'item',
cond: { $gt: [{ $size: { $ifNull: ['$$item.customFeedbacks', []] } }, 0] }
}
customFeedbackItems: {
$filter: {
input: '$chatitems',
as: 'item',
cond: { $gt: [{ $size: { $ifNull: ['$$item.customFeedbacks', []] } }, 0] }
}
},
markCount: {
$size: {
$filter: {
input: '$chatitems',
as: 'item',
cond: { $ifNull: ['$$item.adminFeedback', false] }
}
markItems: {
$filter: {
input: '$chatitems',
as: 'item',
cond: { $ifNull: ['$$item.adminFeedback', false] }
}
},
chatDetails: {
@@ -161,10 +180,10 @@ async function handler(req: ApiRequestProps<ExportChatLogsBody, {}>, res: NextAp
source: 1,
time: '$updateTime',
messageCount: { $size: '$chatitems' },
userGoodFeedbackCount: 1,
userBadFeedbackCount: 1,
customFeedbacksCount: 1,
markCount: 1,
userGoodFeedbackItems: 1,
userBadFeedbackItems: 1,
customFeedbackItems: 1,
markItems: 1,
outLinkUid: 1,
tmbId: 1,
chatDetails: 1
@@ -187,14 +206,18 @@ async function handler(req: ApiRequestProps<ExportChatLogsBody, {}>, res: NextAp
const time = dayjs(doc.time.toISOString()).format('YYYY-MM-DD HH:mm:ss');
const source = sourcesMap[doc.source as ChatSourceEnum]?.label || doc.source;
const title = doc.customTitle || doc.title;
const tmb = doc.outLinkUid
const tmbName = doc.outLinkUid
? doc.outLinkUid
: teamMembers.find((member) => String(member._id) === String(doc.tmbId))?.name;
: teamMemberWithContact.find((member) => String(member.memberId) === String(doc.tmbId))?.name;
const tmbContact = teamMemberWithContact.find(
(member) => String(member.memberId) === String(doc.tmbId)
)?.contact;
const messageCount = doc.messageCount;
const userFeedbackCount = doc.userGoodFeedbackCount || doc.userBadFeedbackCount || '-';
const customFeedbacksCount = doc.customFeedbacksCount || '-';
const markCount = doc.markCount;
const userGoodFeedbackItems = doc.userGoodFeedbackItems || [];
const userBadFeedbackItems = doc.userBadFeedbackItems || [];
const customFeedbackItems = doc.customFeedbackItems || [];
const markItems = doc.markItems || [];
const chatDetails = doc.chatDetails.map(
(chat: { id: string; value: AIChatItemValueItemType[] }) => {
return chat.value.map((item) => {
@@ -228,9 +251,14 @@ async function handler(req: ApiRequestProps<ExportChatLogsBody, {}>, res: NextAp
});
}
);
let chatDetailsStr = JSON.stringify(chatDetails).replace(/"/g, '""').replace(/\n/g, '\\n');
const res = `\n"${time}","${source}","${tmb}","${title}","${messageCount}","${userFeedbackCount}","${customFeedbacksCount}","${markCount}","${chatDetailsStr}"`;
const userGoodFeedbackItemsStr = formatJsonString(userGoodFeedbackItems);
const userBadFeedbackItemsStr = formatJsonString(userBadFeedbackItems);
const customFeedbackItemsStr = formatJsonString(customFeedbackItems);
const markItemsStr = formatJsonString(markItems);
const chatDetailsStr = formatJsonString(chatDetails);
const res = `\n"${time}","${source}","${tmbName}","${tmbContact}","${title}","${messageCount}","${userGoodFeedbackItemsStr}","${userBadFeedbackItemsStr}","${customFeedbackItemsStr}","${markItemsStr}","${chatDetailsStr}"`;
write(res);
});

View File

@@ -1,5 +1,6 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import type {
GetPathProps,
ParentIdType,
ParentTreePathItemType
} from '@fastgpt/global/common/parentFolder/type.d';
@@ -12,15 +13,15 @@ async function handler(
req: NextApiRequest,
res: NextApiResponse<any>
): Promise<ParentTreePathItemType[]> {
const { parentId } = req.query as { parentId: string };
const { sourceId: appId, type } = req.query as GetPathProps;
if (!parentId) {
if (!appId) {
return [];
}
await authApp({ req, authToken: true, appId: parentId, per: ReadPermissionVal });
const { app } = await authApp({ req, authToken: true, appId, per: ReadPermissionVal });
return await getParents(parentId);
return await getParents(type === 'current' ? appId : app.parentId);
}
export default NextAPI(handler);

View File

@@ -1,11 +1,9 @@
import type { ApiRequestProps, ApiResponseType } from '@fastgpt/service/type/next';
import { NextAPI } from '@/service/middleware/entry';
import { ParentIdType, ParentTreePathItemType } from '@fastgpt/global/common/parentFolder/type';
import { GetPathProps, ParentTreePathItemType } from '@fastgpt/global/common/parentFolder/type';
import { getSystemPlugins } from '@/service/core/app/plugin';
export type pathQuery = {
parentId: ParentIdType;
};
export type pathQuery = GetPathProps;
export type pathBody = {};
@@ -15,18 +13,18 @@ async function handler(
req: ApiRequestProps<pathBody, pathQuery>,
res: ApiResponseType<any>
): Promise<pathResponse> {
const { parentId } = req.query;
const { sourceId: pluginId, type } = req.query;
if (!parentId) return [];
if (!pluginId) return [];
const plugins = await getSystemPlugins();
const plugin = plugins.find((item) => item.id === parentId);
const plugin = plugins.find((item) => item.id === pluginId);
if (!plugin) return [];
return [
{
parentId: plugin.id,
parentId: type === 'current' ? plugin.id : plugin.parentId,
parentName: plugin.name
}
];

View File

@@ -1,20 +1,28 @@
import type { NextApiRequest } from 'next';
import { MongoDataset } from '@fastgpt/service/core/dataset/schema';
import type { ParentTreePathItemType } from '@fastgpt/global/common/parentFolder/type.d';
import type {
GetPathProps,
ParentTreePathItemType
} from '@fastgpt/global/common/parentFolder/type.d';
import { authDataset } from '@fastgpt/service/support/permission/dataset/auth';
import { ReadPermissionVal } from '@fastgpt/global/support/permission/constant';
import { NextAPI } from '@/service/middleware/entry';
async function handler(req: NextApiRequest) {
const { parentId } = req.query as { parentId: string };
const { sourceId: datasetId, type } = req.query as GetPathProps;
if (!parentId) {
if (!datasetId) {
return [];
}
await authDataset({ req, authToken: true, datasetId: parentId, per: ReadPermissionVal });
const { dataset } = await authDataset({
req,
authToken: true,
datasetId,
per: ReadPermissionVal
});
return await getParents(parentId);
return await getParents(type === 'current' ? dataset._id : dataset.parentId);
}
async function getParents(parentId?: string): Promise<ParentTreePathItemType[]> {

View File

@@ -97,10 +97,10 @@ function DatasetContextProvider({ children }: { children: React.ReactNode }) {
);
const { data: paths = [], runAsync: refetchPaths } = useRequest2(
() => getDatasetPaths(parentId),
async () => getDatasetPaths({ sourceId: parentId, type: 'current' }),
{
manual: false,
refreshDeps: [parentId]
refreshDeps: [folderDetail]
}
);