Skip to main content

ContextContribution

Struct ContextContribution 

Source
pub struct ContextContribution {
    pub source_name: String,
    pub elements: Vec<ContextElement>,
    pub app: Option<String>,
    pub window: Option<String>,
    pub stream_status: StreamStatus,
}
Expand description

Generic source contribution accepted by ContextMerger.

Fields§

§source_name: String§elements: Vec<ContextElement>§app: Option<String>§window: Option<String>§stream_status: StreamStatus

Implementations§

Source§

impl ContextContribution

Source

pub fn new( source_name: impl Into<String>, elements: Vec<ContextElement>, ) -> Self

Examples found in repository?
examples/dump_context.rs (lines 12-37)
10fn main() {
11    let mut merger = ContextMerger::new().with_defaults("Example App", "Main");
12    merger.push(ContextContribution::new(
13        "test_source",
14        vec![ContextElement {
15            id: "button:submit".into(),
16            label: Some("Submit".into()),
17            description: None,
18            element_type: "button".into(),
19            value: None,
20            bounds: Some(Bounds {
21                x: 100,
22                y: 200,
23                width: 120,
24                height: 32,
25            }),
26            state: ElementState {
27                focused: true,
28                ..Default::default()
29            },
30            parent_id: None,
31            actions: Vec::new(),
32            confidence: 0.0,
33            source: ContextSource::NativeApi,
34            content_role: ContentRole::Interactive,
35            properties: Default::default(),
36        }],
37    ));
38
39    let ctx = merger.build();
40    println!("{}", serde_json::to_string_pretty(&ctx).unwrap());
41
42    let reference = ctx.elements[0].to_reference(1920, 1080);
43    let resolved = cel_context::resolve_reference(&ctx, &reference);
44    println!(
45        "resolved reference: {:?}",
46        resolved.map(|el| el.id.as_str())
47    );
48
49    let mut watchdog = ContextWatchdog::new();
50    assert!(watchdog.tick(&ctx, true).is_empty());
51
52    let mut changed = ctx.clone();
53    changed.elements[0].state.focused = false;
54    let events = watchdog.tick(&changed, true);
55    println!("watchdog events: {events:?}");
56}
More examples
Hide additional examples
examples/context_snapshot.rs (lines 19-42)
14fn main() {
15    let json_output = std::env::args().any(|arg| arg == "--json");
16
17    let mut merger = ContextMerger::new().with_defaults("Example CRM", "Customer Detail");
18    merger.push(
19        ContextContribution::new(
20            "crm_api",
21            vec![
22                element(
23                    "crm:title",
24                    "heading",
25                    "Customer: Acme Corp",
26                    ContextSource::NativeApi,
27                    None,
28                ),
29                element(
30                    "crm:button:save",
31                    "button",
32                    "Save changes",
33                    ContextSource::NativeApi,
34                    Some(Bounds {
35                        x: 720,
36                        y: 64,
37                        width: 128,
38                        height: 36,
39                    }),
40                ),
41            ],
42        )
43        .with_app("Example CRM")
44        .with_window("Customer Detail")
45        .with_stream_status(StreamStatus {
46            accessibility: false,
47            display: false,
48            network: false,
49            signals: false,
50            vision: false,
51            audio_capture: false,
52        }),
53    );
54    merger.push(ContextContribution::new(
55        "browser_dom",
56        vec![element(
57            "dom:input:notes",
58            "textbox",
59            "Internal notes",
60            ContextSource::Cdp,
61            Some(Bounds {
62                x: 160,
63                y: 420,
64                width: 520,
65                height: 120,
66            }),
67        )],
68    ));
69
70    let ctx = merger.build();
71
72    if json_output {
73        println!("{}", serde_json::to_string_pretty(&ctx).unwrap());
74        return;
75    }
76
77    println!("=== CEL Context Snapshot ===");
78    println!("App:       {}", ctx.app);
79    println!("Window:    {}", ctx.window);
80    println!("Timestamp: {} ms", ctx.timestamp_ms);
81    println!("Elements:  {}", ctx.elements.len());
82    println!();
83
84    for (i, elem) in ctx.elements.iter().enumerate() {
85        let label = elem.label.as_deref().unwrap_or("(no label)");
86        let bounds = elem
87            .bounds
88            .as_ref()
89            .map(|b| format!("[{},{} {}x{}]", b.x, b.y, b.width, b.height))
90            .unwrap_or_else(|| "(no bounds)".into());
91        println!(
92            "  {:>2}. [{:.2}] {:12} {:22} {} {:?}",
93            i + 1,
94            elem.confidence,
95            elem.element_type,
96            label,
97            bounds,
98            elem.source
99        );
100    }
101}
Source

