123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- 'use client'
- import type { FC } from 'react'
- import React from 'react'
- import copy from 'copy-to-clipboard'
- import { useTranslation } from 'react-i18next'
- import { useContext } from 'use-context-selector'
- import { useBoolean } from 'ahooks'
- import produce from 'immer'
- import {
- RiDeleteBinLine,
- RiErrorWarningFill,
- } from '@remixicon/react'
- import s from './style.module.css'
- import MessageTypeSelector from './message-type-selector'
- import ConfirmAddVar from './confirm-add-var'
- import PromptEditorHeightResizeWrap from './prompt-editor-height-resize-wrap'
- import cn from '@/utils/classnames'
- import type { PromptRole, PromptVariable } from '@/models/debug'
- import {
- Clipboard,
- ClipboardCheck,
- } from '@/app/components/base/icons/src/vender/line/files'
- import Button from '@/app/components/base/button'
- import Tooltip from '@/app/components/base/tooltip'
- import PromptEditor from '@/app/components/base/prompt-editor'
- import ConfigContext from '@/context/debug-configuration'
- import { getNewVar, getVars } from '@/utils/var'
- import { AppType } from '@/types/app'
- import { useModalContext } from '@/context/modal-context'
- import type { ExternalDataTool } from '@/models/common'
- import { useToastContext } from '@/app/components/base/toast'
- import { useEventEmitterContextContext } from '@/context/event-emitter'
- import { ADD_EXTERNAL_DATA_TOOL } from '@/app/components/app/configuration/config-var'
- import { INSERT_VARIABLE_VALUE_BLOCK_COMMAND } from '@/app/components/base/prompt-editor/plugins/variable-block'
- type Props = {
- type: PromptRole
- isChatMode: boolean
- value: string
- onTypeChange: (value: PromptRole) => void
- onChange: (value: string) => void
- canDelete: boolean
- onDelete: () => void
- promptVariables: PromptVariable[]
- isContextMissing: boolean
- onHideContextMissingTip: () => void
- noResize?: boolean
- }
- const AdvancedPromptInput: FC<Props> = ({
- type,
- isChatMode,
- value,
- onChange,
- onTypeChange,
- canDelete,
- onDelete,
- promptVariables,
- isContextMissing,
- onHideContextMissingTip,
- noResize,
- }) => {
- const { t } = useTranslation()
- const { eventEmitter } = useEventEmitterContextContext()
- const {
- mode,
- hasSetBlockStatus,
- modelConfig,
- setModelConfig,
- conversationHistoriesRole,
- showHistoryModal,
- dataSets,
- showSelectDataSet,
- externalDataToolsConfig,
- } = useContext(ConfigContext)
- const { notify } = useToastContext()
- const { setShowExternalDataToolModal } = useModalContext()
- const handleOpenExternalDataToolModal = () => {
- setShowExternalDataToolModal({
- payload: {},
- onSaveCallback: (newExternalDataTool: ExternalDataTool) => {
- eventEmitter?.emit({
- type: ADD_EXTERNAL_DATA_TOOL,
- payload: newExternalDataTool,
- } as any)
- eventEmitter?.emit({
- type: INSERT_VARIABLE_VALUE_BLOCK_COMMAND,
- payload: newExternalDataTool.variable,
- } as any)
- },
- onValidateBeforeSaveCallback: (newExternalDataTool: ExternalDataTool) => {
- for (let i = 0; i < promptVariables.length; i++) {
- if (promptVariables[i].key === newExternalDataTool.variable) {
- notify({ type: 'error', message: t('appDebug.varKeyError.keyAlreadyExists', { key: promptVariables[i].key }) })
- return false
- }
- }
- return true
- },
- })
- }
- const isChatApp = mode !== AppType.completion
- const [isCopied, setIsCopied] = React.useState(false)
- const promptVariablesObj = (() => {
- const obj: Record<string, boolean> = {}
- promptVariables.forEach((item) => {
- obj[item.key] = true
- })
- return obj
- })()
- const [newPromptVariables, setNewPromptVariables] = React.useState<PromptVariable[]>(promptVariables)
- const [isShowConfirmAddVar, { setTrue: showConfirmAddVar, setFalse: hideConfirmAddVar }] = useBoolean(false)
- const handlePromptChange = (newValue: string) => {
- if (value === newValue)
- return
- onChange(newValue)
- }
- const handleBlur = () => {
- const keys = getVars(value)
- const newPromptVariables = keys.filter(key => !(key in promptVariablesObj) && !externalDataToolsConfig.find(item => item.variable === key)).map(key => getNewVar(key, ''))
- if (newPromptVariables.length > 0) {
- setNewPromptVariables(newPromptVariables)
- showConfirmAddVar()
- }
- }
- const handleAutoAdd = (isAdd: boolean) => {
- return () => {
- if (isAdd) {
- const newModelConfig = produce(modelConfig, (draft) => {
- draft.configs.prompt_variables = [...draft.configs.prompt_variables, ...newPromptVariables]
- })
- setModelConfig(newModelConfig)
- }
- hideConfirmAddVar()
- }
- }
- const minHeight = 102
- const [editorHeight, setEditorHeight] = React.useState(isChatMode ? 200 : 508)
- const contextMissing = (
- <div
- className='flex justify-between items-center h-11 pt-2 pr-3 pb-1 pl-4 rounded-tl-xl rounded-tr-xl'
- style={{
- background: 'linear-gradient(180deg, #FEF0C7 0%, rgba(254, 240, 199, 0) 100%)',
- }}
- >
- <div className='flex items-center pr-2' >
- <RiErrorWarningFill className='mr-1 w-4 h-4 text-[#F79009]' />
- <div className='leading-[18px] text-[13px] font-medium text-[#DC6803]'>{t('appDebug.promptMode.contextMissing')}</div>
- </div>
- <Button
- size='small'
- variant='secondary-accent'
- onClick={onHideContextMissingTip}
- >{t('common.operation.ok')}</Button>
- </div>
- )
- return (
- <div className={`bg-gradient-to-r from-components-input-border-active-prompt-1 to-components-input-border-active-prompt-2 rounded-xl p-0.5 shadow-xs ${!isContextMissing ? '' : s.warningBorder}`}>
- <div className='rounded-xl bg-background-default'>
- {isContextMissing
- ? contextMissing
- : (
- <div className={cn(s.boxHeader, 'flex justify-between items-center h-11 pt-2 pr-3 pb-1 pl-4 rounded-tl-xl rounded-tr-xl bg-background-default hover:shadow-xs')}>
- {isChatMode
- ? (
- <MessageTypeSelector value={type} onChange={onTypeChange} />
- )
- : (
- <div className='flex items-center space-x-1'>
- <div className='text-sm font-semibold uppercase text-indigo-800'>{t('appDebug.pageTitle.line1')}
- </div>
- <Tooltip
- popupContent={
- <div className='w-[180px]'>
- {t('appDebug.promptTip')}
- </div>
- }
- />
- </div>)}
- <div className={cn(s.optionWrap, 'items-center space-x-1')}>
- {canDelete && (
- <RiDeleteBinLine onClick={onDelete} className='h-6 w-6 p-1 text-text-tertiary cursor-pointer' />
- )}
- {!isCopied
- ? (
- <Clipboard className='h-6 w-6 p-1 text-text-tertiary cursor-pointer' onClick={() => {
- copy(value)
- setIsCopied(true)
- }} />
- )
- : (
- <ClipboardCheck className='h-6 w-6 p-1 text-text-tertiary' />
- )}
- </div>
- </div>
- )}
- <PromptEditorHeightResizeWrap
- className='px-4 min-h-[102px] overflow-y-auto text-sm text-text-secondary'
- height={editorHeight}
- minHeight={minHeight}
- onHeightChange={setEditorHeight}
- footer={(
- <div className='pl-4 pb-2 flex'>
- <div className="h-[18px] leading-[18px] px-1 rounded-md bg-divider-regular text-xs text-text-tertiary">{value.length}</div>
- </div>
- )}
- hideResize={noResize}
- >
- <PromptEditor
- className='min-h-[84px]'
- value={value}
- contextBlock={{
- show: true,
- selectable: !hasSetBlockStatus.context,
- datasets: dataSets.map(item => ({
- id: item.id,
- name: item.name,
- type: item.data_source_type,
- })),
- onAddContext: showSelectDataSet,
- }}
- variableBlock={{
- show: true,
- variables: modelConfig.configs.prompt_variables.filter(item => item.type !== 'api').map(item => ({
- name: item.name,
- value: item.key,
- })),
- }}
- externalToolBlock={{
- externalTools: modelConfig.configs.prompt_variables.filter(item => item.type === 'api').map(item => ({
- name: item.name,
- variableName: item.key,
- icon: item.icon,
- icon_background: item.icon_background,
- })),
- onAddExternalTool: handleOpenExternalDataToolModal,
- }}
- historyBlock={{
- show: !isChatMode && isChatApp,
- selectable: !hasSetBlockStatus.history,
- history: {
- user: conversationHistoriesRole?.user_prefix,
- assistant: conversationHistoriesRole?.assistant_prefix,
- },
- onEditRole: showHistoryModal,
- }}
- queryBlock={{
- show: !isChatMode && isChatApp,
- selectable: !hasSetBlockStatus.query,
- }}
- onChange={handlePromptChange}
- onBlur={handleBlur}
- />
- </PromptEditorHeightResizeWrap>
- </div>
- {isShowConfirmAddVar && (
- <ConfirmAddVar
- varNameArr={newPromptVariables.map(v => v.name)}
- onConfirm={handleAutoAdd(true)}
- onCancel={handleAutoAdd(false)}
- onHide={hideConfirmAddVar}
- />
- )}
- </div>
- )
- }
- export default React.memo(AdvancedPromptInput)
|