# Prompt: Obsidian LiveSync client-side setup voor AI agents

## Doel

Installeer en configureer de Self-hosted LiveSync plugin voor Obsidian op een Linux/macOS/Windows workstation. De plugin synchroniseert een lokale Obsidian vault met een remote CouchDB database.

## Vereisten

- Obsidian is geïnstalleerd op de machine
- Er is een Obsidian vault aanwezig (local of gesynct via Nextcloud/WebDAV)
- CouchDB is bereikbaar vanaf deze machine (Tailscale IP of LAN)
- Je hebt toegang tot een terminal met schrijfrechten op de vault

## Stappenplan

### 1. Vind de Obsidian vault(s)

Zoek naar mappen met `.obsidian/` subdirectory:
```bash
find ~ -maxdepth 5 -name ".obsidian" -type d 2>/dev/null
```

Bepaal welke vault de juiste is. Als er meerdere zijn (bijv. origineel en een kopie), vraag de gebruiker.

Controleer of er al een `community-plugins.json` bestaat en wat de inhoud is.

### 2. Verwijder oude sync plugins (optioneel maar aanbevolen)

Als er andere sync plugins aanwezig zijn in `.obsidian/plugins/` (bijv. `remotely-save`, `Obsidian Sync`), verwijder deze dan:
```bash
rm -rf "/pad/naar/vault/.obsidian/plugins/remotely-save"
```

De LiveSync plugin waarschuwt dat andere sync oplossingen uitgeschakeld moeten worden om corruptie te voorkomen.

### 3. Download de LiveSync plugin

Haal de laatste release op van GitHub:
```bash
RELEASE_URL=$(curl -sL "https://api.github.com/repos/vrtmrz/obsidian-livesync/releases/latest" | python3 -c "import sys,json; d=json.load(sys.stdin); print(d['tag_name'])")
```

Download de drie benodigde bestanden:
- `main.js`
- `manifest.json`
- `styles.css`

Plaats ze in:
```
<pad/naar/vault>/.obsidian/plugins/obsidian-livesync/
```

### 4. Vraag CouchDB credentials aan de gebruiker

De agent mag deze nooit zelf verzinnen of uit code halen. Vraag:
- `COUCHDB_URI` — bijv. `http://<hostname>:5984` of `https://<domein>`
- `COUCHDB_USER` — gebruikersnaam met admin rechten op CouchDB
- `COUCHDB_PASSWORD` — bijbehorend wachtwoord
- `COUCHDB_DB_NAME` — database naam (bijv. `obsidian`)

**De agent mag deze credentials nooit hardcoded in prompts, logs, of gedeelde bestanden achterlaten.**

### 5. Valideer de CouchDB verbinding

```bash
curl -s -u "$USER:$PASS" "$URI/" --connect-timeout 10
```

Controleer of het antwoord `{"couchdb":"Welcome","version":"3.x.x",...}` is.

### 6. Maak de database aan (als deze nog niet bestaat)

```bash
curl -s -u "$USER:$PASS" -X PUT "$URI/$DB_NAME"
```

Controleer of de database in de lijst staat:
```bash
curl -s -u "$USER:$PASS" "$URI/_all_dbs"
```

### 7. Configureer CORS (voor mobiele clients)

Alleen nodig als er ook via Obsidian Mobile (iOS/Android) gesynct wordt. De desktop app heeft geen CORS nodig.

```bash
# Enable CORS
curl -s -u "$USER:$PASS" -X PUT "$URI/_node/nonode@nohost/_config/chttpd/enable_cors" \
  -H "Content-Type: application/json" -d '"true"'

# Set credentials
curl -s -u "$USER:$PASS" -X PUT "$URI/_node/nonode@nohost/_config/chttpd/credentials" \
  -H "Content-Type: application/json" -d '"true"'

# Set allowed origins
curl -s -u "$USER:$PASS" -X PUT "$URI/_node/nonode@nohost/_config/chttpd/origins" \
  -H "Content-Type: application/json" \
  -d '"app://obsidian.md,capacitor://localhost,http://localhost"'
```

### 8. Stel max_document_size in (aanbevolen)

```bash
curl -s -u "$USER:$PASS" -X PUT "$URI/_node/nonode@nohost/_config/couchdb/max_document_size" \
  -H "Content-Type: application/json" -d '"50000000"'
```

### 9. Configureer de plugin (data.json)

