您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

config-prompt.tsx 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useCallback } from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import produce from 'immer'
  6. import type { PromptItem, ValueSelector, Var } from '../../../types'
  7. import { PromptRole } from '../../../types'
  8. import useAvailableVarList from '../../_base/hooks/use-available-var-list'
  9. import ConfigPromptItem from './config-prompt-item'
  10. import Editor from '@/app/components/workflow/nodes/_base/components/prompt/editor'
  11. import AddButton from '@/app/components/workflow/nodes/_base/components/add-button'
  12. const i18nPrefix = 'workflow.nodes.llm'
  13. type Props = {
  14. readOnly: boolean
  15. nodeId: string
  16. filterVar: (payload: Var, selector: ValueSelector) => boolean
  17. isChatModel: boolean
  18. isChatApp: boolean
  19. payload: PromptItem | PromptItem[]
  20. onChange: (payload: PromptItem | PromptItem[]) => void
  21. isShowContext: boolean
  22. hasSetBlockStatus: {
  23. context: boolean
  24. history: boolean
  25. query: boolean
  26. }
  27. }
  28. const ConfigPrompt: FC<Props> = ({
  29. readOnly,
  30. nodeId,
  31. filterVar,
  32. isChatModel,
  33. isChatApp,
  34. payload,
  35. onChange,
  36. isShowContext,
  37. hasSetBlockStatus,
  38. }) => {
  39. const { t } = useTranslation()
  40. const {
  41. availableVars,
  42. availableNodes,
  43. } = useAvailableVarList(nodeId, {
  44. onlyLeafNodeVar: false,
  45. filterVar,
  46. })
  47. const handleChatModePromptChange = useCallback((index: number) => {
  48. return (prompt: string) => {
  49. const newPrompt = produce(payload as PromptItem[], (draft) => {
  50. draft[index].text = prompt
  51. })
  52. onChange(newPrompt)
  53. }
  54. }, [onChange, payload])
  55. const handleChatModeMessageRoleChange = useCallback((index: number) => {
  56. return (role: PromptRole) => {
  57. const newPrompt = produce(payload as PromptItem[], (draft) => {
  58. draft[index].role = role
  59. })
  60. onChange(newPrompt)
  61. }
  62. }, [onChange, payload])
  63. const handleAddPrompt = useCallback(() => {
  64. const newPrompt = produce(payload as PromptItem[], (draft) => {
  65. if (draft.length === 0) {
  66. draft.push({ role: PromptRole.system, text: '' })
  67. return
  68. }
  69. const isLastItemUser = draft[draft.length - 1].role === PromptRole.user
  70. draft.push({ role: isLastItemUser ? PromptRole.assistant : PromptRole.user, text: '' })
  71. })
  72. onChange(newPrompt)
  73. }, [onChange, payload])
  74. const handleRemove = useCallback((index: number) => {
  75. return () => {
  76. const newPrompt = produce(payload as PromptItem[], (draft) => {
  77. draft.splice(index, 1)
  78. })
  79. onChange(newPrompt)
  80. }
  81. }, [onChange, payload])
  82. const handleCompletionPromptChange = useCallback((prompt: string) => {
  83. const newPrompt = produce(payload as PromptItem, (draft) => {
  84. draft.text = prompt
  85. })
  86. onChange(newPrompt)
  87. }, [onChange, payload])
  88. // console.log(getInputVars((payload as PromptItem).text))
  89. return (
  90. <div>
  91. {(isChatModel && Array.isArray(payload))
  92. ? (
  93. <div>
  94. <div className='space-y-2'>
  95. {
  96. (payload as PromptItem[]).map((item, index) => {
  97. return (
  98. <ConfigPromptItem
  99. key={`${payload.length}-${index}`}
  100. canRemove={payload.length > 1}
  101. readOnly={readOnly}
  102. id={`${payload.length}-${index}`}
  103. handleChatModeMessageRoleChange={handleChatModeMessageRoleChange(index)}
  104. isChatModel={isChatModel}
  105. isChatApp={isChatApp}
  106. payload={item}
  107. onPromptChange={handleChatModePromptChange(index)}
  108. onRemove={handleRemove(index)}
  109. isShowContext={isShowContext}
  110. hasSetBlockStatus={hasSetBlockStatus}
  111. availableVars={availableVars}
  112. availableNodes={availableNodes}
  113. />
  114. )
  115. })
  116. }
  117. </div>
  118. <AddButton
  119. className='mt-2'
  120. text={t(`${i18nPrefix}.addMessage`)}
  121. onClick={handleAddPrompt}
  122. />
  123. </div>
  124. )
  125. : (
  126. <div>
  127. <Editor
  128. instanceId={`${nodeId}-chat-workflow-llm-prompt-editor`}
  129. title={<span className='capitalize'>{t(`${i18nPrefix}.prompt`)}</span>}
  130. value={(payload as PromptItem).text}
  131. onChange={handleCompletionPromptChange}
  132. readOnly={readOnly}
  133. isChatModel={isChatModel}
  134. isChatApp={isChatApp}
  135. isShowContext={isShowContext}
  136. hasSetBlockStatus={hasSetBlockStatus}
  137. nodesOutputVars={availableVars}
  138. availableNodes={availableNodes}
  139. />
  140. </div>
  141. )}
  142. </div>
  143. )
  144. }
  145. export default React.memo(ConfigPrompt)