rusty_claw 0.1.0

Rust implementation of the Claude Agent SDK
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
//! Hook response types for permission decisions and context injection.

use serde::Serialize;
use serde_json::Value;

/// Permission decision for tool use or other controlled actions.
///
/// Used within [`HookResponse`] to signal allow/deny/ask to the CLI.
///
/// # Examples
///
/// ```
/// use rusty_claw::prelude::*;
///
/// let decision = PermissionDecision::Allow;
/// assert_eq!(serde_json::to_string(&decision).unwrap(), r#""allow""#);
/// ```
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum PermissionDecision {
    /// Allow the action to proceed
    Allow,
    /// Deny the action
    Deny,
    /// Ask the user for permission
    Ask,
}

/// Helper function for default true value
#[allow(dead_code)]
fn default_true() -> bool {
    true
}

/// Typed hook output variants for specific response capabilities.
///
/// Each variant represents a different kind of hook response, providing
/// richer control than a plain allow/deny decision:
///
/// - [`HookOutput::Approve`]: Allow the action, with an optional message.
/// - [`HookOutput::Deny`]: Block the action with a reason.
/// - [`HookOutput::InjectSystemMessage`]: Inject a system message into the conversation.
/// - [`HookOutput::Stop`]: Stop the agent session.
/// - [`HookOutput::SuppressOutput`]: Suppress the tool output from the transcript.
/// - [`HookOutput::ModifyOutput`]: Replace tool output (for PostToolUse hooks).
///
/// # Examples
///
/// ```
/// use rusty_claw::hooks::HookOutput;
///
/// // Inject safety context
/// let out = HookOutput::InjectSystemMessage {
///     content: "Warning: destructive tool detected.".to_string(),
/// };
///
/// // Block a dangerous operation
/// let out = HookOutput::Deny {
///     reason: "Blocked for safety reasons".to_string(),
/// };
///
/// // Suppress verbose tool output
/// let out = HookOutput::SuppressOutput;
/// ```
#[derive(Debug, Clone)]
pub enum HookOutput {
    /// Allow the action to proceed.
    Approve {
        /// Optional message explaining the approval (shown in logs).
        message: Option<String>,
    },

    /// Deny the action.
    Deny {
        /// Reason for the denial (shown to the user).
        reason: String,
    },

    /// Inject a system message into the conversation context.
    ///
    /// The injected message is added as a system-level message before the
    /// next model turn. Useful for adding safety warnings or audit context.
    InjectSystemMessage {
        /// The system message content to inject.
        content: String,
    },

    /// Stop the agent session.
    Stop {
        /// Reason for stopping (used in the session transcript).
        reason: String,
    },

    /// Suppress the tool output from the conversation transcript.
    ///
    /// The tool still runs, but its output is not shown to the model.
    SuppressOutput,

    /// Replace the tool's output with a modified version.
    ///
    /// Available for `PostToolUse` hooks. The provided value replaces
    /// the tool's actual output before it is sent to the model.
    ModifyOutput {
        /// The replacement tool output value.
        updated_output: Value,
    },
}

impl HookOutput {
    /// Convert to a [`HookResponse`] for use in the control protocol.
    pub fn into_response(self) -> HookResponse {
        match self {
            HookOutput::Approve { message } => HookResponse {
                permission_decision: Some(PermissionDecision::Allow),
                permission_decision_reason: message,
                ..Default::default()
            },
            HookOutput::Deny { reason } => HookResponse {
                permission_decision: Some(PermissionDecision::Deny),
                permission_decision_reason: Some(reason),
                should_continue: false,
                ..Default::default()
            },
            HookOutput::InjectSystemMessage { content } => HookResponse {
                additional_context: Some(content),
                ..Default::default()
            },
            HookOutput::Stop { reason } => HookResponse {
                permission_decision: Some(PermissionDecision::Deny),
                permission_decision_reason: Some(reason),
                should_continue: false,
                ..Default::default()
            },
            HookOutput::SuppressOutput => HookResponse {
                suppress_output: true,
                ..Default::default()
            },
            HookOutput::ModifyOutput { updated_output } => HookResponse {
                updated_input: Some(updated_output),
                ..Default::default()
            },
        }
    }
}

/// Response from a hook callback.
///
/// For richer, typed responses consider constructing a [`HookOutput`] and
/// calling [`.into_response()`](HookOutput::into_response).
///
/// # Examples
///
/// ```
/// use rusty_claw::prelude::*;
///
/// // Allow with reason
/// let response = HookResponse::allow("Safe operation");
///
/// // Deny with reason
/// let response = HookResponse::deny("Dangerous operation detected");
///
/// // Ask user
/// let response = HookResponse::ask("Confirm destructive operation?");
///
/// // Custom response with additional context
/// let response = HookResponse::default()
///     .with_permission(PermissionDecision::Allow)
///     .with_context("Additional context for Claude");
/// ```
#[derive(Debug, Clone, Serialize)]
pub struct HookResponse {
    /// Permission decision (Allow/Deny/Ask)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub permission_decision: Option<PermissionDecision>,

