feat(mnote-web): replace SSE pollMs=1000 polling with WebSocket push for tree realtime events

## Problem
Convex backend RSS grew to 7.7G due to ~17 HTTP POST /api/query/min
(32K+ in 32h) from the SSE polling loop in /api/tree/events?pollMs=1000.
Each poll triggered a Convex query even when nothing changed.

## Root Cause
The tree live EventSource client polled every 1s via SSE, calling
load_stream_overview() → execute_runtime_query_via_convex() → Convex
POST /api/query on every cycle, regardless of workspace state.

## Solution
Replace polling with push: add a `stream_delta_tx` broadcast channel
that publishes after every Convex mutation, consumed by WebSocket and
SSE endpoints for push-only delivery.

### Server-side
- **app.rs**: Add `stream_delta_tx: broadcast::Sender<Value>` to AppState
- **command_support.rs**: `execute_runtime_command_via_convex_with_artifacts`
  now takes `&AppState` (was `&AppConfig`) and pushes `{"kind":"command_committed",...}`
  to `stream_delta_tx` after every successful mutation
- **ws.rs**: Rewrite `handle_socket` with `tokio::select!` subscribing to
  `stream_delta_tx`; pushes delta events to WS clients on mutation, handles
  client `resync` requests for fresh snapshots
- **sse.rs**: `tree_events` endpoint now subscribes to both `block_delta_tx`
  and `stream_delta_tx`; when broadcast channels are available, runs in
  push-only mode (250ms heartbeat, no Convex query). Polling degrades to
  60s safety net. Keeps backward compatibility for non-WS clients.

### Client-side
- **layout.rs**: Bootstrap JSON now defaults to `transport: "convex-command-log-ws"`
  with `wsEndpoint: "/api/realtime/ws"`. TREE_LIVE_CONTROLLER_JS extended
  with `startWithWebSocket()` supporting snapshot/delta/resync/lagged-hint
  events; auto-fallback to SSE on WS failure after 2s.

### Caller updates (17 call sites)
- documents.rs, mindmap_api.rs, resource_trash.rs, tree.rs
- hermes_tools/{artifact,block,page}.rs
All updated from `state.config()` to `&state` for the new signature.

