You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.tsx 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import {
  2. Background,
  3. ConnectionMode,
  4. NodeTypes,
  5. ReactFlow,
  6. } from '@xyflow/react';
  7. import '@xyflow/react/dist/style.css';
  8. import { useEffect } from 'react';
  9. import { ChatSheet } from '../chat/chat-sheet';
  10. import {
  11. AgentChatContext,
  12. AgentChatLogContext,
  13. AgentInstanceContext,
  14. } from '../context';
  15. import FormSheet from '../form-sheet/next';
  16. import {
  17. useHandleDrop,
  18. useSelectCanvasData,
  19. useValidateConnection,
  20. } from '../hooks';
  21. import { useAddNode } from '../hooks/use-add-node';
  22. import { useBeforeDelete } from '../hooks/use-before-delete';
  23. import { useCacheChatLog } from '../hooks/use-cache-chat-log';
  24. import { useShowDrawer, useShowLogSheet } from '../hooks/use-show-drawer';
  25. import { LogSheet } from '../log-sheet';
  26. import RunSheet from '../run-sheet';
  27. import { ButtonEdge } from './edge';
  28. import styles from './index.less';
  29. import { RagNode } from './node';
  30. import { AgentNode } from './node/agent-node';
  31. import { BeginNode } from './node/begin-node';
  32. import { CategorizeNode } from './node/categorize-node';
  33. import { EmailNode } from './node/email-node';
  34. import { GenerateNode } from './node/generate-node';
  35. import { InvokeNode } from './node/invoke-node';
  36. import { IterationNode, IterationStartNode } from './node/iteration-node';
  37. import { KeywordNode } from './node/keyword-node';
  38. import { LogicNode } from './node/logic-node';
  39. import { MessageNode } from './node/message-node';
  40. import NoteNode from './node/note-node';
  41. import { RelevantNode } from './node/relevant-node';
  42. import { RetrievalNode } from './node/retrieval-node';
  43. import { RewriteNode } from './node/rewrite-node';
  44. import { SwitchNode } from './node/switch-node';
  45. import { TemplateNode } from './node/template-node';
  46. import { ToolNode } from './node/tool-node';
  47. const nodeTypes: NodeTypes = {
  48. ragNode: RagNode,
  49. categorizeNode: CategorizeNode,
  50. beginNode: BeginNode,
  51. relevantNode: RelevantNode,
  52. logicNode: LogicNode,
  53. noteNode: NoteNode,
  54. switchNode: SwitchNode,
  55. generateNode: GenerateNode,
  56. retrievalNode: RetrievalNode,
  57. messageNode: MessageNode,
  58. rewriteNode: RewriteNode,
  59. keywordNode: KeywordNode,
  60. invokeNode: InvokeNode,
  61. templateNode: TemplateNode,
  62. emailNode: EmailNode,
  63. group: IterationNode,
  64. iterationStartNode: IterationStartNode,
  65. agentNode: AgentNode,
  66. toolNode: ToolNode,
  67. };
  68. const edgeTypes = {
  69. buttonEdge: ButtonEdge,
  70. };
  71. interface IProps {
  72. drawerVisible: boolean;
  73. hideDrawer(): void;
  74. }
  75. function AgentCanvas({ drawerVisible, hideDrawer }: IProps) {
  76. const {
  77. nodes,
  78. edges,
  79. onConnect,
  80. onEdgesChange,
  81. onNodesChange,
  82. onSelectionChange,
  83. } = useSelectCanvasData();
  84. const isValidConnection = useValidateConnection();
  85. const { onDrop, onDragOver, setReactFlowInstance, reactFlowInstance } =
  86. useHandleDrop();
  87. const {
  88. onNodeClick,
  89. onPaneClick,
  90. clickedNode,
  91. formDrawerVisible,
  92. hideFormDrawer,
  93. singleDebugDrawerVisible,
  94. hideSingleDebugDrawer,
  95. showSingleDebugDrawer,
  96. chatVisible,
  97. runVisible,
  98. hideRunOrChatDrawer,
  99. showChatModal,
  100. } = useShowDrawer({
  101. drawerVisible,
  102. hideDrawer,
  103. });
  104. const {
  105. addEventList,
  106. setCurrentMessageId,
  107. currentEventListWithoutMessage,
  108. clearEventList,
  109. } = useCacheChatLog();
  110. const { showLogSheet, logSheetVisible, hideLogSheet } = useShowLogSheet({
  111. setCurrentMessageId,
  112. });
  113. const { handleBeforeDelete } = useBeforeDelete();
  114. const { addCanvasNode } = useAddNode(reactFlowInstance);
  115. useEffect(() => {
  116. if (!chatVisible) {
  117. clearEventList();
  118. }
  119. }, [chatVisible, clearEventList]);
  120. return (
  121. <div className={styles.canvasWrapper}>
  122. <svg
  123. xmlns="http://www.w3.org/2000/svg"
  124. style={{ position: 'absolute', top: 10, left: 0 }}
  125. >
  126. <defs>
  127. <marker
  128. fill="rgb(157 149 225)"
  129. id="logo"
  130. viewBox="0 0 40 40"
  131. refX="8"
  132. refY="5"
  133. markerUnits="strokeWidth"
  134. markerWidth="20"
  135. markerHeight="20"
  136. orient="auto-start-reverse"
  137. >
  138. <path d="M 0 0 L 10 5 L 0 10 z" />
  139. </marker>
  140. </defs>
  141. </svg>
  142. <AgentInstanceContext.Provider value={{ addCanvasNode }}>
  143. <ReactFlow
  144. connectionMode={ConnectionMode.Loose}
  145. nodes={nodes}
  146. onNodesChange={onNodesChange}
  147. edges={edges}
  148. onEdgesChange={onEdgesChange}
  149. fitView
  150. onConnect={onConnect}
  151. nodeTypes={nodeTypes}
  152. edgeTypes={edgeTypes}
  153. onDrop={onDrop}
  154. onDragOver={onDragOver}
  155. onNodeClick={onNodeClick}
  156. onPaneClick={onPaneClick}
  157. onInit={setReactFlowInstance}
  158. onSelectionChange={onSelectionChange}
  159. nodeOrigin={[0.5, 0]}
  160. isValidConnection={isValidConnection}
  161. defaultEdgeOptions={{
  162. type: 'buttonEdge',
  163. markerEnd: 'logo',
  164. style: {
  165. strokeWidth: 2,
  166. stroke: 'rgb(202 197 245)',
  167. },
  168. zIndex: 1001, // https://github.com/xyflow/xyflow/discussions/3498
  169. }}
  170. deleteKeyCode={['Delete', 'Backspace']}
  171. onBeforeDelete={handleBeforeDelete}
  172. >
  173. <Background />
  174. </ReactFlow>
  175. </AgentInstanceContext.Provider>
  176. {formDrawerVisible && (
  177. <AgentInstanceContext.Provider value={{ addCanvasNode }}>
  178. <FormSheet
  179. node={clickedNode}
  180. visible={formDrawerVisible}
  181. hideModal={hideFormDrawer}
  182. singleDebugDrawerVisible={singleDebugDrawerVisible}
  183. hideSingleDebugDrawer={hideSingleDebugDrawer}
  184. showSingleDebugDrawer={showSingleDebugDrawer}
  185. ></FormSheet>
  186. </AgentInstanceContext.Provider>
  187. )}
  188. {chatVisible && (
  189. <AgentChatContext.Provider value={{ showLogSheet }}>
  190. <AgentChatLogContext.Provider
  191. value={{ addEventList, setCurrentMessageId }}
  192. >
  193. <ChatSheet hideModal={hideRunOrChatDrawer}></ChatSheet>
  194. </AgentChatLogContext.Provider>
  195. </AgentChatContext.Provider>
  196. )}
  197. {runVisible && (
  198. <RunSheet
  199. hideModal={hideRunOrChatDrawer}
  200. showModal={showChatModal}
  201. ></RunSheet>
  202. )}
  203. {logSheetVisible && (
  204. <LogSheet
  205. hideModal={hideLogSheet}
  206. currentEventListWithoutMessage={currentEventListWithoutMessage}
  207. ></LogSheet>
  208. )}
  209. </div>
  210. );
  211. }
  212. export default AgentCanvas;