Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support array reference multi-select #3041

Open
wants to merge 7 commits into
base: 4.8.13-dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions packages/global/core/workflow/runtime/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,50 @@ export const getReferenceVariableValue = ({
return outputValue;
};

export const getReferenceArrayValue = ({
value,
nodes,
variables
}: {
value: ReferenceValueProps;
nodes: RuntimeNodeItemType[];
variables: Record<string, any>;
}) => {
const nodeIds = nodes.map((node) => node.nodeId);
if (
!Array.isArray(value) ||
!value.every(
(val) => val?.length === 2 && typeof val[0] === 'string' && typeof val[1] === 'string'
)
) {
return value;
}
const result = value.map((val) => {
if (!isReferenceValue(val, nodeIds)) {
return [val];
}

const sourceNodeId = val?.[0];
const outputId = val?.[1];

if (sourceNodeId === VARIABLE_NODE_ID && outputId) {
const variableValue = variables[outputId];
return Array.isArray(variableValue) ? variableValue : [variableValue];
}

const node = nodes.find((node) => node.nodeId === sourceNodeId);

if (!node) {
return undefined;
}

const outputValue = node.outputs.find((output) => output.id === outputId)?.value;
return Array.isArray(outputValue) ? outputValue : [outputValue];
});

return result.flat();
};

export const textAdaptGptResponse = ({
text,
model = '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const getOneQuoteInputTemplate = ({
}): FlowNodeInputItemType => ({
key,
renderTypeList: [FlowNodeInputTypeEnum.reference],
label: `${i18nT('workflow:quote_num')},{ num: ${index} }`,
label: `${i18nT('workflow:quote_num')}`,
debugLabel: i18nT('workflow:knowledge_base_reference'),
canEdit: true,
valueType: WorkflowIOValueTypeEnum.datasetQuote
Expand Down
18 changes: 12 additions & 6 deletions packages/global/core/workflow/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
} from '../app/constants';
import { IfElseResultEnum } from './template/system/ifElse/constant';
import { RuntimeNodeItemType } from './runtime/type';
import { getReferenceVariableValue } from './runtime/utils';
import { getReferenceArrayValue, getReferenceVariableValue } from './runtime/utils';
import {
Input_Template_File_Link,
Input_Template_History,
Expand Down Expand Up @@ -383,14 +383,20 @@ export function replaceEditorVariable({
// Get runningNode inputs(Will be replaced with reference)
const customInputs = runningNode.inputs.flatMap((item) => {
if (Array.isArray(item.value)) {
let value = getReferenceVariableValue({
value: item.value as ReferenceValueProps,
nodes,
variables
});
value = getReferenceArrayValue({
value,
nodes,
variables
});
return [
{
id: item.key,
value: getReferenceVariableValue({
value: item.value as ReferenceValueProps,
nodes,
variables
}),
value,
nodeId: runningNode.nodeId
}
];
Expand Down
12 changes: 10 additions & 2 deletions packages/service/core/workflow/dispatch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ import { removeSystemVariable, valueTypeFormat } from './utils';
import {
filterWorkflowEdges,
checkNodeRunStatus,
textAdaptGptResponse
textAdaptGptResponse,
getReferenceArrayValue
} from '@fastgpt/global/core/workflow/runtime/utils';
import { ChatNodeUsageType } from '@fastgpt/global/support/wallet/bill/type';
import { dispatchRunTools } from './agent/runTool/index';
Expand Down Expand Up @@ -494,13 +495,20 @@ export async function dispatchWorkFlow(data: Props): Promise<DispatchFlowRespons
runningNode: node
});

// replace reference variables
// replace original reference variables
value = getReferenceVariableValue({
value,
nodes: runtimeNodes,
variables
});

// replace new reference variables
value = getReferenceArrayValue({
value,
nodes: runtimeNodes,
variables
});

// Dynamic input is stored in the dynamic key
if (input.canEdit && dynamicInput && params[dynamicInput.key]) {
params[dynamicInput.key][input.key] = valueTypeFormat(value, input.valueType);
Expand Down
12 changes: 10 additions & 2 deletions packages/service/core/workflow/dispatch/tools/runIfElse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import {
} from '@fastgpt/global/core/workflow/template/system/ifElse/type';
import { ModuleDispatchProps } from '@fastgpt/global/core/workflow/runtime/type';
import { getElseIFLabel, getHandleId } from '@fastgpt/global/core/workflow/utils';
import { getReferenceVariableValue } from '@fastgpt/global/core/workflow/runtime/utils';
import {
getReferenceArrayValue,
getReferenceVariableValue
} from '@fastgpt/global/core/workflow/runtime/utils';

type Props = ModuleDispatchProps<{
[NodeInputKeyEnum.condition]: IfElseConditionType;
Expand Down Expand Up @@ -105,11 +108,16 @@ function getResult(
const listResult = list.map((item) => {
const { variable, condition: variableCondition, value } = item;

const inputValue = getReferenceVariableValue({
let inputValue = getReferenceVariableValue({
value: variable,
variables,
nodes: runtimeNodes
});
inputValue = getReferenceArrayValue({
value: inputValue,
nodes: runtimeNodes,
variables
});

return checkCondition(variableCondition as VariableConditionEnum, inputValue, value || '');
});
Expand Down
14 changes: 12 additions & 2 deletions packages/service/core/workflow/dispatch/tools/runUpdateVar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import {
SseResponseEventEnum
} from '@fastgpt/global/core/workflow/runtime/constants';
import { DispatchNodeResultType } from '@fastgpt/global/core/workflow/runtime/type';
import { getReferenceVariableValue } from '@fastgpt/global/core/workflow/runtime/utils';
import {
getReferenceArrayValue,
getReferenceVariableValue
} from '@fastgpt/global/core/workflow/runtime/utils';
import { TUpdateListItem } from '@fastgpt/global/core/workflow/template/system/variableUpdate/type';
import { ModuleDispatchProps } from '@fastgpt/global/core/workflow/runtime/type';
import { removeSystemVariable, valueTypeFormat } from '../utils';
Expand Down Expand Up @@ -40,11 +43,18 @@ export const dispatchUpdateVariable = async (props: Props): Promise<Response> =>
})
: formatValue;
} else {
return getReferenceVariableValue({
let value = getReferenceVariableValue({
value: item.value,
variables,
nodes: runtimeNodes
});
value = getReferenceArrayValue({
value,
nodes: runtimeNodes,
variables
});

return value;
}
})();

Expand Down
131 changes: 85 additions & 46 deletions packages/web/components/common/MySelect/MultipleRowSelect.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React, { useRef, useCallback, useState } from 'react';
import { Button, useDisclosure, Box, Flex, useOutsideClick } from '@chakra-ui/react';
import { Button, useDisclosure, Box, Flex, useOutsideClick, Checkbox } from '@chakra-ui/react';
import { MultipleSelectProps } from './type';
import EmptyTip from '../EmptyTip';
import { useTranslation } from 'next-i18next';
import MyIcon from '../../common/Icon';
import { ChevronDownIcon } from '@chakra-ui/icons';

const MultipleRowSelect = ({
placeholder,
Expand All @@ -14,12 +15,14 @@ const MultipleRowSelect = ({
maxH = 300,
onSelect,
popDirection = 'bottom',
styles
styles,
isArray = false
}: MultipleSelectProps) => {
const { t } = useTranslation();
const ref = useRef<HTMLDivElement>(null);
const { isOpen, onOpen, onClose } = useDisclosure();
const [cloneValue, setCloneValue] = useState(value);

const [navigationPath, setNavigationPath] = useState<string[]>([]);

useOutsideClick({
ref: ref,
Expand All @@ -28,59 +31,80 @@ const MultipleRowSelect = ({

const RenderList = useCallback(
({ index, list }: { index: number; list: MultipleSelectProps['list'] }) => {
const selectedValue = cloneValue[index];
const selectedIndex = list.findIndex((item) => item.value === selectedValue);
const currentNav = navigationPath[index];
const selectedIndex = list.findIndex((item) => item.value === currentNav);
const children = list[selectedIndex]?.children || [];
const hasChildren = list.some((item) => item.children && item.children?.length > 0);

const handleSelect = (item: any) => {
if (hasChildren) {
// Update parent menu path
const newPath = [...navigationPath];
newPath[index] = item.value;
// Clear sub paths
newPath.splice(index + 1);
setNavigationPath(newPath);
} else {
if (!isArray) {
onSelect([navigationPath[0], item.value]);
onClose();
} else {
const parentValue = navigationPath[0];
const newValues = [...value];
const newValue = [parentValue, item.value];

if (newValues.some((v) => v[0] === parentValue && v[1] === item.value)) {
onSelect(newValues.filter((v) => !(v[0] === parentValue && v[1] === item.value)));
} else {
onSelect([...newValues, newValue]);
}
}
}
};

return (
<>
<Box
className="nowheel"
flex={'1 0 auto'}
// width={0}
px={2}
borderLeft={index !== 0 ? 'base' : 'none'}
maxH={`${maxH}px`}
overflowY={'auto'}
whiteSpace={'nowrap'}
>
{list.map((item) => (
<Flex
key={item.value}
py={2}
cursor={'pointer'}
px={2}
borderRadius={'md'}
_hover={{
bg: 'primary.50',
color: 'primary.600'
}}
onClick={() => {
const newValue = [...cloneValue];
{list.map((item) => {
const isSelected = item.value === currentNav;
const showCheckbox = isArray && index !== 0;
const isChecked =
showCheckbox &&
value.some((v) => v[1] === item.value && v[0] === navigationPath[0]);

if (item.value === selectedValue) {
newValue[index] = undefined;
setCloneValue(newValue);
onSelect(newValue);
} else {
newValue[index] = item.value;
setCloneValue(newValue);
if (!hasChildren) {
onSelect(newValue);
onClose();
}
}
}}
{...(item.value === selectedValue
? {
color: 'primary.600'
}
: {})}
>
{item.label}
</Flex>
))}
return (
<Flex
key={item.value}
py={2}
cursor={'pointer'}
px={2}
borderRadius={'md'}
_hover={{
bg: 'primary.50',
color: 'primary.600'
}}
onClick={() => handleSelect(item)}
{...(isSelected ? { color: 'primary.600' } : {})}
>
{showCheckbox && (
<Checkbox
isChecked={isChecked}
icon={<MyIcon name={'common/check'} w={'12px'} />}
mr={1}
/>
)}
{item.label}
</Flex>
);
})}
{list.length === 0 && (
<EmptyTip
text={emptyTip ?? t('common:common.MultipleRowSelect.No data')}
Expand All @@ -93,28 +117,39 @@ const MultipleRowSelect = ({
</>
);
},
[cloneValue]
[navigationPath, value, isArray, onSelect]
);

const onOpenSelect = useCallback(() => {
setCloneValue(value);
setNavigationPath(isArray ? [] : [value[0]?.[0], value[0]?.[1]]);
onOpen();
}, [value, onOpen]);
}, [value, isArray, onOpen]);

return (
<Box ref={ref} position={'relative'}>
<Button
<Flex
justifyContent={'space-between'}
alignItems={'center'}
overflow={'auto'}
width={'100%'}
variant={'whitePrimaryOutline'}
size={'lg'}
fontSize={'sm'}
px={3}
py={1}
minH={10}
maxH={24}
outline={'none'}
rightIcon={<MyIcon name={'core/chat/chevronDown'} w={4} color={'myGray.500'} />}
border={'1px solid'}
borderRadius={'md'}
bg={'white'}
_active={{
transform: 'none'
}}
_hover={{
borderColor: 'primary.500'
}}
{...(isOpen
? {
borderColor: 'primary.600',
Expand All @@ -127,9 +162,13 @@ const MultipleRowSelect = ({
})}
{...styles}
onClick={() => (isOpen ? onClose() : onOpenSelect())}
className="nowheel"
>
<Box>{label ?? placeholder}</Box>
</Button>
<Flex alignItems={'center'} ml={1}>
<ChevronDownIcon />
</Flex>
</Flex>
{isOpen && (
<Box
position={'absolute'}
Expand Down
Loading
Loading