Respect name= for register_tools, closes #1032

This commit is contained in:
Simon Willison 2025-05-21 21:53:22 -07:00
parent e172e9e52d
commit e90705ebf1
2 changed files with 8 additions and 8 deletions

View file

@ -145,10 +145,10 @@ def get_tools() -> Dict[str, Tool]:
) -> None: ) -> None:
# If they handed us a bare function, wrap it in a Tool # If they handed us a bare function, wrap it in a Tool
if not isinstance(tool_or_function, Tool): if not isinstance(tool_or_function, Tool):
tool_or_function = Tool.function(tool_or_function) tool_or_function = Tool.function(tool_or_function, name=name)
tool = cast(Tool, tool_or_function) tool = cast(Tool, tool_or_function)
prefix = tool.name prefix = name or tool.name
suffix = 0 suffix = 0
candidate = prefix candidate = prefix

View file

@ -210,8 +210,8 @@ def test_register_tools(tmpdir):
@hookimpl @hookimpl
def register_tools(self, register): def register_tools(self, register):
register(upper) register(llm.Tool.function(upper))
register(llm.Tool.function(count_character_in_word), name="count_chars") register(count_character_in_word, name="count_chars")
try: try:
plugins.pm.register(ToolsPlugin(), name="ToolsPlugin") plugins.pm.register(ToolsPlugin(), name="ToolsPlugin")
@ -227,8 +227,8 @@ def test_register_tools(tmpdir):
}, },
implementation=upper, implementation=upper,
), ),
"count_character_in_word": llm.Tool( "count_chars": llm.Tool(
name="count_character_in_word", name="count_chars",
description="Count the number of occurrences of a character in a word.", description="Count the number of occurrences of a character in a word.",
input_schema={ input_schema={
"properties": { "properties": {
@ -248,7 +248,7 @@ def test_register_tools(tmpdir):
assert result.output == ( assert result.output == (
"upper(text: str) -> str\n" "upper(text: str) -> str\n"
" Convert text to uppercase.\n" " Convert text to uppercase.\n"
"count_character_in_word(text: str, character: str) -> int\n" "count_chars(text: str, character: str) -> int\n"
" Count the number of occurrences of a character in a word.\n" " Count the number of occurrences of a character in a word.\n"
) )
# And --json # And --json
@ -263,7 +263,7 @@ def test_register_tools(tmpdir):
"type": "object", "type": "object",
}, },
}, },
"count_character_in_word": { "count_chars": {
"description": "Count the number of occurrences of a character in a word.", "description": "Count the number of occurrences of a character in a word.",
"arguments": { "arguments": {
"properties": { "properties": {