feat: 导入 MemRelay 初始源码

This commit is contained in:
Nixevol
2026-09-23 21:40:46 +08:00
commit 0ade3bb318
167 changed files with 53511 additions and 0 deletions
+84
View File
@@ -0,0 +1,84 @@
FROM python:3.12-slim-bookworm
ARG UID=1000
ARG GID=1000
ARG BUILD_HTTP_PROXY=""
ARG BUILD_HTTPS_PROXY=""
ARG BUILD_ALL_PROXY=""
ARG BUILD_APT_MIRROR=""
COPY --from=ghcr.io/astral-sh/uv:0.8.4 /uv /uvx /bin/
ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
UV_PROJECT_ENVIRONMENT=/opt/venv \
UV_DYNAMIC_VERSIONING_BYPASS=0.22.1 \
BASIC_MEMORY_HOME=/data/basic-memory/memories \
BASIC_MEMORY_CONFIG_DIR=/data/basic-memory/config \
BASIC_MEMORY_PROJECT_ROOT=/data/basic-memory/memories \
BASIC_MEMORY_SEMANTIC_SEARCH_ENABLED=true \
BASIC_MEMORY_SEMANTIC_EMBEDDING_PROVIDER=fastembed \
BASIC_MEMORY_SEMANTIC_EMBEDDING_MODEL=sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2 \
BASIC_MEMORY_SEMANTIC_EMBEDDING_CACHE_DIR=/data/basic-memory/cache \
BASIC_MEMORY_AUTO_UPDATE=false \
HTTP_PROXY="" \
HTTPS_PROXY="" \
ALL_PROXY="" \
http_proxy="" \
https_proxy="" \
all_proxy="" \
PATH=/opt/venv/bin:$PATH
WORKDIR /app
COPY third_party/basic-memory/pyproject.toml third_party/basic-memory/uv.lock ./
COPY third_party/basic-memory/src ./src
COPY third_party/basic-memory/README.md ./
RUN HTTP_PROXY="$BUILD_HTTP_PROXY" \
HTTPS_PROXY="$BUILD_HTTPS_PROXY" \
ALL_PROXY="$BUILD_ALL_PROXY" \
http_proxy="$BUILD_HTTP_PROXY" \
https_proxy="$BUILD_HTTPS_PROXY" \
all_proxy="$BUILD_ALL_PROXY" \
uv sync --locked --no-dev --python /usr/local/bin/python
COPY scripts/preload_basic_memory_model.py /usr/local/lib/memrelay/preload_basic_memory_model.py
RUN HTTP_PROXY="$BUILD_HTTP_PROXY" \
HTTPS_PROXY="$BUILD_HTTPS_PROXY" \
ALL_PROXY="$BUILD_ALL_PROXY" \
http_proxy="$BUILD_HTTP_PROXY" \
https_proxy="$BUILD_HTTPS_PROXY" \
all_proxy="$BUILD_ALL_PROXY" \
python /usr/local/lib/memrelay/preload_basic_memory_model.py \
--cache-dir /opt/basic-memory-model \
--threads 2
COPY deploy/basic-memory/entrypoint.sh /usr/local/bin/basic-memory-entrypoint
RUN HTTP_PROXY="$BUILD_HTTP_PROXY" \
HTTPS_PROXY="$BUILD_HTTPS_PROXY" \
ALL_PROXY="$BUILD_ALL_PROXY" \
http_proxy="$BUILD_HTTP_PROXY" \
https_proxy="$BUILD_HTTPS_PROXY" \
all_proxy="$BUILD_ALL_PROXY" \
sh -c 'if [ -n "$BUILD_APT_MIRROR" ]; then sed -i "s|http://deb.debian.org|${BUILD_APT_MIRROR%/}|g" /etc/apt/sources.list.d/debian.sources; fi' \
&& \
groupadd --gid "$GID" appgroup \
&& useradd --uid "$UID" --gid "$GID" --create-home --shell /usr/sbin/nologin appuser \
&& apt-get update \
&& apt-get install --yes --no-install-recommends gosu \
&& rm -rf /var/lib/apt/lists/* \
&& mkdir -p /data/basic-memory /opt/basic-memory-model \
&& chmod +x /usr/local/bin/basic-memory-entrypoint \
&& chown -R appuser:appgroup /data/basic-memory /opt/basic-memory-model
USER root
EXPOSE 8000
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
CMD python -c "import socket; socket.create_connection(('127.0.0.1', 8000), 5).close()" || exit 1
ENTRYPOINT ["basic-memory-entrypoint"]
CMD ["basic-memory", "mcp", "--transport", "streamable-http", "--host", "0.0.0.0", "--port", "8000", "--path", "/mcp"]
+13
View File
@@ -0,0 +1,13 @@
#!/bin/sh
set -eu
cache_dir="${BASIC_MEMORY_SEMANTIC_EMBEDDING_CACHE_DIR:-/data/basic-memory/cache}"
mkdir -p "$cache_dir"
if [ -z "$(find "$cache_dir" -mindepth 1 -maxdepth 1 -print -quit)" ]; then
cp -R /opt/basic-memory-model/. "$cache_dir"/
fi
chown -R appuser:appgroup /data/basic-memory
exec gosu appuser "$@"
+122
View File
@@ -0,0 +1,122 @@
FROM node:22-bookworm-slim AS frontend-builder
ARG BUILD_HTTP_PROXY=""
ARG BUILD_HTTPS_PROXY=""
ARG BUILD_ALL_PROXY=""
ENV HTTP_PROXY=${BUILD_HTTP_PROXY} \
HTTPS_PROXY=${BUILD_HTTPS_PROXY} \
ALL_PROXY=${BUILD_ALL_PROXY} \
http_proxy=${BUILD_HTTP_PROXY} \
https_proxy=${BUILD_HTTPS_PROXY} \
all_proxy=${BUILD_ALL_PROXY}
WORKDIR /build/frontend
RUN npm install --global pnpm@10.28.0
COPY frontend/package.json frontend/pnpm-lock.yaml frontend/pnpm-workspace.yaml ./
RUN pnpm install --frozen-lockfile
COPY frontend/ ./
RUN pnpm build
FROM node:22-bookworm-slim AS bitwarden-builder
ARG BUILD_HTTP_PROXY=""
ARG BUILD_HTTPS_PROXY=""
ARG BUILD_ALL_PROXY=""
ENV HTTP_PROXY=${BUILD_HTTP_PROXY} \
HTTPS_PROXY=${BUILD_HTTPS_PROXY} \
ALL_PROXY=${BUILD_ALL_PROXY} \
http_proxy=${BUILD_HTTP_PROXY} \
https_proxy=${BUILD_HTTPS_PROXY} \
all_proxy=${BUILD_ALL_PROXY}
RUN npm install --global --omit=dev @bitwarden/cli@2026.7.0
FROM ghcr.io/astral-sh/uv:0.8.4-python3.12-bookworm-slim AS backend-builder
ARG BUILD_HTTP_PROXY=""
ARG BUILD_HTTPS_PROXY=""
ARG BUILD_ALL_PROXY=""
ENV UV_LINK_MODE=copy \
UV_PROJECT_ENVIRONMENT=/opt/venv \
HTTP_PROXY=${BUILD_HTTP_PROXY} \
HTTPS_PROXY=${BUILD_HTTPS_PROXY} \
ALL_PROXY=${BUILD_ALL_PROXY} \
http_proxy=${BUILD_HTTP_PROXY} \
https_proxy=${BUILD_HTTPS_PROXY} \
all_proxy=${BUILD_ALL_PROXY}
WORKDIR /build/backend
COPY backend/pyproject.toml backend/uv.lock backend/README.md ./
COPY backend/src ./src
RUN uv sync --locked --no-dev --no-editable
FROM python:3.12-slim-bookworm
ARG UID=1000
ARG GID=1000
ARG BUILD_HTTP_PROXY=""
ARG BUILD_HTTPS_PROXY=""
ARG BUILD_ALL_PROXY=""
ARG BUILD_APT_MIRROR=""
ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
PATH=/opt/venv/bin:$PATH \
MEMRELAY_DATA_DIR=/data/memrelay \
MEMRELAY_FRONTEND_DIR=/app/frontend \
BITWARDENCLI_APPDATA_DIR=/data/memrelay/bitwarden \
HTTP_PROXY="" \
HTTPS_PROXY="" \
ALL_PROXY="" \
http_proxy="" \
https_proxy="" \
all_proxy="" \
NO_PROXY="basic-memory,localhost,127.0.0.1"
RUN HTTP_PROXY="$BUILD_HTTP_PROXY" \
HTTPS_PROXY="$BUILD_HTTPS_PROXY" \
ALL_PROXY="$BUILD_ALL_PROXY" \
http_proxy="$BUILD_HTTP_PROXY" \
https_proxy="$BUILD_HTTPS_PROXY" \
all_proxy="$BUILD_ALL_PROXY" \
sh -c 'if [ -n "$BUILD_APT_MIRROR" ]; then sed -i "s|http://deb.debian.org|${BUILD_APT_MIRROR%/}|g" /etc/apt/sources.list.d/debian.sources; fi' \
&& \
apt-get update \
&& apt-get install --yes --no-install-recommends \
ca-certificates \
git \
gosu \
libstdc++6 \
openssh-client \
poppler-utils \
tesseract-ocr \
tesseract-ocr-chi-sim \
&& rm -rf /var/lib/apt/lists/* \
&& groupadd --gid "$GID" appgroup \
&& useradd --uid "$UID" --gid "$GID" --create-home --shell /usr/sbin/nologin appuser \
&& mkdir -p /app/backend /app/frontend /data/memrelay \
&& chown -R appuser:appgroup /app /data
COPY --from=backend-builder /opt/venv /opt/venv
COPY backend/alembic.ini /app/backend/alembic.ini
COPY backend/migrations /app/backend/migrations
COPY --from=frontend-builder /build/frontend/dist /app/frontend
COPY --from=bitwarden-builder /usr/local/bin/node /usr/local/bin/node
COPY --from=bitwarden-builder /usr/local/lib/node_modules/@bitwarden/cli /usr/local/lib/node_modules/@bitwarden/cli
COPY deploy/memrelay/entrypoint.sh /usr/local/bin/memrelay-entrypoint
RUN ln -s /usr/local/lib/node_modules/@bitwarden/cli/build/bw.js /usr/local/bin/bw \
&& chmod +x /usr/local/bin/memrelay-entrypoint
WORKDIR /app/backend
USER root
EXPOSE 8080
HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 \
CMD python -c "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/api/v1/health/ready', timeout=3).read()" || exit 1
ENTRYPOINT ["memrelay-entrypoint"]
CMD ["uvicorn", "memrelay.app:app", "--host", "0.0.0.0", "--port", "8080", "--proxy-headers", "--forwarded-allow-ips", "*"]
+8
View File
@@ -0,0 +1,8 @@
#!/bin/sh
set -eu
mkdir -p /data/memrelay
chown -R appuser:appgroup /data/memrelay
exec gosu appuser "$@"
+25
View File
@@ -0,0 +1,25 @@
location ^~ /assets/ {
proxy_pass http://127.0.0.1:52001;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header Connection "";
proxy_http_version 1.1;
proxy_cache memrelay_assets;
proxy_cache_methods GET HEAD;
proxy_cache_valid 200 30d;
proxy_cache_lock on;
proxy_cache_lock_timeout 120s;
proxy_cache_lock_age 120s;
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
proxy_cache_background_update on;
proxy_ignore_headers Expires Cache-Control Set-Cookie;
proxy_hide_header Set-Cookie;
add_header X-Cache $upstream_cache_status always;
add_header Cache-Control "public, max-age=31536000, immutable" always;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
}
+6
View File
@@ -0,0 +1,6 @@
proxy_cache_path /www/sites/memrelay/cache
levels=1:2
keys_zone=memrelay_assets:10m
max_size=256m
inactive=30d
use_temp_path=off;