import { Body, Controller, Post } from "@nestjs/common"; import { ApiOperation, ApiResponse, ApiTags } from "@nestjs/swagger"; import { BrowserService, ExecResult, OpenResult } from "./browser.service"; import { CodeExecutorService } from "../code-executor/code-executor.service"; import { OpenDto } from "./dto/open.dto"; import { ExecDto } from "./dto/exec.dto"; @ApiTags("browser") @Controller() export class BrowserController { constructor( private readonly browserService: BrowserService, private readonly codeExecutor: CodeExecutorService, ) {} @Post("open") @ApiOperation({ summary: "Open a URL with a stored session (cookies + localStorage)", }) @ApiResponse({ status: 201, description: "Page loaded successfully", schema: { properties: { url: { type: "string" }, title: { type: "string" }, content: { type: "string" }, }, }, }) @ApiResponse({ status: 400, description: "Invalid input" }) @ApiResponse({ status: 404, description: "Session not found" }) @ApiResponse({ status: 500, description: "Browser automation failed" }) open(@Body() dto: OpenDto): Promise { return this.browserService.open( dto.sessionName, dto.url, dto.readerMode ?? false, dto.selector, ); } @Post("exec") @ApiOperation({ summary: "Execute custom Playwright JavaScript within a stored session", description: "The `code` string is executed as an async function body with `page` (Playwright Page) and `context` (BrowserContext) in scope. The return value is serialised and returned as `result`.", }) @ApiResponse({ status: 201, description: "Code executed successfully", schema: { properties: { result: {} } }, }) @ApiResponse({ status: 400, description: "Invalid input" }) @ApiResponse({ status: 404, description: "Session not found" }) @ApiResponse({ status: 500, description: "Execution failed" }) exec(@Body() dto: ExecDto): Promise { this.codeExecutor.validate(dto.code); return this.browserService.exec(dto.sessionName, dto.code, dto.url); } }