ShouldInject checks the request's tools array against the configured injection rules and returns the first matching enabled rule, or nil if none match. format must be "openai" or "claude".
(rawJSON []byte, rules []config.ToolCallInjectionRule, modelName, format string)
| 76 | // |
| 77 | // format must be "openai" or "claude". |
| 78 | func ShouldInject(rawJSON []byte, rules []config.ToolCallInjectionRule, modelName, format string) *config.ToolCallInjectionRule { |
| 79 | if len(rules) == 0 { |
| 80 | return nil |
| 81 | } |
| 82 | |
| 83 | // Collect tool names from the request. |
| 84 | toolNames := collectToolNames(rawJSON, format) |
| 85 | if len(toolNames) == 0 { |
| 86 | return nil |
| 87 | } |
| 88 | |
| 89 | for i := range rules { |
| 90 | rule := &rules[i] |
| 91 | if !rule.Enabled { |
| 92 | continue |
| 93 | } |
| 94 | if _, ok := toolNames[rule.ToolName]; !ok { |
| 95 | continue |
| 96 | } |
| 97 | // Check model pattern. |
| 98 | if rule.ModelPattern != "" && !matchModelPattern(rule.ModelPattern, modelName) { |
| 99 | continue |
| 100 | } |
| 101 | // Check max injections. |
| 102 | maxInj := rule.MaxInjections |
| 103 | if maxInj <= 0 { |
| 104 | maxInj = 1 // default: inject once |
| 105 | } |
| 106 | if countExistingInjections(rawJSON, format) >= maxInj { |
| 107 | continue |
| 108 | } |
| 109 | return rule |
| 110 | } |
| 111 | return nil |
| 112 | } |
| 113 | |
| 114 | // collectToolNames returns a set of tool function names present in the request. |
| 115 | func collectToolNames(rawJSON []byte, format string) map[string]struct{} { |