spec-ai 0.6.12

A framework for building AI agents with structured outputs, policy enforcement, and execution tracking
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
//! Input widget for text entry

use crate::spec_ai_tui::buffer::Buffer;
use crate::spec_ai_tui::geometry::Rect;
use crate::spec_ai_tui::style::{Color, Style};
use crate::spec_ai_tui::widget::StatefulWidget;

/// State for input widget
#[derive(Debug, Clone, Default)]
pub struct InputState {
    /// Current input text
    pub value: String,
    /// Cursor position (byte index)
    pub cursor: usize,
    /// Scroll offset for long text
    pub scroll: usize,
    /// Whether the input has focus
    pub focused: bool,
}

impl InputState {
    /// Create a new empty input state
    pub fn new() -> Self {
        Self {
            value: String::new(),
            cursor: 0,
            scroll: 0,
            focused: true,
        }
    }

    /// Create input state with initial value
    pub fn with_value<S: Into<String>>(value: S) -> Self {
        let value = value.into();
        let cursor = value.len();
        Self {
            value,
            cursor,
            scroll: 0,
            focused: true,
        }
    }

    /// Get the current value
    pub fn value(&self) -> &str {
        &self.value
    }

    /// Set the value (and reset cursor to end)
    pub fn set_value<S: Into<String>>(&mut self, value: S) {
        self.value = value.into();
        self.cursor = self.value.len();
        self.scroll = 0;
    }

    /// Insert a character at the cursor position
    pub fn insert(&mut self, c: char) {
        self.value.insert(self.cursor, c);
        self.cursor += c.len_utf8();
    }

    /// Insert a string at the cursor position
    pub fn insert_str(&mut self, s: &str) {
        self.value.insert_str(self.cursor, s);
        self.cursor += s.len();
    }

    /// Delete character before cursor (backspace)
    pub fn backspace(&mut self) {
        if self.cursor > 0 {
            // Find the previous character boundary
            let prev = self.value[..self.cursor]
                .char_indices()
                .last()
                .map(|(i, _)| i)
                .unwrap_or(0);
            self.value.remove(prev);
            self.cursor = prev;
        }
    }

    /// Delete character at cursor (delete)
    pub fn delete(&mut self) {
        if self.cursor < self.value.len() {
            self.value.remove(self.cursor);
        }
    }

    /// Move cursor left by one character
    pub fn move_left(&mut self) {
        if self.cursor > 0 {
            self.cursor = self.value[..self.cursor]
                .char_indices()
                .last()
                .map(|(i, _)| i)
                .unwrap_or(0);
        }
    }

    /// Move cursor right by one character
    pub fn move_right(&mut self) {
        if self.cursor < self.value.len() {
            self.cursor = self.value[self.cursor..]
                .char_indices()
                .nth(1)
                .map(|(i, _)| self.cursor + i)
                .unwrap_or(self.value.len());
        }
    }

    /// Move cursor to start of line
    pub fn move_home(&mut self) {
        self.cursor = 0;
    }

    /// Move cursor to end of line
    pub fn move_end(&mut self) {
        self.cursor = self.value.len();
    }

    /// Move cursor left by one word
    pub fn move_word_left(&mut self) {
        if self.cursor == 0 {
            return;
        }

        // Skip whitespace
        let before_cursor = &self.value[..self.cursor];
        let trimmed_end = before_cursor.trim_end();

        if trimmed_end.is_empty() {
            self.cursor = 0;
            return;
        }

        // Find word boundary
        self.cursor = trimmed_end
            .rfind(|c: char| c.is_whitespace())
            .map(|i| i + 1)
            .unwrap_or(0);
    }

    /// Move cursor right by one word
    pub fn move_word_right(&mut self) {
        if self.cursor >= self.value.len() {
            return;
        }

        let after_cursor = &self.value[self.cursor..];

        // Skip current word
        let skip_word = after_cursor
            .find(|c: char| c.is_whitespace())
            .unwrap_or(after_cursor.len());

        // Skip whitespace
        let skip_space = after_cursor[skip_word..]
            .find(|c: char| !c.is_whitespace())
            .unwrap_or(after_cursor.len() - skip_word);

        self.cursor = self.cursor + skip_word + skip_space;
    }

    /// Clear the input
    pub fn clear(&mut self) {
        self.value.clear();
        self.cursor = 0;
        self.scroll = 0;
    }

    /// Take the value and clear the state
    pub fn take(&mut self) -> String {
        let value = std::mem::take(&mut self.value);
        self.cursor = 0;
        self.scroll = 0;
        value
    }

    /// Get the cursor position in characters (not bytes)
    pub fn cursor_char_pos(&self) -> usize {
        self.value[..self.cursor].chars().count()
    }
}

/// Input widget for text entry
#[derive(Debug, Clone, Default)]
pub struct Input {
    /// Base style for the input
    style: Style,
    /// Style for the cursor
    cursor_style: Style,
    /// Placeholder text when empty
    placeholder: Option<String>,
    /// Placeholder style
    placeholder_style: Style,
    /// Mask character for passwords
    mask: Option<char>,
}

impl Input {
    /// Create a new input widget
    pub fn new() -> Self {
        Self {
            style: Style::default(),
            cursor_style: Style::new().bg(Color::White).fg(Color::Black),
            placeholder: None,
            placeholder_style: Style::new().fg(Color::DarkGrey),
            mask: None,
        }
    }

    /// Set the base style
    pub fn style(mut self, style: Style) -> Self {
        self.style = style;
        self
    }

