File size: 7,723 Bytes
5a81b95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# 🚀 HUGGINGFACE SPACE - KOMPLET SETUP GUIDE

**Space URL:** https://huggingface.co/spaces/Kraft102/widgetdc-cortex  
**Backend API:** https://kraft102-widgetdc-cortex.hf.space/  
**Status:** Deployed ✅ (Environment setup needed)  
**Dato:** 2025-12-12

---

## ✅ HVAD ER GJORT

- ✅ Backend files uploaded til HuggingFace Space
- ✅ Dockerfile oprettet
- ✅ README.md med Space metadata
- ✅ All source files kopieret korrekt
- ✅ Python deployment script virker

---

## 🔧 STEP 1: TILFØJ ENVIRONMENT VARIABLES (GØR NU!)

### Åbn Space Settings

1. Gå til: **https://huggingface.co/spaces/Kraft102/widgetdc-cortex/settings**
2. Scroll ned til **"Variables and secrets"**
3. Klik **"New secret"** for hver variabel

---

### 🔑 API KEYS (Fra din .env fil)

**Copy-paste disse direkte fra din .env fil:**

#### AI Provider Keys:
```
Name: GEMINI_API_KEY
Value: <copy fra .env>

Name: ANTHROPIC_API_KEY
Value: <copy fra .env>

Name: OPENAI_API_KEY  
Value: <copy fra .env>

Name: GOOGLE_API_KEY
Value: <copy fra .env>

Name: DEEPSEEK_API_KEY
Value: <copy fra .env>

Name: DEEPSEEK_BASE_URL
Value: https://api.deepseek.com
```

#### Tool & Service Keys:
```
Name: FIRECRAWL_API_KEY
Value: <copy fra .env>

Name: EXA_API_KEY
Value: <copy fra .env>

Name: BROWSERBASE_API_KEY
Value: <copy fra .env>

Name: HUGGINGFACE_API_KEY
Value: <copy fra .env>
```

---

### 🗄️ DATABASE CREDENTIALS

**VIGTIGT:** HuggingFace Spaces har IKKE persistent storage!  
Du skal bruge remote database services:

#### Neo4j (Graph Database):
```
Name: NEO4J_URI
Value: <din neo4j uri - f.eks. neo4j+s://xxxx.databases.neo4j.io>

Name: NEO4J_USER
Value: neo4j

Name: NEO4J_PASSWORD
Value: <din neo4j password>
```

**Hvis du IKKE har Neo4j endnu:**
→ Opret gratis på: https://neo4j.com/cloud/aura/
→ Vælg "AuraDB Free"
→ Copy connection URI og credentials

#### PostgreSQL (Vector Database):
```
Name: POSTGRES_HOST
Value: <din postgres host>

Name: POSTGRES_PORT
Value: 5432

Name: POSTGRES_USER
Value: <din postgres user>

Name: POSTGRES_PASSWORD
Value: <din postgres password>

Name: POSTGRES_DB
Value: widgetdc

Name: DATABASE_URL
Value: postgresql://<user>:<password>@<host>:5432/widgetdc
```

**Hvis du IKKE har PostgreSQL endnu:**
→ Opret gratis på: https://neon.tech/
→ Copy connection string

#### Redis (Cache):
```
Name: REDIS_URL
Value: <din redis url>
```

**Hvis du IKKE har Redis endnu:**
→ Opret gratis på: https://upstash.com/
→ Copy Redis URL

---

### ⚙️ BACKEND CONFIGURATION

```
Name: NODE_ENV
Value: production

Name: PORT
Value: 7860

Name: LOG_LEVEL
Value: info

Name: CORS_ORIGIN
Value: *
```

---

## 📊 STEP 2: VERIFICER BUILD STATUS

### Check Docker Build Progress

1. Gå til **"Logs"** tab: https://huggingface.co/spaces/Kraft102/widgetdc-cortex/logs
2. Se Docker build progress
3. Vent til "Running" status (~5-10 minutter)

### Forventede Build Logs:
```
Building...
[+] Building Docker image
[1/8] FROM docker.io/library/node:20
[2/8] COPY package*.json ./
[3/8] RUN npm ci
[4/8] COPY . .
[5/8] RUN npm run build
[6/8] EXPOSE 7860
[7/8] CMD ["npm", "start"]
[8/8] Running...
```

### Space Status Indicator:
- 🔵 **Building** - Docker bygger (5-10 min)
- 🟢 **Running** - Backend kører! ✅
- 🔴 **Runtime Error** - Check logs for fejl
- 🟡 **Sleeping** - Space er paused (klik for at wake up)

---

## 🧪 STEP 3: TEST BACKEND

### Når Space viser "Running" status:

#### Test 1: Health Check
```bash
curl https://kraft102-widgetdc-cortex.hf.space/health
```

