import { existsSync } from 'node:fs';
import { join } from 'node:path';
import { homedir } from 'node:os';
import type { AgentAdapter } from './base.js';
import { createSkillXml } from './base.js';
import type { Skill, AgentType } from '../core/types.js';
export class OpenCodeAdapter implements AgentAdapter {
readonly type: AgentType = 'opencode';
readonly name = 'OpenCode';
readonly skillsDir = '.opencode/skills';
readonly configFile = 'AGENTS.md';
generateConfig(skills: Skill[]): string {
const enabledSkills = skills.filter(s => s.enabled);
if (enabledSkills.length === 0) {
return '';
}
const skillsXml = enabledSkills.map(createSkillXml).join('\n\n');
return `
# Skills
The following skills are available to help complete tasks:
${skillsXml}
## How to Use
When a task matches a skill's description:
\`\`\`bash
skillkit read
\`\`\`
This loads the skill's instructions into context.
`;
}
parseConfig(content: string): string[] {
const skillNames: string[] = [];
const skillRegex = /([^<]+)<\/name>/g;
let match;
while ((match = skillRegex.exec(content)) !== null) {
skillNames.push(match[1].trim());
}
return skillNames;
}
getInvokeCommand(skillName: string): string {
return `skillkit read ${skillName}`;
}
async isDetected(): Promise {
const opencodeDir = join(process.cwd(), '.opencode');
const globalOpencode = join(homedir(), '.opencode');
return existsSync(opencodeDir) || existsSync(globalOpencode);
}
}