forked from ultraworkers/claw-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloadPluginAgents.ts
More file actions
348 lines (323 loc) · 12.2 KB
/
Copy pathloadPluginAgents.ts
File metadata and controls
348 lines (323 loc) · 12.2 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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import memoize from 'lodash-es/memoize.js'
import { basename } from 'path'
import { isAutoMemoryEnabled } from '../../memdir/paths.js'
import type { AgentColorName } from '../../tools/AgentTool/agentColorManager.js'
import {
type AgentMemoryScope,
loadAgentMemoryPrompt,
} from '../../tools/AgentTool/agentMemory.js'
import type { AgentDefinition } from '../../tools/AgentTool/loadAgentsDir.js'
import { FILE_EDIT_TOOL_NAME } from '../../tools/FileEditTool/constants.js'
import { FILE_READ_TOOL_NAME } from '../../tools/FileReadTool/prompt.js'
import { FILE_WRITE_TOOL_NAME } from '../../tools/FileWriteTool/prompt.js'
import { getPluginErrorMessage } from '../../types/plugin.js'
import { logForDebugging } from '../debug.js'
import { EFFORT_LEVELS, parseEffortValue } from '../effort.js'
import {
coerceDescriptionToString,
parseFrontmatter,
parsePositiveIntFromFrontmatter,
} from '../frontmatterParser.js'
import { getFsImplementation, isDuplicatePath } from '../fsOperations.js'
import {
parseAgentToolsFromFrontmatter,
parseSlashCommandToolsFromFrontmatter,
} from '../markdownConfigLoader.js'
import { loadAllPluginsCacheOnly } from './pluginLoader.js'
import {
loadPluginOptions,
substitutePluginVariables,
substituteUserConfigInContent,
} from './pluginOptionsStorage.js'
import type { PluginManifest } from './schemas.js'
import { walkPluginMarkdown } from './walkPluginMarkdown.js'
const VALID_MEMORY_SCOPES: AgentMemoryScope[] = ['user', 'project', 'local']
async function loadAgentsFromDirectory(
agentsPath: string,
pluginName: string,
sourceName: string,
pluginPath: string,
pluginManifest: PluginManifest,
loadedPaths: Set<string>,
): Promise<AgentDefinition[]> {
const agents: AgentDefinition[] = []
await walkPluginMarkdown(
agentsPath,
async (fullPath, namespace) => {
const agent = await loadAgentFromFile(
fullPath,
pluginName,
namespace,
sourceName,
pluginPath,
pluginManifest,
loadedPaths,
)
if (agent) agents.push(agent)
},
{ logLabel: 'agents' },
)
return agents
}
async function loadAgentFromFile(
filePath: string,
pluginName: string,
namespace: string[],
sourceName: string,
pluginPath: string,
pluginManifest: PluginManifest,
loadedPaths: Set<string>,
): Promise<AgentDefinition | null> {
const fs = getFsImplementation()
if (isDuplicatePath(fs, filePath, loadedPaths)) {
return null
}
try {
const content = await fs.readFile(filePath, { encoding: 'utf-8' })
const { frontmatter, content: markdownContent } = parseFrontmatter(
content,
filePath,
)
const baseAgentName =
(frontmatter.name as string) || basename(filePath).replace(/\.md$/, '')
// Apply namespace prefixing like we do for commands
const nameParts = [pluginName, ...namespace, baseAgentName]
const agentType = nameParts.join(':')
// Parse agent metadata from frontmatter
const whenToUse =
coerceDescriptionToString(frontmatter.description, agentType) ??
coerceDescriptionToString(frontmatter['when-to-use'], agentType) ??
`Agent from ${pluginName} plugin`
let tools = parseAgentToolsFromFrontmatter(frontmatter.tools)
const skills = parseSlashCommandToolsFromFrontmatter(frontmatter.skills)
const color = frontmatter.color as AgentColorName | undefined
const modelRaw = frontmatter.model
let model: string | undefined
if (typeof modelRaw === 'string' && modelRaw.trim().length > 0) {
const trimmed = modelRaw.trim()
model = trimmed.toLowerCase() === 'inherit' ? 'inherit' : trimmed
}
const backgroundRaw = frontmatter.background
const background =
backgroundRaw === 'true' || backgroundRaw === true ? true : undefined
// Substitute ${CLAUDE_PLUGIN_ROOT} so agents can reference bundled files,
// and ${user_config.X} (non-sensitive only) so they can embed configured
// usernames, endpoints, etc. Sensitive refs resolve to a placeholder.
let systemPrompt = substitutePluginVariables(markdownContent.trim(), {
path: pluginPath,
source: sourceName,
})
if (pluginManifest.userConfig) {
systemPrompt = substituteUserConfigInContent(
systemPrompt,
loadPluginOptions(sourceName),
pluginManifest.userConfig,
)
}
// Parse memory scope
const memoryRaw = frontmatter.memory as string | undefined
let memory: AgentMemoryScope | undefined
if (memoryRaw !== undefined) {
if (VALID_MEMORY_SCOPES.includes(memoryRaw as AgentMemoryScope)) {
memory = memoryRaw as AgentMemoryScope
} else {
logForDebugging(
`Plugin agent file ${filePath} has invalid memory value '${memoryRaw}'. Valid options: ${VALID_MEMORY_SCOPES.join(', ')}`,
)
}
}
// Parse isolation mode
const isolationRaw = frontmatter.isolation as string | undefined
const isolation =
isolationRaw === 'worktree' ? ('worktree' as const) : undefined
// Parse effort (string level or integer)
const effortRaw = frontmatter.effort
const effort =
effortRaw !== undefined ? parseEffortValue(effortRaw) : undefined
if (effortRaw !== undefined && effort === undefined) {
logForDebugging(
`Plugin agent file ${filePath} has invalid effort '${effortRaw}'. Valid options: ${EFFORT_LEVELS.join(', ')} or an integer`,
)
}
// permissionMode, hooks, and mcpServers are intentionally NOT parsed for
// plugin agents. Plugins are third-party marketplace code; these fields
// escalate what the agent can do beyond what the user approved at install
// time. For this level of control, define the agent in .claude/agents/
// where the user explicitly wrote the frontmatter. (Note: plugins can
// still ship hooks and MCP servers at the manifest level — that's the
// install-time trust boundary. Per-agent declarations would let a single
// agent file buried in agents/ silently add them.) See PR #22558 review.
for (const field of ['permissionMode', 'hooks', 'mcpServers'] as const) {
if (frontmatter[field] !== undefined) {
logForDebugging(
`Plugin agent file ${filePath} sets ${field}, which is ignored for plugin agents. Use .claude/agents/ for this level of control.`,
{ level: 'warn' },
)
}
}
// Parse maxTurns
const maxTurnsRaw = frontmatter.maxTurns
const maxTurns = parsePositiveIntFromFrontmatter(maxTurnsRaw)
if (maxTurnsRaw !== undefined && maxTurns === undefined) {
logForDebugging(
`Plugin agent file ${filePath} has invalid maxTurns '${maxTurnsRaw}'. Must be a positive integer.`,
)
}
// Parse disallowedTools
const disallowedTools =
frontmatter.disallowedTools !== undefined
? parseAgentToolsFromFrontmatter(frontmatter.disallowedTools)
: undefined
// If memory is enabled, inject Write/Edit/Read tools for memory access
if (isAutoMemoryEnabled() && memory && tools !== undefined) {
const toolSet = new Set(tools)
for (const tool of [
FILE_WRITE_TOOL_NAME,
FILE_EDIT_TOOL_NAME,
FILE_READ_TOOL_NAME,
]) {
if (!toolSet.has(tool)) {
tools = [...tools, tool]
}
}
}
return {
agentType,
whenToUse,
tools,
...(disallowedTools !== undefined ? { disallowedTools } : {}),
...(skills !== undefined ? { skills } : {}),
getSystemPrompt: () => {
if (isAutoMemoryEnabled() && memory) {
const memoryPrompt = loadAgentMemoryPrompt(agentType, memory)
return systemPrompt + '\n\n' + memoryPrompt
}
return systemPrompt
},
source: 'plugin' as const,
color,
model,
filename: baseAgentName,
plugin: sourceName,
...(background ? { background } : {}),
...(memory ? { memory } : {}),
...(isolation ? { isolation } : {}),
...(effort !== undefined ? { effort } : {}),
...(maxTurns !== undefined ? { maxTurns } : {}),
} as AgentDefinition
} catch (error) {
logForDebugging(`Failed to load agent from ${filePath}: ${error}`, {
level: 'error',
})
return null
}
}
export const loadPluginAgents = memoize(
async (): Promise<AgentDefinition[]> => {
// Only load agents from enabled plugins
const { enabled, errors } = await loadAllPluginsCacheOnly()
if (errors.length > 0) {
logForDebugging(
`Plugin loading errors: ${errors.map(e => getPluginErrorMessage(e)).join(', ')}`,
)
}
// Process plugins in parallel; each plugin has its own loadedPaths scope
const perPluginAgents = await Promise.all(
enabled.map(async (plugin): Promise<AgentDefinition[]> => {
// Track loaded file paths to prevent duplicates within this plugin
const loadedPaths = new Set<string>()
const pluginAgents: AgentDefinition[] = []
// Load agents from default agents directory
if (plugin.agentsPath) {
try {
const agents = await loadAgentsFromDirectory(
plugin.agentsPath,
plugin.name,
plugin.source,
plugin.path,
plugin.manifest,
loadedPaths,
)
pluginAgents.push(...agents)
if (agents.length > 0) {
logForDebugging(
`Loaded ${agents.length} agents from plugin ${plugin.name} default directory`,
)
}
} catch (error) {
logForDebugging(
`Failed to load agents from plugin ${plugin.name} default directory: ${error}`,
{ level: 'error' },
)
}
}
// Load agents from additional paths specified in manifest
if (plugin.agentsPaths) {
// Process all agentsPaths in parallel. isDuplicatePath is synchronous
// (check-and-add), so concurrent access to loadedPaths is safe.
const pathResults = await Promise.all(
plugin.agentsPaths.map(
async (agentPath): Promise<AgentDefinition[]> => {
try {
const fs = getFsImplementation()
const stats = await fs.stat(agentPath)
if (stats.isDirectory()) {
// Load all .md files from directory
const agents = await loadAgentsFromDirectory(
agentPath,
plugin.name,
plugin.source,
plugin.path,
plugin.manifest,
loadedPaths,
)
if (agents.length > 0) {
logForDebugging(
`Loaded ${agents.length} agents from plugin ${plugin.name} custom path: ${agentPath}`,
)
}
return agents
} else if (stats.isFile() && agentPath.endsWith('.md')) {
// Load single agent file
const agent = await loadAgentFromFile(
agentPath,
plugin.name,
[],
plugin.source,
plugin.path,
plugin.manifest,
loadedPaths,
)
if (agent) {
logForDebugging(
`Loaded agent from plugin ${plugin.name} custom file: ${agentPath}`,
)
return [agent]
}
}
return []
} catch (error) {
logForDebugging(
`Failed to load agents from plugin ${plugin.name} custom path ${agentPath}: ${error}`,
{ level: 'error' },
)
return []
}
},
),
)
for (const agents of pathResults) {
pluginAgents.push(...agents)
}
}
return pluginAgents
}),
)
const allAgents = perPluginAgents.flat()
logForDebugging(`Total plugin agents loaded: ${allAgents.length}`)
return allAgents
},
)
export function clearPluginAgentCache(): void {
loadPluginAgents.cache?.clear?.()
}