oxframe-cli 0.17.0

The `oxframe` command: create, preview with hot reload, render and check oxframe projects
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
//! `oxframe mcp`: an MCP server on stdin/stdout.
//!
//! Speaks newline-delimited JSON-RPC, which Claude Code and the MCP SDK use,
//! and also the `Content-Length` framing of older clients. Logs stay on stderr
//! so the protocol owns stdout.

use std::io::{BufRead, Write};

use serde_json::{Value, json};

use crate::reply::Done;
use crate::{agents, check, exit, render, snapshot};

const PROTOCOL: &str = "2024-11-05";

/// Serves until stdin closes.
pub fn serve() -> Done {
    let stdin = std::io::stdin();
    let mut reader = std::io::BufReader::new(stdin.lock());
    while let Some(msg) = read_message(&mut reader) {
        if let Some(resp) = handle(&msg) {
            write_message(&resp);
        }
    }
    Done::printed(exit::OK)
}

/// One request in, a response out. Notifications return `None`.
pub fn handle(msg: &Value) -> Option<Value> {
    let method = msg.get("method").and_then(|m| m.as_str()).unwrap_or("");
    let id = msg.get("id").cloned()?;
    let params = msg.get("params").cloned().unwrap_or(json!({}));
    Some(match method {
        "initialize" => result(
            id,
            json!({
                "protocolVersion": PROTOCOL,
                "capabilities": { "tools": {} },
                "serverInfo": { "name": "oxframe", "version": env!("CARGO_PKG_VERSION") },
            }),
        ),
        "ping" => result(id, json!({})),
        "tools/list" => result(id, json!({ "tools": tools() })),
        "tools/call" => {
            let name = params.get("name").and_then(|n| n.as_str()).unwrap_or("");
            let args = params.get("arguments").cloned().unwrap_or(json!({}));
            let (text, is_error) = call_tool(name, &args);
            result(
                id,
                json!({
                    "content": [{ "type": "text", "text": text }],
                    "isError": is_error,
                }),
            )
        }
        _ => error(id, -32601, format!("method not found: {method}")),
    })
}

fn tools() -> Vec<Value> {
    vec![
        tool(
            "check",
            "Compile the project and run the visual check. Returns report.json, the storyboard path and hints for common mistakes.",
            json!({
                "type": "object",
                "properties": {
                    "frames": { "type": "boolean", "description": "Also write every sampled frame as its own PNG." }
                }
            }),
        ),
        tool(
            "storyboard",
            "Run the visual check and return the storyboard path plus a short summary of report.json.",
            json!({ "type": "object", "properties": {} }),
        ),
        tool(
            "render_frame",
            "Render one frame as a PNG and return the path the project prints.",
            json!({
                "type": "object",
                "properties": {
                    "at": { "type": "number", "description": "Time in seconds." },
                    "scene": { "type": "string" },
                    "size": { "type": "string", "description": "WxH, default 960x540." }
                },
                "required": ["at"]
            }),
        ),
        tool(
            "inspect",
            "Scene tree at one time: kind, name, bounds, text and file:line. While a transition is playing, also kind, direction and progress.",
            json!({
                "type": "object",
                "properties": {
                    "at": { "type": "number" },
                    "scene": { "type": "string" }
                },
                "required": ["at"]
            }),
        ),
        tool(
            "render_graph",
            "Render passes at one time, in the order they run: vector passes, layers composited over the frame (opacity, blend, blur, clip), and effect passes with their shader (lens_drops reads the frame, ripple changes one node). Use it to see why a shader is skipped or a layer is costly.",
            json!({
                "type": "object",
                "properties": {
                    "at": { "type": "number", "description": "Time in seconds." },
                    "scene": { "type": "string" }
                },
                "required": ["at"]
            }),
        ),
        tool(
            "list_components",
            "The explainer components (TitleCard, BulletList, CodeExplainer, FlowDiagram, Callout, LowerThird, EndCard, ChapterMarker) and a one-line example of each.",
            json!({ "type": "object", "properties": {} }),
        ),
        tool(
            "docs_search",
            "Search this version of the oxframe docs bundled with the CLI (AGENTS.md, the skill and the API cheat sheet).",
            json!({
                "type": "object",
                "properties": {
                    "query": { "type": "string" }
                },
                "required": ["query"]
            }),
        ),
        tool(
            "render",
            "Release-build the project and render the video (or a PNG sequence).",
            json!({
                "type": "object",
                "properties": {
                    "scene": { "type": "string" },
                    "aspect": { "type": "string", "description": "16:9, 9:16, 1:1 or all." },
                    "format": { "type": "string" },
                    "size": { "type": "string" },
                    "cpu": { "type": "boolean" }
                }
            }),
        ),
    ]
}

