feat(kernel): complete tree-first graph tasks 074-080
This commit is contained in:
@@ -0,0 +1,282 @@
|
||||
use crate::app::{AppConfig, AppState};
|
||||
use crate::context::RequestContext;
|
||||
use crate::error::WebError;
|
||||
use crate::transport::convex::execute_sidebar_dataset_query;
|
||||
use axum::extract::{Extension, Query, State};
|
||||
use axum::http::StatusCode;
|
||||
use axum::Json;
|
||||
use bridge_runtime::{
|
||||
execute_runtime_input, execute_runtime_query, RuntimeActorWire, RuntimeBridgeContextWire,
|
||||
RuntimeExecutionPlan, RuntimeInput, RuntimeQueryEnvelopeWire, RuntimeSourceWire,
|
||||
};
|
||||
use core_protocol::{KernelGraphDirection, KernelNodeType, KernelProjectionKind};
|
||||
use serde::Deserialize;
|
||||
use serde_json::{json, Value};
|
||||
use std::env;
|
||||
|
||||
#[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>,
|
||||
}
|
||||
|
||||
fn runtime_context(context: &RequestContext) -> RuntimeBridgeContextWire {
|
||||
RuntimeBridgeContextWire {
|
||||
deployment_id: context.workspace.deployment_id.clone(),
|
||||
project_id: context.workspace.project_id.clone(),
|
||||
workspace_id: context.workspace.workspace_id.clone(),
|
||||
request_id: context.trace.request_id.clone(),
|
||||
trace_id: context.trace.trace_id.clone(),
|
||||
actor: RuntimeActorWire {
|
||||
actor_type: context.auth.actor_type.clone(),
|
||||
actor_id: context.auth.actor_id.clone(),
|
||||
session_id: context.auth.session_id.clone(),
|
||||
},
|
||||
source: RuntimeSourceWire {
|
||||
channel: context.source.channel.clone(),
|
||||
client: context.source.client.clone(),
|
||||
},
|
||||
tenant_id: context.workspace.tenant_id.clone(),
|
||||
auth_token: context.auth.authorization.clone(),
|
||||
idempotency_key: context.source.idempotency_key.clone(),
|
||||
validate_only: false,
|
||||
dry_run: false,
|
||||
}
|
||||
}
|
||||
|
||||
fn build_sidebar_dataset_plan(
|
||||
config: &AppConfig,
|
||||
context: &RequestContext,
|
||||
workspace_id: &str,
|
||||
) -> Result<Value, WebError> {
|
||||
let runtime_input = RuntimeInput::Query {
|
||||
context: runtime_context(context),
|
||||
query: RuntimeQueryEnvelopeWire {
|
||||
name: "sidebar.dataset.list".into(),
|
||||
payload: json!({
|
||||
"workspaceId": workspace_id,
|
||||
}),
|
||||
},
|
||||
data: None,
|
||||
};
|
||||
let RuntimeExecutionPlan::Query(plan) = execute_runtime_input(runtime_input)
|
||||
.map_err(|error| WebError::bad_request(error.message).with_context(context))?
|
||||
else {
|
||||
return Err(WebError::internal("sidebar.dataset.list 未返回 query plan").with_context(context));
|
||||
};
|
||||
|
||||
let dataset = execute_sidebar_dataset_query(config, &plan)?;
|
||||
Ok(dataset)
|
||||
}
|
||||
|
||||
fn load_sidebar_dataset(
|
||||
config: &AppConfig,
|
||||
context: &RequestContext,
|
||||
workspace_id: Option<&str>,
|
||||
) -> Result<Value, WebError> {
|
||||
if let Ok(raw) = env::var("MNOTE_WEB_KERNEL_SIDEBAR_FIXTURE_JSON") {
|
||||
let trimmed = raw.trim();
|
||||
if !trimmed.is_empty() {
|
||||
return serde_json::from_str(trimmed).map_err(|error| {
|
||||
WebError::internal(format!("kernel fixture JSON 非法: {error}")).with_context(context)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
let workspace_id = workspace_id
|
||||
.or(context.workspace.workspace_id.as_deref())
|
||||
.unwrap_or("ws_demo");
|
||||
build_sidebar_dataset_plan(config, context, workspace_id)
|
||||
}
|
||||
|
||||
fn execute_kernel_query(
|
||||
context: &RequestContext,
|
||||
query: RuntimeQueryEnvelopeWire,
|
||||
dataset: Value,
|
||||
) -> Result<Value, WebError> {
|
||||
execute_runtime_query(RuntimeInput::Query {
|
||||
context: runtime_context(context),
|
||||
query,
|
||||
data: Some(dataset),
|
||||
})
|
||||
.map_err(|error| WebError::bad_request(error.message).with_context(context))
|
||||
}
|
||||
|
||||
pub async fn project_sidebar(
|
||||
State(state): State<AppState>,
|
||||
Extension(context): Extension<RequestContext>,
|
||||
Query(query): Query<KernelProjectionQuery>,
|
||||
) -> Result<(StatusCode, Json<Value>), WebError> {
|
||||
let dataset = load_sidebar_dataset(state.config(), &context, query.workspace_id.as_deref())?;
|
||||
let result = execute_kernel_query(
|
||||
&context,
|
||||
RuntimeQueryEnvelopeWire {
|
||||
name: "kernel.project_view".into(),
|
||||
payload: json!({
|
||||
"projection": KernelProjectionKind::SidebarTree,
|
||||
"workspaceId": query.workspace_id,
|
||||
"rootNodeId": query.root_node_id,
|
||||
"depth": query.depth,
|
||||
"includeEdges": true,
|
||||
"includeContent": false,
|
||||
"nodeTypes": [KernelNodeType::Page],
|
||||
}),
|
||||
},
|
||||
dataset,
|
||||
)?;
|
||||
|
||||
Ok((StatusCode::OK, Json(json!({
|
||||
"ok": true,
|
||||
"requestId": context.trace.request_id,
|
||||
"traceId": context.trace.trace_id,
|
||||
"result": result,
|
||||
}))))
|
||||
}
|
||||
|
||||
pub async fn subtree(
|
||||
State(state): State<AppState>,
|
||||
Extension(context): Extension<RequestContext>,
|
||||
Query(query): Query<KernelSubtreeQuery>,
|
||||
) -> Result<(StatusCode, Json<Value>), WebError> {
|
||||
let dataset = load_sidebar_dataset(state.config(), &context, query.workspace_id.as_deref())?;
|
||||
let result = execute_kernel_query(
|
||||
&context,
|
||||
RuntimeQueryEnvelopeWire {
|
||||
name: "kernel.subtree.get".into(),
|
||||
payload: json!({
|
||||
"workspaceId": query.workspace_id,
|
||||
"rootNodeId": query.root_node_id,
|
||||
"depth": query.depth,
|
||||
"includeEdges": true,
|
||||
"nodeTypes": [KernelNodeType::Page],
|
||||
}),
|
||||
},
|
||||
dataset,
|
||||
)?;
|
||||
Ok((StatusCode::OK, Json(json!({
|
||||
"ok": true,
|
||||
"requestId": context.trace.request_id,
|
||||
"traceId": context.trace.trace_id,
|
||||
"result": result,
|
||||
}))))
|
||||
}
|
||||
|
||||
pub async fn edges(
|
||||
State(state): State<AppState>,
|
||||
Extension(context): Extension<RequestContext>,
|
||||
Query(query): Query<KernelEdgesQuery>,
|
||||
) -> Result<(StatusCode, Json<Value>), WebError> {
|
||||
let dataset = load_sidebar_dataset(state.config(), &context, query.workspace_id.as_deref())?;
|
||||
let result = execute_kernel_query(
|
||||
&context,
|
||||
RuntimeQueryEnvelopeWire {
|
||||
name: "kernel.edges.list".into(),
|
||||
payload: json!({
|
||||
"workspaceId": query.workspace_id,
|
||||
"nodeId": query.node_id,
|
||||
"direction": KernelGraphDirection::Both,
|
||||
}),
|
||||
},
|
||||
dataset,
|
||||
)?;
|
||||
Ok((StatusCode::OK, Json(json!({
|
||||
"ok": true,
|
||||
"requestId": context.trace.request_id,
|
||||
"traceId": context.trace.trace_id,
|
||||
"result": result,
|
||||
}))))
|
||||
}
|
||||
|
||||
pub async fn graph(
|
||||
State(state): State<AppState>,
|
||||
Extension(context): Extension<RequestContext>,
|
||||
Query(query): Query<KernelGraphQuery>,
|
||||
) -> Result<(StatusCode, Json<Value>), WebError> {
|
||||
let dataset = load_sidebar_dataset(state.config(), &context, query.workspace_id.as_deref())?;
|
||||
let result = execute_kernel_query(
|
||||
&context,
|
||||
RuntimeQueryEnvelopeWire {
|
||||
name: "kernel.graph.traverse".into(),
|
||||
payload: json!({
|
||||
"workspaceId": query.workspace_id,
|
||||
"startNodeId": query.start_node_id,
|
||||
"maxDepth": query.max_depth.unwrap_or(2),
|
||||
"edgeTypes": [],
|
||||
}),
|
||||
},
|
||||
dataset,
|
||||
)?;
|
||||
Ok((StatusCode::OK, Json(json!({
|
||||
"ok": true,
|
||||
"requestId": context.trace.request_id,
|
||||
"traceId": context.trace.trace_id,
|
||||
"result": result,
|
||||
}))))
|
||||
}
|
||||
|
||||
#[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 {
|
||||
std::env::set_var(
|
||||
"MNOTE_WEB_KERNEL_SIDEBAR_FIXTURE_JSON",
|
||||
r#"{"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"}]}"#,
|
||||
);
|
||||
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,
|
||||
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()
|
||||
.uri("/api/kernel/projections/sidebar?rootNodeId=page_root")
|
||||
.body(Body::empty())
|
||||
.expect("request"),
|
||||
)
|
||||
.await
|
||||
.expect("response");
|
||||
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user