summaryrefslogtreecommitdiff
path: root/.opencode/plugins/superpowers.js
diff options
context:
space:
mode:
authorGoldJohnKing <GoldJohnKing@live.cn>2026-08-07 20:16:01 +0800
committerGoldJohnKing <GoldJohnKing@live.cn>2026-08-07 20:18:03 +0800
commit2ee628174705a5db2dcd1bf2eeafe295a8fb48b7 (patch)
tree593a5c9d38ed331aeb724f6eb84784f1a40e5047 /.opencode/plugins/superpowers.js
parentc367f804bbf136a8a2814252c94dd286ed1c9a67 (diff)
feat(opencode): add V2 (opencode2) plugin compatibility
Add dual V1/V2 support to the OpenCode plugin. The same source file now works on both OpenCode V1 (opencode) and V2 (opencode2) without version detection at runtime. V2 changes: - Add default export { id, server, setup } for V2 PluginSupervisor - setup() registers skills via ctx.skill.transform() (V2 native API) - setup() injects bootstrap via ctx.session.hook('context') (V2 equivalent of V1's experimental.chat.messages.transform) - config hook guards against V2 array-format skills to avoid conflicts Both APIs confirmed active at runtime via diagnostics in the V2 beta. No external dependencies added — pure JavaScript throughout. Docs updated with V2 install instructions, OPENCODE_CONFIG_DIR side-by-side setup, and accurate How It Works section for both versions.
Diffstat (limited to '.opencode/plugins/superpowers.js')
-rw-r--r--.opencode/plugins/superpowers.js130
1 files changed, 87 insertions, 43 deletions
diff --git a/.opencode/plugins/superpowers.js b/.opencode/plugins/superpowers.js
index 423e5ed..557e3c2 100644
--- a/.opencode/plugins/superpowers.js
+++ b/.opencode/plugins/superpowers.js
@@ -1,17 +1,29 @@
/**
* Superpowers plugin for OpenCode.ai
*
- * Injects superpowers bootstrap context via message transform.
- * Auto-registers skills directory via config hook (no symlinks needed).
+ * Dual-compatible with OpenCode V1 and V2.
+ *
+ * V1 (opencode): loaded via named export SuperpowersPlugin — provides config
+ * hook for skills registration and experimental.chat.messages.transform for
+ * bootstrap injection.
+ *
+ * V2 (opencode2): loaded via default export { id, setup } by PluginSupervisor.
+ * setup() registers skills natively via ctx.skill.transform(), and injects
+ * bootstrap context via ctx.session.hook("context").
+ *
+ * No external dependencies — pure JavaScript works in both V1 and V2 without
+ * installing @opencode-ai/plugin or effect.
*/
import path from 'path';
import fs from 'fs';
-import os from 'os';
import { fileURLToPath } from 'url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
+// Skills directory shared by V1 (config hook) and V2 (setup/ctx.skill.transform)
+const superpowersSkillsDir = path.resolve(__dirname, '../../skills');
+
// Simple frontmatter extraction (avoid dependency on skills-core for bootstrap)
const extractAndStripFrontmatter = (content) => {
const match = content.match(/^---\n([\s\S]*?)\n---\n([\s\S]*)$/);
@@ -33,47 +45,28 @@ const extractAndStripFrontmatter = (content) => {
return { frontmatter, content: body };
};
-// Normalize a path: trim whitespace, expand ~, resolve to absolute
-const normalizePath = (p, homeDir) => {
- if (!p || typeof p !== 'string') return null;
- let normalized = p.trim();
- if (!normalized) return null;
- if (normalized.startsWith('~/')) {
- normalized = path.join(homeDir, normalized.slice(2));
- } else if (normalized === '~') {
- normalized = homeDir;
- }
- return path.resolve(normalized);
-};
-
// Module-level cache for bootstrap content.
// The SKILL.md file does not change during a session, so reading + parsing it
// once eliminates redundant fs.existsSync + fs.readFileSync + regex work on
// every agent step. See #1202 for the full analysis.
let _bootstrapCache = undefined; // undefined = not yet loaded, null = file missing
-export const SuperpowersPlugin = async ({ client, directory }) => {
- const homeDir = os.homedir();
- const superpowersSkillsDir = path.resolve(__dirname, '../../skills');
- const envConfigDir = normalizePath(process.env.OPENCODE_CONFIG_DIR, homeDir);
- const configDir = envConfigDir || path.join(homeDir, '.config/opencode');
-
- // Helper to generate bootstrap content (cached after first call)
- const getBootstrapContent = () => {
- // Return cached result on subsequent calls
- if (_bootstrapCache !== undefined) return _bootstrapCache;
-
- // Try to load using-superpowers skill
- const skillPath = path.join(superpowersSkillsDir, 'using-superpowers', 'SKILL.md');
- if (!fs.existsSync(skillPath)) {
- _bootstrapCache = null;
- return null;
- }
+// Helper to generate bootstrap content (cached after first call)
+const getBootstrapContent = () => {
+ // Return cached result on subsequent calls
+ if (_bootstrapCache !== undefined) return _bootstrapCache;
+
+ // Try to load using-superpowers skill
+ const skillPath = path.join(superpowersSkillsDir, 'using-superpowers', 'SKILL.md');
+ if (!fs.existsSync(skillPath)) {
+ _bootstrapCache = null;
+ return null;
+ }
- const fullContent = fs.readFileSync(skillPath, 'utf8');
- const { content } = extractAndStripFrontmatter(fullContent);
+ const fullContent = fs.readFileSync(skillPath, 'utf8');
+ const { content } = extractAndStripFrontmatter(fullContent);
- const toolMapping = `**Tool Mapping for OpenCode:**
+ const toolMapping = `**Tool Mapping for OpenCode:**
When skills request actions, substitute OpenCode equivalents:
- Create or update todos → \`todowrite\`
- \`Subagent (general-purpose):\` → \`task\` with \`subagent_type: "general"\`
@@ -86,7 +79,7 @@ When skills request actions, substitute OpenCode equivalents:
Use OpenCode's native \`skill\` tool to list and load skills.`;
- _bootstrapCache = `<EXTREMELY_IMPORTANT>
+ _bootstrapCache = `<EXTREMELY_IMPORTANT>
You have superpowers.
**IMPORTANT: The using-superpowers skill content is included below. It is ALREADY LOADED - you are currently following it. Do NOT use the skill tool to load "using-superpowers" again - that would be redundant.**
@@ -96,15 +89,25 @@ ${content}
${toolMapping}
</EXTREMELY_IMPORTANT>`;
- return _bootstrapCache;
- };
+ return _bootstrapCache;
+};
+/**
+ * V1 Plugin Function (named export + default.server)
+ *
+ * Used by V1 (OpenCode 1.x): discovered via named export scanning.
+ * Provides: config hook (V1 skills registration) + bootstrap injection
+ * (experimental.chat.messages.transform).
+ */
+export const SuperpowersPlugin = async ({ client, directory }) => {
return {
// Inject skills path into live config so OpenCode discovers superpowers skills
// without requiring manual symlinks or config file edits.
- // This works because Config.get() returns a cached singleton — modifications
- // here are visible when skills are lazily discovered later.
config: async (config) => {
+ // V2: skills is a flat array — skip, setup() handles V2 skill registration
+ if (Array.isArray(config.skills)) return;
+
+ // V1: skills is { paths: [...] }
config.skills = config.skills || {};
config.skills.paths = config.skills.paths || [];
if (!config.skills.paths.includes(superpowersSkillsDir)) {
@@ -128,8 +131,6 @@ ${toolMapping}
if (!firstUser || !firstUser.parts.length) return;
// Guard: skip if first user message already contains bootstrap.
- // This prevents double injection when OpenCode passes an already
- // transformed in-memory message array through the hook again.
if (firstUser.parts.some(p => p.type === 'text' && p.text.includes('EXTREMELY_IMPORTANT'))) return;
const ref = firstUser.parts[0];
@@ -137,3 +138,46 @@ ${toolMapping}
}
};
};
+
+/**
+ * V2 Setup Function (default.setup)
+ *
+ * Called by V2 PluginSupervisor (packages/core/src/plugin/).
+ * Performs two things:
+ *
+ * 1. Registers the skills directory natively via ctx.skill.transform().
+ * 2. Injects bootstrap context via ctx.session.hook("context"), the V2
+ * equivalent of V1's experimental.chat.messages.transform.
+ */
+async function setup(ctx) {
+ // 1. Register skills
+ await ctx.skill.transform((draft) => {
+ draft.source({
+ type: 'directory',
+ path: superpowersSkillsDir,
+ });
+ });
+
+ // 2. Inject bootstrap into first user message via V2 session context hook
+ await ctx.session.hook('context', (event) => {
+ const bootstrap = getBootstrapContent();
+ if (!bootstrap || !event.messages || !event.messages.length) return;
+ const firstUser = event.messages.find(m => m.role === 'user');
+ if (!firstUser || !firstUser.content || !firstUser.content.length) return;
+ if (firstUser.content.some(p => p.type === 'text' && p.text && p.text.includes('EXTREMELY_IMPORTANT'))) return;
+ firstUser.content.unshift({ type: 'text', text: bootstrap });
+ });
+}
+
+/**
+ * Default Export: { id, server, setup }
+ *
+ * V2 PluginSupervisor reads { id, setup }.
+ * V1 reads named export SuperpowersPlugin.
+ * server() is exported for V1 compatibility.
+ */
+export default {
+ id: 'superpowers',
+ server: SuperpowersPlugin,
+ setup,
+};