Skip to content

Context

context

ContextTool — one-shot project context dump.

ContextTool

Bases: AXMTool

One-shot project context: stack, patterns, module ranking.

Registered as ast_context via axm.tools entry point. Workspace-aware: if path is a uv workspace root, returns workspace-level context with all packages.

Source code in packages/axm-ast/src/axm_ast/tools/context.py
Python
class ContextTool(AXMTool):
    """One-shot project context: stack, patterns, module ranking.

    Registered as ``ast_context`` via axm.tools entry point.
    Workspace-aware: if path is a uv workspace root, returns
    workspace-level context with all packages.
    """

    @property
    def name(self) -> str:
        """Return tool name for registry lookup."""
        return "ast_context"

    @safe_execute
    def execute(
        self,
        *,
        path: str = ".",
        depth: int | None = 1,
        **kwargs: object,
    ) -> ToolResult:
        """Dump complete project context for AI agents.

        Args:
            path: Path to package or workspace directory.
            depth: Detail level (0=top-5, 1=sub-packages,
                2=modules, 3+=symbols, None=full).

        Returns:
            ToolResult with project context data.
        """
        project_path = Path(path).resolve()
        if not project_path.is_dir():
            return ToolResult(success=False, error=f"Not a directory: {project_path}")

        try:
            from axm_ast.core.workspace import (
                build_workspace_context,
                format_workspace_context,
                format_workspace_text,
            )

            ws_ctx = build_workspace_context(project_path)
            ws_formatted = format_workspace_context(
                ws_ctx, depth=depth if depth is not None else 1
            )
            return ToolResult(
                success=True,
                data=cast("dict[str, object]", ws_formatted),
                text=format_workspace_text(ws_formatted),
            )
        except ValueError:
            pass

        from axm_ast.core.context import (
            build_context,
            format_context_json,
            format_context_text,
        )

        ctx: ContextResult = build_context(project_path)
        formatted: FormattedContext = format_context_json(ctx, depth=depth)
        text: str | None
        try:
            text = format_context_text(
                formatted, depth=depth if depth is not None else 0
            )
        except (KeyError, TypeError):
            text = None
        # ``ToolResult.data`` is declared ``dict[str, Any]`` upstream
        # (axm.tools.base); ``FormattedContext`` is structurally compatible
        # but ``TypedDict`` is invariant under ``dict[...]`` — narrow cast.
        return ToolResult(
            success=True,
            data=cast("dict[str, object]", formatted),
            text=text,
        )
name property

Return tool name for registry lookup.

execute(*, path='.', depth=1, **kwargs)

Dump complete project context for AI agents.

Parameters:

Name Type Description Default
path str

Path to package or workspace directory.

'.'
depth int | None

Detail level (0=top-5, 1=sub-packages, 2=modules, 3+=symbols, None=full).

1

Returns:

Type Description
ToolResult

ToolResult with project context data.

Source code in packages/axm-ast/src/axm_ast/tools/context.py
Python
@safe_execute
def execute(
    self,
    *,
    path: str = ".",
    depth: int | None = 1,
    **kwargs: object,
) -> ToolResult:
    """Dump complete project context for AI agents.

    Args:
        path: Path to package or workspace directory.
        depth: Detail level (0=top-5, 1=sub-packages,
            2=modules, 3+=symbols, None=full).

    Returns:
        ToolResult with project context data.
    """
    project_path = Path(path).resolve()
    if not project_path.is_dir():
        return ToolResult(success=False, error=f"Not a directory: {project_path}")

    try:
        from axm_ast.core.workspace import (
            build_workspace_context,
            format_workspace_context,
            format_workspace_text,
        )

        ws_ctx = build_workspace_context(project_path)
        ws_formatted = format_workspace_context(
            ws_ctx, depth=depth if depth is not None else 1
        )
        return ToolResult(
            success=True,
            data=cast("dict[str, object]", ws_formatted),
            text=format_workspace_text(ws_formatted),
        )
    except ValueError:
        pass

    from axm_ast.core.context import (
        build_context,
        format_context_json,
        format_context_text,
    )

    ctx: ContextResult = build_context(project_path)
    formatted: FormattedContext = format_context_json(ctx, depth=depth)
    text: str | None
    try:
        text = format_context_text(
            formatted, depth=depth if depth is not None else 0
        )
    except (KeyError, TypeError):
        text = None
    # ``ToolResult.data`` is declared ``dict[str, Any]`` upstream
    # (axm.tools.base); ``FormattedContext`` is structurally compatible
    # but ``TypedDict`` is invariant under ``dict[...]`` — narrow cast.
    return ToolResult(
        success=True,
        data=cast("dict[str, object]", formatted),
        text=text,
    )