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
@@ -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,