Skip to content

Commit

Permalink
fix: share page data;Adapt findLastIndex api (labring#3147)
Browse files Browse the repository at this point in the history
* perf: share page data

* perf: adapt findLastIndex
  • Loading branch information
c121914yu authored Nov 13, 2024
1 parent 519b519 commit c3cc51c
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 56 deletions.
8 changes: 6 additions & 2 deletions packages/global/core/chat/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,15 @@ export const getHistoryPreview = (
};

export const filterPublicNodeResponseData = ({
flowResponses = []
flowResponses = [],
responseDetail = false
}: {
flowResponses?: ChatHistoryItemResType[];
responseDetail?: boolean;
}) => {
const filedList = ['quoteList', 'moduleType', 'pluginOutput', 'runningTime'];
const filedList = responseDetail
? ['quoteList', 'moduleType', 'pluginOutput', 'runningTime']
: ['moduleType', 'pluginOutput', 'runningTime'];
const filterModuleTypeList: any[] = [
FlowNodeTypeEnum.pluginModule,
FlowNodeTypeEnum.datasetSearchNode,
Expand Down
12 changes: 12 additions & 0 deletions packages/service/core/workflow/dispatch/dataset/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ export async function dispatchDatasetSearch(
}

if (!userChatInput) {
return {
quoteQA: [],
[DispatchNodeResponseKeyEnum.nodeResponse]: {
totalPoints: 0,
query: '',
limit,
searchMode
},
nodeDispatchUsages: [],
[DispatchNodeResponseKeyEnum.toolResponses]: []
};

return Promise.reject(i18nT('common:core.chat.error.User input empty'));
}

Expand Down
9 changes: 6 additions & 3 deletions packages/web/hooks/useWidthVariable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ export const useWidthVariable = <T = any>({
}) => {
const value = useMemo(() => {
// 根据 width 计算,找到第一个大于 width 的值
const index = widthList.findLastIndex((item) => width > item);
const reversedWidthList = [...widthList].reverse();
const reversedList = [...list].reverse();
const index = reversedWidthList.findIndex((item) => width > item);

if (index === -1) {
return list[0];
return reversedList[0];
}
return list[index];
return reversedList[index];
}, [list, width, widthList]);

return value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const ResponseTags = ({
const { t } = useTranslation();
const quoteListRef = React.useRef<HTMLDivElement>(null);
const dataId = historyItem.dataId;

const {
totalQuoteList: quoteList = [],
llmModuleAccount = 0,
Expand Down
9 changes: 7 additions & 2 deletions projects/app/src/global/core/chat/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,23 @@ import { FlowNodeTypeEnum } from '@fastgpt/global/core/workflow/node/constant';
const isLLMNode = (item: ChatHistoryItemResType) =>
item.moduleType === FlowNodeTypeEnum.chatNode || item.moduleType === FlowNodeTypeEnum.tools;

export function transformPreviewHistories(histories: ChatItemType[]): ChatItemType[] {
export function transformPreviewHistories(
histories: ChatItemType[],
responseDetail: boolean
): ChatItemType[] {
return histories.map((item) => {
return {
...addStatisticalDataToHistoryItem(item),
responseData: undefined
responseData: undefined,
...(responseDetail ? {} : { totalQuoteList: undefined })
};
});
}

export function addStatisticalDataToHistoryItem(historyItem: ChatItemType) {
if (historyItem.obj !== ChatRoleEnum.AI) return historyItem;
if (historyItem.totalQuoteList !== undefined) return historyItem;
if (!historyItem.responseData) return historyItem;

// Flat children
const flatResData: ChatHistoryItemResType[] =
Expand Down
9 changes: 7 additions & 2 deletions projects/app/src/pages/api/core/chat/getPaginationRecords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,16 @@ async function handler(
limit: pageSize
});

const responseDetail = !shareChat || shareChat.responseDetail;

// Remove important information
if (shareChat && app.type !== AppTypeEnum.plugin) {
histories.forEach((item) => {
if (item.obj === ChatRoleEnum.AI) {
item.responseData = filterPublicNodeResponseData({ flowResponses: item.responseData });
item.responseData = filterPublicNodeResponseData({
flowResponses: item.responseData,
responseDetail
});

if (shareChat.showNodeStatus === false) {
item.value = item.value.filter((v) => v.type !== ChatItemValueTypeEnum.tool);
Expand All @@ -96,7 +101,7 @@ async function handler(
}

return {
list: isPlugin ? histories : transformPreviewHistories(histories),
list: isPlugin ? histories : transformPreviewHistories(histories, responseDetail),
total
};
}
Expand Down
74 changes: 44 additions & 30 deletions projects/app/src/pages/api/core/chat/getResData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ChatHistoryItemResType } from '@fastgpt/global/core/chat/type';
import { OutLinkChatAuthProps } from '@fastgpt/global/support/permission/chat';
import { authApp } from '@fastgpt/service/support/permission/app/auth';
import { filterPublicNodeResponseData } from '@fastgpt/global/core/chat/utils';
import { MongoOutLink } from '@fastgpt/service/support/outLink/schema';

export type getResDataQuery = OutLinkChatAuthProps & {
chatId?: string;
Expand All @@ -26,44 +27,57 @@ async function handler(
req: ApiRequestProps<getResDataBody, getResDataQuery>,
res: ApiResponseType<any>
): Promise<getResDataResponse> {
const { appId, chatId, dataId } = req.query;
const { appId, chatId, dataId, shareId } = req.query;
if (!appId || !chatId || !dataId) {
return {};
}

// 1. Un login api: share chat, team chat
// 2. Login api: account chat, chat log
try {
await authChatCrud({
req,
authToken: true,
authApiKey: true,
...req.query,
per: ReadPermissionVal
});
} catch (error) {
await authApp({
req,
authToken: true,
authApiKey: true,
appId,
per: ManagePermissionVal
});
}
const authData = await (() => {
try {
return authChatCrud({
req,
authToken: true,
authApiKey: true,
...req.query,
per: ReadPermissionVal
});
} catch (error) {
return authApp({
req,
authToken: true,
authApiKey: true,
appId,
per: ManagePermissionVal
});
}
})();

const [chatData] = await Promise.all([
MongoChatItem.findOne(
{
appId,
chatId,
dataId
},
'obj responseData'
).lean(),
shareId ? MongoOutLink.findOne({ shareId }).lean() : Promise.resolve(null)
]);

const chatData = await MongoChatItem.findOne(
{
appId,
chatId,
dataId
},
'obj responseData'
).lean();
if (chatData?.obj !== ChatRoleEnum.AI) {
return {};
}

if (chatData?.obj === ChatRoleEnum.AI) {
const data = chatData.responseData || {};
return req.query.shareId ? filterPublicNodeResponseData(data) : data;
} else return {};
const flowResponses = chatData.responseData ?? {};
return req.query.shareId
? filterPublicNodeResponseData({
// @ts-ignore
responseDetail: authData.responseDetail,
flowResponses: chatData.responseData
})
: flowResponses;
}

export default NextAPI(handler);
12 changes: 5 additions & 7 deletions projects/app/src/pages/api/v1/chat/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
/* select fe response field */
const feResponseData = canWrite
? flowResponses
: filterPublicNodeResponseData({ flowResponses });
: filterPublicNodeResponseData({ flowResponses, responseDetail });

if (stream) {
workflowResponseWrite({
Expand All @@ -380,12 +380,10 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
});

if (detail) {
if (responseDetail || isPlugin) {
workflowResponseWrite({
event: SseResponseEventEnum.flowResponses,
data: feResponseData
});
}
workflowResponseWrite({
event: SseResponseEventEnum.flowResponses,
data: feResponseData
});
}

res.end();
Expand Down
22 changes: 12 additions & 10 deletions projects/app/src/service/support/permission/auth/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,19 @@ export async function authChatCrud({
chat?: ChatSchema;
isOutLink: boolean;
uid?: string;
responseDetail: boolean;
}> {
const isOutLink = Boolean((shareId || spaceTeamId) && outLinkUid);
if (!chatId) return { isOutLink, uid: outLinkUid };
if (!chatId) return { isOutLink, uid: outLinkUid, responseDetail: true };

const chat = await MongoChat.findOne({ appId, chatId }).lean();

const { uid } = await (async () => {
const { uid, responseDetail } = await (async () => {
// outLink Auth
if (shareId && outLinkUid) {
const { uid } = await authOutLink({ shareId, outLinkUid });
const { uid, shareChat } = await authOutLink({ shareId, outLinkUid });
if (!chat || (chat.shareId === shareId && chat.outLinkUid === uid)) {
return { uid };
return { uid, responseDetail: shareChat.responseDetail };
}
return Promise.reject(ChatErrEnum.unAuthChat);
}
Expand All @@ -62,12 +63,12 @@ export async function authChatCrud({
const { uid } = await authTeamSpaceToken({ teamId: spaceTeamId, teamToken });
addLog.debug('Auth team token', { uid, spaceTeamId, teamToken, chatUid: chat?.outLinkUid });
if (!chat || (String(chat.teamId) === String(spaceTeamId) && chat.outLinkUid === uid)) {
return { uid };
return { uid, responseDetail: true };
}
return Promise.reject(ChatErrEnum.unAuthChat);
}

if (!chat) return { id: outLinkUid };
if (!chat) return { id: outLinkUid, responseDetail: true };

// auth req
const { teamId, tmbId, permission } = await authApp({
Expand All @@ -80,18 +81,19 @@ export async function authChatCrud({

if (String(teamId) !== String(chat.teamId)) return Promise.reject(ChatErrEnum.unAuthChat);

if (permission.hasManagePer) return { uid: outLinkUid };
if (String(tmbId) === String(chat.tmbId)) return { uid: outLinkUid };
if (permission.hasManagePer) return { uid: outLinkUid, responseDetail: true };
if (String(tmbId) === String(chat.tmbId)) return { uid: outLinkUid, responseDetail: true };

return Promise.reject(ChatErrEnum.unAuthChat);
})();

if (!chat) return { isOutLink, uid };
if (!chat) return { isOutLink, uid, responseDetail };

return {
chat,
isOutLink,
uid
uid,
responseDetail
};
}

Expand Down

0 comments on commit c3cc51c

Please sign in to comment.