Skip to main content

strands/types/
interrupt.rs

1//! Interrupt request and response types.
2
3use serde::{Deserialize, Serialize};
4
5use super::json::JsonValue;
6
7/// Parameters for raising an interrupt (`InterruptParams`).
8///
9/// Build with [`InterruptParams::new`] (or `"name".into()`) and the `with_*` setters.
10#[derive(Debug, Clone, PartialEq)]
11#[non_exhaustive]
12pub struct InterruptParams {
13    /// Name of the interrupt, unique within one hook callback or tool execution.
14    pub name: String,
15    /// Why the interrupt was raised.
16    pub reason: Option<JsonValue>,
17    /// A preemptive response. When set, the interrupt returns it at once instead of halting, for
18    /// example to reuse an approval saved in an earlier session.
19    pub response: Option<JsonValue>,
20}
21
22impl InterruptParams {
23    /// Creates parameters with a name.
24    pub fn new(name: impl Into<String>) -> Self {
25        Self {
26            name: name.into(),
27            reason: None,
28            response: None,
29        }
30    }
31
32    /// Sets the reason.
33    #[must_use]
34    pub fn with_reason(mut self, reason: impl Into<JsonValue>) -> Self {
35        self.reason = Some(reason.into());
36        self
37    }
38
39    /// Sets a preemptive response.
40    #[must_use]
41    pub fn with_response(mut self, response: impl Into<JsonValue>) -> Self {
42        self.response = Some(response.into());
43        self
44    }
45}
46
47impl From<&str> for InterruptParams {
48    fn from(name: &str) -> Self {
49        Self::new(name)
50    }
51}
52
53impl From<String> for InterruptParams {
54    fn from(name: String) -> Self {
55        Self::new(name)
56    }
57}
58
59/// A user's response to an interrupt (`InterruptResponse`).
60#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
61#[serde(rename_all = "camelCase")]
62#[non_exhaustive]
63pub struct InterruptResponse {
64    /// The id of the interrupt being answered.
65    pub interrupt_id: String,
66    /// The user's response.
67    pub response: JsonValue,
68}
69
70impl InterruptResponse {
71    /// Creates a response to an interrupt.
72    pub fn new(interrupt_id: impl Into<String>, response: impl Into<JsonValue>) -> Self {
73        Self {
74            interrupt_id: interrupt_id.into(),
75            response: response.into(),
76        }
77    }
78}
79
80/// Content that resumes an interrupted agent with a response (`InterruptResponseContent`).
81///
82/// Wire shape: `{"interruptResponse": {"interruptId": "...", "response": ...}}`.
83#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
84#[serde(rename_all = "camelCase")]
85#[non_exhaustive]
86pub struct InterruptResponseContent {
87    /// The interrupt response.
88    pub interrupt_response: InterruptResponse,
89}
90
91impl InterruptResponseContent {
92    /// Wraps an interrupt response.
93    pub fn new(interrupt_response: InterruptResponse) -> Self {
94        Self { interrupt_response }
95    }
96}
97
98impl From<InterruptResponse> for InterruptResponseContent {
99    fn from(interrupt_response: InterruptResponse) -> Self {
100        Self::new(interrupt_response)
101    }
102}
103
104#[cfg(test)]
105mod tests {
106    use serde_json::json;
107
108    use super::*;
109
110    #[test]
111    fn interrupt_response_content_uses_the_wrapped_wire_shape() {
112        let content = InterruptResponseContent::new(InterruptResponse::new("int-1", "approved"));
113        assert_eq!(
114            serde_json::to_value(&content).unwrap(),
115            json!({"interruptResponse": {"interruptId": "int-1", "response": "approved"}})
116        );
117    }
118
119    #[test]
120    fn interrupt_params_convert_from_a_name() {
121        let params = InterruptParams::from("confirm").with_reason(json!("Please confirm"));
122        assert_eq!(params.name, "confirm");
123        assert_eq!(params.reason, Some(json!("Please confirm")));
124        assert_eq!(
125            InterruptParams::from(String::from("confirm")),
126            InterruptParams::new("confirm")
127        );
128    }
129}