aethershell 11.0.1

The world's first multi-agent shell with typed functional pipelines and multi-modal AI
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
//! Runtime value model for Aether Shell.
//!
//! - Strong, ergonomic `Value` with helpers (`type_name`, accessors).
//! - Pretty-printing with color via `crossterm` (tables, records, arrays).
//! - A minimal `Uri` type + parser (scheme validation; not limited to HTTP).
//! - JSON interop helpers.

use std::collections::BTreeMap;
use std::fmt;

use serde::{Deserialize, Serialize};

use crate::ast::Expr;

/// A very small URI type (not tied to just URLs).
///
/// This is intentionally permissive but checks:
/// - a scheme that begins with an alphabetic char and continues with
/// - alphanumerics, '+', '-', or '.'
#[allow(dead_code)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct Uri {
    pub raw: String,
    pub scheme: String,
}

#[allow(dead_code)]
impl Uri {
    /// Returns `Some(Uri)` if `s` parses with a valid scheme (RFC3986-ish).
    pub fn parse(s: &str) -> Option<Self> {
        // scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) ":"
        let (scheme, _rest) = s.split_once(':')?;

        // must exist
        let mut chars = scheme.chars();
        let first = chars.next()?;
        if !first.is_ascii_alphabetic() {
            return None;
        }
        if !chars.all(|c| c.is_ascii_alphanumeric() || matches!(c, '+' | '-' | '.')) {
            return None;
        }
        Some(Self {
            raw: s.to_string(),
            scheme: scheme.to_string(),
        })
    }

    pub fn as_str(&self) -> &str {
        &self.raw
    }
}

impl fmt::Display for Uri {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.raw)
    }
}

/// A lambda closure.
///
/// `captured` holds the free variables of `body` — the names it uses that are
/// neither its own parameters nor resolvable as builtins — snapshotted at the
/// moment the lambda was created.
///
/// Without it a lambda closed over nothing: it was evaluated in the *caller's*
/// environment, so `fn(factor) => fn(x) => x * factor` lost `factor` the
/// instant the outer call returned (AS-2026-13). Capturing free variables
/// rather than a whole `Env` keeps this type serializable and comparable,
/// which an environment handle would not.
///
/// Only names that are actually bound at creation time are captured. A name
/// that is not yet defined stays a dynamic lookup, so the long-standing
/// behaviour of referring to a binding introduced later still works.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Lambda {
    pub params: Vec<String>,
    pub body: Box<Expr>,
    #[serde(default)]
    pub captured: BTreeMap<String, Value>,
}

/// An async lambda closure. See [`Lambda`] for what `captured` is for.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct AsyncLambda {
    pub params: Vec<String>,
    pub body: Box<Expr>,
    #[serde(default)]
    pub captured: BTreeMap<String, Value>,
}

/// A future value representing an async computation.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Future {
    /// The async lambda to execute
    pub lambda: AsyncLambda,
    /// Captured arguments
    pub args: Vec<Value>,
}

/// A simple table representation.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Table {
    pub rows: Vec<BTreeMap<String, Value>>,
    pub schema: Vec<String>,
}

/// A reference to a builtin function (for module system)
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct BuiltinRef {
    pub name: String,
}

/// The dynamic value space of the shell.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum Value {
    Null,
    Bool(bool),
    Int(i64),
    Float(f64),
    Str(String),
    Uri(String),
    Array(Vec<Value>),
    Record(BTreeMap<String, Value>),
    Table(Table),
    Lambda(Lambda),
    /// An async lambda (not yet called)
    AsyncLambda(AsyncLambda),
    /// A future value that can be awaited
    Future(Future),
    /// An error value for try/catch handling
    Error(String),
    /// A reference to a builtin function
    Builtin(BuiltinRef),
}

