modelsdev 0.11.4

A fast TUI and CLI for browsing AI models, benchmarks, and coding agents
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
// src/agents/changelog_parser.rs

use comrak::nodes::NodeValue;
use comrak::{parse_document, Arena, Options};

/// A normalized block in a parsed changelog.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ChangelogBlock {
    /// A section heading (from ## or ### headers).
    Heading(String),
    /// A bullet item (from list items). May contain nested bullets.
    Bullet(String),
    /// A prose paragraph.
    Paragraph(String),
}

/// Structured changelog: a flat list of blocks representing the release body.
#[derive(Debug, Clone)]
pub struct Changelog {
    pub blocks: Vec<ChangelogBlock>,
}

/// Headers that wrap the actual content without adding structure.
const SKIP_HEADERS: &[&str] = &["What's Changed", "Changelog", "Full Changelog"];

fn is_skip_header(name: &str) -> bool {
    SKIP_HEADERS.iter().any(|h| name.starts_with(h))
}

/// Extract plain text content from an AST node and its inline children.
fn collect_text<'a>(node: &'a comrak::nodes::AstNode<'a>) -> String {
    let mut text = String::new();
    for child in node.children() {
        match &child.data.borrow().value {
            NodeValue::Text(t) => text.push_str(t),
            NodeValue::Code(c) => {
                text.push('`');
                text.push_str(&c.literal);
                text.push('`');
            }
            NodeValue::SoftBreak | NodeValue::LineBreak => text.push(' '),
            NodeValue::Emph => {
                let inner = collect_text(child);
                text.push_str(&inner);
            }
            NodeValue::Strong => {
                let inner = collect_text(child);
                text.push_str(&inner);
            }
            NodeValue::Link(link) => {
                let label = collect_text(child);
                if label.is_empty() {
                    text.push_str(&link.url);
                } else {
                    text.push_str(&label);
                }
            }
            NodeValue::Strikethrough => {
                let inner = collect_text(child);
                text.push_str(&inner);
            }
            _ => {
                // Recurse into other inline nodes
                let inner = collect_text(child);
                text.push_str(&inner);
            }
        }
    }
    text
}

/// Collect bullet items from a list node, flattening nested lists.
fn collect_list_items<'a>(node: &'a comrak::nodes::AstNode<'a>, items: &mut Vec<String>) {
    for child in node.children() {
        if let NodeValue::Item(_) = &child.data.borrow().value {
            let mut item_text = String::new();
            let mut has_nested = false;
            for item_child in child.children() {
                match &item_child.data.borrow().value {
                    NodeValue::Paragraph => {
                        let para = collect_text(item_child);
                        if !para.is_empty() {
                            if !item_text.is_empty() {
                                item_text.push(' ');
                            }
                            item_text.push_str(&para);
                        }
                    }
                    NodeValue::List(_) => {
                        // Push the parent item first, then recurse
                        has_nested = true;
                        if !item_text.is_empty() {
                            items.push(item_text.clone());
                            item_text.clear();
                        }
                        collect_list_items(item_child, items);
                    }
                    _ => {
                        let text = collect_text(item_child);
                        if !text.is_empty() {
                            if !item_text.is_empty() {
                                item_text.push(' ');
                            }
                            item_text.push_str(&text);
                        }
                    }
                }
            }
            if !item_text.is_empty() || !has_nested {
                items.push(item_text);
            }
        }
    }
}

