mirror of
https://github.com/logseq/logseq.git
synced 2026-05-24 04:34:14 +00:00
simplify session request
This commit is contained in:
26
deps/db-sync/scripts/start-weather-session.sh
vendored
26
deps/db-sync/scripts/start-weather-session.sh
vendored
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
13
deps/db-sync/src/logseq/db_sync/worker/agent/request.cljs
vendored
Normal file
13
deps/db-sync/src/logseq/db_sync/worker/agent/request.cljs
vendored
Normal 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)))))
|
||||
@@ -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
|
||||
|
||||
@@ -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))
|
||||
|
||||
35
deps/db-sync/test/logseq/db_sync/agent_request_test.cljs
vendored
Normal file
35
deps/db-sync/test/logseq/db_sync/agent_request_test.cljs
vendored
Normal 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)))))
|
||||
@@ -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]
|
||||
|
||||
Reference in New Issue
Block a user