Files
oh-my-opencode/src/features/claude-code-agent-loader/agent-definitions-loader.ts
T
YeonGyu-Kim e5d3fe96c4 fix(agents): address all PR #2299 code review findings
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
2026-04-15 10:58:16 +09:00

88 lines
2.6 KiB
TypeScript

import { existsSync, readFileSync } from "fs"
import { basename, extname } from "path"
import { parseFrontmatter } from "../../shared/frontmatter"
import { log } from "../../shared/logger"
import { parseToolsConfig } from "../../shared/parse-tools-config"
import { parseJsonAgentFile } from "./json-agent-loader"
import { mapClaudeModelToOpenCode } from "./claude-model-mapper"
import type { AgentScope, AgentFrontmatter, ClaudeCodeAgentConfig, LoadedAgent } from "./types"
export function parseMarkdownAgentFile(filePath: string, scope: AgentScope): LoadedAgent | null {
try {
if (!existsSync(filePath)) {
return null
}
const content = readFileSync(filePath, "utf-8")
const { data, body } = parseFrontmatter<AgentFrontmatter>(content)
const fileName = basename(filePath)
const agentName = fileName.replace(/\.md$/i, "")
const name = data.name || agentName
const originalDescription = data.description || ""
const formattedDescription = `(${scope}) ${originalDescription}`
const mappedModelOverride = mapClaudeModelToOpenCode(data.model)
const modelString = mappedModelOverride
? `${mappedModelOverride.providerID}/${mappedModelOverride.modelID}`
: undefined
const config: ClaudeCodeAgentConfig = {
description: formattedDescription,
mode: data.mode || "subagent",
prompt: body.trim(),
...(modelString ? { model: modelString } : {}),
}
const toolsConfig = parseToolsConfig(data.tools)
if (toolsConfig) {
config.tools = toolsConfig
}
return {
name,
path: filePath,
config,
scope,
}
} catch {
return null
}
}
export function loadAgentDefinitions(
paths: string[],
scope: AgentScope
): Record<string, ClaudeCodeAgentConfig> {
const result: Record<string, ClaudeCodeAgentConfig> = Object.create(null)
for (const filePath of paths) {
if (!existsSync(filePath)) {
log(`[agent-definitions-loader] File not found, skipping: ${filePath}`)
continue
}
const ext = extname(filePath).toLowerCase()
let agent: LoadedAgent | null = null
if (ext === ".md") {
agent = parseMarkdownAgentFile(filePath, scope)
} else if (ext === ".json" || ext === ".jsonc") {
agent = parseJsonAgentFile(filePath, scope)
} else {
log(`[agent-definitions-loader] Unsupported file extension: ${ext} for ${filePath}`)
continue
}
if (!agent) {
log(`[agent-definitions-loader] Failed to parse agent file: ${filePath}`)
continue
}
result[agent.name] = agent.config
}
return result
}