index.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* eslint-disable multiline-ternary */
  2. 'use client'
  3. import type { FC } from 'react'
  4. import React, { useEffect, useRef, useState } from 'react'
  5. import cn from 'classnames'
  6. import { useContext } from 'use-context-selector'
  7. import produce from 'immer'
  8. import { useTranslation } from 'react-i18next'
  9. import { useBoolean } from 'ahooks'
  10. import ConfigContext from '@/context/debug-configuration'
  11. import Panel from '@/app/components/app/configuration/base/feature-panel'
  12. import Button from '@/app/components/base/button'
  13. import OperationBtn from '@/app/components/app/configuration/base/operation-btn'
  14. import { getInputKeys } from '@/app/components/base/block-input'
  15. import ConfirmAddVar from '@/app/components/app/configuration/config-prompt/confirm-add-var'
  16. import { getNewVar } from '@/utils/var'
  17. import { varHighlightHTML } from '@/app/components/app/configuration/base/var-highlight'
  18. export type IOpeningStatementProps = {
  19. promptTemplate: string
  20. value: string
  21. onChange: (value: string) => void
  22. }
  23. // regex to match the {{}} and replace it with a span
  24. const regex = /\{\{([^}]+)\}\}/g
  25. const OpeningStatement: FC<IOpeningStatementProps> = ({
  26. value = '',
  27. onChange,
  28. }) => {
  29. const { t } = useTranslation()
  30. const {
  31. modelConfig,
  32. setModelConfig,
  33. } = useContext(ConfigContext)
  34. const promptVariables = modelConfig.configs.prompt_variables
  35. const [notIncludeKeys, setNotIncludeKeys] = useState<string[]>([])
  36. const hasValue = !!(value || '').trim()
  37. const inputRef = useRef<HTMLTextAreaElement>(null)
  38. const [isFocus, { setTrue: didSetFocus, setFalse: setBlur }] = useBoolean(false)
  39. const setFocus = () => {
  40. didSetFocus()
  41. setTimeout(() => {
  42. const input = inputRef.current
  43. if (input) {
  44. input.focus()
  45. input.setSelectionRange(input.value.length, input.value.length)
  46. }
  47. }, 0)
  48. }
  49. const [tempValue, setTempValue] = useState(value)
  50. useEffect(() => {
  51. setTempValue(value || '')
  52. }, [value])
  53. const coloredContent = (tempValue || '')
  54. .replace(/</g, '&lt;')
  55. .replace(/>/g, '&gt;')
  56. .replace(regex, varHighlightHTML({ name: '$1' })) // `<span class="${highLightClassName}">{{$1}}</span>`
  57. .replace(/\n/g, '<br />')
  58. const handleEdit = () => {
  59. setFocus()
  60. }
  61. const [isShowConfirmAddVar, { setTrue: showConfirmAddVar, setFalse: hideConfirmAddVar }] = useBoolean(false)
  62. const handleCancel = () => {
  63. setBlur()
  64. setTempValue(value)
  65. }
  66. const handleConfirm = () => {
  67. const keys = getInputKeys(tempValue)
  68. const promptKeys = promptVariables.map(item => item.key)
  69. let notIncludeKeys: string[] = []
  70. if (promptKeys.length === 0) {
  71. if (keys.length > 0)
  72. notIncludeKeys = keys
  73. }
  74. else {
  75. notIncludeKeys = keys.filter(key => !promptKeys.includes(key))
  76. }
  77. if (notIncludeKeys.length > 0) {
  78. setNotIncludeKeys(notIncludeKeys)
  79. showConfirmAddVar()
  80. return
  81. }
  82. setBlur()
  83. onChange(tempValue)
  84. }
  85. const cancelAutoAddVar = () => {
  86. onChange(tempValue)
  87. hideConfirmAddVar()
  88. setBlur()
  89. }
  90. const autoAddVar = () => {
  91. const newModelConfig = produce(modelConfig, (draft) => {
  92. draft.configs.prompt_variables = [...draft.configs.prompt_variables, ...notIncludeKeys.map(key => getNewVar(key))]
  93. })
  94. onChange(tempValue)
  95. setModelConfig(newModelConfig)
  96. hideConfirmAddVar()
  97. setBlur()
  98. }
  99. const headerRight = (
  100. <OperationBtn type='edit' actionName={hasValue ? '' : t('appDebug.openingStatement.writeOpner') as string} onClick={handleEdit} />
  101. )
  102. return (
  103. <Panel
  104. className={cn(isShowConfirmAddVar && 'h-[220px]', 'relative mt-4')}
  105. title={t('appDebug.openingStatement.title')}
  106. headerIcon={
  107. <svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
  108. <path fillRule="evenodd" clipRule="evenodd" d="M8.33353 1.33301C4.83572 1.33301 2.00019 4.16854 2.00019 7.66634C2.00019 8.37301 2.11619 9.05395 2.3307 9.69036C2.36843 9.80229 2.39063 9.86853 2.40507 9.91738L2.40979 9.93383L2.40729 9.93903C2.39015 9.97437 2.36469 10.0218 2.31705 10.11L1.2158 12.1484C1.14755 12.2746 1.07633 12.4064 1.02735 12.5209C0.978668 12.6348 0.899813 12.8437 0.938613 13.0914C0.984094 13.3817 1.15495 13.6373 1.40581 13.7903C1.61981 13.9208 1.843 13.9279 1.96683 13.9264C2.09141 13.925 2.24036 13.9095 2.38314 13.8947L5.81978 13.5395C5.87482 13.5338 5.9036 13.5309 5.92468 13.5292L5.92739 13.529L5.93564 13.532C5.96154 13.5413 5.99666 13.5548 6.0573 13.5781C6.76459 13.8506 7.53244 13.9997 8.33353 13.9997C11.8313 13.9997 14.6669 11.1641 14.6669 7.66634C14.6669 4.16854 11.8313 1.33301 8.33353 1.33301ZM5.9799 5.72116C6.73142 5.08698 7.73164 5.27327 8.33144 5.96584C8.93125 5.27327 9.91854 5.09365 10.683 5.72116C11.4474 6.34867 11.5403 7.41567 10.9501 8.16572C10.5845 8.6304 9.6668 9.47911 9.02142 10.0576C8.78435 10.2702 8.66582 10.3764 8.52357 10.4192C8.40154 10.456 8.26134 10.456 8.13931 10.4192C7.99706 10.3764 7.87853 10.2702 7.64147 10.0576C6.99609 9.47911 6.07839 8.6304 5.71276 8.16572C5.12259 7.41567 5.22839 6.35534 5.9799 5.72116Z" fill="#E74694" />
  109. </svg>
  110. }
  111. headerRight={headerRight}
  112. hasHeaderBottomBorder={!hasValue}
  113. isFocus={isFocus}
  114. >
  115. <div className='text-gray-700 text-sm'>
  116. {(hasValue || (!hasValue && isFocus))
  117. ? (
  118. <>
  119. {isFocus
  120. ? (
  121. <textarea
  122. ref={inputRef}
  123. value={tempValue}
  124. rows={3}
  125. onChange={e => setTempValue(e.target.value)}
  126. className="w-full px-0 text-sm border-0 bg-transparent focus:outline-none "
  127. placeholder={t('appDebug.openingStatement.placeholder') as string}
  128. >
  129. </textarea>
  130. )
  131. : (
  132. <div dangerouslySetInnerHTML={{
  133. __html: coloredContent,
  134. }}></div>
  135. )}
  136. {/* Operation Bar */}
  137. {isFocus
  138. && (
  139. <div className='mt-2 flex items-center justify-between'>
  140. <div className='text-xs text-gray-500'>{t('appDebug.openingStatement.varTip')}</div>
  141. <div className='flex gap-2'>
  142. <Button className='!h-8 text-sm' onClick={handleCancel}>{t('common.operation.cancel')}</Button>
  143. <Button className='!h-8 text-sm' onClick={handleConfirm} type="primary">{t('common.operation.save')}</Button>
  144. </div>
  145. </div>
  146. )}
  147. </>) : (
  148. <div className='pt-2 pb-1 text-xs text-gray-500'>{t('appDebug.openingStatement.noDataPlaceHolder')}</div>
  149. )}
  150. {isShowConfirmAddVar && (
  151. <ConfirmAddVar
  152. varNameArr={notIncludeKeys}
  153. onConfrim={autoAddVar}
  154. onCancel={cancelAutoAddVar}
  155. onHide={hideConfirmAddVar}
  156. />
  157. )}
  158. </div>
  159. </Panel>
  160. )
  161. }
  162. export default React.memo(OpeningStatement)