qontinui-types 0.6.0

Canonical DTO types for Qontinui. Rust is the source of truth; TypeScript and Python are generated from JSON Schema emitted by schemars.
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
//! Target configuration models for action targeting.
//!
//! Mirrors `src/qontinui_schemas/config/models/targets.py` and the
//! `search.py` helpers it references. Rust is the source of truth; TS
//! and Python bindings regenerate from the JSON Schemas emitted here.
//!
//! The `TargetConfig` discriminated union carries 14 target variants — the
//! wire format uses an internal `type` tag in camelCase ("stateImage",
//! "currentPosition", "resultByImage", etc.) to match the hand-authored
//! Python enums and the existing TS consumers.
//!
//! Field names are wire-preserved via `#[serde(rename = "...")]` where the
//! Python side declares camelCase aliases (e.g. `imageIds`, `searchOptions`,
//! `captureFirst`, `cdpHost`).

use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::geometry::{Coordinates, Region};

// ============================================================================
// Supporting enums
// ============================================================================

/// Strategy for selecting among multiple matches in a FIND action.
///
/// Variants serialize uppercase (`"FIRST"`, `"ALL"`, `"BEST"`, `"EACH"`) to
/// match the existing Python enum and all stored action configs.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub enum SearchStrategy {
    #[serde(rename = "FIRST")]
    First,
    #[serde(rename = "ALL")]
    All,
    #[serde(rename = "BEST")]
    Best,
    #[serde(rename = "EACH")]
    Each,
}

// ============================================================================
// Search helpers (SearchOptions / TextSearchOptions / PatternOptions /
// MatchAdjustment / PollingConfig)
// ============================================================================

/// Polling configuration for search operations.
#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
#[schemars(deny_unknown_fields)]
pub struct PollingConfig {
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "interval")]
    pub interval: Option<i64>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "maxAttempts",
        alias = "max_attempts"
    )]
    pub max_attempts: Option<i64>,
}

/// Template matching method used by OpenCV-style correlation search.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub enum MatchMethod {
    #[serde(rename = "CORRELATION")]
    Correlation,
    #[serde(rename = "CORRELATION_NORMED")]
    CorrelationNormed,
    #[serde(rename = "SQUARED_DIFFERENCE")]
    SquaredDifference,
    #[serde(rename = "SQUARED_DIFFERENCE_NORMED")]
    SquaredDifferenceNormed,
}

/// Advanced pattern-matching options.
#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
#[schemars(deny_unknown_fields)]
pub struct PatternOptions {
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "matchMethod",
        alias = "match_method"
    )]
    pub match_method: Option<MatchMethod>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "scaleInvariant",
        alias = "scale_invariant"
    )]
    pub scale_invariant: Option<bool>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "rotationInvariant",
        alias = "rotation_invariant"
    )]
    pub rotation_invariant: Option<bool>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "minScale",
        alias = "min_scale"
    )]
    pub min_scale: Option<f64>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "maxScale",
        alias = "max_scale"
    )]
    pub max_scale: Option<f64>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "scaleStep",
        alias = "scale_step"
    )]
    pub scale_step: Option<f64>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "minRotation",
        alias = "min_rotation"
    )]
    pub min_rotation: Option<f64>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "maxRotation",
        alias = "max_rotation"
    )]
    pub max_rotation: Option<f64>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "rotationStep",
        alias = "rotation_step"
    )]
    pub rotation_step: Option<f64>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "useGrayscale",
        alias = "use_grayscale"
    )]
    pub use_grayscale: Option<bool>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "useColorReduction",
        alias = "use_color_reduction"
    )]
    pub use_color_reduction: Option<bool>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "colorTolerance",
        alias = "color_tolerance"
    )]
    pub color_tolerance: Option<f64>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "useEdges",
        alias = "use_edges"
    )]
    pub use_edges: Option<bool>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "edgeThreshold1",
        alias = "edge_threshold1"
    )]
    pub edge_threshold1: Option<f64>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "edgeThreshold2",
        alias = "edge_threshold2"
    )]
    pub edge_threshold2: Option<f64>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "nonMaxSuppression",
        alias = "non_max_suppression"
    )]
    pub non_max_suppression: Option<bool>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "nmsThreshold",
        alias = "nms_threshold"
    )]
    pub nms_threshold: Option<f64>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "minDistanceBetweenMatches",
        alias = "min_distance_between_matches"
    )]
    pub min_distance_between_matches: Option<f64>,
}

