/** * console.ts: read console output captured by the recorder. * * list_console_messages -> listConsoleMessages * get_console_message -> getConsoleMessage * * READ vs CAPTURE model (see recorder.ts header): * - default ({ reload: false }): read the target's shared "latest" buffer * (rec-.jsonl) and return its parsed console entries. If no capture * has run for this target, returns an empty list. * - { reload: true }: run a captureWindow (records BOTH console+network into a * unique per-capture file), Page.reload, capture for `durationMs` (default * 2500ms), stop, then read+return the console entries from that capture. The * both-domains capture means a network reload never wipes console history. * * SERVICE WORKERS (1.9.1): list_console_messages accepts `worker:` * and a bare worker target id, Chrome only (Capability "worker.targets"). A * worker's console.log arrives as Runtime.consoleAPICalled on its own session * (measured; `Log.entryAdded` never fires there). There is no reload to drive a * worker capture, so reload:true means "listen for durationMs" — see * recorder.ts's header. */ import { readFile } from "node:fs/promises"; import { CdpError } from "../client.ts"; import type { TargetSelector } from "../types.ts"; import { assertWakeApplies, recFile, captureWindow, resolveRecorderTarget } from "./recorder.ts"; import type { RecLine } from "./recorder.ts"; const DEFAULT_CAPTURE_MS = 2500; /** A normalized console entry returned to callers. */ export interface ConsoleEntry { index: number; /** Origin domain: "console" (Runtime.consoleAPICalled), "exception" (Runtime.exceptionThrown), "log" (Log.entryAdded). */ source: "console" | "exception" | "log"; /** Severity level: log/info/warn/error/debug/verbose. */ level: string; /** Best-effort flattened text of the message. */ text: string; /** Page URL associated with the message, if present. */ url?: string; /** Source line number, if present. */ lineNumber?: number; /** Wall-clock capture time (ms since epoch). */ ts: number; } export interface ListConsoleMessagesArgs { target?: TargetSelector; /** Record fresh by reloading the page and capturing for a window. */ reload?: boolean; /** Capture window for reload mode (ms). Default 2500. */ durationMs?: number; /** Worker targets only, and only with reload:true — start an idle-evicted MV3 worker first (default true). */ wake?: boolean; } export interface GetConsoleMessageArgs { target?: TargetSelector; /** Zero-based index into the parsed console entries. Default 0. */ index?: number; } /** Stringify a single CDP RemoteObject arg into readable text. */ function remoteObjectToText(arg: Record | undefined): string { if (!arg) return ""; if (arg.value !== undefined && arg.value !== null) return String(arg.value); if (typeof arg.description === "string") return arg.description; if (typeof arg.unserializableValue === "string") return arg.unserializableValue; if (arg.type === "undefined") return "undefined"; if (arg.type === "object" && arg.subtype === "null") return "null"; if (typeof arg.type === "string") return `[${arg.type}]`; return ""; } /** Map a persisted JSONL line into a normalized ConsoleEntry. */ function lineToEntry(line: RecLine, index: number): ConsoleEntry | null { const p = line.params; if (line.method === "Runtime.consoleAPICalled") { const args = Array.isArray(p.args) ? (p.args as Array>) : []; return { index, source: "console", level: typeof p.type === "string" ? p.type : "log", text: args.map(remoteObjectToText).join(" "), url: undefined, ts: line.ts, }; } if (line.method === "Runtime.exceptionThrown") { const detail = (p.exceptionDetails ?? {}) as Record; const exception = (detail.exception ?? {}) as Record; const text = remoteObjectToText(exception) || (typeof detail.text === "string" ? detail.text : "uncaught exception"); return { index, source: "exception", level: "error", text, url: typeof detail.url === "string" ? detail.url : undefined, lineNumber: typeof detail.lineNumber === "number" ? detail.lineNumber : undefined, ts: line.ts, }; } if (line.method === "Log.entryAdded") { const entry = (p.entry ?? {}) as Record; return { index, source: "log", level: typeof entry.level === "string" ? entry.level : "info", text: typeof entry.text === "string" ? entry.text : "", url: typeof entry.url === "string" ? entry.url : undefined, lineNumber: typeof entry.lineNumber === "number" ? entry.lineNumber : undefined, ts: line.ts, }; } return null; } /** Parse a JSONL buffer file into console entries (skips network lines). */ async function readConsoleEntries(file: string): Promise { let raw: string; try { raw = await readFile(file, "utf8"); } catch { return []; // no buffer yet } const out: ConsoleEntry[] = []; for (const rawLine of raw.split("\n")) { const trimmed = rawLine.trim(); if (!trimmed) continue; let line: RecLine; try { line = JSON.parse(trimmed) as RecLine; } catch { continue; } if (line.kind !== "console") continue; const entry = lineToEntry(line, out.length); if (entry) out.push(entry); } return out; } /** * list_console_messages: return console output for the target. With * `reload:true`, records a fresh capture window by reloading the page. */ export async function listConsoleMessages(args: ListConsoleMessagesArgs = {}): Promise<{ target: { id: string; url: string; title: string }; count: number; messages: ConsoleEntry[]; droppedWrites?: number; }> { assertWakeApplies(args.target, args.wake, args.reload === true); if (args.reload) { const cap = await captureWindow(args.target, args.durationMs ?? DEFAULT_CAPTURE_MS, { wake: args.wake }); await cap.stop(); const messages = await readConsoleEntries(cap.file); return { target: cap.resolved, count: messages.length, messages, droppedWrites: cap.droppedWrites(), }; } const target = await resolveRecorderTarget(args.target, { capture: false }); const messages = await readConsoleEntries(recFile(target.id)); return { target: { id: target.id, url: target.url, title: target.title }, count: messages.length, messages, }; } /** * get_console_message: return a single console entry by index from the * existing buffer for the target. Throws if the index is out of range. */ export async function getConsoleMessage(args: GetConsoleMessageArgs = {}): Promise { // Reads the SAME buffer list_console_messages just returned, so it resolves // through the same read-path resolver: a worker target already reached this // code through the 1.9.0 bare-id/`worker:` arm, and routing it here is what // makes the miss message match the buffer it is actually reading. It is // deliberately NOT advertised as worker-capable in the manifest (see // WORKER_CAPABLE_TOOLS): index-into-a-buffer is not the worker story, and the // drift test keeps the advertised list to the four tools that are. const target = await resolveRecorderTarget(args.target, { capture: false }); const messages = await readConsoleEntries(recFile(target.id)); const index = args.index ?? 0; const entry = messages[index]; if (!entry) { throw new CdpError( `no console message at index ${index} (have ${messages.length}); run list_console_messages or list with reload:true first`, ); } return entry; } /* * CDP methods / domains used (via recorder.ts + this module): * Page.enable (reload mode, PAGE targets only) * Page.reload (reload mode, PAGE targets only) * Runtime.enable / Runtime.consoleAPICalled / Runtime.exceptionThrown (recorder) * Log.enable / Log.entryAdded (recorder) * * Parity gaps vs chrome-devtools-mcp: * - RemoteObject args are flattened to text best-effort; deep object inspection * (expandable previews) the DevTools UI shows is not reconstructed. * - Stack traces from exceptions are summarized to the top message + url/line, * not the full frame list. * - Without reload:true the result reflects whatever a prior recorder buffered; * the MCP keeps a live in-process buffer for its managed page. */