feat: 收口 Rust Web 3000 主链
This commit is contained in:
@@ -1,36 +1,22 @@
|
||||
use crate::app::AppState;
|
||||
use crate::context::RequestContext;
|
||||
use crate::error::WebError;
|
||||
use crate::routes::query_support::resolve_effective_workspace_id;
|
||||
use crate::routes::snapshot_support::{load_projection_snapshot, ProjectionSnapshotSpec};
|
||||
use axum::body::{Body, Bytes};
|
||||
use axum::extract::Query;
|
||||
use axum::body::Body;
|
||||
use axum::extract::{Extension, State};
|
||||
use axum::http::{header, HeaderMap, HeaderName, HeaderValue, Request, StatusCode};
|
||||
use axum::response::{IntoResponse, Response};
|
||||
use axum::Json;
|
||||
use core_protocol::KernelProjectionKind;
|
||||
use futures_util::{StreamExt, TryStreamExt};
|
||||
use serde::Deserialize;
|
||||
use serde_json::{json, Value};
|
||||
use std::env;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use std::process::Command;
|
||||
use std::time::Duration;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct CompatSidebarQuery {
|
||||
pub workspace_id: Option<String>,
|
||||
}
|
||||
|
||||
pub async fn next_ai_agent_run(
|
||||
State(state): State<AppState>,
|
||||
Extension(context): Extension<RequestContext>,
|
||||
request: Request<Body>,
|
||||
) -> Result<Response, WebError> {
|
||||
let (parts, body) = request.into_parts();
|
||||
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}")))?;
|
||||
@@ -43,24 +29,11 @@ pub async fn next_ai_agent_run(
|
||||
.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 Some(provider) = explicit_agent_provider(&payload) {
|
||||
if provider == "hermes" {
|
||||
return run_direct_hermes_agent(&context, &payload).await;
|
||||
}
|
||||
return Err(WebError::bad_gateway_code(
|
||||
"ai_provider_bridge_unavailable",
|
||||
format!(
|
||||
"{provider} provider 需要可用的 Next AI bridge,不能静默降级到本地页面工具 host。"
|
||||
"{provider} provider 直连链路已退场;当前仅保留 mnote-cli host 主路径,不再静默降级。"
|
||||
),
|
||||
)
|
||||
.with_context(&context)
|
||||
@@ -68,69 +41,7 @@ pub async fn next_ai_agent_run(
|
||||
.with_header("x-mnote-ai-execution-owner", "provider-bridge-unavailable"));
|
||||
}
|
||||
|
||||
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)
|
||||
run_local_mnote_cli_ai_host(&state, &context, &payload).await
|
||||
}
|
||||
|
||||
fn explicit_agent_provider(payload: &Value) -> Option<&'static str> {
|
||||
@@ -149,369 +60,6 @@ fn explicit_agent_provider(payload: &Value) -> Option<&'static str> {
|
||||
}
|
||||
}
|
||||
|
||||
async fn run_direct_hermes_agent(
|
||||
context: &RequestContext,
|
||||
payload: &Value,
|
||||
) -> Result<Response, WebError> {
|
||||
let base_url = resolve_hermes_api_base_url();
|
||||
let api_key = read_env_or_dotenv("MNOTE_HERMES_API_KEY").ok_or_else(|| {
|
||||
WebError::bad_gateway_code(
|
||||
"hermes_bridge_unconfigured",
|
||||
"未配置 MNOTE_HERMES_API_KEY,无法直连 Hermes API。",
|
||||
)
|
||||
.with_context(context)
|
||||
.with_header("x-mnote-web-owner", "mnote-web")
|
||||
.with_header("x-mnote-ai-execution-owner", "hermes")
|
||||
})?;
|
||||
let client = reqwest::Client::builder()
|
||||
.connect_timeout(Duration::from_secs(5))
|
||||
.timeout(Duration::from_secs(180))
|
||||
.redirect(reqwest::redirect::Policy::none())
|
||||
.build()
|
||||
.map_err(|error| WebError::internal(format!("Hermes HTTP 客户端创建失败: {error}")))?;
|
||||
|
||||
let start_url = reqwest::Url::parse(&format!("{}/v1/runs", base_url.trim_end_matches('/')))
|
||||
.map_err(|error| WebError::internal(format!("Hermes runs URL 非法: {error}")))?;
|
||||
let start_response = client
|
||||
.post(start_url)
|
||||
.bearer_auth(&api_key)
|
||||
.json(&build_hermes_run_payload(payload))
|
||||
.send()
|
||||
.await
|
||||
.map_err(|error| {
|
||||
WebError::bad_gateway_code(
|
||||
"hermes_bridge_start_error",
|
||||
format!("Hermes run 启动请求失败: {error}"),
|
||||
)
|
||||
.with_context(context)
|
||||
.with_header("x-mnote-web-owner", "mnote-web")
|
||||
.with_header("x-upstream-service", "hermes-api")
|
||||
.with_header("x-mnote-ai-execution-owner", "hermes")
|
||||
})?;
|
||||
|
||||
if !start_response.status().is_success() {
|
||||
let status = start_response.status();
|
||||
let body = start_response
|
||||
.text()
|
||||
.await
|
||||
.unwrap_or_else(|error| format!("读取 Hermes 错误响应失败: {error}"));
|
||||
return Err(WebError::bad_gateway_code(
|
||||
"hermes_bridge_start_rejected",
|
||||
format!("Hermes run 启动返回 HTTP {}: {}", status.as_u16(), body),
|
||||
)
|
||||
.with_context(context)
|
||||
.with_header("x-mnote-web-owner", "mnote-web")
|
||||
.with_header("x-upstream-service", "hermes-api")
|
||||
.with_header("x-mnote-ai-execution-owner", "hermes"));
|
||||
}
|
||||
|
||||
let started: Value = start_response.json().await.map_err(|error| {
|
||||
WebError::bad_gateway_code(
|
||||
"hermes_bridge_bad_start_response",
|
||||
format!("Hermes run 启动响应不是合法 JSON: {error}"),
|
||||
)
|
||||
.with_context(context)
|
||||
.with_header("x-mnote-web-owner", "mnote-web")
|
||||
.with_header("x-upstream-service", "hermes-api")
|
||||
.with_header("x-mnote-ai-execution-owner", "hermes")
|
||||
})?;
|
||||
let run_id = started
|
||||
.get("run_id")
|
||||
.and_then(Value::as_str)
|
||||
.map(str::trim)
|
||||
.filter(|value| !value.is_empty())
|
||||
.ok_or_else(|| {
|
||||
WebError::bad_gateway_code(
|
||||
"hermes_bridge_missing_run_id",
|
||||
"Hermes run 启动响应缺少 run_id。",
|
||||
)
|
||||
.with_context(context)
|
||||
.with_header("x-mnote-web-owner", "mnote-web")
|
||||
.with_header("x-upstream-service", "hermes-api")
|
||||
.with_header("x-mnote-ai-execution-owner", "hermes")
|
||||
})?;
|
||||
|
||||
let mut events_url =
|
||||
reqwest::Url::parse(&format!("{}/v1/runs/", base_url.trim_end_matches('/')))
|
||||
.map_err(|error| WebError::internal(format!("Hermes events URL 非法: {error}")))?;
|
||||
events_url
|
||||
.path_segments_mut()
|
||||
.map_err(|_| WebError::internal("Hermes events URL 不支持路径拼接"))?
|
||||
.pop_if_empty()
|
||||
.push(run_id)
|
||||
.push("events");
|
||||
|
||||
let events_response = client
|
||||
.get(events_url)
|
||||
.bearer_auth(api_key)
|
||||
.send()
|
||||
.await
|
||||
.map_err(|error| {
|
||||
WebError::bad_gateway_code(
|
||||
"hermes_bridge_events_error",
|
||||
format!("Hermes 事件流请求失败: {error}"),
|
||||
)
|
||||
.with_context(context)
|
||||
.with_header("x-mnote-web-owner", "mnote-web")
|
||||
.with_header("x-upstream-service", "hermes-api")
|
||||
.with_header("x-mnote-ai-execution-owner", "hermes")
|
||||
})?;
|
||||
|
||||
if !events_response.status().is_success() {
|
||||
let status = events_response.status();
|
||||
let body = events_response
|
||||
.text()
|
||||
.await
|
||||
.unwrap_or_else(|error| format!("读取 Hermes 事件流错误响应失败: {error}"));
|
||||
return Err(WebError::bad_gateway_code(
|
||||
"hermes_bridge_events_rejected",
|
||||
format!("Hermes 事件流返回 HTTP {}: {}", status.as_u16(), body),
|
||||
)
|
||||
.with_context(context)
|
||||
.with_header("x-mnote-web-owner", "mnote-web")
|
||||
.with_header("x-upstream-service", "hermes-api")
|
||||
.with_header("x-mnote-ai-execution-owner", "hermes"));
|
||||
}
|
||||
|
||||
let events_text = events_response.text().await.map_err(|error| {
|
||||
WebError::bad_gateway_code(
|
||||
"hermes_bridge_events_read_error",
|
||||
format!("读取 Hermes 事件流失败: {error}"),
|
||||
)
|
||||
.with_context(context)
|
||||
.with_header("x-mnote-web-owner", "mnote-web")
|
||||
.with_header("x-upstream-service", "hermes-api")
|
||||
.with_header("x-mnote-ai-execution-owner", "hermes")
|
||||
})?;
|
||||
|
||||
build_hermes_sse_response(context, parse_sse_data_json_values(&events_text))
|
||||
}
|
||||
|
||||
fn resolve_hermes_api_base_url() -> String {
|
||||
read_env_or_dotenv("MNOTE_HERMES_API_BASE_URL")
|
||||
.unwrap_or_else(|| "http://127.0.0.1:8642".into())
|
||||
.trim()
|
||||
.trim_end_matches('/')
|
||||
.to_string()
|
||||
}
|
||||
|
||||
fn build_hermes_run_payload(payload: &Value) -> Value {
|
||||
let messages = payload
|
||||
.get("messages")
|
||||
.and_then(Value::as_array)
|
||||
.cloned()
|
||||
.unwrap_or_default();
|
||||
let input: Vec<Value> = messages
|
||||
.iter()
|
||||
.map(|message| {
|
||||
json!({
|
||||
"role": message.get("role").and_then(Value::as_str).unwrap_or("user"),
|
||||
"content": message.get("content").and_then(Value::as_str).unwrap_or(""),
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
let conversation_history = if input.len() > 1 {
|
||||
input[..input.len() - 1].to_vec()
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
let session_id = payload
|
||||
.get("options")
|
||||
.and_then(|value| value.get("ai"))
|
||||
.and_then(|value| value.get("sessionId"))
|
||||
.and_then(Value::as_str)
|
||||
.map(str::trim)
|
||||
.filter(|value| !value.is_empty());
|
||||
|
||||
json!({
|
||||
"input": input,
|
||||
"conversation_history": conversation_history,
|
||||
"session_id": session_id,
|
||||
})
|
||||
}
|
||||
|
||||
fn parse_sse_data_json_values(text: &str) -> Vec<Value> {
|
||||
text.split("\n\n")
|
||||
.filter_map(|frame| {
|
||||
let data = frame
|
||||
.lines()
|
||||
.filter_map(|line| line.strip_prefix("data:"))
|
||||
.map(str::trim_start)
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
if data.trim().is_empty() {
|
||||
return None;
|
||||
}
|
||||
serde_json::from_str::<Value>(&data).ok()
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn sse_frame(event: &str, data: Value) -> String {
|
||||
format!("event: {event}\ndata: {}\n\n", data.to_string())
|
||||
}
|
||||
|
||||
fn build_hermes_sse_response(
|
||||
context: &RequestContext,
|
||||
events: Vec<Value>,
|
||||
) -> Result<Response, WebError> {
|
||||
let mut body = String::new();
|
||||
let mut assistant_text = String::new();
|
||||
let mut completed = false;
|
||||
body.push_str(&sse_frame(
|
||||
"ready",
|
||||
json!({"ok": true, "bridgeOwner": "hermes", "requestId": context.trace.request_id}),
|
||||
));
|
||||
for event in events {
|
||||
let event_name = event.get("event").and_then(Value::as_str).unwrap_or("");
|
||||
match event_name {
|
||||
"message.delta" => {
|
||||
if let Some(delta) = event.get("delta").and_then(Value::as_str) {
|
||||
assistant_text.push_str(delta);
|
||||
body.push_str(&sse_frame("assistant_delta", json!({"text": delta})));
|
||||
}
|
||||
}
|
||||
"run.completed" => {
|
||||
completed = true;
|
||||
let output = event
|
||||
.get("output")
|
||||
.and_then(Value::as_str)
|
||||
.map(str::trim)
|
||||
.filter(|value| !value.is_empty())
|
||||
.unwrap_or_else(|| assistant_text.trim());
|
||||
let text = if output.is_empty() {
|
||||
"(无输出)"
|
||||
} else {
|
||||
output
|
||||
};
|
||||
body.push_str(&sse_frame("assistant_message", json!({"text": text})));
|
||||
body.push_str(&sse_frame(
|
||||
"completion",
|
||||
json!({"ok": true, "text": text, "steps": 1}),
|
||||
));
|
||||
}
|
||||
"run.failed" => {
|
||||
completed = true;
|
||||
let message = event
|
||||
.get("error")
|
||||
.and_then(Value::as_str)
|
||||
.unwrap_or("Hermes 执行失败");
|
||||
body.push_str(&sse_frame(
|
||||
"error",
|
||||
json!({"ok": false, "message": message}),
|
||||
));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
if !completed {
|
||||
let text = assistant_text.trim();
|
||||
let text = if text.is_empty() {
|
||||
"(无输出)"
|
||||
} else {
|
||||
text
|
||||
};
|
||||
body.push_str(&sse_frame("assistant_message", json!({"text": text})));
|
||||
body.push_str(&sse_frame(
|
||||
"completion",
|
||||
json!({"ok": true, "text": text, "steps": 1}),
|
||||
));
|
||||
}
|
||||
|
||||
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", "hermes")
|
||||
.body(Body::from(body))
|
||||
.map_err(|error| WebError::internal(format!("Hermes SSE 响应构造失败: {error}")))?;
|
||||
stamp_owner_header(response.headers_mut());
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
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("../../..")
|
||||
}
|
||||
@@ -705,241 +253,18 @@ async fn run_local_mnote_cli_ai_host(
|
||||
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"
|
||||
)
|
||||
}
|
||||
|
||||
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 已确保存在");
|
||||
|
||||
let snapshot = load_projection_snapshot(
|
||||
state.config(),
|
||||
&context,
|
||||
&ProjectionSnapshotSpec {
|
||||
workspace_id: &effective_workspace_id,
|
||||
root_node_id: None,
|
||||
depth: None,
|
||||
query: None,
|
||||
max_results: None,
|
||||
projection: KernelProjectionKind::SidebarTree,
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
|
||||
let mut dataset_object = snapshot.dataset.as_object().cloned().ok_or_else(|| {
|
||||
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")
|
||||
})?;
|
||||
dataset_object.insert("kernel_sidebar_projection".into(), snapshot.projection);
|
||||
|
||||
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 {
|
||||
use crate::app::{build_app, AppConfig, AppState};
|
||||
use crate::context::RequestContext;
|
||||
use crate::routes::compat::{
|
||||
build_hermes_run_payload, build_hermes_sse_response, parse_sse_data_json_values,
|
||||
};
|
||||
use axum::body::Body;
|
||||
use axum::http::{HeaderMap, Method, Request, StatusCode, Uri};
|
||||
use axum::http::{Request, StatusCode};
|
||||
use axum::response::IntoResponse;
|
||||
use serde_json::json;
|
||||
use tokio::net::TcpListener;
|
||||
use tower::util::ServiceExt;
|
||||
|
||||
@@ -966,7 +291,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn compat_sidebar_route_returns_dataset_and_projection() {
|
||||
async fn compat_next_sidebar_route_is_not_registered_by_default() {
|
||||
let response = app()
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
@@ -977,7 +302,7 @@ mod tests {
|
||||
.await
|
||||
.expect("response");
|
||||
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
assert_eq!(response.status(), StatusCode::NOT_FOUND);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
@@ -1027,60 +352,6 @@ mod tests {
|
||||
assert!(!text.contains("legacy_next_compat_disabled"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hermes_payload_uses_page_messages_and_session_id() {
|
||||
let payload = json!({
|
||||
"messages": [
|
||||
{"role": "system", "content": "系统约束"},
|
||||
{"role": "user", "content": "总结当前页面"}
|
||||
],
|
||||
"options": {
|
||||
"ai": {
|
||||
"provider": "hermes",
|
||||
"sessionId": "hermes-session-1"
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let hermes_payload = build_hermes_run_payload(&payload);
|
||||
|
||||
assert_eq!(hermes_payload["session_id"], "hermes-session-1");
|
||||
assert_eq!(hermes_payload["input"][1]["content"], "总结当前页面");
|
||||
assert_eq!(hermes_payload["conversation_history"][0]["role"], "system");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn hermes_sse_is_translated_to_page_ai_events() {
|
||||
let events = parse_sse_data_json_values(
|
||||
"data: {\"event\":\"message.delta\",\"delta\":\"Hel\"}\n\n\
|
||||
data: {\"event\":\"message.delta\",\"delta\":\"lo\"}\n\n\
|
||||
data: {\"event\":\"run.completed\",\"output\":\"Hello\"}\n\n",
|
||||
);
|
||||
let context = RequestContext::from_http_parts(
|
||||
&Method::POST,
|
||||
&"/api/ai-agent/run".parse::<Uri>().expect("uri"),
|
||||
&HeaderMap::new(),
|
||||
);
|
||||
|
||||
let response = build_hermes_sse_response(&context, events).expect("response");
|
||||
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
assert_eq!(
|
||||
response
|
||||
.headers()
|
||||
.get("x-mnote-ai-execution-owner")
|
||||
.and_then(|value| value.to_str().ok()),
|
||||
Some("hermes")
|
||||
);
|
||||
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("event: assistant_message"));
|
||||
assert!(text.contains("event: completion"));
|
||||
assert!(text.contains("Hello"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn explicit_agent_provider_does_not_fallback_to_mnote_cli_when_next_unavailable() {
|
||||
let response = build_app(AppState::new(AppConfig {
|
||||
@@ -1126,11 +397,11 @@ mod tests {
|
||||
.expect("body");
|
||||
let text = String::from_utf8(body.to_vec()).expect("utf8");
|
||||
assert!(text.contains("codex"));
|
||||
assert!(!text.contains("mnote-cli"));
|
||||
assert!(text.contains("provider 直连链路已退场"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn ai_agent_run_proxies_to_next_ai_route_when_legacy_next_compat_enabled() {
|
||||
async fn explicit_hermes_provider_fails_instead_of_using_legacy_next_or_direct_bridge() {
|
||||
let next_app = axum::Router::new().route(
|
||||
"/api/ai-agent/run",
|
||||
axum::routing::post(|| async move {
|
||||
@@ -1180,20 +451,20 @@ mod tests {
|
||||
.await
|
||||
.expect("response");
|
||||
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
assert_eq!(response.status(), StatusCode::BAD_GATEWAY);
|
||||
assert_eq!(
|
||||
response
|
||||
.headers()
|
||||
.get("x-mnote-web-owner")
|
||||
.get("x-mnote-ai-execution-owner")
|
||||
.and_then(|value| value.to_str().ok()),
|
||||
Some("mnote-web")
|
||||
Some("provider-bridge-unavailable")
|
||||
);
|
||||
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"));
|
||||
assert!(text.contains("hermes"));
|
||||
assert!(!text.contains("hello from next ai"));
|
||||
|
||||
server.abort();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user