get.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. import { Component, markRaw } from 'vue';
  2. import { SparklesOutline, DocumentTextOutline } from '@vicons/ionicons5';
  3. import { LLMPin, AssistPin, EndPin, StartPin, KnowledgeWeb, KnowledgeDoc } from '@/views/flow/pin';
  4. import { AssistNode, End, LLMNode, Start } from '@/views/flow/node';
  5. import { renderPropsIcon } from '@/utils';
  6. import type { FlowExportObject } from '@vue-flow/core/dist/types/flow';
  7. export enum TypeEnum {
  8. Start = 'Start',
  9. End = 'End',
  10. Assist = 'Assist',
  11. Http = 'Http',
  12. LLM = 'LLM',
  13. }
  14. export enum PluginEnum {
  15. Input = 'Input',
  16. ExecuteCode = 'ExecuteCode',
  17. Knowledge_Web = 'From Web',
  18. Knowledge_Doc = 'From Doc',
  19. }
  20. enum ColEnum {
  21. Base = 'Base',
  22. Node = 'Node',
  23. Ai = 'Ai',
  24. SendMessage = 'Send Message',
  25. Knowledge = 'Knowledge List',
  26. }
  27. // 节点类型定义,注意非节点不要定义此变量
  28. export const nodeTypes = {
  29. [TypeEnum.Start]: markRaw(Start),
  30. [TypeEnum.End]: markRaw(End),
  31. [TypeEnum.Assist]: markRaw(AssistNode),
  32. [TypeEnum.LLM]: markRaw(LLMNode),
  33. };
  34. export interface Pin {
  35. type?: TypeEnum | PluginEnum;
  36. label?: string;
  37. des?: string;
  38. component?: Component;
  39. col?: ColEnum;
  40. isNode?: boolean;
  41. data?: any;
  42. }
  43. const nodePins: Pin[] = [
  44. {
  45. type: TypeEnum.LLM,
  46. component: LLMPin,
  47. col: ColEnum.Node,
  48. isNode: true,
  49. des: '利用LLM进行文本消息问答',
  50. data: {
  51. model: 'gpt-4',
  52. temperate: 0.7,
  53. },
  54. },
  55. {
  56. type: TypeEnum.Assist,
  57. component: AssistPin,
  58. col: ColEnum.Node,
  59. isNode: true,
  60. des: '利用LLM进行文本消息问答',
  61. },
  62. {
  63. type: TypeEnum.Http,
  64. component: StartPin,
  65. col: ColEnum.Node,
  66. isNode: true,
  67. des: '发送HTTP请求',
  68. },
  69. {
  70. type: TypeEnum.Http,
  71. component: StartPin,
  72. col: ColEnum.Node,
  73. isNode: true,
  74. des: '发送HTTP请求',
  75. },
  76. { type: TypeEnum.End, component: EndPin, col: ColEnum.Base, isNode: true },
  77. { type: TypeEnum.Start, component: StartPin, col: ColEnum.Base, isNode: true },
  78. ];
  79. const pluginPins: Pin[] = [
  80. {
  81. type: PluginEnum.Input,
  82. label: 'Input Node',
  83. component: StartPin,
  84. col: ColEnum.SendMessage,
  85. isNode: false,
  86. },
  87. {
  88. type: PluginEnum.ExecuteCode,
  89. label: 'ExecuteCode Node',
  90. component: StartPin,
  91. col: ColEnum.Ai,
  92. isNode: false,
  93. },
  94. {
  95. type: PluginEnum.Knowledge_Web,
  96. label: 'Knowledge From Web',
  97. component: KnowledgeWeb,
  98. col: ColEnum.Knowledge,
  99. isNode: false,
  100. },
  101. {
  102. type: PluginEnum.Knowledge_Doc,
  103. label: 'Knowledge From Doc',
  104. component: KnowledgeDoc,
  105. col: ColEnum.Knowledge,
  106. isNode: false,
  107. },
  108. ];
  109. export function getPin(type: string | undefined, isNode: boolean): Pin | undefined {
  110. if (type === undefined) {
  111. return;
  112. }
  113. const pins = isNode ? nodePins : pluginPins;
  114. const list = pins.filter((i) => i.type?.toLowerCase() === type.toLowerCase());
  115. return list.length > 0 ? list[0] : undefined;
  116. }
  117. export const collapses = (
  118. isNode: boolean
  119. ): {
  120. key: string;
  121. value: Pin[];
  122. }[] => {
  123. const pins = isNode ? nodePins : pluginPins;
  124. const transformedArray = pins.reduce((result, pin) => {
  125. const { col } = pin;
  126. if (col == undefined || col == ColEnum.Base) {
  127. return result;
  128. }
  129. if (col in result) {
  130. result[col].push(pin);
  131. } else {
  132. result[col] = [pin];
  133. }
  134. return result;
  135. }, {});
  136. // @ts-ignore
  137. return Object.entries(transformedArray).map(([key, value]) => ({
  138. key,
  139. value,
  140. }));
  141. };
  142. export function getDatas(obj: FlowExportObject): any[] {
  143. const data: any[] = [];
  144. obj.edges.forEach((i) => {
  145. data.push({
  146. id: i.id,
  147. source: i.source,
  148. target: i.target,
  149. type: i.type,
  150. animated: i.animated,
  151. data: i.data,
  152. label: i.label,
  153. });
  154. });
  155. obj.nodes.forEach((i) => {
  156. data.push({
  157. id: i.id,
  158. type: i.type,
  159. position: i.position,
  160. data: i.data,
  161. label: i.label,
  162. });
  163. });
  164. return data;
  165. }
  166. const icons = [
  167. {
  168. type: TypeEnum.LLM,
  169. icon: renderPropsIcon(SparklesOutline, { color: '#8a2be2', size: '15px' }),
  170. },
  171. {
  172. type: TypeEnum.Assist,
  173. icon: renderPropsIcon(SparklesOutline, { color: '#8a2be2', size: '15px' }),
  174. },
  175. {
  176. type: TypeEnum.Http,
  177. icon: renderPropsIcon(SparklesOutline, { color: '#8a2be2', size: '15px' }),
  178. },
  179. {
  180. type: PluginEnum.Input,
  181. icon: renderPropsIcon(SparklesOutline, { color: '#8a2be2', size: '15px' }),
  182. },
  183. {
  184. type: PluginEnum.ExecuteCode,
  185. icon: renderPropsIcon(SparklesOutline, { color: '#8a2be2', size: '15px' }),
  186. },
  187. {
  188. type: PluginEnum.Knowledge_Web,
  189. icon: renderPropsIcon(DocumentTextOutline, { size: '15px' }),
  190. },
  191. {
  192. type: PluginEnum.Knowledge_Doc,
  193. icon: renderPropsIcon(DocumentTextOutline, { size: '15px' }),
  194. },
  195. ];
  196. export function renderNodeIcon(type: string) {
  197. const list = icons.filter((i) => i.type == type);
  198. return list.length > 0 ? list[0].icon : undefined;
  199. }
  200. export function renderNodeDes(type: string) {
  201. const list = nodePins.filter((i) => i.type == type);
  202. return list.length > 0 ? list[0].des : undefined;
  203. }