File size: 4,511 Bytes
d3f76b5
bafd190
d3f76b5
c1c78f1
 
d3f76b5
39cd355
 
c1c78f1
 
 
 
39cd355
7e19122
39cd355
 
 
 
 
 
 
 
 
 
 
 
c1c78f1
05c81af
 
 
bafd190
 
05c81af
bafd190
05c81af
 
 
 
c1c78f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3f76b5
05c81af
 
 
48f162a
d3f76b5
 
 
 
dd8819e
05c81af
 
d3f76b5
 
c44084f
 
 
bafd190
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c44084f
 
d3f76b5
c1c78f1
d3f76b5
 
c1c78f1
d3f76b5
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/bin/bash
# Note: no set -e — sync failures must not kill the container

# Create agentmemory data dir
mkdir -p /home/user/.agentmemory

# =============================================================================
# Persistent storage via HF Dataset repo (free)
# Secrets to set in HF Space settings:
#   HF_TOKEN              — write access to the dataset repo
#   GEMINI_API_KEY        — powers graph, embeddings, compression, crystals
#   AGENTMEMORY_DATASET_REPO — optional override (default: Yash030/agentmemory-data)
# =============================================================================
export AGENTMEMORY_DATASET_REPO="${AGENTMEMORY_DATASET_REPO:-Yash030/agentmemory-data}"

echo "[start] Restoring data from HF Dataset..."
python3 /app/sync.py restore

# Background sync loop — backs up every 5 minutes
(
  while true; do
    sleep 300
    python3 /app/sync.py backup
  done
) &

# Internal service URLs (daemon talks to itself on localhost)
export AGENTMEMORY_URL=http://localhost:3111
export III_ENGINE_URL=ws://localhost:49134

# Build CORS allowed origins — always include HF Space public host
CORS_ORIGINS="http://localhost:3111,http://localhost:3113,http://127.0.0.1:3111,http://127.0.0.1:3113"
if [ -n "${SPACE_HOST}" ]; then
  CORS_ORIGINS="${CORS_ORIGINS},https://${SPACE_HOST},http://${SPACE_HOST}"
  export VIEWER_ALLOWED_HOSTS="${VIEWER_ALLOWED_HOSTS:-${SPACE_HOST},${SPACE_HOST}:443,${SPACE_HOST}:7860}"
  export VIEWER_ALLOWED_ORIGINS="${VIEWER_ALLOWED_ORIGINS:-https://${SPACE_HOST},http://${SPACE_HOST},http://localhost:3111,http://localhost:3113,http://127.0.0.1:3111,http://127.0.0.1:3113}"
fi

# Generate HMAC secret on first boot, persist it so it survives dataset restore
HMAC_FILE="/home/user/.agentmemory/.hmac"
if [ ! -s "$HMAC_FILE" ]; then
  SECRET="$(openssl rand -hex 32)"
  printf '%s\n' "$SECRET" > "$HMAC_FILE"
  chmod 600 "$HMAC_FILE"
  echo "================================================================"
  echo "agentmemory: generated HMAC secret on first boot"
  echo "AGENTMEMORY_SECRET=$SECRET"
  echo "Copy this to your Space secrets as AGENTMEMORY_SECRET."
  echo "It will not be printed again."
  echo "================================================================"
fi
export AGENTMEMORY_SECRET="${AGENTMEMORY_SECRET:-$(cat "$HMAC_FILE")}"

# Write .env config for the daemon
cat > /home/user/.agentmemory/.env <<EOF
GEMINI_API_KEY=${GEMINI_API_KEY}
AGENTMEMORY_SECRET=${AGENTMEMORY_SECRET}
AGENTMEMORY_URL=${AGENTMEMORY_URL}
III_ENGINE_URL=${III_ENGINE_URL}
GEMINI_MODEL=${GEMINI_MODEL:-gemini-3.5-flash}
EMBEDDING_PROVIDER=gemini
CONSOLIDATION_ENABLED=true
GRAPH_EXTRACTION_ENABLED=true
AGENTMEMORY_REFLECT=true
AGENTMEMORY_AUTO_COMPRESS=true
VIEWER_ALLOWED_HOSTS=${VIEWER_ALLOWED_HOSTS}
VIEWER_ALLOWED_ORIGINS=${VIEWER_ALLOWED_ORIGINS}
EOF

# Overwrite the baked iii-config.yaml with runtime values (HF Space CORS + absolute paths)
# The agentmemory binary reads from its own dist/ directory by default
cat > /opt/agentmemory/node_modules/@agentmemory/agentmemory/dist/iii-config.yaml <<EOF
workers:
  - name: iii-http
    config:
      port: 3111
      host: 0.0.0.0
      default_timeout: 180000
      cors:
        allowed_origins: [${CORS_ORIGINS}]
        allowed_methods: [GET, POST, PUT, DELETE, OPTIONS]
  - name: iii-state
    config:
      adapter:
        name: kv
        config:
          store_method: file_based
          file_path: /home/user/.agentmemory/state_store.db
  - name: iii-queue
    config:
      adapter:
        name: builtin
  - name: iii-pubsub
    config:
      adapter:
        name: local
  - name: iii-cron
    config:
      adapter:
        name: kv
  - name: iii-stream
    config:
      port: 3112
      host: 0.0.0.0
      adapter:
        name: kv
        config:
          store_method: file_based
          file_path: /home/user/.agentmemory/stream_store
  - name: iii-observability
    config:
      enabled: true
      service_name: agentmemory
      exporter: memory
      sampling_ratio: 1.0
      metrics_enabled: true
      logs_enabled: true
      logs_console_output: true
  - name: iii-exec
    config:
      exec:
        - node dist/index.mjs
EOF

echo "[start] iii-config.yaml written with CORS origins: ${CORS_ORIGINS}"

# Start agentmemory daemon (binary reads iii-config.yaml from its own dist/ dir)
agentmemory &

# Wait for daemon to be ready
sleep 5

# Start Caddy reverse proxy (serves everything on port 7860)
caddy run --config /app/Caddyfile --adapter caddyfile