3a52f7bd4b
Add a composable EnableCondition system for tool filtering that:
1. **User-facing API** (conditions.go):
- EnableCondition interface with Evaluate(ctx) method
- Primitives: FeatureFlag(), ContextBool(), Static(), Always(), Never()
- Combinators: And(), Or(), Not() with short-circuit evaluation
- All bitmask complexity hidden from users
2. **Bitmask compiler** (condition_compiler.go):
- Compiles conditions to O(1) bitmask evaluators at build time
- RequestMask holds pre-computed uint64 bitmask per request
- AND/OR of flags compile to single bitmask operations
- Falls back gracefully for custom ConditionFunc
3. **Pre-sorting optimization** (builder.go):
- Tools, resources, prompts sorted once at build time
- Filtering preserves order, eliminating per-request sorting
- ~45% faster request handling in benchmarks
4. **Integration** (filters.go, registry.go):
- Builder.Build() compiles all EnableConditions
- AvailableTools() builds RequestMask once, evaluates via bitmask
- Backward compatible with legacy Enabled func and feature flags
Usage example:
tool.EnableCondition = Or(
ContextBool("is_cca"), // CCA users bypass flag
FeatureFlag("my_feature"), // Others need flag enabled
)
Benchmarks (1000 requests × 50 tools):
- Before: 23.7ms (with per-request sorting)
- After: 12.9ms (pre-sorted + bitmask)
- Improvement: 46% faster
This makes it easy for remote server to adopt - just set EnableCondition
on tools and the optimization is automatic.