vectorless 0.1.31

Reasoning-native document intelligence engine for AI
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
// Copyright (c) 2026 vectorless developers
// SPDX-License-Identifier: Apache-2.0

//! Command parsing for the agent navigation loop.
//!
//! LLM output is parsed into `Command` variants. The parser is intentionally
//! simple and forgiving — unknown input falls back to `Ls` so the agent can
//! re-observe its surroundings.

use crate::document::{NavigationIndex, NodeId};

/// Parsed command from LLM output.
#[derive(Debug, Clone, PartialEq)]
pub enum Command {
    /// List children of the current node.
    Ls,
    /// Navigate into a child node by name.
    Cd { target: String },
    /// Navigate back to parent.
    CdUp,
    /// Read node content (collects as evidence).
    Cat { target: String },
    /// Search for a keyword in the ReasoningIndex.
    Find { keyword: String },
    /// Regex search across node content in the current subtree.
    Grep { pattern: String },
    /// Preview first N lines of a node without collecting evidence.
    Head { target: String, lines: usize },
    /// Search for nodes by title pattern in the tree.
    FindTree { pattern: String },
    /// Show node content size (lines, chars).
    Wc { target: String },
    /// Show current navigation path.
    Pwd,
    /// Evaluate evidence sufficiency.
    Check,
    /// End navigation.
    Done,
}

/// Strip surrounding quotes from a target string.
///
/// Handles straight quotes (`"`, `'`) and Unicode smart quotes (U+201C/U+201D, U+2018/U+2019).
fn strip_quotes(s: &str) -> String {
    let trimmed = s.trim();
    let chars: Vec<char> = trimmed.chars().collect();
    if chars.len() < 2 {
        return trimmed.to_string();
    }
    let (first, last) = (chars[0], chars[chars.len() - 1]);
    let matching = (first == '"' && last == '"')
        || (first == '\'' && last == '\'')
        || (first == '\u{201c}' && last == '\u{201d}')
        || (first == '\u{2018}' && last == '\u{2019}');
    if matching {
        trimmed[chars[0].len_utf8()..trimmed.len() - chars[chars.len() - 1].len_utf8()].to_string()
    } else {
        trimmed.to_string()
    }
}

/// Parse the first non-empty line of LLM output into a Command.
pub fn parse_command(llm_output: &str) -> Command {
    let line = llm_output
        .lines()
        .find(|l| !l.trim().is_empty())
        .unwrap_or("")
        .trim();

    // Remove common wrapping (markdown code blocks, etc.)
    let line = line.trim_start_matches('`').trim_end_matches('`').trim();

    let parts: Vec<&str> = line.split_whitespace().collect();

    match parts.as_slice() {
        ["ls"] => Command::Ls,
        ["cat"] => Command::Cat {
            target: ".".to_string(),
        },
        ["cd", ".."] => Command::CdUp,
        ["cd", target] => Command::Cd {
            target: strip_quotes(target),
        },
        ["cd", _target, ..] => Command::Cd {
            // Handle "cd some name" by joining remaining parts
            target: strip_quotes(&parts[1..].join(" ")),
        },
        ["cat", target] => Command::Cat {
            target: strip_quotes(target),
        },
        ["cat", _target, ..] => Command::Cat {
            target: strip_quotes(&parts[1..].join(" ")),
        },
        ["find", keyword] => Command::Find {
            keyword: strip_quotes(keyword),
        },
        ["find", _keyword, ..] => Command::Find {
            keyword: strip_quotes(&parts[1..].join(" ")),
        },
        ["grep", pattern] => Command::Grep {
            pattern: strip_quotes(pattern),
        },
        ["grep", _pattern, ..] => Command::Grep {
            pattern: strip_quotes(&parts[1..].join(" ")),
        },
        ["head", target] => Command::Head {
            target: strip_quotes(target),
            lines: 20, // default
        },
        ["head", "-n", n, target @ ..] => Command::Head {
            target: strip_quotes(&target.join(" ")),
            lines: n.parse().unwrap_or(20),
        },
        ["head", _target, ..] => Command::Head {
            target: strip_quotes(&parts[1..].join(" ")),
            lines: 20,
        },
        ["findtree", pattern] => Command::FindTree {
            pattern: strip_quotes(pattern),
        },
        ["findtree", _pattern, ..] => Command::FindTree {
            pattern: strip_quotes(&parts[1..].join(" ")),
        },
        ["wc", target] => Command::Wc {
            target: strip_quotes(target),
        },
        ["wc", _target, ..] => Command::Wc {
            target: strip_quotes(&parts[1..].join(" ")),
        },
        ["pwd"] => Command::Pwd,
        ["check"] => Command::Check,
        ["done"] => Command::Done,
        _ => Command::Ls, // fallback: re-observe
    }
}

