Skip to content

Framework for orchestrating role-playing, autonomous AI agents. By fostering collaborative intelligence, CrewAI empowers agents to work together seamlessly, tackling complex tasks.

48.2 KB
Raw Blame
"""Scaffold a new JSON-first crew project."""

from __future__ import annotations

import asyncio
import json
import os
from pathlib import Path
import re
import sys
from typing import Any
import warnings

import click
from crewai_core.platform_app_defaults import PLATFORM_APP_DEFAULT_TOOLS
from crewai_core.platform_apps import (
    PLATFORM_APPLICATION_CATALOG,
    PLATFORM_APP_CATEGORIES,
    PLATFORM_APP_DISPLAY_NAMES,
    PLATFORM_APP_TOOLS,
    PLATFORM_APP_TOOL_COUNTS,
)
from crewai_core.telemetry import Telemetry
from rich.console import Console
from rich.text import Text

from crewai_cli.constants import ENV_VARS
from crewai_cli.git import initialize_if_git_available
from crewai_cli.model_catalog import get_provider_models
from crewai_cli.tui_picker import pick_many, pick_one
from crewai_cli.utils import (
    copy_assistant_instructions,
    enable_prompt_line_editing,
    get_or_create_project_id,
    is_dmn_mode_enabled,
    load_env_vars,
    render_template,
    write_env_file,
)
from crewai_cli.version import get_crewai_tools_dependency


# ── Provider / model data ───────────────────────────────────────

_PROVIDERS: list[tuple[str, str]] = [
    ("openai", "OpenAI"),
    ("anthropic", "Anthropic"),
    ("gemini", "Google Gemini"),
    ("groq", "Groq"),
    ("ollama", "Ollama"),
    ("bedrock", "AWS Bedrock"),
    ("azure", "Azure OpenAI"),
    ("nvidia_nim", "NVIDIA NIM"),
    ("huggingface", "Hugging Face"),
    ("cerebras", "Cerebras"),
    ("sambanova", "SambaNova"),
    ("watson", "IBM watsonx"),
]

# Curated offline fallback / label source. The picker prefers models pulled
# live from the vendor's own API via ``model_catalog.get_provider_models``;
# this list is the hand-verified backstop used when no API key is available.
# Keep entries to real, current model ids — last verified against each vendor's
# official model docs on 2026-08-17.
_PROVIDER_MODELS: dict[str, list[tuple[str, str]]] = {
    "openai": [
        ("gpt-5.5", "GPT-5.5"),
        ("gpt-5.5-pro", "GPT-5.5 Pro"),
        ("gpt-5.6-sol", "GPT-5.6 Sol"),
        ("gpt-5.6-terra", "GPT-5.6 Terra"),
        ("gpt-5.6-luna", "GPT-5.6 Luna"),
        ("gpt-5.6", "GPT-5.6"),
        ("gpt-5.4", "GPT-5.4"),
        ("gpt-5.4-mini", "GPT-5.4 Mini"),
        ("gpt-5.2", "GPT-5.2"),
        ("gpt-4.1", "GPT-4.1"),
    ],
    "anthropic": [
        ("claude-fable-5", "Claude Fable 5"),
        ("claude-opus-4-8", "Claude Opus 4.8"),
        ("claude-sonnet-5", "Claude Sonnet 5"),
        ("claude-opus-4-7", "Claude Opus 4.7"),
        ("claude-haiku-4-5", "Claude Haiku 4.5"),
        ("claude-sonnet-4-6", "Claude Sonnet 4.6"),
    ],
    "gemini": [
        ("gemini-3.8-flash", "Gemini 3.8 Flash"),
        ("gemini-3.5-flash", "Gemini 3.5 Flash"),
        ("gemini-3.1-pro-preview", "Gemini 3.1 Pro (preview)"),
        ("gemini-3-flash-preview", "Gemini 3 Flash (preview)"),
        ("gemini-2.5-pro", "Gemini 2.5 Pro"),
        ("gemini-2.5-flash", "Gemini 2.5 Flash"),
        ("gemini-2.5-flash-lite", "Gemini 2.5 Flash Lite"),
    ],
    "groq": [
        ("meta-llama/llama-4-maverick-17b-128e-instruct", "Llama 4 Maverick"),
        ("meta-llama/llama-4-scout-17b-16e-instruct", "Llama 4 Scout"),
        ("openai/gpt-oss-120b", "GPT-OSS 120B"),
        ("qwen/qwen3-32b", "Qwen3 32B"),
        ("moonshotai/kimi-k2-instruct-0905", "Kimi K2"),
        ("llama-3.3-70b-versatile", "Llama 3.3 70B"),
    ],
    "ollama": [
        ("llama3.3", "Llama 3.3"),
        ("qwen3", "Qwen 3"),
        ("deepseek-r1", "DeepSeek R1"),
        ("gpt-oss", "GPT-OSS"),
        ("gemma3", "Gemma 3"),
        ("mistral", "Mistral"),
    ],
}

_TEMPLATES_DIR = Path(__file__).parent / "templates" / "json_crew"


# ── Common tools for picker ────────────────────────────────────

