File size: 1,705 Bytes
4f54dc8
7472e7a
 
 
4f54dc8
 
7472e7a
4f54dc8
 
7472e7a
4f54dc8
7472e7a
 
 
 
 
 
 
 
4f54dc8
 
7472e7a
 
4f54dc8
 
 
 
7472e7a
 
 
 
 
 
 
 
4f54dc8
 
 
 
7472e7a
4f54dc8
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/bin/sh
# 1) 若设置了 XDG_DATA_HOME:视为直接使用外部存储(挂载点),只启动 opencode,不做任何同步。
# 2) 若设置了 HF_TOKEN + OPENCODE_DATASET_REPO:从 Dataset 恢复,并用 inotify 在「有改动时」同步到 Dataset。
# 3) 否则:直接启动 opencode serve(与原先行为一致)。

set -e
DATA_DIR="${XDG_DATA_HOME:-$HOME/.local/share/opencode}"
OPENCODE_BIN="/home/user/.opencode/bin/opencode"
SCRIPT_DIR="/home/user/app/scripts"
DEBOUNCE=20

# 1) 直接使用外部存储:不恢复、不同步,只启动
if [ -n "$XDG_DATA_HOME" ]; then
  exec $OPENCODE_BIN serve --port 7860 --hostname 0.0.0.0 \
    --cors https://tacits-candy-shop.vercel.app \
    --cors https://opencode-web-pearl.vercel.app
fi

# 2) Dataset 模式:恢复 + 有改动就同步(inotify + 防抖)
if [ -n "$HF_TOKEN" ] && [ -n "$OPENCODE_DATASET_REPO" ]; then
  python3 "$SCRIPT_DIR/restore_from_dataset.py" || true
  mkdir -p "$DATA_DIR"
  trap 'kill -TERM $OPENCODE_PID 2>/dev/null; kill $INOTIFY_PID 2>/dev/null' TERM INT
  $OPENCODE_BIN serve --port 7860 --hostname 0.0.0.0 \
    --cors https://tacits-candy-shop.vercel.app \
    --cors https://opencode-web-pearl.vercel.app &
  OPENCODE_PID=$!
  (
    while kill -0 $OPENCODE_PID 2>/dev/null; do
      inotifywait -r -q -e modify,create,delete,move "$DATA_DIR" 2>/dev/null || sleep 60
      sleep $DEBOUNCE
      python3 "$SCRIPT_DIR/save_to_dataset.py" || true
    done
  ) &
  INOTIFY_PID=$!
  wait $OPENCODE_PID
  exit $?
fi

# 3) 未配置:直接启动
exec $OPENCODE_BIN serve --port 7860 --hostname 0.0.0.0 \
  --cors https://tacits-candy-shop.vercel.app \
  --cors https://opencode-web-pearl.vercel.app