Skip to main content

rig_core/serve/
recorder.rs

1//! Runtime-independent recording of handler dispatches.
2//!
3//! ```
4//! use rig_core::serve::DispatchContext;
5//!
6//! let origin = DispatchContext::default();
7//! assert!(origin.parent.is_none());
8//! ```
9
10use std::sync::Arc;
11
12use crate::{
13    effect::{Answered, EffectId, EffectKind, HandlerDescriptor, HandlerKey},
14    error::ErrorReport,
15    streaming::{Item, StreamEvent},
16    wasm_compat::{WasmCompatSend, WasmCompatSync},
17};
18
19/// Recorded parent dispatch, stable program scope identifier and purpose,
20/// without live runtime handles.
21#[derive(Clone, Debug, Default, PartialEq, Eq)]
22pub struct DispatchContext {
23    /// The dispatch this one was made from, if a handler made it.
24    pub parent: Option<EffectId>,
25    /// The scope of the program that made it, if its dispatcher was scoped.
26    pub scope: Option<std::sync::Arc<str>>,
27    /// What it was made for, such as `"turn"` or `"summary"`, if its
28    /// dispatcher said.
29    pub purpose: Option<std::borrow::Cow<'static, str>>,
30}
31
32/// What a driver tells about the dispatches it serves. A driver calls
33/// [`handlers`](Self::handlers) once when recording starts,
34/// [`begin`](Self::begin) as each dispatch is handed to its handler,
35/// [`event`](Self::event) for every streamed event when
36/// [`keep_events`](Self::keep_events) says so and for the first otherwise,
37/// and
38/// [`resolve`](Self::resolve) when the outcome is known. A recorder is
39/// shared between the driver and its owner, so every method takes `&self`;
40/// it rides in the dispatch observer and uses the platform compatibility bounds.
41pub trait Recorder: WasmCompatSend + WasmCompatSync + 'static {
42    /// Handlers the driver serves: those registered when recording started,
43    /// then each one installed later, as it is installed. A key described
44    /// again is the same handler re-registered; the latest description
45    /// stands.
46    fn handlers(&self, handlers: Vec<HandlerDescriptor>);
47    /// A dispatch begins: its id, the key it was routed to, the effect, and
48    /// where it came from (its parent and scope).
49    fn begin(&self, id: EffectId, key: HandlerKey, kind: EffectKind, origin: DispatchContext);
50    /// Removes a begun dispatch decided by a layer before handler execution.
51    /// Replay reruns layer decisions rather than recording them as handler outcomes.
52    fn discard(&self, id: EffectId);
53    /// Replaces the recorded request with a same-family layer patch, so it
54    /// reflects the request served by the innermost handler.
55    fn patch(&self, id: EffectId, kind: EffectKind);
56    /// Whether streamed items are wanted verbatim ([`Self::event`]).
57    fn keep_events(&self) -> bool;
58    /// One streamed item of `id`: each one when
59    /// [`keep_events`](Self::keep_events) says so, else only the first, which
60    /// a timed recorder stamps.
61    fn event(&self, id: EffectId, item: &Item<StreamEvent>);
62    /// An error item at its original position in a kept stream. Unlike the
63    /// folded outcome, this includes errors after an earlier terminal item.
64    fn stream_error(&self, _id: EffectId, _error: &ErrorReport) {}
65    /// Who the streamed reply of `id` is from, before its first item, when
66    /// [`keep_events`](Self::keep_events) says so.
67    fn origin(&self, id: EffectId, origin: &crate::message::Origin);
68    /// Explicitly published output, a tool's or a layer's facts, delivered
69    /// before `resolve`. A driver snapshots it without consuming the
70    /// caller's published context.
71    fn tool_output(&self, id: EffectId, output: crate::tool::ToolResultContext);
72    /// The outcome of `id`.
73    fn resolve(&self, id: EffectId, outcome: Result<Answered, ErrorReport>);
74}
75
76/// `work`, the task that drives the dispatch `id`, with a panic in it (in
77/// the handler or in its reply's stream) turned into an internal error that
78/// `recorder`, if any, also records as the dispatch's outcome. Unwinding
79/// drops the dispatch's observer, which records a cancellation; this
80/// replaces it.
81pub fn catch_panics<T>(
82    recorder: Option<Arc<dyn Recorder + Send + Sync>>,
83    id: EffectId,
84    work: impl Future<Output = Result<T, ErrorReport>>,
85) -> impl Future<Output = Result<T, ErrorReport>> {
86    use futures::FutureExt;
87    async move {
88        std::panic::AssertUnwindSafe(work)
89            .catch_unwind()
90            .await
91            .unwrap_or_else(|panic| {
92                let message = panic
93                    .downcast_ref::<&str>()
94                    .copied()
95                    .or_else(|| panic.downcast_ref::<String>().map(String::as_str))
96                    .unwrap_or("no message");
97                let report = ErrorReport::new(
98                    crate::error::ErrorKind::Internal,
99                    format!("panicked: {message}"),
100                );
101                if let Some(recorder) = recorder {
102                    recorder.resolve(id, Err(report.clone()));
103                }
104                Err(report)
105            })
106    }
107}