Skip to content

default_client

Class info

Classes

Name Children Inherits
DefaultACPClient
aclientp.client.implementations.default_client
Default implementation of ACP Client interface for basic operations.

    🛈 DocStrings

    Default ACP client implementation.

    This module provides a basic client implementation for the Agent Client Protocol (ACP) that can be used for testing or as a base for more sophisticated client implementations.

    DefaultACPClient

    Bases: Client

    Default implementation of ACP Client interface for basic operations.

    This provides a basic client implementation that can be used for testing or as a base for more sophisticated client implementations.

    Source code in src/aclientp/client/implementations/default_client.py
    class DefaultACPClient(Client):
        """Default implementation of ACP Client interface for basic operations.
    
        This provides a basic client implementation that can be used for testing
        or as a base for more sophisticated client implementations.
        """
    
        def __init__(
            self,
            *,
            allow_file_operations: bool = False,
            permission_outcomes: list[dict[str, DeniedOutcome | AllowedOutcome]] | None = None,
            use_real_files: bool = True,
        ) -> None:
            """Initialize default ACP client.
    
            Args:
                allow_file_operations: Whether to allow file read/write operations
                permission_outcomes: Queue of permission outcomes for testing
                use_real_files: Whether to use real filesystem or in-memory storage
            """
            self.allow_file_operations = allow_file_operations
            self.use_real_files = use_real_files
            self.permission_outcomes = permission_outcomes or []
            self.files: dict[str, str] = {}  # In-memory file storage for testing
            self.ext_calls: list[tuple[str, dict[str, Any]]] = []
            self.ext_notes: list[tuple[str, dict[str, Any]]] = []
            self.notifications: list[SessionNotification] = []
    
        async def request_permission(
            self, params: RequestPermissionRequest
        ) -> RequestPermissionResponse:
            """Default permission handler - grants all permissions or uses test queue."""
            title = params.tool_call.title or "operation"
            logger.info("Permission requested", title=title)
    
            # If we have test outcomes queued, use them
            if self.permission_outcomes:
                outcome = self.permission_outcomes.pop(0)
                return RequestPermissionResponse.model_validate({"outcome": outcome})
    
            # Default: grant permission for the first option
            if params.options:
                id_ = params.options[0].option_id
                return RequestPermissionResponse.allowed(id_)
    
            # No options - deny
            return RequestPermissionResponse.denied()
    
        async def session_update(self, params: SessionNotification) -> None:
            """Handle session update notifications."""
            msg = "Session update for %s: %s"
            logger.debug(msg, params.session_id, params.update.session_update)
            self.notifications.append(params)
    
        async def write_text_file(self, params: WriteTextFileRequest) -> WriteTextFileResponse:
            """Write text to file (if allowed)."""
            if not self.allow_file_operations:
                raise RuntimeError("File operations not allowed")
    
            if self.use_real_files:
                try:
                    path = Path(params.path)
                    path.write_text(params.content, encoding="utf-8")
                    logger.info("Wrote file", path=params.path)
                except Exception:
                    logger.exception("Failed to write file", path=params.path)
                    raise
            else:
                # In-memory storage for testing
                self.files[str(params.path)] = params.content
    
            return WriteTextFileResponse()
    
        async def read_text_file(self, params: ReadTextFileRequest) -> ReadTextFileResponse:
            """Read text from file (if allowed)."""
            if not self.allow_file_operations:
                raise RuntimeError("File operations not allowed")
    
            if self.use_real_files:
                try:
                    path = Path(params.path)
    
                    if not path.exists():
                        raise FileNotFoundError(f"File not found: {params.path}")  # noqa: TRY301
    
                    content = path.read_text(encoding="utf-8")
    
                    # Apply line filtering if requested
                    if params.line is not None or params.limit is not None:
                        lines = content.splitlines()
                        start_line = (params.line - 1) if params.line else 0
                        end_line = start_line + params.limit if params.limit else len(lines)
                        content = "\n".join(lines[start_line:end_line])
    
                    logger.info("Read file", path=params.path)
                    return ReadTextFileResponse(content=content)
    
                except Exception:
                    logger.exception("Failed to read file", path=params.path)
                    raise
            else:
                # In-memory storage for testing
                content = self.files.get(str(params.path), "default content")
                return ReadTextFileResponse(content=content)
    
        async def create_terminal(self, params: CreateTerminalRequest) -> CreateTerminalResponse:
            """Create terminal (not implemented)."""
            raise NotImplementedError("Terminal operations not implemented")
    
        async def terminal_output(self, params: TerminalOutputRequest) -> TerminalOutputResponse:
            """Get terminal output (not implemented)."""
            raise NotImplementedError("Terminal operations not implemented")
    
        async def release_terminal(
            self, params: ReleaseTerminalRequest
        ) -> ReleaseTerminalResponse | None:
            """Release terminal (not implemented)."""
            raise NotImplementedError("Terminal operations not implemented")
    
        async def wait_for_terminal_exit(
            self, params: WaitForTerminalExitRequest
        ) -> WaitForTerminalExitResponse:
            """Wait for terminal exit (not implemented)."""
            raise NotImplementedError("Terminal operations not implemented")
    
        async def kill_terminal(self, params: KillTerminalRequest) -> KillTerminalResponse | None:
            """Kill terminal (not implemented)."""
            raise NotImplementedError("Terminal operations not implemented")
    
        def get_session_updates(self) -> list[SessionNotification]:
            """Get all received session updates."""
            return self.notifications.copy()
    
        def clear_session_updates(self) -> None:
            """Clear all stored session updates."""
            self.notifications.clear()
    
        async def ext_method(self, method: str, params: dict[str, Any]) -> dict[str, Any]:
            self.ext_calls.append((method, params))
            return {"ok": True, "method": method}
    
        async def elicitation(self, params: ElicitationRequest) -> ElicitationResponse:
            """Decline elicitation by default."""
            from aclientp.schema.elicitation import ElicitationResponse
    
            return ElicitationResponse.decline()
    
        async def elicitation_complete(self, params: ElicitationCompleteNotification) -> None:
            """Ignore elicitation complete notifications."""
    
        async def ext_notification(self, method: str, params: dict[str, Any]) -> None:
            self.ext_notes.append((method, params))
    

    __init__

    __init__(
        *,
        allow_file_operations: bool = False,
        permission_outcomes: list[dict[str, DeniedOutcome | AllowedOutcome]] | None = None,
        use_real_files: bool = True,
    ) -> None
    

    Initialize default ACP client.

    Parameters:

    Name Type Description Default
    allow_file_operations bool

    Whether to allow file read/write operations

    False
    permission_outcomes list[dict[str, DeniedOutcome | AllowedOutcome]] | None

    Queue of permission outcomes for testing

    None
    use_real_files bool

    Whether to use real filesystem or in-memory storage

    True
    Source code in src/aclientp/client/implementations/default_client.py
    def __init__(
        self,
        *,
        allow_file_operations: bool = False,
        permission_outcomes: list[dict[str, DeniedOutcome | AllowedOutcome]] | None = None,
        use_real_files: bool = True,
    ) -> None:
        """Initialize default ACP client.
    
        Args:
            allow_file_operations: Whether to allow file read/write operations
            permission_outcomes: Queue of permission outcomes for testing
            use_real_files: Whether to use real filesystem or in-memory storage
        """
        self.allow_file_operations = allow_file_operations
        self.use_real_files = use_real_files
        self.permission_outcomes = permission_outcomes or []
        self.files: dict[str, str] = {}  # In-memory file storage for testing
        self.ext_calls: list[tuple[str, dict[str, Any]]] = []
        self.ext_notes: list[tuple[str, dict[str, Any]]] = []
        self.notifications: list[SessionNotification] = []
    

    clear_session_updates

    clear_session_updates() -> None
    

    Clear all stored session updates.

    Source code in src/aclientp/client/implementations/default_client.py
    def clear_session_updates(self) -> None:
        """Clear all stored session updates."""
        self.notifications.clear()
    

    create_terminal async

    create_terminal(params: CreateTerminalRequest) -> CreateTerminalResponse
    

    Create terminal (not implemented).

    Source code in src/aclientp/client/implementations/default_client.py
    async def create_terminal(self, params: CreateTerminalRequest) -> CreateTerminalResponse:
        """Create terminal (not implemented)."""
        raise NotImplementedError("Terminal operations not implemented")
    

    elicitation async

    elicitation(params: ElicitationRequest) -> ElicitationResponse
    

    Decline elicitation by default.

    Source code in src/aclientp/client/implementations/default_client.py
    async def elicitation(self, params: ElicitationRequest) -> ElicitationResponse:
        """Decline elicitation by default."""
        from aclientp.schema.elicitation import ElicitationResponse
    
        return ElicitationResponse.decline()
    

    elicitation_complete async

    elicitation_complete(params: ElicitationCompleteNotification) -> None
    

    Ignore elicitation complete notifications.

    Source code in src/aclientp/client/implementations/default_client.py
    async def elicitation_complete(self, params: ElicitationCompleteNotification) -> None:
        """Ignore elicitation complete notifications."""
    

    get_session_updates

    get_session_updates() -> list[SessionNotification]
    

    Get all received session updates.

    Source code in src/aclientp/client/implementations/default_client.py
    def get_session_updates(self) -> list[SessionNotification]:
        """Get all received session updates."""
        return self.notifications.copy()
    

    kill_terminal async

    kill_terminal(params: KillTerminalRequest) -> KillTerminalResponse | None
    

    Kill terminal (not implemented).

    Source code in src/aclientp/client/implementations/default_client.py
    async def kill_terminal(self, params: KillTerminalRequest) -> KillTerminalResponse | None:
        """Kill terminal (not implemented)."""
        raise NotImplementedError("Terminal operations not implemented")
    

    read_text_file async

    read_text_file(params: ReadTextFileRequest) -> ReadTextFileResponse
    

    Read text from file (if allowed).

    Source code in src/aclientp/client/implementations/default_client.py
    async def read_text_file(self, params: ReadTextFileRequest) -> ReadTextFileResponse:
        """Read text from file (if allowed)."""
        if not self.allow_file_operations:
            raise RuntimeError("File operations not allowed")
    
        if self.use_real_files:
            try:
                path = Path(params.path)
    
                if not path.exists():
                    raise FileNotFoundError(f"File not found: {params.path}")  # noqa: TRY301
    
                content = path.read_text(encoding="utf-8")
    
                # Apply line filtering if requested
                if params.line is not None or params.limit is not None:
                    lines = content.splitlines()
                    start_line = (params.line - 1) if params.line else 0
                    end_line = start_line + params.limit if params.limit else len(lines)
                    content = "\n".join(lines[start_line:end_line])
    
                logger.info("Read file", path=params.path)
                return ReadTextFileResponse(content=content)
    
            except Exception:
                logger.exception("Failed to read file", path=params.path)
                raise
        else:
            # In-memory storage for testing
            content = self.files.get(str(params.path), "default content")
            return ReadTextFileResponse(content=content)
    

    release_terminal async

    release_terminal(params: ReleaseTerminalRequest) -> ReleaseTerminalResponse | None
    

    Release terminal (not implemented).

    Source code in src/aclientp/client/implementations/default_client.py
    async def release_terminal(
        self, params: ReleaseTerminalRequest
    ) -> ReleaseTerminalResponse | None:
        """Release terminal (not implemented)."""
        raise NotImplementedError("Terminal operations not implemented")
    

    request_permission async

    request_permission(params: RequestPermissionRequest) -> RequestPermissionResponse
    

    Default permission handler - grants all permissions or uses test queue.

    Source code in src/aclientp/client/implementations/default_client.py
    async def request_permission(
        self, params: RequestPermissionRequest
    ) -> RequestPermissionResponse:
        """Default permission handler - grants all permissions or uses test queue."""
        title = params.tool_call.title or "operation"
        logger.info("Permission requested", title=title)
    
        # If we have test outcomes queued, use them
        if self.permission_outcomes:
            outcome = self.permission_outcomes.pop(0)
            return RequestPermissionResponse.model_validate({"outcome": outcome})
    
        # Default: grant permission for the first option
        if params.options:
            id_ = params.options[0].option_id
            return RequestPermissionResponse.allowed(id_)
    
        # No options - deny
        return RequestPermissionResponse.denied()
    

    session_update async

    session_update(params: SessionNotification) -> None
    

    Handle session update notifications.

    Source code in src/aclientp/client/implementations/default_client.py
    async def session_update(self, params: SessionNotification) -> None:
        """Handle session update notifications."""
        msg = "Session update for %s: %s"
        logger.debug(msg, params.session_id, params.update.session_update)
        self.notifications.append(params)
    

    terminal_output async

    terminal_output(params: TerminalOutputRequest) -> TerminalOutputResponse
    

    Get terminal output (not implemented).

    Source code in src/aclientp/client/implementations/default_client.py
    async def terminal_output(self, params: TerminalOutputRequest) -> TerminalOutputResponse:
        """Get terminal output (not implemented)."""
        raise NotImplementedError("Terminal operations not implemented")
    

    wait_for_terminal_exit async

    wait_for_terminal_exit(params: WaitForTerminalExitRequest) -> WaitForTerminalExitResponse
    

    Wait for terminal exit (not implemented).

    Source code in src/aclientp/client/implementations/default_client.py
    async def wait_for_terminal_exit(
        self, params: WaitForTerminalExitRequest
    ) -> WaitForTerminalExitResponse:
        """Wait for terminal exit (not implemented)."""
        raise NotImplementedError("Terminal operations not implemented")
    

    write_text_file async

    write_text_file(params: WriteTextFileRequest) -> WriteTextFileResponse
    

    Write text to file (if allowed).

    Source code in src/aclientp/client/implementations/default_client.py
    async def write_text_file(self, params: WriteTextFileRequest) -> WriteTextFileResponse:
        """Write text to file (if allowed)."""
        if not self.allow_file_operations:
            raise RuntimeError("File operations not allowed")
    
        if self.use_real_files:
            try:
                path = Path(params.path)
                path.write_text(params.content, encoding="utf-8")
                logger.info("Wrote file", path=params.path)
            except Exception:
                logger.exception("Failed to write file", path=params.path)
                raise
        else:
            # In-memory storage for testing
            self.files[str(params.path)] = params.content
    
        return WriteTextFileResponse()