_TOOL_CATEGORIES: list[tuple[str, list[tuple[str, str]]]] = [
    (
        "CrewAI Platform",
        [
            (
                f"platform:{application.slug}",
                f"{application.display_name} Integration",
            )
            for application in PLATFORM_APPLICATION_CATALOG
        ],
    ),
    (
        "Search & Research",
        [
            ("SerperDevTool", "Google search via Serper API"),
            ("BraveSearchTool", "Web search via Brave Search"),
            ("BraveWebSearchTool", "Focused Brave web search"),
            ("BraveNewsSearchTool", "Search current news with Brave"),
            ("BraveImageSearchTool", "Search images with Brave"),
            ("BraveVideoSearchTool", "Search videos with Brave"),
            ("BraveLocalPOIsTool", "Find local places with Brave"),
            ("BraveLocalPOIsDescriptionTool", "Describe local places with Brave"),
            ("BraveLLMContextTool", "Fetch Brave search context"),
            ("TavilySearchTool", "Web search via Tavily"),
            ("TavilyResearchTool", "Run Tavily research"),
            ("TavilyGetResearchTool", "Retrieve Tavily research results"),
            ("TavilyExtractorTool", "Extract content with Tavily"),
            ("EXASearchTool", "Semantic web search via Exa"),
            ("ExaSearchTool", "Semantic web search via Exa"),
            ("LinkupSearchTool", "Web search via Linkup"),
            ("SerpApiGoogleSearchTool", "Google search via SerpApi"),
            ("SerpApiGoogleShoppingTool", "Google Shopping via SerpApi"),
            ("SerplyWebSearchTool", "Web search via Serply"),
            ("SerplyNewsSearchTool", "News search via Serply"),
            ("SerplyScholarSearchTool", "Scholar search via Serply"),
            ("SerplyJobSearchTool", "Job search via Serply"),
            ("SerplyWebpageToMarkdownTool", "Convert webpages with Serply"),
            ("ParallelSearchTool", "Run parallel web searches"),
            ("BrightDataSearchTool", "Search with Bright Data"),
            ("GithubSearchTool", "Search GitHub repositories"),
            ("ArxivPaperTool", "Search arXiv academic papers"),
        ],
    ),
    (
        "Web Scraping",
        [
            ("ScrapeWebsiteTool", "Extract content from a URL"),
            ("ScrapeElementFromWebsiteTool", "Extract page elements from a URL"),
            ("FirecrawlScrapeWebsiteTool", "Scrape with Firecrawl"),
            ("FirecrawlCrawlWebsiteTool", "Crawl a website with Firecrawl"),
            ("FirecrawlSearchTool", "Search with Firecrawl"),
            ("SeleniumScrapingTool", "Browser-based scraping"),
            ("JinaScrapeWebsiteTool", "Scrape with Jina"),
            ("ScrapegraphScrapeTool", "AI-powered page scraping"),
            ("SerperScrapeWebsiteTool", "Scrape pages with Serper"),
            ("BrowserbaseLoadTool", "Load web pages with Browserbase"),
            ("HyperbrowserLoadTool", "Load web pages with Hyperbrowser"),
            ("MultiOnTool", "Control web workflows with MultiOn"),
            ("SpiderTool", "Crawl websites with Spider"),
            ("StagehandTool", "Browser automation with Stagehand"),
            ("BrightDataWebUnlockerTool", "Unlock websites with Bright Data"),
            ("BrightDataDatasetTool", "Fetch Bright Data datasets"),
            ("WebsiteSearchTool", "RAG search on a website"),
        ],
    ),
    (
        "File & Document",
        [
            ("DirectoryReadTool", "List directory contents"),
            ("DirectorySearchTool", "Search directory contents"),
            ("FileReadTool", "Read local files"),
            ("FileWriterTool", "Write to local files"),
            ("FileCompressorTool", "Compress local files"),
            ("CSVSearchTool", "Search within CSV files"),
            ("PDFSearchTool", "Search within PDF files"),
            ("DOCXSearchTool", "Search within DOCX files"),
            ("MDXSearchTool", "Search within MDX files"),
            ("JSONSearchTool", "Search within JSON files"),
            ("TXTSearchTool", "Search within text files"),
            ("XMLSearchTool", "Search within XML files"),
            ("OCRTool", "Extract text with OCR"),
            ("YoutubeVideoSearchTool", "Search within YouTube videos"),
            ("YoutubeChannelSearchTool", "Search within YouTube channels"),
        ],
    ),
    (
        "Code & Data",
        [
            ("CodeDocsSearchTool", "Search code documentation"),
            ("RagTool", "RAG over custom data sources"),
            ("NL2SQLTool", "Natural language to SQL queries"),
            ("DatabricksQueryTool", "Query Databricks data"),
            ("SingleStoreSearchTool", "Search SingleStore data"),
        ],
    ),
    (
        "Cloud & Storage",
        [
            ("S3ReaderTool", "Read objects from Amazon S3"),
            ("S3WriterTool", "Write objects to Amazon S3"),
            ("BedrockInvokeAgentTool", "Invoke an Amazon Bedrock agent"),
            ("BedrockKBRetrieverTool", "Retrieve from Bedrock knowledge bases"),
        ],
    ),
    (
        "Sandbox & Automation",
        [
            ("E2BExecTool", "Run commands in E2B"),
            ("E2BFileTool", "Manage files in E2B"),
            ("E2BPythonTool", "Run Python in E2B"),
            ("DaytonaExecTool", "Run commands in Daytona"),
            ("DaytonaFileTool", "Manage files in Daytona"),
            ("DaytonaPythonTool", "Run Python in Daytona"),
            ("GenerateCrewaiAutomationTool", "Generate CrewAI automations"),
        ],
    ),
    (
        "AI & Vision",
        [
            ("DallETool", "Generate images with DALL-E"),
            ("VisionTool", "Analyze images with vision models"),
            ("AIMindTool", "Connect to MindStudio agents"),
            ("PatronusEvalTool", "Evaluate output with Patronus"),
            ("PatronusLocalEvaluatorTool", "Run local Patronus evaluations"),
        ],
    ),
]

_FLAT_TOOLS: list[tuple[str, str]] = [
    tool for _cat, tools in _TOOL_CATEGORIES for tool in tools
]

_COMMON_TOOL_ORDER = [
    "SerperDevTool",
    "ScrapeWebsiteTool",
    "FileReadTool",
    "platform:gmail",
    "platform:whatsapp",
]

_ANSI_SEQUENCE_RE = re.compile(r"\x1b\[[0-?]*[ -/]*[@-~]")


# ── Interactive wizard ─────────────────────────────────────────


