ornith / app.py
cloudunity's picture
Create app.py
905b22d verified
Raw
History Blame Contribute Delete
2.28 kB
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)