diff --git a/src/tools/common.ts b/src/tools/common.ts index a04ddaf..71583a5 100644 --- a/src/tools/common.ts +++ b/src/tools/common.ts @@ -20,7 +20,7 @@ import path from 'path'; import { z } from 'zod'; import { zodToJsonSchema } from 'zod-to-json-schema'; -import { captureAriaSnapshot, runAndWait } from './utils'; +import { captureAriaSnapshot, runAndWait, sanitizeForFilePath } from './utils'; import type { ToolFactory, Tool } from './tool'; @@ -127,7 +127,7 @@ export const pdf: Tool = { }, handle: async context => { const page = context.existingPage(); - const fileName = path.join(os.tmpdir(), `/page-${new Date().toISOString()}.pdf`); + const fileName = path.join(os.tmpdir(), sanitizeForFilePath(`page-${new Date().toISOString()}`)) + '.pdf'; await page.pdf({ path: fileName }); return { content: [{ diff --git a/src/tools/utils.ts b/src/tools/utils.ts index 29b6dc9..e9c984e 100644 --- a/src/tools/utils.ts +++ b/src/tools/utils.ts @@ -106,3 +106,7 @@ export async function captureAriaSnapshot(context: Context, status: string = '') content: [{ type: 'text', text: lines.join('\n') }], }; } + +export function sanitizeForFilePath(s: string) { + return s.replace(/[\x00-\x2C\x2E-\x2F\x3A-\x40\x5B-\x60\x7B-\x7F]+/g, '-'); +} diff --git a/tests/basic.spec.ts b/tests/basic.spec.ts index ee9ecc7..5a04cca 100644 --- a/tests/basic.spec.ts +++ b/tests/basic.spec.ts @@ -331,3 +331,25 @@ test('cdp server', async ({ cdpEndpoint, startClient }) => { ` ); }); + +test('save as pdf', async ({ client }) => { + expect(await client.callTool({ + name: 'browser_navigate', + arguments: { + url: 'data:text/html,