aga 2.0.0

AgenticGraphicsAcceleration — standalone agentic-first GPU rendering backend; wgpu replacement with Vulkan, OpenGL, and complete ontology
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
//! List and Table widgets with virtualized scrolling support.

use crate::core::{Color, Position, Rect, TextStyle};
use crate::ontology::{
    AgentAction, AgentCapability, Discoverable, SemanticRole, UiNode, WidgetSchema,
};
use crate::paint::Painter;
use crate::widget::Widget;

/// A scrollable list of items.
pub struct List {
    pub id: String,
    pub items: Vec<String>,
    pub selected: Option<usize>,
    pub scroll_offset: f32,
    pub item_height: f32,
    bg_color: Option<Color>,
    fg_color: Option<Color>,
    corner_radius: Option<f32>,
    font_size: Option<f32>,
    is_bold: bool,
}

impl List {
    #[must_use]
    pub fn new(id: impl Into<String>, items: Vec<String>) -> Self {
        Self {
            id: id.into(),
            items,
            selected: None,
            scroll_offset: 0.0,
            item_height: 28.0,
            bg_color: None,
            fg_color: None,
            corner_radius: None,
            font_size: None,
            is_bold: false,
        }
    }

    #[must_use]
    pub fn selected(mut self, index: usize) -> Self {
        self.selected = Some(index);
        self
    }

    #[must_use]
    pub fn bg(mut self, color: Color) -> Self {
        self.bg_color = Some(color);
        self
    }

    #[must_use]
    pub fn fg(mut self, color: Color) -> Self {
        self.fg_color = Some(color);
        self
    }

    #[must_use]
    pub fn rounded(mut self, radius: f32) -> Self {
        self.corner_radius = Some(radius);
        self
    }

    #[must_use]
    pub fn text_size(mut self, size: f32) -> Self {
        self.font_size = Some(size);
        self
    }

    #[must_use]
    pub fn bold(mut self) -> Self {
        self.is_bold = true;
        self
    }
}

impl Widget for List {
    fn draw(&self, painter: &mut dyn Painter, area: Rect) {
        let bg = self.bg_color.unwrap_or(Color::rgba(0.12, 0.12, 0.15, 1.0));
        let radius = self.corner_radius.unwrap_or(3.0);
        painter.fill_rect(area, bg, radius);

        let text_color = self.fg_color.unwrap_or(Color::WHITE);
        let style = TextStyle {
            font_size: self.font_size.unwrap_or(14.0),
            color: text_color,
            ..TextStyle::default()
        };

        let padding = 6.0;
        let visible_start = (self.scroll_offset / self.item_height).floor() as usize;
        let visible_count = (area.height / self.item_height).ceil() as usize + 1;

        for i in visible_start..self.items.len().min(visible_start + visible_count) {
            let y = area.y + i as f32 * self.item_height - self.scroll_offset;
            if y + self.item_height < area.y || y > area.y + area.height {
                continue;
            }

            let item_rect = Rect::new(area.x, y, area.width, self.item_height);

            if self.selected == Some(i) {
                painter.fill_rect(item_rect, Color::rgba(0.2, 0.4, 0.7, 0.5), 0.0);
            }

            let text_color = if self.selected == Some(i) {
                Color::WHITE
            } else {
                style.color
            };

            let text_style = TextStyle {
                color: text_color,
                ..style.clone()
            };

            painter.text(
                Position::new(
                    area.x + padding,
                    y + (self.item_height - style.font_size) * 0.5,
                ),
                &self.items[i],
                &text_style,
            );
        }
    }

    fn ui_node(&self) -> UiNode {
        UiNode::new("List", SemanticRole::Selection).with_id(&self.id)
    }
}

impl Discoverable for List {
    fn schema(&self) -> WidgetSchema {
        WidgetSchema::new(
            "List",
            "A scrollable list of items",
            SemanticRole::Selection,
        )
    }

    fn capabilities(&self) -> Vec<AgentCapability> {
        vec![
            AgentCapability::Focusable,
            AgentCapability::Scrollable {
                vertical: true,
                horizontal: false,
            },
            AgentCapability::Selectable {
                multi_select: false,
                item_count: self.items.len(),
            },
        ]
    }

    fn actions(&self) -> Vec<AgentAction> {
        vec![
            AgentAction::simple("select", "Select an item by index", true),
            AgentAction::simple("scroll", "Scroll the list", true),
        ]
    }

    fn semantic_role(&self) -> SemanticRole {
        SemanticRole::Selection
    }

    fn agent_state(&self) -> serde_json::Value {
        serde_json::json!({
            "item_count": self.items.len(),
            "selected": self.selected,
        })
    }

    fn execute_action(
        &mut self,
        action: &str,
        params: &serde_json::Value,
    ) -> Result<serde_json::Value, String> {
        match action {
            "select" => {
                if let Some(idx) = params.get("index").and_then(|v| v.as_u64()) {
                    let idx = idx as usize;
                    if idx < self.items.len() {
                        self.selected = Some(idx);
                        Ok(serde_json::json!({ "selected": idx }))
                    } else {
                        Err("Index out of range".into())
                    }
                } else {
                    Err("Missing 'index' parameter".into())
                }
            }
            _ => Err(format!("Unknown action: {action}")),
        }
    }

    fn agent_id(&self) -> Option<&str> {
        Some(&self.id)
    }
}

