Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions src/cdp-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,10 +350,15 @@ export class CometCDPClient {
} catch { /* target gone */ }
}

// Find best target
// Find best target. Prefer the MAIN Perplexity tab — explicitly
// exclude `sidecar` URLs, which Comet uses for its right-panel
// chat helper. Connecting to the sidecar by mistake silently
// routes `sendPrompt` / `stopAgent` to the wrong tab.
const targets = await this.listTargets();
const target = targets.find(t => t.type === 'page' && t.url.includes('perplexity.ai')) ||
targets.find(t => t.type === 'page' && t.url !== 'about:blank');
const target =
targets.find(t => t.type === 'page' && t.url.includes('perplexity.ai') && !t.url.includes('sidecar')) ||
targets.find(t => t.type === 'page' && t.url.includes('perplexity.ai')) ||
targets.find(t => t.type === 'page' && t.url !== 'about:blank');

if (target) {
return await this.connect(target.id);
Expand Down Expand Up @@ -405,16 +410,18 @@ export class CometCDPClient {
*/
async ensureOnPerplexityTab(): Promise<boolean> {
try {
// First check if current connection is valid and on Perplexity
// First check if current connection is valid and on Perplexity.
// Reject the sidecar URL — same reason as in reconnect(): the
// sidecar matches `perplexity.ai` but is a different tab.
if (this.client) {
try {
const urlResult = await this.client.Runtime.evaluate({
expression: 'window.location.href',
timeout: 2000
});
const currentUrl = urlResult.result.value as string;
if (currentUrl?.includes('perplexity.ai')) {
return true; // Already on Perplexity tab
if (currentUrl?.includes('perplexity.ai') && !currentUrl.includes('sidecar')) {
return true; // Already on Perplexity main tab
}
} catch {
// Current connection is stale, continue to reconnect
Expand All @@ -429,10 +436,10 @@ export class CometCDPClient {
return true;
}

// Fallback: find any Perplexity tab
// Fallback: find any Perplexity tab that isn't the sidecar.
const targets = await this.listTargets();
const perplexityTab = targets.find(t =>
t.type === 'page' && t.url.includes('perplexity.ai')
t.type === 'page' && t.url.includes('perplexity.ai') && !t.url.includes('sidecar')
);

if (perplexityTab) {
Expand All @@ -458,7 +465,10 @@ export class CometCDPClient {
timeout: 2000
});
const url = result.result.value as string;
return url?.includes('perplexity.ai') || false;
// Sidecar URLs match `perplexity.ai` but are a different surface; treat
// them as "not the main tab" so callers don't dispatch sendPrompt /
// stopAgent there.
return !!url && url.includes('perplexity.ai') && !url.includes('sidecar');
} catch {
return false;
}
Expand Down
16 changes: 13 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,13 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
const targets = await cometClient.listTargets();
const freshTargets = targets; // Use the same list, no cleanup

// Prefer connecting to existing Perplexity tab, or any page tab
const perplexityTab = freshTargets.find(t => t.type === 'page' && t.url.includes('perplexity.ai'));
// Prefer connecting to the MAIN Perplexity tab (not the sidecar).
// Comet's right-panel chat helper lives at a sidecar URL that also
// matches `perplexity.ai` substring — connecting to it routes
// sendPrompt / stopAgent to the wrong tab.
const perplexityTab =
freshTargets.find(t => t.type === 'page' && t.url.includes('perplexity.ai') && !t.url.includes('sidecar')) ||
freshTargets.find(t => t.type === 'page' && t.url.includes('perplexity.ai'));
const anyPage = perplexityTab || freshTargets.find(t => t.type === 'page');

if (anyPage) {
Expand Down Expand Up @@ -188,7 +193,12 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
try {
await cometClient.startComet(9223);
const targets = await cometClient.listTargets();
const page = targets.find(t => t.type === 'page');
// Prefer Perplexity main tab over the sidecar (see comet_connect
// for rationale). Fall back to any page tab if neither exists.
const page =
targets.find(t => t.type === 'page' && t.url.includes('perplexity.ai') && !t.url.includes('sidecar')) ||
targets.find(t => t.type === 'page' && t.url.includes('perplexity.ai')) ||
targets.find(t => t.type === 'page');
if (page) await cometClient.connect(page.id);
} catch {
return { content: [{ type: "text", text: "Error: Failed to establish connection to Comet browser" }] };
Expand Down
Loading