"""Command line: python -m ai_coach.cli session.json [--level hs_varsity] [--fallback-only] [--no-cache] [--json]""" from __future__ import annotations import argparse import json import sys from pathlib import Path from .config import Settings from .input_builder import InputError from .observability import configure_logging from .response import render_response from .service import CoachService def main(argv: list[str] | None = None) -> int: parser = argparse.ArgumentParser(prog="ai-coach", description="Write a coaching note for a session JSON") parser.add_argument("session", type=Path, help="pipeline session JSON (baseball_mvp output)") parser.add_argument("--level", help="hs_jv | hs_varsity | college (if not in the session JSON)") parser.add_argument("--fallback-only", action="store_true", help="never call the model") parser.add_argument("--no-cache", action="store_true", help="neither read nor write the response cache") parser.add_argument("--json", action="store_true", help="print the full response (v2.0) as JSON") args = parser.parse_args(argv) settings = Settings() if args.fallback_only: settings = settings.model_copy(update={"google_api_key": None}) if args.no_cache: settings = settings.model_copy(update={"cache_enabled": False}) configure_logging(settings) service = CoachService(settings) try: response = service.respond(json.loads(args.session.read_text(encoding="utf-8")), level=args.level) except (InputError, OSError, ValueError) as exc: print(f"error: {exc}", file=sys.stderr) return 2 if args.json: print(response.model_dump_json(indent=2)) else: print(render_response(response)) meta = response.meta print( f"\n[source={meta.source} model={meta.model} cache_hit={meta.cache_hit} fallback_reason={meta.fallback_reason}]", file=sys.stderr, ) return 0 if __name__ == "__main__": raise SystemExit(main())