aidaemon_core/event.rs
1use serde::{Deserialize, Serialize};
2
3use crate::hook::{HookFailure, HookPoint};
4use crate::limits::LimitKind;
5use crate::outcome::RunStatus;
6use crate::provider::{ModelRequest, Usage};
7use crate::receipt::Receipt;
8use crate::state::{Interruption, RunId};
9use crate::stuck::StuckSignal;
10
11/// Everything observable about a run, in order. One stream feeds UIs,
12/// tracing exporters, stores and eval assertions.
13#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
14#[serde(rename_all = "snake_case", tag = "type")]
15#[non_exhaustive]
16pub enum Event {
17 RunStarted {
18 session_id: String,
19 },
20 RunResumed,
21 /// This run was started by a tool call of `parent_run`.
22 RunLinked {
23 parent_run: RunId,
24 },
25 TurnStarted {
26 turn: u32,
27 },
28 /// Streamed model text, before the turn's `ModelResponded`.
29 TextDelta {
30 turn: u32,
31 text: String,
32 },
33 /// The run continued after a crash or restart.
34 Recovered {
35 reran: Vec<String>,
36 indeterminate: Vec<String>,
37 },
38 /// A [`crate::Failover`] chain moved to its next provider during this
39 /// turn's model call.
40 ProviderSwitched {
41 turn: u32,
42 switch: crate::ProviderSwitch,
43 },
44 /// The model is about to be called; pairs with `ModelResponded`, or
45 /// with `ModelRetried` or `Finished` if the call fails. Content is not
46 /// journaled: see [`EventSink::observe_request`].
47 ModelRequested {
48 turn: u32,
49 model: String,
50 messages: usize,
51 tools: usize,
52 },
53 ModelResponded {
54 turn: u32,
55 usage: Usage,
56 text: String,
57 tool_calls: usize,
58 /// Why the reply may be incomplete, in the provider's words.
59 #[serde(default, skip_serializing_if = "Option::is_none")]
60 note: Option<String>,
61 },
62 ToolStarted {
63 call_id: String,
64 tool: String,
65 },
66 ToolFinished {
67 receipt: Receipt,
68 },
69 Stuck {
70 signal: StuckSignal,
71 },
72 StopRejected {
73 message: String,
74 },
75 /// A hook made a decision other than letting things proceed. Together
76 /// with `HookFailed` this says which hook did what, and why.
77 HookDecided {
78 point: HookPoint,
79 hook: String,
80 decision: String,
81 #[serde(default, skip_serializing_if = "Option::is_none")]
82 reason: Option<String>,
83 },
84 /// A hook panicked, timed out or was cancelled, and gave no answer.
85 /// See [`crate::Hook`] for what the run does instead.
86 HookFailed {
87 point: HookPoint,
88 hook: String,
89 failure: HookFailure,
90 },
91 /// A hook answered in the model's place, or supplied the response of a
92 /// failed call; the model's own reply was not used.
93 ModelAnswered {
94 turn: u32,
95 hook: String,
96 },
97 /// An `after_model` hook rejected this turn's response.
98 ModelRejected {
99 turn: u32,
100 message: String,
101 },
102 /// The model call failed and an `on_model_error` hook asked for a retry.
103 ModelRetried {
104 turn: u32,
105 error: String,
106 after_ms: u64,
107 },
108 /// An `on_limit` hook raised a limit; `amount` is in the limit's unit.
109 LimitExtended {
110 limit: LimitKind,
111 amount: u64,
112 },
113 /// An `edit_transcript` hook replaced the stored transcript.
114 TranscriptEdited {
115 turn: u32,
116 before: usize,
117 after: usize,
118 },
119 /// An `edit_transcript` hook's edit was malformed and was not applied.
120 TranscriptEditRefused {
121 turn: u32,
122 reason: String,
123 },
124 Interrupted {
125 interruption: Interruption,
126 },
127 Finished {
128 status: RunStatus,
129 usage: Usage,
130 turns: u32,
131 /// How long the run took, summed across resumes, on the runtime's
132 /// clock (see [`crate::RunOutcome::elapsed`]).
133 #[serde(default)]
134 elapsed_ms: u64,
135 },
136}
137
138/// Receives events synchronously as they happen. Keep it cheap; forward to a
139/// channel if the consumer is slow. Events carry no timestamps: a sink is
140/// called the moment each happens, so it stamps them with whatever clock it
141/// keeps (receipts carry each call's `duration_ms`, and
142/// [`Event::Finished`] the run's `elapsed_ms`).
143pub trait EventSink: Send + Sync {
144 fn emit(&self, run_id: RunId, event: &Event);
145
146 /// The exact request about to go to the model, after every hook has
147 /// shaped it. Prompts are not events, so they are never journaled; a
148 /// sink that exports them takes on deciding what may leave.
149 fn observe_request(&self, _run_id: RunId, _request: &ModelRequest) {}
150}
151
152impl<F> EventSink for F
153where
154 F: Fn(RunId, &Event) + Send + Sync,
155{
156 fn emit(&self, run_id: RunId, event: &Event) {
157 self(run_id, event)
158 }
159}