MCPcopy
hub / github.com/RedPlanetHQ/core / launchSession

Function launchSession

packages/cli/src/utils/browser-manager.ts:153–218  ·  view source on GitHub ↗
(
	sessionName: string,
	headed: boolean,
)

Source from the content-addressed store, hash-verified

151}
152
153export async function launchSession(
154 sessionName: string,
155 headed: boolean,
156): Promise<{session: BrowserSession; error?: string}> {
157 // Close existing instance if any
158 const existing = sessionMap.get(sessionName);
159 if (existing) {
160 await existing.context.close().catch(() => {});
161 sessionMap.delete(sessionName);
162 }
163
164 const sessionConfig = getSessionConfig(sessionName);
165 if (!sessionConfig) {
166 return {
167 session: undefined as unknown as BrowserSession,
168 error: `Session "${sessionName}" is not configured. Run: corebrain browser create-session ${sessionName} --profile <profile>`,
169 };
170 }
171
172 try {
173 const {chromium} = await import('playwright');
174
175 const profileDir = getProfileDir(sessionConfig.profile);
176 fs.mkdirSync(profileDir, {recursive: true});
177
178 const browserConfig = getBrowserExecutable();
179 const launchOptions: import('playwright').LaunchOptions = {
180 headless: !headed,
181 args: ['--remote-debugging-port=0'],
182 };
183 if (browserConfig.type !== 'default' && browserConfig.path) {
184 launchOptions.executablePath = browserConfig.path;
185 }
186
187 const context = await chromium.launchPersistentContext(profileDir, launchOptions);
188
189 context.on('close', () => {
190 sessionMap.delete(sessionName);
191 });
192
193 const page = context.pages()[0] ?? (await context.newPage());
194
195 const cdp = await captureCdpEndpoint(profileDir);
196
197 const session: BrowserSession = {
198 context,
199 page,
200 sessionName,
201 profile: sessionConfig.profile,
202 profileDir,
203 headed,
204 createdAt: Date.now(),
205 cdpPort: cdp?.port,
206 cdpWsEndpoint: cdp?.wsEndpoint,
207 cdpHttpEndpoint: cdp?.httpEndpoint,
208 };
209
210 sessionMap.set(sessionName, session);

Callers 2

runOpenHeadFunction · 0.90
runBrowserOpenFunction · 0.90

Calls 9

getSessionConfigFunction · 0.90
getProfileDirFunction · 0.90
getBrowserExecutableFunction · 0.90
captureCdpEndpointFunction · 0.85
catchMethod · 0.80
onMethod · 0.80
getMethod · 0.65
closeMethod · 0.65
deleteMethod · 0.65

Tested by

no test coverage detected