File size: 2,280 Bytes
905b22d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import os
import subprocess
import sys

PORT = int(os.environ.get("PORT", 7860))
WORKSPACE = "/data/workspace"
CLAUDE_HOME = "/data/claude-config"
INDEX_HTML = "/app/www/index.html"

os.makedirs(WORKSPACE, exist_ok=True)
os.makedirs(CLAUDE_HOME, exist_ok=True)

env = os.environ.copy()
env["HOME"] = CLAUDE_HOME

# --- Custom API endpoint support ---
# Two modes:
#
# 1. Direct Anthropic-compatible server:
#    Set ANTHROPIC_BASE_URL to a server that already implements the
#    Anthropic /v1/messages schema. Nothing else needed.
#
# 2. OpenAI-compatible server (e.g. your LiteRT-LM Flask server):
#    Set OPENAI_BASE_URL (and OPENAI_API_KEY / OPENAI_MODEL as needed).
#    This launches proxy.py locally, which translates Claude Code's
#    Anthropic-format requests into OpenAI-format calls against your
#    server, then translates responses back. Claude Code is pointed at
#    the local proxy automatically.
custom_base_url = os.environ.get("ANTHROPIC_BASE_URL")
openai_base_url = os.environ.get("OPENAI_BASE_URL")

proxy_proc = None
if openai_base_url and not custom_base_url:
    proxy_port = os.environ.get("PROXY_PORT", "8317")
    print(f"Starting OpenAI->Anthropic proxy for backend: {openai_base_url}", flush=True)
    proxy_env = env.copy()
    proxy_env["PROXY_PORT"] = proxy_port
    proxy_proc = subprocess.Popen(
        [sys.executable, "/app/proxy.py"],
        env=proxy_env,
    )
    env["ANTHROPIC_BASE_URL"] = f"http://localhost:{proxy_port}"
    print(f"Claude Code will use proxy at http://localhost:{proxy_port}", flush=True)
elif custom_base_url:
    env["ANTHROPIC_BASE_URL"] = custom_base_url
    print(f"Using custom Anthropic-compatible endpoint: {custom_base_url}", flush=True)
else:
    print("No custom endpoint set, using default api.anthropic.com", flush=True)

ttyd_cmd = [
    "ttyd",
    "-p", str(PORT),
    "-W",
    "-I", INDEX_HTML,
    "-t", "fontSize=13",
    "-t", "theme={\"background\":\"#1e1e1e\"}",
    "-t", "disableLeaveAlert=true",
    "bash", "-c", f"cd {WORKSPACE} && claude",
]

print(f"Starting ttyd on port {PORT}...", flush=True)

proc = subprocess.Popen(ttyd_cmd, env=env)

try:
    proc.wait()
except KeyboardInterrupt:
    proc.terminate()
finally:
    if proxy_proc:
        proxy_proc.terminate()
    sys.exit(0)