node.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import type { LexicalNode, NodeKey, SerializedLexicalNode } from 'lexical'
  2. import { DecoratorNode } from 'lexical'
  3. import type { WorkflowVariableBlockType } from '../../types'
  4. import WorkflowVariableBlockComponent from './component'
  5. import type { GetVarType } from '../../types'
  6. import type { Var } from '@/app/components/workflow/types'
  7. export type WorkflowNodesMap = WorkflowVariableBlockType['workflowNodesMap']
  8. export type SerializedNode = SerializedLexicalNode & {
  9. variables: string[]
  10. workflowNodesMap: WorkflowNodesMap
  11. getVarType?: GetVarType
  12. environmentVariables?: Var[]
  13. conversationVariables?: Var[]
  14. }
  15. export class WorkflowVariableBlockNode extends DecoratorNode<React.JSX.Element> {
  16. __variables: string[]
  17. __workflowNodesMap: WorkflowNodesMap
  18. __getVarType?: GetVarType
  19. __environmentVariables?: Var[]
  20. __conversationVariables?: Var[]
  21. static getType(): string {
  22. return 'workflow-variable-block'
  23. }
  24. static clone(node: WorkflowVariableBlockNode): WorkflowVariableBlockNode {
  25. return new WorkflowVariableBlockNode(node.__variables, node.__workflowNodesMap, node.__getVarType, node.__key, node.__environmentVariables, node.__conversationVariables)
  26. }
  27. isInline(): boolean {
  28. return true
  29. }
  30. constructor(variables: string[], workflowNodesMap: WorkflowNodesMap, getVarType: any, key?: NodeKey, environmentVariables?: Var[], conversationVariables?: Var[]) {
  31. super(key)
  32. this.__variables = variables
  33. this.__workflowNodesMap = workflowNodesMap
  34. this.__getVarType = getVarType
  35. this.__environmentVariables = environmentVariables
  36. this.__conversationVariables = conversationVariables
  37. }
  38. createDOM(): HTMLElement {
  39. const div = document.createElement('div')
  40. div.classList.add('inline-flex', 'items-center', 'align-middle')
  41. return div
  42. }
  43. updateDOM(): false {
  44. return false
  45. }
  46. decorate(): React.JSX.Element {
  47. return (
  48. <WorkflowVariableBlockComponent
  49. nodeKey={this.getKey()}
  50. variables={this.__variables}
  51. workflowNodesMap={this.__workflowNodesMap}
  52. getVarType={this.__getVarType!}
  53. environmentVariables={this.__environmentVariables}
  54. conversationVariables={this.__conversationVariables}
  55. />
  56. )
  57. }
  58. static importJSON(serializedNode: SerializedNode): WorkflowVariableBlockNode {
  59. const node = $createWorkflowVariableBlockNode(serializedNode.variables, serializedNode.workflowNodesMap, serializedNode.getVarType, serializedNode.environmentVariables, serializedNode.conversationVariables)
  60. return node
  61. }
  62. exportJSON(): SerializedNode {
  63. return {
  64. type: 'workflow-variable-block',
  65. version: 1,
  66. variables: this.getVariables(),
  67. workflowNodesMap: this.getWorkflowNodesMap(),
  68. getVarType: this.getVarType(),
  69. environmentVariables: this.getEnvironmentVariables(),
  70. conversationVariables: this.getConversationVariables(),
  71. }
  72. }
  73. getVariables(): string[] {
  74. const self = this.getLatest()
  75. return self.__variables
  76. }
  77. getWorkflowNodesMap(): WorkflowNodesMap {
  78. const self = this.getLatest()
  79. return self.__workflowNodesMap
  80. }
  81. getVarType(): any {
  82. const self = this.getLatest()
  83. return self.__getVarType
  84. }
  85. getEnvironmentVariables(): any {
  86. const self = this.getLatest()
  87. return self.__environmentVariables
  88. }
  89. getConversationVariables(): any {
  90. const self = this.getLatest()
  91. return self.__conversationVariables
  92. }
  93. getTextContent(): string {
  94. return `{{#${this.getVariables().join('.')}#}}`
  95. }
  96. }
  97. export function $createWorkflowVariableBlockNode(variables: string[], workflowNodesMap: WorkflowNodesMap, getVarType?: GetVarType, environmentVariables?: Var[], conversationVariables?: Var[]): WorkflowVariableBlockNode {
  98. return new WorkflowVariableBlockNode(variables, workflowNodesMap, getVarType, undefined, environmentVariables, conversationVariables)
  99. }
  100. export function $isWorkflowVariableBlockNode(
  101. node: WorkflowVariableBlockNode | LexicalNode | null | undefined,
  102. ): node is WorkflowVariableBlockNode {
  103. return node instanceof WorkflowVariableBlockNode
  104. }