aether_core/events/observer.rs
1use crate::events::{AgentEvent, TraceContext};
2use std::sync::Arc;
3
4/// Observer of the agent's event stream. Observers receive the same events
5/// the agent sends on its event channel, synchronously and in order —
6/// including events that channel consumers such as UIs and session
7/// persistence filter out downstream.
8pub trait AgentObserver: Send {
9 fn on_event(&mut self, message: &AgentEvent);
10
11 /// Trace context to propagate to whatever executes `tool_id`, available once
12 /// the observer has seen that tool's
13 /// [`ExecutionStarted`](crate::events::ToolEvent::ExecutionStarted).
14 fn tool_trace_context(&self, _tool_id: &str) -> Option<TraceContext> {
15 None
16 }
17}
18
19/// Instrumentation for one inbound MCP request, held by the server handling it.
20pub trait McpRequestInstrumentation: Send {
21 /// Context beneath the request, for application work the request spawns.
22 fn trace_context(&self) -> Option<TraceContext>;
23
24 /// Completes the request. `error` is the public operation error, if any.
25 fn finish(self: Box<Self>, error: Option<&str>);
26}
27
28/// Creates observers isolated to one agent or one inbound MCP request.
29pub trait ObserverFactory: Send + Sync {
30 /// A fresh observer for one agent, continuing `parent`'s trace when supplied.
31 fn agent(&self, parent: Option<&TraceContext>) -> Box<dyn AgentObserver>;
32
33 /// Instrumentation for one inbound `tools/call` request.
34 fn tool_call_request(&self, tool_name: &str, parent: Option<&TraceContext>) -> Box<dyn McpRequestInstrumentation>;
35}
36
37pub type DynObserverFactory = Arc<dyn ObserverFactory>;