e5d3fe96c4
Blocking fixes: - B1: Return empty restrictions for unknown/custom agents instead of EXPLORATION_AGENT_DENYLIST, allowing custom agents full tool access - B2: Use Object.create(null) consistently across all 5 agent-loading result objects to prevent prototype pollution - B3: Add code comment documenting custom agent bash access trust model - B4: Mock getOpenCodeConfigDir in opencode-config-agents-reader tests to prevent global config dir leakage Non-blocking fixes: - N1: Use resolveAgentDefinitionPaths with project boundary enforcement in opencode-config-agents-reader for path containment - N2: Add session-scoped 30s TTL cache to resolveCallableAgents to avoid redundant SDK IPC calls per tool invocation - N3: Extract shared parseToolsConfig into src/shared/parse-tools-config.ts replacing 4 duplicated local implementations - N4: Add .min(1) to AgentDefinitionPathSchema rejecting empty paths - N5: Add resolve-agent-definition-paths.test.ts covering tilde expansion, relative paths, boundary enforcement, and null containmentDir - N6: Validate agent mode against allowed values instead of bare type assertion in opencode-config-agents-reader
60 lines
2.2 KiB
TypeScript
60 lines
2.2 KiB
TypeScript
import { existsSync, readdirSync, readFileSync } from "fs"
|
|
import { basename, join } from "path"
|
|
import { parseFrontmatter } from "../../shared/frontmatter"
|
|
import { isMarkdownFile } from "../../shared/file-utils"
|
|
import { log } from "../../shared/logger"
|
|
import { parseToolsConfig } from "../../shared/parse-tools-config"
|
|
import type { AgentFrontmatter, ClaudeCodeAgentConfig } from "../claude-code-agent-loader/types"
|
|
import { mapClaudeModelToOpenCode } from "../claude-code-agent-loader/claude-model-mapper"
|
|
import type { LoadedPlugin } from "./types"
|
|
|
|
export function loadPluginAgents(plugins: LoadedPlugin[]): Record<string, ClaudeCodeAgentConfig> {
|
|
const agents: Record<string, ClaudeCodeAgentConfig> = {}
|
|
|
|
for (const plugin of plugins) {
|
|
if (!plugin.agentsDir || !existsSync(plugin.agentsDir)) continue
|
|
|
|
const entries = readdirSync(plugin.agentsDir, { withFileTypes: true })
|
|
|
|
for (const entry of entries) {
|
|
if (!isMarkdownFile(entry)) continue
|
|
|
|
const agentPath = join(plugin.agentsDir, entry.name)
|
|
const agentName = basename(entry.name, ".md")
|
|
const namespacedName = `${plugin.name}:${agentName}`
|
|
|
|
try {
|
|
const content = readFileSync(agentPath, "utf-8")
|
|
const { data, body } = parseFrontmatter<AgentFrontmatter>(content)
|
|
|
|
const originalDescription = data.description || ""
|
|
const formattedDescription = `(plugin: ${plugin.name}) ${originalDescription}`
|
|
|
|
const mappedModelOverride = mapClaudeModelToOpenCode(data.model)
|
|
const modelString = mappedModelOverride
|
|
? `${mappedModelOverride.providerID}/${mappedModelOverride.modelID}`
|
|
: undefined
|
|
|
|
const config: ClaudeCodeAgentConfig = {
|
|
description: formattedDescription,
|
|
mode: "subagent",
|
|
prompt: body.trim(),
|
|
...(modelString ? { model: modelString } : {}),
|
|
}
|
|
|
|
const toolsConfig = parseToolsConfig(data.tools)
|
|
if (toolsConfig) {
|
|
config.tools = toolsConfig
|
|
}
|
|
|
|
agents[namespacedName] = config
|
|
log(`Loaded plugin agent: ${namespacedName}`, { path: agentPath })
|
|
} catch (error) {
|
|
log(`Failed to load plugin agent: ${agentPath}`, error)
|
|
}
|
|
}
|
|
}
|
|
|
|
return agents
|
|
}
|