config-prompt-item.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useEffect, useState } from 'react'
  4. import { uniqueId } from 'lodash-es'
  5. import { useTranslation } from 'react-i18next'
  6. import { RiQuestionLine } from '@remixicon/react'
  7. import type { PromptItem, Variable } from '../../../types'
  8. import { EditionType } from '../../../types'
  9. import Editor from '@/app/components/workflow/nodes/_base/components/prompt/editor'
  10. import TypeSelector from '@/app/components/workflow/nodes/_base/components/selector'
  11. import TooltipPlus from '@/app/components/base/tooltip-plus'
  12. import { PromptRole } from '@/models/debug'
  13. const i18nPrefix = 'workflow.nodes.llm'
  14. type Props = {
  15. className?: string
  16. headerClassName?: string
  17. canNotChooseSystemRole?: boolean
  18. readOnly: boolean
  19. id: string
  20. canRemove: boolean
  21. isChatModel: boolean
  22. isChatApp: boolean
  23. payload: PromptItem
  24. handleChatModeMessageRoleChange: (role: PromptRole) => void
  25. onPromptChange: (p: string) => void
  26. onEditionTypeChange: (editionType: EditionType) => void
  27. onRemove: () => void
  28. isShowContext: boolean
  29. hasSetBlockStatus: {
  30. context: boolean
  31. history: boolean
  32. query: boolean
  33. }
  34. availableVars: any
  35. availableNodes: any
  36. varList: Variable[]
  37. handleAddVariable: (payload: any) => void
  38. }
  39. const roleOptions = [
  40. {
  41. label: 'system',
  42. value: PromptRole.system,
  43. },
  44. {
  45. label: 'user',
  46. value: PromptRole.user,
  47. },
  48. {
  49. label: 'assistant',
  50. value: PromptRole.assistant,
  51. },
  52. ]
  53. const roleOptionsWithoutSystemRole = roleOptions.filter(item => item.value !== PromptRole.system)
  54. const ConfigPromptItem: FC<Props> = ({
  55. className,
  56. headerClassName,
  57. canNotChooseSystemRole,
  58. readOnly,
  59. id,
  60. canRemove,
  61. handleChatModeMessageRoleChange,
  62. isChatModel,
  63. isChatApp,
  64. payload,
  65. onPromptChange,
  66. onEditionTypeChange,
  67. onRemove,
  68. isShowContext,
  69. hasSetBlockStatus,
  70. availableVars,
  71. availableNodes,
  72. varList,
  73. handleAddVariable,
  74. }) => {
  75. const { t } = useTranslation()
  76. const [instanceId, setInstanceId] = useState(uniqueId())
  77. useEffect(() => {
  78. setInstanceId(`${id}-${uniqueId()}`)
  79. }, [id])
  80. return (
  81. <Editor
  82. className={className}
  83. headerClassName={headerClassName}
  84. instanceId={instanceId}
  85. key={instanceId}
  86. title={
  87. <div className='relative left-1 flex items-center'>
  88. {payload.role === PromptRole.system
  89. ? (<div className='relative left-[-4px] text-xs font-semibold text-gray-700 uppercase'>
  90. SYSTEM
  91. </div>)
  92. : (
  93. <TypeSelector
  94. value={payload.role as string}
  95. allOptions={roleOptions}
  96. options={canNotChooseSystemRole ? roleOptionsWithoutSystemRole : roleOptions}
  97. onChange={handleChatModeMessageRoleChange}
  98. triggerClassName='text-xs font-semibold text-gray-700 uppercase'
  99. itemClassName='text-[13px] font-medium text-gray-700'
  100. />
  101. )}
  102. <TooltipPlus
  103. popupContent={
  104. <div className='max-w-[180px]'>{t(`${i18nPrefix}.roleDescription.${payload.role}`)}</div>
  105. }
  106. >
  107. <RiQuestionLine className='w-3.5 h-3.5 text-gray-400' />
  108. </TooltipPlus>
  109. </div>
  110. }
  111. value={payload.edition_type === EditionType.jinja2 ? (payload.jinja2_text || '') : payload.text}
  112. onChange={onPromptChange}
  113. readOnly={readOnly}
  114. showRemove={canRemove}
  115. onRemove={onRemove}
  116. isChatModel={isChatModel}
  117. isChatApp={isChatApp}
  118. isShowContext={isShowContext}
  119. hasSetBlockStatus={hasSetBlockStatus}
  120. nodesOutputVars={availableVars}
  121. availableNodes={availableNodes}
  122. isSupportJinja
  123. editionType={payload.edition_type}
  124. onEditionTypeChange={onEditionTypeChange}
  125. varList={varList}
  126. handleAddVariable={handleAddVariable}
  127. />
  128. )
  129. }
  130. export default React.memo(ConfigPromptItem)