nils-macos-agent 0.6.5

CLI crate for nils-macos-agent in the nils-cli workspace.
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
use clap::ValueEnum;
use screen_record::types::{PermissionState, PermissionStatusSchema};
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::error::{CliError, ErrorCategory};
use crate::screen_record_adapter::{AppInfo, WindowInfo};

#[derive(Debug, Clone, Serialize)]
pub struct SuccessEnvelope<T>
where
    T: Serialize,
{
    pub schema_version: u8,
    pub ok: bool,
    pub command: &'static str,
    pub result: T,
}

impl<T> SuccessEnvelope<T>
where
    T: Serialize,
{
    pub fn new(command: &'static str, result: T) -> Self {
        Self {
            schema_version: 1,
            ok: true,
            command,
            result,
        }
    }
}

#[derive(Debug, Clone, Serialize)]
pub struct ErrorEnvelope {
    pub schema_version: u8,
    pub ok: bool,
    pub error: ErrorResult,
}

impl ErrorEnvelope {
    pub fn from_error(err: &CliError) -> Self {
        Self {
            schema_version: 1,
            ok: false,
            error: ErrorResult::from(err),
        }
    }
}

#[derive(Debug, Clone, Serialize)]
pub struct ErrorResult {
    pub category: &'static str,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub operation: Option<String>,
    pub message: String,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub hints: Vec<String>,
}

impl From<&CliError> for ErrorResult {
    fn from(err: &CliError) -> Self {
        let category = match err.category() {
            ErrorCategory::Usage => "usage",
            ErrorCategory::Runtime => "runtime",
        };
        Self {
            category,
            operation: err.operation().map(str::to_string),
            message: err.message().to_string(),
            hints: err.hints().to_vec(),
        }
    }
}

#[derive(Debug, Clone, Copy, Serialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum PermissionStateResult {
    Ready,
    Blocked,
    Unknown,
}