/// Resolve a cd/cat target string to a NodeId using multi-level matching.
///
/// Matching priority:
/// 1. Exact title match
/// 2. Case-insensitive title match
/// 3. Substring (contains) match
/// 4. Numeric index match ("1" → first child, "2" → second, etc.)
pub fn resolve_target(
    target: &str,
    nav_index: &NavigationIndex,
    current_node: NodeId,
) -> Option<NodeId> {
    let target = strip_quotes(target);
    let routes = nav_index.get_child_routes(current_node)?;

    // 1. Exact match
    if let Some(r) = routes.iter().find(|r| r.title == target) {
        return Some(r.node_id);
    }

    // 2. Case-insensitive match
    let target_lower = target.to_lowercase();
    if let Some(r) = routes
        .iter()
        .find(|r| r.title.to_lowercase() == target_lower)
    {
        return Some(r.node_id);
    }

    // 3. Substring (contains) match
    if let Some(r) = routes
        .iter()
        .find(|r| r.title.to_lowercase().contains(&target_lower))
    {
        return Some(r.node_id);
    }

    // 4. Numeric index match ("1" → first child)
    if let Ok(idx) = target.parse::<usize>() {
        if idx > 0 && idx <= routes.len() {
            return Some(routes[idx - 1].node_id);
        }
    }

    None
}

