|
| 1 | +# OpenCode Plugin OpenSpec Design |
| 2 | + |
| 3 | +## 1. Overview |
| 4 | +This plugin integrates OpenSpec into OpenCode. It is **context-aware**: it activates only when it detects an OpenSpec-initialized project. When active, it dynamically registers an `openspec-plan` agent via the `config` hook, tailored for architectural planning. |
| 5 | + |
| 6 | +## 2. Activation Logic |
| 7 | +The plugin checks for OpenSpec presence at startup. |
| 8 | + |
| 9 | +- **Condition**: Existence of `openspec/AGENTS.md` (or `AGENTS.md`). |
| 10 | +- **Action**: If detected, the plugin hooks into the configuration loading process to inject the OpenSpec agent. |
| 11 | + |
| 12 | +## 3. Features (Active Mode) |
| 13 | + |
| 14 | +### 3.1. Agent Injection (`config` hook) |
| 15 | +Instead of just relying on runtime hooks, we will use the `config` hook to modify the OpenCode configuration directly. This is how `oh-my-opencode` replaces the default plan agent. |
| 16 | + |
| 17 | +**Implementation Strategy:** |
| 18 | +1. **Hook**: `config` |
| 19 | +2. **Logic**: |
| 20 | + - Check if `openspec/AGENTS.md` exists. |
| 21 | + - If yes, inject a new agent `openspec-plan` into the `agent` configuration object. |
| 22 | + - **Agent Configuration**: |
| 23 | + - `name`: "openspec-plan" |
| 24 | + - `mode`: "primary" |
| 25 | + - `description`: "OpenSpec Architect - Plan and specify software architecture." |
| 26 | + - `prompt`: (Custom System Prompt for OpenSpec) |
| 27 | + - `permission`: |
| 28 | + - Explicitly `allow` editing `**/*.spec.md`, `project.md`, `AGENTS.md`. |
| 29 | + - This avoids reliance on the global `permission.ask` hook for basic operations, though we can keep `permission.ask` as a fallback or for finer control. |
| 30 | + - **Optional**: Hide/Demote default `plan` or `sisyphus-plan` to reduce confusion. |
| 31 | + |
| 32 | +### 3.2. Auto-Permission (Fallback) |
| 33 | +While the agent config handles most permissions, we can still use `permission.ask` for edge cases or to ensure a smooth experience if the static permission config isn't enough. |
| 34 | + |
| 35 | +- **Scope**: `openspec/**/*.md`, `specs/**/*.md`. |
| 36 | +- **Logic**: Intercept `permission.ask` and return `allow` for these patterns. |
| 37 | + |
| 38 | +## 4. Dependencies |
| 39 | +- `@opencode-ai/plugin`: ^1.1.1 |
| 40 | +- `@opencode-ai/sdk`: ^1.1.1 |
| 41 | + |
| 42 | +## 5. Project Structure |
| 43 | +``` |
| 44 | +opencode-plugin-openspec/ |
| 45 | +├── package.json |
| 46 | +├── tsconfig.json |
| 47 | +├── README.md |
| 48 | +├── DESIGN.md |
| 49 | +└── src/ |
| 50 | + ├── index.ts # Plugin entry point |
| 51 | + ├── config.ts # Config hook implementation (Agent injection) |
| 52 | + ├── prompts.ts # System prompts |
| 53 | + └── utils/ |
| 54 | + └── detection.ts # Detection logic |
| 55 | +``` |
| 56 | + |
| 57 | +## 6. Implementation Details |
| 58 | + |
| 59 | +### 6.1. Config Hook (`src/config.ts`) |
| 60 | +```typescript |
| 61 | +export const configHook: Hooks["config"] = async (config, ctx) => { |
| 62 | + if (!await isOpenSpecProject(ctx)) return config; |
| 63 | + |
| 64 | + // Define OpenSpec Plan Agent |
| 65 | + const openSpecAgent = { |
| 66 | + name: "openspec-plan", |
| 67 | + mode: "primary", |
| 68 | + description: "OpenSpec Architect", |
| 69 | + prompt: OPENSPEC_SYSTEM_PROMPT, |
| 70 | + permission: { |
| 71 | + edit: { |
| 72 | + "**/*.spec.md": "allow", |
| 73 | + "**/project.md": "allow", |
| 74 | + "**/AGENTS.md": "allow" |
| 75 | + } |
| 76 | + } |
| 77 | + }; |
| 78 | + |
| 79 | + // Inject into config |
| 80 | + return { |
| 81 | + ...config, |
| 82 | + agent: { |
| 83 | + ...(config.agent || {}), |
| 84 | + "openspec-plan": openSpecAgent |
| 85 | + } |
| 86 | + }; |
| 87 | +}; |
| 88 | +``` |
| 89 | + |
| 90 | +### 6.2. System Prompt |
| 91 | +The prompt will instruct the model to: |
| 92 | +- Act as an Architect. |
| 93 | +- Read `project.md` and `AGENTS.md` for context. |
| 94 | +- Create/Update `specs/*.spec.md` files. |
| 95 | +- **NOT** write implementation code. |
0 commit comments