|
#!/usr/bin/env python3 |
|
|
|
import json |
|
import os |
|
import sys |
|
from http.client import HTTPConnection, HTTPSConnection |
|
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer |
|
from urllib.parse import urlparse, urlunparse |
|
|
|
|
|
LISTEN_HOST = os.getenv("OPENCODE_PROXY_HOST", "127.0.0.1") |
|
LISTEN_PORT = int(os.getenv("OPENCODE_PROXY_PORT", "4010")) |
|
TARGET_BASE = os.getenv("LITELLM_BASE_URL", "http://127.0.0.1:4000/v1").rstrip("/") |
|
DEBUG = os.getenv("PROXY_DEBUG", "false").lower() in {"1", "true", "yes", "on"} |
|
|
|
HOP_BY_HOP_HEADERS = { |
|
"connection", |
|
"keep-alive", |
|
"proxy-authenticate", |
|
"proxy-authorization", |
|
"te", |
|
"trailer", |
|
"transfer-encoding", |
|
"upgrade", |
|
} |
|
|
|
|
|
class ToolCallProxyHandler(BaseHTTPRequestHandler): |
|
protocol_version = "HTTP/1.1" |
|
|
|
def do_GET(self): |
|
self._proxy_request() |
|
|
|
def do_POST(self): |
|
self._proxy_request() |
|
|
|
def do_OPTIONS(self): |
|
self._proxy_request() |
|
|
|
def log_message(self, fmt, *args): |
|
sys.stderr.write("[%s] %s\n" % (self.log_date_time_string(), fmt % args)) |
|
|
|
def _proxy_request(self): |
|
response_started = False |
|
conn = None |
|
|
|
try: |
|
target_url = self._target_url() |
|
parsed = urlparse(target_url) |
|
|
|
body = self._read_body() |
|
headers = self._forward_headers(parsed.netloc, body) |
|
|
|
if DEBUG: |
|
print( |
|
f"proxy_request method={self.command} incoming={self.path} target={target_url}", |
|
file=sys.stderr, |
|
flush=True, |
|
) |
|
|
|
conn_cls = HTTPSConnection if parsed.scheme == "https" else HTTPConnection |
|
conn = conn_cls(parsed.hostname, parsed.port, timeout=300) |
|
|
|
upstream_path = parsed.path or "/" |
|
if parsed.query: |
|
upstream_path += "?" + parsed.query |
|
|
|
conn.request( |
|
method=self.command, |
|
url=upstream_path, |
|
body=body, |
|
headers=headers, |
|
) |
|
|
|
upstream = conn.getresponse() |
|
content_type = upstream.getheader("content-type", "") or "" |
|
|
|
print( |
|
f"upstream_status={upstream.status} {upstream.reason} path={self.path}", |
|
file=sys.stderr, |
|
flush=True, |
|
) |
|
|
|
self.send_response(upstream.status, upstream.reason) |
|
|
|
for key, value in self._response_headers(upstream, content_type): |
|
self.send_header(key, value) |
|
|
|
if self._is_sse(content_type): |
|
self.send_header("Cache-Control", "no-cache") |
|
self.send_header("Connection", "close") |
|
self.close_connection = True |
|
|
|
self.end_headers() |
|
response_started = True |
|
|
|
if self._is_sse(content_type): |
|
self._stream_and_normalize_sse(upstream) |
|
else: |
|
self._copy_response(upstream) |
|
|
|
except (BrokenPipeError, ConnectionResetError): |
|
return |
|
|
|
except Exception as exc: |
|
print(f"proxy_error={type(exc).__name__}: {exc}", file=sys.stderr, flush=True) |
|
|
|
if response_started: |
|
return |
|
|
|
try: |
|
self._send_error(exc) |
|
except (BrokenPipeError, ConnectionResetError): |
|
return |
|
|
|
finally: |
|
if conn is not None: |
|
conn.close() |
|
|
|
def _target_url(self): |
|
base = urlparse(TARGET_BASE) |
|
base_path = base.path.rstrip("/") |
|
|
|
incoming = urlparse(self.path) |
|
incoming_path = incoming.path |
|
|
|
if base_path.endswith("/v1") and incoming_path.startswith("/v1/"): |
|
incoming_path = incoming_path[len("/v1"):] |
|
|
|
if not incoming_path.startswith("/"): |
|
incoming_path = "/" + incoming_path |
|
|
|
target_path = base_path + incoming_path |
|
|
|
return urlunparse( |
|
( |
|
base.scheme, |
|
base.netloc, |
|
target_path, |
|
"", |
|
incoming.query, |
|
"", |
|
) |
|
) |
|
|
|
def _read_body(self): |
|
length = self.headers.get("content-length") |
|
if not length: |
|
return None |
|
return self.rfile.read(int(length)) |
|
|
|
def _forward_headers(self, target_host, body): |
|
headers = {} |
|
|
|
for key, value in self.headers.items(): |
|
lower = key.lower() |
|
|
|
if lower in HOP_BY_HOP_HEADERS: |
|
continue |
|
if lower == "host": |
|
continue |
|
if lower == "content-length": |
|
continue |
|
|
|
headers[key] = value |
|
|
|
headers["Host"] = target_host |
|
headers["Accept-Encoding"] = "identity" |
|
|
|
if body is not None: |
|
headers["Content-Length"] = str(len(body)) |
|
|
|
return headers |
|
|
|
def _response_headers(self, upstream, content_type): |
|
headers = [] |
|
|
|
for key, value in upstream.getheaders(): |
|
lower = key.lower() |
|
|
|
if lower in HOP_BY_HOP_HEADERS: |
|
continue |
|
if self._is_sse(content_type) and lower == "content-length": |
|
continue |
|
if lower == "content-encoding": |
|
continue |
|
|
|
headers.append((key, value)) |
|
|
|
return headers |
|
|
|
def _copy_response(self, upstream): |
|
while True: |
|
chunk = upstream.read(64 * 1024) |
|
if not chunk: |
|
break |
|
|
|
self.wfile.write(chunk) |
|
self.wfile.flush() |
|
|
|
def _stream_and_normalize_sse(self, upstream): |
|
tool_names_by_index = {} |
|
tool_names_by_id = {} |
|
event_lines = [] |
|
|
|
while True: |
|
raw = upstream.readline() |
|
|
|
if not raw: |
|
if event_lines: |
|
self._write_sse_event( |
|
event_lines, |
|
tool_names_by_index, |
|
tool_names_by_id, |
|
) |
|
break |
|
|
|
try: |
|
line = raw.decode("utf-8") |
|
except UnicodeDecodeError: |
|
self.wfile.write(raw) |
|
self.wfile.flush() |
|
continue |
|
|
|
if line in ("\n", "\r\n"): |
|
self._write_sse_event( |
|
event_lines, |
|
tool_names_by_index, |
|
tool_names_by_id, |
|
) |
|
event_lines = [] |
|
else: |
|
event_lines.append(line.rstrip("\r\n")) |
|
|
|
def _write_sse_event(self, event_lines, tool_names_by_index, tool_names_by_id): |
|
if not event_lines: |
|
return |
|
|
|
fixed_lines = [] |
|
|
|
for line in event_lines: |
|
if line.startswith("data:"): |
|
fixed_lines.append( |
|
self._fix_data_line( |
|
line, |
|
tool_names_by_index, |
|
tool_names_by_id, |
|
) |
|
) |
|
else: |
|
fixed_lines.append(line) |
|
|
|
fixed_event = "\n".join(fixed_lines) + "\n\n" |
|
|
|
self.wfile.write(fixed_event.encode("utf-8")) |
|
self.wfile.flush() |
|
|
|
def _fix_data_line(self, line, tool_names_by_index, tool_names_by_id): |
|
payload = line[len("data:"):].strip() |
|
|
|
if payload == "[DONE]": |
|
return line |
|
|
|
try: |
|
data = json.loads(payload) |
|
except json.JSONDecodeError: |
|
return line |
|
|
|
self._normalize_chunk(data, tool_names_by_index, tool_names_by_id) |
|
|
|
return "data: " + json.dumps( |
|
data, |
|
separators=(",", ":"), |
|
ensure_ascii=False, |
|
) |
|
|
|
def _normalize_chunk(self, data, tool_names_by_index, tool_names_by_id): |
|
choices = data.get("choices") or [] |
|
|
|
for choice in choices: |
|
delta = choice.get("delta") |
|
|
|
if not isinstance(delta, dict): |
|
continue |
|
|
|
# Strict-client cleanup. |
|
delta.pop("reasoning", None) |
|
delta.pop("reasoning_content", None) |
|
delta.pop("provider_specific_fields", None) |
|
|
|
tool_calls = delta.get("tool_calls") or [] |
|
|
|
for tool_call in tool_calls: |
|
if not isinstance(tool_call, dict): |
|
continue |
|
|
|
function = tool_call.get("function") |
|
|
|
if not isinstance(function, dict): |
|
continue |
|
|
|
index = tool_call.get("index", 0) |
|
tool_id = tool_call.get("id") |
|
name = function.get("name") |
|
|
|
if DEBUG: |
|
print( |
|
"tool_chunk_before=" |
|
+ json.dumps(tool_call, ensure_ascii=False, separators=(",", ":")), |
|
file=sys.stderr, |
|
flush=True, |
|
) |
|
|
|
if isinstance(name, str) and name: |
|
tool_names_by_index[index] = name |
|
|
|
if isinstance(tool_id, str) and tool_id: |
|
tool_names_by_id[tool_id] = name |
|
|
|
else: |
|
remembered = None |
|
|
|
if isinstance(tool_id, str) and tool_id: |
|
remembered = tool_names_by_id.get(tool_id) |
|
|
|
if remembered is None: |
|
remembered = tool_names_by_index.get(index) |
|
|
|
if remembered: |
|
function["name"] = remembered |
|
else: |
|
function.pop("name", None) |
|
|
|
if function.get("name") is None: |
|
function.pop("name", None) |
|
|
|
if DEBUG: |
|
print( |
|
"tool_chunk_after=" |
|
+ json.dumps(tool_call, ensure_ascii=False, separators=(",", ":")), |
|
file=sys.stderr, |
|
flush=True, |
|
) |
|
|
|
def _send_error(self, exc): |
|
body = json.dumps({"error": str(exc)}).encode("utf-8") |
|
|
|
self.send_response(502, "Bad Gateway") |
|
self.send_header("Content-Type", "application/json") |
|
self.send_header("Content-Length", str(len(body))) |
|
self.send_header("Connection", "close") |
|
self.close_connection = True |
|
self.end_headers() |
|
self.wfile.write(body) |
|
self.wfile.flush() |
|
|
|
@staticmethod |
|
def _is_sse(content_type): |
|
return "text/event-stream" in content_type.lower() |
|
|
|
|
|
def main(): |
|
server = ThreadingHTTPServer((LISTEN_HOST, LISTEN_PORT), ToolCallProxyHandler) |
|
|
|
print( |
|
f"OpenCode LiteLLM tool-call proxy listening on http://{LISTEN_HOST}:{LISTEN_PORT}", |
|
file=sys.stderr, |
|
flush=True, |
|
) |
|
print(f"Forwarding to {TARGET_BASE}", file=sys.stderr, flush=True) |
|
print("Recommended OpenCode baseURL: http://127.0.0.1:4010", file=sys.stderr, flush=True) |
|
|
|
try: |
|
server.serve_forever() |
|
except KeyboardInterrupt: |
|
print("\nStopping proxy.", file=sys.stderr, flush=True) |
|
finally: |
|
server.server_close() |
|
|
|
|
|
if __name__ == "__main__": |
|
main() |