Skip to content

Commit f302b45

Browse files
author
Natallia Harshunova
committed
Fix comments
1 parent 42fca71 commit f302b45

File tree

3 files changed

+78
-63
lines changed

3 files changed

+78
-63
lines changed

src/McpResponse.ts

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
} from './formatters/networkFormatter.js';
2424
import {formatSnapshotNode} from './formatters/snapshotFormatter.js';
2525
import {getIssueDescription} from './issue-descriptions.js';
26+
import {logger} from './logger.js';
2627
import type {McpContext} from './McpContext.js';
2728
import type {
2829
ConsoleMessage,
@@ -283,57 +284,58 @@ export class McpResponse implements Response {
283284
});
284285
}
285286

286-
consoleListData = await Promise.all(
287-
messages.map(async (item): Promise<ConsoleMessageData> => {
288-
const consoleMessageStableId =
289-
context.getConsoleMessageStableId(item);
290-
if ('args' in item) {
291-
const consoleMessage = item as ConsoleMessage;
292-
return {
293-
consoleMessageStableId,
294-
type: consoleMessage.type(),
295-
message: consoleMessage.text(),
296-
args: await Promise.all(
297-
consoleMessage.args().map(async arg => {
298-
const stringArg = await arg.jsonValue().catch(() => {
299-
// Ignore errors.
300-
});
301-
return typeof stringArg === 'object'
302-
? JSON.stringify(stringArg)
303-
: String(stringArg);
304-
}),
305-
),
306-
};
307-
}
308-
if (item instanceof AggregatedIssue) {
309-
const count = item.getAggregatedIssuesCount();
310-
const filename = item.getDescription()?.file;
311-
const rawMarkdown = filename ? getIssueDescription(filename) : null;
312-
if (!rawMarkdown) {
287+
consoleListData = (
288+
await Promise.all(
289+
messages.map(async (item): Promise<ConsoleMessageData | null> => {
290+
const consoleMessageStableId =
291+
context.getConsoleMessageStableId(item);
292+
if ('args' in item) {
293+
const consoleMessage = item as ConsoleMessage;
294+
return {
295+
consoleMessageStableId,
296+
type: consoleMessage.type(),
297+
message: consoleMessage.text(),
298+
args: await Promise.all(
299+
consoleMessage.args().map(async arg => {
300+
const stringArg = await arg.jsonValue().catch(() => {
301+
// Ignore errors.
302+
});
303+
return typeof stringArg === 'object'
304+
? JSON.stringify(stringArg)
305+
: String(stringArg);
306+
}),
307+
),
308+
};
309+
}
310+
if (item instanceof AggregatedIssue) {
311+
const count = item.getAggregatedIssuesCount();
312+
const filename = item.getDescription()?.file;
313+
const rawMarkdown = filename
314+
? getIssueDescription(filename)
315+
: null;
316+
if (!rawMarkdown) {
317+
logger(`no markdown ${filename} found for issue:` + item.code);
318+
return null;
319+
}
320+
const markdownAst = Marked.Marked.lexer(rawMarkdown);
321+
const title = findTitleFromMarkdownAst(markdownAst);
313322
return {
314323
consoleMessageStableId,
315324
type: 'issue',
316-
message: `${item.code()} (count: ${count})`,
325+
message: `${title}`,
326+
count,
317327
args: [],
318328
};
319329
}
320-
const markdownAst = Marked.Marked.lexer(rawMarkdown);
321-
const title = findTitleFromMarkdownAst(markdownAst);
322330
return {
323331
consoleMessageStableId,
324-
type: 'issue',
325-
message: `${title} (count: ${count})`,
332+
type: 'error',
333+
message: (item as Error).message,
326334
args: [],
327335
};
328-
}
329-
return {
330-
consoleMessageStableId,
331-
type: 'error',
332-
message: (item as Error).message,
333-
args: [],
334-
};
335-
}),
336-
);
336+
}),
337+
)
338+
).filter(item => item !== null);
337339
}
338340

339341
return this.format(toolName, context, {

src/PageCollector.ts

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
} from '../node_modules/chrome-devtools-frontend/mcp/mcp.js';
1414

1515
import {FakeIssuesManager} from './DevtoolsUtils.js';
16+
import {logger} from './logger.js';
1617
import type {ConsoleMessage} from './third_party/index.js';
1718
import {
1819
type Browser,
@@ -242,28 +243,36 @@ export class ConsoleCollector extends PageCollector<
242243
},
243244
);
244245

245-
const session = await page.createCDPSession();
246-
session.on('Audits.issueAdded', data => {
247-
// @ts-expect-error Types of protocol from Puppeteer and CDP are incopatible for Issues but it's the same type
248-
const issue = createIssuesFromProtocolIssue(null, data.issue)[0];
249-
if (!issue) {
250-
return;
251-
}
252-
const seenKeys = this.#seenIssueKeys.get(page)!;
253-
const primaryKey = issue.primaryKey();
254-
if (seenKeys.has(primaryKey)) return;
255-
seenKeys.add(primaryKey);
256-
257-
const mockManager = this.#mockIssuesManagers.get(page);
258-
if (mockManager) {
259-
mockManager.dispatchEventToListeners(IssuesManagerEvents.ISSUE_ADDED, {
260-
issue,
261-
// @ts-expect-error We don't care that issues model is null
262-
issuesModel: null,
263-
});
264-
}
265-
});
266-
await session.send('Audits.enable');
246+
try {
247+
const session = await page.createCDPSession();
248+
session.on('Audits.issueAdded', data => {
249+
// @ts-expect-error Types of protocol from Puppeteer and CDP are incopatible for Issues but it's the same type
250+
const issue = createIssuesFromProtocolIssue(null, data.issue)[0];
251+
if (!issue) {
252+
return;
253+
}
254+
const seenKeys = this.#seenIssueKeys.get(page)!;
255+
const primaryKey = issue.primaryKey();
256+
if (seenKeys.has(primaryKey)) return;
257+
seenKeys.add(primaryKey);
258+
259+
const mockManager = this.#mockIssuesManagers.get(page);
260+
if (mockManager) {
261+
mockManager.dispatchEventToListeners(
262+
IssuesManagerEvents.ISSUE_ADDED,
263+
{
264+
issue,
265+
// @ts-expect-error We don't care that issues model is null
266+
issuesModel: null,
267+
},
268+
);
269+
}
270+
});
271+
await session.send('Audits.enable');
272+
} catch (e) {
273+
const errorText = e instanceof Error ? e.message : JSON.stringify(e);
274+
logger(`Error subscribing to issues: ${errorText}`);
275+
}
267276
}
268277

269278
override cleanupPageDestroyed(page: Page) {

src/formatters/consoleFormatter.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@ export interface ConsoleMessageData {
88
consoleMessageStableId: number;
99
type?: string;
1010
message?: string;
11+
count?: number;
1112
args?: string[];
1213
}
1314

1415
// The short format for a console message, based on a previous format.
1516
export function formatConsoleEventShort(msg: ConsoleMessageData): string {
17+
if (msg.type === 'issue') {
18+
return `msgid=${msg.consoleMessageStableId} [${msg.type}] ${msg.message} (count: ${msg.count}) (${msg.args?.length ?? 0} args)`;
19+
}
1620
return `msgid=${msg.consoleMessageStableId} [${msg.type}] ${msg.message} (${msg.args?.length ?? 0} args)`;
1721
}
1822

0 commit comments

Comments
 (0)