def _prompt_text(
    label: str,
    default: str = "",
    *,
    spacing_before: bool = True,
) -> str:
    if spacing_before:
        click.echo()

    prompt = click.style(f"  {label}", fg="cyan")
    if default:
        prompt += f" [{default}]"
    prompt += click.style(" > ", fg="bright_white")

    try:
        value = input(_readline_safe_prompt(prompt))
    except (KeyboardInterrupt, EOFError):
        raise click.Abort() from None

    if not value and default:
        value = default
    return value.strip()


def _readline_safe_prompt(prompt: str) -> str:
    if not sys.stdin.isatty():
        return prompt

    try:
        import readline  # noqa: F401
    except ImportError:
        return prompt

    return _ANSI_SEQUENCE_RE.sub(lambda match: f"\001{match.group(0)}\002", prompt)


def _confirm(label: str, default: bool = False) -> bool:
    click.echo()
    return click.confirm(
        click.style(f"  {label}", fg="cyan"),
        default=default,
        prompt_suffix=click.style(" > ", fg="bright_white"),
    )


def _success(message: str, *, bold: bool = False, dim: bool = False) -> None:
    click.echo()
    click.secho(f"  ✔ {message}", fg="green", bold=bold, dim=dim)


def _highlight_placeholders(text: str) -> Text:
    highlighted = Text(text, style="dim")
    highlighted.highlight_regex(r"\{[A-Za-z_][A-Za-z0-9_]*\}", style="bold cyan")
    return highlighted


def _show_interpolation_hint(kind: str) -> None:
    console = Console()
    console.print(
        _highlight_placeholders(
            "  Tip: Use {placeholder} for dynamic values you want to change later."
        )
    )


def _tool_label(name: str, description: str) -> str:
    if name.startswith("platform:"):
        app_name = description.removesuffix(" Integration").replace(" ", "")
        tool_count = PLATFORM_APP_TOOL_COUNTS.get(name.removeprefix("platform:"), 0)
        return (
            f"{description:<48s} Platform: {app_name.replace(' ', '')}Integration "
            f"({tool_count} tools)"
        )
    return f"{description:<48s} {name}"


def _tool_category_label(category: str) -> str:
    return f"── {category} ──"


def _category_row_label(
    category: str,
    tools: list[tuple[str, str]],
    selected: set[str],
    expanded: bool,
    *,
    item_label: str | None = None,
) -> str:
    """Render an accordion category row with tool/selection counts."""
    marker = "▾" if expanded else "▸"
    sel_count = sum(1 for name, _desc in tools if name in selected)
    item_label = item_label or (
        "applications" if category == "CrewAI Platform" else "tools"
    )
    suffix = f"{len(tools)} {item_label}"
    if sel_count:
        suffix += f", {sel_count} selected"
    return f"{marker} {category}  ({suffix})"


def _select_tools() -> list[str]:
    """Accordion tool picker.

    Common tools are always visible at the top; every other category shows
    as a single expandable row. Expanding one category collapses the others.
    Selections persist while expanding/collapsing.
    """
    tools_by_name = {name: desc for name, desc in _FLAT_TOOLS}
    common_tools = [
        (name, tools_by_name[name])
        for name in _COMMON_TOOL_ORDER
        if name in tools_by_name
    ]
    common_tool_names = {name for name, _desc in common_tools}

    platform_tools = next(
        tools for category, tools in _TOOL_CATEGORIES if category == "CrewAI Platform"
    )
    platform_groups = [
        (
            category.name,
            [
                (
                    f"platform:{app}",
                    f"{PLATFORM_APP_DISPLAY_NAMES[app]} Integration",
                )
                for app in category.apps
            ],
        )
        for category in PLATFORM_APP_CATEGORIES
    ]
    categories: list[tuple[str, list[tuple[str, str]]]] = []
    for category, category_tools in _TOOL_CATEGORIES:
        if category == "CrewAI Platform":
            continue
        remaining_tools = [
            (name, desc)
            for name, desc in category_tools
            if name not in common_tool_names
        ]
        if remaining_tools:
            categories.append((category, remaining_tools))

    selected: set[str] = set()
    expanded: str | None = None
    expanded_platform_group: str | None = None
    focus_category: str | None = None
    first_render = True

    while True:
        labels: list[str] = []
        tool_by_index: dict[int, str] = {}
        separator_indices: set[int] = set()
        action_indices: set[int] = set()
        action_by_index: dict[int, tuple[str, str]] = {}
        preselected: set[int] = set()
        initial_cursor: int | None = None

        separator_indices.add(len(labels))
        labels.append(_tool_category_label("Common tools"))
        for name, desc in common_tools:
            if name in selected:
                preselected.add(len(labels))
            tool_by_index[len(labels)] = name
            labels.append(_tool_label(name, desc))

        platform_row = len(labels)
        action_indices.add(platform_row)
        action_by_index[platform_row] = ("category", "CrewAI Platform")
        if focus_category == "CrewAI Platform":
            initial_cursor = platform_row
        labels.append(
            _category_row_label(
                "CrewAI Platform",
                platform_tools,
                selected,
                expanded == "CrewAI Platform",
            )
        )
        if expanded == "CrewAI Platform":
            for group, apps in platform_groups:
                row = len(labels)
                action_indices.add(row)
                action_by_index[row] = ("platform_group", group)
                is_expanded = group == expanded_platform_group
                if group == focus_category:
                    initial_cursor = row
                labels.append(
                    "  "
                    + _category_row_label(
                        group,
                        apps,
                        selected,
                        is_expanded,
                        item_label="applications",
                    )
                )
                if is_expanded:
                    for name, description in apps:
                        if name in selected:
                            preselected.add(len(labels))
                        tool_by_index[len(labels)] = name
                        labels.append("    " + _tool_label(name, description))

        for category, category_tools in categories:
            row = len(labels)
            action_indices.add(row)
            action_by_index[row] = ("category", category)
            is_expanded = category == expanded
            if category == focus_category:
                initial_cursor = row
            labels.append(
                _category_row_label(category, category_tools, selected, is_expanded)
            )
            if is_expanded:
                for name, desc in category_tools:
                    if name in selected:
                        preselected.add(len(labels))
                    tool_by_index[len(labels)] = name
                    labels.append(_tool_label(name, desc))

        indices, action = pick_many(
            "Tools (space to toggle, enter to confirm):" if first_render else "",
            labels,
            action_indices=action_indices,
            separator_indices=separator_indices,
            preselected=preselected,
            initial_cursor=initial_cursor,
        )
        first_render = False

        # Carry over toggles made on this screen; tools not visible in this
        # render keep their previous state.
        visible = set(tool_by_index.values())
        chosen = {tool_by_index[i] for i in indices if i in tool_by_index}
        selected = (selected - visible) | chosen

        if action is None:
            break
        action_type, category = action_by_index[action]
        if action_type == "platform_group":
            focus_category = category
            expanded_platform_group = (
                None if category == expanded_platform_group else category
            )
            continue

        focus_category = category
        if category == "CrewAI Platform":
            expanded = None if category == expanded else category
            expanded_platform_group = None
        else:
            expanded = None if category == expanded else category
            expanded_platform_group = None

    ordered = (
        [name for name, _desc in common_tools]
        + [
            f"platform:{app}"
            for category in PLATFORM_APP_CATEGORIES
            for app in category.apps
            if f"platform:{app}" not in common_tool_names
        ]
        + [name for _cat, cat_tools in categories for name, _desc in cat_tools]
    )
    return [name for name in ordered if name in selected]


