File size: 2,023 Bytes
35e80ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""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())