Skip to main content

vtcode_commons/
tool_intent.rs

1//! Pure tool-intent probes shared with surfaces that cannot use the full classifier.
2//!
3//! `vtcode-core::tools::tool_intent` owns intent classification for dispatch,
4//! security, and execution. The probes here are the pure JSON subset a
5//! transcript needs to route display — session-action routing for the
6//! exec-session labels ([`crate::tool_headlines`]) — so the desktop app,
7//! which must not depend on `vtcode-core`, shares them instead of mirroring
8//! them. Core re-exports both probes, so every existing import path keeps
9//! working and dispatch call sites are untouched.
10
11use serde_json::Value;
12
13/// Whether the payload carries indexed command parts (`command.0`/`command.1`).
14///
15/// Only the `0`/`1` seeds are probed: their presence proves the indexed form,
16/// and deeper indices never appear without them.
17#[must_use]
18pub fn has_indexed_command_parts(args: &Value) -> bool {
19    let Some(payload) = args.as_object() else {
20        return false;
21    };
22
23    payload.contains_key("command.0") || payload.contains_key("command.1")
24}
25
26/// Determine the action for command-session calls based on args.
27///
28/// Returns the action string or `None` when no inference is possible. An
29/// explicit `action` wins; otherwise standard command, code, input, inspect,
30/// and session-id fields route in that order.
31#[must_use]
32pub fn command_session_action(args: &Value) -> Option<&str> {
33    args.get("action").and_then(|v| v.as_str()).or_else(|| {
34        // Check for standard command fields
35        if args.get("command").is_some()
36            || args.get("cmd").is_some()
37            || args.get("raw_command").is_some()
38            || has_indexed_command_parts(args)
39        {
40            Some("run")
41        } else if args.get("code").is_some() {
42            Some("code")
43        } else if args.get("input").is_some() || args.get("chars").is_some() || args.get("text").is_some() {
44            Some("write")
45        } else if args.get("spool_path").is_some()
46            || args.get("query").is_some()
47            || args.get("head_lines").is_some()
48            || args.get("tail_lines").is_some()
49            || args.get("max_matches").is_some()
50            || args.get("literal").is_some()
51        {
52            Some("inspect")
53        } else if args.get("session_id").is_some() || args.get("s").is_some() {
54            Some("poll")
55        } else {
56            None
57        }
58    })
59}
60
61#[cfg(test)]
62mod tests;