Skip to main content

vtcode_core/tools/registry/
labels.rs

1use std::borrow::Cow;
2
3use serde_json::Value;
4use vtcode_commons::tool_headlines::{humanize_key, strip_mcp_prefix};
5
6use crate::config::constants::tools as tool_names;
7use crate::tools::tool_intent;
8
9// Canonical session labels live in `vtcode-commons::tool_headlines` so
10// surfaces that cannot depend on this crate share them; re-exported here so
11// every existing import path keeps working.
12pub use vtcode_commons::tool_headlines::{unified_exec_action_label, write_stdin_action_label};
13
14pub fn tool_action_label(tool_name: &str, args: &Value) -> Cow<'static, str> {
15    if tool_intent::is_command_run_tool_call(tool_name, args) {
16        return Cow::Borrowed("Run command");
17    }
18
19    let actual_tool_name = strip_mcp_prefix(tool_name);
20
21    match actual_tool_name {
22        name if name == tool_names::EXEC_COMMAND => Cow::Borrowed("Run command"),
23        name if name == tool_names::WRITE_STDIN => Cow::Borrowed(write_stdin_action_label(args)),
24        name if name == tool_names::RUN_PTY_CMD => Cow::Borrowed("Run command"),
25        name if name == tool_names::EXECUTE_CODE => Cow::Borrowed("Run code"),
26        name if name == tool_names::GET_ERRORS => Cow::Borrowed("List errors"),
27        name if name == tool_names::MCP => Cow::Borrowed("MCP discovery"),
28        name if name == tool_names::MCP_CONNECT_SERVER => Cow::Borrowed("Connect MCP server"),
29        name if name == tool_names::MCP_DISCONNECT_SERVER => Cow::Borrowed("Disconnect MCP server"),
30        name if name == tool_names::LIST_SKILLS => Cow::Borrowed("List skills"),
31        name if name == tool_names::LOAD_SKILL => Cow::Borrowed("Load skill"),
32        name if name == tool_names::LOAD_SKILL_RESOURCE => Cow::Borrowed("Load skill resource"),
33        name if name == tool_names::READ_FILE => Cow::Borrowed("Read file"),
34        name if name == tool_names::WRITE_FILE => Cow::Borrowed("Write file"),
35        name if name == tool_names::EDIT_FILE => Cow::Borrowed("Edit file"),
36        name if name == tool_names::CREATE_FILE => Cow::Borrowed("Create file"),
37        name if name == tool_names::DELETE_FILE => Cow::Borrowed("Delete file"),
38        name if name == tool_names::APPLY_PATCH => Cow::Borrowed("Apply patch"),
39        name if name == tool_names::SEARCH_REPLACE => Cow::Borrowed("Search/replace"),
40        name if name == tool_names::CREATE_PTY_SESSION => Cow::Borrowed("Create command session"),
41        name if name == tool_names::READ_PTY_SESSION => Cow::Borrowed("Read command session"),
42        name if name == tool_names::LIST_PTY_SESSIONS => Cow::Borrowed("List command sessions"),
43        name if name == tool_names::SEND_PTY_INPUT => Cow::Borrowed("Send command input"),
44        name if name == tool_names::CLOSE_PTY_SESSION => Cow::Borrowed("Close command session"),
45        name if name == tool_names::RESIZE_PTY_SESSION => Cow::Borrowed("Resize command session"),
46        name if name == tool_names::UNIFIED_EXEC => Cow::Borrowed(unified_exec_action_label(args)),
47        name if name == tool_names::CODE_SEARCH => Cow::Borrowed("Search code"),
48        name if name == tool_names::UNIFIED_FILE => match tool_intent::file_operation_action(args).unwrap_or("read") {
49            "read" => Cow::Borrowed("Read file"),
50            "write" => Cow::Borrowed("Write file"),
51            "edit" => Cow::Borrowed("Edit file"),
52            "patch" | tool_names::APPLY_PATCH => Cow::Borrowed("Apply patch"),
53            "delete" => Cow::Borrowed("Delete file"),
54            "move" => Cow::Borrowed("Move file"),
55            "copy" => Cow::Borrowed("Copy file"),
56            _ => Cow::Borrowed("File operation"),
57        },
58        "fetch" | tool_names::WEB_FETCH | tool_names::FETCH_URL => Cow::Borrowed("Fetch"),
59        _ => Cow::Owned(humanize_key(actual_tool_name)),
60    }
61}
62
63#[cfg(test)]
64mod tests {
65    use super::tool_action_label;
66    use crate::config::constants::tools;
67    use serde_json::json;
68
69    #[test]
70    fn code_search_uses_stable_label() {
71        assert_eq!(tool_action_label(tools::CODE_SEARCH, &json!({"query": "Widget"})), "Search code");
72    }
73
74    #[test]
75    fn write_stdin_distinguishes_wait_from_send() {
76        assert_eq!(
77            tool_action_label(tools::WRITE_STDIN, &json!({"session_id": "run-wait-1", "action": "wait"})),
78            "Wait for command session"
79        );
80        assert_eq!(
81            tool_action_label(tools::WRITE_STDIN, &json!({"session_id": "run-send-2", "chars": "y\n"})),
82            "Send command input"
83        );
84        assert_eq!(tool_action_label(tools::WRITE_STDIN, &json!({"session_id": "run-poll-3"})), "Read command session");
85    }
86
87    #[test]
88    fn unified_exec_wait_is_case_insensitive() {
89        for action in ["wait", "Wait", "WAIT", "WaIt"] {
90            assert_eq!(
91                tool_action_label(tools::UNIFIED_EXEC, &json!({"session_id": "run-abc", "action": action})),
92                "Wait for command session",
93                "action {action} must map case-insensitively"
94            );
95        }
96    }
97
98    #[test]
99    fn write_stdin_explicit_action_wins_over_chars() {
100        let chars = "y\n";
101        assert_eq!(
102            tool_action_label(
103                tools::WRITE_STDIN,
104                &json!({"session_id": "run-explicit-wait", "action": "wait", "chars": chars})
105            ),
106            "Wait for command session"
107        );
108        assert_eq!(
109            tool_action_label(
110                tools::WRITE_STDIN,
111                &json!({"session_id": "run-explicit-write", "action": "write", "chars": chars})
112            ),
113            "Send command input"
114        );
115    }
116
117    #[test]
118    fn write_stdin_empty_vs_whitespace_chars_boundary() {
119        assert_eq!(
120            tool_action_label(tools::WRITE_STDIN, &json!({"session_id": "run-empty", "chars": ""})),
121            "Read command session"
122        );
123        assert_eq!(
124            tool_action_label(tools::WRITE_STDIN, &json!({"session_id": "run-space", "chars": "   "})),
125            "Send command input",
126            "whitespace-only chars are still stdin bytes"
127        );
128    }
129
130    #[test]
131    fn write_stdin_explicit_write_without_input_reads_as_poll() {
132        // Matches `write_stdin_dispatch`: explicit write with empty/missing
133        // input executes as Poll, so the label must not claim a send.
134        assert_eq!(
135            tool_action_label(
136                tools::WRITE_STDIN,
137                &json!({"session_id": "run-write-empty", "action": "write", "chars": ""})
138            ),
139            "Read command session"
140        );
141        assert_eq!(
142            tool_action_label(tools::WRITE_STDIN, &json!({"session_id": "run-write-none", "action": "write"})),
143            "Read command session"
144        );
145    }
146}