-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathclaude-code-plugins.ts
More file actions
161 lines (137 loc) · 4.24 KB
/
claude-code-plugins.ts
File metadata and controls
161 lines (137 loc) · 4.24 KB
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
import fs from 'fs';
import os from 'os';
import path from 'path';
import type { AutocompleteItem } from '@mariozechner/pi-tui';
import type { AiProviderId } from 'cli/ai/providers';
const CLAUDE_CODE_PLUGIN_PROVIDERS: AiProviderId[] = [ 'anthropic-claude', 'anthropic-api-key' ];
export function isClaudeCodePluginProvider( provider?: AiProviderId ): boolean {
return provider !== undefined && CLAUDE_CODE_PLUGIN_PROVIDERS.includes( provider );
}
interface InstalledPluginEntry {
scope: string;
installPath: string;
}
interface InstalledPluginsManifest {
version: number;
plugins: Record< string, InstalledPluginEntry[] >;
}
function readInstalledPlugins(): InstalledPluginEntry[] {
const manifestPath = path.join( os.homedir(), '.claude', 'plugins', 'installed_plugins.json' );
try {
const content = fs.readFileSync( manifestPath, 'utf8' );
const manifest: InstalledPluginsManifest = JSON.parse( content );
const entries: InstalledPluginEntry[] = [];
for ( const pluginEntries of Object.values( manifest.plugins ) ) {
for ( const entry of pluginEntries ) {
if ( entry.installPath && fs.existsSync( entry.installPath ) ) {
entries.push( entry );
}
}
}
return entries;
} catch {
return [];
}
}
export function getClaudeCodePlugins(): Array< { type: 'local'; path: string } > {
return readInstalledPlugins().map( ( entry ) => ( {
type: 'local' as const,
path: entry.installPath,
} ) );
}
function parseFrontmatter( content: string ): { name?: string; description?: string } {
const match = content.match( /^---\s*\n([\s\S]*?)\n---/ );
if ( ! match ) {
return {};
}
const frontmatter = match[ 1 ];
const nameMatch = frontmatter.match( /^name:\s*["']?([^"'\n]+)["']?\s*$/m );
const descMatch = frontmatter.match( /^description:\s*["']?([^"'\n]+)["']?\s*$/m );
return {
name: nameMatch?.[ 1 ]?.trim(),
description: descMatch?.[ 1 ]?.trim(),
};
}
function readPluginName( pluginPath: string ): string | undefined {
const pluginJsonPath = path.join( pluginPath, '.claude-plugin', 'plugin.json' );
try {
const content = fs.readFileSync( pluginJsonPath, 'utf8' );
const json = JSON.parse( content );
return typeof json.name === 'string' ? json.name : undefined;
} catch {
return undefined;
}
}
function scanSkills( pluginPath: string, pluginName: string ): AutocompleteItem[] {
const skillsDir = path.join( pluginPath, 'skills' );
const items: AutocompleteItem[] = [];
let skillDirs: string[];
try {
skillDirs = fs.readdirSync( skillsDir );
} catch {
return items;
}
for ( const skillDir of skillDirs ) {
const skillPath = path.join( skillsDir, skillDir, 'SKILL.md' );
try {
const content = fs.readFileSync( skillPath, 'utf8' );
const { name, description } = parseFrontmatter( content );
if ( name ) {
items.push( {
value: `${ pluginName }:${ name }`,
label: name,
description: `(${ pluginName }) ${ description ?? '' }`.trim(),
} );
}
} catch {
// Skip unreadable skills
}
}
return items;
}
function scanMarkdownDir(
pluginPath: string,
dirName: string,
pluginName: string
): AutocompleteItem[] {
const dir = path.join( pluginPath, dirName );
const items: AutocompleteItem[] = [];
let files: string[];
try {
files = fs.readdirSync( dir ).filter( ( f ) => f.endsWith( '.md' ) );
} catch {
return items;
}
for ( const file of files ) {
const filePath = path.join( dir, file );
try {
const content = fs.readFileSync( filePath, 'utf8' );
const { name, description } = parseFrontmatter( content );
const baseName = name ?? path.basename( file, '.md' );
items.push( {
value: `${ pluginName }:${ baseName }`,
label: `${ pluginName }:${ baseName }`,
description: description ?? '',
} );
} catch {
// Skip unreadable files
}
}
return items;
}
export function getClaudeCodeSkillCommands(): AutocompleteItem[] {
const entries = readInstalledPlugins();
const items: AutocompleteItem[] = [];
for ( const entry of entries ) {
const pluginName = readPluginName( entry.installPath );
if ( ! pluginName ) {
continue;
}
items.push(
...scanSkills( entry.installPath, pluginName ),
...scanMarkdownDir( entry.installPath, 'commands', pluginName ),
...scanMarkdownDir( entry.installPath, 'agents', pluginName )
);
}
return items;
}