skills.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import { spawnSync } from 'node:child_process';
  2. import { CUSTOM_SKILLS } from './custom-skills';
  3. /**
  4. * A recommended skill to install via `npx skills add`.
  5. */
  6. export interface RecommendedSkill {
  7. /** Human-readable name for prompts */
  8. name: string;
  9. /** GitHub repo URL for `npx skills add` */
  10. repo: string;
  11. /** Skill name within the repo (--skill flag) */
  12. skillName: string;
  13. /** List of agents that should auto-allow this skill */
  14. allowedAgents: string[];
  15. /** Description shown to user during install */
  16. description: string;
  17. /** Optional commands to run after the skill is added */
  18. postInstallCommands?: string[];
  19. }
  20. /**
  21. * A skill that is managed externally (e.g. user-installed) and needs
  22. * permission grants but is NOT installed by this plugin's CLI.
  23. */
  24. export interface PermissionOnlySkill {
  25. /** Skill name — must match the name OpenCode uses for permission checks */
  26. name: string;
  27. /** List of agents that should auto-allow this skill */
  28. allowedAgents: string[];
  29. /** Human-readable description (for documentation only) */
  30. description: string;
  31. }
  32. /**
  33. * List of recommended skills.
  34. * Add new skills here to include them in the installation flow.
  35. */
  36. export const RECOMMENDED_SKILLS: RecommendedSkill[] = [
  37. {
  38. name: 'agent-browser',
  39. repo: 'https://github.com/vercel-labs/agent-browser',
  40. skillName: 'agent-browser',
  41. allowedAgents: ['designer'],
  42. description: 'High-performance browser automation',
  43. postInstallCommands: [
  44. 'npm install -g agent-browser',
  45. 'agent-browser install',
  46. ],
  47. },
  48. ];
  49. /**
  50. * Skills managed externally (not installed by this plugin's CLI).
  51. * Entries here only affect agent permission grants — nothing is installed.
  52. */
  53. export const PERMISSION_ONLY_SKILLS: PermissionOnlySkill[] = [
  54. {
  55. name: 'requesting-code-review',
  56. allowedAgents: ['oracle'],
  57. description:
  58. 'Code review template for reviewer subagents in multi-step workflows',
  59. },
  60. ];
  61. /**
  62. * Install a skill using `npx skills add`.
  63. * @param skill - The skill to install
  64. * @returns True if installation succeeded, false otherwise
  65. */
  66. export function installSkill(skill: RecommendedSkill): boolean {
  67. const args = [
  68. 'skills',
  69. 'add',
  70. skill.repo,
  71. '--skill',
  72. skill.skillName,
  73. '-a',
  74. 'opencode',
  75. '-y',
  76. '--global',
  77. ];
  78. try {
  79. const result = spawnSync('npx', args, { stdio: 'inherit' });
  80. if (result.status !== 0) {
  81. return false;
  82. }
  83. // Run post-install commands if any
  84. if (skill.postInstallCommands && skill.postInstallCommands.length > 0) {
  85. console.log(`Running post-install commands for ${skill.name}...`);
  86. for (const cmd of skill.postInstallCommands) {
  87. console.log(`> ${cmd}`);
  88. const [command, ...cmdArgs] = cmd.split(' ');
  89. const cmdResult = spawnSync(command, cmdArgs, { stdio: 'inherit' });
  90. if (cmdResult.status !== 0) {
  91. console.warn(`Post-install command failed: ${cmd}`);
  92. }
  93. }
  94. }
  95. return true;
  96. } catch (error) {
  97. console.error(`Failed to install skill: ${skill.name}`, error);
  98. return false;
  99. }
  100. }
  101. /**
  102. * Get permission presets for a specific agent based on recommended skills.
  103. * @param agentName - The name of the agent
  104. * @param skillList - Optional explicit list of skills to allow (overrides recommendations)
  105. * @returns Permission rules for the skill permission type
  106. */
  107. export function getSkillPermissionsForAgent(
  108. agentName: string,
  109. skillList?: string[],
  110. ): Record<string, 'allow' | 'ask' | 'deny'> {
  111. // Orchestrator gets all skills by default, others are restricted
  112. const permissions: Record<string, 'allow' | 'ask' | 'deny'> = {
  113. '*': agentName === 'orchestrator' ? 'allow' : 'deny',
  114. };
  115. // If the user provided an explicit skill list (even empty), honor it
  116. if (skillList) {
  117. permissions['*'] = 'deny';
  118. for (const name of skillList) {
  119. if (name === '*') {
  120. permissions['*'] = 'allow';
  121. } else if (name.startsWith('!')) {
  122. permissions[name.slice(1)] = 'deny';
  123. } else {
  124. permissions[name] = 'allow';
  125. }
  126. }
  127. return permissions;
  128. }
  129. // Otherwise, use recommended defaults
  130. for (const skill of RECOMMENDED_SKILLS) {
  131. const isAllowed =
  132. skill.allowedAgents.includes('*') ||
  133. skill.allowedAgents.includes(agentName);
  134. if (isAllowed) {
  135. permissions[skill.skillName] = 'allow';
  136. }
  137. }
  138. // Apply permissions from bundled custom skills
  139. for (const skill of CUSTOM_SKILLS) {
  140. const isAllowed =
  141. skill.allowedAgents.includes('*') ||
  142. skill.allowedAgents.includes(agentName);
  143. if (isAllowed) {
  144. permissions[skill.name] = 'allow';
  145. }
  146. }
  147. // Apply permissions for externally-managed skills (not installed by this plugin)
  148. for (const skill of PERMISSION_ONLY_SKILLS) {
  149. const isAllowed =
  150. skill.allowedAgents.includes('*') ||
  151. skill.allowedAgents.includes(agentName);
  152. if (isAllowed) {
  153. permissions[skill.name] = 'allow';
  154. }
  155. }
  156. return permissions;
  157. }