impl Value {
    pub fn type_name(&self) -> &'static str {
        match self {
            Value::Null => "Null",
            Value::Bool(_) => "Bool",
            Value::Int(_) => "Int",
            Value::Float(_) => "Float",
            Value::Str(_) => "String",
            Value::Uri(_) => "Uri",
            Value::Array(_) => "Array",
            Value::Record(_) => "Record",
            Value::Table(_) => "Table",
            Value::Lambda(_) => "Lambda",
            Value::AsyncLambda(_) => "AsyncLambda",
            Value::Future(_) => "Future",
            Value::Error(_) => "Error",
            Value::Builtin(_) => "Builtin",
        }
    }

    // ----------- typed accessors (Result) -----------

    pub fn as_bool(&self) -> anyhow::Result<bool> {
        if let Value::Bool(b) = self {
            Ok(*b)
        } else {
            Err(anyhow::anyhow!("expected Bool"))
        }
    }
    pub fn as_int(&self) -> anyhow::Result<i64> {
        if let Value::Int(n) = self {
            Ok(*n)
        } else {
            Err(anyhow::anyhow!("expected Int"))
        }
    }
    pub fn as_float(&self) -> anyhow::Result<f64> {
        if let Value::Float(x) = self {
            Ok(*x)
        } else {
            Err(anyhow::anyhow!("expected Float"))
        }
    }
    pub fn as_str(&self) -> anyhow::Result<&str> {
        if let Value::Str(s) = self {
            Ok(s)
        } else {
            Err(anyhow::anyhow!("expected String"))
        }
    }
    pub fn as_uri(&self) -> anyhow::Result<&str> {
        if let Value::Uri(s) = self {
            Ok(s)
        } else {
            Err(anyhow::anyhow!("expected Uri"))
        }
    }
    pub fn as_array(&self) -> anyhow::Result<&[Value]> {
        if let Value::Array(v) = self {
            Ok(v)
        } else {
            Err(anyhow::anyhow!("expected Array"))
        }
    }
    pub fn as_record(&self) -> anyhow::Result<&BTreeMap<String, Value>> {
        if let Value::Record(m) = self {
            Ok(m)
        } else {
            Err(anyhow::anyhow!("expected Record"))
        }
    }
    pub fn as_table(&self) -> anyhow::Result<&Table> {
        if let Value::Table(t) = self {
            Ok(t)
        } else {
            Err(anyhow::anyhow!("expected Table"))
        }
    }

    /// Convert a Value to a display string (without quotes for Str values)
    pub fn to_display_string(&self) -> String {
        match self {
            Value::Str(s) => s.clone(),
            Value::Uri(u) => u.clone(),
            Value::Int(n) => n.to_string(),
            Value::Float(f) => f.to_string(),
            Value::Bool(b) => b.to_string(),
            Value::Null => "null".to_string(),
            Value::Array(arr) => {
                let items: Vec<String> = arr.iter().map(|v| v.to_display_string()).collect();
                format!("[{}]", items.join(", "))
            }
            Value::Record(rec) => {
                let items: Vec<String> = rec
                    .iter()
                    .map(|(k, v)| format!("{}: {}", k, v.to_display_string()))
                    .collect();
                format!("{{{}}}", items.join(", "))
            }
            Value::Table(t) => format!("<Table rows={}>", t.rows.len()),
            Value::Lambda(_) => "<lambda>".to_string(),
            Value::AsyncLambda(_) => "<async lambda>".to_string(),
            Value::Future(_) => "<future>".to_string(),
            Value::Error(msg) => format!("Error: {}", msg),
            Value::Builtin(b) => format!("<builtin:{}>", b.name),
        }
    }

    // ----------- JSON interop -----------

    pub fn from_json(v: &serde_json::Value) -> Self {
        use serde_json::Value as J;
        match v {
            J::Null => Value::Null,
            J::Bool(b) => Value::Bool(*b),
            J::Number(n) => {
                if let Some(i) = n.as_i64() {
                    Value::Int(i)
                } else if let Some(f) = n.as_f64() {
                    Value::Float(f)
                } else {
                    Value::Null
                }
            }
            J::String(s) => Value::Str(s.clone()),
            J::Array(a) => Value::Array(a.iter().map(Self::from_json).collect()),
            J::Object(m) => {
                let mut rec = BTreeMap::new();
                for (k, v) in m {
                    rec.insert(k.clone(), Self::from_json(v));
                }
                Value::Record(rec)
            }
        }
    }

    pub fn to_json(&self) -> serde_json::Value {
        use serde_json::json;
        match self {
            Value::Null => serde_json::Value::Null,
            Value::Bool(b) => json!(b),
            Value::Int(n) => json!(n),
            Value::Float(x) => json!(x),
            Value::Str(s) => json!(s),
            Value::Uri(u) => json!(u),
            Value::Array(a) => serde_json::Value::Array(a.iter().map(|v| v.to_json()).collect()),
            Value::Record(m) => {
                let mut obj = serde_json::Map::new();
                for (k, v) in m {
                    obj.insert(k.clone(), v.to_json());
                }
                serde_json::Value::Object(obj)
            }
            Value::Table(t) => {
                let mut rows = Vec::new();
                for r in &t.rows {
                    let mut obj = serde_json::Map::new();
                    for (k, v) in r {
                        obj.insert(k.clone(), v.to_json());
                    }
                    rows.push(serde_json::Value::Object(obj));
                }
                serde_json::json!({ "schema": t.schema, "rows": rows })
            }
            Value::Lambda(_) => json!("<lambda>"),
            Value::AsyncLambda(_) => json!("<async lambda>"),
            Value::Future(_) => json!("<future>"),
            Value::Error(msg) => json!({"error": msg}),
            Value::Builtin(b) => json!({"_type": "Builtin", "name": b.name}),
        }
    }
}

