Files
mnote/rust/crates/mnote-web/src/hermes_tools/artifact.rs
T
lix-2026 9d8e361e43 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
2026-05-17 12:58:27 +08:00

186 lines
5.9 KiB
Rust

use crate::app::AppState;
use crate::context::RequestContext;
use crate::error::WebError;
use crate::hermes_tools::ToolCallInput;
use crate::routes::command_support::execute_runtime_command_via_convex_with_artifacts;
use bridge_runtime::{
RuntimeActorWire, RuntimeCommandEnvelopeWire, RuntimeSourceWire, RuntimeTargetWire,
};
use serde_json::{json, Value};
pub async fn create_summary(
state: &AppState,
context: &RequestContext,
input: &ToolCallInput,
) -> Result<Value, WebError> {
create_artifact_node(
state,
context,
input,
"summary",
"mnote.artifact.create_summary",
)
.await
}
pub async fn create_ai_note(
state: &AppState,
context: &RequestContext,
input: &ToolCallInput,
) -> Result<Value, WebError> {
create_artifact_node(
state,
context,
input,
"ai_note",
"mnote.artifact.create_ai_note",
)
.await
}
async fn create_artifact_node(
state: &AppState,
context: &RequestContext,
input: &ToolCallInput,
node_type: &str,
tool_name: &str,
) -> Result<Value, WebError> {
ensure_write_contract(context, input)?;
let document_id = input.effective_document_id().ok_or_else(|| {
WebError::bad_request_code("mnote_tool_bad_request", "artifact 工具缺少 documentId")
.with_context(context)
})?;
let workspace_id = input.effective_workspace_id();
let content = input
.arg_string("summary")
.or_else(|| input.arg_string("content"))
.ok_or_else(|| {
WebError::bad_request_code("mnote_tool_bad_request", "artifact 工具缺少内容")
.with_context(context)
})?;
let idempotency_key =
input.idempotency_key_or_default(&format!("{tool_name}_{}", context.trace.request_id));
let command_id = format!(
"{}_{}",
tool_name.replace('.', "_"),
context.trace.request_id
);
let artifact_document_id = if node_type == "summary" {
format!("summary_{}", document_id)
} else {
format!("ai_note_{}_{}", document_id, context.trace.request_id)
};
if input.dry_run.unwrap_or(false) {
return Ok(json!({
"dryRun": true,
"commandName": "tree.node.create",
"commandId": command_id,
"artifactType": node_type,
"artifactDocumentId": artifact_document_id,
"documentId": document_id,
"workspaceId": workspace_id,
"diff": [{"op": "create_artifact", "artifactType": node_type}]
}));
}
let command = RuntimeCommandEnvelopeWire {
name: "tree.node.create".into(),
command_id: command_id.clone(),
idempotency_key: Some(idempotency_key),
actor: RuntimeActorWire {
actor_type: context.auth.actor_type.clone(),
actor_id: context.auth.actor_id.clone(),
session_id: input
.session_id
.clone()
.or_else(|| context.auth.session_id.clone()),
},
source: RuntimeSourceWire {
channel: "hermes".into(),
client: "mnote-hermes-plugin".into(),
source_kind: None,
root_uri: None,
workspace_id: workspace_id.clone(),
capabilities: input.capability_scope.clone().unwrap_or_default(),
},
target: Some(RuntimeTargetWire {
workspace_id: workspace_id.clone(),
page_id: Some(document_id.clone()),
block_id: None,
}),
payload: json!({
"workspaceId": workspace_id,
"parentId": document_id,
"documentId": artifact_document_id,
"accessScope": "private",
"nodeType": node_type,
"title": if node_type == "summary" { "AI Summary" } else { "AI Note" },
"content": [
{
"id": format!("{}_body", node_type),
"type": "paragraph",
"content": [{"type": "text", "text": content}]
}
],
"artifact": {
"kind": node_type,
"sourceDocumentId": document_id,
"source": "hermes",
"sessionId": input.session_id,
"runId": input.run_id,
"toolCallId": input.tool_call_id,
"traceId": input.effective_trace_id(&context.trace.trace_id)
},
"referenceEdge": {
"from": document_id,
"kind": "ai_artifact_reference"
}
}),
preflight_data: None,
reason: Some(tool_name.into()),
refs: vec![tool_name.into(), "hermes-tool-call".into()],
dry_run: false,
validate_only: false,
};
let execution = execute_runtime_command_via_convex_with_artifacts(
&state,
context,
workspace_id.as_deref(),
command,
)
.await?;
Ok(json!({
"commandName": "tree.node.create",
"commandId": command_id,
"artifactType": node_type,
"artifactDocumentId": artifact_document_id,
"referenceEdge": {
"from": document_id,
"to": artifact_document_id,
"kind": "ai_artifact_reference"
},
"result": execution.result,
"artifacts": execution.artifacts,
"artifactError": execution.artifact_error
}))
}
fn ensure_write_contract(context: &RequestContext, input: &ToolCallInput) -> Result<(), WebError> {
if !input.has_idempotency_key() {
return Err(WebError::bad_request_code(
"mnote_tool_idempotency_required",
"写入型 mnote Hermes tool 必须携带 idempotencyKey",
)
.with_context(context));
}
if input.dry_run.is_none() {
return Err(WebError::bad_request_code(
"mnote_tool_dry_run_required",
"写入型 mnote Hermes tool 必须显式携带 dryRun",
)
.with_context(context));
}
Ok(())
}