From 30377319d8ddd18cb28512eee5715013a0ba2999 Mon Sep 17 00:00:00 2001 From: balibabu Date: Tue, 9 Dec 2025 17:59:49 +0800 Subject: [PATCH] Fix: The variables in the message node are not displaying correctly. #11839 (#11841) ### What problem does this PR solve? Fix: The variables in the message node are not displaying correctly. #11839 ### Type of change - [x] Bug Fix (non-breaking change which fixes an issue) --- web/src/locales/zh.ts | 2 +- .../pages/agent/canvas/node/message-node.tsx | 6 +- .../agent/canvas/node/variable-display.tsx | 89 +++++++++++++++++++ 3 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 web/src/pages/agent/canvas/node/variable-display.tsx diff --git a/web/src/locales/zh.ts b/web/src/locales/zh.ts index 5d114594c..8c879ffc5 100644 --- a/web/src/locales/zh.ts +++ b/web/src/locales/zh.ts @@ -1059,7 +1059,7 @@ General:实体和关系提取提示来自 GitHub - microsoft/graphrag:基于 exceptionMethod: '异常处理方法', maxRounds: '最大反思轮数', delayEfterError: '错误后延迟', - maxRetries: '最大反思轮数', + maxRetries: '最大重试轮数', advancedSettings: '高级设置', addTools: '添加工具', sysPromptDefultValue: ` diff --git a/web/src/pages/agent/canvas/node/message-node.tsx b/web/src/pages/agent/canvas/node/message-node.tsx index aff1cf171..9ad071bc4 100644 --- a/web/src/pages/agent/canvas/node/message-node.tsx +++ b/web/src/pages/agent/canvas/node/message-node.tsx @@ -1,6 +1,7 @@ import { NodeCollapsible } from '@/components/collapse'; import { IMessageNode } from '@/interfaces/database/flow'; import { cn } from '@/lib/utils'; +import { useGetVariableLabelOrTypeByValue } from '@/pages/agent/hooks/use-get-begin-query'; import { NodeProps } from '@xyflow/react'; import classNames from 'classnames'; import { get } from 'lodash'; @@ -11,9 +12,12 @@ import styles from './index.less'; import NodeHeader from './node-header'; import { NodeWrapper } from './node-wrapper'; import { ToolBar } from './toolbar'; +import { VariableDisplay } from './variable-display'; function InnerMessageNode({ id, data, selected }: NodeProps) { const messages: string[] = get(data, 'form.content', []); + const { getLabel } = useGetVariableLabelOrTypeByValue({ nodeId: id }); + return ( @@ -32,7 +36,7 @@ function InnerMessageNode({ id, data, selected }: NodeProps) { {(x, idx) => ( - {x} + )} diff --git a/web/src/pages/agent/canvas/node/variable-display.tsx b/web/src/pages/agent/canvas/node/variable-display.tsx new file mode 100644 index 000000000..ceca80faa --- /dev/null +++ b/web/src/pages/agent/canvas/node/variable-display.tsx @@ -0,0 +1,89 @@ +import i18n from '@/locales/config'; +import { BeginId } from '@/pages/agent/constant'; +import { ReactNode } from 'react'; + +const prefix = BeginId + '@'; + +interface VariableDisplayProps { + content: string; + getLabel?: (value?: string) => string | ReactNode; +} + +// This component mimics the VariableNode's decorate function from PromptEditor +function VariableNodeDisplay({ + value, + label, +}: { + value: string; + label: ReactNode; +}) { + let content: ReactNode = {label}; + + if (value.startsWith(prefix)) { + content = ( +
+ {i18n.t(`flow.begin`)} / {content} +
+ ); + } + + return
{content}
; +} + +export function VariableDisplay({ content, getLabel }: VariableDisplayProps) { + if (!content) return null; + + // Regular expression to match content within {} + const regex = /{([^}]*)}/g; + let match; + let lastIndex = 0; + const elements: ReactNode[] = []; + + const findLabelByValue = (value: string) => { + if (getLabel) { + const label = getLabel(value); + return label; + } + return null; + }; + + while ((match = regex.exec(content)) !== null) { + const { 1: variableValue, index, 0: template } = match; + + // Add the previous text part (if any) + if (index > lastIndex) { + elements.push( + {content.slice(lastIndex, index)}, + ); + } + + // Try to find the label + const label = findLabelByValue(variableValue); + + if (label && label !== variableValue) { + // If we found a valid label, render as variable node + elements.push( + , + ); + } else { + // If no label found, keep as original text + elements.push({template}); + } + + // Update index + lastIndex = regex.lastIndex; + } + + // Add the last part of text (if any) + if (lastIndex < content.length) { + elements.push( + {content.slice(lastIndex)}, + ); + } + + return <>{elements.length > 0 ? elements : content}; +}