feat: update interactive can push items

This commit is contained in:
archer
2025-09-15 10:50:11 +08:00
parent 04b7153c5e
commit 23aae0a287
4 changed files with 69 additions and 50 deletions

View File

@@ -17,6 +17,7 @@ import { MongoAppChatLog } from '../app/logs/chatLogsSchema';
import { writePrimary } from '../../common/mongo/utils';
import { MongoChatItemResponse } from './chatItemResponseSchema';
import { chatValue2RuntimePrompt } from '@fastgpt/global/core/chat/adapt';
import { ConfirmPlanAgentText } from '@fastgpt/global/core/workflow/runtime/constants';
type Props = {
chatId: string;
@@ -105,7 +106,7 @@ const getChatDataLog = async ({
};
};
export async function saveChat(props: Props) {
export const saveChat = async (props: Props) => {
const {
chatId,
appId,
@@ -289,20 +290,30 @@ export async function saveChat(props: Props) {
).catch();
}
} catch (error) {
addLog.error(`update chat history error`, error);
addLog.error(`Save chat history error`, error);
}
}
};
/*
更新交互节点,包含两种情况:
1. 更新当前的 items并把 value 追加到当前 items
2. 新增 items, 次数只需要改当前的 items 里的交互节点值即可,其他属性追加在新增的 items 里
*/
export const updateInteractiveChat = async (props: Props) => {
const {
chatId,
appId,
userContent,
aiContent,
variables,
durationSeconds,
errorMsg,
teamId,
tmbId,
newTitle,
metadata
} = props;
export const updateInteractiveChat = async ({
teamId,
chatId,
appId,
userContent,
aiContent,
variables,
durationSeconds,
errorMsg
}: Props) => {
if (!chatId) return;
const chatItem = await MongoChatItem.findOne({ appId, chatId, obj: ChatRoleEnum.AI }).sort({
@@ -319,7 +330,8 @@ export const updateInteractiveChat = async ({
}
interactiveValue.interactive.params = interactiveValue.interactive.params || {};
// Update interactive value
// Get interactive value
const { text: userInteractiveVal } = chatValue2RuntimePrompt(userContent.value);
const parsedUserInteractiveVal = (() => {
const { text: userInteractiveVal } = chatValue2RuntimePrompt(userContent.value);
try {
@@ -334,10 +346,18 @@ export const updateInteractiveChat = async ({
errorMsg
});
let finalInteractive = extractDeepestInteractive(interactiveValue.interactive);
// 拿到的是实参
const finalInteractive = extractDeepestInteractive(interactiveValue.interactive);
const pushNewItems =
finalInteractive.type === 'agentPlanAskQuery' ||
(finalInteractive.type === 'agentPlanCheck' && userInteractiveVal !== ConfirmPlanAgentText);
if (finalInteractive.type === 'userSelect') {
finalInteractive.params.userSelectedVal = parsedUserInteractiveVal;
// Update interactive value
if (
finalInteractive.type === 'userSelect' ||
finalInteractive.type === 'agentPlanAskUserSelect'
) {
finalInteractive.params.userSelectedVal = userInteractiveVal;
} else if (
(finalInteractive.type === 'userInput' || finalInteractive.type === 'agentPlanAskUserForm') &&
typeof parsedUserInteractiveVal === 'object'
@@ -356,23 +376,27 @@ export const updateInteractiveChat = async ({
finalInteractive.params.confirmed = true;
}
if (aiResponse.customFeedbacks) {
chatItem.customFeedbacks = chatItem.customFeedbacks
? [...chatItem.customFeedbacks, ...aiResponse.customFeedbacks]
: aiResponse.customFeedbacks;
}
if (aiResponse.value) {
chatItem.value = chatItem.value ? [...chatItem.value, ...aiResponse.value] : aiResponse.value;
}
if (aiResponse.citeCollectionIds) {
chatItem.citeCollectionIds = chatItem.citeCollectionIds
? [...chatItem.citeCollectionIds, ...aiResponse.citeCollectionIds]
: aiResponse.citeCollectionIds;
}
// Update current items
if (!pushNewItems) {
if (aiContent.customFeedbacks) {
chatItem.customFeedbacks = chatItem.customFeedbacks
? [...chatItem.customFeedbacks, ...aiContent.customFeedbacks]
: aiContent.customFeedbacks;
}
if (aiContent.value) {
chatItem.value = chatItem.value ? [...chatItem.value, ...aiContent.value] : aiContent.value;
}
if (aiResponse.citeCollectionIds) {
chatItem.citeCollectionIds = chatItem.citeCollectionIds
? [...chatItem.citeCollectionIds, ...aiResponse.citeCollectionIds]
: aiResponse.citeCollectionIds;
}
chatItem.durationSeconds = chatItem.durationSeconds
? +(chatItem.durationSeconds + durationSeconds).toFixed(2)
: durationSeconds;
chatItem.durationSeconds = chatItem.durationSeconds
? +(chatItem.durationSeconds + durationSeconds).toFixed(2)
: durationSeconds;
}
chatItem.markModified('value');
await mongoSessionRun(async (session) => {
await chatItem.save({ session });

View File

@@ -233,10 +233,10 @@ export const dispatchRunAgent = async (props: DispatchAgentModuleProps): Promise
]
},
// Mock返回 plan check
// [DispatchNodeResponseKeyEnum.interactive]: {
// type: 'agentPlanCheck',
// params: {}
// }
[DispatchNodeResponseKeyEnum.interactive]: {
type: 'agentPlanCheck',
params: {}
}
// Mock: 返回 plan user select
// [DispatchNodeResponseKeyEnum.interactive]: {
@@ -273,12 +273,12 @@ export const dispatchRunAgent = async (props: DispatchAgentModuleProps): Promise
// }
// }
// Mock: 返回 plan user query
[DispatchNodeResponseKeyEnum.interactive]: {
type: 'agentPlanAskQuery',
params: {
content: '请提供 xxxxx'
}
}
// [DispatchNodeResponseKeyEnum.interactive]: {
// type: 'agentPlanAskQuery',
// params: {
// content: '请提供 xxxxx'
// }
// }
};
}

View File

@@ -15,12 +15,8 @@ import { useCopyData } from '@fastgpt/web/hooks/useCopyData';
import MyIcon from '@fastgpt/web/components/common/Icon';
import MyTooltip from '@fastgpt/web/components/common/MyTooltip';
import { useTranslation } from 'next-i18next';
import type {
AIChatItemType,
UserChatItemValueItemType} from '@fastgpt/global/core/chat/type';
import {
type AIChatItemValueItemType
} from '@fastgpt/global/core/chat/type';
import type { AIChatItemType, UserChatItemValueItemType } from '@fastgpt/global/core/chat/type';
import { type AIChatItemValueItemType } from '@fastgpt/global/core/chat/type';
import { CodeClassNameEnum } from '@/components/Markdown/utils';
import { isEqual } from 'lodash';
import { useSystem } from '@fastgpt/web/hooks/useSystem';

View File

@@ -206,7 +206,6 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
// save chat
const isInteractiveRequest = !!getLastInteractiveValue(histories);
const { text: userInteractiveVal } = chatValue2RuntimePrompt(userQuestion.value);
const newTitle = isPlugin
? variables.cTime ?? formatTime2YMDHM()