jev-repl 0.7.0

Terminal REPL for shaping TypeSafe AI System One requests: noul, choice and score questions, with a live sketch editor
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
//! Sketch notation: the whole request written as one page of plain text, the way you would
//! scribble a rubric on paper. The shape of each question is read off its punctuation, so there
//! is nothing to select — you write what you mean and the gutter tells you what it became.
//!
//! ```text
//! The payout failed again, third time this month. I'm done waiting.
//! ---
//! is_urgent? The message conveys urgency or time-sensitivity
//!   yes: A deadline, a threat to leave, or "ASAP"
//!   no: Routine, no time pressure
//!
//! department: Which team should handle this
//!   billing = Payment or subscription issues
//!   technical = Bugs or integration problems
//!   sales
//!
//! frustration: How frustrated the customer appears
//!   Calm < Frustrated but civil < Very angry
//! ```
//!
//! - Everything above the first `---` line is the state (JSON if it parses as JSON).
//! - `name? instructions` is a yes/no question (noul); `yes:` / `no:` lines describe the outcomes.
//! - `name: instructions` followed by `label = description` lines (or bare labels) is a choice.
//! - `name: instructions` followed by levels joined with `<` is a score, lowest first.
//! - `name! {json}` sends a hand-built question object, like [`Question::Raw`].
//! - The parts of a question can also go on its first line, separated by `|`.
//! - `@model jev-2` pins the model; `#` starts a comment. Indentation is only for reading.

use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span};
use serde_json::Value;
use typesafe::{Choice, Noul, Question, Score};

use crate::format::{CHOICE, DIM, NOUL, SCORE, WARN};
use crate::session::Session;

/// What one line of a sketch turned out to be; shown in the editor gutter.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Tag {
    Blank,
    State,
    Rule,
    Comment,
    Model,
    Noul,
    Choice,
    Score,
    Raw,
    Yes,
    No,
    Option,
    Level,
    Json,
    /// A line that could not be placed; it always carries a problem.
    Stray,
}

