simplify session request

This commit is contained in:
Tienson Qin
2026-02-03 10:12:31 +08:00
parent 1f27977680
commit c9846d8d81
7 changed files with 79 additions and 36 deletions

View File

@@ -3,34 +3,20 @@ set -euo pipefail
BASE_URL="${BASE_URL:-http://127.0.0.1:8787}"
TOKEN="${TOKEN:-dev-token}"
SESSION_ID="${SESSION_ID:-weather}"
SESSION_ID="${SESSION_ID:-p2}"
create_payload() {
cat <<JSON
{
"id": "${SESSION_ID}",
"source": {
"node-id": "task-node-1",
"node-title": "Check weather in Hangzhou",
"node-revision": "2026-02-02T00:00:00Z",
"snapshot": {
"content": "#Task tell me the weather today in Hangzhou",
"references": [],
"attachments": []
}
},
"intent": {
"title": "Weather query",
"summary": "Ask codex for weather in Hangzhou today"
},
"session-id": "${SESSION_ID}",
"node-id": "task-node-1",
"node-title": "Check weather in Hangzhou",
"content": "Tell me the weather today in Hangzhou.",
"attachments": [],
"agent": {
"provider": "codex",
"mode": "build",
"permission-mode": "default"
},
"audit": {
"requested-at": 1769980800000,
"priority": "normal"
}
}
JSON

View File

@@ -217,22 +217,14 @@
:any
http-error-response-schema])
(def agent-task-source-schema
(def sessions-create-request-schema
[:map
[:session-id :string]
[:node-id :string]
[:node-title :string]
[:node-revision :any]
[:snapshot {:optional true} :any]])
(def agent-task-schema
[:map
[:id :string]
[:source agent-task-source-schema]
[:intent {:optional true} :map]
[:agent {:optional true} [:or :string :map]]
[:audit {:optional true} :map]])
(def sessions-create-request-schema agent-task-schema)
[:content :string]
[:attachments [:sequential :string]]
[:agent [:or :string :map]]])
(def sessions-message-request-schema
[:map

View File

@@ -0,0 +1,13 @@
(ns logseq.db-sync.worker.agent.request)
(defn normalize-session-create
[body]
(when (map? body)
(let [attachments (:attachments body)
attachments (when (sequential? attachments) (vec attachments))]
(cond-> {:id (:session-id body)
:source {:node-id (:node-id body)
:node-title (:node-title body)}
:intent {:content (:content body)}
:agent (:agent body)}
(some? attachments) (assoc-in [:intent :attachments] attachments)))))

View File

@@ -24,6 +24,22 @@
(defn- strip-trailing-slash [s]
(string/replace (or s "") #"/+$" ""))
(defn- normalize-provider [value]
(when (string? value)
(let [normalized (-> value string/trim string/lower-case)]
(when-not (string/blank? normalized) normalized))))
(defn provider-kind [^js env]
(or (normalize-provider (env-str env "AGENT_RUNTIME_PROVIDER"))
"sprites"))
(defn runtime-provider-kind [^js env runtime]
(or (normalize-provider (:provider runtime))
(provider-kind env)))
(defn fill-template [template sandbox-id]
(string/replace (or template "") "{sandbox_id}" (or sandbox-id "")))
(defn- sanitize-name [name]
(let [name (or name "task")
sanitized (-> name

View File

@@ -2,6 +2,7 @@
(:require [lambdaisland.glogi :as log]
[logseq.db-sync.common :as common]
[logseq.db-sync.platform.core :as platform]
[logseq.db-sync.worker.agent.request :as agent-request]
[logseq.db-sync.worker.auth :as auth]
[logseq.db-sync.worker.http :as http]
[logseq.db-sync.worker.routes.index :as routes]
@@ -48,8 +49,7 @@
(http/bad-request "missing body")
(let [body (js->clj result :keywordize-keys true)
body (http/coerce-http-request :sessions/create body)
idempotency-key (.get (.-headers request) "idempotency-key")
session-id (or (:id body) idempotency-key)]
session-id (:session-id body)]
(cond
(nil? body)
(http/bad-request "invalid body")
@@ -61,7 +61,7 @@
(if-let [^js stub (session-stub env session-id)]
(let [headers (base-headers request claims)
_ (.set headers "x-stream-base" (.-origin url))
task (assoc body :id session-id)
task (agent-request/normalize-session-create body)
body-json (js/JSON.stringify (clj->js task))
do-url (str (.-origin url) "/__session__/init")]
(forward-request stub do-url "POST" headers body-json))

View File

@@ -0,0 +1,35 @@
(ns logseq.db-sync.agent-request-test
(:require [cljs.test :refer [deftest is testing]]
[logseq.db-sync.worker.agent.request :as request]
[logseq.db-sync.worker.http :as http]))
(deftest sessions-create-coerce-test
(testing "accepts simplified sessions/create request"
(let [body {:session-id "sess-1"
:node-id "node-1"
:node-title "Title"
:content "Hello"
:attachments ["https://example.com/a.png"]
:agent "codex"}
coerced (http/coerce-http-request :sessions/create body)]
(is (= body coerced))))
(testing "rejects missing required fields"
(is (nil? (http/coerce-http-request :sessions/create {:session-id "sess-1"})))))
(deftest normalize-session-create-test
(testing "normalizes simplified request into agent task"
(let [body {:session-id "sess-1"
:node-id "node-1"
:node-title "Title"
:content "Hello"
:attachments ["https://example.com/a.png" "https://example.com/b.png"]
:agent {:provider "codex"}}
normalized (request/normalize-session-create body)]
(is (= {:id "sess-1"
:source {:node-id "node-1"
:node-title "Title"}
:intent {:content "Hello"
:attachments ["https://example.com/a.png"
"https://example.com/b.png"]}
:agent {:provider "codex"}}
normalized)))))

View File

@@ -1,5 +1,6 @@
(ns logseq.db-sync.test-runner
(:require [cljs.test :as ct]
[logseq.db-sync.agent-request-test]
[logseq.db-sync.agent-runtime-provider-test]
[logseq.db-sync.node-config-test]
[logseq.db-sync.platform-test]