Add shared tool permission types and markdown file reader.

This commit is contained in:
CREDO23 2026-05-01 20:30:20 +02:00
parent 30cd530ac6
commit 77f52af6cc
2 changed files with 53 additions and 0 deletions

View file

@ -0,0 +1,14 @@
"""Load markdown files shipped alongside a route package."""
from __future__ import annotations
from importlib import resources
def read_md_file(package: str, stem: str) -> str:
"""Load ``{stem}.md`` from ``package`` via importlib resources, or return empty."""
ref = resources.files(package).joinpath(f"{stem}.md")
if not ref.is_file():
return ""
text = ref.read_text(encoding="utf-8")
return text.rstrip("\n")

View file

@ -0,0 +1,39 @@
"""Typed tool-permission rows: allow vs ask (``name`` + optional ``tool``)."""
from __future__ import annotations
from typing import NotRequired, TypedDict
from langchain_core.tools import BaseTool
class ToolPermissionItem(TypedDict):
"""``name`` is always set; ``tool`` is present when a bound tool exists."""
name: str
tool: NotRequired[BaseTool]
class ToolsPermissions(TypedDict):
"""Same shape for native factories and MCP name-only policy rows."""
allow: list[ToolPermissionItem]
ask: list[ToolPermissionItem]
def tool_permission_row(tool: BaseTool) -> ToolPermissionItem:
"""Build one allow/ask row for a loaded tool."""
return {"name": getattr(tool, "name", "") or "", "tool": tool}
def merge_tools_permissions(
base: ToolsPermissions,
extra: ToolsPermissions | None,
) -> ToolsPermissions:
"""Concatenate allow/ask lists (e.g. native factory + MCP bucket) before building HITL maps."""
if not extra:
return base
return {
"allow": [*base["allow"], *extra["allow"]],
"ask": [*base["ask"], *extra["ask"]],
}