|
| 1 | +import path from "path" |
| 2 | + |
| 3 | +const agent = "plan" |
| 4 | +const root = ".opencode/plans" |
| 5 | + |
| 6 | +function file(id) { |
| 7 | + return path.posix.join(root, `${id}.md`) |
| 8 | +} |
| 9 | + |
| 10 | +function note(id) { |
| 11 | + return [ |
| 12 | + "<system-reminder>", |
| 13 | + "Planner mode is active.", |
| 14 | + "You must not edit source files, run bash, change config, or make commits.", |
| 15 | + "You may only use read-only tools, ask clarifying questions, delegate exploration or design with the task tool, and edit allowed markdown plan files.", |
| 16 | + `Write the plan to ${file(id)} or another allowed *.plan.md/*.spec.md file.`, |
| 17 | + "When the plan is complete, call the submit_plan tool to open Plannotator for review.", |
| 18 | + "</system-reminder>", |
| 19 | + ].join("\n") |
| 20 | +} |
| 21 | + |
| 22 | +function partID() { |
| 23 | + return `prt_${crypto.randomUUID()}` |
| 24 | +} |
| 25 | + |
| 26 | +function rules(input) { |
| 27 | + if (!input) return {} |
| 28 | + if (typeof input === "string") return { "*": input } |
| 29 | + return input |
| 30 | +} |
| 31 | + |
| 32 | +function merge(a, b) { |
| 33 | + const left = rules(a) |
| 34 | + const right = rules(b) |
| 35 | + const out = { ...left, ...right } |
| 36 | + |
| 37 | + for (const key of Object.keys(left)) { |
| 38 | + const x = left[key] |
| 39 | + const y = right[key] |
| 40 | + if (!x || !y || typeof x !== "object" || typeof y !== "object") continue |
| 41 | + if (Array.isArray(x) || Array.isArray(y)) continue |
| 42 | + out[key] = { ...x, ...y } |
| 43 | + } |
| 44 | + |
| 45 | + return out |
| 46 | +} |
| 47 | + |
| 48 | +function mode(input = {}) { |
| 49 | + const base = { |
| 50 | + mode: "primary", |
| 51 | + color: "info", |
| 52 | + description: "Researches the codebase and writes execution plans without editing source files.", |
| 53 | + prompt: [ |
| 54 | + "Use this agent when the user wants a design, implementation plan, or scoped investigation before coding.", |
| 55 | + "Stay in planning mode: inspect the codebase, ask targeted questions when needed, and write a concise execution plan before implementation.", |
| 56 | + "Default plan path: .opencode/plans/<session-id>.md.", |
| 57 | + "Prefer the task tool with the explore and general subagents for deeper research.", |
| 58 | + "Do not stop after writing the plan; call submit_plan to submit the plan for review.", |
| 59 | + ].join("\n\n"), |
| 60 | + permission: { |
| 61 | + "*": "deny", |
| 62 | + read: { |
| 63 | + "*": "allow", |
| 64 | + "*.env": "ask", |
| 65 | + "*.env.*": "ask", |
| 66 | + "*.env.example": "allow", |
| 67 | + }, |
| 68 | + glob: "allow", |
| 69 | + grep: "allow", |
| 70 | + question: "allow", |
| 71 | + task: { |
| 72 | + "*": "deny", |
| 73 | + explore: "allow", |
| 74 | + general: "allow", |
| 75 | + }, |
| 76 | + webfetch: "allow", |
| 77 | + websearch: "allow", |
| 78 | + codesearch: "allow", |
| 79 | + batch: "allow", |
| 80 | + submit_plan: "allow", |
| 81 | + edit: { |
| 82 | + "*": "deny", |
| 83 | + [path.posix.join(root, "*.md")]: "allow", |
| 84 | + "plans/*.md": "allow", |
| 85 | + "specs/*.md": "allow", |
| 86 | + "**/*.plan.md": "allow", |
| 87 | + "**/*.spec.md": "allow", |
| 88 | + }, |
| 89 | + bash: "deny", |
| 90 | + skill: "deny", |
| 91 | + todowrite: "deny", |
| 92 | + }, |
| 93 | + } |
| 94 | + |
| 95 | + return { |
| 96 | + ...base, |
| 97 | + ...input, |
| 98 | + prompt: [base.prompt, input?.prompt].filter(Boolean).join("\n\n"), |
| 99 | + permission: merge(base.permission, input?.permission), |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +export default async function plannerPlugin() { |
| 104 | + const seen = new Set() |
| 105 | + |
| 106 | + return { |
| 107 | + async config(cfg) { |
| 108 | + cfg.agent ??= {} |
| 109 | + cfg.agent[agent] = mode(cfg.agent[agent]) |
| 110 | + }, |
| 111 | + async "chat.message"(input, output) { |
| 112 | + if (input.agent !== agent) { |
| 113 | + seen.delete(input.sessionID) |
| 114 | + return |
| 115 | + } |
| 116 | + |
| 117 | + seen.add(input.sessionID) |
| 118 | + output.parts.push({ |
| 119 | + id: partID(), |
| 120 | + messageID: output.message.id, |
| 121 | + sessionID: output.message.sessionID, |
| 122 | + type: "text", |
| 123 | + text: note(input.sessionID), |
| 124 | + synthetic: true, |
| 125 | + }) |
| 126 | + }, |
| 127 | + async "experimental.chat.system.transform"(input, output) { |
| 128 | + if (!input.sessionID || !seen.has(input.sessionID)) return |
| 129 | + output.system.push(note(input.sessionID)) |
| 130 | + }, |
| 131 | + } |
| 132 | +} |
0 commit comments