|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import argparse |
| 4 | +import json |
| 5 | +import errno |
| 6 | +import os |
| 7 | +import sys |
| 8 | +import webbrowser |
| 9 | + |
| 10 | +from .app import create_app |
| 11 | +from .config import CLIENT_ID_DEFAULT |
| 12 | +from .oauth import OAuthHTTPServer, OAuthHandler, REQUIRED_PORT, URL_BASE |
| 13 | +from .utils import eprint, get_home_dir, load_chatgpt_tokens, parse_jwt_claims, read_auth_file |
| 14 | + |
| 15 | + |
| 16 | +def cmd_login(no_browser: bool, verbose: bool) -> int: |
| 17 | + home_dir = get_home_dir() |
| 18 | + client_id = CLIENT_ID_DEFAULT |
| 19 | + if not client_id: |
| 20 | + eprint("ERROR: No OAuth client id configured. Set CHATGPT_LOCAL_CLIENT_ID.") |
| 21 | + return 1 |
| 22 | + |
| 23 | + try: |
| 24 | + httpd = OAuthHTTPServer(("127.0.0.1", REQUIRED_PORT), OAuthHandler, home_dir=home_dir, client_id=client_id, verbose=verbose) |
| 25 | + except OSError as e: |
| 26 | + eprint(f"ERROR: {e}") |
| 27 | + if e.errno == errno.EADDRINUSE: |
| 28 | + return 13 |
| 29 | + return 1 |
| 30 | + |
| 31 | + auth_url = httpd.auth_url() |
| 32 | + with httpd: |
| 33 | + eprint(f"Starting local login server on {URL_BASE}") |
| 34 | + if not no_browser: |
| 35 | + try: |
| 36 | + webbrowser.open(auth_url, new=1, autoraise=True) |
| 37 | + except Exception as e: |
| 38 | + eprint(f"Failed to open browser: {e}") |
| 39 | + eprint(f"If your browser did not open, navigate to:\n{auth_url}") |
| 40 | + try: |
| 41 | + httpd.serve_forever() |
| 42 | + except KeyboardInterrupt: |
| 43 | + eprint("\nKeyboard interrupt received, exiting.") |
| 44 | + return httpd.exit_code |
| 45 | + |
| 46 | + |
| 47 | +def cmd_serve( |
| 48 | + host: str, |
| 49 | + port: int, |
| 50 | + verbose: bool, |
| 51 | + reasoning_effort: str, |
| 52 | + reasoning_summary: str, |
| 53 | + reasoning_compat: str, |
| 54 | + debug_model: str | None, |
| 55 | +) -> int: |
| 56 | + app = create_app( |
| 57 | + verbose=verbose, |
| 58 | + reasoning_effort=reasoning_effort, |
| 59 | + reasoning_summary=reasoning_summary, |
| 60 | + reasoning_compat=reasoning_compat, |
| 61 | + debug_model=debug_model, |
| 62 | + ) |
| 63 | + |
| 64 | + app.run(host=host, debug=False, use_reloader=False, port=port, threaded=True) |
| 65 | + return 0 |
| 66 | + |
| 67 | + |
| 68 | +def main() -> None: |
| 69 | + parser = argparse.ArgumentParser(description="ChatGPT Local: login & OpenAI-compatible proxy") |
| 70 | + sub = parser.add_subparsers(dest="command", required=True) |
| 71 | + |
| 72 | + p_login = sub.add_parser("login", help="Authorize with ChatGPT and store tokens") |
| 73 | + p_login.add_argument("--no-browser", action="store_true", help="Do not open the browser automatically") |
| 74 | + p_login.add_argument("--verbose", action="store_true", help="Enable verbose logging") |
| 75 | + |
| 76 | + p_serve = sub.add_parser("serve", help="Run local OpenAI-compatible server") |
| 77 | + p_serve.add_argument("--host", default="127.0.0.1") |
| 78 | + p_serve.add_argument("--port", type=int, default=8000) |
| 79 | + p_serve.add_argument("--verbose", action="store_true", help="Enable verbose logging") |
| 80 | + p_serve.add_argument( |
| 81 | + "--debug-model", |
| 82 | + dest="debug_model", |
| 83 | + default=os.getenv("CHATGPT_LOCAL_DEBUG_MODEL"), |
| 84 | + help="Forcibly override requested 'model' with this value", |
| 85 | + ) |
| 86 | + p_serve.add_argument( |
| 87 | + "--reasoning-effort", |
| 88 | + choices=["low", "medium", "high", "none"], |
| 89 | + default=os.getenv("CHATGPT_LOCAL_REASONING_EFFORT", "medium").lower(), |
| 90 | + help="Reasoning effort level for Responses API (default: medium)", |
| 91 | + ) |
| 92 | + p_serve.add_argument( |
| 93 | + "--reasoning-summary", |
| 94 | + choices=["auto", "concise", "detailed", "none"], |
| 95 | + default=os.getenv("CHATGPT_LOCAL_REASONING_SUMMARY", "auto").lower(), |
| 96 | + help="Reasoning summary verbosity (default: auto)", |
| 97 | + ) |
| 98 | + p_serve.add_argument( |
| 99 | + "--reasoning-compat", |
| 100 | + choices=["legacy", "o3", "think-tags", "current"], |
| 101 | + default=os.getenv("CHATGPT_LOCAL_REASONING_COMPAT", "think-tags").lower(), |
| 102 | + help=( |
| 103 | + "Compatibility mode for exposing reasoning to clients (legacy|o3|think-tags). " |
| 104 | + "'current' is accepted as an alias for 'legacy'" |
| 105 | + ), |
| 106 | + ) |
| 107 | + |
| 108 | + p_info = sub.add_parser("info", help="Print current stored tokens and derived account id") |
| 109 | + p_info.add_argument("--json", action="store_true", help="Output raw auth.json contents") |
| 110 | + |
| 111 | + args = parser.parse_args() |
| 112 | + |
| 113 | + if args.command == "login": |
| 114 | + sys.exit(cmd_login(no_browser=args.no_browser, verbose=args.verbose)) |
| 115 | + elif args.command == "serve": |
| 116 | + sys.exit( |
| 117 | + cmd_serve( |
| 118 | + host=args.host, |
| 119 | + port=args.port, |
| 120 | + verbose=args.verbose, |
| 121 | + reasoning_effort=args.reasoning_effort, |
| 122 | + reasoning_summary=args.reasoning_summary, |
| 123 | + reasoning_compat=args.reasoning_compat, |
| 124 | + debug_model=args.debug_model, |
| 125 | + ) |
| 126 | + ) |
| 127 | + elif args.command == "info": |
| 128 | + auth = read_auth_file() |
| 129 | + if getattr(args, "json", False): |
| 130 | + print(json.dumps(auth or {}, indent=2)) |
| 131 | + sys.exit(0) |
| 132 | + access_token, account_id, id_token = load_chatgpt_tokens() |
| 133 | + if not access_token or not id_token: |
| 134 | + print("👤 Account") |
| 135 | + print(" • Not signed in") |
| 136 | + print(" • Run: python3 chatmock.py login") |
| 137 | + sys.exit(0) |
| 138 | + |
| 139 | + id_claims = parse_jwt_claims(id_token) or {} |
| 140 | + access_claims = parse_jwt_claims(access_token) or {} |
| 141 | + |
| 142 | + email = id_claims.get("email") or id_claims.get("preferred_username") or "<unknown>" |
| 143 | + plan_raw = (access_claims.get("https://api.openai.com/auth") or {}).get("chatgpt_plan_type") or "unknown" |
| 144 | + plan_map = { |
| 145 | + "plus": "Plus", |
| 146 | + "pro": "Pro", |
| 147 | + "free": "Free", |
| 148 | + "team": "Team", |
| 149 | + "enterprise": "Enterprise", |
| 150 | + } |
| 151 | + plan = plan_map.get(str(plan_raw).lower(), str(plan_raw).title() if isinstance(plan_raw, str) else "Unknown") |
| 152 | + |
| 153 | + print("👤 Account") |
| 154 | + print(" • Signed in with ChatGPT") |
| 155 | + print(f" • Login: {email}") |
| 156 | + print(f" • Plan: {plan}") |
| 157 | + if account_id: |
| 158 | + print(f" • Account ID: {account_id}") |
| 159 | + sys.exit(0) |
| 160 | + else: |
| 161 | + parser.error("Unknown command") |
| 162 | + |
| 163 | + |
| 164 | +if __name__ == "__main__": |
| 165 | + main() |
0 commit comments