42445f5130
- Case-insensitive .md extension stripping for agent name extraction - Resolve project agent_definitions paths relative to config dir (.opencode/) - Use getOpenCodeConfigDir() to respect OPENCODE_CONFIG_DIR/XDG_CONFIG_HOME - First-write-wins semantics for both inline and definition-file agents so project-level agents always take precedence over global-level
100 lines
2.8 KiB
TypeScript
100 lines
2.8 KiB
TypeScript
import { existsSync, readFileSync } from "fs"
|
|
import { basename, extname } from "path"
|
|
import { parseFrontmatter } from "../../shared/frontmatter"
|
|
import { log } from "../../shared/logger"
|
|
import { parseJsonAgentFile } from "./json-agent-loader"
|
|
import { mapClaudeModelToOpenCode } from "./claude-model-mapper"
|
|
import type { AgentScope, AgentFrontmatter, ClaudeCodeAgentConfig, LoadedAgent } from "./types"
|
|
|
|
function parseToolsConfig(toolsStr?: string): Record<string, boolean> | undefined {
|
|
if (!toolsStr) return undefined
|
|
|
|
const tools = toolsStr.split(",").map((t) => t.trim()).filter(Boolean)
|
|
if (tools.length === 0) return undefined
|
|
|
|
const result: Record<string, boolean> = {}
|
|
for (const tool of tools) {
|
|
result[tool.toLowerCase()] = true
|
|
}
|
|
return result
|
|
}
|
|
|
|
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> = {}
|
|
|
|
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
|
|
}
|