/// Resolve a cd/cat target with additional context from the tree node titles.
///
/// This extended resolver also checks against the actual tree node titles
/// (in case NavEntry titles differ from TreeNode titles).
pub fn resolve_target_extended(
    target: &str,
    nav_index: &NavigationIndex,
    current_node: NodeId,
    tree: &crate::document::DocumentTree,
) -> Option<NodeId> {
    let target = strip_quotes(target);
    // Try the primary resolver first
    if let Some(id) = resolve_target(&target, nav_index, current_node) {
        return Some(id);
    }

    // Extended: check all children by their TreeNode titles
    let children: Vec<NodeId> = tree.children_iter(current_node).collect();
    let target_lower = target.to_lowercase();

    for child_id in &children {
        if let Some(node) = tree.get(*child_id) {
            if node.title.to_lowercase().contains(&target_lower) {
                return Some(*child_id);
            }
        }
    }

    None
}

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

    #[test]
    fn test_parse_ls() {
        assert_eq!(parse_command("ls"), Command::Ls);
        assert_eq!(parse_command("  ls  "), Command::Ls);
    }

    #[test]
    fn test_parse_cd() {
        assert_eq!(parse_command("cd .."), Command::CdUp);
        assert_eq!(
            parse_command("cd Getting Started"),
            Command::Cd {
                target: "Getting Started".to_string()
            }
        );
        assert_eq!(
            parse_command("cd some long name"),
            Command::Cd {
                target: "some long name".to_string()
            }
        );
        // Quoted multi-word targets should have quotes stripped
        assert_eq!(
            parse_command("cd \"Vectorless Architecture Guide\""),
            Command::Cd {
                target: "Vectorless Architecture Guide".to_string()
            }
        );
        assert_eq!(
            parse_command("cd 'Vectorless Architecture Guide'"),
            Command::Cd {
                target: "Vectorless Architecture Guide".to_string()
            }
        );
        // Smart quotes
        assert_eq!(
            parse_command("\u{201c}Vectorless Architecture Guide\u{201d}"),
            Command::Ls // doesn't start with a command keyword
        );
    }

    #[test]
    fn test_strip_quotes_straight() {
        assert_eq!(strip_quotes("\"hello\""), "hello");
        assert_eq!(strip_quotes("'hello'"), "hello");
        assert_eq!(strip_quotes("hello"), "hello");
        assert_eq!(strip_quotes("\"only left"), "\"only left");
    }

    #[test]
    fn test_strip_quotes_smart() {
        assert_eq!(strip_quotes("\u{201c}hello\u{201d}"), "hello");
        assert_eq!(strip_quotes("\u{2018}hello\u{2019}"), "hello");
    }

    #[test]
    fn test_resolve_target_quoted() {
        use crate::document::{ChildRoute, DocumentTree};

        let mut tree = DocumentTree::new("Root", "");
        let root = tree.root();
        let c1 = tree.add_child(root, "Vectorless Architecture Guide", "content");

        let mut nav_index = NavigationIndex::new();
        nav_index.add_child_routes(
            root,
            vec![ChildRoute {
                node_id: c1,
                title: "Vectorless Architecture Guide".to_string(),
                description: "Main guide".to_string(),
                leaf_count: 5,
            }],
        );

        // Quoted target should still resolve
        assert_eq!(
            resolve_target("\"Vectorless Architecture Guide\"", &nav_index, root),
            Some(c1)
        );
        assert_eq!(
            resolve_target("'Vectorless Architecture Guide'", &nav_index, root),
            Some(c1)
        );
    }

    #[test]
    fn test_parse_cat() {
        assert_eq!(
            parse_command("cat Installation"),
            Command::Cat {
                target: "Installation".to_string()
            }
        );
        assert_eq!(
            parse_command("cat API Reference"),
            Command::Cat {
                target: "API Reference".to_string()
            }
        );
    }

    #[test]
    fn test_parse_find() {
        assert_eq!(
            parse_command("find authentication"),
            Command::Find {
                keyword: "authentication".to_string()
            }
        );
    }

    #[test]
    fn test_parse_misc() {
        assert_eq!(parse_command("pwd"), Command::Pwd);
        assert_eq!(parse_command("check"), Command::Check);
        assert_eq!(parse_command("done"), Command::Done);
    }

    #[test]
    fn test_parse_fallback() {
        assert_eq!(parse_command(""), Command::Ls);
        assert_eq!(parse_command("unknown command"), Command::Ls);
        assert_eq!(parse_command("blah blah"), Command::Ls);
    }

    #[test]
    fn test_parse_with_wrapping() {
        assert_eq!(parse_command("`ls`"), Command::Ls);
        assert_eq!(parse_command("```ls```"), Command::Ls);
    }

    #[test]
    fn test_parse_multiline() {
        // Should parse the first non-empty line
        assert_eq!(parse_command("\n\nls\n\n// listing children"), Command::Ls);
    }

    #[test]
    fn test_resolve_target_numeric() {
        use crate::document::{ChildRoute, DocumentTree};

        let mut tree = DocumentTree::new("Root", "");
        let root = tree.root();
        let c1 = tree.add_child(root, "Getting Started", "content");
        let c2 = tree.add_child(root, "API Reference", "content");

        let mut nav_index = NavigationIndex::new();
        nav_index.add_child_routes(
            root,
            vec![
                ChildRoute {
                    node_id: c1,
                    title: "Getting Started".to_string(),
                    description: "Setup guide".to_string(),
                    leaf_count: 3,
                },
                ChildRoute {
                    node_id: c2,
                    title: "API Reference".to_string(),
                    description: "API docs".to_string(),
                    leaf_count: 7,
                },
            ],
        );

        assert_eq!(resolve_target("1", &nav_index, root), Some(c1));
        assert_eq!(resolve_target("2", &nav_index, root), Some(c2));
        assert_eq!(resolve_target("3", &nav_index, root), None);
    }

    #[test]
    fn test_resolve_target_exact() {
        use crate::document::{ChildRoute, DocumentTree};

        let mut tree = DocumentTree::new("Root", "");
        let root = tree.root();
        let c1 = tree.add_child(root, "Getting Started", "content");

        let mut nav_index = NavigationIndex::new();
        nav_index.add_child_routes(
            root,
            vec![ChildRoute {
                node_id: c1,
                title: "Getting Started".to_string(),
                description: "Setup".to_string(),
                leaf_count: 3,
            }],
        );

        assert_eq!(
            resolve_target("Getting Started", &nav_index, root),
            Some(c1)
        );
    }

    #[test]
    fn test_resolve_target_case_insensitive() {
        use crate::document::{ChildRoute, DocumentTree};

        let mut tree = DocumentTree::new("Root", "");
        let root = tree.root();
        let c1 = tree.add_child(root, "Getting Started", "content");

        let mut nav_index = NavigationIndex::new();
        nav_index.add_child_routes(
            root,
            vec![ChildRoute {
                node_id: c1,
                title: "Getting Started".to_string(),
                description: "Setup".to_string(),
                leaf_count: 3,
            }],
        );

        assert_eq!(
            resolve_target("getting started", &nav_index, root),
            Some(c1)
        );
        assert_eq!(
            resolve_target("GETTING STARTED", &nav_index, root),
            Some(c1)
        );
    }

    #[test]
    fn test_resolve_target_contains() {
        use crate::document::{ChildRoute, DocumentTree};

        let mut tree = DocumentTree::new("Root", "");
        let root = tree.root();
        let c1 = tree.add_child(root, "API Reference", "content");

        let mut nav_index = NavigationIndex::new();
        nav_index.add_child_routes(
            root,
            vec![ChildRoute {
                node_id: c1,
                title: "API Reference".to_string(),
                description: "API docs".to_string(),
                leaf_count: 7,
            }],
        );

        assert_eq!(resolve_target("api", &nav_index, root), Some(c1));
        assert_eq!(resolve_target("reference", &nav_index, root), Some(c1));
    }

    #[test]
    fn test_resolve_target_no_routes() {
        let nav_index = NavigationIndex::new();
        let tree = crate::document::DocumentTree::new("Root", "");
        assert!(resolve_target("anything", &nav_index, tree.root()).is_none());
    }

    #[test]
    fn test_parse_grep() {
        assert_eq!(
            parse_command("grep EBITDA"),
            Command::Grep {
                pattern: "EBITDA".to_string()
            }
        );
        assert_eq!(
            parse_command("grep revenue.*2024"),
            Command::Grep {
                pattern: "revenue.*2024".to_string()
            }
        );
    }

    #[test]
    fn test_parse_head() {
        assert_eq!(
            parse_command("head Installation"),
            Command::Head {
                target: "Installation".to_string(),
                lines: 20
            }
        );
        assert_eq!(
            parse_command("head -n 5 API Reference"),
            Command::Head {
                target: "API Reference".to_string(),
                lines: 5
            }
        );
    }

    #[test]
    fn test_parse_findtree() {
        assert_eq!(
            parse_command("findtree revenue"),
            Command::FindTree {
                pattern: "revenue".to_string()
            }
        );
        assert_eq!(
            parse_command("findtree API Reference"),
            Command::FindTree {
                pattern: "API Reference".to_string()
            }
        );
    }

    #[test]
    fn test_parse_wc() {
        assert_eq!(
            parse_command("wc Installation"),
            Command::Wc {
                target: "Installation".to_string()
            }
        );
        assert_eq!(
            parse_command("wc API Reference"),
            Command::Wc {
                target: "API Reference".to_string()
            }
        );
    }
}