/// Parse a GitHub release body into a normalized changelog IR using comrak.
pub fn parse_changelog(body: &str) -> Changelog {
    let arena = Arena::new();
    let opts = Options::default();
    let root = parse_document(&arena, body, &opts);

    let mut blocks = Vec::new();

    for node in root.children() {
        match &node.data.borrow().value {
            NodeValue::Heading(heading) => {
                // Only process ## and ### (level 2+), skip # (title)
                if heading.level >= 2 {
                    let text = collect_text(node);
                    let trimmed = text.trim().to_string();
                    if !is_skip_header(&trimmed) && !trimmed.is_empty() {
                        blocks.push(ChangelogBlock::Heading(trimmed));
                    }
                }
            }
            NodeValue::List(_) => {
                let mut items = Vec::new();
                collect_list_items(node, &mut items);
                for item in items {
                    blocks.push(ChangelogBlock::Bullet(item));
                }
            }
            NodeValue::Paragraph => {
                let text = collect_text(node);
                let trimmed = text.trim().to_string();
                if !trimmed.is_empty() {
                    blocks.push(ChangelogBlock::Paragraph(trimmed));
                }
            }
            _ => {}
        }
    }

    Changelog { blocks }
}

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

    #[derive(Debug)]
    struct Section {
        name: String,
        changes: Vec<String>,
    }

    /// Convert IR back to legacy format for backward-compatibility tests.
    fn parse_release_body(body: &str) -> (Vec<Section>, Vec<String>) {
        let changelog = parse_changelog(body);
        let mut sections: Vec<Section> = Vec::new();
        let mut ungrouped: Vec<String> = Vec::new();
        let mut current_section: Option<Section> = None;

        for block in changelog.blocks {
            match block {
                ChangelogBlock::Heading(name) => {
                    if let Some(sec) = current_section.take() {
                        if !sec.changes.is_empty() {
                            sections.push(sec);
                        }
                    }
                    current_section = Some(Section {
                        name,
                        changes: Vec::new(),
                    });
                }
                ChangelogBlock::Bullet(text) | ChangelogBlock::Paragraph(text) => {
                    if let Some(ref mut sec) = current_section {
                        sec.changes.push(text);
                    } else {
                        ungrouped.push(text);
                    }
                }
            }
        }

        if let Some(sec) = current_section {
            if !sec.changes.is_empty() {
                sections.push(sec);
            }
        }

        (sections, ungrouped)
    }

    // --- Legacy API tests (must not regress) ---

    #[test]
    fn sectioned_changelog() {
        let body = "\
## Bug Fixes
- Fixed crash on startup
- Fixed memory leak

## Features
- Added dark mode
- Added export to CSV";

        let (sections, ungrouped) = parse_release_body(body);
        assert!(ungrouped.is_empty());
        assert_eq!(sections.len(), 2);
        assert_eq!(sections[0].name, "Bug Fixes");
        assert_eq!(sections[0].changes.len(), 2);
        assert_eq!(sections[0].changes[0], "Fixed crash on startup");
        assert_eq!(sections[0].changes[1], "Fixed memory leak");
        assert_eq!(sections[1].name, "Features");
        assert_eq!(sections[1].changes.len(), 2);
        assert_eq!(sections[1].changes[0], "Added dark mode");
        assert_eq!(sections[1].changes[1], "Added export to CSV");
    }

    #[test]
    fn ungrouped_changes() {
        let body = "\
- Fixed crash on startup
- Added dark mode
- Improved performance";

        let (sections, ungrouped) = parse_release_body(body);
        assert!(sections.is_empty());
        assert_eq!(ungrouped.len(), 3);
        assert_eq!(ungrouped[0], "Fixed crash on startup");
        assert_eq!(ungrouped[1], "Added dark mode");
        assert_eq!(ungrouped[2], "Improved performance");
    }

    #[test]
    fn skip_whats_changed_header() {
        let body = "\
## What's Changed
- Fixed crash on startup
- Added dark mode";

        let (sections, ungrouped) = parse_release_body(body);
        assert!(sections.is_empty());
        assert_eq!(ungrouped.len(), 2);
        assert_eq!(ungrouped[0], "Fixed crash on startup");
        assert_eq!(ungrouped[1], "Added dark mode");
    }

    #[test]
    fn asterisk_bullets() {
        let body = "\
## Changes
* Fixed crash on startup
* Added dark mode";

        let (sections, ungrouped) = parse_release_body(body);
        assert!(ungrouped.is_empty());
        assert_eq!(sections.len(), 1);
        assert_eq!(sections[0].changes.len(), 2);
        assert_eq!(sections[0].changes[0], "Fixed crash on startup");
        assert_eq!(sections[0].changes[1], "Added dark mode");
    }

    #[test]
    fn empty_body() {
        let (sections, ungrouped) = parse_release_body("");
        assert!(sections.is_empty());
        assert!(ungrouped.is_empty());
    }

    #[test]
    fn mixed_sections_and_ungrouped() {
        let body = "\
- Ungrouped item 1
- Ungrouped item 2

## Bug Fixes
- Fixed crash on startup

## Features
- Added dark mode";

        let (sections, ungrouped) = parse_release_body(body);
        assert_eq!(ungrouped.len(), 2);
        assert_eq!(ungrouped[0], "Ungrouped item 1");
        assert_eq!(ungrouped[1], "Ungrouped item 2");
        assert_eq!(sections.len(), 2);
        assert_eq!(sections[0].name, "Bug Fixes");
        assert_eq!(sections[0].changes[0], "Fixed crash on startup");
        assert_eq!(sections[1].name, "Features");
        assert_eq!(sections[1].changes[0], "Added dark mode");
    }

    // --- New IR tests ---

    #[test]
    fn ir_preserves_paragraphs() {
        let body = "\
Some introductory text about this release.

## Changes
- Fixed a bug";

        let changelog = parse_changelog(body);
        assert_eq!(changelog.blocks.len(), 3);
        assert_eq!(
            changelog.blocks[0],
            ChangelogBlock::Paragraph("Some introductory text about this release.".to_string())
        );
        assert_eq!(
            changelog.blocks[1],
            ChangelogBlock::Heading("Changes".to_string())
        );
        assert_eq!(
            changelog.blocks[2],
            ChangelogBlock::Bullet("Fixed a bug".to_string())
        );
    }

    #[test]
    fn ir_handles_inline_formatting() {
        let body = "- Fixed **crash** in `main()` function";

        let changelog = parse_changelog(body);
        assert_eq!(changelog.blocks.len(), 1);
        assert_eq!(
            changelog.blocks[0],
            ChangelogBlock::Bullet("Fixed crash in `main()` function".to_string())
        );
    }

    #[test]
    fn ir_handles_links() {
        let body = "- See [the docs](https://example.com) for details";

        let changelog = parse_changelog(body);
        assert_eq!(changelog.blocks.len(), 1);
        assert_eq!(
            changelog.blocks[0],
            ChangelogBlock::Bullet("See the docs for details".to_string())
        );
    }

    #[test]
    fn ir_flattens_nested_lists() {
        let body = "\
- Parent item
  - Child item 1
  - Child item 2";

        let changelog = parse_changelog(body);
        assert_eq!(changelog.blocks.len(), 3);
        assert_eq!(
            changelog.blocks[0],
            ChangelogBlock::Bullet("Parent item".to_string())
        );
        assert_eq!(
            changelog.blocks[1],
            ChangelogBlock::Bullet("Child item 1".to_string())
        );
        assert_eq!(
            changelog.blocks[2],
            ChangelogBlock::Bullet("Child item 2".to_string())
        );
    }

    #[test]
    fn ir_skips_wrapper_headers() {
        let body = "\
## What's Changed
- Item 1

## Full Changelog
https://github.com/example/compare/v1...v2";

        let changelog = parse_changelog(body);
        // "What's Changed" and "Full Changelog" headers should be skipped
        assert!(changelog
            .blocks
            .iter()
            .all(|b| !matches!(b, ChangelogBlock::Heading(_))));
        assert_eq!(
            changelog.blocks[0],
            ChangelogBlock::Bullet("Item 1".to_string())
        );
    }

    #[test]
    fn ir_whitespace_only_body() {
        let changelog = parse_changelog("   \n\n  ");
        assert!(changelog.blocks.is_empty());
    }

    #[test]
    fn ir_mixed_prose_and_sections() {
        let body = "\
This release includes important updates.

## Breaking Changes
- Removed deprecated API
- Changed default timeout

Some additional notes about migration.

## Bug Fixes
- Fixed memory leak";

        let changelog = parse_changelog(body);
        let headings: Vec<_> = changelog
            .blocks
            .iter()
            .filter_map(|b| match b {
                ChangelogBlock::Heading(h) => Some(h.as_str()),
                _ => None,
            })
            .collect();
        assert_eq!(headings, vec!["Breaking Changes", "Bug Fixes"]);

        let bullets: Vec<_> = changelog
            .blocks
            .iter()
            .filter_map(|b| match b {
                ChangelogBlock::Bullet(t) => Some(t.as_str()),
                _ => None,
            })
            .collect();
        assert_eq!(
            bullets,
            vec![
                "Removed deprecated API",
                "Changed default timeout",
                "Fixed memory leak"
            ]
        );

        let paragraphs: Vec<_> = changelog
            .blocks
            .iter()
            .filter_map(|b| match b {
                ChangelogBlock::Paragraph(t) => Some(t.as_str()),
                _ => None,
            })
            .collect();
        assert_eq!(
            paragraphs,
            vec![
                "This release includes important updates.",
                "Some additional notes about migration."
            ]
        );
    }
}