Feat: Add a tool operator node from the agent form #3221 (#8344)

### What problem does this PR solve?
Feat: Add a tool operator node from the agent form #3221

### Type of change


- [x] New Feature (non-breaking change which adds functionality)
This commit is contained in:
balibabu
2025-06-18 16:40:08 +08:00
committed by GitHub
parent 3671d20e43
commit e96cf89524
9 changed files with 207 additions and 23 deletions

View File

@ -3,15 +3,33 @@ import {
PopoverContent,
PopoverTrigger,
} from '@/components/ui/popover';
import { PropsWithChildren } from 'react';
import { Operator } from '@/pages/agent/constant';
import { AgentFormContext, AgentInstanceContext } from '@/pages/agent/context';
import { Position } from '@xyflow/react';
import { PropsWithChildren, useCallback, useContext } from 'react';
import { ToolCommand } from './tool-command';
export function ToolPopover({ children }: PropsWithChildren) {
const { addCanvasNode } = useContext(AgentInstanceContext);
const node = useContext(AgentFormContext);
const handleChange = useCallback(
(value: string[]) => {
if (Array.isArray(value) && value.length > 0) {
addCanvasNode(Operator.Tool, {
position: Position.Bottom,
nodeId: node?.id,
})();
}
},
[addCanvasNode, node?.id],
);
return (
<Popover>
<PopoverTrigger asChild>{children}</PopoverTrigger>
<PopoverContent className="w-80 p-0">
<ToolCommand></ToolCommand>
<ToolCommand onChange={handleChange}></ToolCommand>
</PopoverContent>
</Popover>
);

View File

@ -0,0 +1,61 @@
import { FormContainer } from '@/components/form-container';
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from '@/components/ui/form';
import { RAGFlowSelect } from '@/components/ui/select';
import { zodResolver } from '@hookform/resolvers/zod';
import { useForm } from 'react-hook-form';
import { z } from 'zod';
import { INextOperatorForm } from '../../interface';
import { useValues } from './use-values';
import { useWatchFormChange } from './use-watch-change';
const MessageForm = ({ node }: INextOperatorForm) => {
const values = useValues(node);
const FormSchema = z.object({
query: z.string(),
});
const form = useForm({
defaultValues: values,
resolver: zodResolver(FormSchema),
});
useWatchFormChange(node?.id, form);
return (
<Form {...form}>
<form
className="space-y-5 px-5 "
autoComplete="off"
onSubmit={(e) => {
e.preventDefault();
}}
>
<FormContainer>
<FormField
control={form.control}
name="query"
render={({ field }) => (
<FormItem>
<FormLabel>Username</FormLabel>
<FormControl>
<RAGFlowSelect placeholder="shadcn" {...field} options={[]} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</FormContainer>
</form>
</Form>
);
};
export default MessageForm;

View File

@ -0,0 +1,23 @@
import { RAGFlowNodeType } from '@/interfaces/database/flow';
import { isEmpty } from 'lodash';
import { useMemo } from 'react';
const defaultValues = {
content: [],
};
export function useValues(node?: RAGFlowNodeType) {
const values = useMemo(() => {
const formData = node?.data?.form;
if (isEmpty(formData)) {
return defaultValues;
}
return {
...formData,
};
}, [node]);
return values;
}

View File

@ -0,0 +1,22 @@
import { useEffect } from 'react';
import { UseFormReturn, useWatch } from 'react-hook-form';
import useGraphStore from '../../store';
export function useWatchFormChange(id?: string, form?: UseFormReturn) {
let values = useWatch({ control: form?.control });
const updateNodeForm = useGraphStore((state) => state.updateNodeForm);
useEffect(() => {
// Manually triggered form updates are synchronized to the canvas
if (id && form?.formState.isDirty) {
values = form?.getValues();
let nextValues: any = values;
nextValues = {
...values,
};
updateNodeForm(id, nextValues);
}
}, [form?.formState.isDirty, id, updateNodeForm, values]);
}

View File

@ -0,0 +1,7 @@
import { INextOperatorForm } from '../../interface';
const ToolForm = ({ node }: INextOperatorForm) => {
return <section>xxx</section>;
};
export default ToolForm;