2026-04-16 22:01:51 +08:00
|
|
|
use crate::app::AppState;
|
|
|
|
|
use crate::context::RequestContext;
|
2026-04-17 00:25:28 +08:00
|
|
|
use crate::error::WebError;
|
2026-04-18 05:43:49 +08:00
|
|
|
use crate::routes::query_support::resolve_effective_workspace_id;
|
2026-04-29 12:24:44 +08:00
|
|
|
use crate::routes::snapshot_support::{load_projection_snapshot, ProjectionSnapshotSpec};
|
2026-05-06 21:44:20 +08:00
|
|
|
use axum::body::{Body, Bytes};
|
2026-04-17 00:25:28 +08:00
|
|
|
use axum::extract::Query;
|
2026-04-16 22:01:51 +08:00
|
|
|
use axum::extract::{Extension, State};
|
2026-05-06 21:44:20 +08:00
|
|
|
use axum::http::{header, HeaderMap, HeaderName, HeaderValue, Request, StatusCode};
|
|
|
|
|
use axum::response::{IntoResponse, Response};
|
2026-04-29 12:24:44 +08:00
|
|
|
use axum::Json;
|
2026-04-18 05:43:49 +08:00
|
|
|
use core_protocol::KernelProjectionKind;
|
2026-05-06 21:44:20 +08:00
|
|
|
use futures_util::{StreamExt, TryStreamExt};
|
2026-04-17 00:25:28 +08:00
|
|
|
use serde::Deserialize;
|
2026-04-29 12:24:44 +08:00
|
|
|
use serde_json::{json, Value};
|
2026-05-06 21:44:20 +08:00
|
|
|
use std::env;
|
|
|
|
|
use std::fs;
|
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
use std::process::Command;
|
|
|
|
|
use std::time::Duration;
|
2026-04-16 22:01:51 +08:00
|
|
|
|
2026-04-17 00:25:28 +08:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
|
pub struct CompatSidebarQuery {
|
|
|
|
|
pub workspace_id: Option<String>,
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-16 22:01:51 +08:00
|
|
|
pub async fn next_ai_agent_run(
|
|
|
|
|
State(state): State<AppState>,
|
|
|
|
|
Extension(context): Extension<RequestContext>,
|
2026-05-06 21:44:20 +08:00
|
|
|
request: Request<Body>,
|
|
|
|
|
) -> Result<Response, WebError> {
|
|
|
|
|
let (parts, body) = request.into_parts();
|
|
|
|
|
let body = axum::body::to_bytes(body, 10 * 1024 * 1024)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(|error| WebError::internal(format!("读取 AI 请求体失败: {error}")))?;
|
|
|
|
|
let payload: Value = serde_json::from_slice(&body).map_err(|error| {
|
|
|
|
|
WebError::bad_request_code(
|
|
|
|
|
"ai_agent_bad_request",
|
|
|
|
|
format!("AI 请求体不是合法 JSON: {error}"),
|
|
|
|
|
)
|
|
|
|
|
.with_context(&context)
|
|
|
|
|
.with_header("x-mnote-web-owner", "mnote-web")
|
|
|
|
|
})?;
|
|
|
|
|
|
|
|
|
|
if state.config().enable_legacy_next_compat {
|
|
|
|
|
if let Some(base_url) = state.config().legacy_next_base_url.as_deref() {
|
|
|
|
|
if let Ok(response) =
|
|
|
|
|
proxy_ai_agent_run_to_next(base_url, &context, &parts.headers, body.clone()).await
|
|
|
|
|
{
|
|
|
|
|
return Ok(response);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Ok(response) = run_local_mnote_cli_ai_host(&state, &context, &payload).await {
|
|
|
|
|
return Ok(response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let Some(backend_url) = resolve_ai_orchestrator_backend_url() else {
|
|
|
|
|
return Err(WebError::bad_gateway_code(
|
|
|
|
|
"ai_orchestrator_unavailable",
|
|
|
|
|
"未配置 BACKEND_URL,无法连接 document AI orchestrator。",
|
|
|
|
|
)
|
|
|
|
|
.with_context(&context)
|
|
|
|
|
.with_header("x-mnote-web-owner", "mnote-web"));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let upstream_url = reqwest::Url::parse(&format!(
|
|
|
|
|
"{}/api/v1/ai-agent/document/run",
|
|
|
|
|
backend_url.trim_end_matches('/')
|
|
|
|
|
))
|
|
|
|
|
.map_err(|error| WebError::internal(format!("AI orchestrator URL 非法: {error}")))?;
|
|
|
|
|
let forward_payload = build_document_ai_orchestrator_payload(&state, &context, &payload);
|
|
|
|
|
let client = reqwest::Client::builder()
|
|
|
|
|
.connect_timeout(Duration::from_secs(5))
|
|
|
|
|
.redirect(reqwest::redirect::Policy::none())
|
|
|
|
|
.build()
|
|
|
|
|
.map_err(|error| {
|
|
|
|
|
WebError::internal(format!("AI orchestrator HTTP 客户端创建失败: {error}"))
|
|
|
|
|
})?;
|
|
|
|
|
|
|
|
|
|
let mut upstream_request = client.post(upstream_url).json(&forward_payload);
|
|
|
|
|
upstream_request = apply_ai_forward_headers(upstream_request, &parts.headers, &context);
|
|
|
|
|
if let Some(api_key) = read_env_or_dotenv("MNOTE_AI_ORCHESTRATOR_API_KEY") {
|
|
|
|
|
upstream_request = upstream_request.header("x-mnote-ai-key", api_key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let upstream_response = upstream_request.send().await.map_err(|error| {
|
|
|
|
|
WebError::bad_gateway_code(
|
|
|
|
|
"ai_orchestrator_proxy_error",
|
|
|
|
|
format!("document AI orchestrator 请求失败: {error}"),
|
|
|
|
|
)
|
|
|
|
|
.with_context(&context)
|
|
|
|
|
.with_header("x-mnote-web-owner", "mnote-web")
|
|
|
|
|
.with_header("x-upstream-service", "document-ai-orchestrator")
|
|
|
|
|
})?;
|
|
|
|
|
|
|
|
|
|
if !upstream_response.status().is_success() {
|
|
|
|
|
let status = upstream_response.status();
|
|
|
|
|
let body = upstream_response
|
|
|
|
|
.text()
|
|
|
|
|
.await
|
|
|
|
|
.unwrap_or_else(|error| format!("读取 upstream 错误响应失败: {error}"));
|
|
|
|
|
return Err(WebError::bad_gateway_code(
|
|
|
|
|
"ai_orchestrator_upstream_error",
|
|
|
|
|
format!(
|
|
|
|
|
"document AI orchestrator 返回 HTTP {}: {}",
|
|
|
|
|
status.as_u16(),
|
|
|
|
|
body
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
.with_context(&context)
|
|
|
|
|
.with_header("x-mnote-web-owner", "mnote-web")
|
|
|
|
|
.with_header("x-upstream-service", "document-ai-orchestrator"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
build_ai_sse_proxy_response(upstream_response, &context)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn proxy_ai_agent_run_to_next(
|
|
|
|
|
base_url: &str,
|
|
|
|
|
context: &RequestContext,
|
|
|
|
|
headers: &HeaderMap,
|
|
|
|
|
body_bytes: Bytes,
|
|
|
|
|
) -> Result<Response, WebError> {
|
|
|
|
|
let upstream_url = reqwest::Url::parse(&format!(
|
|
|
|
|
"{}/api/ai-agent/run",
|
|
|
|
|
base_url.trim_end_matches('/')
|
|
|
|
|
))
|
|
|
|
|
.map_err(|error| WebError::internal(format!("Next AI route URL 非法: {error}")))?;
|
|
|
|
|
let client = reqwest::Client::builder()
|
|
|
|
|
.connect_timeout(Duration::from_secs(5))
|
|
|
|
|
.redirect(reqwest::redirect::Policy::none())
|
|
|
|
|
.build()
|
|
|
|
|
.map_err(|error| WebError::internal(format!("Next AI HTTP 客户端创建失败: {error}")))?;
|
|
|
|
|
|
|
|
|
|
let mut upstream_request = client.post(upstream_url).body(body_bytes.clone());
|
|
|
|
|
for (name, value) in headers.iter() {
|
|
|
|
|
if is_hop_by_hop_header(name.as_str())
|
|
|
|
|
|| name == header::HOST
|
|
|
|
|
|| name == header::CONTENT_LENGTH
|
|
|
|
|
|| name == header::COOKIE
|
|
|
|
|
|| name == header::AUTHORIZATION
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
upstream_request = upstream_request.header(name.as_str(), value.as_bytes());
|
|
|
|
|
}
|
|
|
|
|
if let Some(cookie) = context.auth.cookie_header.as_deref() {
|
|
|
|
|
upstream_request = upstream_request.header(header::COOKIE, cookie);
|
|
|
|
|
}
|
|
|
|
|
if let Some(authorization) = context.auth.authorization.as_deref() {
|
|
|
|
|
upstream_request = upstream_request.header(header::AUTHORIZATION, authorization);
|
|
|
|
|
}
|
|
|
|
|
upstream_request = upstream_request.header("x-request-id", context.trace.request_id.as_str());
|
|
|
|
|
upstream_request = upstream_request.header("x-trace-id", context.trace.trace_id.as_str());
|
|
|
|
|
upstream_request = upstream_request.header("x-mnote-source-channel", "rust_web_route");
|
|
|
|
|
upstream_request = upstream_request.header("x-mnote-source-client", "mnote-web");
|
|
|
|
|
upstream_request = upstream_request.header("x-mnote-actor-id", context.auth.actor_id.as_str());
|
|
|
|
|
upstream_request =
|
|
|
|
|
upstream_request.header("x-mnote-actor-type", context.auth.actor_type.as_str());
|
|
|
|
|
if let Some(workspace_id) = context.workspace.workspace_id.as_deref() {
|
|
|
|
|
upstream_request = upstream_request.header("x-mnote-workspace-id", workspace_id);
|
|
|
|
|
}
|
|
|
|
|
if let Some(session_id) = context.auth.session_id.as_deref() {
|
|
|
|
|
upstream_request = upstream_request.header("x-mnote-session-id", session_id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let upstream_response = upstream_request.send().await.map_err(|error| {
|
|
|
|
|
WebError::bad_gateway_code(
|
|
|
|
|
"next_ai_proxy_error",
|
|
|
|
|
format!("Next /api/ai-agent/run 请求失败: {error}"),
|
|
|
|
|
)
|
|
|
|
|
.with_context(context)
|
|
|
|
|
.with_header("x-mnote-web-owner", "mnote-web")
|
|
|
|
|
.with_header("x-upstream-service", "next-ai-route")
|
|
|
|
|
})?;
|
|
|
|
|
|
|
|
|
|
if !upstream_response.status().is_success() {
|
|
|
|
|
let status = upstream_response.status();
|
|
|
|
|
let body = upstream_response
|
|
|
|
|
.text()
|
|
|
|
|
.await
|
|
|
|
|
.unwrap_or_else(|error| format!("读取 Next AI 错误响应失败: {error}"));
|
|
|
|
|
return Err(WebError::bad_gateway_code(
|
|
|
|
|
"next_ai_upstream_error",
|
|
|
|
|
format!(
|
|
|
|
|
"Next /api/ai-agent/run 返回 HTTP {}: {}",
|
|
|
|
|
status.as_u16(),
|
|
|
|
|
body
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
.with_context(context)
|
|
|
|
|
.with_header("x-mnote-web-owner", "mnote-web")
|
|
|
|
|
.with_header("x-upstream-service", "next-ai-route"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
build_ai_sse_proxy_response(upstream_response, context)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn resolve_repo_root() -> PathBuf {
|
|
|
|
|
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../..")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn build_mnote_cli_args(context: &RequestContext, payload: &Value) -> Vec<String> {
|
|
|
|
|
let ai = payload
|
|
|
|
|
.get("options")
|
|
|
|
|
.and_then(|value| value.get("ai"))
|
|
|
|
|
.cloned()
|
|
|
|
|
.unwrap_or(Value::Null);
|
|
|
|
|
let runtime_context = payload.get("context").cloned().unwrap_or(Value::Null);
|
|
|
|
|
let document_id = runtime_context
|
|
|
|
|
.get("documentId")
|
|
|
|
|
.and_then(Value::as_str)
|
|
|
|
|
.unwrap_or("current");
|
|
|
|
|
let workspace_id = runtime_context
|
|
|
|
|
.get("workspaceId")
|
|
|
|
|
.and_then(Value::as_str)
|
|
|
|
|
.or(context.workspace.workspace_id.as_deref());
|
|
|
|
|
let session_id = ai
|
|
|
|
|
.get("sessionId")
|
|
|
|
|
.and_then(Value::as_str)
|
|
|
|
|
.filter(|value| !value.trim().is_empty())
|
|
|
|
|
.map(str::to_string)
|
|
|
|
|
.unwrap_or_else(|| format!("ai-{}", context.trace.request_id));
|
|
|
|
|
let args_json = json!({
|
|
|
|
|
"pageId": document_id,
|
|
|
|
|
"documentId": document_id,
|
|
|
|
|
"workspaceId": workspace_id,
|
|
|
|
|
"provider": ai.get("provider").cloned().unwrap_or(Value::Null),
|
|
|
|
|
"modelKey": ai.get("modelKey").cloned().unwrap_or(Value::Null),
|
|
|
|
|
"profileId": ai.get("profileId").cloned().unwrap_or(Value::Null),
|
|
|
|
|
"selectedUids": runtime_context.get("selectedUids").cloned().unwrap_or(Value::Null),
|
|
|
|
|
"pageOptions": runtime_context.get("pageOptions").cloned().unwrap_or(Value::Null),
|
2026-04-16 22:01:51 +08:00
|
|
|
})
|
2026-05-06 21:44:20 +08:00
|
|
|
.to_string();
|
|
|
|
|
|
|
|
|
|
vec![
|
|
|
|
|
"run".into(),
|
|
|
|
|
"--quiet".into(),
|
|
|
|
|
"--manifest-path".into(),
|
|
|
|
|
resolve_repo_root()
|
|
|
|
|
.join("rust")
|
|
|
|
|
.join("Cargo.toml")
|
|
|
|
|
.to_string_lossy()
|
|
|
|
|
.to_string(),
|
|
|
|
|
"-p".into(),
|
|
|
|
|
"mnote-cli".into(),
|
|
|
|
|
"--".into(),
|
|
|
|
|
"--json".into(),
|
|
|
|
|
"--validate-only".into(),
|
|
|
|
|
"--dry-run".into(),
|
|
|
|
|
"--actor-id".into(),
|
|
|
|
|
context.auth.actor_id.clone(),
|
|
|
|
|
"--actor-type".into(),
|
|
|
|
|
context.auth.actor_type.clone(),
|
|
|
|
|
"--session-id".into(),
|
|
|
|
|
session_id,
|
|
|
|
|
"--reason".into(),
|
|
|
|
|
"ai-agent-run:mnote-web-rust-host".into(),
|
|
|
|
|
"tool".into(),
|
|
|
|
|
"run".into(),
|
|
|
|
|
"--tool-name".into(),
|
|
|
|
|
"doc_get".into(),
|
|
|
|
|
"--kind".into(),
|
|
|
|
|
"query".into(),
|
|
|
|
|
"--mode".into(),
|
|
|
|
|
"explain-plan".into(),
|
|
|
|
|
"--args-json".into(),
|
|
|
|
|
args_json,
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn run_local_mnote_cli_ai_host(
|
|
|
|
|
state: &AppState,
|
|
|
|
|
context: &RequestContext,
|
|
|
|
|
payload: &Value,
|
|
|
|
|
) -> Result<Response, WebError> {
|
|
|
|
|
let stream = payload
|
|
|
|
|
.get("stream")
|
|
|
|
|
.and_then(Value::as_bool)
|
|
|
|
|
.unwrap_or(true);
|
|
|
|
|
let args = build_mnote_cli_args(context, payload);
|
|
|
|
|
let repo_root = resolve_repo_root();
|
|
|
|
|
let actor_id =
|
|
|
|
|
if context.auth.actor_id.trim().is_empty() || context.auth.actor_id == "anonymous" {
|
|
|
|
|
state.config().dev_user_id.clone()
|
|
|
|
|
} else {
|
|
|
|
|
context.auth.actor_id.clone()
|
|
|
|
|
};
|
|
|
|
|
let actor_type =
|
|
|
|
|
if context.auth.actor_type.trim().is_empty() || context.auth.actor_type == "anonymous" {
|
|
|
|
|
"user".to_string()
|
|
|
|
|
} else {
|
|
|
|
|
context.auth.actor_type.clone()
|
|
|
|
|
};
|
|
|
|
|
let dev_email = state.config().dev_user_email.clone();
|
|
|
|
|
let dev_name = state.config().dev_user_name.clone();
|
|
|
|
|
|
|
|
|
|
let output = tokio::task::spawn_blocking(move || {
|
|
|
|
|
Command::new("cargo")
|
|
|
|
|
.args(args)
|
|
|
|
|
.current_dir(repo_root)
|
|
|
|
|
.env("CARGO_TERM_COLOR", "never")
|
|
|
|
|
.env(
|
|
|
|
|
"RUSTUP_TOOLCHAIN",
|
|
|
|
|
env::var("RUSTUP_TOOLCHAIN")
|
|
|
|
|
.ok()
|
|
|
|
|
.filter(|value| !value.trim().is_empty())
|
|
|
|
|
.unwrap_or_else(|| "1.89.0".into()),
|
|
|
|
|
)
|
|
|
|
|
.env("DEV_USER_ID", actor_id)
|
|
|
|
|
.env("DEV_USER_EMAIL", dev_email)
|
|
|
|
|
.env("DEV_USER_NAME", dev_name)
|
|
|
|
|
.env("MNOTE_CLI_ALLOW_CREATE_PAGE", "1")
|
|
|
|
|
.env("MNOTE_CLI_ALLOW_EDIT", "1")
|
|
|
|
|
.env("MNOTE_ACTOR_TYPE", actor_type)
|
|
|
|
|
.output()
|
|
|
|
|
})
|
|
|
|
|
.await
|
|
|
|
|
.map_err(|error| {
|
|
|
|
|
WebError::bad_gateway_code(
|
|
|
|
|
"mnote_cli_host_join_error",
|
|
|
|
|
format!("mnote-cli host join 失败: {error}"),
|
|
|
|
|
)
|
|
|
|
|
.with_context(context)
|
|
|
|
|
.with_header("x-mnote-web-owner", "mnote-web")
|
|
|
|
|
})?
|
|
|
|
|
.map_err(|error| {
|
|
|
|
|
WebError::bad_gateway_code(
|
|
|
|
|
"mnote_cli_host_spawn_error",
|
|
|
|
|
format!("mnote-cli host 启动失败: {error}"),
|
|
|
|
|
)
|
|
|
|
|
.with_context(context)
|
|
|
|
|
.with_header("x-mnote-web-owner", "mnote-web")
|
|
|
|
|
})?;
|
|
|
|
|
|
|
|
|
|
let stdout = String::from_utf8_lossy(&output.stdout).trim().to_string();
|
|
|
|
|
let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
|
|
|
|
|
if !stream {
|
|
|
|
|
if !output.status.success() {
|
|
|
|
|
return Err(WebError::bad_gateway_code(
|
|
|
|
|
"mnote_cli_host_failed",
|
|
|
|
|
if stderr.is_empty() {
|
|
|
|
|
"mnote-cli 执行失败".into()
|
|
|
|
|
} else {
|
|
|
|
|
stderr
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.with_context(context)
|
|
|
|
|
.with_header("x-mnote-web-owner", "mnote-web"));
|
|
|
|
|
}
|
|
|
|
|
let mut response = Json(json!({
|
|
|
|
|
"ok": true,
|
|
|
|
|
"bridgeOwner": "mnote-cli",
|
|
|
|
|
"text": if stdout.is_empty() { "mnote-cli 无输出" } else { &stdout },
|
|
|
|
|
}))
|
|
|
|
|
.into_response();
|
|
|
|
|
stamp_owner_header(response.headers_mut());
|
|
|
|
|
response.headers_mut().insert(
|
|
|
|
|
HeaderName::from_static("x-mnote-ai-execution-owner"),
|
|
|
|
|
HeaderValue::from_static("mnote-cli"),
|
|
|
|
|
);
|
|
|
|
|
return Ok(response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let body = if output.status.success() {
|
|
|
|
|
format!(
|
|
|
|
|
"event: ready\ndata: {}\n\nevent: assistant_message\ndata: {}\n\nevent: completion\ndata: {}\n\n",
|
|
|
|
|
json!({"ok": true, "bridgeOwner": "mnote-cli"}).to_string(),
|
|
|
|
|
json!({"text": if stdout.is_empty() { "mnote-cli 无输出" } else { &stdout }}).to_string(),
|
|
|
|
|
json!({"ok": true, "text": if stdout.is_empty() { "mnote-cli 无输出" } else { &stdout }, "steps": 1}).to_string(),
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
format!(
|
|
|
|
|
"event: ready\ndata: {}\n\nevent: error\ndata: {}\n\n",
|
|
|
|
|
json!({"ok": true, "bridgeOwner": "mnote-cli"}).to_string(),
|
|
|
|
|
json!({"ok": false, "message": if stderr.is_empty() { "mnote-cli 执行失败" } else { &stderr }}).to_string(),
|
|
|
|
|
)
|
|
|
|
|
};
|
|
|
|
|
let mut response = Response::builder()
|
|
|
|
|
.status(StatusCode::OK)
|
|
|
|
|
.header(header::CONTENT_TYPE, "text/event-stream; charset=utf-8")
|
|
|
|
|
.header(header::CACHE_CONTROL, "no-cache, no-transform")
|
|
|
|
|
.header(header::CONNECTION, "keep-alive")
|
|
|
|
|
.header("x-accel-buffering", "no")
|
|
|
|
|
.header("x-mnote-ai-execution-owner", "mnote-cli")
|
|
|
|
|
.body(Body::from(body))
|
|
|
|
|
.map_err(|error| WebError::internal(format!("mnote-cli SSE 响应构造失败: {error}")))?;
|
|
|
|
|
stamp_owner_header(response.headers_mut());
|
|
|
|
|
Ok(response)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn build_document_ai_orchestrator_payload(
|
|
|
|
|
state: &AppState,
|
|
|
|
|
context: &RequestContext,
|
|
|
|
|
payload: &Value,
|
|
|
|
|
) -> Value {
|
|
|
|
|
let ai_options = payload.get("options").and_then(|value| value.get("ai"));
|
|
|
|
|
let user_id = if context.auth.actor_id.trim().is_empty() || context.auth.actor_id == "anonymous"
|
|
|
|
|
{
|
|
|
|
|
state.config().dev_user_id.clone()
|
|
|
|
|
} else {
|
|
|
|
|
context.auth.actor_id.clone()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
json!({
|
|
|
|
|
"userId": user_id,
|
|
|
|
|
"sessionId": ai_options
|
|
|
|
|
.and_then(|value| value.get("sessionId"))
|
|
|
|
|
.and_then(Value::as_str)
|
|
|
|
|
.map(str::trim)
|
|
|
|
|
.filter(|value| !value.is_empty()),
|
|
|
|
|
"model": ai_options
|
|
|
|
|
.and_then(|value| value.get("model"))
|
|
|
|
|
.and_then(Value::as_str)
|
|
|
|
|
.map(str::trim)
|
|
|
|
|
.filter(|value| !value.is_empty()),
|
|
|
|
|
"modelKey": ai_options
|
|
|
|
|
.and_then(|value| value.get("modelKey"))
|
|
|
|
|
.and_then(Value::as_str)
|
|
|
|
|
.map(str::trim)
|
|
|
|
|
.filter(|value| !value.is_empty()),
|
|
|
|
|
"profileId": ai_options
|
|
|
|
|
.and_then(|value| value.get("profileId"))
|
|
|
|
|
.and_then(Value::as_str)
|
|
|
|
|
.map(str::trim)
|
|
|
|
|
.filter(|value| !value.is_empty()),
|
|
|
|
|
"maxSteps": payload.get("maxSteps").filter(|value| !value.is_null()).cloned().unwrap_or_else(|| json!(10)),
|
|
|
|
|
"messages": payload.get("messages").cloned().unwrap_or_else(|| json!([])),
|
|
|
|
|
"context": build_document_ai_context(payload.get("context")),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn build_document_ai_context(context: Option<&Value>) -> Value {
|
|
|
|
|
let get = |key: &str| {
|
|
|
|
|
context
|
|
|
|
|
.and_then(|value| value.get(key))
|
|
|
|
|
.cloned()
|
|
|
|
|
.unwrap_or(Value::Null)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
json!({
|
|
|
|
|
"source": get("source"),
|
|
|
|
|
"action": get("action"),
|
|
|
|
|
"documentId": get("documentId"),
|
|
|
|
|
"workspaceId": get("workspaceId"),
|
|
|
|
|
"selectedBlockId": get("selectedBlockId"),
|
|
|
|
|
"selectedBlockIndex": get("selectedBlockIndex"),
|
|
|
|
|
"selectedUids": get("selectedUids"),
|
|
|
|
|
"selectedText": get("selectedText"),
|
|
|
|
|
"selection": get("selection"),
|
|
|
|
|
"tiptapDocument": get("tiptapDocument"),
|
|
|
|
|
"documentBlocks": get("documentBlocks"),
|
|
|
|
|
"node": get("node"),
|
|
|
|
|
"subtree": get("subtree"),
|
|
|
|
|
"outline": get("outline"),
|
|
|
|
|
"evidence": get("evidence"),
|
|
|
|
|
"pageOptions": get("pageOptions"),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn apply_ai_forward_headers(
|
|
|
|
|
mut request: reqwest::RequestBuilder,
|
|
|
|
|
headers: &HeaderMap,
|
|
|
|
|
context: &RequestContext,
|
|
|
|
|
) -> reqwest::RequestBuilder {
|
|
|
|
|
for (name, value) in headers.iter() {
|
|
|
|
|
if is_hop_by_hop_header(name.as_str())
|
|
|
|
|
|| name == header::HOST
|
|
|
|
|
|| name == header::CONTENT_LENGTH
|
|
|
|
|
|| name == header::CONTENT_TYPE
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
request = request.header(name.as_str(), value.as_bytes());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
request = request.header(header::CONTENT_TYPE.as_str(), "application/json");
|
|
|
|
|
request = request.header("x-request-id", context.trace.request_id.as_str());
|
|
|
|
|
request = request.header("x-trace-id", context.trace.trace_id.as_str());
|
|
|
|
|
request = request.header("x-mnote-source-channel", "rust_web_route");
|
|
|
|
|
request = request.header("x-mnote-source-client", "mnote-web");
|
|
|
|
|
request = request.header("x-mnote-actor-id", context.auth.actor_id.as_str());
|
|
|
|
|
request.header("x-mnote-actor-type", context.auth.actor_type.as_str())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn build_ai_sse_proxy_response(
|
|
|
|
|
upstream_response: reqwest::Response,
|
|
|
|
|
context: &RequestContext,
|
|
|
|
|
) -> Result<Response, WebError> {
|
|
|
|
|
let status =
|
|
|
|
|
StatusCode::from_u16(upstream_response.status().as_u16()).unwrap_or(StatusCode::OK);
|
|
|
|
|
let ready = Bytes::from(format!(
|
|
|
|
|
"event: ready\ndata: {}\n\n",
|
|
|
|
|
json!({"ok": true, "requestId": context.trace.request_id}).to_string()
|
|
|
|
|
));
|
|
|
|
|
let upstream_stream = upstream_response
|
|
|
|
|
.bytes_stream()
|
|
|
|
|
.map_err(std::io::Error::other);
|
|
|
|
|
let body_stream = futures_util::stream::once(async move { Ok::<Bytes, std::io::Error>(ready) })
|
|
|
|
|
.chain(upstream_stream);
|
|
|
|
|
|
|
|
|
|
let mut response = Response::builder()
|
|
|
|
|
.status(status)
|
|
|
|
|
.header(header::CONTENT_TYPE, "text/event-stream; charset=utf-8")
|
|
|
|
|
.header(header::CACHE_CONTROL, "no-cache, no-transform")
|
|
|
|
|
.header(header::CONNECTION, "keep-alive")
|
|
|
|
|
.header("x-accel-buffering", "no")
|
|
|
|
|
.body(Body::from_stream(body_stream))
|
|
|
|
|
.map_err(|error| WebError::internal(format!("AI SSE 响应构造失败: {error}")))?;
|
|
|
|
|
stamp_owner_header(response.headers_mut());
|
|
|
|
|
Ok(response)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn stamp_owner_header(headers: &mut HeaderMap) {
|
|
|
|
|
if let Ok(name) = HeaderName::from_lowercase(b"x-mnote-web-owner") {
|
|
|
|
|
headers.insert(name, HeaderValue::from_static("mnote-web"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn resolve_ai_orchestrator_backend_url() -> Option<String> {
|
|
|
|
|
read_env_or_dotenv("BACKEND_INTERNAL_URL")
|
|
|
|
|
.or_else(|| read_env_or_dotenv("BACKEND_URL"))
|
|
|
|
|
.map(|value| value.trim().trim_end_matches('/').to_string())
|
|
|
|
|
.filter(|value| !value.is_empty())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn read_env_or_dotenv(key: &str) -> Option<String> {
|
|
|
|
|
if let Ok(value) = env::var(key) {
|
|
|
|
|
let trimmed = value.trim().trim_matches('"').to_string();
|
|
|
|
|
if !trimmed.is_empty() {
|
|
|
|
|
return Some(trimmed);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
|
|
|
|
|
.join("../../..")
|
|
|
|
|
.join(".env.all");
|
|
|
|
|
let content = fs::read_to_string(root).ok()?;
|
|
|
|
|
for line in content.lines() {
|
|
|
|
|
let line = line.trim_end_matches('\r');
|
|
|
|
|
if line.starts_with('#') || line.trim().is_empty() {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
let Some((k, v)) = line.split_once('=') else {
|
|
|
|
|
continue;
|
|
|
|
|
};
|
|
|
|
|
if k.trim() != key {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
let trimmed = v.trim().trim_matches('"').to_string();
|
|
|
|
|
if !trimmed.is_empty() {
|
|
|
|
|
return Some(trimmed);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn is_hop_by_hop_header(name: &str) -> bool {
|
|
|
|
|
matches!(
|
|
|
|
|
name.to_ascii_lowercase().as_str(),
|
|
|
|
|
"connection"
|
|
|
|
|
| "keep-alive"
|
|
|
|
|
| "proxy-authenticate"
|
|
|
|
|
| "proxy-authorization"
|
|
|
|
|
| "te"
|
|
|
|
|
| "trailers"
|
|
|
|
|
| "transfer-encoding"
|
|
|
|
|
| "upgrade"
|
|
|
|
|
)
|
2026-04-16 22:01:51 +08:00
|
|
|
}
|
2026-04-17 00:25:28 +08:00
|
|
|
|
|
|
|
|
pub async fn next_sidebar(
|
|
|
|
|
State(state): State<AppState>,
|
|
|
|
|
Extension(context): Extension<RequestContext>,
|
|
|
|
|
Query(query): Query<CompatSidebarQuery>,
|
|
|
|
|
) -> Result<(StatusCode, Json<Value>), WebError> {
|
|
|
|
|
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(
|
2026-04-17 00:25:28 +08:00
|
|
|
state.config(),
|
|
|
|
|
&context,
|
2026-04-18 05:43:49 +08:00
|
|
|
&ProjectionSnapshotSpec {
|
|
|
|
|
workspace_id: &effective_workspace_id,
|
|
|
|
|
root_node_id: None,
|
|
|
|
|
depth: None,
|
2026-04-26 19:35:52 +08:00
|
|
|
query: None,
|
|
|
|
|
max_results: None,
|
2026-04-18 05:43:49 +08:00
|
|
|
projection: KernelProjectionKind::SidebarTree,
|
2026-04-17 00:25:28 +08:00
|
|
|
},
|
2026-04-17 23:36:24 +08:00
|
|
|
)
|
|
|
|
|
.await?;
|
2026-04-17 00:25:28 +08:00
|
|
|
|
2026-04-18 05:43:49 +08:00
|
|
|
let mut dataset_object = snapshot.dataset.as_object().cloned().ok_or_else(|| {
|
2026-04-17 00:25:28 +08:00
|
|
|
WebError::bad_gateway_code("convex_bad_response", "sidebar.dataset.list 返回值不是对象")
|
|
|
|
|
.with_context(&context)
|
|
|
|
|
.with_header("x-error-phase", "compat_sidebar_shape")
|
|
|
|
|
.with_header("x-upstream-service", "convex")
|
|
|
|
|
})?;
|
2026-04-18 09:38:16 +08:00
|
|
|
dataset_object.insert("kernel_sidebar_projection".into(), snapshot.projection);
|
2026-04-17 00:25:28 +08:00
|
|
|
|
|
|
|
|
Ok((
|
|
|
|
|
StatusCode::OK,
|
|
|
|
|
Json(json!({
|
|
|
|
|
"ok": true,
|
|
|
|
|
"boundary": "next_sidebar_compat",
|
|
|
|
|
"requestId": context.trace.request_id,
|
|
|
|
|
"traceId": context.trace.trace_id,
|
|
|
|
|
"workspaceId": effective_workspace_id,
|
|
|
|
|
"result": Value::Object(dataset_object),
|
|
|
|
|
})),
|
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
2026-04-29 12:24:44 +08:00
|
|
|
use crate::app::{build_app, AppConfig, AppState};
|
2026-04-17 00:25:28 +08:00
|
|
|
use axum::body::Body;
|
|
|
|
|
use axum::http::{Request, StatusCode};
|
2026-05-06 21:44:20 +08:00
|
|
|
use axum::response::IntoResponse;
|
|
|
|
|
use tokio::net::TcpListener;
|
2026-04-17 00:25:28 +08:00
|
|
|
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(),
|
2026-04-29 12:24:44 +08:00
|
|
|
public_bind_addr: "127.0.0.1:3000".into(),
|
|
|
|
|
legacy_next_base_url: Some("http://127.0.0.1:3100".into()),
|
|
|
|
|
enable_legacy_next_compat: true,
|
2026-04-23 07:38:34 +08:00
|
|
|
enable_debug_shell_routes: false,
|
2026-04-17 00:25:28 +08:00
|
|
|
hermes_base_path: "/api/hermes".into(),
|
|
|
|
|
compat_next_base_path: "/api/compat/next".into(),
|
|
|
|
|
convex_url: None,
|
|
|
|
|
convex_admin_key: None,
|
|
|
|
|
allow_dev_fixtures: true,
|
2026-04-17 23:36:24 +08:00
|
|
|
query_fixtures_json: Some(r#"{"sidebar:datasetList":{"active_workspace_id":"ws_demo","workspaces":[],"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"}],"trashed_documents":[],"media_assets":[],"trashed_media_assets":[],"mindmap_assets":[],"trashed_mindmap_assets":[],"table_assets":[],"trashed_table_assets":[],"mindmap_docs":[],"mindmap_asset_children":{}},"bridgeLogs:listWorkspaceOverview":{"workspace_id":"ws_demo","command_logs":[],"domain_events":[],"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-17 00:25:28 +08:00
|
|
|
dev_user_id: "dev-user".into(),
|
|
|
|
|
dev_user_name: "开发用户".into(),
|
|
|
|
|
dev_user_email: "dev@mnote.local".into(),
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
async fn compat_sidebar_route_returns_dataset_and_projection() {
|
|
|
|
|
let response = app()
|
|
|
|
|
.oneshot(
|
|
|
|
|
Request::builder()
|
|
|
|
|
.uri("/api/compat/next/sidebar?workspaceId=ws_demo")
|
|
|
|
|
.body(Body::empty())
|
|
|
|
|
.expect("request"),
|
|
|
|
|
)
|
|
|
|
|
.await
|
|
|
|
|
.expect("response");
|
|
|
|
|
|
|
|
|
|
assert_eq!(response.status(), StatusCode::OK);
|
|
|
|
|
}
|
2026-05-06 21:44:20 +08:00
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
async fn direct_ai_agent_run_is_owned_by_rust_web_when_next_compat_disabled() {
|
|
|
|
|
let response = build_app(AppState::new(AppConfig {
|
|
|
|
|
service_name: "mnote-web".into(),
|
|
|
|
|
service_version: "0.1.0".into(),
|
|
|
|
|
bind_addr: "127.0.0.1:0".into(),
|
|
|
|
|
public_bind_addr: "127.0.0.1:3000".into(),
|
|
|
|
|
legacy_next_base_url: Some("http://127.0.0.1:3100".into()),
|
|
|
|
|
enable_legacy_next_compat: false,
|
|
|
|
|
enable_debug_shell_routes: false,
|
|
|
|
|
hermes_base_path: "/api/hermes".into(),
|
|
|
|
|
compat_next_base_path: "/api/compat/next".into(),
|
|
|
|
|
convex_url: None,
|
|
|
|
|
convex_admin_key: None,
|
|
|
|
|
allow_dev_fixtures: true,
|
|
|
|
|
query_fixtures_json: None,
|
|
|
|
|
mutation_fixtures_json: None,
|
|
|
|
|
dev_user_id: "dev-user".into(),
|
|
|
|
|
dev_user_name: "开发用户".into(),
|
|
|
|
|
dev_user_email: "dev@mnote.local".into(),
|
|
|
|
|
}))
|
|
|
|
|
.oneshot(
|
|
|
|
|
Request::builder()
|
|
|
|
|
.method("POST")
|
|
|
|
|
.uri("/api/ai-agent/run")
|
|
|
|
|
.header("content-type", "application/json")
|
|
|
|
|
.body(Body::from(r#"{"stream":true,"scope":"document","messages":[{"role":"user","content":"ping"}],"context":{"documentId":"doc-1"},"options":{"ai":{"provider":"online"}}}"#))
|
|
|
|
|
.expect("request"),
|
|
|
|
|
)
|
|
|
|
|
.await
|
|
|
|
|
.expect("response");
|
|
|
|
|
|
|
|
|
|
assert_ne!(response.status(), StatusCode::SERVICE_UNAVAILABLE);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
response
|
|
|
|
|
.headers()
|
|
|
|
|
.get("x-mnote-web-owner")
|
|
|
|
|
.and_then(|value| value.to_str().ok()),
|
|
|
|
|
Some("mnote-web")
|
|
|
|
|
);
|
|
|
|
|
let body = axum::body::to_bytes(response.into_body(), usize::MAX)
|
|
|
|
|
.await
|
|
|
|
|
.expect("body");
|
|
|
|
|
let text = String::from_utf8(body.to_vec()).expect("utf8");
|
|
|
|
|
assert!(!text.contains("legacy_next_compat_disabled"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
async fn ai_agent_run_proxies_to_next_ai_route_when_legacy_next_compat_enabled() {
|
|
|
|
|
let next_app = axum::Router::new().route(
|
|
|
|
|
"/api/ai-agent/run",
|
|
|
|
|
axum::routing::post(|| async move {
|
|
|
|
|
(
|
|
|
|
|
[(
|
|
|
|
|
axum::http::header::CONTENT_TYPE,
|
|
|
|
|
"text/event-stream; charset=utf-8",
|
|
|
|
|
)],
|
|
|
|
|
"event: assistant_message\ndata: {\"text\":\"hello from next ai\"}\n\n",
|
|
|
|
|
)
|
|
|
|
|
.into_response()
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
let listener = TcpListener::bind("127.0.0.1:0").await.expect("bind next");
|
|
|
|
|
let addr = listener.local_addr().expect("local addr");
|
|
|
|
|
let server = tokio::spawn(async move {
|
|
|
|
|
axum::serve(listener, next_app).await.expect("serve next");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let response = build_app(AppState::new(AppConfig {
|
|
|
|
|
service_name: "mnote-web".into(),
|
|
|
|
|
service_version: "0.1.0".into(),
|
|
|
|
|
bind_addr: "127.0.0.1:0".into(),
|
|
|
|
|
public_bind_addr: "127.0.0.1:3000".into(),
|
|
|
|
|
legacy_next_base_url: Some(format!("http://{}", addr)),
|
|
|
|
|
enable_legacy_next_compat: true,
|
|
|
|
|
enable_debug_shell_routes: false,
|
|
|
|
|
hermes_base_path: "/api/hermes".into(),
|
|
|
|
|
compat_next_base_path: "/api/compat/next".into(),
|
|
|
|
|
convex_url: None,
|
|
|
|
|
convex_admin_key: None,
|
|
|
|
|
allow_dev_fixtures: true,
|
|
|
|
|
query_fixtures_json: None,
|
|
|
|
|
mutation_fixtures_json: None,
|
|
|
|
|
dev_user_id: "dev-user".into(),
|
|
|
|
|
dev_user_name: "开发用户".into(),
|
|
|
|
|
dev_user_email: "dev@mnote.local".into(),
|
|
|
|
|
}))
|
|
|
|
|
.oneshot(
|
|
|
|
|
Request::builder()
|
|
|
|
|
.method("POST")
|
|
|
|
|
.uri("/api/ai-agent/run")
|
|
|
|
|
.header("content-type", "application/json")
|
|
|
|
|
.body(Body::from(r#"{"stream":true,"scope":"document","messages":[{"role":"user","content":"ping"}],"context":{"documentId":"doc-1"},"options":{"ai":{"provider":"hermes"}}}"#))
|
|
|
|
|
.expect("request"),
|
|
|
|
|
)
|
|
|
|
|
.await
|
|
|
|
|
.expect("response");
|
|
|
|
|
|
|
|
|
|
assert_eq!(response.status(), StatusCode::OK);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
response
|
|
|
|
|
.headers()
|
|
|
|
|
.get("x-mnote-web-owner")
|
|
|
|
|
.and_then(|value| value.to_str().ok()),
|
|
|
|
|
Some("mnote-web")
|
|
|
|
|
);
|
|
|
|
|
let body = axum::body::to_bytes(response.into_body(), usize::MAX)
|
|
|
|
|
.await
|
|
|
|
|
.expect("body");
|
|
|
|
|
let text = String::from_utf8(body.to_vec()).expect("utf8");
|
|
|
|
|
assert!(text.contains("assistant_message"));
|
|
|
|
|
assert!(text.contains("hello from next ai"));
|
|
|
|
|
|
|
|
|
|
server.abort();
|
|
|
|
|
}
|
2026-04-17 00:25:28 +08:00
|
|
|
}
|