1#![deny(clippy::print_stdout)]
5#![deny(clippy::print_stderr)]
6
7use std::path::PathBuf;
8
9use serde::{Deserialize, Serialize};
10use serde_json::Value;
11
12pub mod agent_mail;
13pub mod agent_run;
14pub mod event_msg;
15pub mod fleet;
16pub mod ids;
17pub mod journal;
18pub mod op;
19pub mod runtime;
20pub mod workroom;
21
22pub trait Status {
28 fn is_terminal(&self) -> bool;
31
32 fn is_active(&self) -> bool;
35
36 fn is_paused(&self) -> bool;
39}
40
41#[derive(Debug, Clone, Serialize, Deserialize)]
42pub struct Envelope<T> {
43 pub request_id: String,
44 #[serde(skip_serializing_if = "Option::is_none")]
45 pub thread_id: Option<String>,
46 pub body: T,
47}
48
49#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
50#[serde(rename_all = "snake_case")]
51pub enum ThreadStatus {
52 Running,
53 Idle,
54 Completed,
55 Failed,
56 Paused,
57 Archived,
58}
59
60impl Status for ThreadStatus {
61 fn is_terminal(&self) -> bool {
62 matches!(self, Self::Completed | Self::Failed | Self::Archived)
63 }
64 fn is_active(&self) -> bool {
65 matches!(self, Self::Running)
66 }
67 fn is_paused(&self) -> bool {
68 matches!(self, Self::Paused)
69 }
70}
71
72#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
73#[serde(rename_all = "snake_case")]
74pub enum SessionSource {
75 Interactive,
76 Resume,
77 Fork,
78 Api,
79 Unknown,
80}
81
82#[derive(Debug, Clone, Serialize, Deserialize)]
83pub struct Thread {
84 pub id: String,
85 pub preview: String,
86 pub ephemeral: bool,
87 pub model_provider: String,
88 pub created_at: i64,
89 pub updated_at: i64,
90 pub status: ThreadStatus,
91 #[serde(skip_serializing_if = "Option::is_none")]
92 pub path: Option<PathBuf>,
93 pub cwd: PathBuf,
94 pub cli_version: String,
95 pub source: SessionSource,
96 #[serde(skip_serializing_if = "Option::is_none")]
97 pub name: Option<String>,
98}
99
100#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
101#[serde(rename_all = "snake_case")]
102pub enum ThreadGoalStatus {
103 Active,
104 Paused,
105 Blocked,
106 UsageLimited,
107 BudgetLimited,
108 Complete,
109}
110
111impl Status for ThreadGoalStatus {
112 fn is_terminal(&self) -> bool {
113 matches!(self, Self::Complete)
114 }
115 fn is_active(&self) -> bool {
116 matches!(self, Self::Active)
117 }
118 fn is_paused(&self) -> bool {
119 matches!(self, Self::Paused)
120 }
121}
122
123#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
124pub struct ThreadGoal {
125 pub thread_id: String,
126 pub goal_id: String,
127 pub objective: String,
128 pub status: ThreadGoalStatus,
129 #[serde(skip_serializing_if = "Option::is_none")]
130 pub token_budget: Option<i64>,
131 pub tokens_used: i64,
132 pub time_used_seconds: i64,
133 pub continuation_count: i64,
134 pub created_at: i64,
135 pub updated_at: i64,
136}
137
138#[derive(Debug, Clone, Serialize, Deserialize)]
139pub struct ThreadStartParams {
140 #[serde(skip_serializing_if = "Option::is_none")]
141 pub model: Option<String>,
142 #[serde(skip_serializing_if = "Option::is_none")]
143 pub model_provider: Option<String>,
144 #[serde(skip_serializing_if = "Option::is_none")]
145 pub cwd: Option<PathBuf>,
146 #[serde(default)]
147 pub persist_extended_history: bool,
148}
149
150#[derive(Debug, Clone, Serialize, Deserialize)]
151pub struct ThreadResumeParams {
152 pub thread_id: String,
153 #[serde(skip_serializing_if = "Option::is_none")]
154 pub history: Option<Vec<Value>>,
155 #[serde(skip_serializing_if = "Option::is_none")]
156 pub path: Option<PathBuf>,
157 #[serde(skip_serializing_if = "Option::is_none")]
158 pub model: Option<String>,
159 #[serde(skip_serializing_if = "Option::is_none")]
160 pub model_provider: Option<String>,
161 #[serde(skip_serializing_if = "Option::is_none")]
162 pub cwd: Option<PathBuf>,
163 #[serde(skip_serializing_if = "Option::is_none")]
164 pub approval_policy: Option<String>,
165 #[serde(skip_serializing_if = "Option::is_none")]
166 pub sandbox: Option<String>,
167 #[serde(skip_serializing_if = "Option::is_none")]
168 pub config: Option<Value>,
169 #[serde(skip_serializing_if = "Option::is_none")]
170 pub base_instructions: Option<String>,
171 #[serde(skip_serializing_if = "Option::is_none")]
172 pub developer_instructions: Option<String>,
173 #[serde(skip_serializing_if = "Option::is_none")]
174 pub personality: Option<String>,
175 #[serde(default)]
176 pub persist_extended_history: bool,
177}
178
179#[derive(Debug, Clone, Serialize, Deserialize)]
180pub struct ThreadForkParams {
181 pub thread_id: String,
182 #[serde(skip_serializing_if = "Option::is_none")]
183 pub path: Option<PathBuf>,
184 #[serde(skip_serializing_if = "Option::is_none")]
185 pub model: Option<String>,
186 #[serde(skip_serializing_if = "Option::is_none")]
187 pub model_provider: Option<String>,
188 #[serde(skip_serializing_if = "Option::is_none")]
189 pub cwd: Option<PathBuf>,
190 #[serde(skip_serializing_if = "Option::is_none")]
191 pub approval_policy: Option<String>,
192 #[serde(skip_serializing_if = "Option::is_none")]
193 pub sandbox: Option<String>,
194 #[serde(skip_serializing_if = "Option::is_none")]
195 pub config: Option<Value>,
196 #[serde(skip_serializing_if = "Option::is_none")]
197 pub base_instructions: Option<String>,
198 #[serde(skip_serializing_if = "Option::is_none")]
199 pub developer_instructions: Option<String>,
200 #[serde(default)]
201 pub persist_extended_history: bool,
202}
203
204#[derive(Debug, Clone, Serialize, Deserialize)]
205pub struct ThreadListParams {
206 #[serde(default)]
207 pub include_archived: bool,
208 #[serde(skip_serializing_if = "Option::is_none")]
209 pub limit: Option<usize>,
210}
211
212#[derive(Debug, Clone, Serialize, Deserialize)]
213pub struct ThreadReadParams {
214 pub thread_id: String,
215}
216
217#[derive(Debug, Clone, Serialize, Deserialize)]
218pub struct ThreadSetNameParams {
219 pub thread_id: String,
220 pub name: String,
221}
222
223#[derive(Debug, Clone, Serialize, Deserialize)]
224pub struct ThreadGoalSetParams {
225 pub thread_id: String,
226 pub objective: String,
227 #[serde(skip_serializing_if = "Option::is_none")]
228 pub token_budget: Option<i64>,
229}
230
231#[derive(Debug, Clone, Serialize, Deserialize)]
232pub struct ThreadGoalGetParams {
233 pub thread_id: String,
234}
235
236#[derive(Debug, Clone, Serialize, Deserialize)]
237pub struct ThreadGoalClearParams {
238 pub thread_id: String,
239}
240
241#[derive(Debug, Clone, Serialize, Deserialize)]
242pub struct ThreadGoalProgressParams {
243 pub thread_id: String,
244 #[serde(default)]
245 pub token_delta: i64,
246 #[serde(default)]
247 pub time_delta_seconds: i64,
248 #[serde(default)]
249 pub record_continuation: bool,
250}
251
252#[derive(Debug, Clone, Serialize, Deserialize)]
253#[serde(tag = "kind", rename_all = "snake_case")]
254pub enum ThreadRequest {
255 Create {
256 #[serde(default)]
257 metadata: Value,
258 },
259 Start(ThreadStartParams),
260 Resume(ThreadResumeParams),
261 Fork(ThreadForkParams),
262 List(ThreadListParams),
263 Read(ThreadReadParams),
264 SetName(ThreadSetNameParams),
265 GoalSet(ThreadGoalSetParams),
266 GoalGet(ThreadGoalGetParams),
267 GoalClear(ThreadGoalClearParams),
268 GoalRecordProgress(ThreadGoalProgressParams),
269 Archive {
270 thread_id: String,
271 },
272 Unarchive {
273 thread_id: String,
274 },
275 Message {
276 thread_id: String,
277 input: String,
278 },
279}
280
281#[derive(Debug, Clone, Serialize, Deserialize)]
283pub struct ThreadResponse {
284 pub thread_id: String,
286 pub status: String,
288 #[serde(skip_serializing_if = "Option::is_none")]
290 pub thread: Option<Thread>,
291 #[serde(default)]
293 pub threads: Vec<Thread>,
294 #[serde(skip_serializing_if = "Option::is_none")]
296 pub goal: Option<ThreadGoal>,
297 #[serde(skip_serializing_if = "Option::is_none")]
299 pub model: Option<String>,
300 #[serde(skip_serializing_if = "Option::is_none")]
302 pub model_provider: Option<String>,
303 #[serde(skip_serializing_if = "Option::is_none")]
305 pub cwd: Option<PathBuf>,
306 #[serde(skip_serializing_if = "Option::is_none")]
308 pub approval_policy: Option<String>,
309 #[serde(skip_serializing_if = "Option::is_none")]
311 pub sandbox: Option<String>,
312 #[serde(default)]
314 pub events: Vec<EventFrame>,
315 #[serde(default)]
317 pub data: Value,
318}
319
320#[derive(Debug, Clone, Serialize, Deserialize)]
322#[serde(tag = "kind", rename_all = "snake_case")]
323pub enum AppRequest {
324 Capabilities,
326 ConfigGet { key: String },
328 ConfigSet { key: String, value: String },
330 ConfigUnset { key: String },
332 ConfigList,
334 ConfigReload,
347 Models,
349 ThreadLoadedList,
351 SubmitUserInput {
356 request_id: String,
357 answers: Vec<UserInputAnswerEvent>,
358 },
359}
360
361#[derive(Debug, Clone, Serialize, Deserialize)]
363pub struct AppResponse {
364 pub ok: bool,
366 pub data: Value,
368 #[serde(default)]
370 pub events: Vec<EventFrame>,
371}
372
373#[derive(Debug, Clone, Serialize, Deserialize)]
375pub struct PromptRequest {
376 #[serde(skip_serializing_if = "Option::is_none")]
378 pub thread_id: Option<String>,
379 pub prompt: String,
381 #[serde(skip_serializing_if = "Option::is_none")]
383 pub model: Option<String>,
384}
385
386#[derive(Debug, Clone, Serialize, Deserialize)]
388pub struct PromptResponse {
389 pub output: String,
391 pub model: String,
393 #[serde(default)]
395 pub events: Vec<EventFrame>,
396}
397
398#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
400#[serde(rename_all = "snake_case")]
401pub enum AskForApproval {
402 UnlessTrusted,
404 OnFailure,
406 OnRequest,
408 Reject {
410 sandbox_approval: bool,
411 rules: bool,
412 mcp_elicitations: bool,
413 },
414 Never,
416}
417
418#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
420#[serde(rename_all = "snake_case")]
421pub enum ToolKind {
422 Function,
424 Mcp,
426}
427
428#[derive(Debug, Clone, Serialize, Deserialize)]
430pub struct LocalShellParams {
431 pub command: String,
433 #[serde(skip_serializing_if = "Option::is_none")]
435 pub cwd: Option<String>,
436 #[serde(skip_serializing_if = "Option::is_none")]
438 pub timeout_ms: Option<u64>,
439}
440
441#[derive(Debug, Clone, Serialize, Deserialize)]
443#[serde(tag = "type", rename_all = "snake_case")]
444pub enum ToolPayload {
445 Function { arguments: String },
447 Custom { input: String },
449 LocalShell { params: LocalShellParams },
451 Mcp {
453 server: String,
454 tool: String,
455 raw_arguments: Value,
456 #[serde(skip_serializing_if = "Option::is_none")]
457 raw_tool_call_id: Option<String>,
458 },
459}
460
461#[derive(Debug, Clone, Serialize, Deserialize)]
463#[serde(tag = "type", rename_all = "snake_case")]
464pub enum ToolOutput {
465 Function {
467 #[serde(skip_serializing_if = "Option::is_none")]
469 body: Option<Value>,
470 success: bool,
472 },
473 Mcp {
475 result: Value,
477 },
478}
479
480impl ToolOutput {
481 pub fn success(&self) -> bool {
486 match self {
487 Self::Function { success, .. } => *success,
488 Self::Mcp { result } => {
489 matches!(result.get("isError"), None | Some(Value::Bool(false)))
490 }
491 }
492 }
493}
494
495#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
497#[serde(rename_all = "snake_case")]
498pub enum NetworkPolicyRuleAction {
499 Allow,
501 Deny,
503}
504
505#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
507pub struct NetworkPolicyAmendment {
508 pub host: String,
510 pub action: NetworkPolicyRuleAction,
512}
513
514#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
516#[serde(tag = "type", rename_all = "snake_case")]
517pub enum ReviewDecision {
518 Approved,
520 ApprovedExecpolicyAmendment,
522 ApprovedForSession,
524 NetworkPolicyAmendment {
526 host: String,
527 action: NetworkPolicyRuleAction,
528 },
529 Denied,
531 Abort,
533}
534
535#[derive(Debug, Clone, Serialize, Deserialize)]
537#[serde(rename_all = "snake_case")]
538pub enum McpStartupStatus {
539 Starting,
541 Ready,
543 Failed { error: String },
545 Cancelled,
547}
548
549#[derive(Debug, Clone, Serialize, Deserialize)]
551pub struct McpStartupUpdateEvent {
552 pub server_name: String,
554 pub status: McpStartupStatus,
556}
557
558#[derive(Debug, Clone, Serialize, Deserialize)]
560pub struct McpStartupFailure {
561 pub server_name: String,
563 pub error: String,
565}
566
567#[derive(Debug, Clone, Serialize, Deserialize)]
569pub struct McpStartupCompleteEvent {
570 pub ready: Vec<String>,
572 pub failed: Vec<McpStartupFailure>,
574 pub cancelled: Vec<String>,
576}
577
578#[derive(Debug, Clone, Serialize, Deserialize)]
580pub struct NetworkApprovalContext {
581 pub host: String,
583 pub protocol: String,
585}
586
587#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
594pub struct UserInputOptionEvent {
595 pub label: String,
597 pub description: String,
599}
600
601#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
603pub struct UserInputQuestionEvent {
604 pub header: String,
606 pub id: String,
608 pub question: String,
610 pub options: Vec<UserInputOptionEvent>,
612 #[serde(default)]
614 pub allow_free_text: bool,
615 #[serde(default)]
617 pub multi_select: bool,
618}
619
620#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
626pub struct UserInputRequestEvent {
627 pub call_id: String,
629 pub turn_id: String,
631 pub request_id: String,
633 pub questions: Vec<UserInputQuestionEvent>,
635}
636
637#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
639pub struct UserInputAnswerEvent {
640 pub id: String,
642 pub label: String,
644 pub value: String,
646}
647
648#[derive(Debug, Clone, Serialize, Deserialize)]
650pub struct ExecApprovalRequestEvent {
651 pub call_id: String,
653 pub approval_id: String,
655 pub turn_id: String,
657 pub command: String,
659 pub cwd: String,
661 pub reason: String,
663 #[serde(default, skip_serializing_if = "Option::is_none")]
665 pub matched_rule: Option<Box<str>>,
666 #[serde(skip_serializing_if = "Option::is_none")]
668 pub network_approval_context: Option<NetworkApprovalContext>,
669 #[serde(default)]
671 pub proposed_execpolicy_amendment: Vec<String>,
672 #[serde(default)]
674 pub proposed_network_policy_amendments: Vec<NetworkPolicyAmendment>,
675 #[serde(default)]
677 pub additional_permissions: Vec<String>,
678 #[serde(default)]
680 pub available_decisions: Vec<ReviewDecision>,
681}
682
683#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize, PartialEq, Eq)]
685#[serde(rename_all = "snake_case")]
686pub enum ResponseChannel {
687 #[default]
689 Text,
690 Reasoning,
692}
693
694impl ResponseChannel {
695 pub const fn is_text(&self) -> bool {
697 matches!(self, ResponseChannel::Text)
698 }
699}
700
701#[derive(Debug, Clone, Serialize, Deserialize)]
703pub struct ApprovalDecisionRequest {
704 pub decision: String,
706 #[serde(default)]
708 pub remember: bool,
709}
710
711#[derive(Debug, Clone, Serialize, Deserialize)]
717#[serde(tag = "event", rename_all = "snake_case")]
718pub enum EventFrame {
719 ResponseStart { response_id: String },
721 ResponseDelta {
723 response_id: String,
724 delta: String,
725 #[serde(default, skip_serializing_if = "ResponseChannel::is_text")]
726 channel: ResponseChannel,
727 },
728 ResponseEnd { response_id: String },
730 ToolCallStart {
732 response_id: String,
733 tool_name: String,
734 arguments: Value,
735 },
736 ToolCallResult {
738 response_id: String,
739 tool_name: String,
740 output: Value,
741 },
742 McpStartupUpdate { update: McpStartupUpdateEvent },
744 McpStartupComplete { summary: McpStartupCompleteEvent },
746 McpToolCallBegin {
748 server_name: String,
749 tool_name: String,
750 },
751 McpToolCallEnd {
753 server_name: String,
754 tool_name: String,
755 ok: bool,
756 },
757 ExecApprovalRequest { request: ExecApprovalRequestEvent },
759 ApplyPatchApprovalRequest { request: ExecApprovalRequestEvent },
761 UserInputRequest { request: UserInputRequestEvent },
766 ElicitationRequest {
768 server_name: String,
769 request_id: String,
770 prompt: String,
771 },
772 ExecCommandBegin { command: String, cwd: String },
774 ExecCommandOutputDelta { command: String, delta: String },
776 ExecCommandEnd { command: String, exit_code: i32 },
778 PatchApplyBegin { path: String },
780 PatchApplyEnd { path: String, ok: bool },
782 TurnStarted { turn_id: String },
784 TurnComplete { turn_id: String },
786 TurnAborted { turn_id: String, reason: String },
788 ThreadGoalUpdated { goal: ThreadGoal },
790 ThreadGoalCleared { thread_id: String },
792 Error {
794 response_id: String,
795 message: String,
796 },
797}