2025-04-04 15:22:00 -07:00
|
|
|
/**
|
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { z } from 'zod';
|
2025-07-22 07:53:33 -07:00
|
|
|
import { defineTabTool } from './tool.js';
|
2025-04-04 15:22:00 -07:00
|
|
|
|
2025-04-30 23:06:56 +02:00
|
|
|
import * as javascript from '../javascript.js';
|
|
|
|
|
import { outputFile } from '../config.js';
|
2025-08-15 06:42:16 -06:00
|
|
|
import { ArtifactManagerRegistry } from '../artifactManager.js';
|
2025-04-04 15:22:00 -07:00
|
|
|
|
2025-05-09 03:04:18 +09:00
|
|
|
const pdfSchema = z.object({
|
|
|
|
|
filename: z.string().optional().describe('File name to save the pdf to. Defaults to `page-{timestamp}.pdf` if not specified.'),
|
|
|
|
|
});
|
|
|
|
|
|
2025-07-22 07:53:33 -07:00
|
|
|
const pdf = defineTabTool({
|
2025-04-04 17:14:30 -07:00
|
|
|
capability: 'pdf',
|
2025-04-16 19:36:48 -07:00
|
|
|
|
2025-04-04 15:22:00 -07:00
|
|
|
schema: {
|
|
|
|
|
name: 'browser_pdf_save',
|
2025-05-05 17:38:22 -07:00
|
|
|
title: 'Save as PDF',
|
2025-04-04 15:22:00 -07:00
|
|
|
description: 'Save page as PDF',
|
2025-05-09 03:04:18 +09:00
|
|
|
inputSchema: pdfSchema,
|
2025-05-05 17:38:22 -07:00
|
|
|
type: 'readOnly',
|
2025-04-04 15:22:00 -07:00
|
|
|
},
|
2025-04-16 19:36:48 -07:00
|
|
|
|
2025-07-22 16:36:21 -07:00
|
|
|
handle: async (tab, params, response) => {
|
2025-08-15 06:42:16 -06:00
|
|
|
// Use centralized artifact storage if configured
|
|
|
|
|
let fileName: string;
|
|
|
|
|
const registry = ArtifactManagerRegistry.getInstance();
|
|
|
|
|
const artifactManager = tab.context.sessionId ? registry.getManager(tab.context.sessionId) : undefined;
|
|
|
|
|
|
|
|
|
|
if (artifactManager) {
|
|
|
|
|
const defaultName = params.filename ?? `page-${new Date().toISOString()}.pdf`;
|
|
|
|
|
fileName = artifactManager.getArtifactPath(defaultName);
|
|
|
|
|
} else {
|
|
|
|
|
fileName = await outputFile(tab.context.config, params.filename ?? `page-${new Date().toISOString()}.pdf`);
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-22 16:36:21 -07:00
|
|
|
response.addCode(`// Save page as ${fileName}`);
|
|
|
|
|
response.addCode(`await page.pdf(${javascript.formatObject({ path: fileName })});`);
|
|
|
|
|
response.addResult(`Saved page as ${fileName}`);
|
|
|
|
|
await tab.page.pdf({ path: fileName });
|
2025-04-04 15:22:00 -07:00
|
|
|
},
|
2025-04-22 13:24:38 +02:00
|
|
|
});
|
2025-04-04 15:22:00 -07:00
|
|
|
|
|
|
|
|
export default [
|
|
|
|
|
pdf,
|
|
|
|
|
];
|