// ----------- Pretty printing (crossterm) - native only -----------

#[cfg(feature = "native")]
pub mod pretty {
    use super::*;
    use crossterm::style::{StyledContent, Stylize};

    #[derive(Debug, Clone)]
    pub struct Theme {
        pub key: fn(&str) -> StyledContent<String>,
        pub string: fn(&str) -> StyledContent<String>,
        pub number: fn(&str) -> StyledContent<String>,
        pub boolean: fn(&str) -> StyledContent<String>,
        pub null: fn(&str) -> StyledContent<String>,
        pub uri: fn(&str) -> StyledContent<String>,
        pub header: fn(&str) -> StyledContent<String>,
        pub dim: fn(&str) -> StyledContent<String>,
    }

    impl Theme {
        /// A theme that emits no escape sequences at all.
        ///
        /// `ContentStyle::default()` sets no attributes, so crossterm writes
        /// the content unchanged. Used whenever the destination is not a
        /// terminal, so that `print(x) | grep` and `$(...)` capture see the
        /// same characters a human does.
        pub fn plain() -> Self {
            Self {
                key: |s| s.to_string().stylize(),
                string: |s| s.to_string().stylize(),
                number: |s| s.to_string().stylize(),
                boolean: |s| s.to_string().stylize(),
                null: |s| s.to_string().stylize(),
                uri: |s| s.to_string().stylize(),
                header: |s| s.to_string().stylize(),
                dim: |s| s.to_string().stylize(),
            }
        }

        /// The default theme when the destination can show colour, the plain
        /// one otherwise.
        pub fn for_stdout() -> Self {
            if crate::config::colors_enabled() {
                Self::default()
            } else {
                Self::plain()
            }
        }
    }

    impl Default for Theme {
        fn default() -> Self {
            Self {
                key: |s| s.to_string().cyan(),
                string: |s| s.to_string().green(),
                number: |s| s.to_string().blue(),
                boolean: |s| s.to_string().magenta(),
                null: |s| s.to_string().dark_grey(),
                uri: |s| s.to_string().yellow(),
                header: |s| s.to_string().bold().underlined(),
                dim: |s| s.to_string().dark_grey(),
            }
        }
    }

    /// Write a `Value` to any `fmt::Write`, using colors.
    pub fn fmt_value<W: fmt::Write>(w: &mut W, v: &Value, theme: &Theme) -> fmt::Result {
        match v {
            Value::Null => write!(w, "{}", (theme.null)("null")),
            Value::Bool(b) => write!(w, "{}", (theme.boolean)(&b.to_string())),
            Value::Int(n) => write!(w, "{}", (theme.number)(&n.to_string())),
            Value::Float(x) => write!(w, "{}", (theme.number)(&x.to_string())),
            Value::Str(s) => write!(w, "\"{}\"", (theme.string)(s)),
            Value::Uri(u) => write!(w, "{}", (theme.uri)(u)),
            Value::Array(a) => {
                write!(w, "[")?;
                for (i, el) in a.iter().enumerate() {
                    if i > 0 {
                        write!(w, ", ")?;
                    }
                    fmt_value(w, el, theme)?;
                }
                write!(w, "]")
            }
            Value::Record(m) => {
                write!(w, "{{")?;
                let mut first = true;
                for (k, v) in m {
                    if !first {
                        write!(w, ", ")?;
                    } else {
                        first = false;
                    }
                    write!(w, "{}: ", (theme.key)(k))?;
                    fmt_value(w, v, theme)?;
                }
                write!(w, "}}")
            }
            Value::Table(t) => fmt_table(w, t, theme),
            Value::Lambda(_) => write!(w, "{}", (theme.dim)("<lambda>")),
            Value::AsyncLambda(_) => write!(w, "{}", (theme.dim)("<async lambda>")),
            Value::Future(_) => write!(w, "{}", (theme.dim)("<future>")),
            Value::Error(msg) => write!(w, "Error: {}", msg),
            Value::Builtin(b) => write!(w, "{}", (theme.dim)(&format!("<builtin:{}>", b.name))),
        }
    }

    fn fmt_table<W: fmt::Write>(w: &mut W, t: &Table, theme: &Theme) -> fmt::Result {
        if t.schema.is_empty() {
            return write!(w, "{}", (theme.dim)("<Table rows=0 cols=0>"));
        }

        // Compute column widths from schema + a sample of rows
        let mut widths: Vec<usize> = t.schema.iter().map(|s| s.len()).collect();
        let sample = t.rows.iter().take(50);
        for r in sample {
            for (i, col) in t.schema.iter().enumerate() {
                let cell = r.get(col).map(summarize).unwrap_or_default();
                widths[i] = widths[i].max(cell.len());
            }
        }

        // Header
        for (i, col) in t.schema.iter().enumerate() {
            if i > 0 {
                write!(w, " ")?;
            }
            let h = (theme.header)(col).to_string();
            write!(w, "{h:width$}", width = widths[i])?;
        }
        writeln!(w)?;

        // Rows
        for r in &t.rows {
            for (i, col) in t.schema.iter().enumerate() {
                if i > 0 {
                    write!(w, " ")?;
                }
                let cell = r
                    .get(col)
                    .map(|v| display_inline(v, theme))
                    .unwrap_or_else(|| (theme.dim)("-").to_string());
                write!(w, "{cell:width$}", width = widths[i])?;
            }
            writeln!(w)?;
        }
        Ok(())
    }

