Skip to main content

oxicode_agent/agent_loop/
stream_outcome.rs

1//! Stream outcome types for TTSR integration.
2//!
3//! Extends the return type of `stream_assistant_response`
4//! to signal TTSR rule violations without repurposing the existing cancel /
5//! error mechanisms.
6
7use super::ttsr::Rule;
8use oxicode_ai::AssistantMessage;
9
10/// Result of a streaming completion attempt.
11pub enum StreamOutcome {
12    /// Normal completion — the assistant message is complete.
13    Complete(AssistantMessage),
14
15    /// User-initiated cancellation (Ctrl+C).
16    Cancelled(AssistantMessage),
17
18    /// TTSR rule violation detected during streaming.
19    /// The caller should inject the rule as a system reminder and retry.
20    RuleInterrupt {
21        /// The partial assistant message at the point of interruption.
22        partial: AssistantMessage,
23        /// The rule that was violated.
24        rule: Rule,
25    },
26
27    /// Provider error (stream ended with an error event).
28    Error {
29        /// The partial assistant message at the point of error.
30        message: AssistantMessage,
31        /// Human-readable error detail from the provider.
32        detail: String,
33    },
34}
35
36impl StreamOutcome {
37    /// Extract the assistant message regardless of outcome.
38    pub fn into_message(self) -> AssistantMessage {
39        match self {
40            StreamOutcome::Complete(m)
41            | StreamOutcome::Cancelled(m)
42            | StreamOutcome::Error { message: m, .. } => m,
43            StreamOutcome::RuleInterrupt { partial, .. } => partial,
44        }
45    }
46}