Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,8 @@ Add the plugin to your `opencode.json`:
}
```

> [!CAUTION]
> **One-time setup required**: OpenCode does not execute npm postinstall scripts.
> Run this command once after initial installation to enable slash commands:
>
> ```bash
> cd ~/.cache/opencode/node_modules/opencode-agent-modes && node dist/postinstall.js
> ```

The following command files are copied to `~/.config/opencode/command/`:
The following command files are automatically copied to
`~/.config/opencode/command/` when the plugin initializes:

- `mode-performance.md`
- `mode-economy.md`
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "opencode-agent-modes",
"version": "0.1.0",
"version": "0.1.1",
"description": "OpenCode plugin to switch agent modes between performance and economy presets",
"module": "src/index.ts",
"main": "dist/index.js",
Expand All @@ -24,14 +24,13 @@
},
"scripts": {
"typecheck": "tsc --noEmit",
"build": "bun build src/index.ts --outdir dist --format esm && bun build scripts/postinstall.ts --outdir dist --format esm --target node && tsc -p tsconfig.build.json",
"build": "bun build src/index.ts --outdir dist --format esm --target bun && tsc -p tsconfig.build.json",
"lint": "bunx biome check src/",
"format": "prettier --write 'src/**/*.ts'",
"format:check": "prettier --check 'src/**/*.ts'",
"test": "bun test",
"test:watch": "bun test --watch",
"test:coverage": "bun test --coverage",
"postinstall": "node dist/postinstall.js",
"prepublishOnly": "bun run build"
},
"keywords": [
Expand Down
47 changes: 0 additions & 47 deletions scripts/postinstall.ts

This file was deleted.

55 changes: 55 additions & 0 deletions src/config/command-installer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { copyFileSync, mkdirSync, readdirSync, existsSync } from "node:fs";
import { homedir } from "node:os";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";

/**
* Target directory for OpenCode command files
*/
const COMMANDS_DEST = join(homedir(), ".config", "opencode", "command");

/**
* Copies slash command markdown files to OpenCode's command directory.
*
* This function is called during plugin initialization to ensure
* command files are available without manual postinstall execution.
*
* @returns Number of files copied, or -1 if source directory not found
*/
export function copyCommandFiles(): number {
// Resolve the commands directory relative to this file
// In dist: dist/config/command-installer.js -> dist/../commands = commands/
const __dirname = dirname(fileURLToPath(import.meta.url));
const commandsSrc = join(__dirname, "..", "..", "commands");

// Skip if commands directory doesn't exist (shouldn't happen in production)
if (!existsSync(commandsSrc)) {
console.warn(
"[agent-mode-switcher] Commands directory not found:",
commandsSrc
);
return -1;
}

try {
// Ensure destination directory exists
mkdirSync(COMMANDS_DEST, { recursive: true });

// Find all markdown files in the commands directory
const files = readdirSync(commandsSrc).filter((f) => f.endsWith(".md"));

// Copy each command file to the OpenCode config directory
for (const file of files) {
copyFileSync(join(commandsSrc, file), join(COMMANDS_DEST, file));
}

return files.length;
} catch (error) {
// Non-fatal: log warning but don't block plugin initialization
console.warn(
"[agent-mode-switcher] Warning: Could not copy command files:",
error instanceof Error ? error.message : String(error)
);
return -1;
}
}
1 change: 1 addition & 0 deletions src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './types.ts'
export * from './loader.ts'
export * from './initializer.ts'
export * from './command-installer.ts'
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { tool } from "@opencode-ai/plugin";
import type { Plugin } from "@opencode-ai/plugin";
import { ModeManager } from "./modes/index.ts";
import { copyCommandFiles } from "./config/index.ts";

/**
* OpenCode Agent Mode Switcher Plugin
Expand All @@ -14,6 +15,8 @@ const modeSwitcherPlugin: Plugin = async ({ client }) => {
// Initialize on startup with error handling
try {
await modeManager.initialize();
// Copy slash command files to ~/.config/opencode/command/
copyCommandFiles();
} catch (error) {
// Log error but don't block opencode startup
console.error(
Expand Down