impl From<PermissionState> for PermissionStateResult {
    fn from(value: PermissionState) -> Self {
        match value {
            PermissionState::Ready => Self::Ready,
            PermissionState::Blocked => Self::Blocked,
            PermissionState::Unknown => Self::Unknown,
        }
    }
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct PermissionStatusResult {
    pub screen_recording: PermissionStateResult,
    pub accessibility: PermissionStateResult,
    pub automation: PermissionStateResult,
    pub ready: bool,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub hints: Vec<String>,
}

impl From<&PermissionStatusSchema> for PermissionStatusResult {
    fn from(value: &PermissionStatusSchema) -> Self {
        Self {
            screen_recording: value.screen_recording.into(),
            accessibility: value.accessibility.into(),
            automation: value.automation.into(),
            ready: value.ready,
            hints: value.hints.clone(),
        }
    }
}

#[derive(Debug, Clone, Serialize)]
pub struct ActionMeta {
    pub action_id: String,
    pub elapsed_ms: u64,
    pub dry_run: bool,
    pub retries: u8,
    pub attempts_used: u8,
    pub timeout_ms: u64,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct ActionPolicyResult {
    pub dry_run: bool,
    pub retries: u8,
    pub retry_delay_ms: u64,
    pub timeout_ms: u64,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct WindowRow {
    pub window_id: u32,
    pub owner_name: String,
    pub window_title: String,
    pub x: i32,
    pub y: i32,
    pub width: i32,
    pub height: i32,
    pub on_screen: bool,
    pub active: bool,
    pub owner_pid: i32,
    pub z_order: usize,
}

impl From<&WindowInfo> for WindowRow {
    fn from(window: &WindowInfo) -> Self {
        Self {
            window_id: window.id,
            owner_name: window.owner_name.clone(),
            window_title: window.title.clone(),
            x: window.bounds.x,
            y: window.bounds.y,
            width: window.bounds.width,
            height: window.bounds.height,
            on_screen: window.on_screen,
            active: window.active,
            owner_pid: window.owner_pid,
            z_order: window.z_order,
        }
    }
}

impl WindowRow {
    pub fn tsv_line(&self) -> String {
        format!(
            "{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}",
            self.window_id,
            normalize_tsv_field(&self.owner_name),
            normalize_tsv_field(&self.window_title),
            self.x,
            self.y,
            self.width,
            self.height,
            if self.on_screen { "true" } else { "false" }
        )
    }
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct AppRow {
    pub app_name: String,
    pub pid: i32,
    pub bundle_id: String,
}

impl From<&AppInfo> for AppRow {
    fn from(app: &AppInfo) -> Self {
        Self {
            app_name: app.name.clone(),
            pid: app.pid,
            bundle_id: app.bundle_id.clone(),
        }
    }
}

impl AppRow {
    pub fn tsv_line(&self) -> String {
        format!(
            "{}\t{}\t{}",
            normalize_tsv_field(&self.app_name),
            self.pid,
            normalize_tsv_field(&self.bundle_id)
        )
    }
}

#[derive(Debug, Clone, Serialize)]
pub struct ListWindowsResult {
    pub windows: Vec<WindowRow>,
}

#[derive(Debug, Clone, Serialize)]
pub struct ListAppsResult {
    pub apps: Vec<AppRow>,
}

#[derive(Debug, Clone, Serialize)]
pub struct ScreenshotResult {
    pub path: String,
    pub target: WindowRow,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub selector: Option<ScreenshotSelectorResult>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub if_changed: Option<IfChangedResult>,
}

#[derive(Debug, Clone, Serialize)]
pub struct WaitResult {
    pub condition: &'static str,
    pub attempts: u32,
    pub elapsed_ms: u64,
    pub terminal_status: &'static str,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub matched_count: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub selector_explain: Option<AxSelectorExplain>,
}

#[derive(Debug, Clone, Serialize)]
pub struct ScreenshotSelectorResult {
    pub node_id: String,
    pub matched_count: usize,
    pub padding: i32,
    pub frame: AxFrame,
    pub capture_region: AxFrame,
}

#[derive(Debug, Clone, Serialize)]
pub struct IfChangedResult {
    pub changed: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub baseline_hash: Option<String>,
    pub current_hash: String,
    pub threshold: u32,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub captured_path: Option<String>,
}

#[derive(Debug, Clone, Serialize)]
pub struct WindowActivateResult {
    pub selected_app: String,
    pub selected_window_id: Option<u32>,
    pub wait_ms: Option<u64>,
    pub policy: ActionPolicyResult,
    pub meta: ActionMeta,
}

#[derive(Debug, Clone, Serialize)]
pub struct InputClickResult {
    pub x: i32,
    pub y: i32,
    pub button: &'static str,
    pub count: u8,
    pub policy: ActionPolicyResult,
    pub meta: ActionMeta,
}

#[derive(Debug, Clone, Serialize)]
pub struct InputTypeResult {
    pub text_length: usize,
    pub enter: bool,
    pub delay_ms: Option<u64>,
    pub policy: ActionPolicyResult,
    pub meta: ActionMeta,
}

#[derive(Debug, Clone, Serialize)]
pub struct InputHotkeyResult {
    pub mods: Vec<String>,
    pub key: String,
    pub policy: ActionPolicyResult,
    pub meta: ActionMeta,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
pub struct AxFrame {
    pub x: f64,
    pub y: f64,
    pub width: f64,
    pub height: f64,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
pub struct AxNode {
    pub node_id: String,
    pub role: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub subrole: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub identifier: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_preview: Option<String>,
    #[serde(default)]
    pub enabled: bool,
    #[serde(default)]
    pub focused: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub frame: Option<AxFrame>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub actions: Vec<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub path: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct AxTarget {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub session_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub app: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bundle_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub window_title_contains: Option<String>,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Default, ValueEnum)]
#[serde(rename_all = "kebab-case")]
#[value(rename_all = "kebab-case")]
pub enum AxMatchStrategy {
    #[default]
    Contains,
    Exact,
    Prefix,
    Suffix,
    Regex,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, ValueEnum)]
#[serde(rename_all = "kebab-case")]
#[value(rename_all = "kebab-case")]
pub enum AxClickFallbackStage {
    AxPress,
    AxConfirm,
    FrameCenter,
    Coordinate,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct AxSelectorExplainStage {
    pub stage: String,
    pub before_count: usize,
    pub after_count: usize,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct AxSelectorExplain {
    pub strategy: AxMatchStrategy,
    pub total_candidates: usize,
    pub matched_count: usize,
    pub selected_count: usize,
    pub stage_results: Vec<AxSelectorExplainStage>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub selected_node_id: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct AxSelector {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub node_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub role: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title_contains: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub identifier_contains: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_contains: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub subrole: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub focused: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub enabled: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub nth: Option<usize>,
    #[serde(default, skip_serializing_if = "is_match_strategy_contains")]
    pub match_strategy: AxMatchStrategy,
    #[serde(default, skip_serializing_if = "is_false")]
    pub explain: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct AxListRequest {
    #[serde(default)]
    pub target: AxTarget,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub role: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title_contains: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub identifier_contains: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_contains: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub subrole: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub focused: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub enabled: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_depth: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub limit: Option<usize>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct AxClickRequest {
    #[serde(default)]
    pub target: AxTarget,
    #[serde(default)]
    pub selector: AxSelector,
    #[serde(default)]
    pub allow_coordinate_fallback: bool,
    #[serde(default)]
    pub reselect_before_click: bool,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub fallback_order: Vec<AxClickFallbackStage>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct AxTypeRequest {
    #[serde(default)]
    pub target: AxTarget,
    #[serde(default)]
    pub selector: AxSelector,
    pub text: String,
    #[serde(default)]
    pub clear_first: bool,
    #[serde(default)]
    pub submit: bool,
    #[serde(default)]
    pub paste: bool,
    #[serde(default)]
    pub allow_keyboard_fallback: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
pub struct AxListResult {
    pub nodes: Vec<AxNode>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub warnings: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
pub struct AxClickResult {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub node_id: Option<String>,
    pub matched_count: usize,
    pub action: String,
    #[serde(default)]
    pub used_coordinate_fallback: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fallback_x: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fallback_y: Option<i32>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub fallback_order: Vec<AxClickFallbackStage>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub attempted_stages: Vec<AxClickFallbackStage>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub selector_explain: Option<AxSelectorExplain>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub gates: Option<AxGateResult>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub postconditions: Option<AxPostconditionResult>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
pub struct AxTypeResult {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub node_id: Option<String>,
    pub matched_count: usize,
    pub applied_via: String,
    pub text_length: usize,
    #[serde(default)]
    pub submitted: bool,
    #[serde(default)]
    pub used_keyboard_fallback: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub selector_explain: Option<AxSelectorExplain>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub gates: Option<AxGateResult>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub postconditions: Option<AxPostconditionResult>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct AxGateCheckResult {
    pub gate: String,
    pub terminal_status: String,
    pub attempts: u32,
    pub elapsed_ms: u64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub matched_count: Option<usize>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct AxGateResult {
    pub timeout_ms: u64,
    pub poll_ms: u64,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub checks: Vec<AxGateCheckResult>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
pub struct AxPostconditionCheckResult {
    pub check: String,
    pub terminal_status: String,
    pub attempts: u32,
    pub elapsed_ms: u64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub attribute: Option<String>,
    pub expected: Value,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub observed: Option<Value>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
pub struct AxPostconditionResult {
    pub timeout_ms: u64,
    pub poll_ms: u64,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub checks: Vec<AxPostconditionCheckResult>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct DebugBundleArtifactEntry {
    pub id: String,
    pub path: String,
    pub ok: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct DebugBundleResult {
    pub output_dir: String,
    pub artifact_index_path: String,
    pub partial_failure: bool,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub artifacts: Vec<DebugBundleArtifactEntry>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct AxAttrGetRequest {
    #[serde(default)]
    pub target: AxTarget,
    #[serde(default)]
    pub selector: AxSelector,
    pub name: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct AxAttrSetRequest {
    #[serde(default)]
    pub target: AxTarget,
    #[serde(default)]
    pub selector: AxSelector,
    pub name: String,
    pub value: Value,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct AxActionPerformRequest {
    #[serde(default)]
    pub target: AxTarget,
    #[serde(default)]
    pub selector: AxSelector,
    pub name: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct AxAttrGetResult {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub node_id: Option<String>,
    pub matched_count: usize,
    pub name: String,
    pub value: Value,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct AxAttrSetResult {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub node_id: Option<String>,
    pub matched_count: usize,
    pub name: String,
    pub applied: bool,
    pub value_type: String,
}

#[derive(Debug, Clone, Serialize)]
pub struct AxAttrSetCommandResult {
    #[serde(flatten)]
    pub detail: AxAttrSetResult,
    pub policy: ActionPolicyResult,
    pub meta: ActionMeta,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct AxActionPerformResult {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub node_id: Option<String>,
    pub matched_count: usize,
    pub name: String,
    pub performed: bool,
}

#[derive(Debug, Clone, Serialize)]
pub struct AxActionPerformCommandResult {
    #[serde(flatten)]
    pub detail: AxActionPerformResult,
    pub policy: ActionPolicyResult,
    pub meta: ActionMeta,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct AxSessionStartRequest {
    #[serde(default)]
    pub target: AxTarget,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub session_id: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct AxSessionStopRequest {
    pub session_id: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct AxSessionInfo {
    pub session_id: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub app: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bundle_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pid: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub window_title_contains: Option<String>,
    pub created_at_ms: u64,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct AxSessionStartResult {
    #[serde(flatten)]
    pub session: AxSessionInfo,
    pub created: bool,
}

#[derive(Debug, Clone, Serialize)]
pub struct AxSessionStartCommandResult {
    #[serde(flatten)]
    pub detail: AxSessionStartResult,
    pub policy: ActionPolicyResult,
    pub meta: ActionMeta,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct AxSessionListResult {
    pub sessions: Vec<AxSessionInfo>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct AxSessionStopResult {
    pub session_id: String,
    pub removed: bool,
}

#[derive(Debug, Clone, Serialize)]
pub struct AxSessionStopCommandResult {
    #[serde(flatten)]
    pub detail: AxSessionStopResult,
    pub policy: ActionPolicyResult,
    pub meta: ActionMeta,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct AxWatchStartRequest {
    pub session_id: String,
    #[serde(default)]
    pub events: Vec<String>,
    #[serde(default)]
    pub max_buffer: usize,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub watch_id: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct AxWatchPollRequest {
    pub watch_id: String,
    #[serde(default)]
    pub limit: usize,
    #[serde(default)]
    pub drain: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct AxWatchStopRequest {
    pub watch_id: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct AxWatchEvent {
    pub watch_id: String,
    pub event: String,
    pub at_ms: u64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub role: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub identifier: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pid: Option<i32>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct AxWatchStartResult {
    pub watch_id: String,
    pub session_id: String,
    pub events: Vec<String>,
    pub max_buffer: usize,
    pub started: bool,
}

#[derive(Debug, Clone, Serialize)]
pub struct AxWatchStartCommandResult {
    #[serde(flatten)]
    pub detail: AxWatchStartResult,
    pub policy: ActionPolicyResult,
    pub meta: ActionMeta,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct AxWatchPollResult {
    pub watch_id: String,
    pub events: Vec<AxWatchEvent>,
    pub dropped: usize,
    pub running: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct AxWatchStopResult {
    pub watch_id: String,
    pub stopped: bool,
    pub drained: usize,
}

#[derive(Debug, Clone, Serialize)]
pub struct AxWatchStopCommandResult {
    #[serde(flatten)]
    pub detail: AxWatchStopResult,
    pub policy: ActionPolicyResult,
    pub meta: ActionMeta,
}

#[derive(Debug, Clone, Serialize)]
pub struct AxClickCommandResult {
    #[serde(flatten)]
    pub detail: AxClickResult,
    pub policy: ActionPolicyResult,
    pub meta: ActionMeta,
}

#[derive(Debug, Clone, Serialize)]
pub struct AxTypeCommandResult {
    #[serde(flatten)]
    pub detail: AxTypeResult,
    pub policy: ActionPolicyResult,
    pub meta: ActionMeta,
}

#[derive(Debug, Clone, Serialize)]
pub struct ScenarioStepResult {
    pub step_id: String,
    pub ok: bool,
    pub exit_code: i32,
    pub elapsed_ms: u64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub operation: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ax_path: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fallback_used: Option<bool>,
    #[serde(skip_serializing_if = "String::is_empty")]
    pub stdout: String,
    #[serde(skip_serializing_if = "String::is_empty")]
    pub stderr: String,
}

#[derive(Debug, Clone, Serialize)]
pub struct ScenarioRunResult {
    pub file: String,
    pub total_steps: usize,
    pub passed_steps: usize,
    pub failed_steps: usize,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub first_failed_step_id: Option<String>,
    pub steps: Vec<ScenarioStepResult>,
}

#[derive(Debug, Clone, Serialize)]
pub struct ProfileValidateResult {
    pub file: String,
    pub valid: bool,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub issues: Vec<String>,
}

#[derive(Debug, Clone, Serialize)]
pub struct ProfileInitResult {
    pub path: String,
    pub profile_name: String,
}

#[derive(Debug, Clone, Serialize)]
pub struct InputSourceCurrentResult {
    pub current: String,
}

#[derive(Debug, Clone, Serialize)]
pub struct InputSourceSwitchResult {
    pub previous: String,
    pub current: String,
    pub switched: bool,
}

fn normalize_tsv_field(value: &str) -> String {
    value
        .chars()
        .map(|ch| {
            if ch == '\t' || ch == '\n' || ch == '\r' {
                ' '
            } else {
                ch
            }
        })
        .collect()
}

fn is_false(value: &bool) -> bool {
    !*value
}

fn is_match_strategy_contains(value: &AxMatchStrategy) -> bool {
    *value == AxMatchStrategy::Contains
}

#[cfg(test)]
mod tests {
    use pretty_assertions::assert_eq;

    use super::normalize_tsv_field;

    #[test]
    fn normalize_tsv_field_replaces_control_whitespace() {
        assert_eq!(normalize_tsv_field("A\tB\nC\rD"), "A B C D");
    }
}