Skip to main content

conversation_api/execution/
event.rs

1//! Durable Agent events and their delivery boundary.
2
3use async_trait::async_trait;
4use serde::{Deserialize, Serialize};
5use serde_json::Value;
6
7use crate::execution::{
8    ActionId, EventId, ExternalError, InvocationContext, MessageId, RuntimeSnapshot, UsageSummary,
9};
10
11/// Private execution projection. Only `item` is exposed to clients.
12#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
13#[serde(deny_unknown_fields)]
14pub struct InteractionRequest {
15    pub item: crate::PendingInteractionItem,
16    /// Retire a real prepared record on conversation termination, never a business session.
17    pub prepared_action_id: Option<ActionId>,
18}
19
20/// Event emitted by the Runtime.
21#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
22#[serde(tag = "type", rename_all = "snake_case")]
23#[serde(deny_unknown_fields)]
24pub enum AgentEvent {
25    /// Inputs committed to the prompt history of the original run.
26    Steered {
27        inputs: Vec<crate::execution::SteeringInput>,
28    },
29    /// A run has started or resumed.
30    #[serde(deserialize_with = "crate::execution::deserialize_empty_variant")]
31    Started,
32    /// The run requires user input before it can continue.
33    InteractionRequired {
34        /// Batch identifier answered by `SubmitInteraction`.
35        batch_id: MessageId,
36        /// Prepared actions awaiting one decision each.
37        items: Vec<InteractionRequest>,
38        /// Usage accumulated before Runtime suspended the run.
39        usage: UsageSummary,
40    },
41    /// The run completed successfully.
42    Completed {
43        /// Final structured result.
44        result: Value,
45        /// Aggregated usage for the completed run.
46        usage: UsageSummary,
47    },
48    /// The run failed definitively.
49    Failed {
50        /// Catalog member from [`crate::RunErrorCode`].
51        code: String,
52        /// Safe user-facing or diagnostic message.
53        message: String,
54        /// Usage settled before the failure, cancellation, or interruption.
55        usage: UsageSummary,
56    },
57}
58
59/// Best-effort, request-scoped observation that is not part of the durable outbox.
60#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
61#[serde(tag = "type", rename_all = "snake_case")]
62#[serde(deny_unknown_fields)]
63pub enum AgentObservation {
64    /// Coarse progress for live presentation.
65    Progress {
66        /// Stable progress label.
67        status: String,
68    },
69    /// Temporary assistant text emitted before a tool batch.
70    CompanionText {
71        /// Text shown while work continues.
72        content: String,
73    },
74    /// A tool attempt is starting.
75    ToolStarted {
76        /// Model tool-call identifier.
77        call_id: String,
78        /// Stable tool name.
79        tool_name: String,
80    },
81    /// A tool attempt produced a result.
82    ToolResult {
83        /// Model tool-call identifier.
84        call_id: String,
85        /// Stable tool name.
86        tool_name: String,
87        /// Structured result.
88        result: Value,
89        /// Whether execution failed.
90        is_error: bool,
91    },
92    /// A validated model tool-call batch has been checkpointed for execution.
93    ToolCallsScheduled {
94        /// Tool names in model order.
95        tool_names: Vec<String>,
96    },
97    /// A tool batch completed and its results are available to the next model step.
98    ToolCallsCompleted {
99        /// Number of completed tool calls.
100        count: u32,
101    },
102    /// Optional diagnostic payload requested by the caller.
103    DebugData {
104        /// Diagnostic namespace.
105        scope: String,
106        /// Structured diagnostic value.
107        payload: Value,
108    },
109}
110
111/// Sequenced event persisted as part of a thread transition.
112#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
113pub struct DurableEvent {
114    /// Globally unique event identifier used for deduplication.
115    pub id: EventId,
116    /// Monotonic sequence within a thread.
117    pub sequence: u64,
118    /// Frozen routing, ownership, and run identity for this event.
119    pub context: InvocationContext,
120    /// Event payload.
121    pub event: AgentEvent,
122}
123
124/// Delivery boundary for events that have already been durably committed.
125#[async_trait]
126pub trait EventPublisher: Send + Sync {
127    /// Publishes one committed stable-boundary event with at-least-once delivery semantics.
128    ///
129    /// `snapshot` is the exact Runtime generation committed with `event`. Implementations must
130    /// never reload a newer snapshot while encoding the event.
131    async fn publish(
132        &self,
133        snapshot: &RuntimeSnapshot,
134        event: &DurableEvent,
135    ) -> Result<(), ExternalError>;
136}
137
138/// Best-effort observer for non-durable UI progress.
139#[async_trait]
140pub trait Observer: Send + Sync {
141    /// Delivers one request-scoped observation.
142    async fn observe(&self, context: &InvocationContext, observation: &AgentObservation);
143}