**Forventet svar:**
```json
{
  "status": "ok",
  "timestamp": "2025-12-12T11:00:00.000Z",
  "uptime": 123.45
}
```

#### Test 2: API Status
```bash
curl https://kraft102-widgetdc-cortex.hf.space/api/status
```

#### Test 3: I Browser
Åbn: https://kraft102-widgetdc-cortex.hf.space/

Du burde se backend response (ikke 404)

---

## 🎨 STEP 4: DEPLOY FRONTEND

### Option A: Netlify (ANBEFALET - Gratis & Hurtig)

```bash
# Install Netlify CLI
npm install -g netlify-cli

# Login til Netlify
netlify login

# Build frontend
cd apps/matrix-frontend
npm run build

# Deploy
netlify deploy --prod --dir=dist

# Note URL som netlify returnerer (f.eks. https://widgetdc.netlify.app)
```

### Option B: Vercel

```bash
# Install Vercel CLI
npm install -g vercel

# Login
vercel login

# Deploy (fra matrix-frontend dir)
cd apps/matrix-frontend
vercel --prod
```

### Option C: Separat HuggingFace Space

```bash
# Opret ny Space for frontend
python -c "from huggingface_hub import create_repo; create_repo('Kraft102/widgetdc-frontend', repo_type='space', space_sdk='static')"

# Build frontend
cd apps/matrix-frontend
npm run build

# Upload
python -c "from huggingface_hub import HfApi; api = HfApi(); api.upload_folder(folder_path='dist', repo_id='Kraft102/widgetdc-frontend', repo_type='space')"
```

---

## 🔗 STEP 5: CONNECT FRONTEND ↔ BACKEND

### Update Frontend Configuration

Opret/opdater `apps/matrix-frontend/.env.production`:

```bash
VITE_API_URL=https://kraft102-widgetdc-cortex.hf.space
VITE_WS_URL=wss://kraft102-widgetdc-cortex.hf.space
VITE_BACKEND_URL=https://kraft102-widgetdc-cortex.hf.space
```

### Rebuild & Redeploy Frontend

```bash
cd apps/matrix-frontend
npm run build
# Deploy igen til chosen platform (netlify/vercel/hf)
```

---

## ✅ STEP 6: END-TO-END VERIFICATION

### Checklist:

- [ ] Backend Space shows "Running" status
- [ ] Health endpoint returns 200 OK
- [ ] All environment variables added
- [ ] Database connections working
- [ ] Frontend deployed
- [ ] Frontend loads in browser
- [ ] Frontend connects to backend
- [ ] No console errors
- [ ] Widgets render correctly
- [ ] Backend logs show no errors

---

## 🚨 TROUBLESHOOTING

### Problem: "Runtime Error" Status

**Løsning:**
1. Check Logs tab
2. Look for missing environment variables
3. Verify database connection strings
4. Check for port conflicts (should be 7860)

### Problem: "Connection Refused"

**Løsning:**
1. Verify Space is "Running" (not "Building" or "Sleeping")
2. Check CORS settings
3. Verify API URL format

###  Problem: Database Connection Errors

**Løsning:**
1. Verify all database credentials
2. Check database is accessible from internet
3. Whitelist HuggingFace IPs if needed
4. Test connection locally first

### Problem: Frontend Can't Connect

**Løsning:**
1. Check `.env.production` variables
2. Verify backend CORS allows frontend domain
3. Check browser console for errors
4. Test API endpoint directly with curl

---

## 📱 MONITORING

### View Space Status:
```bash
python -c "from huggingface_hub import HfApi; api = HfApi(); info = api.repo_info('Kraft102/widgetdc-cortex', repo_type='space'); print(f'Status: {info}')

"
```

### View Logs:
https://huggingface.co/spaces/Kraft102/widgetdc-cortex/logs

### Restart Space:
Settings → Factory Reboot

---

## 🎯 QUICK START COMMANDS

### Check hvis Space er live:
```bash
curl -I https://kraft102-widgetdc-cortex.hf.space/health
```

### Test WebSocket:
```javascript
const ws = new WebSocket('wss://kraft102-widgetdc-cortex.hf.space');
ws.onopen = () => console.log('Connected!');
```

### Deploy Frontend til Netlify:
```bash
cd apps/matrix-frontend && npm run build && netlify deploy --prod --dir=dist
```

---

## 📞 NÆSTE SKRIDT

### RIGHT NOW:
1. ✅ Gå til https://huggingface.co/spaces/Kraft102/widgetdc-cortex/settings
2. ✅ Add ALLE environment variables (især API keys)
3. ✅ Vent på build completion (~5-10 min)
4. ✅ Test health endpoint

### THEN:
5. ✅ Deploy frontend (Netlify anbefalet)
6. ✅ Configure frontend environment
7. ✅ Test complete flow
8. ✅ You're LIVE! 🎉

---

**Du er 90% færdig!** 🚀  
Fortæl mig når environment variables er tilføjet!