    fn summarize(v: &Value) -> String {
        match v {
            Value::Null => "null".into(),
            Value::Bool(b) => b.to_string(),
            Value::Int(n) => n.to_string(),
            Value::Float(x) => x.to_string(),
            Value::Str(s) => truncate(s, 40),
            Value::Uri(u) => truncate(u, 48),
            Value::Array(a) => format!("[{}]", a.len()),
            Value::Record(_) => "{…}".into(),
            Value::Table(t) => format!("<Table rows={}>", t.rows.len()),
            Value::Lambda(_) => "<lambda>".into(),
            Value::AsyncLambda(_) => "<async lambda>".into(),
            Value::Future(_) => "<future>".into(),
            Value::Error(msg) => format!("Error: {}", truncate(msg, 30)),
            Value::Builtin(b) => format!("<builtin:{}>", b.name),
        }
    }

    /// Render a value in full, with no length cap.
    ///
    /// `display_inline` truncates at 80 characters, which is right for the
    /// compact diagnostics `debug` and `trace` emit and wrong for `print`:
    /// `print` is how a script produces output, and it was silently discarding
    /// everything past the 80th character of any string.
    pub fn display_full(v: &Value, theme: &Theme) -> String {
        let mut buf = String::new();
        let _ = fmt_value(&mut buf, v, theme);
        buf
    }

    pub fn display_inline(v: &Value, theme: &Theme) -> String {
        let mut buf = String::new();
        let _ = fmt_value(&mut buf, v, theme);
        truncate(&buf, 80)
    }

    /// Truncate to at most `n` *characters*.
    ///
    /// This used to slice `&s[..n]` on a byte index, which panics whenever byte
    /// `n` lands inside a multi-byte character:
    ///
    /// ```text
    /// end byte index 80 is not a char boundary; it is inside '─'
    /// ```
    ///
    /// Five shipped examples crashed the shell that way — any box-drawing rule,
    /// accented word or emoji long enough to reach the limit did it. Counting
    /// characters also makes the limit mean what it says: 80 bytes of CJK is
    /// 26 glyphs, not 80.
    fn truncate(s: &str, n: usize) -> String {
        if s.chars().count() <= n {
            s.to_string()
        } else {
            let cut: String = s.chars().take(n).collect();
            format!("{}", cut)
        }
    }
}

// ----------- Minimal Display -----------

#[cfg(feature = "native")]
impl fmt::Display for Value {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        use crate::value::pretty::{fmt_value, Theme};
        let theme = Theme::default();
        fmt_value(f, self, &theme)
    }
}

#[cfg(not(feature = "native"))]
impl fmt::Display for Value {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Value::Null => write!(f, "null"),
            Value::Bool(b) => write!(f, "{}", b),
            Value::Int(n) => write!(f, "{}", n),
            Value::Float(x) => write!(f, "{}", x),
            Value::Str(s) => write!(f, "\"{}\"", s),
            Value::Uri(u) => write!(f, "{}", u),
            Value::Array(a) => {
                write!(f, "[")?;
                for (i, el) in a.iter().enumerate() {
                    if i > 0 {
                        write!(f, ", ")?;
                    }
                    write!(f, "{}", el)?;
                }
                write!(f, "]")
            }
            Value::Record(m) => {
                write!(f, "{{")?;
                let mut first = true;
                for (k, v) in m {
                    if !first {
                        write!(f, ", ")?;
                    } else {
                        first = false;
                    }
                    write!(f, "{}: {}", k, v)?;
                }
                write!(f, "}}")
            }
            Value::Table(t) => write!(f, "<Table rows={} cols={}>", t.rows.len(), t.schema.len()),
            Value::Lambda(_) => write!(f, "<lambda>"),
            Value::AsyncLambda(_) => write!(f, "<async lambda>"),
            Value::Future(_) => write!(f, "<future>"),
            Value::Error(msg) => write!(f, "Error: {}", msg),
            // Mirrors the native renderer, which prints `<builtin:name>`.
            // Omitting this arm broke the wasm32 build outright.
            Value::Builtin(b) => write!(f, "<builtin:{}>", b.name),
        }
    }
}