mirror of
https://github.com/langgenius/dify.git
synced 2026-03-27 01:00:13 +08:00
Frontend: - Migrate deprecated imports: modal→dialog, toast→ui/toast, tooltip→tooltip-plus, portal-to-follow-elem→portal-to-follow-elem-plus, select→ui/select, confirm→alert-dialog - Replace next/* with @/next/* wrapper modules - Convert TypeScript enums to const objects (erasable-syntax-only) - Replace all `any` types with `unknown` or specific types in workflow types - Fix unused vars, react-hooks-extra, react-refresh/only-export-components - Extract InteractionMode to separate module, tool-block commands to commands.ts Backend: - Fix pyrefly errors: type narrowing, null guards, getattr patterns - Remove unused TYPE_CHECKING imports in LLM node - Add ignore_imports entries to .importlinter for dify_graph boundary violations Made-with: Cursor
119 lines
3.3 KiB
TypeScript
119 lines
3.3 KiB
TypeScript
import { BlockEnum } from '@/app/components/workflow/types'
|
|
|
|
/* eslint-disable ts/no-redeclare -- const + type share names (erasable enum replacement) */
|
|
export const NodeSelectorScene = {
|
|
Workflow: 'workflow',
|
|
Chatflow: 'chatflow',
|
|
RagPipeline: 'rag-pipeline',
|
|
Subgraph: 'subgraph',
|
|
} as const
|
|
export type NodeSelectorScene = typeof NodeSelectorScene[keyof typeof NodeSelectorScene]
|
|
|
|
export const NodeSelectorSandboxMode = {
|
|
Enabled: 'enabled',
|
|
Disabled: 'disabled',
|
|
Unsupported: 'unsupported',
|
|
} as const
|
|
export type NodeSelectorSandboxMode = typeof NodeSelectorSandboxMode[keyof typeof NodeSelectorSandboxMode]
|
|
|
|
export const NODE_SELECTOR_SCENE_SUPPORTS_SANDBOX: Record<NodeSelectorScene, boolean> = {
|
|
[NodeSelectorScene.Workflow]: true,
|
|
[NodeSelectorScene.Chatflow]: true,
|
|
[NodeSelectorScene.RagPipeline]: false,
|
|
[NodeSelectorScene.Subgraph]: true,
|
|
}
|
|
|
|
type NodeAvailabilityRule = {
|
|
sandboxOnly?: boolean
|
|
hiddenWhenSandboxEnabled?: boolean
|
|
hiddenInScenes?: NodeSelectorScene[]
|
|
}
|
|
|
|
export const NODE_SELECTOR_AVAILABILITY_RULES: Partial<Record<BlockEnum, NodeAvailabilityRule>> = {
|
|
[BlockEnum.Command]: { sandboxOnly: true },
|
|
[BlockEnum.FileUpload]: { sandboxOnly: true },
|
|
[BlockEnum.Agent]: { hiddenWhenSandboxEnabled: true },
|
|
[BlockEnum.HumanInput]: { hiddenInScenes: [NodeSelectorScene.RagPipeline] },
|
|
}
|
|
|
|
export type NodeSelectorAvailabilityContext = {
|
|
scene: NodeSelectorScene
|
|
sandboxMode: NodeSelectorSandboxMode
|
|
}
|
|
|
|
type BuildNodeSelectorAvailabilityContextProps = {
|
|
scene: NodeSelectorScene
|
|
isSandboxRuntime?: boolean
|
|
isSandboxFeatureEnabled?: boolean
|
|
supportsSandbox?: boolean
|
|
}
|
|
|
|
const resolveSandboxMode = ({
|
|
scene,
|
|
isSandboxRuntime = false,
|
|
isSandboxFeatureEnabled = false,
|
|
supportsSandbox = NODE_SELECTOR_SCENE_SUPPORTS_SANDBOX[scene],
|
|
}: BuildNodeSelectorAvailabilityContextProps): NodeSelectorSandboxMode => {
|
|
if (!supportsSandbox)
|
|
return NodeSelectorSandboxMode.Unsupported
|
|
|
|
return (isSandboxRuntime || isSandboxFeatureEnabled)
|
|
? NodeSelectorSandboxMode.Enabled
|
|
: NodeSelectorSandboxMode.Disabled
|
|
}
|
|
|
|
export const buildNodeSelectorAvailabilityContext = ({
|
|
scene,
|
|
isSandboxRuntime,
|
|
isSandboxFeatureEnabled,
|
|
supportsSandbox,
|
|
}: BuildNodeSelectorAvailabilityContextProps): NodeSelectorAvailabilityContext => {
|
|
return {
|
|
scene,
|
|
sandboxMode: resolveSandboxMode({
|
|
scene,
|
|
isSandboxRuntime,
|
|
isSandboxFeatureEnabled,
|
|
supportsSandbox,
|
|
}),
|
|
}
|
|
}
|
|
|
|
export const isNodeAvailableInSelector = (
|
|
nodeType: BlockEnum,
|
|
{ scene, sandboxMode }: NodeSelectorAvailabilityContext,
|
|
) => {
|
|
const rule = NODE_SELECTOR_AVAILABILITY_RULES[nodeType]
|
|
if (!rule)
|
|
return true
|
|
|
|
const sandboxEnabled = sandboxMode === NodeSelectorSandboxMode.Enabled
|
|
if (rule.sandboxOnly && !sandboxEnabled)
|
|
return false
|
|
|
|
if (rule.hiddenWhenSandboxEnabled && sandboxEnabled)
|
|
return false
|
|
|
|
if (rule.hiddenInScenes?.includes(scene))
|
|
return false
|
|
|
|
return true
|
|
}
|
|
|
|
type NodeLike = {
|
|
metaData: {
|
|
type: BlockEnum
|
|
}
|
|
}
|
|
|
|
export const filterNodesForSelector = <T extends NodeLike>(
|
|
nodes: T[],
|
|
context: NodeSelectorAvailabilityContext,
|
|
) => {
|
|
const filteredNodes = nodes.filter(node => isNodeAvailableInSelector(node.metaData.type, context))
|
|
if (filteredNodes.length === nodes.length)
|
|
return nodes
|
|
|
|
return filteredNodes
|
|
}
|