Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/jaypopat/cf_ai_duet/llms.txt

Use this file to discover all available pages before exploring further.

Overview

The room package provides room management functionality for collaborative sessions, including workspace isolation, client connections, and resource cleanup.

Types

Manager

Manages all active rooms and their lifecycle.
type Manager struct {
    rooms     map[string]*Room
    mu        sync.RWMutex
    workerURL string
    aiClient  *ai.Client
    logger    *log.Logger
}

Room

Represents a collaborative session with shared terminal and AI chat.
type Room struct {
    ID           string
    Description  string
    Host         string
    Connections  []*Client
    Terminal     *terminal.Terminal
    AIMessages   []AIMessage
    WorkspaceDir string
}

Client

Represents a connected user in a room.
type Client struct {
    ID       string
    Username string
    IsHost   bool
    Events   chan RoomEvent
}

RoomEvent

Events broadcast to room participants.
type RoomEvent struct {
    Type     string    // "join", "leave", "typing", "ai_sync"
    Username string
    Data     string
}

AIMessage

AI chat message stored in room history.
type AIMessage struct {
    Role   string `json:"role"`
    UserID string `json:"user_id"`
    Text   string `json:"text"`
    Ts     int64  `json:"ts"`
}

Manager Functions

NewManager

Creates a new room manager instance.
func NewManager(workerURL string, aiClient *ai.Client, logger *log.Logger) *Manager
workerURL
string
URL for the AI worker service (empty string disables AI features)
aiClient
*ai.Client
Shared AI client for all rooms
logger
*log.Logger
Logger instance for room events

CreateRoom

Creates a new collaborative room with isolated workspace.
func (m *Manager) CreateRoom(host, description string) (*Room, error)
host
string
required
Username of the room host
description
string
Room description (used to generate workspace name)
Returns:
  • *Room: The created room with unique ID
  • error: If workspace creation fails
Workspace Naming:
  • Slugified description if provided (e.g., “my project” → “my-project”)
  • Random readable name if empty (e.g., “swift-phoenix”, “cosmic-dragon”)
  • Max 30 characters
Example:
room, err := manager.CreateRoom("alice", "React Dashboard")
if err != nil {
    return err
}
fmt.Println("Room ID:", room.ID)
fmt.Println("Workspace:", room.WorkspaceDir)

GetRoom

Retrieves an existing room by ID.
func (m *Manager) GetRoom(roomID string) (*Room, error)
roomID
string
required
Unique room identifier
Returns:
  • *Room: The requested room
  • error: ErrRoomNotFound if room doesn’t exist

LeaveRoom

Removes a client from a room and cleans up if empty.
func (m *Manager) LeaveRoom(roomID, clientID string) bool
roomID
string
required
Room to leave
clientID
string
required
Client identifier
Returns: true if the room was destroyed (last client left) Cleanup Actions:
  • Closes terminal if exists
  • Removes workspace directory
  • Calls worker API to cleanup sandbox resources
  • Deletes room from manager

GetAIClient

Retrieves the shared AI client.
func (m *Manager) GetAIClient() *ai.Client
Returns: The AI client instance (may be nil)

RoomCount

Returns the number of active rooms.
func (m *Manager) RoomCount() int

Room Methods

AddClient

Adds a client to the room and broadcasts join event.
func (r *Room) AddClient(client *Client)
client
*Client
required
Client to add (replaces existing client with same ID)

RemoveClient

Removes a client and broadcasts leave event.
func (r *Room) RemoveClient(clientID string)

BroadcastEvent

Sends an event to all clients except one.
func (r *Room) BroadcastEvent(event RoomEvent, excludeClientID string)
event
RoomEvent
required
Event to broadcast
excludeClientID
string
Client ID to exclude (typically the sender)

GetClients

Returns a copy of all connected clients.
func (r *Room) GetClients() []*Client

ClientCount

Returns the number of connected clients.
func (r *Room) ClientCount() int

SetAIMessages / GetAIMessages

Thread-safe AI message history management.
func (r *Room) SetAIMessages(msgs []AIMessage)
func (r *Room) GetAIMessages() []AIMessage

Errors

var ErrRoomNotFound = errors.New("room not found")

Workspace Isolation

Each room gets an isolated workspace directory:
  1. Production: Copies /app/workspace-template/ to /app/workspaces/{name}/
  2. Development: Creates empty directory in temp folder
This provides filesystem isolation for each collaborative session.

Build docs developers (and LLMs) love