diff options
| author | amankumarsingh77 <amankumarsingh7702@gmal.com> | 2026-05-02 16:34:39 +0530 |
|---|---|---|
| committer | amankumarsingh77 <amankumarsingh7702@gmal.com> | 2026-05-02 16:34:39 +0530 |
| commit | dc07a5157188c09b79487af8b47fbda51b6af7d6 (patch) | |
| tree | 64ea434fb12e72f7ad511cbd9963463085db849d /src | |
| parent | 5664dc8f6f41e93a8c320d7084451e25f3d93438 (diff) | |
feat(domains): migrate viewport_resize
Diffstat (limited to 'src')
| -rw-r--r-- | src/domains/viewport.ts | 40 | ||||
| -rw-r--r-- | src/registry.ts | 2 | ||||
| -rw-r--r-- | src/tools.ts | 47 |
3 files changed, 42 insertions, 47 deletions
diff --git a/src/domains/viewport.ts b/src/domains/viewport.ts new file mode 100644 index 0000000..015ac68 --- /dev/null +++ b/src/domains/viewport.ts @@ -0,0 +1,40 @@ +import { Type } from "typebox"; +import { type Result, err, ok } from "../util/result"; +import { defineBrowserTool, type ToolErr, type ToolOk } from "../util/tool"; + +const ViewportArgs = Type.Object({ + width: Type.Integer({ minimum: 100, maximum: 8000, description: "Viewport CSS pixel width" }), + height: Type.Integer({ minimum: 100, maximum: 8000, description: "Viewport CSS pixel height" }), + deviceScaleFactor: Type.Optional( + Type.Number({ minimum: 0.5, maximum: 4, default: 1, description: "Device pixel ratio" }), + ), +}); + +export const viewportResizeTool = defineBrowserTool({ + name: "browser_viewport_resize", + label: "Browser Viewport Resize", + description: "Override the viewport size and device pixel ratio for responsive testing.", + promptSnippet: "Resize the viewport (responsive testing)", + promptGuidelines: [ + "Width/height in CSS pixels (e.g., 375x667 for iPhone SE).", + "Pass deviceScaleFactor=2 to simulate retina displays.", + ], + parameters: ViewportArgs, + async handler(args, { client }): Promise<Result<ToolOk, ToolErr>> { + const r = await client.session().call("Emulation.setDeviceMetricsOverride", { + width: args.width, + height: args.height, + deviceScaleFactor: args.deviceScaleFactor ?? 1, + mobile: false, + }); + if (!r.success) return err({ kind: "cdp_error", message: r.error.message }); + return ok({ + text: `Viewport set to ${args.width}x${args.height} @${args.deviceScaleFactor ?? 1}x`, + details: { + width: args.width, + height: args.height, + deviceScaleFactor: args.deviceScaleFactor ?? 1, + }, + }); + }, +}); diff --git a/src/registry.ts b/src/registry.ts index 9490cbf..2e8e574 100644 --- a/src/registry.ts +++ b/src/registry.ts @@ -11,6 +11,7 @@ import { navigateTool, openUrlsTool } from "./domains/navigate"; import { goBackTool, goForwardTool, reloadTool } from "./domains/history"; import { listTabsTool, currentTabTool, switchTabTool, newTabTool } from "./domains/tabs"; import { uploadFileTool, downloadTool, printToPdfTool } from "./domains/files"; +import { viewportResizeTool } from "./domains/viewport"; const TOOLS: ReadonlyArray<AnyBrowserToolDefinition> = [ clickTool, @@ -35,6 +36,7 @@ const TOOLS: ReadonlyArray<AnyBrowserToolDefinition> = [ uploadFileTool, downloadTool, printToPdfTool, + viewportResizeTool, ]; export const registerAllTools = (pi: ExtensionAPI, client: BrowserClient): void => { diff --git a/src/tools.ts b/src/tools.ts index 44bf5c6..1e5308e 100644 --- a/src/tools.ts +++ b/src/tools.ts @@ -270,53 +270,6 @@ export function registerTools(pi: ExtensionAPI, daemon: BrowserDaemon): void { }); // ═══════════════════════════════════════════════════════════════════════════ - // browser_viewport_resize - // ═══════════════════════════════════════════════════════════════════════════ - pi.registerTool({ - name: "browser_viewport_resize", - label: "Browser Viewport Resize", - description: - "Resize the browser viewport. Useful for responsive testing or when a page " + - "requires a specific viewport width. Adjusts both the visible viewport and " + - "the device pixel ratio.", - promptSnippet: "Resize the browser viewport (width, height, deviceScaleFactor)", - promptGuidelines: [ - "Use browser_viewport_resize for responsive testing or when a page needs specific dimensions.", - "width and height are in CSS pixels (not device pixels).", - "deviceScaleFactor defaults to 1. Use 2 for HiDPI/Retina emulation.", - "Resizing may cause layout shifts — take a screenshot afterward to confirm.", - ], - parameters: Type.Object({ - width: Type.Number({ description: "Viewport width in CSS pixels" }), - height: Type.Number({ description: "Viewport height in CSS pixels" }), - deviceScaleFactor: Type.Optional(Type.Number({ description: "Device pixel ratio. Default: 1" })), - }), - async execute(_id, params) { - try { - await daemon.ensureAlive(); - await daemon.setViewportSize( - params.width, - params.height, - params.deviceScaleFactor, - ); - return { - content: [{ - type: "text" as const, - text: `Viewport resized to ${params.width}x${params.height}${params.deviceScaleFactor ? ` @${params.deviceScaleFactor}x` : ""}.`, - }], - details: undefined, - }; - } catch (err) { - return { - isError: true, - content: [{ type: "text" as const, text: `Viewport resize failed: ${err instanceof Error ? err.message : String(err)}` }], - details: undefined, - }; - } - }, - }); - - // ═══════════════════════════════════════════════════════════════════════════ // browser_drag_and_drop // ═══════════════════════════════════════════════════════════════════════════ pi.registerTool({ |
