123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- 'use client'
- import type { FC } from 'react'
- import React, { useEffect, useState } from 'react'
- import { uniqueId } from 'lodash-es'
- import { useTranslation } from 'react-i18next'
- import { RiQuestionLine } from '@remixicon/react'
- import type { PromptItem, Variable } from '../../../types'
- import { EditionType } from '../../../types'
- import Editor from '@/app/components/workflow/nodes/_base/components/prompt/editor'
- import TypeSelector from '@/app/components/workflow/nodes/_base/components/selector'
- import TooltipPlus from '@/app/components/base/tooltip-plus'
- import { PromptRole } from '@/models/debug'
- const i18nPrefix = 'workflow.nodes.llm'
- type Props = {
- className?: string
- headerClassName?: string
- canNotChooseSystemRole?: boolean
- readOnly: boolean
- id: string
- canRemove: boolean
- isChatModel: boolean
- isChatApp: boolean
- payload: PromptItem
- handleChatModeMessageRoleChange: (role: PromptRole) => void
- onPromptChange: (p: string) => void
- onEditionTypeChange: (editionType: EditionType) => void
- onRemove: () => void
- isShowContext: boolean
- hasSetBlockStatus: {
- context: boolean
- history: boolean
- query: boolean
- }
- availableVars: any
- availableNodes: any
- varList: Variable[]
- handleAddVariable: (payload: any) => void
- }
- const roleOptions = [
- {
- label: 'system',
- value: PromptRole.system,
- },
- {
- label: 'user',
- value: PromptRole.user,
- },
- {
- label: 'assistant',
- value: PromptRole.assistant,
- },
- ]
- const roleOptionsWithoutSystemRole = roleOptions.filter(item => item.value !== PromptRole.system)
- const ConfigPromptItem: FC<Props> = ({
- className,
- headerClassName,
- canNotChooseSystemRole,
- readOnly,
- id,
- canRemove,
- handleChatModeMessageRoleChange,
- isChatModel,
- isChatApp,
- payload,
- onPromptChange,
- onEditionTypeChange,
- onRemove,
- isShowContext,
- hasSetBlockStatus,
- availableVars,
- availableNodes,
- varList,
- handleAddVariable,
- }) => {
- const { t } = useTranslation()
- const [instanceId, setInstanceId] = useState(uniqueId())
- useEffect(() => {
- setInstanceId(`${id}-${uniqueId()}`)
- }, [id])
- return (
- <Editor
- className={className}
- headerClassName={headerClassName}
- instanceId={instanceId}
- key={instanceId}
- title={
- <div className='relative left-1 flex items-center'>
- {payload.role === PromptRole.system
- ? (<div className='relative left-[-4px] text-xs font-semibold text-gray-700 uppercase'>
- SYSTEM
- </div>)
- : (
- <TypeSelector
- value={payload.role as string}
- allOptions={roleOptions}
- options={canNotChooseSystemRole ? roleOptionsWithoutSystemRole : roleOptions}
- onChange={handleChatModeMessageRoleChange}
- triggerClassName='text-xs font-semibold text-gray-700 uppercase'
- itemClassName='text-[13px] font-medium text-gray-700'
- />
- )}
- <TooltipPlus
- popupContent={
- <div className='max-w-[180px]'>{t(`${i18nPrefix}.roleDescription.${payload.role}`)}</div>
- }
- >
- <RiQuestionLine className='w-3.5 h-3.5 text-gray-400' />
- </TooltipPlus>
- </div>
- }
- value={payload.edition_type === EditionType.jinja2 ? (payload.jinja2_text || '') : payload.text}
- onChange={onPromptChange}
- readOnly={readOnly}
- showRemove={canRemove}
- onRemove={onRemove}
- isChatModel={isChatModel}
- isChatApp={isChatApp}
- isShowContext={isShowContext}
- hasSetBlockStatus={hasSetBlockStatus}
- nodesOutputVars={availableVars}
- availableNodes={availableNodes}
- isSupportJinja
- editionType={payload.edition_type}
- onEditionTypeChange={onEditionTypeChange}
- varList={varList}
- handleAddVariable={handleAddVariable}
- />
- )
- }
- export default React.memo(ConfigPromptItem)
|