pub fn with_app(self, app: impl Into<String>) -> Self

Examples found in repository?
examples/context_snapshot.rs (line 43)
14fn main() {
15    let json_output = std::env::args().any(|arg| arg == "--json");
16
17    let mut merger = ContextMerger::new().with_defaults("Example CRM", "Customer Detail");
18    merger.push(
19        ContextContribution::new(
20            "crm_api",
21            vec![
22                element(
23                    "crm:title",
24                    "heading",
25                    "Customer: Acme Corp",
26                    ContextSource::NativeApi,
27                    None,
28                ),
29                element(
30                    "crm:button:save",
31                    "button",
32                    "Save changes",
33                    ContextSource::NativeApi,
34                    Some(Bounds {
35                        x: 720,
36                        y: 64,
37                        width: 128,
38                        height: 36,
39                    }),
40                ),
41            ],
42        )
43        .with_app("Example CRM")
44        .with_window("Customer Detail")
45        .with_stream_status(StreamStatus {
46            accessibility: false,
47            display: false,
48            network: false,
49            signals: false,
50            vision: false,
51            audio_capture: false,
52        }),
53    );
54    merger.push(ContextContribution::new(
55        "browser_dom",
56        vec![element(
57            "dom:input:notes",
58            "textbox",
59            "Internal notes",
60            ContextSource::Cdp,
61            Some(Bounds {
62                x: 160,
63                y: 420,
64                width: 520,
65                height: 120,
66            }),
67        )],
68    ));
69
70    let ctx = merger.build();
71
72    if json_output {
73        println!("{}", serde_json::to_string_pretty(&ctx).unwrap());
74        return;
75    }
76
77    println!("=== CEL Context Snapshot ===");
78    println!("App:       {}", ctx.app);
79    println!("Window:    {}", ctx.window);
80    println!("Timestamp: {} ms", ctx.timestamp_ms);
81    println!("Elements:  {}", ctx.elements.len());
82    println!();
83
84    for (i, elem) in ctx.elements.iter().enumerate() {
85        let label = elem.label.as_deref().unwrap_or("(no label)");
86        let bounds = elem
87            .bounds
88            .as_ref()
89            .map(|b| format!("[{},{} {}x{}]", b.x, b.y, b.width, b.height))
90            .unwrap_or_else(|| "(no bounds)".into());
91        println!(
92            "  {:>2}. [{:.2}] {:12} {:22} {} {:?}",
93            i + 1,
94            elem.confidence,
95            elem.element_type,
96            label,
97            bounds,
98            elem.source
99        );
100    }
101}
Source

pub fn with_window(self, window: impl Into<String>) -> Self

