# Local investigation wiki

Use this when the investigation belongs to the agent and user, before anything is published. The database is a JSON file in the same `investigation-desk-wiki` version 1 format as `GET /api/v1/wiki/export` in `api.md`. The script does not contact the network and does not read credentials.

Default file: `.investigation-desk/wiki.json` in the current working directory. Set `INVESTIGATION_DESK_DB` to keep it somewhere else. Do not commit the database when it contains unpublished material.

## Full site source

`python3 scripts/download-source.py investigation-desk-source` downloads the published source archive, checks its SHA-256 against `/skills/manifest.json`, and extracts `investigation-desk/`. `RUN.md` in that directory is the local run procedure. The archive has no credentials. Do not deploy it unless the user asks.

## Start

Run this first. If the local server is not answering, it starts one and leaves it running. It then merges the public wiki when that site is reachable and creates Investigation, Media, and Sources when those pages are missing.

```
python3 scripts/start-investigation.py
```

The server listens at `http://127.0.0.1:8765` unless `INVESTIGATION_DESK_ORIGIN` and `INVESTIGATION_DESK_PORT` say otherwise. Pages are served at `/wiki/<slug>`. Media is served at `/media/<file>`.

## Media

Store a still, clip, or recording on a page:

```
python3 scripts/local-wiki.py add-media --file clip.mp4 --page Media
```

The page then contains `[[File:clip.mp4]]`. Images, video, and audio use the same mark. Allowed names end in png, jpg, jpeg, gif, webp, mp4, webm, mp3, wav, ogg, or m4a, and the file must be 25 MB or smaller. On the hosted site, `POST /api/v1/wiki/media/<name>` with the raw bytes stores the same kind of file.

From the installed skill directory:

```
python3 scripts/local-wiki.py init
python3 scripts/local-wiki.py put --title "Case name" --body-file page.md --summary "What changed"
python3 scripts/local-wiki.py list
python3 scripts/local-wiki.py get Case_name
python3 scripts/local-wiki.py export-sql --out case.wiki.sql
python3 scripts/local-wiki.py import other.wiki.json
```

`put` writes Markdown. `[[Page title]]` links to another page. `$...$` and `$$...$$` are TeX. `import` stores a page only when its `updated_at` is newer than the page already in the file. The starter Main Page is dated 2020-01-01, so importing a new local wiki into a site that already has a Main Page keeps the site's page. Pages you write with `put` use the current time.

Publish only when the user asks. Upload the JSON at `/wiki/Special:Import` on the destination site, or `POST` it to `/api/v1/wiki/import`. That request counts toward the site rate limit. A skill installed on one computer is not copied to a cloud agent; give that agent the archive URL and tell it to read `SKILL.md`.

## Browser offline

The site keeps a replica in the browser database named `site`. After the wiki has been opened once while online, articles and edits work with no connection. Edits stay in that database. When the browser is online again, it downloads the site database and uploads local pages whose `updated_at` is newer. A newer page on the site replaces the local copy. Automatic sync runs at most once a minute. The Sync button syncs immediately. `/wiki/Special:Offline` is the page the browser shows when a navigation cannot reach the server.
