style(prompt-editor): normalize line endings and indentation in workflow variable block components

pull/21802/head
Mminamiyama 10 months ago
parent 7bdf234ab3
commit 5762784df0

@ -1,215 +1,215 @@
import { import {
memo, memo,
useCallback, useCallback,
useEffect, useEffect,
useState, useState,
} from 'react' } from 'react'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import { import {
COMMAND_PRIORITY_EDITOR, COMMAND_PRIORITY_EDITOR,
} from 'lexical' } from 'lexical'
import { mergeRegister } from '@lexical/utils' import { mergeRegister } from '@lexical/utils'
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext' import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext'
import { import {
RiErrorWarningFill, RiErrorWarningFill,
RiMoreLine, RiMoreLine,
} from '@remixicon/react' } from '@remixicon/react'
import { useReactFlow, useStoreApi } from 'reactflow' import { useReactFlow, useStoreApi } from 'reactflow'
import { useSelectOrDelete } from '../../hooks' import { useSelectOrDelete } from '../../hooks'
import type { WorkflowNodesMap } from './node' import type { WorkflowNodesMap } from './node'
import { WorkflowVariableBlockNode } from './node' import { WorkflowVariableBlockNode } from './node'
import { import {
DELETE_WORKFLOW_VARIABLE_BLOCK_COMMAND, DELETE_WORKFLOW_VARIABLE_BLOCK_COMMAND,
UPDATE_WORKFLOW_NODES_MAP, UPDATE_WORKFLOW_NODES_MAP,
} from './index' } from './index'
import cn from '@/utils/classnames' import cn from '@/utils/classnames'
import { Variable02 } from '@/app/components/base/icons/src/vender/solid/development' import { Variable02 } from '@/app/components/base/icons/src/vender/solid/development'
import { BubbleX, Env } from '@/app/components/base/icons/src/vender/line/others' import { BubbleX, Env } from '@/app/components/base/icons/src/vender/line/others'
import { VarBlockIcon } from '@/app/components/workflow/block-icon' import { VarBlockIcon } from '@/app/components/workflow/block-icon'
import { Line3 } from '@/app/components/base/icons/src/public/common' import { Line3 } from '@/app/components/base/icons/src/public/common'
import { isConversationVar, isENV, isSystemVar } from '@/app/components/workflow/nodes/_base/components/variable/utils' import { isConversationVar, isENV, isSystemVar } from '@/app/components/workflow/nodes/_base/components/variable/utils'
import Tooltip from '@/app/components/base/tooltip' import Tooltip from '@/app/components/base/tooltip'
import { isExceptionVariable } from '@/app/components/workflow/utils' import { isExceptionVariable } from '@/app/components/workflow/utils'
import VarFullPathPanel from '@/app/components/workflow/nodes/_base/components/variable/var-full-path-panel' import VarFullPathPanel from '@/app/components/workflow/nodes/_base/components/variable/var-full-path-panel'
import { Type } from '@/app/components/workflow/nodes/llm/types' import { Type } from '@/app/components/workflow/nodes/llm/types'
import type { ValueSelector, Var } from '@/app/components/workflow/types' import type { ValueSelector, Var } from '@/app/components/workflow/types'
type WorkflowVariableBlockComponentProps = { type WorkflowVariableBlockComponentProps = {
nodeKey: string nodeKey: string
variables: string[] variables: string[]
workflowNodesMap: WorkflowNodesMap workflowNodesMap: WorkflowNodesMap
environmentVariables?: Var[] environmentVariables?: Var[]
conversationVariables?: Var[] conversationVariables?: Var[]
getVarType?: (payload: { getVarType?: (payload: {
nodeId: string, nodeId: string,
valueSelector: ValueSelector, valueSelector: ValueSelector,
}) => Type }) => Type
} }
const WorkflowVariableBlockComponent = ({ const WorkflowVariableBlockComponent = ({
nodeKey, nodeKey,
variables, variables,
workflowNodesMap = {}, workflowNodesMap = {},
getVarType, getVarType,
environmentVariables, environmentVariables,
conversationVariables, conversationVariables,
}: WorkflowVariableBlockComponentProps) => { }: WorkflowVariableBlockComponentProps) => {
const { t } = useTranslation() const { t } = useTranslation()
const [editor] = useLexicalComposerContext() const [editor] = useLexicalComposerContext()
const [ref, isSelected] = useSelectOrDelete(nodeKey, DELETE_WORKFLOW_VARIABLE_BLOCK_COMMAND) const [ref, isSelected] = useSelectOrDelete(nodeKey, DELETE_WORKFLOW_VARIABLE_BLOCK_COMMAND)
const variablesLength = variables.length const variablesLength = variables.length
const isShowAPart = variablesLength > 2 const isShowAPart = variablesLength > 2
const varName = ( const varName = (
() => { () => {
const isSystem = isSystemVar(variables) const isSystem = isSystemVar(variables)
const varName = variables[variablesLength - 1] const varName = variables[variablesLength - 1]
return `${isSystem ? 'sys.' : ''}${varName}` return `${isSystem ? 'sys.' : ''}${varName}`
} }
)() )()
const [localWorkflowNodesMap, setLocalWorkflowNodesMap] = useState<WorkflowNodesMap>(workflowNodesMap) const [localWorkflowNodesMap, setLocalWorkflowNodesMap] = useState<WorkflowNodesMap>(workflowNodesMap)
const node = localWorkflowNodesMap![variables[0]] const node = localWorkflowNodesMap![variables[0]]
const isEnv = isENV(variables) const isEnv = isENV(variables)
const isChatVar = isConversationVar(variables) const isChatVar = isConversationVar(variables)
const isException = isExceptionVariable(varName, node?.type) const isException = isExceptionVariable(varName, node?.type)
let variableValid = true let variableValid = true
if (isEnv) { if (isEnv) {
if (environmentVariables) if (environmentVariables)
variableValid = environmentVariables.some(v => v.variable === `${variables?.[0] ?? ''}.${variables?.[1] ?? ''}`) variableValid = environmentVariables.some(v => v.variable === `${variables?.[0] ?? ''}.${variables?.[1] ?? ''}`)
} }
else if (isChatVar) { else if (isChatVar) {
if (conversationVariables) if (conversationVariables)
variableValid = conversationVariables.some(v => v.variable === `${variables?.[0] ?? ''}.${variables?.[1] ?? ''}`) variableValid = conversationVariables.some(v => v.variable === `${variables?.[0] ?? ''}.${variables?.[1] ?? ''}`)
} }
else { else {
variableValid = !!node variableValid = !!node
} }
const reactflow = useReactFlow() const reactflow = useReactFlow()
const store = useStoreApi() const store = useStoreApi()
useEffect(() => { useEffect(() => {
if (!editor.hasNodes([WorkflowVariableBlockNode])) if (!editor.hasNodes([WorkflowVariableBlockNode]))
throw new Error('WorkflowVariableBlockPlugin: WorkflowVariableBlock not registered on editor') throw new Error('WorkflowVariableBlockPlugin: WorkflowVariableBlock not registered on editor')
return mergeRegister( return mergeRegister(
editor.registerCommand( editor.registerCommand(
UPDATE_WORKFLOW_NODES_MAP, UPDATE_WORKFLOW_NODES_MAP,
(workflowNodesMap: WorkflowNodesMap) => { (workflowNodesMap: WorkflowNodesMap) => {
setLocalWorkflowNodesMap(workflowNodesMap) setLocalWorkflowNodesMap(workflowNodesMap)
return true return true
}, },
COMMAND_PRIORITY_EDITOR, COMMAND_PRIORITY_EDITOR,
), ),
) )
}, [editor]) }, [editor])
const handleVariableJump = useCallback(() => { const handleVariableJump = useCallback(() => {
const workflowContainer = document.getElementById('workflow-container') const workflowContainer = document.getElementById('workflow-container')
const { const {
clientWidth, clientWidth,
clientHeight, clientHeight,
} = workflowContainer! } = workflowContainer!
const { const {
setViewport, setViewport,
} = reactflow } = reactflow
const { transform } = store.getState() const { transform } = store.getState()
const zoom = transform[2] const zoom = transform[2]
const position = node.position const position = node.position
setViewport({ setViewport({
x: (clientWidth - 400 - node.width! * zoom) / 2 - position!.x * zoom, x: (clientWidth - 400 - node.width! * zoom) / 2 - position!.x * zoom,
y: (clientHeight - node.height! * zoom) / 2 - position!.y * zoom, y: (clientHeight - node.height! * zoom) / 2 - position!.y * zoom,
zoom: transform[2], zoom: transform[2],
}) })
}, [node, reactflow, store]) }, [node, reactflow, store])
const Item = ( const Item = (
<div <div
className={cn( className={cn(
'group/wrap relative mx-0.5 flex h-[18px] select-none items-center rounded-[5px] border pl-0.5 pr-[3px] hover:border-state-accent-solid hover:bg-state-accent-hover', 'group/wrap relative mx-0.5 flex h-[18px] select-none items-center rounded-[5px] border pl-0.5 pr-[3px] hover:border-state-accent-solid hover:bg-state-accent-hover',
isSelected ? ' border-state-accent-solid bg-state-accent-hover' : ' border-components-panel-border-subtle bg-components-badge-white-to-dark', isSelected ? ' border-state-accent-solid bg-state-accent-hover' : ' border-components-panel-border-subtle bg-components-badge-white-to-dark',
!variableValid && '!border-state-destructive-solid !bg-state-destructive-hover', !variableValid && '!border-state-destructive-solid !bg-state-destructive-hover',
)} )}
onClick={(e) => { onClick={(e) => {
e.stopPropagation() e.stopPropagation()
handleVariableJump() handleVariableJump()
}} }}
ref={ref} ref={ref}
> >
{!isEnv && !isChatVar && ( {!isEnv && !isChatVar && (
<div className='flex items-center'> <div className='flex items-center'>
{ {
node?.type && ( node?.type && (
<div className='p-[1px]'> <div className='p-[1px]'>
<VarBlockIcon <VarBlockIcon
className='!text-text-secondary' className='!text-text-secondary'
type={node?.type} type={node?.type}
/> />
</div> </div>
) )
} }
<div className='mx-0.5 max-w-[60px] shrink-0 truncate text-xs font-medium text-text-secondary' title={node?.title} style={{ <div className='mx-0.5 max-w-[60px] shrink-0 truncate text-xs font-medium text-text-secondary' title={node?.title} style={{
}}>{node?.title}</div> }}>{node?.title}</div>
<Line3 className='mr-0.5 text-divider-deep'></Line3> <Line3 className='mr-0.5 text-divider-deep'></Line3>
</div> </div>
)} )}
{isShowAPart && ( {isShowAPart && (
<div className='flex items-center'> <div className='flex items-center'>
<RiMoreLine className='h-3 w-3 text-text-secondary' /> <RiMoreLine className='h-3 w-3 text-text-secondary' />
<Line3 className='mr-0.5 text-divider-deep'></Line3> <Line3 className='mr-0.5 text-divider-deep'></Line3>
</div> </div>
)} )}
<div className='flex items-center text-text-accent'> <div className='flex items-center text-text-accent'>
{!isEnv && !isChatVar && <Variable02 className={cn('h-3.5 w-3.5 shrink-0', isException && 'text-text-warning')} />} {!isEnv && !isChatVar && <Variable02 className={cn('h-3.5 w-3.5 shrink-0', isException && 'text-text-warning')} />}
{isEnv && <Env className='h-3.5 w-3.5 shrink-0 text-util-colors-violet-violet-600' />} {isEnv && <Env className='h-3.5 w-3.5 shrink-0 text-util-colors-violet-violet-600' />}
{isChatVar && <BubbleX className='h-3.5 w-3.5 text-util-colors-teal-teal-700' />} {isChatVar && <BubbleX className='h-3.5 w-3.5 text-util-colors-teal-teal-700' />}
<div className={cn( <div className={cn(
'ml-0.5 shrink-0 truncate text-xs font-medium', 'ml-0.5 shrink-0 truncate text-xs font-medium',
isEnv && 'text-util-colors-violet-violet-600', isEnv && 'text-util-colors-violet-violet-600',
isChatVar && 'text-util-colors-teal-teal-700', isChatVar && 'text-util-colors-teal-teal-700',
isException && 'text-text-warning', isException && 'text-text-warning',
)} title={varName}>{varName}</div> )} title={varName}>{varName}</div>
{ {
!variableValid && ( !variableValid && (
<RiErrorWarningFill className='ml-0.5 h-3 w-3 text-text-destructive' /> <RiErrorWarningFill className='ml-0.5 h-3 w-3 text-text-destructive' />
) )
} }
</div> </div>
</div> </div>
) )
if (!variableValid) { if (!variableValid) {
return ( return (
<Tooltip popupContent={t('workflow.errorMsg.invalidVariable')}> <Tooltip popupContent={t('workflow.errorMsg.invalidVariable')}>
{Item} {Item}
</Tooltip> </Tooltip>
) )
} }
if (!node) if (!node)
return Item return Item
return ( return (
<Tooltip <Tooltip
noDecoration noDecoration
popupContent={ popupContent={
<VarFullPathPanel <VarFullPathPanel
nodeName={node.title} nodeName={node.title}
path={variables.slice(1)} path={variables.slice(1)}
varType={getVarType ? getVarType({ varType={getVarType ? getVarType({
nodeId: variables[0], nodeId: variables[0],
valueSelector: variables, valueSelector: variables,
}) : Type.string} }) : Type.string}
nodeType={node?.type} nodeType={node?.type}
/>} />}
disabled={!isShowAPart} disabled={!isShowAPart}
> >
<div>{Item}</div> <div>{Item}</div>
</Tooltip> </Tooltip>
) )
} }
export default memo(WorkflowVariableBlockComponent) export default memo(WorkflowVariableBlockComponent)

