Skip to content

Commit dc76a94

Browse files
fix: edit block operation shouldn't require page id
Address feedback. This was an implementation detail that added a needless constraint
1 parent 0422d75 commit dc76a94

File tree

2 files changed

+52
-36
lines changed

2 files changed

+52
-36
lines changed

deps/cli/src/logseq/cli/common/mcp/server.cljs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@
143143
* :id - For :edit, this _must_ be a string uuid. For :add, use a temporary unique string if the new page is referenced by later operations e.g. add blocks
144144
* :data - A map of fields to set or update. This map can have the following keys:
145145
* :title - A page/tag/property's name or a block's content
146-
* :page-id - A page string uuid of a block. Required when entityType is :block.
146+
* :page-id - A page string uuid of a block. Required when adding a block.
147147
* :tags - A list of tags as string uuids
148148
* :property-type - A property's type
149149
* :property-cardinality - A property's cardinality. Must be :one or :many
@@ -181,7 +181,6 @@
181181
:tags [\"00000002-1282-1814-5700-000000000000\"]}}]}
182182
183183
Additional advice for building operations:
184-
* When building a block update operation, use the 'page' key of the searchBlocks tool to fill in the value of :page-id under :data
185184
* Before creating any page, tag or property, check that it exists with getPage"
186185
:inputSchema
187186
#js {:operations

deps/cli/src/logseq/cli/common/mcp/tools.cljs

Lines changed: 51 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@
3030
(map (fn [e]
3131
(if expand
3232
(cond-> (into {} e)
33-
true
34-
(dissoc e :block/tags :block/order :block/refs :block/name :db/index
35-
:logseq.property.embedding/hnsw-label-updated-at :logseq.property/default-value)
36-
true
37-
(update :block/uuid str)
38-
(:logseq.property/classes e)
39-
(update :logseq.property/classes #(mapv :db/ident %))
40-
(:logseq.property/description e)
41-
(update :logseq.property/description db-property/property-value-content))
33+
true
34+
(dissoc e :block/tags :block/order :block/refs :block/name :db/index
35+
:logseq.property.embedding/hnsw-label-updated-at :logseq.property/default-value)
36+
true
37+
(update :block/uuid str)
38+
(:logseq.property/classes e)
39+
(update :logseq.property/classes #(mapv :db/ident %))
40+
(:logseq.property/description e)
41+
(update :logseq.property/description db-property/property-value-content))
4242
{:block/title (:block/title e)
4343
:block/uuid (str (:block/uuid e))})))))
4444

@@ -130,16 +130,47 @@
130130
(or (get idents title)
131131
(throw (ex-info (str "No ident found for " (pr-str title)) {}))))
132132

133+
(defn- build-add-block [op {:keys [class-idents]}]
134+
(cond-> {:block/title (get-in op [:data :title])}
135+
(get-in op [:data :tags])
136+
(assoc :build/tags (mapv #(get-ident class-idents %) (get-in op [:data :tags])))))
137+
138+
(defn- ops->existing-pages-and-blocks
139+
"Converts block operations for existing pages and prepares them for :pages-and-blocks"
140+
[db operations idents]
141+
(let [new-blocks-for-existing-pages
142+
(->> (filter #(and (= "block" (:entityType %))
143+
(= "add" (:operation %))
144+
(common-util/uuid-string? (get-in % [:data :page-id]))) operations)
145+
(map (fn [op] (assoc op ::page-id (uuid (get-in op [:data :page-id]))))))
146+
edit-blocks
147+
(->> (filter #(and (= "block" (:entityType %)) (= "edit" (:operation %))) operations)
148+
(map (fn [op]
149+
(let [block-uuid (uuid (:id op))
150+
ent (d/entity db [:block/uuid block-uuid])]
151+
(when-not (:block/page ent)
152+
(throw (ex-info "Block edit operation requires a block to have a page." {})))
153+
(assoc op ::page-id (get-in ent [:block/page :block/uuid]))))))]
154+
(->> (concat new-blocks-for-existing-pages edit-blocks)
155+
(group-by ::page-id)
156+
(map (fn [[page-id ops]]
157+
{:page {:block/uuid page-id}
158+
:blocks (mapv (fn [op]
159+
(prn :block-op op)
160+
(if (= "add" (:operation op))
161+
(build-add-block op idents)
162+
;; edit :block
163+
(cond-> {:block/uuid (uuid (:id op))}
164+
(get-in op [:data :title])
165+
(assoc :block/title (get-in op [:data :title])))))
166+
ops)})))))
167+
133168
(defn- ops->pages-and-blocks
134-
[operations {:keys [class-idents]}]
135-
(let [blocks-by-page
169+
[db operations idents]
170+
(let [new-blocks-by-page
136171
(group-by #(get-in % [:data :page-id])
137-
(filter #(= "block" (:entityType %)) operations))
172+
(filter #(and (= "block" (:entityType %)) (= "add" (:operation %))) operations))
138173
new-pages (filter #(and (= "page" (:entityType %)) (= "add" (:operation %))) operations)
139-
add-block (fn add-block [op]
140-
(cond-> {:block/title (get-in op [:data :title])}
141-
(get-in op [:data :tags])
142-
(assoc :build/tags (mapv #(get-ident class-idents %) (get-in op [:data :tags])))))
143174
pages-and-blocks
144175
(into (mapv (fn [op]
145176
(cond-> {:page (if-let [journal-day (date-time-util/journal-title->int
@@ -148,24 +179,11 @@
148179
(date-time-util/safe-journal-title-formatters nil))]
149180
{:build/journal journal-day}
150181
{:block/title (get-in op [:data :title])})}
151-
(some->> (:id op) (get blocks-by-page))
182+
(some->> (:id op) (get new-blocks-by-page))
152183
(assoc :blocks
153-
(mapv add-block (get blocks-by-page (:id op))))))
184+
(mapv #(build-add-block % idents) (get new-blocks-by-page (:id op))))))
154185
new-pages)
155-
;; existing pages
156-
(map (fn [[page-id ops]]
157-
(when-not (common-util/uuid-string? page-id)
158-
(throw (ex-info (str "Existing page id " (pr-str page-id) " must be a uuid") {})))
159-
{:page {:block/uuid (uuid page-id)}
160-
:blocks (mapv (fn [op]
161-
(if (= "add" (:operation op))
162-
(add-block op)
163-
;; edit
164-
(cond-> {:block/uuid (uuid (:id op))}
165-
(get-in op [:data :title])
166-
(assoc :block/title (get-in op [:data :title])))))
167-
ops)})
168-
(apply dissoc blocks-by-page (map :id new-pages))))]
186+
(ops->existing-pages-and-blocks db operations idents))]
169187
pages-and-blocks))
170188

171189
(defn- ops->classes
@@ -299,7 +317,6 @@
299317
[:id uuid-string]
300318
;; :tags not supported yet
301319
[:data [:map {:closed true}
302-
[:page-id uuid-string]
303320
[:title :string]]]]]
304321
;; other edit's
305322
[::m/default [:map [:id uuid-string]]]]])
@@ -365,7 +382,7 @@
365382
(throw (ex-info (str "Tool arguments are invalid:\n" (me/humanize errors))
366383
{:errors errors})))
367384
idents (operations->idents db operations)
368-
pages-and-blocks (ops->pages-and-blocks operations idents)
385+
pages-and-blocks (ops->pages-and-blocks db operations idents)
369386
classes (ops->classes operations idents)
370387
properties (ops->properties operations idents)
371388
import-edn

0 commit comments

Comments
 (0)