Skip to main content

rinne_types/worker/
event.rs

1//! Streaming worker events (`CONTEXT.md` §8 lifecycle, §6 stream pane).
2//!
3//! A worker emits events as it runs; the dispatcher forwards them to the
4//! interface's stream pane. The channel is the worker's only way to narrate
5//! progress before its final [`ExecuteResult`](super::ExecuteResult).
6
7use serde::{Deserialize, Serialize};
8
9/// A single streamed event from a running worker.
10#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
11#[serde(rename_all = "snake_case", tag = "kind", content = "data")]
12pub enum WorkerEvent {
13    /// A line of human-readable narration ("reading src/, editing app.ts").
14    Message(String),
15    /// A raw streaming text delta from a token-streaming worker (API/HTTP, Grok).
16    /// Consumers concatenate these verbatim — fragments are often mid-word, so no
17    /// separator is inserted between them. This is what lets the UI reconstruct
18    /// and markdown-render any streamed model output regardless of the worker.
19    Token(String),
20    /// A reasoning ("thinking") delta from a reasoning model (e.g. DeepSeek-R1,
21    /// o-series). Streamed separately from [`Token`] content and never part of
22    /// the final answer, so the UI can show it dimmed without polluting the
23    /// result passed to the next node.
24    Thinking(String),
25    /// The worker began reading a file/path.
26    Reading(String),
27    /// The worker edited a file/path.
28    Editing(String),
29    /// The worker invoked a tool/command.
30    ToolUse(String),
31    /// Raw stdout/stderr passthrough, used when no richer structure exists.
32    Raw(String),
33    /// A visible Harness Stage session opened (PTY / Stage UI). `backend` is
34    /// `pty` or `headless` (or future `external-terminal`).
35    SessionOpened {
36        worker: String,
37        model: Option<String>,
38        backend: String,
39    },
40    /// Terminal marker: the worker finished emitting events.
41    Done,
42}
43
44/// The sink a worker writes events to. The dispatcher owns the receiver.
45///
46/// Unbounded so a worker never blocks on a slow consumer; events are cheap and
47/// the UI drains them promptly.
48pub type EventSink = tokio::sync::mpsc::UnboundedSender<WorkerEvent>;
49
50/// Convenience: send an event, ignoring the error if the receiver is gone
51/// (e.g. the run was cancelled and the dispatcher dropped its end).
52pub fn emit(sink: &EventSink, event: WorkerEvent) {
53    let _ = sink.send(event);
54}