|
| 1 | +"""GitHub Action job wrapper around ParitokGateway (TDD §5 / §10 / §22 M2.2). |
| 2 | +
|
| 3 | +Starts the local proxy via ``ParitokGateway``, exports ``OPENAI_BASE_URL`` for |
| 4 | +the compressed LLM binding, runs ``python -m leanci``, and always stops the |
| 5 | +proxy. Does not re-implement proxy spawn/health logic. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import os |
| 11 | +import sys |
| 12 | +from collections.abc import Callable, Mapping, MutableMapping, Sequence |
| 13 | +from pathlib import Path |
| 14 | +from typing import Any |
| 15 | + |
| 16 | +from leanci.__main__ import main as leanci_main |
| 17 | +from leanci.paritok_gateway import ParitokGateway |
| 18 | +from leanci.upstream_forwarder import UpstreamForwarder |
| 19 | + |
| 20 | +GatewayFactory = Callable[..., Any] |
| 21 | +Runner = Callable[[list[str], dict[str, str]], int] |
| 22 | + |
| 23 | +_DEFAULT_PORT = 8080 |
| 24 | +_FORWARDER_PORT = 8099 |
| 25 | + |
| 26 | + |
| 27 | +def run_action_job( |
| 28 | + argv: Sequence[str] | None = None, |
| 29 | + *, |
| 30 | + env: Mapping[str, str] | MutableMapping[str, str] | None = None, |
| 31 | + gateway: Any | None = None, |
| 32 | + gateway_factory: GatewayFactory | None = None, |
| 33 | + runner: Runner | None = None, |
| 34 | +) -> int: |
| 35 | + """Start Paritok, run LeanCI with ``OPENAI_BASE_URL`` set, always stop proxy.""" |
| 36 | + working_env = dict(os.environ if env is None else env) |
| 37 | + leanci_argv = list(argv) if argv is not None else [] |
| 38 | + |
| 39 | + forwarder = _maybe_start_forwarder(working_env) |
| 40 | + gw = gateway if gateway is not None else _build_gateway(working_env, gateway_factory) |
| 41 | + run = runner if runner is not None else _default_runner |
| 42 | + |
| 43 | + try: |
| 44 | + gw.start() |
| 45 | + working_env["OPENAI_BASE_URL"] = gw.base_url |
| 46 | + # Propagate into this process so in-process runners (and children) see it. |
| 47 | + os.environ["OPENAI_BASE_URL"] = gw.base_url |
| 48 | + return run(leanci_argv, working_env) |
| 49 | + finally: |
| 50 | + gw.stop() |
| 51 | + if forwarder is not None: |
| 52 | + forwarder.stop() |
| 53 | + |
| 54 | + |
| 55 | +def _maybe_start_forwarder(env: MutableMapping[str, str]) -> UpstreamForwarder | None: |
| 56 | + """For Groq (Cloudflare-fronted), front Paritok with a UA-bearing local hop.""" |
| 57 | + openai_url = (env.get("LEANCI_OPENAI_URL") or "").strip() |
| 58 | + if "api.groq.com" not in openai_url: |
| 59 | + return None |
| 60 | + forwarder = UpstreamForwarder( |
| 61 | + target_base="https://api.groq.com/openai", |
| 62 | + port=int(env.get("LEANCI_UPSTREAM_FORWARD_PORT") or str(_FORWARDER_PORT)), |
| 63 | + ) |
| 64 | + forwarder.start() |
| 65 | + env["LEANCI_OPENAI_URL"] = forwarder.openai_url |
| 66 | + os.environ["LEANCI_OPENAI_URL"] = forwarder.openai_url |
| 67 | + return forwarder |
| 68 | + |
| 69 | + |
| 70 | +def _build_gateway( |
| 71 | + env: Mapping[str, str], |
| 72 | + factory: GatewayFactory | None, |
| 73 | +) -> Any: |
| 74 | + port = int(env.get("LEANCI_PARITOK_PORT", str(_DEFAULT_PORT))) |
| 75 | + runner_temp = env.get("RUNNER_TEMP") or env.get("TMPDIR") or str(Path.cwd()) |
| 76 | + config_path = Path(runner_temp) / "paritok.yaml" |
| 77 | + kwargs: dict[str, Any] = { |
| 78 | + "port": port, |
| 79 | + "config_path": config_path, |
| 80 | + "paritok_api_key": env.get("PARITOK_API_KEY"), |
| 81 | + "openai_api_key": env.get("OPENAI_API_KEY"), |
| 82 | + "openai_url": env.get("LEANCI_OPENAI_URL") or None, |
| 83 | + } |
| 84 | + if factory is not None: |
| 85 | + return factory(**kwargs) |
| 86 | + return ParitokGateway(**kwargs) |
| 87 | + |
| 88 | + |
| 89 | +def _default_runner(argv: list[str], env: dict[str, str]) -> int: |
| 90 | + return leanci_main(argv, env=env) |
| 91 | + |
| 92 | + |
| 93 | +def main(argv: Sequence[str] | None = None) -> int: |
| 94 | + """CLI used by ``action/entrypoint.sh``: ``python -m leanci.action_runtime``.""" |
| 95 | + args = list(sys.argv[1:] if argv is None else argv) |
| 96 | + return run_action_job(args) |
| 97 | + |
| 98 | + |
| 99 | +if __name__ == "__main__": |
| 100 | + raise SystemExit(main()) |
0 commit comments