1
0
Эх сурвалжийг харах

enhance(db-sync): protocol.md add http endpoints

fix: make t_before mandatory
rcmerci 1 долоо хоног өмнө
parent
commit
9f530799b6

+ 8 - 1
deps/db-sync/src/logseq/db_sync/worker.cljs

@@ -290,10 +290,17 @@
 
 (defn- handle-tx-batch! [^js self sender txs t-before]
   (let [current-t (t-now self)]
-    (if (and (number? t-before) (not= t-before current-t))
+    (cond
+      (not (number? t-before))
+      {:type "tx/reject"
+       :reason "invalid t_before"}
+
+      (not= t-before current-t)
       {:type "tx/reject"
        :reason "stale"
        :t current-t}
+
+      :else
       (let [tx-data (mapcat protocol/transit->tx txs)]
         (if (seq tx-data)
           (let [new-t (apply-tx! self sender tx-data)]

+ 4 - 0
docs/agent-guide/db-sync/db-sync-guide.md

@@ -41,6 +41,10 @@ This guide helps AI agents implement and review db-sync features consistently ac
   - `{"type":"pong"}`: keepalive response.
   - `{"type":"error","message":"..."}`: invalid/unknown message.
 
+## HTTP API (Bootstrap + Assets)
+- HTTP endpoints backfill initial graph data, snapshots, and assets.
+- See `docs/agent-guide/db-sync/protocol.md` for request/response shapes.
+
 ## Testing & Verification
 - Local dev(client+server): `bb dev:db-sync-start` runs the db-sync watcher, `wrangler dev`, and `yarn watch` with `ENABLE_DB_SYNC_LOCAL=true`
 - Unit tests: `bb dev:lint-and-test`

+ 40 - 2
docs/agent-guide/db-sync/protocol.md

@@ -12,7 +12,7 @@
 - `{"type":"pull","since":<t>}`
   - Request txs after `since` (defaults to 0).
 - `{"type":"tx/batch","t_before":<t>,"txs":["<tx-transit>", ...]}`
-  - Upload a batch of txs based on `t_before`.
+  - Upload a batch of txs based on `t_before` (required).
 - `{"type":"ping"}`
   - Optional keepalive; server replies `pong`.
 
@@ -29,9 +29,47 @@
   - Client tx is based on stale t.
 - `{"type":"tx/reject","reason":"cycle","data":"<transit {:attr <kw> :server_values ...}>"}`
   - Cycle detected with server values.
-- `{"type":"tx/reject","reason":"empty tx data"|"invalid tx"}`
+- `{"type":"tx/reject","reason":"empty tx data"|"invalid tx"|"invalid t_before"}`
   - Invalid batch.
 - `{"type":"pong"}`
   - Keepalive response.
 - `{"type":"error","message":"..."}`
   - Invalid/unknown message.
+
+## HTTP API
+- Auth: Bearer token via `Authorization: Bearer <token>` or `?token=...`.
+- JSON body/response unless noted.
+
+### Graphs (index DO)
+- `GET /graphs`
+  - List graphs the user owns. Response: `{"graphs":[{graph_id, graph_name, schema_version, created_at, updated_at}...]}`.
+- `POST /graphs`
+  - Create graph. Body: `{"graph_name":"...","schema_version":"<major>"}`. Response: `{"graph_id":"..."}`.
+- `GET /graphs/:graph-id/access`
+  - Access check. Response: `{"ok":true}` or `403`.
+- `DELETE /graphs/:graph-id`
+  - Delete graph and reset data. Response: `{"graph_id":"...","deleted":true}`.
+
+### Sync (per-graph DO, via `/sync/:graph-id/...`)
+- `GET /sync/:graph-id/health`
+  - Health check. Response: `{"ok":true}`.
+- `GET /sync/:graph-id/pull?since=<t>`
+  - Same as WS pull. Response: `{"type":"pull/ok","t":<t>,"txs":[{"t":<t>,"tx":"<tx-transit>"}...]}`.
+- `POST /sync/:graph-id/tx/batch`
+  - Same as WS tx/batch. Body: `{"t_before":<t>,"txs":["<tx-transit>", ...]}`.
+  - Response: `{"type":"tx/batch/ok","t":<t>}` or `{"type":"tx/reject","reason":...}`.
+- `GET /sync/:graph-id/snapshot/rows?after=<addr>&limit=<n>`
+  - Pull sqlite kvs rows. Response: `{"rows":[[addr,content,addresses]...],"last_addr":<addr>,"done":true|false}`.
+- `POST /sync/:graph-id/snapshot/import`
+  - Import sqlite kvs rows. Body: `{"reset":true|false,"rows":[[addr,content,addresses]...]}`.
+  - Response: `{"ok":true,"count":<n>}`.
+- `DELETE /sync/:graph-id/admin/reset`
+  - Drop/recreate per-graph tables. Response: `{"ok":true}`.
+
+### Assets
+- `GET /assets/:graph-id/:asset-uuid.:ext`
+  - Download asset (binary response, `content-type` set).
+- `PUT /assets/:graph-id/:asset-uuid.:ext`
+  - Upload asset (binary body). Size limit ~100MB. Response: `{"ok":true}`.
+- `DELETE /assets/:graph-id/:asset-uuid.:ext`
+  - Delete asset. Response: `{"ok":true}`.