from pathlib import Path from typing import Optional import click from rich.console import Console from rich.panel import Panel from rich.table import Table from afk_developer.grillme.questions import auto_discover_project from afk_developer.grillme.spec_model import DetectedContext, TaskIntent, TaskSpec console = Console() KEYWORDS_ANALYZE = [ "summarize", "analyse", "analyze", "audit", "list all", "list every", "explain", "document", "describe", "review", "what does", "how does", "find all", ] KEYWORDS_IMPLEMENT = [ "add", "create", "build", "make", "implement", "develop", "write", "transform", "extend", "modify", "port", ] KEYWORDS_REFACTOR = [ "refactor", "cleanup", "clean up", "reorganize", "restructure", "migrate", "rename", "extract", "simplify", "optimize", ] KEYWORDS_DEBUG = [ "fix", "bug", "broken", "error", "crash", "failing", "incorrect", "wrong", "debug", "not working", "issue", ] NON_ANSWERS = {"i do not know", "idk", "i don't know", "not sure", "dunno", "unknown", "none", ""} RECOMMENDED_STACK: dict[str, dict[str, str]] = { "python": {"test": "pytest", "linter": "ruff", "framework": ""}, "go": {"test": "go test", "linter": "golangci-lint", "framework": ""}, "golang": {"test": "go test", "linter": "golangci-lint", "framework": ""}, "javascript": {"test": "jest", "linter": "eslint", "framework": ""}, "js": {"test": "jest", "linter": "eslint", "framework": ""}, "typescript": {"test": "vitest", "linter": "eslint", "framework": ""}, "ts": {"test": "vitest", "linter": "eslint", "framework": ""}, "rust": {"test": "cargo test", "linter": "clippy", "framework": ""}, "java": {"test": "junit", "linter": "checkstyle", "framework": ""}, "kotlin": {"test": "kotlin.test", "linter": "detekt", "framework": ""}, "c": {"test": "ctest", "linter": "", "framework": ""}, "c++": {"test": "gtest", "linter": "clang-tidy", "framework": ""}, "cpp": {"test": "gtest", "linter": "clang-tidy", "framework": ""}, "ruby": {"test": "rspec", "linter": "rubocop", "framework": ""}, "php": {"test": "phpunit", "linter": "phpcs", "framework": ""}, "swift": {"test": "xctest", "linter": "swiftlint", "framework": ""}, "csharp": {"test": "xunit", "linter": "dotnet format", "framework": ""}, "c#": {"test": "xunit", "linter": "dotnet format", "framework": ""}, } def _recommend_stack(language: str) -> dict[str, str]: lang_key = language.lower().strip() return RECOMMENDED_STACK.get(lang_key, {"test": "", "linter": "", "framework": ""}) def _is_non_answer(value: str) -> bool: return value.lower().strip() in NON_ANSWERS def _prompt_multiline(prompt_text: str) -> str: console.print(prompt_text) lines: list[str] = [] is_first_line = True while True: try: line = input() except (EOFError, KeyboardInterrupt): break if line == "" and not is_first_line: break if line == "" and is_first_line: continue lines.append(line) is_first_line = False return "\n".join(lines).strip() def _detect_intent(goal: str) -> TaskIntent: goal_lower = goal.lower() # A build request can legitimately include terms such as "fix", "document", or "review" # in its acceptance criteria. Treat its explicit implementation verb as authoritative. # Treat its explicit implementation verb as authoritative rather than misclassifying it # as debug or read-only analysis work. if any(kw in goal_lower for kw in KEYWORDS_IMPLEMENT): return TaskIntent.IMPLEMENT if any(kw in goal_lower for kw in KEYWORDS_DEBUG): return TaskIntent.DEBUG if any(kw in goal_lower for kw in KEYWORDS_REFACTOR): return TaskIntent.REFACTOR if any(kw in goal_lower for kw in KEYWORDS_ANALYZE): return TaskIntent.ANALYZE return TaskIntent.IMPLEMENT def _display_detected(ctx: DetectedContext): table = Table(title="Auto-Detected Project Context") table.add_column("Property", style="cyan") table.add_column("Value", style="green") rows = [ ("Language", ctx.language or "(not detected)"), ("Framework", ctx.framework or "(not detected)"), ("Test Framework", ctx.test_framework or "(not detected)"), ("Linter", ctx.linter or "(not detected)"), ("Package Manager", ctx.package_manager or "(not detected)"), ("Source Files", str(ctx.existing_files_count)), ("Test Files", str(ctx.existing_tests_count)), ] for prop, value in rows: table.add_row(prop, value) console.print(table) def run_interview(project_dir_str: str, goal: Optional[str] = None, yes: bool = False) -> TaskSpec: project_dir = Path(project_dir_str).resolve() auto = goal is not None or yes if not project_dir.exists(): console.print(f"[yellow]Directory does not exist: {project_dir}[/yellow]") if auto or click.confirm("Create it?", default=True): project_dir.mkdir(parents=True, exist_ok=True) console.print(f"[green]Created: {project_dir}[/green]") else: raise click.BadParameter(f"Project directory does not exist: {project_dir}") console.print( Panel.fit( "[bold]AFK Developer[/bold] — Task Intake\n" "I'll ask a few questions to understand your task, then generate a plan.", border_style="blue", ) ) auto_ctx = auto_discover_project(project_dir) _display_detected(auto_ctx) project_name = project_dir.name interactive = goal is None if not goal: goal = _prompt_multiline( "\nWhat do you want done? (paste text, press Enter twice on empty line to finish)", ) intent = _detect_intent(goal) intent_display = { TaskIntent.ANALYZE: "Analysis (read-only)", TaskIntent.REFACTOR: "Refactor", TaskIntent.DEBUG: "Debug/Fix", TaskIntent.IMPLEMENT: "Implement (new code)", } console.print(f"\nDetected intent: [bold cyan]{intent_display[intent]}[/bold cyan]") if intent == TaskIntent.ANALYZE: return TaskSpec( project_dir=project_dir, project_name=project_name, goal=goal, intent=intent, language=auto_ctx.language, framework=auto_ctx.framework, test_framework=auto_ctx.test_framework, linter=auto_ctx.linter, auto_detected=auto_ctx, approved=True, ) if interactive: language = click.prompt( "\nLanguage/Framework", type=str, default=auto_ctx.language or "", show_default=True, ).strip() language = "" if _is_non_answer(language) else language else: language = auto_ctx.language or "" console.print(f"\nLanguage/Framework: [dim]{language or 'python (default)'}[/dim]") if not language: language = "python" console.print( "[dim]Using Python (specify with full interactive mode: afk grillme)[/dim]" ) recommended = _recommend_stack(language) if interactive: framework = click.prompt( "Specific framework (if any)", type=str, default=auto_ctx.framework or "none", show_default=True, ).strip() else: framework = auto_ctx.framework or "none" if framework.lower() == "none" or _is_non_answer(framework): framework = recommended.get("framework", "") suggested_test = recommended.get("test", "") if interactive: test_framework = click.prompt( "Test framework", type=str, default=auto_ctx.test_framework or suggested_test or "none", show_default=True, ).strip() else: test_framework = auto_ctx.test_framework or suggested_test if _is_non_answer(test_framework): test_framework = suggested_test if not test_framework and suggested_test: test_framework = suggested_test console.print(f"[dim]Using recommended test framework: {test_framework}[/dim]") suggested_linter = recommended.get("linter", "") if interactive: linter = click.prompt( "Linter/formatter", type=str, default=auto_ctx.linter or suggested_linter or "none", show_default=True, ).strip() else: linter = auto_ctx.linter or suggested_linter if _is_non_answer(linter): linter = suggested_linter if not linter and suggested_linter: linter = suggested_linter console.print(f"[dim]Using recommended linter: {linter}[/dim]") if interactive: constraints_raw = click.prompt( "Constraints (comma-separated. e.g. 'SQLite only, no new deps')", type=str, default="", show_default=False, ).strip() constraints = ( [c.strip() for c in constraints_raw.split(",") if c.strip()] if constraints_raw else [] ) additional = click.prompt( "Any additional context or notes?", type=str, default="", show_default=False, ).strip() else: constraints = [] additional = "" spec = TaskSpec( project_dir=project_dir, project_name=project_name, goal=goal, intent=intent, language=language, framework=framework, test_framework=test_framework, linter=linter, constraints=constraints, auto_detected=auto_ctx, additional_context=additional, approved=False, ) console.print() console.print(Panel.fit(_format_spec(spec), title="Generated Task Spec", border_style="yellow")) if interactive: confirmed = click.confirm("\nProceed with this spec?", default=True) else: confirmed = True console.print("[dim]Auto-approved (--goal mode)[/dim]") spec.approved = confirmed return spec def _format_spec(spec: TaskSpec) -> str: lines = [ f"Project: {spec.project_dir}", f"Goal: {spec.goal}", f"Intent: {spec.intent.value}", f"Language: {spec.language or '(not specified)'}", f"Framework: {spec.framework or '(not specified)'}", f"Test Framework: {spec.test_framework or '(not specified)'}", f"Linter: {spec.linter or '(not specified)'}", ] if spec.constraints: lines.append(f"Constraints: {', '.join(spec.constraints)}") if spec.additional_context: lines.append(f"Additional Context: {spec.additional_context}") return "\n".join(lines)