@ -1,67 +1,67 @@
import { import {
memo, memo,
useCallback, useCallback,
useEffect, useEffect,
} from 'react' } from 'react'
import type { TextNode } from 'lexical' import type { TextNode } from 'lexical'
import { $applyNodeReplacement } from 'lexical' import { $applyNodeReplacement } from 'lexical'
import { mergeRegister } from '@lexical/utils' import { mergeRegister } from '@lexical/utils'
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext' import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext'
import { decoratorTransform } from '../../utils' import { decoratorTransform } from '../../utils'
import type { WorkflowVariableBlockType } from '../../types' import type { WorkflowVariableBlockType } from '../../types'
import { CustomTextNode } from '../custom-text/node' import { CustomTextNode } from '../custom-text/node'
import { $createWorkflowVariableBlockNode } from './node' import { $createWorkflowVariableBlockNode } from './node'
import { WorkflowVariableBlockNode } from './index' import { WorkflowVariableBlockNode } from './index'
import { VAR_REGEX as REGEX, resetReg } from '@/config' import { VAR_REGEX as REGEX, resetReg } from '@/config'
const WorkflowVariableBlockReplacementBlock = ({ const WorkflowVariableBlockReplacementBlock = ({
workflowNodesMap, workflowNodesMap,
getVarType, getVarType,
onInsert, onInsert,
variables, variables,
}: WorkflowVariableBlockType) => { }: WorkflowVariableBlockType) => {
const [editor] = useLexicalComposerContext() const [editor] = useLexicalComposerContext()
useEffect(() => { useEffect(() => {
if (!editor.hasNodes([WorkflowVariableBlockNode])) if (!editor.hasNodes([WorkflowVariableBlockNode]))
throw new Error('WorkflowVariableBlockNodePlugin: WorkflowVariableBlockNode not registered on editor') throw new Error('WorkflowVariableBlockNodePlugin: WorkflowVariableBlockNode not registered on editor')
}, [editor]) }, [editor])
const createWorkflowVariableBlockNode = useCallback((textNode: TextNode): WorkflowVariableBlockNode => { const createWorkflowVariableBlockNode = useCallback((textNode: TextNode): WorkflowVariableBlockNode => {
if (onInsert) if (onInsert)
onInsert() onInsert()
const nodePathString = textNode.getTextContent().slice(3, -3) const nodePathString = textNode.getTextContent().slice(3, -3)
return $applyNodeReplacement($createWorkflowVariableBlockNode(nodePathString.split('.'), workflowNodesMap, getVarType, variables?.find(o => o.nodeId === 'env')?.vars || [], variables?.find(o => o.nodeId === 'conversation')?.vars || [])) return $applyNodeReplacement($createWorkflowVariableBlockNode(nodePathString.split('.'), workflowNodesMap, getVarType, variables?.find(o => o.nodeId === 'env')?.vars || [], variables?.find(o => o.nodeId === 'conversation')?.vars || []))
}, [onInsert, workflowNodesMap, getVarType, variables]) }, [onInsert, workflowNodesMap, getVarType, variables])
const getMatch = useCallback((text: string) => { const getMatch = useCallback((text: string) => {
const matchArr = REGEX.exec(text) const matchArr = REGEX.exec(text)
if (matchArr === null) if (matchArr === null)
return null return null
const startOffset = matchArr.index const startOffset = matchArr.index
const endOffset = startOffset + matchArr[0].length const endOffset = startOffset + matchArr[0].length
return { return {
end: endOffset, end: endOffset,
start: startOffset, start: startOffset,
} }
}, []) }, [])
const transformListener = useCallback((textNode: CustomTextNode) => { const transformListener = useCallback((textNode: CustomTextNode) => {
resetReg() resetReg()
return decoratorTransform(textNode, getMatch, createWorkflowVariableBlockNode) return decoratorTransform(textNode, getMatch, createWorkflowVariableBlockNode)
}, [createWorkflowVariableBlockNode, getMatch]) }, [createWorkflowVariableBlockNode, getMatch])
useEffect(() => { useEffect(() => {
resetReg() resetReg()
return mergeRegister( return mergeRegister(
editor.registerNodeTransform(CustomTextNode, transformListener), editor.registerNodeTransform(CustomTextNode, transformListener),
) )
}, []) }, [])
return null return null
} }
export default memo(WorkflowVariableBlockReplacementBlock) export default memo(WorkflowVariableBlockReplacementBlock)

Loading…
Cancel
Save