|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Code Generator CLI using GitHub Models (OpenAI-compatible API) |
| 4 | +
|
| 5 | +Usage: |
| 6 | + python code_generator.py "Program description" language |
| 7 | +
|
| 8 | +Reads the token from secret.txt (same directory) and prints the generated code. |
| 9 | +""" |
| 10 | + |
| 11 | +import sys |
| 12 | +import argparse |
| 13 | +import logging |
| 14 | +import socket |
| 15 | +import ssl |
| 16 | +from urllib.parse import urlparse |
| 17 | +from pathlib import Path |
| 18 | +from typing import Optional |
| 19 | +from openai import OpenAI, APIConnectionError, RateLimitError, APIStatusError |
| 20 | + |
| 21 | +DEFAULT_ENDPOINT = "https://models.github.ai/inference" |
| 22 | +DEFAULT_MODEL = "openai/gpt-4o-mini" |
| 23 | + |
| 24 | + |
| 25 | +def load_token_from_secret() -> str: |
| 26 | + secret_path = Path("secret.txt") |
| 27 | + if not secret_path.exists(): |
| 28 | + print("Error: secret.txt not found. Place your GitHub token in secret.txt") |
| 29 | + sys.exit(1) |
| 30 | + token = secret_path.read_text(encoding="utf-8").strip() |
| 31 | + if not token: |
| 32 | + print("Error: secret.txt is empty.") |
| 33 | + sys.exit(1) |
| 34 | + return token |
| 35 | + |
| 36 | + |
| 37 | +def generate_code(program_name: str, language: str, token: str, endpoint: str = DEFAULT_ENDPOINT, model: str = DEFAULT_MODEL) -> str: |
| 38 | + client = OpenAI(api_key=token, base_url=endpoint, timeout=30.0) |
| 39 | + |
| 40 | + prompt = f""" |
| 41 | + Generate a complete, working {language} program for: {program_name} |
| 42 | +
|
| 43 | + Requirements: |
| 44 | + - Include clear comments and minimal error handling |
| 45 | + - Include example usage if applicable |
| 46 | + - Follow best practices for {language} |
| 47 | + - Only return the code, no explanations or markdown formatting |
| 48 | + """ |
| 49 | + |
| 50 | + # Minimal retry loop for transient network errors |
| 51 | + last_error: Optional[Exception] = None |
| 52 | + for _ in range(3): |
| 53 | + try: |
| 54 | + response = client.chat.completions.create( |
| 55 | + model=model, |
| 56 | + messages=[ |
| 57 | + {"role": "system", "content": f"You are an expert {language} programmer. Generate clean, well-documented code."}, |
| 58 | + {"role": "user", "content": prompt} |
| 59 | + ], |
| 60 | + max_tokens=2000, |
| 61 | + temperature=0.7, |
| 62 | + ) |
| 63 | + return response.choices[0].message.content.strip() |
| 64 | + except (APIConnectionError, RateLimitError, APIStatusError) as e: |
| 65 | + last_error = e |
| 66 | + |
| 67 | + raise Exception(f"Failed to generate after retries: {last_error}") |
| 68 | + |
| 69 | +import os |
| 70 | +def debug_connection(token: str, endpoint: str = DEFAULT_ENDPOINT) -> None: |
| 71 | + """Deep connectivity diagnostics with extensive logging.""" |
| 72 | + logging.info("Starting connection diagnostics") |
| 73 | + logging.info("Endpoint: %s", endpoint) |
| 74 | + logging.info("Env HTTPS_PROXY=%s", os.environ.get("HTTPS_PROXY")) |
| 75 | + logging.info("Env HTTP_PROXY=%s", os.environ.get("HTTP_PROXY")) |
| 76 | + logging.info("Env SSL_CERT_FILE=%s", os.environ.get("SSL_CERT_FILE")) |
| 77 | + logging.info("Env REQUESTS_CA_BUNDLE=%s", os.environ.get("REQUESTS_CA_BUNDLE")) |
| 78 | + |
| 79 | + # CA bundle path (certifi if available) |
| 80 | + ca_path = None |
| 81 | + try: |
| 82 | + import certifi # type: ignore |
| 83 | + ca_path = certifi.where() |
| 84 | + logging.info("certifi.where() => %s", ca_path) |
| 85 | + except Exception as e: |
| 86 | + logging.warning("certifi not available or failed: %s", repr(e)) |
| 87 | + |
| 88 | + parsed = urlparse(endpoint) |
| 89 | + host = parsed.hostname or "" |
| 90 | + port = parsed.port or 443 |
| 91 | + path = (parsed.path.rstrip("/") or "") + "/v1/models" |
| 92 | + logging.info("Resolved request target host=%s port=%d path=%s", host, port, path) |
| 93 | + |
| 94 | + # DNS resolution |
| 95 | + try: |
| 96 | + addrs = socket.getaddrinfo(host, port, proto=socket.IPPROTO_TCP) |
| 97 | + unique_ips = sorted({ai[4][0] for ai in addrs}) |
| 98 | + logging.info("DNS A/AAAA results: %s", ", ".join(unique_ips)) |
| 99 | + except Exception as e: |
| 100 | + logging.error("DNS resolution failed: %s", repr(e)) |
| 101 | + |
| 102 | + # TLS handshake and simple GET using stdlib |
| 103 | + try: |
| 104 | + context = ssl.create_default_context() |
| 105 | + if ca_path: |
| 106 | + context.load_verify_locations(cafile=ca_path) |
| 107 | + with socket.create_connection((host, port), timeout=10) as sock: |
| 108 | + with context.wrap_socket(sock, server_hostname=host) as ssock: |
| 109 | + logging.info("TLS handshake OK. Cipher=%s, TLSVersion=%s", ssock.cipher(), ssock.version()) |
| 110 | + # Minimal HTTP/1.1 request |
| 111 | + req = ( |
| 112 | + f"GET {path} HTTP/1.1\r\n" |
| 113 | + f"Host: {host}\r\n" |
| 114 | + f"Authorization: Bearer {token[:6]}...\r\n" |
| 115 | + f"User-Agent: debug-connection-cli\r\n" |
| 116 | + f"Connection: close\r\n\r\n" |
| 117 | + ) |
| 118 | + ssock.sendall(req.encode("utf-8")) |
| 119 | + data = ssock.recv(4096) |
| 120 | + preview = data.decode("latin1", errors="replace") |
| 121 | + logging.info("Initial HTTP response bytes=%d", len(data)) |
| 122 | + logging.debug("HTTP response preview:\n%s", preview[:1000]) |
| 123 | + except ssl.SSLError as e: |
| 124 | + logging.error("TLS/SSL error: %s", repr(e)) |
| 125 | + except Exception as e: |
| 126 | + logging.error("Socket/HTTP error: %s", repr(e)) |
| 127 | + |
| 128 | + |
| 129 | +def parse_args() -> argparse.Namespace: |
| 130 | + parser = argparse.ArgumentParser(description="Generate code using GitHub Models") |
| 131 | + parser.add_argument("program_name", help="Name or description of the program to generate") |
| 132 | + parser.add_argument("language", help="Programming language for the generated code") |
| 133 | + parser.add_argument("--log-level", default="INFO", choices=["DEBUG","INFO","WARNING","ERROR","CRITICAL"], help="Logging level") |
| 134 | + parser.add_argument("--debug-connect", action="store_true", help="Run deep connection diagnostics before generation") |
| 135 | + return parser.parse_args() |
| 136 | + |
| 137 | + |
| 138 | +def main() -> None: |
| 139 | + args = parse_args() |
| 140 | + logging.basicConfig( |
| 141 | + level=getattr(logging, args.log_level.upper(), logging.INFO), |
| 142 | + format="%(asctime)s | %(levelname)s | %(name)s | %(message)s", |
| 143 | + datefmt="%H:%M:%S", |
| 144 | + ) |
| 145 | + token = load_token_from_secret() |
| 146 | + if args.debug_connect: |
| 147 | + debug_connection(token, DEFAULT_ENDPOINT) |
| 148 | + try: |
| 149 | + code = generate_code(args.program_name, args.language, token) |
| 150 | + print(code) |
| 151 | + except KeyboardInterrupt: |
| 152 | + print("\nOperation cancelled.") |
| 153 | + sys.exit(1) |
| 154 | + except Exception as e: |
| 155 | + print(f"Error: {e}") |
| 156 | + sys.exit(1) |
| 157 | + |
| 158 | + |
| 159 | +if __name__ == "__main__": |
| 160 | + main() |
| 161 | + |
0 commit comments