fn tool(name: &str, description: &str, schema: Value) -> Value {
    json!({
        "name": name,
        "description": description,
        "inputSchema": schema,
    })
}

fn call_tool(name: &str, args: &Value) -> (String, bool) {
    match name {
        "check" => text_of(check::run(args["frames"].as_bool().unwrap_or(false), true)),
        "storyboard" => {
            let done = check::run(false, true);
            let summary = storyboard_summary(&done.body);
            let mut body = done.body.clone();
            if let Some(obj) = body.as_object_mut() {
                obj.insert("summary".to_owned(), json!(summary));
            }
            (
                serde_json::to_string_pretty(&body).unwrap_or_default(),
                done.code != exit::OK,
            )
        }
        "render_frame" => {
            let at = args["at"].as_f64();
            if at.is_none() {
                return ("render_frame needs a number `at` (seconds).".into(), true);
            }
            text_of(snapshot::run(snapshot::SnapshotArgs {
                at: vec![at.unwrap()],
                out: None,
                scene: arg_string(args, "scene"),
                size: arg_string(args, "size"),
                release: false,
                json: true,
            }))
        }
        "inspect" => {
            let Some(at) = args["at"].as_f64() else {
                return ("inspect needs a number `at` (seconds).".into(), true);
            };
            text_of(check::inspect(at, arg_string(args, "scene"), false, true))
        }
        "render_graph" => {
            let Some(at) = args["at"].as_f64() else {
                return ("render_graph needs a number `at` (seconds).".into(), true);
            };
            let done = check::inspect(at, arg_string(args, "scene"), true, true);
            let failed = done.code != exit::OK;
            (
                serde_json::to_string_pretty(&passes_only(done.body)).unwrap_or_default(),
                failed,
            )
        }
        "list_components" => (COMPONENTS.into(), false),
        "docs_search" => {
            let query = args["query"].as_str().unwrap_or("").trim();
            if query.is_empty() {
                return ("docs_search needs a `query`.".into(), true);
            }
            (search_docs(query), false)
        }
        "render" => text_of(render::run(render::RenderArgs {
            out: None,
            scene: arg_string(args, "scene"),
            range: None,
            format: arg_string(args, "format"),
            size: arg_string(args, "size"),
            aspect: arg_string(args, "aspect"),
            cpu: args["cpu"].as_bool().unwrap_or(false),
            no_audio: false,
            json: true,
        })),
        _ => (format!("unknown tool: {name}"), true),
    }
}

fn text_of(done: Done) -> (String, bool) {
    (
        serde_json::to_string_pretty(&done.body).unwrap_or_default(),
        done.code != exit::OK,
    )
}

fn arg_string(args: &Value, key: &str) -> Option<String> {
    args.get(key)
        .and_then(|v| v.as_str())
        .map(str::trim)
        .filter(|s| !s.is_empty())
        .map(str::to_owned)
}

/// `inspect --graph` without the node tree: the time, the frame and the passes.
fn passes_only(mut body: Value) -> Value {
    if let Some(inspect) = body.get_mut("inspect").and_then(Value::as_object_mut) {
        inspect.retain(|k, _| matches!(k.as_str(), "time" | "frame" | "passes" | "transition"));
    }
    body
}

