Skip to main content

ed_agent/
sink.rs

1//! Where Ed reports what just happened.
2
3use onde::inference::EngineStatus;
4
5use crate::maybe::{MaybeSend, MaybeSync};
6use crate::{AgentReply, ApprovalRequest, ChatReply, ToolCall, ToolExecutionResult};
7
8/// Receives lifecycle transitions and completed replies.
9///
10/// `onde` reports status by polling
11/// [`info()`](onde::inference::ChatEngine::info), which is the wrong shape for
12/// a UI: nothing tells the view that a load finished, so hosts end up either
13/// timing a poll loop or threading their own event emission through every call
14/// site. Ed pushes instead, and a host implements this once to bridge into
15/// whatever its UI listens on.
16///
17/// Both methods are synchronous and should stay cheap — they are called while
18/// Ed is mid-operation. Hand off to a channel or an event bus rather than
19/// blocking. Implementations must be `Send + Sync` (on `wasm32-unknown-unknown`, where the agent is single-threaded, that relaxes to nothing; see [`maybe`](crate::maybe)) because a load or a send
20/// may be driven from any task.
21pub trait EventSink: MaybeSend + MaybeSync + 'static {
22    /// The engine moved to a new lifecycle state.
23    ///
24    /// `model_name` is the model involved where one is known, and `error`
25    /// carries the reason when `status` is [`EngineStatus::Error`].
26    fn status_changed(
27        &self,
28        _status: EngineStatus,
29        _model_name: Option<&str>,
30        _error: Option<&str>,
31    ) {
32    }
33
34    /// An inference turn finished, successfully or not.
35    ///
36    /// Delivered as a notification rather than only as a return value because
37    /// inference is slow: a host that awaits the call from a UI callback can
38    /// have that callback collected before it resolves. Emitting lets the view
39    /// pick the result up independently.
40    fn replied(&self, _reply: &ChatReply) {}
41
42    /// A piece of the assistant's reply, as it is generated.
43    ///
44    /// Called zero or more times during [`Ed::run`](crate::Ed::run) before
45    /// the final reply is reported. Concatenating the deltas gives the reply
46    /// text. The default does nothing, so sinks written before streaming
47    /// existed keep compiling and keep seeing only the finished reply.
48    fn text_delta(&self, _delta: &str) {}
49
50    fn tool_requested(&self, _call: &ToolCall) {}
51    fn approval_requested(&self, _request: &ApprovalRequest) {}
52    fn tool_started(&self, _call: &ToolCall) {}
53    fn tool_finished(&self, _result: &ToolExecutionResult) {}
54    fn agent_replied(&self, _reply: &AgentReply) {}
55    fn warning(&self, _message: &str) {}
56}
57
58/// Compatibility name retained for the original Ed API.
59pub use EventSink as StatusSink;
60
61/// A [`StatusSink`] that drops everything.
62///
63/// The default for [`Ed::new`](crate::Ed::new), and what you want in tests or
64/// a host that only ever polls [`Ed::info`](crate::Ed::info).
65#[derive(Debug, Clone, Copy, Default)]
66pub struct NoopSink;
67
68impl EventSink for NoopSink {}
69
70#[cfg(test)]
71mod tests {
72    use super::*;
73
74    struct Bare;
75    impl EventSink for Bare {}
76
77    #[test]
78    fn text_delta_defaults_to_a_no_op() {
79        // A sink that predates streaming must still compile and not panic.
80        Bare.text_delta("hello");
81        NoopSink.text_delta("hello");
82    }
83}