constants.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. import type { Var } from './types'
  2. import { BlockEnum, VarType } from './types'
  3. import StartNodeDefault from './nodes/start/default'
  4. import AnswerDefault from './nodes/answer/default'
  5. import LLMDefault from './nodes/llm/default'
  6. import KnowledgeRetrievalDefault from './nodes/knowledge-retrieval/default'
  7. import QuestionClassifierDefault from './nodes/question-classifier/default'
  8. import IfElseDefault from './nodes/if-else/default'
  9. import CodeDefault from './nodes/code/default'
  10. import TemplateTransformDefault from './nodes/template-transform/default'
  11. import HttpRequestDefault from './nodes/http/default'
  12. import ParameterExtractorDefault from './nodes/parameter-extractor/default'
  13. import ToolDefault from './nodes/tool/default'
  14. import VariableAssignerDefault from './nodes/variable-assigner/default'
  15. import AssignerDefault from './nodes/assigner/default'
  16. import EndNodeDefault from './nodes/end/default'
  17. import IterationDefault from './nodes/iteration/default'
  18. import DocExtractorDefault from './nodes/document-extractor/default'
  19. import ListFilterDefault from './nodes/list-operator/default'
  20. import IterationStartDefault from './nodes/iteration-start/default'
  21. type NodesExtraData = {
  22. author: string
  23. about: string
  24. availablePrevNodes: BlockEnum[]
  25. availableNextNodes: BlockEnum[]
  26. getAvailablePrevNodes: (isChatMode: boolean) => BlockEnum[]
  27. getAvailableNextNodes: (isChatMode: boolean) => BlockEnum[]
  28. checkValid: any
  29. }
  30. export const NODES_EXTRA_DATA: Record<BlockEnum, NodesExtraData> = {
  31. [BlockEnum.Start]: {
  32. author: 'Dify',
  33. about: '',
  34. availablePrevNodes: [],
  35. availableNextNodes: [],
  36. getAvailablePrevNodes: StartNodeDefault.getAvailablePrevNodes,
  37. getAvailableNextNodes: StartNodeDefault.getAvailableNextNodes,
  38. checkValid: StartNodeDefault.checkValid,
  39. },
  40. [BlockEnum.End]: {
  41. author: 'Dify',
  42. about: '',
  43. availablePrevNodes: [],
  44. availableNextNodes: [],
  45. getAvailablePrevNodes: EndNodeDefault.getAvailablePrevNodes,
  46. getAvailableNextNodes: EndNodeDefault.getAvailableNextNodes,
  47. checkValid: EndNodeDefault.checkValid,
  48. },
  49. [BlockEnum.Answer]: {
  50. author: 'Dify',
  51. about: '',
  52. availablePrevNodes: [],
  53. availableNextNodes: [],
  54. getAvailablePrevNodes: AnswerDefault.getAvailablePrevNodes,
  55. getAvailableNextNodes: AnswerDefault.getAvailableNextNodes,
  56. checkValid: AnswerDefault.checkValid,
  57. },
  58. [BlockEnum.LLM]: {
  59. author: 'Dify',
  60. about: '',
  61. availablePrevNodes: [],
  62. availableNextNodes: [],
  63. getAvailablePrevNodes: LLMDefault.getAvailablePrevNodes,
  64. getAvailableNextNodes: LLMDefault.getAvailableNextNodes,
  65. checkValid: LLMDefault.checkValid,
  66. },
  67. [BlockEnum.KnowledgeRetrieval]: {
  68. author: 'Dify',
  69. about: '',
  70. availablePrevNodes: [],
  71. availableNextNodes: [],
  72. getAvailablePrevNodes: KnowledgeRetrievalDefault.getAvailablePrevNodes,
  73. getAvailableNextNodes: KnowledgeRetrievalDefault.getAvailableNextNodes,
  74. checkValid: KnowledgeRetrievalDefault.checkValid,
  75. },
  76. [BlockEnum.IfElse]: {
  77. author: 'Dify',
  78. about: '',
  79. availablePrevNodes: [],
  80. availableNextNodes: [],
  81. getAvailablePrevNodes: IfElseDefault.getAvailablePrevNodes,
  82. getAvailableNextNodes: IfElseDefault.getAvailableNextNodes,
  83. checkValid: IfElseDefault.checkValid,
  84. },
  85. [BlockEnum.Iteration]: {
  86. author: 'Dify',
  87. about: '',
  88. availablePrevNodes: [],
  89. availableNextNodes: [],
  90. getAvailablePrevNodes: IterationDefault.getAvailablePrevNodes,
  91. getAvailableNextNodes: IterationDefault.getAvailableNextNodes,
  92. checkValid: IterationDefault.checkValid,
  93. },
  94. [BlockEnum.IterationStart]: {
  95. author: 'Dify',
  96. about: '',
  97. availablePrevNodes: [],
  98. availableNextNodes: [],
  99. getAvailablePrevNodes: IterationStartDefault.getAvailablePrevNodes,
  100. getAvailableNextNodes: IterationStartDefault.getAvailableNextNodes,
  101. checkValid: IterationStartDefault.checkValid,
  102. },
  103. [BlockEnum.Code]: {
  104. author: 'Dify',
  105. about: '',
  106. availablePrevNodes: [],
  107. availableNextNodes: [],
  108. getAvailablePrevNodes: CodeDefault.getAvailablePrevNodes,
  109. getAvailableNextNodes: CodeDefault.getAvailableNextNodes,
  110. checkValid: CodeDefault.checkValid,
  111. },
  112. [BlockEnum.TemplateTransform]: {
  113. author: 'Dify',
  114. about: '',
  115. availablePrevNodes: [],
  116. availableNextNodes: [],
  117. getAvailablePrevNodes: TemplateTransformDefault.getAvailablePrevNodes,
  118. getAvailableNextNodes: TemplateTransformDefault.getAvailableNextNodes,
  119. checkValid: TemplateTransformDefault.checkValid,
  120. },
  121. [BlockEnum.QuestionClassifier]: {
  122. author: 'Dify',
  123. about: '',
  124. availablePrevNodes: [],
  125. availableNextNodes: [],
  126. getAvailablePrevNodes: QuestionClassifierDefault.getAvailablePrevNodes,
  127. getAvailableNextNodes: QuestionClassifierDefault.getAvailableNextNodes,
  128. checkValid: QuestionClassifierDefault.checkValid,
  129. },
  130. [BlockEnum.HttpRequest]: {
  131. author: 'Dify',
  132. about: '',
  133. availablePrevNodes: [],
  134. availableNextNodes: [],
  135. getAvailablePrevNodes: HttpRequestDefault.getAvailablePrevNodes,
  136. getAvailableNextNodes: HttpRequestDefault.getAvailableNextNodes,
  137. checkValid: HttpRequestDefault.checkValid,
  138. },
  139. [BlockEnum.VariableAssigner]: {
  140. author: 'Dify',
  141. about: '',
  142. availablePrevNodes: [],
  143. availableNextNodes: [],
  144. getAvailablePrevNodes: VariableAssignerDefault.getAvailablePrevNodes,
  145. getAvailableNextNodes: VariableAssignerDefault.getAvailableNextNodes,
  146. checkValid: VariableAssignerDefault.checkValid,
  147. },
  148. [BlockEnum.Assigner]: {
  149. author: 'Dify',
  150. about: '',
  151. availablePrevNodes: [],
  152. availableNextNodes: [],
  153. getAvailablePrevNodes: AssignerDefault.getAvailablePrevNodes,
  154. getAvailableNextNodes: AssignerDefault.getAvailableNextNodes,
  155. checkValid: AssignerDefault.checkValid,
  156. },
  157. [BlockEnum.VariableAggregator]: {
  158. author: 'Dify',
  159. about: '',
  160. availablePrevNodes: [],
  161. availableNextNodes: [],
  162. getAvailablePrevNodes: VariableAssignerDefault.getAvailablePrevNodes,
  163. getAvailableNextNodes: VariableAssignerDefault.getAvailableNextNodes,
  164. checkValid: VariableAssignerDefault.checkValid,
  165. },
  166. [BlockEnum.ParameterExtractor]: {
  167. author: 'Dify',
  168. about: '',
  169. availablePrevNodes: [],
  170. availableNextNodes: [],
  171. getAvailablePrevNodes: ParameterExtractorDefault.getAvailablePrevNodes,
  172. getAvailableNextNodes: ParameterExtractorDefault.getAvailableNextNodes,
  173. checkValid: ParameterExtractorDefault.checkValid,
  174. },
  175. [BlockEnum.Tool]: {
  176. author: 'Dify',
  177. about: '',
  178. availablePrevNodes: [],
  179. availableNextNodes: [],
  180. getAvailablePrevNodes: ToolDefault.getAvailablePrevNodes,
  181. getAvailableNextNodes: ToolDefault.getAvailableNextNodes,
  182. checkValid: ToolDefault.checkValid,
  183. },
  184. [BlockEnum.DocExtractor]: {
  185. author: 'Dify',
  186. about: '',
  187. availablePrevNodes: [],
  188. availableNextNodes: [],
  189. getAvailablePrevNodes: DocExtractorDefault.getAvailablePrevNodes,
  190. getAvailableNextNodes: DocExtractorDefault.getAvailableNextNodes,
  191. checkValid: DocExtractorDefault.checkValid,
  192. },
  193. [BlockEnum.ListFilter]: {
  194. author: 'Dify',
  195. about: '',
  196. availablePrevNodes: [],
  197. availableNextNodes: [],
  198. getAvailablePrevNodes: ListFilterDefault.getAvailablePrevNodes,
  199. getAvailableNextNodes: ListFilterDefault.getAvailableNextNodes,
  200. checkValid: ListFilterDefault.checkValid,
  201. },
  202. }
  203. export const ALL_CHAT_AVAILABLE_BLOCKS = Object.keys(NODES_EXTRA_DATA).filter(key => key !== BlockEnum.End && key !== BlockEnum.Start) as BlockEnum[]
  204. export const ALL_COMPLETION_AVAILABLE_BLOCKS = Object.keys(NODES_EXTRA_DATA).filter(key => key !== BlockEnum.Answer && key !== BlockEnum.Start) as BlockEnum[]
  205. export const NODES_INITIAL_DATA = {
  206. [BlockEnum.Start]: {
  207. type: BlockEnum.Start,
  208. title: '',
  209. desc: '',
  210. ...StartNodeDefault.defaultValue,
  211. },
  212. [BlockEnum.End]: {
  213. type: BlockEnum.End,
  214. title: '',
  215. desc: '',
  216. ...EndNodeDefault.defaultValue,
  217. },
  218. [BlockEnum.Answer]: {
  219. type: BlockEnum.Answer,
  220. title: '',
  221. desc: '',
  222. ...AnswerDefault.defaultValue,
  223. },
  224. [BlockEnum.LLM]: {
  225. type: BlockEnum.LLM,
  226. title: '',
  227. desc: '',
  228. variables: [],
  229. ...LLMDefault.defaultValue,
  230. },
  231. [BlockEnum.KnowledgeRetrieval]: {
  232. type: BlockEnum.KnowledgeRetrieval,
  233. title: '',
  234. desc: '',
  235. query_variable_selector: [],
  236. dataset_ids: [],
  237. retrieval_mode: 'single',
  238. ...KnowledgeRetrievalDefault.defaultValue,
  239. },
  240. [BlockEnum.IfElse]: {
  241. type: BlockEnum.IfElse,
  242. title: '',
  243. desc: '',
  244. ...IfElseDefault.defaultValue,
  245. },
  246. [BlockEnum.Iteration]: {
  247. type: BlockEnum.Iteration,
  248. title: '',
  249. desc: '',
  250. ...IterationDefault.defaultValue,
  251. },
  252. [BlockEnum.IterationStart]: {
  253. type: BlockEnum.IterationStart,
  254. title: '',
  255. desc: '',
  256. ...IterationStartDefault.defaultValue,
  257. },
  258. [BlockEnum.Code]: {
  259. type: BlockEnum.Code,
  260. title: '',
  261. desc: '',
  262. variables: [],
  263. code_language: 'python3',
  264. code: '',
  265. outputs: [],
  266. ...CodeDefault.defaultValue,
  267. },
  268. [BlockEnum.TemplateTransform]: {
  269. type: BlockEnum.TemplateTransform,
  270. title: '',
  271. desc: '',
  272. variables: [],
  273. template: '',
  274. ...TemplateTransformDefault.defaultValue,
  275. },
  276. [BlockEnum.QuestionClassifier]: {
  277. type: BlockEnum.QuestionClassifier,
  278. title: '',
  279. desc: '',
  280. query_variable_selector: [],
  281. topics: [],
  282. ...QuestionClassifierDefault.defaultValue,
  283. },
  284. [BlockEnum.HttpRequest]: {
  285. type: BlockEnum.HttpRequest,
  286. title: '',
  287. desc: '',
  288. variables: [],
  289. ...HttpRequestDefault.defaultValue,
  290. },
  291. [BlockEnum.ParameterExtractor]: {
  292. type: BlockEnum.ParameterExtractor,
  293. title: '',
  294. desc: '',
  295. variables: [],
  296. ...ParameterExtractorDefault.defaultValue,
  297. },
  298. [BlockEnum.VariableAssigner]: {
  299. type: BlockEnum.VariableAssigner,
  300. title: '',
  301. desc: '',
  302. variables: [],
  303. output_type: '',
  304. ...VariableAssignerDefault.defaultValue,
  305. },
  306. [BlockEnum.VariableAggregator]: {
  307. type: BlockEnum.VariableAggregator,
  308. title: '',
  309. desc: '',
  310. variables: [],
  311. output_type: '',
  312. ...VariableAssignerDefault.defaultValue,
  313. },
  314. [BlockEnum.Assigner]: {
  315. type: BlockEnum.Assigner,
  316. title: '',
  317. desc: '',
  318. ...AssignerDefault.defaultValue,
  319. },
  320. [BlockEnum.Tool]: {
  321. type: BlockEnum.Tool,
  322. title: '',
  323. desc: '',
  324. ...ToolDefault.defaultValue,
  325. },
  326. [BlockEnum.DocExtractor]: {
  327. type: BlockEnum.DocExtractor,
  328. title: '',
  329. desc: '',
  330. ...DocExtractorDefault.defaultValue,
  331. },
  332. [BlockEnum.ListFilter]: {
  333. type: BlockEnum.ListFilter,
  334. title: '',
  335. desc: '',
  336. ...ListFilterDefault.defaultValue,
  337. },
  338. }
  339. export const MAX_ITERATION_PARALLEL_NUM = 10
  340. export const MIN_ITERATION_PARALLEL_NUM = 1
  341. export const DEFAULT_ITER_TIMES = 1
  342. export const NODE_WIDTH = 240
  343. export const X_OFFSET = 60
  344. export const NODE_WIDTH_X_OFFSET = NODE_WIDTH + X_OFFSET
  345. export const Y_OFFSET = 39
  346. export const MAX_TREE_DEPTH = 50
  347. export const START_INITIAL_POSITION = { x: 80, y: 282 }
  348. export const AUTO_LAYOUT_OFFSET = {
  349. x: -42,
  350. y: 243,
  351. }
  352. export const ITERATION_NODE_Z_INDEX = 1
  353. export const ITERATION_CHILDREN_Z_INDEX = 1002
  354. export const ITERATION_PADDING = {
  355. top: 65,
  356. right: 16,
  357. bottom: 20,
  358. left: 16,
  359. }
  360. export const PARALLEL_LIMIT = 10
  361. export const PARALLEL_DEPTH_LIMIT = 3
  362. export const RETRIEVAL_OUTPUT_STRUCT = `{
  363. "content": "",
  364. "title": "",
  365. "url": "",
  366. "icon": "",
  367. "metadata": {
  368. "dataset_id": "",
  369. "dataset_name": "",
  370. "document_id": [],
  371. "document_name": "",
  372. "document_data_source_type": "",
  373. "segment_id": "",
  374. "segment_position": "",
  375. "segment_word_count": "",
  376. "segment_hit_count": "",
  377. "segment_index_node_hash": "",
  378. "score": ""
  379. }
  380. }`
  381. export const SUPPORT_OUTPUT_VARS_NODE = [
  382. BlockEnum.Start, BlockEnum.LLM, BlockEnum.KnowledgeRetrieval, BlockEnum.Code, BlockEnum.TemplateTransform,
  383. BlockEnum.HttpRequest, BlockEnum.Tool, BlockEnum.VariableAssigner, BlockEnum.VariableAggregator, BlockEnum.QuestionClassifier,
  384. BlockEnum.ParameterExtractor, BlockEnum.Iteration,
  385. BlockEnum.DocExtractor, BlockEnum.ListFilter,
  386. ]
  387. export const LLM_OUTPUT_STRUCT: Var[] = [
  388. {
  389. variable: 'text',
  390. type: VarType.string,
  391. },
  392. ]
  393. export const KNOWLEDGE_RETRIEVAL_OUTPUT_STRUCT: Var[] = [
  394. {
  395. variable: 'result',
  396. type: VarType.arrayObject,
  397. },
  398. ]
  399. export const TEMPLATE_TRANSFORM_OUTPUT_STRUCT: Var[] = [
  400. {
  401. variable: 'output',
  402. type: VarType.string,
  403. },
  404. ]
  405. export const QUESTION_CLASSIFIER_OUTPUT_STRUCT = [
  406. {
  407. variable: 'class_name',
  408. type: VarType.string,
  409. },
  410. ]
  411. export const HTTP_REQUEST_OUTPUT_STRUCT: Var[] = [
  412. {
  413. variable: 'body',
  414. type: VarType.string,
  415. },
  416. {
  417. variable: 'status_code',
  418. type: VarType.number,
  419. },
  420. {
  421. variable: 'headers',
  422. type: VarType.object,
  423. },
  424. {
  425. variable: 'files',
  426. type: VarType.arrayFile,
  427. },
  428. ]
  429. export const TOOL_OUTPUT_STRUCT: Var[] = [
  430. {
  431. variable: 'text',
  432. type: VarType.string,
  433. },
  434. {
  435. variable: 'files',
  436. type: VarType.arrayFile,
  437. },
  438. {
  439. variable: 'json',
  440. type: VarType.arrayObject,
  441. },
  442. ]
  443. export const PARAMETER_EXTRACTOR_COMMON_STRUCT: Var[] = [
  444. {
  445. variable: '__is_success',
  446. type: VarType.number,
  447. },
  448. {
  449. variable: '__reason',
  450. type: VarType.string,
  451. },
  452. ]
  453. export const FILE_STRUCT: Var[] = [
  454. {
  455. variable: 'name',
  456. type: VarType.string,
  457. },
  458. {
  459. variable: 'size',
  460. type: VarType.number,
  461. },
  462. {
  463. variable: 'type',
  464. type: VarType.string,
  465. },
  466. {
  467. variable: 'extension',
  468. type: VarType.string,
  469. },
  470. {
  471. variable: 'mime_type',
  472. type: VarType.string,
  473. },
  474. {
  475. variable: 'transfer_method',
  476. type: VarType.string,
  477. },
  478. {
  479. variable: 'url',
  480. type: VarType.string,
  481. },
  482. ]
  483. export const DEFAULT_FILE_UPLOAD_SETTING = {
  484. allowed_file_upload_methods: ['local_file', 'remote_url'],
  485. max_length: 5,
  486. allowed_file_types: ['image'],
  487. allowed_file_extensions: [],
  488. }
  489. export const WORKFLOW_DATA_UPDATE = 'WORKFLOW_DATA_UPDATE'
  490. export const CUSTOM_NODE = 'custom'
  491. export const CUSTOM_EDGE = 'custom'
  492. export const DSL_EXPORT_CHECK = 'DSL_EXPORT_CHECK'