    /// Reason for the permission decision (shown to user)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub permission_decision_reason: Option<String>,

    /// Additional context to inject into Claude's prompt
    #[serde(skip_serializing_if = "Option::is_none")]
    pub additional_context: Option<String>,

    /// Whether to continue processing subsequent hooks
    #[serde(rename = "continue", default = "default_true")]
    pub should_continue: bool,

    /// Modified tool input (if tool input should be transformed)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub updated_input: Option<Value>,

    /// Modified tool output (for PostToolUse hooks)
    ///
    /// When set, the tool's actual output is replaced with this value
    /// before being sent to the model.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub updated_output: Option<Value>,

    /// Stop reason (when the hook signals session stop)
    ///
    /// When set, the agent session is stopped after the hook completes.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stop_reason: Option<String>,

    /// System message to inject into the conversation
    ///
    /// When set, this string is injected as a system-level message before
    /// the next model turn.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub system_message: Option<String>,

    /// Whether to suppress tool output from the transcript
    #[serde(skip_serializing_if = "std::ops::Not::not")]
    pub suppress_output: bool,
}

impl Default for HookResponse {
    fn default() -> Self {
        Self {
            permission_decision: None,
            permission_decision_reason: None,
            additional_context: None,
            should_continue: true,
            updated_input: None,
            updated_output: None,
            stop_reason: None,
            system_message: None,
            suppress_output: false,
        }
    }
}

impl HookResponse {
    /// Create a response that allows the action
    pub fn allow(reason: impl Into<String>) -> Self {
        Self {
            permission_decision: Some(PermissionDecision::Allow),
            permission_decision_reason: Some(reason.into()),
            ..Default::default()
        }
    }

    /// Create a response that denies the action
    pub fn deny(reason: impl Into<String>) -> Self {
        Self {
            permission_decision: Some(PermissionDecision::Deny),
            permission_decision_reason: Some(reason.into()),
            should_continue: false,
            ..Default::default()
        }
    }

    /// Create a response that asks the user
    pub fn ask(prompt: impl Into<String>) -> Self {
        Self {
            permission_decision: Some(PermissionDecision::Ask),
            permission_decision_reason: Some(prompt.into()),
            ..Default::default()
        }
    }

    /// Set permission decision
    pub fn with_permission(mut self, decision: PermissionDecision) -> Self {
        self.permission_decision = Some(decision);
        self
    }

    /// Set permission reason
    pub fn with_reason(mut self, reason: impl Into<String>) -> Self {
        self.permission_decision_reason = Some(reason.into());
        self
    }

    /// Add additional context for Claude
    pub fn with_context(mut self, context: impl Into<String>) -> Self {
        self.additional_context = Some(context.into());
        self
    }

    /// Set whether to continue processing hooks
    pub fn with_continue(mut self, should_continue: bool) -> Self {
        self.should_continue = should_continue;
        self
    }

    /// Set updated tool input
    pub fn with_updated_input(mut self, input: Value) -> Self {
        self.updated_input = Some(input);
        self
    }

    /// Set updated tool output (for PostToolUse hooks)
    ///
    /// The provided value replaces the tool's actual output before it
    /// is sent to the model.
    pub fn with_updated_output(mut self, output: Value) -> Self {
        self.updated_output = Some(output);
        self
    }

    /// Set a stop reason (signals the agent to stop)
    pub fn with_stop_reason(mut self, reason: impl Into<String>) -> Self {
        self.stop_reason = Some(reason.into());
        self
    }

    /// Set a system message to inject into the conversation
    pub fn with_system_message(mut self, message: impl Into<String>) -> Self {
        self.system_message = Some(message.into());
        self
    }

