Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

1.7 KB
Raw Blame
import type { AppRouterInstance } from "@/app/router";

const MAX_ROUTE_RESULTS = 30;

function getRouteMap(router: AppRouterInstance): string[] {
  const routes = router.getRoutes();
  const paths = new Set<string>();

  for (const route of routes) {
    const path = route.path;
    // Skip empty, auth-only, and error routes
    if (
      !path ||
      path.startsWith("/auth") ||
      path === "/403" ||
      path === "/404"
    ) {
      continue;
    }
    // Skip internal layout-only routes (no name, have children)
    if (!route.name && route.children.length > 0) continue;

    paths.add(path);
  }

  return [...paths].sort((a, b) => a.localeCompare(b));
}

export function createNavigateTool(router: AppRouterInstance) {
  return async (args: {
    path?: string;
    list?: boolean;
    query?: string;
  }): Promise<string> => {
    if (args.list) {
      const query = args.query?.trim().toLowerCase();
      const matchingRoutes = getRouteMap(router).filter(
        (path) => !query || path.toLowerCase().includes(query)
      );
      return JSON.stringify({
        routes: matchingRoutes.slice(0, MAX_ROUTE_RESULTS),
        total: matchingRoutes.length,
        truncated: matchingRoutes.length > MAX_ROUTE_RESULTS,
      });
    }

    if (!args.path) {
      return JSON.stringify({ error: "path is required when list is not set" });
    }

    try {
      await router.push(args.path);
      return JSON.stringify({
        navigated: true,
        currentPath: router.currentRoute.value.fullPath,
      });
    } catch (err) {
      return JSON.stringify({
        error: `Navigation failed: ${err instanceof Error ? err.message : String(err)}`,
      });
    }
  };
}