def _select_platform_actions(selected_tools: list[str]) -> list[str]:
    """Replace selected Platform applications with the chosen actions for an agent."""
    platform_apps = [
        tool.removeprefix("platform:")
        for tool in selected_tools
        if tool.startswith("platform:") and "/" not in tool
    ]
    non_platform_tools = [
        tool for tool in selected_tools if not tool.startswith("platform:")
    ]
    platform_actions = [
        tool for tool in selected_tools if tool.startswith("platform:") and "/" in tool
    ]

    for app in platform_apps:
        app_tools = PLATFORM_APP_TOOLS[app]
        default_slugs = set(PLATFORM_APP_DEFAULT_TOOLS[app])
        action_indices = pick_many(
            f"{PLATFORM_APP_DISPLAY_NAMES[app]} actions for this agent (space to toggle):",
            [f"{item.display_name} ({item.slug})" for item in app_tools],
            searchable=True,
            preselected={
                index
                for index, item in enumerate(app_tools)
                if item.slug in default_slugs
            },
        )
        platform_actions.extend(
            f"platform:{app}/{app_tools[index].slug}" for index in action_indices
        )

    return non_platform_tools + platform_actions


def _wizard_agent(
    agent_num: int,
    existing_names: list[str],
    skip_provider: bool = False,
    last_llm: str | None = None,
    preset_llm: str | None = None,
) -> dict[str, Any] | None:
    """Interactive wizard for one agent. Returns agent dict or None if skipped."""
    click.echo()
    click.secho(f"  Agent {agent_num}", fg="cyan", bold=True)

    role = _prompt_text("Role", spacing_before=False)
    if not role:
        return None

    name_default = role.lower().replace(" ", "_")[:30]
    name_default = re.sub(r"[^a-z0-9_]", "", name_default)
    if not name_default:
        # Roles made only of symbols would otherwise produce an empty slug
        # and an invalid agents/.jsonc file name.
        name_default = f"agent_{agent_num}"
    while name_default in existing_names:
        name_default += "_2"

    goal = _prompt_text("Goal", spacing_before=False)

    backstory = _prompt_text("Backstory", spacing_before=False)

    # LLM model
    if preset_llm:
        llm = preset_llm
        _success(llm)
    elif skip_provider:
        llm = last_llm or "openai/gpt-4o"
    elif last_llm:
        reuse_labels = [
            f"Same as before  ({last_llm})",
            "Choose a different model",
        ]
        r_idx = pick_one("LLM:", reuse_labels)
        if r_idx == 1:
            llm = _select_model()
        else:
            llm = last_llm
        _success(llm)
    else:
        llm = _select_model()

    tools = _select_platform_actions(_select_tools())
    if tools:
        _success(f"{len(tools)} tool{'s' if len(tools) != 1 else ''}")
    else:
        _success("No tools", dim=True)

    # Planning
    planning = _confirm("Enable step-by-step planning?", default=False)

    # Allow delegation
    allow_delegation = _confirm("Allow delegation to other agents?", default=False)

    return {
        "name": name_default,
        "role": role,
        "goal": goal,
        "backstory": backstory,
        "llm": llm,
        "tools": tools,
        "planning": planning,
        "allow_delegation": allow_delegation,
    }


def _wizard_task(
    task_num: int,
    agent_names: list[str],
    prior_task_names: list[str],
) -> dict[str, Any] | None:
    """Interactive wizard for one task. Returns task dict or None if skipped."""
    click.echo()
    click.secho(f"  Task {task_num}", fg="cyan", bold=True)

    description = _prompt_text("Description", spacing_before=False)
    if not description:
        return None

    # Auto-generate name from first few words of description
    words = description.lower().split()[:4]
    base = re.sub(r"[^a-z0-9_]", "", "_".join(words))
    name = f"{base}_task" if base else f"task_{task_num}"
    while name in prior_task_names:
        name += "_2"

    expected_output = _prompt_text("Expected output", spacing_before=False)

    # Agent assignment
    if len(agent_names) == 1:
        assigned_agent = agent_names[0]
    else:
        a_idx = pick_one("Assign to agent:", agent_names)
        while a_idx < 0:
            click.secho("  Every task needs an agent — pick one to continue.", dim=True)
            a_idx = pick_one("Assign to agent:", agent_names)
        assigned_agent = agent_names[a_idx]
        _success(f"Agent: {assigned_agent}")

    # Context dependencies
    context: list[str] = []
    if prior_task_names:
        ctx_indices = pick_many(
            "Context from prior tasks (space to toggle):",
            [*prior_task_names, "None"],
        )
        context = [
            prior_task_names[i] for i in ctx_indices if i < len(prior_task_names)
        ]
        if context:
            _success(f"Context: {', '.join(context)}")

    return {
        "name": name,
        "description": description,
        "expected_output": expected_output,
        "agent": assigned_agent,
        "context": context,
    }


