From 040e4ad8a50d074ae99aca6062b8533f16a1903d Mon Sep 17 00:00:00 2001 From: balibabu Date: Wed, 2 Jul 2025 18:34:21 +0800 Subject: [PATCH] Feat: Convert the arguments parameter of the code operator to a dictionary #3221 (#8623) ### What problem does this PR solve? Feat: Convert the arguments parameter of the code operator to a dictionary #3221 ### Type of change - [x] New Feature (non-breaking change which adds functionality) --- .../components/next-message-item/index.tsx | 18 ++-- web/src/interfaces/database/chat.ts | 2 +- .../agent/canvas/node/iteration-node.tsx | 6 +- web/src/pages/agent/chat/hooks.ts | 17 +++- web/src/pages/agent/chat/input-form.tsx | 86 ------------------- web/src/pages/agent/constant.tsx | 14 ++- .../agent/form/begin-form/use-watch-change.ts | 2 +- .../pages/agent/form/code-form/use-values.ts | 24 +++--- .../agent/form/code-form/use-watch-change.ts | 13 ++- .../prompt-editor/variable-node.tsx | 2 +- .../prompt-editor/variable-picker-plugin.tsx | 4 + 11 files changed, 63 insertions(+), 125 deletions(-) delete mode 100644 web/src/pages/agent/chat/input-form.tsx diff --git a/web/src/components/next-message-item/index.tsx b/web/src/components/next-message-item/index.tsx index bc01010e3..81e9c67e7 100644 --- a/web/src/components/next-message-item/index.tsx +++ b/web/src/components/next-message-item/index.tsx @@ -130,16 +130,14 @@ const MessageItem = ({ {isAssistant ? ( - index !== 0 && ( - - ) + ) : ( + +
+
diff --git a/web/src/pages/agent/chat/hooks.ts b/web/src/pages/agent/chat/hooks.ts index 733ad53fe..7638f7d29 100644 --- a/web/src/pages/agent/chat/hooks.ts +++ b/web/src/pages/agent/chat/hooks.ts @@ -22,6 +22,8 @@ import { useParams } from 'umi'; import { v4 as uuid } from 'uuid'; import { BeginId } from '../constant'; import { AgentChatLogContext } from '../context'; +import { transferInputsArrayToObject } from '../form/begin-form/use-watch-change'; +import { useGetBeginNodeDataQuery } from '../hooks/use-get-begin-query'; import { BeginQuery } from '../interface'; import useGraphStore from '../store'; import { receiveMessageError } from '../utils'; @@ -109,6 +111,7 @@ export const useSendNextMessage = () => { const { handleInputChange, value, setValue } = useHandleMessageInputChange(); const { refetch } = useFetchAgent(); const { addEventList } = useContext(AgentChatLogContext); + const getBeginNodeDataQuery = useGetBeginNodeDataQuery(); const { send, answerList, done, stopOutputMessage } = useSendMessageBySSE( api.runCanvas, @@ -191,12 +194,22 @@ export const useSendNextMessage = () => { ); useEffect(() => { - if (prologue) { + const query = getBeginNodeDataQuery(); + if (query.length > 0) { + send({ id: agentId, inputs: transferInputsArrayToObject(query) }); + } else if (prologue) { addNewestOneAnswer({ answer: prologue, }); } - }, [addNewestOneAnswer, prologue]); + }, [ + addNewestOneAnswer, + agentId, + getBeginNodeDataQuery, + prologue, + send, + sendFormMessage, + ]); useEffect(() => { addEventList(answerList); diff --git a/web/src/pages/agent/chat/input-form.tsx b/web/src/pages/agent/chat/input-form.tsx deleted file mode 100644 index b82dbaf84..000000000 --- a/web/src/pages/agent/chat/input-form.tsx +++ /dev/null @@ -1,86 +0,0 @@ -'use client'; - -import { zodResolver } from '@hookform/resolvers/zod'; -import { useForm } from 'react-hook-form'; -import { toast } from 'sonner'; -import { z } from 'zod'; - -import { Button } from '@/components/ui/button'; -import { - Form, - FormControl, - FormField, - FormItem, - FormLabel, - FormMessage, -} from '@/components/ui/form'; -import { Input } from '@/components/ui/input'; -import { Message } from '@/interfaces/database/chat'; -import { get } from 'lodash'; -import { useParams } from 'umi'; -import { useSendNextMessage } from './hooks'; - -const FormSchema = z.object({ - username: z.string().min(2, { - message: 'Username must be at least 2 characters.', - }), -}); - -type InputFormProps = Pick, 'send'> & { - message: Message; -}; - -export function InputForm({ send, message }: InputFormProps) { - const form = useForm>({ - resolver: zodResolver(FormSchema), - defaultValues: { - username: '', - }, - }); - - const { id: canvasId } = useParams(); - - function onSubmit(data: z.infer) { - const inputs = get(message, 'data.inputs', {}); - - const nextInputs = Object.entries(inputs).reduce((pre, [key, val]) => { - pre[key] = { ...val, value: data.username }; - - return pre; - }, {}); - - send({ - inputs: nextInputs, - id: canvasId, - }); - - toast('You submitted the following values', { - description: ( -
-          {JSON.stringify(data, null, 2)}
-        
- ), - }); - } - - return ( -
- - ( - - Username - - - - - - )} - /> - - - - ); -} diff --git a/web/src/pages/agent/constant.tsx b/web/src/pages/agent/constant.tsx index f069b17cf..bbb44966f 100644 --- a/web/src/pages/agent/constant.tsx +++ b/web/src/pages/agent/constant.tsx @@ -661,16 +661,12 @@ export const initialIterationStartValues = { }; export const initialCodeValues = { - lang: 'python', + lang: ProgrammingLanguage.Python, script: CodeTemplateStrMap[ProgrammingLanguage.Python], - arguments: [ - { - name: 'arg1', - }, - { - name: 'arg2', - }, - ], + arguments: { + arg1: '', + arg2: '', + }, }; export const initialWaitingDialogueValues = {}; diff --git a/web/src/pages/agent/form/begin-form/use-watch-change.ts b/web/src/pages/agent/form/begin-form/use-watch-change.ts index 00b40003f..f0da58068 100644 --- a/web/src/pages/agent/form/begin-form/use-watch-change.ts +++ b/web/src/pages/agent/form/begin-form/use-watch-change.ts @@ -4,7 +4,7 @@ import { UseFormReturn, useWatch } from 'react-hook-form'; import { BeginQuery } from '../../interface'; import useGraphStore from '../../store'; -function transferInputsArrayToObject(inputs: BeginQuery[] = []) { +export function transferInputsArrayToObject(inputs: BeginQuery[] = []) { return inputs.reduce>>((pre, cur) => { pre[cur.key] = omit(cur, 'key'); diff --git a/web/src/pages/agent/form/code-form/use-values.ts b/web/src/pages/agent/form/code-form/use-values.ts index 9dc7d68d4..ce8ef1f9b 100644 --- a/web/src/pages/agent/form/code-form/use-values.ts +++ b/web/src/pages/agent/form/code-form/use-values.ts @@ -1,27 +1,25 @@ -import { CodeTemplateStrMap, ProgrammingLanguage } from '@/constants/agent'; import { RAGFlowNodeType } from '@/interfaces/database/flow'; import { isEmpty } from 'lodash'; import { useMemo } from 'react'; +import { initialCodeValues } from '../../constant'; + +function convertToArray(args: Record) { + return Object.entries(args).map(([key, value]) => ({ + name: key, + component_id: value, + })); +} export function useValues(node?: RAGFlowNodeType) { - const defaultValues = useMemo( - () => ({ - lang: ProgrammingLanguage.Python, - script: CodeTemplateStrMap[ProgrammingLanguage.Python], - arguments: [], - }), - [], - ); - const values = useMemo(() => { const formData = node?.data?.form; if (isEmpty(formData)) { - return defaultValues; + return initialCodeValues; } - return formData; - }, [defaultValues, node?.data?.form]); + return { ...formData, arguments: convertToArray(formData.arguments) }; + }, [node?.data?.form]); return values; } diff --git a/web/src/pages/agent/form/code-form/use-watch-change.ts b/web/src/pages/agent/form/code-form/use-watch-change.ts index 8f882b0a3..6ce0187af 100644 --- a/web/src/pages/agent/form/code-form/use-watch-change.ts +++ b/web/src/pages/agent/form/code-form/use-watch-change.ts @@ -3,6 +3,14 @@ import { useCallback, useEffect } from 'react'; import { UseFormReturn, useWatch } from 'react-hook-form'; import useGraphStore from '../../store'; +function convertToObject(list: Array<{ name: string; component_id: string }>) { + return list.reduce>((pre, cur) => { + pre[cur.name] = cur.component_id; + + return pre; + }, {}); +} + export function useWatchFormChange(id?: string, form?: UseFormReturn) { let values = useWatch({ control: form?.control }); const updateNodeForm = useGraphStore((state) => state.updateNodeForm); @@ -11,7 +19,10 @@ export function useWatchFormChange(id?: string, form?: UseFormReturn) { // Manually triggered form updates are synchronized to the canvas if (id) { values = form?.getValues() || {}; - let nextValues: any = values; + let nextValues: any = { + ...values, + arguments: convertToObject(values.arguments), + }; updateNodeForm(id, nextValues); } diff --git a/web/src/pages/agent/form/components/prompt-editor/variable-node.tsx b/web/src/pages/agent/form/components/prompt-editor/variable-node.tsx index e2a8cc29f..d8947727b 100644 --- a/web/src/pages/agent/form/components/prompt-editor/variable-node.tsx +++ b/web/src/pages/agent/form/components/prompt-editor/variable-node.tsx @@ -37,7 +37,7 @@ export class VariableNode extends DecoratorNode { let content: ReactNode = ( {this.__label} ); - if (this.__value.startsWith(prefix)) { + if (this.__value?.startsWith(prefix)) { content = (
{i18n.t(`flow.begin`)} / {content} diff --git a/web/src/pages/agent/form/components/prompt-editor/variable-picker-plugin.tsx b/web/src/pages/agent/form/components/prompt-editor/variable-picker-plugin.tsx index 8797f2236..6f0ca8e67 100644 --- a/web/src/pages/agent/form/components/prompt-editor/variable-picker-plugin.tsx +++ b/web/src/pages/agent/form/components/prompt-editor/variable-picker-plugin.tsx @@ -220,6 +220,10 @@ export default function VariablePickerMenuPlugin({ } $getRoot().clear().append(paragraph); + + if ($isRangeSelection($getSelection())) { + $getRoot().selectEnd(); + } }, [findLabelByValue], );