Skip to main content

jev_repl/
headless.rs

1//! jev with no terminal in the way: a page in, an answer page out.
2//!
3//! The REPL is the place to shape a request; once it is shaped, the same session is something a
4//! script wants — in a pipe, in a Makefile, in CI. Everything here is the pure half of that: text
5//! in, text out, no terminal and no process, so a test can drive it without a PTY.
6//!
7//! ```
8//! # use jev_repl::headless;
9//! let session = headless::load("A payout failed.\n---\nis_urgent? Conveys urgency").unwrap();
10//! println!("{}", headless::request_text(&session, "jev-latest"));
11//! ```
12
13use ratatui::text::Line;
14use serde_json::Value;
15use typesafe::{Answer, Question, SystemOneResponse, Usage};
16
17use crate::cost::Rates;
18use crate::format::{answer_lines, cost_lines};
19use crate::session::Session;
20use crate::{codegen, cost, mock, session, sketch};
21
22/// The subcommands that run without a terminal, and what each one prints.
23pub const COMMANDS: &[(&str, &str)] = &[
24    ("run", "send the request and print the answers"),
25    (
26        "eval",
27        "run a page over a file of labelled cases and score the answers",
28    ),
29    ("json", "the exact request body this session POSTs"),
30    (
31        "cost",
32        "what a call costs, per question and on both sides of the wire",
33    ),
34    ("rust", "the session as a program against typesafe-ai-sdk"),
35    ("check", "parse the input and report what is wrong with it"),
36];
37
38/// Whether `word` names a subcommand, so `jev <word>` is not mistaken for a flag or a path.
39pub fn is_command(word: &str) -> bool {
40    COMMANDS.iter().any(|(name, _)| *name == word)
41}
42
43/// One question's answer, or `None` when nothing answered it.
44pub type Answered = (String, Option<Answer>);
45
46/// Read a session from a sketch page or a request body.
47///
48/// Which one it is comes from the text, not the file name: stdin has no extension, and a here-doc
49/// piped in should behave the same as the file it was copied from. A leading `{` is a request
50/// body; anything else is a page.
51pub fn load(text: &str) -> Result<Session, String> {
52    if text.trim().is_empty() {
53        return Err("Nothing to read: the input is empty.".to_owned());
54    }
55    if text.trim_start().starts_with('{') {
56        return session::from_body(text);
57    }
58    let page = sketch::parse(text);
59    match page.problems.first() {
60        Some(problem) => Err(format!("line {}: {}", problem.line + 1, problem.message)),
61        None => Ok(page.to_session()),
62    }
63}
64
65/// What `jev check` says about a page: every problem, not just the first.
66pub fn check_text(text: &str) -> Result<String, String> {
67    if text.trim().is_empty() {
68        return Err("Nothing to read: the input is empty.".to_owned());
69    }
70    if text.trim_start().starts_with('{') {
71        return session::from_body(text).map(|s| describe(&s));
72    }
73    let page = sketch::parse(text);
74    if !page.ok() {
75        return Err(page
76            .problems
77            .iter()
78            .map(|p| format!("line {}: {}", p.line + 1, p.message))
79            .collect::<Vec<_>>()
80            .join("\n"));
81    }
82    Ok(describe(&page.to_session()))
83}
84
85/// `3 questions: is_urgent (noul), department (choice)` — enough to see the parse landed right.
86fn describe(session: &Session) -> String {
87    let n = session.questions.len();
88    let kinds = session
89        .questions
90        .iter()
91        .map(|(name, q)| format!("{name} ({})", kind_of(q)))
92        .collect::<Vec<_>>()
93        .join(", ");
94    let plural = if n == 1 { "" } else { "s" };
95    let head = if kinds.is_empty() {
96        format!("{n} question{plural}")
97    } else {
98        format!("{n} question{plural}: {kinds}")
99    };
100    if session.state_is_empty() {
101        format!("{head}\nno state — pass --state <text> before sending")
102    } else {
103        head
104    }
105}
106
107/// The wire `type` of a question, for the one-line summary `check` prints.
108fn kind_of(question: &Question) -> String {
109    match question {
110        Question::Noul(_) => "noul".to_owned(),
111        Question::Choice(_) => "choice".to_owned(),
112        Question::Score(_) => "score".to_owned(),
113        Question::Raw(value) => value
114            .get("type")
115            .and_then(Value::as_str)
116            .unwrap_or("raw")
117            .to_owned(),
118        // `Question` is non-exhaustive: a shape this build does not know still has a name.
119        _ => "question".to_owned(),
120    }
121}
122
123/// Why this session cannot be sent yet, if it cannot.
124pub fn sendable(session: &Session) -> Option<String> {
125    if session.questions.is_empty() {
126        return Some("No questions: the request would ask nothing.".to_owned());
127    }
128    if session.state_is_empty() {
129        return Some("No state: pass --state <text>, or put one above the `---`.".to_owned());
130    }
131    None
132}
133
134/// The exact body the SDK would POST.
135pub fn request_text(session: &Session, model: &str) -> String {
136    format!("{}\n", session.request_json(model))
137}
138
139/// Simulated answers, the same deterministic ones the REPL shows offline.
140pub fn mock_answers(session: &Session) -> Vec<Answered> {
141    session
142        .questions
143        .iter()
144        .map(|(name, q)| {
145            let json = serde_json::to_value(q).unwrap_or(Value::Null);
146            (name.clone(), mock::answer(&session.state, name, &json))
147        })
148        .collect()
149}
150
151/// The answers a live response carries, lined up with the questions that were asked.
152pub fn live_answers(session: &Session, response: &SystemOneResponse) -> Vec<Answered> {
153    session
154        .questions
155        .iter()
156        .map(|(name, _)| (name.clone(), response.answers.get(name).cloned()))
157        .collect()
158}
159
160/// The answers a cached body carries, lined up with the questions that were asked.
161///
162/// The wire body is all the cache keeps, and `SystemOneResponse` cannot be rebuilt outside the SDK
163/// crate, so this is [`live_answers`] for a response that arrived from disk instead of the
164/// network. `None` means the file is not a System One body, which is a cache miss and not an error.
165pub fn cached_answers(session: &Session, body: &Value) -> Option<(Vec<Answered>, Option<Usage>)> {
166    let object = body.as_object()?;
167    object.get("model")?.as_str()?;
168    let mut decoded: Vec<(String, Answer)> = Vec::new();
169    for (name, value) in object.get("answers")?.as_object()? {
170        let answer = match value.get("type")?.as_str()? {
171            "noul" => Answer::Noul(serde_json::from_value(value.clone()).ok()?),
172            "choice" => Answer::Choice(serde_json::from_value(value.clone()).ok()?),
173            "score" => Answer::Score(serde_json::from_value(value.clone()).ok()?),
174            // An answer this build does not model is skipped, the way the SDK's decoder skips it.
175            _ => continue,
176        };
177        decoded.push((name.clone(), answer));
178    }
179    let answers = session
180        .questions
181        .iter()
182        .map(|(name, _)| {
183            let answer = decoded
184                .iter()
185                .find(|(n, _)| n == name)
186                .map(|(_, a)| a.clone());
187            (name.clone(), answer)
188        })
189        .collect();
190    let usage = object
191        .get("usage")
192        .and_then(|u| serde_json::from_value::<Usage>(u.clone()).ok());
193    Some((answers, usage))
194}
195
196/// The answer page: the same bars and labels the REPL draws, minus the colour.
197pub fn answers_text(answers: &[Answered], threshold: f64) -> String {
198    let mut out = String::new();
199    for (name, answer) in answers {
200        match answer {
201            Some(a) => out.push_str(&plain(answer_lines(name, a, threshold))),
202            None => out.push_str(&format!(
203                "  {name}: no answer came back for this question.\n"
204            )),
205        }
206    }
207    out
208}
209
210/// The raw body, for `--json`: what arrived live, or the shape a mock answer would have arrived in.
211pub fn answers_json(answers: &[Answered], model: &str, raw: Option<&Value>) -> String {
212    match raw {
213        Some(value) => format!(
214            "{}\n",
215            serde_json::to_string_pretty(value).unwrap_or_default()
216        ),
217        None => format!("{}\n", mock::body(answers, model)),
218    }
219}
220
221/// The token table, priced when rates were supplied.
222pub fn cost_text(session: &Session, model: &str, rates: Option<Rates>) -> String {
223    let estimate = cost::estimate(session, model);
224    let hint = "--price 0.20/1.00 prices it: dollars per million tokens, input then output";
225    plain(cost_lines(&estimate, rates, hint))
226}
227
228/// The one-line footer under an answer page: tokens, and money when the rates are known.
229pub fn usage_text(
230    session: &Session,
231    model: &str,
232    rates: Option<Rates>,
233    usage: Option<&Usage>,
234) -> String {
235    if let Some(usage) = usage
236        && let (Some(input), Some(output)) = (usage.input_tokens, usage.output_tokens)
237    {
238        let money = match rates.and_then(|r| cost::price_usage(usage, r)) {
239            Some(cost) => format!(" · {}", cost::usd(cost.total)),
240            None => String::new(),
241        };
242        return format!("  {input} in / {output} out tokens{money}\n");
243    }
244    let estimate = cost::estimate(session, model);
245    let money = match rates {
246        Some(rates) => format!(
247            " · {}",
248            cost::usd(cost::price_estimate(&estimate, rates).total)
249        ),
250        None => String::new(),
251    };
252    format!(
253        "  ≈ {} in / {} out tokens{money} — estimated, nothing was counted\n",
254        estimate.input_tokens, estimate.output_tokens
255    )
256}
257
258/// The session as code, for `jev rust`.
259pub fn code_text(session: &Session, model: &str, threshold: f64) -> String {
260    let code = codegen::rust(session, model, threshold);
261    if code.ends_with('\n') {
262        code
263    } else {
264        format!("{code}\n")
265    }
266}
267
268/// Styled lines as the plain text a pipe wants.
269fn plain(lines: Vec<Line<'static>>) -> String {
270    let mut out = String::new();
271    for line in lines {
272        for span in &line.spans {
273            out.push_str(span.content.as_ref());
274        }
275        out.push('\n');
276    }
277    out
278}