feat(capabilities): adopt `ToolSelector` across caps + 4 new tool-scoping caps
DO
22d ago+2120+2120+0123456789012345678901234567890123456789-203-203-012345678901234567890123456789
Summary
Adopts ToolSelector consistently across tool-modifying capabilities and toolsets, and adds four new tool-scoping capabilities.
New capabilities (all spec-serializable when using non-callable selectors):
FilterTools(tools=...)— filters which tools the model sees.DeferToolLoading(tools=...)— marks tools for deferred loading (companion toToolSearch, useful for tools coming from external toolsets where you can't setTool(defer_loading=True)).RequireToolApproval(tools=..., handler=...)— single capability that both marks tools for approval AND optionally resolves approvals inline via a per-call handler. Handler is scoped to the capability's owntoolsselector, so multipleRequireToolApprovalinstances with different selectors/handlers compose cleanly (others' approvals pass through).SetToolDescription(tools=..., replace=... | append=... | prepend=...)— surgically modifies tool descriptions. Spec-serializable; key use case is LLM auto-optimization loops mutating agent specs.
ToolSelector added to existing capabilities/toolsets (all backwards-compatible):
ToolSearch.tools— union with the per-tooldefer_loadingflag (additive; defaultNonepreserves existing behavior).PrefixTools.tools— only prefix matching tools; alsowrappedis now optional so it can work standalone viaget_wrapper_toolset.PrepareTools.tools/PrepareOutputTools.tools— restrict which tools the prepare function sees; others pass through.- Toolset wrappers:
FilteredToolset,PrefixedToolset,PreparedToolset,DeferredLoadingToolset,ApprovalRequiredToolset,IncludeReturnSchemasToolset,SetMetadataToolsetall gained atools: ToolSelectorparameter. - Convenience methods on
AbstractToolset:.filtered(),.prefixed(),.prepared(),.approval_required(),.defer_loading(),.include_return_schemas(),.with_metadata()updated. New.with_description(). WrapperCapability.wrappedis now optional (Nonedefault) so subclasses can work standalone and just modify agent-wide tools viaget_wrapper_toolset.SetToolMetadata/with_metadatanow accept an explicitmetadatadict alongside**kwargs(fixes the case wheremetadatawas a desired key name).
Backward compatibility:
FilteredToolset(wrapped, callable)still works (callables are validToolSelectors).FilteredToolset(wrapped, filter_func=...)keyword form is deprecated.DeferredLoadingToolset(wrapped, tool_names=...)is deprecated in favor oftools=....ApprovalRequiredToolsetkeepsapproval_required_func(not deprecated) for per-call args-aware refinement;toolsis additive for tool-level scoping.
Test plan
-
pytest tests/test_capabilities.py tests/test_toolsets.py tests/test_tool_search.py tests/test_tools.py— 964 passed locally (50 deselected on env-only issues: google-genai not installed, anthropic SDK version mismatch,pythonbinary off PATH). - 22 new tests covering:
toolswith name list / metadata dict / callable on every updated toolset and capability; deprecation warnings;PrefixToolsstandalone + with wrapped + with selector;RequireToolApprovalwith/without handler, denial, compose across multiple instances;SetToolDescriptionreplace/append/prepend/validation;ToolSearch.toolsunion semantics + selector-only mode + backwards-compat withNonedefault. - All snapshot tests regenerated and pass.
Checklist
- Any AI generated code has been reviewed line-by-line by the human PR author, who stands by it.
- No breaking changes in accordance with the version policy.
- PR title is fit for the release changelog.