/// Match adjustment — modify the matched region before acting.
#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
#[schemars(deny_unknown_fields)]
pub struct MatchAdjustment {
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "targetPosition",
        alias = "target_position"
    )]
    pub target_position: Option<String>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "targetOffset",
        alias = "target_offset"
    )]
    pub target_offset: Option<Coordinates>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "addW",
        alias = "add_w"
    )]
    pub add_w: Option<i64>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "addH",
        alias = "add_h"
    )]
    pub add_h: Option<i64>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "absoluteW",
        alias = "absolute_w"
    )]
    pub absolute_w: Option<i64>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "absoluteH",
        alias = "absolute_h"
    )]
    pub absolute_h: Option<i64>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "addX",
        alias = "add_x"
    )]
    pub add_x: Option<i64>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "addY",
        alias = "add_y"
    )]
    pub add_y: Option<i64>,
}

/// Search options for target finding.
#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
#[schemars(deny_unknown_fields)]
pub struct SearchOptions {
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "similarity")]
    pub similarity: Option<f64>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "timeout")]
    pub timeout: Option<i64>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "searchRegions",
        alias = "search_regions"
    )]
    pub search_regions: Option<Vec<Region>>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "searchStrategy",
        alias = "strategy"
    )]
    pub strategy: Option<SearchStrategy>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "useDefinedRegion",
        alias = "use_defined_region"
    )]
    pub use_defined_region: Option<bool>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "maxMatchesToActOn",
        alias = "max_matches_to_act_on"
    )]
    pub max_matches_to_act_on: Option<i64>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "minMatches",
        alias = "min_matches"
    )]
    pub min_matches: Option<i64>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "maxMatches",
        alias = "max_matches"
    )]
    pub max_matches: Option<i64>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "polling")]
    pub polling: Option<PollingConfig>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "pattern")]
    pub pattern: Option<PatternOptions>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "adjustment")]
    pub adjustment: Option<MatchAdjustment>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "captureImage",
        alias = "capture_image"
    )]
    pub capture_image: Option<bool>,
}

/// OCR engine for text search.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub enum OcrEngine {
    #[serde(rename = "TESSERACT")]
    Tesseract,
    #[serde(rename = "EASYOCR")]
    EasyOcr,
    #[serde(rename = "PADDLEOCR")]
    PaddleOcr,
    #[serde(rename = "NATIVE")]
    Native,
}

/// Text-match comparison mode.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub enum TextMatchType {
    #[serde(rename = "EXACT")]
    Exact,
    #[serde(rename = "CONTAINS")]
    Contains,
    #[serde(rename = "STARTS_WITH")]
    StartsWith,
    #[serde(rename = "ENDS_WITH")]
    EndsWith,
    #[serde(rename = "REGEX")]
    Regex,
    #[serde(rename = "FUZZY")]
    Fuzzy,
}

/// Text search options for OCR-based finding.
#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
#[schemars(deny_unknown_fields)]
pub struct TextSearchOptions {
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "ocrEngine",
        alias = "ocr_engine"
    )]
    pub ocr_engine: Option<OcrEngine>,
    #[serde(default, skip_serializing_if = "Option::is_none", alias = "language")]
    pub language: Option<String>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "whitelistChars",
        alias = "whitelist_chars"
    )]
    pub whitelist_chars: Option<String>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "blacklistChars",
        alias = "blacklist_chars"
    )]
    pub blacklist_chars: Option<String>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "matchType",
        alias = "match_type"
    )]
    pub match_type: Option<TextMatchType>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "caseSensitive",
        alias = "case_sensitive"
    )]
    pub case_sensitive: Option<bool>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "ignoreWhitespace",
        alias = "ignore_whitespace"
    )]
    pub ignore_whitespace: Option<bool>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "normalizeUnicode",
        alias = "normalize_unicode"
    )]
    pub normalize_unicode: Option<bool>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "fuzzyThreshold",
        alias = "fuzzy_threshold"
    )]
    pub fuzzy_threshold: Option<f64>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "editDistance",
        alias = "edit_distance"
    )]
    pub edit_distance: Option<i64>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        alias = "preprocessing"
    )]
    pub preprocessing: Option<Vec<String>>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "scaleFactor",
        alias = "scale_factor"
    )]
    pub scale_factor: Option<f64>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "psmMode",
        alias = "psm_mode"
    )]
    pub psm_mode: Option<i64>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "oemMode",
        alias = "oem_mode"
    )]
    pub oem_mode: Option<i64>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        rename = "confidenceThreshold",
        alias = "confidence_threshold"
    )]
    pub confidence_threshold: Option<f64>,
}

// ============================================================================
// Role criterion for AccessibilityTarget — single or list of role strings
// ============================================================================

/// Role selector criterion — either a single role name or a list of roles.
/// Matches the Python `str | list[str] | None` type on
/// `AccessibilityTarget.role`.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(untagged)]
pub enum AccessibilityRoleCriterion {
    Single(String),
    Any(Vec<String>),
}

