From 936a6149578f4cb264206728b2dc8d0dab9a4c03 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Sat, 6 Jun 2026 00:16:13 -0400 Subject: [PATCH] refac --- .dockerignore | 17 ++ CHANGELOG.md | 19 ++ Dockerfile | 55 ++++ cptr/app.py | 8 + .../src/lib/components/FileBrowser.svelte | 260 ++++++++++++----- cptr/routers/proxy.py | 273 ++++++++++++++---- cptr/utils/proxy_middleware.py | 195 +++++++++++++ 7 files changed, 698 insertions(+), 129 deletions(-) create mode 100644 .dockerignore create mode 100644 CHANGELOG.md create mode 100644 Dockerfile create mode 100644 cptr/utils/proxy_middleware.py diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..bb28d6f --- /dev/null +++ b/.dockerignore @@ -0,0 +1,17 @@ +.git +.github +.venv +.cptr +.claude +__pycache__ +*.pyc +*.egg-info +dist/ +build/ +cptr/frontend/node_modules/ +cptr/frontend/.svelte-kit/ +cptr/frontend/build/ +*.md +LICENSE +CONTRIBUTOR_LICENSE_AGREEMENT +.DS_Store diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..1f58d93 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,19 @@ +# Changelog + +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [0.0.1] - 2026-06-05 + +### Added + +- 🚀 **Initial release.** First public version of cptr: your computer, from anywhere. Code, manage, and control your machine from the web. +- 🖥️ **Terminal emulator.** Full PTY-backed terminal accessible from the browser with support for macOS and Linux. +- 💬 **AI chat interface.** Built-in chat panel with multi-provider LLM support (OpenAI, Anthropic, Ollama, and OpenAI-compatible endpoints), tool calling, and streaming responses. +- 🔧 **Tool system.** Extensible tool framework enabling AI agents to interact with the local filesystem, run commands, search the web, and read URLs. +- 📁 **File browser.** Web-based file explorer with directory navigation, file viewing, and management capabilities. +- 🔐 **Authentication.** Username/password authentication with JWT-based session management. +- 🐳 **Docker support.** Multi-stage Dockerfile and GitHub Actions workflow for building and publishing to GHCR. +- 📦 **PyPI packaging.** Hatchling-based build with frontend assets bundled into the wheel, published via trusted OIDC publishing. \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..c27eedd --- /dev/null +++ b/Dockerfile @@ -0,0 +1,55 @@ +# ── Stage 1: Build the SvelteKit frontend ────────────────── +FROM node:22-slim AS frontend-builder + +WORKDIR /build/frontend +COPY cptr/frontend/package.json cptr/frontend/package-lock.json ./ +RUN npm ci +COPY cptr/frontend/ ./ +RUN npm run build + + +# ── Stage 2: Install Python dependencies & build wheel ───── +FROM ghcr.io/astral-sh/uv:python3.12-bookworm-slim AS backend-builder + +WORKDIR /build +COPY pyproject.toml uv.lock ./ +COPY cptr/ cptr/ + +# Drop the pre-built frontend into the package tree +COPY --from=frontend-builder /build/frontend/build cptr/frontend/build + +# Build the wheel (includes frontend build as an artifact via hatch) +RUN uv build --wheel --out-dir /dist + + +# ── Stage 3: Minimal runtime image ───────────────────────── +FROM ghcr.io/astral-sh/uv:python3.12-bookworm-slim AS runtime + +LABEL org.opencontainers.image.source="https://github.com/open-webui/computer" +LABEL org.opencontainers.image.description="cptr - your computer, from anywhere" +LABEL org.opencontainers.image.licenses="BSL-1.1" + +# Runtime deps: git for git operations, tini for PID 1 +RUN apt-get update && \ + apt-get install -y --no-install-recommends git tini && \ + rm -rf /var/lib/apt/lists/* + +# Create non-root user +RUN useradd --create-home --shell /bin/bash cptr +USER cptr +WORKDIR /home/cptr + +# Install the wheel into an isolated venv +COPY --from=backend-builder /dist/*.whl /tmp/ +RUN uv venv /home/cptr/.venv && \ + uv pip install --python /home/cptr/.venv/bin/python /tmp/*.whl && \ + rm /tmp/*.whl + +ENV PATH="/home/cptr/.venv/bin:$PATH" +ENV CPTR_DATA_DIR="/data" + +EXPOSE 8000 +VOLUME ["/data"] + +ENTRYPOINT ["tini", "--"] +CMD ["cptr", "run", "--host", "0.0.0.0", "--port", "8000", "--headless"] diff --git a/cptr/app.py b/cptr/app.py index ef64bc2..b17855d 100644 --- a/cptr/app.py +++ b/cptr/app.py @@ -63,6 +63,14 @@ async def auth_middleware(request: Request, call_next): return await call_next(request) +# Proxy fallback middleware: intercepts sub-resource requests for proxied +# dev servers (JS modules, CSS, etc.) based on the Referer header chain. +# Added after auth middleware so it wraps outermost (runs before auth), +# since proxied sub-resources don't need cptr auth. +from cptr.utils.proxy_middleware import ProxyFallbackMiddleware +app.add_middleware(ProxyFallbackMiddleware) + + # Path normalization middleware (Windows: \ → / in JSON responses) import platform if platform.system() == "Windows": diff --git a/cptr/frontend/src/lib/components/FileBrowser.svelte b/cptr/frontend/src/lib/components/FileBrowser.svelte index b54a4ee..4c804ac 100644 --- a/cptr/frontend/src/lib/components/FileBrowser.svelte +++ b/cptr/frontend/src/lib/components/FileBrowser.svelte @@ -1,3 +1,9 @@ + +