def _wizard_agents_and_tasks(
    skip_provider: bool = False,
    default_llm: str | None = None,
) -> tuple[list[dict[str, Any]], list[dict[str, Any]], dict[str, Any]]:
    """Run the full interactive wizard. Returns (agents, tasks, crew_settings)."""
    agents: list[dict[str, Any]] = []
    tasks: list[dict[str, Any]] = []

    # ── Step 1: Agents ──
    click.echo()
    click.secho("  Step 1/3 — Agents", fg="cyan", bold=True)
    click.secho("  Define the AI agents in your crew.", dim=True)
    _show_interpolation_hint("agents")

    while True:
        last_llm = agents[-1]["llm"] if agents else None
        agent = _wizard_agent(
            agent_num=len(agents) + 1,
            existing_names=[a["name"] for a in agents],
            skip_provider=skip_provider,
            last_llm=last_llm,
            preset_llm=default_llm if not agents else None,
        )
        if agent is None and not agents:
            click.secho("  Need at least one agent.", fg="yellow")
            continue
        if agent is not None:
            agents.append(agent)
            _success(f"{agent['role']} added", bold=True)

        if not _confirm("Add another agent?", default=False):
            break

    # ── Step 2: Tasks ──
    click.echo()
    click.secho("  Step 2/3 — Tasks", fg="cyan", bold=True)
    click.secho("  Define what your agents should do.", dim=True)
    _show_interpolation_hint("tasks")

    agent_names = [a["name"] for a in agents]
    task_names: list[str] = []

    while True:
        task = _wizard_task(
            task_num=len(tasks) + 1,
            agent_names=agent_names,
            prior_task_names=task_names,
        )
        if task is None and not tasks:
            click.secho("  Need at least one task.", fg="yellow")
            continue
        if task is not None:
            tasks.append(task)
            task_names.append(task["name"])
            _success(f"Task {len(tasks)} added", bold=True)

        if not _confirm("Add another task?", default=False):
            break

    # ── Step 3: Settings ──
    click.echo()
    click.secho("  Step 3/3 — Settings", fg="cyan", bold=True)

    process = "sequential"
    memory = _confirm("Enable crew memory?", default=True)

    crew_settings = {
        "process": process,
        "memory": memory,
        "inputs": {},
    }

    return agents, tasks, crew_settings


def _default_agents_and_tasks(
    default_llm: str | None = None,
) -> tuple[list[dict[str, Any]], list[dict[str, Any]], dict[str, Any]]:
    """Return deterministic scaffold data for non-interactive project creation."""
    llm = default_llm or "openai/gpt-4o"
    agents = [
        {
            "name": "researcher",
            "role": "Senior Researcher",
            "goal": "Research the requested topic and identify useful findings.",
            "backstory": (
                "You are an experienced researcher who finds relevant information "
                "and presents it clearly."
            ),
            "llm": llm,
            "tools": [],
            "planning": False,
            "allow_delegation": False,
        }
    ]
    tasks = [
        {
            "name": "research_task",
            "description": "Research current AI trends and write a concise summary.",
            "expected_output": "A concise markdown report with key findings.",
            "agent": "researcher",
            "context": [],
        }
    ]
    crew_settings = {
        "process": "sequential",
        "memory": True,
        "inputs": {},
    }
    return agents, tasks, crew_settings


# ── JSONC generation from wizard data ──────────────────────────


def _agent_to_jsonc(agent: dict[str, Any]) -> str:
    """Convert agent wizard data to JSONC string with comments."""
    has_planning = agent["planning"]
    settings_block = _render_json_crew_template(
        "agent_settings.jsonc",
        {
            "allow_delegation": "true" if agent["allow_delegation"] else "false",
            "delegation_comma": "," if has_planning else "",
            "planning_line": '"planning": true'
            if has_planning
            else '// "planning": false',
        },
    )

    return _render_json_crew_template(
        "agent.jsonc",
        {
            "role_json": json.dumps(agent["role"]),
            "goal_json": json.dumps(agent["goal"]),
            "backstory_json": json.dumps(agent["backstory"]),
            "llm_json": json.dumps(agent["llm"]),
            "tools_json": json.dumps(agent["tools"]),
            "settings_block": settings_block,
        },
    )


def _task_to_json_fragment(task: dict[str, Any]) -> str:
    """Convert task wizard data to a JSON-like fragment for embedding in crew JSONC."""
    has_context = bool(task.get("context"))
    has_output_file = bool(task.get("output_file"))
    context_block = ""
    output_file_block = ""

    if has_context:
        context_block = (
            "\n\n"
            "      // Task outputs used as context\n"
            f'      "context": {json.dumps(task["context"])}'
            f"{',' if has_output_file else ''}"
        )

    if has_output_file:
        output_file_block = (
            "\n\n"
            "      // Save output to a file\n"
            f'      "output_file": {json.dumps(task["output_file"])}'
        )

    return _render_json_crew_template(
        "task.jsonc",
        {
            "name_json": json.dumps(task["name"]),
            "description_json": json.dumps(task["description"]),
            "expected_output_json": json.dumps(task["expected_output"]),
            "agent_json": json.dumps(task["agent"]),
            "agent_comma": "," if has_context or has_output_file else "",
            "context_block": context_block,
            "output_file_block": output_file_block,
        },
    )


