jev-repl 0.6.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
//! Turning answers, questions and errors into styled transcript lines.

use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span};
use serde_json::Value;
use typesafe::{Answer, Error, Question};

use crate::cost::{Estimate, Rates, Thread, format_rates, price, price_estimate, usd};
use crate::session::Turn;

pub const NOUL: Color = Color::Cyan;
pub const CHOICE: Color = Color::Magenta;
pub const SCORE: Color = Color::Green;
pub const DIM: Color = Color::DarkGray;
pub const WARN: Color = Color::Yellow;
pub const BAD: Color = Color::Red;
pub const ACCENT: Color = Color::LightBlue;

pub fn dim(text: impl Into<String>) -> Span<'static> {
    Span::styled(text.into(), Style::new().fg(DIM))
}

pub fn plain(text: impl Into<String>) -> Line<'static> {
    Line::from(text.into())
}

pub fn styled(text: impl Into<String>, color: Color) -> Line<'static> {
    Line::from(Span::styled(text.into(), Style::new().fg(color)))
}

pub fn bold(text: impl Into<String>) -> Span<'static> {
    Span::styled(text.into(), Style::new().add_modifier(Modifier::BOLD))
}

/// A probability meter. Eighteen columns is enough to read a distribution at a glance.
pub fn bar(p: f64, width: usize) -> String {
    let filled = ((p.clamp(0.0, 1.0) * width as f64).round() as usize).min(width);
    format!("{}{}", "".repeat(filled), "".repeat(width - filled))
}

pub fn color_for(kind: &str) -> Color {
    match kind {
        "noul" => NOUL,
        "choice" => CHOICE,
        "score" => SCORE,
        _ => DIM,
    }
}

/// Render one answer: the number, the distribution it came from, and what it means.
pub fn answer_lines(name: &str, answer: &Answer, threshold: f64) -> Vec<Line<'static>> {
    let kind = answer.kind();
    let mut out = vec![Line::from(vec![
        Span::raw("  "),
        bold(name.to_owned()),
        Span::raw("  "),
        Span::styled(kind.to_owned(), Style::new().fg(color_for(kind))),
    ])];

    match answer {
        Answer::Noul(a) => {
            let yes = a.is_yes(threshold);
            out.push(Line::from(vec![
                Span::raw("    "),
                bold(format!("{:.2}", a.noul)),
                Span::raw("  "),
                Span::styled(bar(a.noul, 18), Style::new().fg(NOUL)),
                Span::raw("  "),
                Span::styled(
                    if yes { "yes" } else { "no" }.to_owned(),
                    Style::new()
                        .fg(if yes { SCORE } else { DIM })
                        .add_modifier(Modifier::BOLD),
                ),
                dim(format!(" at threshold {threshold:.2}")),
            ]));
        }
        Answer::Choice(a) => {
            out.push(Line::from(vec![
                Span::raw(""),
                Span::styled(
                    a.choice.clone(),
                    Style::new().fg(CHOICE).add_modifier(Modifier::BOLD),
                ),
                Span::raw("   "),
                dim("confidence "),
                confidence_span(a.confidence),
            ]));
            let ranked = a.ranked();
            let pad = ranked.iter().map(|(l, _)| l.len()).max().unwrap_or(0);
            for (label, p) in ranked {
                out.push(Line::from(vec![
                    Span::raw("      "),
                    Span::styled(
                        format!("{label:pad$}"),
                        Style::new().fg(if label == a.choice { Color::Reset } else { DIM }),
                    ),
                    Span::raw("  "),
                    Span::raw(format!("{p:.2}")),
                    Span::raw("  "),
                    Span::styled(bar(p, 18), Style::new().fg(CHOICE)),
                ]));
            }
        }
        Answer::Score(a) => {
            let top = a.legend.keys().next_back().copied().unwrap_or(0);
            out.push(Line::from(vec![
                Span::raw("    "),
                Span::styled(
                    format!("{:.2}", a.score),
                    Style::new().fg(SCORE).add_modifier(Modifier::BOLD),
                ),
                dim(format!(" of {top}")),
                Span::raw("   "),
                dim("confidence "),
                confidence_span(a.confidence),
                dim(format!(
                    "   most likely level {}",
                    a.most_likely_level()
                        .map(|l| l.to_string())
                        .unwrap_or_else(|| "-".into())
                )),
            ]));
            let labels: Vec<(u32, String)> =
                a.legend.iter().map(|(i, v)| (*i, text_of(v))).collect();
            let pad = labels
                .iter()
                .map(|(_, l)| l.len())
                .max()
                .unwrap_or(0)
                .min(40);
            for (level, label) in labels {
                let p = a.probabilities.get(&level).copied().unwrap_or(0.0);
                let marker = if a.rounded_level() == level {
                    ""
                } else {
                    " "
                };
                out.push(Line::from(vec![
                    Span::raw(format!("     {marker} ")),
                    dim(format!("{level} ")),
                    Span::styled(format!("{label:pad$}"), Style::new().fg(Color::Reset)),
                    Span::raw("  "),
                    Span::raw(format!("{p:.2}")),
                    Span::raw("  "),
                    Span::styled(bar(p, 18), Style::new().fg(SCORE)),
                ]));
            }
        }
        _ => out.push(styled(
            format!("    (this SDK version does not model {kind} answers; see :last)"),
            WARN,
        )),
    }
    out
}

