// @bun
import {
  AdkError
} from "./chunk-7krp2mgz.js";

// src/utils/agent-root.ts
import path from "path";
import fs from "fs/promises";
async function findAgentRoot(startPath) {
  let currentPath = path.resolve(startPath);
  const root = path.parse(currentPath).root;
  while (currentPath !== root) {
    try {
      await fs.access(path.join(currentPath, "agent.config.ts"));
      return currentPath;
    } catch {
      currentPath = path.dirname(currentPath);
    }
  }
  return null;
}
async function findAgentRootOrFail(startPath) {
  const root = await findAgentRoot(startPath);
  if (!root) {
    throw new AdkError({
      code: "PROJECT_NOT_FOUND",
      message: `No ADK agent root found in the current directory or its parents.

` + "Run `adk help` to see all commands\n" + "Run `adk init` to create a new agent",
      expected: true
    });
  }
  return root;
}

export { findAgentRoot, findAgentRootOrFail };
