Skip to main content

causa_runtime/
event.rs

1//! Optional synchronous observations; consumers own retained copies.
2
3use crate::executor::ToolProcessingError;
4use causa_kernel::{ContextBlock, InvocationId, ModelOutput, ModelRequest, StreamDelta, ToolBatch};
5
6/// Borrowed execution observations. Callbacks must return promptly.
7///
8/// No event history is retained by the runner. `ModelDelta` excludes `Done`
9/// and `Error`; a committed notification describes only the actual append.
10#[derive(Debug)]
11#[non_exhaustive]
12pub enum RunEvent<'a> {
13    /// A complete logical request, immediately before gateway entry.
14    ModelRequestReady {
15        /// The request to be sent.
16        request: &'a ModelRequest,
17    },
18    /// A nonterminal streaming observation.
19    ModelDelta {
20        /// The logical invocation.
21        invocation_id: &'a InvocationId,
22        /// Text, reasoning, tool-call or usage observation.
23        delta: &'a StreamDelta,
24    },
25    /// A complete model output, before material conversion.
26    ModelOutput {
27        /// The logical invocation.
28        invocation_id: &'a InvocationId,
29        /// The accepted complete output.
30        output: &'a ModelOutput,
31    },
32    /// An atomic append succeeded, including an empty append.
33    BlocksCommitted {
34        /// The logical invocation.
35        invocation_id: &'a InvocationId,
36        /// Actual appended blocks borrowed from the context.
37        blocks: &'a [ContextBlock],
38    },
39    /// Newly committed declarations are about to enter tool processing.
40    ToolBatchReady {
41        /// The logical invocation.
42        invocation_id: &'a InvocationId,
43        /// The complete uncommitted batch.
44        batch: &'a ToolBatch,
45    },
46    /// Tool processing actually returned; results are not yet committed.
47    ToolBatchReturned {
48        /// The logical invocation.
49        invocation_id: &'a InvocationId,
50        /// The complete returned batch.
51        batch: &'a ToolBatch,
52        /// The module's return status, independent of subsequent commit.
53        result: Result<(), &'a ToolProcessingError>,
54    },
55}
56/// A synchronous callback borrowing each observation for the duration of the call.
57pub type RunObserver = dyn for<'a> Fn(&RunEvent<'a>) + Send + Sync;