def _crew_to_jsonc(
    name: str,
    agents: list[dict[str, Any]],
    tasks: list[dict[str, Any]],
    settings: dict[str, Any],
) -> str:
    """Generate the full crew.jsonc from wizard data."""
    agent_names_json = json.dumps([a["name"] for a in agents])
    tasks_fragments = ",\n".join(_task_to_json_fragment(t) for t in tasks)
    inputs_json = json.dumps(settings.get("inputs", {}), indent=4)
    # Re-indent inputs to 4-space
    inputs_lines = inputs_json.split("\n")
    if len(inputs_lines) > 1:
        inputs_json = (
            inputs_lines[0] + "\n" + "\n".join("  " + line for line in inputs_lines[1:])
        )

    memory = "true" if settings.get("memory") else "false"

    return _render_json_crew_template(
        "crew.jsonc",
        {
            "name_json": json.dumps(name),
            "agent_names_json": agent_names_json,
            "tasks_fragments": tasks_fragments,
            "process_json": json.dumps(settings.get("process", "sequential")),
            "memory": memory,
            "manager_agent_name": agents[0]["name"],
            "inputs_json": inputs_json,
        },
    )


# ── Model selection ─────────────────────────────────────────────


def _select_model() -> str:
    """Two-step arrow-key selection: provider, then model."""
    provider_labels = [label for _, label in _PROVIDERS]
    provider_labels.append("Other (enter manually)")

    p_idx = pick_one("LLM Provider:", provider_labels)
    if p_idx < 0:
        return "openai/gpt-4o"

    if p_idx == len(_PROVIDERS):
        custom: str = click.prompt(
            click.style("  Enter model (provider/model)", fg="cyan"),
            type=str,
            prompt_suffix=click.style(" > ", fg="bright_white"),
        )
        return custom.strip()

    provider_key, provider_name = _PROVIDERS[p_idx]
    click.secho(f"  → {provider_name}", fg="green")

    # Prefer the latest models pulled live from the vendor / LiteLLM; the
    # curated ``_PROVIDER_MODELS`` entry is the offline fallback and label source.
    models = get_provider_models(provider_key, _PROVIDER_MODELS.get(provider_key, []))
    if not models:
        custom = click.prompt(
            click.style(f"  Enter model name for {provider_key}/", fg="cyan"),
            type=str,
            prompt_suffix=click.style(" > ", fg="bright_white"),
        )
        return f"{provider_key}/{custom.strip()}"

    model_labels = [f"{label}  ({model_id})" for model_id, label in models]
    model_labels.append("Other (enter model name)")

    m_idx = pick_one(f"{provider_name} Model:", model_labels)
    if m_idx < 0:
        return f"{provider_key}/{models[0][0]}"

    if m_idx == len(models):
        custom = click.prompt(
            click.style(f"  Enter model name for {provider_key}/", fg="cyan"),
            type=str,
            prompt_suffix=click.style(" > ", fg="bright_white"),
        )
        result = f"{provider_key}/{custom.strip()}"
    else:
        model_id = models[m_idx][0]
        result = f"{provider_key}/{model_id}"

    click.secho(f"  → {result}", fg="green")
    return result


def _default_model_for_provider(provider: str | None) -> str | None:
    """Return the default provider/model string for a ``--provider`` value."""
    if not provider:
        return None
    normalized = provider.strip().lower()
    if not normalized:
        return None
    if "/" in normalized:
        return normalized
    models = _PROVIDER_MODELS.get(normalized)
    if not models:
        return None
    return f"{normalized}/{models[0][0]}"


# ── Helpers ─────────────────────────────────────────────────────


def _render_json_crew_template(
    template_name: str, replacements: dict[str, str] | None = None
) -> str:
    return render_template(_TEMPLATES_DIR / template_name, replacements or {})


def _write_jsonc(path: Path, content: str) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_text(content, encoding="utf-8")


def _setup_env(folder_path: Path, llm_model: str) -> None:
    """Prompt for API keys based on the selected provider."""
    click.echo()
    env_vars = load_env_vars(folder_path)
    env_vars["MODEL"] = llm_model

    provider = llm_model.split("/")[0] if "/" in llm_model else llm_model
    if provider in ENV_VARS:
        for details in ENV_VARS[provider]:
            if details.get("default", False):
                for key, value in details.items():
                    if key not in ["prompt", "key_name", "default"]:
                        env_vars[key] = value
            elif "key_name" in details:
                api_key_value = click.prompt(
                    click.style(f"  {details['prompt']}", fg="cyan"),
                    default="",
                    show_default=False,
                    prompt_suffix=click.style(" > ", fg="bright_white"),
                )
                if api_key_value.strip():
                    env_vars[details["key_name"]] = api_key_value

    if env_vars:
        write_env_file(folder_path, env_vars)
        click.secho("  API keys and model saved to .env file", fg="green")


def _platform_apps_from_agents(agents: list[dict[str, Any]]) -> list[str]:
    """Return unique platform application slugs selected across all agents."""
    apps: list[str] = []
    for agent in agents:
        for tool in agent.get("tools", []):
            if isinstance(tool, str) and tool.startswith("platform:"):
                app = tool.removeprefix("platform:").split("/", maxsplit=1)[0]
                if app and app not in apps:
                    apps.append(app)
    return apps


def _platform_app_name(app: str) -> str:
    """Return the display name for a platform application slug."""
    return PLATFORM_APP_DISPLAY_NAMES.get(app, app.replace("_", " ").title())


