feat(agents): add agent_definitions schema, eager path resolution, and JSON agent loader
Wave 1 of agent definitions enhancement (PR #2299): Schema & Configuration: - Add agent_definitions field to oh-my-opencode config schema - Support list of file paths to .md or .json agent definition files - Add to PARTIAL_STRING_ARRAY_KEYS for Set-union merge semantics - Implement eager path resolution in loadPluginConfig() before merging Path Resolution: - Create resolve-agent-definition-paths.ts helper - User-level paths resolve from ~/.config/opencode/ (no containment) - Project-level paths resolve from project root (with containment check) - Homedir expansion, absolute/relative path handling JSON Agent Loader: - Create parseJsonAgentFile() for .json/.jsonc agent definitions - Validate required fields (name, prompt) - Support tools as string (comma-separated) or array - Map model via mapClaudeModelToOpenCode() - Comprehensive test suite (7 test cases, all passing) Type Extensions: - Extend AgentScope: add 'definition-file' and 'opencode-config' - Add AgentJsonDefinition interface for JSON agent schema All automated checks passing: - lsp_diagnostics clean on all changed files - json-agent-loader.test.ts: 7/7 passing - Full typecheck: zero new errors - QA evidence saved to .sisyphus/evidence/
This commit is contained in:
committed by
YeonGyu-Kim
parent
1e85a88db0
commit
fd28f7e668
@@ -0,0 +1,67 @@
|
||||
import { existsSync, readFileSync } from "fs"
|
||||
import { parseJsoncSafe } from "../../shared/jsonc-parser"
|
||||
import { mapClaudeModelToOpenCode } from "./claude-model-mapper"
|
||||
import type { AgentScope, AgentJsonDefinition, ClaudeCodeAgentConfig, LoadedAgent } from "./types"
|
||||
|
||||
function parseToolsConfig(tools?: string | string[]): Record<string, boolean> | undefined {
|
||||
if (!tools) return undefined
|
||||
|
||||
const toolsArray = Array.isArray(tools) ? tools : tools.split(",").map((t) => t.trim())
|
||||
const filtered = toolsArray.filter((t) => typeof t === "string" && t.length > 0)
|
||||
|
||||
if (filtered.length === 0) return undefined
|
||||
|
||||
const result: Record<string, boolean> = {}
|
||||
for (const tool of filtered) {
|
||||
result[tool.toLowerCase()] = true
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
export function parseJsonAgentFile(filePath: string, scope: AgentScope): LoadedAgent | null {
|
||||
try {
|
||||
if (!existsSync(filePath)) {
|
||||
return null
|
||||
}
|
||||
|
||||
const content = readFileSync(filePath, "utf-8")
|
||||
const { data } = parseJsoncSafe<AgentJsonDefinition>(content)
|
||||
|
||||
if (!data) {
|
||||
return null
|
||||
}
|
||||
|
||||
if (!data.name || !data.prompt) {
|
||||
return null
|
||||
}
|
||||
|
||||
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: data.prompt.trim(),
|
||||
...(modelString ? { model: modelString } : {}),
|
||||
}
|
||||
|
||||
const toolsConfig = parseToolsConfig(data.tools)
|
||||
if (toolsConfig) {
|
||||
config.tools = toolsConfig
|
||||
}
|
||||
|
||||
return {
|
||||
name: data.name,
|
||||
path: filePath,
|
||||
config,
|
||||
scope,
|
||||
}
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user