1+ import path from "path"
12import { BusEvent } from "@/bus/bus-event"
23import { InstanceState } from "@/effect"
34import { EffectBridge } from "@/effect"
@@ -6,11 +7,19 @@ import { SessionID, MessageID } from "@/session/schema"
67import { Effect , Layer , Context } from "effect"
78import z from "zod"
89import { Config } from "../config"
10+ import { ConfigMarkdown } from "../config"
11+ import { Global } from "@/global"
12+ import { Flag } from "@/flag/flag"
913import { MCP } from "../mcp"
1014import { Skill } from "../skill"
15+ import { Filesystem } from "../util"
16+ import { Glob } from "@opencode-ai/shared/util/glob"
17+ import { Log } from "../util"
1118import PROMPT_INITIALIZE from "./template/initialize.txt"
1219import PROMPT_REVIEW from "./template/review.txt"
1320
21+ const log = Log . create ( { service : "command" } )
22+
1423type State = {
1524 commands : Record < string , Info >
1625}
@@ -33,7 +42,7 @@ export const Info = z
3342 description : z . string ( ) . optional ( ) ,
3443 agent : z . string ( ) . optional ( ) ,
3544 model : z . string ( ) . optional ( ) ,
36- source : z . enum ( [ "command" , "mcp" , "skill" ] ) . optional ( ) ,
45+ source : z . enum ( [ "command" , "mcp" , "skill" , "claude" ] ) . optional ( ) ,
3746 // workaround for zod not supporting async functions natively so we use getters
3847 // https://zod.dev/v4/changelog?id=zfunction
3948 template : z . promise ( z . string ( ) ) . or ( z . string ( ) ) ,
@@ -57,6 +66,112 @@ export function hints(template: string) {
5766 return result
5867}
5968
69+ // #33 Tier 3: Load Claude Code-style markdown commands from:
70+ // - ~/.claude/commands/**/*.md (global user commands)
71+ // - <worktree-ancestors>/.claude/commands/**/*.md (project commands)
72+ // - ~/.claude/plugins/cache/**/commands/**/*.md (plugin commands)
73+ //
74+ // Name derivation follows Claude Code convention: relative path from the
75+ // commands/ root, "/" → ":", ".md" stripped. So sub/foo.md → "sub:foo".
76+ // First occurrence wins (user → project → plugin) to keep personal
77+ // commands authoritative over plugins.
78+ async function loadClaudeCommands ( worktree : string , directory : string ) : Promise < Record < string , Info > > {
79+ const result : Record < string , Info > = { }
80+
81+ // #33 Derive a colon-namespaced command name from a file path relative
82+ // to its commands/ root. Normalizes path separators and strips ".md".
83+ const nameFrom = ( rel : string ) => rel . replace ( / \\ / g, "/" ) . replace ( / \. m d $ / i, "" ) . replace ( / \/ / g, ":" )
84+
85+ const parseCommand = async ( file : string , name : string , scope : string ) => {
86+ if ( result [ name ] ) return
87+ try {
88+ const md = await ConfigMarkdown . parse ( file )
89+ const data = md . data as Record < string , unknown >
90+ const description = typeof data . description === "string" ? data . description : undefined
91+ const agent = typeof data . agent === "string" ? data . agent : undefined
92+ const model = typeof data . model === "string" ? data . model : undefined
93+ const content = md . content
94+ result [ name ] = {
95+ name,
96+ description,
97+ agent,
98+ model,
99+ source : "claude" ,
100+ get template ( ) {
101+ return content
102+ } ,
103+ hints : hints ( content ) ,
104+ }
105+ } catch ( err ) {
106+ log . error ( "failed to load claude command" , { file, scope, err } )
107+ }
108+ }
109+
110+ const scanRoot = async ( root : string , scope : string ) => {
111+ if ( ! ( await Filesystem . isDir ( root ) ) ) return
112+ try {
113+ const matches = await Glob . scan ( "**/*.md" , {
114+ cwd : root ,
115+ absolute : true ,
116+ include : "file" ,
117+ dot : false ,
118+ } )
119+ for ( const match of matches ) {
120+ const rel = path . relative ( root , match )
121+ const name = nameFrom ( rel )
122+ if ( ! name ) continue
123+ await parseCommand ( match , name , scope )
124+ }
125+ } catch ( err ) {
126+ log . error ( `failed to scan ${ scope } claude commands` , { root, err } )
127+ }
128+ }
129+
130+ // 1) Global user commands: ~/.claude/commands
131+ const userRoot = path . join ( Global . Path . home , ".claude" , "commands" )
132+ await scanRoot ( userRoot , "user" )
133+
134+ // 2) Project commands via walk-up: <cwd>/<..>/.claude/commands
135+ try {
136+ for await ( const claudeDir of Filesystem . up ( {
137+ targets : [ ".claude" ] ,
138+ start : directory ,
139+ stop : worktree ,
140+ } ) ) {
141+ const commandsDir = path . join ( claudeDir , "commands" )
142+ await scanRoot ( commandsDir , "project" )
143+ }
144+ } catch ( err ) {
145+ log . error ( "failed to walk up for claude commands" , { directory, worktree, err } )
146+ }
147+
148+ // 3) Plugin cache commands: ~/.claude/plugins/cache/**/commands/**/*.md
149+ const pluginHome = path . join ( Global . Path . home , ".claude" )
150+ if ( await Filesystem . isDir ( pluginHome ) ) {
151+ try {
152+ const pluginMatches = await Glob . scan ( "plugins/cache/**/commands/**/*.md" , {
153+ cwd : pluginHome ,
154+ absolute : true ,
155+ include : "file" ,
156+ dot : false ,
157+ } )
158+ for ( const match of pluginMatches ) {
159+ const normalized = match . replace ( / \\ / g, "/" )
160+ const idx = normalized . lastIndexOf ( "/commands/" )
161+ if ( idx === - 1 ) continue
162+ const rel = normalized . slice ( idx + "/commands/" . length )
163+ const name = nameFrom ( rel )
164+ if ( ! name ) continue
165+ await parseCommand ( match , name , "plugin" )
166+ }
167+ } catch ( err ) {
168+ log . error ( "failed to scan plugin claude commands" , { pluginHome, err } )
169+ }
170+ }
171+
172+ return result
173+ }
174+
60175export const Default = {
61176 INIT : "init" ,
62177 REVIEW : "review" ,
@@ -145,6 +260,17 @@ export const layer = Layer.effect(
145260 }
146261 }
147262
263+ // #33 Tier 3: Merge Claude Code markdown commands. Priority order:
264+ // Default > Config > MCP > Claude > Skill. Skip-if-exists guards
265+ // below ensure higher-priority commands shadow Claude commands.
266+ if ( ! Flag . OPENCODE_DISABLE_EXTERNAL_COMMANDS ) {
267+ const claudeCommands = yield * Effect . promise ( ( ) => loadClaudeCommands ( ctx . worktree , ctx . directory ) )
268+ for ( const [ name , cmd ] of Object . entries ( claudeCommands ) ) {
269+ if ( commands [ name ] ) continue
270+ commands [ name ] = cmd
271+ }
272+ }
273+
148274 for ( const item of yield * skill . all ( ) ) {
149275 if ( commands [ item . name ] ) continue
150276 commands [ item . name ] = {
0 commit comments