11package config
22
33import (
4+ "slices"
45 "strings"
56
67 "github.com/docker/docker-agent/pkg/config/latest"
@@ -10,55 +11,41 @@ import (
1011// Each string is treated as a shell command to run.
1112// Empty strings are silently skipped.
1213func HooksFromCLI (preToolUse , postToolUse , sessionStart , sessionEnd , onUserInput []string ) * latest.HooksConfig {
13- hooks := & latest.HooksConfig {}
14-
15- if len (preToolUse ) > 0 {
16- var defs []latest.HookDefinition
17- for _ , cmd := range preToolUse {
18- if strings .TrimSpace (cmd ) == "" {
19- continue
20- }
21- defs = append (defs , latest.HookDefinition {Type : "command" , Command : cmd })
22- }
23- if len (defs ) > 0 {
24- hooks .PreToolUse = []latest.HookMatcherConfig {{Hooks : defs }}
25- }
14+ hooks := & latest.HooksConfig {
15+ PreToolUse : matcherFromCommands (preToolUse ),
16+ PostToolUse : matcherFromCommands (postToolUse ),
17+ SessionStart : defsFromCommands (sessionStart ),
18+ SessionEnd : defsFromCommands (sessionEnd ),
19+ OnUserInput : defsFromCommands (onUserInput ),
2620 }
2721
28- if len (postToolUse ) > 0 {
29- var defs []latest.HookDefinition
30- for _ , cmd := range postToolUse {
31- if strings .TrimSpace (cmd ) == "" {
32- continue
33- }
34- defs = append (defs , latest.HookDefinition {Type : "command" , Command : cmd })
35- }
36- if len (defs ) > 0 {
37- hooks .PostToolUse = []latest.HookMatcherConfig {{Hooks : defs }}
38- }
22+ if hooks .IsEmpty () {
23+ return nil
3924 }
25+ return hooks
26+ }
4027
41- for _ , cmd := range sessionStart {
42- if strings .TrimSpace (cmd ) != "" {
43- hooks .SessionStart = append (hooks .SessionStart , latest.HookDefinition {Type : "command" , Command : cmd })
44- }
45- }
46- for _ , cmd := range sessionEnd {
47- if strings .TrimSpace (cmd ) != "" {
48- hooks .SessionEnd = append (hooks .SessionEnd , latest.HookDefinition {Type : "command" , Command : cmd })
49- }
50- }
51- for _ , cmd := range onUserInput {
52- if strings .TrimSpace (cmd ) != "" {
53- hooks .OnUserInput = append (hooks .OnUserInput , latest.HookDefinition {Type : "command" , Command : cmd })
28+ // defsFromCommands turns a list of CLI shell commands into hook definitions,
29+ // skipping any blank entries.
30+ func defsFromCommands (cmds []string ) []latest.HookDefinition {
31+ var defs []latest.HookDefinition
32+ for _ , cmd := range cmds {
33+ if strings .TrimSpace (cmd ) == "" {
34+ continue
5435 }
36+ defs = append (defs , latest.HookDefinition {Type : "command" , Command : cmd })
5537 }
38+ return defs
39+ }
5640
57- if hooks .IsEmpty () {
41+ // matcherFromCommands wraps the result of defsFromCommands in a single
42+ // HookMatcherConfig so the commands apply to all tools (empty matcher).
43+ func matcherFromCommands (cmds []string ) []latest.HookMatcherConfig {
44+ defs := defsFromCommands (cmds )
45+ if len (defs ) == 0 {
5846 return nil
5947 }
60-
61- return hooks
48+ return []latest.HookMatcherConfig {{Hooks : defs }}
6249}
6350
6451// MergeHooks merges CLI hooks into an existing HooksConfig.
@@ -74,18 +61,18 @@ func MergeHooks(base, cli *latest.HooksConfig) *latest.HooksConfig {
7461 }
7562
7663 merged := & latest.HooksConfig {
77- PreToolUse : append ( append ([]latest. HookMatcherConfig {}, base .PreToolUse ... ) , cli .PreToolUse ... ),
78- PostToolUse : append ( append ([]latest. HookMatcherConfig {}, base .PostToolUse ... ) , cli .PostToolUse ... ),
79- SessionStart : append ( append ([]latest. HookDefinition {}, base .SessionStart ... ) , cli .SessionStart ... ),
80- TurnStart : append ( append ([]latest. HookDefinition {}, base .TurnStart ... ) , cli .TurnStart ... ),
81- BeforeLLMCall : append ( append ([]latest. HookDefinition {}, base .BeforeLLMCall ... ) , cli .BeforeLLMCall ... ),
82- AfterLLMCall : append ( append ([]latest. HookDefinition {}, base .AfterLLMCall ... ) , cli .AfterLLMCall ... ),
83- SessionEnd : append ( append ([]latest. HookDefinition {}, base .SessionEnd ... ) , cli .SessionEnd ... ),
84- OnUserInput : append ( append ([]latest. HookDefinition {}, base .OnUserInput ... ) , cli .OnUserInput ... ),
85- Stop : append ( append ([]latest. HookDefinition {}, base .Stop ... ) , cli .Stop ... ),
86- Notification : append ( append ([]latest. HookDefinition {}, base .Notification ... ) , cli .Notification ... ),
87- OnError : append ( append ([]latest. HookDefinition {}, base .OnError ... ) , cli .OnError ... ),
88- OnMaxIterations : append ( append ([]latest. HookDefinition {}, base .OnMaxIterations ... ) , cli .OnMaxIterations ... ),
64+ PreToolUse : slices . Concat ( base .PreToolUse , cli .PreToolUse ),
65+ PostToolUse : slices . Concat ( base .PostToolUse , cli .PostToolUse ),
66+ SessionStart : slices . Concat ( base .SessionStart , cli .SessionStart ),
67+ TurnStart : slices . Concat ( base .TurnStart , cli .TurnStart ),
68+ BeforeLLMCall : slices . Concat ( base .BeforeLLMCall , cli .BeforeLLMCall ),
69+ AfterLLMCall : slices . Concat ( base .AfterLLMCall , cli .AfterLLMCall ),
70+ SessionEnd : slices . Concat ( base .SessionEnd , cli .SessionEnd ),
71+ OnUserInput : slices . Concat ( base .OnUserInput , cli .OnUserInput ),
72+ Stop : slices . Concat ( base .Stop , cli .Stop ),
73+ Notification : slices . Concat ( base .Notification , cli .Notification ),
74+ OnError : slices . Concat ( base .OnError , cli .OnError ),
75+ OnMaxIterations : slices . Concat ( base .OnMaxIterations , cli .OnMaxIterations ),
8976 }
9077 return merged
9178}
0 commit comments