mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-23 05:12:39 +00:00

* readme * Add queue log * Test interactive (#4509) * Support nested node interaction (#4503) * feat: Add a new InteractiveContext type and update InteractiveBasicType, adding an optional context property to support more complex interaction state management. * feat: Enhance workflow interactivity by adding InteractiveContext support and updating dispatch logic to manage nested contexts and entry nodes more effectively. * feat: Refactor dispatchWorkFlow to utilize InteractiveContext for improved context management * feat: Enhance entry node resolution by adding validation for entryNodeIds and recursive search in InteractiveContext * feat: Remove workflowDepth from InteractiveContext and update recovery logic to utilize parentContext for improved context management * feat: Update getWorkflowEntryNodeIds to use lastInteractive for improved context handling in runtime nodes * feat: Add lastInteractive support to enhance context management across workflow components * feat: Enhance interactive workflow by adding stopForInteractive flag and improving memory edge validation in runtime logic * feat: Refactor InteractiveContext by removing interactiveAppId and updating runtime edge handling in dispatchRunApp for improved context management * feat: Simplify runtime node and edge initialization in dispatchRunApp by using ternary operators for improved readability and maintainability * feat: Improve memory edge validation in initWorkflowEdgeStatus by adding detailed comments for better understanding of subset checks and recursive context searching * feat: Remove commented-out current level information from InteractiveContext for cleaner code and improved readability * feat: Simplify stopForInteractive check in dispatchWorkFlow for improved code clarity and maintainability * feat: Remove stopForInteractive handling and related references for improved code clarity and maintainability * feat: Add interactive response handling in dispatchRunAppNode for enhanced workflow interactivity * feat: Add context property to InteractiveBasicType and InteractiveNodeType for improved interactivity management * feat: remove comments * feat: Remove the node property from ChatDispatchProps to simplify type definitions * feat: Remove workflowInteractiveResponse from dispatchRunAppNode for cleaner code * feat: Refactor interactive value handling in chat history processing for improved clarity * feat: Simplify initWorkflowEdgeStatus logic for better readability and maintainability * feat: Add workflowInteractiveResponse to dispatchWorkFlow for enhanced functionality * feat: Enhance interactive response handling with nested children support * feat: Remove commented-out code for interactive node handling to improve clarity * feat: remove InteractiveContext type * feat: Refactor UserSelectInteractive and UserInputInteractive params for improved structure and clarity * feat: remove * feat: The front end supports extracting the deepest interaction parameters to enhance interaction processing * feat: The front end supports extracting the deepest interaction parameters to enhance interaction processing * fix: handle undefined interactive values in runtimeEdges and runtimeNodes initialization * fix: handle undefined interactive values in runtimeNodes and runtimeEdges initialization * fix: update runtimeNodes and runtimeEdges initialization to use last interactive value * fix: remove unused imports and replace getLastInteractiveValue with lastInteractive in runtimeEdges initialization * fix: import WorkflowInteractiveResponseType and handle lastInteractive as undefined in chatTest * feat: implement extractDeepestInteractive function and refactor usage in AIResponseBox and ChatBox utils * fix: refactor initWorkflowEdgeStatus and getWorkflowEntryNodeIds calls in dispatchRunAppNode for recovery handling * fix: ensure lastInteractive is handled consistently as undefined in runtimeEdges and runtimeNodes initialization * fix: update dispatchFormInput and dispatchUserSelect to use lastInteractive consistently * fix: update condition checks in dispatchFormInput and dispatchUserSelect to ensure lastInteractive type is validated correctly * fix: refactor dispatchRunAppNode to replace isRecovery with childrenInteractive for improved clarity in runtimeNodes and runtimeEdges initialization * refactor: streamline runtimeNodes and runtimeEdges initialization in dispatchRunAppNode for improved readability and maintainability * fix: update rewriteNodeOutputByHistories function to accept runtimeNodes and interactive as parameters for improved clarity * fix: simplify interactiveResponse assignment in dispatchWorkFlow for improved clarity * fix: update entryNodeIds check in getWorkflowEntryNodeIds to ensure it's an array for improved reliability * remove some invalid code --------- Co-authored-by: Theresa <63280168+sd0ric4@users.noreply.github.com> * update doc * update log * fix: update debug workflow to conditionally include nextStepSkipNodes… (#4511) * fix: update debug workflow to conditionally include nextStepSkipNodes based on lastInteractive for improved debugging accuracy * fix : type error * remove invalid code * fix: QA queue * fix: interactive * Test log (#4519) * add log (#4504) * add log * update log i18n * update log * delete template * add i18NT * add team operation log --------- Co-authored-by: gggaaallleee <91131304+gggaaallleee@users.noreply.github.com> * remove search * update doc --------- Co-authored-by: Theresa <63280168+sd0ric4@users.noreply.github.com> Co-authored-by: gggaaallleee <91131304+gggaaallleee@users.noreply.github.com>
268 lines
7.0 KiB
TypeScript
268 lines
7.0 KiB
TypeScript
import type { AIChatItemType, UserChatItemType } from '@fastgpt/global/core/chat/type.d';
|
|
import { MongoApp } from '../app/schema';
|
|
import {
|
|
ChatItemValueTypeEnum,
|
|
ChatRoleEnum,
|
|
ChatSourceEnum
|
|
} from '@fastgpt/global/core/chat/constants';
|
|
import { MongoChatItem } from './chatItemSchema';
|
|
import { MongoChat } from './chatSchema';
|
|
import { addLog } from '../../common/system/log';
|
|
import { mongoSessionRun } from '../../common/mongo/sessionRun';
|
|
import { StoreNodeItemType } from '@fastgpt/global/core/workflow/type/node';
|
|
import { getAppChatConfig, getGuideModule } from '@fastgpt/global/core/workflow/utils';
|
|
import { AppChatConfigType } from '@fastgpt/global/core/app/type';
|
|
import { mergeChatResponseData } from '@fastgpt/global/core/chat/utils';
|
|
import { pushChatLog } from './pushChatLog';
|
|
import { FlowNodeTypeEnum } from '@fastgpt/global/core/workflow/node/constant';
|
|
import { DispatchNodeResponseKeyEnum } from '@fastgpt/global/core/workflow/runtime/constants';
|
|
import { extractDeepestInteractive } from '@fastgpt/global/core/workflow/runtime/utils';
|
|
|
|
type Props = {
|
|
chatId: string;
|
|
appId: string;
|
|
teamId: string;
|
|
tmbId: string;
|
|
nodes: StoreNodeItemType[];
|
|
appChatConfig?: AppChatConfigType;
|
|
variables?: Record<string, any>;
|
|
isUpdateUseTime: boolean;
|
|
newTitle: string;
|
|
source: `${ChatSourceEnum}`;
|
|
sourceName?: string;
|
|
shareId?: string;
|
|
outLinkUid?: string;
|
|
content: [UserChatItemType & { dataId?: string }, AIChatItemType & { dataId?: string }];
|
|
metadata?: Record<string, any>;
|
|
};
|
|
|
|
export async function saveChat({
|
|
chatId,
|
|
appId,
|
|
teamId,
|
|
tmbId,
|
|
nodes,
|
|
appChatConfig,
|
|
variables,
|
|
isUpdateUseTime,
|
|
newTitle,
|
|
source,
|
|
sourceName,
|
|
shareId,
|
|
outLinkUid,
|
|
content,
|
|
metadata = {}
|
|
}: Props) {
|
|
try {
|
|
const chat = await MongoChat.findOne(
|
|
{
|
|
appId,
|
|
chatId
|
|
},
|
|
'_id metadata'
|
|
);
|
|
|
|
const metadataUpdate = {
|
|
...chat?.metadata,
|
|
...metadata
|
|
};
|
|
const { welcomeText, variables: variableList } = getAppChatConfig({
|
|
chatConfig: appChatConfig,
|
|
systemConfigNode: getGuideModule(nodes),
|
|
isPublicFetch: false
|
|
});
|
|
const pluginInputs = nodes?.find(
|
|
(node) => node.flowNodeType === FlowNodeTypeEnum.pluginInput
|
|
)?.inputs;
|
|
|
|
// Format save chat content: Remove quote q/a
|
|
const processedContent = content.map((item) => {
|
|
if (item.obj === ChatRoleEnum.AI) {
|
|
const nodeResponse = item[DispatchNodeResponseKeyEnum.nodeResponse];
|
|
|
|
if (nodeResponse) {
|
|
return {
|
|
...item,
|
|
[DispatchNodeResponseKeyEnum.nodeResponse]: nodeResponse.map((responseItem) => {
|
|
if (
|
|
responseItem.moduleType === FlowNodeTypeEnum.datasetSearchNode &&
|
|
responseItem.quoteList
|
|
) {
|
|
return {
|
|
...responseItem,
|
|
quoteList: responseItem.quoteList.map((quote: any) => ({
|
|
id: quote.id,
|
|
chunkIndex: quote.chunkIndex,
|
|
datasetId: quote.datasetId,
|
|
collectionId: quote.collectionId,
|
|
sourceId: quote.sourceId,
|
|
sourceName: quote.sourceName,
|
|
score: quote.score,
|
|
tokens: quote.tokens
|
|
}))
|
|
};
|
|
}
|
|
return responseItem;
|
|
})
|
|
};
|
|
}
|
|
}
|
|
return item;
|
|
});
|
|
|
|
await mongoSessionRun(async (session) => {
|
|
const [{ _id: chatItemIdHuman }, { _id: chatItemIdAi }] = await MongoChatItem.insertMany(
|
|
processedContent.map((item) => ({
|
|
chatId,
|
|
teamId,
|
|
tmbId,
|
|
appId,
|
|
...item
|
|
})),
|
|
{ session }
|
|
);
|
|
|
|
await MongoChat.updateOne(
|
|
{
|
|
appId,
|
|
chatId
|
|
},
|
|
{
|
|
$set: {
|
|
teamId,
|
|
tmbId,
|
|
appId,
|
|
chatId,
|
|
variableList,
|
|
welcomeText,
|
|
variables: variables || {},
|
|
pluginInputs,
|
|
title: newTitle,
|
|
source,
|
|
sourceName,
|
|
shareId,
|
|
outLinkUid,
|
|
metadata: metadataUpdate,
|
|
updateTime: new Date()
|
|
}
|
|
},
|
|
{
|
|
session,
|
|
upsert: true
|
|
}
|
|
);
|
|
|
|
pushChatLog({
|
|
chatId,
|
|
chatItemIdHuman: String(chatItemIdHuman),
|
|
chatItemIdAi: String(chatItemIdAi),
|
|
appId
|
|
});
|
|
});
|
|
|
|
if (isUpdateUseTime) {
|
|
await MongoApp.findByIdAndUpdate(appId, {
|
|
updateTime: new Date()
|
|
});
|
|
}
|
|
} catch (error) {
|
|
addLog.error(`update chat history error`, error);
|
|
}
|
|
}
|
|
|
|
export const updateInteractiveChat = async ({
|
|
chatId,
|
|
appId,
|
|
userInteractiveVal,
|
|
aiResponse,
|
|
newVariables
|
|
}: {
|
|
chatId: string;
|
|
appId: string;
|
|
userInteractiveVal: string;
|
|
aiResponse: AIChatItemType & { dataId?: string };
|
|
newVariables?: Record<string, any>;
|
|
}) => {
|
|
if (!chatId) return;
|
|
|
|
const chatItem = await MongoChatItem.findOne({ appId, chatId, obj: ChatRoleEnum.AI }).sort({
|
|
_id: -1
|
|
});
|
|
|
|
if (!chatItem || chatItem.obj !== ChatRoleEnum.AI) return;
|
|
|
|
// Update interactive value
|
|
const interactiveValue = chatItem.value[chatItem.value.length - 1];
|
|
|
|
if (
|
|
!interactiveValue ||
|
|
interactiveValue.type !== ChatItemValueTypeEnum.interactive ||
|
|
!interactiveValue.interactive?.params
|
|
) {
|
|
return;
|
|
}
|
|
|
|
const parsedUserInteractiveVal = (() => {
|
|
try {
|
|
return JSON.parse(userInteractiveVal);
|
|
} catch (err) {
|
|
return userInteractiveVal;
|
|
}
|
|
})();
|
|
|
|
let finalInteractive = extractDeepestInteractive(interactiveValue.interactive);
|
|
|
|
if (finalInteractive.type === 'userSelect') {
|
|
finalInteractive.params.userSelectedVal = userInteractiveVal;
|
|
} else if (
|
|
finalInteractive.type === 'userInput' &&
|
|
typeof parsedUserInteractiveVal === 'object'
|
|
) {
|
|
finalInteractive.params.inputForm = finalInteractive.params.inputForm.map((item) => {
|
|
const itemValue = parsedUserInteractiveVal[item.label];
|
|
return itemValue !== undefined
|
|
? {
|
|
...item,
|
|
value: itemValue
|
|
}
|
|
: item;
|
|
});
|
|
finalInteractive.params.submitted = true;
|
|
}
|
|
|
|
if (aiResponse.customFeedbacks) {
|
|
chatItem.customFeedbacks = chatItem.customFeedbacks
|
|
? [...chatItem.customFeedbacks, ...aiResponse.customFeedbacks]
|
|
: aiResponse.customFeedbacks;
|
|
}
|
|
|
|
if (aiResponse.responseData) {
|
|
chatItem.responseData = chatItem.responseData
|
|
? mergeChatResponseData([...chatItem.responseData, ...aiResponse.responseData])
|
|
: aiResponse.responseData;
|
|
}
|
|
|
|
if (aiResponse.value) {
|
|
chatItem.value = chatItem.value ? [...chatItem.value, ...aiResponse.value] : aiResponse.value;
|
|
}
|
|
|
|
await mongoSessionRun(async (session) => {
|
|
await chatItem.save({ session });
|
|
await MongoChat.updateOne(
|
|
{
|
|
appId,
|
|
chatId
|
|
},
|
|
{
|
|
$set: {
|
|
variables: newVariables,
|
|
updateTime: new Date()
|
|
}
|
|
},
|
|
{
|
|
session
|
|
}
|
|
);
|
|
});
|
|
};
|