#!/usr/bin/env node
import {
  getAgentDefinitions
} from "../../chunk-JTTTXGHX.js";
import {
  isClaudeCLIAvailable
} from "../../chunk-CJFWO46A.js";
import {
  BaseCommand,
  EXIT_CODES
} from "../../chunk-LNA6M2IE.js";
import {
  resolveSource
} from "../../chunk-GFDGYQ6M.js";
import "../../chunk-T4EXUIBY.js";
import {
  fileExists,
  readFile
} from "../../chunk-C4QI54PN.js";
import {
  CLAUDE_DIR,
  CLI_COLORS
} from "../../chunk-FTD5Z6QD.js";
import {
  init_esm_shims
} from "../../chunk-DHET7RCE.js";

// src/cli/commands/new/agent.tsx
init_esm_shims();
import { useState } from "react";
import { Args, Flags } from "@oclif/core";
import { TextInput } from "@inkjs/ui";
import { spawn } from "child_process";
import matter from "gray-matter";
import { render, Box, Text, useApp, useInput } from "ink";
import path from "path";
import { jsx, jsxs } from "react/jsx-runtime";
var META_AGENT_NAME = "agent-summoner";
var PurposeInput = ({ onSubmit, onCancel }) => {
  const { exit } = useApp();
  const [error, setError] = useState(null);
  useInput((_input, key) => {
    if (key.escape) {
      onCancel();
      exit();
    }
  });
  const handleSubmit = (value) => {
    const trimmed = value.trim();
    if (!trimmed) {
      setError("Purpose is required");
      return;
    }
    onSubmit(trimmed);
    exit();
  };
  return /* @__PURE__ */ jsxs(Box, { flexDirection: "column", children: [
    /* @__PURE__ */ jsx(Text, { bold: true, children: "Create New Agent" }),
    /* @__PURE__ */ jsx(Text, { children: "What should this agent do?" }),
    /* @__PURE__ */ jsx(Text, { dimColor: true, children: "e.g., Manages database migrations with rollback support" }),
    /* @__PURE__ */ jsx(Text, { children: " " }),
    /* @__PURE__ */ jsx(TextInput, { placeholder: "Enter agent purpose...", onSubmit: handleSubmit }),
    error && /* @__PURE__ */ jsx(Box, { marginTop: 1, children: /* @__PURE__ */ jsx(Text, { color: CLI_COLORS.ERROR, children: error }) })
  ] });
};
function parseCompiledAgent(content) {
  const { data: frontmatter, content: body } = matter(content);
  const tools = typeof frontmatter.tools === "string" ? frontmatter.tools.split(",").map((t) => t.trim()) : frontmatter.tools;
  return {
    description: frontmatter.description || "Creates new agents",
    prompt: body.trim(),
    model: frontmatter.model,
    tools
  };
}
async function loadMetaAgent(projectDir, source, forceRefresh) {
  const compiledFileName = `${META_AGENT_NAME}.md`;
  const localAgentPath = path.join(projectDir, CLAUDE_DIR, "agents", compiledFileName);
  if (await fileExists(localAgentPath)) {
    return parseCompiledAgent(await readFile(localAgentPath));
  }
  try {
    const agentPaths = await getAgentDefinitions(source, { forceRefresh, projectDir });
    const remoteAgentPath = path.join(
      agentPaths.sourcePath,
      CLAUDE_DIR,
      "agents",
      compiledFileName
    );
    if (await fileExists(remoteAgentPath)) {
      return parseCompiledAgent(await readFile(remoteAgentPath));
    }
  } catch {
  }
  throw new Error(
    `Agent '${META_AGENT_NAME}' not found.

Run 'compile' first to generate agents.`
  );
}
function buildAgentPrompt(agentName, purpose, outputDir) {
  return `Create a new Claude Code agent named "${agentName}" in the directory "${outputDir}".

Agent Purpose: ${purpose}

Requirements:
1. Create the agent directory structure at ${outputDir}/${agentName}/
2. Create agent.yaml with appropriate configuration
3. Create intro.md with the agent's role and context
4. Create workflow.md with the agent's operational process
5. Optionally create examples.md if relevant examples would help
6. Optionally create critical-requirements.md for important rules
7. Include \`custom: true\` in the agent.yaml configuration

Follow the existing agent patterns in the codebase. Keep the agent focused and practical.`;
}
async function invokeMetaAgent(agentDef, prompt, nonInteractive) {
  const agentsJson = JSON.stringify({
    [META_AGENT_NAME]: {
      description: agentDef.description,
      prompt: agentDef.prompt,
      model: agentDef.model,
      tools: agentDef.tools
    }
  });
  const args = ["--agents", agentsJson, "--agent", META_AGENT_NAME];
  if (nonInteractive) {
    args.push("-p", prompt);
  } else {
    args.push("--prompt", prompt);
  }
  return new Promise((resolve, reject) => {
    const child = spawn("claude", args, {
      stdio: "inherit"
    });
    child.on("error", (error) => {
      reject(new Error(`Failed to spawn claude CLI: ${error.message}`));
    });
    child.on("close", (code) => {
      if (code === 0) {
        resolve();
      } else {
        reject(new Error(`Claude CLI exited with code ${code}`));
      }
    });
  });
}
var NewAgent = class _NewAgent extends BaseCommand {
  static summary = "Create a new custom agent using AI generation";
  static description = "Uses the agent-summoner meta-agent to scaffold a new agent with proper structure and documentation.";
  static args = {
    name: Args.string({
      description: "Name of the agent to create",
      required: true
    })
  };
  static flags = {
    ...BaseCommand.baseFlags,
    purpose: Flags.string({
      char: "p",
      description: "Purpose/description of the agent",
      required: false
    }),
    "non-interactive": Flags.boolean({
      char: "n",
      description: "Run in non-interactive mode",
      default: false
    }),
    refresh: Flags.boolean({
      char: "r",
      description: "Force refresh remote source",
      default: false
    })
  };
  async run() {
    const { args, flags } = await this.parse(_NewAgent);
    const projectDir = process.cwd();
    const cliAvailable = await isClaudeCLIAvailable();
    if (!cliAvailable) {
      this.error(
        "Claude CLI not found. Please install it first:\n  npm install -g @anthropic-ai/claude-code",
        { exit: EXIT_CODES.ERROR }
      );
    }
    let purpose = flags.purpose;
    if (!purpose) {
      let inputResult = null;
      let cancelled = false;
      const { waitUntilExit } = render(
        /* @__PURE__ */ jsx(
          PurposeInput,
          {
            onSubmit: (value) => {
              inputResult = value;
            },
            onCancel: () => {
              cancelled = true;
            }
          }
        )
      );
      await waitUntilExit();
      if (cancelled || !inputResult) {
        this.log("Cancelled");
        this.exit(EXIT_CODES.CANCELLED);
      }
      purpose = inputResult;
    }
    const outputDir = path.join(projectDir, CLAUDE_DIR, "agents", "_custom");
    this.log("");
    this.log(`Agent name: ${args.name}`);
    this.log(`Purpose: ${purpose}`);
    this.log(`Output: ${outputDir}`);
    this.log("");
    this.log("Fetching agent-summoner from source...");
    try {
      const sourceConfig = await resolveSource(flags.source, projectDir);
      const agentDef = await loadMetaAgent(projectDir, sourceConfig.source, flags.refresh);
      this.log("Meta-agent loaded");
      this.log("");
      const agentPrompt = buildAgentPrompt(args.name, purpose, outputDir);
      this.log("Invoking agent-summoner to create your agent...");
      this.log("\u2500".repeat(60));
      this.log("");
      await invokeMetaAgent(agentDef, agentPrompt, flags["non-interactive"]);
      this.log("");
      this.log("\u2500".repeat(60));
      this.logSuccess("Agent creation complete!");
    } catch (error) {
      this.handleError(error);
    }
  }
};
export {
  buildAgentPrompt,
  NewAgent as default
};
//# sourceMappingURL=agent.js.map