2026-04-18 05:43:49 +08:00
|
|
|
use crate::app::AppState;
|
2026-04-16 22:01:51 +08:00
|
|
|
use crate::context::RequestContext;
|
|
|
|
|
use crate::error::WebError;
|
2026-04-17 00:25:28 +08:00
|
|
|
use crate::routes::query_support::{
|
2026-04-18 05:43:49 +08:00
|
|
|
resolve_effective_workspace_id,
|
|
|
|
|
};
|
|
|
|
|
use crate::routes::snapshot_support::{
|
|
|
|
|
execute_kernel_query, load_projection_snapshot, load_sidebar_dataset, subtree_query,
|
|
|
|
|
ProjectionSnapshotSpec,
|
2026-04-17 00:25:28 +08:00
|
|
|
};
|
2026-04-16 22:01:51 +08:00
|
|
|
use axum::extract::{Extension, Query, State};
|
|
|
|
|
use axum::http::StatusCode;
|
|
|
|
|
use axum::Json;
|
2026-04-17 00:25:28 +08:00
|
|
|
use bridge_runtime::RuntimeQueryEnvelopeWire;
|
2026-04-18 05:43:49 +08:00
|
|
|
use core_protocol::{KernelGraphDirection, KernelProjectionKind};
|
2026-04-16 22:01:51 +08:00
|
|
|
use serde::Deserialize;
|
|
|
|
|
use serde_json::{json, Value};
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
|
pub struct KernelProjectionQuery {
|
|
|
|
|
pub workspace_id: Option<String>,
|
|
|
|
|
pub root_node_id: Option<String>,
|
|
|
|
|
pub depth: Option<u32>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
|
pub struct KernelSubtreeQuery {
|
|
|
|
|
pub workspace_id: Option<String>,
|
|
|
|
|
pub root_node_id: String,
|
|
|
|
|
pub depth: Option<u32>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
|
pub struct KernelEdgesQuery {
|
|
|
|
|
pub workspace_id: Option<String>,
|
|
|
|
|
pub node_id: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
|
pub struct KernelGraphQuery {
|
|
|
|
|
pub workspace_id: Option<String>,
|
|
|
|
|
pub start_node_id: String,
|
|
|
|
|
pub max_depth: Option<u32>,
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-17 00:25:28 +08:00
|
|
|
fn ok_response(context: &RequestContext, result: Value) -> (StatusCode, Json<Value>) {
|
|
|
|
|
(
|
|
|
|
|
StatusCode::OK,
|
|
|
|
|
Json(json!({
|
|
|
|
|
"ok": true,
|
|
|
|
|
"requestId": context.trace.request_id,
|
|
|
|
|
"traceId": context.trace.trace_id,
|
|
|
|
|
"result": result,
|
|
|
|
|
})),
|
|
|
|
|
)
|
2026-04-16 22:01:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn project_sidebar(
|
|
|
|
|
State(state): State<AppState>,
|
|
|
|
|
Extension(context): Extension<RequestContext>,
|
|
|
|
|
Query(query): Query<KernelProjectionQuery>,
|
|
|
|
|
) -> Result<(StatusCode, Json<Value>), WebError> {
|
2026-04-17 00:25:28 +08:00
|
|
|
let effective_workspace_id =
|
|
|
|
|
resolve_effective_workspace_id(&context, query.workspace_id.as_deref(), true)?
|
|
|
|
|
.expect("workspace_required 已确保存在");
|
2026-04-18 05:43:49 +08:00
|
|
|
let snapshot = load_projection_snapshot(
|
|
|
|
|
state.config(),
|
2026-04-16 22:01:51 +08:00
|
|
|
&context,
|
2026-04-18 05:43:49 +08:00
|
|
|
&ProjectionSnapshotSpec {
|
|
|
|
|
workspace_id: &effective_workspace_id,
|
|
|
|
|
root_node_id: query.root_node_id.as_deref(),
|
|
|
|
|
depth: query.depth,
|
|
|
|
|
projection: KernelProjectionKind::SidebarTree,
|
2026-04-16 22:01:51 +08:00
|
|
|
},
|
2026-04-18 05:43:49 +08:00
|
|
|
)
|
|
|
|
|
.await?;
|
2026-04-16 22:01:51 +08:00
|
|
|
|
2026-04-18 05:43:49 +08:00
|
|
|
Ok(ok_response(&context, snapshot.projection))
|
2026-04-16 22:01:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn subtree(
|
|
|
|
|
State(state): State<AppState>,
|
|
|
|
|
Extension(context): Extension<RequestContext>,
|
|
|
|
|
Query(query): Query<KernelSubtreeQuery>,
|
|
|
|
|
) -> Result<(StatusCode, Json<Value>), WebError> {
|
2026-04-17 00:25:28 +08:00
|
|
|
let effective_workspace_id =
|
|
|
|
|
resolve_effective_workspace_id(&context, query.workspace_id.as_deref(), true)?
|
|
|
|
|
.expect("workspace_required 已确保存在");
|
2026-04-17 23:36:24 +08:00
|
|
|
let dataset = load_sidebar_dataset(state.config(), &context, &effective_workspace_id).await?;
|
2026-04-16 22:01:51 +08:00
|
|
|
let result = execute_kernel_query(
|
|
|
|
|
&context,
|
2026-04-17 00:25:28 +08:00
|
|
|
&effective_workspace_id,
|
2026-04-18 05:43:49 +08:00
|
|
|
subtree_query(&effective_workspace_id, &query.root_node_id, query.depth),
|
2026-04-16 22:01:51 +08:00
|
|
|
dataset,
|
|
|
|
|
)?;
|
2026-04-17 00:25:28 +08:00
|
|
|
Ok(ok_response(&context, result))
|
2026-04-16 22:01:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn edges(
|
|
|
|
|
State(state): State<AppState>,
|
|
|
|
|
Extension(context): Extension<RequestContext>,
|
|
|
|
|
Query(query): Query<KernelEdgesQuery>,
|
|
|
|
|
) -> Result<(StatusCode, Json<Value>), WebError> {
|
2026-04-17 00:25:28 +08:00
|
|
|
let effective_workspace_id =
|
|
|
|
|
resolve_effective_workspace_id(&context, query.workspace_id.as_deref(), true)?
|
|
|
|
|
.expect("workspace_required 已确保存在");
|
2026-04-17 23:36:24 +08:00
|
|
|
let dataset = load_sidebar_dataset(state.config(), &context, &effective_workspace_id).await?;
|
2026-04-16 22:01:51 +08:00
|
|
|
let result = execute_kernel_query(
|
|
|
|
|
&context,
|
2026-04-17 00:25:28 +08:00
|
|
|
&effective_workspace_id,
|
2026-04-16 22:01:51 +08:00
|
|
|
RuntimeQueryEnvelopeWire {
|
|
|
|
|
name: "kernel.edges.list".into(),
|
|
|
|
|
payload: json!({
|
2026-04-17 00:25:28 +08:00
|
|
|
"workspaceId": effective_workspace_id,
|
2026-04-16 22:01:51 +08:00
|
|
|
"nodeId": query.node_id,
|
|
|
|
|
"direction": KernelGraphDirection::Both,
|
|
|
|
|
}),
|
|
|
|
|
},
|
|
|
|
|
dataset,
|
|
|
|
|
)?;
|
2026-04-17 00:25:28 +08:00
|
|
|
Ok(ok_response(&context, result))
|
2026-04-16 22:01:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn graph(
|
|
|
|
|
State(state): State<AppState>,
|
|
|
|
|
Extension(context): Extension<RequestContext>,
|
|
|
|
|
Query(query): Query<KernelGraphQuery>,
|
|
|
|
|
) -> Result<(StatusCode, Json<Value>), WebError> {
|
2026-04-17 00:25:28 +08:00
|
|
|
let effective_workspace_id =
|
|
|
|
|
resolve_effective_workspace_id(&context, query.workspace_id.as_deref(), true)?
|
|
|
|
|
.expect("workspace_required 已确保存在");
|
2026-04-17 23:36:24 +08:00
|
|
|
let dataset = load_sidebar_dataset(state.config(), &context, &effective_workspace_id).await?;
|
2026-04-16 22:01:51 +08:00
|
|
|
let result = execute_kernel_query(
|
|
|
|
|
&context,
|
2026-04-17 00:25:28 +08:00
|
|
|
&effective_workspace_id,
|
2026-04-16 22:01:51 +08:00
|
|
|
RuntimeQueryEnvelopeWire {
|
|
|
|
|
name: "kernel.graph.traverse".into(),
|
|
|
|
|
payload: json!({
|
2026-04-17 00:25:28 +08:00
|
|
|
"workspaceId": effective_workspace_id,
|
2026-04-16 22:01:51 +08:00
|
|
|
"startNodeId": query.start_node_id,
|
|
|
|
|
"maxDepth": query.max_depth.unwrap_or(2),
|
|
|
|
|
"edgeTypes": [],
|
|
|
|
|
}),
|
|
|
|
|
},
|
|
|
|
|
dataset,
|
|
|
|
|
)?;
|
2026-04-17 00:25:28 +08:00
|
|
|
Ok(ok_response(&context, result))
|
2026-04-16 22:01:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use crate::app::{build_app, AppConfig, AppState};
|
|
|
|
|
use axum::body::Body;
|
|
|
|
|
use axum::http::{Request, StatusCode};
|
|
|
|
|
use tower::util::ServiceExt;
|
|
|
|
|
|
|
|
|
|
fn app() -> axum::Router {
|
|
|
|
|
build_app(AppState::new(AppConfig {
|
|
|
|
|
service_name: "mnote-web".into(),
|
|
|
|
|
service_version: "0.1.0".into(),
|
|
|
|
|
bind_addr: "127.0.0.1:0".into(),
|
|
|
|
|
hermes_base_path: "/api/hermes".into(),
|
|
|
|
|
compat_next_base_path: "/api/compat/next".into(),
|
|
|
|
|
convex_url: None,
|
|
|
|
|
convex_admin_key: None,
|
2026-04-17 00:25:28 +08:00
|
|
|
allow_dev_fixtures: true,
|
2026-04-17 23:36:24 +08:00
|
|
|
query_fixtures_json: Some(r#"{"sidebar:datasetList":{"documents":[{"id":"page_root","workspace_id":"ws_demo","title":"工作区首页","parent_id":null,"sort_order":0,"is_starred":true,"is_template":false,"created_at":"2026-04-16T00:00:00Z","updated_at":"2026-04-16T00:00:00Z"},{"id":"page_child","workspace_id":"ws_demo","title":"子页面","parent_id":"page_root","sort_order":1,"is_starred":false,"is_template":false,"created_at":"2026-04-16T00:00:00Z","updated_at":"2026-04-16T00:00:00Z"}]},"bridgeLogs:listWorkspaceOverview":{"workspace_id":"ws_demo","command_logs":[{"command_id":"cmd_1","request_id":"req_1","status":"applied","created_at":"2026-04-16T00:00:00Z"}],"domain_events":[{"command_id":"cmd_1","status":"published","created_at":"2026-04-16T00:00:00Z"}],"next_cursor":null,"has_more":false,"filters":{"command_status":null,"event_status":null,"target_page_id":null,"target_block_id":null,"aggregate_type":null,"aggregate_id":null},"generated_at":"2026-04-16T00:00:00Z"}}"#.into()),
|
|
|
|
|
mutation_fixtures_json: None,
|
2026-04-16 22:01:51 +08:00
|
|
|
dev_user_id: "dev-user".into(),
|
|
|
|
|
dev_user_name: "开发用户".into(),
|
|
|
|
|
dev_user_email: "dev@mnote.local".into(),
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
async fn kernel_projection_route_returns_sidebar_projection() {
|
|
|
|
|
let response = app()
|
|
|
|
|
.oneshot(
|
|
|
|
|
Request::builder()
|
2026-04-17 00:25:28 +08:00
|
|
|
.uri("/api/kernel/projections/sidebar?workspaceId=ws_demo&rootNodeId=page_root")
|
2026-04-16 22:01:51 +08:00
|
|
|
.body(Body::empty())
|
|
|
|
|
.expect("request"),
|
|
|
|
|
)
|
|
|
|
|
.await
|
|
|
|
|
.expect("response");
|
|
|
|
|
|
|
|
|
|
assert_eq!(response.status(), StatusCode::OK);
|
|
|
|
|
}
|
|
|
|
|
}
|