fn storyboard_summary(body: &Value) -> String {
    let path = body["storyboard"]
        .as_str()
        .unwrap_or("out/check/storyboard.png");
    let errors = body["report"]["errors"].as_u64();
    let warnings = body["report"]["warnings"].as_u64();
    match (errors, warnings) {
        (Some(e), Some(w)) => format!("{path}: {e} errors, {w} warnings"),
        _ => format!("{path}: report.json has no error counts yet"),
    }
}

const COMPONENTS: &str = "\
Explainer components, all in the prelude. Add them like any node. They start invisible and take their colors from the theme.

- TitleCard::new(\"The half\").subtitle(\"A worked example\") — opening card. next() reveals the subtitle.
- ChapterMarker::new(\"2  The search\") — chapter tab on the top safe edge.
- BulletList::new([\"First\", \"Second\"]) — rows stay hidden; each next() reveals one.
- CodeExplainer::new(\"fn main() {}\").step(1, 1, \"the whole program\") — a code card. next() moves to the following step (lines first..=last and a caption).
- FlowDiagram::new().edge(\"Parse\", \"Run\").edge(\"Run\", \"Print\") — boxes in layers from the edges. .down() stacks the layers.
- Callout::new(node).text(\"Watch the gap\") — a label with a line to a node.
- LowerThird::new(\"Ada\").role(\"walks the half\") — name plate on the bottom safe edge.
- EndCard::new(\"That's the search\").note(\"Repeat from the top\") — closing card.

Each has enter(), next() and exit(), with no arguments: card.enter().await, then card.next().await per beat, then card.exit().await.
";

fn search_docs(query: &str) -> String {
    let terms: Vec<String> = query
        .split_whitespace()
        .map(|t| t.to_ascii_lowercase())
        .filter(|t| t.len() > 1)
        .collect();
    let corpus = [
        ("AGENTS.md", agents::AGENTS_MD),
        ("reference.md", agents::REFERENCE_MD),
        ("SKILL.md", agents::SKILL_MD),
    ];
    let mut hits: Vec<(usize, &str, &str)> = Vec::new();
    for (name, text) in corpus {
        for para in text.split("\n\n") {
            let lower = para.to_ascii_lowercase();
            let score = terms.iter().filter(|t| lower.contains(t.as_str())).count();
            if score > 0 {
                hits.push((score, name, para.trim()));
            }
        }
    }
    if hits.is_empty() {
        return format!(
            "No match for {query:?} in the bundled docs. The same text is on https://oxframe.dev/llms-full.txt."
        );
    }
    hits.sort_by(|a, b| b.0.cmp(&a.0).then(a.1.cmp(b.1)));
    hits.truncate(6);
    let mut out = format!(
        "Matches in the oxframe {} docs bundled with this CLI:\n\n",
        env!("CARGO_PKG_VERSION")
    );
    for (_, name, para) in hits {
        out.push_str(&format!("## {name}\n{para}\n\n"));
    }
    out
}

fn result(id: Value, value: Value) -> Value {
    json!({"jsonrpc": "2.0", "id": id, "result": value})
}

fn error(id: Value, code: i32, message: String) -> Value {
    json!({"jsonrpc": "2.0", "id": id, "error": {"code": code, "message": message}})
}

fn write_message(value: &Value) {
    let line = serde_json::to_string(value).unwrap_or_else(|_| "{}".into());
    let mut out = std::io::stdout().lock();
    let _ = writeln!(out, "{line}");
    let _ = out.flush();
}

/// A JSON-RPC message: one JSON line, or an LSP-style `Content-Length` frame.
fn read_message(reader: &mut impl BufRead) -> Option<Value> {
    loop {
        let mut line = String::new();
        let n = reader.read_line(&mut line).ok()?;
        if n == 0 {
            return None;
        }
        let trimmed = line.trim();
        if trimmed.is_empty() {
            continue;
        }
        if trimmed.to_ascii_lowercase().starts_with("content-length:") {
            let len: usize = trimmed.split(':').nth(1)?.trim().parse().ok()?;
            loop {
                let mut header = String::new();
                if reader.read_line(&mut header).ok()? == 0 {
                    return None;
                }
                if header.trim().is_empty() {
                    break;
                }
            }
            let mut buf = vec![0u8; len];
            reader.read_exact(&mut buf).ok()?;
            return serde_json::from_slice(&buf).ok();
        }
        return serde_json::from_str(trimmed).ok();
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn initialize_lists_tools_and_answers_two_of_them() {
        let init = handle(&json!({
            "jsonrpc": "2.0",
            "id": 1,
            "method": "initialize",
            "params": { "protocolVersion": PROTOCOL, "capabilities": {} }
        }))
        .unwrap();
        assert_eq!(init["id"], 1);
        assert!(init["result"]["capabilities"]["tools"].is_object());
        assert!(
            handle(&json!({"jsonrpc": "2.0", "method": "notifications/initialized"})).is_none()
        );

        let list = handle(&json!({"jsonrpc": "2.0", "id": 2, "method": "tools/list"})).unwrap();
        let names: Vec<&str> = list["result"]["tools"]
            .as_array()
            .unwrap()
            .iter()
            .map(|t| t["name"].as_str().unwrap())
            .collect();
        for name in [
            "check",
            "storyboard",
            "render_frame",
            "inspect",
            "render_graph",
            "list_components",
            "docs_search",
            "render",
        ] {
            assert!(names.contains(&name), "{name} missing from {names:?}");
        }

        let listed = handle(&json!({
            "jsonrpc": "2.0",
            "id": 3,
            "method": "tools/call",
            "params": { "name": "list_components", "arguments": {} }
        }))
        .unwrap();
        let text = listed["result"]["content"][0]["text"].as_str().unwrap();
        assert!(text.contains("BulletList"));
        assert_eq!(listed["result"]["isError"], false);

        let found = handle(&json!({
            "jsonrpc": "2.0",
            "id": 4,
            "method": "tools/call",
            "params": { "name": "docs_search", "arguments": { "query": "next_to" } }
        }))
        .unwrap();
        assert!(
            found["result"]["content"][0]["text"]
                .as_str()
                .unwrap()
                .contains("next_to")
        );
    }

    #[test]
    fn inspect_and_check_reject_a_bad_call() {
        let inspect = handle(&json!({
            "jsonrpc": "2.0",
            "id": 5,
            "method": "tools/call",
            "params": { "name": "inspect", "arguments": {} }
        }))
        .unwrap();
        assert_eq!(inspect["result"]["isError"], true);
        assert!(
            inspect["result"]["content"][0]["text"]
                .as_str()
                .unwrap()
                .contains("`at`")
        );
        let frame = handle(&json!({
            "jsonrpc": "2.0",
            "id": 6,
            "method": "tools/call",
            "params": { "name": "render_frame", "arguments": {} }
        }))
        .unwrap();
        assert_eq!(frame["result"]["isError"], true);
        let graph = handle(&json!({
            "jsonrpc": "2.0",
            "id": 8,
            "method": "tools/call",
            "params": { "name": "render_graph", "arguments": {} }
        }))
        .unwrap();
        assert_eq!(graph["result"]["isError"], true);
    }

    #[test]
    fn render_graph_keeps_the_passes_and_drops_the_nodes() {
        let body = json!({
            "ok": true,
            "inspect": {
                "time": 2.5,
                "frame": 75,
                "nodes": [{ "id": 1 }],
                "passes": [{ "kind": "effect", "shader": "lens_drops" }],
                "size": [1920, 1080]
            }
        });
        let out = passes_only(body);
        assert_eq!(out["inspect"]["passes"][0]["shader"], "lens_drops");
        assert!(out["inspect"].get("nodes").is_none());
        assert_eq!(out["ok"], true);
    }

    #[test]
    fn reads_content_length_frames() {
        let body = r#"{"jsonrpc":"2.0","id":7,"method":"ping"}"#;
        let frame = format!("Content-Length: {}\r\n\r\n{body}", body.len());
        let mut reader = std::io::Cursor::new(frame.into_bytes());
        let msg = read_message(&mut reader).unwrap();
        let reply = handle(&msg).unwrap();
        assert_eq!(reply["id"], 7);
        assert!(reply["result"].is_object());
    }
}