nicer logging of returned output

This commit is contained in:
Tommy D. Rossi
2026-01-26 12:46:36 +01:00
parent 905288a790
commit 4fa618134d
+24 -8
View File
@@ -61,6 +61,7 @@ const EXTENSION_NOT_CONNECTED_ERROR = `The Playwriter Chrome extension is not co
const NO_TABS_ENABLED_ERROR = `No browser tabs have Playwriter enabled. Click the extension icon on at least one tab to enable it.` const NO_TABS_ENABLED_ERROR = `No browser tabs have Playwriter enabled. Click the extension icon on at least one tab to enable it.`
const MAX_LOGS_PER_PAGE = 5000 const MAX_LOGS_PER_PAGE = 5000
const DEFAULT_PLAYWRIGHT_TIMEOUT = 10_000
const ALLOWED_MODULES = new Set([ const ALLOWED_MODULES = new Set([
'path', 'node:path', 'path', 'node:path',
@@ -113,6 +114,12 @@ function isRegExp(value: any): value is RegExp {
) )
} }
function isPromise(value: any): value is Promise<unknown> {
return typeof value === 'object' && value !== null && typeof value.then === 'function'
}
export class PlaywrightExecutor { export class PlaywrightExecutor {
private isConnected = false private isConnected = false
private page: Page | null = null private page: Page | null = null
@@ -279,6 +286,8 @@ export class PlaywrightExecutor {
} }
const page = pages[0] const page = pages[0]
context.setDefaultTimeout(DEFAULT_PLAYWRIGHT_TIMEOUT)
context.pages().forEach((p) => this.setupPageConsoleListener(p)) context.pages().forEach((p) => this.setupPageConsoleListener(p))
await this.preserveSystemColorScheme(context) await this.preserveSystemColorScheme(context)
@@ -353,6 +362,8 @@ export class PlaywrightExecutor {
} }
const page = pages[0] const page = pages[0]
context.setDefaultTimeout(DEFAULT_PLAYWRIGHT_TIMEOUT)
context.pages().forEach((p) => this.setupPageConsoleListener(p)) context.pages().forEach((p) => this.setupPageConsoleListener(p))
await this.preserveSystemColorScheme(context) await this.preserveSystemColorScheme(context)
@@ -619,6 +630,7 @@ export class PlaywrightExecutor {
const vmContext = vm.createContext(vmContextObj) const vmContext = vm.createContext(vmContextObj)
const wrappedCode = `(async () => { ${code} })()` const wrappedCode = `(async () => { ${code} })()`
const hasExplicitReturn = /\breturn\b/.test(code)
const result = await Promise.race([ const result = await Promise.race([
vm.runInContext(wrappedCode, vmContext, { timeout, displayErrors: true }), vm.runInContext(wrappedCode, vmContext, { timeout, displayErrors: true }),
@@ -627,15 +639,19 @@ export class PlaywrightExecutor {
let responseText = formatConsoleLogs(consoleLogs) let responseText = formatConsoleLogs(consoleLogs)
if (result !== undefined) { // Only show return value if user explicitly used return
responseText += 'Return value:\n' if (hasExplicitReturn) {
if (typeof result === 'string') { const resolvedResult = isPromise(result) ? await result : result
responseText += result if (resolvedResult !== undefined) {
} else { const formatted = util.inspect(resolvedResult, { depth: 4, colors: false, maxArrayLength: 100, breakLength: 80 })
responseText += JSON.stringify(result, null, 2) if (formatted.trim()) {
responseText += `[return value] ${formatted}\n`
}
} }
} else if (consoleLogs.length === 0) { }
responseText += 'Code executed successfully (no output)'
if (!responseText.trim()) {
responseText = 'Code executed successfully (no output)'
} }
for (const screenshot of screenshotCollector) { for (const screenshot of screenshotCollector) {