feat(tools): Add feasible/annotated tool filtering based on agent-state predicates#168
feat(tools): Add feasible/annotated tool filtering based on agent-state predicates#168
Conversation
…a#148) ToolManager.get_all_tools_schema() exposes every registered tool to the LLM regardless of whether the tool is actually usable by the agent. A wolf with 0 energy still sees move_one_step; an agent with no grid still sees teleport_to_location. Add a requires predicate system: - @tool(requires=fn) accepts an (agent) -> bool predicate that checks whether the tool's preconditions are met - ToolManager.get_feasible_tools_schema(agent) returns only tools whose requires predicate passes (or tools without a predicate) - ToolManager.get_annotated_tools_schema(agent) returns all tools with available/unavailable_reason annotations for planner use Apply requires predicates to built-in tools: - move_one_step, teleport_to_location: require grid/space + position - speak_to: requires at least one other agent The existing get_all_tools_schema() is unchanged for full backward compatibility.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #168 +/- ##
==========================================
+ Coverage 90.08% 91.15% +1.06%
==========================================
Files 19 19
Lines 1503 1582 +79
==========================================
+ Hits 1354 1442 +88
+ Misses 149 140 -9 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Add direct tests for _has_grid_and_position and _has_other_agents predicate functions covering all branches: no grid/space, grid with position, cell coordinate, no position, and agent isolation.
Summary
Closes #148.
Adds support for conditionally filtering tools based on agent-state predicates, so reasoning strategies can present only feasible tools to the LLM (or annotate infeasible ones for planner awareness).
Changes
@tool(requires=...)decorator parameterrequireskeyword on@tool: a callable(agent) -> boolthat returnsTruewhen the tool is usable by the given agent.fn.__tool_requires__on the decorated function.ToolManagermethods_is_tool_feasible(fn, agent)— checks the__tool_requires__predicate; returnsTruewhen no predicate is set or when it passes,Falseon failure or exception.get_feasible_tools_schema(agent, selected_tools=None)— returns schemas only for tools whose preconditions are satisfied.get_annotated_tools_schema(agent, selected_tools=None)— returns all tool schemas with addedavailable(bool) andunavailable_reason(str | None) keys, so planners can reason about currently-infeasible tools.Built-in tool predicates
_has_grid_and_position(agent)— applied tomove_one_stepandteleport_to_location._has_other_agents(agent)— applied tospeak_to.Tests
tests/test_tools/test_feasible_tools.pycovering:requiresalways includedselected_toolsinteractionavailable,unavailable_reason)Backward Compatibility
requiresdefaults toNone, so all existing@tooldecorations are unaffected.get_all_tools_schema()remains unchanged; the new methods are additive.