-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathconsoleFormatter.ts
More file actions
47 lines (38 loc) · 1.04 KB
/
consoleFormatter.ts
File metadata and controls
47 lines (38 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import type {ConsoleMessageData} from '../McpResponse.js';
const logLevels: Record<string, string> = {
log: 'Log',
info: 'Info',
warning: 'Warning',
error: 'Error',
exception: 'Exception',
assert: 'Assert',
};
export function formatConsoleEvent(msg: ConsoleMessageData): string {
const logLevel = logLevels[msg.type] ?? 'Log';
const text = msg.message;
const formattedArgs = formatArgs(msg.args, text);
return `${logLevel}> ${text} ${formattedArgs}`.trim();
}
// Only includes the first arg and indicates that there are more args
function formatArgs(args: string[], messageText: string): string {
if (args.length === 0) {
return '';
}
let formattedArgs = '';
const firstArg = args[0];
if (firstArg !== messageText) {
formattedArgs +=
typeof firstArg === 'object'
? JSON.stringify(firstArg)
: String(firstArg);
}
if (args.length > 1) {
return `${formattedArgs} ...`;
}
return formattedArgs;
}