feat(cli): introduce AI-agent friendly CLI with JSON output, non-interactive mode, and standardized exit behavior

🎯 Objective

This MR enhances the CLI to support AI agents, automation workflows, and headless execution by introducing structured outputs, eliminating interactive dependencies, and standardizing behavior.


Key Features

1. 🧾 JSON Output Support

  • Added global --json flag
  • Outputs machine-readable structured JSON
  • Ensures:
    • No rich tables / ANSI formatting
    • No progress bars (tqdm)
    • Clean output for parsing by scripts/LLMs

2. 🤖 Non-Interactive Mode

  • Added global --non-interactive flag
  • Disables all prompts (input(), confirmations, selections)
  • Behavior:
    • Uses defaults when safe
    • Fails gracefully with clear errors when required input is missing

3. 📊 Structured Job Output

  • Standardized JSON schema for job data:

{
"index": 1,
"mode": "upload",
"status": "completed",
"progress": {
"completed": 1,
"failed": 0,
"total": 1
},
"last_updated": "timestamp",
"completed_files": [],
"failed_files": []
}


4. 🔁 Backward Compatibility

  • Existing CLI behavior remains unchanged by default
  • Rich UI (tables, colors) still works without --json

5. ️ Exit Code Standardization

Scenario Exit Code
Success 0
Validation Error (e.g., invalid job index) 1
API / Network / System Error 2

6. 📤 Output Stream Separation

  • stdout → JSON output only
  • stderr → logs, warnings, errors
  • Prevents JSON corruption in automation pipelines

🛠️ Implementation Highlights

  • Added global flags via Typer context
  • Introduced centralized output handling (output.py)
  • Refactored job commands (list, show) to support dual output modes
  • Added guard for interactive prompts in non-interactive mode
  • Updated error handling to align with exit codes
  • Ensured consistent behavior across commands

Testing & Verification

Manual Tests

  • corpus-client job list --json
  • corpus-client job show 1 --json
  • corpus-client job show 999 --json → returns JSON error + exit code 1
  • corpus-client job --non-interactive

Output Validation

  • JSON is clean and parseable (no extra logs in stdout)
  • stderr correctly captures warnings/errors

Exit Codes

  • Verified using echo $?

📌 Impact

  • Enables AI/LLM integration
  • Supports automation pipelines & scripting
  • Makes CLI production-ready for headless environments

🔮 Future Enhancements (Optional)

  • Add --format yaml
  • Extend JSON output to all commands
  • Add schema validation for outputs
Edited by Mohana Sri Bhavitha

Merge request reports

Loading