Examples found in repository?
examples/context_snapshot.rs (line 44)
14fn main() {
15    let json_output = std::env::args().any(|arg| arg == "--json");
16
17    let mut merger = ContextMerger::new().with_defaults("Example CRM", "Customer Detail");
18    merger.push(
19        ContextContribution::new(
20            "crm_api",
21            vec![
22                element(
23                    "crm:title",
24                    "heading",
25                    "Customer: Acme Corp",
26                    ContextSource::NativeApi,
27                    None,
28                ),
29                element(
30                    "crm:button:save",
31                    "button",
32                    "Save changes",
33                    ContextSource::NativeApi,
34                    Some(Bounds {
35                        x: 720,
36                        y: 64,
37                        width: 128,
38                        height: 36,
39                    }),
40                ),
41            ],
42        )
43        .with_app("Example CRM")
44        .with_window("Customer Detail")
45        .with_stream_status(StreamStatus {
46            accessibility: false,
47            display: false,
48            network: false,
49            signals: false,
50            vision: false,
51            audio_capture: false,
52        }),
53    );
54    merger.push(ContextContribution::new(
55        "browser_dom",
56        vec![element(
57            "dom:input:notes",
58            "textbox",
59            "Internal notes",
60            ContextSource::Cdp,
61            Some(Bounds {
62                x: 160,
63                y: 420,
64                width: 520,
65                height: 120,
66            }),
67        )],
68    ));
69
70    let ctx = merger.build();
71
72    if json_output {
73        println!("{}", serde_json::to_string_pretty(&ctx).unwrap());
74        return;
75    }
76
77    println!("=== CEL Context Snapshot ===");
78    println!("App:       {}", ctx.app);
79    println!("Window:    {}", ctx.window);
80    println!("Timestamp: {} ms", ctx.timestamp_ms);
81    println!("Elements:  {}", ctx.elements.len());
82    println!();
83
84    for (i, elem) in ctx.elements.iter().enumerate() {
85        let label = elem.label.as_deref().unwrap_or("(no label)");
86        let bounds = elem
87            .bounds
88            .as_ref()
89            .map(|b| format!("[{},{} {}x{}]", b.x, b.y, b.width, b.height))
90            .unwrap_or_else(|| "(no bounds)".into());
91        println!(
92            "  {:>2}. [{:.2}] {:12} {:22} {} {:?}",
93            i + 1,
94            elem.confidence,
95            elem.element_type,
96            label,
97            bounds,
98            elem.source
99        );
100    }
101}
Source

pub fn with_stream_status(self, status: StreamStatus) -> Self

Examples found in repository?
examples/context_snapshot.rs (lines 45-52)
14fn main() {
15    let json_output = std::env::args().any(|arg| arg == "--json");
16
17    let mut merger = ContextMerger::new().with_defaults("Example CRM", "Customer Detail");
18    merger.push(
19        ContextContribution::new(
20            "crm_api",
21            vec![
22                element(
23                    "crm:title",
24                    "heading",
25                    "Customer: Acme Corp",
26                    ContextSource::NativeApi,
27                    None,
28                ),
29                element(
30                    "crm:button:save",
31                    "button",
32                    "Save changes",
33                    ContextSource::NativeApi,
34                    Some(Bounds {
35                        x: 720,
36                        y: 64,
37                        width: 128,
38                        height: 36,
39                    }),
40                ),
41            ],
42        )
43        .with_app("Example CRM")
44        .with_window("Customer Detail")
45        .with_stream_status(StreamStatus {
46            accessibility: false,
47            display: false,
48            network: false,
49            signals: false,
50            vision: false,
51            audio_capture: false,
52        }),
53    );
54    merger.push(ContextContribution::new(
55        "browser_dom",
56        vec![element(
57            "dom:input:notes",
58            "textbox",
59            "Internal notes",
60            ContextSource::Cdp,
61            Some(Bounds {
62                x: 160,
63                y: 420,
64                width: 520,
65                height: 120,
66            }),
67        )],
68    ));
69
70    let ctx = merger.build();
71
72    if json_output {
73        println!("{}", serde_json::to_string_pretty(&ctx).unwrap());
74        return;
75    }
76
77    println!("=== CEL Context Snapshot ===");
78    println!("App:       {}", ctx.app);
79    println!("Window:    {}", ctx.window);
80    println!("Timestamp: {} ms", ctx.timestamp_ms);
81    println!("Elements:  {}", ctx.elements.len());
82    println!();
83
84    for (i, elem) in ctx.elements.iter().enumerate() {
85        let label = elem.label.as_deref().unwrap_or("(no label)");
86        let bounds = elem
87            .bounds
88            .as_ref()
89            .map(|b| format!("[{},{} {}x{}]", b.x, b.y, b.width, b.height))
90            .unwrap_or_else(|| "(no bounds)".into());
91        println!(
92            "  {:>2}. [{:.2}] {:12} {:22} {} {:?}",
93            i + 1,
94            elem.confidence,
95            elem.element_type,
96            label,
97            bounds,
98            elem.source
99        );
100    }
101}

Trait Implementations§

Source§

impl Clone for ContextContribution

Source§

fn clone(&self) -> ContextContribution

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ContextContribution

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for ContextContribution

Source§

fn default() -> ContextContribution

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.