fn confidence_span(c: f64) -> Span<'static> {
    let color = if c >= 0.6 {
        SCORE
    } else if c >= 0.35 {
        WARN
    } else {
        BAD
    };
    Span::styled(format!("{c:.2}"), Style::new().fg(color))
}

/// The cost estimate as a small table: which question spends what, and — when rates are set — the
/// money at the bottom. Tokens are estimated, so the numbers are a shape, not a bill. `hint` is how
/// this host sets rates, since the terminal has `:cost` and other hosts have their own. `thread`
/// is what the state costs when it is a conversation, which is more than the last call alone.
pub fn cost_lines(
    estimate: &Estimate,
    rates: Option<Rates>,
    hint: &str,
    thread: Option<&Thread>,
) -> Vec<Line<'static>> {
    let pad = estimate
        .questions
        .iter()
        .map(|q| q.name.chars().count())
        .chain([5, 8, 5])
        .max()
        .unwrap_or(8);
    // `noul` and `choice` fit the seven the table has always been; a long enough thread does not.
    let turns = match thread {
        Some(thread) => {
            let plural = if thread.turns == 1 { "" } else { "s" };
            format!("{} turn{plural}", thread.turns)
        }
        None => String::new(),
    };
    let kind_pad = turns.chars().count().max(7);
    let row = |name: &str, kind: &str, input: String, output: String, color: Option<Color>| {
        Line::from(vec![
            Span::raw("  "),
            match color {
                Some(color) => Span::styled(pad_end(name, pad), Style::new().fg(color)),
                None => Span::raw(pad_end(name, pad)),
            },
            Span::raw("  "),
            dim(pad_end(kind, kind_pad)),
            Span::raw(format!("{input:>6}")),
            Span::raw(format!("{output:>6}")),
        ])
    };

    let mut out = vec![Line::from(vec![
        Span::raw("  "),
        dim(pad_end("", pad)),
        Span::raw("  "),
        dim(pad_end("", kind_pad)),
        dim(format!("{:>6}", "in")),
        dim(format!("{:>6}", "out")),
    ])];
    for q in &estimate.questions {
        out.push(row(
            &q.name,
            &q.kind,
            q.input_tokens.to_string(),
            if q.assumed {
                format!("~{}", q.output_tokens)
            } else {
                q.output_tokens.to_string()
            },
            Some(color_for(&q.kind)),
        ));
    }
    out.push(row(
        "state",
        &turns,
        estimate.state_tokens.to_string(),
        "·".to_owned(),
        None,
    ));
    out.push(row(
        "envelope",
        "",
        estimate.envelope_tokens.to_string(),
        estimate.answer_envelope_tokens.to_string(),
        None,
    ));
    out.push(Line::from(vec![
        Span::raw("  "),
        bold(pad_end("total", pad)),
        Span::raw("  "),
        dim(pad_end("", kind_pad)),
        bold(format!("{:>6}", estimate.input_tokens)),
        bold(format!("{:>6}", estimate.output_tokens)),
        dim(format!(
            "   {} tokens per call",
            estimate.input_tokens + estimate.output_tokens
        )),
    ]));

    let Some(rates) = rates else {
        out.push(Line::from(vec![
            Span::raw("    "),
            dim(format!("no rates set — {hint}")),
        ]));
        out.extend(thread_lines(thread, None));
        return out;
    };
    let cost = price_estimate(estimate, rates);
    out.push(Line::from(vec![
        Span::raw("    "),
        Span::styled(
            usd(cost.total),
            Style::new().fg(SCORE).add_modifier(Modifier::BOLD),
        ),
        dim(" per call   ·   "),
        Span::styled(usd(cost.total * 1000.0), Style::new().fg(SCORE)),
        dim(" per 1,000 calls"),
    ]));
    out.push(Line::from(vec![
        Span::raw("    "),
        dim(format!("at {}", format_rates(rates))),
    ]));
    out.extend(thread_lines(thread, Some(rates)));
    out
}

