浏览代码

fix: read tools should always get correct data

via db-worker thread. Also fix getPageData returning
some temp and untranslated attributes
Gabriel Horner 3 周之前
父节点
当前提交
917df54d74
共有 2 个文件被更改,包括 57 次插入30 次删除
  1. 39 18
      src/main/frontend/worker/db_worker.cljs
  2. 18 12
      src/main/logseq/api.cljs

+ 39 - 18
src/main/frontend/worker/db_worker.cljs

@@ -37,6 +37,7 @@
             [goog.object :as gobj]
             [lambdaisland.glogi :as log]
             [lambdaisland.glogi.console :as glogi-console]
+            [logseq.cli.common.mcp.tools :as cli-common-mcp-tools]
             [logseq.common.util :as common-util]
             [logseq.db :as ldb]
             [logseq.db.common.entity-plus :as entity-plus]
@@ -482,18 +483,18 @@
 (def-thread-api :thread-api/block-refs-check
   [repo id {:keys [unlinked?]}]
   (m/sp
-    (when-let [conn (worker-state/get-datascript-conn repo)]
-      (let [db @conn
-            block (d/entity db id)]
-        (if unlinked?
-          (let [title (string/lower-case (:block/title block))
-                result (m/? (search-blocks repo title {:limit 100}))]
-            (boolean (some (fn [b]
-                             (let [block (d/entity db (:db/id b))]
-                               (and (not= id (:db/id block))
-                                    (not ((set (map :db/id (:block/refs block))) id))
-                                    (string/includes? (string/lower-case (:block/title block)) title)))) result)))
-          (some? (first (common-initial-data/get-block-refs db (:db/id block)))))))))
+   (when-let [conn (worker-state/get-datascript-conn repo)]
+     (let [db @conn
+           block (d/entity db id)]
+       (if unlinked?
+         (let [title (string/lower-case (:block/title block))
+               result (m/? (search-blocks repo title {:limit 100}))]
+           (boolean (some (fn [b]
+                            (let [block (d/entity db (:db/id b))]
+                              (and (not= id (:db/id block))
+                                   (not ((set (map :db/id (:block/refs block))) id))
+                                   (string/includes? (string/lower-case (:block/title block)) title)))) result)))
+         (some? (first (common-initial-data/get-block-refs db (:db/id block)))))))))
 
 (def-thread-api :thread-api/get-block-parents
   [repo id depth]
@@ -782,6 +783,26 @@
   (when-let [conn (worker-state/get-datascript-conn repo)]
     (ldb/get-graph-rtc-uuid @conn)))
 
+(def-thread-api :thread-api/api-get-page-data
+  [repo page-title]
+  (let [conn (worker-state/get-datascript-conn repo)]
+    (cli-common-mcp-tools/get-page-blocks @conn page-title)))
+
+(def-thread-api :thread-api/api-list-properties
+  [repo]
+  (let [conn (worker-state/get-datascript-conn repo)]
+    (cli-common-mcp-tools/list-properties @conn)))
+
+(def-thread-api :thread-api/api-list-tags
+  [repo]
+  (let [conn (worker-state/get-datascript-conn repo)]
+    (cli-common-mcp-tools/list-tags @conn)))
+
+(def-thread-api :thread-api/api-list-pages
+  [repo]
+  (let [conn (worker-state/get-datascript-conn repo)]
+    (cli-common-mcp-tools/list-pages @conn)))
+
 (comment
   (def-thread-api :general/dangerousRemoveAllDbs
     []
@@ -835,14 +856,14 @@
   [repo start-opts]
   (js/Promise.
    (m/sp
-     (c.m/<? (init-sqlite-module!))
-     (when-not (:import-type start-opts)
-       (c.m/<? (start-db! repo start-opts))
-       (assert (some? (worker-state/get-datascript-conn repo))))
+    (c.m/<? (init-sqlite-module!))
+    (when-not (:import-type start-opts)
+      (c.m/<? (start-db! repo start-opts))
+      (assert (some? (worker-state/get-datascript-conn repo))))
      ;; Don't wait for rtc started because the app will be slow to be ready
      ;; for users.
-     (when @worker-state/*rtc-ws-url
-       (rtc.core/new-task--rtc-start true)))))
+    (when @worker-state/*rtc-ws-url
+      (rtc.core/new-task--rtc-start true)))))
 
 (def broadcast-data-types
   (set (map

+ 18 - 12
src/main/logseq/api.cljs

@@ -48,7 +48,6 @@
             [goog.object :as gobj]
             [lambdaisland.glogi :as log]
             [logseq.api.block :as api-block]
-            [logseq.cli.common.mcp.tools :as cli-common-mcp-tools]
             [logseq.common.util :as common-util]
             [logseq.common.util.date-time :as date-time-util]
             [logseq.db :as ldb]
@@ -1178,24 +1177,31 @@
 ;; TODO: Use transit for internal APIs
 (defn ^:export list_tags
   []
-  (clj->js (cli-common-mcp-tools/list-tags (db/get-db))))
+  (p/let [resp (state/<invoke-db-worker :thread-api/api-list-tags (state/get-current-repo))]
+    (clj->js resp)))
 
 (defn ^:export list_properties
   []
-  (clj->js (cli-common-mcp-tools/list-properties (db/get-db))))
+  (p/let [resp (state/<invoke-db-worker :thread-api/api-list-properties (state/get-current-repo))]
+    (clj->js resp)))
+
+(defn ^:export list_pages
+  []
+  (p/let [resp (state/<invoke-db-worker :thread-api/api-list-pages (state/get-current-repo))]
+    (clj->js resp)))
 
 (defn ^:export get_page_data
   "Like get_page_blocks_tree but for MCP tools"
   [page-title]
-  (if-let [tools (cli-common-mcp-tools/get-page-blocks (db/get-db) page-title)]
-    (->> tools
-         (map #(dissoc % :block.temp/has-children? :block.temp/load-status))
-         clj->js)
-    #js {:error (str "Page " (pr-str page-title) " not found")}))
-
-(defn ^:export list_pages
-  []
-  (clj->js (cli-common-mcp-tools/list-pages (db/get-db))))
+  (p/let [tools (state/<invoke-db-worker :thread-api/api-get-page-data (state/get-current-repo) page-title)]
+    (if tools
+      (->> tools
+           (map #(-> %
+                     sdk-utils/remove-hidden-properties
+                     ;; remove unused and untranslated attrs
+                     (dissoc :block/children :block/page)))
+           clj->js)
+      #js {:error (str "Page " (pr-str page-title) " not found")})))
 
 ;; ui
 (def ^:export show_msg sdk-ui/-show_msg)