Metadata-Version: 2.4
Name: vscode-command-client
Version: 1.0.0
Summary: Python client for VSCode Command Server extension
Project-URL: Homepage, https://github.com/crimson206/run-extension
Project-URL: Repository, https://github.com/crimson206/run-extension
Project-URL: Documentation, https://github.com/crimson206/run-extension#readme
Project-URL: Bug Tracker, https://github.com/crimson206/run-extension/issues
Author-email: Sisung Kim <sisung.kim1@gmail.com>
License: MIT License
        
        Copyright (c) 2024 Sisung Kim
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE. 
License-File: LICENSE
Keywords: api,automation,client,command,server,vscode
Requires-Python: >=3.8
Requires-Dist: requests>=2.25.0
Description-Content-Type: text/markdown

# VSCode Command Client

A Python client for communicating with VSCode Command Server extension. Execute VSCode commands remotely via HTTP API.

## Installation

```bash
pip install vscode-command-client
```

## Quick Start

### Python API

```python
from vscode_command_client import VSCodeHTTPClient

# Create client (server must be running)
client = VSCodeHTTPClient(port=3000)

# Check if server is running
status = client.check_status()
print(status)

# Execute VSCode commands
result = client.execute_command("workbench.action.showCommands")
print(result)

# Get available commands
commands = client.get_commands()
print(f"Found {commands['count']} commands")

# Get server info
info = client.get_server_info()
print(f"Workspace: {info.get('workspace')}")

# Connection recovery features
if not client.is_server_running():
    # Try to reconnect
    reconnect_result = client.reconnect()
    print(f"Reconnect: {reconnect_result}")
    
    # Or restart the server
    restart_result = client.restart_server()
    print(f"Restart: {restart_result}")

# Comprehensive health check
health = client.health_check()
print(f"Health: {health}")
```

### Command Line Interface

```bash
# Check server status
vscode-client status

# Get server info
vscode-client info

# Execute a command
vscode-client execute workbench.action.showCommands

# Execute with retry
vscode-client execute workbench.action.showCommands --retry

# List available commands
vscode-client commands --limit 10

# Filter commands
vscode-client commands --filter "terminal"

# Wait for server to start
vscode-client wait --max-wait 60

# Connection recovery commands
vscode-client reconnect --max-attempts 3 --wait-time 2
vscode-client restart-server
vscode-client health-check

# Use different port with auto-retry
vscode-client --port 3001 --auto-retry status

# Get JSON output
vscode-client --json info
```

## Features

- ✅ **Simple API**: Easy-to-use Python client
- 🚀 **CLI Tool**: Command-line interface included
- ⏰ **Timeout Support**: Configurable request timeouts
- 🔄 **Session Management**: Persistent HTTP sessions
- 📊 **Server Monitoring**: Check status and wait for availability
- 🛠️ **Connection Recovery**: Reconnect and restart capabilities
- 🏥 **Health Checks**: Comprehensive server health monitoring
- 🔁 **Auto Retry**: Automatic retry on connection failures
- 🎯 **Type Hints**: Full type annotations
- 🛡️ **Error Handling**: Robust error handling

## Connection Recovery

### Auto Retry
```python
# Enable auto-retry for all requests
client = VSCodeHTTPClient(auto_retry=True)

# Or for specific commands
result = client.execute_command("some.command", retry=True)
```

### Manual Recovery
```python
# Reconnect to server
reconnect_result = client.reconnect(max_attempts=5, wait_time=2)

# Restart the VSCode extension server
restart_result = client.restart_server()

# Comprehensive health check
health = client.health_check()
```

### CLI Recovery Commands
```bash
# Reconnect with custom settings
vscode-client reconnect --max-attempts 5 --wait-time 3

# Restart the server extension
vscode-client restart-server

# Check overall health
vscode-client health-check

# Use auto-retry globally
vscode-client --auto-retry execute workbench.action.showCommands
```

## API Reference

### VSCodeHTTPClient

#### Constructor

```python
VSCodeHTTPClient(port=3000, host="localhost", timeout=30, auto_retry=False)
```

- `port`: Server port (default: 3000)
- `host`: Server host (default: localhost)  
- `timeout`: Request timeout in seconds (default: 30)
- `auto_retry`: Enable automatic retry on connection failure (default: False)

#### Methods

##### `check_status() -> Dict[str, Any]`
Check if the server is running.

##### `execute_command(command: str, args: List[Any] = None, retry: bool = None) -> Dict[str, Any]`
Execute a VSCode command with optional arguments and retry override.

##### `get_commands() -> Dict[str, Any]`
Get list of all available VSCode commands.

##### `get_server_info() -> Dict[str, Any]`
Get server information (port, PID, workspace).

##### `is_server_running() -> bool`
Quick check if server is responding.

##### `wait_for_server(max_wait: int = 30, check_interval: float = 1.0) -> bool`
Wait for server to become available.

##### `reconnect(max_attempts: int = 5, wait_time: int = 2) -> Dict[str, Any]`
Attempt to reconnect to the server.

##### `restart_server() -> Dict[str, Any]`
Restart the VSCode Command Server extension.

##### `health_check() -> Dict[str, Any]`
Comprehensive health check of server connection.

## Context Manager Support

```python
with VSCodeHTTPClient() as client:
    result = client.execute_command("workbench.action.toggleTerminal")
    print(result)
```

## Common VSCode Commands

```python
# File operations
client.execute_command("workbench.action.files.newUntitledFile")
client.execute_command("workbench.action.files.save")

# Editor operations
client.execute_command("workbench.action.closeActiveEditor")
client.execute_command("workbench.action.splitEditor")

# Terminal operations
client.execute_command("workbench.action.terminal.toggleTerminal")
client.execute_command("workbench.action.terminal.new")

# View operations
client.execute_command("workbench.action.showCommands")
client.execute_command("workbench.action.openSettings")
client.execute_command("workbench.action.quickOpen")
```

## Error Handling with Recovery

```python
try:
    result = client.execute_command("some.command")
    if result["success"]:
        print("Command executed successfully")
    else:
        print(f"Command failed: {result['error']}")
        
        # Try recovery if connection failed
        if "connection" in result["error"].lower():
            print("Attempting to reconnect...")
            if client.reconnect()["success"]:
                # Retry the command
                result = client.execute_command("some.command")
                print(f"Retry result: {result}")
            
except Exception as e:
    print(f"Connection error: {e}")
    # Try restarting the server
    restart_result = client.restart_server()
    print(f"Restart attempt: {restart_result}")
```

## Requirements

- Python 3.8+
- requests >= 2.25.0
- VSCode Command Server extension running

## License

MIT License 