Skip to content

ccproxy.plugins.command_replay

ccproxy.plugins.command_replay

Command Replay Plugin - Generate curl and xh commands for provider requests.

CommandReplayConfig

Bases: BaseModel

Configuration for command replay generation.

Generates curl and xh commands for provider requests to enable easy replay and debugging of API calls.

should_generate_for_url

should_generate_for_url(url, is_provider_request=None)

Check if commands should be generated for the given URL.

Parameters:

Name Type Description Default
url str

The request URL to check

required
is_provider_request bool | None

Whether this is a provider request (None = auto-detect)

None

Returns:

Type Description
bool

True if commands should be generated for this URL

Source code in ccproxy/plugins/command_replay/config.py
def should_generate_for_url(
    self, url: str, is_provider_request: bool | None = None
) -> bool:
    """Check if commands should be generated for the given URL.

    Args:
        url: The request URL to check
        is_provider_request: Whether this is a provider request (None = auto-detect)

    Returns:
        True if commands should be generated for this URL
    """
    # Check exclude patterns first
    if self.exclude_url_patterns:
        if any(pattern in url for pattern in self.exclude_url_patterns):
            return False

    # Auto-detect if this is a provider request if not specified
    if is_provider_request is None:
        provider_domains = [
            "api.anthropic.com",
            "claude.ai",
            "api.openai.com",
            "chatgpt.com",
        ]
        is_provider_request = any(
            domain in url.lower() for domain in provider_domains
        )

    # Apply request type filtering
    if self.only_provider_requests and not is_provider_request:
        return False

    if not self.include_client_requests and not is_provider_request:
        return False

    # For provider requests, check include patterns
    if is_provider_request:
        if self.include_url_patterns:
            return any(pattern in url for pattern in self.include_url_patterns)
    else:
        # For client requests, be more permissive
        # Only filter if there are specific include patterns that don't match
        if self.include_url_patterns:
            # If include patterns are all provider domains, allow client requests
            provider_only = all(
                any(
                    provider in pattern.lower()
                    for provider in ["anthropic", "openai", "claude", "chatgpt"]
                )
                for pattern in self.include_url_patterns
            )
            if provider_only:
                return True
            # Otherwise apply normal include pattern matching
            return any(pattern in url for pattern in self.include_url_patterns)

    # Default: generate for all URLs if no patterns specified
    return True

CommandReplayHook

CommandReplayHook(config=None, file_formatter=None)

Bases: Hook

Hook for generating curl and xh command replays of provider requests.

Listens for PROVIDER_REQUEST_PREPARED events and generates command line equivalents that can be used to replay the exact same HTTP requests.

Parameters:

Name Type Description Default
config CommandReplayConfig | None

Command replay configuration

None
file_formatter CommandFileFormatter | None

File formatter for writing commands to files

None
Source code in ccproxy/plugins/command_replay/hook.py
def __init__(
    self,
    config: CommandReplayConfig | None = None,
    file_formatter: CommandFileFormatter | None = None,
) -> None:
    """Initialize the command replay hook.

    Args:
        config: Command replay configuration
        file_formatter: File formatter for writing commands to files
    """
    self.config = config or CommandReplayConfig()
    self.file_formatter = file_formatter

    logger.debug(
        "command_replay_hook_initialized",
        enabled=self.config.enabled,
        generate_curl=self.config.generate_curl,
        generate_xh=self.config.generate_xh,
        include_patterns=self.config.include_url_patterns,
        only_provider_requests=self.config.only_provider_requests,
        include_client_requests=self.config.include_client_requests,
        write_to_files=self.config.write_to_files,
        log_dir=self.config.log_dir,
    )

CommandReplayFactory

CommandReplayFactory()

Bases: SystemPluginFactory

Factory for creating command replay plugin instances.

Source code in ccproxy/plugins/command_replay/plugin.py
def __init__(self) -> None:
    """Initialize factory with manifest."""
    # Create manifest with static declarations
    manifest = PluginManifest(
        name="command_replay",
        version="0.1.0",
        description="Generates curl and xh commands for provider requests",
        is_provider=False,
        config_class=CommandReplayConfig,
    )

    # Initialize with manifest
    super().__init__(manifest)

create_runtime

create_runtime()

Create runtime instance.

Source code in ccproxy/plugins/command_replay/plugin.py
def create_runtime(self) -> CommandReplayRuntime:
    """Create runtime instance."""
    return CommandReplayRuntime(self.manifest)

create_context

create_context(core_services)

Create context for the plugin.

Source code in ccproxy/plugins/command_replay/plugin.py
def create_context(self, core_services: Any) -> Any:
    """Create context for the plugin."""
    # Get base context from parent
    context = super().create_context(core_services)
    return context

CommandReplayRuntime

CommandReplayRuntime(manifest)

Bases: SystemPluginRuntime

Runtime for the command replay plugin.

Generates curl and xh commands for provider requests to enable easy replay and debugging of API calls.

Source code in ccproxy/plugins/command_replay/plugin.py
def __init__(self, manifest: PluginManifest):
    """Initialize runtime."""
    super().__init__(manifest)
    self.config: CommandReplayConfig | None = None
    self.hook: CommandReplayHook | None = None
    self.file_formatter: CommandFileFormatter | None = None

get_health_info

get_health_info()

Get plugin health information.

Source code in ccproxy/plugins/command_replay/plugin.py
def get_health_info(self) -> dict[str, Any]:
    """Get plugin health information."""
    return {
        "enabled": self.config.enabled if self.config else False,
        "hook_registered": self.hook is not None,
        "generate_curl": self.config.generate_curl if self.config else False,
        "generate_xh": self.config.generate_xh if self.config else False,
        "write_to_files": self.config.write_to_files if self.config else False,
        "file_formatter_enabled": self.file_formatter is not None,
        "log_dir": self.config.log_dir if self.config else None,
    }