File size: 947 Bytes
701d9c5
 
 
d094faf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""TCP-probe the CLIProxyAPI port. Endpoint-agnostic — works regardless of
which protocol surfaces (Anthropic / OpenAI / Gemini) the proxy version
exposes."""

from __future__ import annotations

import socket
import time

from .endpoint import ProxyEndpoint


def is_ready(ep: ProxyEndpoint, timeout: float = 2.0) -> bool:
    try:
        with socket.create_connection((ep.host, ep.port), timeout=timeout):
            return True
    except OSError:
        return False


def wait_until_ready(ep: ProxyEndpoint, timeout: float = 30.0) -> None:
    deadline = time.monotonic() + timeout
    while time.monotonic() < deadline:
        if is_ready(ep):
            return
        time.sleep(0.5)
    raise SystemExit(
        f"CLIProxyAPI at {ep.base_url()} did not respond within {timeout:.0f}s.\n"
        f"Start it (e.g. `cliproxy --config ~/.cli-proxy-api/config.yaml`) "
        f"and confirm CLIPROXYAPI_HOST / CLIPROXYAPI_PORT."
    )