Merge dev into main #19

Merged
jimmy merged 4 commits from dev into main 2026-03-03 00:18:15 +08:00
27 changed files with 1294 additions and 4 deletions
+107
View File
@@ -0,0 +1,107 @@
name: Deploy Claude Dev (Dev)
on:
push:
branches: [dev]
paths:
- 'claude-dev/**'
- '.gitea/workflows/deploy-claude-dev-dev.yml'
workflow_dispatch:
inputs:
rollback_image:
description: 'Optional full image ref (for rollback), e.g. claude-dev:dev-abc123'
required: false
jobs:
deploy-claude-dev-dev:
runs-on: ubuntu-latest
container:
volumes:
- /var/packages/ContainerManager/target/usr/bin/docker:/usr/bin/docker
- /volume1/docker/claude-dev:/claude-dev-runtime
steps:
- name: Checkout dev branch
run: git clone --depth 1 --branch dev http://gitea:3000/jimmy/nas-tools.git .
- name: Resolve image tags
run: |
set -euo pipefail
if [ -n "${{ inputs.rollback_image }}" ]; then
image_ref="${{ inputs.rollback_image }}"
image_tag="${image_ref#claude-dev:}"
if [ "$image_ref" = "$image_tag" ]; then
echo "rollback_image must be in claude-dev:<tag> format" >&2
exit 1
fi
echo "ROLLBACK_ONLY=1" >> "$GITHUB_ENV"
else
short_sha="$(git rev-parse --short=12 HEAD)"
date_tag="$(date +%Y%m%d%H%M%S)"
image_tag="dev-${date_tag}-${short_sha}"
image_ref="claude-dev:${image_tag}"
echo "ROLLBACK_ONLY=0" >> "$GITHUB_ENV"
fi
echo "CLAUDE_DEV_IMAGE_TAG=${image_tag}" >> "$GITHUB_ENV"
echo "CLAUDE_DEV_IMAGE=${image_ref}" >> "$GITHUB_ENV"
- name: Build claude-dev image
if: env.ROLLBACK_ONLY == '0'
run: |
set -euo pipefail
DOCKER_BUILDKIT=0 docker build \
--cache-from claude-dev:latest \
-t "$CLAUDE_DEV_IMAGE" \
-t claude-dev:latest \
claude-dev/
- name: Smoke test required tools
if: env.ROLLBACK_ONLY == '0'
run: |
set -euo pipefail
docker run --rm "$CLAUDE_DEV_IMAGE" bash /opt/claude-dev/scripts/smoke-tools.sh
- name: Smoke test network assumptions
if: env.ROLLBACK_ONLY == '0'
run: |
set -euo pipefail
docker run --rm --network host "$CLAUDE_DEV_IMAGE" bash /opt/claude-dev/scripts/smoke-network.sh
- name: Smoke test auth and mount expectations
if: env.ROLLBACK_ONLY == '0'
run: |
set -euo pipefail
docker run --rm --network host \
-e REQUIRE_MOUNTS=1 \
-v /volume1/docker/claude-dev/claude-config:/root/.claude \
-v /volume1/docker/claude-dev/claude-config/.claude.json:/root/.claude.json \
-v /volume1/docker/claude-dev/gitea_token:/root/.gitea_token:ro \
"$CLAUDE_DEV_IMAGE" \
bash /opt/claude-dev/scripts/smoke-auth.sh
- name: Record previous image
run: |
set -euo pipefail
previous_image="$(docker inspect -f '{{.Config.Image}}' claude-dev 2>/dev/null || true)"
if [ -n "$previous_image" ]; then
printf '%s\n' "$previous_image" > /claude-dev-runtime/previous-image.txt
echo "Saved previous image: $previous_image"
else
echo "No existing claude-dev container found"
fi
- name: Sync compose and target tag
run: |
set -euo pipefail
cp claude-dev/docker-compose.yml /claude-dev-runtime/docker-compose.yml
printf '%s\n' "$CLAUDE_DEV_IMAGE_TAG" > /claude-dev-runtime/target-tag.txt
- name: Deploy claude-dev
run: |
set -euo pipefail
CLAUDE_DEV_IMAGE_TAG="$CLAUDE_DEV_IMAGE_TAG" docker compose -f /claude-dev-runtime/docker-compose.yml up -d --pull never
- name: Post-deploy runtime checks
run: |
set -euo pipefail
docker exec claude-dev bash -lc 'claude --version'
docker exec claude-dev bash -lc 'gh --version'
docker exec claude-dev bash -lc 'gh auth status || true'
+14 -1
View File
@@ -17,7 +17,20 @@
- Build images on Mac: `docker build --platform linux/amd64` - Build images on Mac: `docker build --platform linux/amd64`
- Transfer: `docker save <image> | ssh zjgump@100.78.131.124 "/volume1/@appstore/ContainerManager/usr/bin/docker load"` - Transfer: `docker save <image> | ssh zjgump@100.78.131.124 "/volume1/@appstore/ContainerManager/usr/bin/docker load"`
- Deploy: `git push` triggers Gitea Actions CI (`docker compose up -d --pull never`) — do NOT manually restart - Deploy: `git push` triggers Gitea Actions CI (`docker compose up -d --pull never`) — do NOT manually restart
- CI workflow `deploy.yml` is stored in Gitea, not in this repo - CI workflows are in `.gitea/workflows/` in this repo
## claude-dev CI Contract
- Source package lives in `claude-dev/` (`Dockerfile`, `required-tools.txt`, and smoke scripts under `claude-dev/scripts/`)
- CI-first only: update `claude-dev/*` in git, then push; do not do ad-hoc local image rebuild/redeploy for normal changes
- Required capabilities are declared in `claude-dev/required-tools.txt`; adding tools must update this file and related smoke checks
- Build/deploy gate (`.gitea/workflows/deploy-claude-dev-dev.yml`) uses NAS runner with `DOCKER_BUILDKIT=0`, then runs:
- tools smoke (`smoke-tools.sh`)
- network smoke (`smoke-network.sh`)
- auth/mount smoke (`smoke-auth.sh` with required runtime mounts)
- Deployment only happens after smoke pass, using immutable tag format `claude-dev:dev-<timestamp>-<sha>`
- Runtime compose definition is `claude-dev/docker-compose.yml`; CI syncs it to `/volume1/docker/claude-dev/docker-compose.yml`
- Runtime requirements preserved: `network_mode: host`, `/volume1/repos` mount, `/root/.claude` mount, `/root/.claude.json` mount, `/root/.gitea_token` mount
- Rollback: run workflow manually with `rollback_image` (for example the value in `/volume1/docker/claude-dev/previous-image.txt`) to redeploy a known-good immutable tag via CI
## Synology Quirks ## Synology Quirks
- No `crontab` — edit `/etc/crontab` with `sudo` + `printf`, restart crond - No `crontab` — edit `/etc/crontab` with `sudo` + `printf`, restart crond
+2 -1
View File
@@ -176,11 +176,12 @@ nas-tools/
### Phase 14 — Hybrid AI Gateway (LiteLLM + cc-connect) 🚧 ### Phase 14 — Hybrid AI Gateway (LiteLLM + cc-connect) 🚧
49. ~~Add `litellm/` service scaffold (`docker-compose.yml`, `config/litellm.yaml`, `.env.example`) with localhost-only host binding and env-driven provider keys~~ — Done 49. ~~Add `litellm/` service scaffold (`docker-compose.yml`, `config/litellm.yaml`, `.env.example`) with localhost-only host binding and env-driven provider keys~~ — Done
50. ~~Add dashboard backend LiteLLM operational probe (`/api/litellm/health`) and wire into protected routes~~ — Done 50. ~~Add dashboard backend LiteLLM operational probe (`/api/litellm/health`) and wire into protected routes~~ — Done
51. Add `cc-connect/` templates (`README.md`, `config.example.toml`, optional compose) for mobile bridge rollout 51. ~~Add `cc-connect/` templates (`README.md`, `config.example.toml`, optional compose) for mobile bridge rollout~~ — Done
52. ~~Deploy LiteLLM image to NAS using server2 pull + stream load workflow to avoid slow GHCR pulls on NAS~~ — Done 52. ~~Deploy LiteLLM image to NAS using server2 pull + stream load workflow to avoid slow GHCR pulls on NAS~~ — Done
53. ~~Network fix: attach LiteLLM to `nas-dashboard_internal` and set dashboard-dev `LITELLM_URL=http://litellm:4005`~~ — Done 53. ~~Network fix: attach LiteLLM to `nas-dashboard_internal` and set dashboard-dev `LITELLM_URL=http://litellm:4005`~~ — Done
54. ~~Update backend health probe to support auth-required mode and configured health auth key~~ — Done 54. ~~Update backend health probe to support auth-required mode and configured health auth key~~ — Done
55. ~~Add LiteLLM dashboard UI page with sidebar link and health status card~~ — Done 55. ~~Add LiteLLM dashboard UI page with sidebar link and health status card~~ — Done
56. ~~Add cc-connect dashboard UI page, sidebar link, and backend health/status endpoint~~ — Done
### Phase 13 — Off-site Backup ### Phase 13 — Off-site Backup
46. Add cloud backup (OneDrive or Google Drive) for critical data 46. Add cloud backup (OneDrive or Google Drive) for critical data
+60
View File
@@ -0,0 +1,60 @@
FROM node:20-bookworm
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
bash \
ca-certificates \
curl \
fd-find \
gh \
git \
jq \
make \
openssh-client \
python3 \
ripgrep \
rsync \
unzip \
wget \
zip \
&& rm -rf /var/lib/apt/lists/*
RUN ln -sf /usr/bin/fdfind /usr/local/bin/fd
ARG YQ_VERSION=v4.44.6
RUN wget -q -O /usr/local/bin/yq "https://github.com/mikefarah/yq/releases/download/${YQ_VERSION}/yq_linux_amd64" \
&& chmod +x /usr/local/bin/yq
RUN npm install -g @anthropic-ai/claude-code
RUN python3 - <<'PY'
from pathlib import Path
path = Path("/usr/local/lib/node_modules/@anthropic-ai/claude-code/cli.js")
if not path.exists():
raise SystemExit(f"claude cli not found: {path}")
content = path.read_text()
old = 'F8.get("https://api.anthropic.com/api/hello",{headers:K8($)}).catch((()=>!1))'
if old not in content:
raise SystemExit("preflight patch target not found in claude cli; refusing to build")
patched = content.replace(old, "!0")
path.write_text(patched)
if old in path.read_text():
raise SystemExit("preflight patch did not apply cleanly")
print("Applied Claude preflight bypass patch")
PY
WORKDIR /repos/nas-tools
COPY required-tools.txt /opt/claude-dev/required-tools.txt
COPY scripts /opt/claude-dev/scripts
RUN chmod +x /opt/claude-dev/scripts/*.sh
CMD ["bash"]
+15
View File
@@ -0,0 +1,15 @@
services:
claude-dev:
image: claude-dev:${CLAUDE_DEV_IMAGE_TAG:?CLAUDE_DEV_IMAGE_TAG is required}
container_name: claude-dev
restart: unless-stopped
network_mode: host
stdin_open: true
tty: true
working_dir: /repos/nas-tools
volumes:
- /volume1/repos:/repos
- /volume1/docker/claude-dev/claude-config:/root/.claude
- /volume1/docker/claude-dev/claude-config/.claude.json:/root/.claude.json
- /volume1/docker/claude-dev/gitea_token:/root/.gitea_token:ro
- /volume1/docker/claude-dev/bashrc:/root/.bashrc
+16
View File
@@ -0,0 +1,16 @@
gh
git
jq
yq
curl
wget
python3
node
npm
rg
fd
rsync
zip
unzip
make
claude
+60
View File
@@ -0,0 +1,60 @@
#!/usr/bin/env bash
set -euo pipefail
require_mounts="${REQUIRE_MOUNTS:-0}"
auth_ok=true
check_required_file() {
local path="$1"
if [[ ! -e "$path" ]]; then
echo "Missing required mounted path: $path" >&2
auth_ok=false
return
fi
if [[ ! -r "$path" ]]; then
echo "Path is not readable: $path" >&2
auth_ok=false
return
fi
if [[ -f "$path" ]] && [[ ! -s "$path" ]]; then
echo "Path is empty: $path" >&2
auth_ok=false
return
fi
echo "Verified mounted path: $path"
}
if [[ "$require_mounts" == "1" ]]; then
check_required_file "/root/.claude"
check_required_file "/root/.claude.json"
check_required_file "/root/.gitea_token"
if [[ -f "/root/.claude/.git-credentials" ]]; then
echo "Verified mounted path: /root/.claude/.git-credentials"
else
echo "Missing expected git credentials file: /root/.claude/.git-credentials" >&2
auth_ok=false
fi
fi
if [[ -n "${GH_TOKEN:-}" ]]; then
if GH_TOKEN="$GH_TOKEN" gh auth status >/dev/null 2>&1; then
echo "gh auth status succeeded with GH_TOKEN"
else
echo "gh auth status failed with GH_TOKEN provided" >&2
auth_ok=false
fi
else
if gh auth status >/dev/null 2>&1; then
echo "gh auth status succeeded using persisted auth"
else
echo "gh auth status not configured (allowed without GH_TOKEN)"
fi
fi
if [[ "$auth_ok" != "true" ]]; then
exit 1
fi
echo "Auth/config smoke checks passed"
+27
View File
@@ -0,0 +1,27 @@
#!/usr/bin/env bash
set -euo pipefail
check_http_status() {
local url="$1"
local allowed_regex="$2"
local code
code=$(curl -sS -o /dev/null -m 10 -w "%{http_code}" "$url")
if [[ ! "$code" =~ $allowed_regex ]]; then
echo "Unexpected HTTP status from $url: $code" >&2
return 1
fi
echo "$url => HTTP $code"
}
if ! getent hosts api.bytecatcode.org >/dev/null 2>&1; then
echo "DNS lookup failed for api.bytecatcode.org" >&2
exit 1
fi
echo "DNS lookup passed for api.bytecatcode.org"
check_http_status "https://api.bytecatcode.org" '^(200|301|302|400|401|403|404)$'
check_http_status "http://127.0.0.1:3300" '^(200|301|302|401|403|404)$'
echo "Network smoke checks passed"
+26
View File
@@ -0,0 +1,26 @@
#!/usr/bin/env bash
set -euo pipefail
TOOLS_FILE="${1:-/opt/claude-dev/required-tools.txt}"
if [[ ! -f "$TOOLS_FILE" ]]; then
echo "required tools file not found: $TOOLS_FILE" >&2
exit 1
fi
missing=()
while IFS= read -r tool; do
[[ -z "$tool" ]] && continue
[[ "$tool" =~ ^# ]] && continue
if ! command -v "$tool" >/dev/null 2>&1; then
missing+=("$tool")
fi
done < "$TOOLS_FILE"
if (( ${#missing[@]} > 0 )); then
echo "Missing required tools:" >&2
printf ' - %s\n' "${missing[@]}" >&2
exit 1
fi
echo "All required tools are present"
+1
View File
@@ -0,0 +1 @@
auto
+6
View File
@@ -44,10 +44,16 @@ CORS_ORIGINS = os.environ.get("CORS_ORIGINS", "https://nas.jimmygan.com,https://
# Chat Summary # Chat Summary
CHAT_SUMMARY_DB = os.environ.get("CHAT_SUMMARY_DB", "/app/data/chat-summarizer/chat_summary.db") CHAT_SUMMARY_DB = os.environ.get("CHAT_SUMMARY_DB", "/app/data/chat-summarizer/chat_summary.db")
# Info Engine
INFO_ENGINE_DB = os.environ.get("INFO_ENGINE_DB", "/app/data/info-engine/info_engine.db")
# LiteLLM # LiteLLM
LITELLM_URL = os.environ.get("LITELLM_URL", "http://127.0.0.1:4005") LITELLM_URL = os.environ.get("LITELLM_URL", "http://127.0.0.1:4005")
LITELLM_HEALTH_API_KEY = os.environ.get("LITELLM_HEALTH_API_KEY", "") LITELLM_HEALTH_API_KEY = os.environ.get("LITELLM_HEALTH_API_KEY", "")
# CC Connect
CC_CONNECT_URL = os.environ.get("CC_CONNECT_URL", "")
# OpenClaw # OpenClaw
OPENCLAW_GATEWAY_TOKEN = os.environ.get("OPENCLAW_GATEWAY_TOKEN", "") OPENCLAW_GATEWAY_TOKEN = os.environ.get("OPENCLAW_GATEWAY_TOKEN", "")
+5 -1
View File
@@ -6,7 +6,7 @@ from fastapi.responses import JSONResponse
from slowapi import Limiter from slowapi import Limiter
from slowapi.util import get_remote_address from slowapi.util import get_remote_address
from slowapi.errors import RateLimitExceeded from slowapi.errors import RateLimitExceeded
from routers import docker_router, gitea, files, terminal, system, auth, chat_summary, security, passkey, totp, litellm from routers import docker_router, gitea, files, terminal, system, auth, chat_summary, security, passkey, totp, litellm, cc_connect, info_engine
import auth as auth_module import auth as auth_module
from rbac import require_page, require_write from rbac import require_page, require_write
@@ -181,8 +181,12 @@ app.include_router(system.router, prefix="/api/system",
dependencies=[Depends(_inject_user), Depends(require_page("dashboard"))]) dependencies=[Depends(_inject_user), Depends(require_page("dashboard"))])
app.include_router(litellm.router, prefix="/api/litellm", app.include_router(litellm.router, prefix="/api/litellm",
dependencies=[Depends(_inject_user), Depends(require_page("dashboard"))]) dependencies=[Depends(_inject_user), Depends(require_page("dashboard"))])
app.include_router(cc_connect.router, prefix="/api/cc-connect",
dependencies=[Depends(_inject_user), Depends(require_page("dashboard"))])
app.include_router(chat_summary.router, prefix="/api/chat-summary", app.include_router(chat_summary.router, prefix="/api/chat-summary",
dependencies=[Depends(_inject_user), Depends(require_page("chat-digest"))]) dependencies=[Depends(_inject_user), Depends(require_page("chat-digest"))])
app.include_router(info_engine.router, prefix="/api/info-engine",
dependencies=[Depends(_inject_user), Depends(require_page("dashboard"))])
app.include_router(security.router, prefix="/api/security", app.include_router(security.router, prefix="/api/security",
dependencies=[Depends(_inject_user), Depends(require_page("security"))]) dependencies=[Depends(_inject_user), Depends(require_page("security"))])
+188
View File
@@ -0,0 +1,188 @@
import time
import docker
import httpx
from fastapi import APIRouter
from config import CC_CONNECT_URL, DOCKER_HOST
router = APIRouter()
client = docker.DockerClient(base_url=DOCKER_HOST, timeout=5)
def _get_cc_connect_container():
matches = client.containers.list(all=True, filters={"name": "cc-connect"})
return matches[0] if matches else None
@router.post("/start")
def start_cc_connect():
try:
container = _get_cc_connect_container()
except Exception as exc:
return {
"ok": False,
"status": "down",
"container_status": None,
"error": f"docker check failed: {exc}",
}
if not container:
return {
"ok": False,
"status": "down",
"container_status": "not_found",
"error": "cc-connect container not found",
}
try:
container.start()
container.reload()
container_status = container.status
return {
"ok": container_status == "running",
"status": "up" if container_status == "running" else "down",
"container_status": container_status,
"error": None if container_status == "running" else "cc-connect failed to start",
}
except Exception as exc:
return {
"ok": False,
"status": "down",
"container_status": container.status,
"error": f"docker start failed: {exc}",
}
@router.post("/stop")
def stop_cc_connect():
try:
container = _get_cc_connect_container()
except Exception as exc:
return {
"ok": False,
"status": "down",
"container_status": None,
"error": f"docker check failed: {exc}",
}
if not container:
return {
"ok": False,
"status": "down",
"container_status": "not_found",
"error": "cc-connect container not found",
}
try:
container.stop()
container.reload()
container_status = container.status
return {
"ok": container_status != "running",
"status": "down",
"container_status": container_status,
"error": None if container_status != "running" else "cc-connect failed to stop",
}
except Exception as exc:
return {
"ok": False,
"status": "down",
"container_status": container.status,
"error": f"docker stop failed: {exc}",
}
@router.get("/health")
async def cc_connect_health():
url = (CC_CONNECT_URL or "").strip()
container = None
try:
container = _get_cc_connect_container()
except Exception as exc:
return {
"ok": False,
"status": "down",
"source": "docker",
"container_name": None,
"container_status": None,
"endpoint": None,
"http_status": None,
"latency_ms": None,
"error": f"docker check failed: {exc}",
}
container_name = container.name if container else None
container_status = container.status if container else "not_found"
container_up = container_status == "running"
if not url:
return {
"ok": container_up,
"status": "up" if container_up else "down",
"source": "docker",
"container_name": container_name,
"container_status": container_status,
"endpoint": None,
"http_status": None,
"latency_ms": None,
"error": None if container_up else "cc-connect container is not running",
}
timeout = httpx.Timeout(2.0)
started = time.perf_counter()
try:
async with httpx.AsyncClient(timeout=timeout) as http_client:
response = await http_client.get(url)
latency_ms = round((time.perf_counter() - started) * 1000, 1)
if response.is_success and container_up:
return {
"ok": True,
"status": "up",
"source": "docker+http",
"container_name": container_name,
"container_status": container_status,
"endpoint": url,
"http_status": response.status_code,
"latency_ms": latency_ms,
"error": None,
}
if response.is_success and not container_up:
return {
"ok": False,
"status": "down",
"source": "docker+http",
"container_name": container_name,
"container_status": container_status,
"endpoint": url,
"http_status": response.status_code,
"latency_ms": latency_ms,
"error": "HTTP probe succeeded but cc-connect container is not running",
}
return {
"ok": False,
"status": "down",
"source": "docker+http",
"container_name": container_name,
"container_status": container_status,
"endpoint": url,
"http_status": response.status_code,
"latency_ms": latency_ms,
"error": f"HTTP probe returned {response.status_code}",
}
except Exception as exc:
return {
"ok": False,
"status": "down",
"source": "docker+http",
"container_name": container_name,
"container_status": container_status,
"endpoint": url,
"http_status": None,
"latency_ms": None,
"error": str(exc),
}
+111
View File
@@ -0,0 +1,111 @@
import json
import aiosqlite
from fastapi import APIRouter, HTTPException, Query
from config import INFO_ENGINE_DB
router = APIRouter()
async def _db():
return aiosqlite.connect(INFO_ENGINE_DB)
def _row_to_item(row):
tags = []
if row[5]:
try:
tags = json.loads(row[5])
except Exception:
tags = []
return {
"id": row[0],
"source": row[1],
"title": row[2],
"summary": row[3],
"url": row[4],
"tags": tags,
"published_at": row[6],
"collected_at": row[7],
"content_text": row[8],
"content_html": row[9],
}
@router.get("/items")
async def list_items(
limit: int = Query(50, ge=1, le=200),
offset: int = Query(0, ge=0),
source: str = Query(""),
tag: str = Query(""),
since: str = Query(""),
):
where = []
params = []
if source:
where.append("source = ?")
params.append(source)
if tag:
where.append("tags LIKE ?")
params.append(f'%"{tag}"%')
if since:
where.append("collected_at >= ?")
params.append(since)
where_clause = f"WHERE {' AND '.join(where)}" if where else ""
async with await _db() as db:
count_cursor = await db.execute(
f"SELECT COUNT(*) FROM info_items {where_clause}",
tuple(params),
)
total = (await count_cursor.fetchone())[0]
cursor = await db.execute(
f"""
SELECT id, source, title, summary, url, tags, published_at, collected_at, content_text, content_html
FROM info_items
{where_clause}
ORDER BY COALESCE(published_at, collected_at) DESC, id DESC
LIMIT ? OFFSET ?
""",
tuple(params + [limit, offset]),
)
rows = await cursor.fetchall()
return {"items": [_row_to_item(row) for row in rows], "total": total}
@router.get("/items/{item_id}")
async def get_item(item_id: int):
async with await _db() as db:
cursor = await db.execute(
"""
SELECT id, source, title, summary, url, tags, published_at, collected_at, content_text, content_html
FROM info_items
WHERE id = ?
""",
(item_id,),
)
row = await cursor.fetchone()
if not row:
raise HTTPException(404, "Item not found")
return _row_to_item(row)
@router.get("/health")
async def health():
async with await _db() as db:
cursor = await db.execute("SELECT COUNT(*), MAX(collected_at) FROM info_items")
row = await cursor.fetchone()
return {
"status": "ok",
"item_count": row[0] if row else 0,
"last_collect_at": row[1] if row else None,
}
+1
View File
@@ -50,6 +50,7 @@ services:
- /volume1/docker/nas-dashboard/ssh/vps_terminal:/app/ssh/vps_id_ed25519:ro - /volume1/docker/nas-dashboard/ssh/vps_terminal:/app/ssh/vps_id_ed25519:ro
- /volume1/docker/nas-dashboard/ssh/known_hosts:/app/ssh/known_hosts:ro - /volume1/docker/nas-dashboard/ssh/known_hosts:/app/ssh/known_hosts:ro
- /volume1/docker/chat-summarizer/data:/app/data/chat-summarizer:ro - /volume1/docker/chat-summarizer/data:/app/data/chat-summarizer:ro
- /volume1/docker/info-engine/data:/app/data/info-engine:ro
- /volume1/docker/nas-dashboard/rbac.json:/volume1/docker/nas-dashboard/rbac.json - /volume1/docker/nas-dashboard/rbac.json:/volume1/docker/nas-dashboard/rbac.json
healthcheck: healthcheck:
test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:4000/api/health')"] test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:4000/api/health')"]
+6
View File
@@ -10,6 +10,8 @@
import ChatSummary from "./routes/ChatSummary.svelte"; import ChatSummary from "./routes/ChatSummary.svelte";
import Security from "./routes/Security.svelte"; import Security from "./routes/Security.svelte";
import LiteLLM from "./routes/LiteLLM.svelte"; import LiteLLM from "./routes/LiteLLM.svelte";
import CcConnect from "./routes/CcConnect.svelte";
import InfoEngine from "./routes/InfoEngine.svelte";
import Login from "./routes/Login.svelte"; import Login from "./routes/Login.svelte";
import { onMount } from "svelte"; import { onMount } from "svelte";
import { getToken, checkAuth, setToken, setRefreshToken, currentUser, setCurrentUser, getPreferences, savePreferences } from "./lib/api.js"; import { getToken, checkAuth, setToken, setRefreshToken, currentUser, setCurrentUser, getPreferences, savePreferences } from "./lib/api.js";
@@ -156,6 +158,10 @@
<Security /> <Security />
{:else if page === "litellm" && hasPageAccess("dashboard")} {:else if page === "litellm" && hasPageAccess("dashboard")}
<LiteLLM /> <LiteLLM />
{:else if page === "cc-connect" && hasPageAccess("dashboard")}
<CcConnect />
{:else if page === "info-engine" && hasPageAccess("dashboard")}
<InfoEngine />
{:else} {:else}
<Dashboard /> <Dashboard />
{/if} {/if}
@@ -4,7 +4,9 @@
function canAccess(id) { function canAccess(id) {
if (!id) return true; if (!id) return true;
if (allowedPages === "*") return true; if (allowedPages === "*") return true;
return Array.isArray(allowedPages) && allowedPages.includes(id); if (!Array.isArray(allowedPages)) return false;
if (id === "info-engine") return allowedPages.includes("dashboard");
return allowedPages.includes(id);
} }
function canSeeSidebarLink(item) { function canSeeSidebarLink(item) {
@@ -16,6 +18,7 @@
const defaultLinks = [ const defaultLinks = [
{ id: "dashboard", label: "Overview", icon: "grid" }, { id: "dashboard", label: "Overview", icon: "grid" },
{ id: "info-engine", label: "Info Engine", icon: "sparkles" },
{ id: "litellm", label: "LiteLLM", icon: "bolt" }, { id: "litellm", label: "LiteLLM", icon: "bolt" },
{ id: "docker", label: "Docker", icon: "box" }, { id: "docker", label: "Docker", icon: "box" },
{ id: "files", label: "Files", icon: "folder" }, { id: "files", label: "Files", icon: "folder" },
@@ -42,6 +45,7 @@
const defaultTools = [ const defaultTools = [
{ id: "openclaw", label: "OpenClaw", icon: "openclaw" }, { id: "openclaw", label: "OpenClaw", icon: "openclaw" },
{ id: "cc-connect", label: "cc-connect", icon: "users" },
{ id: "chat-digest", label: "Chat Digest", icon: "chat" }, { id: "chat-digest", label: "Chat Digest", icon: "chat" },
{ id: "gitea", label: "Repos", icon: "git" }, { id: "gitea", label: "Repos", icon: "git" },
{ label: "Gitea Web", remoteHref: "https://git.jimmygan.com:8443", icon: "git", external: true }, { label: "Gitea Web", remoteHref: "https://git.jimmygan.com:8443", icon: "git", external: true },
@@ -227,6 +231,8 @@
<span class="w-5 h-5 flex items-center justify-center"> <span class="w-5 h-5 flex items-center justify-center">
{#if icon === "grid"} {#if icon === "grid"}
<svg class="w-[18px] h-[18px]" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.8"><path stroke-linecap="round" stroke-linejoin="round" d="M4 6a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2H6a2 2 0 01-2-2V6zm10 0a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2h-2a2 2 0 01-2-2V6zM4 16a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2H6a2 2 0 01-2-2v-2zm10 0a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2h-2a2 2 0 01-2-2v-2z" /></svg> <svg class="w-[18px] h-[18px]" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.8"><path stroke-linecap="round" stroke-linejoin="round" d="M4 6a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2H6a2 2 0 01-2-2V6zm10 0a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2h-2a2 2 0 01-2-2V6zM4 16a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2H6a2 2 0 01-2-2v-2zm10 0a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2h-2a2 2 0 01-2-2v-2z" /></svg>
{:else if icon === "sparkles"}
<svg class="w-[18px] h-[18px]" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.8"><path stroke-linecap="round" stroke-linejoin="round" d="M12 3l1.8 4.2L18 9l-4.2 1.8L12 15l-1.8-4.2L6 9l4.2-1.8L12 3zm6 11l.9 2.1L21 17l-2.1.9L18 20l-.9-2.1L15 17l2.1-.9L18 14zM5 14l.9 2.1L8 17l-2.1.9L5 20l-.9-2.1L2 17l2.1-.9L5 14z" /></svg>
{:else if icon === "box"} {:else if icon === "box"}
<svg class="w-[18px] h-[18px]" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.8"><path stroke-linecap="round" stroke-linejoin="round" d="M20 7l-8-4-8 4m16 0l-8 4m8-4v10l-8 4m0-10L4 7m8 4v10M4 7v10l8 4" /></svg> <svg class="w-[18px] h-[18px]" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.8"><path stroke-linecap="round" stroke-linejoin="round" d="M20 7l-8-4-8 4m16 0l-8 4m8-4v10l-8 4m0-10L4 7m8 4v10M4 7v10l8 4" /></svg>
{:else if icon === "bolt"} {:else if icon === "bolt"}
+27
View File
@@ -143,6 +143,33 @@ export function getLiteLLMHealth() {
return get("/litellm/health"); return get("/litellm/health");
} }
export function getCcConnectHealth() {
return get("/cc-connect/health");
}
export function startCcConnect() {
return post("/cc-connect/start");
}
export function stopCcConnect() {
return post("/cc-connect/stop");
}
export function getInfoEngineItems(params = {}) {
const query = new URLSearchParams();
if (params.limit !== undefined) query.set("limit", String(params.limit));
if (params.offset !== undefined) query.set("offset", String(params.offset));
if (params.source) query.set("source", params.source);
if (params.tag) query.set("tag", params.tag);
if (params.since) query.set("since", params.since);
const suffix = query.toString() ? `?${query.toString()}` : "";
return get(`/info-engine/items${suffix}`);
}
export function getInfoEngineItem(id) {
return get(`/info-engine/items/${id}`);
}
export function put(path, data) { export function put(path, data) {
return request(path, { method: "PUT", json: data }); return request(path, { method: "PUT", json: data });
} }
@@ -0,0 +1,155 @@
<script>
import { onMount } from "svelte";
import { getCcConnectHealth, startCcConnect, stopCcConnect } from "../lib/api.js";
let loading = $state(true);
let health = $state(null);
let requestError = $state("");
let actionLoading = $state("");
async function loadHealth() {
loading = true;
requestError = "";
try {
health = await getCcConnectHealth();
} catch (e) {
requestError = e?.body?.detail || e?.message || "Failed to load cc-connect health";
health = null;
} finally {
loading = false;
}
}
async function runAction(action) {
actionLoading = action;
requestError = "";
try {
const result = action === "start" ? await startCcConnect() : await stopCcConnect();
if (!result?.ok) {
requestError = result?.error || `Failed to ${action} cc-connect`;
}
} catch (e) {
requestError = e?.body?.detail || e?.message || `Failed to ${action} cc-connect`;
} finally {
actionLoading = "";
await loadHealth();
}
}
function statusBadgeClass(status) {
if (status === "up") {
return "bg-emerald-100 text-emerald-700 dark:bg-emerald-900/40 dark:text-emerald-300";
}
return "bg-rose-100 text-rose-700 dark:bg-rose-900/40 dark:text-rose-300";
}
function valueOrDash(value) {
return value === null || value === undefined || value === "" ? "—" : value;
}
onMount(loadHealth);
</script>
<div class="space-y-6">
<div class="flex items-center justify-between">
<div>
<h1 class="text-2xl font-bold text-surface-900 dark:text-white tracking-tight">cc-connect</h1>
<p class="text-sm text-surface-400 mt-1">Mobile bridge operational status</p>
<div class="mt-2 flex flex-wrap items-center gap-2 text-[11px] font-medium">
<span class="px-2 py-0.5 rounded-full bg-emerald-100 text-emerald-700 dark:bg-emerald-900/40 dark:text-emerald-300">up</span>
<span class="px-2 py-0.5 rounded-full bg-rose-100 text-rose-700 dark:bg-rose-900/40 dark:text-rose-300">down</span>
</div>
</div>
<div class="flex items-center gap-2">
{#if health?.container_status === "running"}
<button
onclick={() => runAction("stop")}
disabled={loading || actionLoading === "stop"}
class="px-3 py-1.5 text-xs font-medium text-rose-600 bg-rose-50 hover:bg-rose-100 rounded-lg transition-colors disabled:opacity-50"
>
{actionLoading === "stop" ? "Stopping..." : "Stop"}
</button>
{:else}
<button
onclick={() => runAction("start")}
disabled={loading || actionLoading === "start"}
class="px-3 py-1.5 text-xs font-medium text-emerald-600 bg-emerald-50 hover:bg-emerald-100 rounded-lg transition-colors disabled:opacity-50"
>
{actionLoading === "start" ? "Starting..." : "Start"}
</button>
{/if}
<button onclick={loadHealth} disabled={loading || Boolean(actionLoading)} class="px-3 py-1.5 text-xs font-medium text-primary-600 bg-primary-50 hover:bg-primary-100 rounded-lg transition-colors disabled:opacity-50 dark:bg-primary-900/30 dark:text-primary-300">
{loading ? "Loading..." : "Refresh"}
</button>
</div>
</div>
{#if requestError}
<div class="bg-rose-50 dark:bg-rose-900/20 border border-rose-200 dark:border-rose-700 rounded-xl p-4 shadow-sm">
<p class="text-sm font-medium text-rose-700 dark:text-rose-300">cc-connect action failed</p>
<p class="text-xs text-rose-600 dark:text-rose-400 mt-1">{requestError}</p>
</div>
{/if}
<div class="bg-white dark:bg-surface-800 rounded-xl border border-surface-200 dark:border-surface-700 p-5 shadow-sm">
{#if loading && !health}
<div class="space-y-3">
<div class="h-6 w-36 rounded bg-surface-100 dark:bg-surface-700 animate-pulse"></div>
<div class="grid grid-cols-1 sm:grid-cols-2 gap-3">
{#each Array(8) as _}
<div class="h-16 rounded-lg bg-surface-100 dark:bg-surface-700 animate-pulse"></div>
{/each}
</div>
</div>
{:else if health}
<div class="flex items-center justify-between mb-4">
<h3 class="text-sm font-semibold text-surface-700 dark:text-surface-200">Health Status</h3>
<span class="px-2.5 py-1 text-xs font-semibold rounded-full {statusBadgeClass(health.status)}">{valueOrDash(health.status)}</span>
</div>
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-3">
<div class="rounded-lg border border-surface-200 dark:border-surface-700 p-3">
<p class="text-xs font-medium text-surface-400 uppercase tracking-wider">OK</p>
<p class="text-sm font-semibold text-surface-800 dark:text-surface-100 mt-1">{String(Boolean(health.ok))}</p>
</div>
<div class="rounded-lg border border-surface-200 dark:border-surface-700 p-3">
<p class="text-xs font-medium text-surface-400 uppercase tracking-wider">Source</p>
<p class="text-sm font-semibold text-surface-800 dark:text-surface-100 mt-1">{valueOrDash(health.source)}</p>
</div>
<div class="rounded-lg border border-surface-200 dark:border-surface-700 p-3">
<p class="text-xs font-medium text-surface-400 uppercase tracking-wider">Container</p>
<p class="text-sm font-semibold text-surface-800 dark:text-surface-100 mt-1">{valueOrDash(health.container_name)}</p>
</div>
<div class="rounded-lg border border-surface-200 dark:border-surface-700 p-3">
<p class="text-xs font-medium text-surface-400 uppercase tracking-wider">Container Status</p>
<p class="text-sm font-semibold text-surface-800 dark:text-surface-100 mt-1">{valueOrDash(health.container_status)}</p>
</div>
<div class="rounded-lg border border-surface-200 dark:border-surface-700 p-3">
<p class="text-xs font-medium text-surface-400 uppercase tracking-wider">Endpoint</p>
<p class="text-sm font-semibold text-surface-800 dark:text-surface-100 mt-1 break-all">{valueOrDash(health.endpoint)}</p>
</div>
<div class="rounded-lg border border-surface-200 dark:border-surface-700 p-3">
<p class="text-xs font-medium text-surface-400 uppercase tracking-wider">HTTP Status</p>
<p class="text-sm font-semibold text-surface-800 dark:text-surface-100 mt-1">{valueOrDash(health.http_status)}</p>
</div>
<div class="rounded-lg border border-surface-200 dark:border-surface-700 p-3">
<p class="text-xs font-medium text-surface-400 uppercase tracking-wider">Latency</p>
<p class="text-sm font-semibold text-surface-800 dark:text-surface-100 mt-1">{health.latency_ms === null || health.latency_ms === undefined ? "—" : `${health.latency_ms} ms`}</p>
</div>
<div class="rounded-lg border border-surface-200 dark:border-surface-700 p-3 sm:col-span-2 lg:col-span-2">
<p class="text-xs font-medium text-surface-400 uppercase tracking-wider">Error</p>
<p class="text-sm font-semibold text-surface-800 dark:text-surface-100 mt-1 break-all">{valueOrDash(health.error)}</p>
</div>
</div>
{:else}
<p class="text-sm text-surface-400">No health data available.</p>
{/if}
</div>
</div>
@@ -0,0 +1,139 @@
<script>
import { onMount } from "svelte";
import { getInfoEngineItems, getInfoEngineItem } from "../lib/api.js";
let loading = $state(true);
let loadingDetail = $state(false);
let requestError = $state("");
let items = $state([]);
let total = $state(0);
let selectedId = $state(null);
let selectedItem = $state(null);
async function loadItems() {
loading = true;
requestError = "";
try {
const res = await getInfoEngineItems({ limit: 50, offset: 0 });
items = res.items || [];
total = res.total || 0;
if (items.length > 0) {
await selectItem(items[0].id);
} else {
selectedId = null;
selectedItem = null;
}
} catch (e) {
requestError = e?.body?.detail || e?.message || "Failed to load items";
items = [];
total = 0;
selectedId = null;
selectedItem = null;
} finally {
loading = false;
}
}
async function selectItem(id) {
selectedId = id;
loadingDetail = true;
requestError = "";
try {
selectedItem = await getInfoEngineItem(id);
} catch (e) {
selectedItem = null;
requestError = e?.body?.detail || e?.message || "Failed to load item";
} finally {
loadingDetail = false;
}
}
function formatDate(value) {
if (!value) return "—";
try {
return new Date(value).toLocaleString();
} catch {
return value;
}
}
onMount(loadItems);
</script>
<div class="space-y-6">
<div class="flex items-center justify-between">
<div>
<h1 class="text-2xl font-bold text-surface-900 dark:text-white tracking-tight">Info Engine</h1>
<p class="text-sm text-surface-400 mt-1">Context-aware stream of curated signals</p>
</div>
<button onclick={loadItems} disabled={loading} class="px-3 py-1.5 text-xs font-medium text-primary-600 bg-primary-50 hover:bg-primary-100 rounded-lg transition-colors disabled:opacity-50 dark:bg-primary-900/30 dark:text-primary-300">
{loading ? "Loading..." : "Refresh"}
</button>
</div>
{#if requestError}
<div class="bg-rose-50 dark:bg-rose-900/20 border border-rose-200 dark:border-rose-700 rounded-xl p-4 shadow-sm">
<p class="text-sm font-medium text-rose-700 dark:text-rose-300">{requestError}</p>
</div>
{/if}
<div class="grid grid-cols-1 lg:grid-cols-3 gap-4">
<div class="lg:col-span-1 bg-white dark:bg-surface-800 rounded-xl border border-surface-200 dark:border-surface-700 shadow-sm">
<div class="px-4 py-3 border-b border-surface-200 dark:border-surface-700 text-xs text-surface-400">
Latest items ({total})
</div>
{#if loading}
<div class="p-4 space-y-2">
{#each Array(6) as _}
<div class="h-12 rounded-lg bg-surface-100 dark:bg-surface-700 animate-pulse"></div>
{/each}
</div>
{:else if items.length === 0}
<div class="p-4 text-sm text-surface-400">No items yet.</div>
{:else}
<div class="max-h-[560px] overflow-y-auto p-2">
{#each items as item}
<button onclick={() => selectItem(item.id)} class="w-full text-left px-3 py-2 rounded-lg mb-1 transition-colors {selectedId === item.id ? 'bg-primary-50 dark:bg-primary-900/30' : 'hover:bg-surface-100 dark:hover:bg-surface-700'}">
<p class="text-sm font-semibold text-surface-800 dark:text-surface-100 line-clamp-2">{item.title}</p>
<p class="text-xs text-surface-400 mt-1">{item.source}</p>
</button>
{/each}
</div>
{/if}
</div>
<div class="lg:col-span-2 bg-white dark:bg-surface-800 rounded-xl border border-surface-200 dark:border-surface-700 p-5 shadow-sm">
{#if loadingDetail}
<div class="space-y-3">
<div class="h-7 w-3/4 rounded bg-surface-100 dark:bg-surface-700 animate-pulse"></div>
<div class="h-4 w-full rounded bg-surface-100 dark:bg-surface-700 animate-pulse"></div>
<div class="h-4 w-5/6 rounded bg-surface-100 dark:bg-surface-700 animate-pulse"></div>
<div class="h-24 w-full rounded bg-surface-100 dark:bg-surface-700 animate-pulse"></div>
</div>
{:else if selectedItem}
<h2 class="text-xl font-bold text-surface-900 dark:text-white">{selectedItem.title}</h2>
<div class="mt-2 text-xs text-surface-400 space-y-1">
<p>Source: {selectedItem.source}</p>
<p>Published: {formatDate(selectedItem.published_at)}</p>
<p>Collected: {formatDate(selectedItem.collected_at)}</p>
</div>
{#if selectedItem.tags?.length}
<div class="mt-3 flex flex-wrap gap-2">
{#each selectedItem.tags as tag}
<span class="px-2 py-0.5 text-xs rounded-full bg-surface-100 text-surface-600 dark:bg-surface-700 dark:text-surface-300">{tag}</span>
{/each}
</div>
{/if}
<p class="mt-4 text-sm text-surface-700 dark:text-surface-300 whitespace-pre-wrap">{selectedItem.summary || selectedItem.content_text || "No summary available."}</p>
<a href={selectedItem.url} target="_blank" rel="noopener" class="inline-flex mt-4 px-3 py-1.5 text-xs font-medium rounded-lg bg-primary-600 text-white hover:bg-primary-700">
Open source
</a>
{:else}
<p class="text-sm text-surface-400">Select an item to view details.</p>
{/if}
</div>
</div>
</div>
+6
View File
@@ -0,0 +1,6 @@
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "-u", "main.py"]
+151
View File
@@ -0,0 +1,151 @@
import logging
import os
from datetime import datetime
from email.utils import parsedate_to_datetime
from urllib.parse import urlparse
import xml.etree.ElementTree as ET
import httpx
log = logging.getLogger(__name__)
def _source_name(url: str) -> str:
host = urlparse(url).netloc
return host or url
def _first_text(parent, paths: list[str]) -> str:
for path in paths:
node = parent.find(path)
if node is not None and node.text:
value = node.text.strip()
if value:
return value
return ""
def _atom_link(entry) -> str:
for link in entry.findall("{http://www.w3.org/2005/Atom}link"):
href = (link.attrib.get("href") or "").strip()
rel = (link.attrib.get("rel") or "alternate").strip()
if href and rel == "alternate":
return href
links = entry.findall("{http://www.w3.org/2005/Atom}link")
if links:
return (links[0].attrib.get("href") or "").strip()
return ""
def _parse_datetime(value: str | None) -> str | None:
if not value:
return None
value = value.strip()
if not value:
return None
try:
return parsedate_to_datetime(value).isoformat()
except Exception:
pass
try:
iso = value.replace("Z", "+00:00")
return datetime.fromisoformat(iso).isoformat()
except Exception:
return None
def _parse_feed(feed_url: str, xml_text: str) -> list[dict]:
root = ET.fromstring(xml_text)
source = _source_name(feed_url)
items = []
channel = root.find("channel")
if channel is not None:
for item in channel.findall("item"):
title = _first_text(item, ["title"])
link = _first_text(item, ["link"])
summary = _first_text(item, ["description"])
pub = _first_text(item, ["pubDate", "date"])
tags = [n.text.strip() for n in item.findall("category") if n.text and n.text.strip()]
if title and link:
items.append(
{
"source": source,
"title": title,
"summary": summary,
"url": link,
"tags": tags,
"published_at": _parse_datetime(pub),
"content_text": summary,
"content_html": summary,
}
)
return items
for entry in root.findall("{http://www.w3.org/2005/Atom}entry"):
title = _first_text(entry, ["{http://www.w3.org/2005/Atom}title"])
link = _atom_link(entry)
summary = _first_text(
entry,
[
"{http://www.w3.org/2005/Atom}summary",
"{http://www.w3.org/2005/Atom}content",
],
)
pub = _first_text(
entry,
[
"{http://www.w3.org/2005/Atom}published",
"{http://www.w3.org/2005/Atom}updated",
],
)
tags = [
(cat.attrib.get("term") or "").strip()
for cat in entry.findall("{http://www.w3.org/2005/Atom}category")
if (cat.attrib.get("term") or "").strip()
]
if title and link:
items.append(
{
"source": source,
"title": title,
"summary": summary,
"url": link,
"tags": tags,
"published_at": _parse_datetime(pub),
"content_text": summary,
"content_html": summary,
}
)
return items
async def collect_sources() -> list[dict]:
sources_env = os.environ.get(
"INFO_ENGINE_SOURCES",
"https://hnrss.org/frontpage,https://lobste.rs/rss",
)
sources = [s.strip() for s in sources_env.split(",") if s.strip()]
if not sources:
log.warning("No INFO_ENGINE_SOURCES configured")
return []
timeout = httpx.Timeout(15.0)
all_items = []
async with httpx.AsyncClient(timeout=timeout, follow_redirects=True) as client:
for source_url in sources:
try:
response = await client.get(source_url)
response.raise_for_status()
parsed = _parse_feed(source_url, response.text)
all_items.extend(parsed)
log.info("Collected %d items from %s", len(parsed), source_url)
except Exception as exc:
log.error("Failed to collect from %s: %s", source_url, exc)
return all_items
+64
View File
@@ -0,0 +1,64 @@
import json
import os
from datetime import datetime, timezone
import aiosqlite
DB_PATH = os.environ.get("DB_PATH", "/app/data/info_engine.db")
async def init_db():
async with aiosqlite.connect(DB_PATH) as db:
await db.execute(
"""
CREATE TABLE IF NOT EXISTS info_items (
id INTEGER PRIMARY KEY AUTOINCREMENT,
source TEXT NOT NULL,
title TEXT NOT NULL,
summary TEXT NOT NULL,
url TEXT NOT NULL UNIQUE,
tags TEXT NOT NULL,
published_at DATETIME,
collected_at DATETIME NOT NULL,
content_text TEXT,
content_html TEXT
)
"""
)
await db.execute("CREATE INDEX IF NOT EXISTS idx_info_items_collected_at ON info_items(collected_at DESC)")
await db.execute("CREATE INDEX IF NOT EXISTS idx_info_items_published_at ON info_items(published_at DESC)")
await db.execute("CREATE INDEX IF NOT EXISTS idx_info_items_source ON info_items(source)")
await db.commit()
async def upsert_items(items: list[dict]) -> int:
if not items:
return 0
inserted = 0
now = datetime.now(timezone.utc).isoformat()
async with aiosqlite.connect(DB_PATH) as db:
for item in items:
cursor = await db.execute(
"""
INSERT OR IGNORE INTO info_items
(source, title, summary, url, tags, published_at, collected_at, content_text, content_html)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
(
item["source"],
item["title"],
item.get("summary", ""),
item["url"],
json.dumps(item.get("tags", []), ensure_ascii=False),
item.get("published_at"),
now,
item.get("content_text"),
item.get("content_html"),
),
)
inserted += cursor.rowcount
await db.commit()
return inserted
+16
View File
@@ -0,0 +1,16 @@
services:
info-engine:
build: .
container_name: info-engine
restart: unless-stopped
volumes:
- /volume1/docker/info-engine/data:/app/data
environment:
- DB_PATH=/app/data/info_engine.db
- INFO_ENGINE_INTERVAL_SECONDS=${INFO_ENGINE_INTERVAL_SECONDS:-900}
- INFO_ENGINE_SOURCES=${INFO_ENGINE_SOURCES:-https://hnrss.org/frontpage,https://lobste.rs/rss}
logging:
driver: json-file
options:
max-size: "10m"
max-file: "3"
+37
View File
@@ -0,0 +1,37 @@
import asyncio
import logging
import os
from collector import collect_sources
from db import init_db, upsert_items
from processor import process_items
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(name)s %(levelname)s %(message)s")
log = logging.getLogger(__name__)
COLLECT_INTERVAL_SECONDS = int(os.environ.get("INFO_ENGINE_INTERVAL_SECONDS", "900"))
async def run_once() -> int:
raw_items = await collect_sources()
processed = process_items(raw_items)
inserted = await upsert_items(processed)
return inserted
async def main():
await init_db()
log.info("Info engine started (interval=%ss)", COLLECT_INTERVAL_SECONDS)
while True:
try:
inserted = await run_once()
log.info("Info engine cycle complete (inserted=%d)", inserted)
except Exception as exc:
log.error("Info engine cycle failed: %s", exc)
await asyncio.sleep(COLLECT_INTERVAL_SECONDS)
if __name__ == "__main__":
asyncio.run(main())
+45
View File
@@ -0,0 +1,45 @@
from urllib.parse import urldefrag, urlparse
def _normalize_url(url: str) -> str:
clean, _ = urldefrag(url.strip())
parsed = urlparse(clean)
if parsed.scheme and parsed.netloc:
return clean
return ""
def process_items(items: list[dict]) -> list[dict]:
seen = set()
processed = []
for item in items:
title = (item.get("title") or "").strip()
url = _normalize_url(item.get("url") or "")
if not title or not url:
continue
dedupe_key = url.lower()
if dedupe_key in seen:
continue
seen.add(dedupe_key)
summary = (item.get("summary") or "").strip()
tags = item.get("tags") or []
if not isinstance(tags, list):
tags = []
processed.append(
{
"source": (item.get("source") or "unknown").strip() or "unknown",
"title": title,
"summary": summary,
"url": url,
"tags": [str(t).strip() for t in tags if str(t).strip()],
"published_at": item.get("published_at"),
"content_text": item.get("content_text") or summary,
"content_html": item.get("content_html") or summary,
}
)
return processed
+2
View File
@@ -0,0 +1,2 @@
aiosqlite
httpx