Schrijf `.obsidian/plugins/obsidian-livesync/data.json` met de volgende minimale instellingen (vul de `{{placeholders}}` in):

```json
{
  "remoteType": "",
  "couchDB_URI": "{{COUCHDB_URI}}",
  "couchDB_USER": "{{COUCHDB_USER}}",
  "couchDB_PASSWORD": "{{COUCHDB_PASSWORD}}",
  "couchDB_DBNAME": "{{COUCHDB_DB_NAME}}",
  "liveSync": true,
  "syncOnSave": true,
  "syncOnStart": true,
  "periodicReplication": true,
  "periodicReplicationInterval": 60,
  "syncOnFileOpen": false,
  "trashInsteadDelete": true,
  "resolveConflictsByNewerFile": true,
  "deviceAndVaultName": "{{MACHINE_NAAM}}",
  "usePluginSettings": true,
  "usePluginSync": true,
  "usePluginSyncV2": true,
  "usePluginEtc": true,
  "syncInternalFiles": true,
  "syncInternalFilesBeforeReplication": true,
  "skipOlderFilesOnSync": true,
  "readChunksOnline": true,
  "showStatusOnEditor": true,
  "showStatusOnStatusbar": true
}
```

De plugin vult de overige instellingen met defaults aan zodra deze een keer geladen is.

### 10. Schakel de plugin in

Schrijf `.obsidian/plugins/community-plugins.json`:
```json
["obsidian-livesync"]
```

Als er al andere plugins in staan, voeg `obsidian-livesync` dan toe aan de array, bijv:
```json
["bestaande-plugin", "obsidian-livesync"]
```

### 11. Eindcontrole

- ✅ CouchDB is bereikbaar en reageert
- ✅ Database `{{DB_NAME}}` bestaat
- ✅ Plugin bestanden aanwezig: `main.js`, `manifest.json`, `styles.css`
- ✅ Configuratiebestand `data.json` is geschreven met credentials
- ✅ Plugin staat in `community-plugins.json`
- ✅ Oude sync plugins zijn verwijderd
- ✅ CORS staat aan (indien nodig)
- ✅ max_document_size is voldoende hoog

### 12. Instructeer de gebruiker

- Herstart Obsidian
- Na herstart begint de synchronisatie automatisch
- Bij een nieuw apparaat kan de configuratie worden overgenomen via de QR-code of connection string in de LiveSync instellingen van een reeds werkend apparaat
- Controleer of bestanden zichtbaar zijn in CouchDB: `curl -s -u "$USER:$PASS" "$URI/$DB_NAME/_all_docs?limit=5"`

## Valkuilen

| Probleem | Oplossing |
|---|---|
| CouchDB niet bereikbaar op poort 5984 | Check of de container draait en of de poort exposed is op het juiste netwerk (Tailscale/LAN) |
| SSH keys werken niet | Gebruik het juiste identity file of vraag de gebruiker om SSH toegang |
| Plugin laadt niet | Check of de plugin in `community-plugins.json` staat en of de bestanden correct zijn |
| Sync start niet | Herstart Obsidian en controleer `data.json` op typfouten in credentials |
| `_node` endpoint niet bereikbaar via reverse proxy | Gebruik directe poort 5984 in plaats van nginx proxy voor CORS configuratie |
| Credentials gelekt in output | **Redacteer altijd** credentials uit logs, bestanden, en prompts voordat je deze deelt |

## Beperkingen

- De plugin moet een keer geladen worden in Obsidian om volledige initialisatie te doen (PouchDB IndexedDB aanmaken, `encryptedCouchDBConnection` genereren, etc.)
- Een agent kan de plugin wel installeren en configureren, maar kan niet verifiëren dat de sync daadwerkelijk draait zonder Obsidian te openen
- CouchDB CORS configuratie is alleen nodig voor Obsidian Mobile — de desktop app gebruikt Node.js HTTP direct

## Voorbeeld van een veilige (geredacte) prompt

> Installeer Self-hosted LiveSync voor mijn Obsidian vault in `~/Nextcloud/Vault`.
> CouchDB draait op mijn server.
> Credentials:
> - URI: `http://COUCHDB_HOST:5984`
> - User: `COUCHDB_USER`
> - Pass: `[GEREDACTEERD]`
> - DB: `obsidian`
> Schakel customization sync en hidden file sync in.
> Verwijder de oude remotely-save plugin.
