Skip to main content

session_core/
review_command.rs

1//! `/review` (packages/coding-agent/src/ultron/review.ts): the typed command becomes a short prompt that asks the
2//! model to run Ultron's built-in review in one REPL cell and relay its report. The review itself is Python
3//! (`review_api`); nothing about it is in the system prompt of ordinary turns.
4//!
5//! The session expands it where every prompt and queued message enters it (agent-controller-provider.ts
6//! `toTextPrompt`: `prompt`, `steer`, `followUp` and `nextRun`; `Core::user_text`), so the TUI, queued messages,
7//! `ultron -p "/review"` and RPC clients behave the same. Clients send `/review <args>` as typed.
8
9/// `reviewPrompt`: the model-facing prompt for `/review <args>`.
10pub fn review_prompt(args: &str) -> String {
11    let trimmed = jsrt::string::trim(args);
12    // A JSON string literal is a valid Python string literal for any argument text.
13    let literal = serde_json::to_string(trimmed).unwrap_or_else(|_| "\"\"".into());
14    let mut lines = vec![
15        if trimmed.is_empty() {
16            "/review".to_string()
17        } else {
18            format!("/review {trimmed}")
19        },
20        String::new(),
21        "Run Ultron's built-in code review in one REPL cell, exactly:".into(),
22        String::new(),
23        "import review_api".into(),
24        format!("review = await review_api.run(rlm, {literal})"),
25        "print(review.report)".into(),
26        String::new(),
27        "Then reply with the printed report as it is. Do not review the code yourself, edit files, or run the review again unless the cell raised an error.".into(),
28    ];
29    if posts(trimmed) {
30        lines.push(String::new());
31        lines.push("If the report says a post is pending, ask me whether to post it to the pull request. Post only after I answer yes in a later message, with `await review_api.post(review, confirm=True)`; without that answer, never post.".into());
32    }
33    lines.join("\n")
34}
35
36/// `/(^|\s)--post(\s|=|$)/`.
37fn posts(args: &str) -> bool {
38    args.match_indices("--post").any(|(at, flag)| {
39        let before = args[..at].chars().next_back();
40        let after = args[at + flag.len()..].chars().next();
41        before.is_none_or(jsrt::string::is_space) && after.is_none_or(|c| jsrt::string::is_space(c) || c == '=')
42    })
43}
44
45/// `expandReviewCommand`: `/review [args]` (after trimming; `/^\/review(?:\s+([\s\S]*))?$/`) becomes the review
46/// prompt; any other text is returned unchanged.
47pub fn expand_review_command(text: &str) -> String {
48    let Some(rest) = jsrt::string::trim(text).strip_prefix("/review") else {
49        return text.to_string();
50    };
51    if rest.is_empty() {
52        return review_prompt("");
53    }
54    if rest.starts_with(jsrt::string::is_space) {
55        return review_prompt(rest);
56    }
57    text.to_string()
58}