impl Tag {
    pub fn label(self) -> &'static str {
        match self {
            Tag::Blank | Tag::Rule => "",
            Tag::State => "state",
            Tag::Comment => "#",
            Tag::Model => "model",
            Tag::Noul => "noul",
            Tag::Choice => "choice",
            Tag::Score => "score",
            Tag::Raw => "raw",
            Tag::Yes => "yes",
            Tag::No => "no",
            Tag::Option => "option",
            Tag::Level => "level",
            Tag::Json => "json",
            Tag::Stray => "?",
        }
    }

    pub fn color(self) -> Color {
        match self {
            Tag::Noul | Tag::Yes | Tag::No => NOUL,
            Tag::Choice | Tag::Option => CHOICE,
            Tag::Score | Tag::Level => SCORE,
            Tag::Raw | Tag::Json => WARN,
            Tag::Stray => Color::Red,
            _ => DIM,
        }
    }

    /// Body lines carry their question's colour; heads are bold.
    pub fn is_head(self) -> bool {
        matches!(self, Tag::Noul | Tag::Choice | Tag::Score | Tag::Raw)
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Problem {
    /// Zero-based line.
    pub line: usize,
    pub message: String,
}

/// A parsed sketch. Questions with problems are left out of `questions` but keep their tags, so
/// the page still reads sensibly while it is being fixed.
#[derive(Debug, Default)]
pub struct Parsed {
    pub state: Value,
    pub model: Option<String>,
    pub questions: Vec<(String, Question)>,
    /// One per line of the input.
    pub tags: Vec<Tag>,
    pub problems: Vec<Problem>,
}

impl Parsed {
    pub fn ok(&self) -> bool {
        self.problems.is_empty()
    }

    pub fn to_session(&self) -> Session {
        Session {
            state: self.state.clone(),
            questions: self.questions.clone(),
            model: self.model.clone(),
        }
    }

    /// The first problem on or after a line — what the status line shows for the cursor.
    pub fn problem_at(&self, line: usize) -> Option<&Problem> {
        self.problems.iter().find(|p| p.line == line)
    }
}

/// A question block under construction: its head, and the parts collected from the head line
/// (after `|`) and the lines below it.
struct Block {
    line: usize,
    name: String,
    marker: char,
    /// The head line after the marker, unsplit — raw questions need it whole.
    rest: String,
    parts: Vec<(usize, String)>,
}

pub fn parse(text: &str) -> Parsed {
    let lines: Vec<&str> = text.split('\n').collect();
    let mut out = Parsed {
        tags: vec![Tag::Blank; lines.len()],
        ..Default::default()
    };

    let rule = lines.iter().position(|l| l.trim() == "---");
    let state_end = rule.unwrap_or(lines.len());
    for (i, line) in lines[..state_end].iter().enumerate() {
        out.tags[i] = if line.trim().is_empty() {
            Tag::Blank
        } else {
            Tag::State
        };
    }
    out.state = state_value(&lines[..state_end].join("\n"));

    let Some(rule) = rule else {
        if lines.iter().any(|l| !l.trim().is_empty()) {
            out.problems.push(Problem {
                line: lines.len() - 1,
                message: "no `---` yet — the questions go below one".into(),
            });
        }
        return out;
    };
    out.tags[rule] = Tag::Rule;

    let mut block: Option<Block> = None;
    for (i, raw) in lines.iter().enumerate().skip(rule + 1) {
        let line = raw.trim();
        if line.is_empty() {
            continue;
        }
        if line.starts_with('#') {
            out.tags[i] = Tag::Comment;
            continue;
        }
        if let Some(directive) = line.strip_prefix('@') {
            let (key, arg) = directive
                .split_once(char::is_whitespace)
                .map(|(k, a)| (k, a.trim()))
                .unwrap_or((directive, ""));
            match key {
                "model" if !arg.is_empty() => {
                    out.tags[i] = Tag::Model;
                    out.model = Some(arg.to_owned());
                }
                "model" => {
                    out.tags[i] = Tag::Stray;
                    out.problems.push(Problem {
                        line: i,
                        message: "`@model` needs a name, e.g. `@model jev-latest`".into(),
                    });
                }
                other => {
                    out.tags[i] = Tag::Stray;
                    out.problems.push(Problem {
                        line: i,
                        message: format!("unknown directive `@{other}`; only `@model` exists"),
                    });
                }
            }
            continue;
        }
        if let Some((name, marker, rest)) = head(line) {
            if let Some(done) = block.take() {
                finish(done, &mut out);
            }
            block = Some(Block {
                line: i,
                name,
                marker,
                rest: rest.to_owned(),
                parts: Vec::new(),
            });
            continue;
        }
        match block.as_mut() {
            Some(b) => b.parts.push((i, line.to_owned())),
            None => {
                out.tags[i] = Tag::Stray;
                out.problems.push(Problem {
                    line: i,
                    message: "not a question — start one with `name?` (yes/no) or `name:` (options or levels)".into(),
                });
            }
        }
    }
    if let Some(done) = block.take() {
        finish(done, &mut out);
    }
    out
}

/// `name?` / `name:` / `name!` at the start of a line, with the name a plain identifier.
/// `yes:` and `no:` are a noul's body, never a head.
fn head(line: &str) -> Option<(String, char, &str)> {
    let end = line.find(['?', ':', '!'])?;
    let (name, rest) = line.split_at(end);
    if !is_name(name) {
        return None;
    }
    let marker = rest.chars().next()?;
    let after = &rest[1..];
    if !(after.is_empty() || after.starts_with(char::is_whitespace)) {
        return None;
    }
    if marker == ':' && is_criterion(name) {
        return None;
    }
    Some((name.to_owned(), marker, after.trim()))
}

fn is_name(s: &str) -> bool {
    let mut chars = s.chars();
    matches!(chars.next(), Some(c) if c.is_alphabetic() || c == '_')
        && chars.all(|c| c.is_alphanumeric() || c == '_' || c == '-')
}

fn is_criterion(word: &str) -> bool {
    matches!(
        word.to_ascii_lowercase().as_str(),
        "yes" | "no" | "true" | "false"
    )
}

/// Turn a finished block into a question, or into problems.
fn finish(b: Block, out: &mut Parsed) {
    let mut problem = |line: usize, message: String| {
        out.problems.push(Problem { line, message });
    };
    if out.questions.iter().any(|(n, _)| *n == b.name) {
        out.tags[b.line] = Tag::Stray;
        problem(
            b.line,
            format!("another question is already named `{}`", b.name),
        );
        return;
    }

    if b.marker == '!' {
        out.tags[b.line] = Tag::Raw;
        let mut text = b.rest.clone();
        for (i, part) in &b.parts {
            out.tags[*i] = Tag::Json;
            text.push('\n');
            text.push_str(part);
        }
        match serde_json::from_str::<Value>(&text) {
            Ok(v) if v.get("type").and_then(Value::as_str).is_some_and(|t| !t.is_empty()) => {
                out.questions.push((b.name, Question::Raw(v)));
            }
            Ok(_) => problem(
                b.line,
                "a raw question is a JSON object with a `type`, e.g. {\"type\": \"noul\", \"instructions\": \"\"}"
                    .into(),
            ),
            Err(e) => problem(b.line, format!("raw question is not valid JSON: {e}")),
        }
        return;
    }

    // `name: instructions | part | part` — inline parts join the ones below.
    let mut parts: Vec<(usize, String)> = Vec::new();
    let mut inline = split_top(&b.rest, '|').into_iter().map(str::trim);
    let instructions = inline.next().unwrap_or("").to_owned();
    parts.extend(
        inline
            .filter(|p| !p.is_empty())
            .map(|p| (b.line, p.to_owned())),
    );
    parts.extend(b.parts.iter().cloned());
    for (_, p) in &mut parts {
        if let Some(rest) = p
            .strip_prefix("- ")
            .or_else(|| p.strip_prefix("* "))
            .or_else(|| p.strip_prefix(""))
        {
            *p = rest.trim().to_owned();
        }
    }

    if instructions.is_empty() {
        out.tags[b.line] = Tag::Stray;
        problem(
            b.line,
            format!(
                "`{}` needs instructions after the `{}` — what should the model decide?",
                b.name, b.marker
            ),
        );
        return;
    }
    let instructions = value(&instructions);

    let all_criteria = !parts.is_empty()
        && parts.iter().all(|(_, p)| {
            p.split_once(':')
                .is_some_and(|(k, _)| is_criterion(k.trim()))
        });

    if b.marker == '?' || all_criteria {
        out.tags[b.line] = Tag::Noul;
        let mut q = Noul::new(instructions);
        let mut bad = false;
        for (i, part) in &parts {
            match part.split_once(':').map(|(k, v)| (k.trim(), v.trim())) {
                Some((k, v)) if is_criterion(k) => {
                    let yes = matches!(k.to_ascii_lowercase().as_str(), "yes" | "true");
                    out.tags[*i] = if yes { Tag::Yes } else { Tag::No };
                    q = if yes {
                        q.when_true(value(v))
                    } else {
                        q.when_false(value(v))
                    };
                }
                _ => {
                    bad = true;
                    out.tags[*i] = Tag::Stray;
                    problem(
                        *i,
                        "a yes/no question only takes `yes: …` and `no: …` lines".into(),
                    );
                }
            }
        }
        if !bad {
            out.questions.push((b.name, q.into()));
        }
        return;
    }

    if parts.is_empty() {
        out.tags[b.line] = Tag::Stray;
        problem(
            b.line,
            "add options (`billing = Payments`) or ordered levels (`Calm < Annoyed < Furious`), or end the name with `?` for yes/no"
                .into(),
        );
        return;
    }

    let is_choice = parts.iter().any(|(_, p)| split_top(p, '=').len() > 1);
    // A `<` only means "level" in a part that is not an option; descriptions may contain one.
    let has_levels = parts
        .iter()
        .any(|(_, p)| split_top(p, '=').len() == 1 && split_top(p, '<').len() > 1);
    if is_choice && has_levels {
        out.tags[b.line] = Tag::Stray;
        for (i, part) in &parts {
            out.tags[*i] = if split_top(part, '=').len() > 1 {
                Tag::Option
            } else {
                Tag::Level
            };
        }
        problem(
            b.line,
            "options (`label = why`) and levels (`low < high`) are mixed — a question is a choice or a score, not both"
                .into(),
        );
        return;
    }
    let is_score = !is_choice && has_levels;

    if is_score {
        out.tags[b.line] = Tag::Score;
        let mut levels = Vec::new();
        for (i, part) in &parts {
            out.tags[*i] = Tag::Level;
            levels.extend(
                split_top(part, '<')
                    .into_iter()
                    .map(str::trim)
                    .filter(|l| !l.is_empty())
                    .map(value),
            );
        }
        if levels.len() < 2 {
            problem(
                b.line,
                "a score needs at least two levels, lowest first".into(),
            );
            return;
        }
        out.questions
            .push((b.name, Score::new(instructions, levels).into()));
        return;
    }

    out.tags[b.line] = Tag::Choice;
    let mut q = Choice::new(instructions);
    let mut count = 0;
    let mut bad = false;
    for (i, part) in &parts {
        out.tags[*i] = Tag::Option;
        let (label, desc) = match split_top(part, '=').as_slice() {
            [l, d, ..] => (l.trim(), d.trim()),
            _ => (part.as_str(), ""),
        };
        if label.is_empty() {
            bad = true;
            out.tags[*i] = Tag::Stray;
            problem(*i, "an option needs a label before the `=`".into());
            continue;
        }
        q = if desc.is_empty() {
            q.label(label)
        } else {
            q.option(label, value(desc))
        };
        count += 1;
    }
    if bad {
        return;
    }
    if count < 2 {
        problem(
            b.line,
            "one option is not a choice — add another, or write ordered levels as `a < b < c`"
                .into(),
        );
        return;
    }
    out.questions.push((b.name, q.into()));
}

/// Split on `sep`, except inside a double-quoted JSON string — so a quoted value can carry the
/// notation's own punctuation. Splits at most once for `=`, since a description may contain it.
fn split_top(text: &str, sep: char) -> Vec<&str> {
    let mut parts = Vec::new();
    let mut start = 0;
    let mut quoted = false;
    let mut escaped = false;
    for (i, c) in text.char_indices() {
        if escaped {
            escaped = false;
            continue;
        }
        match c {
            '\\' if quoted => escaped = true,
            '"' => quoted = !quoted,
            c if c == sep && !quoted && (sep != '=' || parts.is_empty()) => {
                parts.push(&text[start..i]);
                start = i + c.len_utf8();
            }
            _ => {}
        }
    }
    parts.push(&text[start..]);
    parts
}

/// Text becomes a JSON value when it is written as one (an object, an array or a quoted
/// string); anything else is sent as the plain string it is.
pub fn value(text: &str) -> Value {
    let t = text.trim();
    if (t.starts_with('{') || t.starts_with('[') || t.starts_with('"'))
        && let Ok(v) = serde_json::from_str::<Value>(t)
    {
        return v;
    }
    Value::String(t.to_owned())
}

fn state_value(text: &str) -> Value {
    let t = text.trim();
    if (t.starts_with('{') || t.starts_with('['))
        && let Ok(v) = serde_json::from_str::<Value>(t)
    {
        return v;
    }
    Value::String(t.to_owned())
}

// ---- rendering ----------------------------------------------------------------------------

/// The session as a sketch — the inverse of [`parse`], so a page can be opened, edited and
/// applied without losing anything.
pub fn render(session: &Session) -> String {
    let mut out = String::new();
    match &session.state {
        Value::String(s) => out.push_str(s.trim_end()),
        Value::Null => {}
        other => out.push_str(&serde_json::to_string_pretty(other).unwrap_or_default()),
    }
    out.push_str("\n---\n");
    if let Some(model) = &session.model {
        out.push_str(&format!("@model {model}\n"));
    }
    for (i, (name, question)) in session.questions.iter().enumerate() {
        if i > 0 || session.model.is_some() {
            out.push('\n');
        }
        out.push_str(&render_question(name, question));
    }
    out
}

fn render_question(name: &str, question: &Question) -> String {
    let v = serde_json::to_value(question).unwrap_or(Value::Null);
    let kind = v.get("type").and_then(Value::as_str).unwrap_or("");
    let instructions = v.get("instructions").map(part).unwrap_or_default();
    let criteria = v.get("criteria");
    let mut s = String::new();
    match (question, kind) {
        (Question::Raw(raw), _) => {
            s.push_str(&format!("{name}! {raw}\n"));
        }
        (_, "noul") => {
            s.push_str(&format!("{name}? {instructions}\n"));
            if let Some(yes) = criteria.and_then(|c| c.get("true")) {
                s.push_str(&format!("  yes: {}\n", part(yes)));
            }
            if let Some(no) = criteria.and_then(|c| c.get("false")) {
                s.push_str(&format!("  no: {}\n", part(no)));
            }
        }
        (_, "choice") => {
            s.push_str(&format!("{name}: {instructions}\n"));
            if let Some(map) = criteria.and_then(Value::as_object) {
                for (label, desc) in map {
                    match desc {
                        Value::Null => s.push_str(&format!("  {label}\n")),
                        d => s.push_str(&format!("  {label} = {}\n", part(d))),
                    }
                }
            }
        }
        (_, "score") => {
            s.push_str(&format!("{name}: {instructions}\n"));
            let levels: Vec<String> = criteria
                .and_then(Value::as_array)
                .map(|a| a.iter().map(part).collect())
                .unwrap_or_default();
            let one_line = levels.join(" < ");
            if one_line.chars().count() <= 60 {
                s.push_str(&format!("  {one_line}\n"));
            } else {
                for (i, level) in levels.iter().enumerate() {
                    if i == 0 {
                        s.push_str(&format!("  {level}\n"));
                    } else {
                        s.push_str(&format!("  < {level}\n"));
                    }
                }
            }
        }
        _ => s.push_str(&format!("{name}! {v}\n")),
    }
    s
}

/// A value as it goes on a sketch line: plain text when it can be read back as itself, a JSON
/// literal when it could not (structured values, or text the notation would otherwise split).
fn part(v: &Value) -> String {
    match v {
        Value::String(s)
            if !s.contains(['|', '<', '=', '\n', '\r'])
                && !s.starts_with(['{', '[', '"', '#', '@', '-', '*', ''])
                && head(s).is_none()
                && s.trim() == s
                && !s.is_empty() =>
        {
            s.clone()
        }
        other => other.to_string(),
    }
}

/// A sketch, highlighted for the transcript: heads bold in their kind's colour, bodies in it.
pub fn highlight(text: &str) -> Vec<Line<'static>> {
    let parsed = parse(text);
    text.split('\n')
        .zip(parsed.tags.iter())
        .map(|(line, tag)| {
            let style = match tag {
                t if t.is_head() => Style::new().fg(t.color()).add_modifier(Modifier::BOLD),
                Tag::State => Style::new(),
                Tag::Rule | Tag::Comment | Tag::Blank => Style::new().fg(DIM),
                t => Style::new().fg(t.color()),
            };
            Line::from(vec![Span::raw("  "), Span::styled(line.to_owned(), style)])
        })
        .collect()
}