Skip to content

Commit

Permalink
feat: get node variables in prompt editor (#2087)
Browse files Browse the repository at this point in the history
* feat: get node variables in prompt editor

* fix

* fix build

* merge

* fix build

* delete default parent

* fix

* fix
  • Loading branch information
newfish-cmyk authored Jul 23, 2024
1 parent f24e41f commit a4787bc
Show file tree
Hide file tree
Showing 23 changed files with 891 additions and 55 deletions.
72 changes: 70 additions & 2 deletions packages/global/core/workflow/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import {
WorkflowIOValueTypeEnum,
NodeInputKeyEnum,
VariableInputEnum,
variableMap
variableMap,
VARIABLE_NODE_ID
} from './constants';
import { FlowNodeInputItemType, FlowNodeOutputItemType } from './type/io.d';
import { FlowNodeInputItemType, FlowNodeOutputItemType, ReferenceValueProps } from './type/io.d';
import { StoreNodeItemType } from './type/node';
import type {
VariableItemType,
Expand All @@ -23,6 +24,7 @@ import {
} from '../app/constants';
import { IfElseResultEnum } from './template/system/ifElse/constant';
import { RuntimeNodeItemType } from './runtime/type';
import { getReferenceVariableValue } from './runtime/utils';

export const getHandleId = (nodeId: string, type: 'source' | 'target', key: string) => {
return `${nodeId}-${type}-${key}`;
Expand Down Expand Up @@ -226,3 +228,69 @@ export const updatePluginInputByVariables = (
: node
);
};

export function replaceVariableLabel({
text,
nodes,
variables,
runningNode
}: {
text: any;
nodes: RuntimeNodeItemType[];
variables: Record<string, string | number>;
runningNode: RuntimeNodeItemType;
}) {
if (!(typeof text === 'string')) return text;

const globalVariables = Object.keys(variables).map((key) => {
return {
nodeId: VARIABLE_NODE_ID,
id: key,
value: variables[key]
};
});

const nodeVariables = nodes
.map((node) => {
return node.outputs.map((output) => {
return {
nodeId: node.nodeId,
id: output.id,
value: output.value
};
});
})
.flat();

const customInputs = runningNode.inputs.flatMap((item) => {
if (Array.isArray(item.value)) {
return [
{
id: item.key,
value: getReferenceVariableValue({
value: item.value as ReferenceValueProps,
nodes,
variables
}),
nodeId: runningNode.nodeId
}
];
}
return [];
});

const allVariables = [...globalVariables, ...nodeVariables, ...customInputs];

for (const key in allVariables) {
const val = allVariables[key];
const regex = new RegExp(`\\{\\{\\$(${val.nodeId}\\.${val.id})\\$\\}\\}`, 'g');
if (['string', 'number'].includes(typeof val.value)) {
text = text.replace(regex, String(val.value));
} else if (['object'].includes(typeof val.value)) {
text = text.replace(regex, JSON.stringify(val.value));
} else {
continue;
}
}
return text || '';
}
10 changes: 10 additions & 0 deletions packages/service/core/workflow/dispatch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
import { replaceVariable } from '@fastgpt/global/common/string/tools';
import { responseWriteNodeStatus } from '../../../common/response';
import { getSystemTime } from '@fastgpt/global/common/time/timezone';
import { replaceVariableLabel } from '@fastgpt/global/core/workflow/utils';

import { dispatchWorkflowStart } from './init/workflowStart';
import { dispatchChatCompletion } from './chat/oneapi';
Expand Down Expand Up @@ -54,6 +55,7 @@ import { surrenderProcess } from '../../../common/system/tools';
import { dispatchRunCode } from './code/run';
import { dispatchTextEditor } from './tools/textEditor';
import { dispatchCustomFeedback } from './tools/customFeedback';
import { ReferenceValueProps } from '@fastgpt/global/core/workflow/type/io';

const callbackMap: Record<FlowNodeTypeEnum, Function> = {
[FlowNodeTypeEnum.workflowStart]: dispatchWorkflowStart,
Expand Down Expand Up @@ -291,6 +293,14 @@ export async function dispatchWorkFlow(data: Props): Promise<DispatchFlowRespons
// replace {{}} variables
let value = replaceVariable(input.value, variables);

// replace {{$$}} variables
value = replaceVariableLabel({
text: value,
nodes: runtimeNodes,
variables: variables,
runningNode: node
});

// replace reference variables
value = getReferenceVariableValue({
value,
Expand Down
7 changes: 4 additions & 3 deletions packages/web/components/common/Avatar/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { Image } from '@chakra-ui/react';
import { Box, Flex, Image } from '@chakra-ui/react';
import type { ImageProps } from '@chakra-ui/react';
import { LOGO_ICON } from '@fastgpt/global/common/system/constants';
import MyIcon from '../Icon';
Expand All @@ -10,12 +10,13 @@ const Avatar = ({ w = '30px', src, ...props }: ImageProps) => {
const isIcon = !!iconPaths[src as any];

return isIcon ? (
<MyIcon name={src as any} w={w} borderRadius={props.borderRadius} />
<Box {...props}>
<MyIcon name={src as any} w={w} borderRadius={props.borderRadius} />
</Box>
) : (
<Image
fallbackSrc={LOGO_ICON}
fallbackStrategy={'onError'}
// borderRadius={'md'}
objectFit={'contain'}
alt=""
w={w}
Expand Down
1 change: 1 addition & 0 deletions packages/web/components/common/Icon/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ export const iconPaths = {
'core/workflow/template/textConcat': () =>
import('./icons/core/workflow/template/textConcat.svg'),
'core/workflow/template/toolCall': () => import('./icons/core/workflow/template/toolCall.svg'),
'core/workflow/template/variable': () => import('./icons/core/workflow/template/variable.svg'),
'core/workflow/template/variableUpdate': () =>
import('./icons/core/workflow/template/variableUpdate.svg'),
'core/workflow/template/workflowStart': () =>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ContentEditable } from '@lexical/react/LexicalContentEditable';
import { HistoryPlugin } from '@lexical/react/LexicalHistoryPlugin';
import { OnChangePlugin } from '@lexical/react/LexicalOnChangePlugin';
import LexicalErrorBoundary from '@lexical/react/LexicalErrorBoundary';
import VariablePickerPlugin from './plugins/VariablePickerPlugin';
import VariableLabelPickerPlugin from './plugins/VariableLabelPickerPlugin';
import { Box } from '@chakra-ui/react';
import styles from './index.module.scss';
import VariablePlugin from './plugins/VariablePlugin';
Expand All @@ -18,6 +18,8 @@ import { getNanoid } from '@fastgpt/global/common/string/tools';
import FocusPlugin from './plugins/FocusPlugin';
import { textToEditorState } from './utils';
import { MaxLengthPlugin } from './plugins/MaxLengthPlugin';
import { VariableLabelNode } from './plugins/VariableLabelPlugin/node';
import VariableLabelPlugin from './plugins/VariableLabelPlugin';

export default function Editor({
h = 200,
Expand Down Expand Up @@ -51,7 +53,7 @@ export default function Editor({

const initialConfig = {
namespace: 'promptEditor',
nodes: [VariableNode],
nodes: [VariableNode, VariableLabelNode],
editorState: textToEditorState(value),
onError: (error: Error) => {
throw error;
Expand Down Expand Up @@ -127,8 +129,9 @@ export default function Editor({
});
}}
/>
<VariablePickerPlugin variables={variables} />
<VariableLabelPickerPlugin variables={variables} />
<VariablePlugin variables={variables} />
<VariableLabelPlugin variables={variables} />
<OnBlurPlugin onBlur={onBlur} />
</LexicalComposer>
{showResize && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
position: relative;
height: 100%;
width: 100%;
border: 1px solid var(--chakra-colors-borderColor-base);
border: 1px solid rgb(232, 235, 240);
border-radius: var(--chakra-radii-md);
padding: 8px 12px;
background: var(--chakra-colors-gray-50);
background: #fff;

font-size: var(--chakra-fontSizes-sm);
overflow-y: auto;
}
Expand Down
Loading

0 comments on commit a4787bc

Please sign in to comment.