Khanna, Videh Rakesh Rakesh
feat: add device control, app automation, tests, and other updates
8576f52 | """JARVIS VS Code & Copilot Tools — open editors, run commands, assign tasks to Copilot.""" | |
| import subprocess | |
| import os | |
| import json | |
| from tools import tool | |
| def _run_osascript(script: str, timeout: int = 10) -> str: | |
| result = subprocess.run( | |
| ["osascript", "-e", script], | |
| capture_output=True, text=True, timeout=timeout, | |
| ) | |
| return result.stdout.strip() | |
| def _vscode_cli(*args, timeout: int = 15) -> str: | |
| """Run VS Code CLI command.""" | |
| code_paths = [ | |
| "/usr/local/bin/code", | |
| "/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code", | |
| os.path.expanduser("~/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code"), | |
| ] | |
| code_cmd = None | |
| for p in code_paths: | |
| if os.path.exists(p): | |
| code_cmd = p | |
| break | |
| if code_cmd is None: | |
| # Try PATH | |
| result = subprocess.run(["which", "code"], capture_output=True, text=True) | |
| if result.returncode == 0: | |
| code_cmd = result.stdout.strip() | |
| else: | |
| return "Error: VS Code CLI ('code') not found. Install from VS Code: Cmd+Shift+P → 'Shell Command: Install code command'" | |
| result = subprocess.run( | |
| [code_cmd] + list(args), | |
| capture_output=True, text=True, timeout=timeout, | |
| ) | |
| output = result.stdout.strip() | |
| if result.stderr.strip(): | |
| output += f"\n{result.stderr.strip()}" | |
| return output or "(done)" | |
| # ─── VS Code Operations ────────────────────────── | |
| def vscode_open(path: str, new_window: bool = False) -> str: | |
| path = os.path.expanduser(path) | |
| args = ["--goto", path] if os.path.isfile(path) else [path] | |
| if new_window: | |
| args.insert(0, "--new-window") | |
| return _vscode_cli(*args) | |
| def vscode_open_terminal(folder: str = "") -> str: | |
| folder = os.path.expanduser(folder) if folder else os.getcwd() | |
| # Open folder in VS Code, then use AppleScript to open integrated terminal | |
| _vscode_cli(folder) | |
| _run_osascript(''' | |
| tell application "Visual Studio Code" to activate | |
| delay 0.5 | |
| tell application "System Events" | |
| keystroke "`" using {control down} | |
| end tell | |
| ''') | |
| return f"VS Code terminal opened in {folder}" | |
| def vscode_run_command(command: str) -> str: | |
| # Use AppleScript to trigger Command Palette and type the command | |
| _run_osascript(f''' | |
| tell application "Visual Studio Code" to activate | |
| delay 0.3 | |
| tell application "System Events" | |
| keystroke "p" using {{shift down, command down}} | |
| delay 0.3 | |
| keystroke ">{command}" | |
| delay 0.2 | |
| keystroke return | |
| end tell | |
| ''') | |
| return f"Executed VS Code command: {command}" | |
| def copilot_chat(message: str, agent: str = "") -> str: | |
| # Sanitize message for AppleScript | |
| safe_msg = message.replace('"', '\\"').replace("\\", "\\\\") | |
| agent_prefix = "" | |
| if agent == "workspace": | |
| agent_prefix = "@workspace " | |
| elif agent == "terminal": | |
| agent_prefix = "@terminal " | |
| full_msg = f"{agent_prefix}{safe_msg}" | |
| _run_osascript(f''' | |
| tell application "Visual Studio Code" to activate | |
| delay 0.3 | |
| tell application "System Events" | |
| -- Open Copilot Chat (Ctrl+Cmd+I) | |
| keystroke "i" using {{control down, command down}} | |
| delay 0.5 | |
| keystroke "{full_msg}" | |
| delay 0.2 | |
| keystroke return | |
| end tell | |
| ''', timeout=15) | |
| return f"Sent to Copilot Chat: {full_msg[:100]}" | |
| def copilot_inline(instruction: str) -> str: | |
| safe_instr = instruction.replace('"', '\\"').replace("\\", "\\\\") | |
| _run_osascript(f''' | |
| tell application "Visual Studio Code" to activate | |
| delay 0.3 | |
| tell application "System Events" | |
| keystroke "i" using {{command down}} | |
| delay 0.5 | |
| keystroke "{safe_instr}" | |
| delay 0.2 | |
| keystroke return | |
| end tell | |
| ''', timeout=15) | |
| return f"Copilot inline edit triggered: {instruction[:80]}" | |
| def vscode_list_extensions() -> str: | |
| output = _vscode_cli("--list-extensions") | |
| extensions = output.strip().split("\n") | |
| return f"Installed extensions ({len(extensions)}):\n" + "\n".join(f" • {e}" for e in extensions[:30]) | |
| def vscode_diff(file1: str, file2: str) -> str: | |
| f1 = os.path.expanduser(file1) | |
| f2 = os.path.expanduser(file2) | |
| return _vscode_cli("--diff", f1, f2) | |