def _prompt_platform_token() -> str:
    """Explain how to prompt for an AMP Enterprise Action Auth Token."""
    click.secho(
        "  To use CrewAI Platform tools, you need an Enterprise Action Auth Token.",
        fg="yellow",
    )
    click.secho(
        "  Get your token from CrewAI AMP: https://app.crewai.com "
        "→ Settings → Account → Enterprise Action Auth Token.",
        fg="cyan",
    )
    return str(
        click.prompt(
            click.style("  CREWAI_ENTERPRISE_ACTION_AUTH_TOKEN", fg="cyan"),
            hide_input=False,
            prompt_suffix=click.style(" > ", fg="bright_white"),
        )
    ).strip()


def _check_platform_app(
    app: str, application_selector: Any, client_for_selector: Any
) -> tuple[Any | None, Exception | None]:
    """Check one AMP application with the synchronous platform client."""
    try:
        selector = application_selector.from_string(app)
        return client_for_selector(selector).get_actions([selector]), None
    except Exception as error:
        return None, error


async def _check_platform_apps_concurrently(
    apps: list[str], application_selector: Any, client_for_selector: Any
) -> list[tuple[Any | None, Exception | None]]:
    """Run independent AMP application checks concurrently."""
    return await asyncio.gather(
        *(
            asyncio.to_thread(
                _check_platform_app, app, application_selector, client_for_selector
            )
            for app in apps
        )
    )


def _report_platform_app_validation(
    app: str,
    actions: Any | None,
    error: Exception | None,
    failed: list[str],
) -> bool:
    """Print one AMP application validation result and return token validity."""
    app_name = _platform_app_name(app)
    if error is not None:
        status_code = getattr(getattr(error, "response", None), "status_code", None)
        if status_code in {401, 403}:
            click.secho(
                "  ✘ Enterprise Action Auth Token is invalid or expired",
                fg="red",
            )
            return True
        click.secho(
            f"  ✘ {app_name} integration could not be validated: {error}",
            fg="red",
        )
        failed.append(app)
        return False

    if not actions:
        click.secho(
            f"  ✘ {app_name} integration is not connected on CrewAI Platform",
            fg="red",
        )
        failed.append(app)
    else:
        click.secho(
            f"  ✔ {app_name} integration is connected on CrewAI Platform",
            fg="green",
        )
    return False


def _validate_platform_apps(
    apps: list[str], application_selector: Any, client_for_selector: Any
) -> tuple[list[str], bool]:
    """Check selected AMP applications concurrently when no event loop is running."""
    failed: list[str] = []
    try:
        asyncio.get_running_loop()
    except RuntimeError:
        app_names = [_platform_app_name(app) for app in apps]
        click.echo()
        click.secho(
            "  Checking "
            f"{', '.join(app_names)} integration{'s' if len(app_names) != 1 else ''} "
            "together on AMP...",
            fg="cyan",
        )
        results = asyncio.run(
            _check_platform_apps_concurrently(
                apps, application_selector, client_for_selector
            )
        )
        for app, (actions, error) in zip(apps, results, strict=True):
            if _report_platform_app_validation(app, actions, error, failed):
                return failed, True
    else:
        for app in apps:
            app_name = _platform_app_name(app)
            click.echo()
            click.secho(
                "  Checking Enterprise Action Auth Token and "
                f"{app_name} integration on AMP...",
                fg="cyan",
            )
            actions, error = _check_platform_app(
                app, application_selector, client_for_selector
            )
            if _report_platform_app_validation(app, actions, error, failed):
                return failed, True
    return failed, False


def _show_platform_validation_guidance(
    failed_apps: list[str], token_invalid: bool
) -> None:
    """Tell the user what to fix before revalidating AMP integrations."""
    click.echo()
    if token_invalid:
        click.secho(
            "  Check your Enterprise Action Auth Token in AMP.",
            fg="yellow",
        )
        return

    failed_app_names = [_platform_app_name(app) for app in failed_apps]
    click.secho(
        "  Check the "
        f"{', '.join(failed_app_names)} integration"
        f"{'s' if len(failed_app_names) != 1 else ''} and your CrewAI "
        "Enterprise Action Auth Token in AMP.",
        fg="yellow",
    )


def _prompt_platform_revalidation_token() -> str:
    """Prompt for an optional replacement token before the next validation pass."""
    click.echo()
    return str(
        click.prompt(
            click.style(
                "  Press Enter to revalidate, or enter a replacement token",
                fg="cyan",
            ),
            default="",
            show_default=False,
            hide_input=False,
            prompt_suffix=click.style(" > ", fg="bright_white"),
        )
    ).strip()


def _setup_platform_auth(agents: list[dict[str, Any]]) -> str | None:
    """Get and validate AMP authentication for selected platform applications."""
    apps = _platform_apps_from_agents(agents)
    if not apps:
        return None

    click.echo()
    try:
        # Importing crewai_tools currently initializes optional tool SDKs. Keep
        # their import-time warnings out of the interactive token setup flow.
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            from crewai_tools.tools.crewai_platform_tools.integrations_client import (
                ApplicationSelector,
                client_for_selector,
            )
    except ImportError as error:
        raise click.ClickException(
            "Platform tools require the 'crewai-tools' package. "
            "Install it with `uv add crewai-tools` or "
            "`pip install 'crewai[tools]'`."
        ) from error

    click.secho(
        "  Checking for CREWAI_ENTERPRISE_ACTION_AUTH_TOKEN...",
        fg="cyan",
    )
    token = os.environ.get("CREWAI_ENTERPRISE_ACTION_AUTH_TOKEN", "")
    credential_location: str | None = None
    if token:
        credential_location = "CREWAI_ENTERPRISE_ACTION_AUTH_TOKEN environment variable"

    while True:
        if not token:
            click.secho(
                "  No CREWAI_ENTERPRISE_ACTION_AUTH_TOKEN found; prompting for one.",
                fg="yellow",
            )
            token = _prompt_platform_token()
            if token:
                credential_location = "interactive prompt"
        if not token:
            click.secho(
                "  An Enterprise Action Auth Token is required to validate "
                "the selected integrations.",
                fg="yellow",
            )
            continue

        _success(
            f"Enterprise Action Auth Token found via {credential_location}", dim=True
        )
        os.environ["CREWAI_ENTERPRISE_ACTION_AUTH_TOKEN"] = token
        failed_apps, token_invalid = _validate_platform_apps(
            apps, ApplicationSelector, client_for_selector
        )
        if not failed_apps and not token_invalid:
            _success("Enterprise Action Auth Token set", bold=True)
            _success(
                "CrewAI Platform integrations connected: "
                f"{', '.join(_platform_app_name(app) for app in apps)}"
            )
            return token

        _show_platform_validation_guidance(failed_apps, token_invalid)
        replacement_token = _prompt_platform_revalidation_token()
        if replacement_token:
            token = replacement_token
            credential_location = "interactive replacement prompt"
            os.environ["CREWAI_ENTERPRISE_ACTION_AUTH_TOKEN"] = token


