-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmatcher.go
More file actions
181 lines (170 loc) · 5.15 KB
/
Copy pathmatcher.go
File metadata and controls
181 lines (170 loc) · 5.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package agenthooks
import (
"fmt"
"sort"
"strings"
)
// ToolMatcher is the unified tool matcher used both by config generation
// (compiled to each provider's matcher dialect where expressible) and by the
// runner's in-process filter (--filter flag) where not.
type ToolMatcher struct {
Names []string // exact native tool names
Canonical []CanonicalTool // canonical classes
MCP []string // "server/*" or "server/tool" globs
}
// IsEmpty reports whether the matcher matches everything.
func (m ToolMatcher) IsEmpty() bool {
return len(m.Names) == 0 && len(m.Canonical) == 0 && len(m.MCP) == 0
}
// Matches reports whether the tool call satisfies the matcher. An empty
// matcher matches every tool.
func (m ToolMatcher) Matches(t ToolCall) bool {
if m.IsEmpty() {
return true
}
for _, n := range m.Names {
if strings.EqualFold(n, t.Name) {
return true
}
}
for _, c := range m.Canonical {
if c == t.Canonical {
return true
}
}
if t.MCP != nil {
for _, g := range m.MCP {
server, tool, ok := strings.Cut(g, "/")
if !ok {
server, tool = g, "*"
}
if (server == "*" || server == t.MCP.Server) && (tool == "*" || tool == t.MCP.Tool) {
return true
}
}
}
return false
}
// Encode serializes the matcher for the --filter flag baked into generated
// configs whose provider dialect can't express it.
func (m ToolMatcher) Encode() string {
var parts []string
if len(m.Names) > 0 {
parts = append(parts, "names="+strings.Join(m.Names, ","))
}
if len(m.Canonical) > 0 {
cs := make([]string, len(m.Canonical))
for i, c := range m.Canonical {
cs[i] = string(c)
}
parts = append(parts, "canonical="+strings.Join(cs, ","))
}
if len(m.MCP) > 0 {
parts = append(parts, "mcp="+strings.Join(m.MCP, ","))
}
return strings.Join(parts, ";")
}
// ParseToolMatcher parses the --filter flag format produced by Encode.
func ParseToolMatcher(s string) (ToolMatcher, error) {
var m ToolMatcher
if strings.TrimSpace(s) == "" {
return m, nil
}
for _, part := range strings.Split(s, ";") {
key, val, ok := strings.Cut(part, "=")
if !ok {
return m, fmt.Errorf("agenthooks: bad filter segment %q", part)
}
items := strings.Split(val, ",")
switch key {
case "names":
m.Names = append(m.Names, items...)
case "canonical":
for _, it := range items {
m.Canonical = append(m.Canonical, CanonicalTool(it))
}
case "mcp":
m.MCP = append(m.MCP, items...)
default:
return m, fmt.Errorf("agenthooks: unknown filter key %q", key)
}
}
return m, nil
}
// canonicalNativeNames expands a canonical class to the native names a
// provider uses for it, for matcher-dialect compilation.
func canonicalNativeNames(p Provider, c CanonicalTool) []string {
type key struct {
p Provider
c CanonicalTool
}
table := map[key][]string{
{ProviderClaudeCode, ToolShell}: {"Bash"},
{ProviderClaudeCode, ToolFileRead}: {"Read"},
{ProviderClaudeCode, ToolFileWrite}: {"Write"},
{ProviderClaudeCode, ToolFileEdit}: {"Edit", "MultiEdit", "NotebookEdit"},
{ProviderClaudeCode, ToolSearch}: {"Grep", "Glob"},
{ProviderClaudeCode, ToolFetch}: {"WebFetch", "WebSearch"},
{ProviderClaudeCode, ToolTask}: {"Task"},
{ProviderCodex, ToolShell}: {"shell", "local_shell"},
{ProviderCodex, ToolFileEdit}: {"apply_patch"},
{ProviderGemini, ToolShell}: {"run_shell_command"},
{ProviderGemini, ToolFileRead}: {"read_file", "read_many_files"},
{ProviderGemini, ToolFileWrite}: {"write_file"},
{ProviderGemini, ToolFileEdit}: {"replace"},
{ProviderGemini, ToolSearch}: {"search_file_content", "glob", "list_directory"},
{ProviderGemini, ToolFetch}: {"web_fetch", "google_web_search"},
// Kimi native names are documented sparsely; only the ones the hooks
// docs themselves use are mapped.
{ProviderKimi, ToolShell}: {"Bash"},
{ProviderKimi, ToolFileWrite}: {"WriteFile"},
{ProviderKimi, ToolFileEdit}: {"StrReplaceFile"},
}
return table[key{p, c}]
}
// CompileMatcher renders the matcher in the provider's dialect. ok is false
// when the dialect can't express it (e.g. Cursor has no matchers), in which
// case config generation matches broadly and the runner filters in-process.
func CompileMatcher(p Provider, m ToolMatcher) (expr string, ok bool) {
if m.IsEmpty() {
return "", true
}
switch p {
case ProviderClaudeCode, ProviderCodex, ProviderGemini, ProviderKimi:
// Kimi shares Claude's mcp__<server>__<tool> dialect with the
// configured name verbatim (verified against kimi-code 0.22.2), so
// its MCP globs compile natively.
var alts []string
alts = append(alts, m.Names...)
for _, c := range m.Canonical {
alts = append(alts, canonicalNativeNames(p, c)...)
}
for _, g := range m.MCP {
server, tool, cut := strings.Cut(g, "/")
if !cut {
server, tool = g, "*"
}
sep := "__"
prefix := "mcp__"
if p == ProviderGemini {
sep = "_"
prefix = "mcp_"
}
sp, tp := server, tool
if sp == "*" {
sp = ".*"
}
if tp == "*" {
tp = ".*"
}
alts = append(alts, prefix+sp+sep+tp)
}
if len(alts) == 0 {
return "", false
}
sort.Strings(alts)
return strings.Join(alts, "|"), true
default:
return "", false
}
}