CI / lint (push) Successful in 58s
CI / security (push) Successful in 40s
CI / test (push) Successful in 1m15s
CI / build (push) Successful in 37s
CI / e2e (push) Successful in 10m15s
Desktop Build / build-windows (push) Canceled after 0s
Desktop Build / build-linux (push) Canceled after 0s
114 lines
3.8 KiB
Python
114 lines
3.8 KiB
Python
"""Built-in tool services (Phase 0 — read/search).
|
|
|
|
These functions are the single source of truth consumed by both the in-app
|
|
assistant and the MCP server. They delegate to the shared business-logic
|
|
services (``backend.services``) so routes and tools never diverge.
|
|
Mutating tools (create/edit/rename/move/delete) are added in later phases.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from typing import Any
|
|
|
|
from backend.services.files import read_file_text
|
|
from backend.services.search import list_tags as _list_tags
|
|
from backend.services.search import search_vaults
|
|
from backend.services.vaults import browse_directory, list_accessible_vaults
|
|
from backend.tools.context import ToolContext, ToolRisk
|
|
from backend.tools.registry import tool
|
|
from backend.tools.schemas import (
|
|
ListDirectoryInput,
|
|
ListTagsInput,
|
|
ListVaultsInput,
|
|
ReadFileInput,
|
|
SearchFulltextInput,
|
|
)
|
|
|
|
logger = logging.getLogger("obsigate.tools.service")
|
|
|
|
# Maximum file size returned by ``read_file`` (bytes).
|
|
TOOL_MAX_READ_BYTES = 200_000
|
|
|
|
|
|
@tool(
|
|
name="list_vaults",
|
|
description="List the vaults the current user is allowed to access.",
|
|
input_model=ListVaultsInput,
|
|
risk=ToolRisk.READ,
|
|
)
|
|
def list_vaults(ctx: ToolContext, _params: ListVaultsInput) -> list[dict[str, Any]]:
|
|
"""Return accessible vaults with a file count."""
|
|
return [
|
|
{"name": v["name"], "file_count": v["file_count"]}
|
|
for v in list_accessible_vaults(ctx.user)
|
|
]
|
|
|
|
|
|
@tool(
|
|
name="list_directory",
|
|
description="List files and subdirectories of a directory inside a vault.",
|
|
input_model=ListDirectoryInput,
|
|
risk=ToolRisk.READ,
|
|
requires_vault=True,
|
|
)
|
|
def list_directory(ctx: ToolContext, params: ListDirectoryInput) -> list[dict[str, Any]]:
|
|
"""Return the entries of a vault directory (direct children only)."""
|
|
data = browse_directory(params.vault, params.path)
|
|
return [
|
|
{"name": item["name"], "path": item["path"], "type": item["type"]}
|
|
for item in data["items"]
|
|
]
|
|
|
|
|
|
@tool(
|
|
name="read_file",
|
|
description="Read the text content of a file inside a vault (secrets redacted).",
|
|
input_model=ReadFileInput,
|
|
risk=ToolRisk.READ,
|
|
requires_vault=True,
|
|
)
|
|
def read_file(ctx: ToolContext, params: ReadFileInput) -> dict[str, Any]:
|
|
"""Return the (redacted) content of a vault file."""
|
|
return read_file_text(
|
|
params.vault,
|
|
params.path,
|
|
redact=True,
|
|
max_bytes=TOOL_MAX_READ_BYTES,
|
|
)
|
|
|
|
|
|
@tool(
|
|
name="search_fulltext",
|
|
description="Full-text search across one vault or all accessible vaults.",
|
|
input_model=SearchFulltextInput,
|
|
risk=ToolRisk.READ,
|
|
)
|
|
def search_fulltext(ctx: ToolContext, params: SearchFulltextInput) -> list[dict[str, Any]]:
|
|
"""Return ranked search results, filtered to accessible vaults."""
|
|
payload = search_vaults(params.q, vault=params.vault, tag=params.tag, limit=params.limit)
|
|
return [r for r in payload["results"] if ctx.has_vault_access(r["vault"])]
|
|
|
|
|
|
@tool(
|
|
name="list_tags",
|
|
description="List tags with their occurrence counts for a vault or all accessible vaults.",
|
|
input_model=ListTagsInput,
|
|
risk=ToolRisk.READ,
|
|
)
|
|
def list_tags(ctx: ToolContext, params: ListTagsInput) -> list[dict[str, Any]]:
|
|
"""Return tags sorted by descending count."""
|
|
if params.vault and params.vault != "all":
|
|
ctx.require_vault_access(params.vault)
|
|
return [{"tag": tag, "count": count} for tag, count in _list_tags(params.vault).items()]
|
|
|
|
from backend.indexer import get_vault_names
|
|
|
|
merged: dict[str, int] = {}
|
|
for name in get_vault_names():
|
|
if not ctx.has_vault_access(name):
|
|
continue
|
|
for tag, count in _list_tags(name).items():
|
|
merged[tag] = merged.get(tag, 0) + count
|
|
return [{"tag": tag, "count": count} for tag, count in sorted(merged.items(), key=lambda x: -x[1])]
|