    /// Suppress tool output from the transcript
    pub fn with_suppress_output(mut self, suppress: bool) -> Self {
        self.suppress_output = suppress;
        self
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn test_permission_decision_serialization() {
        assert_eq!(
            serde_json::to_string(&PermissionDecision::Allow).unwrap(),
            r#""allow""#
        );
        assert_eq!(
            serde_json::to_string(&PermissionDecision::Deny).unwrap(),
            r#""deny""#
        );
        assert_eq!(
            serde_json::to_string(&PermissionDecision::Ask).unwrap(),
            r#""ask""#
        );
    }

    #[test]
    fn test_hook_response_allow() {
        let response = HookResponse::allow("Safe operation");
        assert!(matches!(
            response.permission_decision,
            Some(PermissionDecision::Allow)
        ));
        assert_eq!(
            response.permission_decision_reason,
            Some("Safe operation".to_string())
        );
        assert!(response.should_continue);
    }

    #[test]
    fn test_hook_response_deny() {
        let response = HookResponse::deny("Dangerous operation");
        assert!(matches!(
            response.permission_decision,
            Some(PermissionDecision::Deny)
        ));
        assert_eq!(
            response.permission_decision_reason,
            Some("Dangerous operation".to_string())
        );
        assert!(!response.should_continue);
    }

    #[test]
    fn test_hook_response_ask() {
        let response = HookResponse::ask("Confirm?");
        assert!(matches!(
            response.permission_decision,
            Some(PermissionDecision::Ask)
        ));
        assert_eq!(
            response.permission_decision_reason,
            Some("Confirm?".to_string())
        );
    }

    #[test]
    fn test_hook_response_builder() {
        let response = HookResponse::default()
            .with_permission(PermissionDecision::Allow)
            .with_reason("test reason")
            .with_context("test context")
            .with_continue(false)
            .with_updated_input(json!({"key": "value"}));

        assert!(matches!(
            response.permission_decision,
            Some(PermissionDecision::Allow)
        ));
        assert_eq!(
            response.permission_decision_reason,
            Some("test reason".to_string())
        );
        assert_eq!(
            response.additional_context,
            Some("test context".to_string())
        );
        assert!(!response.should_continue);
        assert!(response.updated_input.is_some());
    }

    #[test]
    fn test_hook_response_serialization() {
        let response = HookResponse::allow("test");
        let json = serde_json::to_value(&response).unwrap();

        assert_eq!(json["permission_decision"], "allow");
        assert_eq!(json["permission_decision_reason"], "test");
        assert_eq!(json["continue"], true);
    }

    #[test]
    fn test_hook_response_default_serialization() {
        let response = HookResponse::default();
        let json = serde_json::to_value(&response).unwrap();

        assert!(
            !json
                .as_object()
                .unwrap()
                .contains_key("permission_decision")
        );
        assert!(
            !json
                .as_object()
                .unwrap()
                .contains_key("permission_decision_reason")
        );
        assert_eq!(json["continue"], true);
        assert!(!json.as_object().unwrap().contains_key("suppress_output"));
    }

    #[test]
    fn test_hook_output_approve() {
        let out = HookOutput::Approve {
            message: Some("Safe".to_string()),
        };
        let resp = out.into_response();
        assert!(matches!(
            resp.permission_decision,
            Some(PermissionDecision::Allow)
        ));
        assert_eq!(resp.permission_decision_reason, Some("Safe".to_string()));
    }

    #[test]
    fn test_hook_output_deny() {
        let out = HookOutput::Deny {
            reason: "Too dangerous".to_string(),
        };
        let resp = out.into_response();
        assert!(matches!(
            resp.permission_decision,
            Some(PermissionDecision::Deny)
        ));
        assert!(!resp.should_continue);
    }

    #[test]
    fn test_hook_output_inject_system_message() {
        let out = HookOutput::InjectSystemMessage {
            content: "Safety warning injected".to_string(),
        };
        let resp = out.into_response();
        assert_eq!(
            resp.additional_context,
            Some("Safety warning injected".to_string())
        );
    }

    #[test]
    fn test_hook_output_stop() {
        let out = HookOutput::Stop {
            reason: "User requested stop".to_string(),
        };
        let resp = out.into_response();
        assert!(matches!(
            resp.permission_decision,
            Some(PermissionDecision::Deny)
        ));
        assert!(!resp.should_continue);
    }

    #[test]
    fn test_hook_output_suppress_output() {
        let out = HookOutput::SuppressOutput;
        let resp = out.into_response();
        assert!(resp.suppress_output);
    }

    #[test]
    fn test_hook_output_modify_output() {
        let out = HookOutput::ModifyOutput {
            updated_output: json!({"sanitized": true}),
        };
        let resp = out.into_response();
        assert!(resp.updated_input.is_some());
        assert_eq!(resp.updated_input.unwrap()["sanitized"], true);
    }

    #[test]
    fn test_hook_response_updated_output() {
        let response = HookResponse::default().with_updated_output(json!({"result": "sanitized"}));
        assert!(response.updated_output.is_some());
        assert_eq!(response.updated_output.unwrap()["result"], "sanitized");
    }

    #[test]
    fn test_hook_response_stop_reason() {
        let response = HookResponse::default().with_stop_reason("Detected dangerous pattern");
        assert_eq!(
            response.stop_reason,
            Some("Detected dangerous pattern".to_string())
        );
    }

    #[test]
    fn test_hook_response_system_message() {
        let response = HookResponse::default().with_system_message("Additional safety context");
        assert_eq!(
            response.system_message,
            Some("Additional safety context".to_string())
        );
    }

    #[test]
    fn test_hook_response_suppress_output() {
        let response = HookResponse::default().with_suppress_output(true);
        assert!(response.suppress_output);

        let json = serde_json::to_value(&response).unwrap();
        assert_eq!(json["suppress_output"], true);
    }

    #[test]
    fn test_hook_response_approve_without_message() {
        let out = HookOutput::Approve { message: None };
        let resp = out.into_response();
        assert!(matches!(
            resp.permission_decision,
            Some(PermissionDecision::Allow)
        ));
        assert!(resp.permission_decision_reason.is_none());
    }
}