fix: route agent bridge tasks from id datoms

This commit is contained in:
rcmerci
2026-05-25 18:04:30 +08:00
parent 6cb5d999ca
commit 30498f8fd2
2 changed files with 108 additions and 10 deletions

View File

@@ -1083,13 +1083,41 @@
(or (string/includes? (:v datom) (str "[[" agent-name "]]"))
(string/includes? (:v datom) "[["))))
(defn- task-routability-datom?
[datom]
(and (true? (:added datom))
(or (and (= :block/tags (:a datom))
(= :logseq.class/Task (:v datom)))
(and (= :logseq.property/status (:a datom))
(= :logseq.property/status.todo (:v datom))))))
(def ^:private routability-entity-selector
[:db/id :db/ident])
(defn- task-routability-attr?
[attr]
(or (= :block/tags attr)
(= :logseq.property/status attr)))
(defn- task-routability-ident?
[attr ident]
(case attr
:block/tags (= :logseq.class/Task ident)
:logseq.property/status (= :logseq.property/status.todo ident)
false))
(defn- resolve-routability-datom-ident
[cfg repo datom]
(let [value (:v datom)]
(if (keyword? value)
(p/resolved value)
(p/let [entity (transport/invoke cfg :thread-api/pull [repo routability-entity-selector value])]
(:db/ident entity)))))
(defn- resolve-routability-datoms
[cfg repo tx-data]
(let [candidates (filter #(and (true? (:added %))
(task-routability-attr? (:a %)))
tx-data)]
(p/let [resolved (p/all
(mapv (fn [datom]
(p/let [ident (resolve-routability-datom-ident cfg repo datom)]
(when (task-routability-ident? (:a datom) ident)
datom)))
candidates))]
(keep identity resolved))))
(defn- pull-assignee-property
[cfg repo]
@@ -1294,9 +1322,9 @@
(defn- process-sync-db-changes-event!
[cfg {:keys [repo] :as opts} {:keys [tx-data]}]
(p/let [assignee-datoms (resolve-assignee-datoms cfg repo tx-data)]
(let [routability-datoms (filter task-routability-datom? tx-data)
comment-datoms (filter #(comment-title-datom? % (:agent-name opts)) tx-data)
(p/let [assignee-datoms (resolve-assignee-datoms cfg repo tx-data)
routability-datoms (resolve-routability-datoms cfg repo tx-data)]
(let [comment-datoms (filter #(comment-title-datom? % (:agent-name opts)) tx-data)
routing (vec (concat (map #(route-assignee-datom! cfg opts %) assignee-datoms)
(map #(route-routability-datom! cfg opts %) routability-datoms)
(map #(route-comment-datom! cfg opts %) comment-datoms)))]

View File

@@ -518,6 +518,76 @@
(:template (ex-data e))))))
(p/finally done)))))
(deftest test-agent-bridge-listener-routes-id-valued-task-routability-datoms
(async done
(let [calls* (atom [])
task (task-block {})
route-opts {:repo "logseq_db_demo"
:graph "demo"
:agent-name "build-host"
:prompt-templates {:task "Task {{graph}} {{block-uuid}} {{agent-name}}\n{{task-block-tree}}"}
:routing-blocks* (atom #{})}]
(-> (p/with-redefs [transport/invoke
(fn [_cfg method args]
(swap! calls* conj [method args])
(case method
:thread-api/pull
(let [[_repo _selector lookup] args]
(case lookup
42 (p/resolved task)
900 (p/resolved {:db/id 900
:db/ident :logseq.class/Task})
901 (p/resolved {:db/id 901
:db/ident :logseq.property/status.todo})
(p/rejected (ex-info "unexpected pull"
{:lookup lookup}))))
:thread-api/q
(p/resolved :logseq.property/status.todo)
:thread-api/apply-outliner-ops
(p/resolved {:ok true})
(p/rejected (ex-info "unexpected invoke"
{:method method
:args args}))))
show-command/execute-show
(fn [_action _cfg]
(p/resolved {:status :ok
:data {:message "- Ship the CLI bridge"}}))
cli-server/ensure-server!
(fn [cfg _repo]
(assoc cfg :base-url "http://127.0.0.1:1234"))
agent-command/start-codex!
(fn [_command _opts]
(swap! calls* conj [:codex])
(p/resolved {:session "session-123"
:status :running}))
agent-command/record-session!
(fn [_cfg _session-record]
true)
agent-command/write-agent-session-id!
(fn [_cfg _repo _block-uuid _session-id]
(p/resolved true))]
(#'agent-command/process-sync-db-changes-event!
{:root-dir "/tmp/logseq"
:base-url "http://127.0.0.1:1234"
:log-fn (fn [_] nil)}
route-opts
{:tx-data [{:e 42
:a :block/tags
:v 900
:added true}
{:e 42
:a :logseq.property/status
:v 901
:added true}]}))
(p/then (fn [_]
(is (= 1 (count (filter #(= [:codex] %) @calls*))))))
(p/catch (fn [e]
(is false (str "unexpected error: " e))))
(p/finally done)))))
(deftest test-agent-bridge-listener-routes-comment-mention-with-context-and-reactions
(async done
(let [root (temp-root)