mirror of
https://github.com/langgenius/dify.git
synced 2026-04-21 11:17:38 +08:00
- move MCP availability context to block-selector/context and update imports - preserve sandbox gating, parent-provider inheritance, and blockedBy semantics - add context tests on top of refactor baseline cases - regenerate and prune eslint suppressions
64 lines
1.7 KiB
TypeScript
64 lines
1.7 KiB
TypeScript
'use client'
|
|
import type { ReactNode } from 'react'
|
|
import { createContext, useContext, useMemo } from 'react'
|
|
|
|
type MCPToolAvailabilityContextValue = {
|
|
versionSupported?: boolean
|
|
sandboxEnabled?: boolean
|
|
}
|
|
|
|
const MCPToolAvailabilityContext = createContext<MCPToolAvailabilityContextValue | undefined>(undefined)
|
|
|
|
export type MCPToolAvailability = {
|
|
allowed: boolean
|
|
blockedBy?: 'version' | 'sandbox'
|
|
}
|
|
|
|
type ProviderProps = {
|
|
versionSupported?: boolean
|
|
sandboxEnabled?: boolean
|
|
children: ReactNode
|
|
}
|
|
|
|
export function MCPToolAvailabilityProvider({
|
|
versionSupported,
|
|
sandboxEnabled,
|
|
children,
|
|
}: ProviderProps): ReactNode {
|
|
const parent = useContext(MCPToolAvailabilityContext)
|
|
|
|
const value = useMemo<MCPToolAvailabilityContextValue>(() => ({
|
|
versionSupported: versionSupported ?? parent?.versionSupported,
|
|
sandboxEnabled: sandboxEnabled ?? parent?.sandboxEnabled,
|
|
}), [versionSupported, sandboxEnabled, parent])
|
|
|
|
return (
|
|
<MCPToolAvailabilityContext.Provider value={value}>
|
|
{children}
|
|
</MCPToolAvailabilityContext.Provider>
|
|
)
|
|
}
|
|
|
|
// eslint-disable-next-line react-refresh/only-export-components
|
|
export function useMCPToolAvailability(): MCPToolAvailability {
|
|
const context = useContext(MCPToolAvailabilityContext)
|
|
|
|
if (!context)
|
|
return { allowed: true }
|
|
|
|
const versionAllowed = context.versionSupported ?? true
|
|
const sandboxAllowed = context.sandboxEnabled ?? true
|
|
const allowed = versionAllowed && sandboxAllowed
|
|
|
|
let blockedBy: MCPToolAvailability['blockedBy']
|
|
if (!versionAllowed)
|
|
blockedBy = 'version'
|
|
else if (!sandboxAllowed)
|
|
blockedBy = 'sandbox'
|
|
|
|
if (blockedBy)
|
|
return { allowed, blockedBy }
|
|
|
|
return { allowed }
|
|
}
|