diff --git a/packages/global/core/app/plugin/utils.ts b/packages/global/core/app/plugin/utils.ts index fa56622d8..1aaa06ec9 100644 --- a/packages/global/core/app/plugin/utils.ts +++ b/packages/global/core/app/plugin/utils.ts @@ -5,6 +5,20 @@ import { FlowNodeTypeEnum } from '../../workflow/node/constant'; export const getPluginInputsFromStoreNodes = (nodes: StoreNodeItemType[]) => { return nodes.find((node) => node.flowNodeType === FlowNodeTypeEnum.pluginInput)?.inputs || []; }; -export const getPluginRunContent = (e: { pluginInputs: FlowNodeInputItemType[] }) => { - return JSON.stringify(e); +export const getPluginRunContent = ({ + pluginInputs, + variables +}: { + pluginInputs: FlowNodeInputItemType[]; + variables: Record; +}) => { + const pluginInputsWithValue = pluginInputs.map((input) => { + const { key } = input; + const value = variables?.hasOwnProperty(key) ? variables[key] : input.defaultValue; + return { + ...input, + value + }; + }); + return JSON.stringify(pluginInputsWithValue); }; diff --git a/projects/app/src/components/core/chat/ChatContainer/PluginRunBox/components/RenderInput.tsx b/projects/app/src/components/core/chat/ChatContainer/PluginRunBox/components/RenderInput.tsx index 13149d767..0b2fdef72 100644 --- a/projects/app/src/components/core/chat/ChatContainer/PluginRunBox/components/RenderInput.tsx +++ b/projects/app/src/components/core/chat/ChatContainer/PluginRunBox/components/RenderInput.tsx @@ -31,10 +31,34 @@ const RenderInput = () => { ); }, [pluginInputs]); + const historyFormValues = useMemo(() => { + if (histories.length === 0) return undefined; + + try { + const inputValueString = histories[0].value[0].text?.content || '[]'; + return JSON.parse(inputValueString).reduce( + ( + acc: Record, + { + key, + value + }: { + key: string; + value: any; + } + ) => ({ ...acc, [key]: value }), + {} + ); + } catch (error) { + console.error('Failed to parse input value:', error); + return undefined; + } + }, [histories]); + useEffect(() => { if (isEqual(getValues(), defaultFormValues)) return; - reset(defaultFormValues); - }, [defaultFormValues, histories]); + reset(historyFormValues || defaultFormValues); + }, [defaultFormValues, historyFormValues]); const isDisabledInput = histories.length > 0; diff --git a/projects/app/src/components/core/chat/ChatContainer/PluginRunBox/context.tsx b/projects/app/src/components/core/chat/ChatContainer/PluginRunBox/context.tsx index 3a190308e..4d6686478 100644 --- a/projects/app/src/components/core/chat/ChatContainer/PluginRunBox/context.tsx +++ b/projects/app/src/components/core/chat/ChatContainer/PluginRunBox/context.tsx @@ -51,7 +51,7 @@ const PluginRunContextProvider = ({ }, []); const generatingMessage = useCallback( - ({ event, text = '', status, name, tool, variables }: generatingMessageProps) => { + ({ event, text = '', status, name, tool }: generatingMessageProps) => { setHistories((state) => state.map((item, index) => { if (index !== state.length - 1 || item.obj !== ChatRoleEnum.AI) return item; @@ -170,7 +170,8 @@ const PluginRunContextProvider = ({ type: ChatItemValueTypeEnum.text, text: { content: getPluginRunContent({ - pluginInputs + pluginInputs, + variables: e }) } } diff --git a/projects/app/src/pages/api/v1/chat/completions.ts b/projects/app/src/pages/api/v1/chat/completions.ts index b002ed04f..975702de5 100644 --- a/projects/app/src/pages/api/v1/chat/completions.ts +++ b/projects/app/src/pages/api/v1/chat/completions.ts @@ -201,7 +201,8 @@ async function handler(req: NextApiRequest, res: NextApiResponse) { type: ChatItemValueTypeEnum.text, text: { content: getPluginRunContent({ - pluginInputs: getPluginInputsFromStoreNodes(app.modules) + pluginInputs: getPluginInputsFromStoreNodes(app.modules), + variables }) } }