Files
beaver_project/app-instance/backend/beaver/tools/registry/tool_registry.py

56 lines
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""Beaver 工具注册表。
这层只做三件事:
1. 注册工具
2. 按名称查找工具
3. 导出 provider 可消费的 tool schemas
不要把执行逻辑塞进这里。
执行属于 runtime/executor那样边界更清晰。
"""
from __future__ import annotations
from typing import Iterable
from beaver.tools.base import BaseTool, ToolSpec
class ToolRegistry:
"""统一维护当前 runtime 可用的工具集合。"""
def __init__(self) -> None:
self._tools: dict[str, BaseTool] = {}
def register(self, tool: BaseTool, *, replace: bool = False) -> None:
"""注册一个工具。
默认不允许重名覆盖,避免 loader/runtime 不小心把同名工具静默冲掉。
"""
name = tool.spec.name
if not replace and name in self._tools:
raise ValueError(f"Tool '{name}' is already registered")
self._tools[name] = tool
def register_many(self, tools: Iterable[BaseTool], *, replace: bool = False) -> None:
for tool in tools:
self.register(tool, replace=replace)
def get(self, name: str) -> BaseTool | None:
return self._tools.get(name)
def require(self, name: str) -> BaseTool:
tool = self.get(name)
if tool is None:
raise KeyError(f"Unknown tool '{name}'")
return tool
def list_specs(self) -> list[ToolSpec]:
return [tool.spec for tool in self._tools.values()]
def export_provider_schemas(self) -> list[dict]:
"""导出给 provider 的函数工具 schema 列表。"""
return [spec.to_provider_schema() for spec in self.list_specs()]