diff --git a/web/src/views/Workflow/components/AddChatVariable/index.tsx b/web/src/views/Workflow/components/AddChatVariable/index.tsx index 7ebce7df..d6741bf9 100644 --- a/web/src/views/Workflow/components/AddChatVariable/index.tsx +++ b/web/src/views/Workflow/components/AddChatVariable/index.tsx @@ -1,6 +1,5 @@ -import React, { useState, useImperativeHandle, forwardRef, useRef } from 'react'; -import { Button, Input, Space, Typography, Tooltip, message, List } from 'antd'; -import { PlusOutlined, EditOutlined, DeleteOutlined } from '@ant-design/icons'; +import { useState, useImperativeHandle, forwardRef, useRef } from 'react'; +import { Button, Space, List } from 'antd'; import { useTranslation } from 'react-i18next'; import type { ChatVariable, AddChatVariableRef } from '../../types'; import type { ChatVariableModalRef } from './types' diff --git a/web/src/views/Workflow/components/Properties/HttpRequest/EditableTable.tsx b/web/src/views/Workflow/components/Properties/HttpRequest/EditableTable.tsx index 251409dd..e0912c0a 100644 --- a/web/src/views/Workflow/components/Properties/HttpRequest/EditableTable.tsx +++ b/web/src/views/Workflow/components/Properties/HttpRequest/EditableTable.tsx @@ -131,7 +131,7 @@ const EditableTable: React.FC = ({ const AddButton = ({ block = false }: { block?: boolean }) => ( diff --git a/web/src/views/Workflow/components/Properties/index.tsx b/web/src/views/Workflow/components/Properties/index.tsx index a555b4fe..8752121e 100644 --- a/web/src/views/Workflow/components/Properties/index.tsx +++ b/web/src/views/Workflow/components/Properties/index.tsx @@ -22,6 +22,7 @@ import ConditionList from './ConditionList' import CycleVarsList from './CycleVarsList' import AssignmentList from './AssignmentList' import ToolConfig from './ToolConfig' +import MemoryConfig from './MemoryConfig' // import { calculateVariableList } from './utils/variableListCalculator' interface PropertiesProps { @@ -1230,6 +1231,20 @@ const Properties: FC = ({ ) } + if (config.type === 'memoryConfig') { + return ( + + + + ) + } return ( boolean | void; parseEvent: () => boolean | void; handleSave: (flag?: boolean) => Promise; + chatVariables: ChatVariable[]; + setChatVariables: React.Dispatch>; } export const edge_color = '#155EEF'; @@ -54,6 +56,7 @@ export const useWorkflowGraph = ({ const [canRedo, setCanRedo] = useState(false); const [isHandMode, setIsHandMode] = useState(false); const [config, setConfig] = useState(null); + const [chatVariables, setChatVariables] = useState([]) useEffect(() => { getConfig() @@ -63,16 +66,15 @@ export const useWorkflowGraph = ({ getWorkflowConfig(id) .then(res => { const { variables, ...rest } = res as WorkflowConfig - setConfig({ - ...rest, - variables: variables.map(v => { - const { default: _, ...cleanV } = v - return { - ...cleanV, - defaultValue: v.default ?? '' - } - }) + const initChatVariables = variables.map(v => { + const { default: _, ...cleanV } = v + return { + ...cleanV, + defaultValue: v.default ?? '' + } }) + setChatVariables(initChatVariables) + setConfig({ ...rest, variables: initChatVariables }) }) } @@ -94,7 +96,17 @@ export const useWorkflowGraph = ({ if (nodeLibraryConfig?.config) { Object.keys(nodeLibraryConfig.config).forEach(key => { - if (key === 'knowledge_retrieval' && nodeLibraryConfig.config && nodeLibraryConfig.config[key]) { + if (key === 'memory' && nodeLibraryConfig.config && nodeLibraryConfig.config[key]) { + const { memory, messages } = config as any; + if (memory?.enable && messages && messages.length > 0) { + const lastMessage = messages[messages.length - 1] + nodeLibraryConfig.config[key].defaultValue = { + ...memory, + messages: lastMessage.content + } + nodeLibraryConfig.config.messages.defaultValue.splice(-1, 1) + } + } else if (key === 'knowledge_retrieval' && nodeLibraryConfig.config && nodeLibraryConfig.config[key]) { const { query, ...rest } = config nodeLibraryConfig.config[key].defaultValue = { ...rest @@ -917,13 +929,13 @@ export const useWorkflowGraph = ({ const params = { ...config, - variables: config.variables.map(v => { - const { defaultValue, ...cleanV } = v - return { - ...cleanV, - default: defaultValue ?? '' - } - }), + variables: chatVariables.map(v => { + const { defaultValue, ...cleanV } = v + return { + ...cleanV, + default: defaultValue ?? '' + } + }), nodes: nodes.map((node: Node) => { const data = node.getData(); const position = node.getPosition(); @@ -931,7 +943,15 @@ export const useWorkflowGraph = ({ if (data.config) { Object.keys(data.config).forEach(key => { - if (data.config[key] && 'defaultValue' in data.config[key] && key === 'group_variables') { + if (key === 'memory' && data.config[key] && 'defaultValue' in data.config[key]) { + const { messages, ...rest } = data.config[key].defaultValue + let memoryMessage = { role: 'USER', content: data.config[key].defaultValue.messages } + itemConfig = { + ...itemConfig, + messages: rest.enable ? [...itemConfig.messages, memoryMessage] : itemConfig.messages, + memory: { ...rest }, + } + } else if (data.config[key] && 'defaultValue' in data.config[key] && key === 'group_variables') { let group_variables = data.config.group.defaultValue ? {} : data.config[key].defaultValue if (data.config.group.defaultValue) { data.config[key].defaultValue.map((vo: any) => { @@ -1077,5 +1097,7 @@ export const useWorkflowGraph = ({ copyEvent, parseEvent, handleSave, + chatVariables, + setChatVariables }; }; diff --git a/web/src/views/Workflow/index.tsx b/web/src/views/Workflow/index.tsx index 70803373..ba17a63a 100644 --- a/web/src/views/Workflow/index.tsx +++ b/web/src/views/Workflow/index.tsx @@ -8,7 +8,7 @@ import PortClickHandler from './components/PortClickHandler'; import { useWorkflowGraph } from './hooks/useWorkflowGraph'; import type { WorkflowRef } from '@/views/ApplicationConfig/types' import Chat from './components/Chat/Chat'; -import type { ChatRef, AddChatVariableRef, ChatVariable } from './types' +import type { ChatRef, AddChatVariableRef } from './types' import arrowIcon from '@/assets/images/workflow/arrow.png' import AddChatVariable from './components/AddChatVariable'; @@ -21,7 +21,6 @@ const Workflow = forwardRef((_props, ref) => { // 使用自定义Hook初始化工作流图 const { config, - setConfig, graphRef, selectedNode, setSelectedNode, @@ -38,6 +37,8 @@ const Workflow = forwardRef((_props, ref) => { copyEvent, parseEvent, handleSave, + chatVariables, + setChatVariables } = useWorkflowGraph({ containerRef, miniMapRef }); const onDragOver = (event: React.DragEvent) => { @@ -52,15 +53,6 @@ const Workflow = forwardRef((_props, ref) => { const addVariable = () => { addChatVariableRef.current?.handleOpen() } - const handleUpdateChatVariable = (variables: ChatVariable[]) => { - setConfig(prev => { - if (!prev) return null - return { - ...prev, - variables - } - }) - } useImperativeHandle(ref, () => ({ handleSave, @@ -125,8 +117,8 @@ const Workflow = forwardRef((_props, ref) => { );