src — cloud
src — cloud
This document provides a developer-focused overview of the src/cloud/cloud-sessions.ts module, which is responsible for managing cloud-based coding sessions and their synchronization with local environments.
Cloud Sessions and Teleport Module
The src/cloud/cloud-sessions.ts module is the core component for interacting with remote, cloud-hosted coding environments. It provides functionality for creating, managing the lifecycle of, sharing, and synchronizing these sessions. This module abstracts away the underlying cloud infrastructure, presenting a consistent API for session management and state transfer.
1. Overview
This module defines two primary classes: CloudSessionManager and TeleportManager.
- The
CloudSessionManagerhandles the fundamental operations related to cloud coding sessions, such as creation, status changes (pause, resume, terminate), listing, and sharing. - The
TeleportManagerfocuses on the synchronization aspects, enabling the transfer of state between local development environments and remote cloud sessions.
Together, these components facilitate a seamless experience for developers working across local and cloud environments.
2. Key Concepts
CloudSession Interface
The CloudSession interface defines the structure of a remote coding session. It's the central data model managed by this module.
export interface CloudSession {
id: string;
status: 39;starting39; | 39;running39; | 39;paused39; | 39;completed39; | 39;failed39;;
createdAt: number;
lastActivity: number;
task?: string; class="hl-cmt">// Description of the session39;s purpose
visibility: 39;private39; | 39;team39; | 39;public39;;
repoAccess?: boolean; class="hl-cmt">// Whether the session has access to a code repository
networkAccess: 39;none39; | 39;limited39; | 39;full39;; class="hl-cmt">// Network permissions
vmImage?: string; class="hl-cmt">// The VM image used for the session
}
Key properties:
id: A unique identifier for the session.status: The current state of the session, dictating what operations are possible.task: A human-readable description of the session's purpose.visibility: Controls who can access or discover the session.networkAccess: Defines the session's internet access level.
CloudConfig Interface
The CloudConfig interface specifies configuration options for the cloud integration, including API endpoints and default settings for new sessions.
export interface CloudConfig {
apiEndpoint: string;
authToken?: string;
defaultVisibility: 39;private39; | 39;team39; | 39;public39;;
defaultNetworkAccess: 39;none39; | 39;limited39; | 39;full39;;
allowedDomains: string[];
}
A DEFAULT_CONFIG object is provided, using URL_CONFIG.CLOUD_API_ENDPOINT for the default API endpoint.
3. Core Components
3.1. CloudSessionManager
The CloudSessionManager class is responsible for the lifecycle management of cloud coding sessions. It maintains an in-memory collection of CloudSession objects.
Initialization:
constructor(config?: Partial: Initializes the manager. It merges provided configuration with) DEFAULT_CONFIG.
Key Methods:
createSession(task: string, options?: Partial): Promise - Initiates a new cloud coding session.
- Requires a
taskdescription. - Generates a unique
idand setsstatusto'starting'(then immediately to'running'in the current simulation). - Applies default
visibilityandnetworkAccessfromCloudConfigif not overridden byoptions.
listSessions(): CloudSession[]- Returns an array of all currently managed
CloudSessionobjects.
getSession(id: string): CloudSession | null- Retrieves a specific
CloudSessionby its ID, ornullif not found.
pauseSession(id: string): Promise- Attempts to change the status of a
runningsession topaused. - Returns
trueon success,falseif the session is not found or not in arunningstate.
resumeSession(id: string): Promise- Attempts to change the status of a
pausedsession torunning. - Returns
trueon success,falseif the session is not found or not in apausedstate.
terminateSession(id: string): Promise- Marks a session as
completed. - Returns
trueon success,falseif the session is not found or already completed/failed.
shareSession(id: string, visibility: CloudSession['visibility']): Promise- Updates the
visibilityof an existing session. - Returns a simulated share URL for the session.
- Throws an error if the session is not found.
sendToCloud(task: string): Promise- A convenience method that creates a new session with
networkAccessset to'full'. This is used when a task needs to be "sent to the cloud" for execution with full internet capabilities.
getActiveCount(): number- Returns the number of sessions currently in
runningorstartingstatus.
getTotalCount(): number- Returns the total number of sessions managed by the instance.
3.2. TeleportManager
The TeleportManager class is responsible for orchestrating the synchronization and transfer of state between local and cloud environments. It depends on an instance of CloudSessionManager to perform its operations.
Initialization:
constructor(cloudManager: CloudSessionManager): Initializes the manager with a reference to theCloudSessionManager.
Key Methods:
teleport(sessionId: string): Promise<{ success: boolean; localSessionId?: string; filesTransferred?: number; diffSummary?: string; }>- Simulates pulling the state of a cloud session to a local environment.
- Requires the cloud
sessionId. - Returns a success status and a simulated
localSessionIdalong with a summary. - Fails if the cloud session is not found or not in a
runningorpausedstate.
pushToCloud(localSessionId: string): Promise- Creates a new cloud session, simulating the "pushing" of a local session's state to the cloud.
- Internally calls
cloudManager.createSessionwith a task derived from thelocalSessionIdandrepoAccess: true. - Throws an error if
localSessionIdis invalid.
syncState(sessionId: string): Promise<{ conflicts: string[]; merged: number; }>- Simulates synchronizing the state of an existing cloud session with a local environment.
- Returns a summary of conflicts and merged items (currently empty/zero in the simulation).
- Throws an error if the session is not found.
getDiff(sessionId: string): Promise- Simulates retrieving a diff summary between the local and cloud state for a given session.
- Returns a descriptive string (currently a placeholder).
- Throws an error if the session is not found.
4. Module Architecture and Interactions
The cloud-sessions.ts module is designed with a clear separation of concerns: CloudSessionManager handles the core session lifecycle, while TeleportManager focuses on state synchronization, leveraging the CloudSessionManager for session access and creation.
graph TD
A[CloudSessionManager] -- Manages --> B(CloudSession)
C[TeleportManager] -- Uses --> A
C -- Operates on --> B
CloudSessionManagermaintains its internal state using aMap.TeleportManagerinteracts withCloudSessionManagervia its public methods likegetSessionandcreateSession.- The module utilizes
randomUUIDfor generating unique identifiers andloggerfor internal debugging and informational messages. - Configuration is flexible, allowing partial overrides of
DEFAULT_CONFIGduringCloudSessionManagerinstantiation.
5. Integration Points
This module serves as a foundational layer for any features requiring remote coding environments or state synchronization.
- Internal Dependencies:
src/utils/logger.js: Used for logging operational events and debugging information.src/config/constants.js: Provides the default cloud API endpoint.crypto: Used for generating UUIDs for session IDs.
- External Consumers:
tests/features/cloud-lsp-ide.test.ts: This module is heavily tested by thecloud-lsp-ide.test.tssuite. This indicates its central role in the cloud-based IDE functionality, where session lifecycle, sharing, and teleport operations are critical features. Tests directly instantiateCloudSessionManagerandTeleportManagerand invoke their various methods to ensure correct behavior.
6. Contributing to this Module
When extending or modifying this module:
- Session State Transitions: Ensure that any changes to a
CloudSession'sstatusare handled correctly, and thelastActivitytimestamp is updated. - Error Handling: Maintain consistent error handling. For non-critical failures (e.g., pausing a non-running session), methods typically return
false. For critical failures (e.g., sharing a non-existent session), errors are thrown. - Separation of Concerns: Consider whether new functionality belongs in
CloudSessionManager(core session lifecycle) orTeleportManager(state transfer/sync). - Simulation vs. Real Implementation: Note that many operations (e.g., actual VM startup, file transfer, diffing) are currently simulated. When integrating with a real cloud backend, these methods will require actual API calls and state management.