mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-12-19 03:56:42 +08:00
### 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:
@ -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>
|
||||
);
|
||||
|
||||
61
web/src/pages/agent/form/tavily-form/index.tsx
Normal file
61
web/src/pages/agent/form/tavily-form/index.tsx
Normal 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;
|
||||
23
web/src/pages/agent/form/tavily-form/use-values.ts
Normal file
23
web/src/pages/agent/form/tavily-form/use-values.ts
Normal 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;
|
||||
}
|
||||
22
web/src/pages/agent/form/tavily-form/use-watch-change.ts
Normal file
22
web/src/pages/agent/form/tavily-form/use-watch-change.ts
Normal 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]);
|
||||
}
|
||||
7
web/src/pages/agent/form/tool-form/index.tsx
Normal file
7
web/src/pages/agent/form/tool-form/index.tsx
Normal file
@ -0,0 +1,7 @@
|
||||
import { INextOperatorForm } from '../../interface';
|
||||
|
||||
const ToolForm = ({ node }: INextOperatorForm) => {
|
||||
return <section>xxx</section>;
|
||||
};
|
||||
|
||||
export default ToolForm;
|
||||
Reference in New Issue
Block a user