diff --git a/packages/global/core/app/utils.ts b/packages/global/core/app/utils.ts index 7ba2fa68491..679282528e0 100644 --- a/packages/global/core/app/utils.ts +++ b/packages/global/core/app/utils.ts @@ -105,7 +105,7 @@ export const appWorkflow2Form = ({ nodes }: { nodes: StoreNodeItemType[] }) => { } else if (node.flowNodeType === FlowNodeTypeEnum.systemConfig) { const { welcomeText, - variableModules, + variableNodes, questionGuide, ttsConfig, whisperConfig, @@ -114,7 +114,7 @@ export const appWorkflow2Form = ({ nodes }: { nodes: StoreNodeItemType[] }) => { defaultAppForm.userGuide = { welcomeText: welcomeText, - variables: variableModules, + variables: variableNodes, questionGuide: questionGuide, tts: ttsConfig, whisper: whisperConfig, diff --git a/packages/global/core/chat/type.d.ts b/packages/global/core/chat/type.d.ts index ffc71fa89ca..59b70683fbc 100644 --- a/packages/global/core/chat/type.d.ts +++ b/packages/global/core/chat/type.d.ts @@ -10,7 +10,7 @@ import { import { FlowNodeTypeEnum } from '../workflow/node/constant'; import { NodeOutputKeyEnum } from '../workflow/constants'; import { DispatchNodeResponseKeyEnum } from '../workflow/runtime/constants'; -import { AppSchema } from '../app/type'; +import { AppSchema, VariableItemType } from '../app/type'; import type { AppSchema as AppType } from '@fastgpt/global/core/app/type.d'; import { DatasetSearchModeEnum } from '../dataset/constants'; import { ChatBoxInputType } from '../../../../projects/app/src/components/ChatBox/type'; @@ -27,11 +27,13 @@ export type ChatSchema = { title: string; customTitle: string; top: boolean; - variables: Record; source: `${ChatSourceEnum}`; shareId?: string; outLinkUid?: string; - content: ChatItemType[]; + + variableList?: VariableItemType[]; + welcomeText?: string; + variables: Record; metadata?: Record; }; diff --git a/packages/global/core/workflow/utils.ts b/packages/global/core/workflow/utils.ts index d27d56a1a11..7a672d41f82 100644 --- a/packages/global/core/workflow/utils.ts +++ b/packages/global/core/workflow/utils.ts @@ -36,13 +36,17 @@ export const checkInputIsReference = (input: FlowNodeInputItemType) => { /* node */ export const getGuideModule = (modules: StoreNodeItemType[]) => - modules.find((item) => item.flowNodeType === FlowNodeTypeEnum.systemConfig); - + modules.find( + (item) => + item.flowNodeType === FlowNodeTypeEnum.systemConfig || + // @ts-ignore (adapt v1) + item.flowType === FlowNodeTypeEnum.systemConfig + ); export const splitGuideModule = (guideModules?: StoreNodeItemType) => { const welcomeText: string = guideModules?.inputs?.find((item) => item.key === NodeInputKeyEnum.welcomeText)?.value || ''; - const variableModules: VariableItemType[] = + const variableNodes: VariableItemType[] = guideModules?.inputs.find((item) => item.key === NodeInputKeyEnum.variables)?.value || []; const questionGuide: boolean = @@ -63,13 +67,43 @@ export const splitGuideModule = (guideModules?: StoreNodeItemType) => { return { welcomeText, - variableModules, + variableNodes, questionGuide, ttsConfig, whisperConfig, scheduledTriggerConfig }; }; +export const replaceAppChatConfig = ({ + node, + variableList, + welcomeText +}: { + node?: StoreNodeItemType; + variableList?: VariableItemType[]; + welcomeText?: string; +}): StoreNodeItemType | undefined => { + if (!node) return; + return { + ...node, + inputs: node.inputs.map((input) => { + if (input.key === NodeInputKeyEnum.variables && variableList) { + return { + ...input, + value: variableList + }; + } + if (input.key === NodeInputKeyEnum.welcomeText && welcomeText) { + return { + ...input, + value: welcomeText + }; + } + + return input; + }) + }; +}; export const getOrInitModuleInputValue = (input: FlowNodeInputItemType) => { if (input.value !== undefined || !input.valueType) return input.value; diff --git a/packages/service/core/ai/rerank/index.ts b/packages/service/core/ai/rerank/index.ts index b72c96cebbe..b7eb54588f7 100644 --- a/packages/service/core/ai/rerank/index.ts +++ b/packages/service/core/ai/rerank/index.ts @@ -41,6 +41,10 @@ export function reRankRecall({ .then((data) => { addLog.info('ReRank finish:', { time: Date.now() - start }); + if (!data?.results || data?.results?.length === 0) { + addLog.error('ReRank error, empty result', data); + } + return data?.results?.map((item) => ({ id: documents[item.index].id, score: item.relevance_score diff --git a/packages/service/core/chat/chatSchema.ts b/packages/service/core/chat/chatSchema.ts index 588e437f1d9..86e44f8f361 100644 --- a/packages/service/core/chat/chatSchema.ts +++ b/packages/service/core/chat/chatSchema.ts @@ -61,7 +61,15 @@ const ChatSchema = new Schema({ outLinkUid: { type: String }, + + variableList: { + type: Array + }, + welcomeText: { + type: String + }, variables: { + // variable value type: Object, default: {} }, diff --git a/projects/app/src/components/ChatBox/Provider.tsx b/projects/app/src/components/ChatBox/Provider.tsx index d4ff14cf5dd..fd9a8499d75 100644 --- a/projects/app/src/components/ChatBox/Provider.tsx +++ b/projects/app/src/components/ChatBox/Provider.tsx @@ -12,7 +12,7 @@ import { ChatSiteItemType } from '@fastgpt/global/core/chat/type'; type useChatStoreType = OutLinkChatAuthProps & { welcomeText: string; - variableModules: VariableItemType[]; + variableNodes: VariableItemType[]; questionGuide: boolean; ttsConfig: AppTTSConfigType; whisperConfig: AppWhisperConfigType; @@ -41,7 +41,7 @@ type useChatStoreType = OutLinkChatAuthProps & { }; const StateContext = createContext({ welcomeText: '', - variableModules: [], + variableNodes: [], questionGuide: false, ttsConfig: { type: 'none', @@ -110,7 +110,7 @@ const Provider = ({ }: ChatProviderProps) => { const [chatHistories, setChatHistories] = useState([]); - const { welcomeText, variableModules, questionGuide, ttsConfig, whisperConfig } = useMemo( + const { welcomeText, variableNodes, questionGuide, ttsConfig, whisperConfig } = useMemo( () => splitGuideModule(userGuideModule), [userGuideModule] ); @@ -150,7 +150,7 @@ const Provider = ({ teamId, teamToken, welcomeText, - variableModules, + variableNodes, questionGuide, ttsConfig, whisperConfig, diff --git a/projects/app/src/components/ChatBox/components/VariableInput.tsx b/projects/app/src/components/ChatBox/components/VariableInput.tsx index 9135a19dc6b..bb258709e10 100644 --- a/projects/app/src/components/ChatBox/components/VariableInput.tsx +++ b/projects/app/src/components/ChatBox/components/VariableInput.tsx @@ -1,5 +1,5 @@ import { VariableItemType } from '@fastgpt/global/core/app/type.d'; -import React, { useEffect, useState } from 'react'; +import React from 'react'; import { UseFormReturn } from 'react-hook-form'; import { useTranslation } from 'next-i18next'; import { Box, Button, Card, Input, Textarea } from '@chakra-ui/react'; @@ -12,34 +12,19 @@ import { ChatBoxInputFormType } from '../type.d'; const VariableInput = ({ appAvatar, - variableModules, - variableIsFinish, + variableNodes, chatForm, onSubmitVariables }: { appAvatar?: string; - variableModules: VariableItemType[]; - variableIsFinish: boolean; + variableNodes: VariableItemType[]; onSubmitVariables: (e: Record) => void; chatForm: UseFormReturn; }) => { const { t } = useTranslation(); - const { register, unregister, setValue, handleSubmit: handleSubmitChat, watch } = chatForm; + const { register, setValue, handleSubmit: handleSubmitChat, watch } = chatForm; const variables = watch('variables'); - - useEffect(() => { - // 重新注册所有字段 - variableModules.forEach((item) => { - register(`variables.${item.key}`, { required: item.required }); - }); - - return () => { - // 组件卸载时注销所有字段 - variableModules.forEach((item) => { - unregister(`variables.${item.key}`); - }); - }; - }, [register, unregister, variableModules]); + const chatStarted = watch('chatStarted'); return ( @@ -55,7 +40,7 @@ const VariableInput = ({ bg={'white'} boxShadow={'0 0 8px rgba(0,0,0,0.15)'} > - {variableModules.map((item) => ( + {variableNodes.map((item) => ( {item.label} @@ -73,7 +58,6 @@ const VariableInput = ({ {item.type === VariableInputEnum.input && ( ({ label: item.value, value: item.value @@ -110,7 +92,7 @@ const VariableInput = ({ )} ))} - {!variableIsFinish && ( + {!chatStarted && (