|
| 1 | +/** |
| 2 | + * Boss Ghost MCP — Multi-profile management with CDP port allocation. |
| 3 | + * |
| 4 | + * Profiles define browser instances with isolated user data dirs, |
| 5 | + * CDP ports, and launch configurations. Inspired by OpenClaw's profile system. |
| 6 | + */ |
| 7 | + |
| 8 | +import {getConfig} from './config.js'; |
| 9 | +import type {ProfileConfig} from './types.js'; |
| 10 | +import {logger} from '../logger.js'; |
| 11 | + |
| 12 | +// CDP port range: 18800-18899 (same as OpenClaw) |
| 13 | +const CDP_PORT_RANGE_START = 18800; |
| 14 | +const CDP_PORT_RANGE_END = 18899; |
| 15 | + |
| 16 | +export interface ResolvedProfile { |
| 17 | + name: string; |
| 18 | + cdpPort: number; |
| 19 | + cdpUrl?: string; |
| 20 | + userDataDir?: string; |
| 21 | + driver: 'managed' | 'existing-session'; |
| 22 | + headless: boolean; |
| 23 | + channel: 'stable' | 'canary' | 'beta' | 'dev'; |
| 24 | + executablePath?: string; |
| 25 | + extraArgs: string[]; |
| 26 | + attachOnly: boolean; |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Build a ResolvedProfile from a ProfileConfig and its name. |
| 31 | + * Fills in defaults for any missing fields. |
| 32 | + */ |
| 33 | +function resolveFromConfig( |
| 34 | + name: string, |
| 35 | + config: ProfileConfig, |
| 36 | + portIndex: number, |
| 37 | +): ResolvedProfile { |
| 38 | + const cdpPort = config.cdpPort ?? CDP_PORT_RANGE_START + portIndex; |
| 39 | + |
| 40 | + if (cdpPort < CDP_PORT_RANGE_START || cdpPort > CDP_PORT_RANGE_END) { |
| 41 | + logger( |
| 42 | + 'Profile "%s" CDP port %d is outside range %d-%d', |
| 43 | + name, |
| 44 | + cdpPort, |
| 45 | + CDP_PORT_RANGE_START, |
| 46 | + CDP_PORT_RANGE_END, |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + return { |
| 51 | + name, |
| 52 | + cdpPort, |
| 53 | + cdpUrl: config.cdpUrl, |
| 54 | + userDataDir: config.userDataDir, |
| 55 | + driver: config.driver ?? 'managed', |
| 56 | + headless: config.headless ?? false, |
| 57 | + channel: config.channel ?? 'stable', |
| 58 | + executablePath: config.executablePath, |
| 59 | + extraArgs: config.extraArgs ?? [], |
| 60 | + attachOnly: config.attachOnly ?? false, |
| 61 | + }; |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Create a default profile with sensible defaults. |
| 66 | + * Used when no profiles are configured at all. |
| 67 | + */ |
| 68 | +function createDefaultProfile(): ResolvedProfile { |
| 69 | + return { |
| 70 | + name: 'default', |
| 71 | + cdpPort: CDP_PORT_RANGE_START, |
| 72 | + driver: 'managed', |
| 73 | + headless: false, |
| 74 | + channel: 'stable', |
| 75 | + extraArgs: [], |
| 76 | + attachOnly: false, |
| 77 | + }; |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * Get the default profile name from config, falling back to "default". |
| 82 | + */ |
| 83 | +function getDefaultProfileName(): string { |
| 84 | + const config = getConfig(); |
| 85 | + return config.defaultProfile ?? 'default'; |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * Get or create the default profile. |
| 90 | + * If the default profile is configured, resolves it. Otherwise creates |
| 91 | + * a minimal managed profile automatically. |
| 92 | + */ |
| 93 | +export function getDefaultProfile(): ResolvedProfile { |
| 94 | + const config = getConfig(); |
| 95 | + const defaultName = getDefaultProfileName(); |
| 96 | + const profiles = config.profiles; |
| 97 | + |
| 98 | + if (profiles && profiles[defaultName]) { |
| 99 | + const keys = Object.keys(profiles); |
| 100 | + const portIndex = keys.indexOf(defaultName); |
| 101 | + return resolveFromConfig(defaultName, profiles[defaultName], portIndex); |
| 102 | + } |
| 103 | + |
| 104 | + logger('No profile "%s" found in config — using auto-generated default', defaultName); |
| 105 | + return createDefaultProfile(); |
| 106 | +} |
| 107 | + |
| 108 | +/** |
| 109 | + * Resolve a named profile from config. |
| 110 | + * Returns null if the profile does not exist. |
| 111 | + */ |
| 112 | +export function resolveProfile(name: string): ResolvedProfile | null { |
| 113 | + const config = getConfig(); |
| 114 | + const profiles = config.profiles; |
| 115 | + |
| 116 | + if (!profiles || !profiles[name]) { |
| 117 | + logger('Profile "%s" not found in config', name); |
| 118 | + return null; |
| 119 | + } |
| 120 | + |
| 121 | + const keys = Object.keys(profiles); |
| 122 | + const portIndex = keys.indexOf(name); |
| 123 | + return resolveFromConfig(name, profiles[name], portIndex); |
| 124 | +} |
| 125 | + |
| 126 | +/** |
| 127 | + * List all configured profiles with their resolved settings. |
| 128 | + * If no profiles are configured, returns a single auto-generated default. |
| 129 | + */ |
| 130 | +export function listProfiles(): ResolvedProfile[] { |
| 131 | + const config = getConfig(); |
| 132 | + const profiles = config.profiles; |
| 133 | + |
| 134 | + if (!profiles || Object.keys(profiles).length === 0) { |
| 135 | + return [createDefaultProfile()]; |
| 136 | + } |
| 137 | + |
| 138 | + const keys = Object.keys(profiles); |
| 139 | + return keys.map((name, idx) => resolveFromConfig(name, profiles[name], idx)); |
| 140 | +} |
| 141 | + |
| 142 | +/** |
| 143 | + * Allocate the next available CDP port in the 18800-18899 range. |
| 144 | + * Skips any ports already in the usedPorts set. |
| 145 | + * Throws if no ports are available. |
| 146 | + */ |
| 147 | +export function allocateCdpPort(usedPorts: Set<number>): number { |
| 148 | + for (let port = CDP_PORT_RANGE_START; port <= CDP_PORT_RANGE_END; port++) { |
| 149 | + if (!usedPorts.has(port)) { |
| 150 | + return port; |
| 151 | + } |
| 152 | + } |
| 153 | + throw new Error( |
| 154 | + `No available CDP ports in range ${CDP_PORT_RANGE_START}-${CDP_PORT_RANGE_END}. ` + |
| 155 | + `All ${CDP_PORT_RANGE_END - CDP_PORT_RANGE_START + 1} ports are in use.`, |
| 156 | + ); |
| 157 | +} |
| 158 | + |
| 159 | +/** |
| 160 | + * Ensure the default profile exists. |
| 161 | + * Returns the resolved default profile, auto-creating one if no profiles |
| 162 | + * are configured. |
| 163 | + */ |
| 164 | +export function ensureDefaultProfile(): ResolvedProfile { |
| 165 | + return getDefaultProfile(); |
| 166 | +} |
0 commit comments