refactor(server): extract amc_server package from monolithic script

Split the 860+ line bin/amc-server into a modular Python package:

  amc_server/
    __init__.py         - Package marker
    context.py          - Shared constants (DATA_DIR, PORT, CLAUDE_PROJECTS_DIR, etc.)
    handler.py          - AMCHandler class using mixin composition
    logging_utils.py    - Structured logging setup with signal handlers
    server.py           - Main entry point (ThreadingHTTPServer)
    mixins/
      __init__.py       - Mixin package marker
      control.py        - Session control (dismiss, respond via Zellij)
      conversation.py   - Conversation history parsing (Claude JSONL format)
      discovery.py      - Session discovery (Codex pane inspection, Zellij cache)
      http.py           - HTTP response helpers (CORS, JSON, static files)
      parsing.py        - Session state parsing and aggregation
      state.py          - Session state endpoint logic

The monolithic bin/amc-server becomes a thin launcher that just imports
and calls main(). This separation enables:

- Easier testing of individual components
- Better IDE support (proper Python package structure)
- Cleaner separation of concerns (discovery vs parsing vs control)
- ThreadingHTTPServer instead of single-threaded (handles concurrent requests)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
teernisse
2026-02-25 14:02:00 -05:00
parent e718c44555
commit a7b2b3b902
13 changed files with 1437 additions and 854 deletions

140
amc_server/mixins/http.py Normal file
View File

@@ -0,0 +1,140 @@
import json
import urllib.parse
from amc_server.context import DASHBOARD_DIR
from amc_server.logging_utils import LOGGER
class HttpMixin:
def _send_bytes_response(self, code, content, content_type="application/json", extra_headers=None):
"""Send a generic byte response; ignore expected disconnect errors."""
try:
self.send_response(code)
self.send_header("Content-Type", content_type)
if extra_headers:
for key, value in extra_headers.items():
self.send_header(key, value)
self.send_header("Content-Length", str(len(content)))
self.end_headers()
self.wfile.write(content)
return True
except (BrokenPipeError, ConnectionResetError, OSError):
return False
def _send_json(self, code, payload):
"""Send JSON response with CORS header."""
content = json.dumps(payload).encode()
return self._send_bytes_response(
code,
content,
content_type="application/json",
extra_headers={"Access-Control-Allow-Origin": "*"},
)
def do_GET(self):
try:
if self.path == "/" or self.path == "/index.html":
self._serve_dashboard_file("index.html")
elif self.path.startswith("/") and not self.path.startswith("/api/"):
# Serve static files from dashboard directory
file_path = self.path.lstrip("/")
if file_path and ".." not in file_path:
self._serve_dashboard_file(file_path)
else:
self._json_error(404, "Not Found")
elif self.path == "/api/state":
self._serve_state()
elif self.path == "/api/stream":
self._serve_stream()
elif self.path.startswith("/api/events/"):
session_id = urllib.parse.unquote(self.path[len("/api/events/"):])
self._serve_events(session_id)
elif self.path.startswith("/api/conversation/"):
# Parse session_id and query params
path_part = self.path[len("/api/conversation/"):]
if "?" in path_part:
session_id, query = path_part.split("?", 1)
params = urllib.parse.parse_qs(query)
project_dir = params.get("project_dir", [""])[0]
agent = params.get("agent", ["claude"])[0]
else:
session_id = path_part
project_dir = ""
agent = "claude"
self._serve_conversation(urllib.parse.unquote(session_id), urllib.parse.unquote(project_dir), agent)
else:
self._json_error(404, "Not Found")
except Exception:
LOGGER.exception("Unhandled GET error for path=%s", self.path)
try:
self._json_error(500, "Internal Server Error")
except Exception:
pass
def do_POST(self):
try:
if self.path.startswith("/api/dismiss/"):
session_id = urllib.parse.unquote(self.path[len("/api/dismiss/"):])
self._dismiss_session(session_id)
elif self.path.startswith("/api/respond/"):
session_id = urllib.parse.unquote(self.path[len("/api/respond/"):])
self._respond_to_session(session_id)
else:
self._json_error(404, "Not Found")
except Exception:
LOGGER.exception("Unhandled POST error for path=%s", self.path)
try:
self._json_error(500, "Internal Server Error")
except Exception:
pass
def do_OPTIONS(self):
# CORS preflight for respond endpoint
self.send_response(204)
self.send_header("Access-Control-Allow-Origin", "*")
self.send_header("Access-Control-Allow-Methods", "POST, OPTIONS")
self.send_header("Access-Control-Allow-Headers", "Content-Type")
self.end_headers()
def _serve_dashboard_file(self, file_path):
"""Serve a static file from the dashboard directory."""
# Content type mapping
content_types = {
".html": "text/html; charset=utf-8",
".css": "text/css; charset=utf-8",
".js": "application/javascript; charset=utf-8",
".json": "application/json; charset=utf-8",
".svg": "image/svg+xml",
".png": "image/png",
".ico": "image/x-icon",
}
try:
full_path = DASHBOARD_DIR / file_path
# Security: ensure path doesn't escape dashboard directory
full_path = full_path.resolve()
if not str(full_path).startswith(str(DASHBOARD_DIR.resolve())):
self._json_error(403, "Forbidden")
return
content = full_path.read_bytes()
ext = full_path.suffix.lower()
content_type = content_types.get(ext, "application/octet-stream")
# No caching during development
self._send_bytes_response(
200,
content,
content_type=content_type,
extra_headers={"Cache-Control": "no-cache, no-store, must-revalidate"},
)
except FileNotFoundError:
self._json_error(404, f"File not found: {file_path}")
def _json_error(self, code, message):
"""Send a JSON error response."""
self._send_json(code, {"ok": False, "error": message})
def log_message(self, format, *args):
"""Suppress default request logging to keep output clean."""
pass