File size: 11,888 Bytes
064bfd6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 | import { useCallback, useEffect } from 'react'
import type { Command } from '../commands.js'
import { useNotifications } from '../context/notifications.js'
import {
type AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
logEvent,
} from '../services/analytics/index.js'
import { reinitializeLspServerManager } from '../services/lsp/manager.js'
import { useAppState, useSetAppState } from '../state/AppState.js'
import type { AgentDefinition } from '../tools/AgentTool/loadAgentsDir.js'
import { count } from '../utils/array.js'
import { logForDebugging } from '../utils/debug.js'
import { logForDiagnosticsNoPII } from '../utils/diagLogs.js'
import { toError } from '../utils/errors.js'
import { logError } from '../utils/log.js'
import { loadPluginAgents } from '../utils/plugins/loadPluginAgents.js'
import { getPluginCommands } from '../utils/plugins/loadPluginCommands.js'
import { loadPluginHooks } from '../utils/plugins/loadPluginHooks.js'
import { loadPluginLspServers } from '../utils/plugins/lspPluginIntegration.js'
import { loadPluginMcpServers } from '../utils/plugins/mcpPluginIntegration.js'
import { detectAndUninstallDelistedPlugins } from '../utils/plugins/pluginBlocklist.js'
import { getFlaggedPlugins } from '../utils/plugins/pluginFlagging.js'
import { loadAllPlugins } from '../utils/plugins/pluginLoader.js'
/**
* Hook to manage plugin state and synchronize with AppState.
*
* On mount: loads all plugins, runs delisting enforcement, surfaces flagged-
* plugin notifications, populates AppState.plugins. This is the initial
* Layer-3 load — subsequent refresh goes through /reload-plugins.
*
* On needsRefresh: shows a notification directing the user to /reload-plugins.
* Does NOT auto-refresh. All Layer-3 swap (commands, agents, hooks, MCP)
* goes through refreshActivePlugins() via /reload-plugins for one consistent
* mental model. See Outline: declarative-settings-hXHBMDIf4b PR 5c.
*/
export function useManagePlugins({
enabled = true,
}: {
enabled?: boolean
} = {}) {
const setAppState = useSetAppState()
const needsRefresh = useAppState(s => s.plugins.needsRefresh)
const { addNotification } = useNotifications()
// Initial plugin load. Runs once on mount. NOT used for refresh — all
// post-mount refresh goes through /reload-plugins → refreshActivePlugins().
// Unlike refreshActivePlugins, this also runs delisting enforcement and
// flagged-plugin notifications (session-start concerns), and does NOT bump
// mcp.pluginReconnectKey (MCP effects fire on their own mount).
const initialPluginLoad = useCallback(async () => {
try {
// Load all plugins - capture errors array
const { enabled, disabled, errors } = await loadAllPlugins()
// Detect delisted plugins, auto-uninstall them, and record as flagged.
await detectAndUninstallDelistedPlugins()
// Notify if there are flagged plugins pending dismissal
const flagged = getFlaggedPlugins()
if (Object.keys(flagged).length > 0) {
addNotification({
key: 'plugin-delisted-flagged',
text: 'Plugins flagged. Check /plugins',
color: 'warning',
priority: 'high',
})
}
// Load commands, agents, and hooks with individual error handling
// Errors are added to the errors array for user visibility in Doctor UI
let commands: Command[] = []
let agents: AgentDefinition[] = []
try {
commands = await getPluginCommands()
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : String(error)
errors.push({
type: 'generic-error',
source: 'plugin-commands',
error: `Failed to load plugin commands: ${errorMessage}`,
})
}
try {
agents = await loadPluginAgents()
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : String(error)
errors.push({
type: 'generic-error',
source: 'plugin-agents',
error: `Failed to load plugin agents: ${errorMessage}`,
})
}
try {
await loadPluginHooks()
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : String(error)
errors.push({
type: 'generic-error',
source: 'plugin-hooks',
error: `Failed to load plugin hooks: ${errorMessage}`,
})
}
// Load MCP server configs per plugin to get an accurate count.
// LoadedPlugin.mcpServers is not populated by loadAllPlugins — it's a
// cache slot that extractMcpServersFromPlugins fills later, which races
// with this metric. Calling loadPluginMcpServers directly (as
// cli/handlers/plugins.ts does) gives the correct count and also
// warms the cache for the MCP connection manager.
//
// Runs BEFORE setAppState so any errors pushed by these loaders make it
// into AppState.plugins.errors (Doctor UI), not just telemetry.
const mcpServerCounts = await Promise.all(
enabled.map(async p => {
if (p.mcpServers) return Object.keys(p.mcpServers).length
const servers = await loadPluginMcpServers(p, errors)
if (servers) p.mcpServers = servers
return servers ? Object.keys(servers).length : 0
}),
)
const mcp_count = mcpServerCounts.reduce((sum, n) => sum + n, 0)
// LSP: the primary fix for issue #15521 is in refresh.ts (via
// performBackgroundPluginInstallations → refreshActivePlugins, which
// clears caches first). This reinit is defensive — it reads the same
// memoized loadAllPlugins() result as the original init unless a cache
// invalidation happened between main.tsx:3203 and REPL mount (e.g.
// seed marketplace registration or policySettings hot-reload).
const lspServerCounts = await Promise.all(
enabled.map(async p => {
if (p.lspServers) return Object.keys(p.lspServers).length
const servers = await loadPluginLspServers(p, errors)
if (servers) p.lspServers = servers
return servers ? Object.keys(servers).length : 0
}),
)
const lsp_count = lspServerCounts.reduce((sum, n) => sum + n, 0)
reinitializeLspServerManager()
// Update AppState - merge errors to preserve LSP errors
setAppState(prevState => {
// Keep existing LSP/non-plugin-loading errors (source 'lsp-manager' or 'plugin:*')
const existingLspErrors = prevState.plugins.errors.filter(
e => e.source === 'lsp-manager' || e.source.startsWith('plugin:'),
)
// Deduplicate: remove existing LSP errors that are also in new errors
const newErrorKeys = new Set(
errors.map(e =>
e.type === 'generic-error'
? `generic-error:${e.source}:${e.error}`
: `${e.type}:${e.source}`,
),
)
const filteredExisting = existingLspErrors.filter(e => {
const key =
e.type === 'generic-error'
? `generic-error:${e.source}:${e.error}`
: `${e.type}:${e.source}`
return !newErrorKeys.has(key)
})
const mergedErrors = [...filteredExisting, ...errors]
return {
...prevState,
plugins: {
...prevState.plugins,
enabled,
disabled,
commands,
errors: mergedErrors,
},
}
})
logForDebugging(
`Loaded plugins - Enabled: ${enabled.length}, Disabled: ${disabled.length}, Commands: ${commands.length}, Agents: ${agents.length}, Errors: ${errors.length}`,
)
// Count component types across enabled plugins
const hook_count = enabled.reduce((sum, p) => {
if (!p.hooksConfig) return sum
return (
sum +
Object.values(p.hooksConfig).reduce(
(s, matchers) =>
s + (matchers?.reduce((h, m) => h + m.hooks.length, 0) ?? 0),
0,
)
)
}, 0)
return {
enabled_count: enabled.length,
disabled_count: disabled.length,
inline_count: count(enabled, p => p.source.endsWith('@inline')),
marketplace_count: count(enabled, p => !p.source.endsWith('@inline')),
error_count: errors.length,
skill_count: commands.length,
agent_count: agents.length,
hook_count,
mcp_count,
lsp_count,
// Ant-only: which plugins are enabled, to correlate with RSS/FPS.
// Kept separate from base metrics so it doesn't flow into
// logForDiagnosticsNoPII.
ant_enabled_names:
process.env.USER_TYPE === 'ant' && enabled.length > 0
? (enabled
.map(p => p.name)
.sort()
.join(
',',
) as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS)
: undefined,
}
} catch (error) {
// Only plugin loading errors should reach here - log for monitoring
const errorObj = toError(error)
logError(errorObj)
logForDebugging(`Error loading plugins: ${error}`)
// Set empty state on error, but preserve LSP errors and add the new error
setAppState(prevState => {
// Keep existing LSP/non-plugin-loading errors
const existingLspErrors = prevState.plugins.errors.filter(
e => e.source === 'lsp-manager' || e.source.startsWith('plugin:'),
)
const newError = {
type: 'generic-error' as const,
source: 'plugin-system',
error: errorObj.message,
}
return {
...prevState,
plugins: {
...prevState.plugins,
enabled: [],
disabled: [],
commands: [],
errors: [...existingLspErrors, newError],
},
}
})
return {
enabled_count: 0,
disabled_count: 0,
inline_count: 0,
marketplace_count: 0,
error_count: 1,
skill_count: 0,
agent_count: 0,
hook_count: 0,
mcp_count: 0,
lsp_count: 0,
load_failed: true,
ant_enabled_names: undefined,
}
}
}, [setAppState, addNotification])
// Load plugins on mount and emit telemetry
useEffect(() => {
if (!enabled) return
void initialPluginLoad().then(metrics => {
const { ant_enabled_names, ...baseMetrics } = metrics
const allMetrics = {
...baseMetrics,
has_custom_plugin_cache_dir: !!process.env.CLAUDE_CODE_PLUGIN_CACHE_DIR,
}
logEvent('tengu_plugins_loaded', {
...allMetrics,
...(ant_enabled_names !== undefined && {
enabled_names: ant_enabled_names,
}),
})
logForDiagnosticsNoPII('info', 'tengu_plugins_loaded', allMetrics)
})
}, [initialPluginLoad, enabled])
// Plugin state changed on disk (background reconcile, /plugin menu,
// external settings edit). Show a notification; user runs /reload-plugins
// to apply. The previous auto-refresh here had a stale-cache bug (only
// cleared loadAllPlugins, downstream memoized loaders returned old data)
// and was incomplete (no MCP, no agentDefinitions). /reload-plugins
// handles all of that correctly via refreshActivePlugins().
useEffect(() => {
if (!enabled || !needsRefresh) return
addNotification({
key: 'plugin-reload-pending',
text: 'Plugins changed. Run /reload-plugins to activate.',
color: 'suggestion',
priority: 'low',
})
// Do NOT auto-refresh. Do NOT reset needsRefresh — /reload-plugins
// consumes it via refreshActivePlugins().
}, [enabled, needsRefresh, addNotification])
}
|