/// A data table with column headers and rows.
pub struct Table {
    pub id: String,
    pub columns: Vec<String>,
    pub rows: Vec<Vec<String>>,
    pub selected_row: Option<usize>,
    pub scroll_offset: f32,
    pub row_height: f32,
    pub header_height: f32,
    bg_color: Option<Color>,
    fg_color: Option<Color>,
    corner_radius: Option<f32>,
    font_size: Option<f32>,
    is_bold: bool,
}

impl Table {
    #[must_use]
    pub fn new(id: impl Into<String>, columns: Vec<String>, rows: Vec<Vec<String>>) -> Self {
        Self {
            id: id.into(),
            columns,
            rows,
            selected_row: None,
            scroll_offset: 0.0,
            row_height: 28.0,
            header_height: 32.0,
            bg_color: None,
            fg_color: None,
            corner_radius: None,
            font_size: None,
            is_bold: false,
        }
    }

    #[must_use]
    pub fn bg(mut self, color: Color) -> Self {
        self.bg_color = Some(color);
        self
    }

    #[must_use]
    pub fn fg(mut self, color: Color) -> Self {
        self.fg_color = Some(color);
        self
    }

    #[must_use]
    pub fn rounded(mut self, radius: f32) -> Self {
        self.corner_radius = Some(radius);
        self
    }

    #[must_use]
    pub fn text_size(mut self, size: f32) -> Self {
        self.font_size = Some(size);
        self
    }

    #[must_use]
    pub fn bold(mut self) -> Self {
        self.is_bold = true;
        self
    }
}

impl Widget for Table {
    fn draw(&self, painter: &mut dyn Painter, area: Rect) {
        let bg = self.bg_color.unwrap_or(Color::rgba(0.1, 0.1, 0.13, 1.0));
        let radius = self.corner_radius.unwrap_or(3.0);
        painter.fill_rect(area, bg, radius);

        let col_count = self.columns.len().max(1);
        let col_width = area.width / col_count as f32;
        let fs = self.font_size.unwrap_or(13.0);

        let header_style = TextStyle {
            font_size: fs,
            color: self.fg_color.unwrap_or(Color::rgba(0.7, 0.7, 0.8, 1.0)),
            ..TextStyle::default()
        };

        // Header
        let header_rect = Rect::new(area.x, area.y, area.width, self.header_height);
        painter.fill_rect(header_rect, Color::rgba(0.15, 0.15, 0.2, 1.0), 0.0);

        for (i, col) in self.columns.iter().enumerate() {
            painter.text(
                Position::new(
                    area.x + i as f32 * col_width + 6.0,
                    area.y + (self.header_height - header_style.font_size) * 0.5,
                ),
                col,
                &header_style,
            );
        }

        // Rows
        let row_style = TextStyle {
            font_size: fs,
            color: self.fg_color.unwrap_or(Color::WHITE),
            ..TextStyle::default()
        };

        let body_y = area.y + self.header_height;
        for (ri, row) in self.rows.iter().enumerate() {
            let y = body_y + ri as f32 * self.row_height - self.scroll_offset;
            if y + self.row_height < body_y || y > area.y + area.height {
                continue;
            }

            if self.selected_row == Some(ri) {
                let row_rect = Rect::new(area.x, y, area.width, self.row_height);
                painter.fill_rect(row_rect, Color::rgba(0.2, 0.4, 0.7, 0.4), 0.0);
            }

            for (ci, cell) in row.iter().enumerate().take(col_count) {
                painter.text(
                    Position::new(
                        area.x + ci as f32 * col_width + 6.0,
                        y + (self.row_height - row_style.font_size) * 0.5,
                    ),
                    cell,
                    &row_style,
                );
            }
        }
    }

    fn ui_node(&self) -> UiNode {
        UiNode::new("Table", SemanticRole::DataVisualization).with_id(&self.id)
    }
}

impl Discoverable for Table {
    fn schema(&self) -> WidgetSchema {
        WidgetSchema::new(
            "Table",
            "A data table with headers and rows",
            SemanticRole::DataVisualization,
        )
    }

    fn capabilities(&self) -> Vec<AgentCapability> {
        vec![
            AgentCapability::Focusable,
            AgentCapability::Scrollable {
                vertical: true,
                horizontal: false,
            },
            AgentCapability::Selectable {
                multi_select: false,
                item_count: self.rows.len(),
            },
            AgentCapability::Sortable {
                columns: self.columns.clone(),
            },
        ]
    }

    fn actions(&self) -> Vec<AgentAction> {
        vec![
            AgentAction::simple("select_row", "Select a table row", true),
            AgentAction::simple("scroll", "Scroll the table", true),
            AgentAction::simple("sort", "Sort by column", false),
        ]
    }

    fn semantic_role(&self) -> SemanticRole {
        SemanticRole::DataVisualization
    }

    fn agent_state(&self) -> serde_json::Value {
        serde_json::json!({
            "columns": self.columns,
            "row_count": self.rows.len(),
            "selected_row": self.selected_row,
        })
    }

    fn execute_action(
        &mut self,
        action: &str,
        params: &serde_json::Value,
    ) -> Result<serde_json::Value, String> {
        match action {
            "select_row" => {
                if let Some(idx) = params.get("index").and_then(|v| v.as_u64()) {
                    let idx = idx as usize;
                    if idx < self.rows.len() {
                        self.selected_row = Some(idx);
                        Ok(serde_json::json!({ "selected_row": idx }))
                    } else {
                        Err("Index out of range".into())
                    }
                } else {
                    Err("Missing 'index' parameter".into())
                }
            }
            _ => Err(format!("Unknown action: {action}")),
        }
    }

    fn agent_id(&self) -> Option<&str> {
        Some(&self.id)
    }
}