    /// Set the cursor style
    pub fn cursor_style(mut self, style: Style) -> Self {
        self.cursor_style = style;
        self
    }

    /// Set placeholder text
    pub fn placeholder<S: Into<String>>(mut self, placeholder: S) -> Self {
        self.placeholder = Some(placeholder.into());
        self
    }

    /// Set placeholder style
    pub fn placeholder_style(mut self, style: Style) -> Self {
        self.placeholder_style = style;
        self
    }

    /// Enable password mode (mask with asterisks)
    pub fn password(mut self) -> Self {
        self.mask = Some('*');
        self
    }

    /// Set a custom mask character
    pub fn mask(mut self, c: char) -> Self {
        self.mask = Some(c);
        self
    }
}

impl StatefulWidget for Input {
    type State = InputState;

    fn render(&self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
        if area.is_empty() {
            return;
        }

        let width = area.width as usize;

        // Determine what to display
        let (display_text, display_style) = if state.value.is_empty() {
            // Show placeholder
            (
                self.placeholder.clone().unwrap_or_default(),
                self.placeholder_style,
            )
        } else if let Some(mask_char) = self.mask {
            // Show masked text
            (
                mask_char.to_string().repeat(state.value.chars().count()),
                self.style,
            )
        } else {
            // Show actual text
            (state.value.clone(), self.style)
        };

        // Calculate cursor position in characters
        let cursor_pos = if state.value.is_empty() {
            0
        } else if self.mask.is_some() {
            state.value[..state.cursor].chars().count()
        } else {
            unicode_width::UnicodeWidthStr::width(&state.value[..state.cursor])
        };

        // Adjust scroll to keep cursor visible
        if cursor_pos < state.scroll {
            state.scroll = cursor_pos;
        } else if cursor_pos >= state.scroll + width {
            state.scroll = cursor_pos - width + 1;
        }

        // Render the visible portion
        let display_chars: Vec<char> = display_text.chars().collect();
        let visible_start = state.scroll.min(display_chars.len());
        let visible_end = (state.scroll + width).min(display_chars.len());

        let mut x = area.x;
        for (i, c) in display_chars[visible_start..visible_end].iter().enumerate() {
            if x >= area.right() {
                break;
            }

            let char_pos = visible_start + i;
            let is_cursor = state.focused && !state.value.is_empty() && char_pos == cursor_pos;

            let style = if is_cursor {
                self.cursor_style
            } else {
                display_style
            };

            if let Some(cell) = buf.get_mut(x, area.y) {
                cell.symbol = c.to_string();
                cell.fg = style.fg;
                cell.bg = style.bg;
                cell.modifier = style.modifier;
            }

            let char_width = unicode_width::UnicodeWidthChar::width(*c).unwrap_or(1);
            x = x.saturating_add(char_width as u16);
        }

        // Draw cursor at end if cursor is at the end of text
        if state.focused && (state.value.is_empty() || cursor_pos >= display_chars.len()) {
            let cursor_x = area.x + (cursor_pos - state.scroll).min(width - 1) as u16;
            if cursor_x < area.right() {
                if let Some(cell) = buf.get_mut(cursor_x, area.y) {
                    if cell.symbol == " " || display_text.is_empty() {
                        cell.symbol = " ".to_string();
                    }
                    cell.fg = self.cursor_style.fg;
                    cell.bg = self.cursor_style.bg;
                    cell.modifier = self.cursor_style.modifier;
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_input_state_new() {
        let state = InputState::new();
        assert!(state.value.is_empty());
        assert_eq!(state.cursor, 0);
    }

    #[test]
    fn test_input_state_insert() {
        let mut state = InputState::new();
        state.insert('H');
        state.insert('i');

        assert_eq!(state.value, "Hi");
        assert_eq!(state.cursor, 2);
    }

    #[test]
    fn test_input_state_backspace() {
        let mut state = InputState::with_value("Hello");
        state.backspace();

        assert_eq!(state.value, "Hell");
        assert_eq!(state.cursor, 4);
    }

    #[test]
    fn test_input_state_move() {
        let mut state = InputState::with_value("Hello");
        assert_eq!(state.cursor, 5);

        state.move_left();
        assert_eq!(state.cursor, 4);

        state.move_home();
        assert_eq!(state.cursor, 0);

        state.move_right();
        assert_eq!(state.cursor, 1);

        state.move_end();
        assert_eq!(state.cursor, 5);
    }

    #[test]
    fn test_input_state_unicode() {
        let mut state = InputState::with_value("日本語");
        assert_eq!(state.cursor, 9); // 3 chars * 3 bytes each

        state.move_left();
        assert_eq!(state.cursor, 6); // After "日本"

        state.move_left();
        assert_eq!(state.cursor, 3); // After "日"
    }

    #[test]
    fn test_input_state_take() {
        let mut state = InputState::with_value("Hello");
        let value = state.take();

        assert_eq!(value, "Hello");
        assert!(state.value.is_empty());
        assert_eq!(state.cursor, 0);
    }

    #[test]
    fn test_input_render() {
        let input = Input::new();
        let area = Rect::new(0, 0, 20, 1);
        let mut buf = Buffer::new(area);
        let mut state = InputState::with_value("Hello");

        input.render(area, &mut buf, &mut state);

        assert_eq!(buf.get(0, 0).unwrap().symbol, "H");
        assert_eq!(buf.get(4, 0).unwrap().symbol, "o");
    }
}