1use serde::{Deserialize, Serialize};
5use std::collections::HashMap;
6
7#[derive(Debug, Clone, Serialize)]
17pub struct OutboundControlRequest<'a, B: Serialize> {
18 #[serde(rename = "type")]
20 pub kind: &'static str, pub request_id: &'a str,
23 pub request: &'a B,
25}
26
27#[derive(Debug, Clone, Serialize, Default)]
35#[serde(rename_all = "camelCase")]
36pub struct Initialize {
37 #[serde(skip_serializing_if = "Option::is_none")]
39 pub hooks: Option<HashMap<HookEventKind, Vec<HookMatcherConfig>>>,
40 #[serde(skip_serializing_if = "Option::is_none")]
42 pub system_prompt: Option<String>,
43 #[serde(skip_serializing_if = "Option::is_none")]
45 pub append_system_prompt: Option<String>,
46 #[serde(skip_serializing_if = "Option::is_none")]
48 pub agents: Option<serde_json::Value>,
49 #[serde(skip_serializing_if = "Option::is_none")]
52 pub json_schema: Option<serde_json::Value>,
53 #[serde(skip_serializing_if = "Option::is_none")]
55 pub title: Option<String>,
56 #[serde(skip_serializing_if = "Option::is_none")]
58 pub skills: Option<Vec<String>>,
59 #[serde(skip_serializing_if = "Option::is_none")]
61 pub plugins: Option<Vec<serde_json::Value>>,
62 #[serde(skip_serializing_if = "Option::is_none")]
66 pub mcp_servers: Option<serde_json::Map<String, serde_json::Value>>,
67 #[serde(skip_serializing_if = "Option::is_none")]
69 pub plan_mode_instructions: Option<String>,
70 #[serde(skip_serializing_if = "Option::is_none")]
73 pub exclude_dynamic_sections: Option<bool>,
74 #[serde(skip_serializing_if = "Option::is_none")]
76 pub system_prompt_snapshot: Option<bool>,
77 #[serde(skip_serializing_if = "Option::is_none")]
79 pub prompt_suggestions: Option<bool>,
80 #[serde(skip_serializing_if = "Option::is_none")]
82 pub agent_progress_summaries: Option<bool>,
83 #[serde(skip_serializing_if = "Option::is_none")]
85 pub forward_subagent_text: Option<bool>,
86 #[serde(skip_serializing_if = "Option::is_none")]
88 pub tool_aliases: Option<HashMap<String, String>>,
89 #[serde(skip_serializing_if = "Option::is_none")]
92 pub supported_dialog_kinds: Option<Vec<String>>,
93 #[serde(skip_serializing_if = "Option::is_none")]
95 pub per_task_stop_affordance: Option<bool>,
96 #[serde(skip_serializing_if = "Option::is_none")]
98 pub workspace_trust: Option<WorkspaceTrust>,
99}
100
101#[derive(Debug, Clone, Serialize)]
104pub struct WorkspaceTrust {
105 pub accepted: bool,
107 pub directory: String,
109}
110
111#[derive(Debug, Clone, Serialize)]
114#[serde(rename_all = "camelCase")]
115pub struct HookMatcherConfig {
116 pub matcher: Option<String>,
118 pub hook_callback_ids: Vec<String>,
120 #[serde(skip_serializing_if = "Option::is_none")]
122 pub timeout: Option<u32>,
123}
124
125#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
128#[serde(rename_all = "camelCase")]
129pub enum PermissionMode {
130 Default,
132 AcceptEdits,
134 BypassPermissions,
136 Plan,
138 DontAsk,
140 Auto,
142}
143
144impl PermissionMode {
145 #[must_use]
147 pub fn as_str(self) -> &'static str {
148 match self {
149 Self::Default => "default",
150 Self::AcceptEdits => "acceptEdits",
151 Self::BypassPermissions => "bypassPermissions",
152 Self::Plan => "plan",
153 Self::DontAsk => "dontAsk",
154 Self::Auto => "auto",
155 }
156 }
157}
158
159#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
162#[serde(rename_all = "lowercase")]
163pub enum Effort {
164 Low,
166 Medium,
168 High,
170 Xhigh,
172 Max,
174}
175
176impl Effort {
177 #[must_use]
179 pub fn as_str(self) -> &'static str {
180 match self {
181 Self::Low => "low",
182 Self::Medium => "medium",
183 Self::High => "high",
184 Self::Xhigh => "xhigh",
185 Self::Max => "max",
186 }
187 }
188}
189
190#[derive(Debug, Clone, Copy, PartialEq, Eq)]
192pub enum Thinking {
193 Enabled,
195 Adaptive,
197 Disabled,
199}
200
201impl Thinking {
202 #[must_use]
204 pub fn as_str(self) -> &'static str {
205 match self {
206 Self::Enabled => "enabled",
207 Self::Adaptive => "adaptive",
208 Self::Disabled => "disabled",
209 }
210 }
211}
212
213#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
216#[serde(rename_all = "lowercase")]
217pub enum ThinkingDisplay {
218 Summarized,
220 Omitted,
222 Highlights,
224}
225
226impl ThinkingDisplay {
227 #[must_use]
229 pub fn as_str(self) -> &'static str {
230 match self {
231 Self::Summarized => "summarized",
232 Self::Omitted => "omitted",
233 Self::Highlights => "highlights",
234 }
235 }
236}
237
238#[derive(Debug, Clone, Copy, PartialEq, Eq)]
240pub enum Autocompact {
241 Auto,
243 Tokens(u64),
245}
246
247impl Autocompact {
248 #[must_use]
250 pub fn to_arg(self) -> String {
251 match self {
252 Self::Auto => "auto".into(),
253 Self::Tokens(n) => n.to_string(),
254 }
255 }
256}
257
258#[derive(Debug, Clone, Copy, PartialEq, Eq)]
260pub enum PermissionPrompts {
261 Host,
263 None,
265}
266
267impl PermissionPrompts {
268 #[must_use]
270 pub fn as_str(self) -> &'static str {
271 match self {
272 Self::Host => "host",
273 Self::None => "none",
274 }
275 }
276}
277
278#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
281#[serde(rename_all = "lowercase")]
282pub enum Priority {
283 Now,
286 Next,
288 Later,
290}
291
292#[derive(Debug, Clone, Serialize, Deserialize)]
295pub struct UserMessage {
296 #[serde(rename = "type")]
298 pub kind: String, pub message: UserMessageBody,
301 pub parent_tool_use_id: Option<String>,
303 pub uuid: String,
306 #[serde(default, skip_serializing_if = "Option::is_none")]
308 pub priority: Option<Priority>,
309 #[serde(default, rename = "shouldQuery", skip_serializing_if = "Option::is_none")]
311 pub should_query: Option<bool>,
312 #[serde(default, skip_serializing_if = "Option::is_none")]
315 pub timestamp: Option<String>,
316 #[serde(default, skip_serializing_if = "Option::is_none")]
320 pub client_composed: Option<bool>,
321}
322
323impl UserMessage {
324 pub fn text(content: impl Into<String>) -> Self {
326 Self::blocks(vec![ContentBlock::Text { text: content.into() }])
327 }
328
329 #[must_use]
331 pub fn blocks(content: Vec<ContentBlock>) -> Self {
332 Self {
333 kind: "user".into(),
334 message: UserMessageBody { role: "user".into(), content },
335 parent_tool_use_id: None,
336 uuid: uuid::Uuid::new_v4().to_string(),
337 priority: None,
338 should_query: None,
339 timestamp: None,
340 client_composed: None,
341 }
342 }
343
344 #[must_use]
346 pub fn with_timestamp(mut self, timestamp: impl Into<String>) -> Self {
347 self.timestamp = Some(timestamp.into());
348 self
349 }
350
351 #[must_use]
354 pub fn client_composed(mut self) -> Self {
355 self.client_composed = Some(true);
356 self
357 }
358
359 #[must_use]
361 pub fn with_priority(mut self, priority: Priority) -> Self {
362 self.priority = Some(priority);
363 self
364 }
365
366 #[must_use]
369 pub fn context_only(mut self) -> Self {
370 self.should_query = Some(false);
371 self
372 }
373}
374
375#[derive(Debug, Clone, Serialize, Deserialize)]
377pub struct UserMessageBody {
378 pub role: String,
380 pub content: Vec<ContentBlock>,
382}
383
384#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
386#[serde(tag = "type")]
387pub enum ContentBlock {
388 #[serde(rename = "text")]
390 Text {
391 text: String,
393 },
394 #[serde(rename = "image")]
396 Image {
397 source: ImageSource,
399 },
400 #[serde(rename = "document")]
402 Document {
403 source: DocumentSource,
405 #[serde(default, skip_serializing_if = "Option::is_none")]
407 title: Option<String>,
408 },
409 #[serde(rename = "tool_result")]
411 ToolResult {
412 tool_use_id: String,
414 content: Vec<ContentBlock>,
416 #[serde(default, skip_serializing_if = "Option::is_none")]
418 is_error: Option<bool>,
419 },
420}
421
422impl ContentBlock {
423 pub fn image_base64(media_type: impl Into<String>, data: impl Into<String>) -> Self {
425 Self::Image {
426 source: ImageSource::Base64 { media_type: media_type.into(), data: data.into() },
427 }
428 }
429
430 pub fn pdf_base64(data: impl Into<String>) -> Self {
432 Self::Document {
433 source: DocumentSource::Base64 { media_type: "application/pdf".into(), data: data.into() },
434 title: None,
435 }
436 }
437}
438
439#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
441#[serde(tag = "type", rename_all = "lowercase")]
442pub enum DocumentSource {
443 Base64 {
445 media_type: String,
447 data: String,
449 },
450 Text {
452 media_type: String,
454 data: String,
456 },
457 Url {
459 url: String,
461 },
462}
463
464#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
466#[serde(tag = "type", rename_all = "lowercase")]
467pub enum ImageSource {
468 Base64 {
470 media_type: String,
472 data: String,
474 },
475 Url {
477 url: String,
479 },
480}
481
482#[derive(Debug, Clone, Serialize)]
485pub struct ControlResponse {
486 #[serde(rename = "type")]
488 pub kind: &'static str, pub response: ControlResponseBody,
491}
492
493#[derive(Debug, Clone, Serialize)]
495#[serde(tag = "subtype", rename_all = "lowercase")]
496pub enum ControlResponseBody {
497 Success {
499 request_id: String,
501 response: serde_json::Value,
503 },
504 Error {
506 request_id: String,
508 error: String,
510 },
511}
512
513impl ControlResponse {
514 pub fn success(request_id: impl Into<String>, response: serde_json::Value) -> Self {
516 Self {
517 kind: "control_response",
518 response: ControlResponseBody::Success { request_id: request_id.into(), response },
519 }
520 }
521 pub fn error(request_id: impl Into<String>, error: impl Into<String>) -> Self {
523 Self {
524 kind: "control_response",
525 response: ControlResponseBody::Error { request_id: request_id.into(), error: error.into() },
526 }
527 }
528}
529
530#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
534#[serde(tag = "behavior", rename_all = "camelCase")]
535pub enum ToolDecision {
536 #[serde(rename = "allow")]
540 Allow {
541 #[serde(rename = "updatedInput", skip_serializing_if = "Option::is_none")]
543 updated_input: Option<serde_json::Value>,
544 #[serde(rename = "updatedPermissions", default, skip_serializing_if = "Option::is_none")]
546 updated_permissions: Option<Vec<serde_json::Value>>,
547 },
548 #[serde(rename = "deny")]
550 Deny {
551 message: String,
553 #[serde(skip_serializing_if = "Option::is_none")]
555 interrupt: Option<bool>,
556 },
557}
558
559impl ToolDecision {
560 #[must_use]
562 pub fn allow() -> Self { Self::Allow { updated_input: None, updated_permissions: None } }
563 #[must_use]
565 pub fn allow_with_input(input: serde_json::Value) -> Self {
566 Self::Allow { updated_input: Some(input), updated_permissions: None }
567 }
568 #[must_use]
571 pub fn allow_and_remember(updates: Vec<serde_json::Value>) -> Self {
572 Self::Allow { updated_input: None, updated_permissions: Some(updates) }
573 }
574 pub fn deny(message: impl Into<String>) -> Self {
576 Self::Deny { message: message.into(), interrupt: None }
577 }
578}
579
580#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
584#[serde(rename_all = "camelCase")]
585pub struct HookOutput {
586 #[serde(rename = "continue", skip_serializing_if = "Option::is_none")]
588 pub continue_: Option<bool>,
589 #[serde(skip_serializing_if = "Option::is_none")]
591 pub suppress_output: Option<bool>,
592 #[serde(skip_serializing_if = "Option::is_none")]
594 pub stop_reason: Option<String>,
595 #[serde(skip_serializing_if = "Option::is_none")]
597 pub decision: Option<HookDecision>,
598 #[serde(skip_serializing_if = "Option::is_none")]
600 pub system_message: Option<String>,
601 #[serde(skip_serializing_if = "Option::is_none")]
603 pub reason: Option<String>,
604 #[serde(skip_serializing_if = "Option::is_none")]
606 pub hook_specific_output: Option<serde_json::Value>,
607}
608
609#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
611#[serde(rename_all = "lowercase")]
612pub enum HookDecision {
613 Approve,
615 Block,
617}
618
619impl HookOutput {
620 #[must_use]
622 pub fn allow() -> Self { Self::default() }
623
624 pub fn block(reason: impl Into<String>) -> Self {
626 Self { decision: Some(HookDecision::Block), reason: Some(reason.into()), ..Self::default() }
627 }
628
629 pub fn with_context(event: HookEventKind, context: impl Into<String>) -> Self {
631 Self {
632 hook_specific_output: Some(serde_json::json!({
633 "hookEventName": event,
634 "additionalContext": context.into(),
635 })),
636 ..Self::default()
637 }
638 }
639
640 #[must_use]
642 pub fn watch_paths(event: HookEventKind, paths: impl IntoIterator<Item = impl Into<String>>) -> Self {
643 let paths: Vec<String> = paths.into_iter().map(Into::into).collect();
644 Self {
645 hook_specific_output: Some(serde_json::json!({
646 "hookEventName": event,
647 "watchPaths": paths,
648 })),
649 ..Self::default()
650 }
651 }
652
653 #[must_use]
655 pub fn is_block(&self) -> bool { self.decision == Some(HookDecision::Block) }
656}
657
658#[derive(Debug, Clone, Serialize)]
662pub struct BackgroundTasksRequest {
663 pub subtype: &'static str, #[serde(skip_serializing_if = "Option::is_none")]
667 pub tool_use_id: Option<String>,
668}
669
670impl BackgroundTasksRequest {
671 #[must_use]
673 pub fn all() -> Self {
674 Self { subtype: "background_tasks", tool_use_id: None }
675 }
676 pub fn one(tool_use_id: impl Into<String>) -> Self {
678 Self { subtype: "background_tasks", tool_use_id: Some(tool_use_id.into()) }
679 }
680}
681
682#[derive(Debug, Clone, Deserialize)]
687#[serde(tag = "type")]
688#[non_exhaustive]
689pub enum InboundMessage {
690 #[serde(rename = "system")]
692 System(SystemMessage),
693 #[serde(rename = "user")]
695 User(InboundUserMessage),
696 #[serde(rename = "assistant")]
698 Assistant(AssistantMessage),
699 #[serde(rename = "stream_event")]
701 StreamEvent(StreamEventMessage),
702 #[serde(rename = "result")]
704 Result(ResultMessage),
705 #[serde(rename = "control_request")]
708 ControlRequest(ControlRequest),
709 #[serde(rename = "control_response")]
712 ControlResponse(InboundControlResponse),
713 #[serde(rename = "keep_alive")]
716 KeepAlive(KeepAliveMessage),
717 #[serde(rename = "control_cancel_request")]
720 ControlCancelRequest(ControlCancelRequest),
721 #[serde(skip_deserializing)]
724 Other(serde_json::Value),
725 #[doc(hidden)]
728 #[serde(other)]
729 Unknown,
730}
731
732#[derive(Debug, Clone, Deserialize)]
734#[non_exhaustive]
735pub struct ControlCancelRequest {
736 pub request_id: String,
738}
739
740#[derive(Debug, Clone, Serialize, Deserialize)]
748#[non_exhaustive]
749pub struct SystemMessage {
750 pub subtype: String, pub session_id: String,
754 #[serde(default)]
756 pub cwd: String,
757 #[serde(default)]
759 pub model: String,
760 #[serde(default)]
762 pub claude_code_version: String,
763 #[serde(default)]
765 pub permission_mode: String,
766 #[serde(default)]
768 pub tools: Vec<String>,
769 #[serde(default)]
771 pub betas: Vec<String>,
772 #[serde(default)]
774 pub capabilities: Vec<String>,
775 #[serde(default)]
777 pub skills: Vec<serde_json::Value>,
778 #[serde(default)]
780 pub plugins: Vec<serde_json::Value>,
781 #[serde(flatten)]
783 pub extra: HashMap<String, serde_json::Value>,
784}
785
786impl SystemMessage {
787 #[must_use]
789 pub fn code_mode_active(&self) -> bool {
790 self.tools.iter().any(|t| t == "REPL" || t == "Eval")
791 }
792}
793
794#[derive(Debug, Clone, Deserialize)]
796#[non_exhaustive]
797pub struct AssistantMessage {
798 pub message: AssistantMessageBody,
800 #[serde(default)]
802 pub session_id: Option<String>,
803 #[serde(default)]
805 pub parent_tool_use_id: Option<String>,
806}
807
808#[derive(Debug, Clone, Deserialize)]
810#[non_exhaustive]
811pub struct AssistantMessageBody {
812 pub role: String,
814 pub content: Vec<AssistantBlock>,
816 #[serde(default)]
818 pub stop_reason: Option<String>,
819}
820
821#[derive(Debug, Clone, Deserialize)]
823#[serde(tag = "type")]
824#[non_exhaustive]
825pub enum AssistantBlock {
826 #[serde(rename = "text")]
828 Text {
829 text: String,
831 },
832 #[serde(rename = "tool_use")]
834 ToolUse {
835 id: String,
837 name: String,
839 input: serde_json::Value,
841 },
842 #[serde(rename = "thinking")]
844 Thinking {
845 thinking: String,
847 },
848 #[serde(rename = "redacted_thinking")]
850 RedactedThinking {
851 data: String,
853 },
854 #[serde(rename = "server_tool_use")]
856 ServerToolUse {
857 id: String,
859 name: String,
861 input: serde_json::Value,
863 },
864 #[serde(rename = "web_search_tool_result")]
866 WebSearchToolResult {
867 tool_use_id: String,
869 content: serde_json::Value,
871 },
872 #[serde(other)]
874 Unknown,
875}
876
877#[derive(Debug, Clone, Deserialize)]
880#[non_exhaustive]
881pub struct InboundUserMessage {
882 #[serde(default)]
884 pub uuid: Option<String>,
885 #[serde(default, rename = "isReplay")]
887 pub is_replay: Option<bool>,
888 #[serde(default, rename = "isSynthetic")]
890 pub is_synthetic: Option<bool>,
891 #[serde(default)]
893 pub parent_tool_use_id: Option<String>,
894 #[serde(default)]
896 pub agent_id: Option<String>,
897 #[serde(default)]
899 pub message: Option<InboundUserBody>,
900 #[serde(default)]
903 pub tool_use_result: Option<serde_json::Value>,
904}
905
906#[derive(Debug, Clone, Deserialize)]
908#[non_exhaustive]
909pub struct InboundUserBody {
910 #[serde(default)]
912 pub content: serde_json::Value,
913}
914
915#[derive(Debug, Clone, Deserialize)]
917#[non_exhaustive]
918pub struct InboundToolResult {
919 pub tool_use_id: String,
921 #[serde(default)]
923 pub content: serde_json::Value,
924 #[serde(default)]
926 pub is_error: bool,
927}
928
929impl InboundUserMessage {
930 #[must_use]
932 pub fn tool_results(&self) -> Vec<InboundToolResult> {
933 let Some(blocks) = self.message.as_ref().and_then(|m| m.content.as_array()) else {
934 return vec![];
935 };
936 blocks
937 .iter()
938 .filter(|b| b.get("type").and_then(|t| t.as_str()) == Some("tool_result"))
939 .filter_map(|b| InboundToolResult::deserialize(b).ok())
940 .collect()
941 }
942}
943
944#[derive(Debug, Clone, Deserialize)]
947#[non_exhaustive]
948pub struct StreamEventMessage {
949 pub event: serde_json::Value,
951 #[serde(default)]
953 pub session_id: Option<String>,
954 #[serde(default)]
956 pub ttft_ms: Option<u64>,
957 #[serde(default)]
959 pub parent_tool_use_id: Option<String>,
960}
961
962#[derive(Debug, Clone, Default, Deserialize)]
965#[non_exhaustive]
966pub struct ResultMessage {
967 #[serde(default)]
970 pub subtype: String,
971 #[serde(default)]
973 pub is_error: bool,
974 #[serde(default)]
976 pub result: Option<String>,
977 #[serde(default)]
979 pub errors: Option<Vec<serde_json::Value>>,
980 #[serde(default)]
982 pub session_id: Option<String>,
983 #[serde(default)]
985 pub total_cost_usd: Option<f64>,
986 #[serde(default)]
988 pub ttft_ms: Option<u64>,
989 #[serde(default)]
991 pub duration_ms: Option<u64>,
992 #[serde(default)]
994 pub usage: Option<UsageSummary>,
995 #[serde(default)]
997 pub stop_reason: Option<String>,
998 #[serde(default)]
1000 pub duration_api_ms: Option<u64>,
1001 #[serde(default)]
1003 pub num_turns: Option<u64>,
1004 #[serde(default)]
1006 pub terminal_reason: Option<String>,
1007 #[serde(default)]
1009 pub structured_output: Option<serde_json::Value>,
1010 #[serde(default)]
1012 pub permission_denials: Vec<serde_json::Value>,
1013 #[serde(default, rename = "modelUsage")]
1015 pub model_usage: Option<serde_json::Value>,
1016 #[serde(default)]
1018 pub user_message_uuids: Vec<String>,
1019 #[serde(default)]
1022 pub origin: Option<serde_json::Value>,
1023 #[serde(flatten)]
1025 pub extra: HashMap<String, serde_json::Value>,
1026}
1027
1028impl ResultMessage {
1029 #[must_use]
1031 pub fn is_success(&self) -> bool {
1032 self.subtype == "success" && !self.is_error
1033 }
1034
1035 #[must_use]
1038 pub fn answers(&self, uuid: &str) -> bool {
1039 self.user_message_uuids.iter().any(|u| u.eq_ignore_ascii_case(uuid))
1040 }
1041
1042 #[must_use]
1044 pub fn error_kind(&self) -> TurnErrorKind {
1045 match self.subtype.as_str() {
1046 "error_during_execution" => TurnErrorKind::Execution,
1047 "error_max_turns" => TurnErrorKind::MaxTurns,
1048 "error_max_budget_usd" => TurnErrorKind::MaxBudget,
1049 "error_max_structured_output_retries" => TurnErrorKind::StructuredOutputRetries,
1050 "success" => TurnErrorKind::Api,
1051 other => TurnErrorKind::Other(other.to_string()),
1052 }
1053 }
1054
1055 #[must_use]
1058 pub fn error_message(&self) -> String {
1059 if let Some(r) = self.result.as_ref().filter(|r| self.is_error && !r.is_empty()) {
1060 return r.clone();
1061 }
1062 let details: Vec<String> = self.errors.iter().flatten()
1063 .map(|v| v.as_str().map_or_else(|| v.to_string(), str::to_owned))
1064 .collect();
1065 if details.is_empty() { self.subtype.clone() } else { format!("{}: {}", self.subtype, details.join("; ")) }
1066 }
1067}
1068
1069#[derive(Debug, Clone, PartialEq, Eq)]
1071#[non_exhaustive]
1072pub enum TurnErrorKind {
1073 Execution,
1075 MaxTurns,
1077 MaxBudget,
1079 StructuredOutputRetries,
1081 Api,
1083 SessionClosed,
1085 Cancelled,
1088 Other(String),
1090}
1091
1092#[derive(Debug, Clone, Deserialize, Default)]
1095#[non_exhaustive]
1096pub struct UsageSummary {
1097 #[serde(default)]
1099 pub input_tokens: u64,
1100 #[serde(default)]
1102 pub output_tokens: u64,
1103 #[serde(default)]
1105 pub cache_read_input_tokens: Option<u64>,
1106 #[serde(default)]
1108 pub cache_creation_input_tokens: Option<u64>,
1109 #[serde(default)]
1111 pub cache_creation: Option<serde_json::Value>,
1112}
1113
1114#[derive(Debug, Clone, Deserialize)]
1118#[non_exhaustive]
1119pub struct ControlRequest {
1120 pub request_id: String,
1122 pub request: ControlRequestBody,
1124}
1125
1126#[derive(Debug, Clone, Deserialize)]
1128#[serde(tag = "subtype")]
1129#[non_exhaustive]
1130pub enum ControlRequestBody {
1131 #[serde(rename = "can_use_tool")]
1133 CanUseTool(CanUseToolRequest),
1134 #[serde(rename = "hook_callback")]
1136 HookCallback(HookCallbackRequest),
1137 #[serde(rename = "mcp_message")]
1139 McpMessage(McpMessageRequest),
1140 #[serde(rename = "elicitation")]
1142 Elicitation(ElicitationRequest),
1143 #[serde(rename = "request_user_dialog")]
1146 RequestUserDialog(UserDialogRequest),
1147 #[serde(other)]
1149 Unsupported,
1150}
1151
1152#[derive(Debug, Clone, Deserialize)]
1154#[non_exhaustive]
1155pub struct CanUseToolRequest {
1156 pub tool_name: String,
1158 pub input: serde_json::Value,
1160 #[serde(default)]
1162 pub tool_use_id: Option<String>,
1163 #[serde(default)]
1166 pub permission_suggestions: Option<Vec<serde_json::Value>>,
1167 #[serde(default)]
1169 pub decision_reason: Option<String>,
1170 #[serde(default)]
1172 pub decision_reason_type: Option<String>,
1173 #[serde(default)]
1175 pub mcp_server: Option<serde_json::Value>,
1176 #[serde(default)]
1178 pub blocked_path: Option<String>,
1179 #[serde(default)]
1181 pub classifier_approvable: Option<bool>,
1182 #[serde(default)]
1184 pub suppress_always_allow_rule: Option<bool>,
1185 #[serde(default)]
1187 pub default_to_no: Option<bool>,
1188 #[serde(default)]
1190 pub matched_ask_rule: Option<serde_json::Value>,
1191 #[serde(default)]
1193 pub title: Option<String>,
1194 #[serde(default)]
1196 pub display_name: Option<String>,
1197 #[serde(default)]
1199 pub description: Option<String>,
1200 #[serde(default)]
1202 pub agent_id: Option<String>,
1203 #[serde(default)]
1205 pub requires_user_interaction: Option<bool>,
1206 #[serde(flatten)]
1208 pub extra: HashMap<String, serde_json::Value>,
1209}
1210
1211#[derive(Debug, Clone, Deserialize)]
1213#[non_exhaustive]
1214pub struct ElicitationRequest {
1215 pub mcp_server_name: String,
1217 pub message: String,
1219 #[serde(default)]
1221 pub mode: Option<String>,
1222 #[serde(default)]
1224 pub url: Option<String>,
1225 #[serde(default)]
1227 pub elicitation_id: Option<String>,
1228 #[serde(default)]
1230 pub requested_schema: Option<serde_json::Value>,
1231 #[serde(default)]
1233 pub property_order: Option<Vec<String>>,
1234 #[serde(default)]
1236 pub title: Option<String>,
1237 #[serde(default)]
1239 pub display_name: Option<String>,
1240 #[serde(default)]
1242 pub description: Option<String>,
1243 #[serde(flatten)]
1245 pub extra: HashMap<String, serde_json::Value>,
1246}
1247
1248#[derive(Debug, Clone, Serialize)]
1250pub struct ElicitationResponse {
1251 pub action: ElicitationAction,
1253 #[serde(skip_serializing_if = "Option::is_none")]
1255 pub content: Option<serde_json::Map<String, serde_json::Value>>,
1256}
1257
1258#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
1260#[serde(rename_all = "lowercase")]
1261pub enum ElicitationAction {
1262 Accept,
1264 Decline,
1266 Cancel,
1268}
1269
1270impl ElicitationResponse {
1271 #[must_use]
1273 pub fn accept(content: serde_json::Map<String, serde_json::Value>) -> Self {
1274 Self { action: ElicitationAction::Accept, content: Some(content) }
1275 }
1276 #[must_use]
1278 pub fn decline() -> Self {
1279 Self { action: ElicitationAction::Decline, content: None }
1280 }
1281 #[must_use]
1283 pub fn cancel() -> Self {
1284 Self { action: ElicitationAction::Cancel, content: None }
1285 }
1286}
1287
1288#[derive(Debug, Clone, Deserialize)]
1290#[non_exhaustive]
1291pub struct UserDialogRequest {
1292 pub dialog_kind: String,
1294 #[serde(default)]
1296 pub payload: serde_json::Value,
1297 #[serde(default)]
1299 pub tool_use_id: Option<String>,
1300}
1301
1302#[derive(Debug, Clone, Serialize, PartialEq)]
1305pub struct UserDialogResponse {
1306 pub behavior: UserDialogBehavior,
1308 #[serde(skip_serializing_if = "Option::is_none")]
1311 pub result: Option<serde_json::Value>,
1312}
1313
1314#[derive(Debug, Clone, Copy, Serialize, PartialEq, Eq)]
1316#[serde(rename_all = "lowercase")]
1317pub enum UserDialogBehavior {
1318 Completed,
1320 Cancelled,
1322}
1323
1324impl UserDialogResponse {
1325 pub fn completed(result: impl Into<serde_json::Value>) -> Self {
1327 Self { behavior: UserDialogBehavior::Completed, result: Some(result.into()) }
1328 }
1329 #[must_use]
1331 pub fn cancelled() -> Self {
1332 Self { behavior: UserDialogBehavior::Cancelled, result: None }
1333 }
1334}
1335
1336#[derive(Debug, Clone, Deserialize)]
1338#[non_exhaustive]
1339pub struct McpMessageRequest {
1340 pub server_name: String,
1342 pub message: serde_json::Value,
1344}
1345
1346#[derive(Debug, Clone, Deserialize)]
1348#[non_exhaustive]
1349pub struct HookCallbackRequest {
1350 pub callback_id: String,
1352 pub input: HookEventPayload,
1354 #[serde(default)]
1356 pub tool_use_id: Option<String>,
1357 #[serde(default)]
1359 pub issued_at: Option<u64>,
1360}
1361
1362#[derive(Debug, Clone, Deserialize)]
1365#[non_exhaustive]
1366pub struct InboundControlResponse {
1367 pub response: InboundControlResponseBody,
1369}
1370
1371#[derive(Debug, Clone, Deserialize)]
1373#[non_exhaustive]
1374pub struct InboundControlResponseBody {
1375 pub subtype: String, pub request_id: String,
1379 #[serde(default)]
1381 pub response: Option<serde_json::Value>,
1382 #[serde(default)]
1384 pub error: Option<String>,
1385}
1386
1387#[derive(Debug, Clone, Deserialize)]
1390#[non_exhaustive]
1391pub struct HookEventPayload {
1392 pub hook_event_name: HookEventKind,
1394 #[serde(default)]
1396 pub tool_name: Option<String>,
1397 #[serde(default)]
1399 pub tool_input: Option<serde_json::Value>,
1400 #[serde(default)]
1402 pub tool_use_id: Option<String>,
1403 #[serde(default)]
1405 pub error: Option<String>,
1406 #[serde(default)]
1408 pub is_interrupt: Option<bool>,
1409 #[serde(default)]
1411 pub session_id: Option<String>,
1412 #[serde(default)]
1414 pub cwd: Option<String>,
1415 #[serde(default)]
1417 pub file_path: Option<String>,
1418 #[serde(default)]
1420 pub event: Option<String>, #[serde(flatten)]
1423 pub extra: HashMap<String, serde_json::Value>,
1424}
1425
1426#[derive(Debug, Clone, Deserialize)]
1429#[non_exhaustive]
1430pub struct BackgroundTask {
1431 pub task_id: String,
1433 #[serde(default)]
1435 pub run_id: Option<String>,
1436 #[serde(default)]
1438 pub task_type: String,
1439 #[serde(default)]
1441 pub subagent_type: Option<String>,
1442 #[serde(default)]
1444 pub description: String,
1445 #[serde(default)]
1447 pub parent_task_id: Option<String>,
1448 #[serde(default)]
1450 pub ambient: Option<bool>,
1451 #[serde(default)]
1453 pub awaited: Option<bool>,
1454 #[serde(default)]
1456 pub off_host: Option<bool>,
1457}
1458
1459#[derive(Debug, Clone, Deserialize)]
1462#[non_exhaustive]
1463pub struct KeepAliveMessage {
1464 #[serde(flatten)]
1466 pub extra: HashMap<String, serde_json::Value>,
1467}
1468
1469#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
1473pub struct ToolDef {
1474 pub name: String,
1476 pub description: String,
1478 #[serde(skip_serializing_if = "Option::is_none")]
1480 pub input_schema: Option<serde_json::Value>,
1481 #[serde(default, skip_serializing_if = "Option::is_none")]
1483 pub title: Option<String>,
1484 #[serde(default, skip_serializing_if = "Option::is_none")]
1486 pub output_schema: Option<serde_json::Value>,
1487 #[serde(default, skip_serializing_if = "Option::is_none")]
1489 pub annotations: Option<ToolAnnotations>,
1490}
1491
1492#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
1494#[serde(rename_all = "camelCase")]
1495pub struct ToolAnnotations {
1496 #[serde(default, skip_serializing_if = "Option::is_none")]
1498 pub read_only_hint: Option<bool>,
1499 #[serde(default, skip_serializing_if = "Option::is_none")]
1501 pub destructive_hint: Option<bool>,
1502 #[serde(default, skip_serializing_if = "Option::is_none")]
1504 pub idempotent_hint: Option<bool>,
1505 #[serde(default, skip_serializing_if = "Option::is_none")]
1507 pub open_world_hint: Option<bool>,
1508}
1509
1510#[derive(Debug, Clone, Serialize, Deserialize)]
1512pub struct McpManifest {
1513 pub name: String,
1515 pub tools: Vec<ToolDef>,
1517 #[serde(default)]
1519 pub server_info: Option<serde_json::Value>,
1520}
1521
1522#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
1525#[serde(rename_all = "PascalCase")]
1526#[non_exhaustive]
1527pub enum HookEventKind {
1528 PreToolUse,
1530 PostToolUse,
1532 PostToolUseFailure,
1534 PostToolBatch,
1536 Notification,
1538 UserPromptSubmit,
1540 UserPromptExpansion,
1542 SessionStart,
1544 SessionEnd,
1546 Stop,
1548 StopFailure,
1550 SubagentStart,
1552 SubagentStop,
1554 PreCompact,
1556 PostCompact,
1558 PreModelSwitch,
1560 PostModelSwitch,
1562 PermissionRequest,
1564 PermissionDenied,
1566 Setup,
1568 TeammateIdle,
1570 TaskCreated,
1572 TaskCompleted,
1574 Elicitation,
1576 ElicitationResult,
1578 ConfigChange,
1580 WorktreeCreate,
1582 WorktreeRemove,
1584 InstructionsLoaded,
1586 CwdChanged,
1588 FileChanged,
1590 DirectoryAdded,
1592 MessageDisplay,
1594}
1595