/// The line under the table when the state is a conversation: a call per turn, all of it added up.
///
/// A thread is sent whole every time it is asked about, so the tokens grow with the square of the
/// turns rather than with the transcript. Saying so once, in numbers, is cheaper than finding out.
fn thread_lines(thread: Option<&Thread>, rates: Option<Rates>) -> Vec<Line<'static>> {
    let Some(thread) = thread else {
        return Vec::new();
    };
    let plural = if thread.turns == 1 { "" } else { "s" };
    let total = thread.input_tokens + thread.output_tokens;
    let mut spans = vec![
        Span::raw("    "),
        dim("asked after every turn: "),
        Span::raw(format!("{} call{plural}", thread.turns)),
        dim(format!(
            ", {} in / {} out",
            thread.input_tokens, thread.output_tokens
        )),
        dim(format!("   {total} tokens for the thread")),
    ];
    if let Some(rates) = rates {
        let spent = price(
            thread.input_tokens as u64,
            thread.output_tokens as u64,
            rates,
        );
        spans.push(dim("   ·   "));
        spans.push(Span::styled(usd(spent.total), Style::new().fg(SCORE)));
    }
    vec![Line::from(spans)]
}

/// One turn of the conversation held as the state, as `:turn` and `:state` echo it back.
pub fn turn_lines(index: usize, turn: &Turn) -> Vec<Line<'static>> {
    vec![Line::from(vec![
        dim(format!("  {}. ", index + 1)),
        match &turn.who {
            Some(who) => Span::styled(who.clone(), Style::new().fg(ACCENT)),
            None => dim("(unattributed)"),
        },
        Span::raw("  "),
        Span::raw(turn.said.clone()),
    ])]
}

fn pad_end(text: &str, width: usize) -> String {
    let len = text.chars().count();
    if len >= width {
        text.to_owned()
    } else {
        format!("{text}{}", " ".repeat(width - len))
    }
}

