fix: vision(#623): disinto-chat container scaffold (no auth) (#705)

This commit is contained in:
Claude 2026-04-12 00:23:54 +00:00
parent 2006125ade
commit eada673493
7 changed files with 544 additions and 2 deletions

33
docker/chat/Dockerfile Normal file
View file

@ -0,0 +1,33 @@
# disinto-chat — minimal HTTP+WebSocket backend for Claude chat UI
#
# Small Debian slim base with Python runtime and websockets library.
# Chosen for simplicity and small image size (~100MB vs ~150MB for Go).
#
# Image size: ~100MB (well under the 200MB ceiling)
#
# The claude binary is mounted from the host at runtime via docker-compose,
# not baked into the image — same pattern as the agents container.
FROM debian:bookworm-slim
# Install Python and websockets (no build-time network access needed)
RUN apt-get update && apt-get install -y --no-install-recommends \
python3 \
python3-pip \
&& pip3 install --break-system-packages websockets \
&& rm -rf /var/lib/apt/lists/*
# Non-root user
RUN useradd -m -u 1000 -s /bin/bash chat
# Copy application files
COPY server.py /usr/local/bin/server.py
COPY entrypoint-chat.sh /entrypoint-chat.sh
COPY ui/ /var/chat/ui/
RUN chmod +x /entrypoint-chat.sh /usr/local/bin/server.py
USER chat
WORKDIR /var/chat
ENTRYPOINT ["/entrypoint-chat.sh"]

Binary file not shown.

27
docker/chat/entrypoint-chat.sh Executable file
View file

@ -0,0 +1,27 @@
#!/usr/bin/env bash
set -euo pipefail
# entrypoint-chat.sh — Start the disinto-chat backend server
#
# Exec-replace pattern: this script is the container entrypoint and runs
# the server directly (no wrapper needed). Logs to stdout for docker logs.
LOGFILE="/var/chat/chat.log"
log() {
printf '[%s] %s\n' "$(date -u '+%Y-%m-%d %H:%M:%S UTC')" "$*" | tee -a "$LOGFILE"
}
# Verify Claude CLI is available (expected via volume mount from host).
if ! command -v claude &>/dev/null; then
log "FATAL: claude CLI not found in PATH"
log "Mount the host binary into the container, e.g.:"
log " volumes:"
log " - /usr/local/bin/claude:/usr/local/bin/claude:ro"
exit 1
fi
log "Claude CLI: $(claude --version 2>&1 || true)"
# Start the Python server (exec-replace so signals propagate correctly)
log "Starting disinto-chat server on port 8080..."
exec python3 /usr/local/bin/server.py

233
docker/chat/server.py Normal file
View file

@ -0,0 +1,233 @@
#!/usr/bin/env python3
"""
disinto-chat server minimal HTTP backend for Claude chat UI.
Routes:
GET / serves index.html
GET /static/* serves static assets (htmx.min.js, etc.)
POST /chat spawns `claude --print` with user message, returns response
GET /ws reserved for future streaming upgrade (returns 501)
The claude binary is expected to be mounted from the host at /usr/local/bin/claude.
"""
import os
import subprocess
import sys
from http.server import HTTPServer, BaseHTTPRequestHandler
from urllib.parse import urlparse, parse_qs
# Configuration
HOST = os.environ.get("CHAT_HOST", "0.0.0.0")
PORT = int(os.environ.get("CHAT_PORT", 8080))
UI_DIR = "/var/chat/ui"
STATIC_DIR = os.path.join(UI_DIR, "static")
CLAUDE_BIN = "/usr/local/bin/claude"
# MIME types for static files
MIME_TYPES = {
".html": "text/html; charset=utf-8",
".js": "application/javascript; charset=utf-8",
".css": "text/css; charset=utf-8",
".json": "application/json; charset=utf-8",
".png": "image/png",
".jpg": "image/jpeg",
".svg": "image/svg+xml",
".ico": "image/x-icon",
}
class ChatHandler(BaseHTTPRequestHandler):
"""HTTP request handler for disinto-chat."""
def log_message(self, format, *args):
"""Log to stdout instead of stderr."""
print(f"[{self.log_date_time_string()}] {format % args}", file=sys.stderr)
def send_error(self, code, message=None):
"""Custom error response."""
self.send_response(code)
self.send_header("Content-Type", "text/plain; charset=utf-8")
self.end_headers()
if message:
self.wfile.write(message.encode("utf-8"))
def do_GET(self):
"""Handle GET requests."""
parsed = urlparse(self.path)
path = parsed.path
# Serve index.html at root
if path == "/" or path == "/chat":
self.serve_index()
return
# Serve static files
if path.startswith("/static/"):
self.serve_static(path)
return
# Reserved WebSocket endpoint (future use)
if path == "/ws" or path.startswith("/ws"):
self.send_error(501, "WebSocket upgrade not yet implemented")
return
# 404 for unknown paths
self.send_error(404, "Not found")
def do_POST(self):
"""Handle POST requests."""
parsed = urlparse(self.path)
path = parsed.path
# Chat endpoint
if path == "/chat" or path == "/chat/":
self.handle_chat()
return
# 404 for unknown paths
self.send_error(404, "Not found")
def serve_index(self):
"""Serve the main index.html file."""
index_path = os.path.join(UI_DIR, "index.html")
if not os.path.exists(index_path):
self.send_error(500, "UI not found")
return
try:
with open(index_path, "r", encoding="utf-8") as f:
content = f.read()
self.send_response(200)
self.send_header("Content-Type", MIME_TYPES[".html"])
self.send_header("Content-Length", len(content.encode("utf-8")))
self.end_headers()
self.wfile.write(content.encode("utf-8"))
except IOError as e:
self.send_error(500, f"Error reading index.html: {e}")
def serve_static(self, path):
"""Serve static files from the static directory."""
# Sanitize path to prevent directory traversal
relative_path = path[len("/static/"):]
if ".." in relative_path or relative_path.startswith("/"):
self.send_error(403, "Forbidden")
return
file_path = os.path.join(STATIC_DIR, relative_path)
if not os.path.exists(file_path):
self.send_error(404, "Not found")
return
# Determine MIME type
_, ext = os.path.splitext(file_path)
content_type = MIME_TYPES.get(ext.lower(), "application/octet-stream")
try:
with open(file_path, "rb") as f:
content = f.read()
self.send_response(200)
self.send_header("Content-Type", content_type)
self.send_header("Content-Length", len(content))
self.end_headers()
self.wfile.write(content)
except IOError as e:
self.send_error(500, f"Error reading file: {e}")
def handle_chat(self):
"""
Handle chat requests by spawning `claude --print` with the user message.
Returns the response as plain text.
"""
# Read request body
content_length = int(self.headers.get("Content-Length", 0))
if content_length == 0:
self.send_error(400, "No message provided")
return
body = self.rfile.read(content_length)
try:
# Parse form-encoded body
body_str = body.decode("utf-8")
params = parse_qs(body_str)
message = params.get("message", [""])[0]
except (UnicodeDecodeError, KeyError):
self.send_error(400, "Invalid message format")
return
if not message:
self.send_error(400, "Empty message")
return
# Validate Claude binary exists
if not os.path.exists(CLAUDE_BIN):
self.send_error(500, "Claude CLI not found")
return
try:
# Spawn claude --print with streaming output
# Using stream-json format for structured parsing capability
proc = subprocess.Popen(
[CLAUDE_BIN, "--print", message, "--output-format", "stream-json"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=False,
bufsize=0, # Unbuffered for streaming
)
# Read and stream response
response_parts = []
error_parts = []
# Read stdout in chunks
while True:
chunk = proc.stdout.read(4096)
if not chunk:
break
try:
response_parts.append(chunk.decode("utf-8"))
except UnicodeDecodeError:
response_parts.append(chunk.decode("utf-8", errors="replace"))
# Read stderr (should be minimal, mostly for debugging)
if proc.stderr:
error_output = proc.stderr.read()
if error_output:
error_parts.append(error_output.decode("utf-8", errors="replace"))
# Wait for process to complete
proc.wait()
# Check for errors
if proc.returncode != 0:
self.send_error(500, f"Claude CLI failed with exit code {proc.returncode}")
return
# Combine response parts
response = "".join(response_parts)
# If using stream-json, we could parse and reformat here.
# For now, return as-is (HTMX will display it in the UI).
self.send_response(200)
self.send_header("Content-Type", "text/plain; charset=utf-8")
self.send_header("Content-Length", len(response.encode("utf-8")))
self.end_headers()
self.wfile.write(response.encode("utf-8"))
except FileNotFoundError:
self.send_error(500, "Claude CLI not found")
except Exception as e:
self.send_error(500, f"Error: {e}")
def main():
"""Start the HTTP server."""
server_address = (HOST, PORT)
httpd = HTTPServer(server_address, ChatHandler)
print(f"Starting disinto-chat server on {HOST}:{PORT}", file=sys.stderr)
print(f"UI available at http://localhost:{PORT}/", file=sys.stderr)
httpd.serve_forever()
if __name__ == "__main__":
main()

226
docker/chat/ui/index.html Normal file
View file

@ -0,0 +1,226 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>disinto-chat</title>
<script src="/static/htmx.min.js"></script>
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, sans-serif;
background: #1a1a2e;
color: #eaeaea;
min-height: 100vh;
display: flex;
flex-direction: column;
}
header {
background: #16213e;
padding: 1rem 2rem;
border-bottom: 1px solid #0f3460;
}
header h1 {
font-size: 1.25rem;
font-weight: 600;
}
main {
flex: 1;
display: flex;
flex-direction: column;
max-width: 900px;
margin: 0 auto;
width: 100%;
padding: 1rem;
}
#messages {
flex: 1;
overflow-y: auto;
padding: 1rem;
background: #16213e;
border-radius: 8px;
margin-bottom: 1rem;
}
.message {
margin-bottom: 1rem;
padding: 0.75rem 1rem;
border-radius: 8px;
line-height: 1.5;
}
.message.user {
background: #0f3460;
margin-left: 2rem;
}
.message.assistant {
background: #1a1a2e;
margin-right: 2rem;
}
.message.system {
background: #1a1a2e;
font-style: italic;
color: #888;
text-align: center;
}
.message .role {
font-weight: 600;
font-size: 0.875rem;
margin-bottom: 0.25rem;
opacity: 0.8;
}
.message .content {
white-space: pre-wrap;
word-wrap: break-word;
}
.input-area {
display: flex;
gap: 0.5rem;
padding: 1rem;
background: #16213e;
border-radius: 8px;
}
textarea {
flex: 1;
background: #1a1a2e;
border: 1px solid #0f3460;
border-radius: 6px;
padding: 0.75rem;
color: #eaeaea;
font-family: inherit;
font-size: 1rem;
resize: none;
min-height: 80px;
}
textarea:focus {
outline: none;
border-color: #e94560;
}
button {
background: #e94560;
color: white;
border: none;
border-radius: 6px;
padding: 0.75rem 1.5rem;
font-size: 1rem;
font-weight: 600;
cursor: pointer;
transition: background 0.2s;
}
button:hover {
background: #d63447;
}
button:disabled {
background: #555;
cursor: not-allowed;
}
.loading {
opacity: 0.6;
}
</style>
</head>
<body>
<header>
<h1>disinto-chat</h1>
</header>
<main>
<div id="messages">
<div class="message system">
<div class="role">system</div>
<div class="content">Welcome to disinto-chat. Type a message to start chatting with Claude.</div>
</div>
</div>
<form class="input-area" hx-post="/chat" hx-swap="none" hx-target="#messages">
<textarea name="message" placeholder="Type your message..." required></textarea>
<button type="submit" id="send-btn">Send</button>
</form>
</main>
<script>
const messagesDiv = document.getElementById('messages');
const sendBtn = document.getElementById('send-btn');
const textarea = document.querySelector('textarea');
function addMessage(role, content, streaming = false) {
const msgDiv = document.createElement('div');
msgDiv.className = `message ${role}`;
msgDiv.innerHTML = `
<div class="role">${role}</div>
<div class="content${streaming ? ' streaming' : ''}">${escapeHtml(content)}</div>
`;
messagesDiv.appendChild(msgDiv);
messagesDiv.scrollTop = messagesDiv.scrollHeight;
return msgDiv.querySelector('.content');
}
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML.replace(/\n/g, '<br>');
}
// Handle HTMX swap for streaming responses
document.body.addEventListener('htmx:afterSwap', function(event) {
const newContent = event.detail.xhr.responseText;
// HTMX will handle the swap; we just need to scroll to bottom
messagesDiv.scrollTop = messagesDiv.scrollHeight;
});
// Send message handler
sendBtn.addEventListener('click', async () => {
const message = textarea.value.trim();
if (!message) return;
// Disable input
textarea.disabled = true;
sendBtn.disabled = true;
sendBtn.textContent = 'Sending...';
// Add user message
addMessage('user', message);
textarea.value = '';
try {
// Use fetch for better control over streaming
const formData = new FormData();
formData.append('message', message);
const response = await fetch('/chat', {
method: 'POST',
body: formData
});
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
// Read the response as text and add assistant message
const content = await response.text();
addMessage('assistant', content);
} catch (error) {
addMessage('system', `Error: ${error.message}`);
} finally {
textarea.disabled = false;
sendBtn.disabled = false;
sendBtn.textContent = 'Send';
textarea.focus();
messagesDiv.scrollTop = messagesDiv.scrollHeight;
}
});
// Handle Enter key in textarea
textarea.addEventListener('keydown', (e) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
sendBtn.click();
}
});
// Initial focus
textarea.focus();
</script>
</body>
</html>

1
docker/chat/ui/static/htmx.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View file

@ -473,12 +473,34 @@ services:
- disinto-net
command: ["echo", "staging slot — replace with project image"]
# Chat container — Claude chat UI backend (#705)
# Internal service only; edge proxy routes to chat:8080
chat:
build:
context: ./docker/chat
dockerfile: Dockerfile
container_name: disinto-chat
restart: unless-stopped
security_opt:
- apparmor=unconfined
volumes:
# Mount claude binary from host (same as agents)
- CLAUDE_BIN_PLACEHOLDER:/usr/local/bin/claude:ro
# Throwaway named volume for chat config (isolated from host ~/.claude)
- chat-config:/var/chat/config
environment:
CHAT_HOST: "0.0.0.0"
CHAT_PORT: "8080"
networks:
- disinto-net
volumes:
forgejo-data:
woodpecker-data:
agent-data:
project-repos:
caddy_data:
chat-config:
networks:
disinto-net:
@ -574,9 +596,9 @@ _generate_caddyfile_impl() {
reverse_proxy staging:80
}
# Chat placeholder — returns 503 until #705
# Chat service — reverse proxy to disinto-chat backend (#705)
handle /chat/* {
respond "chat not yet deployed" 503
reverse_proxy chat:8080
}
}
CADDYFILEEOF