Feature:Add a loading status to the agent canvas page.

This commit is contained in:
chanx 2025-12-04 10:47:21 +08:00
parent 24bdf1731a
commit 8fab4bba11
20 changed files with 152 additions and 24 deletions

View file

@ -40,6 +40,7 @@ import { useDropdownManager } from './context';
import { AgentBackground } from '@/components/canvas/background'; import { AgentBackground } from '@/components/canvas/background';
import Spotlight from '@/components/spotlight'; import Spotlight from '@/components/spotlight';
import { useNodeLoading } from '../hooks/use-node-loading';
import { import {
useHideFormSheetOnNodeDeletion, useHideFormSheetOnNodeDeletion,
useShowDrawer, useShowDrawer,
@ -172,6 +173,8 @@ function AgentCanvas({ drawerVisible, hideDrawer }: IProps) {
}); });
const [lastSendLoading, setLastSendLoading] = useState(false); const [lastSendLoading, setLastSendLoading] = useState(false);
const [currentSendLoading, setCurrentSendLoading] = useState(false);
const { handleBeforeDelete } = useBeforeDelete(); const { handleBeforeDelete } = useBeforeDelete();
const { addCanvasNode, addNoteNode } = useAddNode(reactFlowInstance); const { addCanvasNode, addNoteNode } = useAddNode(reactFlowInstance);
@ -188,6 +191,7 @@ function AgentCanvas({ drawerVisible, hideDrawer }: IProps) {
}, [chatVisible, clearEventList, currentTaskId, stopMessage]); }, [chatVisible, clearEventList, currentTaskId, stopMessage]);
const setLastSendLoadingFunc = (loading: boolean, messageId: string) => { const setLastSendLoadingFunc = (loading: boolean, messageId: string) => {
setCurrentSendLoading(!!loading);
if (messageId === currentMessageId) { if (messageId === currentMessageId) {
setLastSendLoading(loading); setLastSendLoading(loading);
} else { } else {
@ -255,7 +259,10 @@ function AgentCanvas({ drawerVisible, hideDrawer }: IProps) {
clearActiveDropdown, clearActiveDropdown,
removePlaceholderNode, removePlaceholderNode,
]); ]);
const { lastNode, setDerivedMessages, startButNotFinishedNodeIds } =
useNodeLoading({
currentEventListWithoutMessageById,
});
return ( return (
<div className={cn(styles.canvasWrapper, 'px-5 pb-5')}> <div className={cn(styles.canvasWrapper, 'px-5 pb-5')}>
<svg <svg
@ -291,7 +298,15 @@ function AgentCanvas({ drawerVisible, hideDrawer }: IProps) {
</marker> </marker>
</defs> </defs>
</svg> </svg>
<AgentInstanceContext.Provider value={{ addCanvasNode, showFormDrawer }}> <AgentInstanceContext.Provider
value={{
addCanvasNode,
showFormDrawer,
lastNode,
currentSendLoading,
startButNotFinishedNodeIds,
}}
>
<ReactFlow <ReactFlow
connectionMode={ConnectionMode.Loose} connectionMode={ConnectionMode.Loose}
nodes={nodes} nodes={nodes}
@ -386,9 +401,10 @@ function AgentCanvas({ drawerVisible, hideDrawer }: IProps) {
></FormSheet> ></FormSheet>
</AgentInstanceContext.Provider> </AgentInstanceContext.Provider>
)} )}
{chatVisible && ( {chatVisible && (
<AgentChatContext.Provider <AgentChatContext.Provider
value={{ showLogSheet, setLastSendLoadingFunc }} value={{ showLogSheet, setLastSendLoadingFunc, setDerivedMessages }}
> >
<AgentChatLogContext.Provider <AgentChatLogContext.Provider
value={{ addEventList, setCurrentMessageId }} value={{ addEventList, setCurrentMessageId }}

View file

@ -44,7 +44,7 @@ function InnerAgentNode({
return ( return (
<ToolBar selected={selected} id={id} label={data.label}> <ToolBar selected={selected} id={id} label={data.label}>
<NodeWrapper selected={selected}> <NodeWrapper selected={selected} id={id}>
{isHeadAgent && ( {isHeadAgent && (
<> <>
<LeftEndHandle></LeftEndHandle> <LeftEndHandle></LeftEndHandle>

View file

@ -24,7 +24,7 @@ function InnerBeginNode({ data, id, selected }: NodeProps<IBeginNode>) {
const inputs: Record<string, BeginQuery> = get(data, 'form.inputs', {}); const inputs: Record<string, BeginQuery> = get(data, 'form.inputs', {});
return ( return (
<NodeWrapper selected={selected}> <NodeWrapper selected={selected} id={id}>
<CommonHandle <CommonHandle
type="source" type="source"
position={Position.Right} position={Position.Right}

View file

@ -18,7 +18,7 @@ export function InnerCategorizeNode({
const { positions } = useBuildCategorizeHandlePositions({ data, id }); const { positions } = useBuildCategorizeHandlePositions({ data, id });
return ( return (
<ToolBar selected={selected} id={id} label={data.label}> <ToolBar selected={selected} id={id} label={data.label}>
<NodeWrapper selected={selected}> <NodeWrapper selected={selected} id={id}>
<LeftEndHandle></LeftEndHandle> <LeftEndHandle></LeftEndHandle>
<NodeHeader id={id} name={data.name} label={data.label}></NodeHeader> <NodeHeader id={id} name={data.name} label={data.label}></NodeHeader>

View file

@ -14,7 +14,7 @@ export function ExitLoopNode({ id, data, selected }: NodeProps<BaseNode<any>>) {
showRun={false} showRun={false}
showCopy={false} showCopy={false}
> >
<NodeWrapper selected={selected}> <NodeWrapper selected={selected} id={id}>
<LeftEndHandle></LeftEndHandle> <LeftEndHandle></LeftEndHandle>
<NodeHeader id={id} name={data.name} label={data.label}></NodeHeader> <NodeHeader id={id} name={data.name} label={data.label}></NodeHeader>
</NodeWrapper> </NodeWrapper>

View file

@ -23,7 +23,7 @@ function InnerFileNode({ data, id, selected }: NodeProps<IBeginNode>) {
const inputs: Record<string, BeginQuery> = get(data, 'form.inputs', {}); const inputs: Record<string, BeginQuery> = get(data, 'form.inputs', {});
return ( return (
<NodeWrapper selected={selected}> <NodeWrapper selected={selected} id={id}>
<CommonHandle <CommonHandle
type="source" type="source"
position={Position.Right} position={Position.Right}

View file

@ -26,7 +26,7 @@ function InnerRagNode({
showRun={needsSingleStepDebugging(data.label)} showRun={needsSingleStepDebugging(data.label)}
showCopy={showCopyIcon(data.label)} showCopy={showCopyIcon(data.label)}
> >
<NodeWrapper selected={selected}> <NodeWrapper selected={selected} id={id}>
<LeftEndHandle></LeftEndHandle> <LeftEndHandle></LeftEndHandle>
<CommonHandle <CommonHandle
type="source" type="source"

View file

@ -14,7 +14,7 @@ function InnerMessageNode({ id, data, selected }: NodeProps<IMessageNode>) {
const messages: string[] = get(data, 'form.messages', []); const messages: string[] = get(data, 'form.messages', []);
return ( return (
<ToolBar selected={selected} id={id} label={data.label}> <ToolBar selected={selected} id={id} label={data.label}>
<NodeWrapper selected={selected}> <NodeWrapper selected={selected} id={id}>
<LeftEndHandle></LeftEndHandle> <LeftEndHandle></LeftEndHandle>
{/* <CommonHandle {/* <CommonHandle
type="source" type="source"

View file

@ -1,9 +1,13 @@
import { cn } from '@/lib/utils'; import { cn } from '@/lib/utils';
import { HTMLAttributes } from 'react'; import { Loader } from 'lucide-react';
import { HTMLAttributes, useContext } from 'react';
import { AgentInstanceContext } from '../../context';
type IProps = HTMLAttributes<HTMLDivElement> & { selected?: boolean }; type IProps = HTMLAttributes<HTMLDivElement> & { selected?: boolean };
export function NodeWrapper({ children, className, selected }: IProps) { export function NodeWrapper({ children, className, selected, id }: IProps) {
const { currentSendLoading, startButNotFinishedNodeIds = [] } =
useContext(AgentInstanceContext);
return ( return (
<section <section
className={cn( className={cn(
@ -12,6 +16,13 @@ export function NodeWrapper({ children, className, selected }: IProps) {
className, className,
)} )}
> >
{id &&
startButNotFinishedNodeIds.indexOf(id as string) > -1 &&
currentSendLoading && (
<div className=" absolute right-0 left-0 top-0 flex items-start justify-end p-2">
<Loader size={12} className=" animate-spin" />
</div>
)}
{children} {children}
</section> </section>
); );

View file

@ -19,7 +19,7 @@ function ParserNode({
}: NodeProps<BaseNode<ParserFormSchemaType>>) { }: NodeProps<BaseNode<ParserFormSchemaType>>) {
const { t } = useTranslation(); const { t } = useTranslation();
return ( return (
<NodeWrapper selected={selected}> <NodeWrapper selected={selected} id={id}>
<CommonHandle <CommonHandle
id={NodeHandleId.End} id={NodeHandleId.End}
type="target" type="target"

View file

@ -27,7 +27,7 @@ function InnerRetrievalNode({
return ( return (
<ToolBar selected={selected} id={id} label={data.label}> <ToolBar selected={selected} id={id} label={data.label}>
<NodeWrapper selected={selected}> <NodeWrapper selected={selected} id={id}>
<LeftEndHandle></LeftEndHandle> <LeftEndHandle></LeftEndHandle>
<CommonHandle <CommonHandle
id={NodeHandleId.Start} id={NodeHandleId.Start}

View file

@ -25,7 +25,7 @@ function InnerSplitterNode({
showCopy={false} showCopy={false}
showRun={false} showRun={false}
> >
<NodeWrapper selected={selected}> <NodeWrapper selected={selected} id={id}>
<CommonHandle <CommonHandle
id={NodeHandleId.End} id={NodeHandleId.End}
type="target" type="target"

View file

@ -65,7 +65,7 @@ function InnerSwitchNode({ id, data, selected }: NodeProps<ISwitchNode>) {
const { positions } = useBuildSwitchHandlePositions({ data, id }); const { positions } = useBuildSwitchHandlePositions({ data, id });
return ( return (
<ToolBar selected={selected} id={id} label={data.label} showRun={false}> <ToolBar selected={selected} id={id} label={data.label} showRun={false}>
<NodeWrapper selected={selected}> <NodeWrapper selected={selected} id={id}>
<LeftEndHandle></LeftEndHandle> <LeftEndHandle></LeftEndHandle>
<NodeHeader id={id} name={data.name} label={data.label}></NodeHeader> <NodeHeader id={id} name={data.name} label={data.label}></NodeHeader>
<section className="gap-2.5 flex flex-col"> <section className="gap-2.5 flex flex-col">

View file

@ -27,7 +27,7 @@ function TokenizerNode({
showRun={false} showRun={false}
showCopy={false} showCopy={false}
> >
<NodeWrapper selected={selected}> <NodeWrapper selected={selected} id={id}>
<CommonHandle <CommonHandle
id={NodeHandleId.End} id={NodeHandleId.End}
type="target" type="target"

View file

@ -44,7 +44,7 @@ function InnerToolNode({
); );
return ( return (
<NodeWrapper selected={selected}> <NodeWrapper selected={selected} id={id}>
<Handle <Handle
id={NodeHandleId.End} id={NodeHandleId.End}
type="target" type="target"

View file

@ -13,8 +13,9 @@ import {
} from '@/hooks/use-agent-request'; } from '@/hooks/use-agent-request';
import { useFetchUserInfo } from '@/hooks/use-user-setting-request'; import { useFetchUserInfo } from '@/hooks/use-user-setting-request';
import { buildMessageUuidWithRole } from '@/utils/chat'; import { buildMessageUuidWithRole } from '@/utils/chat';
import { memo, useCallback } from 'react'; import { memo, useCallback, useContext } from 'react';
import { useParams } from 'umi'; import { useParams } from 'umi';
import { AgentChatContext } from '../context';
import DebugContent from '../debug-content'; import DebugContent from '../debug-content';
import { useAwaitCompentData } from '../hooks/use-chat-logic'; import { useAwaitCompentData } from '../hooks/use-chat-logic';
import { useIsTaskMode } from '../hooks/use-get-begin-query'; import { useIsTaskMode } from '../hooks/use-get-begin-query';
@ -49,6 +50,9 @@ function AgentChatBox() {
canvasId: canvasId as string, canvasId: canvasId as string,
}); });
const { setDerivedMessages } = useContext(AgentChatContext);
setDerivedMessages?.(derivedMessages);
const isTaskMode = useIsTaskMode(); const isTaskMode = useIsTaskMode();
const handleUploadFile: NonNullable<FileUploadProps['onUpload']> = const handleUploadFile: NonNullable<FileUploadProps['onUpload']> =

View file

@ -1,6 +1,8 @@
import { INodeEvent } from '@/hooks/use-send-message';
import { IMessage } from '@/interfaces/database/chat';
import { RAGFlowNodeType } from '@/interfaces/database/flow'; import { RAGFlowNodeType } from '@/interfaces/database/flow';
import { HandleType, Position } from '@xyflow/react'; import { HandleType, Position } from '@xyflow/react';
import { createContext } from 'react'; import { Dispatch, SetStateAction, createContext } from 'react';
import { useAddNode } from './hooks/use-add-node'; import { useAddNode } from './hooks/use-add-node';
import { useCacheChatLog } from './hooks/use-cache-chat-log'; import { useCacheChatLog } from './hooks/use-cache-chat-log';
import { useShowFormDrawer, useShowLogSheet } from './hooks/use-show-drawer'; import { useShowFormDrawer, useShowLogSheet } from './hooks/use-show-drawer';
@ -13,7 +15,11 @@ type AgentInstanceContextType = Pick<
ReturnType<typeof useAddNode>, ReturnType<typeof useAddNode>,
'addCanvasNode' 'addCanvasNode'
> & > &
Pick<ReturnType<typeof useShowFormDrawer>, 'showFormDrawer'>; Pick<ReturnType<typeof useShowFormDrawer>, 'showFormDrawer'> & {
lastNode: INodeEvent | null;
currentSendLoading: boolean;
startButNotFinishedNodeIds: string[];
};
export const AgentInstanceContext = createContext<AgentInstanceContextType>( export const AgentInstanceContext = createContext<AgentInstanceContextType>(
{} as AgentInstanceContextType, {} as AgentInstanceContextType,
@ -22,7 +28,10 @@ export const AgentInstanceContext = createContext<AgentInstanceContextType>(
type AgentChatContextType = Pick< type AgentChatContextType = Pick<
ReturnType<typeof useShowLogSheet>, ReturnType<typeof useShowLogSheet>,
'showLogSheet' 'showLogSheet'
> & { setLastSendLoadingFunc: (loading: boolean, messageId: string) => void }; > & {
setLastSendLoadingFunc: (loading: boolean, messageId: string) => void;
setDerivedMessages: Dispatch<SetStateAction<IMessage[] | undefined>>;
};
export const AgentChatContext = createContext<AgentChatContextType>( export const AgentChatContext = createContext<AgentChatContextType>(
{} as AgentChatContextType, {} as AgentChatContextType,

View file

@ -55,7 +55,7 @@ const FormSheet = ({
<Sheet open={visible} modal={false}> <Sheet open={visible} modal={false}>
<SheetContent <SheetContent
className={cn('top-20 p-0 flex flex-col pb-20', { className={cn('top-20 p-0 flex flex-col pb-20', {
'right-[620px]': chatVisible, 'right-[clamp(0px,34%,620px)]': chatVisible,
})} })}
closeIcon={false} closeIcon={false}
> >

View file

@ -0,0 +1,88 @@
import {
INodeData,
INodeEvent,
MessageEventType,
} from '@/hooks/use-send-message';
import { IMessage } from '@/interfaces/database/chat';
import { useCallback, useMemo, useState } from 'react';
export const useNodeLoading = ({
currentEventListWithoutMessageById,
}: {
currentEventListWithoutMessageById: (messageId: string) => INodeEvent[];
}) => {
const [derivedMessages, setDerivedMessages] = useState<IMessage[]>();
const lastMessageId = useMemo(() => {
return derivedMessages?.[derivedMessages?.length - 1]?.id;
}, [derivedMessages]);
const currentEventListWithoutMessage = useMemo(() => {
if (!lastMessageId) {
return [];
}
return currentEventListWithoutMessageById(lastMessageId);
}, [currentEventListWithoutMessageById, lastMessageId]);
const startedNodeList = useMemo(() => {
const duplicateList = currentEventListWithoutMessage?.filter(
(x) => x.event === MessageEventType.NodeStarted,
) as INodeEvent[];
// Remove duplicate nodes
return duplicateList?.reduce<Array<INodeEvent>>((pre, cur) => {
if (pre.every((x) => x.data.component_id !== cur.data.component_id)) {
pre.push(cur);
}
return pre;
}, []);
}, [currentEventListWithoutMessage]);
const filterFinishedNodeList = useCallback(() => {
const nodeEventList = currentEventListWithoutMessage
.filter(
(x) => x.event === MessageEventType.NodeFinished,
// x.event === MessageEventType.NodeFinished &&
// (x.data as INodeData)?.component_id === componentId,
)
.map((x) => x.data);
return nodeEventList;
}, [currentEventListWithoutMessage]);
const lastNode = useMemo(() => {
if (!startedNodeList) {
return null;
}
return startedNodeList[startedNodeList.length - 1];
}, [startedNodeList]);
const startNodeIds = useMemo(() => {
if (!startedNodeList) {
return [];
}
return startedNodeList.map((x) => x.data.component_id);
}, [startedNodeList]);
const finishNodeIds = useMemo(() => {
if (!lastNode) {
return [];
}
const nodeDataList = filterFinishedNodeList();
const finishNodeIdsTemp = nodeDataList.map(
(x: INodeData) => x.component_id,
);
return Array.from(new Set(finishNodeIdsTemp));
}, [lastNode, filterFinishedNodeList]);
const startButNotFinishedNodeIds = useMemo(() => {
return startNodeIds.filter((x) => !finishNodeIds.includes(x));
}, [finishNodeIds, startNodeIds]);
return {
lastNode,
startButNotFinishedNodeIds,
filterFinishedNodeList,
setDerivedMessages,
};
};

View file

@ -26,7 +26,7 @@ export function LogSheet({
return ( return (
<Sheet open onOpenChange={hideModal} modal={false}> <Sheet open onOpenChange={hideModal} modal={false}>
<SheetContent <SheetContent
className={cn('top-20 right-[620px]')} className={cn('top-20 right-[clamp(0px,34%,620px)]')}
onInteractOutside={(e) => e.preventDefault()} onInteractOutside={(e) => e.preventDefault()}
> >
<SheetHeader> <SheetHeader>