// ============================================================================
// TargetConfig variants — internally tagged by `type` (camelCase)
// ============================================================================

/// The discriminated union over all 14 target configurations. Wire format is
/// a flat object tagged by the `type` field (`"image"`, `"stateImage"`, …).
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "type")]
pub enum TargetConfig {
    /// Image target configuration supporting multiple images with search
    /// strategies. `imageIds` is required with at least one element — single
    /// image targets use a one-element list.
    #[serde(rename = "image")]
    Image {
        #[serde(rename = "imageIds")]
        image_ids: Vec<String>,
        #[serde(
            default,
            skip_serializing_if = "Option::is_none",
            rename = "searchOptions"
        )]
        search_options: Option<SearchOptions>,
    },

    /// Region target — a rectangular area of the screen.
    #[serde(rename = "region")]
    Region { region: Region },

    /// Text target — OCR-based targeting by visible text.
    #[serde(rename = "text")]
    Text {
        text: String,
        #[serde(
            default,
            skip_serializing_if = "Option::is_none",
            rename = "searchOptions"
        )]
        search_options: Option<SearchOptions>,
        #[serde(
            default,
            skip_serializing_if = "Option::is_none",
            rename = "textOptions"
        )]
        text_options: Option<TextSearchOptions>,
    },

    /// Absolute coordinates target.
    #[serde(rename = "coordinates")]
    Coordinates { coordinates: Coordinates },

    /// State-string target — references one or more strings in a state.
    #[serde(rename = "stateString")]
    StateString {
        #[serde(rename = "stateId")]
        state_id: String,
        #[serde(rename = "stringIds")]
        string_ids: Vec<String>,
        #[serde(default, skip_serializing_if = "Option::is_none", rename = "useAll")]
        use_all: Option<bool>,
    },

    /// Target a StateRegion by ID. Preserves the region's monitor
    /// association instead of using a global default.
    #[serde(rename = "stateRegion")]
    StateRegion {
        #[serde(rename = "regionId")]
        region_id: String,
    },

    /// Target a StateLocation by ID. Preserves the location's monitor
    /// association.
    #[serde(rename = "stateLocation")]
    StateLocation {
        #[serde(rename = "locationId")]
        location_id: String,
    },

    /// Target a StateImage by state ID + image IDs. Used by navigation to
    /// verify state via FIND on state-associated images.
    #[serde(rename = "stateImage")]
    StateImage {
        #[serde(rename = "stateId")]
        state_id: String,
        #[serde(rename = "imageIds")]
        image_ids: Vec<String>,
        #[serde(default, skip_serializing_if = "Option::is_none", rename = "stateName")]
        state_name: Option<String>,
        #[serde(
            default,
            skip_serializing_if = "Option::is_none",
            rename = "imageNames"
        )]
        image_names: Option<Vec<String>>,
    },

    /// Act at the current mouse position (pure action — no localization).
    #[serde(rename = "currentPosition")]
    CurrentPosition,

    /// Use the location from the most recent FIND action.
    #[serde(rename = "lastFindResult")]
    LastFindResult,

    /// Target a specific match (by zero-based index) from the last action
    /// result.
    #[serde(rename = "resultIndex")]
    ResultIndex {
        #[serde(default)]
        index: i64,
    },

    /// Target all matches from the last action result.
    #[serde(rename = "allResults")]
    AllResults,

    /// Target the match that came from a specific image ID in a multi-image
    /// FIND result (requires the previous FIND used `EACH`).
    #[serde(rename = "resultByImage")]
    ResultByImage {
        #[serde(rename = "imageId")]
        image_id: String,
    },

    /// Target an element by accessibility ref or selector — AI-optimized
    /// element selection via `@e1` refs or role/name matching. Requires a
    /// captured accessibility tree.
    #[serde(rename = "accessibility")]
    Accessibility {
        #[serde(default, skip_serializing_if = "Option::is_none")]
        r#ref: Option<String>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        role: Option<AccessibilityRoleCriterion>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        name: Option<String>,
        #[serde(
            default,
            skip_serializing_if = "Option::is_none",
            rename = "nameContains"
        )]
        name_contains: Option<String>,
        #[serde(
            default,
            skip_serializing_if = "Option::is_none",
            rename = "isInteractive"
        )]
        is_interactive: Option<bool>,
        #[serde(default = "default_true", rename = "captureFirst")]
        capture_first: bool,
        #[serde(default = "default_cdp_host", rename = "cdpHost")]
        cdp_host: String,
        #[serde(default = "default_cdp_port", rename = "cdpPort")]
        cdp_port: i64,
    },
}

fn default_true() -> bool {
    true
}
fn default_cdp_host() -> String {
    "localhost".to_string()
}
fn default_cdp_port() -> i64 {
    9222
}