Skip to main content

vtcode_core/tools/tool_intent/
actions.rs

1use serde_json::Value;
2
3use crate::config::constants::tools;
4
5/// Determine the action for file_operation tool based on args.
6/// Returns the action string or a default if inference is possible.
7pub fn file_operation_action(args: &Value) -> Option<&str> {
8    args.get("action").and_then(|v| v.as_str()).or_else(|| {
9        let has_read_path = args.get("path").is_some()
10            || args.get("file_path").is_some()
11            || args.get("filepath").is_some()
12            || args.get("target_path").is_some()
13            || args.get("file").is_some()
14            || args.get("p").is_some();
15
16        if args.get("old_str").is_some() {
17            Some("edit")
18        } else if args.get("content").is_some() {
19            Some("write")
20        } else if args.get("destination").is_some() {
21            Some("move")
22        } else if has_read_path {
23            Some("read")
24        } else {
25            None
26        }
27    })
28}
29
30// Canonical probe lives in `vtcode-commons::tool_intent` so transcript
31// surfaces share it; re-exported here so dispatch import paths are untouched.
32pub use vtcode_commons::tool_intent::command_session_action;
33
34fn action_matches(action: Option<&str>, expected: &str) -> bool {
35    action.is_some_and(|candidate| candidate.eq_ignore_ascii_case(expected))
36}
37
38pub fn action_matches_any(action: Option<&str>, expected: &[&str]) -> bool {
39    action.is_some_and(|candidate| {
40        expected
41            .iter()
42            .any(|expected_action| candidate.eq_ignore_ascii_case(expected_action))
43    })
44}
45
46pub fn file_operation_action_is(args: &Value, expected: &str) -> bool {
47    action_matches(file_operation_action(args), expected)
48}
49
50pub fn file_operation_action_in(args: &Value, expected: &[&str]) -> bool {
51    action_matches_any(file_operation_action(args), expected)
52}
53
54pub fn command_session_action_is(args: &Value, expected: &str) -> bool {
55    action_matches(command_session_action(args), expected)
56}
57
58pub fn command_session_action_in(args: &Value, expected: &[&str]) -> bool {
59    action_matches_any(command_session_action(args), expected)
60}
61
62/// Return the explicit action requested through the consolidated MCP tool.
63pub fn mcp_action(args: &Value) -> Option<&str> {
64    args.get("action").and_then(Value::as_str)
65}
66
67pub fn mcp_action_is(args: &Value, expected: &str) -> bool {
68    action_matches(mcp_action(args), expected)
69}
70
71/// Return the policy identity for actions whose risk differs from their
72/// otherwise low-risk parent tool.
73pub fn action_qualified_policy_name(tool_name: &str, args: Option<&Value>) -> Option<&'static str> {
74    if tool_name == tools::MCP_CONNECT_SERVER
75        || (tool_name == tools::MCP && args.is_some_and(|args| mcp_action_is(args, "connect")))
76    {
77        return Some("mcp:connect");
78    }
79
80    if tool_name == tools::MCP_DISCONNECT_SERVER
81        || (tool_name == tools::MCP && args.is_some_and(|args| mcp_action_is(args, "disconnect")))
82    {
83        return Some("mcp:disconnect");
84    }
85
86    None
87}