/// One line per question, the way it will go on the wire.
pub fn question_lines(index: usize, name: &str, question: &Question) -> Vec<Line<'static>> {
    let v = serde_json::to_value(question).unwrap_or(Value::Null);
    let kind = v.get("type").and_then(Value::as_str).unwrap_or("raw");
    let instructions = v.get("instructions").map(text_of).unwrap_or_default();
    let mut lines = vec![Line::from(vec![
        dim(format!("  {}. ", index + 1)),
        bold(name.to_owned()),
        Span::raw("  "),
        Span::styled(kind.to_owned(), Style::new().fg(color_for(kind))),
        Span::raw("  "),
        dim(instructions),
    ])];
    match (kind, v.get("criteria")) {
        ("choice", Some(Value::Object(map))) => {
            for (label, desc) in map {
                lines.push(Line::from(vec![
                    Span::raw("       "),
                    Span::styled(label.clone(), Style::new().fg(CHOICE)),
                    dim(match desc {
                        Value::Null => String::new(),
                        v => format!("{}", text_of(v)),
                    }),
                ]));
            }
        }
        ("score", Some(Value::Array(levels))) => {
            for (i, level) in levels.iter().enumerate() {
                lines.push(Line::from(vec![
                    Span::raw("       "),
                    Span::styled(i.to_string(), Style::new().fg(SCORE)),
                    dim(format!(" {}", text_of(level))),
                ]));
            }
        }
        ("noul", Some(Value::Object(map))) => {
            for (key, v) in map {
                let label = if key == "true" { "yes" } else { "no" };
                lines.push(Line::from(vec![
                    Span::raw("       "),
                    Span::styled(label.to_owned(), Style::new().fg(NOUL)),
                    dim(format!("{}", text_of(v))),
                ]));
            }
        }
        _ => {}
    }
    lines
}

/// Errors are part of the lesson: show the variant, what it means, and what to do.
pub fn error_lines(err: &Error) -> Vec<Line<'static>> {
    let (variant, advice) = match err {
        Error::Config(_) => ("Config", "Fix the client settings — :key sets an API key."),
        Error::InvalidRequest(_) => (
            "InvalidRequest",
            "Rejected before anything was sent; nothing reached the API.",
        ),
        Error::Api(e) => (
            "Api",
            match e.status {
                401 => "The API key is missing or wrong.",
                403 => "The key is valid but not allowed to do this.",
                422 => "The server rejected the body — check the question criteria.",
                429 => "Rate limited; the SDK already retried with backoff.",
                s if s >= 500 => "Server-side; the SDK already retried with backoff.",
                _ => "Non-2xx after retries.",
            },
        ),
        Error::Connection(_) => (
            "Connection",
            "No response: DNS, TLS, reset or a dropped body.",
        ),
        Error::Timeout(_) => (
            "Timeout",
            "An attempt ran past its per-attempt timeout — see :timeout.",
        ),
        Error::ResponseValidation(_) => (
            "ResponseValidation",
            "A 2xx body was missing required data; field_path points at it.",
        ),
        _ => ("Error", "Unhandled variant."),
    };

    let mut lines = vec![Line::from(vec![
        Span::styled(
            format!("  {variant}  "),
            Style::new().fg(BAD).add_modifier(Modifier::BOLD),
        ),
        Span::raw(err.to_string()),
    ])];
    if let Error::ResponseValidation(e) = err {
        lines.push(Line::from(vec![
            Span::raw("    "),
            dim(format!("field_path: {}", e.field_path)),
        ]));
    }
    if let Some(api) = err.as_api() {
        lines.push(Line::from(vec![
            Span::raw("    "),
            dim(format!("kind: {:?}", api.kind)),
            dim(match api.retry_after() {
                Some(d) => format!("   retry after {:.1}s", d.as_secs_f64()),
                None => String::new(),
            }),
        ]));
    }
    if let Some(id) = err.request_id() {
        lines.push(Line::from(vec![
            Span::raw("    "),
            dim(format!("request_id: {id}")),
        ]));
    }
    lines.push(Line::from(vec![Span::raw("    "), dim(advice)]));
    lines
}

/// JSON strings read better unquoted; everything else stays JSON.
pub fn text_of(v: &Value) -> String {
    match v {
        Value::String(s) => s.clone(),
        other => other.to_string(),
    }
}