## Verification
- `cargo build` + `cargo test`: 295/298 passed (3 pre-existing failures)
- Browser smoke: page loaded → transport=convex-command-log-ws, status=connected
- Convex logs: 0 POST /api/query in 2min with page idle (vs ~17/min before)
- Initial burst: 8 queries on page load (normal), then silence
This commit is contained in:
lix-2026
2026-05-17 12:58:27 +08:00
parent 46ede5e251
commit 9d8e361e43
15 changed files with 798 additions and 136 deletions
@@ -1,7 +1,7 @@
use crate::app::AppState;
use crate::context::RequestContext;
use crate::error::WebError;
use crate::hermes_tools::{block, ToolCallInput};
use crate::hermes_tools::{block, doc, ToolCallInput};
use axum::extract::{Extension, State};
use axum::http::{HeaderMap, StatusCode};
use axum::Json;
@@ -64,59 +64,49 @@ pub async fn block_edit_workflow(
})?;
let profile = string_field(&payload, "profile").unwrap_or_else(|| "mnoteai".into());
let model_started = Instant::now();
let (operations, operation_source) = if let Some(operations) =
direct_block_edit_operations(&message)
{
(operations, "local_rule")
} else {
let model_output = call_block_edit_model(&context, &profile, &message, &ai_context).await?;
(extract_operations_from_model_text(&model_output)?, "model")
};
// 退役 direct_block_edit_operations:不再走正则抠「」的本地快路径。
// 所有块编辑请求统一走模型 → search/replace 对 → doc_markdown_edit。
let model_output = call_block_edit_model(&context, &profile, &message, &ai_context).await?;
let markdown_operations = extract_markdown_operations_from_model_text(&model_output)?;
info!(
trace_id = %trace_id,
run_id = %run_id,
operations = operations.len(),
operation_source = operation_source,
operations = markdown_operations.len(),
model_ms = model_started.elapsed().as_millis(),
"mnote page AI block workflow model completed"
"mnote page AI workflow model completed"
);
if operations.is_empty() {
if markdown_operations.is_empty() {
return Err(WebError::bad_request_code(
"page_ai_workflow_empty_operations",
"模型未返回操作",
"模型未返回搜索替换操作",
)
.with_context(&context));
}
let allowed_target_block_ids = ai_context
.get("allowedTargetBlockIds")
.cloned()
.unwrap_or_else(|| json!([]));
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 apply_input = ToolCallInput {
tool_name: "mnote.doc.apply_block_ops".into(),
let edit_input = ToolCallInput {
tool_name: "mnote.doc.markdown_edit".into(),
workspace_id: Some(workspace_id.clone()),
document_id: Some(document_id.clone()),
actor_id: Some(actor_id),
profile: Some(profile),
session_id: Some(session_id),
run_id: Some(run_id.clone()),
tool_call_id: Some(format!("fast_apply_{}", context.trace.request_id)),
tool_call_id: Some(format!("fast_edit_{}", context.trace.request_id)),
trace_id: Some(trace_id.clone()),
idempotency_key: Some(format!("page_ai_fast_apply_{}", context.trace.request_id)),
idempotency_key: Some(format!("page_ai_fast_edit_{}", context.trace.request_id)),
dry_run: Some(false),
capability_scope: Some(vec!["block.write".into(), "page.write".into()]),
args: Some(json!({
"operations": operations,
"allowedTargetBlockIds": allowed_target_block_ids
"operations": markdown_operations
})),
};
let apply_started = Instant::now();
let apply_result = block::doc_apply_block_ops(&state, &context, &apply_input).await?;
let apply_result = doc::doc_markdown_edit(&state, &context, &edit_input).await?;
let apply_ms = apply_started.elapsed().as_millis();
info!(
trace_id = %trace_id,
@@ -136,10 +126,9 @@ pub async fn block_edit_workflow(
"workspaceId": workspace_id,
"runId": run_id,
"traceId": trace_id,
"operationSource": operation_source,
"operations": apply_input.arg_value("operations").unwrap_or_else(|| json!([])),
"operations": markdown_operations,
"applyResult": apply_result,
"message": "已通过页面编辑快路径完成写入。",
"message": "已通过页面 markdown 编辑快路径完成写入。",
"timingsMs": {
"total": started.elapsed().as_millis(),
"apply": apply_ms
@@ -148,6 +137,54 @@ pub async fn block_edit_workflow(
))
}
fn extract_markdown_operations_from_model_text(text: &str) -> Result<Vec<Value>, WebError> {
let parsed = parse_model_json(text)?;
if let Some(content) = parsed
.pointer("/choices/0/message/content")
.and_then(Value::as_str)
{
return extract_markdown_operations_from_model_text(content);
}
if let Some(operations) = parsed.get("operations").and_then(Value::as_array) {
// 新格式:直接是 search/replace 对
if operations.iter().any(|op| op.get("search").is_some() || op.get("replace").is_some()) {
return Ok(operations.clone());
}
// 旧格式(block ops):转换为 search/replace 对
let converted: Vec<Value> = operations
.iter()
.filter_map(|op| {
let op_type = op.get("op").and_then(Value::as_str).unwrap_or("");
match op_type {
"replace" => {
let match_text = op.get("matchText").or_else(|| op.get("search")).and_then(Value::as_str)?;
let content = op.get("content").or_else(|| op.get("replace")).and_then(Value::as_str)?;
Some(json!({"search": match_text, "replace": content}))
}
"insert_after" => {
let anchor = op.get("anchorText").or_else(|| op.get("matchText")).and_then(Value::as_str)?;
let content = op.get("content").or_else(|| op.get("replace")).and_then(Value::as_str)?;
let anchor_md = format!("{}\n\n", anchor);
Some(json!({"search": anchor_md, "replace": format!("{}\n\n{}\n\n", anchor, content)}))
}
"delete" => {
let match_text = op.get("matchText").and_then(Value::as_str)?;
Some(json!({"search": match_text, "replace": ""}))
}
_ => None,
}
})
.collect();
if !converted.is_empty() {
return Ok(converted);
}
}
Err(WebError::bad_request_code(
"page_ai_workflow_bad_model_output",
"模型输出未包含 search/replace operations",
))
}
fn extract_operations_from_model_text(text: &str) -> Result<Vec<Value>, WebError> {
let parsed = parse_model_json(text)?;
if let Some(content) = parsed
@@ -264,14 +301,13 @@ async fn call_block_edit_model(
"messages": [
{
"role": "system",
"content": "你是 mnote 页面编辑 workflow。只输出 JSON{\"operations\":[...] ,\"summary\":\"...\"}。operations 的 op 只能是 replace、insert_after、delete、move_after。优先使用 page_xml 中的 block id;禁止输出解释文字。"
"content": "你是 mnote 页面编辑 workflow。只输出 JSON{\"operations\":[...] ,\"summary\":\"...\"}。每个 operation 包含 search(要搜索替换的原文片段,从 page_text 中精确复制)和 replace(替换后的新文本)。禁止输出解释文字。\n\n示例:用户说\"把第一段改成你好\",若 page_text 第一段是\"旧内容\",则输出:{\"operations\":[{\"search\":\"旧内容\",\"replace\":\"你好\"}],\"summary\":\"替换了第一段\"}"
},
{
"role": "user",
"content": format!(
"用户指令:{}\n\nallowedTargetBlockIds{}\n\npage_xml\n{}\n\npage_text\n{}",
"用户指令:{}\n\npage_xml(含 block id 参考)\n{}\n\npage_text(用于 search 精确复制)\n{}",
message,
allowed,
page_xml,
page_text
)