File size: 4,980 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 | /**
* Built-in Plugin Registry
*
* Manages built-in plugins that ship with the CLI and can be enabled/disabled
* by users via the /plugin UI.
*
* Built-in plugins differ from bundled skills (src/skills/bundled/) in that:
* - They appear in the /plugin UI under a "Built-in" section
* - Users can enable/disable them (persisted to user settings)
* - They can provide multiple components (skills, hooks, MCP servers)
*
* Plugin IDs use the format `{name}@builtin` to distinguish them from
* marketplace plugins (`{name}@{marketplace}`).
*/
import type { Command } from '../commands.js'
import type { BundledSkillDefinition } from '../skills/bundledSkills.js'
import type { BuiltinPluginDefinition, LoadedPlugin } from '../types/plugin.js'
import { getSettings_DEPRECATED } from '../utils/settings/settings.js'
const BUILTIN_PLUGINS: Map<string, BuiltinPluginDefinition> = new Map()
export const BUILTIN_MARKETPLACE_NAME = 'builtin'
/**
* Register a built-in plugin. Call this from initBuiltinPlugins() at startup.
*/
export function registerBuiltinPlugin(
definition: BuiltinPluginDefinition,
): void {
BUILTIN_PLUGINS.set(definition.name, definition)
}
/**
* Check if a plugin ID represents a built-in plugin (ends with @builtin).
*/
export function isBuiltinPluginId(pluginId: string): boolean {
return pluginId.endsWith(`@${BUILTIN_MARKETPLACE_NAME}`)
}
/**
* Get a specific built-in plugin definition by name.
* Useful for the /plugin UI to show the skills/hooks/MCP list without
* a marketplace lookup.
*/
export function getBuiltinPluginDefinition(
name: string,
): BuiltinPluginDefinition | undefined {
return BUILTIN_PLUGINS.get(name)
}
/**
* Get all registered built-in plugins as LoadedPlugin objects, split into
* enabled/disabled based on user settings (with defaultEnabled as fallback).
* Plugins whose isAvailable() returns false are omitted entirely.
*/
export function getBuiltinPlugins(): {
enabled: LoadedPlugin[]
disabled: LoadedPlugin[]
} {
const settings = getSettings_DEPRECATED()
const enabled: LoadedPlugin[] = []
const disabled: LoadedPlugin[] = []
for (const [name, definition] of BUILTIN_PLUGINS) {
if (definition.isAvailable && !definition.isAvailable()) {
continue
}
const pluginId = `${name}@${BUILTIN_MARKETPLACE_NAME}`
const userSetting = settings?.enabledPlugins?.[pluginId]
// Enabled state: user preference > plugin default > true
const isEnabled =
userSetting !== undefined
? userSetting === true
: (definition.defaultEnabled ?? true)
const plugin: LoadedPlugin = {
name,
manifest: {
name,
description: definition.description,
version: definition.version,
},
path: BUILTIN_MARKETPLACE_NAME, // sentinel — no filesystem path
source: pluginId,
repository: pluginId,
enabled: isEnabled,
isBuiltin: true,
hooksConfig: definition.hooks,
mcpServers: definition.mcpServers,
}
if (isEnabled) {
enabled.push(plugin)
} else {
disabled.push(plugin)
}
}
return { enabled, disabled }
}
/**
* Get skills from enabled built-in plugins as Command objects.
* Skills from disabled plugins are not returned.
*/
export function getBuiltinPluginSkillCommands(): Command[] {
const { enabled } = getBuiltinPlugins()
const commands: Command[] = []
for (const plugin of enabled) {
const definition = BUILTIN_PLUGINS.get(plugin.name)
if (!definition?.skills) continue
for (const skill of definition.skills) {
commands.push(skillDefinitionToCommand(skill))
}
}
return commands
}
/**
* Clear built-in plugins registry (for testing).
*/
export function clearBuiltinPlugins(): void {
BUILTIN_PLUGINS.clear()
}
// --
function skillDefinitionToCommand(definition: BundledSkillDefinition): Command {
return {
type: 'prompt',
name: definition.name,
description: definition.description,
hasUserSpecifiedDescription: true,
allowedTools: definition.allowedTools ?? [],
argumentHint: definition.argumentHint,
whenToUse: definition.whenToUse,
model: definition.model,
disableModelInvocation: definition.disableModelInvocation ?? false,
userInvocable: definition.userInvocable ?? true,
contentLength: 0,
// 'bundled' not 'builtin' — 'builtin' in Command.source means hardcoded
// slash commands (/help, /clear). Using 'bundled' keeps these skills in
// the Skill tool's listing, analytics name logging, and prompt-truncation
// exemption. The user-toggleable aspect is tracked on LoadedPlugin.isBuiltin.
source: 'bundled',
loadedFrom: 'bundled',
hooks: definition.hooks,
context: definition.context,
agent: definition.agent,
isEnabled: definition.isEnabled ?? (() => true),
isHidden: !(definition.userInvocable ?? true),
progressMessage: 'running',
getPromptForCommand: definition.getPromptForCommand,
}
}
|