refactor: content-addressed assets, cross-channel multimodal, infra simplification (#63)

* refactor(attachment): multimodal attachment refactor with snapshot schema and storage layer

- Add snapshot schema migration (0008) and update init/versions/snapshots
- Add internal/attachment and internal/channel normalize for unified attachment handling
- Move containerfs provider from internal/media to internal/storage
- Update agent types, channel adapters (Telegram/Feishu), inbound and handlers
- Add containerd snapshot lineage and local_channel tests
- Regenerate sqlc, swagger and SDK

* refactor(media): content-addressed asset system with unified naming

- Replace asset_id foreign key with content_hash as sole identifier
  for bot_history_message_assets (pure soft-link model)
- Remove mime, size_bytes, storage_key from DB; derive at read time
  via media.Resolve from actual storage
- Merge migrations 0008/0009 into single 0008; keep 0001 as canonical schema
- Add Docker initdb script for deterministic migration execution order
- Fix cross-channel real-time image display (Telegram → WebUI SSE)
- Fix message disappearing on refresh (null assets fallback)
- Fix file icon instead of image preview (mime derivation from storage)
- Unify AssetID → ContentHash naming across Go, Agent, and Frontend
- Change storage key prefix from 4-char to 2-char for directory sharding
- Add server-entrypoint.sh for Docker deployment migration handling

* refactor(infra): embedded migrations, Docker simplification, and config consolidation

- Embed SQL migrations into Go binary, removing shell-based migration scripts
- Consolidate config files into conf/ directory (app.example.toml, app.docker.toml, app.dev.toml)
- Simplify Docker setup: remove initdb.d scripts, streamline nginx config and entrypoint
- Remove legacy CLI, feishu-echo commands, and obsolete incremental migration files
- Update install script and docs to require sudo for one-click install
- Add mise tasks for dev environment orchestration

* chore: recover migrations

---------

Co-authored-by: Acbox <acbox0328@gmail.com>
This commit is contained in:
BBQ
2026-02-19 00:20:27 +08:00
committed by GitHub
parent 740f620fe4
commit bc374fe8cd
104 changed files with 6133 additions and 2987 deletions
+1 -5
View File
@@ -28,11 +28,7 @@ GOOS="$TARGET_OS" GOARCH="$TARGET_ARCH" go build -trimpath -ldflags "-s -w" -o "
mv -f "${APP_DIR}/${BIN_NAME}.new" "${APP_DIR}/${BIN_NAME}"
if [ -n "$CONTAINER_NAME" ]; then
if [ "$(uname -s)" = "Darwin" ]; then
limactl shell default -- nerdctl kill -s "$STOP_SIGNAL" "$CONTAINER_NAME"
else
nerdctl kill -s "$STOP_SIGNAL" "$CONTAINER_NAME"
fi
nerdctl kill -s "$STOP_SIGNAL" "$CONTAINER_NAME"
else
echo "CONTAINER_NAME is empty; skip sending stop signal."
fi
+2 -60
View File
@@ -1,47 +1,9 @@
#!/bin/bash
set -e
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
CONFIG_FILE="${PROJECT_ROOT}/config.toml"
MIGRATIONS_DIR="${PROJECT_ROOT}/db/migrations"
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m'
if [ ! -f "$CONFIG_FILE" ]; then
echo -e "${RED}Error: Config file not found${NC}"
exit 1
fi
if [ ! -d "$MIGRATIONS_DIR" ]; then
echo -e "${RED}Error: Migrations directory not found${NC}"
exit 1
fi
parse_toml_value() {
local key=$1
local section=$2
grep -A 20 "^\[$section\]" "$CONFIG_FILE" | grep "^$key" | head -1 | sed 's/.*=[ ]*//' | tr -d '"' | tr -d "'"
}
DB_HOST=$(parse_toml_value "host" "postgres")
DB_PORT=$(parse_toml_value "port" "postgres")
DB_USER=$(parse_toml_value "user" "postgres")
DB_PASSWORD=$(parse_toml_value "password" "postgres")
DB_NAME=$(parse_toml_value "database" "postgres")
DB_SSLMODE=$(parse_toml_value "sslmode" "postgres")
if [ -z "$DB_HOST" ] || [ -z "$DB_PORT" ] || [ -z "$DB_USER" ] || [ -z "$DB_NAME" ]; then
echo -e "${RED}Error: Invalid database configuration${NC}"
exit 1
fi
DB_SSLMODE=${DB_SSLMODE:-disable}
echo -e "${YELLOW}WARNING: This will drop all database tables!${NC}"
echo "WARNING: This will roll back all database migrations!"
read -p "Type 'yes' to confirm: " confirmation
if [ "$confirmation" != "yes" ]; then
@@ -49,24 +11,4 @@ if [ "$confirmation" != "yes" ]; then
exit 0
fi
export PGPASSWORD="$DB_PASSWORD"
PSQL_CMD="psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME"
if ! $PSQL_CMD -c "SELECT 1;" > /dev/null 2>&1; then
echo -e "${RED}Error: Cannot connect to database${NC}"
exit 1
fi
for migration_file in $(ls -r "$MIGRATIONS_DIR"/*.down.sql 2>/dev/null); do
if [ -f "$migration_file" ]; then
if ! $PSQL_CMD -f "$migration_file" > /dev/null 2>&1; then
echo -e "${RED}Error: Drop failed - $(basename "$migration_file")${NC}"
exit 1
fi
fi
done
echo -e "${GREEN}✓ Database tables dropped${NC}"
unset PGPASSWORD
go run "${PROJECT_ROOT}/cmd/agent/main.go" migrate down
+1 -58
View File
@@ -1,63 +1,6 @@
#!/bin/bash
set -e
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
CONFIG_FILE="${PROJECT_ROOT}/config.toml"
MIGRATIONS_DIR="${PROJECT_ROOT}/db/migrations"
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m'
if [ ! -f "$CONFIG_FILE" ]; then
echo -e "${RED}Error: Config file not found${NC}"
exit 1
fi
if [ ! -d "$MIGRATIONS_DIR" ]; then
echo -e "${RED}Error: Migrations directory not found${NC}"
exit 1
fi
parse_toml_value() {
local key=$1
local section=$2
grep -A 20 "^\[$section\]" "$CONFIG_FILE" | grep "^$key" | head -1 | sed 's/.*=[ ]*//' | tr -d '"' | tr -d "'"
}
DB_HOST=$(parse_toml_value "host" "postgres")
DB_PORT=$(parse_toml_value "port" "postgres")
DB_USER=$(parse_toml_value "user" "postgres")
DB_PASSWORD=$(parse_toml_value "password" "postgres")
DB_NAME=$(parse_toml_value "database" "postgres")
DB_SSLMODE=$(parse_toml_value "sslmode" "postgres")
if [ -z "$DB_HOST" ] || [ -z "$DB_PORT" ] || [ -z "$DB_USER" ] || [ -z "$DB_NAME" ]; then
echo -e "${RED}Error: Invalid database configuration${NC}"
exit 1
fi
DB_SSLMODE=${DB_SSLMODE:-disable}
export PGPASSWORD="$DB_PASSWORD"
PSQL_CMD="psql -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME"
if ! $PSQL_CMD -c "SELECT 1;" > /dev/null 2>&1; then
echo -e "${RED}Error: Cannot connect to database${NC}"
exit 1
fi
for migration_file in "$MIGRATIONS_DIR"/*.up.sql; do
if [ -f "$migration_file" ]; then
if ! $PSQL_CMD -f "$migration_file" > /dev/null 2>&1; then
echo -e "${RED}Error: Migration failed - $(basename "$migration_file")${NC}"
exit 1
fi
fi
done
echo -e "${GREEN}✓ Database migration completed${NC}"
unset PGPASSWORD
go run "${PROJECT_ROOT}/cmd/agent/main.go" migrate up
+17 -7
View File
@@ -28,13 +28,23 @@ echo "${GREEN} Memoh One-Click Install${NC}"
echo "${GREEN}========================================${NC}"
echo ""
# Check Docker
# Check Docker and determine if sudo is needed
DOCKER="docker"
if ! command -v docker >/dev/null 2>&1; then
echo "${RED}Error: Docker is not installed${NC}"
echo "Install Docker first: https://docs.docker.com/get-docker/"
exit 1
fi
if ! docker compose version >/dev/null 2>&1; then
if ! docker info >/dev/null 2>&1; then
if sudo docker info >/dev/null 2>&1; then
DOCKER="sudo docker"
else
echo "${RED}Error: Cannot connect to Docker daemon${NC}"
echo "Try: sudo usermod -aG docker \$USER && newgrp docker"
exit 1
fi
fi
if ! $DOCKER compose version >/dev/null 2>&1; then
echo "${RED}Error: Docker Compose v2 is required${NC}"
echo "Install: https://docs.docker.com/compose/install/"
exit 1
@@ -122,7 +132,7 @@ else
fi
# Generate config.toml from template
cp docker/config/config.docker.toml config.toml
cp conf/app.docker.toml config.toml
sed -i.bak "s|username = \"admin\"|username = \"${ADMIN_USER}\"|" config.toml
sed -i.bak "s|password = \"admin123\"|password = \"${ADMIN_PASS}\"|" config.toml
sed -i.bak "s|jwt_secret = \".*\"|jwt_secret = \"${JWT_SECRET}\"|" config.toml
@@ -138,7 +148,7 @@ mkdir -p "$MEMOH_DATA_DIR"
echo ""
echo "${GREEN}Starting services (first build may take a few minutes)...${NC}"
docker compose up -d --build
$DOCKER compose up -d --build
echo ""
echo "${GREEN}========================================${NC}"
@@ -152,8 +162,8 @@ echo ""
echo " Admin login: ${ADMIN_USER} / ${ADMIN_PASS}"
echo ""
echo "Commands:"
echo " cd ${INSTALL_DIR} && docker compose ps # Status"
echo " cd ${INSTALL_DIR} && docker compose logs -f # Logs"
echo " cd ${INSTALL_DIR} && docker compose down # Stop"
echo " cd ${INSTALL_DIR} && $DOCKER compose ps # Status"
echo " cd ${INSTALL_DIR} && $DOCKER compose logs -f # Logs"
echo " cd ${INSTALL_DIR} && $DOCKER compose down # Stop"
echo ""
echo "${YELLOW}First startup may take 1-2 minutes, please be patient.${NC}"
-89
View File
@@ -1,89 +0,0 @@
#!/usr/bin/env sh
set -e
if [ "$(uname -s)" != "Darwin" ]; then
exit 0
fi
host_yaml="$HOME/.lima/default/lima.yaml"
if [ -f "$host_yaml" ]; then
if ! awk '
BEGIN {in=0; entries=0}
/^portForwards:/ {in=1; next}
in && /^[^ ]/ {in=0}
in && /^ - / {entries=1; exit}
END {if (entries) exit 0; else exit 1}
' "$host_yaml"; then
tmp="${host_yaml}.tmp"
awk '
BEGIN {found=0; inserted=0}
/^portForwards: *\[/ {
found=1
print "portForwards:"
if (!inserted) {
print " - guestSocket: \"/run/containerd/containerd.sock\""
print " hostSocket: \"{{.Dir}}/sock/containerd/containerd.sock\""
inserted=1
}
next
}
/^portForwards:/ {
found=1
print
if (!inserted) {
print " - guestSocket: \"/run/containerd/containerd.sock\""
print " hostSocket: \"{{.Dir}}/sock/containerd/containerd.sock\""
inserted=1
}
next
}
{print}
END {
if (!found) {
print "portForwards:"
print " - guestSocket: \"/run/containerd/containerd.sock\""
print " hostSocket: \"{{.Dir}}/sock/containerd/containerd.sock\""
}
}
' "$host_yaml" > "$tmp" && mv "$tmp" "$host_yaml"
fi
fi
limactl start default
limactl shell default -- sudo -n chmod 666 /run/containerd/containerd.sock
if ! limactl shell default -- sh -lc 'command -v memoh-cli >/dev/null 2>&1'; then
vm_arch=$(limactl shell default -- uname -m)
if [ "$vm_arch" = "aarch64" ] || [ "$vm_arch" = "arm64" ]; then
go_arch="arm64"
else
go_arch="amd64"
fi
bin_path="/tmp/memoh-cli-linux-$go_arch"
GOOS=linux GOARCH=$go_arch go build -trimpath -ldflags "-s -w" -o "$bin_path" ./cmd/cli
limactl shell default -- sudo -n mkdir -p /usr/local/bin
limactl shell default -- sudo -n tee /usr/local/bin/memoh-cli >/dev/null < "$bin_path"
limactl shell default -- sudo -n chmod +x /usr/local/bin/memoh-cli
fi
limactl shell default -- sh -lc 'command -v curl >/dev/null 2>&1' || {
echo "curl not found in Lima VM; install curl and rerun"
exit 1
}
limactl shell default -- sh -lc 'test -x /opt/cni/bin/bridge' || {
vm_arch=$(limactl shell default -- uname -m)
if [ "$vm_arch" = "aarch64" ] || [ "$vm_arch" = "arm64" ]; then
cni_arch="arm64"
else
cni_arch="amd64"
fi
url="https://github.com/containernetworking/plugins/releases/download/v1.9.0/cni-plugins-linux-${cni_arch}-v1.9.0.tgz"
limactl shell default -- sudo -n mkdir -p /opt/cni/bin
limactl shell default -- sudo -n curl -L -o /tmp/cni-plugins.tgz "$url"
limactl shell default -- sudo -n tar -C /opt/cni/bin -xzf /tmp/cni-plugins.tgz
}
limactl shell default -- sudo -n mkdir -p /etc/cni/net.d
limactl shell default -- sudo -n sh -lc 'test -f /etc/cni/net.d/10-memoh-bridge.conflist' || \
limactl shell default -- sudo -n sh -lc 'printf "%s\n" "{" " \"cniVersion\": \"0.4.0\"," " \"name\": \"memoh-bridge\"," " \"plugins\": [" " {" " \"type\": \"bridge\"," " \"bridge\": \"cni0\"," " \"isGateway\": true," " \"ipMasq\": true," " \"promiscMode\": false," " \"hairpinMode\": true," " \"ipam\": {" " \"type\": \"host-local\"," " \"subnet\": \"10.88.0.0/16\"," " \"routes\": [" " {\"dst\": \"0.0.0.0/0\"}" " ]" " }" " }," " {\"type\": \"portmap\", \"capabilities\": {\"portMappings\": true}}," " {\"type\": \"firewall\"}," " {\"type\": \"tuning\"}" " ]" "}" > /etc/cni/net.d/10-memoh-bridge.conflist'