From b99311baa043f6f24e00de3b9914ed9e3435dd51 Mon Sep 17 00:00:00 2001 From: Joel Date: Mon, 26 Jan 2026 18:08:46 +0800 Subject: [PATCH] chore: remove useless toolid --- .../nodes/llm/components/config-prompt.tsx | 38 +++++++++++++++++-- .../skill/hooks/use-skill-save-manager.tsx | 17 +-------- web/app/components/workflow/utils/index.ts | 1 + .../components/workflow/utils/tool-token.ts | 15 ++++++++ 4 files changed, 51 insertions(+), 20 deletions(-) create mode 100644 web/app/components/workflow/utils/tool-token.ts diff --git a/web/app/components/workflow/nodes/llm/components/config-prompt.tsx b/web/app/components/workflow/nodes/llm/components/config-prompt.tsx index bffe7312e5..662ae70472 100644 --- a/web/app/components/workflow/nodes/llm/components/config-prompt.tsx +++ b/web/app/components/workflow/nodes/llm/components/config-prompt.tsx @@ -17,6 +17,7 @@ import { import AddButton from '@/app/components/workflow/nodes/_base/components/add-button' import Editor from '@/app/components/workflow/nodes/_base/components/prompt/editor' import VarReferenceVars from '@/app/components/workflow/nodes/_base/components/variable/var-reference-vars' +import { extractToolConfigIds } from '@/app/components/workflow/utils' import { cn } from '@/utils/classnames' import { useWorkflowStore } from '../../../store' import { BlockEnum, EditionType, isPromptMessageContext, PromptRole, VarType } from '../../../types' @@ -25,6 +26,28 @@ import ConfigContextItem from './config-context-item' import ConfigPromptItem from './config-prompt-item' const i18nPrefix = 'nodes.llm' + +const cleanupToolMetadata = (content: string, metadata: Record) => { + if (!metadata || typeof metadata !== 'object' || !('tools' in metadata)) + return metadata + const rawTools = (metadata as Record).tools + if (!rawTools || typeof rawTools !== 'object') + return metadata + const toolIds = extractToolConfigIds(content) + const entries = Object.entries(rawTools as Record) + const nextTools = entries.reduce>((acc, [id, value]) => { + if (toolIds.has(id)) + acc[id] = value + return acc + }, {}) + const nextMetadata = { ...(metadata as Record) } + if (Object.keys(nextTools).length > 0) + nextMetadata.tools = nextTools + else + delete nextMetadata.tools + return nextMetadata +} + type Props = { readOnly: boolean nodeId: string @@ -120,8 +143,11 @@ const ConfigPrompt: FC = ({ return (metadata: Record) => { const newPrompt = produce(payload as PromptTemplateItem[], (draft) => { const item = draft[index] - if (!isPromptMessageContext(item)) - (item as PromptItem).metadata = metadata + if (!isPromptMessageContext(item)) { + const content = item.text + const nextMetadata = cleanupToolMetadata(content, metadata) + ; (item as PromptItem).metadata = nextMetadata + } }) onChange(newPrompt) } @@ -228,8 +254,12 @@ const ConfigPrompt: FC = ({ }, [onChange, payload]) const handleCompletionMetadataChange = useCallback((metadata: Record) => { + const promptItem = payload as PromptItem + const contentKey = promptItem.edition_type === EditionType.jinja2 ? 'jinja2_text' : 'text' + const content = (promptItem[contentKey] ?? '') as string + const nextMetadata = cleanupToolMetadata(content, metadata) const newPrompt = produce(payload as PromptItem, (draft) => { - draft.metadata = metadata + draft.metadata = nextMetadata }) onChange(newPrompt) }, [onChange, payload]) @@ -351,7 +381,7 @@ const ConfigPrompt: FC = ({
{}} + onClick={() => { }} />
diff --git a/web/app/components/workflow/skill/hooks/use-skill-save-manager.tsx b/web/app/components/workflow/skill/hooks/use-skill-save-manager.tsx index b93617f43c..b488f5bc24 100644 --- a/web/app/components/workflow/skill/hooks/use-skill-save-manager.tsx +++ b/web/app/components/workflow/skill/hooks/use-skill-save-manager.tsx @@ -6,6 +6,7 @@ import { useCallback, useMemo, useRef } from 'react' import { useTranslation } from 'react-i18next' import Toast from '@/app/components/base/toast' import { useWorkflowStore } from '@/app/components/workflow/store' +import { extractToolConfigIds } from '@/app/components/workflow/utils' import { consoleQuery } from '@/service/client' import { useUpdateAppAssetFileContent } from '@/service/use-app-asset' import { START_TAB_ID } from '../constants' @@ -52,22 +53,6 @@ type SkillSaveProviderProps = { const SkillSaveContext = React.createContext(null) -const TOOL_TOKEN_REGEX = /§\[tool\]\.\[[\w-]+(?:\/[\w-]+)*\]\.\[[\w-]+\]\.\[([a-fA-F0-9-]{36})\]§/g - -const extractToolConfigIds = (content: string) => { - const ids = new Set() - if (!content) - return ids - TOOL_TOKEN_REGEX.lastIndex = 0 - let match = TOOL_TOKEN_REGEX.exec(content) - while (match) { - if (match[1]) - ids.add(match[1]) - match = TOOL_TOKEN_REGEX.exec(content) - } - return ids -} - export const SkillSaveProvider = ({ appId, children, diff --git a/web/app/components/workflow/utils/index.ts b/web/app/components/workflow/utils/index.ts index 715ce081a3..3fd8e5d604 100644 --- a/web/app/components/workflow/utils/index.ts +++ b/web/app/components/workflow/utils/index.ts @@ -5,6 +5,7 @@ export * from './elk-layout' export * from './gen-node-meta-data' export * from './node' export * from './tool' +export * from './tool-token' export * from './variable' export * from './workflow' export * from './workflow-init' diff --git a/web/app/components/workflow/utils/tool-token.ts b/web/app/components/workflow/utils/tool-token.ts new file mode 100644 index 0000000000..ac0f8ea0e5 --- /dev/null +++ b/web/app/components/workflow/utils/tool-token.ts @@ -0,0 +1,15 @@ +const TOOL_TOKEN_REGEX = /§\[tool\]\.\[[\w-]+(?:\/[\w-]+)*\]\.\[[\w-]+\]\.\[([a-fA-F0-9-]{36})\]§/g + +export const extractToolConfigIds = (content: string) => { + const ids = new Set() + if (!content) + return ids + TOOL_TOKEN_REGEX.lastIndex = 0 + let match = TOOL_TOKEN_REGEX.exec(content) + while (match) { + if (match[1]) + ids.add(match[1]) + match = TOOL_TOKEN_REGEX.exec(content) + } + return ids +}