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
@@ -144,7 +144,7 @@ async fn create_artifact_node(
validate_only: false,
};
let execution = execute_runtime_command_via_convex_with_artifacts(
state.config(),
&state,
context,
workspace_id.as_deref(),
command,
@@ -1032,7 +1032,7 @@ async fn execute_page_body_save(
validate_only: false,
};
let execution = execute_runtime_command_via_convex_with_artifacts(
state.config(),
&state,
context,
workspace_id.as_deref(),
command,
@@ -1106,7 +1106,7 @@ async fn execute_page_body_save_from_aggregate(
validate_only: false,
};
let execution = execute_runtime_command_via_convex_with_artifacts(
state.config(),
&state,
context,
workspace_id.as_deref(),
command,
+348 -8
View File
@@ -46,6 +46,44 @@ pub async fn doc_fetch(
.filter(|block| block_id_of(block).as_deref() == Some(block_id.as_str()))
.collect()
}
"section" => {
// 提取第一个标题到下一个同级/高级标题之间的节
let heading_idx = blocks.iter().position(|block| {
block
.get("type")
.and_then(Value::as_str)
.map(|t| t == "heading")
.unwrap_or(false)
});
match heading_idx {
Some(start) => {
let level = blocks[start]
.pointer("/attrs/level")
.and_then(Value::as_u64)
.unwrap_or(2);
let mut end = blocks.len();
for (idx, block) in blocks.iter().enumerate().skip(start + 1) {
if block
.get("type")
.and_then(Value::as_str)
.map(|t| t == "heading")
.unwrap_or(false)
{
if let Some(hl) =
block.pointer("/attrs/level").and_then(Value::as_u64)
{
if hl <= level {
end = idx;
break;
}
}
}
}
blocks.drain(start..end).collect()
}
None => blocks, // 没有标题时返回全文
}
}
"keyword" => {
let query = input.arg_string("query").ok_or_else(|| {
WebError::bad_request_code(
@@ -76,6 +114,10 @@ pub async fn doc_fetch(
}
_ => blocks,
};
let max_chars = input
.arg_value("maxChars")
.and_then(|v| v.as_u64())
.unwrap_or(0) as usize;
let truncated = blocks.len() > max_blocks;
blocks.truncate(max_blocks);
let format = input
@@ -83,21 +125,59 @@ pub async fn doc_fetch(
.unwrap_or_else(|| "json".into())
.to_ascii_lowercase();
let include_ids = detail == "with_ids" || detail == "full";
let content = blocks_to_content(&format, &blocks, include_ids, &document_id, &aggregate);
let warnings = if truncated {
json!([{
let raw_content = blocks_to_content(&format, &blocks, include_ids, &document_id, &aggregate);
// maxChars 截断 + 片段包装
let mut content = raw_content;
let mut char_truncated = false;
if max_chars > 0 && content.chars().count() > max_chars {
content = content.chars().take(max_chars).collect();
char_truncated = true;
}
// 片段包装:非 full scope 时包裹注释标记
let is_partial = scope != "full";
if is_partial {
let marker = match scope.as_str() {
"section" => format!("<!-- fragment: section -->\n"),
"outline" => "<!-- fragment: outline -->\n".to_string(),
"keyword" => {
let query = input.arg_string("query").unwrap_or_default();
format!("<!-- fragment: keyword \"{}\" -->\n", query)
}
"block" => "<!-- fragment: block -->\n".to_string(),
"selection" => "<!-- fragment: selection -->\n".to_string(),
_ => String::new(),
};
if !marker.is_empty() {
content = format!("{}{}\n<!-- /fragment -->", marker, content);
}
}
let truncated_final = truncated || char_truncated;
let mut warnings_list = Vec::new();
if truncated {
warnings_list.push(json!({
"code": "mnote_doc_fetch_truncated",
"message": "结果已按 maxBlocks 裁剪",
"maxBlocks": max_blocks
}])
}));
}
if char_truncated {
warnings_list.push(json!({
"code": "mnote_doc_fetch_char_truncated",
"message": "结果已按 maxChars 裁剪",
"maxChars": max_chars
}));
}
let source = if document_id.starts_with('/') || document_id.starts_with("./") || document_id.contains('/') {
"local_fs"
} else {
json!([])
"convex"
};
Ok(json!({
"ok": true,
"schema": "mnote.page_ai_context.v1",
"documentId": document_id,
"workspaceId": workspace_id,
"source": source,
"revision": aggregate.pointer("/body/revision").cloned().unwrap_or(Value::Null),
"conflictDetectionKey": aggregate.pointer("/body/conflictDetectionKey").cloned().unwrap_or(Value::Null),
"format": format,
@@ -106,9 +186,9 @@ pub async fn doc_fetch(
"content": content,
"blocks": blocks,
"allowedTargetBlockIds": selected_block_ids(input),
"truncated": truncated,
"continuation": if truncated { json!({"maxBlocks": max_blocks}) } else { Value::Null },
"warnings": warnings
"truncated": truncated_final,
"continuation": if truncated_final { json!({"maxBlocks": max_blocks, "maxChars": max_chars}) } else { Value::Null },
"warnings": warnings_list
}))
}
@@ -536,3 +616,263 @@ fn escape_xml(value: &str) -> String {
.replace('"', "&quot;")
.replace('\'', "&apos;")
}
// ── mnote.doc.markdown_edit ──────────────────────────────────────────
pub async fn doc_markdown_edit(
state: &AppState,
context: &RequestContext,
input: &ToolCallInput,
) -> Result<Value, WebError> {
let document_id = input.effective_document_id().unwrap_or_default();
let workspace_id = input.effective_workspace_id();
// 1. 读取当前文档内容(markdown 形式)
let aggregate = aggregate_value(state, context, input).await?;
let blocks = block_projection_blocks(&aggregate);
let current_md = blocks_to_markdown(&blocks, true); // with_ids 用于操作后定位
// 2. 解析 operations
let operations: Vec<Value> = if let Some(full) = input.arg_string("full_content") {
// 全文替换模式
vec![json!({"search": current_md.trim(), "replace": full.trim()})]
} else {
input
.arg_value("operations")
.and_then(|v| v.as_array().cloned())
.ok_or_else(|| {
WebError::bad_request_code(
"mnote_tool_bad_request",
"mnote.doc.markdown_edit 缺少 operations 或 full_content",
)
.with_context(context)
})?
};
if operations.is_empty() {
return Err(WebError::bad_request_code(
"mnote_tool_bad_request",
"mnote.doc.markdown_edit operations 不能为空",
)
.with_context(context));
}
if operations.len() > 20 {
return Err(WebError::bad_request_code(
"mnote_tool_bad_request",
"mnote.doc.markdown_edit 一次最多允许 20 个操作",
)
.with_context(context));
}
// 3. 逐条执行 search_replace
let mut applied = 0usize;
let mut failed = Vec::new();
let mut md = current_md.clone();
for (idx, op) in operations.iter().enumerate() {
let search = op
.get("search")
.and_then(Value::as_str)
.map(str::to_string)
.unwrap_or_default();
let replace = op
.get("replace")
.and_then(Value::as_str)
.map(str::to_string)
.unwrap_or_default();
if search.is_empty() {
failed.push(json!({
"index": idx,
"reason": "search 不能为空",
"search": search
}));
continue;
}
match search_replace(&md, &search, &replace) {
Ok(new_md) => {
md = new_md;
applied += 1;
}
Err(reason) => {
failed.push(json!({
"index": idx,
"reason": reason,
"search": search
}));
}
}
}
// 4. 构建 changedText 摘要
let changed_text = if applied > 0 {
operations
.iter()
.take(applied)
.map(|op| {
let s = op.get("search").and_then(Value::as_str).unwrap_or("");
let r = op.get("replace").and_then(Value::as_str).unwrap_or("");
format!("{}」→「{}", s, r)
})
.collect::<Vec<_>>()
.join("\n")
} else {
String::from("无操作已应用")
};
// 5. 通过 block apply 写回(将 markdown 修改转换为 block 操作)
// Phase A 简化:使用 mnote.page.save 或 rebuild blocks
// 更精确的实现(Phase B):计算 block-level diff 并用 doc_apply_block_ops
let revision_before = aggregate
.pointer("/body/revision")
.cloned()
.unwrap_or(Value::Null);
// 构建 block ops:将修改后的 markdown 重新注入
let block_ops = build_block_ops_from_markdown_edit(&blocks, &operations, applied);
let apply_input = ToolCallInput {
tool_name: "mnote.doc.apply_block_ops".into(),
workspace_id: workspace_id.clone(),
document_id: Some(document_id.clone()),
actor_id: input.actor_id.clone(),
profile: input.profile.clone(),
session_id: input.session_id.clone(),
run_id: input.run_id.clone(),
tool_call_id: Some(format!("md_edit_{}", context.trace.request_id)),
trace_id: input.trace_id.clone(),
idempotency_key: Some(format!(
"md_edit_{}",
context.trace.request_id
)),
dry_run: input.dry_run,
capability_scope: Some(vec!["block.write".into(), "page.write".into()]),
args: Some(json!({
"operations": block_ops,
"allowedTargetBlockIds": json!([])
})),
};
let apply_result =
crate::hermes_tools::block::doc_apply_block_ops(state, context, &apply_input).await?;
Ok(json!({
"ok": true,
"schema": "mnote.doc.markdown_edit.v1",
"source": "convex",
"documentId": document_id,
"workspaceId": workspace_id,
"revision": {
"before": revision_before,
"after": apply_result.pointer("/revision").cloned().unwrap_or(Value::Null)
},
"operationsApplied": applied,
"operationsFailed": failed.len(),
"failedOperations": failed,
"changedText": changed_text,
"applyResult": apply_result
}))
}
/// 四级搜索替换:精确 → 忽略空白 → 段落 fuzzy → 失败
fn search_replace(text: &str, search: &str, replace: &str) -> Result<String, String> {
// Level 1: 精确匹配
if text.contains(search) {
return Ok(text.replacen(search, replace, 1));
}
// Level 2: 忽略首尾空白和全角/半角差异
let normalized = |s: &str| -> String {
s.trim()
.chars()
.map(|ch| match ch {
''..='' => ((ch as u32).saturating_sub('' as u32) + 'A' as u32).try_into().unwrap_or(ch),
''..='' => ((ch as u32).saturating_sub('' as u32) + 'a' as u32).try_into().unwrap_or(ch),
''..='' => ((ch as u32).saturating_sub('' as u32) + '0' as u32).try_into().unwrap_or(ch),
'\u{3000}' => ' ',
_ => ch,
})
.collect()
};
let norm_search = normalized(search);
for line in text.lines() {
let norm_line = normalized(line);
if norm_line.contains(&norm_search) {
let start = norm_line.find(&norm_search).unwrap();
let end = start + norm_search.len();
let replaced = format!(
"{}{}{}",
&line[..line.char_indices().nth(start).map(|(i, _)| i).unwrap_or(0)],
replace,
&line[line.char_indices().nth(end).map(|(i, _)| i).unwrap_or(line.len())..]
);
return Ok(text.replacen(line, &replaced, 1));
}
}
// Level 3: 按段落 fuzzy30% 字符差异容限)
for para in text.split("\n\n") {
if fuzzy_match(para, search, 0.3) {
let idx = text.find(para).unwrap();
let replaced = format!("{}{}{}", &text[..idx], replace, &text[idx + para.len()..]);
return Ok(replaced);
}
}
// Level 4: 失败
Err(format!("无法匹配 \"{}\"", if search.len() > 60 { format!("{}...", &search[..60]) } else { search.to_string() }))
}
fn fuzzy_match(text: &str, pattern: &str, max_diff_ratio: f64) -> bool {
let text_chars: Vec<char> = text.chars().collect();
let pat_chars: Vec<char> = pattern.chars().collect();
let max_dist = (pat_chars.len() as f64 * max_diff_ratio).ceil() as usize;
// 简单的滑动窗口匹配
for window in text_chars.windows(pat_chars.len().min(text_chars.len())) {
let dist = window
.iter()
.zip(pat_chars.iter())
.filter(|(a, b)| a != b)
.count();
if dist <= max_dist {
return true;
}
}
false
}
/// 将 markdown_edit 的 operations 转换为 block ops 供 doc_apply_block_ops 执行
fn build_block_ops_from_markdown_edit(
blocks: &[Value],
operations: &[Value],
applied_count: usize,
) -> Vec<Value> {
let mut block_ops = Vec::new();
for op in operations.iter().take(applied_count) {
let search = op.get("search").and_then(Value::as_str).unwrap_or("");
let replace = op.get("replace").and_then(Value::as_str).unwrap_or("");
let _content_val = op.get("content").cloned().unwrap_or_else(|| json!(replace));
// 在 blocks 中查找匹配文本
let matched_block = blocks.iter().find(|block| {
block
.get("text")
.and_then(Value::as_str)
.map(|t| t.contains(search))
.unwrap_or(false)
});
if let Some(block) = matched_block {
let block_id = block_id_of(block).unwrap_or_default();
let block_text = block
.get("text")
.and_then(Value::as_str)
.unwrap_or("");
let new_text = block_text.replacen(search, replace, 1);
block_ops.push(json!({
"op": "replace",
"blockId": block_id,
"content": new_text
}));
}
}
block_ops
}
@@ -22,6 +22,7 @@ pub fn manifest() -> Value {
block_delete_tool(),
block_move_after_tool(),
doc_apply_block_ops_tool(),
doc_markdown_edit_tool(),
page_get_tool(),
page_save_tool(),
available_tool("mnote.page.update_title", "更新当前页面标题", ["page.write"]),
@@ -409,6 +410,43 @@ fn available_tool(
})
}
fn doc_markdown_edit_tool() -> Value {
let mut properties = base_identity_properties();
if let Value::Object(map) = &mut properties {
map.insert(
"operations".into(),
json!({
"type": "array",
"description": "搜索替换操作列表",
"items": {
"type": "object",
"properties": {
"search": { "type": "string", "description": "要搜索的原文片段" },
"replace": { "type": "string", "description": "替换后的新文本" }
},
"required": ["search", "replace"]
}
}),
);
map.insert(
"full_content".into(),
json!({
"type": "string",
"description": "完整修改后的 markdown 文本(替代 operations"
}),
);
}
json!({
"name": "mnote.doc.markdown_edit",
"description": "通过文本级搜索替换编辑 markdown 内容(AI 编辑主路径)。在线 Convex 文档和本地 .md 文件共用,不需要 blockId。",
"schemaVersion": TOOL_SCHEMA_VERSION,
"capabilityScope": ["block.write", "page.write"],
"status": "available",
"properties": properties,
"annotations": tool_annotations(false, false, true, false)
})
}
fn tool_annotations(
readonly: bool,
destructive: bool,
@@ -233,7 +233,7 @@ async fn page_command(
validate_only: false,
};
let execution = execute_runtime_command_via_convex_with_artifacts(
state.config(),
&state,
context,
workspace_id.as_deref(),
command,