fd28f7e668
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/
140 lines
4.4 KiB
TypeScript
140 lines
4.4 KiB
TypeScript
import { describe, test, expect, afterEach } from "bun:test"
|
|
import { mkdtempSync, writeFileSync, rmSync } from "fs"
|
|
import { join } from "path"
|
|
import { tmpdir } from "os"
|
|
import { parseJsonAgentFile } from "./json-agent-loader"
|
|
|
|
describe("json-agent-loader", () => {
|
|
const dirs: string[] = []
|
|
|
|
afterEach(() => {
|
|
for (const dir of dirs) {
|
|
rmSync(dir, { recursive: true, force: true })
|
|
}
|
|
dirs.length = 0
|
|
})
|
|
|
|
function trackDir(dir: string): string {
|
|
dirs.push(dir)
|
|
return dir
|
|
}
|
|
|
|
test("parses valid JSON agent file", () => {
|
|
const dir = trackDir(mkdtempSync(join(tmpdir(), "json-agent-loader-test-")))
|
|
const filePath = join(dir, "agent.json")
|
|
|
|
writeFileSync(filePath, JSON.stringify({
|
|
name: "test-agent",
|
|
description: "A test agent",
|
|
prompt: "You are a test agent.",
|
|
tools: ["Bash", "Read"],
|
|
model: "claude-3-5-sonnet-20241022",
|
|
mode: "subagent",
|
|
}), "utf-8")
|
|
|
|
const result = parseJsonAgentFile(filePath, "definition-file")
|
|
|
|
expect(result).not.toBeNull()
|
|
expect(result?.name).toBe("test-agent")
|
|
expect(result?.path).toBe(filePath)
|
|
expect(result?.scope).toBe("definition-file")
|
|
expect(result?.config.description).toBe("(definition-file) A test agent")
|
|
expect(result?.config.prompt).toBe("You are a test agent.")
|
|
expect(result?.config.mode).toBe("subagent")
|
|
expect(result?.config.tools).toEqual({ bash: true, read: true })
|
|
})
|
|
|
|
test("parses JSONC with comments", () => {
|
|
const dir = trackDir(mkdtempSync(join(tmpdir(), "json-agent-loader-test-")))
|
|
const filePath = join(dir, "agent.jsonc")
|
|
|
|
writeFileSync(filePath, `{
|
|
// Agent name
|
|
"name": "commented-agent",
|
|
"description": "Agent with comments",
|
|
"prompt": "Do something.",
|
|
"tools": ["Bash"], // Tools for the agent
|
|
// Model specification
|
|
"model": "claude-3-5-sonnet-20241022"
|
|
}`, "utf-8")
|
|
|
|
const result = parseJsonAgentFile(filePath, "definition-file")
|
|
|
|
expect(result).not.toBeNull()
|
|
expect(result?.name).toBe("commented-agent")
|
|
expect(result?.config.tools).toEqual({ bash: true })
|
|
})
|
|
|
|
test("returns null when required fields are missing (name)", () => {
|
|
const dir = trackDir(mkdtempSync(join(tmpdir(), "json-agent-loader-test-")))
|
|
const filePath = join(dir, "agent.json")
|
|
|
|
writeFileSync(filePath, JSON.stringify({
|
|
description: "Missing name",
|
|
prompt: "You are an agent.",
|
|
}), "utf-8")
|
|
|
|
const result = parseJsonAgentFile(filePath, "definition-file")
|
|
expect(result).toBeNull()
|
|
})
|
|
|
|
test("returns null when required fields are missing (prompt)", () => {
|
|
const dir = trackDir(mkdtempSync(join(tmpdir(), "json-agent-loader-test-")))
|
|
const filePath = join(dir, "agent.json")
|
|
|
|
writeFileSync(filePath, JSON.stringify({
|
|
name: "missing-prompt",
|
|
description: "Missing prompt",
|
|
}), "utf-8")
|
|
|
|
const result = parseJsonAgentFile(filePath, "definition-file")
|
|
expect(result).toBeNull()
|
|
})
|
|
|
|
test("defaults optional fields correctly", () => {
|
|
const dir = trackDir(mkdtempSync(join(tmpdir(), "json-agent-loader-test-")))
|
|
const filePath = join(dir, "agent.json")
|
|
|
|
writeFileSync(filePath, JSON.stringify({
|
|
name: "minimal-agent",
|
|
prompt: "You are minimal.",
|
|
}), "utf-8")
|
|
|
|
const result = parseJsonAgentFile(filePath, "definition-file")
|
|
|
|
expect(result).not.toBeNull()
|
|
expect(result?.config.description).toBe("(definition-file) ")
|
|
expect(result?.config.mode).toBe("subagent")
|
|
expect(result?.config.tools).toBeUndefined()
|
|
expect(result?.config.model).toBeUndefined()
|
|
})
|
|
|
|
test("handles tools as string comma-separated list", () => {
|
|
const dir = trackDir(mkdtempSync(join(tmpdir(), "json-agent-loader-test-")))
|
|
const filePath = join(dir, "agent.json")
|
|
|
|
writeFileSync(filePath, JSON.stringify({
|
|
name: "string-tools-agent",
|
|
prompt: "You are an agent.",
|
|
tools: "Bash, Read, Grep",
|
|
}), "utf-8")
|
|
|
|
const result = parseJsonAgentFile(filePath, "definition-file")
|
|
|
|
expect(result?.config.tools).toEqual({ bash: true, read: true, grep: true })
|
|
})
|
|
|
|
test("returns null for malformed JSON", () => {
|
|
const dir = trackDir(mkdtempSync(join(tmpdir(), "json-agent-loader-test-")))
|
|
const filePath = join(dir, "agent.json")
|
|
|
|
writeFileSync(filePath, `{
|
|
"name": "broken",
|
|
"prompt": "incomplete json`,
|
|
"utf-8")
|
|
|
|
const result = parseJsonAgentFile(filePath, "definition-file")
|
|
expect(result).toBeNull()
|
|
})
|
|
})
|