feat(scenario): run logs, lint/format tooling, CONTRIBUTING
- add ScenarioRunLogEntity to persist step script output to DB - stepLogger dual-writes to NestJS logger and DB (fire-and-forget) - add GET /scenarios/:id/run/:runId returning run, stepRuns and logs - add POST /scenarios/:id/run/:runId/wait (polls until terminal state) - 9 new integration tests for the two endpoints (136 total) - add eslint with typescript-eslint and eslint-config-prettier - add npm scripts: format, lint, lint:fix - resolve all lint errors across src and test (no any types) - add CONTRIBUTING.md covering dev workflow
This commit is contained in:
1 parent
b0c02a1cdc
commit
9a9ccbfe37
67 files changed
+3809
-1412
No files matched your search
+29
-20
@@ -1,33 +1,42 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { ScheduleModule } from '@nestjs/schedule';
|
||||
import { HealthModule } from './health/health.module';
|
||||
import { AuthModule } from './auth/auth.module';
|
||||
import { BrowserModule } from './browser/browser.module';
|
||||
import { SessionEntity } from './session/session.entity';
|
||||
import { EnvironmentEntity } from './environment/environment.entity';
|
||||
import { EnvironmentModule } from './environment/environment.module';
|
||||
import { McpModule } from './mcp/mcp.module';
|
||||
import { ScenarioEntity } from './scenario/scenario.entity';
|
||||
import { ScenarioStepEntity } from './scenario/scenario-step.entity';
|
||||
import { ScenarioRunEntity } from './scenario/scenario-run.entity';
|
||||
import { ScenarioRunStepEntity } from './scenario/scenario-run-step.entity';
|
||||
import { ScenarioModule } from './scenario/scenario.module';
|
||||
import { Module } from "@nestjs/common";
|
||||
import { ConfigModule, ConfigService } from "@nestjs/config";
|
||||
import { TypeOrmModule } from "@nestjs/typeorm";
|
||||
import { ScheduleModule } from "@nestjs/schedule";
|
||||
import { HealthModule } from "./health/health.module";
|
||||
import { AuthModule } from "./auth/auth.module";
|
||||
import { BrowserModule } from "./browser/browser.module";
|
||||
import { SessionEntity } from "./session/session.entity";
|
||||
import { EnvironmentEntity } from "./environment/environment.entity";
|
||||
import { EnvironmentModule } from "./environment/environment.module";
|
||||
import { McpModule } from "./mcp/mcp.module";
|
||||
import { ScenarioEntity } from "./scenario/scenario.entity";
|
||||
import { ScenarioStepEntity } from "./scenario/scenario-step.entity";
|
||||
import { ScenarioRunEntity } from "./scenario/scenario-run.entity";
|
||||
import { ScenarioRunStepEntity } from "./scenario/scenario-run-step.entity";
|
||||
import { ScenarioRunLogEntity } from "./scenario/scenario-run-log.entity";
|
||||
import { ScenarioModule } from "./scenario/scenario.module";
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
ConfigModule.forRoot({
|
||||
isGlobal: true,
|
||||
envFilePath: '.env',
|
||||
envFilePath: ".env",
|
||||
}),
|
||||
ScheduleModule.forRoot(),
|
||||
TypeOrmModule.forRootAsync({
|
||||
inject: [ConfigService],
|
||||
useFactory: (config: ConfigService) => ({
|
||||
type: 'better-sqlite3',
|
||||
database: config.get<string>('DB_PATH', 'data/sessions.db'),
|
||||
entities: [SessionEntity, EnvironmentEntity, ScenarioEntity, ScenarioStepEntity, ScenarioRunEntity, ScenarioRunStepEntity],
|
||||
type: "better-sqlite3",
|
||||
database: config.get<string>("DB_PATH", "data/sessions.db"),
|
||||
entities: [
|
||||
SessionEntity,
|
||||
EnvironmentEntity,
|
||||
ScenarioEntity,
|
||||
ScenarioStepEntity,
|
||||
ScenarioRunEntity,
|
||||
ScenarioRunStepEntity,
|
||||
ScenarioRunLogEntity,
|
||||
],
|
||||
synchronize: true,
|
||||
}),
|
||||
}),
|
||||
|
||||
+40
-15
@@ -1,26 +1,51 @@
|
||||
import { Body, Controller, Get, Post } from '@nestjs/common';
|
||||
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
|
||||
import { AuthService } from './auth.service';
|
||||
import { LoginDto } from './dto/login.dto';
|
||||
import { Body, Controller, Get, Post } from "@nestjs/common";
|
||||
import { ApiOperation, ApiResponse, ApiTags } from "@nestjs/swagger";
|
||||
import { AuthService } from "./auth.service";
|
||||
import { LoginDto } from "./dto/login.dto";
|
||||
|
||||
@ApiTags('auth')
|
||||
@ApiTags("auth")
|
||||
@Controller()
|
||||
export class AuthController {
|
||||
constructor(private readonly authService: AuthService) {}
|
||||
|
||||
@Get('keys')
|
||||
@ApiOperation({ summary: 'List available key identifiers from the keys directory' })
|
||||
@ApiResponse({ status: 200, description: 'List of key names', schema: { properties: { keys: { type: 'array', items: { type: 'string' } } } } })
|
||||
@Get("keys")
|
||||
@ApiOperation({
|
||||
summary: "List available key identifiers from the keys directory",
|
||||
})
|
||||
@ApiResponse({
|
||||
status: 200,
|
||||
description: "List of key names",
|
||||
schema: {
|
||||
properties: { keys: { type: "array", items: { type: "string" } } },
|
||||
},
|
||||
})
|
||||
listKeys(): { keys: string[] } {
|
||||
return { keys: this.authService.listKeys() };
|
||||
}
|
||||
|
||||
@Post('login')
|
||||
@ApiOperation({ summary: 'Log in using a file key and return the session token' })
|
||||
@ApiResponse({ status: 201, description: 'Login successful', schema: { properties: { token: { type: 'string' }, sessionName: { type: 'string' } } } })
|
||||
@ApiResponse({ status: 400, description: 'Key not found or invalid' })
|
||||
@ApiResponse({ status: 500, description: 'Automation failed' })
|
||||
login(@Body() dto: LoginDto): Promise<{ token: string; sessionName: string }> {
|
||||
return this.authService.login(dto.key, dto.environmentName, dto.sessionName);
|
||||
@Post("login")
|
||||
@ApiOperation({
|
||||
summary: "Log in using a file key and return the session token",
|
||||
})
|
||||
@ApiResponse({
|
||||
status: 201,
|
||||
description: "Login successful",
|
||||
schema: {
|
||||
properties: {
|
||||
token: { type: "string" },
|
||||
sessionName: { type: "string" },
|
||||
},
|
||||
},
|
||||
})
|
||||
@ApiResponse({ status: 400, description: "Key not found or invalid" })
|
||||
@ApiResponse({ status: 500, description: "Automation failed" })
|
||||
login(
|
||||
@Body() dto: LoginDto,
|
||||
): Promise<{ token: string; sessionName: string }> {
|
||||
return this.authService.login(
|
||||
dto.key,
|
||||
dto.environmentName,
|
||||
dto.sessionName,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { AuthController } from './auth.controller';
|
||||
import { AuthService } from './auth.service';
|
||||
import { SessionModule } from '../session/session.module';
|
||||
import { EnvironmentModule } from '../environment/environment.module';
|
||||
import { Module } from "@nestjs/common";
|
||||
import { AuthController } from "./auth.controller";
|
||||
import { AuthService } from "./auth.service";
|
||||
import { SessionModule } from "../session/session.module";
|
||||
import { EnvironmentModule } from "../environment/environment.module";
|
||||
|
||||
@Module({
|
||||
imports: [SessionModule, EnvironmentModule],
|
||||
|
||||
+93
-45
@@ -3,16 +3,16 @@ import {
|
||||
BadRequestException,
|
||||
InternalServerErrorException,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { TraceLogger } from '../common/trace-logger';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { chromium } from 'playwright';
|
||||
import type { Page } from 'playwright';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import * as crypto from 'crypto';
|
||||
import { SessionService } from '../session/session.service';
|
||||
import { EnvironmentService } from '../environment/environment.service';
|
||||
} from "@nestjs/common";
|
||||
import { TraceLogger } from "../common/trace-logger";
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
import { chromium } from "playwright";
|
||||
import type { Page } from "playwright";
|
||||
import * as fs from "fs";
|
||||
import * as path from "path";
|
||||
import * as crypto from "crypto";
|
||||
import { SessionService } from "../session/session.service";
|
||||
import { EnvironmentService } from "../environment/environment.service";
|
||||
|
||||
interface KeyDescriptor {
|
||||
keyFile?: string;
|
||||
@@ -30,16 +30,15 @@ export class AuthService {
|
||||
private readonly sessionService: SessionService,
|
||||
private readonly environmentService: EnvironmentService,
|
||||
) {
|
||||
this.keysDir = path.resolve(
|
||||
this.config.get<string>('KEYS_DIR', 'keys'),
|
||||
);
|
||||
this.keysDir = path.resolve(this.config.get<string>("KEYS_DIR", "keys"));
|
||||
}
|
||||
|
||||
listKeys(): string[] {
|
||||
if (!fs.existsSync(this.keysDir)) return [];
|
||||
return fs.readdirSync(this.keysDir)
|
||||
.filter(f => f.endsWith('.json'))
|
||||
.map(f => path.basename(f, '.json'));
|
||||
return fs
|
||||
.readdirSync(this.keysDir)
|
||||
.filter((f) => f.endsWith(".json"))
|
||||
.map((f) => path.basename(f, ".json"));
|
||||
}
|
||||
|
||||
private loadKeyDescriptor(keyId: string): KeyDescriptor {
|
||||
@@ -48,51 +47,73 @@ export class AuthService {
|
||||
throw new BadRequestException(`Key not found: ${keyId}`);
|
||||
}
|
||||
try {
|
||||
return JSON.parse(fs.readFileSync(keyJsonPath, 'utf-8')) as KeyDescriptor;
|
||||
return JSON.parse(fs.readFileSync(keyJsonPath, "utf-8")) as KeyDescriptor;
|
||||
} catch (err) {
|
||||
throw new BadRequestException(`Cannot read key descriptor: ${keyId}`, { cause: err });
|
||||
throw new BadRequestException(`Cannot read key descriptor: ${keyId}`, {
|
||||
cause: err,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async login(keyId: string, environmentName: string, sessionName?: string): Promise<{ token: string; sessionName: string }> {
|
||||
async login(
|
||||
keyId: string,
|
||||
environmentName: string,
|
||||
sessionName?: string,
|
||||
): Promise<{ token: string; sessionName: string }> {
|
||||
const resolvedSession = sessionName ?? crypto.randomUUID();
|
||||
|
||||
const env = await this.environmentService.findAll().then(({ data }) => data.find(e => e.name === environmentName));
|
||||
if (!env) throw new NotFoundException(`Environment "${environmentName}" not found`);
|
||||
const env = await this.environmentService
|
||||
.findAll()
|
||||
.then(({ data }) => data.find((e) => e.name === environmentName));
|
||||
if (!env)
|
||||
throw new NotFoundException(`Environment "${environmentName}" not found`);
|
||||
|
||||
const loginUrl = env.urls.id_url;
|
||||
const cabinetUrl = env.urls.cabinet_url;
|
||||
if (!loginUrl) throw new BadRequestException(`Environment "${environmentName}" is missing id_url`);
|
||||
if (!cabinetUrl) throw new BadRequestException(`Environment "${environmentName}" is missing cabinet_url`);
|
||||
if (!loginUrl)
|
||||
throw new BadRequestException(
|
||||
`Environment "${environmentName}" is missing id_url`,
|
||||
);
|
||||
if (!cabinetUrl)
|
||||
throw new BadRequestException(
|
||||
`Environment "${environmentName}" is missing cabinet_url`,
|
||||
);
|
||||
|
||||
const descriptor = this.loadKeyDescriptor(keyId);
|
||||
const useLoginPassword = !!descriptor.login;
|
||||
|
||||
if (!useLoginPassword) {
|
||||
if (!descriptor.keyFile) {
|
||||
throw new BadRequestException(`Key descriptor for "${keyId}" must have either "login" or "keyFile"`);
|
||||
throw new BadRequestException(
|
||||
`Key descriptor for "${keyId}" must have either "login" or "keyFile"`,
|
||||
);
|
||||
}
|
||||
const keyFilePath = path.resolve(this.keysDir, descriptor.keyFile);
|
||||
if (!fs.existsSync(keyFilePath)) {
|
||||
throw new BadRequestException(`Key file not found: ${descriptor.keyFile}`);
|
||||
throw new BadRequestException(
|
||||
`Key file not found: ${descriptor.keyFile}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const browser = await chromium.launch({ headless: true, executablePath: process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH });
|
||||
const browser = await chromium.launch({
|
||||
headless: true,
|
||||
executablePath: process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH,
|
||||
});
|
||||
try {
|
||||
const context = await browser.newContext();
|
||||
const page = await context.newPage();
|
||||
|
||||
this.logger.log(`Navigating to ${loginUrl}`);
|
||||
await page.goto(loginUrl, { waitUntil: 'networkidle' });
|
||||
await page.goto(loginUrl, { waitUntil: "networkidle" });
|
||||
|
||||
if (useLoginPassword) {
|
||||
// Click "Логін і пароль" auth method
|
||||
await page.locator('p[aria-label="Логін і пароль"]').click();
|
||||
|
||||
// Fill login and password
|
||||
await page.getByLabel('Електронна пошта').fill(descriptor.login!);
|
||||
await page.getByLabel('Пароль').fill(descriptor.password);
|
||||
await page.getByLabel("Електронна пошта").fill(descriptor.login!);
|
||||
await page.getByLabel("Пароль").fill(descriptor.password);
|
||||
|
||||
// Click "Увійти"
|
||||
await page.locator('button:has-text("Увійти")').click();
|
||||
@@ -100,19 +121,21 @@ export class AuthService {
|
||||
const keyFilePath = path.resolve(this.keysDir, descriptor.keyFile!);
|
||||
|
||||
// Click "Файловий ключ" button
|
||||
await page.getByText('Файловий ключ').click();
|
||||
await page.getByText("Файловий ключ").click();
|
||||
|
||||
// Upload key file via hidden file input
|
||||
const fileInput = page.locator('input[accept=".dat,.pfx,.pk8,.zs2,.jks"][type="file"]');
|
||||
const fileInput = page.locator(
|
||||
'input[accept=".dat,.pfx,.pk8,.zs2,.jks"][type="file"]',
|
||||
);
|
||||
await fileInput.setInputFiles(keyFilePath);
|
||||
|
||||
// Enter password
|
||||
await page
|
||||
.locator('#id-app-login-file-key-password')
|
||||
.locator("#id-app-login-file-key-password")
|
||||
.fill(descriptor.password);
|
||||
|
||||
// Click "Продовжити"
|
||||
await page.locator('#id-app-login-file-key-sign-button').click();
|
||||
await page.locator("#id-app-login-file-key-sign-button").click();
|
||||
}
|
||||
|
||||
// Wait until redirected to cabinet
|
||||
@@ -120,11 +143,11 @@ export class AuthService {
|
||||
await page.waitForURL(cabinetUrl, { timeout: 30000 });
|
||||
|
||||
// Extract token from localStorage
|
||||
const token = await page.evaluate(() => localStorage.getItem('token'));
|
||||
const token = await page.evaluate(() => localStorage.getItem("token"));
|
||||
|
||||
if (!token) {
|
||||
throw new InternalServerErrorException(
|
||||
'Login succeeded but token was not found in localStorage',
|
||||
"Login succeeded but token was not found in localStorage",
|
||||
);
|
||||
}
|
||||
|
||||
@@ -134,14 +157,21 @@ export class AuthService {
|
||||
const entries: Record<string, string> = {};
|
||||
for (let i = 0; i < window.localStorage.length; i++) {
|
||||
const k = window.localStorage.key(i);
|
||||
if (k !== null) entries[k] = window.localStorage.getItem(k) ?? '';
|
||||
if (k !== null) entries[k] = window.localStorage.getItem(k) ?? "";
|
||||
}
|
||||
return entries;
|
||||
});
|
||||
|
||||
await this.sessionService.upsert(resolvedSession, token, cookies, localStorageData);
|
||||
await this.sessionService.upsert(
|
||||
resolvedSession,
|
||||
token,
|
||||
cookies,
|
||||
localStorageData,
|
||||
);
|
||||
|
||||
this.logger.log(`Login successful for key ${keyId}, session: ${resolvedSession}`);
|
||||
this.logger.log(
|
||||
`Login successful for key ${keyId}, session: ${resolvedSession}`,
|
||||
);
|
||||
return { token, sessionName: resolvedSession };
|
||||
} catch (err) {
|
||||
if (
|
||||
@@ -164,26 +194,40 @@ export class AuthService {
|
||||
const descriptor = this.loadKeyDescriptor(keyId);
|
||||
|
||||
if (!descriptor.keyFile) {
|
||||
throw new BadRequestException(`Key descriptor for "${keyId}" must have "keyFile" to sign`);
|
||||
throw new BadRequestException(
|
||||
`Key descriptor for "${keyId}" must have "keyFile" to sign`,
|
||||
);
|
||||
}
|
||||
|
||||
const keyFilePath = path.resolve(this.keysDir, descriptor.keyFile);
|
||||
if (!fs.existsSync(keyFilePath)) {
|
||||
throw new BadRequestException(`Key file not found: ${descriptor.keyFile}`);
|
||||
throw new BadRequestException(
|
||||
`Key file not found: ${descriptor.keyFile}`,
|
||||
);
|
||||
}
|
||||
|
||||
this.logger.log(`Signing with key ${keyId} on page: ${page.url()}`);
|
||||
|
||||
// Open the EDS sign widget
|
||||
await page.locator('button').filter({ hasText: /підпис|sign/i }).first().click();
|
||||
await page
|
||||
.locator("button")
|
||||
.filter({ hasText: /підпис|sign/i })
|
||||
.first()
|
||||
.click();
|
||||
await page.waitForTimeout(500);
|
||||
|
||||
// Select the file key tab inside the widget
|
||||
await page.locator('button, [role="tab"], li').filter({ hasText: /файлов|file key/i }).first().click();
|
||||
await page
|
||||
.locator('button, [role="tab"], li')
|
||||
.filter({ hasText: /файлов|file key/i })
|
||||
.first()
|
||||
.click();
|
||||
await page.waitForTimeout(300);
|
||||
|
||||
// Upload the key file
|
||||
const fileInput = page.locator('input[accept=".dat,.pfx,.pk8,.zs2,.jks"][type="file"]');
|
||||
const fileInput = page.locator(
|
||||
'input[accept=".dat,.pfx,.pk8,.zs2,.jks"][type="file"]',
|
||||
);
|
||||
await fileInput.setInputFiles(keyFilePath);
|
||||
await page.waitForTimeout(300);
|
||||
|
||||
@@ -192,8 +236,12 @@ export class AuthService {
|
||||
await page.waitForTimeout(200);
|
||||
|
||||
// Submit
|
||||
await page.locator('button').filter({ hasText: /підпис|sign|підтвер/i }).last().click();
|
||||
await page.waitForLoadState('networkidle');
|
||||
await page
|
||||
.locator("button")
|
||||
.filter({ hasText: /підпис|sign|підтвер/i })
|
||||
.last()
|
||||
.click();
|
||||
await page.waitForLoadState("networkidle");
|
||||
|
||||
this.logger.log(`Sign completed for key ${keyId}`);
|
||||
}
|
||||
|
||||
@@ -1,26 +1,28 @@
|
||||
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { IsNotEmpty, IsOptional, IsString } from 'class-validator';
|
||||
import { ApiProperty, ApiPropertyOptional } from "@nestjs/swagger";
|
||||
import { IsNotEmpty, IsOptional, IsString } from "class-validator";
|
||||
|
||||
export class LoginDto {
|
||||
@ApiProperty({
|
||||
description: 'Key identifier — filename (without extension) from the keys/ directory',
|
||||
example: '3273334361',
|
||||
description:
|
||||
"Key identifier — filename (without extension) from the keys/ directory",
|
||||
example: "3273334361",
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
key: string;
|
||||
|
||||
@ApiProperty({
|
||||
description: 'Environment name to resolve login/cabinet URLs from',
|
||||
example: 'liquio-diia-stg',
|
||||
description: "Environment name to resolve login/cabinet URLs from",
|
||||
example: "liquio-diia-stg",
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
environmentName: string;
|
||||
|
||||
@ApiPropertyOptional({
|
||||
description: 'Session name to store credentials under. Auto-generated UUID if omitted.',
|
||||
example: 'my-test-session',
|
||||
description:
|
||||
"Session name to store credentials under. Auto-generated UUID if omitted.",
|
||||
example: "my-test-session",
|
||||
})
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
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';
|
||||
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')
|
||||
@ApiTags("browser")
|
||||
@Controller()
|
||||
export class BrowserController {
|
||||
constructor(
|
||||
@@ -13,39 +13,47 @@ export class BrowserController {
|
||||
private readonly codeExecutor: CodeExecutorService,
|
||||
) {}
|
||||
|
||||
@Post('open')
|
||||
@ApiOperation({ summary: 'Open a URL with a stored session (cookies + localStorage)' })
|
||||
@Post("open")
|
||||
@ApiOperation({
|
||||
summary: "Open a URL with a stored session (cookies + localStorage)",
|
||||
})
|
||||
@ApiResponse({
|
||||
status: 201,
|
||||
description: 'Page loaded successfully',
|
||||
description: "Page loaded successfully",
|
||||
schema: {
|
||||
properties: {
|
||||
url: { type: 'string' },
|
||||
title: { type: 'string' },
|
||||
content: { type: 'string' },
|
||||
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' })
|
||||
@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<OpenResult> {
|
||||
return this.browserService.open(dto.sessionName, dto.url, dto.readerMode ?? false, dto.selector);
|
||||
return this.browserService.open(
|
||||
dto.sessionName,
|
||||
dto.url,
|
||||
dto.readerMode ?? false,
|
||||
dto.selector,
|
||||
);
|
||||
}
|
||||
|
||||
@Post('exec')
|
||||
@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`.',
|
||||
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',
|
||||
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' })
|
||||
@ApiResponse({ status: 400, description: "Invalid input" })
|
||||
@ApiResponse({ status: 404, description: "Session not found" })
|
||||
@ApiResponse({ status: 500, description: "Execution failed" })
|
||||
exec(@Body() dto: ExecDto): Promise<ExecResult> {
|
||||
this.codeExecutor.validate(dto.code);
|
||||
return this.browserService.exec(dto.sessionName, dto.code, dto.url);
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { BrowserController } from './browser.controller';
|
||||
import { BrowserService } from './browser.service';
|
||||
import { SessionModule } from '../session/session.module';
|
||||
import { CodeExecutorModule } from '../code-executor/code-executor.module';
|
||||
import { Module } from "@nestjs/common";
|
||||
import { BrowserController } from "./browser.controller";
|
||||
import { BrowserService } from "./browser.service";
|
||||
import { SessionModule } from "../session/session.module";
|
||||
import { CodeExecutorModule } from "../code-executor/code-executor.module";
|
||||
|
||||
@Module({
|
||||
imports: [SessionModule, CodeExecutorModule],
|
||||
|
||||
@@ -3,17 +3,17 @@ import {
|
||||
HttpException,
|
||||
InternalServerErrorException,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { TraceLogger } from '../common/trace-logger';
|
||||
import { chromium } from 'playwright';
|
||||
import type { BrowserContext } from 'playwright';
|
||||
import { Readability } from '@mozilla/readability';
|
||||
import { JSDOM } from 'jsdom';
|
||||
import { SessionService } from '../session/session.service';
|
||||
import { CodeExecutorService } from '../code-executor/code-executor.service';
|
||||
import type { ExecResult } from '../code-executor/code-executor.service';
|
||||
} from "@nestjs/common";
|
||||
import { TraceLogger } from "../common/trace-logger";
|
||||
import { chromium } from "playwright";
|
||||
import type { BrowserContext, Cookie } from "playwright";
|
||||
import { Readability } from "@mozilla/readability";
|
||||
import { JSDOM } from "jsdom";
|
||||
import { SessionService } from "../session/session.service";
|
||||
import { CodeExecutorService } from "../code-executor/code-executor.service";
|
||||
import type { ExecResult } from "../code-executor/code-executor.service";
|
||||
|
||||
export type { ExecResult } from '../code-executor/code-executor.service';
|
||||
export type { ExecResult } from "../code-executor/code-executor.service";
|
||||
|
||||
export interface OpenResult {
|
||||
url: string;
|
||||
@@ -30,19 +30,25 @@ export class BrowserService {
|
||||
private readonly codeExecutor: CodeExecutorService,
|
||||
) {}
|
||||
|
||||
private async setupSession(context: BrowserContext, sessionName: string | undefined): Promise<void> {
|
||||
private async setupSession(
|
||||
context: BrowserContext,
|
||||
sessionName: string | undefined,
|
||||
): Promise<void> {
|
||||
if (!sessionName) return;
|
||||
const session = await this.sessionService.findBySessionName(sessionName);
|
||||
if (!session) {
|
||||
throw new NotFoundException(`Session not found: ${sessionName}`);
|
||||
}
|
||||
let cookies: any[];
|
||||
let cookies: Cookie[];
|
||||
let localStorageData: Record<string, string>;
|
||||
try {
|
||||
cookies = JSON.parse(session.cookies);
|
||||
localStorageData = JSON.parse(session.localStorage);
|
||||
} catch (err) {
|
||||
throw new InternalServerErrorException('Failed to deserialize session data', { cause: err });
|
||||
throw new InternalServerErrorException(
|
||||
"Failed to deserialize session data",
|
||||
{ cause: err },
|
||||
);
|
||||
}
|
||||
await context.addCookies(cookies);
|
||||
await context.addInitScript((entries: Record<string, string>) => {
|
||||
@@ -62,16 +68,24 @@ export class BrowserService {
|
||||
);
|
||||
}
|
||||
|
||||
async open(sessionName: string | undefined, url: string, readerMode = false, selector?: string): Promise<OpenResult> {
|
||||
const label = sessionName ?? 'anonymous';
|
||||
const browser = await chromium.launch({ headless: true, executablePath: process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH });
|
||||
async open(
|
||||
sessionName: string | undefined,
|
||||
url: string,
|
||||
readerMode = false,
|
||||
selector?: string,
|
||||
): Promise<OpenResult> {
|
||||
const label = sessionName ?? "anonymous";
|
||||
const browser = await chromium.launch({
|
||||
headless: true,
|
||||
executablePath: process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH,
|
||||
});
|
||||
try {
|
||||
const context = await browser.newContext();
|
||||
await this.setupSession(context, sessionName);
|
||||
const page = await context.newPage();
|
||||
|
||||
this.logger.log(`[${label}] Opening ${url}`);
|
||||
await page.goto(url, { waitUntil: 'networkidle' });
|
||||
await page.goto(url, { waitUntil: "networkidle" });
|
||||
|
||||
const finalUrl = page.url();
|
||||
const title = await page.title();
|
||||
@@ -82,28 +96,39 @@ export class BrowserService {
|
||||
const dom = new JSDOM(rawHtml, { url: finalUrl });
|
||||
const el = dom.window.document.querySelector(selector);
|
||||
content = readerMode
|
||||
? (el?.textContent?.replace(/\s+/g, ' ').trim() ?? '')
|
||||
: (el?.outerHTML ?? '');
|
||||
? (el?.textContent?.replace(/\s+/g, " ").trim() ?? "")
|
||||
: (el?.outerHTML ?? "");
|
||||
} else if (readerMode) {
|
||||
const dom = new JSDOM(rawHtml, { url: finalUrl });
|
||||
const article = new Readability(dom.window.document).parse();
|
||||
content = article ? article.textContent.replace(/\s+/g, ' ').trim() : rawHtml;
|
||||
content = article
|
||||
? article.textContent.replace(/\s+/g, " ").trim()
|
||||
: rawHtml;
|
||||
} else {
|
||||
content = rawHtml;
|
||||
}
|
||||
|
||||
this.logger.log(`[${label}] Loaded: ${finalUrl} — "${title}"${readerMode ? ' (reader mode)' : ''}${selector ? ` (selector: ${selector})` : ''}`);
|
||||
this.logger.log(
|
||||
`[${label}] Loaded: ${finalUrl} — "${title}"${readerMode ? " (reader mode)" : ""}${selector ? ` (selector: ${selector})` : ""}`,
|
||||
);
|
||||
return { url: finalUrl, title, content };
|
||||
} catch (err) {
|
||||
this.rethrow(err, label, 'open');
|
||||
this.rethrow(err, label, "open");
|
||||
} finally {
|
||||
await browser.close();
|
||||
}
|
||||
}
|
||||
|
||||
async exec(sessionName: string | undefined, code: string, url?: string): Promise<ExecResult> {
|
||||
const label = sessionName ?? 'anonymous';
|
||||
const browser = await chromium.launch({ headless: true, executablePath: process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH });
|
||||
async exec(
|
||||
sessionName: string | undefined,
|
||||
code: string,
|
||||
url?: string,
|
||||
): Promise<ExecResult> {
|
||||
const label = sessionName ?? "anonymous";
|
||||
const browser = await chromium.launch({
|
||||
headless: true,
|
||||
executablePath: process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH,
|
||||
});
|
||||
try {
|
||||
const context = await browser.newContext();
|
||||
await this.setupSession(context, sessionName);
|
||||
@@ -111,7 +136,7 @@ export class BrowserService {
|
||||
|
||||
if (url) {
|
||||
this.logger.log(`[${label}] exec: navigating to ${url}`);
|
||||
await page.goto(url, { waitUntil: 'networkidle' });
|
||||
await page.goto(url, { waitUntil: "networkidle" });
|
||||
}
|
||||
|
||||
this.logger.log(`[${label}] exec: running user code`);
|
||||
@@ -120,7 +145,7 @@ export class BrowserService {
|
||||
this.logger.log(`[${label}] exec: done`);
|
||||
return result;
|
||||
} catch (err) {
|
||||
this.rethrow(err, label, 'exec');
|
||||
this.rethrow(err, label, "exec");
|
||||
} finally {
|
||||
await browser.close();
|
||||
}
|
||||
|
||||
@@ -1,26 +1,29 @@
|
||||
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { IsOptional, IsString, IsUrl } from 'class-validator';
|
||||
import { ApiProperty, ApiPropertyOptional } from "@nestjs/swagger";
|
||||
import { IsOptional, IsString, IsUrl } from "class-validator";
|
||||
|
||||
export class ExecDto {
|
||||
@ApiPropertyOptional({
|
||||
description: 'Session name previously created by POST /login. If omitted, executes without a stored session.',
|
||||
example: 'test-session-1',
|
||||
description:
|
||||
"Session name previously created by POST /login. If omitted, executes without a stored session.",
|
||||
example: "test-session-1",
|
||||
})
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
sessionName?: string;
|
||||
|
||||
@ApiPropertyOptional({
|
||||
description: 'URL to navigate to before executing code. Skipped if omitted.',
|
||||
example: 'https://cabinet-liquio-diia-stg.kitsoft.ua/messages',
|
||||
description:
|
||||
"URL to navigate to before executing code. Skipped if omitted.",
|
||||
example: "https://cabinet-liquio-diia-stg.kitsoft.ua/messages",
|
||||
})
|
||||
@IsOptional()
|
||||
@IsUrl({ require_tld: true, require_protocol: true })
|
||||
url?: string;
|
||||
|
||||
@ApiProperty({
|
||||
description: 'JavaScript code to execute. Receives `page` (Playwright Page) and `context` (BrowserContext) as arguments. May be async. Return value is serialised and returned.',
|
||||
example: 'return await page.title();',
|
||||
description:
|
||||
"JavaScript code to execute. Receives `page` (Playwright Page) and `context` (BrowserContext) as arguments. May be async. Return value is serialised and returned.",
|
||||
example: "return await page.title();",
|
||||
})
|
||||
@IsString()
|
||||
code: string;
|
||||
|
||||
@@ -1,24 +1,26 @@
|
||||
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { IsBoolean, IsOptional, IsString, IsUrl } from 'class-validator';
|
||||
import { ApiProperty, ApiPropertyOptional } from "@nestjs/swagger";
|
||||
import { IsBoolean, IsOptional, IsString, IsUrl } from "class-validator";
|
||||
|
||||
export class OpenDto {
|
||||
@ApiPropertyOptional({
|
||||
description: 'Session name previously created by POST /login. If omitted, opens the URL without a stored session.',
|
||||
example: 'test-session-1',
|
||||
description:
|
||||
"Session name previously created by POST /login. If omitted, opens the URL without a stored session.",
|
||||
example: "test-session-1",
|
||||
})
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
sessionName?: string;
|
||||
|
||||
@ApiProperty({
|
||||
description: 'URL to open with the authenticated session',
|
||||
example: 'https://cabinet-liquio-diia-stg.kitsoft.ua/messages',
|
||||
description: "URL to open with the authenticated session",
|
||||
example: "https://cabinet-liquio-diia-stg.kitsoft.ua/messages",
|
||||
})
|
||||
@IsUrl({ require_tld: true, require_protocol: true })
|
||||
url: string;
|
||||
|
||||
@ApiPropertyOptional({
|
||||
description: 'When true, return a plain-text reader-mode summary instead of raw HTML',
|
||||
description:
|
||||
"When true, return a plain-text reader-mode summary instead of raw HTML",
|
||||
default: false,
|
||||
})
|
||||
@IsOptional()
|
||||
@@ -26,8 +28,9 @@ export class OpenDto {
|
||||
readerMode?: boolean;
|
||||
|
||||
@ApiPropertyOptional({
|
||||
description: 'CSS selector whose matching element content is returned. When omitted the full page HTML is used.',
|
||||
example: '#main-content',
|
||||
description:
|
||||
"CSS selector whose matching element content is returned. When omitted the full page HTML is used.",
|
||||
example: "#main-content",
|
||||
})
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { CodeExecutorService } from './code-executor.service';
|
||||
import { Module } from "@nestjs/common";
|
||||
import { CodeExecutorService } from "./code-executor.service";
|
||||
|
||||
@Module({
|
||||
providers: [CodeExecutorService],
|
||||
|
||||
@@ -1,14 +1,21 @@
|
||||
import { BadRequestException, Injectable, InternalServerErrorException } from '@nestjs/common';
|
||||
import { TraceLogger } from '../common/trace-logger';
|
||||
import { parse } from 'acorn';
|
||||
import type { Page, BrowserContext } from 'playwright';
|
||||
import { dumpDom } from './dom-helpers';
|
||||
import {
|
||||
BadRequestException,
|
||||
Injectable,
|
||||
InternalServerErrorException,
|
||||
} from "@nestjs/common";
|
||||
import { TraceLogger } from "../common/trace-logger";
|
||||
import { parse } from "acorn";
|
||||
import type { Page, BrowserContext } from "playwright";
|
||||
import { dumpDom } from "./dom-helpers";
|
||||
|
||||
export interface ExecResult {
|
||||
result: unknown;
|
||||
}
|
||||
|
||||
export type ScriptLogger = (level: 'log' | 'warn' | 'error', message: string) => void;
|
||||
export type ScriptLogger = (
|
||||
level: "log" | "warn" | "error",
|
||||
message: string,
|
||||
) => void;
|
||||
|
||||
@Injectable()
|
||||
export class CodeExecutorService {
|
||||
@@ -23,7 +30,10 @@ export class CodeExecutorService {
|
||||
try {
|
||||
parse(wrapped, { ecmaVersion: 2022 });
|
||||
} catch (err) {
|
||||
throw new BadRequestException(`Code parse error: ${(err as Error).message}`, { cause: err });
|
||||
throw new BadRequestException(
|
||||
`Code parse error: ${(err as Error).message}`,
|
||||
{ cause: err },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,34 +41,51 @@ export class CodeExecutorService {
|
||||
* Executes `code` as an async function body with `page` and `context` in
|
||||
* scope. Always call `validate()` before this method.
|
||||
*/
|
||||
async execute(page: Page, context: BrowserContext, code: string, log?: ScriptLogger): Promise<ExecResult> {
|
||||
const scriptLog: ScriptLogger = log ?? ((level, msg) => this.logger[level](msg));
|
||||
const toStr = (args: unknown[]) => args.map((a) => (typeof a === 'object' ? JSON.stringify(a) : String(a))).join(' ');
|
||||
async execute(
|
||||
page: Page,
|
||||
context: BrowserContext,
|
||||
code: string,
|
||||
log?: ScriptLogger,
|
||||
): Promise<ExecResult> {
|
||||
const scriptLog: ScriptLogger =
|
||||
log ?? ((level, msg) => this.logger[level](msg));
|
||||
const toStr = (args: unknown[]) =>
|
||||
args
|
||||
.map((a) => (typeof a === "object" ? JSON.stringify(a) : String(a)))
|
||||
.join(" ");
|
||||
|
||||
try {
|
||||
const pageHelpers = {
|
||||
dumpDom: (selector?: string) => dumpDom(page, selector),
|
||||
log: (...args: unknown[]) => scriptLog('log', toStr(args)),
|
||||
warn: (...args: unknown[]) => scriptLog('warn', toStr(args)),
|
||||
error: (...args: unknown[]) => scriptLog('error', toStr(args)),
|
||||
log: (...args: unknown[]) => scriptLog("log", toStr(args)),
|
||||
warn: (...args: unknown[]) => scriptLog("warn", toStr(args)),
|
||||
error: (...args: unknown[]) => scriptLog("error", toStr(args)),
|
||||
};
|
||||
|
||||
const fakeConsole = {
|
||||
log: (...args: unknown[]) => scriptLog('log', toStr(args)),
|
||||
warn: (...args: unknown[]) => scriptLog('warn', toStr(args)),
|
||||
error: (...args: unknown[]) => scriptLog('error', toStr(args)),
|
||||
info: (...args: unknown[]) => scriptLog('log', toStr(args)),
|
||||
debug: (...args: unknown[]) => scriptLog('log', toStr(args)),
|
||||
log: (...args: unknown[]) => scriptLog("log", toStr(args)),
|
||||
warn: (...args: unknown[]) => scriptLog("warn", toStr(args)),
|
||||
error: (...args: unknown[]) => scriptLog("error", toStr(args)),
|
||||
info: (...args: unknown[]) => scriptLog("log", toStr(args)),
|
||||
debug: (...args: unknown[]) => scriptLog("log", toStr(args)),
|
||||
};
|
||||
|
||||
// Passing `console` as a named parameter shadows the global in the script scope.
|
||||
// eslint-disable-next-line no-new-func
|
||||
const fn = new Function('page', 'context', 'helpers', 'console', `return (async (page, context, helpers) => { ${code} })(page, context, helpers)`);
|
||||
this.logger.debug('Executing user code');
|
||||
const fn = new Function(
|
||||
"page",
|
||||
"context",
|
||||
"helpers",
|
||||
"console",
|
||||
`return (async (page, context, helpers) => { ${code} })(page, context, helpers)`,
|
||||
);
|
||||
this.logger.debug("Executing user code");
|
||||
const result = await fn(page, context, pageHelpers, fakeConsole);
|
||||
return { result };
|
||||
} catch (err) {
|
||||
throw new InternalServerErrorException(`Code execution failed: ${(err as Error).message}`, { cause: err });
|
||||
throw new InternalServerErrorException(
|
||||
`Code execution failed: ${(err as Error).message}`,
|
||||
{ cause: err },
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { Page } from 'playwright';
|
||||
import type { Page } from "playwright";
|
||||
|
||||
export interface DomNode {
|
||||
tag: string;
|
||||
@@ -26,50 +26,99 @@ export interface DomNode {
|
||||
*
|
||||
* Useful for debugging Playwright selectors without screenshotting.
|
||||
*/
|
||||
export async function dumpDom(page: Page, rootSelector = 'body'): Promise<DomNode> {
|
||||
export async function dumpDom(
|
||||
page: Page,
|
||||
rootSelector = "body",
|
||||
): Promise<DomNode> {
|
||||
return page.evaluate(
|
||||
([sel, maxDepth]) => {
|
||||
const root = document.querySelector(sel as string);
|
||||
if (!root) return { tag: 'ERROR', text: `selector not found: ${sel}`, children: [] };
|
||||
if (!root)
|
||||
return {
|
||||
tag: "ERROR",
|
||||
text: `selector not found: ${sel}`,
|
||||
children: [],
|
||||
};
|
||||
|
||||
const STRUCTURAL_TAGS = new Set([
|
||||
'BODY', 'MAIN', 'HEADER', 'FOOTER', 'NAV', 'ASIDE', 'SECTION',
|
||||
'FORM', 'DIALOG', 'DETAILS', 'SUMMARY', 'TABLE', 'THEAD', 'TBODY',
|
||||
'TR', 'FIELDSET', 'LEGEND',
|
||||
"BODY",
|
||||
"MAIN",
|
||||
"HEADER",
|
||||
"FOOTER",
|
||||
"NAV",
|
||||
"ASIDE",
|
||||
"SECTION",
|
||||
"FORM",
|
||||
"DIALOG",
|
||||
"DETAILS",
|
||||
"SUMMARY",
|
||||
"TABLE",
|
||||
"THEAD",
|
||||
"TBODY",
|
||||
"TR",
|
||||
"FIELDSET",
|
||||
"LEGEND",
|
||||
]);
|
||||
|
||||
const INTERACTIVE_TAGS = new Set([
|
||||
'A', 'BUTTON', 'INPUT', 'SELECT', 'TEXTAREA', 'LABEL',
|
||||
'TH', 'TD',
|
||||
"A",
|
||||
"BUTTON",
|
||||
"INPUT",
|
||||
"SELECT",
|
||||
"TEXTAREA",
|
||||
"LABEL",
|
||||
"TH",
|
||||
"TD",
|
||||
]);
|
||||
|
||||
const IGNORED_TAGS = new Set(['SCRIPT', 'STYLE', 'SVG', 'PATH', 'DEFS', 'USE', 'CIRCLE', 'RECT', 'POLYGON', 'POLYLINE', 'LINE', 'ELLIPSE', 'G', 'CLIPPATH', 'IMAGE']);
|
||||
const IGNORED_TAGS = new Set([
|
||||
"SCRIPT",
|
||||
"STYLE",
|
||||
"SVG",
|
||||
"PATH",
|
||||
"DEFS",
|
||||
"USE",
|
||||
"CIRCLE",
|
||||
"RECT",
|
||||
"POLYGON",
|
||||
"POLYLINE",
|
||||
"LINE",
|
||||
"ELLIPSE",
|
||||
"G",
|
||||
"CLIPPATH",
|
||||
"IMAGE",
|
||||
]);
|
||||
|
||||
function trimText(el: Element): string | undefined {
|
||||
const t = (el as HTMLElement).innerText?.trim() ?? el.textContent?.trim() ?? '';
|
||||
const t =
|
||||
(el as HTMLElement).innerText?.trim() ?? el.textContent?.trim() ?? "";
|
||||
// Only include if short enough to be meaningful, not a dump of all child text
|
||||
const ownText = Array.from(el.childNodes)
|
||||
.filter(n => n.nodeType === Node.TEXT_NODE)
|
||||
.map(n => n.textContent?.trim() ?? '')
|
||||
.filter((n) => n.nodeType === Node.TEXT_NODE)
|
||||
.map((n) => n.textContent?.trim() ?? "")
|
||||
.filter(Boolean)
|
||||
.join(' ');
|
||||
.join(" ");
|
||||
const candidate = ownText || t;
|
||||
return candidate.length > 0 ? candidate.substring(0, 80) : undefined;
|
||||
}
|
||||
|
||||
function isVisible(el: Element): boolean {
|
||||
const s = window.getComputedStyle(el);
|
||||
return s.display !== 'none' && s.visibility !== 'hidden' && (el as HTMLElement).offsetParent !== null;
|
||||
return (
|
||||
s.display !== "none" &&
|
||||
s.visibility !== "hidden" &&
|
||||
(el as HTMLElement).offsetParent !== null
|
||||
);
|
||||
}
|
||||
|
||||
function isSignificant(el: Element): boolean {
|
||||
if (STRUCTURAL_TAGS.has(el.tagName)) return true;
|
||||
if (INTERACTIVE_TAGS.has(el.tagName)) return true;
|
||||
if (el.getAttribute('role')) return true;
|
||||
if (el.getAttribute('data-testid')) return true;
|
||||
if (el.getAttribute('data-qa')) return true;
|
||||
if (el.getAttribute('data-action')) return true;
|
||||
if (el.getAttribute('data-element-id')) return true;
|
||||
if (el.getAttribute("role")) return true;
|
||||
if (el.getAttribute("data-testid")) return true;
|
||||
if (el.getAttribute("data-qa")) return true;
|
||||
if (el.getAttribute("data-action")) return true;
|
||||
if (el.getAttribute("data-element-id")) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -98,41 +147,44 @@ export async function dumpDom(page: Page, rootSelector = 'body'): Promise<DomNod
|
||||
if (!significant && childResults.length === 1) return childResults[0];
|
||||
|
||||
// If not significant but has children, keep as anonymous group only if > 1 child
|
||||
if (!significant) return { tag: el.tagName.toLowerCase(), children: childResults };
|
||||
if (!significant)
|
||||
return { tag: el.tagName.toLowerCase(), children: childResults };
|
||||
|
||||
const node: DomNode = {
|
||||
tag: el.tagName.toLowerCase(),
|
||||
children: childResults,
|
||||
};
|
||||
|
||||
const role = el.getAttribute('role');
|
||||
const role = el.getAttribute("role");
|
||||
if (role) node.role = role;
|
||||
|
||||
const testid = el.getAttribute('data-testid');
|
||||
const testid = el.getAttribute("data-testid");
|
||||
if (testid) node.testid = testid;
|
||||
|
||||
const qa = el.getAttribute('data-qa');
|
||||
const qa = el.getAttribute("data-qa");
|
||||
if (qa) node.qa = qa;
|
||||
|
||||
const action = el.getAttribute('data-action');
|
||||
const action = el.getAttribute("data-action");
|
||||
if (action) node.action = action;
|
||||
|
||||
const elementId = el.getAttribute('data-element-id');
|
||||
const elementId = el.getAttribute("data-element-id");
|
||||
if (elementId) node.elementId = elementId;
|
||||
|
||||
const id = el.id;
|
||||
if (id) node.id = id;
|
||||
|
||||
const type = (el as HTMLInputElement).type;
|
||||
if (type && type !== 'submit' && el.tagName !== 'BUTTON') node.type = type;
|
||||
if (type && type !== "submit" && el.tagName !== "BUTTON")
|
||||
node.type = type;
|
||||
|
||||
const name = (el as HTMLInputElement).name;
|
||||
if (name) node.name = name;
|
||||
|
||||
const href = (el as HTMLAnchorElement).href;
|
||||
if (href && el.tagName === 'A') node.href = href.replace(window.location.origin, '');
|
||||
if (href && el.tagName === "A")
|
||||
node.href = href.replace(window.location.origin, "");
|
||||
|
||||
if ('checked' in el) node.checked = (el as HTMLInputElement).checked;
|
||||
if ("checked" in el) node.checked = (el as HTMLInputElement).checked;
|
||||
if ((el as HTMLButtonElement).disabled) node.disabled = true;
|
||||
|
||||
const text = trimText(el);
|
||||
@@ -142,7 +194,7 @@ export async function dumpDom(page: Page, rootSelector = 'body'): Promise<DomNod
|
||||
}
|
||||
|
||||
const result = build(root, 0);
|
||||
return result ?? { tag: 'empty', children: [] };
|
||||
return result ?? { tag: "empty", children: [] };
|
||||
},
|
||||
[rootSelector, 12] as [string, number],
|
||||
);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { Type } from 'class-transformer';
|
||||
import { IsIn, IsInt, IsOptional, IsString, Min } from 'class-validator';
|
||||
import { ApiPropertyOptional } from "@nestjs/swagger";
|
||||
import { Type } from "class-transformer";
|
||||
import { IsIn, IsInt, IsOptional, IsString, Min } from "class-validator";
|
||||
|
||||
export class PaginationQueryDto<TOrderBy extends string = string> {
|
||||
@ApiPropertyOptional({ example: 1, default: 1 })
|
||||
@@ -17,15 +17,19 @@ export class PaginationQueryDto<TOrderBy extends string = string> {
|
||||
@Min(1)
|
||||
limit?: number = 20;
|
||||
|
||||
@ApiPropertyOptional({ example: 'id', default: 'id', description: 'Field to order by' })
|
||||
@ApiPropertyOptional({
|
||||
example: "id",
|
||||
default: "id",
|
||||
description: "Field to order by",
|
||||
})
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
orderBy?: TOrderBy;
|
||||
|
||||
@ApiPropertyOptional({ enum: ['ASC', 'DESC'], default: 'ASC' })
|
||||
@ApiPropertyOptional({ enum: ["ASC", "DESC"], default: "ASC" })
|
||||
@IsOptional()
|
||||
@IsIn(['ASC', 'DESC'])
|
||||
orderDir?: 'ASC' | 'DESC' = 'ASC';
|
||||
@IsIn(["ASC", "DESC"])
|
||||
orderDir?: "ASC" | "DESC" = "ASC";
|
||||
}
|
||||
|
||||
export interface PaginatedResult<T> {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { AsyncLocalStorage } from 'async_hooks';
|
||||
import { AsyncLocalStorage } from "async_hooks";
|
||||
|
||||
export interface TraceStore {
|
||||
traceId: string;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { ConsoleLogger, ConsoleLoggerOptions, LogLevel } from '@nestjs/common';
|
||||
import { getTraceId } from './trace-context';
|
||||
import { ConsoleLogger, ConsoleLoggerOptions, LogLevel } from "@nestjs/common";
|
||||
import { getTraceId } from "./trace-context";
|
||||
|
||||
export class TraceLogger extends ConsoleLogger {
|
||||
constructor(context?: string, options: ConsoleLoggerOptions = {}) {
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { IsInt, IsString, Min, Max } from 'class-validator';
|
||||
import { IsInt, IsString, Min, Max } from "class-validator";
|
||||
|
||||
import pkg from '../../package.json';
|
||||
import pkg from "../../package.json";
|
||||
|
||||
export class AppConfig {
|
||||
@IsString()
|
||||
NODE_ENV: string = 'development';
|
||||
NODE_ENV: string = "development";
|
||||
|
||||
@IsInt()
|
||||
@Min(1)
|
||||
|
||||
@@ -1,19 +1,19 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsNotEmpty, IsObject, IsString } from 'class-validator';
|
||||
import { EnvironmentUrls } from '../environment.entity';
|
||||
import { ApiProperty } from "@nestjs/swagger";
|
||||
import { IsNotEmpty, IsObject, IsString } from "class-validator";
|
||||
import { EnvironmentUrls } from "../environment.entity";
|
||||
|
||||
export class CreateEnvironmentDto {
|
||||
@ApiProperty({ example: 'liquio-diia-stg' })
|
||||
@ApiProperty({ example: "liquio-diia-stg" })
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
name: string;
|
||||
|
||||
@ApiProperty({
|
||||
description: 'Map of URL identifiers to URL strings',
|
||||
description: "Map of URL identifiers to URL strings",
|
||||
example: {
|
||||
id_url: 'https://id-liquio-diia-stg.kitsoft.ua/',
|
||||
cabinet_url: 'https://cabinet-liquio-diia-stg.kitsoft.ua/',
|
||||
admin_url: 'https://admin-liquio-diia-stg.kitsoft.ua/',
|
||||
id_url: "https://id-liquio-diia-stg.kitsoft.ua/",
|
||||
cabinet_url: "https://cabinet-liquio-diia-stg.kitsoft.ua/",
|
||||
admin_url: "https://admin-liquio-diia-stg.kitsoft.ua/",
|
||||
},
|
||||
})
|
||||
@IsObject()
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { PartialType } from '@nestjs/swagger';
|
||||
import { CreateEnvironmentDto } from './create-environment.dto';
|
||||
import { PartialType } from "@nestjs/swagger";
|
||||
import { CreateEnvironmentDto } from "./create-environment.dto";
|
||||
|
||||
export class UpdateEnvironmentDto extends PartialType(CreateEnvironmentDto) {}
|
||||
@@ -9,56 +9,59 @@ import {
|
||||
Patch,
|
||||
Post,
|
||||
Query,
|
||||
} from '@nestjs/common';
|
||||
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
|
||||
import { EnvironmentService } from './environment.service';
|
||||
import { CreateEnvironmentDto } from './dto/create-environment.dto';
|
||||
import { UpdateEnvironmentDto } from './dto/update-environment.dto';
|
||||
import { PaginationQueryDto } from '../common/dto/pagination.dto';
|
||||
import { EnvironmentOrderBy } from './environment.service';
|
||||
} from "@nestjs/common";
|
||||
import { ApiOperation, ApiResponse, ApiTags } from "@nestjs/swagger";
|
||||
import { EnvironmentService } from "./environment.service";
|
||||
import { CreateEnvironmentDto } from "./dto/create-environment.dto";
|
||||
import { UpdateEnvironmentDto } from "./dto/update-environment.dto";
|
||||
import { PaginationQueryDto } from "../common/dto/pagination.dto";
|
||||
import { EnvironmentOrderBy } from "./environment.service";
|
||||
|
||||
@ApiTags('environments')
|
||||
@Controller('environments')
|
||||
@ApiTags("environments")
|
||||
@Controller("environments")
|
||||
export class EnvironmentController {
|
||||
constructor(private readonly environmentService: EnvironmentService) {}
|
||||
|
||||
@Post()
|
||||
@ApiOperation({ summary: 'Create a new environment' })
|
||||
@ApiResponse({ status: 201, description: 'Environment created' })
|
||||
@ApiResponse({ status: 409, description: 'Environment name already exists' })
|
||||
@ApiOperation({ summary: "Create a new environment" })
|
||||
@ApiResponse({ status: 201, description: "Environment created" })
|
||||
@ApiResponse({ status: 409, description: "Environment name already exists" })
|
||||
create(@Body() dto: CreateEnvironmentDto) {
|
||||
return this.environmentService.create(dto);
|
||||
}
|
||||
|
||||
@Get()
|
||||
@ApiOperation({ summary: 'List all environments (paginated)' })
|
||||
@ApiResponse({ status: 200, description: 'Paginated environments' })
|
||||
@ApiOperation({ summary: "List all environments (paginated)" })
|
||||
@ApiResponse({ status: 200, description: "Paginated environments" })
|
||||
findAll(@Query() query: PaginationQueryDto<EnvironmentOrderBy>) {
|
||||
return this.environmentService.findAll(query);
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
@ApiOperation({ summary: 'Get environment by ID' })
|
||||
@ApiResponse({ status: 200, description: 'Environment record' })
|
||||
@ApiResponse({ status: 404, description: 'Environment not found' })
|
||||
findOne(@Param('id', ParseIntPipe) id: number) {
|
||||
@Get(":id")
|
||||
@ApiOperation({ summary: "Get environment by ID" })
|
||||
@ApiResponse({ status: 200, description: "Environment record" })
|
||||
@ApiResponse({ status: 404, description: "Environment not found" })
|
||||
findOne(@Param("id", ParseIntPipe) id: number) {
|
||||
return this.environmentService.findOne(id);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
@ApiOperation({ summary: 'Update an environment' })
|
||||
@ApiResponse({ status: 200, description: 'Environment updated' })
|
||||
@ApiResponse({ status: 404, description: 'Environment not found' })
|
||||
update(@Param('id', ParseIntPipe) id: number, @Body() dto: UpdateEnvironmentDto) {
|
||||
@Patch(":id")
|
||||
@ApiOperation({ summary: "Update an environment" })
|
||||
@ApiResponse({ status: 200, description: "Environment updated" })
|
||||
@ApiResponse({ status: 404, description: "Environment not found" })
|
||||
update(
|
||||
@Param("id", ParseIntPipe) id: number,
|
||||
@Body() dto: UpdateEnvironmentDto,
|
||||
) {
|
||||
return this.environmentService.update(id, dto);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
@Delete(":id")
|
||||
@HttpCode(204)
|
||||
@ApiOperation({ summary: 'Delete an environment' })
|
||||
@ApiResponse({ status: 204, description: 'Environment deleted' })
|
||||
@ApiResponse({ status: 404, description: 'Environment not found' })
|
||||
remove(@Param('id', ParseIntPipe) id: number) {
|
||||
@ApiOperation({ summary: "Delete an environment" })
|
||||
@ApiResponse({ status: 204, description: "Environment deleted" })
|
||||
@ApiResponse({ status: 404, description: "Environment not found" })
|
||||
remove(@Param("id", ParseIntPipe) id: number) {
|
||||
return this.environmentService.remove(id);
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@ import {
|
||||
Column,
|
||||
CreateDateColumn,
|
||||
UpdateDateColumn,
|
||||
} from 'typeorm';
|
||||
} from "typeorm";
|
||||
|
||||
export interface EnvironmentUrls {
|
||||
id_url?: string;
|
||||
@@ -13,7 +13,7 @@ export interface EnvironmentUrls {
|
||||
[key: string]: string | undefined;
|
||||
}
|
||||
|
||||
@Entity('environments')
|
||||
@Entity("environments")
|
||||
export class EnvironmentEntity {
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
@@ -21,7 +21,7 @@ export class EnvironmentEntity {
|
||||
@Column({ unique: true })
|
||||
name: string;
|
||||
|
||||
@Column('simple-json')
|
||||
@Column("simple-json")
|
||||
urls: EnvironmentUrls;
|
||||
|
||||
@CreateDateColumn()
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { EnvironmentEntity } from './environment.entity';
|
||||
import { EnvironmentService } from './environment.service';
|
||||
import { EnvironmentController } from './environment.controller';
|
||||
import { Module } from "@nestjs/common";
|
||||
import { TypeOrmModule } from "@nestjs/typeorm";
|
||||
import { EnvironmentEntity } from "./environment.entity";
|
||||
import { EnvironmentService } from "./environment.service";
|
||||
import { EnvironmentController } from "./environment.controller";
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([EnvironmentEntity])],
|
||||
|
||||
@@ -1,12 +1,19 @@
|
||||
import { ConflictException, Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { EnvironmentEntity } from './environment.entity';
|
||||
import { CreateEnvironmentDto } from './dto/create-environment.dto';
|
||||
import { UpdateEnvironmentDto } from './dto/update-environment.dto';
|
||||
import { PaginationQueryDto, PaginatedResult } from '../common/dto/pagination.dto';
|
||||
import {
|
||||
ConflictException,
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
} from "@nestjs/common";
|
||||
import { InjectRepository } from "@nestjs/typeorm";
|
||||
import { Repository } from "typeorm";
|
||||
import { EnvironmentEntity } from "./environment.entity";
|
||||
import { CreateEnvironmentDto } from "./dto/create-environment.dto";
|
||||
import { UpdateEnvironmentDto } from "./dto/update-environment.dto";
|
||||
import {
|
||||
PaginationQueryDto,
|
||||
PaginatedResult,
|
||||
} from "../common/dto/pagination.dto";
|
||||
|
||||
export type EnvironmentOrderBy = 'id' | 'name' | 'createdAt' | 'updatedAt';
|
||||
export type EnvironmentOrderBy = "id" | "name" | "createdAt" | "updatedAt";
|
||||
|
||||
@Injectable()
|
||||
export class EnvironmentService {
|
||||
@@ -23,11 +30,13 @@ export class EnvironmentService {
|
||||
return this.repo.save(this.repo.create(dto));
|
||||
}
|
||||
|
||||
async findAll(query: PaginationQueryDto<EnvironmentOrderBy> = {}): Promise<PaginatedResult<EnvironmentEntity>> {
|
||||
async findAll(
|
||||
query: PaginationQueryDto<EnvironmentOrderBy> = {},
|
||||
): Promise<PaginatedResult<EnvironmentEntity>> {
|
||||
const page = query.page ?? 1;
|
||||
const limit = query.limit ?? 20;
|
||||
const orderBy = query.orderBy ?? 'id';
|
||||
const orderDir = query.orderDir ?? 'ASC';
|
||||
const orderBy = query.orderBy ?? "id";
|
||||
const orderDir = query.orderDir ?? "ASC";
|
||||
const [data, total] = await this.repo.findAndCount({
|
||||
order: { [orderBy]: orderDir },
|
||||
skip: (page - 1) * limit,
|
||||
@@ -42,7 +51,10 @@ export class EnvironmentService {
|
||||
return env;
|
||||
}
|
||||
|
||||
async update(id: number, dto: UpdateEnvironmentDto): Promise<EnvironmentEntity> {
|
||||
async update(
|
||||
id: number,
|
||||
dto: UpdateEnvironmentDto,
|
||||
): Promise<EnvironmentEntity> {
|
||||
const env = await this.findOne(id);
|
||||
Object.assign(env, dto);
|
||||
return this.repo.save(env);
|
||||
|
||||
@@ -5,9 +5,9 @@ import {
|
||||
ExceptionFilter,
|
||||
HttpException,
|
||||
InternalServerErrorException,
|
||||
} from '@nestjs/common';
|
||||
import { TraceLogger } from '../common/trace-logger';
|
||||
import type { Request, Response } from 'express';
|
||||
} from "@nestjs/common";
|
||||
import { TraceLogger } from "../common/trace-logger";
|
||||
import type { Request, Response } from "express";
|
||||
|
||||
@Catch(BadRequestException, InternalServerErrorException)
|
||||
export class HttpExceptionFilter implements ExceptionFilter {
|
||||
@@ -20,8 +20,8 @@ export class HttpExceptionFilter implements ExceptionFilter {
|
||||
const status = exception.getStatus();
|
||||
const body = exception.getResponse();
|
||||
|
||||
const cause = (exception as any).cause as Error | undefined;
|
||||
const causeMessage = cause ? ` | cause: ${cause.message}` : '';
|
||||
const cause = (exception as unknown as { cause?: Error }).cause;
|
||||
const causeMessage = cause ? ` | cause: ${cause.message}` : "";
|
||||
const message = `${exception.message}${causeMessage}`;
|
||||
|
||||
if (status >= 500) {
|
||||
@@ -30,7 +30,9 @@ export class HttpExceptionFilter implements ExceptionFilter {
|
||||
cause?.stack ?? exception.stack,
|
||||
);
|
||||
} else {
|
||||
this.logger.warn(`[${request.method} ${request.url}] ${status} — ${message}`);
|
||||
this.logger.warn(
|
||||
`[${request.method} ${request.url}] ${status} — ${message}`,
|
||||
);
|
||||
}
|
||||
|
||||
response.status(status).json(body);
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
import { Controller, Get } from '@nestjs/common';
|
||||
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
|
||||
import { Controller, Get } from "@nestjs/common";
|
||||
import { ApiOperation, ApiResponse, ApiTags } from "@nestjs/swagger";
|
||||
|
||||
@ApiTags('health')
|
||||
@ApiTags("health")
|
||||
@Controller()
|
||||
export class HealthController {
|
||||
@Get('healthz')
|
||||
@ApiOperation({ summary: 'Health check' })
|
||||
@ApiResponse({ status: 200, description: 'Service is healthy' })
|
||||
@Get("healthz")
|
||||
@ApiOperation({ summary: "Health check" })
|
||||
@ApiResponse({ status: 200, description: "Service is healthy" })
|
||||
healthz(): { status: string } {
|
||||
return { status: 'ok' };
|
||||
return { status: "ok" };
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { HealthController } from './health.controller';
|
||||
import { Module } from "@nestjs/common";
|
||||
import { HealthController } from "./health.controller";
|
||||
|
||||
@Module({
|
||||
controllers: [HealthController],
|
||||
|
||||
@@ -3,10 +3,10 @@ import {
|
||||
ExecutionContext,
|
||||
Injectable,
|
||||
NestInterceptor,
|
||||
} from '@nestjs/common';
|
||||
import { TraceLogger } from '../common/trace-logger';
|
||||
import type { Request, Response } from 'express';
|
||||
import { Observable, tap } from 'rxjs';
|
||||
} from "@nestjs/common";
|
||||
import { TraceLogger } from "../common/trace-logger";
|
||||
import type { Request, Response } from "express";
|
||||
import { Observable, tap } from "rxjs";
|
||||
|
||||
@Injectable()
|
||||
export class LoggingInterceptor implements NestInterceptor {
|
||||
@@ -18,16 +18,20 @@ export class LoggingInterceptor implements NestInterceptor {
|
||||
const res = http.getResponse<Response>();
|
||||
const { method, url, body } = req;
|
||||
const start = Date.now();
|
||||
const bodyStr = body && Object.keys(body).length ? ` ${JSON.stringify(body)}` : '';
|
||||
const bodyStr =
|
||||
body && Object.keys(body).length ? ` ${JSON.stringify(body)}` : "";
|
||||
|
||||
this.logger.debug(`→ ${method} ${url}${bodyStr}`);
|
||||
|
||||
return next.handle().pipe(
|
||||
tap((responseBody) => {
|
||||
const ms = Date.now() - start;
|
||||
const len = responseBody != null ? JSON.stringify(responseBody).length : 0;
|
||||
const lenStr = len > 0 ? ` [${len}b]` : '';
|
||||
this.logger.debug(`← ${method} ${url} ${res.statusCode} (${ms}ms)${lenStr}`);
|
||||
const len =
|
||||
responseBody != null ? JSON.stringify(responseBody).length : 0;
|
||||
const lenStr = len > 0 ? ` [${len}b]` : "";
|
||||
this.logger.debug(
|
||||
`← ${method} ${url} ${res.statusCode} (${ms}ms)${lenStr}`,
|
||||
);
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,14 +1,20 @@
|
||||
import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common';
|
||||
import type { Request } from 'express';
|
||||
import * as crypto from 'crypto';
|
||||
import { Observable } from 'rxjs';
|
||||
import { traceStorage } from '../common/trace-context';
|
||||
import {
|
||||
CallHandler,
|
||||
ExecutionContext,
|
||||
Injectable,
|
||||
NestInterceptor,
|
||||
} from "@nestjs/common";
|
||||
import type { Request } from "express";
|
||||
import * as crypto from "crypto";
|
||||
import { Observable } from "rxjs";
|
||||
import { traceStorage } from "../common/trace-context";
|
||||
|
||||
@Injectable()
|
||||
export class TraceInterceptor implements NestInterceptor {
|
||||
intercept(context: ExecutionContext, next: CallHandler): Observable<unknown> {
|
||||
const req = context.switchToHttp().getRequest<Request>();
|
||||
const traceId = (req.headers['x-trace-id'] as string | undefined) ?? crypto.randomUUID();
|
||||
const traceId =
|
||||
(req.headers["x-trace-id"] as string | undefined) ?? crypto.randomUUID();
|
||||
|
||||
return new Observable((subscriber) => {
|
||||
traceStorage.run({ traceId }, () => {
|
||||
|
||||
+20
-16
@@ -1,25 +1,27 @@
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { ValidationPipe } from '@nestjs/common';
|
||||
import { TraceLogger } from './common/trace-logger';
|
||||
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
|
||||
import { AppModule } from './app.module';
|
||||
import { HttpExceptionFilter } from './filters/http-exception.filter';
|
||||
import { TraceInterceptor } from './interceptors/trace.interceptor';
|
||||
import { LoggingInterceptor } from './interceptors/logging.interceptor';
|
||||
import { name as pkgName, version as pkgVersion } from '../package.json';
|
||||
import { NestFactory } from "@nestjs/core";
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
import { ValidationPipe } from "@nestjs/common";
|
||||
import { TraceLogger } from "./common/trace-logger";
|
||||
import { DocumentBuilder, SwaggerModule } from "@nestjs/swagger";
|
||||
import { AppModule } from "./app.module";
|
||||
import { HttpExceptionFilter } from "./filters/http-exception.filter";
|
||||
import { TraceInterceptor } from "./interceptors/trace.interceptor";
|
||||
import { LoggingInterceptor } from "./interceptors/logging.interceptor";
|
||||
import { name as pkgName, version as pkgVersion } from "../package.json";
|
||||
|
||||
async function bootstrap() {
|
||||
const logger = new TraceLogger('Bootstrap');
|
||||
const app = await NestFactory.create(AppModule, { logger: new TraceLogger('Bootstrap', { timestamp: true }) });
|
||||
const logger = new TraceLogger("Bootstrap");
|
||||
const app = await NestFactory.create(AppModule, {
|
||||
logger: new TraceLogger("Bootstrap", { timestamp: true }),
|
||||
});
|
||||
|
||||
app.useGlobalPipes(new ValidationPipe({ transform: true }));
|
||||
app.useGlobalFilters(new HttpExceptionFilter());
|
||||
app.useGlobalInterceptors(new TraceInterceptor(), new LoggingInterceptor());
|
||||
|
||||
const config = app.get(ConfigService);
|
||||
const port = config.get<number>('PORT', 3000);
|
||||
const nodeEnv = config.get<string>('NODE_ENV', 'development');
|
||||
const port = config.get<number>("PORT", 3000);
|
||||
const nodeEnv = config.get<string>("NODE_ENV", "development");
|
||||
|
||||
const swaggerConfig = new DocumentBuilder()
|
||||
.setTitle(pkgName)
|
||||
@@ -28,11 +30,13 @@ async function bootstrap() {
|
||||
.build();
|
||||
|
||||
const document = SwaggerModule.createDocument(app, swaggerConfig);
|
||||
SwaggerModule.setup('api', app, document);
|
||||
SwaggerModule.setup("api", app, document);
|
||||
|
||||
await app.listen(port);
|
||||
|
||||
logger.log(`Application "${pkgName}" v${pkgVersion} running on port ${port} [${nodeEnv}]`);
|
||||
logger.log(
|
||||
`Application "${pkgName}" v${pkgVersion} running on port ${port} [${nodeEnv}]`,
|
||||
);
|
||||
logger.log(`Swagger UI available at http://localhost:${port}/api`);
|
||||
logger.log(`MCP endpoint available at http://localhost:${port}/mcp`);
|
||||
}
|
||||
|
||||
+41
-25
@@ -1,52 +1,68 @@
|
||||
import { Controller, Delete, Get, Post, Req, Res } from '@nestjs/common';
|
||||
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
|
||||
import type { Request, Response } from 'express';
|
||||
import { McpService } from './mcp.service';
|
||||
import { Controller, Delete, Get, Post, Req, Res } from "@nestjs/common";
|
||||
import { ApiOperation, ApiResponse, ApiTags } from "@nestjs/swagger";
|
||||
import type { Request, Response } from "express";
|
||||
import { McpService } from "./mcp.service";
|
||||
|
||||
@ApiTags('mcp')
|
||||
@Controller('mcp')
|
||||
@ApiTags("mcp")
|
||||
@Controller("mcp")
|
||||
export class McpController {
|
||||
constructor(private readonly mcpService: McpService) {}
|
||||
|
||||
@Post()
|
||||
@ApiOperation({
|
||||
summary: 'Send JSON-RPC message',
|
||||
summary: "Send JSON-RPC message",
|
||||
description:
|
||||
'Accepts a JSON-RPC request, notification, or response. ' +
|
||||
'Returns either `application/json` for a single response or ' +
|
||||
'`text/event-stream` (SSE) when the server streams multiple messages.',
|
||||
"Accepts a JSON-RPC request, notification, or response. " +
|
||||
"Returns either `application/json` for a single response or " +
|
||||
"`text/event-stream` (SSE) when the server streams multiple messages.",
|
||||
})
|
||||
@ApiResponse({
|
||||
status: 200,
|
||||
description: "JSON-RPC response (application/json or text/event-stream)",
|
||||
})
|
||||
@ApiResponse({
|
||||
status: 202,
|
||||
description: "Accepted — input was a notification or response only",
|
||||
})
|
||||
@ApiResponse({
|
||||
status: 400,
|
||||
description: "Bad Request — malformed JSON-RPC payload",
|
||||
})
|
||||
@ApiResponse({ status: 200, description: 'JSON-RPC response (application/json or text/event-stream)' })
|
||||
@ApiResponse({ status: 202, description: 'Accepted — input was a notification or response only' })
|
||||
@ApiResponse({ status: 400, description: 'Bad Request — malformed JSON-RPC payload' })
|
||||
post(@Req() req: Request, @Res() res: Response): Promise<void> {
|
||||
return this.mcpService.handle(req, res);
|
||||
}
|
||||
|
||||
@Get()
|
||||
@ApiOperation({
|
||||
summary: 'Open server-sent event stream',
|
||||
summary: "Open server-sent event stream",
|
||||
description:
|
||||
'Opens a persistent SSE stream so the server can push JSON-RPC requests and ' +
|
||||
'notifications to the client without a prior POST. ' +
|
||||
'Requires `Accept: text/event-stream`. Pass `Last-Event-ID` to resume a broken stream.',
|
||||
"Opens a persistent SSE stream so the server can push JSON-RPC requests and " +
|
||||
"notifications to the client without a prior POST. " +
|
||||
"Requires `Accept: text/event-stream`. Pass `Last-Event-ID` to resume a broken stream.",
|
||||
})
|
||||
@ApiResponse({ status: 200, description: "SSE stream (text/event-stream)" })
|
||||
@ApiResponse({
|
||||
status: 405,
|
||||
description: "Method Not Allowed — server does not offer an SSE stream",
|
||||
})
|
||||
@ApiResponse({ status: 200, description: 'SSE stream (text/event-stream)' })
|
||||
@ApiResponse({ status: 405, description: 'Method Not Allowed — server does not offer an SSE stream' })
|
||||
get(@Req() req: Request, @Res() res: Response): Promise<void> {
|
||||
return this.mcpService.handle(req, res);
|
||||
}
|
||||
|
||||
@Delete()
|
||||
@ApiOperation({
|
||||
summary: 'Terminate session',
|
||||
summary: "Terminate session",
|
||||
description:
|
||||
'Explicitly terminates a session identified by the `Mcp-Session-Id` header. ' +
|
||||
'The server may return 405 if it does not support client-initiated session termination.',
|
||||
"Explicitly terminates a session identified by the `Mcp-Session-Id` header. " +
|
||||
"The server may return 405 if it does not support client-initiated session termination.",
|
||||
})
|
||||
@ApiResponse({ status: 200, description: "Session terminated" })
|
||||
@ApiResponse({ status: 404, description: "Session not found" })
|
||||
@ApiResponse({
|
||||
status: 405,
|
||||
description:
|
||||
"Method Not Allowed — server does not support session termination",
|
||||
})
|
||||
@ApiResponse({ status: 200, description: 'Session terminated' })
|
||||
@ApiResponse({ status: 404, description: 'Session not found' })
|
||||
@ApiResponse({ status: 405, description: 'Method Not Allowed — server does not support session termination' })
|
||||
delete(@Req() req: Request, @Res() res: Response): Promise<void> {
|
||||
return this.mcpService.handle(req, res);
|
||||
}
|
||||
|
||||
+17
-10
@@ -1,15 +1,22 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { McpController } from './mcp.controller';
|
||||
import { McpService } from './mcp.service';
|
||||
import { AuthModule } from '../auth/auth.module';
|
||||
import { SessionModule } from '../session/session.module';
|
||||
import { EnvironmentModule } from '../environment/environment.module';
|
||||
import { BrowserModule } from '../browser/browser.module';
|
||||
import { CodeExecutorModule } from '../code-executor/code-executor.module';
|
||||
import { ScenarioModule } from '../scenario/scenario.module';
|
||||
import { Module } from "@nestjs/common";
|
||||
import { McpController } from "./mcp.controller";
|
||||
import { McpService } from "./mcp.service";
|
||||
import { AuthModule } from "../auth/auth.module";
|
||||
import { SessionModule } from "../session/session.module";
|
||||
import { EnvironmentModule } from "../environment/environment.module";
|
||||
import { BrowserModule } from "../browser/browser.module";
|
||||
import { CodeExecutorModule } from "../code-executor/code-executor.module";
|
||||
import { ScenarioModule } from "../scenario/scenario.module";
|
||||
|
||||
@Module({
|
||||
imports: [AuthModule, SessionModule, EnvironmentModule, BrowserModule, CodeExecutorModule, ScenarioModule],
|
||||
imports: [
|
||||
AuthModule,
|
||||
SessionModule,
|
||||
EnvironmentModule,
|
||||
BrowserModule,
|
||||
CodeExecutorModule,
|
||||
ScenarioModule,
|
||||
],
|
||||
controllers: [McpController],
|
||||
providers: [McpService],
|
||||
})
|
||||
|
||||
+448
-165
@@ -1,17 +1,17 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
||||
import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
|
||||
import { z } from 'zod';
|
||||
import type { Request, Response } from 'express';
|
||||
import { AuthService } from '../auth/auth.service';
|
||||
import { SessionService } from '../session/session.service';
|
||||
import { EnvironmentService } from '../environment/environment.service';
|
||||
import type { EnvironmentUrls } from '../environment/environment.entity';
|
||||
import { BrowserService } from '../browser/browser.service';
|
||||
import { CodeExecutorService } from '../code-executor/code-executor.service';
|
||||
import { ScenarioService } from '../scenario/scenario.service';
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
||||
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
||||
import { z } from "zod";
|
||||
import type { Request, Response } from "express";
|
||||
import { AuthService } from "../auth/auth.service";
|
||||
import { SessionService } from "../session/session.service";
|
||||
import { EnvironmentService } from "../environment/environment.service";
|
||||
import type { EnvironmentUrls } from "../environment/environment.entity";
|
||||
import { BrowserService } from "../browser/browser.service";
|
||||
import { CodeExecutorService } from "../code-executor/code-executor.service";
|
||||
import { ScenarioService } from "../scenario/scenario.service";
|
||||
|
||||
import pkg from '../../package.json';
|
||||
import pkg from "../../package.json";
|
||||
|
||||
@Injectable()
|
||||
export class McpService {
|
||||
@@ -31,161 +31,273 @@ export class McpService {
|
||||
}
|
||||
|
||||
private registerTools(server: McpServer): void {
|
||||
|
||||
// ── Auth ──────────────────────────────────────────────────────────────────
|
||||
|
||||
server.registerTool(
|
||||
'list_keys',
|
||||
{ description: 'List available key identifiers from the keys directory' },
|
||||
"list_keys",
|
||||
{ description: "List available key identifiers from the keys directory" },
|
||||
async () => {
|
||||
const keys = this.authService.listKeys();
|
||||
return { content: [{ type: 'text' as const, text: JSON.stringify(keys) }] };
|
||||
return {
|
||||
content: [{ type: "text" as const, text: JSON.stringify(keys) }],
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
server.registerTool(
|
||||
'login',
|
||||
"login",
|
||||
{
|
||||
description: 'Log in using a file key against a named environment and store the session',
|
||||
description:
|
||||
"Log in using a file key against a named environment and store the session",
|
||||
inputSchema: {
|
||||
key: z.string().describe('Key identifier (filename without extension from keys/ dir)'),
|
||||
environmentName: z.string().describe('Environment name to resolve login/cabinet URLs'),
|
||||
sessionName: z.string().optional().describe('Session name to store credentials under. Auto-UUID if omitted.'),
|
||||
key: z
|
||||
.string()
|
||||
.describe(
|
||||
"Key identifier (filename without extension from keys/ dir)",
|
||||
),
|
||||
environmentName: z
|
||||
.string()
|
||||
.describe("Environment name to resolve login/cabinet URLs"),
|
||||
sessionName: z
|
||||
.string()
|
||||
.optional()
|
||||
.describe(
|
||||
"Session name to store credentials under. Auto-UUID if omitted.",
|
||||
),
|
||||
},
|
||||
},
|
||||
async ({ key, environmentName, sessionName }) => {
|
||||
const result = await this.authService.login(key, environmentName, sessionName);
|
||||
return { content: [{ type: 'text' as const, text: JSON.stringify(result) }] };
|
||||
const result = await this.authService.login(
|
||||
key,
|
||||
environmentName,
|
||||
sessionName,
|
||||
);
|
||||
return {
|
||||
content: [{ type: "text" as const, text: JSON.stringify(result) }],
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
// ── Sessions ──────────────────────────────────────────────────────────────
|
||||
|
||||
server.registerTool(
|
||||
'list_sessions',
|
||||
"list_sessions",
|
||||
{
|
||||
description: 'List all stored sessions (id, sessionName, createdAt, updatedAt), paginated',
|
||||
description:
|
||||
"List all stored sessions (id, sessionName, createdAt, updatedAt), paginated",
|
||||
inputSchema: {
|
||||
page: z.number().int().min(1).optional().describe('Page number (default 1)'),
|
||||
limit: z.number().int().min(1).optional().describe('Items per page (default 20)'),
|
||||
orderBy: z.enum(['id', 'sessionName', 'createdAt', 'updatedAt']).optional().describe('Field to order by (default id)'),
|
||||
orderDir: z.enum(['ASC', 'DESC']).optional().describe('Sort direction (default ASC)'),
|
||||
page: z
|
||||
.number()
|
||||
.int()
|
||||
.min(1)
|
||||
.optional()
|
||||
.describe("Page number (default 1)"),
|
||||
limit: z
|
||||
.number()
|
||||
.int()
|
||||
.min(1)
|
||||
.optional()
|
||||
.describe("Items per page (default 20)"),
|
||||
orderBy: z
|
||||
.enum(["id", "sessionName", "createdAt", "updatedAt"])
|
||||
.optional()
|
||||
.describe("Field to order by (default id)"),
|
||||
orderDir: z
|
||||
.enum(["ASC", "DESC"])
|
||||
.optional()
|
||||
.describe("Sort direction (default ASC)"),
|
||||
},
|
||||
},
|
||||
async ({ page, limit, orderBy, orderDir }) => {
|
||||
const sessions = await this.sessionService.findAll({ page, limit, orderBy, orderDir });
|
||||
return { content: [{ type: 'text' as const, text: JSON.stringify(sessions) }] };
|
||||
const sessions = await this.sessionService.findAll({
|
||||
page,
|
||||
limit,
|
||||
orderBy,
|
||||
orderDir,
|
||||
});
|
||||
return {
|
||||
content: [{ type: "text" as const, text: JSON.stringify(sessions) }],
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
server.registerTool(
|
||||
'delete_session',
|
||||
"delete_session",
|
||||
{
|
||||
description: 'Delete a session by numeric ID',
|
||||
description: "Delete a session by numeric ID",
|
||||
inputSchema: {
|
||||
id: z.number().int().describe('Session ID to delete'),
|
||||
id: z.number().int().describe("Session ID to delete"),
|
||||
},
|
||||
},
|
||||
async ({ id }) => {
|
||||
const { data } = await this.sessionService.findAll();
|
||||
if (!data.find(s => s.id === id)) {
|
||||
return { isError: true, content: [{ type: 'text' as const, text: `Session ${id} not found` }] };
|
||||
if (!data.find((s) => s.id === id)) {
|
||||
return {
|
||||
isError: true,
|
||||
content: [
|
||||
{ type: "text" as const, text: `Session ${id} not found` },
|
||||
],
|
||||
};
|
||||
}
|
||||
await this.sessionService.remove(id);
|
||||
return { content: [{ type: 'text' as const, text: `Session ${id} deleted` }] };
|
||||
return {
|
||||
content: [{ type: "text" as const, text: `Session ${id} deleted` }],
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
// ── Environments ──────────────────────────────────────────────────────────
|
||||
|
||||
server.registerTool(
|
||||
'list_environments',
|
||||
"list_environments",
|
||||
{
|
||||
description: 'List all environments, paginated',
|
||||
description: "List all environments, paginated",
|
||||
inputSchema: {
|
||||
page: z.number().int().min(1).optional().describe('Page number (default 1)'),
|
||||
limit: z.number().int().min(1).optional().describe('Items per page (default 20)'),
|
||||
orderBy: z.enum(['id', 'name', 'createdAt', 'updatedAt']).optional().describe('Field to order by (default id)'),
|
||||
orderDir: z.enum(['ASC', 'DESC']).optional().describe('Sort direction (default ASC)'),
|
||||
page: z
|
||||
.number()
|
||||
.int()
|
||||
.min(1)
|
||||
.optional()
|
||||
.describe("Page number (default 1)"),
|
||||
limit: z
|
||||
.number()
|
||||
.int()
|
||||
.min(1)
|
||||
.optional()
|
||||
.describe("Items per page (default 20)"),
|
||||
orderBy: z
|
||||
.enum(["id", "name", "createdAt", "updatedAt"])
|
||||
.optional()
|
||||
.describe("Field to order by (default id)"),
|
||||
orderDir: z
|
||||
.enum(["ASC", "DESC"])
|
||||
.optional()
|
||||
.describe("Sort direction (default ASC)"),
|
||||
},
|
||||
},
|
||||
async ({ page, limit, orderBy, orderDir }) => {
|
||||
const envs = await this.environmentService.findAll({ page, limit, orderBy, orderDir });
|
||||
return { content: [{ type: 'text' as const, text: JSON.stringify(envs) }] };
|
||||
const envs = await this.environmentService.findAll({
|
||||
page,
|
||||
limit,
|
||||
orderBy,
|
||||
orderDir,
|
||||
});
|
||||
return {
|
||||
content: [{ type: "text" as const, text: JSON.stringify(envs) }],
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
server.registerTool(
|
||||
'get_environment',
|
||||
"get_environment",
|
||||
{
|
||||
description: 'Get an environment record by ID',
|
||||
description: "Get an environment record by ID",
|
||||
inputSchema: {
|
||||
id: z.number().int().describe('Environment ID'),
|
||||
id: z.number().int().describe("Environment ID"),
|
||||
},
|
||||
},
|
||||
async ({ id }) => {
|
||||
try {
|
||||
const env = await this.environmentService.findOne(id);
|
||||
return { content: [{ type: 'text' as const, text: JSON.stringify(env) }] };
|
||||
return {
|
||||
content: [{ type: "text" as const, text: JSON.stringify(env) }],
|
||||
};
|
||||
} catch {
|
||||
return { isError: true, content: [{ type: 'text' as const, text: `Environment ${id} not found` }] };
|
||||
return {
|
||||
isError: true,
|
||||
content: [
|
||||
{ type: "text" as const, text: `Environment ${id} not found` },
|
||||
],
|
||||
};
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
server.registerTool(
|
||||
'create_environment',
|
||||
"create_environment",
|
||||
{
|
||||
description: 'Create a new named environment with a set of URLs',
|
||||
description: "Create a new named environment with a set of URLs",
|
||||
inputSchema: {
|
||||
name: z.string().describe('Unique environment name, e.g. liquio-diia-stg'),
|
||||
urls: z.record(z.string(), z.string()).describe('Map of URL keys to URL strings (id_url, cabinet_url, admin_url, …)'),
|
||||
name: z
|
||||
.string()
|
||||
.describe("Unique environment name, e.g. liquio-diia-stg"),
|
||||
urls: z
|
||||
.record(z.string(), z.string())
|
||||
.describe(
|
||||
"Map of URL keys to URL strings (id_url, cabinet_url, admin_url, …)",
|
||||
),
|
||||
},
|
||||
},
|
||||
async ({ name, urls }) => {
|
||||
try {
|
||||
const env = await this.environmentService.create({ name, urls: urls as EnvironmentUrls });
|
||||
return { content: [{ type: 'text' as const, text: JSON.stringify(env) }] };
|
||||
const env = await this.environmentService.create({
|
||||
name,
|
||||
urls: urls as EnvironmentUrls,
|
||||
});
|
||||
return {
|
||||
content: [{ type: "text" as const, text: JSON.stringify(env) }],
|
||||
};
|
||||
} catch (err) {
|
||||
return { isError: true, content: [{ type: 'text' as const, text: (err as Error).message }] };
|
||||
return {
|
||||
isError: true,
|
||||
content: [{ type: "text" as const, text: (err as Error).message }],
|
||||
};
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
server.registerTool(
|
||||
'update_environment',
|
||||
"update_environment",
|
||||
{
|
||||
description: 'Update an existing environment (name and/or urls)',
|
||||
description: "Update an existing environment (name and/or urls)",
|
||||
inputSchema: {
|
||||
id: z.number().int().describe('Environment ID to update'),
|
||||
name: z.string().optional().describe('New name'),
|
||||
urls: z.record(z.string(), z.string()).optional().describe('New URLs map'),
|
||||
id: z.number().int().describe("Environment ID to update"),
|
||||
name: z.string().optional().describe("New name"),
|
||||
urls: z
|
||||
.record(z.string(), z.string())
|
||||
.optional()
|
||||
.describe("New URLs map"),
|
||||
},
|
||||
},
|
||||
async ({ id, name, urls }) => {
|
||||
try {
|
||||
const env = await this.environmentService.update(id, { name, urls: urls as EnvironmentUrls | undefined });
|
||||
return { content: [{ type: 'text' as const, text: JSON.stringify(env) }] };
|
||||
const env = await this.environmentService.update(id, {
|
||||
name,
|
||||
urls: urls as EnvironmentUrls | undefined,
|
||||
});
|
||||
return {
|
||||
content: [{ type: "text" as const, text: JSON.stringify(env) }],
|
||||
};
|
||||
} catch (err) {
|
||||
return { isError: true, content: [{ type: 'text' as const, text: (err as Error).message }] };
|
||||
return {
|
||||
isError: true,
|
||||
content: [{ type: "text" as const, text: (err as Error).message }],
|
||||
};
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
server.registerTool(
|
||||
'delete_environment',
|
||||
"delete_environment",
|
||||
{
|
||||
description: 'Delete an environment by ID',
|
||||
description: "Delete an environment by ID",
|
||||
inputSchema: {
|
||||
id: z.number().int().describe('Environment ID to delete'),
|
||||
id: z.number().int().describe("Environment ID to delete"),
|
||||
},
|
||||
},
|
||||
async ({ id }) => {
|
||||
try {
|
||||
await this.environmentService.remove(id);
|
||||
return { content: [{ type: 'text' as const, text: `Environment ${id} deleted` }] };
|
||||
return {
|
||||
content: [
|
||||
{ type: "text" as const, text: `Environment ${id} deleted` },
|
||||
],
|
||||
};
|
||||
} catch (err) {
|
||||
return { isError: true, content: [{ type: 'text' as const, text: (err as Error).message }] };
|
||||
return {
|
||||
isError: true,
|
||||
content: [{ type: "text" as const, text: (err as Error).message }],
|
||||
};
|
||||
}
|
||||
},
|
||||
);
|
||||
@@ -193,43 +305,86 @@ export class McpService {
|
||||
// ── Browser ───────────────────────────────────────────────────────────────
|
||||
|
||||
server.registerTool(
|
||||
'open_url',
|
||||
"open_url",
|
||||
{
|
||||
description: 'Open a URL using a stored session and return the page title and content',
|
||||
description:
|
||||
"Open a URL using a stored session and return the page title and content",
|
||||
inputSchema: {
|
||||
sessionName: z.string().optional().describe('Session name to restore cookies and localStorage from. Omit to open without a stored session.'),
|
||||
url: z.string().url().describe('URL to navigate to'),
|
||||
readerMode: z.boolean().optional().describe('Extract readable plain text instead of raw HTML'),
|
||||
selector: z.string().optional().describe('CSS selector whose matching element content is returned; applied before readerMode'),
|
||||
sessionName: z
|
||||
.string()
|
||||
.optional()
|
||||
.describe(
|
||||
"Session name to restore cookies and localStorage from. Omit to open without a stored session.",
|
||||
),
|
||||
url: z.string().url().describe("URL to navigate to"),
|
||||
readerMode: z
|
||||
.boolean()
|
||||
.optional()
|
||||
.describe("Extract readable plain text instead of raw HTML"),
|
||||
selector: z
|
||||
.string()
|
||||
.optional()
|
||||
.describe(
|
||||
"CSS selector whose matching element content is returned; applied before readerMode",
|
||||
),
|
||||
},
|
||||
},
|
||||
async ({ sessionName, url, readerMode, selector }) => {
|
||||
try {
|
||||
const result = await this.browserService.open(sessionName, url, readerMode ?? false, selector);
|
||||
return { content: [{ type: 'text' as const, text: JSON.stringify(result) }] };
|
||||
const result = await this.browserService.open(
|
||||
sessionName,
|
||||
url,
|
||||
readerMode ?? false,
|
||||
selector,
|
||||
);
|
||||
return {
|
||||
content: [{ type: "text" as const, text: JSON.stringify(result) }],
|
||||
};
|
||||
} catch (err) {
|
||||
return { isError: true, content: [{ type: 'text' as const, text: (err as Error).message }] };
|
||||
return {
|
||||
isError: true,
|
||||
content: [{ type: "text" as const, text: (err as Error).message }],
|
||||
};
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
server.registerTool(
|
||||
'exec_code',
|
||||
"exec_code",
|
||||
{
|
||||
description: 'Execute arbitrary Playwright JavaScript with `page` and `context` in scope',
|
||||
description:
|
||||
"Execute arbitrary Playwright JavaScript with `page` and `context` in scope",
|
||||
inputSchema: {
|
||||
sessionName: z.string().optional().describe('Session name to restore. Omit to run without a stored session.'),
|
||||
code: z.string().describe('JavaScript code body to execute (async-safe, may use `page` and `context`)'),
|
||||
url: z.string().url().optional().describe('Optional URL to navigate to before running code'),
|
||||
sessionName: z
|
||||
.string()
|
||||
.optional()
|
||||
.describe(
|
||||
"Session name to restore. Omit to run without a stored session.",
|
||||
),
|
||||
code: z
|
||||
.string()
|
||||
.describe(
|
||||
"JavaScript code body to execute (async-safe, may use `page` and `context`)",
|
||||
),
|
||||
url: z
|
||||
.string()
|
||||
.url()
|
||||
.optional()
|
||||
.describe("Optional URL to navigate to before running code"),
|
||||
},
|
||||
},
|
||||
async ({ sessionName, code, url }) => {
|
||||
try {
|
||||
this.codeExecutor.validate(code);
|
||||
const result = await this.browserService.exec(sessionName, code, url);
|
||||
return { content: [{ type: 'text' as const, text: JSON.stringify(result) }] };
|
||||
return {
|
||||
content: [{ type: "text" as const, text: JSON.stringify(result) }],
|
||||
};
|
||||
} catch (err) {
|
||||
return { isError: true, content: [{ type: 'text' as const, text: (err as Error).message }] };
|
||||
return {
|
||||
isError: true,
|
||||
content: [{ type: "text" as const, text: (err as Error).message }],
|
||||
};
|
||||
}
|
||||
},
|
||||
);
|
||||
@@ -237,215 +392,341 @@ export class McpService {
|
||||
// ── Scenarios ─────────────────────────────────────────────────────────────
|
||||
|
||||
server.registerTool(
|
||||
'list_scenarios',
|
||||
"list_scenarios",
|
||||
{
|
||||
description: 'List all scenarios (paginated)',
|
||||
description: "List all scenarios (paginated)",
|
||||
inputSchema: {
|
||||
page: z.number().int().min(1).optional().describe('Page number (default 1)'),
|
||||
limit: z.number().int().min(1).optional().describe('Items per page (default 20)'),
|
||||
orderBy: z.enum(['id', 'name', 'createdAt', 'updatedAt']).optional().describe('Field to order by (default id)'),
|
||||
orderDir: z.enum(['ASC', 'DESC']).optional().describe('Sort direction (default ASC)'),
|
||||
page: z
|
||||
.number()
|
||||
.int()
|
||||
.min(1)
|
||||
.optional()
|
||||
.describe("Page number (default 1)"),
|
||||
limit: z
|
||||
.number()
|
||||
.int()
|
||||
.min(1)
|
||||
.optional()
|
||||
.describe("Items per page (default 20)"),
|
||||
orderBy: z
|
||||
.enum(["id", "name", "createdAt", "updatedAt"])
|
||||
.optional()
|
||||
.describe("Field to order by (default id)"),
|
||||
orderDir: z
|
||||
.enum(["ASC", "DESC"])
|
||||
.optional()
|
||||
.describe("Sort direction (default ASC)"),
|
||||
},
|
||||
},
|
||||
async ({ page, limit, orderBy, orderDir }) => {
|
||||
const result = await this.scenarioService.findAll({ page, limit, orderBy, orderDir });
|
||||
return { content: [{ type: 'text' as const, text: JSON.stringify(result) }] };
|
||||
const result = await this.scenarioService.findAll({
|
||||
page,
|
||||
limit,
|
||||
orderBy,
|
||||
orderDir,
|
||||
});
|
||||
return {
|
||||
content: [{ type: "text" as const, text: JSON.stringify(result) }],
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
server.registerTool(
|
||||
'get_scenario',
|
||||
"get_scenario",
|
||||
{
|
||||
description: 'Get a scenario with its steps by ID',
|
||||
description: "Get a scenario with its steps by ID",
|
||||
inputSchema: {
|
||||
id: z.number().int().describe('Scenario ID'),
|
||||
id: z.number().int().describe("Scenario ID"),
|
||||
},
|
||||
},
|
||||
async ({ id }) => {
|
||||
try {
|
||||
const scenario = await this.scenarioService.findOne(id);
|
||||
return { content: [{ type: 'text' as const, text: JSON.stringify(scenario) }] };
|
||||
return {
|
||||
content: [
|
||||
{ type: "text" as const, text: JSON.stringify(scenario) },
|
||||
],
|
||||
};
|
||||
} catch (err) {
|
||||
return { isError: true, content: [{ type: 'text' as const, text: (err as Error).message }] };
|
||||
return {
|
||||
isError: true,
|
||||
content: [{ type: "text" as const, text: (err as Error).message }],
|
||||
};
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
server.registerTool(
|
||||
'create_scenario',
|
||||
"create_scenario",
|
||||
{
|
||||
description: 'Create a new scenario',
|
||||
description: "Create a new scenario",
|
||||
inputSchema: {
|
||||
name: z.string().describe('Scenario name'),
|
||||
name: z.string().describe("Scenario name"),
|
||||
},
|
||||
},
|
||||
async ({ name }) => {
|
||||
try {
|
||||
const scenario = await this.scenarioService.create({ name });
|
||||
return { content: [{ type: 'text' as const, text: JSON.stringify(scenario) }] };
|
||||
return {
|
||||
content: [
|
||||
{ type: "text" as const, text: JSON.stringify(scenario) },
|
||||
],
|
||||
};
|
||||
} catch (err) {
|
||||
return { isError: true, content: [{ type: 'text' as const, text: (err as Error).message }] };
|
||||
return {
|
||||
isError: true,
|
||||
content: [{ type: "text" as const, text: (err as Error).message }],
|
||||
};
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
server.registerTool(
|
||||
'update_scenario',
|
||||
"update_scenario",
|
||||
{
|
||||
description: 'Update a scenario name',
|
||||
description: "Update a scenario name",
|
||||
inputSchema: {
|
||||
id: z.number().int().describe('Scenario ID'),
|
||||
name: z.string().optional().describe('New name'),
|
||||
id: z.number().int().describe("Scenario ID"),
|
||||
name: z.string().optional().describe("New name"),
|
||||
},
|
||||
},
|
||||
async ({ id, name }) => {
|
||||
try {
|
||||
const scenario = await this.scenarioService.update(id, { name });
|
||||
return { content: [{ type: 'text' as const, text: JSON.stringify(scenario) }] };
|
||||
return {
|
||||
content: [
|
||||
{ type: "text" as const, text: JSON.stringify(scenario) },
|
||||
],
|
||||
};
|
||||
} catch (err) {
|
||||
return { isError: true, content: [{ type: 'text' as const, text: (err as Error).message }] };
|
||||
return {
|
||||
isError: true,
|
||||
content: [{ type: "text" as const, text: (err as Error).message }],
|
||||
};
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
server.registerTool(
|
||||
'delete_scenario',
|
||||
"delete_scenario",
|
||||
{
|
||||
description: 'Delete a scenario by ID',
|
||||
description: "Delete a scenario by ID",
|
||||
inputSchema: {
|
||||
id: z.number().int().describe('Scenario ID'),
|
||||
id: z.number().int().describe("Scenario ID"),
|
||||
},
|
||||
},
|
||||
async ({ id }) => {
|
||||
try {
|
||||
await this.scenarioService.remove(id);
|
||||
return { content: [{ type: 'text' as const, text: `Scenario ${id} deleted` }] };
|
||||
return {
|
||||
content: [
|
||||
{ type: "text" as const, text: `Scenario ${id} deleted` },
|
||||
],
|
||||
};
|
||||
} catch (err) {
|
||||
return { isError: true, content: [{ type: 'text' as const, text: (err as Error).message }] };
|
||||
return {
|
||||
isError: true,
|
||||
content: [{ type: "text" as const, text: (err as Error).message }],
|
||||
};
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
server.registerTool(
|
||||
'create_scenario_step',
|
||||
"create_scenario_step",
|
||||
{
|
||||
description: 'Add a step to a scenario',
|
||||
description: "Add a step to a scenario",
|
||||
inputSchema: {
|
||||
scenarioId: z.number().int().describe('Parent scenario ID'),
|
||||
order: z.number().int().min(0).describe('Execution order (ascending)'),
|
||||
type: z.enum(['login', 'exec', 'sign']).describe('Step type'),
|
||||
sessionName: z.string().describe('Session name used by this step'),
|
||||
execCode: z.string().optional().describe('Playwright JS code to execute (exec steps)'),
|
||||
validateCode: z.string().optional().describe('Validation JS code returning { success, description }'),
|
||||
scenarioId: z.number().int().describe("Parent scenario ID"),
|
||||
order: z
|
||||
.number()
|
||||
.int()
|
||||
.min(0)
|
||||
.describe("Execution order (ascending)"),
|
||||
type: z.enum(["login", "exec", "sign"]).describe("Step type"),
|
||||
sessionName: z.string().describe("Session name used by this step"),
|
||||
execCode: z
|
||||
.string()
|
||||
.optional()
|
||||
.describe("Playwright JS code to execute (exec steps)"),
|
||||
validateCode: z
|
||||
.string()
|
||||
.optional()
|
||||
.describe("Validation JS code returning { success, description }"),
|
||||
},
|
||||
},
|
||||
async ({ scenarioId, ...dto }) => {
|
||||
try {
|
||||
const step = await this.scenarioService.createStep(scenarioId, dto);
|
||||
return { content: [{ type: 'text' as const, text: JSON.stringify(step) }] };
|
||||
return {
|
||||
content: [{ type: "text" as const, text: JSON.stringify(step) }],
|
||||
};
|
||||
} catch (err) {
|
||||
return { isError: true, content: [{ type: 'text' as const, text: (err as Error).message }] };
|
||||
return {
|
||||
isError: true,
|
||||
content: [{ type: "text" as const, text: (err as Error).message }],
|
||||
};
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
server.registerTool(
|
||||
'get_scenario_step',
|
||||
"get_scenario_step",
|
||||
{
|
||||
description: 'Get a single step of a scenario',
|
||||
description: "Get a single step of a scenario",
|
||||
inputSchema: {
|
||||
scenarioId: z.number().int().describe('Scenario ID'),
|
||||
stepId: z.number().int().describe('Step ID'),
|
||||
scenarioId: z.number().int().describe("Scenario ID"),
|
||||
stepId: z.number().int().describe("Step ID"),
|
||||
},
|
||||
},
|
||||
async ({ scenarioId, stepId }) => {
|
||||
try {
|
||||
const step = await this.scenarioService.findStep(scenarioId, stepId);
|
||||
return { content: [{ type: 'text' as const, text: JSON.stringify(step) }] };
|
||||
return {
|
||||
content: [{ type: "text" as const, text: JSON.stringify(step) }],
|
||||
};
|
||||
} catch (err) {
|
||||
return { isError: true, content: [{ type: 'text' as const, text: (err as Error).message }] };
|
||||
return {
|
||||
isError: true,
|
||||
content: [{ type: "text" as const, text: (err as Error).message }],
|
||||
};
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
server.registerTool(
|
||||
'update_scenario_step',
|
||||
"update_scenario_step",
|
||||
{
|
||||
description: 'Update a step within a scenario',
|
||||
description: "Update a step within a scenario",
|
||||
inputSchema: {
|
||||
scenarioId: z.number().int().describe('Scenario ID'),
|
||||
stepId: z.number().int().describe('Step ID'),
|
||||
order: z.number().int().min(0).optional().describe('New execution order'),
|
||||
type: z.enum(['login', 'exec', 'sign']).optional().describe('New step type'),
|
||||
sessionName: z.string().optional().describe('New session name'),
|
||||
execCode: z.string().optional().describe('New exec code'),
|
||||
validateCode: z.string().optional().describe('New validation code'),
|
||||
scenarioId: z.number().int().describe("Scenario ID"),
|
||||
stepId: z.number().int().describe("Step ID"),
|
||||
order: z
|
||||
.number()
|
||||
.int()
|
||||
.min(0)
|
||||
.optional()
|
||||
.describe("New execution order"),
|
||||
type: z
|
||||
.enum(["login", "exec", "sign"])
|
||||
.optional()
|
||||
.describe("New step type"),
|
||||
sessionName: z.string().optional().describe("New session name"),
|
||||
execCode: z.string().optional().describe("New exec code"),
|
||||
validateCode: z.string().optional().describe("New validation code"),
|
||||
},
|
||||
},
|
||||
async ({ scenarioId, stepId, ...dto }) => {
|
||||
try {
|
||||
const step = await this.scenarioService.updateStep(scenarioId, stepId, dto);
|
||||
return { content: [{ type: 'text' as const, text: JSON.stringify(step) }] };
|
||||
const step = await this.scenarioService.updateStep(
|
||||
scenarioId,
|
||||
stepId,
|
||||
dto,
|
||||
);
|
||||
return {
|
||||
content: [{ type: "text" as const, text: JSON.stringify(step) }],
|
||||
};
|
||||
} catch (err) {
|
||||
return { isError: true, content: [{ type: 'text' as const, text: (err as Error).message }] };
|
||||
return {
|
||||
isError: true,
|
||||
content: [{ type: "text" as const, text: (err as Error).message }],
|
||||
};
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
server.registerTool(
|
||||
'delete_scenario_step',
|
||||
"delete_scenario_step",
|
||||
{
|
||||
description: 'Delete a step from a scenario',
|
||||
description: "Delete a step from a scenario",
|
||||
inputSchema: {
|
||||
scenarioId: z.number().int().describe('Scenario ID'),
|
||||
stepId: z.number().int().describe('Step ID'),
|
||||
scenarioId: z.number().int().describe("Scenario ID"),
|
||||
stepId: z.number().int().describe("Step ID"),
|
||||
},
|
||||
},
|
||||
async ({ scenarioId, stepId }) => {
|
||||
try {
|
||||
await this.scenarioService.removeStep(scenarioId, stepId);
|
||||
return { content: [{ type: 'text' as const, text: `Step ${stepId} deleted from scenario ${scenarioId}` }] };
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: "text" as const,
|
||||
text: `Step ${stepId} deleted from scenario ${scenarioId}`,
|
||||
},
|
||||
],
|
||||
};
|
||||
} catch (err) {
|
||||
return { isError: true, content: [{ type: 'text' as const, text: (err as Error).message }] };
|
||||
return {
|
||||
isError: true,
|
||||
content: [{ type: "text" as const, text: (err as Error).message }],
|
||||
};
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
server.registerTool(
|
||||
'list_scenario_runs',
|
||||
"list_scenario_runs",
|
||||
{
|
||||
description: 'List runs for a scenario (paginated, optionally filtered by status)',
|
||||
description:
|
||||
"List runs for a scenario (paginated, optionally filtered by status)",
|
||||
inputSchema: {
|
||||
scenarioId: z.number().int().describe('Scenario ID'),
|
||||
status: z.enum(['pending', 'in_progress', 'pass', 'fail']).optional().describe('Filter by run status'),
|
||||
page: z.number().int().min(1).optional().describe('Page number (default 1)'),
|
||||
limit: z.number().int().min(1).optional().describe('Items per page (default 20)'),
|
||||
scenarioId: z.number().int().describe("Scenario ID"),
|
||||
status: z
|
||||
.enum(["pending", "in_progress", "pass", "fail"])
|
||||
.optional()
|
||||
.describe("Filter by run status"),
|
||||
page: z
|
||||
.number()
|
||||
.int()
|
||||
.min(1)
|
||||
.optional()
|
||||
.describe("Page number (default 1)"),
|
||||
limit: z
|
||||
.number()
|
||||
.int()
|
||||
.min(1)
|
||||
.optional()
|
||||
.describe("Items per page (default 20)"),
|
||||
},
|
||||
},
|
||||
async ({ scenarioId, status, page, limit }) => {
|
||||
try {
|
||||
const result = await this.scenarioService.findRuns(scenarioId, { status, page, limit });
|
||||
return { content: [{ type: 'text' as const, text: JSON.stringify(result) }] };
|
||||
const result = await this.scenarioService.findRuns(scenarioId, {
|
||||
status,
|
||||
page,
|
||||
limit,
|
||||
});
|
||||
return {
|
||||
content: [{ type: "text" as const, text: JSON.stringify(result) }],
|
||||
};
|
||||
} catch (err) {
|
||||
return { isError: true, content: [{ type: 'text' as const, text: (err as Error).message }] };
|
||||
return {
|
||||
isError: true,
|
||||
content: [{ type: "text" as const, text: (err as Error).message }],
|
||||
};
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
server.registerTool(
|
||||
'run_scenario',
|
||||
"run_scenario",
|
||||
{
|
||||
description: 'Trigger an immediate run of a scenario by ID',
|
||||
description: "Trigger an immediate run of a scenario by ID",
|
||||
inputSchema: {
|
||||
id: z.number().int().describe('Scenario ID to run'),
|
||||
id: z.number().int().describe("Scenario ID to run"),
|
||||
},
|
||||
},
|
||||
async ({ id }) => {
|
||||
try {
|
||||
const run = await this.scenarioService.createRun(id);
|
||||
return { content: [{ type: 'text' as const, text: JSON.stringify(run) }] };
|
||||
return {
|
||||
content: [{ type: "text" as const, text: JSON.stringify(run) }],
|
||||
};
|
||||
} catch (err) {
|
||||
return { isError: true, content: [{ type: 'text' as const, text: (err as Error).message }] };
|
||||
return {
|
||||
isError: true,
|
||||
content: [{ type: "text" as const, text: (err as Error).message }],
|
||||
};
|
||||
}
|
||||
},
|
||||
);
|
||||
@@ -455,7 +736,9 @@ export class McpService {
|
||||
|
||||
async handle(req: Request, res: Response): Promise<void> {
|
||||
const server = this.createServer();
|
||||
const transport = new StreamableHTTPServerTransport({ sessionIdGenerator: undefined });
|
||||
const transport = new StreamableHTTPServerTransport({
|
||||
sessionIdGenerator: undefined,
|
||||
});
|
||||
await server.connect(transport);
|
||||
try {
|
||||
await transport.handleRequest(req, res, req.body);
|
||||
|
||||
@@ -1,29 +1,43 @@
|
||||
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { IsIn, IsInt, IsNotEmpty, IsOptional, IsString, Min } from 'class-validator';
|
||||
import { StepType } from '../scenario-step.entity';
|
||||
import { ApiProperty, ApiPropertyOptional } from "@nestjs/swagger";
|
||||
import {
|
||||
IsIn,
|
||||
IsInt,
|
||||
IsNotEmpty,
|
||||
IsOptional,
|
||||
IsString,
|
||||
Min,
|
||||
} from "class-validator";
|
||||
import { StepType } from "../scenario-step.entity";
|
||||
|
||||
export class CreateScenarioStepDto {
|
||||
@ApiProperty({ example: 0, description: 'Execution order (ascending)' })
|
||||
@ApiProperty({ example: 0, description: "Execution order (ascending)" })
|
||||
@IsInt()
|
||||
@Min(0)
|
||||
order: number;
|
||||
|
||||
@ApiProperty({ enum: ['login', 'exec', 'sign'], example: 'exec' })
|
||||
@IsIn(['login', 'exec', 'sign'])
|
||||
@ApiProperty({ enum: ["login", "exec", "sign"], example: "exec" })
|
||||
@IsIn(["login", "exec", "sign"])
|
||||
type: StepType;
|
||||
|
||||
@ApiProperty({ description: 'Session name used by this step. login steps create it; exec steps consume it.', example: 'my-session' })
|
||||
@ApiProperty({
|
||||
description:
|
||||
"Session name used by this step. login steps create it; exec steps consume it.",
|
||||
example: "my-session",
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
sessionName: string;
|
||||
|
||||
@ApiPropertyOptional({ example: 'return await page.title();' })
|
||||
@ApiPropertyOptional({ example: "return await page.title();" })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
execCode?: string;
|
||||
|
||||
@ApiPropertyOptional({ example: 'return { success: result !== null, description: "title present" };' })
|
||||
@ApiPropertyOptional({
|
||||
example:
|
||||
'return { success: result !== null, description: "title present" };',
|
||||
})
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsNotEmpty, IsString } from 'class-validator';
|
||||
import { ApiProperty } from "@nestjs/swagger";
|
||||
import { IsNotEmpty, IsString } from "class-validator";
|
||||
|
||||
export class CreateScenarioDto {
|
||||
@ApiProperty({ example: 'Login and verify cabinet' })
|
||||
@ApiProperty({ example: "Login and verify cabinet" })
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
name: string;
|
||||
|
||||
@@ -1 +1 @@
|
||||
export { PaginationQueryDto } from '../../common/dto/pagination.dto';
|
||||
export { PaginationQueryDto } from "../../common/dto/pagination.dto";
|
||||
@@ -1,7 +1,7 @@
|
||||
import { ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { Type } from 'class-transformer';
|
||||
import { IsIn, IsInt, IsOptional, Min } from 'class-validator';
|
||||
import { RunStatus } from '../scenario-run.entity';
|
||||
import { ApiPropertyOptional } from "@nestjs/swagger";
|
||||
import { Type } from "class-transformer";
|
||||
import { IsIn, IsInt, IsOptional, Min } from "class-validator";
|
||||
import { RunStatus } from "../scenario-run.entity";
|
||||
|
||||
export class RunsQueryDto {
|
||||
@ApiPropertyOptional({ example: 1, default: 1 })
|
||||
@@ -19,10 +19,10 @@ export class RunsQueryDto {
|
||||
limit?: number = 20;
|
||||
|
||||
@ApiPropertyOptional({
|
||||
enum: ['pending', 'in_progress', 'pass', 'fail'],
|
||||
description: 'Filter by run status',
|
||||
enum: ["pending", "in_progress", "pass", "fail"],
|
||||
description: "Filter by run status",
|
||||
})
|
||||
@IsOptional()
|
||||
@IsIn(['pending', 'in_progress', 'pass', 'fail'])
|
||||
@IsIn(["pending", "in_progress", "pass", "fail"])
|
||||
status?: RunStatus;
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { Type } from 'class-transformer';
|
||||
import { ApiProperty, ApiPropertyOptional } from "@nestjs/swagger";
|
||||
import { Type } from "class-transformer";
|
||||
import {
|
||||
IsArray,
|
||||
IsIn,
|
||||
@@ -9,8 +9,8 @@ import {
|
||||
IsString,
|
||||
Min,
|
||||
ValidateNested,
|
||||
} from 'class-validator';
|
||||
import { StepType } from '../scenario-step.entity';
|
||||
} from "class-validator";
|
||||
import { StepType } from "../scenario-step.entity";
|
||||
|
||||
export class ScenarioStepExportDto {
|
||||
@ApiProperty()
|
||||
@@ -18,8 +18,8 @@ export class ScenarioStepExportDto {
|
||||
@Min(0)
|
||||
order: number;
|
||||
|
||||
@ApiProperty({ enum: ['login', 'exec', 'sign'] })
|
||||
@IsIn(['login', 'exec', 'sign'])
|
||||
@ApiProperty({ enum: ["login", "exec", "sign"] })
|
||||
@IsIn(["login", "exec", "sign"])
|
||||
type: StepType;
|
||||
|
||||
@ApiProperty()
|
||||
|
||||
@@ -1,6 +1,13 @@
|
||||
import { ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { IsIn, IsInt, IsNotEmpty, IsOptional, IsString, Min } from 'class-validator';
|
||||
import { StepType } from '../scenario-step.entity';
|
||||
import { ApiPropertyOptional } from "@nestjs/swagger";
|
||||
import {
|
||||
IsIn,
|
||||
IsInt,
|
||||
IsNotEmpty,
|
||||
IsOptional,
|
||||
IsString,
|
||||
Min,
|
||||
} from "class-validator";
|
||||
import { StepType } from "../scenario-step.entity";
|
||||
|
||||
export class UpdateScenarioStepDto {
|
||||
@ApiPropertyOptional({ example: 0 })
|
||||
@@ -9,9 +16,9 @@ export class UpdateScenarioStepDto {
|
||||
@Min(0)
|
||||
order?: number;
|
||||
|
||||
@ApiPropertyOptional({ enum: ['login', 'exec', 'sign'] })
|
||||
@ApiPropertyOptional({ enum: ["login", "exec", "sign"] })
|
||||
@IsOptional()
|
||||
@IsIn(['login', 'exec', 'sign'])
|
||||
@IsIn(["login", "exec", "sign"])
|
||||
type?: StepType;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { IsOptional, IsString, IsNotEmpty } from 'class-validator';
|
||||
import { ApiPropertyOptional } from "@nestjs/swagger";
|
||||
import { IsOptional, IsString, IsNotEmpty } from "class-validator";
|
||||
|
||||
export class UpdateScenarioDto {
|
||||
@ApiPropertyOptional({ example: 'Updated scenario name' })
|
||||
@ApiPropertyOptional({ example: "Updated scenario name" })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
import {
|
||||
Entity,
|
||||
PrimaryGeneratedColumn,
|
||||
Column,
|
||||
CreateDateColumn,
|
||||
ManyToOne,
|
||||
JoinColumn,
|
||||
} from "typeorm";
|
||||
import { ScenarioRunEntity } from "./scenario-run.entity";
|
||||
import { ScenarioRunStepEntity } from "./scenario-run-step.entity";
|
||||
|
||||
export type LogLevel = "log" | "warn" | "error";
|
||||
|
||||
@Entity("scenario_run_logs")
|
||||
export class ScenarioRunLogEntity {
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
runId: number;
|
||||
|
||||
@ManyToOne(() => ScenarioRunEntity, { onDelete: "CASCADE" })
|
||||
@JoinColumn({ name: "runId" })
|
||||
run: ScenarioRunEntity;
|
||||
|
||||
@Column({ nullable: true })
|
||||
stepRunId: number | null;
|
||||
|
||||
@ManyToOne(() => ScenarioRunStepEntity, {
|
||||
onDelete: "SET NULL",
|
||||
nullable: true,
|
||||
})
|
||||
@JoinColumn({ name: "stepRunId" })
|
||||
stepRun: ScenarioRunStepEntity | null;
|
||||
|
||||
@Column({ type: "text", default: "log" })
|
||||
level: LogLevel;
|
||||
|
||||
@Column({ type: "text" })
|
||||
message: string;
|
||||
|
||||
@CreateDateColumn()
|
||||
createdAt: Date;
|
||||
}
|
||||
@@ -6,13 +6,19 @@ import {
|
||||
UpdateDateColumn,
|
||||
ManyToOne,
|
||||
JoinColumn,
|
||||
} from 'typeorm';
|
||||
import { ScenarioRunEntity } from './scenario-run.entity';
|
||||
import { ScenarioStepEntity } from './scenario-step.entity';
|
||||
} from "typeorm";
|
||||
import { ScenarioRunEntity } from "./scenario-run.entity";
|
||||
import { ScenarioStepEntity } from "./scenario-step.entity";
|
||||
|
||||
export type RunStepStatus = 'waiting' | 'pending' | 'in_progress' | 'pass' | 'fail' | 'cancelled';
|
||||
export type RunStepStatus =
|
||||
| "waiting"
|
||||
| "pending"
|
||||
| "in_progress"
|
||||
| "pass"
|
||||
| "fail"
|
||||
| "cancelled";
|
||||
|
||||
@Entity('scenario_run_steps')
|
||||
@Entity("scenario_run_steps")
|
||||
export class ScenarioRunStepEntity {
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
@@ -20,24 +26,26 @@ export class ScenarioRunStepEntity {
|
||||
@Column()
|
||||
runId: number;
|
||||
|
||||
@ManyToOne(() => ScenarioRunEntity, (run) => run.stepRuns, { onDelete: 'CASCADE' })
|
||||
@JoinColumn({ name: 'runId' })
|
||||
@ManyToOne(() => ScenarioRunEntity, (run) => run.stepRuns, {
|
||||
onDelete: "CASCADE",
|
||||
})
|
||||
@JoinColumn({ name: "runId" })
|
||||
run: ScenarioRunEntity;
|
||||
|
||||
@Column()
|
||||
scenarioStepId: number;
|
||||
|
||||
@ManyToOne(() => ScenarioStepEntity, { onDelete: 'CASCADE' })
|
||||
@JoinColumn({ name: 'scenarioStepId' })
|
||||
@ManyToOne(() => ScenarioStepEntity, { onDelete: "CASCADE" })
|
||||
@JoinColumn({ name: "scenarioStepId" })
|
||||
scenarioStep: ScenarioStepEntity;
|
||||
|
||||
@Column({ type: 'text', default: 'waiting' })
|
||||
@Column({ type: "text", default: "waiting" })
|
||||
status: RunStepStatus;
|
||||
|
||||
@Column({ default: 0 })
|
||||
order: number;
|
||||
|
||||
@Column({ type: 'text', nullable: true })
|
||||
@Column({ type: "text", nullable: true })
|
||||
description: string | null;
|
||||
|
||||
@CreateDateColumn()
|
||||
|
||||
@@ -7,13 +7,13 @@ import {
|
||||
ManyToOne,
|
||||
OneToMany,
|
||||
JoinColumn,
|
||||
} from 'typeorm';
|
||||
import { ScenarioEntity } from './scenario.entity';
|
||||
import { ScenarioRunStepEntity } from './scenario-run-step.entity';
|
||||
} from "typeorm";
|
||||
import { ScenarioEntity } from "./scenario.entity";
|
||||
import { ScenarioRunStepEntity } from "./scenario-run-step.entity";
|
||||
|
||||
export type RunStatus = 'pending' | 'in_progress' | 'pass' | 'fail';
|
||||
export type RunStatus = "pending" | "in_progress" | "pass" | "fail";
|
||||
|
||||
@Entity('scenario_runs')
|
||||
@Entity("scenario_runs")
|
||||
export class ScenarioRunEntity {
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
@@ -21,11 +21,11 @@ export class ScenarioRunEntity {
|
||||
@Column()
|
||||
scenarioId: number;
|
||||
|
||||
@ManyToOne(() => ScenarioEntity, { onDelete: 'CASCADE' })
|
||||
@JoinColumn({ name: 'scenarioId' })
|
||||
@ManyToOne(() => ScenarioEntity, { onDelete: "CASCADE" })
|
||||
@JoinColumn({ name: "scenarioId" })
|
||||
scenario: ScenarioEntity;
|
||||
|
||||
@Column({ type: 'text', default: 'pending' })
|
||||
@Column({ type: "text", default: "pending" })
|
||||
status: RunStatus;
|
||||
|
||||
@OneToMany(() => ScenarioRunStepEntity, (rs) => rs.run, {
|
||||
|
||||
@@ -1,19 +1,20 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { TraceLogger } from '../common/trace-logger';
|
||||
import { traceStorage } from '../common/trace-context';
|
||||
import { Interval } from '@nestjs/schedule';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import * as crypto from 'crypto';
|
||||
import { chromium } from 'playwright';
|
||||
import type { Browser, BrowserContext, Page } from 'playwright';
|
||||
import { ScenarioRunEntity } from './scenario-run.entity';
|
||||
import { ScenarioRunStepEntity } from './scenario-run-step.entity';
|
||||
import { ScenarioStepEntity } from './scenario-step.entity';
|
||||
import type { ScriptLogger } from '../code-executor/code-executor.service';
|
||||
import { AuthService } from '../auth/auth.service';
|
||||
import { CodeExecutorService } from '../code-executor/code-executor.service';
|
||||
import { SessionService } from '../session/session.service';
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { TraceLogger } from "../common/trace-logger";
|
||||
import { traceStorage } from "../common/trace-context";
|
||||
import { Interval } from "@nestjs/schedule";
|
||||
import { InjectRepository } from "@nestjs/typeorm";
|
||||
import { Repository } from "typeorm";
|
||||
import * as crypto from "crypto";
|
||||
import { chromium } from "playwright";
|
||||
import type { Browser, BrowserContext, Page } from "playwright";
|
||||
import { ScenarioRunEntity } from "./scenario-run.entity";
|
||||
import { ScenarioRunStepEntity } from "./scenario-run-step.entity";
|
||||
import { ScenarioRunLogEntity } from "./scenario-run-log.entity";
|
||||
import { ScenarioStepEntity } from "./scenario-step.entity";
|
||||
import type { ScriptLogger } from "../code-executor/code-executor.service";
|
||||
import { AuthService } from "../auth/auth.service";
|
||||
import { CodeExecutorService } from "../code-executor/code-executor.service";
|
||||
import { SessionService } from "../session/session.service";
|
||||
|
||||
interface ValidateResult {
|
||||
success: boolean;
|
||||
@@ -37,49 +38,69 @@ export class ScenarioSchedulerService {
|
||||
private readonly runRepo: Repository<ScenarioRunEntity>,
|
||||
@InjectRepository(ScenarioRunStepEntity)
|
||||
private readonly runStepRepo: Repository<ScenarioRunStepEntity>,
|
||||
@InjectRepository(ScenarioRunLogEntity)
|
||||
private readonly runLogRepo: Repository<ScenarioRunLogEntity>,
|
||||
private readonly authService: AuthService,
|
||||
private readonly codeExecutor: CodeExecutorService,
|
||||
private readonly sessionService: SessionService,
|
||||
) {}
|
||||
|
||||
private stepLogger(stepRunId: number): ScriptLogger {
|
||||
return (level, msg) => this.logger[level](`StepRun #${stepRunId} script: ${msg}`);
|
||||
private persistLog(
|
||||
runId: number,
|
||||
stepRunId: number | null,
|
||||
level: "log" | "warn" | "error",
|
||||
message: string,
|
||||
): void {
|
||||
void this.runLogRepo.save(
|
||||
this.runLogRepo.create({ runId, stepRunId, level, message }),
|
||||
);
|
||||
}
|
||||
|
||||
private stepLogger(stepRunId: number, runId: number): ScriptLogger {
|
||||
return (level, msg) => {
|
||||
this.logger[level](`StepRun #${stepRunId} script: ${msg}`);
|
||||
this.persistLog(runId, stepRunId, level, msg);
|
||||
};
|
||||
}
|
||||
|
||||
// ── Job: pick up pending runs and process each to completion ─────────────
|
||||
|
||||
@Interval(1000)
|
||||
async pickUpPendingRuns(): Promise<void> {
|
||||
const pending = await this.runRepo.find({ where: { status: 'pending' } });
|
||||
const pending = await this.runRepo.find({ where: { status: "pending" } });
|
||||
for (const run of pending) {
|
||||
if (this.activeRuns.has(run.id)) continue;
|
||||
this.activeRuns.add(run.id);
|
||||
run.status = 'in_progress';
|
||||
run.status = "in_progress";
|
||||
await this.runRepo.save(run);
|
||||
this.logger.log(`Run #${run.id} → in_progress`);
|
||||
const traceId = crypto.randomUUID();
|
||||
void traceStorage.run({ traceId }, () => this.processRunToCompletion(run.id));
|
||||
void traceStorage.run({ traceId }, () =>
|
||||
this.processRunToCompletion(run.id),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private async processRunToCompletion(runId: number): Promise<void> {
|
||||
try {
|
||||
let stepRun = await this.runStepRepo.findOne({
|
||||
where: { runId, status: 'pending' },
|
||||
relations: ['scenarioStep'],
|
||||
order: { order: 'ASC' },
|
||||
where: { runId, status: "pending" },
|
||||
relations: ["scenarioStep"],
|
||||
order: { order: "ASC" },
|
||||
});
|
||||
while (stepRun) {
|
||||
await this.executeStepRun(stepRun);
|
||||
stepRun = await this.runStepRepo.findOne({
|
||||
where: { runId, status: 'pending' },
|
||||
relations: ['scenarioStep'],
|
||||
order: { order: 'ASC' },
|
||||
where: { runId, status: "pending" },
|
||||
relations: ["scenarioStep"],
|
||||
order: { order: "ASC" },
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
this.logger.error(`Run #${runId}: unexpected error: ${(err as Error).message}`);
|
||||
await this.runRepo.update(runId, { status: 'fail' });
|
||||
this.logger.error(
|
||||
`Run #${runId}: unexpected error: ${(err as Error).message}`,
|
||||
);
|
||||
await this.runRepo.update(runId, { status: "fail" });
|
||||
} finally {
|
||||
this.activeRuns.delete(runId);
|
||||
}
|
||||
@@ -88,14 +109,16 @@ export class ScenarioSchedulerService {
|
||||
private async executeStepRun(stepRun: ScenarioRunStepEntity): Promise<void> {
|
||||
const step = stepRun.scenarioStep as ScenarioStepEntity;
|
||||
|
||||
stepRun.status = 'in_progress';
|
||||
stepRun.status = "in_progress";
|
||||
await this.runStepRepo.save(stepRun);
|
||||
this.logger.log(`StepRun #${stepRun.id} (run #${stepRun.runId}, step #${step.id} order=${step.order}) → in_progress`);
|
||||
this.logger.log(
|
||||
`StepRun #${stepRun.id} (run #${stepRun.runId}, step #${step.id} order=${step.order}) → in_progress`,
|
||||
);
|
||||
|
||||
try {
|
||||
if (step.type === 'login') {
|
||||
if (step.type === "login") {
|
||||
await this.executeLoginStep(stepRun, step);
|
||||
} else if (step.type === 'sign') {
|
||||
} else if (step.type === "sign") {
|
||||
await this.executeSignStep(stepRun, step);
|
||||
} else {
|
||||
await this.executeExecStep(stepRun, step);
|
||||
@@ -109,24 +132,33 @@ export class ScenarioSchedulerService {
|
||||
|
||||
// ── Shared browser per run ─────────────────────────────────────────────────
|
||||
|
||||
private async getOrCreateBrowserHandle(runId: number, sessionName: string): Promise<BrowserHandle> {
|
||||
private async getOrCreateBrowserHandle(
|
||||
runId: number,
|
||||
sessionName: string,
|
||||
): Promise<BrowserHandle> {
|
||||
const existing = this.runBrowsers.get(runId);
|
||||
if (existing) return existing;
|
||||
|
||||
const session = await this.sessionService.findBySessionName(sessionName);
|
||||
if (!session) throw new Error(`Session not found: ${sessionName}`);
|
||||
|
||||
const cookies = JSON.parse(session.cookies) as Parameters<BrowserContext['addCookies']>[0];
|
||||
const localStorageData: Record<string, string> = JSON.parse(session.localStorage);
|
||||
const cookies = JSON.parse(session.cookies) as Parameters<
|
||||
BrowserContext["addCookies"]
|
||||
>[0];
|
||||
const localStorageData: Record<string, string> = JSON.parse(
|
||||
session.localStorage,
|
||||
);
|
||||
|
||||
const browser = await chromium.launch({
|
||||
headless: true,
|
||||
// TODO: env var move to config
|
||||
executablePath: process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH,
|
||||
});
|
||||
const context = await browser.newContext();
|
||||
await context.addCookies(cookies);
|
||||
await context.addInitScript((entries: Record<string, string>) => {
|
||||
for (const [k, v] of Object.entries(entries)) window.localStorage.setItem(k, v);
|
||||
for (const [k, v] of Object.entries(entries))
|
||||
window.localStorage.setItem(k, v);
|
||||
}, localStorageData);
|
||||
const page = await context.newPage();
|
||||
|
||||
@@ -144,33 +176,56 @@ export class ScenarioSchedulerService {
|
||||
await handle.browser.close();
|
||||
this.logger.log(`Run #${runId}: browser closed`);
|
||||
} catch (err) {
|
||||
this.logger.warn(`Run #${runId}: error closing browser: ${(err as Error).message}`);
|
||||
this.logger.warn(
|
||||
`Run #${runId}: error closing browser: ${(err as Error).message}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// ── Login step ─────────────────────────────────────────────────────────────
|
||||
|
||||
private async executeLoginStep(stepRun: ScenarioRunStepEntity, step: ScenarioStepEntity): Promise<void> {
|
||||
private async executeLoginStep(
|
||||
stepRun: ScenarioRunStepEntity,
|
||||
step: ScenarioStepEntity,
|
||||
): Promise<void> {
|
||||
// execCode must be a JSON object: { "keyId": "...", "environmentName": "..." }
|
||||
let params: { keyId: string; environmentName: string };
|
||||
try {
|
||||
params = JSON.parse(step.execCode ?? '{}');
|
||||
params = JSON.parse(step.execCode ?? "{}");
|
||||
} catch {
|
||||
throw new Error('login step execCode must be valid JSON with keyId and environmentName');
|
||||
throw new Error(
|
||||
"login step execCode must be valid JSON with keyId and environmentName",
|
||||
);
|
||||
}
|
||||
if (!params.keyId || !params.environmentName) {
|
||||
throw new Error('login step execCode must include keyId and environmentName');
|
||||
throw new Error(
|
||||
"login step execCode must include keyId and environmentName",
|
||||
);
|
||||
}
|
||||
|
||||
const loginResult = await this.authService.login(params.keyId, params.environmentName, step.sessionName);
|
||||
this.logger.log(`StepRun #${stepRun.id}: login OK, session=${loginResult.sessionName}`);
|
||||
const loginResult = await this.authService.login(
|
||||
params.keyId,
|
||||
params.environmentName,
|
||||
step.sessionName,
|
||||
);
|
||||
this.logger.log(
|
||||
`StepRun #${stepRun.id}: login OK, session=${loginResult.sessionName}`,
|
||||
);
|
||||
|
||||
if (step.validateCode) {
|
||||
const { page, context } = await this.getOrCreateBrowserHandle(stepRun.runId, step.sessionName);
|
||||
const { page, context } = await this.getOrCreateBrowserHandle(
|
||||
stepRun.runId,
|
||||
step.sessionName,
|
||||
);
|
||||
this.codeExecutor.validate(step.validateCode);
|
||||
const { result } = await this.codeExecutor.execute(page, context, step.validateCode, this.stepLogger(stepRun.id));
|
||||
const { result } = await this.codeExecutor.execute(
|
||||
page,
|
||||
context,
|
||||
step.validateCode,
|
||||
this.stepLogger(stepRun.id, stepRun.runId),
|
||||
);
|
||||
const vr = this.parseValidateResult(result);
|
||||
if (!vr.success) throw new Error(vr.description ?? 'Validation failed');
|
||||
if (!vr.success) throw new Error(vr.description ?? "Validation failed");
|
||||
}
|
||||
|
||||
await this.passStepRun(stepRun, null);
|
||||
@@ -178,19 +233,35 @@ export class ScenarioSchedulerService {
|
||||
|
||||
// ── Exec step ──────────────────────────────────────────────────────────────
|
||||
|
||||
private async executeExecStep(stepRun: ScenarioRunStepEntity, step: ScenarioStepEntity): Promise<void> {
|
||||
if (!step.execCode) throw new Error('exec step has no execCode');
|
||||
private async executeExecStep(
|
||||
stepRun: ScenarioRunStepEntity,
|
||||
step: ScenarioStepEntity,
|
||||
): Promise<void> {
|
||||
if (!step.execCode) throw new Error("exec step has no execCode");
|
||||
|
||||
this.codeExecutor.validate(step.execCode);
|
||||
const { page, context } = await this.getOrCreateBrowserHandle(stepRun.runId, step.sessionName);
|
||||
await this.codeExecutor.execute(page, context, step.execCode, this.stepLogger(stepRun.id));
|
||||
const { page, context } = await this.getOrCreateBrowserHandle(
|
||||
stepRun.runId,
|
||||
step.sessionName,
|
||||
);
|
||||
await this.codeExecutor.execute(
|
||||
page,
|
||||
context,
|
||||
step.execCode,
|
||||
this.stepLogger(stepRun.id, stepRun.runId),
|
||||
);
|
||||
this.logger.log(`StepRun #${stepRun.id}: exec OK`);
|
||||
|
||||
if (step.validateCode) {
|
||||
this.codeExecutor.validate(step.validateCode);
|
||||
const { result } = await this.codeExecutor.execute(page, context, step.validateCode, this.stepLogger(stepRun.id));
|
||||
const { result } = await this.codeExecutor.execute(
|
||||
page,
|
||||
context,
|
||||
step.validateCode,
|
||||
this.stepLogger(stepRun.id, stepRun.runId),
|
||||
);
|
||||
const vr = this.parseValidateResult(result);
|
||||
if (!vr.success) throw new Error(vr.description ?? 'Validation failed');
|
||||
if (!vr.success) throw new Error(vr.description ?? "Validation failed");
|
||||
}
|
||||
|
||||
await this.passStepRun(stepRun, null);
|
||||
@@ -198,25 +269,36 @@ export class ScenarioSchedulerService {
|
||||
|
||||
// ── Sign step ──────────────────────────────────────────────────────────────
|
||||
|
||||
private async executeSignStep(stepRun: ScenarioRunStepEntity, step: ScenarioStepEntity): Promise<void> {
|
||||
private async executeSignStep(
|
||||
stepRun: ScenarioRunStepEntity,
|
||||
step: ScenarioStepEntity,
|
||||
): Promise<void> {
|
||||
// execCode must be a JSON object: { "keyId": "..." }
|
||||
let params: { keyId: string };
|
||||
try {
|
||||
params = JSON.parse(step.execCode ?? '{}');
|
||||
params = JSON.parse(step.execCode ?? "{}");
|
||||
} catch {
|
||||
throw new Error('sign step execCode must be valid JSON with keyId');
|
||||
throw new Error("sign step execCode must be valid JSON with keyId");
|
||||
}
|
||||
if (!params.keyId) throw new Error('sign step execCode must include keyId');
|
||||
if (!params.keyId) throw new Error("sign step execCode must include keyId");
|
||||
|
||||
const { page, context } = await this.getOrCreateBrowserHandle(stepRun.runId, step.sessionName);
|
||||
const { page, context } = await this.getOrCreateBrowserHandle(
|
||||
stepRun.runId,
|
||||
step.sessionName,
|
||||
);
|
||||
await this.authService.signWithKey(params.keyId, page);
|
||||
this.logger.log(`StepRun #${stepRun.id}: sign OK`);
|
||||
|
||||
if (step.validateCode) {
|
||||
this.codeExecutor.validate(step.validateCode);
|
||||
const { result } = await this.codeExecutor.execute(page, context, step.validateCode, this.stepLogger(stepRun.id));
|
||||
const { result } = await this.codeExecutor.execute(
|
||||
page,
|
||||
context,
|
||||
step.validateCode,
|
||||
this.stepLogger(stepRun.id, stepRun.runId),
|
||||
);
|
||||
const vr = this.parseValidateResult(result);
|
||||
if (!vr.success) throw new Error(vr.description ?? 'Validation failed');
|
||||
if (!vr.success) throw new Error(vr.description ?? "Validation failed");
|
||||
}
|
||||
|
||||
await this.passStepRun(stepRun, null);
|
||||
@@ -225,12 +307,13 @@ export class ScenarioSchedulerService {
|
||||
// ── Validation helper ──────────────────────────────────────────────────────
|
||||
|
||||
private parseValidateResult(raw: unknown): ValidateResult {
|
||||
if (typeof raw === 'boolean') return { success: raw };
|
||||
if (raw && typeof raw === 'object') {
|
||||
if (typeof raw === "boolean") return { success: raw };
|
||||
if (raw && typeof raw === "object") {
|
||||
const r = raw as Record<string, unknown>;
|
||||
return {
|
||||
success: Boolean(r['success']),
|
||||
description: r['description'] != null ? String(r['description']) : undefined,
|
||||
success: Boolean(r["success"]),
|
||||
description:
|
||||
r["description"] != null ? String(r["description"]) : undefined,
|
||||
};
|
||||
}
|
||||
return { success: Boolean(raw) };
|
||||
@@ -238,40 +321,46 @@ export class ScenarioSchedulerService {
|
||||
|
||||
// ── Pass / fail helpers ────────────────────────────────────────────────────
|
||||
|
||||
private async passStepRun(stepRun: ScenarioRunStepEntity, description: string | null): Promise<void> {
|
||||
stepRun.status = 'pass';
|
||||
private async passStepRun(
|
||||
stepRun: ScenarioRunStepEntity,
|
||||
description: string | null,
|
||||
): Promise<void> {
|
||||
stepRun.status = "pass";
|
||||
stepRun.description = description;
|
||||
await this.runStepRepo.save(stepRun);
|
||||
this.logger.log(`StepRun #${stepRun.id} → pass`);
|
||||
|
||||
// Find the next waiting step in this run (next by order)
|
||||
const nextStep = await this.runStepRepo.findOne({
|
||||
where: { runId: stepRun.runId, status: 'waiting' },
|
||||
order: { order: 'ASC' },
|
||||
where: { runId: stepRun.runId, status: "waiting" },
|
||||
order: { order: "ASC" },
|
||||
});
|
||||
|
||||
if (nextStep) {
|
||||
nextStep.status = 'pending';
|
||||
nextStep.status = "pending";
|
||||
await this.runStepRepo.save(nextStep);
|
||||
} else {
|
||||
// No more waiting steps — check if any are still in_progress/pending (shouldn't be, but guard anyway)
|
||||
const remaining = await this.runStepRepo.count({
|
||||
where: [
|
||||
{ runId: stepRun.runId, status: 'pending' },
|
||||
{ runId: stepRun.runId, status: 'in_progress' },
|
||||
{ runId: stepRun.runId, status: 'waiting' },
|
||||
{ runId: stepRun.runId, status: "pending" },
|
||||
{ runId: stepRun.runId, status: "in_progress" },
|
||||
{ runId: stepRun.runId, status: "waiting" },
|
||||
],
|
||||
});
|
||||
if (remaining === 0) {
|
||||
await this.closeBrowserHandle(stepRun.runId);
|
||||
await this.runRepo.update(stepRun.runId, { status: 'pass' });
|
||||
await this.runRepo.update(stepRun.runId, { status: "pass" });
|
||||
this.logger.log(`Run #${stepRun.runId} → pass (all steps passed)`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async failStepRun(stepRun: ScenarioRunStepEntity, description: string): Promise<void> {
|
||||
stepRun.status = 'fail';
|
||||
private async failStepRun(
|
||||
stepRun: ScenarioRunStepEntity,
|
||||
description: string,
|
||||
): Promise<void> {
|
||||
stepRun.status = "fail";
|
||||
stepRun.description = description;
|
||||
await this.runStepRepo.save(stepRun);
|
||||
this.logger.log(`StepRun #${stepRun.id} → fail: ${description}`);
|
||||
@@ -280,17 +369,17 @@ export class ScenarioSchedulerService {
|
||||
await this.runStepRepo
|
||||
.createQueryBuilder()
|
||||
.update()
|
||||
.set({ status: 'cancelled' })
|
||||
.where('runId = :runId AND status IN (:...statuses)', {
|
||||
.set({ status: "cancelled" })
|
||||
.where("runId = :runId AND status IN (:...statuses)", {
|
||||
runId: stepRun.runId,
|
||||
statuses: ['waiting', 'pending'],
|
||||
statuses: ["waiting", "pending"],
|
||||
})
|
||||
.execute();
|
||||
|
||||
this.logger.log(`Run #${stepRun.runId}: remaining steps cancelled`);
|
||||
|
||||
await this.closeBrowserHandle(stepRun.runId);
|
||||
await this.runRepo.update(stepRun.runId, { status: 'fail' });
|
||||
await this.runRepo.update(stepRun.runId, { status: "fail" });
|
||||
this.logger.log(`Run #${stepRun.runId} → fail`);
|
||||
}
|
||||
}
|
||||
@@ -6,12 +6,12 @@ import {
|
||||
UpdateDateColumn,
|
||||
ManyToOne,
|
||||
JoinColumn,
|
||||
} from 'typeorm';
|
||||
import { ScenarioEntity } from './scenario.entity';
|
||||
} from "typeorm";
|
||||
import { ScenarioEntity } from "./scenario.entity";
|
||||
|
||||
export type StepType = 'login' | 'exec' | 'sign';
|
||||
export type StepType = "login" | "exec" | "sign";
|
||||
|
||||
@Entity('scenario_steps')
|
||||
@Entity("scenario_steps")
|
||||
export class ScenarioStepEntity {
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
@@ -20,24 +20,24 @@ export class ScenarioStepEntity {
|
||||
scenarioId: number;
|
||||
|
||||
@ManyToOne(() => ScenarioEntity, (scenario) => scenario.steps, {
|
||||
onDelete: 'CASCADE',
|
||||
onDelete: "CASCADE",
|
||||
})
|
||||
@JoinColumn({ name: 'scenarioId' })
|
||||
@JoinColumn({ name: "scenarioId" })
|
||||
scenario: ScenarioEntity;
|
||||
|
||||
@Column({ default: 0 })
|
||||
order: number;
|
||||
|
||||
@Column({ type: 'text' })
|
||||
@Column({ type: "text" })
|
||||
type: StepType;
|
||||
|
||||
@Column({ type: 'text' })
|
||||
@Column({ type: "text" })
|
||||
sessionName: string;
|
||||
|
||||
@Column({ type: 'text', nullable: true })
|
||||
@Column({ type: "text", nullable: true })
|
||||
execCode: string | null;
|
||||
|
||||
@Column({ type: 'text', nullable: true })
|
||||
@Column({ type: "text", nullable: true })
|
||||
validateCode: string | null;
|
||||
|
||||
@CreateDateColumn()
|
||||
|
||||
@@ -9,145 +9,176 @@ import {
|
||||
Patch,
|
||||
Post,
|
||||
Query,
|
||||
} from '@nestjs/common';
|
||||
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
|
||||
import { ScenarioService } from './scenario.service';
|
||||
import { CreateScenarioDto } from './dto/create-scenario.dto';
|
||||
import { UpdateScenarioDto } from './dto/update-scenario.dto';
|
||||
import { CreateScenarioStepDto } from './dto/create-scenario-step.dto';
|
||||
import { UpdateScenarioStepDto } from './dto/update-scenario-step.dto';
|
||||
import { PaginationQueryDto } from './dto/pagination-query.dto';
|
||||
import { ScenarioOrderBy } from './scenario.service';
|
||||
import { RunsQueryDto } from './dto/runs-query.dto';
|
||||
import { ScenarioExportDto } from './dto/scenario-export.dto';
|
||||
} from "@nestjs/common";
|
||||
import { ApiOperation, ApiResponse, ApiTags } from "@nestjs/swagger";
|
||||
import { ScenarioService } from "./scenario.service";
|
||||
import { CreateScenarioDto } from "./dto/create-scenario.dto";
|
||||
import { UpdateScenarioDto } from "./dto/update-scenario.dto";
|
||||
import { CreateScenarioStepDto } from "./dto/create-scenario-step.dto";
|
||||
import { UpdateScenarioStepDto } from "./dto/update-scenario-step.dto";
|
||||
import { PaginationQueryDto } from "./dto/pagination-query.dto";
|
||||
import { ScenarioOrderBy } from "./scenario.service";
|
||||
import { RunsQueryDto } from "./dto/runs-query.dto";
|
||||
import { ScenarioExportDto } from "./dto/scenario-export.dto";
|
||||
|
||||
@ApiTags('scenarios')
|
||||
@Controller('scenarios')
|
||||
@ApiTags("scenarios")
|
||||
@Controller("scenarios")
|
||||
export class ScenarioController {
|
||||
constructor(private readonly scenarioService: ScenarioService) {}
|
||||
|
||||
// ── Scenarios ─────────────────────────────────────────────────────────────
|
||||
|
||||
@Post()
|
||||
@ApiOperation({ summary: 'Create a scenario' })
|
||||
@ApiResponse({ status: 201, description: 'Scenario created' })
|
||||
@ApiOperation({ summary: "Create a scenario" })
|
||||
@ApiResponse({ status: 201, description: "Scenario created" })
|
||||
create(@Body() dto: CreateScenarioDto) {
|
||||
return this.scenarioService.create(dto);
|
||||
}
|
||||
|
||||
@Post('import')
|
||||
@ApiOperation({ summary: 'Import a scenario from an export payload' })
|
||||
@ApiResponse({ status: 201, description: 'Scenario imported' })
|
||||
@Post("import")
|
||||
@ApiOperation({ summary: "Import a scenario from an export payload" })
|
||||
@ApiResponse({ status: 201, description: "Scenario imported" })
|
||||
importScenario(@Body() dto: ScenarioExportDto) {
|
||||
return this.scenarioService.importScenario(dto);
|
||||
}
|
||||
|
||||
@Get()
|
||||
@ApiOperation({ summary: 'List all scenarios (paginated)' })
|
||||
@ApiOperation({ summary: "List all scenarios (paginated)" })
|
||||
@ApiResponse({ status: 200 })
|
||||
findAll(@Query() query: PaginationQueryDto<ScenarioOrderBy>) {
|
||||
return this.scenarioService.findAll(query);
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
@ApiOperation({ summary: 'Get a scenario with its steps' })
|
||||
@Get(":id")
|
||||
@ApiOperation({ summary: "Get a scenario with its steps" })
|
||||
@ApiResponse({ status: 200 })
|
||||
@ApiResponse({ status: 404, description: 'Scenario not found' })
|
||||
findOne(@Param('id', ParseIntPipe) id: number) {
|
||||
@ApiResponse({ status: 404, description: "Scenario not found" })
|
||||
findOne(@Param("id", ParseIntPipe) id: number) {
|
||||
return this.scenarioService.findOne(id);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
@ApiOperation({ summary: 'Update a scenario' })
|
||||
@Patch(":id")
|
||||
@ApiOperation({ summary: "Update a scenario" })
|
||||
@ApiResponse({ status: 200 })
|
||||
@ApiResponse({ status: 404, description: 'Scenario not found' })
|
||||
update(@Param('id', ParseIntPipe) id: number, @Body() dto: UpdateScenarioDto) {
|
||||
@ApiResponse({ status: 404, description: "Scenario not found" })
|
||||
update(
|
||||
@Param("id", ParseIntPipe) id: number,
|
||||
@Body() dto: UpdateScenarioDto,
|
||||
) {
|
||||
return this.scenarioService.update(id, dto);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
@Delete(":id")
|
||||
@HttpCode(204)
|
||||
@ApiOperation({ summary: 'Delete a scenario and all its steps' })
|
||||
@ApiOperation({ summary: "Delete a scenario and all its steps" })
|
||||
@ApiResponse({ status: 204 })
|
||||
@ApiResponse({ status: 404, description: 'Scenario not found' })
|
||||
remove(@Param('id', ParseIntPipe) id: number) {
|
||||
@ApiResponse({ status: 404, description: "Scenario not found" })
|
||||
remove(@Param("id", ParseIntPipe) id: number) {
|
||||
return this.scenarioService.remove(id);
|
||||
}
|
||||
|
||||
@Get(':id/export')
|
||||
@ApiOperation({ summary: 'Export a scenario as a portable JSON payload' })
|
||||
@Get(":id/export")
|
||||
@ApiOperation({ summary: "Export a scenario as a portable JSON payload" })
|
||||
@ApiResponse({ status: 200 })
|
||||
@ApiResponse({ status: 404, description: 'Scenario not found' })
|
||||
exportScenario(@Param('id', ParseIntPipe) id: number) {
|
||||
@ApiResponse({ status: 404, description: "Scenario not found" })
|
||||
exportScenario(@Param("id", ParseIntPipe) id: number) {
|
||||
return this.scenarioService.exportScenario(id);
|
||||
}
|
||||
|
||||
// ── Steps ─────────────────────────────────────────────────────────────────
|
||||
|
||||
@Post(':id/steps')
|
||||
@ApiOperation({ summary: 'Add a step to a scenario' })
|
||||
@ApiResponse({ status: 201, description: 'Step created' })
|
||||
@ApiResponse({ status: 404, description: 'Scenario not found' })
|
||||
@Post(":id/steps")
|
||||
@ApiOperation({ summary: "Add a step to a scenario" })
|
||||
@ApiResponse({ status: 201, description: "Step created" })
|
||||
@ApiResponse({ status: 404, description: "Scenario not found" })
|
||||
createStep(
|
||||
@Param('id', ParseIntPipe) id: number,
|
||||
@Param("id", ParseIntPipe) id: number,
|
||||
@Body() dto: CreateScenarioStepDto,
|
||||
) {
|
||||
return this.scenarioService.createStep(id, dto);
|
||||
}
|
||||
|
||||
@Get(':id/steps/:stepId')
|
||||
@ApiOperation({ summary: 'Get a single step' })
|
||||
@Get(":id/steps/:stepId")
|
||||
@ApiOperation({ summary: "Get a single step" })
|
||||
@ApiResponse({ status: 200 })
|
||||
@ApiResponse({ status: 404, description: 'Scenario or step not found' })
|
||||
@ApiResponse({ status: 404, description: "Scenario or step not found" })
|
||||
findStep(
|
||||
@Param('id', ParseIntPipe) id: number,
|
||||
@Param('stepId', ParseIntPipe) stepId: number,
|
||||
@Param("id", ParseIntPipe) id: number,
|
||||
@Param("stepId", ParseIntPipe) stepId: number,
|
||||
) {
|
||||
return this.scenarioService.findStep(id, stepId);
|
||||
}
|
||||
|
||||
@Patch(':id/steps/:stepId')
|
||||
@ApiOperation({ summary: 'Update a step' })
|
||||
@Patch(":id/steps/:stepId")
|
||||
@ApiOperation({ summary: "Update a step" })
|
||||
@ApiResponse({ status: 200 })
|
||||
@ApiResponse({ status: 404, description: 'Scenario or step not found' })
|
||||
@ApiResponse({ status: 404, description: "Scenario or step not found" })
|
||||
updateStep(
|
||||
@Param('id', ParseIntPipe) id: number,
|
||||
@Param('stepId', ParseIntPipe) stepId: number,
|
||||
@Param("id", ParseIntPipe) id: number,
|
||||
@Param("stepId", ParseIntPipe) stepId: number,
|
||||
@Body() dto: UpdateScenarioStepDto,
|
||||
) {
|
||||
return this.scenarioService.updateStep(id, stepId, dto);
|
||||
}
|
||||
|
||||
@Delete(':id/steps/:stepId')
|
||||
@Delete(":id/steps/:stepId")
|
||||
@HttpCode(204)
|
||||
@ApiOperation({ summary: 'Delete a step' })
|
||||
@ApiOperation({ summary: "Delete a step" })
|
||||
@ApiResponse({ status: 204 })
|
||||
@ApiResponse({ status: 404, description: 'Scenario or step not found' })
|
||||
@ApiResponse({ status: 404, description: "Scenario or step not found" })
|
||||
removeStep(
|
||||
@Param('id', ParseIntPipe) id: number,
|
||||
@Param('stepId', ParseIntPipe) stepId: number,
|
||||
@Param("id", ParseIntPipe) id: number,
|
||||
@Param("stepId", ParseIntPipe) stepId: number,
|
||||
) {
|
||||
return this.scenarioService.removeStep(id, stepId);
|
||||
}
|
||||
|
||||
// ── Runs ──────────────────────────────────────────────────────────────────
|
||||
|
||||
@Get(':id/runs')
|
||||
@ApiOperation({ summary: 'List runs for a scenario (paginated, filterable by status)' })
|
||||
@Get(":id/runs")
|
||||
@ApiOperation({
|
||||
summary: "List runs for a scenario (paginated, filterable by status)",
|
||||
})
|
||||
@ApiResponse({ status: 200 })
|
||||
@ApiResponse({ status: 404, description: 'Scenario not found' })
|
||||
@ApiResponse({ status: 404, description: "Scenario not found" })
|
||||
findRuns(
|
||||
@Param('id', ParseIntPipe) id: number,
|
||||
@Param("id", ParseIntPipe) id: number,
|
||||
@Query() query: RunsQueryDto,
|
||||
) {
|
||||
return this.scenarioService.findRuns(id, query);
|
||||
}
|
||||
|
||||
@Post(':id/run')
|
||||
@ApiOperation({ summary: 'Create a new run for a scenario' })
|
||||
@ApiResponse({ status: 201, description: 'Run created with step runs' })
|
||||
@ApiResponse({ status: 404, description: 'Scenario not found' })
|
||||
createRun(@Param('id', ParseIntPipe) id: number) {
|
||||
@Post(":id/run")
|
||||
@ApiOperation({ summary: "Create a new run for a scenario" })
|
||||
@ApiResponse({ status: 201, description: "Run created with step runs" })
|
||||
@ApiResponse({ status: 404, description: "Scenario not found" })
|
||||
createRun(@Param("id", ParseIntPipe) id: number) {
|
||||
return this.scenarioService.createRun(id);
|
||||
}
|
||||
|
||||
@Get(":id/run/:runId")
|
||||
@ApiOperation({ summary: "Get a specific run with step runs and logs" })
|
||||
@ApiResponse({ status: 200 })
|
||||
@ApiResponse({ status: 404, description: "Scenario or run not found" })
|
||||
findRun(
|
||||
@Param("id", ParseIntPipe) id: number,
|
||||
@Param("runId", ParseIntPipe) runId: number,
|
||||
) {
|
||||
return this.scenarioService.findRun(id, runId);
|
||||
}
|
||||
|
||||
@Post(":id/run/:runId/wait")
|
||||
@HttpCode(200)
|
||||
@ApiOperation({
|
||||
summary:
|
||||
"Block until the run reaches pass or fail (max 5 min), then return run with logs",
|
||||
})
|
||||
@ApiResponse({ status: 200 })
|
||||
@ApiResponse({ status: 404, description: "Scenario or run not found" })
|
||||
waitForRun(
|
||||
@Param("id", ParseIntPipe) id: number,
|
||||
@Param("runId", ParseIntPipe) runId: number,
|
||||
) {
|
||||
return this.scenarioService.waitForRun(id, runId);
|
||||
}
|
||||
}
|
||||
@@ -5,10 +5,10 @@ import {
|
||||
CreateDateColumn,
|
||||
UpdateDateColumn,
|
||||
OneToMany,
|
||||
} from 'typeorm';
|
||||
import { ScenarioStepEntity } from './scenario-step.entity';
|
||||
} from "typeorm";
|
||||
import { ScenarioStepEntity } from "./scenario-step.entity";
|
||||
|
||||
@Entity('scenarios')
|
||||
@Entity("scenarios")
|
||||
export class ScenarioEntity {
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
|
||||
@@ -1,19 +1,26 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { ScenarioEntity } from './scenario.entity';
|
||||
import { ScenarioStepEntity } from './scenario-step.entity';
|
||||
import { ScenarioRunEntity } from './scenario-run.entity';
|
||||
import { ScenarioRunStepEntity } from './scenario-run-step.entity';
|
||||
import { ScenarioService } from './scenario.service';
|
||||
import { ScenarioController } from './scenario.controller';
|
||||
import { ScenarioSchedulerService } from './scenario-scheduler.service';
|
||||
import { AuthModule } from '../auth/auth.module';
|
||||
import { CodeExecutorModule } from '../code-executor/code-executor.module';
|
||||
import { SessionModule } from '../session/session.module';
|
||||
import { Module } from "@nestjs/common";
|
||||
import { TypeOrmModule } from "@nestjs/typeorm";
|
||||
import { ScenarioEntity } from "./scenario.entity";
|
||||
import { ScenarioStepEntity } from "./scenario-step.entity";
|
||||
import { ScenarioRunEntity } from "./scenario-run.entity";
|
||||
import { ScenarioRunStepEntity } from "./scenario-run-step.entity";
|
||||
import { ScenarioRunLogEntity } from "./scenario-run-log.entity";
|
||||
import { ScenarioService } from "./scenario.service";
|
||||
import { ScenarioController } from "./scenario.controller";
|
||||
import { ScenarioSchedulerService } from "./scenario-scheduler.service";
|
||||
import { AuthModule } from "../auth/auth.module";
|
||||
import { CodeExecutorModule } from "../code-executor/code-executor.module";
|
||||
import { SessionModule } from "../session/session.module";
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
TypeOrmModule.forFeature([ScenarioEntity, ScenarioStepEntity, ScenarioRunEntity, ScenarioRunStepEntity]),
|
||||
TypeOrmModule.forFeature([
|
||||
ScenarioEntity,
|
||||
ScenarioStepEntity,
|
||||
ScenarioRunEntity,
|
||||
ScenarioRunStepEntity,
|
||||
ScenarioRunLogEntity,
|
||||
]),
|
||||
AuthModule,
|
||||
CodeExecutorModule,
|
||||
SessionModule,
|
||||
|
||||
@@ -1,20 +1,24 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { ScenarioEntity } from './scenario.entity';
|
||||
import { ScenarioStepEntity } from './scenario-step.entity';
|
||||
import { ScenarioRunEntity } from './scenario-run.entity';
|
||||
import { ScenarioRunStepEntity } from './scenario-run-step.entity';
|
||||
import { CreateScenarioDto } from './dto/create-scenario.dto';
|
||||
import { UpdateScenarioDto } from './dto/update-scenario.dto';
|
||||
import { CreateScenarioStepDto } from './dto/create-scenario-step.dto';
|
||||
import { UpdateScenarioStepDto } from './dto/update-scenario-step.dto';
|
||||
import { PaginationQueryDto, PaginatedResult } from '../common/dto/pagination.dto';
|
||||
import { RunsQueryDto } from './dto/runs-query.dto';
|
||||
import { ScenarioExportDto } from './dto/scenario-export.dto';
|
||||
import { Injectable, NotFoundException } from "@nestjs/common";
|
||||
import { InjectRepository } from "@nestjs/typeorm";
|
||||
import { Repository } from "typeorm";
|
||||
import { ScenarioEntity } from "./scenario.entity";
|
||||
import { ScenarioStepEntity } from "./scenario-step.entity";
|
||||
import { ScenarioRunEntity } from "./scenario-run.entity";
|
||||
import { ScenarioRunStepEntity } from "./scenario-run-step.entity";
|
||||
import { ScenarioRunLogEntity } from "./scenario-run-log.entity";
|
||||
import { CreateScenarioDto } from "./dto/create-scenario.dto";
|
||||
import { UpdateScenarioDto } from "./dto/update-scenario.dto";
|
||||
import { CreateScenarioStepDto } from "./dto/create-scenario-step.dto";
|
||||
import { UpdateScenarioStepDto } from "./dto/update-scenario-step.dto";
|
||||
import {
|
||||
PaginationQueryDto,
|
||||
PaginatedResult,
|
||||
} from "../common/dto/pagination.dto";
|
||||
import { RunsQueryDto } from "./dto/runs-query.dto";
|
||||
import { ScenarioExportDto } from "./dto/scenario-export.dto";
|
||||
|
||||
export { PaginatedResult } from '../common/dto/pagination.dto';
|
||||
export type ScenarioOrderBy = 'id' | 'name' | 'createdAt' | 'updatedAt';
|
||||
export { PaginatedResult } from "../common/dto/pagination.dto";
|
||||
export type ScenarioOrderBy = "id" | "name" | "createdAt" | "updatedAt";
|
||||
|
||||
@Injectable()
|
||||
export class ScenarioService {
|
||||
@@ -27,6 +31,8 @@ export class ScenarioService {
|
||||
private readonly runRepo: Repository<ScenarioRunEntity>,
|
||||
@InjectRepository(ScenarioRunStepEntity)
|
||||
private readonly runStepRepo: Repository<ScenarioRunStepEntity>,
|
||||
@InjectRepository(ScenarioRunLogEntity)
|
||||
private readonly runLogRepo: Repository<ScenarioRunLogEntity>,
|
||||
) {}
|
||||
|
||||
// ── Scenarios ─────────────────────────────────────────────────────────────
|
||||
@@ -35,11 +41,13 @@ export class ScenarioService {
|
||||
return this.scenarioRepo.save(this.scenarioRepo.create(dto));
|
||||
}
|
||||
|
||||
async findAll(query: PaginationQueryDto<ScenarioOrderBy>): Promise<PaginatedResult<ScenarioEntity>> {
|
||||
async findAll(
|
||||
query: PaginationQueryDto<ScenarioOrderBy>,
|
||||
): Promise<PaginatedResult<ScenarioEntity>> {
|
||||
const page = query.page ?? 1;
|
||||
const limit = query.limit ?? 20;
|
||||
const orderBy = query.orderBy ?? 'id';
|
||||
const orderDir = query.orderDir ?? 'ASC';
|
||||
const orderBy = query.orderBy ?? "id";
|
||||
const orderDir = query.orderDir ?? "ASC";
|
||||
const [data, total] = await this.scenarioRepo.findAndCount({
|
||||
order: { [orderBy]: orderDir },
|
||||
skip: (page - 1) * limit,
|
||||
@@ -51,8 +59,8 @@ export class ScenarioService {
|
||||
async findOne(id: number): Promise<ScenarioEntity> {
|
||||
const scenario = await this.scenarioRepo.findOne({
|
||||
where: { id },
|
||||
relations: ['steps'],
|
||||
order: { steps: { order: 'ASC' } },
|
||||
relations: ["steps"],
|
||||
order: { steps: { order: "ASC" } },
|
||||
});
|
||||
if (!scenario) throw new NotFoundException(`Scenario ${id} not found`);
|
||||
return scenario;
|
||||
@@ -71,7 +79,10 @@ export class ScenarioService {
|
||||
|
||||
// ── Steps ─────────────────────────────────────────────────────────────────
|
||||
|
||||
async createStep(scenarioId: number, dto: CreateScenarioStepDto): Promise<ScenarioStepEntity> {
|
||||
async createStep(
|
||||
scenarioId: number,
|
||||
dto: CreateScenarioStepDto,
|
||||
): Promise<ScenarioStepEntity> {
|
||||
await this.findOne(scenarioId);
|
||||
return this.stepRepo.save(
|
||||
this.stepRepo.create({
|
||||
@@ -83,13 +94,23 @@ export class ScenarioService {
|
||||
);
|
||||
}
|
||||
|
||||
async findStep(scenarioId: number, stepId: number): Promise<ScenarioStepEntity> {
|
||||
async findStep(
|
||||
scenarioId: number,
|
||||
stepId: number,
|
||||
): Promise<ScenarioStepEntity> {
|
||||
const step = await this.stepRepo.findOneBy({ id: stepId, scenarioId });
|
||||
if (!step) throw new NotFoundException(`Step ${stepId} not found in scenario ${scenarioId}`);
|
||||
if (!step)
|
||||
throw new NotFoundException(
|
||||
`Step ${stepId} not found in scenario ${scenarioId}`,
|
||||
);
|
||||
return step;
|
||||
}
|
||||
|
||||
async updateStep(scenarioId: number, stepId: number, dto: UpdateScenarioStepDto): Promise<ScenarioStepEntity> {
|
||||
async updateStep(
|
||||
scenarioId: number,
|
||||
stepId: number,
|
||||
dto: UpdateScenarioStepDto,
|
||||
): Promise<ScenarioStepEntity> {
|
||||
const step = await this.findStep(scenarioId, stepId);
|
||||
Object.assign(step, dto);
|
||||
return this.stepRepo.save(step);
|
||||
@@ -100,26 +121,71 @@ export class ScenarioService {
|
||||
await this.stepRepo.delete(stepId);
|
||||
}
|
||||
|
||||
async findRuns(scenarioId: number, query: RunsQueryDto): Promise<PaginatedResult<ScenarioRunEntity>> {
|
||||
async findRuns(
|
||||
scenarioId: number,
|
||||
query: RunsQueryDto,
|
||||
): Promise<PaginatedResult<ScenarioRunEntity>> {
|
||||
await this.findOne(scenarioId); // 404 guard
|
||||
const page = query.page ?? 1;
|
||||
const limit = query.limit ?? 20;
|
||||
const where: Record<string, unknown> = { scenarioId };
|
||||
if (query.status) where['status'] = query.status;
|
||||
if (query.status) where["status"] = query.status;
|
||||
const [data, total] = await this.runRepo.findAndCount({
|
||||
where,
|
||||
relations: ['stepRuns'],
|
||||
order: { id: 'DESC', stepRuns: { order: 'ASC' } },
|
||||
relations: ["stepRuns"],
|
||||
order: { id: "DESC", stepRuns: { order: "ASC" } },
|
||||
skip: (page - 1) * limit,
|
||||
take: limit,
|
||||
});
|
||||
return { data, total, page, limit };
|
||||
}
|
||||
|
||||
async createRun(scenarioId: number): Promise<ScenarioRunEntity> { const scenario = await this.findOne(scenarioId);
|
||||
async findRun(
|
||||
scenarioId: number,
|
||||
runId: number,
|
||||
): Promise<ScenarioRunEntity & { logs: ScenarioRunLogEntity[] }> {
|
||||
await this.findOne(scenarioId); // 404 guard
|
||||
const run = await this.runRepo.findOne({
|
||||
where: { id: runId, scenarioId },
|
||||
relations: ["stepRuns", "stepRuns.scenarioStep"],
|
||||
order: { stepRuns: { order: "ASC" } },
|
||||
});
|
||||
if (!run)
|
||||
throw new NotFoundException(
|
||||
`Run ${runId} not found in scenario ${scenarioId}`,
|
||||
);
|
||||
const logs = await this.runLogRepo.find({
|
||||
where: { runId },
|
||||
order: { createdAt: "ASC" },
|
||||
});
|
||||
return Object.assign(run, { logs });
|
||||
}
|
||||
|
||||
async waitForRun(
|
||||
scenarioId: number,
|
||||
runId: number,
|
||||
timeoutMs = 300_000,
|
||||
): Promise<ScenarioRunEntity & { logs: ScenarioRunLogEntity[] }> {
|
||||
const deadline = Date.now() + timeoutMs;
|
||||
while (Date.now() < deadline) {
|
||||
const run = await this.runRepo.findOneBy({ id: runId, scenarioId });
|
||||
if (!run)
|
||||
throw new NotFoundException(
|
||||
`Run ${runId} not found in scenario ${scenarioId}`,
|
||||
);
|
||||
if (run.status === "pass" || run.status === "fail") {
|
||||
return this.findRun(scenarioId, runId);
|
||||
}
|
||||
await new Promise<void>((resolve) => setTimeout(resolve, 500));
|
||||
}
|
||||
return this.findRun(scenarioId, runId);
|
||||
}
|
||||
|
||||
async createRun(scenarioId: number): Promise<ScenarioRunEntity> {
|
||||
const scenario = await this.findOne(scenarioId);
|
||||
|
||||
const run = await this.runRepo.save(
|
||||
this.runRepo.create({ scenarioId, status: 'pending' }),
|
||||
this.runRepo.create({ scenarioId, status: "pending" }),
|
||||
);
|
||||
|
||||
const stepRuns = scenario.steps.map((step, index) =>
|
||||
@@ -127,7 +193,7 @@ export class ScenarioService {
|
||||
runId: run.id,
|
||||
scenarioStepId: step.id,
|
||||
order: step.order,
|
||||
status: index === 0 ? 'pending' : 'waiting',
|
||||
status: index === 0 ? "pending" : "waiting",
|
||||
description: null,
|
||||
}),
|
||||
);
|
||||
@@ -136,8 +202,8 @@ export class ScenarioService {
|
||||
|
||||
return this.runRepo.findOne({
|
||||
where: { id: run.id },
|
||||
relations: ['stepRuns'],
|
||||
order: { stepRuns: { order: 'ASC' } },
|
||||
relations: ["stepRuns"],
|
||||
order: { stepRuns: { order: "ASC" } },
|
||||
}) as Promise<ScenarioRunEntity>;
|
||||
}
|
||||
|
||||
@@ -177,4 +243,3 @@ export class ScenarioService {
|
||||
return this.findOne(scenario.id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,28 +1,36 @@
|
||||
import { Controller, Delete, Get, NotFoundException, Param, ParseIntPipe, Query } from '@nestjs/common';
|
||||
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
|
||||
import { SessionService } from './session.service';
|
||||
import { PaginationQueryDto } from '../common/dto/pagination.dto';
|
||||
import { SessionOrderBy } from './session.service';
|
||||
import {
|
||||
Controller,
|
||||
Delete,
|
||||
Get,
|
||||
NotFoundException,
|
||||
Param,
|
||||
ParseIntPipe,
|
||||
Query,
|
||||
} from "@nestjs/common";
|
||||
import { ApiOperation, ApiResponse, ApiTags } from "@nestjs/swagger";
|
||||
import { SessionService } from "./session.service";
|
||||
import { PaginationQueryDto } from "../common/dto/pagination.dto";
|
||||
import { SessionOrderBy } from "./session.service";
|
||||
|
||||
@ApiTags('sessions')
|
||||
@Controller('sessions')
|
||||
@ApiTags("sessions")
|
||||
@Controller("sessions")
|
||||
export class SessionController {
|
||||
constructor(private readonly sessionService: SessionService) {}
|
||||
|
||||
@Get()
|
||||
@ApiOperation({ summary: 'List all stored sessions (paginated)' })
|
||||
@ApiResponse({ status: 200, description: 'Paginated sessions' })
|
||||
@ApiOperation({ summary: "List all stored sessions (paginated)" })
|
||||
@ApiResponse({ status: 200, description: "Paginated sessions" })
|
||||
findAll(@Query() query: PaginationQueryDto<SessionOrderBy>) {
|
||||
return this.sessionService.findAll(query);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
@ApiOperation({ summary: 'Delete a session by ID' })
|
||||
@ApiResponse({ status: 200, description: 'Session deleted' })
|
||||
@ApiResponse({ status: 404, description: 'Session not found' })
|
||||
async remove(@Param('id', ParseIntPipe) id: number): Promise<void> {
|
||||
@Delete(":id")
|
||||
@ApiOperation({ summary: "Delete a session by ID" })
|
||||
@ApiResponse({ status: 200, description: "Session deleted" })
|
||||
@ApiResponse({ status: 404, description: "Session not found" })
|
||||
async remove(@Param("id", ParseIntPipe) id: number): Promise<void> {
|
||||
const sessions = await this.sessionService.findAll();
|
||||
if (!sessions.data.find(s => s.id === id)) {
|
||||
if (!sessions.data.find((s) => s.id === id)) {
|
||||
throw new NotFoundException(`Session ${id} not found`);
|
||||
}
|
||||
await this.sessionService.remove(id);
|
||||
|
||||
@@ -4,9 +4,9 @@ import {
|
||||
Column,
|
||||
CreateDateColumn,
|
||||
UpdateDateColumn,
|
||||
} from 'typeorm';
|
||||
} from "typeorm";
|
||||
|
||||
@Entity('sessions')
|
||||
@Entity("sessions")
|
||||
export class SessionEntity {
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
@@ -14,13 +14,13 @@ export class SessionEntity {
|
||||
@Column({ unique: true })
|
||||
sessionName: string;
|
||||
|
||||
@Column('text')
|
||||
@Column("text")
|
||||
token: string;
|
||||
|
||||
@Column('text')
|
||||
@Column("text")
|
||||
cookies: string; // JSON-serialised Cookie[] from Playwright
|
||||
|
||||
@Column('text', { default: '{}' })
|
||||
@Column("text", { default: "{}" })
|
||||
localStorage: string; // JSON-serialised Record<string, string> from Playwright
|
||||
|
||||
@CreateDateColumn()
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { SessionEntity } from './session.entity';
|
||||
import { SessionService } from './session.service';
|
||||
import { SessionController } from './session.controller';
|
||||
import { Module } from "@nestjs/common";
|
||||
import { TypeOrmModule } from "@nestjs/typeorm";
|
||||
import { SessionEntity } from "./session.entity";
|
||||
import { SessionService } from "./session.service";
|
||||
import { SessionController } from "./session.controller";
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([SessionEntity])],
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { SessionEntity } from './session.entity';
|
||||
import type { Cookie } from 'playwright';
|
||||
import { PaginationQueryDto, PaginatedResult } from '../common/dto/pagination.dto';
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { InjectRepository } from "@nestjs/typeorm";
|
||||
import { Repository } from "typeorm";
|
||||
import { SessionEntity } from "./session.entity";
|
||||
import type { Cookie } from "playwright";
|
||||
import {
|
||||
PaginationQueryDto,
|
||||
PaginatedResult,
|
||||
} from "../common/dto/pagination.dto";
|
||||
|
||||
export type SessionOrderBy = 'id' | 'sessionName' | 'createdAt' | 'updatedAt';
|
||||
export type SessionOrderBy = "id" | "sessionName" | "createdAt" | "updatedAt";
|
||||
|
||||
@Injectable()
|
||||
export class SessionService {
|
||||
@@ -43,13 +46,19 @@ export class SessionService {
|
||||
return this.repo.findOneBy({ sessionName });
|
||||
}
|
||||
|
||||
async findAll(query: PaginationQueryDto<SessionOrderBy> = {}): Promise<PaginatedResult<Pick<SessionEntity, 'id' | 'sessionName' | 'createdAt' | 'updatedAt'>>> {
|
||||
async findAll(
|
||||
query: PaginationQueryDto<SessionOrderBy> = {},
|
||||
): Promise<
|
||||
PaginatedResult<
|
||||
Pick<SessionEntity, "id" | "sessionName" | "createdAt" | "updatedAt">
|
||||
>
|
||||
> {
|
||||
const page = query.page ?? 1;
|
||||
const limit = query.limit ?? 20;
|
||||
const orderBy = query.orderBy ?? 'id';
|
||||
const orderDir = query.orderDir ?? 'ASC';
|
||||
const orderBy = query.orderBy ?? "id";
|
||||
const orderDir = query.orderDir ?? "ASC";
|
||||
const [data, total] = await this.repo.findAndCount({
|
||||
select: ['id', 'sessionName', 'createdAt', 'updatedAt'],
|
||||
select: ["id", "sessionName", "createdAt", "updatedAt"],
|
||||
order: { [orderBy]: orderDir },
|
||||
skip: (page - 1) * limit,
|
||||
take: limit,
|
||||
|
||||
Reference in new issue
Block a user