2026-02-08 16:21:37 +09:00
|
|
|
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"
|
2026-04-15 10:41:32 +09:00
|
|
|
import { parseToolsConfig } from "../../shared/parse-tools-config"
|
2026-03-11 17:07:23 +09:00
|
|
|
import type { AgentFrontmatter, ClaudeCodeAgentConfig } from "../claude-code-agent-loader/types"
|
2026-03-06 11:56:03 +09:00
|
|
|
import { mapClaudeModelToOpenCode } from "../claude-code-agent-loader/claude-model-mapper"
|
2026-02-08 16:21:37 +09:00
|
|
|
import type { LoadedPlugin } from "./types"
|
|
|
|
|
|
2026-03-11 17:07:23 +09:00
|
|
|
export function loadPluginAgents(plugins: LoadedPlugin[]): Record<string, ClaudeCodeAgentConfig> {
|
|
|
|
|
const agents: Record<string, ClaudeCodeAgentConfig> = {}
|
2026-02-08 16:21:37 +09:00
|
|
|
|
|
|
|
|
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}`
|
|
|
|
|
|
2026-03-11 17:07:23 +09:00
|
|
|
const mappedModelOverride = mapClaudeModelToOpenCode(data.model)
|
2026-03-14 05:16:50 +00:00
|
|
|
const modelString = mappedModelOverride
|
|
|
|
|
? `${mappedModelOverride.providerID}/${mappedModelOverride.modelID}`
|
|
|
|
|
: undefined
|
2026-03-06 11:56:03 +09:00
|
|
|
|
2026-03-11 17:07:23 +09:00
|
|
|
const config: ClaudeCodeAgentConfig = {
|
2026-02-08 16:21:37 +09:00
|
|
|
description: formattedDescription,
|
|
|
|
|
mode: "subagent",
|
|
|
|
|
prompt: body.trim(),
|
2026-03-14 05:16:50 +00:00
|
|
|
...(modelString ? { model: modelString } : {}),
|
2026-02-08 16:21:37 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|