import { createRequire as __createRequire } from "node:module"; const require = __createRequire(import.meta.url);
import {
  resolveCliProjectRoot
} from "../../../chunks/chunk-NTYHFBUA.js";
import "../../../chunks/chunk-TG2EQLX2.js";

// builtin/extensions/init/index.ts
import fs from "node:fs";
import path from "node:path";
function resolveProjectAgentsTarget(cwd) {
  const projectRoot = resolveCliProjectRoot(cwd);
  return {
    projectRoot,
    agentsPath: path.join(projectRoot, "AGENTS.md")
  };
}
async function loadExistingKnowledge(cwd) {
  const target = resolveProjectAgentsTarget(cwd);
  try {
    if (fs.existsSync(target.agentsPath)) {
      return { target, content: fs.readFileSync(target.agentsPath, "utf-8") };
    }
  } catch {
  }
  return { target, content: null };
}
function buildResearchPrompt(params) {
  const { isUpdate, existingContent, userIntent, cwd, projectRoot, agentsPath } = params;
  if (isUpdate && existingContent) {
    const truncatedContent = existingContent.length > 4e3 ? existingContent.slice(0, 4e3) + "\n... (truncated)" : existingContent;
    return `\u{1F4DD} \u66F4\u65B0 root AGENTS.md \u9879\u76EE\u4E0A\u4E0B\u6587

\u76EE\u6807\u6587\u4EF6\uFF1A${agentsPath}
\u9879\u76EE\u6839\u76EE\u5F55\uFF1A${projectRoot}
\u5F53\u524D\u5DE5\u4F5C\u76EE\u5F55\uFF1A${cwd}

\u5F53\u524D root AGENTS.md \u5185\u5BB9\uFF1A
\`\`\`
${truncatedContent}
\`\`\`

${userIntent ? `\u{1F4CC} \u7528\u6237\u66F4\u65B0\u610F\u56FE\uFF1A${userIntent}

` : ""}\u8BF7\u91CD\u65B0\u7814\u7A76\u9879\u76EE\u5E76\u66F4\u65B0 root AGENTS.md\uFF1A

1. \u4FDD\u6301 AGENTS.md \u662F progressive disclosure index\uFF0C\u4E0D\u8981\u5199\u6210\u957F\u7BC7\u9879\u76EE\u767E\u79D1
2. \u4FDD\u7559\u7B80\u77ED root instructions\uFF0C\u7136\u540E\u7EF4\u62A4 Repo Map
3. Repo Map \u91CC\u7684\u6BCF\u4E2A\u91CD\u8981\u76EE\u5F55\u90FD\u8981\u6709 desc\uFF0C\u8BF4\u660E\u4EC0\u4E48\u65F6\u5019\u9700\u8981\u8BFB\u8BE5\u76EE\u5F55\u6216\u76F8\u5173\u6587\u6863
4. \u53EA\u8865\u5145\u7A33\u5B9A\u89C4\u5219\u3001\u76EE\u5F55\u8DEF\u7531\u3001\u9A8C\u8BC1\u95E8\u7981\u548C\u751F\u6210\u7269\u89C4\u5219
5. \u5220\u9664\u8FC7\u65F6\u6216\u91CD\u590D README/ARCHITECTURE \u7684\u5927\u6BB5\u5185\u5BB9

\u8BF7\u4F7F\u7528 write_file \u5DE5\u5177\u66F4\u65B0\u76EE\u6807\u6587\u4EF6\uFF1A${agentsPath}
CLI \u6587\u4EF6\u5DE5\u5177\u4F1A\u4EE5\u9879\u76EE\u6839\u76EE\u5F55\u4F5C\u4E3A\u8BBF\u95EE\u8FB9\u754C\uFF1B\u4E0D\u8981\u4F7F\u7528\u989D\u5916\u7684 root \u53C2\u6570\u3002`;
  }
  return `\u{1F50D} \u521D\u59CB\u5316 root AGENTS.md \u9879\u76EE\u4E0A\u4E0B\u6587

\u76EE\u6807\u6587\u4EF6\uFF1A${agentsPath}
\u9879\u76EE\u6839\u76EE\u5F55\uFF1A${projectRoot}
\u5F53\u524D\u5DE5\u4F5C\u76EE\u5F55\uFF1A${cwd}

${userIntent ? `\u{1F4CC} \u7528\u6237\u7814\u7A76\u610F\u56FE\uFF1A${userIntent}

` : ""}\u8BF7\u7814\u7A76\u5F53\u524D\u9879\u76EE\u5E76\u521B\u5EFA root \`AGENTS.md\`\u3002\u8FD9\u4E2A\u6587\u4EF6\u5E94\u8BE5\u662F progressive disclosure index\uFF0C\u7528\u6765\u6307\u5BFC\u540E\u7EED agent \u6309\u9700\u8BFB\u53D6\u76F8\u5173\u6587\u6863\uFF0C\u800C\u4E0D\u662F\u4E00\u6B21\u6027\u603B\u7ED3\u6574\u4E2A\u4ED3\u5E93\u3002

\u7814\u7A76\u4EFB\u52A1\uFF1A
1. \u8BC6\u522B\u9879\u76EE\u7C7B\u578B\u548C\u6280\u672F\u6808\uFF08\u67E5\u770B package.json, Cargo.toml, go.mod, requirements.txt, pyproject.toml \u7B49\uFF09
2. \u63A2\u7D22\u9876\u5C42\u76EE\u5F55\u7ED3\u6784\u548C\u5173\u952E package/product \u8FB9\u754C
3. \u8BFB\u53D6 README\u3001ARCHITECTURE\u3001docs/public-api \u7B49\u5DF2\u6709\u6587\u6863\uFF0C\u907F\u514D\u91CD\u590D\u7C98\u8D34
4. \u8BC6\u522B\u751F\u6210\u7269\u89C4\u5219\u3001\u6D4B\u8BD5\u95E8\u7981\u3001\u4EE3\u7801\u5206\u5C42\u548C\u9AD8\u98CE\u9669\u6539\u52A8\u8FB9\u754C
5. \u751F\u6210\u7B80\u77ED\u3001\u7A33\u5B9A\u3001\u53EF\u7EF4\u62A4\u7684\u76EE\u5F55\u8DEF\u7531\u8BF4\u660E

\u8BF7\u751F\u6210\u5305\u542B\u4EE5\u4E0B\u7AE0\u8282\u7684 AGENTS.md\uFF1A

\`\`\`
# AGENTS.md

## How To Use This File
Briefly explain that this file is an index/router. Agents should read linked docs on demand.

## Repo Map
- \`path\`
  desc: When to read this directory and what it owns.

## Architecture Rules
Stable cross-package boundaries only.

## Context And Generated Artifacts
Rules for AGENTS.md, generated bundles, and source-of-truth files.

## Verification Map
Which focused tests/checks to run by touched area.
\`\`\`

\u8BF7\u4F7F\u7528 write_file \u5DE5\u5177\u521B\u5EFA\u76EE\u6807\u6587\u4EF6\uFF1A${agentsPath}
CLI \u6587\u4EF6\u5DE5\u5177\u4F1A\u4EE5\u9879\u76EE\u6839\u76EE\u5F55\u4F5C\u4E3A\u8BBF\u95EE\u8FB9\u754C\uFF1B\u4E0D\u8981\u4F7F\u7528\u989D\u5916\u7684 root \u53C2\u6570\u3002`;
}
async function initExtension(ctx) {
  ctx.log("Init extension loaded");
  let cachedKnowledge = null;
  try {
    const existing = await loadExistingKnowledge(ctx.cwd);
    cachedKnowledge = existing.content;
    if (cachedKnowledge) {
      ctx.log(`Pre-loaded root AGENTS.md (${cachedKnowledge.length} chars) from ${existing.target.agentsPath}`);
    } else {
      ctx.log(`No existing root AGENTS.md found at ${existing.target.agentsPath}`);
    }
  } catch (err) {
    ctx.log(`Failed to pre-load root AGENTS.md: ${err}`);
  }
  ctx.registerCommand({
    path: ["tools", "init"],
    description: "\u7814\u7A76\u9879\u76EE\u5E76\u751F\u6210/\u66F4\u65B0 root AGENTS.md \u9879\u76EE\u4E0A\u4E0B\u6587\u7D22\u5F15",
    handler: async (ctx2, args) => {
      const existing = await loadExistingKnowledge(ctx2.cwd);
      const existingContent = existing.content;
      const isUpdate = !!existing.content;
      const userIntent = args?.trim();
      cachedKnowledge = existingContent;
      if (isUpdate) {
        if (userIntent) {
          ctx2.ui.notify(`\u{1F4DD} \u6B63\u5728\u66F4\u65B0\u9879\u76EE\u77E5\u8BC6\uFF08${userIntent}\uFF09...`, "info");
        } else {
          ctx2.ui.notify("\u{1F4DD} \u6B63\u5728\u66F4\u65B0 root AGENTS.md...", "info");
        }
      } else {
        ctx2.ui.notify("\u{1F50D} \u6B63\u5728\u521D\u59CB\u5316 root AGENTS.md...", "info");
      }
      const researchPrompt = buildResearchPrompt({
        isUpdate,
        existingContent,
        userIntent,
        cwd: ctx2.cwd,
        projectRoot: existing.target.projectRoot,
        agentsPath: existing.target.agentsPath
      });
      return researchPrompt;
    }
  });
  ctx.log("Init extension ready");
}
export {
  initExtension as default
};