# ── Main ────────────────────────────────────────────────────────


def create_json_crew(
    name: str,
    provider: str | None = None,
    skip_provider: bool = False,
) -> None:
    """Scaffold a new JSON-first crew project."""
    import keyword
    import shutil

    dmn_mode = is_dmn_mode_enabled()
    if not dmn_mode:
        enable_prompt_line_editing()

    name = name.rstrip("/")
    if not name.strip():
        raise ValueError("Project name cannot be empty")

    folder_name = name.replace(" ", "_").replace("-", "_").lower()
    folder_name = re.sub(r"[^a-zA-Z0-9_]", "", folder_name)

    if not folder_name or folder_name[0].isdigit():
        raise ValueError(
            f"Project name '{name}' produces invalid folder name '{folder_name}'"
        )

    if keyword.iskeyword(folder_name):
        raise ValueError(f"'{folder_name}' is a reserved Python keyword")

    folder_path = Path(folder_name)
    if folder_path.exists():
        if dmn_mode:
            raise click.ClickException(f"Folder {folder_name} already exists.")
        if not click.confirm(f"Folder {folder_name} already exists. Override?"):
            click.secho("Cancelled.", fg="yellow")
            sys.exit(0)
        shutil.rmtree(folder_path)

    click.echo()
    click.secho(f"  Creating crew: {name}", fg="green", bold=True)

    default_llm = _default_model_for_provider(provider)
    if dmn_mode:
        agents, tasks, crew_settings = _default_agents_and_tasks(default_llm)
    else:
        agents, tasks, crew_settings = _wizard_agents_and_tasks(
            skip_provider=skip_provider,
            default_llm=default_llm,
        )

    # Authenticate only after the full wizard is complete, but before any
    # project files are created. The returned token is then persisted below.
    platform_token = _setup_platform_auth(agents) if not dmn_mode else None

    # Create directories only after platform authentication succeeds.
    folder_path.mkdir(parents=True)
    (folder_path / "agents").mkdir()
    (folder_path / "tools").mkdir()
    (folder_path / "skills").mkdir()
    (folder_path / "knowledge").mkdir()
    copy_assistant_instructions(folder_path)

    if platform_token:
        os.environ["CREWAI_ENTERPRISE_ACTION_AUTH_TOKEN"] = platform_token
        env_vars = load_env_vars(folder_path)
        env_vars["CREWAI_ENTERPRISE_ACTION_AUTH_TOKEN"] = platform_token
        write_env_file(folder_path, env_vars)
        _success("Enterprise Action Auth Token saved to .env")

    for agent in agents:
        _write_jsonc(
            folder_path / "agents" / f"{agent['name']}.jsonc",
            _agent_to_jsonc(agent),
        )

    _write_jsonc(
        folder_path / "crew.jsonc",
        _crew_to_jsonc(name, agents, tasks, crew_settings),
    )

    # Write pyproject.toml
    (folder_path / "pyproject.toml").write_text(
        _render_json_crew_template(
            "pyproject.toml",
            {
                "folder_name": folder_name,
                "name": name,
                "crewai_tools_dependency": get_crewai_tools_dependency(),
            },
        ),
        encoding="utf-8",
    )

    # Write .gitignore
    (folder_path / ".gitignore").write_text(
        _render_json_crew_template(".gitignore"),
        encoding="utf-8",
    )

    # Write README
    (folder_path / "README.md").write_text(
        _render_json_crew_template("README.md", {"name": name}),
        encoding="utf-8",
    )

    # Write knowledge placeholder
    (folder_path / "knowledge" / "user_preference.txt").write_text(
        _render_json_crew_template("knowledge/user_preference.txt"),
        encoding="utf-8",
    )

    # Keep skills dir tracked by git
    (folder_path / "skills" / ".gitkeep").write_text("", encoding="utf-8")

    # Setup .env with API keys
    if not skip_provider and not dmn_mode:
        models = list({a["llm"] for a in agents})
        for model in models:
            _setup_env(folder_path, model)

    # Minted at creation so the project has a stable identity from run one.
    # This is the default `crewai create crew` path, not just --classic.
    project_id = get_or_create_project_id(folder_path / "pyproject.toml")
    Telemetry().project_created_span("json_crew", project_id)
    initialize_if_git_available(folder_path)

    click.echo()
    click.secho(f"  ✔ Crew {name} created successfully!", fg="green", bold=True)
    click.echo()
    click.secho("  Next steps:", bold=True)
    click.echo()
    click.echo(f"    cd {folder_name}")
    click.echo()
    click.secho("  Run your crew:", fg="cyan")
    click.echo("    crewai run")
    click.echo()
    click.secho("  Customize your crew:", fg="cyan")
    click.echo("    agents/*.jsonc    Define agent roles, goals, and LLMs")
    click.echo("    crew.jsonc        Configure tasks and optional input defaults")
    click.echo("    tools/            Add custom tools (Python)")
    click.echo()