@@ -48,6 +48,8 @@ import { Shell } from "@/shell/shell"
4848import { AppFileSystem } from "@/filesystem"
4949import { Truncate } from "@/tool/truncate"
5050import { decodeDataUrl } from "@/util/data-url"
51+ import { runHooks , type HookEnv } from "../hook"
52+ import { Config } from "../config/config"
5153import { Process } from "@/util/process"
5254import { Cause , Effect , Exit , Layer , Option , Scope , ServiceMap } from "effect"
5355import { InstanceState } from "@/effect/instance-state"
@@ -100,6 +102,7 @@ export namespace SessionPrompt {
100102 const filetime = yield * FileTime . Service
101103 const registry = yield * ToolRegistry . Service
102104 const truncate = yield * Truncate . Service
105+ const config = yield * Config . Service
103106 const spawner = yield * ChildProcessSpawner . ChildProcessSpawner
104107 const scope = yield * Scope . Scope
105108 const instruction = yield * Instruction . Service
@@ -458,6 +461,27 @@ NOTE: At any point in time through this workflow you should feel free to ask the
458461 return Effect . runPromise (
459462 Effect . gen ( function * ( ) {
460463 const ctx = context ( args , options )
464+
465+ // Config hooks (shell scripts) — before plugin hooks
466+ const hookCfg = yield * config . get ( )
467+ const hookEnv : HookEnv = {
468+ OPENCODE_HOOK_EVENT : "PreToolUse" ,
469+ OPENCODE_TOOL_NAME : item . id ,
470+ OPENCODE_TOOL_INPUT : JSON . stringify ( args ) ,
471+ OPENCODE_PROJECT_DIR : Instance . directory ,
472+ OPENCODE_SESSION_ID : ctx . sessionID ,
473+ }
474+ const hookResult = yield * Effect . promise ( ( ) =>
475+ runHooks ( hookCfg . hooks ?. PreToolUse , item . id , hookEnv ) ,
476+ )
477+ if ( hookResult . action === "block" ) {
478+ return {
479+ title : "Blocked by hook" ,
480+ output : hookResult . message ?? "Tool execution blocked by hook" ,
481+ metadata : { } as any ,
482+ }
483+ }
484+
461485 yield * plugin . trigger (
462486 "tool.execute.before" ,
463487 { tool : item . id , sessionID : ctx . sessionID , callID : ctx . callID } ,
@@ -478,6 +502,15 @@ NOTE: At any point in time through this workflow you should feel free to ask the
478502 { tool : item . id , sessionID : ctx . sessionID , callID : ctx . callID , args } ,
479503 output ,
480504 )
505+
506+ // PostToolUse hooks
507+ yield * Effect . promise ( ( ) =>
508+ runHooks ( hookCfg . hooks ?. PostToolUse , item . id , {
509+ ...hookEnv ,
510+ OPENCODE_HOOK_EVENT : "PostToolUse" ,
511+ } ) ,
512+ )
513+
481514 return output
482515 } ) ,
483516 )
@@ -496,6 +529,30 @@ NOTE: At any point in time through this workflow you should feel free to ask the
496529 Effect . runPromise (
497530 Effect . gen ( function * ( ) {
498531 const ctx = context ( args , opts )
532+
533+ // Config hooks (shell scripts) — before plugin hooks
534+ const mcpHookCfg = yield * config . get ( )
535+ const mcpHookEnv : HookEnv = {
536+ OPENCODE_HOOK_EVENT : "PreToolUse" ,
537+ OPENCODE_TOOL_NAME : key ,
538+ OPENCODE_TOOL_INPUT : JSON . stringify ( args ) ,
539+ OPENCODE_PROJECT_DIR : Instance . directory ,
540+ OPENCODE_SESSION_ID : ctx . sessionID ,
541+ }
542+ const mcpHookResult = yield * Effect . promise ( ( ) =>
543+ runHooks ( mcpHookCfg . hooks ?. PreToolUse , key , mcpHookEnv ) ,
544+ )
545+ if ( mcpHookResult . action === "block" ) {
546+ return {
547+ content : [
548+ {
549+ type : "text" as const ,
550+ text : mcpHookResult . message ?? "Tool execution blocked by hook" ,
551+ } ,
552+ ] ,
553+ }
554+ }
555+
499556 yield * plugin . trigger (
500557 "tool.execute.before" ,
501558 { tool : key , sessionID : ctx . sessionID , callID : opts . toolCallId } ,
@@ -511,6 +568,14 @@ NOTE: At any point in time through this workflow you should feel free to ask the
511568 result ,
512569 )
513570
571+ // PostToolUse hooks
572+ yield * Effect . promise ( ( ) =>
573+ runHooks ( mcpHookCfg . hooks ?. PostToolUse , key , {
574+ ...mcpHookEnv ,
575+ OPENCODE_HOOK_EVENT : "PostToolUse" ,
576+ } ) ,
577+ )
578+
514579 const textParts : string [ ] = [ ]
515580 const attachments : Omit < MessageV2 . FilePart , "id" | "sessionID" | "messageID" > [ ] = [ ]
516581 for ( const contentItem of result . content ) {
@@ -1330,6 +1395,16 @@ NOTE: At any point in time through this workflow you should feel free to ask the
13301395 yield * sessions . setPermission ( { sessionID : session . id , permission : permissions } )
13311396 }
13321397
1398+ // SessionStart hooks
1399+ const startHookCfg = yield * config . get ( )
1400+ yield * Effect . promise ( ( ) =>
1401+ runHooks ( startHookCfg . hooks ?. SessionStart , "" , {
1402+ OPENCODE_HOOK_EVENT : "SessionStart" ,
1403+ OPENCODE_PROJECT_DIR : Instance . directory ,
1404+ OPENCODE_SESSION_ID : input . sessionID ,
1405+ } ) ,
1406+ )
1407+
13331408 if ( input . noReply === true ) return message
13341409 return yield * loop ( { sessionID : input . sessionID } )
13351410 } ,
@@ -1744,6 +1819,7 @@ NOTE: At any point in time through this workflow you should feel free to ask the
17441819 Layer . provide ( Agent . defaultLayer ) ,
17451820 Layer . provide ( Bus . layer ) ,
17461821 Layer . provide ( CrossSpawnSpawner . defaultLayer ) ,
1822+ Layer . provide ( Config . defaultLayer ) ,
17471823 ) ,
17481824 ) ,
17491825 )
0 commit comments